Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * TCP over IPv6 | |
1ab1457c | 3 | * Linux INET6 implementation |
1da177e4 LT |
4 | * |
5 | * Authors: | |
1ab1457c | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
1da177e4 | 7 | * |
1ab1457c | 8 | * Based on: |
1da177e4 LT |
9 | * linux/net/ipv4/tcp.c |
10 | * linux/net/ipv4/tcp_input.c | |
11 | * linux/net/ipv4/tcp_output.c | |
12 | * | |
13 | * Fixes: | |
14 | * Hideaki YOSHIFUJI : sin6_scope_id support | |
15 | * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which | |
16 | * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind | |
17 | * a single port at the same time. | |
18 | * YOSHIFUJI Hideaki @USAGI: convert /proc/net/tcp6 to seq_file. | |
19 | * | |
20 | * This program is free software; you can redistribute it and/or | |
21 | * modify it under the terms of the GNU General Public License | |
22 | * as published by the Free Software Foundation; either version | |
23 | * 2 of the License, or (at your option) any later version. | |
24 | */ | |
25 | ||
eb4dea58 | 26 | #include <linux/bottom_half.h> |
1da177e4 | 27 | #include <linux/module.h> |
1da177e4 LT |
28 | #include <linux/errno.h> |
29 | #include <linux/types.h> | |
30 | #include <linux/socket.h> | |
31 | #include <linux/sockios.h> | |
32 | #include <linux/net.h> | |
33 | #include <linux/jiffies.h> | |
34 | #include <linux/in.h> | |
35 | #include <linux/in6.h> | |
36 | #include <linux/netdevice.h> | |
37 | #include <linux/init.h> | |
38 | #include <linux/jhash.h> | |
39 | #include <linux/ipsec.h> | |
40 | #include <linux/times.h> | |
5a0e3ad6 | 41 | #include <linux/slab.h> |
1da177e4 LT |
42 | |
43 | #include <linux/ipv6.h> | |
44 | #include <linux/icmpv6.h> | |
45 | #include <linux/random.h> | |
46 | ||
47 | #include <net/tcp.h> | |
48 | #include <net/ndisc.h> | |
5324a040 | 49 | #include <net/inet6_hashtables.h> |
8129765a | 50 | #include <net/inet6_connection_sock.h> |
1da177e4 LT |
51 | #include <net/ipv6.h> |
52 | #include <net/transp_v6.h> | |
53 | #include <net/addrconf.h> | |
54 | #include <net/ip6_route.h> | |
55 | #include <net/ip6_checksum.h> | |
56 | #include <net/inet_ecn.h> | |
57 | #include <net/protocol.h> | |
58 | #include <net/xfrm.h> | |
1da177e4 LT |
59 | #include <net/snmp.h> |
60 | #include <net/dsfield.h> | |
6d6ee43e | 61 | #include <net/timewait_sock.h> |
18134bed | 62 | #include <net/netdma.h> |
3d58b5fa | 63 | #include <net/inet_common.h> |
6e5714ea | 64 | #include <net/secure_seq.h> |
d1a4c0b3 | 65 | #include <net/tcp_memcontrol.h> |
1da177e4 LT |
66 | |
67 | #include <asm/uaccess.h> | |
68 | ||
69 | #include <linux/proc_fs.h> | |
70 | #include <linux/seq_file.h> | |
71 | ||
cfb6eeb4 YH |
72 | #include <linux/crypto.h> |
73 | #include <linux/scatterlist.h> | |
74 | ||
cfb6eeb4 | 75 | static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb); |
6edafaaf GJ |
76 | static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb, |
77 | struct request_sock *req); | |
1da177e4 LT |
78 | |
79 | static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb); | |
8ad50d96 | 80 | static void __tcp_v6_send_check(struct sk_buff *skb, |
b71d1d42 ED |
81 | const struct in6_addr *saddr, |
82 | const struct in6_addr *daddr); | |
1da177e4 | 83 | |
3b401a81 SH |
84 | static const struct inet_connection_sock_af_ops ipv6_mapped; |
85 | static const struct inet_connection_sock_af_ops ipv6_specific; | |
a928630a | 86 | #ifdef CONFIG_TCP_MD5SIG |
b2e4b3de SH |
87 | static const struct tcp_sock_af_ops tcp_sock_ipv6_specific; |
88 | static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific; | |
9501f972 YH |
89 | #else |
90 | static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk, | |
b71d1d42 | 91 | const struct in6_addr *addr) |
9501f972 YH |
92 | { |
93 | return NULL; | |
94 | } | |
a928630a | 95 | #endif |
1da177e4 | 96 | |
1da177e4 LT |
97 | static void tcp_v6_hash(struct sock *sk) |
98 | { | |
99 | if (sk->sk_state != TCP_CLOSE) { | |
8292a17a | 100 | if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) { |
1da177e4 LT |
101 | tcp_prot.hash(sk); |
102 | return; | |
103 | } | |
104 | local_bh_disable(); | |
9327f705 | 105 | __inet6_hash(sk, NULL); |
1da177e4 LT |
106 | local_bh_enable(); |
107 | } | |
108 | } | |
109 | ||
684f2176 | 110 | static __inline__ __sum16 tcp_v6_check(int len, |
b71d1d42 ED |
111 | const struct in6_addr *saddr, |
112 | const struct in6_addr *daddr, | |
868c86bc | 113 | __wsum base) |
1da177e4 LT |
114 | { |
115 | return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base); | |
116 | } | |
117 | ||
cf533ea5 | 118 | static __u32 tcp_v6_init_sequence(const struct sk_buff *skb) |
1da177e4 | 119 | { |
0660e03f ACM |
120 | return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32, |
121 | ipv6_hdr(skb)->saddr.s6_addr32, | |
aa8223c7 ACM |
122 | tcp_hdr(skb)->dest, |
123 | tcp_hdr(skb)->source); | |
1da177e4 LT |
124 | } |
125 | ||
1ab1457c | 126 | static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, |
1da177e4 LT |
127 | int addr_len) |
128 | { | |
129 | struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr; | |
1ab1457c | 130 | struct inet_sock *inet = inet_sk(sk); |
d83d8461 | 131 | struct inet_connection_sock *icsk = inet_csk(sk); |
1da177e4 LT |
132 | struct ipv6_pinfo *np = inet6_sk(sk); |
133 | struct tcp_sock *tp = tcp_sk(sk); | |
20c59de2 | 134 | struct in6_addr *saddr = NULL, *final_p, final; |
493f377d | 135 | struct rt6_info *rt; |
4c9483b2 | 136 | struct flowi6 fl6; |
1da177e4 LT |
137 | struct dst_entry *dst; |
138 | int addr_type; | |
139 | int err; | |
140 | ||
1ab1457c | 141 | if (addr_len < SIN6_LEN_RFC2133) |
1da177e4 LT |
142 | return -EINVAL; |
143 | ||
1ab1457c | 144 | if (usin->sin6_family != AF_INET6) |
a02cec21 | 145 | return -EAFNOSUPPORT; |
1da177e4 | 146 | |
4c9483b2 | 147 | memset(&fl6, 0, sizeof(fl6)); |
1da177e4 LT |
148 | |
149 | if (np->sndflow) { | |
4c9483b2 DM |
150 | fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK; |
151 | IP6_ECN_flow_init(fl6.flowlabel); | |
152 | if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) { | |
1da177e4 | 153 | struct ip6_flowlabel *flowlabel; |
4c9483b2 | 154 | flowlabel = fl6_sock_lookup(sk, fl6.flowlabel); |
1da177e4 LT |
155 | if (flowlabel == NULL) |
156 | return -EINVAL; | |
4e3fd7a0 | 157 | usin->sin6_addr = flowlabel->dst; |
1da177e4 LT |
158 | fl6_sock_release(flowlabel); |
159 | } | |
160 | } | |
161 | ||
162 | /* | |
1ab1457c YH |
163 | * connect() to INADDR_ANY means loopback (BSD'ism). |
164 | */ | |
165 | ||
166 | if(ipv6_addr_any(&usin->sin6_addr)) | |
167 | usin->sin6_addr.s6_addr[15] = 0x1; | |
1da177e4 LT |
168 | |
169 | addr_type = ipv6_addr_type(&usin->sin6_addr); | |
170 | ||
171 | if(addr_type & IPV6_ADDR_MULTICAST) | |
172 | return -ENETUNREACH; | |
173 | ||
174 | if (addr_type&IPV6_ADDR_LINKLOCAL) { | |
175 | if (addr_len >= sizeof(struct sockaddr_in6) && | |
176 | usin->sin6_scope_id) { | |
177 | /* If interface is set while binding, indices | |
178 | * must coincide. | |
179 | */ | |
180 | if (sk->sk_bound_dev_if && | |
181 | sk->sk_bound_dev_if != usin->sin6_scope_id) | |
182 | return -EINVAL; | |
183 | ||
184 | sk->sk_bound_dev_if = usin->sin6_scope_id; | |
185 | } | |
186 | ||
187 | /* Connect to link-local address requires an interface */ | |
188 | if (!sk->sk_bound_dev_if) | |
189 | return -EINVAL; | |
190 | } | |
191 | ||
192 | if (tp->rx_opt.ts_recent_stamp && | |
193 | !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) { | |
194 | tp->rx_opt.ts_recent = 0; | |
195 | tp->rx_opt.ts_recent_stamp = 0; | |
196 | tp->write_seq = 0; | |
197 | } | |
198 | ||
4e3fd7a0 | 199 | np->daddr = usin->sin6_addr; |
4c9483b2 | 200 | np->flow_label = fl6.flowlabel; |
1da177e4 LT |
201 | |
202 | /* | |
203 | * TCP over IPv4 | |
204 | */ | |
205 | ||
206 | if (addr_type == IPV6_ADDR_MAPPED) { | |
d83d8461 | 207 | u32 exthdrlen = icsk->icsk_ext_hdr_len; |
1da177e4 LT |
208 | struct sockaddr_in sin; |
209 | ||
210 | SOCK_DEBUG(sk, "connect: ipv4 mapped\n"); | |
211 | ||
212 | if (__ipv6_only_sock(sk)) | |
213 | return -ENETUNREACH; | |
214 | ||
215 | sin.sin_family = AF_INET; | |
216 | sin.sin_port = usin->sin6_port; | |
217 | sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3]; | |
218 | ||
d83d8461 | 219 | icsk->icsk_af_ops = &ipv6_mapped; |
1da177e4 | 220 | sk->sk_backlog_rcv = tcp_v4_do_rcv; |
cfb6eeb4 YH |
221 | #ifdef CONFIG_TCP_MD5SIG |
222 | tp->af_specific = &tcp_sock_ipv6_mapped_specific; | |
223 | #endif | |
1da177e4 LT |
224 | |
225 | err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin)); | |
226 | ||
227 | if (err) { | |
d83d8461 ACM |
228 | icsk->icsk_ext_hdr_len = exthdrlen; |
229 | icsk->icsk_af_ops = &ipv6_specific; | |
1da177e4 | 230 | sk->sk_backlog_rcv = tcp_v6_do_rcv; |
cfb6eeb4 YH |
231 | #ifdef CONFIG_TCP_MD5SIG |
232 | tp->af_specific = &tcp_sock_ipv6_specific; | |
233 | #endif | |
1da177e4 LT |
234 | goto failure; |
235 | } else { | |
c720c7e8 ED |
236 | ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr); |
237 | ipv6_addr_set_v4mapped(inet->inet_rcv_saddr, | |
238 | &np->rcv_saddr); | |
1da177e4 LT |
239 | } |
240 | ||
241 | return err; | |
242 | } | |
243 | ||
244 | if (!ipv6_addr_any(&np->rcv_saddr)) | |
245 | saddr = &np->rcv_saddr; | |
246 | ||
4c9483b2 | 247 | fl6.flowi6_proto = IPPROTO_TCP; |
4e3fd7a0 AD |
248 | fl6.daddr = np->daddr; |
249 | fl6.saddr = saddr ? *saddr : np->saddr; | |
4c9483b2 DM |
250 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
251 | fl6.flowi6_mark = sk->sk_mark; | |
1958b856 DM |
252 | fl6.fl6_dport = usin->sin6_port; |
253 | fl6.fl6_sport = inet->inet_sport; | |
1da177e4 | 254 | |
4c9483b2 | 255 | final_p = fl6_update_dst(&fl6, np->opt, &final); |
1da177e4 | 256 | |
4c9483b2 | 257 | security_sk_classify_flow(sk, flowi6_to_flowi(&fl6)); |
beb8d13b | 258 | |
4c9483b2 | 259 | dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true); |
68d0c6d3 DM |
260 | if (IS_ERR(dst)) { |
261 | err = PTR_ERR(dst); | |
1da177e4 | 262 | goto failure; |
14e50e57 | 263 | } |
1da177e4 LT |
264 | |
265 | if (saddr == NULL) { | |
4c9483b2 | 266 | saddr = &fl6.saddr; |
4e3fd7a0 | 267 | np->rcv_saddr = *saddr; |
1da177e4 LT |
268 | } |
269 | ||
270 | /* set the source address */ | |
4e3fd7a0 | 271 | np->saddr = *saddr; |
c720c7e8 | 272 | inet->inet_rcv_saddr = LOOPBACK4_IPV6; |
1da177e4 | 273 | |
f83ef8c0 | 274 | sk->sk_gso_type = SKB_GSO_TCPV6; |
8e1ef0a9 | 275 | __ip6_dst_store(sk, dst, NULL, NULL); |
1da177e4 | 276 | |
493f377d DM |
277 | rt = (struct rt6_info *) dst; |
278 | if (tcp_death_row.sysctl_tw_recycle && | |
279 | !tp->rx_opt.ts_recent_stamp && | |
81166dd6 DM |
280 | ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) |
281 | tcp_fetch_timewait_stamp(sk, dst); | |
493f377d | 282 | |
d83d8461 | 283 | icsk->icsk_ext_hdr_len = 0; |
1da177e4 | 284 | if (np->opt) |
d83d8461 ACM |
285 | icsk->icsk_ext_hdr_len = (np->opt->opt_flen + |
286 | np->opt->opt_nflen); | |
1da177e4 LT |
287 | |
288 | tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr); | |
289 | ||
c720c7e8 | 290 | inet->inet_dport = usin->sin6_port; |
1da177e4 LT |
291 | |
292 | tcp_set_state(sk, TCP_SYN_SENT); | |
d8313f5c | 293 | err = inet6_hash_connect(&tcp_death_row, sk); |
1da177e4 LT |
294 | if (err) |
295 | goto late_failure; | |
296 | ||
297 | if (!tp->write_seq) | |
298 | tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32, | |
299 | np->daddr.s6_addr32, | |
c720c7e8 ED |
300 | inet->inet_sport, |
301 | inet->inet_dport); | |
1da177e4 LT |
302 | |
303 | err = tcp_connect(sk); | |
304 | if (err) | |
305 | goto late_failure; | |
306 | ||
307 | return 0; | |
308 | ||
309 | late_failure: | |
310 | tcp_set_state(sk, TCP_CLOSE); | |
311 | __sk_dst_reset(sk); | |
312 | failure: | |
c720c7e8 | 313 | inet->inet_dport = 0; |
1da177e4 LT |
314 | sk->sk_route_caps = 0; |
315 | return err; | |
316 | } | |
317 | ||
318 | static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, | |
d5fdd6ba | 319 | u8 type, u8 code, int offset, __be32 info) |
1da177e4 | 320 | { |
b71d1d42 | 321 | const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data; |
505cbfc5 | 322 | const struct tcphdr *th = (struct tcphdr *)(skb->data+offset); |
1da177e4 LT |
323 | struct ipv6_pinfo *np; |
324 | struct sock *sk; | |
325 | int err; | |
1ab1457c | 326 | struct tcp_sock *tp; |
1da177e4 | 327 | __u32 seq; |
ca12a1a4 | 328 | struct net *net = dev_net(skb->dev); |
1da177e4 | 329 | |
ca12a1a4 | 330 | sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr, |
d86e0dac | 331 | th->dest, &hdr->saddr, th->source, skb->dev->ifindex); |
1da177e4 LT |
332 | |
333 | if (sk == NULL) { | |
e41b5368 DL |
334 | ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev), |
335 | ICMP6_MIB_INERRORS); | |
1da177e4 LT |
336 | return; |
337 | } | |
338 | ||
339 | if (sk->sk_state == TCP_TIME_WAIT) { | |
9469c7b4 | 340 | inet_twsk_put(inet_twsk(sk)); |
1da177e4 LT |
341 | return; |
342 | } | |
343 | ||
344 | bh_lock_sock(sk); | |
345 | if (sock_owned_by_user(sk)) | |
de0744af | 346 | NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS); |
1da177e4 LT |
347 | |
348 | if (sk->sk_state == TCP_CLOSE) | |
349 | goto out; | |
350 | ||
e802af9c SH |
351 | if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) { |
352 | NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP); | |
353 | goto out; | |
354 | } | |
355 | ||
1da177e4 | 356 | tp = tcp_sk(sk); |
1ab1457c | 357 | seq = ntohl(th->seq); |
1da177e4 LT |
358 | if (sk->sk_state != TCP_LISTEN && |
359 | !between(seq, tp->snd_una, tp->snd_nxt)) { | |
de0744af | 360 | NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); |
1da177e4 LT |
361 | goto out; |
362 | } | |
363 | ||
364 | np = inet6_sk(sk); | |
365 | ||
ec18d9a2 DM |
366 | if (type == NDISC_REDIRECT) { |
367 | struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie); | |
368 | ||
369 | if (dst && dst->ops->redirect) | |
370 | dst->ops->redirect(dst,skb); | |
371 | } | |
372 | ||
1da177e4 | 373 | if (type == ICMPV6_PKT_TOOBIG) { |
68d0c6d3 | 374 | struct dst_entry *dst; |
1da177e4 LT |
375 | |
376 | if (sock_owned_by_user(sk)) | |
377 | goto out; | |
378 | if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) | |
379 | goto out; | |
380 | ||
381 | /* icmp should have updated the destination cache entry */ | |
382 | dst = __sk_dst_check(sk, np->dst_cookie); | |
383 | ||
384 | if (dst == NULL) { | |
385 | struct inet_sock *inet = inet_sk(sk); | |
4c9483b2 | 386 | struct flowi6 fl6; |
1da177e4 LT |
387 | |
388 | /* BUGGG_FUTURE: Again, it is not clear how | |
389 | to handle rthdr case. Ignore this complexity | |
390 | for now. | |
391 | */ | |
4c9483b2 DM |
392 | memset(&fl6, 0, sizeof(fl6)); |
393 | fl6.flowi6_proto = IPPROTO_TCP; | |
4e3fd7a0 AD |
394 | fl6.daddr = np->daddr; |
395 | fl6.saddr = np->saddr; | |
4c9483b2 DM |
396 | fl6.flowi6_oif = sk->sk_bound_dev_if; |
397 | fl6.flowi6_mark = sk->sk_mark; | |
1958b856 DM |
398 | fl6.fl6_dport = inet->inet_dport; |
399 | fl6.fl6_sport = inet->inet_sport; | |
4c9483b2 DM |
400 | security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); |
401 | ||
402 | dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false); | |
68d0c6d3 DM |
403 | if (IS_ERR(dst)) { |
404 | sk->sk_err_soft = -PTR_ERR(dst); | |
1da177e4 LT |
405 | goto out; |
406 | } | |
407 | ||
408 | } else | |
409 | dst_hold(dst); | |
410 | ||
81aded24 DM |
411 | dst->ops->update_pmtu(dst, ntohl(info)); |
412 | ||
d83d8461 | 413 | if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) { |
1da177e4 LT |
414 | tcp_sync_mss(sk, dst_mtu(dst)); |
415 | tcp_simple_retransmit(sk); | |
416 | } /* else let the usual retransmit timer handle it */ | |
417 | dst_release(dst); | |
418 | goto out; | |
419 | } | |
420 | ||
421 | icmpv6_err_convert(type, code, &err); | |
422 | ||
60236fdd | 423 | /* Might be for an request_sock */ |
1da177e4 | 424 | switch (sk->sk_state) { |
60236fdd | 425 | struct request_sock *req, **prev; |
1da177e4 LT |
426 | case TCP_LISTEN: |
427 | if (sock_owned_by_user(sk)) | |
428 | goto out; | |
429 | ||
8129765a ACM |
430 | req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr, |
431 | &hdr->saddr, inet6_iif(skb)); | |
1da177e4 LT |
432 | if (!req) |
433 | goto out; | |
434 | ||
435 | /* ICMPs are not backlogged, hence we cannot get | |
436 | * an established socket here. | |
437 | */ | |
547b792c | 438 | WARN_ON(req->sk != NULL); |
1da177e4 | 439 | |
2e6599cb | 440 | if (seq != tcp_rsk(req)->snt_isn) { |
de0744af | 441 | NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS); |
1da177e4 LT |
442 | goto out; |
443 | } | |
444 | ||
463c84b9 | 445 | inet_csk_reqsk_queue_drop(sk, req, prev); |
1da177e4 LT |
446 | goto out; |
447 | ||
448 | case TCP_SYN_SENT: | |
449 | case TCP_SYN_RECV: /* Cannot happen. | |
1ab1457c | 450 | It can, it SYNs are crossed. --ANK */ |
1da177e4 | 451 | if (!sock_owned_by_user(sk)) { |
1da177e4 LT |
452 | sk->sk_err = err; |
453 | sk->sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */ | |
454 | ||
455 | tcp_done(sk); | |
456 | } else | |
457 | sk->sk_err_soft = err; | |
458 | goto out; | |
459 | } | |
460 | ||
461 | if (!sock_owned_by_user(sk) && np->recverr) { | |
462 | sk->sk_err = err; | |
463 | sk->sk_error_report(sk); | |
464 | } else | |
465 | sk->sk_err_soft = err; | |
466 | ||
467 | out: | |
468 | bh_unlock_sock(sk); | |
469 | sock_put(sk); | |
470 | } | |
471 | ||
472 | ||
9f10d3f6 NC |
473 | static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst, |
474 | struct flowi6 *fl6, | |
3840a06e | 475 | struct request_sock *req, |
fff32699 ED |
476 | struct request_values *rvp, |
477 | u16 queue_mapping) | |
1da177e4 | 478 | { |
ca304b61 | 479 | struct inet6_request_sock *treq = inet6_rsk(req); |
1da177e4 LT |
480 | struct ipv6_pinfo *np = inet6_sk(sk); |
481 | struct sk_buff * skb; | |
9494218f | 482 | int err = -ENOMEM; |
1da177e4 | 483 | |
9f10d3f6 NC |
484 | /* First, grab a route. */ |
485 | if (!dst && (dst = inet6_csk_route_req(sk, fl6, req)) == NULL) | |
fd80eb94 | 486 | goto done; |
9494218f | 487 | |
e6b4d113 | 488 | skb = tcp_make_synack(sk, dst, req, rvp); |
9494218f | 489 | |
1da177e4 | 490 | if (skb) { |
8ad50d96 | 491 | __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr); |
1da177e4 | 492 | |
9f10d3f6 | 493 | fl6->daddr = treq->rmt_addr; |
fff32699 | 494 | skb_set_queue_mapping(skb, queue_mapping); |
43264e0b | 495 | err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass); |
b9df3cb8 | 496 | err = net_xmit_eval(err); |
1da177e4 LT |
497 | } |
498 | ||
499 | done: | |
1da177e4 LT |
500 | return err; |
501 | } | |
502 | ||
72659ecc OP |
503 | static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req, |
504 | struct request_values *rvp) | |
505 | { | |
9f10d3f6 NC |
506 | struct flowi6 fl6; |
507 | ||
72659ecc | 508 | TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS); |
9f10d3f6 | 509 | return tcp_v6_send_synack(sk, NULL, &fl6, req, rvp, 0); |
72659ecc OP |
510 | } |
511 | ||
60236fdd | 512 | static void tcp_v6_reqsk_destructor(struct request_sock *req) |
1da177e4 | 513 | { |
800d55f1 | 514 | kfree_skb(inet6_rsk(req)->pktopts); |
1da177e4 LT |
515 | } |
516 | ||
cfb6eeb4 YH |
517 | #ifdef CONFIG_TCP_MD5SIG |
518 | static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk, | |
b71d1d42 | 519 | const struct in6_addr *addr) |
cfb6eeb4 | 520 | { |
a915da9b | 521 | return tcp_md5_do_lookup(sk, (union tcp_md5_addr *)addr, AF_INET6); |
cfb6eeb4 YH |
522 | } |
523 | ||
524 | static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk, | |
525 | struct sock *addr_sk) | |
526 | { | |
527 | return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr); | |
528 | } | |
529 | ||
530 | static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk, | |
531 | struct request_sock *req) | |
532 | { | |
533 | return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr); | |
534 | } | |
535 | ||
cfb6eeb4 YH |
536 | static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval, |
537 | int optlen) | |
538 | { | |
539 | struct tcp_md5sig cmd; | |
540 | struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr; | |
cfb6eeb4 YH |
541 | |
542 | if (optlen < sizeof(cmd)) | |
543 | return -EINVAL; | |
544 | ||
545 | if (copy_from_user(&cmd, optval, sizeof(cmd))) | |
546 | return -EFAULT; | |
547 | ||
548 | if (sin6->sin6_family != AF_INET6) | |
549 | return -EINVAL; | |
550 | ||
551 | if (!cmd.tcpm_keylen) { | |
e773e4fa | 552 | if (ipv6_addr_v4mapped(&sin6->sin6_addr)) |
a915da9b ED |
553 | return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3], |
554 | AF_INET); | |
555 | return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr, | |
556 | AF_INET6); | |
cfb6eeb4 YH |
557 | } |
558 | ||
559 | if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN) | |
560 | return -EINVAL; | |
561 | ||
a915da9b ED |
562 | if (ipv6_addr_v4mapped(&sin6->sin6_addr)) |
563 | return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3], | |
564 | AF_INET, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL); | |
cfb6eeb4 | 565 | |
a915da9b ED |
566 | return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr, |
567 | AF_INET6, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL); | |
cfb6eeb4 YH |
568 | } |
569 | ||
49a72dfb | 570 | static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp, |
b71d1d42 ED |
571 | const struct in6_addr *daddr, |
572 | const struct in6_addr *saddr, int nbytes) | |
cfb6eeb4 | 573 | { |
cfb6eeb4 | 574 | struct tcp6_pseudohdr *bp; |
49a72dfb | 575 | struct scatterlist sg; |
8d26d76d | 576 | |
cfb6eeb4 | 577 | bp = &hp->md5_blk.ip6; |
cfb6eeb4 | 578 | /* 1. TCP pseudo-header (RFC2460) */ |
4e3fd7a0 AD |
579 | bp->saddr = *saddr; |
580 | bp->daddr = *daddr; | |
49a72dfb | 581 | bp->protocol = cpu_to_be32(IPPROTO_TCP); |
00b1304c | 582 | bp->len = cpu_to_be32(nbytes); |
cfb6eeb4 | 583 | |
49a72dfb AL |
584 | sg_init_one(&sg, bp, sizeof(*bp)); |
585 | return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp)); | |
586 | } | |
c7da57a1 | 587 | |
49a72dfb | 588 | static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key, |
b71d1d42 | 589 | const struct in6_addr *daddr, struct in6_addr *saddr, |
318cf7aa | 590 | const struct tcphdr *th) |
49a72dfb AL |
591 | { |
592 | struct tcp_md5sig_pool *hp; | |
593 | struct hash_desc *desc; | |
594 | ||
595 | hp = tcp_get_md5sig_pool(); | |
596 | if (!hp) | |
597 | goto clear_hash_noput; | |
598 | desc = &hp->md5_desc; | |
599 | ||
600 | if (crypto_hash_init(desc)) | |
601 | goto clear_hash; | |
602 | if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2)) | |
603 | goto clear_hash; | |
604 | if (tcp_md5_hash_header(hp, th)) | |
605 | goto clear_hash; | |
606 | if (tcp_md5_hash_key(hp, key)) | |
607 | goto clear_hash; | |
608 | if (crypto_hash_final(desc, md5_hash)) | |
cfb6eeb4 | 609 | goto clear_hash; |
cfb6eeb4 | 610 | |
cfb6eeb4 | 611 | tcp_put_md5sig_pool(); |
cfb6eeb4 | 612 | return 0; |
49a72dfb | 613 | |
cfb6eeb4 YH |
614 | clear_hash: |
615 | tcp_put_md5sig_pool(); | |
616 | clear_hash_noput: | |
617 | memset(md5_hash, 0, 16); | |
49a72dfb | 618 | return 1; |
cfb6eeb4 YH |
619 | } |
620 | ||
49a72dfb | 621 | static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key, |
318cf7aa ED |
622 | const struct sock *sk, |
623 | const struct request_sock *req, | |
624 | const struct sk_buff *skb) | |
cfb6eeb4 | 625 | { |
b71d1d42 | 626 | const struct in6_addr *saddr, *daddr; |
49a72dfb AL |
627 | struct tcp_md5sig_pool *hp; |
628 | struct hash_desc *desc; | |
318cf7aa | 629 | const struct tcphdr *th = tcp_hdr(skb); |
cfb6eeb4 YH |
630 | |
631 | if (sk) { | |
632 | saddr = &inet6_sk(sk)->saddr; | |
633 | daddr = &inet6_sk(sk)->daddr; | |
49a72dfb | 634 | } else if (req) { |
cfb6eeb4 YH |
635 | saddr = &inet6_rsk(req)->loc_addr; |
636 | daddr = &inet6_rsk(req)->rmt_addr; | |
49a72dfb | 637 | } else { |
b71d1d42 | 638 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
49a72dfb AL |
639 | saddr = &ip6h->saddr; |
640 | daddr = &ip6h->daddr; | |
cfb6eeb4 | 641 | } |
49a72dfb AL |
642 | |
643 | hp = tcp_get_md5sig_pool(); | |
644 | if (!hp) | |
645 | goto clear_hash_noput; | |
646 | desc = &hp->md5_desc; | |
647 | ||
648 | if (crypto_hash_init(desc)) | |
649 | goto clear_hash; | |
650 | ||
651 | if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len)) | |
652 | goto clear_hash; | |
653 | if (tcp_md5_hash_header(hp, th)) | |
654 | goto clear_hash; | |
655 | if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2)) | |
656 | goto clear_hash; | |
657 | if (tcp_md5_hash_key(hp, key)) | |
658 | goto clear_hash; | |
659 | if (crypto_hash_final(desc, md5_hash)) | |
660 | goto clear_hash; | |
661 | ||
662 | tcp_put_md5sig_pool(); | |
663 | return 0; | |
664 | ||
665 | clear_hash: | |
666 | tcp_put_md5sig_pool(); | |
667 | clear_hash_noput: | |
668 | memset(md5_hash, 0, 16); | |
669 | return 1; | |
cfb6eeb4 YH |
670 | } |
671 | ||
318cf7aa | 672 | static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb) |
cfb6eeb4 | 673 | { |
cf533ea5 | 674 | const __u8 *hash_location = NULL; |
cfb6eeb4 | 675 | struct tcp_md5sig_key *hash_expected; |
b71d1d42 | 676 | const struct ipv6hdr *ip6h = ipv6_hdr(skb); |
318cf7aa | 677 | const struct tcphdr *th = tcp_hdr(skb); |
cfb6eeb4 | 678 | int genhash; |
cfb6eeb4 YH |
679 | u8 newhash[16]; |
680 | ||
681 | hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr); | |
7d5d5525 | 682 | hash_location = tcp_parse_md5sig_option(th); |
cfb6eeb4 | 683 | |
785957d3 DM |
684 | /* We've parsed the options - do we have a hash? */ |
685 | if (!hash_expected && !hash_location) | |
686 | return 0; | |
687 | ||
688 | if (hash_expected && !hash_location) { | |
689 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND); | |
cfb6eeb4 YH |
690 | return 1; |
691 | } | |
692 | ||
785957d3 DM |
693 | if (!hash_expected && hash_location) { |
694 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED); | |
cfb6eeb4 YH |
695 | return 1; |
696 | } | |
697 | ||
698 | /* check the signature */ | |
49a72dfb AL |
699 | genhash = tcp_v6_md5_hash_skb(newhash, |
700 | hash_expected, | |
701 | NULL, NULL, skb); | |
702 | ||
cfb6eeb4 | 703 | if (genhash || memcmp(hash_location, newhash, 16) != 0) { |
e87cc472 JP |
704 | net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n", |
705 | genhash ? "failed" : "mismatch", | |
706 | &ip6h->saddr, ntohs(th->source), | |
707 | &ip6h->daddr, ntohs(th->dest)); | |
cfb6eeb4 YH |
708 | return 1; |
709 | } | |
710 | return 0; | |
711 | } | |
712 | #endif | |
713 | ||
c6aefafb | 714 | struct request_sock_ops tcp6_request_sock_ops __read_mostly = { |
1da177e4 | 715 | .family = AF_INET6, |
2e6599cb | 716 | .obj_size = sizeof(struct tcp6_request_sock), |
72659ecc | 717 | .rtx_syn_ack = tcp_v6_rtx_synack, |
60236fdd ACM |
718 | .send_ack = tcp_v6_reqsk_send_ack, |
719 | .destructor = tcp_v6_reqsk_destructor, | |
72659ecc OP |
720 | .send_reset = tcp_v6_send_reset, |
721 | .syn_ack_timeout = tcp_syn_ack_timeout, | |
1da177e4 LT |
722 | }; |
723 | ||
cfb6eeb4 | 724 | #ifdef CONFIG_TCP_MD5SIG |
b2e4b3de | 725 | static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = { |
cfb6eeb4 | 726 | .md5_lookup = tcp_v6_reqsk_md5_lookup, |
e3afe7b7 | 727 | .calc_md5_hash = tcp_v6_md5_hash_skb, |
cfb6eeb4 | 728 | }; |
b6332e6c | 729 | #endif |
cfb6eeb4 | 730 | |
8ad50d96 | 731 | static void __tcp_v6_send_check(struct sk_buff *skb, |
b71d1d42 | 732 | const struct in6_addr *saddr, const struct in6_addr *daddr) |
1da177e4 | 733 | { |
aa8223c7 | 734 | struct tcphdr *th = tcp_hdr(skb); |
1da177e4 | 735 | |
84fa7933 | 736 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
8ad50d96 | 737 | th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0); |
663ead3b | 738 | skb->csum_start = skb_transport_header(skb) - skb->head; |
ff1dcadb | 739 | skb->csum_offset = offsetof(struct tcphdr, check); |
1da177e4 | 740 | } else { |
8ad50d96 HX |
741 | th->check = tcp_v6_check(skb->len, saddr, daddr, |
742 | csum_partial(th, th->doff << 2, | |
743 | skb->csum)); | |
1da177e4 LT |
744 | } |
745 | } | |
746 | ||
bb296246 | 747 | static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb) |
8ad50d96 HX |
748 | { |
749 | struct ipv6_pinfo *np = inet6_sk(sk); | |
750 | ||
751 | __tcp_v6_send_check(skb, &np->saddr, &np->daddr); | |
752 | } | |
753 | ||
a430a43d HX |
754 | static int tcp_v6_gso_send_check(struct sk_buff *skb) |
755 | { | |
b71d1d42 | 756 | const struct ipv6hdr *ipv6h; |
a430a43d HX |
757 | struct tcphdr *th; |
758 | ||
759 | if (!pskb_may_pull(skb, sizeof(*th))) | |
760 | return -EINVAL; | |
761 | ||
0660e03f | 762 | ipv6h = ipv6_hdr(skb); |
aa8223c7 | 763 | th = tcp_hdr(skb); |
a430a43d HX |
764 | |
765 | th->check = 0; | |
84fa7933 | 766 | skb->ip_summed = CHECKSUM_PARTIAL; |
8ad50d96 | 767 | __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr); |
a430a43d HX |
768 | return 0; |
769 | } | |
1da177e4 | 770 | |
36990673 HX |
771 | static struct sk_buff **tcp6_gro_receive(struct sk_buff **head, |
772 | struct sk_buff *skb) | |
684f2176 | 773 | { |
b71d1d42 | 774 | const struct ipv6hdr *iph = skb_gro_network_header(skb); |
684f2176 HX |
775 | |
776 | switch (skb->ip_summed) { | |
777 | case CHECKSUM_COMPLETE: | |
86911732 | 778 | if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr, |
684f2176 HX |
779 | skb->csum)) { |
780 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
781 | break; | |
782 | } | |
783 | ||
784 | /* fall through */ | |
785 | case CHECKSUM_NONE: | |
786 | NAPI_GRO_CB(skb)->flush = 1; | |
787 | return NULL; | |
788 | } | |
789 | ||
790 | return tcp_gro_receive(head, skb); | |
791 | } | |
684f2176 | 792 | |
36990673 | 793 | static int tcp6_gro_complete(struct sk_buff *skb) |
684f2176 | 794 | { |
b71d1d42 | 795 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
684f2176 HX |
796 | struct tcphdr *th = tcp_hdr(skb); |
797 | ||
798 | th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb), | |
799 | &iph->saddr, &iph->daddr, 0); | |
800 | skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6; | |
801 | ||
802 | return tcp_gro_complete(skb); | |
803 | } | |
684f2176 | 804 | |
626e264d | 805 | static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win, |
b903d324 | 806 | u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass) |
1da177e4 | 807 | { |
cf533ea5 ED |
808 | const struct tcphdr *th = tcp_hdr(skb); |
809 | struct tcphdr *t1; | |
1da177e4 | 810 | struct sk_buff *buff; |
4c9483b2 | 811 | struct flowi6 fl6; |
adf30907 | 812 | struct net *net = dev_net(skb_dst(skb)->dev); |
e5047992 | 813 | struct sock *ctl_sk = net->ipv6.tcp_sk; |
77c676da | 814 | unsigned int tot_len = sizeof(struct tcphdr); |
adf30907 | 815 | struct dst_entry *dst; |
81ada62d | 816 | __be32 *topt; |
1da177e4 | 817 | |
626e264d IJ |
818 | if (ts) |
819 | tot_len += TCPOLEN_TSTAMP_ALIGNED; | |
cfb6eeb4 | 820 | #ifdef CONFIG_TCP_MD5SIG |
cfb6eeb4 YH |
821 | if (key) |
822 | tot_len += TCPOLEN_MD5SIG_ALIGNED; | |
823 | #endif | |
824 | ||
cfb6eeb4 | 825 | buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len, |
1da177e4 | 826 | GFP_ATOMIC); |
1ab1457c YH |
827 | if (buff == NULL) |
828 | return; | |
1da177e4 | 829 | |
cfb6eeb4 | 830 | skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len); |
1da177e4 | 831 | |
cfb6eeb4 | 832 | t1 = (struct tcphdr *) skb_push(buff, tot_len); |
6651ffc8 | 833 | skb_reset_transport_header(buff); |
1da177e4 LT |
834 | |
835 | /* Swap the send and the receive. */ | |
836 | memset(t1, 0, sizeof(*t1)); | |
837 | t1->dest = th->source; | |
838 | t1->source = th->dest; | |
cfb6eeb4 | 839 | t1->doff = tot_len / 4; |
626e264d IJ |
840 | t1->seq = htonl(seq); |
841 | t1->ack_seq = htonl(ack); | |
842 | t1->ack = !rst || !th->ack; | |
843 | t1->rst = rst; | |
844 | t1->window = htons(win); | |
1da177e4 | 845 | |
81ada62d IJ |
846 | topt = (__be32 *)(t1 + 1); |
847 | ||
626e264d IJ |
848 | if (ts) { |
849 | *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | | |
850 | (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP); | |
851 | *topt++ = htonl(tcp_time_stamp); | |
852 | *topt++ = htonl(ts); | |
853 | } | |
854 | ||
cfb6eeb4 YH |
855 | #ifdef CONFIG_TCP_MD5SIG |
856 | if (key) { | |
81ada62d IJ |
857 | *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | |
858 | (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG); | |
859 | tcp_v6_md5_hash_hdr((__u8 *)topt, key, | |
78e645cb IJ |
860 | &ipv6_hdr(skb)->saddr, |
861 | &ipv6_hdr(skb)->daddr, t1); | |
cfb6eeb4 YH |
862 | } |
863 | #endif | |
864 | ||
4c9483b2 | 865 | memset(&fl6, 0, sizeof(fl6)); |
4e3fd7a0 AD |
866 | fl6.daddr = ipv6_hdr(skb)->saddr; |
867 | fl6.saddr = ipv6_hdr(skb)->daddr; | |
1da177e4 | 868 | |
e5700aff DM |
869 | buff->ip_summed = CHECKSUM_PARTIAL; |
870 | buff->csum = 0; | |
871 | ||
4c9483b2 | 872 | __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr); |
1da177e4 | 873 | |
4c9483b2 DM |
874 | fl6.flowi6_proto = IPPROTO_TCP; |
875 | fl6.flowi6_oif = inet6_iif(skb); | |
1958b856 DM |
876 | fl6.fl6_dport = t1->dest; |
877 | fl6.fl6_sport = t1->source; | |
4c9483b2 | 878 | security_skb_classify_flow(skb, flowi6_to_flowi(&fl6)); |
1da177e4 | 879 | |
c20121ae DL |
880 | /* Pass a socket to ip6_dst_lookup either it is for RST |
881 | * Underlying function will use this to retrieve the network | |
882 | * namespace | |
883 | */ | |
4c9483b2 | 884 | dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false); |
68d0c6d3 DM |
885 | if (!IS_ERR(dst)) { |
886 | skb_dst_set(buff, dst); | |
b903d324 | 887 | ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass); |
68d0c6d3 DM |
888 | TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS); |
889 | if (rst) | |
890 | TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS); | |
891 | return; | |
1da177e4 LT |
892 | } |
893 | ||
894 | kfree_skb(buff); | |
895 | } | |
896 | ||
626e264d | 897 | static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb) |
1da177e4 | 898 | { |
cf533ea5 | 899 | const struct tcphdr *th = tcp_hdr(skb); |
626e264d | 900 | u32 seq = 0, ack_seq = 0; |
fa3e5b4e | 901 | struct tcp_md5sig_key *key = NULL; |
658ddaaf SL |
902 | #ifdef CONFIG_TCP_MD5SIG |
903 | const __u8 *hash_location = NULL; | |
904 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); | |
905 | unsigned char newhash[16]; | |
906 | int genhash; | |
907 | struct sock *sk1 = NULL; | |
908 | #endif | |
1da177e4 | 909 | |
626e264d | 910 | if (th->rst) |
1da177e4 LT |
911 | return; |
912 | ||
626e264d IJ |
913 | if (!ipv6_unicast_destination(skb)) |
914 | return; | |
1da177e4 | 915 | |
cfb6eeb4 | 916 | #ifdef CONFIG_TCP_MD5SIG |
658ddaaf SL |
917 | hash_location = tcp_parse_md5sig_option(th); |
918 | if (!sk && hash_location) { | |
919 | /* | |
920 | * active side is lost. Try to find listening socket through | |
921 | * source port, and then find md5 key through listening socket. | |
922 | * we are not loose security here: | |
923 | * Incoming packet is checked with md5 hash with finding key, | |
924 | * no RST generated if md5 hash doesn't match. | |
925 | */ | |
926 | sk1 = inet6_lookup_listener(dev_net(skb_dst(skb)->dev), | |
927 | &tcp_hashinfo, &ipv6h->daddr, | |
928 | ntohs(th->source), inet6_iif(skb)); | |
929 | if (!sk1) | |
930 | return; | |
931 | ||
932 | rcu_read_lock(); | |
933 | key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr); | |
934 | if (!key) | |
935 | goto release_sk1; | |
936 | ||
937 | genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, NULL, skb); | |
938 | if (genhash || memcmp(hash_location, newhash, 16) != 0) | |
939 | goto release_sk1; | |
940 | } else { | |
941 | key = sk ? tcp_v6_md5_do_lookup(sk, &ipv6h->saddr) : NULL; | |
942 | } | |
cfb6eeb4 YH |
943 | #endif |
944 | ||
626e264d IJ |
945 | if (th->ack) |
946 | seq = ntohl(th->ack_seq); | |
947 | else | |
948 | ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len - | |
949 | (th->doff << 2); | |
1da177e4 | 950 | |
b903d324 | 951 | tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0); |
658ddaaf SL |
952 | |
953 | #ifdef CONFIG_TCP_MD5SIG | |
954 | release_sk1: | |
955 | if (sk1) { | |
956 | rcu_read_unlock(); | |
957 | sock_put(sk1); | |
958 | } | |
959 | #endif | |
626e264d | 960 | } |
1da177e4 | 961 | |
626e264d | 962 | static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts, |
b903d324 | 963 | struct tcp_md5sig_key *key, u8 tclass) |
626e264d | 964 | { |
b903d324 | 965 | tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass); |
1da177e4 LT |
966 | } |
967 | ||
968 | static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb) | |
969 | { | |
8feaf0c0 | 970 | struct inet_timewait_sock *tw = inet_twsk(sk); |
cfb6eeb4 | 971 | struct tcp_timewait_sock *tcptw = tcp_twsk(sk); |
1da177e4 | 972 | |
9501f972 | 973 | tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt, |
8feaf0c0 | 974 | tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale, |
b903d324 ED |
975 | tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw), |
976 | tw->tw_tclass); | |
1da177e4 | 977 | |
8feaf0c0 | 978 | inet_twsk_put(tw); |
1da177e4 LT |
979 | } |
980 | ||
6edafaaf GJ |
981 | static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb, |
982 | struct request_sock *req) | |
1da177e4 | 983 | { |
9501f972 | 984 | tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent, |
b903d324 | 985 | tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0); |
1da177e4 LT |
986 | } |
987 | ||
988 | ||
989 | static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb) | |
990 | { | |
60236fdd | 991 | struct request_sock *req, **prev; |
aa8223c7 | 992 | const struct tcphdr *th = tcp_hdr(skb); |
1da177e4 LT |
993 | struct sock *nsk; |
994 | ||
995 | /* Find possible connection requests. */ | |
8129765a | 996 | req = inet6_csk_search_req(sk, &prev, th->source, |
0660e03f ACM |
997 | &ipv6_hdr(skb)->saddr, |
998 | &ipv6_hdr(skb)->daddr, inet6_iif(skb)); | |
1da177e4 LT |
999 | if (req) |
1000 | return tcp_check_req(sk, skb, req, prev); | |
1001 | ||
3b1e0a65 | 1002 | nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo, |
d86e0dac PE |
1003 | &ipv6_hdr(skb)->saddr, th->source, |
1004 | &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb)); | |
1da177e4 LT |
1005 | |
1006 | if (nsk) { | |
1007 | if (nsk->sk_state != TCP_TIME_WAIT) { | |
1008 | bh_lock_sock(nsk); | |
1009 | return nsk; | |
1010 | } | |
9469c7b4 | 1011 | inet_twsk_put(inet_twsk(nsk)); |
1da177e4 LT |
1012 | return NULL; |
1013 | } | |
1014 | ||
c6aefafb | 1015 | #ifdef CONFIG_SYN_COOKIES |
af9b4738 | 1016 | if (!th->syn) |
c6aefafb | 1017 | sk = cookie_v6_check(sk, skb); |
1da177e4 LT |
1018 | #endif |
1019 | return sk; | |
1020 | } | |
1021 | ||
1da177e4 LT |
1022 | /* FIXME: this is substantially similar to the ipv4 code. |
1023 | * Can some kind of merge be done? -- erics | |
1024 | */ | |
1025 | static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb) | |
1026 | { | |
4957faad | 1027 | struct tcp_extend_values tmp_ext; |
e6b4d113 | 1028 | struct tcp_options_received tmp_opt; |
cf533ea5 | 1029 | const u8 *hash_location; |
e6b4d113 | 1030 | struct request_sock *req; |
ca304b61 | 1031 | struct inet6_request_sock *treq; |
1da177e4 | 1032 | struct ipv6_pinfo *np = inet6_sk(sk); |
1da177e4 | 1033 | struct tcp_sock *tp = tcp_sk(sk); |
e6b4d113 | 1034 | __u32 isn = TCP_SKB_CB(skb)->when; |
493f377d | 1035 | struct dst_entry *dst = NULL; |
3840a06e | 1036 | struct flowi6 fl6; |
a2a385d6 | 1037 | bool want_cookie = false; |
1da177e4 LT |
1038 | |
1039 | if (skb->protocol == htons(ETH_P_IP)) | |
1040 | return tcp_v4_conn_request(sk, skb); | |
1041 | ||
1042 | if (!ipv6_unicast_destination(skb)) | |
1ab1457c | 1043 | goto drop; |
1da177e4 | 1044 | |
463c84b9 | 1045 | if (inet_csk_reqsk_queue_is_full(sk) && !isn) { |
946cedcc ED |
1046 | want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6"); |
1047 | if (!want_cookie) | |
1048 | goto drop; | |
1da177e4 LT |
1049 | } |
1050 | ||
463c84b9 | 1051 | if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1) |
1da177e4 LT |
1052 | goto drop; |
1053 | ||
ca304b61 | 1054 | req = inet6_reqsk_alloc(&tcp6_request_sock_ops); |
1da177e4 LT |
1055 | if (req == NULL) |
1056 | goto drop; | |
1057 | ||
cfb6eeb4 YH |
1058 | #ifdef CONFIG_TCP_MD5SIG |
1059 | tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops; | |
1060 | #endif | |
1061 | ||
1da177e4 LT |
1062 | tcp_clear_options(&tmp_opt); |
1063 | tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr); | |
1064 | tmp_opt.user_mss = tp->rx_opt.user_mss; | |
bb5b7c11 | 1065 | tcp_parse_options(skb, &tmp_opt, &hash_location, 0); |
4957faad WAS |
1066 | |
1067 | if (tmp_opt.cookie_plus > 0 && | |
1068 | tmp_opt.saw_tstamp && | |
1069 | !tp->rx_opt.cookie_out_never && | |
1070 | (sysctl_tcp_cookie_size > 0 || | |
1071 | (tp->cookie_values != NULL && | |
1072 | tp->cookie_values->cookie_desired > 0))) { | |
1073 | u8 *c; | |
1074 | u32 *d; | |
1075 | u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS]; | |
1076 | int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE; | |
1077 | ||
1078 | if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0) | |
1079 | goto drop_and_free; | |
1080 | ||
1081 | /* Secret recipe starts with IP addresses */ | |
0eae88f3 | 1082 | d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0]; |
4957faad WAS |
1083 | *mess++ ^= *d++; |
1084 | *mess++ ^= *d++; | |
1085 | *mess++ ^= *d++; | |
1086 | *mess++ ^= *d++; | |
0eae88f3 | 1087 | d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0]; |
4957faad WAS |
1088 | *mess++ ^= *d++; |
1089 | *mess++ ^= *d++; | |
1090 | *mess++ ^= *d++; | |
1091 | *mess++ ^= *d++; | |
1092 | ||
1093 | /* plus variable length Initiator Cookie */ | |
1094 | c = (u8 *)mess; | |
1095 | while (l-- > 0) | |
1096 | *c++ ^= *hash_location++; | |
1da177e4 | 1097 | |
a2a385d6 | 1098 | want_cookie = false; /* not our kind of cookie */ |
4957faad WAS |
1099 | tmp_ext.cookie_out_never = 0; /* false */ |
1100 | tmp_ext.cookie_plus = tmp_opt.cookie_plus; | |
1101 | } else if (!tp->rx_opt.cookie_in_always) { | |
1102 | /* redundant indications, but ensure initialization. */ | |
1103 | tmp_ext.cookie_out_never = 1; /* true */ | |
1104 | tmp_ext.cookie_plus = 0; | |
1105 | } else { | |
1106 | goto drop_and_free; | |
1107 | } | |
1108 | tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always; | |
1da177e4 | 1109 | |
4dfc2817 | 1110 | if (want_cookie && !tmp_opt.saw_tstamp) |
c6aefafb | 1111 | tcp_clear_options(&tmp_opt); |
c6aefafb | 1112 | |
1da177e4 LT |
1113 | tmp_opt.tstamp_ok = tmp_opt.saw_tstamp; |
1114 | tcp_openreq_init(req, &tmp_opt, skb); | |
1115 | ||
ca304b61 | 1116 | treq = inet6_rsk(req); |
4e3fd7a0 AD |
1117 | treq->rmt_addr = ipv6_hdr(skb)->saddr; |
1118 | treq->loc_addr = ipv6_hdr(skb)->daddr; | |
172d69e6 | 1119 | if (!want_cookie || tmp_opt.tstamp_ok) |
bd14b1b2 | 1120 | TCP_ECN_create_request(req, skb); |
c6aefafb | 1121 | |
4d0fe50c ED |
1122 | treq->iif = sk->sk_bound_dev_if; |
1123 | ||
1124 | /* So that link locals have meaning */ | |
1125 | if (!sk->sk_bound_dev_if && | |
1126 | ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL) | |
1127 | treq->iif = inet6_iif(skb); | |
1128 | ||
2bbdf389 | 1129 | if (!isn) { |
c6aefafb GG |
1130 | if (ipv6_opt_accepted(sk, skb) || |
1131 | np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo || | |
1132 | np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) { | |
1133 | atomic_inc(&skb->users); | |
1134 | treq->pktopts = skb; | |
1135 | } | |
493f377d DM |
1136 | |
1137 | if (want_cookie) { | |
2bbdf389 FW |
1138 | isn = cookie_v6_init_sequence(sk, skb, &req->mss); |
1139 | req->cookie_ts = tmp_opt.tstamp_ok; | |
493f377d DM |
1140 | goto have_isn; |
1141 | } | |
1142 | ||
1143 | /* VJ's idea. We save last timestamp seen | |
1144 | * from the destination in peer table, when entering | |
1145 | * state TIME-WAIT, and check against it before | |
1146 | * accepting new connection request. | |
1147 | * | |
1148 | * If "isn" is not zero, this request hit alive | |
1149 | * timewait bucket, so that all the necessary checks | |
1150 | * are made in the function processing timewait state. | |
1151 | */ | |
1152 | if (tmp_opt.saw_tstamp && | |
1153 | tcp_death_row.sysctl_tw_recycle && | |
81166dd6 DM |
1154 | (dst = inet6_csk_route_req(sk, &fl6, req)) != NULL) { |
1155 | if (!tcp_peer_is_proven(req, dst, true)) { | |
493f377d DM |
1156 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED); |
1157 | goto drop_and_release; | |
1158 | } | |
1159 | } | |
1160 | /* Kill the following clause, if you dislike this way. */ | |
1161 | else if (!sysctl_tcp_syncookies && | |
1162 | (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) < | |
1163 | (sysctl_max_syn_backlog >> 2)) && | |
81166dd6 | 1164 | !tcp_peer_is_proven(req, dst, false)) { |
493f377d DM |
1165 | /* Without syncookies last quarter of |
1166 | * backlog is filled with destinations, | |
1167 | * proven to be alive. | |
1168 | * It means that we continue to communicate | |
1169 | * to destinations, already remembered | |
1170 | * to the moment of synflood. | |
1171 | */ | |
1172 | LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n", | |
1173 | &treq->rmt_addr, ntohs(tcp_hdr(skb)->source)); | |
1174 | goto drop_and_release; | |
2bbdf389 | 1175 | } |
493f377d DM |
1176 | |
1177 | isn = tcp_v6_init_sequence(skb); | |
c6aefafb | 1178 | } |
493f377d | 1179 | have_isn: |
2e6599cb | 1180 | tcp_rsk(req)->snt_isn = isn; |
9ad7c049 | 1181 | tcp_rsk(req)->snt_synack = tcp_time_stamp; |
1da177e4 | 1182 | |
437c5b53 NC |
1183 | if (security_inet_conn_request(sk, skb, req)) |
1184 | goto drop_and_release; | |
4237c75c | 1185 | |
9f10d3f6 | 1186 | if (tcp_v6_send_synack(sk, dst, &fl6, req, |
fff32699 ED |
1187 | (struct request_values *)&tmp_ext, |
1188 | skb_get_queue_mapping(skb)) || | |
4957faad | 1189 | want_cookie) |
e6b4d113 | 1190 | goto drop_and_free; |
1da177e4 | 1191 | |
e6b4d113 WAS |
1192 | inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT); |
1193 | return 0; | |
1da177e4 | 1194 | |
493f377d DM |
1195 | drop_and_release: |
1196 | dst_release(dst); | |
e6b4d113 WAS |
1197 | drop_and_free: |
1198 | reqsk_free(req); | |
1da177e4 | 1199 | drop: |
1da177e4 LT |
1200 | return 0; /* don't send reset */ |
1201 | } | |
1202 | ||
1203 | static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb, | |
60236fdd | 1204 | struct request_sock *req, |
1da177e4 LT |
1205 | struct dst_entry *dst) |
1206 | { | |
78d15e82 | 1207 | struct inet6_request_sock *treq; |
1da177e4 LT |
1208 | struct ipv6_pinfo *newnp, *np = inet6_sk(sk); |
1209 | struct tcp6_sock *newtcp6sk; | |
1210 | struct inet_sock *newinet; | |
1211 | struct tcp_sock *newtp; | |
1212 | struct sock *newsk; | |
cfb6eeb4 YH |
1213 | #ifdef CONFIG_TCP_MD5SIG |
1214 | struct tcp_md5sig_key *key; | |
1215 | #endif | |
3840a06e | 1216 | struct flowi6 fl6; |
1da177e4 LT |
1217 | |
1218 | if (skb->protocol == htons(ETH_P_IP)) { | |
1219 | /* | |
1220 | * v6 mapped | |
1221 | */ | |
1222 | ||
1223 | newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst); | |
1224 | ||
1ab1457c | 1225 | if (newsk == NULL) |
1da177e4 LT |
1226 | return NULL; |
1227 | ||
1228 | newtcp6sk = (struct tcp6_sock *)newsk; | |
1229 | inet_sk(newsk)->pinet6 = &newtcp6sk->inet6; | |
1230 | ||
1231 | newinet = inet_sk(newsk); | |
1232 | newnp = inet6_sk(newsk); | |
1233 | newtp = tcp_sk(newsk); | |
1234 | ||
1235 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); | |
1236 | ||
c720c7e8 | 1237 | ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr); |
1da177e4 | 1238 | |
c720c7e8 | 1239 | ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr); |
1da177e4 | 1240 | |
4e3fd7a0 | 1241 | newnp->rcv_saddr = newnp->saddr; |
1da177e4 | 1242 | |
8292a17a | 1243 | inet_csk(newsk)->icsk_af_ops = &ipv6_mapped; |
1da177e4 | 1244 | newsk->sk_backlog_rcv = tcp_v4_do_rcv; |
cfb6eeb4 YH |
1245 | #ifdef CONFIG_TCP_MD5SIG |
1246 | newtp->af_specific = &tcp_sock_ipv6_mapped_specific; | |
1247 | #endif | |
1248 | ||
676a1184 YZ |
1249 | newnp->ipv6_ac_list = NULL; |
1250 | newnp->ipv6_fl_list = NULL; | |
1da177e4 LT |
1251 | newnp->pktoptions = NULL; |
1252 | newnp->opt = NULL; | |
505cbfc5 | 1253 | newnp->mcast_oif = inet6_iif(skb); |
0660e03f | 1254 | newnp->mcast_hops = ipv6_hdr(skb)->hop_limit; |
4c507d28 | 1255 | newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb)); |
1da177e4 | 1256 | |
e6848976 ACM |
1257 | /* |
1258 | * No need to charge this sock to the relevant IPv6 refcnt debug socks count | |
1259 | * here, tcp_create_openreq_child now does this for us, see the comment in | |
1260 | * that function for the gory details. -acme | |
1da177e4 | 1261 | */ |
1da177e4 LT |
1262 | |
1263 | /* It is tricky place. Until this moment IPv4 tcp | |
8292a17a | 1264 | worked with IPv6 icsk.icsk_af_ops. |
1da177e4 LT |
1265 | Sync it now. |
1266 | */ | |
d83d8461 | 1267 | tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie); |
1da177e4 LT |
1268 | |
1269 | return newsk; | |
1270 | } | |
1271 | ||
78d15e82 | 1272 | treq = inet6_rsk(req); |
1da177e4 LT |
1273 | |
1274 | if (sk_acceptq_is_full(sk)) | |
1275 | goto out_overflow; | |
1276 | ||
493f377d | 1277 | if (!dst) { |
3840a06e | 1278 | dst = inet6_csk_route_req(sk, &fl6, req); |
493f377d | 1279 | if (!dst) |
1da177e4 | 1280 | goto out; |
1ab1457c | 1281 | } |
1da177e4 LT |
1282 | |
1283 | newsk = tcp_create_openreq_child(sk, req, skb); | |
1284 | if (newsk == NULL) | |
093d2823 | 1285 | goto out_nonewsk; |
1da177e4 | 1286 | |
e6848976 ACM |
1287 | /* |
1288 | * No need to charge this sock to the relevant IPv6 refcnt debug socks | |
1289 | * count here, tcp_create_openreq_child now does this for us, see the | |
1290 | * comment in that function for the gory details. -acme | |
1291 | */ | |
1da177e4 | 1292 | |
59eed279 | 1293 | newsk->sk_gso_type = SKB_GSO_TCPV6; |
8e1ef0a9 | 1294 | __ip6_dst_store(newsk, dst, NULL, NULL); |
1da177e4 LT |
1295 | |
1296 | newtcp6sk = (struct tcp6_sock *)newsk; | |
1297 | inet_sk(newsk)->pinet6 = &newtcp6sk->inet6; | |
1298 | ||
1299 | newtp = tcp_sk(newsk); | |
1300 | newinet = inet_sk(newsk); | |
1301 | newnp = inet6_sk(newsk); | |
1302 | ||
1303 | memcpy(newnp, np, sizeof(struct ipv6_pinfo)); | |
1304 | ||
4e3fd7a0 AD |
1305 | newnp->daddr = treq->rmt_addr; |
1306 | newnp->saddr = treq->loc_addr; | |
1307 | newnp->rcv_saddr = treq->loc_addr; | |
2e6599cb | 1308 | newsk->sk_bound_dev_if = treq->iif; |
1da177e4 | 1309 | |
1ab1457c | 1310 | /* Now IPv6 options... |
1da177e4 LT |
1311 | |
1312 | First: no IPv4 options. | |
1313 | */ | |
f6d8bd05 | 1314 | newinet->inet_opt = NULL; |
676a1184 | 1315 | newnp->ipv6_ac_list = NULL; |
d35690be | 1316 | newnp->ipv6_fl_list = NULL; |
1da177e4 LT |
1317 | |
1318 | /* Clone RX bits */ | |
1319 | newnp->rxopt.all = np->rxopt.all; | |
1320 | ||
1321 | /* Clone pktoptions received with SYN */ | |
1322 | newnp->pktoptions = NULL; | |
2e6599cb ACM |
1323 | if (treq->pktopts != NULL) { |
1324 | newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC); | |
ab185d7b | 1325 | consume_skb(treq->pktopts); |
2e6599cb | 1326 | treq->pktopts = NULL; |
1da177e4 LT |
1327 | if (newnp->pktoptions) |
1328 | skb_set_owner_r(newnp->pktoptions, newsk); | |
1329 | } | |
1330 | newnp->opt = NULL; | |
505cbfc5 | 1331 | newnp->mcast_oif = inet6_iif(skb); |
0660e03f | 1332 | newnp->mcast_hops = ipv6_hdr(skb)->hop_limit; |
4c507d28 | 1333 | newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb)); |
1da177e4 LT |
1334 | |
1335 | /* Clone native IPv6 options from listening socket (if any) | |
1336 | ||
1337 | Yes, keeping reference count would be much more clever, | |
1338 | but we make one more one thing there: reattach optmem | |
1339 | to newsk. | |
1340 | */ | |
43264e0b RL |
1341 | if (np->opt) |
1342 | newnp->opt = ipv6_dup_options(newsk, np->opt); | |
1da177e4 | 1343 | |
d83d8461 | 1344 | inet_csk(newsk)->icsk_ext_hdr_len = 0; |
1da177e4 | 1345 | if (newnp->opt) |
d83d8461 ACM |
1346 | inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen + |
1347 | newnp->opt->opt_flen); | |
1da177e4 | 1348 | |
5d424d5a | 1349 | tcp_mtup_init(newsk); |
1da177e4 | 1350 | tcp_sync_mss(newsk, dst_mtu(dst)); |
0dbaee3b | 1351 | newtp->advmss = dst_metric_advmss(dst); |
d135c522 NC |
1352 | if (tcp_sk(sk)->rx_opt.user_mss && |
1353 | tcp_sk(sk)->rx_opt.user_mss < newtp->advmss) | |
1354 | newtp->advmss = tcp_sk(sk)->rx_opt.user_mss; | |
1355 | ||
1da177e4 | 1356 | tcp_initialize_rcv_mss(newsk); |
9ad7c049 JC |
1357 | if (tcp_rsk(req)->snt_synack) |
1358 | tcp_valid_rtt_meas(newsk, | |
1359 | tcp_time_stamp - tcp_rsk(req)->snt_synack); | |
1360 | newtp->total_retrans = req->retrans; | |
1da177e4 | 1361 | |
c720c7e8 ED |
1362 | newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6; |
1363 | newinet->inet_rcv_saddr = LOOPBACK4_IPV6; | |
1da177e4 | 1364 | |
cfb6eeb4 YH |
1365 | #ifdef CONFIG_TCP_MD5SIG |
1366 | /* Copy over the MD5 key from the original socket */ | |
1367 | if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) { | |
1368 | /* We're using one, so create a matching key | |
1369 | * on the newsk structure. If we fail to get | |
1370 | * memory, then we end up not copying the key | |
1371 | * across. Shucks. | |
1372 | */ | |
a915da9b ED |
1373 | tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newnp->daddr, |
1374 | AF_INET6, key->key, key->keylen, GFP_ATOMIC); | |
cfb6eeb4 YH |
1375 | } |
1376 | #endif | |
1377 | ||
093d2823 BS |
1378 | if (__inet_inherit_port(sk, newsk) < 0) { |
1379 | sock_put(newsk); | |
1380 | goto out; | |
1381 | } | |
9327f705 | 1382 | __inet6_hash(newsk, NULL); |
1da177e4 LT |
1383 | |
1384 | return newsk; | |
1385 | ||
1386 | out_overflow: | |
de0744af | 1387 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS); |
093d2823 | 1388 | out_nonewsk: |
1da177e4 | 1389 | dst_release(dst); |
093d2823 BS |
1390 | out: |
1391 | NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS); | |
1da177e4 LT |
1392 | return NULL; |
1393 | } | |
1394 | ||
b51655b9 | 1395 | static __sum16 tcp_v6_checksum_init(struct sk_buff *skb) |
1da177e4 | 1396 | { |
84fa7933 | 1397 | if (skb->ip_summed == CHECKSUM_COMPLETE) { |
684f2176 | 1398 | if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr, |
0660e03f | 1399 | &ipv6_hdr(skb)->daddr, skb->csum)) { |
fb286bb2 | 1400 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
1da177e4 | 1401 | return 0; |
fb286bb2 | 1402 | } |
1da177e4 | 1403 | } |
fb286bb2 | 1404 | |
684f2176 | 1405 | skb->csum = ~csum_unfold(tcp_v6_check(skb->len, |
0660e03f ACM |
1406 | &ipv6_hdr(skb)->saddr, |
1407 | &ipv6_hdr(skb)->daddr, 0)); | |
fb286bb2 | 1408 | |
1da177e4 | 1409 | if (skb->len <= 76) { |
fb286bb2 | 1410 | return __skb_checksum_complete(skb); |
1da177e4 LT |
1411 | } |
1412 | return 0; | |
1413 | } | |
1414 | ||
1415 | /* The socket must have it's spinlock held when we get | |
1416 | * here. | |
1417 | * | |
1418 | * We have a potential double-lock case here, so even when | |
1419 | * doing backlog processing we use the BH locking scheme. | |
1420 | * This is because we cannot sleep with the original spinlock | |
1421 | * held. | |
1422 | */ | |
1423 | static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb) | |
1424 | { | |
1425 | struct ipv6_pinfo *np = inet6_sk(sk); | |
1426 | struct tcp_sock *tp; | |
1427 | struct sk_buff *opt_skb = NULL; | |
1428 | ||
1429 | /* Imagine: socket is IPv6. IPv4 packet arrives, | |
1430 | goes to IPv4 receive handler and backlogged. | |
1431 | From backlog it always goes here. Kerboom... | |
1432 | Fortunately, tcp_rcv_established and rcv_established | |
1433 | handle them correctly, but it is not case with | |
1434 | tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK | |
1435 | */ | |
1436 | ||
1437 | if (skb->protocol == htons(ETH_P_IP)) | |
1438 | return tcp_v4_do_rcv(sk, skb); | |
1439 | ||
cfb6eeb4 YH |
1440 | #ifdef CONFIG_TCP_MD5SIG |
1441 | if (tcp_v6_inbound_md5_hash (sk, skb)) | |
1442 | goto discard; | |
1443 | #endif | |
1444 | ||
fda9ef5d | 1445 | if (sk_filter(sk, skb)) |
1da177e4 LT |
1446 | goto discard; |
1447 | ||
1448 | /* | |
1449 | * socket locking is here for SMP purposes as backlog rcv | |
1450 | * is currently called with bh processing disabled. | |
1451 | */ | |
1452 | ||
1453 | /* Do Stevens' IPV6_PKTOPTIONS. | |
1454 | ||
1455 | Yes, guys, it is the only place in our code, where we | |
1456 | may make it not affecting IPv4. | |
1457 | The rest of code is protocol independent, | |
1458 | and I do not like idea to uglify IPv4. | |
1459 | ||
1460 | Actually, all the idea behind IPV6_PKTOPTIONS | |
1461 | looks not very well thought. For now we latch | |
1462 | options, received in the last packet, enqueued | |
1463 | by tcp. Feel free to propose better solution. | |
1ab1457c | 1464 | --ANK (980728) |
1da177e4 LT |
1465 | */ |
1466 | if (np->rxopt.all) | |
1467 | opt_skb = skb_clone(skb, GFP_ATOMIC); | |
1468 | ||
1469 | if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */ | |
bdeab991 | 1470 | sock_rps_save_rxhash(sk, skb); |
aa8223c7 | 1471 | if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len)) |
1da177e4 | 1472 | goto reset; |
1da177e4 LT |
1473 | if (opt_skb) |
1474 | goto ipv6_pktoptions; | |
1475 | return 0; | |
1476 | } | |
1477 | ||
ab6a5bb6 | 1478 | if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb)) |
1da177e4 LT |
1479 | goto csum_err; |
1480 | ||
1ab1457c | 1481 | if (sk->sk_state == TCP_LISTEN) { |
1da177e4 LT |
1482 | struct sock *nsk = tcp_v6_hnd_req(sk, skb); |
1483 | if (!nsk) | |
1484 | goto discard; | |
1485 | ||
1486 | /* | |
1487 | * Queue it on the new socket if the new socket is active, | |
1488 | * otherwise we just shortcircuit this and continue with | |
1489 | * the new socket.. | |
1490 | */ | |
1ab1457c | 1491 | if(nsk != sk) { |
bdeab991 | 1492 | sock_rps_save_rxhash(nsk, skb); |
1da177e4 LT |
1493 | if (tcp_child_process(sk, nsk, skb)) |
1494 | goto reset; | |
1495 | if (opt_skb) | |
1496 | __kfree_skb(opt_skb); | |
1497 | return 0; | |
1498 | } | |
47482f13 | 1499 | } else |
bdeab991 | 1500 | sock_rps_save_rxhash(sk, skb); |
1da177e4 | 1501 | |
aa8223c7 | 1502 | if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len)) |
1da177e4 | 1503 | goto reset; |
1da177e4 LT |
1504 | if (opt_skb) |
1505 | goto ipv6_pktoptions; | |
1506 | return 0; | |
1507 | ||
1508 | reset: | |
cfb6eeb4 | 1509 | tcp_v6_send_reset(sk, skb); |
1da177e4 LT |
1510 | discard: |
1511 | if (opt_skb) | |
1512 | __kfree_skb(opt_skb); | |
1513 | kfree_skb(skb); | |
1514 | return 0; | |
1515 | csum_err: | |
63231bdd | 1516 | TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS); |
1da177e4 LT |
1517 | goto discard; |
1518 | ||
1519 | ||
1520 | ipv6_pktoptions: | |
1521 | /* Do you ask, what is it? | |
1522 | ||
1523 | 1. skb was enqueued by tcp. | |
1524 | 2. skb is added to tail of read queue, rather than out of order. | |
1525 | 3. socket is not in passive state. | |
1526 | 4. Finally, it really contains options, which user wants to receive. | |
1527 | */ | |
1528 | tp = tcp_sk(sk); | |
1529 | if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt && | |
1530 | !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) { | |
333fad53 | 1531 | if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo) |
505cbfc5 | 1532 | np->mcast_oif = inet6_iif(opt_skb); |
333fad53 | 1533 | if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) |
0660e03f | 1534 | np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit; |
4c507d28 JB |
1535 | if (np->rxopt.bits.rxtclass) |
1536 | np->rcv_tclass = ipv6_tclass(ipv6_hdr(skb)); | |
1da177e4 LT |
1537 | if (ipv6_opt_accepted(sk, opt_skb)) { |
1538 | skb_set_owner_r(opt_skb, sk); | |
1539 | opt_skb = xchg(&np->pktoptions, opt_skb); | |
1540 | } else { | |
1541 | __kfree_skb(opt_skb); | |
1542 | opt_skb = xchg(&np->pktoptions, NULL); | |
1543 | } | |
1544 | } | |
1545 | ||
800d55f1 | 1546 | kfree_skb(opt_skb); |
1da177e4 LT |
1547 | return 0; |
1548 | } | |
1549 | ||
e5bbef20 | 1550 | static int tcp_v6_rcv(struct sk_buff *skb) |
1da177e4 | 1551 | { |
cf533ea5 | 1552 | const struct tcphdr *th; |
b71d1d42 | 1553 | const struct ipv6hdr *hdr; |
1da177e4 LT |
1554 | struct sock *sk; |
1555 | int ret; | |
a86b1e30 | 1556 | struct net *net = dev_net(skb->dev); |
1da177e4 LT |
1557 | |
1558 | if (skb->pkt_type != PACKET_HOST) | |
1559 | goto discard_it; | |
1560 | ||
1561 | /* | |
1562 | * Count it even if it's bad. | |
1563 | */ | |
63231bdd | 1564 | TCP_INC_STATS_BH(net, TCP_MIB_INSEGS); |
1da177e4 LT |
1565 | |
1566 | if (!pskb_may_pull(skb, sizeof(struct tcphdr))) | |
1567 | goto discard_it; | |
1568 | ||
aa8223c7 | 1569 | th = tcp_hdr(skb); |
1da177e4 LT |
1570 | |
1571 | if (th->doff < sizeof(struct tcphdr)/4) | |
1572 | goto bad_packet; | |
1573 | if (!pskb_may_pull(skb, th->doff*4)) | |
1574 | goto discard_it; | |
1575 | ||
60476372 | 1576 | if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb)) |
1da177e4 LT |
1577 | goto bad_packet; |
1578 | ||
aa8223c7 | 1579 | th = tcp_hdr(skb); |
e802af9c | 1580 | hdr = ipv6_hdr(skb); |
1da177e4 LT |
1581 | TCP_SKB_CB(skb)->seq = ntohl(th->seq); |
1582 | TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin + | |
1583 | skb->len - th->doff*4); | |
1584 | TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq); | |
1585 | TCP_SKB_CB(skb)->when = 0; | |
b82d1bb4 | 1586 | TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr); |
1da177e4 LT |
1587 | TCP_SKB_CB(skb)->sacked = 0; |
1588 | ||
9a1f27c4 | 1589 | sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest); |
1da177e4 LT |
1590 | if (!sk) |
1591 | goto no_tcp_socket; | |
1592 | ||
1593 | process: | |
1594 | if (sk->sk_state == TCP_TIME_WAIT) | |
1595 | goto do_time_wait; | |
1596 | ||
e802af9c SH |
1597 | if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) { |
1598 | NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP); | |
1599 | goto discard_and_relse; | |
1600 | } | |
1601 | ||
1da177e4 LT |
1602 | if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) |
1603 | goto discard_and_relse; | |
1604 | ||
fda9ef5d | 1605 | if (sk_filter(sk, skb)) |
1da177e4 LT |
1606 | goto discard_and_relse; |
1607 | ||
1608 | skb->dev = NULL; | |
1609 | ||
293b9c42 | 1610 | bh_lock_sock_nested(sk); |
1da177e4 LT |
1611 | ret = 0; |
1612 | if (!sock_owned_by_user(sk)) { | |
1a2449a8 | 1613 | #ifdef CONFIG_NET_DMA |
1ab1457c | 1614 | struct tcp_sock *tp = tcp_sk(sk); |
b4caea8a | 1615 | if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list) |
a2bd1140 | 1616 | tp->ucopy.dma_chan = net_dma_find_channel(); |
1ab1457c YH |
1617 | if (tp->ucopy.dma_chan) |
1618 | ret = tcp_v6_do_rcv(sk, skb); | |
1619 | else | |
1a2449a8 CL |
1620 | #endif |
1621 | { | |
1622 | if (!tcp_prequeue(sk, skb)) | |
1623 | ret = tcp_v6_do_rcv(sk, skb); | |
1624 | } | |
da882c1f ED |
1625 | } else if (unlikely(sk_add_backlog(sk, skb, |
1626 | sk->sk_rcvbuf + sk->sk_sndbuf))) { | |
6b03a53a | 1627 | bh_unlock_sock(sk); |
6cce09f8 | 1628 | NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP); |
6b03a53a ZY |
1629 | goto discard_and_relse; |
1630 | } | |
1da177e4 LT |
1631 | bh_unlock_sock(sk); |
1632 | ||
1633 | sock_put(sk); | |
1634 | return ret ? -1 : 0; | |
1635 | ||
1636 | no_tcp_socket: | |
1637 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) | |
1638 | goto discard_it; | |
1639 | ||
1640 | if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) { | |
1641 | bad_packet: | |
63231bdd | 1642 | TCP_INC_STATS_BH(net, TCP_MIB_INERRS); |
1da177e4 | 1643 | } else { |
cfb6eeb4 | 1644 | tcp_v6_send_reset(NULL, skb); |
1da177e4 LT |
1645 | } |
1646 | ||
1647 | discard_it: | |
1648 | ||
1649 | /* | |
1650 | * Discard frame | |
1651 | */ | |
1652 | ||
1653 | kfree_skb(skb); | |
1654 | return 0; | |
1655 | ||
1656 | discard_and_relse: | |
1657 | sock_put(sk); | |
1658 | goto discard_it; | |
1659 | ||
1660 | do_time_wait: | |
1661 | if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) { | |
9469c7b4 | 1662 | inet_twsk_put(inet_twsk(sk)); |
1da177e4 LT |
1663 | goto discard_it; |
1664 | } | |
1665 | ||
1666 | if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) { | |
63231bdd | 1667 | TCP_INC_STATS_BH(net, TCP_MIB_INERRS); |
9469c7b4 | 1668 | inet_twsk_put(inet_twsk(sk)); |
1da177e4 LT |
1669 | goto discard_it; |
1670 | } | |
1671 | ||
9469c7b4 | 1672 | switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) { |
1da177e4 LT |
1673 | case TCP_TW_SYN: |
1674 | { | |
1675 | struct sock *sk2; | |
1676 | ||
c346dca1 | 1677 | sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo, |
0660e03f | 1678 | &ipv6_hdr(skb)->daddr, |
505cbfc5 | 1679 | ntohs(th->dest), inet6_iif(skb)); |
1da177e4 | 1680 | if (sk2 != NULL) { |
295ff7ed ACM |
1681 | struct inet_timewait_sock *tw = inet_twsk(sk); |
1682 | inet_twsk_deschedule(tw, &tcp_death_row); | |
1683 | inet_twsk_put(tw); | |
1da177e4 LT |
1684 | sk = sk2; |
1685 | goto process; | |
1686 | } | |
1687 | /* Fall through to ACK */ | |
1688 | } | |
1689 | case TCP_TW_ACK: | |
1690 | tcp_v6_timewait_ack(sk, skb); | |
1691 | break; | |
1692 | case TCP_TW_RST: | |
1693 | goto no_tcp_socket; | |
1694 | case TCP_TW_SUCCESS:; | |
1695 | } | |
1696 | goto discard_it; | |
1697 | } | |
1698 | ||
ccb7c410 DM |
1699 | static struct timewait_sock_ops tcp6_timewait_sock_ops = { |
1700 | .twsk_obj_size = sizeof(struct tcp6_timewait_sock), | |
1701 | .twsk_unique = tcp_twsk_unique, | |
1702 | .twsk_destructor= tcp_twsk_destructor, | |
ccb7c410 DM |
1703 | }; |
1704 | ||
3b401a81 | 1705 | static const struct inet_connection_sock_af_ops ipv6_specific = { |
543d9cfe ACM |
1706 | .queue_xmit = inet6_csk_xmit, |
1707 | .send_check = tcp_v6_send_check, | |
1708 | .rebuild_header = inet6_sk_rebuild_header, | |
1709 | .conn_request = tcp_v6_conn_request, | |
1710 | .syn_recv_sock = tcp_v6_syn_recv_sock, | |
543d9cfe | 1711 | .net_header_len = sizeof(struct ipv6hdr), |
67469601 | 1712 | .net_frag_header_len = sizeof(struct frag_hdr), |
543d9cfe ACM |
1713 | .setsockopt = ipv6_setsockopt, |
1714 | .getsockopt = ipv6_getsockopt, | |
1715 | .addr2sockaddr = inet6_csk_addr2sockaddr, | |
1716 | .sockaddr_len = sizeof(struct sockaddr_in6), | |
ab1e0a13 | 1717 | .bind_conflict = inet6_csk_bind_conflict, |
3fdadf7d | 1718 | #ifdef CONFIG_COMPAT |
543d9cfe ACM |
1719 | .compat_setsockopt = compat_ipv6_setsockopt, |
1720 | .compat_getsockopt = compat_ipv6_getsockopt, | |
3fdadf7d | 1721 | #endif |
1da177e4 LT |
1722 | }; |
1723 | ||
cfb6eeb4 | 1724 | #ifdef CONFIG_TCP_MD5SIG |
b2e4b3de | 1725 | static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = { |
cfb6eeb4 | 1726 | .md5_lookup = tcp_v6_md5_lookup, |
49a72dfb | 1727 | .calc_md5_hash = tcp_v6_md5_hash_skb, |
cfb6eeb4 | 1728 | .md5_parse = tcp_v6_parse_md5_keys, |
cfb6eeb4 | 1729 | }; |
a928630a | 1730 | #endif |
cfb6eeb4 | 1731 | |
1da177e4 LT |
1732 | /* |
1733 | * TCP over IPv4 via INET6 API | |
1734 | */ | |
1735 | ||
3b401a81 | 1736 | static const struct inet_connection_sock_af_ops ipv6_mapped = { |
543d9cfe ACM |
1737 | .queue_xmit = ip_queue_xmit, |
1738 | .send_check = tcp_v4_send_check, | |
1739 | .rebuild_header = inet_sk_rebuild_header, | |
1740 | .conn_request = tcp_v6_conn_request, | |
1741 | .syn_recv_sock = tcp_v6_syn_recv_sock, | |
543d9cfe ACM |
1742 | .net_header_len = sizeof(struct iphdr), |
1743 | .setsockopt = ipv6_setsockopt, | |
1744 | .getsockopt = ipv6_getsockopt, | |
1745 | .addr2sockaddr = inet6_csk_addr2sockaddr, | |
1746 | .sockaddr_len = sizeof(struct sockaddr_in6), | |
ab1e0a13 | 1747 | .bind_conflict = inet6_csk_bind_conflict, |
3fdadf7d | 1748 | #ifdef CONFIG_COMPAT |
543d9cfe ACM |
1749 | .compat_setsockopt = compat_ipv6_setsockopt, |
1750 | .compat_getsockopt = compat_ipv6_getsockopt, | |
3fdadf7d | 1751 | #endif |
1da177e4 LT |
1752 | }; |
1753 | ||
cfb6eeb4 | 1754 | #ifdef CONFIG_TCP_MD5SIG |
b2e4b3de | 1755 | static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = { |
cfb6eeb4 | 1756 | .md5_lookup = tcp_v4_md5_lookup, |
49a72dfb | 1757 | .calc_md5_hash = tcp_v4_md5_hash_skb, |
cfb6eeb4 | 1758 | .md5_parse = tcp_v6_parse_md5_keys, |
cfb6eeb4 | 1759 | }; |
a928630a | 1760 | #endif |
cfb6eeb4 | 1761 | |
1da177e4 LT |
1762 | /* NOTE: A lot of things set to zero explicitly by call to |
1763 | * sk_alloc() so need not be done here. | |
1764 | */ | |
1765 | static int tcp_v6_init_sock(struct sock *sk) | |
1766 | { | |
6687e988 | 1767 | struct inet_connection_sock *icsk = inet_csk(sk); |
1da177e4 | 1768 | |
900f65d3 | 1769 | tcp_init_sock(sk); |
1da177e4 | 1770 | |
8292a17a | 1771 | icsk->icsk_af_ops = &ipv6_specific; |
1da177e4 | 1772 | |
cfb6eeb4 | 1773 | #ifdef CONFIG_TCP_MD5SIG |
ac807fa8 | 1774 | tcp_sk(sk)->af_specific = &tcp_sock_ipv6_specific; |
cfb6eeb4 YH |
1775 | #endif |
1776 | ||
1da177e4 LT |
1777 | return 0; |
1778 | } | |
1779 | ||
7d06b2e0 | 1780 | static void tcp_v6_destroy_sock(struct sock *sk) |
1da177e4 | 1781 | { |
1da177e4 | 1782 | tcp_v4_destroy_sock(sk); |
7d06b2e0 | 1783 | inet6_destroy_sock(sk); |
1da177e4 LT |
1784 | } |
1785 | ||
952a10be | 1786 | #ifdef CONFIG_PROC_FS |
1da177e4 | 1787 | /* Proc filesystem TCPv6 sock list dumping. */ |
1ab1457c | 1788 | static void get_openreq6(struct seq_file *seq, |
cf533ea5 | 1789 | const struct sock *sk, struct request_sock *req, int i, int uid) |
1da177e4 | 1790 | { |
1da177e4 | 1791 | int ttd = req->expires - jiffies; |
b71d1d42 ED |
1792 | const struct in6_addr *src = &inet6_rsk(req)->loc_addr; |
1793 | const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr; | |
1da177e4 LT |
1794 | |
1795 | if (ttd < 0) | |
1796 | ttd = 0; | |
1797 | ||
1da177e4 LT |
1798 | seq_printf(seq, |
1799 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | |
71338aa7 | 1800 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n", |
1da177e4 LT |
1801 | i, |
1802 | src->s6_addr32[0], src->s6_addr32[1], | |
1803 | src->s6_addr32[2], src->s6_addr32[3], | |
fd507037 | 1804 | ntohs(inet_rsk(req)->loc_port), |
1da177e4 LT |
1805 | dest->s6_addr32[0], dest->s6_addr32[1], |
1806 | dest->s6_addr32[2], dest->s6_addr32[3], | |
2e6599cb | 1807 | ntohs(inet_rsk(req)->rmt_port), |
1da177e4 LT |
1808 | TCP_SYN_RECV, |
1809 | 0,0, /* could print option size, but that is af dependent. */ | |
1ab1457c YH |
1810 | 1, /* timers active (only the expire timer) */ |
1811 | jiffies_to_clock_t(ttd), | |
1da177e4 LT |
1812 | req->retrans, |
1813 | uid, | |
1ab1457c | 1814 | 0, /* non standard timer */ |
1da177e4 LT |
1815 | 0, /* open_requests have no inode */ |
1816 | 0, req); | |
1817 | } | |
1818 | ||
1819 | static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i) | |
1820 | { | |
b71d1d42 | 1821 | const struct in6_addr *dest, *src; |
1da177e4 LT |
1822 | __u16 destp, srcp; |
1823 | int timer_active; | |
1824 | unsigned long timer_expires; | |
cf533ea5 ED |
1825 | const struct inet_sock *inet = inet_sk(sp); |
1826 | const struct tcp_sock *tp = tcp_sk(sp); | |
463c84b9 | 1827 | const struct inet_connection_sock *icsk = inet_csk(sp); |
cf533ea5 | 1828 | const struct ipv6_pinfo *np = inet6_sk(sp); |
1da177e4 LT |
1829 | |
1830 | dest = &np->daddr; | |
1831 | src = &np->rcv_saddr; | |
c720c7e8 ED |
1832 | destp = ntohs(inet->inet_dport); |
1833 | srcp = ntohs(inet->inet_sport); | |
463c84b9 ACM |
1834 | |
1835 | if (icsk->icsk_pending == ICSK_TIME_RETRANS) { | |
1da177e4 | 1836 | timer_active = 1; |
463c84b9 ACM |
1837 | timer_expires = icsk->icsk_timeout; |
1838 | } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) { | |
1da177e4 | 1839 | timer_active = 4; |
463c84b9 | 1840 | timer_expires = icsk->icsk_timeout; |
1da177e4 LT |
1841 | } else if (timer_pending(&sp->sk_timer)) { |
1842 | timer_active = 2; | |
1843 | timer_expires = sp->sk_timer.expires; | |
1844 | } else { | |
1845 | timer_active = 0; | |
1846 | timer_expires = jiffies; | |
1847 | } | |
1848 | ||
1849 | seq_printf(seq, | |
1850 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | |
71338aa7 | 1851 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n", |
1da177e4 LT |
1852 | i, |
1853 | src->s6_addr32[0], src->s6_addr32[1], | |
1854 | src->s6_addr32[2], src->s6_addr32[3], srcp, | |
1855 | dest->s6_addr32[0], dest->s6_addr32[1], | |
1856 | dest->s6_addr32[2], dest->s6_addr32[3], destp, | |
1ab1457c | 1857 | sp->sk_state, |
47da8ee6 SS |
1858 | tp->write_seq-tp->snd_una, |
1859 | (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq), | |
1da177e4 LT |
1860 | timer_active, |
1861 | jiffies_to_clock_t(timer_expires - jiffies), | |
463c84b9 | 1862 | icsk->icsk_retransmits, |
1da177e4 | 1863 | sock_i_uid(sp), |
6687e988 | 1864 | icsk->icsk_probes_out, |
1da177e4 LT |
1865 | sock_i_ino(sp), |
1866 | atomic_read(&sp->sk_refcnt), sp, | |
7be87351 SH |
1867 | jiffies_to_clock_t(icsk->icsk_rto), |
1868 | jiffies_to_clock_t(icsk->icsk_ack.ato), | |
463c84b9 | 1869 | (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong, |
0b6a05c1 IJ |
1870 | tp->snd_cwnd, |
1871 | tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh | |
1da177e4 LT |
1872 | ); |
1873 | } | |
1874 | ||
1ab1457c | 1875 | static void get_timewait6_sock(struct seq_file *seq, |
8feaf0c0 | 1876 | struct inet_timewait_sock *tw, int i) |
1da177e4 | 1877 | { |
b71d1d42 | 1878 | const struct in6_addr *dest, *src; |
1da177e4 | 1879 | __u16 destp, srcp; |
cf533ea5 | 1880 | const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw); |
1da177e4 LT |
1881 | int ttd = tw->tw_ttd - jiffies; |
1882 | ||
1883 | if (ttd < 0) | |
1884 | ttd = 0; | |
1885 | ||
0fa1a53e ACM |
1886 | dest = &tw6->tw_v6_daddr; |
1887 | src = &tw6->tw_v6_rcv_saddr; | |
1da177e4 LT |
1888 | destp = ntohs(tw->tw_dport); |
1889 | srcp = ntohs(tw->tw_sport); | |
1890 | ||
1891 | seq_printf(seq, | |
1892 | "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X " | |
71338aa7 | 1893 | "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n", |
1da177e4 LT |
1894 | i, |
1895 | src->s6_addr32[0], src->s6_addr32[1], | |
1896 | src->s6_addr32[2], src->s6_addr32[3], srcp, | |
1897 | dest->s6_addr32[0], dest->s6_addr32[1], | |
1898 | dest->s6_addr32[2], dest->s6_addr32[3], destp, | |
1899 | tw->tw_substate, 0, 0, | |
1900 | 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0, | |
1901 | atomic_read(&tw->tw_refcnt), tw); | |
1902 | } | |
1903 | ||
1da177e4 LT |
1904 | static int tcp6_seq_show(struct seq_file *seq, void *v) |
1905 | { | |
1906 | struct tcp_iter_state *st; | |
1907 | ||
1908 | if (v == SEQ_START_TOKEN) { | |
1909 | seq_puts(seq, | |
1910 | " sl " | |
1911 | "local_address " | |
1912 | "remote_address " | |
1913 | "st tx_queue rx_queue tr tm->when retrnsmt" | |
1914 | " uid timeout inode\n"); | |
1915 | goto out; | |
1916 | } | |
1917 | st = seq->private; | |
1918 | ||
1919 | switch (st->state) { | |
1920 | case TCP_SEQ_STATE_LISTENING: | |
1921 | case TCP_SEQ_STATE_ESTABLISHED: | |
1922 | get_tcp6_sock(seq, v, st->num); | |
1923 | break; | |
1924 | case TCP_SEQ_STATE_OPENREQ: | |
1925 | get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid); | |
1926 | break; | |
1927 | case TCP_SEQ_STATE_TIME_WAIT: | |
1928 | get_timewait6_sock(seq, v, st->num); | |
1929 | break; | |
1930 | } | |
1931 | out: | |
1932 | return 0; | |
1933 | } | |
1934 | ||
73cb88ec AV |
1935 | static const struct file_operations tcp6_afinfo_seq_fops = { |
1936 | .owner = THIS_MODULE, | |
1937 | .open = tcp_seq_open, | |
1938 | .read = seq_read, | |
1939 | .llseek = seq_lseek, | |
1940 | .release = seq_release_net | |
1941 | }; | |
1942 | ||
1da177e4 | 1943 | static struct tcp_seq_afinfo tcp6_seq_afinfo = { |
1da177e4 LT |
1944 | .name = "tcp6", |
1945 | .family = AF_INET6, | |
73cb88ec | 1946 | .seq_fops = &tcp6_afinfo_seq_fops, |
9427c4b3 DL |
1947 | .seq_ops = { |
1948 | .show = tcp6_seq_show, | |
1949 | }, | |
1da177e4 LT |
1950 | }; |
1951 | ||
2c8c1e72 | 1952 | int __net_init tcp6_proc_init(struct net *net) |
1da177e4 | 1953 | { |
6f8b13bc | 1954 | return tcp_proc_register(net, &tcp6_seq_afinfo); |
1da177e4 LT |
1955 | } |
1956 | ||
6f8b13bc | 1957 | void tcp6_proc_exit(struct net *net) |
1da177e4 | 1958 | { |
6f8b13bc | 1959 | tcp_proc_unregister(net, &tcp6_seq_afinfo); |
1da177e4 LT |
1960 | } |
1961 | #endif | |
1962 | ||
1963 | struct proto tcpv6_prot = { | |
1964 | .name = "TCPv6", | |
1965 | .owner = THIS_MODULE, | |
1966 | .close = tcp_close, | |
1967 | .connect = tcp_v6_connect, | |
1968 | .disconnect = tcp_disconnect, | |
463c84b9 | 1969 | .accept = inet_csk_accept, |
1da177e4 LT |
1970 | .ioctl = tcp_ioctl, |
1971 | .init = tcp_v6_init_sock, | |
1972 | .destroy = tcp_v6_destroy_sock, | |
1973 | .shutdown = tcp_shutdown, | |
1974 | .setsockopt = tcp_setsockopt, | |
1975 | .getsockopt = tcp_getsockopt, | |
1da177e4 | 1976 | .recvmsg = tcp_recvmsg, |
7ba42910 CG |
1977 | .sendmsg = tcp_sendmsg, |
1978 | .sendpage = tcp_sendpage, | |
1da177e4 | 1979 | .backlog_rcv = tcp_v6_do_rcv, |
46d3ceab | 1980 | .release_cb = tcp_release_cb, |
1da177e4 | 1981 | .hash = tcp_v6_hash, |
ab1e0a13 ACM |
1982 | .unhash = inet_unhash, |
1983 | .get_port = inet_csk_get_port, | |
1da177e4 LT |
1984 | .enter_memory_pressure = tcp_enter_memory_pressure, |
1985 | .sockets_allocated = &tcp_sockets_allocated, | |
1986 | .memory_allocated = &tcp_memory_allocated, | |
1987 | .memory_pressure = &tcp_memory_pressure, | |
0a5578cf | 1988 | .orphan_count = &tcp_orphan_count, |
1da177e4 LT |
1989 | .sysctl_wmem = sysctl_tcp_wmem, |
1990 | .sysctl_rmem = sysctl_tcp_rmem, | |
1991 | .max_header = MAX_TCP_HEADER, | |
1992 | .obj_size = sizeof(struct tcp6_sock), | |
3ab5aee7 | 1993 | .slab_flags = SLAB_DESTROY_BY_RCU, |
6d6ee43e | 1994 | .twsk_prot = &tcp6_timewait_sock_ops, |
60236fdd | 1995 | .rsk_prot = &tcp6_request_sock_ops, |
39d8cda7 | 1996 | .h.hashinfo = &tcp_hashinfo, |
7ba42910 | 1997 | .no_autobind = true, |
543d9cfe ACM |
1998 | #ifdef CONFIG_COMPAT |
1999 | .compat_setsockopt = compat_tcp_setsockopt, | |
2000 | .compat_getsockopt = compat_tcp_getsockopt, | |
2001 | #endif | |
d1a4c0b3 GC |
2002 | #ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM |
2003 | .proto_cgroup = tcp_proto_cgroup, | |
2004 | #endif | |
1da177e4 LT |
2005 | }; |
2006 | ||
41135cc8 | 2007 | static const struct inet6_protocol tcpv6_protocol = { |
1da177e4 LT |
2008 | .handler = tcp_v6_rcv, |
2009 | .err_handler = tcp_v6_err, | |
a430a43d | 2010 | .gso_send_check = tcp_v6_gso_send_check, |
adcfc7d0 | 2011 | .gso_segment = tcp_tso_segment, |
684f2176 HX |
2012 | .gro_receive = tcp6_gro_receive, |
2013 | .gro_complete = tcp6_gro_complete, | |
1da177e4 LT |
2014 | .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL, |
2015 | }; | |
2016 | ||
1da177e4 LT |
2017 | static struct inet_protosw tcpv6_protosw = { |
2018 | .type = SOCK_STREAM, | |
2019 | .protocol = IPPROTO_TCP, | |
2020 | .prot = &tcpv6_prot, | |
2021 | .ops = &inet6_stream_ops, | |
1da177e4 | 2022 | .no_check = 0, |
d83d8461 ACM |
2023 | .flags = INET_PROTOSW_PERMANENT | |
2024 | INET_PROTOSW_ICSK, | |
1da177e4 LT |
2025 | }; |
2026 | ||
2c8c1e72 | 2027 | static int __net_init tcpv6_net_init(struct net *net) |
93ec926b | 2028 | { |
5677242f DL |
2029 | return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6, |
2030 | SOCK_RAW, IPPROTO_TCP, net); | |
93ec926b DL |
2031 | } |
2032 | ||
2c8c1e72 | 2033 | static void __net_exit tcpv6_net_exit(struct net *net) |
93ec926b | 2034 | { |
5677242f | 2035 | inet_ctl_sock_destroy(net->ipv6.tcp_sk); |
b099ce26 EB |
2036 | } |
2037 | ||
2c8c1e72 | 2038 | static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list) |
b099ce26 EB |
2039 | { |
2040 | inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6); | |
93ec926b DL |
2041 | } |
2042 | ||
2043 | static struct pernet_operations tcpv6_net_ops = { | |
b099ce26 EB |
2044 | .init = tcpv6_net_init, |
2045 | .exit = tcpv6_net_exit, | |
2046 | .exit_batch = tcpv6_net_exit_batch, | |
93ec926b DL |
2047 | }; |
2048 | ||
7f4e4868 | 2049 | int __init tcpv6_init(void) |
1da177e4 | 2050 | { |
7f4e4868 DL |
2051 | int ret; |
2052 | ||
2053 | ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP); | |
2054 | if (ret) | |
2055 | goto out; | |
2056 | ||
1da177e4 | 2057 | /* register inet6 protocol */ |
7f4e4868 DL |
2058 | ret = inet6_register_protosw(&tcpv6_protosw); |
2059 | if (ret) | |
2060 | goto out_tcpv6_protocol; | |
2061 | ||
93ec926b | 2062 | ret = register_pernet_subsys(&tcpv6_net_ops); |
7f4e4868 DL |
2063 | if (ret) |
2064 | goto out_tcpv6_protosw; | |
2065 | out: | |
2066 | return ret; | |
ae0f7d5f | 2067 | |
7f4e4868 DL |
2068 | out_tcpv6_protocol: |
2069 | inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP); | |
2070 | out_tcpv6_protosw: | |
2071 | inet6_unregister_protosw(&tcpv6_protosw); | |
2072 | goto out; | |
2073 | } | |
2074 | ||
09f7709f | 2075 | void tcpv6_exit(void) |
7f4e4868 | 2076 | { |
93ec926b | 2077 | unregister_pernet_subsys(&tcpv6_net_ops); |
7f4e4868 DL |
2078 | inet6_unregister_protosw(&tcpv6_protosw); |
2079 | inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP); | |
1da177e4 | 2080 | } |