blob: 7249e4bb9b8a7441ef490302d5e3f70180560816 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * TCP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09008 * Based on:
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Herbert Xueb4dea52008-12-29 23:04:08 -080026#include <linux/bottom_half.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090041#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
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>
Arnaldo Carvalho de Melo5324a042005-08-12 09:26:18 -030049#include <net/inet6_hashtables.h>
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -080050#include <net/inet6_connection_sock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070059#include <net/snmp.h>
60#include <net/dsfield.h>
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -080061#include <net/timewait_sock.h>
Jeff Garzik18134be2007-10-26 22:53:14 -070062#include <net/netdma.h>
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -070063#include <net/inet_common.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070064#include <net/secure_seq.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000065#include <net/tcp_memcontrol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67#include <asm/uaccess.h>
68
69#include <linux/proc_fs.h>
70#include <linux/seq_file.h>
71
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080072#include <linux/crypto.h>
73#include <linux/scatterlist.h>
74
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080075static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
Gui Jianfeng6edafaa2008-08-06 23:50:04 -070076static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
77 struct request_sock *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
Herbert Xu8ad50d92010-04-11 02:15:54 +000080static void __tcp_v6_send_check(struct sk_buff *skb,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000081 const struct in6_addr *saddr,
82 const struct in6_addr *daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Stephen Hemminger3b401a82009-09-01 19:25:04 +000084static const struct inet_connection_sock_af_ops ipv6_mapped;
85static const struct inet_connection_sock_af_ops ipv6_specific;
David S. Millera9286302006-11-14 19:53:22 -080086#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +000087static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
88static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +090089#else
90static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000091 const struct in6_addr *addr)
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +090092{
93 return NULL;
94}
David S. Millera9286302006-11-14 19:53:22 -080095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static void tcp_v6_hash(struct sock *sk)
98{
99 if (sk->sk_state != TCP_CLOSE) {
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800100 if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 tcp_prot.hash(sk);
102 return;
103 }
104 local_bh_disable();
Eric Dumazet9327f702009-12-04 03:46:54 +0000105 __inet6_hash(sk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 local_bh_enable();
107 }
108}
109
Herbert Xu684f2172009-01-08 10:41:23 -0800110static __inline__ __sum16 tcp_v6_check(int len,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000111 const struct in6_addr *saddr,
112 const struct in6_addr *daddr,
Al Viro868c86b2006-11-14 21:35:48 -0800113 __wsum base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
116}
117
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400118static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700120 return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
121 ipv6_hdr(skb)->saddr.s6_addr32,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700122 tcp_hdr(skb)->dest,
123 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900126static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 int addr_len)
128{
129 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900130 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800131 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct ipv6_pinfo *np = inet6_sk(sk);
133 struct tcp_sock *tp = tcp_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000134 struct in6_addr *saddr = NULL, *final_p, final;
David S. Miller493f3772010-12-02 12:14:29 -0800135 struct rt6_info *rt;
David S. Miller4c9483b2011-03-12 16:22:43 -0500136 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct dst_entry *dst;
138 int addr_type;
139 int err;
140
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900141 if (addr_len < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EINVAL;
143
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900144 if (usin->sin6_family != AF_INET6)
Eric Dumazeta02cec22010-09-22 20:43:57 +0000145 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
David S. Miller4c9483b2011-03-12 16:22:43 -0500147 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (np->sndflow) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500150 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
151 IP6_ECN_flow_init(fl6.flowlabel);
152 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 struct ip6_flowlabel *flowlabel;
David S. Miller4c9483b2011-03-12 16:22:43 -0500154 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 if (flowlabel == NULL)
156 return -EINVAL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000157 usin->sin6_addr = flowlabel->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 fl6_sock_release(flowlabel);
159 }
160 }
161
162 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900163 * 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
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
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000199 np->daddr = usin->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500200 np->flow_label = fl6.flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 /*
203 * TCP over IPv4
204 */
205
206 if (addr_type == IPV6_ADDR_MAPPED) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800207 u32 exthdrlen = icsk->icsk_ext_hdr_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 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
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800219 icsk->icsk_af_ops = &ipv6_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 sk->sk_backlog_rcv = tcp_v4_do_rcv;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800221#ifdef CONFIG_TCP_MD5SIG
222 tp->af_specific = &tcp_sock_ipv6_mapped_specific;
223#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
226
227 if (err) {
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800228 icsk->icsk_ext_hdr_len = exthdrlen;
229 icsk->icsk_af_ops = &ipv6_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 sk->sk_backlog_rcv = tcp_v6_do_rcv;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800231#ifdef CONFIG_TCP_MD5SIG
232 tp->af_specific = &tcp_sock_ipv6_specific;
233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 goto failure;
235 } else {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000236 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
237 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
238 &np->rcv_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 return err;
242 }
243
244 if (!ipv6_addr_any(&np->rcv_saddr))
245 saddr = &np->rcv_saddr;
246
David S. Miller4c9483b2011-03-12 16:22:43 -0500247 fl6.flowi6_proto = IPPROTO_TCP;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000248 fl6.daddr = np->daddr;
249 fl6.saddr = saddr ? *saddr : np->saddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500250 fl6.flowi6_oif = sk->sk_bound_dev_if;
251 fl6.flowi6_mark = sk->sk_mark;
David S. Miller1958b852011-03-12 16:36:19 -0500252 fl6.fl6_dport = usin->sin6_port;
253 fl6.fl6_sport = inet->inet_sport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
David S. Miller4c9483b2011-03-12 16:22:43 -0500255 final_p = fl6_update_dst(&fl6, np->opt, &final);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256
David S. Miller4c9483b2011-03-12 16:22:43 -0500257 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700258
David S. Miller4c9483b2011-03-12 16:22:43 -0500259 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800260 if (IS_ERR(dst)) {
261 err = PTR_ERR(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 goto failure;
David S. Miller14e50e52007-05-24 18:17:54 -0700263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (saddr == NULL) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500266 saddr = &fl6.saddr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000267 np->rcv_saddr = *saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
270 /* set the source address */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000271 np->saddr = *saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000272 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Herbert Xuf83ef8c2006-06-30 13:37:03 -0700274 sk->sk_gso_type = SKB_GSO_TCPV6;
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700275 __ip6_dst_store(sk, dst, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
David S. Miller493f3772010-12-02 12:14:29 -0800277 rt = (struct rt6_info *) dst;
278 if (tcp_death_row.sysctl_tw_recycle &&
279 !tp->rx_opt.ts_recent_stamp &&
David S. Miller81166dd2012-07-10 03:14:24 -0700280 ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr))
281 tcp_fetch_timewait_stamp(sk, dst);
David S. Miller493f3772010-12-02 12:14:29 -0800282
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800283 icsk->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (np->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800285 icsk->icsk_ext_hdr_len = (np->opt->opt_flen +
286 np->opt->opt_nflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
288 tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
289
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000290 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 tcp_set_state(sk, TCP_SYN_SENT);
Arnaldo Carvalho de Melod8313f52005-12-13 23:25:44 -0800293 err = inet6_hash_connect(&tcp_death_row, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 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,
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000300 inet->inet_sport,
301 inet->inet_dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 err = tcp_connect(sk);
304 if (err)
305 goto late_failure;
306
307 return 0;
308
309late_failure:
310 tcp_set_state(sk, TCP_CLOSE);
311 __sk_dst_reset(sk);
312failure:
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000313 inet->inet_dport = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 sk->sk_route_caps = 0;
315 return err;
316}
317
318static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700319 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000321 const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300322 const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 struct ipv6_pinfo *np;
324 struct sock *sk;
325 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900326 struct tcp_sock *tp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 __u32 seq;
Pavel Emelyanovca12a1a2008-07-16 20:28:42 -0700328 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Pavel Emelyanovca12a1a2008-07-16 20:28:42 -0700330 sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -0800331 th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 if (sk == NULL) {
Denis V. Luneve41b5362008-10-08 10:33:26 -0700334 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
335 ICMP6_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return;
337 }
338
339 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700340 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 return;
342 }
343
344 bh_lock_sock(sk);
345 if (sock_owned_by_user(sk))
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700346 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (sk->sk_state == TCP_CLOSE)
349 goto out;
350
Stephen Hemmingere802af92010-04-22 15:24:53 -0700351 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 tp = tcp_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900357 seq = ntohl(th->seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (sk->sk_state != TCP_LISTEN &&
359 !between(seq, tp->snd_una, tp->snd_nxt)) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700360 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 goto out;
362 }
363
364 np = inet6_sk(sk);
365
David S. Millerec18d9a2012-07-12 00:25:15 -0700366 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (type == ICMPV6_PKT_TOOBIG) {
David S. Miller68d0c6d2011-03-01 13:19:07 -0800374 struct dst_entry *dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375
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);
David S. Miller4c9483b2011-03-12 16:22:43 -0500386 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
388 /* BUGGG_FUTURE: Again, it is not clear how
389 to handle rthdr case. Ignore this complexity
390 for now.
391 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500392 memset(&fl6, 0, sizeof(fl6));
393 fl6.flowi6_proto = IPPROTO_TCP;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000394 fl6.daddr = np->daddr;
395 fl6.saddr = np->saddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500396 fl6.flowi6_oif = sk->sk_bound_dev_if;
397 fl6.flowi6_mark = sk->sk_mark;
David S. Miller1958b852011-03-12 16:36:19 -0500398 fl6.fl6_dport = inet->inet_dport;
399 fl6.fl6_sport = inet->inet_sport;
David S. Miller4c9483b2011-03-12 16:22:43 -0500400 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
David S. Miller4c9483b2011-03-12 16:22:43 -0500402 dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800403 if (IS_ERR(dst)) {
404 sk->sk_err_soft = -PTR_ERR(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 goto out;
406 }
407
408 } else
409 dst_hold(dst);
410
David S. Miller81aded22012-06-15 14:54:11 -0700411 dst->ops->update_pmtu(dst, ntohl(info));
412
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800413 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 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
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700423 /* Might be for an request_sock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700425 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 case TCP_LISTEN:
427 if (sock_owned_by_user(sk))
428 goto out;
429
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -0800430 req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
431 &hdr->saddr, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 if (!req)
433 goto out;
434
435 /* ICMPs are not backlogged, hence we cannot get
436 * an established socket here.
437 */
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700438 WARN_ON(req->sk != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700440 if (seq != tcp_rsk(req)->snt_isn) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700441 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 goto out;
443 }
444
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700445 inet_csk_reqsk_queue_drop(sk, req, prev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 goto out;
447
448 case TCP_SYN_SENT:
449 case TCP_SYN_RECV: /* Cannot happen.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900450 It can, it SYNs are crossed. --ANK */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!sock_owned_by_user(sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 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
467out:
468 bh_unlock_sock(sk);
469 sock_put(sk);
470}
471
472
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000473static int tcp_v6_send_synack(struct sock *sk, struct dst_entry *dst,
474 struct flowi6 *fl6,
Neal Cardwell3840a062012-06-28 12:34:19 +0000475 struct request_sock *req,
Eric Dumazetfff32692012-06-01 01:47:50 +0000476 struct request_values *rvp,
477 u16 queue_mapping)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800479 struct inet6_request_sock *treq = inet6_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct ipv6_pinfo *np = inet6_sk(sk);
481 struct sk_buff * skb;
Neal Cardwell94942182012-06-28 12:34:20 +0000482 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000484 /* First, grab a route. */
485 if (!dst && (dst = inet6_csk_route_req(sk, fl6, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800486 goto done;
Neal Cardwell94942182012-06-28 12:34:20 +0000487
William Allen Simpsone6b4d112009-12-02 18:07:39 +0000488 skb = tcp_make_synack(sk, dst, req, rvp);
Neal Cardwell94942182012-06-28 12:34:20 +0000489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 if (skb) {
Herbert Xu8ad50d92010-04-11 02:15:54 +0000491 __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000493 fl6->daddr = treq->rmt_addr;
Eric Dumazetfff32692012-06-01 01:47:50 +0000494 skb_set_queue_mapping(skb, queue_mapping);
RongQing.Li43264e02012-07-01 17:18:59 +0000495 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200496 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498
499done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return err;
501}
502
Octavian Purdila72659ec2010-01-17 19:09:39 -0800503static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
504 struct request_values *rvp)
505{
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000506 struct flowi6 fl6;
507
Octavian Purdila72659ec2010-01-17 19:09:39 -0800508 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000509 return tcp_v6_send_synack(sk, NULL, &fl6, req, rvp, 0);
Octavian Purdila72659ec2010-01-17 19:09:39 -0800510}
511
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700512static void tcp_v6_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513{
Wei Yongjun800d55f2009-02-23 21:45:33 +0000514 kfree_skb(inet6_rsk(req)->pktopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515}
516
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800517#ifdef CONFIG_TCP_MD5SIG
518static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000519 const struct in6_addr *addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800520{
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000521 return tcp_md5_do_lookup(sk, (union tcp_md5_addr *)addr, AF_INET6);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800522}
523
524static 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
530static 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
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800536static 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;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800541
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) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700552 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000553 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);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800557 }
558
559 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
560 return -EINVAL;
561
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000562 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);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800565
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000566 return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
567 AF_INET6, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800568}
569
Adam Langley49a72df2008-07-19 00:01:42 -0700570static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000571 const struct in6_addr *daddr,
572 const struct in6_addr *saddr, int nbytes)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800573{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800574 struct tcp6_pseudohdr *bp;
Adam Langley49a72df2008-07-19 00:01:42 -0700575 struct scatterlist sg;
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +0900576
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800577 bp = &hp->md5_blk.ip6;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800578 /* 1. TCP pseudo-header (RFC2460) */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000579 bp->saddr = *saddr;
580 bp->daddr = *daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700581 bp->protocol = cpu_to_be32(IPPROTO_TCP);
Adam Langley00b13042008-07-31 21:36:07 -0700582 bp->len = cpu_to_be32(nbytes);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800583
Adam Langley49a72df2008-07-19 00:01:42 -0700584 sg_init_one(&sg, bp, sizeof(*bp));
585 return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
586}
David S. Millerc7da57a2007-10-26 00:41:21 -0700587
Adam Langley49a72df2008-07-19 00:01:42 -0700588static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000589 const struct in6_addr *daddr, struct in6_addr *saddr,
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400590 const struct tcphdr *th)
Adam Langley49a72df2008-07-19 00:01:42 -0700591{
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))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800609 goto clear_hash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800610
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800611 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800612 return 0;
Adam Langley49a72df2008-07-19 00:01:42 -0700613
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800614clear_hash:
615 tcp_put_md5sig_pool();
616clear_hash_noput:
617 memset(md5_hash, 0, 16);
Adam Langley49a72df2008-07-19 00:01:42 -0700618 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800619}
620
Adam Langley49a72df2008-07-19 00:01:42 -0700621static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400622 const struct sock *sk,
623 const struct request_sock *req,
624 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800625{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000626 const struct in6_addr *saddr, *daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700627 struct tcp_md5sig_pool *hp;
628 struct hash_desc *desc;
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400629 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800630
631 if (sk) {
632 saddr = &inet6_sk(sk)->saddr;
633 daddr = &inet6_sk(sk)->daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700634 } else if (req) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800635 saddr = &inet6_rsk(req)->loc_addr;
636 daddr = &inet6_rsk(req)->rmt_addr;
Adam Langley49a72df2008-07-19 00:01:42 -0700637 } else {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000638 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Adam Langley49a72df2008-07-19 00:01:42 -0700639 saddr = &ip6h->saddr;
640 daddr = &ip6h->daddr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800641 }
Adam Langley49a72df2008-07-19 00:01:42 -0700642
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
665clear_hash:
666 tcp_put_md5sig_pool();
667clear_hash_noput:
668 memset(md5_hash, 0, 16);
669 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800670}
671
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400672static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800673{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400674 const __u8 *hash_location = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800675 struct tcp_md5sig_key *hash_expected;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000676 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400677 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800678 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800679 u8 newhash[16];
680
681 hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +0900682 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800683
David S. Miller785957d2008-07-30 03:03:15 -0700684 /* 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);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800690 return 1;
691 }
692
David S. Miller785957d2008-07-30 03:03:15 -0700693 if (!hash_expected && hash_location) {
694 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800695 return 1;
696 }
697
698 /* check the signature */
Adam Langley49a72df2008-07-19 00:01:42 -0700699 genhash = tcp_v6_md5_hash_skb(newhash,
700 hash_expected,
701 NULL, NULL, skb);
702
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800703 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000704 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));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800708 return 1;
709 }
710 return 0;
711}
712#endif
713
Glenn Griffinc6aefaf2008-02-07 21:49:26 -0800714struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 .family = AF_INET6,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700716 .obj_size = sizeof(struct tcp6_request_sock),
Octavian Purdila72659ec2010-01-17 19:09:39 -0800717 .rtx_syn_ack = tcp_v6_rtx_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700718 .send_ack = tcp_v6_reqsk_send_ack,
719 .destructor = tcp_v6_reqsk_destructor,
Octavian Purdila72659ec2010-01-17 19:09:39 -0800720 .send_reset = tcp_v6_send_reset,
721 .syn_ack_timeout = tcp_syn_ack_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722};
723
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800724#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +0000725static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800726 .md5_lookup = tcp_v6_reqsk_md5_lookup,
John Dykstrae3afe7b2009-07-16 05:04:51 +0000727 .calc_md5_hash = tcp_v6_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800728};
Andrew Mortonb6332e62006-11-30 19:16:28 -0800729#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800730
Herbert Xu8ad50d92010-04-11 02:15:54 +0000731static void __tcp_v6_send_check(struct sk_buff *skb,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000732 const struct in6_addr *saddr, const struct in6_addr *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700734 struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
Patrick McHardy84fa7932006-08-29 16:44:56 -0700736 if (skb->ip_summed == CHECKSUM_PARTIAL) {
Herbert Xu8ad50d92010-04-11 02:15:54 +0000737 th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
Herbert Xu663ead32007-04-09 11:59:07 -0700738 skb->csum_start = skb_transport_header(skb) - skb->head;
Al Viroff1dcad2006-11-20 18:07:29 -0800739 skb->csum_offset = offsetof(struct tcphdr, check);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 } else {
Herbert Xu8ad50d92010-04-11 02:15:54 +0000741 th->check = tcp_v6_check(skb->len, saddr, daddr,
742 csum_partial(th, th->doff << 2,
743 skb->csum));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745}
746
Herbert Xubb296242010-04-11 02:15:55 +0000747static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
Herbert Xu8ad50d92010-04-11 02:15:54 +0000748{
749 struct ipv6_pinfo *np = inet6_sk(sk);
750
751 __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
752}
753
Herbert Xua430a432006-07-08 13:34:56 -0700754static int tcp_v6_gso_send_check(struct sk_buff *skb)
755{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000756 const struct ipv6hdr *ipv6h;
Herbert Xua430a432006-07-08 13:34:56 -0700757 struct tcphdr *th;
758
759 if (!pskb_may_pull(skb, sizeof(*th)))
760 return -EINVAL;
761
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700762 ipv6h = ipv6_hdr(skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700763 th = tcp_hdr(skb);
Herbert Xua430a432006-07-08 13:34:56 -0700764
765 th->check = 0;
Patrick McHardy84fa7932006-08-29 16:44:56 -0700766 skb->ip_summed = CHECKSUM_PARTIAL;
Herbert Xu8ad50d92010-04-11 02:15:54 +0000767 __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
Herbert Xua430a432006-07-08 13:34:56 -0700768 return 0;
769}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Herbert Xu36990672009-05-22 00:45:28 -0700771static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
772 struct sk_buff *skb)
Herbert Xu684f2172009-01-08 10:41:23 -0800773{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000774 const struct ipv6hdr *iph = skb_gro_network_header(skb);
Herbert Xu684f2172009-01-08 10:41:23 -0800775
776 switch (skb->ip_summed) {
777 case CHECKSUM_COMPLETE:
Herbert Xu86911732009-01-29 14:19:50 +0000778 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
Herbert Xu684f2172009-01-08 10:41:23 -0800779 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}
Herbert Xu684f2172009-01-08 10:41:23 -0800792
Herbert Xu36990672009-05-22 00:45:28 -0700793static int tcp6_gro_complete(struct sk_buff *skb)
Herbert Xu684f2172009-01-08 10:41:23 -0800794{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000795 const struct ipv6hdr *iph = ipv6_hdr(skb);
Herbert Xu684f2172009-01-08 10:41:23 -0800796 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}
Herbert Xu684f2172009-01-08 10:41:23 -0800804
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700805static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
Eric Dumazetb903d322011-10-27 00:44:35 -0400806 u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400808 const struct tcphdr *th = tcp_hdr(skb);
809 struct tcphdr *t1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 struct sk_buff *buff;
David S. Miller4c9483b2011-03-12 16:22:43 -0500811 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +0000812 struct net *net = dev_net(skb_dst(skb)->dev);
Daniel Lezcanoe5047992008-03-07 11:16:26 -0800813 struct sock *ctl_sk = net->ipv6.tcp_sk;
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -0800814 unsigned int tot_len = sizeof(struct tcphdr);
Eric Dumazetadf30902009-06-02 05:19:30 +0000815 struct dst_entry *dst;
Al Viroe69a4ad2006-11-14 20:56:00 -0800816 __be32 *topt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (ts)
YOSHIFUJI Hideaki4244f8a2006-10-10 19:40:50 -0700819 tot_len += TCPOLEN_TSTAMP_ALIGNED;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800820#ifdef CONFIG_TCP_MD5SIG
821 if (key)
822 tot_len += TCPOLEN_MD5SIG_ALIGNED;
823#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
825 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
826 GFP_ATOMIC);
827 if (buff == NULL)
828 return;
829
830 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
831
Ilpo Järvinen77c676d2008-10-09 14:41:38 -0700832 t1 = (struct tcphdr *) skb_push(buff, tot_len);
Herbert Xu6651ffc2010-04-21 00:47:15 -0700833 skb_reset_transport_header(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834
835 /* Swap the send and the receive. */
836 memset(t1, 0, sizeof(*t1));
837 t1->dest = th->source;
838 t1->source = th->dest;
Ilpo Järvinen77c676d2008-10-09 14:41:38 -0700839 t1->doff = tot_len / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 t1->seq = htonl(seq);
841 t1->ack_seq = htonl(ack);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700842 t1->ack = !rst || !th->ack;
843 t1->rst = rst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 t1->window = htons(win);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800845
Al Viroe69a4ad2006-11-14 20:56:00 -0800846 topt = (__be32 *)(t1 + 1);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 if (ts) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800849 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
850 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
851 *topt++ = htonl(tcp_time_stamp);
Ilpo Järvinen53b12572008-10-08 14:36:33 -0700852 *topt++ = htonl(ts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 }
854
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800855#ifdef CONFIG_TCP_MD5SIG
856 if (key) {
857 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
858 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
Adam Langley49a72df2008-07-19 00:01:42 -0700859 tcp_v6_md5_hash_hdr((__u8 *)topt, key,
Adam Langley90b7e112008-07-31 20:49:48 -0700860 &ipv6_hdr(skb)->saddr,
861 &ipv6_hdr(skb)->daddr, t1);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800862 }
863#endif
864
David S. Miller4c9483b2011-03-12 16:22:43 -0500865 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000866 fl6.daddr = ipv6_hdr(skb)->saddr;
867 fl6.saddr = ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
David S. Millere5700af2010-04-21 14:59:20 -0700869 buff->ip_summed = CHECKSUM_PARTIAL;
870 buff->csum = 0;
871
David S. Miller4c9483b2011-03-12 16:22:43 -0500872 __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
David S. Miller4c9483b2011-03-12 16:22:43 -0500874 fl6.flowi6_proto = IPPROTO_TCP;
875 fl6.flowi6_oif = inet6_iif(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500876 fl6.fl6_dport = t1->dest;
877 fl6.fl6_sport = t1->source;
David S. Miller4c9483b2011-03-12 16:22:43 -0500878 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700880 /* 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 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500884 dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800885 if (!IS_ERR(dst)) {
886 skb_dst_set(buff, dst);
Eric Dumazetb903d322011-10-27 00:44:35 -0400887 ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800888 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
889 if (rst)
890 TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
891 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 }
893
894 kfree_skb(buff);
895}
896
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700897static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
898{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400899 const struct tcphdr *th = tcp_hdr(skb);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700900 u32 seq = 0, ack_seq = 0;
Guo-Fu Tsengfa3e5b42008-10-09 21:11:56 -0700901 struct tcp_md5sig_key *key = NULL;
Shawn Lu658ddaa2012-01-31 22:35:48 +0000902#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
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700909
910 if (th->rst)
911 return;
912
913 if (!ipv6_unicast_destination(skb))
914 return;
915
916#ifdef CONFIG_TCP_MD5SIG
Shawn Lu658ddaa2012-01-31 22:35:48 +0000917 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 }
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700943#endif
944
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);
950
Eric Dumazetb903d322011-10-27 00:44:35 -0400951 tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
Shawn Lu658ddaa2012-01-31 22:35:48 +0000952
953#ifdef CONFIG_TCP_MD5SIG
954release_sk1:
955 if (sk1) {
956 rcu_read_unlock();
957 sock_put(sk1);
958 }
959#endif
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700960}
961
962static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
Eric Dumazetb903d322011-10-27 00:44:35 -0400963 struct tcp_md5sig_key *key, u8 tclass)
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700964{
Eric Dumazetb903d322011-10-27 00:44:35 -0400965 tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700966}
967
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
969{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700970 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800971 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900973 tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700974 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
Eric Dumazetb903d322011-10-27 00:44:35 -0400975 tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
976 tw->tw_tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700978 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Gui Jianfeng6edafaa2008-08-06 23:50:04 -0700981static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
982 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900984 tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
Eric Dumazetb903d322011-10-27 00:44:35 -0400985 tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988
989static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
990{
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700991 struct request_sock *req, **prev;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700992 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 struct sock *nsk;
994
995 /* Find possible connection requests. */
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -0800996 req = inet6_csk_search_req(sk, &prev, th->source,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700997 &ipv6_hdr(skb)->saddr,
998 &ipv6_hdr(skb)->daddr, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (req)
1000 return tcp_check_req(sk, skb, req, prev);
1001
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001002 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -08001003 &ipv6_hdr(skb)->saddr, th->source,
1004 &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
1006 if (nsk) {
1007 if (nsk->sk_state != TCP_TIME_WAIT) {
1008 bh_lock_sock(nsk);
1009 return nsk;
1010 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001011 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 return NULL;
1013 }
1014
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001015#ifdef CONFIG_SYN_COOKIES
Florian Westphalaf9b4732010-06-03 00:43:44 +00001016 if (!th->syn)
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001017 sk = cookie_v6_check(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018#endif
1019 return sk;
1020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022/* FIXME: this is substantially similar to the ipv4 code.
1023 * Can some kind of merge be done? -- erics
1024 */
1025static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1026{
William Allen Simpson4957faade2009-12-02 18:25:27 +00001027 struct tcp_extend_values tmp_ext;
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001028 struct tcp_options_received tmp_opt;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001029 const u8 *hash_location;
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001030 struct request_sock *req;
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -08001031 struct inet6_request_sock *treq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 struct tcp_sock *tp = tcp_sk(sk);
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001034 __u32 isn = TCP_SKB_CB(skb)->when;
David S. Miller493f3772010-12-02 12:14:29 -08001035 struct dst_entry *dst = NULL;
Neal Cardwell3840a062012-06-28 12:34:19 +00001036 struct flowi6 fl6;
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001037 bool want_cookie = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (skb->protocol == htons(ETH_P_IP))
1040 return tcp_v4_conn_request(sk, skb);
1041
1042 if (!ipv6_unicast_destination(skb))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001043 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001045 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
Eric Dumazet946cedc2011-08-30 03:21:44 +00001046 want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6");
1047 if (!want_cookie)
1048 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
1050
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001051 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 goto drop;
1053
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -08001054 req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 if (req == NULL)
1056 goto drop;
1057
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001058#ifdef CONFIG_TCP_MD5SIG
1059 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
1060#endif
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 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;
David S. Millerbb5b7c12009-12-15 20:56:42 -08001065 tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
William Allen Simpson4957faade2009-12-02 18:25:27 +00001067 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 */
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001082 d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
William Allen Simpson4957faade2009-12-02 18:25:27 +00001083 *mess++ ^= *d++;
1084 *mess++ ^= *d++;
1085 *mess++ ^= *d++;
1086 *mess++ ^= *d++;
Eric Dumazet0eae88f2010-04-20 19:06:52 -07001087 d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
William Allen Simpson4957faade2009-12-02 18:25:27 +00001088 *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++;
1097
Eric Dumazeta2a385d2012-05-16 23:15:34 +00001098 want_cookie = false; /* not our kind of cookie */
William Allen Simpson4957faade2009-12-02 18:25:27 +00001099 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Florian Westphal4dfc2812008-04-10 03:12:40 -07001110 if (want_cookie && !tmp_opt.saw_tstamp)
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001111 tcp_clear_options(&tmp_opt);
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1114 tcp_openreq_init(req, &tmp_opt, skb);
1115
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -08001116 treq = inet6_rsk(req);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001117 treq->rmt_addr = ipv6_hdr(skb)->saddr;
1118 treq->loc_addr = ipv6_hdr(skb)->daddr;
Florian Westphal172d69e2010-06-21 11:48:45 +00001119 if (!want_cookie || tmp_opt.tstamp_ok)
Eric Dumazetbd14b1b2012-05-04 05:14:02 +00001120 TCP_ECN_create_request(req, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Eric Dumazet4d0fe502011-11-23 17:29:23 -05001122 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
Florian Westphal2bbdf382010-06-13 11:29:39 +00001129 if (!isn) {
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001130 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 }
David S. Miller493f3772010-12-02 12:14:29 -08001136
1137 if (want_cookie) {
Florian Westphal2bbdf382010-06-13 11:29:39 +00001138 isn = cookie_v6_init_sequence(sk, skb, &req->mss);
1139 req->cookie_ts = tmp_opt.tstamp_ok;
David S. Miller493f3772010-12-02 12:14:29 -08001140 goto have_isn;
Florian Westphal2bbdf382010-06-13 11:29:39 +00001141 }
David S. Miller493f3772010-12-02 12:14:29 -08001142
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 &&
David S. Miller81166dd2012-07-10 03:14:24 -07001154 (dst = inet6_csk_route_req(sk, &fl6, req)) != NULL) {
1155 if (!tcp_peer_is_proven(req, dst, true)) {
David S. Miller493f3772010-12-02 12:14:29 -08001156 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)) &&
David S. Miller81166dd2012-07-10 03:14:24 -07001164 !tcp_peer_is_proven(req, dst, false)) {
David S. Miller493f3772010-12-02 12:14:29 -08001165 /* 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;
1175 }
1176
1177 isn = tcp_v6_init_sequence(skb);
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001178 }
David S. Miller493f3772010-12-02 12:14:29 -08001179have_isn:
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001180 tcp_rsk(req)->snt_isn = isn;
Jerry Chu9ad7c042011-06-08 11:08:38 +00001181 tcp_rsk(req)->snt_synack = tcp_time_stamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Neal Cardwell437c5b52012-06-23 19:22:00 +00001183 if (security_inet_conn_request(sk, skb, req))
1184 goto drop_and_release;
Venkat Yekkirala4237c752006-07-24 23:32:50 -07001185
Neal Cardwell9f10d3f2012-06-28 12:34:21 +00001186 if (tcp_v6_send_synack(sk, dst, &fl6, req,
Eric Dumazetfff32692012-06-01 01:47:50 +00001187 (struct request_values *)&tmp_ext,
1188 skb_get_queue_mapping(skb)) ||
William Allen Simpson4957faade2009-12-02 18:25:27 +00001189 want_cookie)
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001190 goto drop_and_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001192 inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
1193 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
David S. Miller493f3772010-12-02 12:14:29 -08001195drop_and_release:
1196 dst_release(dst);
William Allen Simpsone6b4d112009-12-02 18:07:39 +00001197drop_and_free:
1198 reqsk_free(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199drop:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 return 0; /* don't send reset */
1201}
1202
1203static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001204 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 struct dst_entry *dst)
1206{
Vegard Nossum78d15e82008-09-12 16:17:43 -07001207 struct inet6_request_sock *treq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 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;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001213#ifdef CONFIG_TCP_MD5SIG
1214 struct tcp_md5sig_key *key;
1215#endif
Neal Cardwell3840a062012-06-28 12:34:19 +00001216 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
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
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001225 if (newsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 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
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001237 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001239 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001241 newnp->rcv_saddr = newnp->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001243 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001245#ifdef CONFIG_TCP_MD5SIG
1246 newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
1247#endif
1248
Yan, Zheng676a1182011-09-25 02:21:30 +00001249 newnp->ipv6_ac_list = NULL;
1250 newnp->ipv6_fl_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 newnp->pktoptions = NULL;
1252 newnp->opt = NULL;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001253 newnp->mcast_oif = inet6_iif(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001254 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
Jiri Benc4c507d22012-02-09 09:35:49 +00001255 newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07001257 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
1263 /* It is tricky place. Until this moment IPv4 tcp
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001264 worked with IPv6 icsk.icsk_af_ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 Sync it now.
1266 */
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001267 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 return newsk;
1270 }
1271
Vegard Nossum78d15e82008-09-12 16:17:43 -07001272 treq = inet6_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
1274 if (sk_acceptq_is_full(sk))
1275 goto out_overflow;
1276
David S. Miller493f3772010-12-02 12:14:29 -08001277 if (!dst) {
Neal Cardwell3840a062012-06-28 12:34:19 +00001278 dst = inet6_csk_route_req(sk, &fl6, req);
David S. Miller493f3772010-12-02 12:14:29 -08001279 if (!dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 newsk = tcp_create_openreq_child(sk, req, skb);
1284 if (newsk == NULL)
Balazs Scheidler093d2822010-10-21 13:06:43 +02001285 goto out_nonewsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07001287 /*
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Stephen Hemminger59eed272006-08-25 15:55:43 -07001293 newsk->sk_gso_type = SKB_GSO_TCPV6;
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001294 __ip6_dst_store(newsk, dst, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
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
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001305 newnp->daddr = treq->rmt_addr;
1306 newnp->saddr = treq->loc_addr;
1307 newnp->rcv_saddr = treq->loc_addr;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001308 newsk->sk_bound_dev_if = treq->iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001310 /* Now IPv6 options...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 First: no IPv4 options.
1313 */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001314 newinet->inet_opt = NULL;
Yan, Zheng676a1182011-09-25 02:21:30 +00001315 newnp->ipv6_ac_list = NULL;
Masayuki Nakagawad35690b2007-03-16 16:14:03 -07001316 newnp->ipv6_fl_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /* Clone RX bits */
1319 newnp->rxopt.all = np->rxopt.all;
1320
1321 /* Clone pktoptions received with SYN */
1322 newnp->pktoptions = NULL;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001323 if (treq->pktopts != NULL) {
1324 newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
Eric Dumazetab185d72012-04-19 02:24:36 +00001325 consume_skb(treq->pktopts);
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001326 treq->pktopts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 if (newnp->pktoptions)
1328 skb_set_owner_r(newnp->pktoptions, newsk);
1329 }
1330 newnp->opt = NULL;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001331 newnp->mcast_oif = inet6_iif(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001332 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
Jiri Benc4c507d22012-02-09 09:35:49 +00001333 newnp->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
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 */
RongQing.Li43264e02012-07-01 17:18:59 +00001341 if (np->opt)
1342 newnp->opt = ipv6_dup_options(newsk, np->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001344 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (newnp->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001346 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
1347 newnp->opt->opt_flen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348
John Heffner5d424d52006-03-20 17:53:41 -08001349 tcp_mtup_init(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 tcp_sync_mss(newsk, dst_mtu(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08001351 newtp->advmss = dst_metric_advmss(dst);
Neal Cardwelld135c522012-04-22 09:45:47 +00001352 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 tcp_initialize_rcv_mss(newsk);
Jerry Chu9ad7c042011-06-08 11:08:38 +00001357 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001362 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1363 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001365#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 */
Eric Dumazeta915da9b2012-01-31 05:18:33 +00001373 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newnp->daddr,
1374 AF_INET6, key->key, key->keylen, GFP_ATOMIC);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001375 }
1376#endif
1377
Balazs Scheidler093d2822010-10-21 13:06:43 +02001378 if (__inet_inherit_port(sk, newsk) < 0) {
1379 sock_put(newsk);
1380 goto out;
1381 }
Eric Dumazet9327f702009-12-04 03:46:54 +00001382 __inet6_hash(newsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 return newsk;
1385
1386out_overflow:
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001387 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001388out_nonewsk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 dst_release(dst);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001390out:
1391 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 return NULL;
1393}
1394
Al Virob51655b2006-11-14 21:40:42 -08001395static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396{
Patrick McHardy84fa7932006-08-29 16:44:56 -07001397 if (skb->ip_summed == CHECKSUM_COMPLETE) {
Herbert Xu684f2172009-01-08 10:41:23 -08001398 if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001399 &ipv6_hdr(skb)->daddr, skb->csum)) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001400 skb->ip_summed = CHECKSUM_UNNECESSARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 return 0;
Herbert Xufb286bb2005-11-10 13:01:24 -08001402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
Herbert Xufb286bb2005-11-10 13:01:24 -08001404
Herbert Xu684f2172009-01-08 10:41:23 -08001405 skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001406 &ipv6_hdr(skb)->saddr,
1407 &ipv6_hdr(skb)->daddr, 0));
Herbert Xufb286bb2005-11-10 13:01:24 -08001408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (skb->len <= 76) {
Herbert Xufb286bb2005-11-10 13:01:24 -08001410 return __skb_checksum_complete(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
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 */
1423static 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
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001440#ifdef CONFIG_TCP_MD5SIG
1441 if (tcp_v6_inbound_md5_hash (sk, skb))
1442 goto discard;
1443#endif
1444
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001445 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 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.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001464 --ANK (980728)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 */
1466 if (np->rxopt.all)
1467 opt_skb = skb_clone(skb, GFP_ATOMIC);
1468
1469 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
Tom Herbertbdeab992011-08-14 19:45:55 +00001470 sock_rps_save_rxhash(sk, skb);
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001471 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 goto reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (opt_skb)
1474 goto ipv6_pktoptions;
1475 return 0;
1476 }
1477
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001478 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 goto csum_err;
1480
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001481 if (sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 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 */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001491 if(nsk != sk) {
Tom Herbertbdeab992011-08-14 19:45:55 +00001492 sock_rps_save_rxhash(nsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 if (tcp_child_process(sk, nsk, skb))
1494 goto reset;
1495 if (opt_skb)
1496 __kfree_skb(opt_skb);
1497 return 0;
1498 }
Neil Horman47482f12011-04-06 13:07:09 -07001499 } else
Tom Herbertbdeab992011-08-14 19:45:55 +00001500 sock_rps_save_rxhash(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001502 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 goto reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (opt_skb)
1505 goto ipv6_pktoptions;
1506 return 0;
1507
1508reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001509 tcp_v6_send_reset(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510discard:
1511 if (opt_skb)
1512 __kfree_skb(opt_skb);
1513 kfree_skb(skb);
1514 return 0;
1515csum_err:
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001516 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 goto discard;
1518
1519
1520ipv6_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))) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001531 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001532 np->mcast_oif = inet6_iif(opt_skb);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001533 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001534 np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
Jiri Benc4c507d22012-02-09 09:35:49 +00001535 if (np->rxopt.bits.rxtclass)
1536 np->rcv_tclass = ipv6_tclass(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 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
Wei Yongjun800d55f2009-02-23 21:45:33 +00001546 kfree_skb(opt_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 return 0;
1548}
1549
Herbert Xue5bbef22007-10-15 12:50:28 -07001550static int tcp_v6_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001552 const struct tcphdr *th;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001553 const struct ipv6hdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 struct sock *sk;
1555 int ret;
Pavel Emelyanova86b1e32008-07-16 20:20:58 -07001556 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (skb->pkt_type != PACKET_HOST)
1559 goto discard_it;
1560
1561 /*
1562 * Count it even if it's bad.
1563 */
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001564 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1567 goto discard_it;
1568
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001569 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
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
Herbert Xu60476372007-04-09 11:59:39 -07001576 if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto bad_packet;
1578
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001579 th = tcp_hdr(skb);
Stephen Hemmingere802af92010-04-22 15:24:53 -07001580 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 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;
Eric Dumazetb82d1bb2011-09-27 02:20:08 -04001586 TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 TCP_SKB_CB(skb)->sacked = 0;
1588
Arnaldo Carvalho de Melo9a1f27c2008-10-07 11:41:57 -07001589 sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 if (!sk)
1591 goto no_tcp_socket;
1592
1593process:
1594 if (sk->sk_state == TCP_TIME_WAIT)
1595 goto do_time_wait;
1596
Stephen Hemmingere802af92010-04-22 15:24:53 -07001597 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1603 goto discard_and_relse;
1604
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001605 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 goto discard_and_relse;
1607
1608 skb->dev = NULL;
1609
Fabio Olive Leite293b9c42006-09-25 22:28:47 -07001610 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 ret = 0;
1612 if (!sock_owned_by_user(sk)) {
Chris Leech1a2449a2006-05-23 18:05:53 -07001613#ifdef CONFIG_NET_DMA
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001614 struct tcp_sock *tp = tcp_sk(sk);
David S. Millerb4caea82007-10-26 04:20:13 -07001615 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
Dave Jianga2bd1142012-04-04 16:10:46 -07001616 tp->ucopy.dma_chan = net_dma_find_channel();
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001617 if (tp->ucopy.dma_chan)
1618 ret = tcp_v6_do_rcv(sk, skb);
1619 else
Chris Leech1a2449a2006-05-23 18:05:53 -07001620#endif
1621 {
1622 if (!tcp_prequeue(sk, skb))
1623 ret = tcp_v6_do_rcv(sk, skb);
1624 }
Eric Dumazetda882c12012-04-22 23:38:54 +00001625 } else if (unlikely(sk_add_backlog(sk, skb,
1626 sk->sk_rcvbuf + sk->sk_sndbuf))) {
Zhu Yi6b03a532010-03-04 18:01:41 +00001627 bh_unlock_sock(sk);
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001628 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
Zhu Yi6b03a532010-03-04 18:01:41 +00001629 goto discard_and_relse;
1630 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 bh_unlock_sock(sk);
1632
1633 sock_put(sk);
1634 return ret ? -1 : 0;
1635
1636no_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)) {
1641bad_packet:
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001642 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001644 tcp_v6_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
1646
1647discard_it:
1648
1649 /*
1650 * Discard frame
1651 */
1652
1653 kfree_skb(skb);
1654 return 0;
1655
1656discard_and_relse:
1657 sock_put(sk);
1658 goto discard_it;
1659
1660do_time_wait:
1661 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001662 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 goto discard_it;
1664 }
1665
1666 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001667 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001668 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 goto discard_it;
1670 }
1671
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001672 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 case TCP_TW_SYN:
1674 {
1675 struct sock *sk2;
1676
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001677 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001678 &ipv6_hdr(skb)->daddr,
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001679 ntohs(th->dest), inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (sk2 != NULL) {
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001681 struct inet_timewait_sock *tw = inet_twsk(sk);
1682 inet_twsk_deschedule(tw, &tcp_death_row);
1683 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 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
David S. Millerccb7c412010-12-01 18:09:13 -08001699static 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,
David S. Millerccb7c412010-12-01 18:09:13 -08001703};
1704
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001705static const struct inet_connection_sock_af_ops ipv6_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001706 .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,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001711 .net_header_len = sizeof(struct ipv6hdr),
Eric Dumazet67469602012-04-24 07:37:38 +00001712 .net_frag_header_len = sizeof(struct frag_hdr),
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001713 .setsockopt = ipv6_setsockopt,
1714 .getsockopt = ipv6_getsockopt,
1715 .addr2sockaddr = inet6_csk_addr2sockaddr,
1716 .sockaddr_len = sizeof(struct sockaddr_in6),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001717 .bind_conflict = inet6_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001718#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001719 .compat_setsockopt = compat_ipv6_setsockopt,
1720 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001721#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722};
1723
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001724#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001725static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001726 .md5_lookup = tcp_v6_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001727 .calc_md5_hash = tcp_v6_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001728 .md5_parse = tcp_v6_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001729};
David S. Millera9286302006-11-14 19:53:22 -08001730#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/*
1733 * TCP over IPv4 via INET6 API
1734 */
1735
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001736static const struct inet_connection_sock_af_ops ipv6_mapped = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001737 .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,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001742 .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),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001747 .bind_conflict = inet6_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001748#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001749 .compat_setsockopt = compat_ipv6_setsockopt,
1750 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001751#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752};
1753
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001754#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001755static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001756 .md5_lookup = tcp_v4_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001757 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001758 .md5_parse = tcp_v6_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001759};
David S. Millera9286302006-11-14 19:53:22 -08001760#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001761
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762/* NOTE: A lot of things set to zero explicitly by call to
1763 * sk_alloc() so need not be done here.
1764 */
1765static int tcp_v6_init_sock(struct sock *sk)
1766{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001767 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Neal Cardwell900f65d2012-04-19 09:55:21 +00001769 tcp_init_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001771 icsk->icsk_af_ops = &ipv6_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001773#ifdef CONFIG_TCP_MD5SIG
David S. Millerac807fa2012-04-23 03:21:58 -04001774 tcp_sk(sk)->af_specific = &tcp_sock_ipv6_specific;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001775#endif
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 return 0;
1778}
1779
Brian Haley7d06b2e2008-06-14 17:04:49 -07001780static void tcp_v6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 tcp_v4_destroy_sock(sk);
Brian Haley7d06b2e2008-06-14 17:04:49 -07001783 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784}
1785
YOSHIFUJI Hideaki952a10b2007-04-21 20:13:44 +09001786#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787/* Proc filesystem TCPv6 sock list dumping. */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001788static void get_openreq6(struct seq_file *seq,
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001789 const struct sock *sk, struct request_sock *req, int i, int uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int ttd = req->expires - jiffies;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001792 const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
1793 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 if (ttd < 0)
1796 ttd = 0;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 seq_printf(seq,
1799 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Dan Rosenberg71338aa2011-05-23 12:17:35 +00001800 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 i,
1802 src->s6_addr32[0], src->s6_addr32[1],
1803 src->s6_addr32[2], src->s6_addr32[3],
KOVACS Krisztianfd507032008-10-19 23:35:58 -07001804 ntohs(inet_rsk(req)->loc_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 dest->s6_addr32[0], dest->s6_addr32[1],
1806 dest->s6_addr32[2], dest->s6_addr32[3],
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -07001807 ntohs(inet_rsk(req)->rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 TCP_SYN_RECV,
1809 0,0, /* could print option size, but that is af dependent. */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001810 1, /* timers active (only the expire timer) */
1811 jiffies_to_clock_t(ttd),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 req->retrans,
1813 uid,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001814 0, /* non standard timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 0, /* open_requests have no inode */
1816 0, req);
1817}
1818
1819static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1820{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001821 const struct in6_addr *dest, *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 __u16 destp, srcp;
1823 int timer_active;
1824 unsigned long timer_expires;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001825 const struct inet_sock *inet = inet_sk(sp);
1826 const struct tcp_sock *tp = tcp_sk(sp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001827 const struct inet_connection_sock *icsk = inet_csk(sp);
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001828 const struct ipv6_pinfo *np = inet6_sk(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
1830 dest = &np->daddr;
1831 src = &np->rcv_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001832 destp = ntohs(inet->inet_dport);
1833 srcp = ntohs(inet->inet_sport);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001834
1835 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001837 timer_expires = icsk->icsk_timeout;
1838 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001840 timer_expires = icsk->icsk_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 } 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 "
Dan Rosenberg71338aa2011-05-23 12:17:35 +00001851 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 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,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001857 sp->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07001858 tp->write_seq-tp->snd_una,
1859 (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 timer_active,
1861 jiffies_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001862 icsk->icsk_retransmits,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 sock_i_uid(sp),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001864 icsk->icsk_probes_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 sock_i_ino(sp),
1866 atomic_read(&sp->sk_refcnt), sp,
Stephen Hemminger7be87352008-06-27 20:00:19 -07001867 jiffies_to_clock_t(icsk->icsk_rto),
1868 jiffies_to_clock_t(icsk->icsk_ack.ato),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001869 (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
Ilpo Järvinen0b6a05c2009-09-15 01:30:10 -07001870 tp->snd_cwnd,
1871 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 );
1873}
1874
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001875static void get_timewait6_sock(struct seq_file *seq,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001876 struct inet_timewait_sock *tw, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001878 const struct in6_addr *dest, *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 __u16 destp, srcp;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001880 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int ttd = tw->tw_ttd - jiffies;
1882
1883 if (ttd < 0)
1884 ttd = 0;
1885
Arnaldo Carvalho de Melo0fa1a532005-12-13 23:23:09 -08001886 dest = &tw6->tw_v6_daddr;
1887 src = &tw6->tw_v6_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 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 "
Dan Rosenberg71338aa2011-05-23 12:17:35 +00001893 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904static 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 }
1931out:
1932 return 0;
1933}
1934
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001935static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943static struct tcp_seq_afinfo tcp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 .name = "tcp6",
1945 .family = AF_INET6,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001946 .seq_fops = &tcp6_afinfo_seq_fops,
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07001947 .seq_ops = {
1948 .show = tcp6_seq_show,
1949 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950};
1951
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001952int __net_init tcp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001954 return tcp_proc_register(net, &tcp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955}
1956
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001957void tcp6_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001959 tcp_proc_unregister(net, &tcp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961#endif
1962
1963struct proto tcpv6_prot = {
1964 .name = "TCPv6",
1965 .owner = THIS_MODULE,
1966 .close = tcp_close,
1967 .connect = tcp_v6_connect,
1968 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001969 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 .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,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 .recvmsg = tcp_recvmsg,
Changli Gao7ba42912010-07-10 20:41:55 +00001977 .sendmsg = tcp_sendmsg,
1978 .sendpage = tcp_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 .backlog_rcv = tcp_v6_do_rcv,
Eric Dumazet46d3cea2012-07-11 05:50:31 +00001980 .release_cb = tcp_release_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 .hash = tcp_v6_hash,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001982 .unhash = inet_unhash,
1983 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 .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,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07001988 .orphan_count = &tcp_orphan_count,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 .sysctl_wmem = sysctl_tcp_wmem,
1990 .sysctl_rmem = sysctl_tcp_rmem,
1991 .max_header = MAX_TCP_HEADER,
1992 .obj_size = sizeof(struct tcp6_sock),
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001993 .slab_flags = SLAB_DESTROY_BY_RCU,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001994 .twsk_prot = &tcp6_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001995 .rsk_prot = &tcp6_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07001996 .h.hashinfo = &tcp_hashinfo,
Changli Gao7ba42912010-07-10 20:41:55 +00001997 .no_autobind = true,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001998#ifdef CONFIG_COMPAT
1999 .compat_setsockopt = compat_tcp_setsockopt,
2000 .compat_getsockopt = compat_tcp_getsockopt,
2001#endif
Glauber Costad1a4c0b2011-12-11 21:47:04 +00002002#ifdef CONFIG_CGROUP_MEM_RES_CTLR_KMEM
2003 .proto_cgroup = tcp_proto_cgroup,
2004#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005};
2006
Alexey Dobriyan41135cc2009-09-14 12:22:28 +00002007static const struct inet6_protocol tcpv6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .handler = tcp_v6_rcv,
2009 .err_handler = tcp_v6_err,
Herbert Xua430a432006-07-08 13:34:56 -07002010 .gso_send_check = tcp_v6_gso_send_check,
Herbert Xuadcfc7d2006-06-30 13:36:15 -07002011 .gso_segment = tcp_tso_segment,
Herbert Xu684f2172009-01-08 10:41:23 -08002012 .gro_receive = tcp6_gro_receive,
2013 .gro_complete = tcp6_gro_complete,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2015};
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017static struct inet_protosw tcpv6_protosw = {
2018 .type = SOCK_STREAM,
2019 .protocol = IPPROTO_TCP,
2020 .prot = &tcpv6_prot,
2021 .ops = &inet6_stream_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 .no_check = 0,
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08002023 .flags = INET_PROTOSW_PERMANENT |
2024 INET_PROTOSW_ICSK,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025};
2026
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002027static int __net_init tcpv6_net_init(struct net *net)
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002028{
Denis V. Lunev56772422008-04-03 14:28:30 -07002029 return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2030 SOCK_RAW, IPPROTO_TCP, net);
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002031}
2032
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002033static void __net_exit tcpv6_net_exit(struct net *net)
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002034{
Denis V. Lunev56772422008-04-03 14:28:30 -07002035 inet_ctl_sock_destroy(net->ipv6.tcp_sk);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002036}
2037
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00002038static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002039{
2040 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002041}
2042
2043static struct pernet_operations tcpv6_net_ops = {
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00002044 .init = tcpv6_net_init,
2045 .exit = tcpv6_net_exit,
2046 .exit_batch = tcpv6_net_exit_batch,
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002047};
2048
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002049int __init tcpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002051 int ret;
David Woodhouseae0f7d52006-01-11 15:53:04 -08002052
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002053 ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
2054 if (ret)
2055 goto out;
2056
2057 /* register inet6 protocol */
2058 ret = inet6_register_protosw(&tcpv6_protosw);
2059 if (ret)
2060 goto out_tcpv6_protocol;
2061
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002062 ret = register_pernet_subsys(&tcpv6_net_ops);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002063 if (ret)
2064 goto out_tcpv6_protosw;
2065out:
2066 return ret;
2067
2068out_tcpv6_protocol:
2069 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2070out_tcpv6_protosw:
2071 inet6_unregister_protosw(&tcpv6_protosw);
2072 goto out;
2073}
2074
Daniel Lezcano09f77092007-12-13 05:34:58 -08002075void tcpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002076{
Daniel Lezcano93ec9262008-03-07 11:16:02 -08002077 unregister_pernet_subsys(&tcpv6_net_ops);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08002078 inet6_unregister_protosw(&tcpv6_protosw);
2079 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080}