blob: 03a5d1ed33405efab3804973c6a807bb6e24344e [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>
Wang Yufen4aa956d2014-03-29 09:27:29 +080042#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#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>
Denis V. Lunev3d58b5f2008-04-03 14:22:32 -070062#include <net/inet_common.h>
David S. Miller6e5714e2011-08-03 20:50:44 -070063#include <net/secure_seq.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000064#include <net/tcp_memcontrol.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030065#include <net/busy_poll.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/proc_fs.h>
68#include <linux/seq_file.h>
69
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080070#include <linux/crypto.h>
71#include <linux/scatterlist.h>
72
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -080073static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
Gui Jianfeng6edafaa2008-08-06 23:50:04 -070074static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
75 struct request_sock *req);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Stephen Hemminger3b401a82009-09-01 19:25:04 +000079static const struct inet_connection_sock_af_ops ipv6_mapped;
80static const struct inet_connection_sock_af_ops ipv6_specific;
David S. Millera9286302006-11-14 19:53:22 -080081#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +000082static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
83static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +090084#else
85static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
Eric Dumazetb71d1d42011-04-22 04:53:02 +000086 const struct in6_addr *addr)
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +090087{
88 return NULL;
89}
David S. Millera9286302006-11-14 19:53:22 -080090#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Neal Cardwellfae6ef82012-08-19 03:30:38 +000092static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
93{
94 struct dst_entry *dst = skb_dst(skb);
95 const struct rt6_info *rt = (const struct rt6_info *)dst;
96
97 dst_hold(dst);
98 sk->sk_rx_dst = dst;
99 inet_sk(sk)->rx_dst_ifindex = skb->skb_iif;
100 if (rt->rt6i_node)
101 inet6_sk(sk)->rx_dst_cookie = rt->rt6i_node->fn_sernum;
102}
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104static void tcp_v6_hash(struct sock *sk)
105{
106 if (sk->sk_state != TCP_CLOSE) {
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -0800107 if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 tcp_prot.hash(sk);
109 return;
110 }
111 local_bh_disable();
Eric Dumazet9327f702009-12-04 03:46:54 +0000112 __inet6_hash(sk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 local_bh_enable();
114 }
115}
116
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400117static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700119 return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
120 ipv6_hdr(skb)->saddr.s6_addr32,
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700121 tcp_hdr(skb)->dest,
122 tcp_hdr(skb)->source);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123}
124
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900125static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 int addr_len)
127{
128 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900129 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -0800130 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 struct ipv6_pinfo *np = inet6_sk(sk);
132 struct tcp_sock *tp = tcp_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +0000133 struct in6_addr *saddr = NULL, *final_p, final;
David S. Miller493f3772010-12-02 12:14:29 -0800134 struct rt6_info *rt;
David S. Miller4c9483b2011-03-12 16:22:43 -0500135 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct dst_entry *dst;
137 int addr_type;
138 int err;
139
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900140 if (addr_len < SIN6_LEN_RFC2133)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 return -EINVAL;
142
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900143 if (usin->sin6_family != AF_INET6)
Eric Dumazeta02cec22010-09-22 20:43:57 +0000144 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
David S. Miller4c9483b2011-03-12 16:22:43 -0500146 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (np->sndflow) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500149 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
150 IP6_ECN_flow_init(fl6.flowlabel);
151 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 struct ip6_flowlabel *flowlabel;
David S. Miller4c9483b2011-03-12 16:22:43 -0500153 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 if (flowlabel == NULL)
155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 fl6_sock_release(flowlabel);
157 }
158 }
159
160 /*
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900161 * connect() to INADDR_ANY means loopback (BSD'ism).
162 */
163
Weilong Chen4c99aa42013-12-19 18:44:34 +0800164 if (ipv6_addr_any(&usin->sin6_addr))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900165 usin->sin6_addr.s6_addr[15] = 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 addr_type = ipv6_addr_type(&usin->sin6_addr);
168
Weilong Chen4c99aa42013-12-19 18:44:34 +0800169 if (addr_type & IPV6_ADDR_MULTICAST)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return -ENETUNREACH;
171
172 if (addr_type&IPV6_ADDR_LINKLOCAL) {
173 if (addr_len >= sizeof(struct sockaddr_in6) &&
174 usin->sin6_scope_id) {
175 /* If interface is set while binding, indices
176 * must coincide.
177 */
178 if (sk->sk_bound_dev_if &&
179 sk->sk_bound_dev_if != usin->sin6_scope_id)
180 return -EINVAL;
181
182 sk->sk_bound_dev_if = usin->sin6_scope_id;
183 }
184
185 /* Connect to link-local address requires an interface */
186 if (!sk->sk_bound_dev_if)
187 return -EINVAL;
188 }
189
190 if (tp->rx_opt.ts_recent_stamp &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700191 !ipv6_addr_equal(&sk->sk_v6_daddr, &usin->sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 tp->rx_opt.ts_recent = 0;
193 tp->rx_opt.ts_recent_stamp = 0;
194 tp->write_seq = 0;
195 }
196
Eric Dumazetefe42082013-10-03 15:42:29 -0700197 sk->sk_v6_daddr = usin->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500198 np->flow_label = fl6.flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Tom Herbertb73c3d02014-07-01 21:32:17 -0700200 ip6_set_txhash(sk);
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /*
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,
Eric Dumazetefe42082013-10-03 15:42:29 -0700238 &sk->sk_v6_rcv_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
241 return err;
242 }
243
Eric Dumazetefe42082013-10-03 15:42:29 -0700244 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
245 saddr = &sk->sk_v6_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
David S. Miller4c9483b2011-03-12 16:22:43 -0500247 fl6.flowi6_proto = IPPROTO_TCP;
Eric Dumazetefe42082013-10-03 15:42:29 -0700248 fl6.daddr = sk->sk_v6_daddr;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000249 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
Steffen Klassert0e0d44a2013-08-28 08:04:14 +0200259 dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
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;
Eric Dumazetefe42082013-10-03 15:42:29 -0700267 sk->sk_v6_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 &&
Eric Dumazetefe42082013-10-03 15:42:29 -0700280 ipv6_addr_equal(&rt->rt6i_dst.addr, &sk->sk_v6_daddr))
David S. Miller81166dd2012-07-10 03:14:24 -0700281 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
Andrey Vagin2b916472012-11-22 01:13:58 +0000297 if (!tp->write_seq && likely(!tp->repair))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
Eric Dumazetefe42082013-10-03 15:42:29 -0700299 sk->sk_v6_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
Eric Dumazet563d34d2012-07-23 09:48:52 +0200318static void tcp_v6_mtu_reduced(struct sock *sk)
319{
320 struct dst_entry *dst;
321
322 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
323 return;
324
325 dst = inet6_csk_update_pmtu(sk, tcp_sk(sk)->mtu_info);
326 if (!dst)
327 return;
328
329 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
330 tcp_sync_mss(sk, dst_mtu(dst));
331 tcp_simple_retransmit(sk);
332 }
333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700336 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Weilong Chen4c99aa42013-12-19 18:44:34 +0800338 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300339 const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 struct ipv6_pinfo *np;
341 struct sock *sk;
342 int err;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900343 struct tcp_sock *tp;
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700344 struct request_sock *fastopen;
345 __u32 seq, snd_una;
Pavel Emelyanovca12a1a2008-07-16 20:28:42 -0700346 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Pavel Emelyanovca12a1a2008-07-16 20:28:42 -0700348 sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -0800349 th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 if (sk == NULL) {
Denis V. Luneve41b5362008-10-08 10:33:26 -0700352 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
353 ICMP6_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return;
355 }
356
357 if (sk->sk_state == TCP_TIME_WAIT) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -0700358 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return;
360 }
361
362 bh_lock_sock(sk);
Eric Dumazet563d34d2012-07-23 09:48:52 +0200363 if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700364 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 if (sk->sk_state == TCP_CLOSE)
367 goto out;
368
Stephen Hemmingere802af92010-04-22 15:24:53 -0700369 if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) {
370 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
371 goto out;
372 }
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 tp = tcp_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900375 seq = ntohl(th->seq);
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700376 /* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
377 fastopen = tp->fastopen_rsk;
378 snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 if (sk->sk_state != TCP_LISTEN &&
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700380 !between(seq, snd_una, tp->snd_nxt)) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700381 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 goto out;
383 }
384
385 np = inet6_sk(sk);
386
David S. Millerec18d9a2012-07-12 00:25:15 -0700387 if (type == NDISC_REDIRECT) {
388 struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie);
389
David S. Miller1ed5c482012-07-12 00:41:25 -0700390 if (dst)
David S. Miller6700c272012-07-17 03:29:28 -0700391 dst->ops->redirect(dst, sk, skb);
Christoph Paasch50a75a82013-04-07 04:53:15 +0000392 goto out;
David S. Millerec18d9a2012-07-12 00:25:15 -0700393 }
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (type == ICMPV6_PKT_TOOBIG) {
Eric Dumazet0d4f0602013-03-18 07:01:28 +0000396 /* We are not interested in TCP_LISTEN and open_requests
397 * (SYN-ACKs send out by Linux are always <576bytes so
398 * they should go through unfragmented).
399 */
400 if (sk->sk_state == TCP_LISTEN)
401 goto out;
402
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100403 if (!ip6_sk_accept_pmtu(sk))
404 goto out;
405
Eric Dumazet563d34d2012-07-23 09:48:52 +0200406 tp->mtu_info = ntohl(info);
407 if (!sock_owned_by_user(sk))
408 tcp_v6_mtu_reduced(sk);
Julian Anastasovd013ef2a2012-09-05 10:53:18 +0000409 else if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED,
410 &tp->tsq_flags))
411 sock_hold(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 goto out;
413 }
414
415 icmpv6_err_convert(type, code, &err);
416
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700417 /* Might be for an request_sock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 switch (sk->sk_state) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700419 struct request_sock *req, **prev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 case TCP_LISTEN:
421 if (sock_owned_by_user(sk))
422 goto out;
423
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -0800424 req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
425 &hdr->saddr, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (!req)
427 goto out;
428
429 /* ICMPs are not backlogged, hence we cannot get
430 * an established socket here.
431 */
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700432 WARN_ON(req->sk != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700434 if (seq != tcp_rsk(req)->snt_isn) {
Pavel Emelyanovde0744a2008-07-16 20:31:16 -0700435 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 goto out;
437 }
438
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700439 inet_csk_reqsk_queue_drop(sk, req, prev);
Vijay Subramanian5f1e9422013-01-31 08:24:19 +0000440 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 goto out;
442
443 case TCP_SYN_SENT:
Yuchung Cheng0a672f72014-05-11 20:22:12 -0700444 case TCP_SYN_RECV:
445 /* Only in fast or simultaneous open. If a fast open socket is
446 * is already accepted it is treated as a connected one below.
447 */
448 if (fastopen && fastopen->sk == NULL)
449 break;
450
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,
Octavian Purdilad6274bd2014-06-25 17:09:58 +0300474 struct flowi *fl,
Neal Cardwell3840a062012-06-28 12:34:19 +0000475 struct request_sock *req,
Daniel Lee3a19ce02014-05-11 20:22:13 -0700476 u16 queue_mapping,
477 struct tcp_fastopen_cookie *foc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
Eric Dumazet634fb9792013-10-09 15:21:29 -0700479 struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct ipv6_pinfo *np = inet6_sk(sk);
Octavian Purdilad6274bd2014-06-25 17:09:58 +0300481 struct flowi6 *fl6 = &fl->u.ip6;
Weilong Chen4c99aa42013-12-19 18:44:34 +0800482 struct sk_buff *skb;
Neal Cardwell94942182012-06-28 12:34:20 +0000483 int err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Neal Cardwell9f10d3f2012-06-28 12:34:21 +0000485 /* First, grab a route. */
486 if (!dst && (dst = inet6_csk_route_req(sk, fl6, req)) == NULL)
Denis V. Lunevfd80eb92008-02-29 11:43:03 -0800487 goto done;
Neal Cardwell94942182012-06-28 12:34:20 +0000488
Daniel Lee3a19ce02014-05-11 20:22:13 -0700489 skb = tcp_make_synack(sk, dst, req, foc);
Neal Cardwell94942182012-06-28 12:34:20 +0000490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (skb) {
Eric Dumazet634fb9792013-10-09 15:21:29 -0700492 __tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
493 &ireq->ir_v6_rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Eric Dumazet634fb9792013-10-09 15:21:29 -0700495 fl6->daddr = ireq->ir_v6_rmt_addr;
Florent Fourcotdf3687f2014-01-17 17:15:03 +0100496 if (np->repflow && (ireq->pktopts != NULL))
497 fl6->flowlabel = ip6_flowlabel(ipv6_hdr(ireq->pktopts));
498
Eric Dumazetfff32692012-06-01 01:47:50 +0000499 skb_set_queue_mapping(skb, queue_mapping);
RongQing.Li43264e02012-07-01 17:18:59 +0000500 err = ip6_xmit(sk, skb, fl6, np->opt, np->tclass);
Gerrit Renkerb9df3cb2006-11-14 11:21:36 -0200501 err = net_xmit_eval(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
503
504done:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return err;
506}
507
Octavian Purdila72659ec2010-01-17 19:09:39 -0800508
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700509static void tcp_v6_reqsk_destructor(struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
Eric Dumazet634fb9792013-10-09 15:21:29 -0700511 kfree_skb(inet_rsk(req)->pktopts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800514#ifdef CONFIG_TCP_MD5SIG
515static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000516 const struct in6_addr *addr)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800517{
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000518 return tcp_md5_do_lookup(sk, (union tcp_md5_addr *)addr, AF_INET6);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800519}
520
521static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
522 struct sock *addr_sk)
523{
Eric Dumazetefe42082013-10-03 15:42:29 -0700524 return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800525}
526
527static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
528 struct request_sock *req)
529{
Eric Dumazet634fb9792013-10-09 15:21:29 -0700530 return tcp_v6_md5_do_lookup(sk, &inet_rsk(req)->ir_v6_rmt_addr);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800531}
532
Wang Yufen4aa956d2014-03-29 09:27:29 +0800533static int tcp_v6_parse_md5_keys(struct sock *sk, char __user *optval,
534 int optlen)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800535{
536 struct tcp_md5sig cmd;
537 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800538
539 if (optlen < sizeof(cmd))
540 return -EINVAL;
541
542 if (copy_from_user(&cmd, optval, sizeof(cmd)))
543 return -EFAULT;
544
545 if (sin6->sin6_family != AF_INET6)
546 return -EINVAL;
547
548 if (!cmd.tcpm_keylen) {
Brian Haleye773e4f2007-08-24 23:16:08 -0700549 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000550 return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
551 AF_INET);
552 return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
553 AF_INET6);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800554 }
555
556 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
557 return -EINVAL;
558
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000559 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
560 return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
561 AF_INET, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800562
Eric Dumazeta915da9b2012-01-31 05:18:33 +0000563 return tcp_md5_do_add(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
564 AF_INET6, cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800565}
566
Adam Langley49a72df2008-07-19 00:01:42 -0700567static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000568 const struct in6_addr *daddr,
569 const struct in6_addr *saddr, int nbytes)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800570{
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800571 struct tcp6_pseudohdr *bp;
Adam Langley49a72df2008-07-19 00:01:42 -0700572 struct scatterlist sg;
YOSHIFUJI Hideaki8d26d762008-04-17 13:19:16 +0900573
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800574 bp = &hp->md5_blk.ip6;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800575 /* 1. TCP pseudo-header (RFC2460) */
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000576 bp->saddr = *saddr;
577 bp->daddr = *daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700578 bp->protocol = cpu_to_be32(IPPROTO_TCP);
Adam Langley00b13042008-07-31 21:36:07 -0700579 bp->len = cpu_to_be32(nbytes);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800580
Adam Langley49a72df2008-07-19 00:01:42 -0700581 sg_init_one(&sg, bp, sizeof(*bp));
582 return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
583}
David S. Millerc7da57a2007-10-26 00:41:21 -0700584
Adam Langley49a72df2008-07-19 00:01:42 -0700585static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000586 const struct in6_addr *daddr, struct in6_addr *saddr,
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400587 const struct tcphdr *th)
Adam Langley49a72df2008-07-19 00:01:42 -0700588{
589 struct tcp_md5sig_pool *hp;
590 struct hash_desc *desc;
591
592 hp = tcp_get_md5sig_pool();
593 if (!hp)
594 goto clear_hash_noput;
595 desc = &hp->md5_desc;
596
597 if (crypto_hash_init(desc))
598 goto clear_hash;
599 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
600 goto clear_hash;
601 if (tcp_md5_hash_header(hp, th))
602 goto clear_hash;
603 if (tcp_md5_hash_key(hp, key))
604 goto clear_hash;
605 if (crypto_hash_final(desc, md5_hash))
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800606 goto clear_hash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800607
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800608 tcp_put_md5sig_pool();
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800609 return 0;
Adam Langley49a72df2008-07-19 00:01:42 -0700610
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800611clear_hash:
612 tcp_put_md5sig_pool();
613clear_hash_noput:
614 memset(md5_hash, 0, 16);
Adam Langley49a72df2008-07-19 00:01:42 -0700615 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800616}
617
Adam Langley49a72df2008-07-19 00:01:42 -0700618static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400619 const struct sock *sk,
620 const struct request_sock *req,
621 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800622{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000623 const struct in6_addr *saddr, *daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700624 struct tcp_md5sig_pool *hp;
625 struct hash_desc *desc;
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400626 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800627
628 if (sk) {
629 saddr = &inet6_sk(sk)->saddr;
Eric Dumazetefe42082013-10-03 15:42:29 -0700630 daddr = &sk->sk_v6_daddr;
Adam Langley49a72df2008-07-19 00:01:42 -0700631 } else if (req) {
Eric Dumazet634fb9792013-10-09 15:21:29 -0700632 saddr = &inet_rsk(req)->ir_v6_loc_addr;
633 daddr = &inet_rsk(req)->ir_v6_rmt_addr;
Adam Langley49a72df2008-07-19 00:01:42 -0700634 } else {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000635 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Adam Langley49a72df2008-07-19 00:01:42 -0700636 saddr = &ip6h->saddr;
637 daddr = &ip6h->daddr;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800638 }
Adam Langley49a72df2008-07-19 00:01:42 -0700639
640 hp = tcp_get_md5sig_pool();
641 if (!hp)
642 goto clear_hash_noput;
643 desc = &hp->md5_desc;
644
645 if (crypto_hash_init(desc))
646 goto clear_hash;
647
648 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
649 goto clear_hash;
650 if (tcp_md5_hash_header(hp, th))
651 goto clear_hash;
652 if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
653 goto clear_hash;
654 if (tcp_md5_hash_key(hp, key))
655 goto clear_hash;
656 if (crypto_hash_final(desc, md5_hash))
657 goto clear_hash;
658
659 tcp_put_md5sig_pool();
660 return 0;
661
662clear_hash:
663 tcp_put_md5sig_pool();
664clear_hash_noput:
665 memset(md5_hash, 0, 16);
666 return 1;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800667}
668
Dmitry Popov9ea88a12014-08-07 02:38:22 +0400669static int __tcp_v6_inbound_md5_hash(struct sock *sk,
670 const struct sk_buff *skb)
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800671{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400672 const __u8 *hash_location = NULL;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800673 struct tcp_md5sig_key *hash_expected;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000674 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
Eric Dumazet318cf7a2011-10-24 02:46:04 -0400675 const struct tcphdr *th = tcp_hdr(skb);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800676 int genhash;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800677 u8 newhash[16];
678
679 hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
YOSHIFUJI Hideaki7d5d5522008-04-17 12:29:53 +0900680 hash_location = tcp_parse_md5sig_option(th);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800681
David S. Miller785957d2008-07-30 03:03:15 -0700682 /* We've parsed the options - do we have a hash? */
683 if (!hash_expected && !hash_location)
684 return 0;
685
686 if (hash_expected && !hash_location) {
687 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800688 return 1;
689 }
690
David S. Miller785957d2008-07-30 03:03:15 -0700691 if (!hash_expected && hash_location) {
692 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800693 return 1;
694 }
695
696 /* check the signature */
Adam Langley49a72df2008-07-19 00:01:42 -0700697 genhash = tcp_v6_md5_hash_skb(newhash,
698 hash_expected,
699 NULL, NULL, skb);
700
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800701 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
Joe Perchese87cc472012-05-13 21:56:26 +0000702 net_info_ratelimited("MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
703 genhash ? "failed" : "mismatch",
704 &ip6h->saddr, ntohs(th->source),
705 &ip6h->daddr, ntohs(th->dest));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800706 return 1;
707 }
708 return 0;
709}
Dmitry Popov9ea88a12014-08-07 02:38:22 +0400710
711static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
712{
713 int ret;
714
715 rcu_read_lock();
716 ret = __tcp_v6_inbound_md5_hash(sk, skb);
717 rcu_read_unlock();
718
719 return ret;
720}
721
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800722#endif
723
Octavian Purdila16bea702014-06-25 17:09:53 +0300724static void tcp_v6_init_req(struct request_sock *req, struct sock *sk,
725 struct sk_buff *skb)
726{
727 struct inet_request_sock *ireq = inet_rsk(req);
728 struct ipv6_pinfo *np = inet6_sk(sk);
729
730 ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
731 ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
732
733 ireq->ir_iif = sk->sk_bound_dev_if;
734
735 /* So that link locals have meaning */
736 if (!sk->sk_bound_dev_if &&
737 ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
738 ireq->ir_iif = inet6_iif(skb);
739
740 if (!TCP_SKB_CB(skb)->when &&
741 (ipv6_opt_accepted(sk, skb) || np->rxopt.bits.rxinfo ||
742 np->rxopt.bits.rxoinfo || np->rxopt.bits.rxhlim ||
743 np->rxopt.bits.rxohlim || np->repflow)) {
744 atomic_inc(&skb->users);
745 ireq->pktopts = skb;
746 }
747}
748
Octavian Purdilad94e0412014-06-25 17:09:55 +0300749static struct dst_entry *tcp_v6_route_req(struct sock *sk, struct flowi *fl,
750 const struct request_sock *req,
751 bool *strict)
752{
753 if (strict)
754 *strict = true;
755 return inet6_csk_route_req(sk, &fl->u.ip6, req);
756}
757
Glenn Griffinc6aefaf2008-02-07 21:49:26 -0800758struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 .family = AF_INET6,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700760 .obj_size = sizeof(struct tcp6_request_sock),
Octavian Purdila5db92c92014-06-25 17:09:59 +0300761 .rtx_syn_ack = tcp_rtx_synack,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700762 .send_ack = tcp_v6_reqsk_send_ack,
763 .destructor = tcp_v6_reqsk_destructor,
Octavian Purdila72659ec2010-01-17 19:09:39 -0800764 .send_reset = tcp_v6_send_reset,
Wang Yufen4aa956d2014-03-29 09:27:29 +0800765 .syn_ack_timeout = tcp_syn_ack_timeout,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766};
767
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +0000768static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
Octavian Purdila2aec4a22014-06-25 17:10:00 +0300769 .mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) -
770 sizeof(struct ipv6hdr),
Octavian Purdila16bea702014-06-25 17:09:53 +0300771#ifdef CONFIG_TCP_MD5SIG
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800772 .md5_lookup = tcp_v6_reqsk_md5_lookup,
John Dykstrae3afe7b2009-07-16 05:04:51 +0000773 .calc_md5_hash = tcp_v6_md5_hash_skb,
Andrew Mortonb6332e62006-11-30 19:16:28 -0800774#endif
Octavian Purdila16bea702014-06-25 17:09:53 +0300775 .init_req = tcp_v6_init_req,
Octavian Purdilafb7b37a2014-06-25 17:09:54 +0300776#ifdef CONFIG_SYN_COOKIES
777 .cookie_init_seq = cookie_v6_init_sequence,
778#endif
Octavian Purdilad94e0412014-06-25 17:09:55 +0300779 .route_req = tcp_v6_route_req,
Octavian Purdila936b8bd2014-06-25 17:09:57 +0300780 .init_seq = tcp_v6_init_sequence,
Octavian Purdilad6274bd2014-06-25 17:09:58 +0300781 .send_synack = tcp_v6_send_synack,
Octavian Purdila695da142014-06-25 17:10:01 +0300782 .queue_hash_add = inet6_csk_reqsk_queue_hash_add,
Octavian Purdila16bea702014-06-25 17:09:53 +0300783};
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800784
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700785static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
Wang Yufen9c76a112014-03-29 09:27:31 +0800786 u32 tsval, u32 tsecr, int oif,
Florent Fourcot1d13a962014-01-16 17:21:22 +0100787 struct tcp_md5sig_key *key, int rst, u8 tclass,
788 u32 label)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400790 const struct tcphdr *th = tcp_hdr(skb);
791 struct tcphdr *t1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 struct sk_buff *buff;
David S. Miller4c9483b2011-03-12 16:22:43 -0500793 struct flowi6 fl6;
Eric Dumazetadf30902009-06-02 05:19:30 +0000794 struct net *net = dev_net(skb_dst(skb)->dev);
Daniel Lezcanoe5047992008-03-07 11:16:26 -0800795 struct sock *ctl_sk = net->ipv6.tcp_sk;
YOSHIFUJI Hideaki9cb57342008-01-12 02:16:03 -0800796 unsigned int tot_len = sizeof(struct tcphdr);
Eric Dumazetadf30902009-06-02 05:19:30 +0000797 struct dst_entry *dst;
Al Viroe69a4adc2006-11-14 20:56:00 -0800798 __be32 *topt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Andrey Vaginee684b62013-02-11 05:50:19 +0000800 if (tsecr)
YOSHIFUJI Hideaki4244f8a2006-10-10 19:40:50 -0700801 tot_len += TCPOLEN_TSTAMP_ALIGNED;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800802#ifdef CONFIG_TCP_MD5SIG
803 if (key)
804 tot_len += TCPOLEN_MD5SIG_ALIGNED;
805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
808 GFP_ATOMIC);
809 if (buff == NULL)
810 return;
811
812 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
813
Ilpo Järvinen77c676d2008-10-09 14:41:38 -0700814 t1 = (struct tcphdr *) skb_push(buff, tot_len);
Herbert Xu6651ffc2010-04-21 00:47:15 -0700815 skb_reset_transport_header(buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Swap the send and the receive. */
818 memset(t1, 0, sizeof(*t1));
819 t1->dest = th->source;
820 t1->source = th->dest;
Ilpo Järvinen77c676d2008-10-09 14:41:38 -0700821 t1->doff = tot_len / 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 t1->seq = htonl(seq);
823 t1->ack_seq = htonl(ack);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700824 t1->ack = !rst || !th->ack;
825 t1->rst = rst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 t1->window = htons(win);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800827
Al Viroe69a4adc2006-11-14 20:56:00 -0800828 topt = (__be32 *)(t1 + 1);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900829
Andrey Vaginee684b62013-02-11 05:50:19 +0000830 if (tsecr) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800831 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
832 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
Andrey Vaginee684b62013-02-11 05:50:19 +0000833 *topt++ = htonl(tsval);
834 *topt++ = htonl(tsecr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800837#ifdef CONFIG_TCP_MD5SIG
838 if (key) {
839 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
840 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
Adam Langley49a72df2008-07-19 00:01:42 -0700841 tcp_v6_md5_hash_hdr((__u8 *)topt, key,
Adam Langley90b7e112008-07-31 20:49:48 -0700842 &ipv6_hdr(skb)->saddr,
843 &ipv6_hdr(skb)->daddr, t1);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800844 }
845#endif
846
David S. Miller4c9483b2011-03-12 16:22:43 -0500847 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000848 fl6.daddr = ipv6_hdr(skb)->saddr;
849 fl6.saddr = ipv6_hdr(skb)->daddr;
Florent Fourcot1d13a962014-01-16 17:21:22 +0100850 fl6.flowlabel = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
David S. Millere5700af2010-04-21 14:59:20 -0700852 buff->ip_summed = CHECKSUM_PARTIAL;
853 buff->csum = 0;
854
David S. Miller4c9483b2011-03-12 16:22:43 -0500855 __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
David S. Miller4c9483b2011-03-12 16:22:43 -0500857 fl6.flowi6_proto = IPPROTO_TCP;
Lorenzo Colittia36dbdb2014-04-11 13:19:12 +0900858 if (rt6_need_strict(&fl6.daddr) && !oif)
Alexey Kuznetsov4c675252012-10-12 04:34:17 +0000859 fl6.flowi6_oif = inet6_iif(skb);
Wang Yufen9c76a112014-03-29 09:27:31 +0800860 else
861 fl6.flowi6_oif = oif;
Lorenzo Colittie1108612014-05-13 10:17:33 -0700862 fl6.flowi6_mark = IP6_REPLY_MARK(net, skb->mark);
David S. Miller1958b852011-03-12 16:36:19 -0500863 fl6.fl6_dport = t1->dest;
864 fl6.fl6_sport = t1->source;
David S. Miller4c9483b2011-03-12 16:22:43 -0500865 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700867 /* Pass a socket to ip6_dst_lookup either it is for RST
868 * Underlying function will use this to retrieve the network
869 * namespace
870 */
Steffen Klassert0e0d44a2013-08-28 08:04:14 +0200871 dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800872 if (!IS_ERR(dst)) {
873 skb_dst_set(buff, dst);
Eric Dumazetb903d322011-10-27 00:44:35 -0400874 ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800875 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
876 if (rst)
877 TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
878 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 }
880
881 kfree_skb(buff);
882}
883
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700884static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
885{
Eric Dumazetcf533ea2011-10-21 05:22:42 -0400886 const struct tcphdr *th = tcp_hdr(skb);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700887 u32 seq = 0, ack_seq = 0;
Guo-Fu Tsengfa3e5b42008-10-09 21:11:56 -0700888 struct tcp_md5sig_key *key = NULL;
Shawn Lu658ddaa2012-01-31 22:35:48 +0000889#ifdef CONFIG_TCP_MD5SIG
890 const __u8 *hash_location = NULL;
891 struct ipv6hdr *ipv6h = ipv6_hdr(skb);
892 unsigned char newhash[16];
893 int genhash;
894 struct sock *sk1 = NULL;
895#endif
Wang Yufen9c76a112014-03-29 09:27:31 +0800896 int oif;
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700897
898 if (th->rst)
899 return;
900
901 if (!ipv6_unicast_destination(skb))
902 return;
903
904#ifdef CONFIG_TCP_MD5SIG
Shawn Lu658ddaa2012-01-31 22:35:48 +0000905 hash_location = tcp_parse_md5sig_option(th);
906 if (!sk && hash_location) {
907 /*
908 * active side is lost. Try to find listening socket through
909 * source port, and then find md5 key through listening socket.
910 * we are not loose security here:
911 * Incoming packet is checked with md5 hash with finding key,
912 * no RST generated if md5 hash doesn't match.
913 */
914 sk1 = inet6_lookup_listener(dev_net(skb_dst(skb)->dev),
Tom Herbert5ba24952013-01-22 09:50:39 +0000915 &tcp_hashinfo, &ipv6h->saddr,
916 th->source, &ipv6h->daddr,
Shawn Lu658ddaa2012-01-31 22:35:48 +0000917 ntohs(th->source), inet6_iif(skb));
918 if (!sk1)
919 return;
920
921 rcu_read_lock();
922 key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr);
923 if (!key)
924 goto release_sk1;
925
926 genhash = tcp_v6_md5_hash_skb(newhash, key, NULL, NULL, skb);
927 if (genhash || memcmp(hash_location, newhash, 16) != 0)
928 goto release_sk1;
929 } else {
930 key = sk ? tcp_v6_md5_do_lookup(sk, &ipv6h->saddr) : NULL;
931 }
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700932#endif
933
934 if (th->ack)
935 seq = ntohl(th->ack_seq);
936 else
937 ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
938 (th->doff << 2);
939
Wang Yufen9c76a112014-03-29 09:27:31 +0800940 oif = sk ? sk->sk_bound_dev_if : 0;
941 tcp_v6_send_response(skb, seq, ack_seq, 0, 0, 0, oif, key, 1, 0, 0);
Shawn Lu658ddaa2012-01-31 22:35:48 +0000942
943#ifdef CONFIG_TCP_MD5SIG
944release_sk1:
945 if (sk1) {
946 rcu_read_unlock();
947 sock_put(sk1);
948 }
949#endif
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700950}
951
Andrey Vaginee684b62013-02-11 05:50:19 +0000952static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack,
Wang Yufen9c76a112014-03-29 09:27:31 +0800953 u32 win, u32 tsval, u32 tsecr, int oif,
Florent Fourcot1d13a962014-01-16 17:21:22 +0100954 struct tcp_md5sig_key *key, u8 tclass,
955 u32 label)
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700956{
Wang Yufen9c76a112014-03-29 09:27:31 +0800957 tcp_v6_send_response(skb, seq, ack, win, tsval, tsecr, oif, key, 0, tclass,
Florent Fourcot1d13a962014-01-16 17:21:22 +0100958 label);
Ilpo Järvinen626e2642008-10-09 14:42:40 -0700959}
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
962{
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700963 struct inet_timewait_sock *tw = inet_twsk(sk);
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -0800964 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
YOSHIFUJI Hideaki9501f972008-04-18 12:45:16 +0900966 tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700967 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
Andrey Vaginee684b62013-02-11 05:50:19 +0000968 tcp_time_stamp + tcptw->tw_ts_offset,
Wang Yufen9c76a112014-03-29 09:27:31 +0800969 tcptw->tw_ts_recent, tw->tw_bound_dev_if, tcp_twsk_md5_key(tcptw),
Florent Fourcot1d13a962014-01-16 17:21:22 +0100970 tw->tw_tclass, (tw->tw_flowlabel << 12));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700972 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973}
974
Gui Jianfeng6edafaa2008-08-06 23:50:04 -0700975static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
976 struct request_sock *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Daniel Lee3a19ce02014-05-11 20:22:13 -0700978 /* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
979 * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
980 */
981 tcp_v6_send_ack(skb, (sk->sk_state == TCP_LISTEN) ?
982 tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt,
983 tcp_rsk(req)->rcv_nxt,
Wang Yufen9c76a112014-03-29 09:27:31 +0800984 req->rcv_wnd, tcp_time_stamp, req->ts_recent, sk->sk_bound_dev_if,
Florent Fourcot1d13a962014-01-16 17:21:22 +0100985 tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr),
986 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
989
Weilong Chen4c99aa42013-12-19 18:44:34 +0800990static struct sock *tcp_v6_hnd_req(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700992 struct request_sock *req, **prev;
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -0700993 const struct tcphdr *th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 struct sock *nsk;
995
996 /* Find possible connection requests. */
Arnaldo Carvalho de Melo81297652005-12-13 23:15:24 -0800997 req = inet6_csk_search_req(sk, &prev, th->source,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700998 &ipv6_hdr(skb)->saddr,
999 &ipv6_hdr(skb)->daddr, inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (req)
Jerry Chu83368862012-08-31 12:29:12 +00001001 return tcp_check_req(sk, skb, req, prev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001003 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -08001004 &ipv6_hdr(skb)->saddr, th->source,
1005 &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
1007 if (nsk) {
1008 if (nsk->sk_state != TCP_TIME_WAIT) {
1009 bh_lock_sock(nsk);
1010 return nsk;
1011 }
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001012 inet_twsk_put(inet_twsk(nsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 return NULL;
1014 }
1015
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001016#ifdef CONFIG_SYN_COOKIES
Florian Westphalaf9b4732010-06-03 00:43:44 +00001017 if (!th->syn)
Glenn Griffinc6aefaf2008-02-07 21:49:26 -08001018 sk = cookie_v6_check(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019#endif
1020 return sk;
1021}
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 if (skb->protocol == htons(ETH_P_IP))
1026 return tcp_v4_conn_request(sk, skb);
1027
1028 if (!ipv6_unicast_destination(skb))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001029 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Octavian Purdila1fb6f152014-06-25 17:10:02 +03001031 return tcp_conn_request(&tcp6_request_sock_ops,
1032 &tcp_request_sock_ipv6_ops, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034drop:
Vijay Subramanian5f1e9422013-01-31 08:24:19 +00001035 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0; /* don't send reset */
1037}
1038
Weilong Chen4c99aa42013-12-19 18:44:34 +08001039static struct sock *tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1040 struct request_sock *req,
1041 struct dst_entry *dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Eric Dumazet634fb9792013-10-09 15:21:29 -07001043 struct inet_request_sock *ireq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1045 struct tcp6_sock *newtcp6sk;
1046 struct inet_sock *newinet;
1047 struct tcp_sock *newtp;
1048 struct sock *newsk;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001049#ifdef CONFIG_TCP_MD5SIG
1050 struct tcp_md5sig_key *key;
1051#endif
Neal Cardwell3840a062012-06-28 12:34:19 +00001052 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 if (skb->protocol == htons(ETH_P_IP)) {
1055 /*
1056 * v6 mapped
1057 */
1058
1059 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1060
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001061 if (newsk == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return NULL;
1063
1064 newtcp6sk = (struct tcp6_sock *)newsk;
1065 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1066
1067 newinet = inet_sk(newsk);
1068 newnp = inet6_sk(newsk);
1069 newtp = tcp_sk(newsk);
1070
1071 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1072
Eric Dumazetefe42082013-10-03 15:42:29 -07001073 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newsk->sk_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001075 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Eric Dumazetefe42082013-10-03 15:42:29 -07001077 newsk->sk_v6_rcv_saddr = newnp->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001079 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001081#ifdef CONFIG_TCP_MD5SIG
1082 newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
1083#endif
1084
Yan, Zheng676a1182011-09-25 02:21:30 +00001085 newnp->ipv6_ac_list = NULL;
1086 newnp->ipv6_fl_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 newnp->pktoptions = NULL;
1088 newnp->opt = NULL;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001089 newnp->mcast_oif = inet6_iif(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001090 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
Florent Fourcot1397ed32013-12-08 15:46:57 +01001091 newnp->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(skb));
Florent Fourcotdf3687f2014-01-17 17:15:03 +01001092 if (np->repflow)
1093 newnp->flow_label = ip6_flowlabel(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07001095 /*
1096 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
1097 * here, tcp_create_openreq_child now does this for us, see the comment in
1098 * that function for the gory details. -acme
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
1101 /* It is tricky place. Until this moment IPv4 tcp
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001102 worked with IPv6 icsk.icsk_af_ops.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 Sync it now.
1104 */
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001105 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
1107 return newsk;
1108 }
1109
Eric Dumazet634fb9792013-10-09 15:21:29 -07001110 ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
1112 if (sk_acceptq_is_full(sk))
1113 goto out_overflow;
1114
David S. Miller493f3772010-12-02 12:14:29 -08001115 if (!dst) {
Neal Cardwell3840a062012-06-28 12:34:19 +00001116 dst = inet6_csk_route_req(sk, &fl6, req);
David S. Miller493f3772010-12-02 12:14:29 -08001117 if (!dst)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 newsk = tcp_create_openreq_child(sk, req, skb);
1122 if (newsk == NULL)
Balazs Scheidler093d2822010-10-21 13:06:43 +02001123 goto out_nonewsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -07001125 /*
1126 * No need to charge this sock to the relevant IPv6 refcnt debug socks
1127 * count here, tcp_create_openreq_child now does this for us, see the
1128 * comment in that function for the gory details. -acme
1129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Stephen Hemminger59eed272006-08-25 15:55:43 -07001131 newsk->sk_gso_type = SKB_GSO_TCPV6;
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -07001132 __ip6_dst_store(newsk, dst, NULL, NULL);
Neal Cardwellfae6ef82012-08-19 03:30:38 +00001133 inet6_sk_rx_dst_set(newsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 newtcp6sk = (struct tcp6_sock *)newsk;
1136 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1137
1138 newtp = tcp_sk(newsk);
1139 newinet = inet_sk(newsk);
1140 newnp = inet6_sk(newsk);
1141
1142 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1143
Eric Dumazet634fb9792013-10-09 15:21:29 -07001144 newsk->sk_v6_daddr = ireq->ir_v6_rmt_addr;
1145 newnp->saddr = ireq->ir_v6_loc_addr;
1146 newsk->sk_v6_rcv_saddr = ireq->ir_v6_loc_addr;
1147 newsk->sk_bound_dev_if = ireq->ir_iif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Tom Herbertb73c3d02014-07-01 21:32:17 -07001149 ip6_set_txhash(newsk);
1150
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001151 /* Now IPv6 options...
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
1153 First: no IPv4 options.
1154 */
Eric Dumazetf6d8bd02011-04-21 09:45:37 +00001155 newinet->inet_opt = NULL;
Yan, Zheng676a1182011-09-25 02:21:30 +00001156 newnp->ipv6_ac_list = NULL;
Masayuki Nakagawad35690b2007-03-16 16:14:03 -07001157 newnp->ipv6_fl_list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /* Clone RX bits */
1160 newnp->rxopt.all = np->rxopt.all;
1161
1162 /* Clone pktoptions received with SYN */
1163 newnp->pktoptions = NULL;
Eric Dumazet634fb9792013-10-09 15:21:29 -07001164 if (ireq->pktopts != NULL) {
1165 newnp->pktoptions = skb_clone(ireq->pktopts,
Mel Gorman99a1dec2012-07-31 16:44:14 -07001166 sk_gfp_atomic(sk, GFP_ATOMIC));
Eric Dumazet634fb9792013-10-09 15:21:29 -07001167 consume_skb(ireq->pktopts);
1168 ireq->pktopts = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 if (newnp->pktoptions)
1170 skb_set_owner_r(newnp->pktoptions, newsk);
1171 }
1172 newnp->opt = NULL;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001173 newnp->mcast_oif = inet6_iif(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001174 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
Florent Fourcot1397ed32013-12-08 15:46:57 +01001175 newnp->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(skb));
Florent Fourcotdf3687f2014-01-17 17:15:03 +01001176 if (np->repflow)
1177 newnp->flow_label = ip6_flowlabel(ipv6_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 /* Clone native IPv6 options from listening socket (if any)
1180
1181 Yes, keeping reference count would be much more clever,
1182 but we make one more one thing there: reattach optmem
1183 to newsk.
1184 */
RongQing.Li43264e02012-07-01 17:18:59 +00001185 if (np->opt)
1186 newnp->opt = ipv6_dup_options(newsk, np->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001188 inet_csk(newsk)->icsk_ext_hdr_len = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (newnp->opt)
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001190 inet_csk(newsk)->icsk_ext_hdr_len = (newnp->opt->opt_nflen +
1191 newnp->opt->opt_flen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 tcp_sync_mss(newsk, dst_mtu(dst));
David S. Miller0dbaee32010-12-13 12:52:14 -08001194 newtp->advmss = dst_metric_advmss(dst);
Neal Cardwelld135c522012-04-22 09:45:47 +00001195 if (tcp_sk(sk)->rx_opt.user_mss &&
1196 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1197 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 tcp_initialize_rcv_mss(newsk);
1200
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001201 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1202 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001204#ifdef CONFIG_TCP_MD5SIG
1205 /* Copy over the MD5 key from the original socket */
Wang Yufen4aa956d2014-03-29 09:27:29 +08001206 key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr);
1207 if (key != NULL) {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001208 /* We're using one, so create a matching key
1209 * on the newsk structure. If we fail to get
1210 * memory, then we end up not copying the key
1211 * across. Shucks.
1212 */
Eric Dumazetefe42082013-10-03 15:42:29 -07001213 tcp_md5_do_add(newsk, (union tcp_md5_addr *)&newsk->sk_v6_daddr,
Mel Gorman99a1dec2012-07-31 16:44:14 -07001214 AF_INET6, key->key, key->keylen,
1215 sk_gfp_atomic(sk, GFP_ATOMIC));
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001216 }
1217#endif
1218
Balazs Scheidler093d2822010-10-21 13:06:43 +02001219 if (__inet_inherit_port(sk, newsk) < 0) {
Christoph Paasche337e242012-12-14 04:07:58 +00001220 inet_csk_prepare_forced_close(newsk);
1221 tcp_done(newsk);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001222 goto out;
1223 }
Eric Dumazet9327f702009-12-04 03:46:54 +00001224 __inet6_hash(newsk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 return newsk;
1227
1228out_overflow:
Pavel Emelyanovde0744a2008-07-16 20:31:16 -07001229 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001230out_nonewsk:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dst_release(dst);
Balazs Scheidler093d2822010-10-21 13:06:43 +02001232out:
1233 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 return NULL;
1235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237/* The socket must have it's spinlock held when we get
1238 * here.
1239 *
1240 * We have a potential double-lock case here, so even when
1241 * doing backlog processing we use the BH locking scheme.
1242 * This is because we cannot sleep with the original spinlock
1243 * held.
1244 */
1245static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1246{
1247 struct ipv6_pinfo *np = inet6_sk(sk);
1248 struct tcp_sock *tp;
1249 struct sk_buff *opt_skb = NULL;
1250
1251 /* Imagine: socket is IPv6. IPv4 packet arrives,
1252 goes to IPv4 receive handler and backlogged.
1253 From backlog it always goes here. Kerboom...
1254 Fortunately, tcp_rcv_established and rcv_established
1255 handle them correctly, but it is not case with
1256 tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
1257 */
1258
1259 if (skb->protocol == htons(ETH_P_IP))
1260 return tcp_v4_do_rcv(sk, skb);
1261
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001262 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 goto discard;
1264
1265 /*
1266 * socket locking is here for SMP purposes as backlog rcv
1267 * is currently called with bh processing disabled.
1268 */
1269
1270 /* Do Stevens' IPV6_PKTOPTIONS.
1271
1272 Yes, guys, it is the only place in our code, where we
1273 may make it not affecting IPv4.
1274 The rest of code is protocol independent,
1275 and I do not like idea to uglify IPv4.
1276
1277 Actually, all the idea behind IPV6_PKTOPTIONS
1278 looks not very well thought. For now we latch
1279 options, received in the last packet, enqueued
1280 by tcp. Feel free to propose better solution.
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001281 --ANK (980728)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 */
1283 if (np->rxopt.all)
Mel Gorman99a1dec2012-07-31 16:44:14 -07001284 opt_skb = skb_clone(skb, sk_gfp_atomic(sk, GFP_ATOMIC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
Eric Dumazet5d299f32012-08-06 05:09:33 +00001287 struct dst_entry *dst = sk->sk_rx_dst;
1288
Tom Herbertbdeab992011-08-14 19:45:55 +00001289 sock_rps_save_rxhash(sk, skb);
Eric Dumazet5d299f32012-08-06 05:09:33 +00001290 if (dst) {
1291 if (inet_sk(sk)->rx_dst_ifindex != skb->skb_iif ||
1292 dst->ops->check(dst, np->rx_dst_cookie) == NULL) {
1293 dst_release(dst);
1294 sk->sk_rx_dst = NULL;
1295 }
1296 }
1297
Vijay Subramanianc995ae22013-09-03 12:23:22 -07001298 tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 if (opt_skb)
1300 goto ipv6_pktoptions;
1301 return 0;
1302 }
1303
Arnaldo Carvalho de Meloab6a5bb2007-03-18 17:43:48 -07001304 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 goto csum_err;
1306
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001307 if (sk->sk_state == TCP_LISTEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1309 if (!nsk)
1310 goto discard;
1311
1312 /*
1313 * Queue it on the new socket if the new socket is active,
1314 * otherwise we just shortcircuit this and continue with
1315 * the new socket..
1316 */
Weilong Chen4c99aa42013-12-19 18:44:34 +08001317 if (nsk != sk) {
Tom Herbertbdeab992011-08-14 19:45:55 +00001318 sock_rps_save_rxhash(nsk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 if (tcp_child_process(sk, nsk, skb))
1320 goto reset;
1321 if (opt_skb)
1322 __kfree_skb(opt_skb);
1323 return 0;
1324 }
Neil Horman47482f12011-04-06 13:07:09 -07001325 } else
Tom Herbertbdeab992011-08-14 19:45:55 +00001326 sock_rps_save_rxhash(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001328 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 goto reset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (opt_skb)
1331 goto ipv6_pktoptions;
1332 return 0;
1333
1334reset:
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001335 tcp_v6_send_reset(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336discard:
1337 if (opt_skb)
1338 __kfree_skb(opt_skb);
1339 kfree_skb(skb);
1340 return 0;
1341csum_err:
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001342 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_CSUMERRORS);
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001343 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 goto discard;
1345
1346
1347ipv6_pktoptions:
1348 /* Do you ask, what is it?
1349
1350 1. skb was enqueued by tcp.
1351 2. skb is added to tail of read queue, rather than out of order.
1352 3. socket is not in passive state.
1353 4. Finally, it really contains options, which user wants to receive.
1354 */
1355 tp = tcp_sk(sk);
1356 if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1357 !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001358 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001359 np->mcast_oif = inet6_iif(opt_skb);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +09001360 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001361 np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
Florent Fourcot82e9f102013-12-08 15:46:59 +01001362 if (np->rxopt.bits.rxflow || np->rxopt.bits.rxtclass)
Florent Fourcot1397ed32013-12-08 15:46:57 +01001363 np->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(opt_skb));
Florent Fourcotdf3687f2014-01-17 17:15:03 +01001364 if (np->repflow)
1365 np->flow_label = ip6_flowlabel(ipv6_hdr(opt_skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (ipv6_opt_accepted(sk, opt_skb)) {
1367 skb_set_owner_r(opt_skb, sk);
1368 opt_skb = xchg(&np->pktoptions, opt_skb);
1369 } else {
1370 __kfree_skb(opt_skb);
1371 opt_skb = xchg(&np->pktoptions, NULL);
1372 }
1373 }
1374
Wei Yongjun800d55f2009-02-23 21:45:33 +00001375 kfree_skb(opt_skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 return 0;
1377}
1378
Herbert Xue5bbef22007-10-15 12:50:28 -07001379static int tcp_v6_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380{
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001381 const struct tcphdr *th;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001382 const struct ipv6hdr *hdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct sock *sk;
1384 int ret;
Pavel Emelyanova86b1e32008-07-16 20:20:58 -07001385 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 if (skb->pkt_type != PACKET_HOST)
1388 goto discard_it;
1389
1390 /*
1391 * Count it even if it's bad.
1392 */
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001393 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
1395 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1396 goto discard_it;
1397
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001398 th = tcp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
1400 if (th->doff < sizeof(struct tcphdr)/4)
1401 goto bad_packet;
1402 if (!pskb_may_pull(skb, th->doff*4))
1403 goto discard_it;
1404
Tom Herberte4f45b72014-05-02 16:29:51 -07001405 if (skb_checksum_init(skb, IPPROTO_TCP, ip6_compute_pseudo))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001406 goto csum_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Arnaldo Carvalho de Meloaa8223c2007-04-10 21:04:22 -07001408 th = tcp_hdr(skb);
Stephen Hemmingere802af92010-04-22 15:24:53 -07001409 hdr = ipv6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1411 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1412 skb->len - th->doff*4);
1413 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1414 TCP_SKB_CB(skb)->when = 0;
Eric Dumazetb82d1bb2011-09-27 02:20:08 -04001415 TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 TCP_SKB_CB(skb)->sacked = 0;
1417
Arnaldo Carvalho de Melo9a1f27c2008-10-07 11:41:57 -07001418 sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 if (!sk)
1420 goto no_tcp_socket;
1421
1422process:
1423 if (sk->sk_state == TCP_TIME_WAIT)
1424 goto do_time_wait;
1425
Stephen Hemmingere802af92010-04-22 15:24:53 -07001426 if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
1427 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
1428 goto discard_and_relse;
1429 }
1430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1432 goto discard_and_relse;
1433
Dmitry Popov9ea88a12014-08-07 02:38:22 +04001434#ifdef CONFIG_TCP_MD5SIG
1435 if (tcp_v6_inbound_md5_hash(sk, skb))
1436 goto discard_and_relse;
1437#endif
1438
Dmitry Mishinfda9ef52006-08-31 15:28:39 -07001439 if (sk_filter(sk, skb))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 goto discard_and_relse;
1441
Eliezer Tamir8b80cda2013-07-10 17:13:26 +03001442 sk_mark_napi_id(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 skb->dev = NULL;
1444
Fabio Olive Leite293b9c42006-09-25 22:28:47 -07001445 bh_lock_sock_nested(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 ret = 0;
1447 if (!sock_owned_by_user(sk)) {
Dan Williams7bced392013-12-30 12:37:29 -08001448 if (!tcp_prequeue(sk, skb))
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001449 ret = tcp_v6_do_rcv(sk, skb);
Eric Dumazetda882c12012-04-22 23:38:54 +00001450 } else if (unlikely(sk_add_backlog(sk, skb,
1451 sk->sk_rcvbuf + sk->sk_sndbuf))) {
Zhu Yi6b03a532010-03-04 18:01:41 +00001452 bh_unlock_sock(sk);
Eric Dumazet6cce09f2010-03-07 23:21:57 +00001453 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
Zhu Yi6b03a532010-03-04 18:01:41 +00001454 goto discard_and_relse;
1455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456 bh_unlock_sock(sk);
1457
1458 sock_put(sk);
1459 return ret ? -1 : 0;
1460
1461no_tcp_socket:
1462 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1463 goto discard_it;
1464
1465 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001466csum_error:
1467 TCP_INC_STATS_BH(net, TCP_MIB_CSUMERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468bad_packet:
Pavel Emelyanov63231bd2008-07-16 20:22:25 -07001469 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 } else {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001471 tcp_v6_send_reset(NULL, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
1474discard_it:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 kfree_skb(skb);
1476 return 0;
1477
1478discard_and_relse:
1479 sock_put(sk);
1480 goto discard_it;
1481
1482do_time_wait:
1483 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001484 inet_twsk_put(inet_twsk(sk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 goto discard_it;
1486 }
1487
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001488 if (skb->len < (th->doff<<2)) {
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001489 inet_twsk_put(inet_twsk(sk));
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +00001490 goto bad_packet;
1491 }
1492 if (tcp_checksum_complete(skb)) {
1493 inet_twsk_put(inet_twsk(sk));
1494 goto csum_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 }
1496
YOSHIFUJI Hideaki9469c7b2006-10-10 19:41:46 -07001497 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 case TCP_TW_SYN:
1499 {
1500 struct sock *sk2;
1501
YOSHIFUJI Hideakic346dca2008-03-25 21:47:49 +09001502 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
Tom Herbert5ba24952013-01-22 09:50:39 +00001503 &ipv6_hdr(skb)->saddr, th->source,
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -07001504 &ipv6_hdr(skb)->daddr,
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -03001505 ntohs(th->dest), inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (sk2 != NULL) {
Arnaldo Carvalho de Melo295ff7e2005-08-09 20:44:40 -07001507 struct inet_timewait_sock *tw = inet_twsk(sk);
1508 inet_twsk_deschedule(tw, &tcp_death_row);
1509 inet_twsk_put(tw);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 sk = sk2;
1511 goto process;
1512 }
1513 /* Fall through to ACK */
1514 }
1515 case TCP_TW_ACK:
1516 tcp_v6_timewait_ack(sk, skb);
1517 break;
1518 case TCP_TW_RST:
1519 goto no_tcp_socket;
Wang Yufen4aa956d2014-03-29 09:27:29 +08001520 case TCP_TW_SUCCESS:
1521 ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523 goto discard_it;
1524}
1525
Eric Dumazetc7109982012-07-26 12:18:11 +00001526static void tcp_v6_early_demux(struct sk_buff *skb)
1527{
1528 const struct ipv6hdr *hdr;
1529 const struct tcphdr *th;
1530 struct sock *sk;
1531
1532 if (skb->pkt_type != PACKET_HOST)
1533 return;
1534
1535 if (!pskb_may_pull(skb, skb_transport_offset(skb) + sizeof(struct tcphdr)))
1536 return;
1537
1538 hdr = ipv6_hdr(skb);
1539 th = tcp_hdr(skb);
1540
1541 if (th->doff < sizeof(struct tcphdr) / 4)
1542 return;
1543
1544 sk = __inet6_lookup_established(dev_net(skb->dev), &tcp_hashinfo,
1545 &hdr->saddr, th->source,
1546 &hdr->daddr, ntohs(th->dest),
1547 inet6_iif(skb));
1548 if (sk) {
1549 skb->sk = sk;
1550 skb->destructor = sock_edemux;
1551 if (sk->sk_state != TCP_TIME_WAIT) {
1552 struct dst_entry *dst = sk->sk_rx_dst;
Neal Cardwellf3f12132012-10-22 21:41:48 +00001553
Eric Dumazetc7109982012-07-26 12:18:11 +00001554 if (dst)
Eric Dumazet5d299f32012-08-06 05:09:33 +00001555 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
Eric Dumazetc7109982012-07-26 12:18:11 +00001556 if (dst &&
Neal Cardwellf3f12132012-10-22 21:41:48 +00001557 inet_sk(sk)->rx_dst_ifindex == skb->skb_iif)
Eric Dumazetc7109982012-07-26 12:18:11 +00001558 skb_dst_set_noref(skb, dst);
1559 }
1560 }
1561}
1562
David S. Millerccb7c412010-12-01 18:09:13 -08001563static struct timewait_sock_ops tcp6_timewait_sock_ops = {
1564 .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
1565 .twsk_unique = tcp_twsk_unique,
Wang Yufen4aa956d2014-03-29 09:27:29 +08001566 .twsk_destructor = tcp_twsk_destructor,
David S. Millerccb7c412010-12-01 18:09:13 -08001567};
1568
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001569static const struct inet_connection_sock_af_ops ipv6_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001570 .queue_xmit = inet6_csk_xmit,
1571 .send_check = tcp_v6_send_check,
1572 .rebuild_header = inet6_sk_rebuild_header,
Eric Dumazet5d299f32012-08-06 05:09:33 +00001573 .sk_rx_dst_set = inet6_sk_rx_dst_set,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001574 .conn_request = tcp_v6_conn_request,
1575 .syn_recv_sock = tcp_v6_syn_recv_sock,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001576 .net_header_len = sizeof(struct ipv6hdr),
Eric Dumazet67469602012-04-24 07:37:38 +00001577 .net_frag_header_len = sizeof(struct frag_hdr),
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001578 .setsockopt = ipv6_setsockopt,
1579 .getsockopt = ipv6_getsockopt,
1580 .addr2sockaddr = inet6_csk_addr2sockaddr,
1581 .sockaddr_len = sizeof(struct sockaddr_in6),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001582 .bind_conflict = inet6_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001583#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001584 .compat_setsockopt = compat_ipv6_setsockopt,
1585 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001586#endif
Neal Cardwell4fab9072014-08-14 12:40:05 -04001587 .mtu_reduced = tcp_v6_mtu_reduced,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588};
1589
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001590#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001591static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001592 .md5_lookup = tcp_v6_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001593 .calc_md5_hash = tcp_v6_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001594 .md5_parse = tcp_v6_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001595};
David S. Millera9286302006-11-14 19:53:22 -08001596#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001597
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598/*
1599 * TCP over IPv4 via INET6 API
1600 */
Stephen Hemminger3b401a82009-09-01 19:25:04 +00001601static const struct inet_connection_sock_af_ops ipv6_mapped = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001602 .queue_xmit = ip_queue_xmit,
1603 .send_check = tcp_v4_send_check,
1604 .rebuild_header = inet_sk_rebuild_header,
Eric Dumazet63d02d12012-08-09 14:11:00 +00001605 .sk_rx_dst_set = inet_sk_rx_dst_set,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001606 .conn_request = tcp_v6_conn_request,
1607 .syn_recv_sock = tcp_v6_syn_recv_sock,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001608 .net_header_len = sizeof(struct iphdr),
1609 .setsockopt = ipv6_setsockopt,
1610 .getsockopt = ipv6_getsockopt,
1611 .addr2sockaddr = inet6_csk_addr2sockaddr,
1612 .sockaddr_len = sizeof(struct sockaddr_in6),
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001613 .bind_conflict = inet6_csk_bind_conflict,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001614#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001615 .compat_setsockopt = compat_ipv6_setsockopt,
1616 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001617#endif
Neal Cardwell4fab9072014-08-14 12:40:05 -04001618 .mtu_reduced = tcp_v4_mtu_reduced,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619};
1620
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001621#ifdef CONFIG_TCP_MD5SIG
Stephen Hemmingerb2e4b3d2009-09-01 19:25:03 +00001622static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001623 .md5_lookup = tcp_v4_md5_lookup,
Adam Langley49a72df2008-07-19 00:01:42 -07001624 .calc_md5_hash = tcp_v4_md5_hash_skb,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001625 .md5_parse = tcp_v6_parse_md5_keys,
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001626};
David S. Millera9286302006-11-14 19:53:22 -08001627#endif
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629/* NOTE: A lot of things set to zero explicitly by call to
1630 * sk_alloc() so need not be done here.
1631 */
1632static int tcp_v6_init_sock(struct sock *sk)
1633{
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001634 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Neal Cardwell900f65d2012-04-19 09:55:21 +00001636 tcp_init_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
Arnaldo Carvalho de Melo8292a172005-12-13 23:15:52 -08001638 icsk->icsk_af_ops = &ipv6_specific;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001640#ifdef CONFIG_TCP_MD5SIG
David S. Millerac807fa2012-04-23 03:21:58 -04001641 tcp_sk(sk)->af_specific = &tcp_sock_ipv6_specific;
YOSHIFUJI Hideakicfb6eeb2006-11-14 19:07:45 -08001642#endif
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return 0;
1645}
1646
Brian Haley7d06b2e2008-06-14 17:04:49 -07001647static void tcp_v6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 tcp_v4_destroy_sock(sk);
Brian Haley7d06b2e2008-06-14 17:04:49 -07001650 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
YOSHIFUJI Hideaki952a10b2007-04-21 20:13:44 +09001653#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654/* Proc filesystem TCPv6 sock list dumping. */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001655static void get_openreq6(struct seq_file *seq,
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06001656 const struct sock *sk, struct request_sock *req, int i, kuid_t uid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 int ttd = req->expires - jiffies;
Eric Dumazet634fb9792013-10-09 15:21:29 -07001659 const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
1660 const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (ttd < 0)
1663 ttd = 0;
1664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 seq_printf(seq,
1666 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001667 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %d %d %pK\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 i,
1669 src->s6_addr32[0], src->s6_addr32[1],
1670 src->s6_addr32[2], src->s6_addr32[3],
Eric Dumazetb44084c2013-10-10 00:04:37 -07001671 inet_rsk(req)->ir_num,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 dest->s6_addr32[0], dest->s6_addr32[1],
1673 dest->s6_addr32[2], dest->s6_addr32[3],
Eric Dumazet634fb9792013-10-09 15:21:29 -07001674 ntohs(inet_rsk(req)->ir_rmt_port),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 TCP_SYN_RECV,
Weilong Chen4c99aa42013-12-19 18:44:34 +08001676 0, 0, /* could print option size, but that is af dependent. */
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001677 1, /* timers active (only the expire timer) */
1678 jiffies_to_clock_t(ttd),
Eric Dumazete6c022a2012-10-27 23:16:46 +00001679 req->num_timeout,
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06001680 from_kuid_munged(seq_user_ns(seq), uid),
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001681 0, /* non standard timer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 0, /* open_requests have no inode */
1683 0, req);
1684}
1685
1686static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1687{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001688 const struct in6_addr *dest, *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 __u16 destp, srcp;
1690 int timer_active;
1691 unsigned long timer_expires;
Eric Dumazetcf533ea2011-10-21 05:22:42 -04001692 const struct inet_sock *inet = inet_sk(sp);
1693 const struct tcp_sock *tp = tcp_sk(sp);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001694 const struct inet_connection_sock *icsk = inet_csk(sp);
Yuchung Cheng0a672f72014-05-11 20:22:12 -07001695 struct fastopen_queue *fastopenq = icsk->icsk_accept_queue.fastopenq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Eric Dumazetefe42082013-10-03 15:42:29 -07001697 dest = &sp->sk_v6_daddr;
1698 src = &sp->sk_v6_rcv_saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +00001699 destp = ntohs(inet->inet_dport);
1700 srcp = ntohs(inet->inet_sport);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001701
1702 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 timer_active = 1;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001704 timer_expires = icsk->icsk_timeout;
1705 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 timer_active = 4;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001707 timer_expires = icsk->icsk_timeout;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 } else if (timer_pending(&sp->sk_timer)) {
1709 timer_active = 2;
1710 timer_expires = sp->sk_timer.expires;
1711 } else {
1712 timer_active = 0;
1713 timer_expires = jiffies;
1714 }
1715
1716 seq_printf(seq,
1717 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Francesco Fuscod14c5ab2013-08-15 13:42:14 +02001718 "%02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %lu %lu %u %u %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 i,
1720 src->s6_addr32[0], src->s6_addr32[1],
1721 src->s6_addr32[2], src->s6_addr32[3], srcp,
1722 dest->s6_addr32[0], dest->s6_addr32[1],
1723 dest->s6_addr32[2], dest->s6_addr32[3], destp,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001724 sp->sk_state,
Sridhar Samudrala47da8ee2006-06-27 13:29:00 -07001725 tp->write_seq-tp->snd_una,
1726 (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 timer_active,
Eric Dumazeta399a802012-08-08 21:13:53 +00001728 jiffies_delta_to_clock_t(timer_expires - jiffies),
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001729 icsk->icsk_retransmits,
Eric W. Biedermana7cb5a42012-05-24 01:10:10 -06001730 from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -03001731 icsk->icsk_probes_out,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 sock_i_ino(sp),
1733 atomic_read(&sp->sk_refcnt), sp,
Stephen Hemminger7be87352008-06-27 20:00:19 -07001734 jiffies_to_clock_t(icsk->icsk_rto),
1735 jiffies_to_clock_t(icsk->icsk_ack.ato),
Weilong Chen4c99aa42013-12-19 18:44:34 +08001736 (icsk->icsk_ack.quick << 1) | icsk->icsk_ack.pingpong,
Ilpo Järvinen0b6a05c2009-09-15 01:30:10 -07001737 tp->snd_cwnd,
Yuchung Cheng0a672f72014-05-11 20:22:12 -07001738 sp->sk_state == TCP_LISTEN ?
1739 (fastopenq ? fastopenq->max_qlen : 0) :
1740 (tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 );
1742}
1743
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001744static void get_timewait6_sock(struct seq_file *seq,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -07001745 struct inet_timewait_sock *tw, int i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001747 const struct in6_addr *dest, *src;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 __u16 destp, srcp;
Eric Dumazet96f817f2013-10-03 14:27:25 -07001749 s32 delta = tw->tw_ttd - inet_tw_time_stamp();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Eric Dumazetefe42082013-10-03 15:42:29 -07001751 dest = &tw->tw_v6_daddr;
1752 src = &tw->tw_v6_rcv_saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 destp = ntohs(tw->tw_dport);
1754 srcp = ntohs(tw->tw_sport);
1755
1756 seq_printf(seq,
1757 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
Dan Rosenberg71338aa2011-05-23 12:17:35 +00001758 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 i,
1760 src->s6_addr32[0], src->s6_addr32[1],
1761 src->s6_addr32[2], src->s6_addr32[3], srcp,
1762 dest->s6_addr32[0], dest->s6_addr32[1],
1763 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1764 tw->tw_substate, 0, 0,
Eric Dumazeta399a802012-08-08 21:13:53 +00001765 3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 atomic_read(&tw->tw_refcnt), tw);
1767}
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769static int tcp6_seq_show(struct seq_file *seq, void *v)
1770{
1771 struct tcp_iter_state *st;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001772 struct sock *sk = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 if (v == SEQ_START_TOKEN) {
1775 seq_puts(seq,
1776 " sl "
1777 "local_address "
1778 "remote_address "
1779 "st tx_queue rx_queue tr tm->when retrnsmt"
1780 " uid timeout inode\n");
1781 goto out;
1782 }
1783 st = seq->private;
1784
1785 switch (st->state) {
1786 case TCP_SEQ_STATE_LISTENING:
1787 case TCP_SEQ_STATE_ESTABLISHED:
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001788 if (sk->sk_state == TCP_TIME_WAIT)
1789 get_timewait6_sock(seq, v, st->num);
1790 else
1791 get_tcp6_sock(seq, v, st->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 break;
1793 case TCP_SEQ_STATE_OPENREQ:
1794 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
1795 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797out:
1798 return 0;
1799}
1800
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001801static const struct file_operations tcp6_afinfo_seq_fops = {
1802 .owner = THIS_MODULE,
1803 .open = tcp_seq_open,
1804 .read = seq_read,
1805 .llseek = seq_lseek,
1806 .release = seq_release_net
1807};
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809static struct tcp_seq_afinfo tcp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 .name = "tcp6",
1811 .family = AF_INET6,
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001812 .seq_fops = &tcp6_afinfo_seq_fops,
Denis V. Lunev9427c4b2008-04-13 22:12:13 -07001813 .seq_ops = {
1814 .show = tcp6_seq_show,
1815 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816};
1817
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001818int __net_init tcp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001820 return tcp_proc_register(net, &tcp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001823void tcp6_proc_exit(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824{
Daniel Lezcano6f8b13b2008-03-21 04:14:45 -07001825 tcp_proc_unregister(net, &tcp6_seq_afinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827#endif
1828
Eric Dumazetf77d6022013-05-09 10:28:16 +00001829static void tcp_v6_clear_sk(struct sock *sk, int size)
1830{
1831 struct inet_sock *inet = inet_sk(sk);
1832
1833 /* we do not want to clear pinet6 field, because of RCU lookups */
1834 sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
1835
1836 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1837 memset(&inet->pinet6 + 1, 0, size);
1838}
1839
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840struct proto tcpv6_prot = {
1841 .name = "TCPv6",
1842 .owner = THIS_MODULE,
1843 .close = tcp_close,
1844 .connect = tcp_v6_connect,
1845 .disconnect = tcp_disconnect,
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -07001846 .accept = inet_csk_accept,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 .ioctl = tcp_ioctl,
1848 .init = tcp_v6_init_sock,
1849 .destroy = tcp_v6_destroy_sock,
1850 .shutdown = tcp_shutdown,
1851 .setsockopt = tcp_setsockopt,
1852 .getsockopt = tcp_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 .recvmsg = tcp_recvmsg,
Changli Gao7ba42912010-07-10 20:41:55 +00001854 .sendmsg = tcp_sendmsg,
1855 .sendpage = tcp_sendpage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 .backlog_rcv = tcp_v6_do_rcv,
Eric Dumazet46d3cea2012-07-11 05:50:31 +00001857 .release_cb = tcp_release_cb,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 .hash = tcp_v6_hash,
Arnaldo Carvalho de Meloab1e0a12008-02-03 04:06:04 -08001859 .unhash = inet_unhash,
1860 .get_port = inet_csk_get_port,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 .enter_memory_pressure = tcp_enter_memory_pressure,
Eric Dumazetc9bee3b72013-07-22 20:27:07 -07001862 .stream_memory_free = tcp_stream_memory_free,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 .sockets_allocated = &tcp_sockets_allocated,
1864 .memory_allocated = &tcp_memory_allocated,
1865 .memory_pressure = &tcp_memory_pressure,
Arnaldo Carvalho de Melo0a5578c2005-08-09 20:11:41 -07001866 .orphan_count = &tcp_orphan_count,
Eric W. Biedermana4fe34b2013-10-19 16:25:36 -07001867 .sysctl_mem = sysctl_tcp_mem,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 .sysctl_wmem = sysctl_tcp_wmem,
1869 .sysctl_rmem = sysctl_tcp_rmem,
1870 .max_header = MAX_TCP_HEADER,
1871 .obj_size = sizeof(struct tcp6_sock),
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001872 .slab_flags = SLAB_DESTROY_BY_RCU,
Arnaldo Carvalho de Melo6d6ee432005-12-13 23:25:19 -08001873 .twsk_prot = &tcp6_timewait_sock_ops,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -07001874 .rsk_prot = &tcp6_request_sock_ops,
Pavel Emelyanov39d8cda2008-03-22 16:50:58 -07001875 .h.hashinfo = &tcp_hashinfo,
Changli Gao7ba42912010-07-10 20:41:55 +00001876 .no_autobind = true,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001877#ifdef CONFIG_COMPAT
1878 .compat_setsockopt = compat_tcp_setsockopt,
1879 .compat_getsockopt = compat_tcp_getsockopt,
1880#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07001881#ifdef CONFIG_MEMCG_KMEM
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001882 .proto_cgroup = tcp_proto_cgroup,
1883#endif
Eric Dumazetf77d6022013-05-09 10:28:16 +00001884 .clear_sk = tcp_v6_clear_sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885};
1886
Alexey Dobriyan41135cc2009-09-14 12:22:28 +00001887static const struct inet6_protocol tcpv6_protocol = {
Eric Dumazetc7109982012-07-26 12:18:11 +00001888 .early_demux = tcp_v6_early_demux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 .handler = tcp_v6_rcv,
1890 .err_handler = tcp_v6_err,
1891 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1892};
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894static struct inet_protosw tcpv6_protosw = {
1895 .type = SOCK_STREAM,
1896 .protocol = IPPROTO_TCP,
1897 .prot = &tcpv6_prot,
1898 .ops = &inet6_stream_ops,
Arnaldo Carvalho de Melod83d8462005-12-13 23:26:10 -08001899 .flags = INET_PROTOSW_PERMANENT |
1900 INET_PROTOSW_ICSK,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901};
1902
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001903static int __net_init tcpv6_net_init(struct net *net)
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001904{
Denis V. Lunev56772422008-04-03 14:28:30 -07001905 return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
1906 SOCK_RAW, IPPROTO_TCP, net);
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001907}
1908
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001909static void __net_exit tcpv6_net_exit(struct net *net)
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001910{
Denis V. Lunev56772422008-04-03 14:28:30 -07001911 inet_ctl_sock_destroy(net->ipv6.tcp_sk);
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00001912}
1913
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001914static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00001915{
1916 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001917}
1918
1919static struct pernet_operations tcpv6_net_ops = {
Eric W. Biedermanb099ce22009-12-03 02:29:09 +00001920 .init = tcpv6_net_init,
1921 .exit = tcpv6_net_exit,
1922 .exit_batch = tcpv6_net_exit_batch,
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001923};
1924
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001925int __init tcpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001927 int ret;
David Woodhouseae0f7d52006-01-11 15:53:04 -08001928
Vlad Yasevich33362882012-11-15 08:49:15 +00001929 ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
1930 if (ret)
Vlad Yasevichc6b641a2012-11-15 08:49:22 +00001931 goto out;
Vlad Yasevich33362882012-11-15 08:49:15 +00001932
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001933 /* register inet6 protocol */
1934 ret = inet6_register_protosw(&tcpv6_protosw);
1935 if (ret)
1936 goto out_tcpv6_protocol;
1937
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001938 ret = register_pernet_subsys(&tcpv6_net_ops);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001939 if (ret)
1940 goto out_tcpv6_protosw;
1941out:
1942 return ret;
1943
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001944out_tcpv6_protosw:
1945 inet6_unregister_protosw(&tcpv6_protosw);
Vlad Yasevich33362882012-11-15 08:49:15 +00001946out_tcpv6_protocol:
1947 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001948 goto out;
1949}
1950
Daniel Lezcano09f77092007-12-13 05:34:58 -08001951void tcpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001952{
Daniel Lezcano93ec9262008-03-07 11:16:02 -08001953 unregister_pernet_subsys(&tcpv6_net_ops);
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001954 inet6_unregister_protosw(&tcpv6_protosw);
1955 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956}