xref: /linux/net/ipv6/tcp_ipv6.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	TCP over IPv6
4  *	Linux INET6 implementation
5  *
6  *	Authors:
7  *	Pedro Roque		<roque@di.fc.ul.pt>
8  *
9  *	Based on:
10  *	linux/net/ipv4/tcp.c
11  *	linux/net/ipv4/tcp_input.c
12  *	linux/net/ipv4/tcp_output.c
13  *
14  *	Fixes:
15  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
16  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
17  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
18  *					a single port at the same time.
19  *	YOSHIFUJI Hideaki @USAGI:	convert /proc/net/tcp6 to seq_file.
20  */
21 
22 #include <linux/bottom_half.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/jiffies.h>
30 #include <linux/in.h>
31 #include <linux/in6.h>
32 #include <linux/netdevice.h>
33 #include <linux/init.h>
34 #include <linux/jhash.h>
35 #include <linux/ipsec.h>
36 #include <linux/times.h>
37 #include <linux/slab.h>
38 #include <linux/uaccess.h>
39 #include <linux/ipv6.h>
40 #include <linux/icmpv6.h>
41 #include <linux/random.h>
42 #include <linux/indirect_call_wrapper.h>
43 
44 #include <net/aligned_data.h>
45 #include <net/tcp.h>
46 #include <net/ndisc.h>
47 #include <net/inet6_hashtables.h>
48 #include <net/inet6_connection_sock.h>
49 #include <net/ipv6.h>
50 #include <net/transp_v6.h>
51 #include <net/addrconf.h>
52 #include <net/ip6_route.h>
53 #include <net/ip6_checksum.h>
54 #include <net/inet_ecn.h>
55 #include <net/protocol.h>
56 #include <net/xfrm.h>
57 #include <net/snmp.h>
58 #include <net/dsfield.h>
59 #include <net/timewait_sock.h>
60 #include <net/inet_common.h>
61 #include <net/secure_seq.h>
62 #include <net/hotdata.h>
63 #include <net/busy_poll.h>
64 #include <net/rstreason.h>
65 #include <net/psp.h>
66 
67 #include <linux/proc_fs.h>
68 #include <linux/seq_file.h>
69 
70 #include <crypto/md5.h>
71 #include <crypto/utils.h>
72 
73 #include <trace/events/tcp.h>
74 
75 static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb,
76 			      enum sk_rst_reason reason);
77 static void	tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
78 				      struct request_sock *req);
79 
80 INDIRECT_CALLABLE_SCOPE int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
81 
82 static const struct inet_connection_sock_af_ops ipv6_mapped;
83 const struct inet_connection_sock_af_ops ipv6_specific;
84 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
85 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
86 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
87 #endif
88 
89 /* Helper returning the inet6 address from a given tcp socket.
90  * It can be used in TCP stack instead of inet6_sk(sk).
91  * This avoids a dereference and allow compiler optimizations.
92  * It is a specialized version of inet6_sk_generic().
93  */
94 #define tcp_inet6_sk(sk) (&container_of_const(tcp_sk(sk), \
95 					      struct tcp6_sock, tcp)->inet6)
96 
97 static void inet6_sk_rx_dst_set(struct sock *sk, const struct sk_buff *skb)
98 {
99 	struct dst_entry *dst = skb_dst(skb);
100 
101 	if (dst && dst_hold_safe(dst)) {
102 		rcu_assign_pointer(sk->sk_rx_dst, dst);
103 		sk->sk_rx_dst_ifindex = skb->skb_iif;
104 		sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
105 	}
106 }
107 
108 INDIRECT_CALLABLE_SCOPE union tcp_seq_and_ts_off
109 tcp_v6_init_seq_and_ts_off(const struct net *net, const struct sk_buff *skb)
110 {
111 	return secure_tcpv6_seq_and_ts_off(net,
112 					   ipv6_hdr(skb)->daddr.s6_addr32,
113 					   ipv6_hdr(skb)->saddr.s6_addr32,
114 					   tcp_hdr(skb)->dest,
115 					   tcp_hdr(skb)->source);
116 }
117 
118 static int tcp_v6_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
119 			      int addr_len)
120 {
121 	/* This check is replicated from tcp_v6_connect() and intended to
122 	 * prevent BPF program called below from accessing bytes that are out
123 	 * of the bound specified by user in addr_len.
124 	 */
125 	if (addr_len < SIN6_LEN_RFC2133)
126 		return -EINVAL;
127 
128 	sock_owned_by_me(sk);
129 
130 	return BPF_CGROUP_RUN_PROG_INET6_CONNECT(sk, uaddr, &addr_len);
131 }
132 
133 static int tcp_v6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
134 			  int addr_len)
135 {
136 	struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
137 	struct inet_connection_sock *icsk = inet_csk(sk);
138 	struct inet_timewait_death_row *tcp_death_row;
139 	struct ipv6_pinfo *np = tcp_inet6_sk(sk);
140 	struct in6_addr *saddr = NULL, *final_p;
141 	struct inet_sock *inet = inet_sk(sk);
142 	struct tcp_sock *tp = tcp_sk(sk);
143 	struct net *net = sock_net(sk);
144 	struct ipv6_txoptions *opt;
145 	struct dst_entry *dst;
146 	struct flowi6 *fl6;
147 	int addr_type;
148 	int err;
149 
150 	if (addr_len < SIN6_LEN_RFC2133)
151 		return -EINVAL;
152 
153 	if (usin->sin6_family != AF_INET6)
154 		return -EAFNOSUPPORT;
155 
156 	if (psp_sk_assoc(sk))
157 		return -EINVAL;
158 
159 	fl6 = &inet_sk(sk)->cork.fl.u.ip6;
160 	memset(fl6, 0, sizeof(*fl6));
161 
162 	if (inet6_test_bit(SNDFLOW, sk)) {
163 		fl6->flowlabel = usin->sin6_flowinfo & IPV6_FLOWINFO_MASK;
164 		IP6_ECN_flow_init(fl6->flowlabel);
165 		if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
166 			struct ip6_flowlabel *flowlabel;
167 			flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
168 			if (IS_ERR(flowlabel))
169 				return -EINVAL;
170 			fl6_sock_release(flowlabel);
171 		}
172 	}
173 
174 	/*
175 	 *	connect() to INADDR_ANY means loopback (BSD'ism).
176 	 */
177 
178 	if (ipv6_addr_any(&usin->sin6_addr)) {
179 		if (ipv6_addr_v4mapped(&sk->sk_v6_rcv_saddr))
180 			ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
181 					       &usin->sin6_addr);
182 		else
183 			usin->sin6_addr = in6addr_loopback;
184 	}
185 
186 	addr_type = ipv6_addr_type(&usin->sin6_addr);
187 
188 	if (addr_type & IPV6_ADDR_MULTICAST)
189 		return -ENETUNREACH;
190 
191 	if (addr_type&IPV6_ADDR_LINKLOCAL) {
192 		if (addr_len >= sizeof(struct sockaddr_in6) &&
193 		    usin->sin6_scope_id) {
194 			/* If interface is set while binding, indices
195 			 * must coincide.
196 			 */
197 			if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id))
198 				return -EINVAL;
199 
200 			sk->sk_bound_dev_if = usin->sin6_scope_id;
201 		}
202 
203 		/* Connect to link-local address requires an interface */
204 		if (!sk->sk_bound_dev_if)
205 			return -EINVAL;
206 	}
207 
208 	if (tp->rx_opt.ts_recent_stamp &&
209 	    !ipv6_addr_equal(&sk->sk_v6_daddr, &usin->sin6_addr)) {
210 		tp->rx_opt.ts_recent = 0;
211 		tp->rx_opt.ts_recent_stamp = 0;
212 		WRITE_ONCE(tp->write_seq, 0);
213 	}
214 
215 	sk->sk_v6_daddr = usin->sin6_addr;
216 	np->flow_label = fl6->flowlabel;
217 
218 	/*
219 	 *	TCP over IPv4
220 	 */
221 
222 	if (addr_type & IPV6_ADDR_MAPPED) {
223 		u32 exthdrlen = icsk->icsk_ext_hdr_len;
224 		struct sockaddr_in sin;
225 
226 		if (ipv6_only_sock(sk))
227 			return -ENETUNREACH;
228 
229 		sin.sin_family = AF_INET;
230 		sin.sin_port = usin->sin6_port;
231 		sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
232 
233 		/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
234 		WRITE_ONCE(icsk->icsk_af_ops, &ipv6_mapped);
235 		if (sk_is_mptcp(sk))
236 			mptcpv6_handle_mapped(sk, true);
237 		sk->sk_backlog_rcv = tcp_v4_do_rcv;
238 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
239 		tp->af_specific = &tcp_sock_ipv6_mapped_specific;
240 #endif
241 
242 		err = tcp_v4_connect(sk, (struct sockaddr_unsized *)&sin, sizeof(sin));
243 
244 		if (err) {
245 			icsk->icsk_ext_hdr_len = exthdrlen;
246 			/* Paired with READ_ONCE() in tcp_(get|set)sockopt() */
247 			WRITE_ONCE(icsk->icsk_af_ops, &ipv6_specific);
248 			if (sk_is_mptcp(sk))
249 				mptcpv6_handle_mapped(sk, false);
250 			sk->sk_backlog_rcv = tcp_v6_do_rcv;
251 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
252 			tp->af_specific = &tcp_sock_ipv6_specific;
253 #endif
254 			goto failure;
255 		}
256 		np->saddr = sk->sk_v6_rcv_saddr;
257 
258 		return err;
259 	}
260 
261 	if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
262 		saddr = &sk->sk_v6_rcv_saddr;
263 
264 	sk_set_txhash(sk);
265 
266 	fl6->flowi6_proto = IPPROTO_TCP;
267 	fl6->daddr = sk->sk_v6_daddr;
268 	fl6->saddr = saddr ? *saddr : np->saddr;
269 	fl6->flowlabel = ip6_make_flowinfo(np->tclass, np->flow_label);
270 	fl6->flowi6_oif = sk->sk_bound_dev_if;
271 	fl6->flowi6_mark = sk->sk_mark;
272 	fl6->fl6_dport = usin->sin6_port;
273 	fl6->fl6_sport = inet->inet_sport;
274 	if (IS_ENABLED(CONFIG_IP_ROUTE_MULTIPATH) && !fl6->fl6_sport)
275 		fl6->flowi6_flags = FLOWI_FLAG_ANY_SPORT;
276 	fl6->flowi6_uid = sk_uid(sk);
277 
278 	opt = rcu_dereference_protected(np->opt, lockdep_sock_is_held(sk));
279 	final_p = fl6_update_dst(fl6, opt, &np->final);
280 
281 	security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
282 
283 	/* Non-zero mp_hash bypasses rt6_multipath_hash() in
284 	 * fib6_select_path(), letting txhash control ECMP path
285 	 * selection so that sk_rethink_txhash() rehashes onto a
286 	 * different path.  Policies 1-3 derive a deterministic
287 	 * hash from the flow keys and must not be overridden.
288 	 */
289 	ip6_ecmp_set_mp_hash(net, fl6, sk->sk_txhash);
290 
291 	dst = ip6_dst_lookup_flow(net, sk, fl6, final_p);
292 	if (IS_ERR(dst)) {
293 		err = PTR_ERR(dst);
294 		goto failure;
295 	}
296 
297 	tp->tcp_usec_ts = dst_tcp_usec_ts(dst);
298 	tcp_death_row = &sock_net(sk)->ipv4.tcp_death_row;
299 
300 	if (!saddr) {
301 		saddr = &fl6->saddr;
302 
303 		err = inet_bhash2_update_saddr(sk, saddr, AF_INET6);
304 		if (err) {
305 			dst_release(dst);
306 			goto failure;
307 		}
308 	}
309 
310 	/* set the source address */
311 	np->saddr = *saddr;
312 	inet->inet_rcv_saddr = LOOPBACK4_IPV6;
313 
314 	sk->sk_gso_type = SKB_GSO_TCPV6;
315 	ip6_dst_store(sk, dst, false, false);
316 
317 	icsk->icsk_ext_hdr_len = 0;
318 	if (opt)
319 		icsk->icsk_ext_hdr_len = opt->opt_flen +
320 					 opt->opt_nflen;
321 
322 	tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
323 
324 	inet->inet_dport = usin->sin6_port;
325 
326 	tcp_set_state(sk, TCP_SYN_SENT);
327 	err = inet6_hash_connect(tcp_death_row, sk);
328 	if (err)
329 		goto late_failure;
330 
331 	if (likely(!tp->repair)) {
332 		union tcp_seq_and_ts_off st;
333 
334 		st = secure_tcpv6_seq_and_ts_off(net,
335 						 np->saddr.s6_addr32,
336 						 sk->sk_v6_daddr.s6_addr32,
337 						 inet->inet_sport,
338 						 inet->inet_dport);
339 		if (!tp->write_seq)
340 			WRITE_ONCE(tp->write_seq, st.seq);
341 		WRITE_ONCE(tp->tsoffset, st.ts_off);
342 	}
343 
344 	if (tcp_fastopen_defer_connect(sk, &err))
345 		return err;
346 	if (err)
347 		goto late_failure;
348 
349 	err = tcp_connect(sk);
350 	if (err)
351 		goto late_failure;
352 
353 	return 0;
354 
355 late_failure:
356 	tcp_set_state(sk, TCP_CLOSE);
357 	inet_bhash2_reset_saddr(sk);
358 failure:
359 	inet->inet_dport = 0;
360 	sk->sk_route_caps = 0;
361 	return err;
362 }
363 
364 static struct dst_entry *inet6_csk_update_pmtu(struct sock *sk, u32 mtu)
365 {
366 	struct flowi6 *fl6 = &inet_sk(sk)->cork.fl.u.ip6;
367 	struct dst_entry *dst;
368 
369 	dst = inet6_csk_route_socket(sk, fl6);
370 
371 	if (IS_ERR(dst))
372 		return NULL;
373 	dst->ops->update_pmtu(dst, sk, NULL, mtu, true);
374 
375 	dst = inet6_csk_route_socket(sk, fl6);
376 	return IS_ERR(dst) ? NULL : dst;
377 }
378 
379 static void tcp_v6_mtu_reduced(struct sock *sk)
380 {
381 	struct dst_entry *dst;
382 	u32 mtu, dmtu;
383 
384 	if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
385 		return;
386 
387 	mtu = READ_ONCE(tcp_sk(sk)->mtu_info);
388 
389 	/* Drop requests trying to increase our current mss.
390 	 * Check done in __ip6_rt_update_pmtu() is too late.
391 	 */
392 	if (tcp_mtu_to_mss(sk, mtu) >= tcp_sk(sk)->mss_cache)
393 		return;
394 
395 	dst = inet6_csk_update_pmtu(sk, mtu);
396 	if (!dst)
397 		return;
398 
399 	dmtu = dst6_mtu(dst);
400 	if (inet_csk(sk)->icsk_pmtu_cookie > dmtu) {
401 		tcp_sync_mss(sk, dmtu);
402 		tcp_simple_retransmit(sk);
403 	}
404 }
405 
406 static int tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
407 		u8 type, u8 code, int offset, __be32 info)
408 {
409 	const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
410 	const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
411 	struct net *net = dev_net_rcu(skb->dev);
412 	struct request_sock *fastopen;
413 	struct ipv6_pinfo *np;
414 	struct tcp_sock *tp;
415 	__u32 seq, snd_una;
416 	struct sock *sk;
417 	bool fatal;
418 	int err;
419 
420 	sk = __inet6_lookup_established(net, &hdr->daddr, th->dest,
421 					&hdr->saddr, ntohs(th->source),
422 					skb->dev->ifindex, inet6_sdif(skb));
423 
424 	if (!sk) {
425 		__ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
426 				  ICMP6_MIB_INERRORS);
427 		return -ENOENT;
428 	}
429 
430 	if (sk->sk_state == TCP_TIME_WAIT) {
431 		/* To increase the counter of ignored icmps for TCP-AO */
432 		tcp_ao_ignore_icmp(sk, AF_INET6, type, code);
433 		inet_twsk_put(inet_twsk(sk));
434 		return 0;
435 	}
436 	seq = ntohl(th->seq);
437 	fatal = icmpv6_err_convert(type, code, &err);
438 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
439 		tcp_req_err(sk, seq, fatal);
440 		return 0;
441 	}
442 
443 	if (tcp_ao_ignore_icmp(sk, AF_INET6, type, code)) {
444 		sock_put(sk);
445 		return 0;
446 	}
447 
448 	bh_lock_sock(sk);
449 	if (sock_owned_by_user(sk) && type != ICMPV6_PKT_TOOBIG)
450 		__NET_INC_STATS(net, LINUX_MIB_LOCKDROPPEDICMPS);
451 
452 	if (sk->sk_state == TCP_CLOSE || inet_test_bit(DEFER_CONNECT, sk))
453 		goto out;
454 
455 	if (static_branch_unlikely(&ip6_min_hopcount)) {
456 		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
457 		if (ipv6_hdr(skb)->hop_limit < READ_ONCE(tcp_inet6_sk(sk)->min_hopcount)) {
458 			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
459 			goto out;
460 		}
461 	}
462 
463 	tp = tcp_sk(sk);
464 	/* XXX (TFO) - tp->snd_una should be ISN (tcp_create_openreq_child() */
465 	fastopen = rcu_dereference(tp->fastopen_rsk);
466 	snd_una = fastopen ? tcp_rsk(fastopen)->snt_isn : tp->snd_una;
467 	if (sk->sk_state != TCP_LISTEN &&
468 	    !between(seq, snd_una, tp->snd_nxt)) {
469 		__NET_INC_STATS(net, LINUX_MIB_OUTOFWINDOWICMPS);
470 		goto out;
471 	}
472 
473 	np = tcp_inet6_sk(sk);
474 
475 	if (type == NDISC_REDIRECT) {
476 		if (!sock_owned_by_user(sk)) {
477 			struct dst_entry *dst = __sk_dst_check(sk, np->dst_cookie);
478 
479 			if (dst)
480 				dst->ops->redirect(dst, sk, skb);
481 		}
482 		goto out;
483 	}
484 
485 	if (type == ICMPV6_PKT_TOOBIG) {
486 		u32 mtu = ntohl(info);
487 
488 		/* We are not interested in TCP_LISTEN and open_requests
489 		 * (SYN-ACKs send out by Linux are always <576bytes so
490 		 * they should go through unfragmented).
491 		 */
492 		if (sk->sk_state == TCP_LISTEN)
493 			goto out;
494 
495 		if (!ip6_sk_accept_pmtu(sk))
496 			goto out;
497 
498 		if (mtu < IPV6_MIN_MTU)
499 			goto out;
500 
501 		WRITE_ONCE(tp->mtu_info, mtu);
502 
503 		if (!sock_owned_by_user(sk))
504 			tcp_v6_mtu_reduced(sk);
505 		else if (!test_and_set_bit(TCP_MTU_REDUCED_DEFERRED,
506 					   &sk->sk_tsq_flags))
507 			sock_hold(sk);
508 		goto out;
509 	}
510 
511 
512 	/* Might be for an request_sock */
513 	switch (sk->sk_state) {
514 	case TCP_SYN_SENT:
515 	case TCP_SYN_RECV:
516 		/* Only in fast or simultaneous open. If a fast open socket is
517 		 * already accepted it is treated as a connected one below.
518 		 */
519 		if (fastopen && !fastopen->sk)
520 			break;
521 
522 		ipv6_icmp_error(sk, skb, err, th->dest, ntohl(info), (u8 *)th);
523 
524 		if (!sock_owned_by_user(sk))
525 			tcp_done_with_error(sk, err);
526 		else
527 			WRITE_ONCE(sk->sk_err_soft, err);
528 		goto out;
529 	case TCP_LISTEN:
530 		break;
531 	default:
532 		/* check if this ICMP message allows revert of backoff.
533 		 * (see RFC 6069)
534 		 */
535 		if (!fastopen && type == ICMPV6_DEST_UNREACH &&
536 		    code == ICMPV6_NOROUTE)
537 			tcp_ld_RTO_revert(sk, seq);
538 	}
539 
540 	if (!sock_owned_by_user(sk) && inet6_test_bit(RECVERR6, sk)) {
541 		WRITE_ONCE(sk->sk_err, err);
542 		sk_error_report(sk);
543 	} else {
544 		WRITE_ONCE(sk->sk_err_soft, err);
545 	}
546 out:
547 	bh_unlock_sock(sk);
548 	sock_put(sk);
549 	return 0;
550 }
551 
552 
553 static int tcp_v6_send_synack(const struct sock *sk, struct dst_entry *dst,
554 			      struct flowi *fl,
555 			      struct request_sock *req,
556 			      struct tcp_fastopen_cookie *foc,
557 			      enum tcp_synack_type synack_type,
558 			      struct sk_buff *syn_skb)
559 {
560 	struct inet_request_sock *ireq = inet_rsk(req);
561 	const struct ipv6_pinfo *np = tcp_inet6_sk(sk);
562 	struct ipv6_txoptions *opt;
563 	struct flowi6 *fl6 = &fl->u.ip6;
564 	struct sk_buff *skb;
565 	int err = -ENOMEM;
566 	u8 tclass;
567 
568 	/* First, grab a route. */
569 	if (!dst && (dst = inet6_csk_route_req(sk, NULL, fl6, req,
570 					       IPPROTO_TCP)) == NULL)
571 		goto done;
572 
573 	skb = tcp_make_synack(sk, dst, req, foc, synack_type, syn_skb);
574 
575 	if (skb) {
576 		tcp_rsk(req)->syn_ect_snt = np->tclass & INET_ECN_MASK;
577 		__tcp_v6_send_check(skb, &ireq->ir_v6_loc_addr,
578 				    &ireq->ir_v6_rmt_addr);
579 
580 		fl6->daddr = ireq->ir_v6_rmt_addr;
581 		if (inet6_test_bit(REPFLOW, sk))
582 			fl6->flowlabel = tcp_rsk(req)->syn_flowlabel;
583 
584 		tclass = READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos) ?
585 				(tcp_rsk(req)->syn_tos & ~INET_ECN_MASK) |
586 				(np->tclass & INET_ECN_MASK) :
587 				np->tclass;
588 
589 		if (!INET_ECN_is_capable(tclass) &&
590 		    tcp_bpf_ca_needs_ecn((struct sock *)req))
591 			tclass |= INET_ECN_ECT_0;
592 
593 		rcu_read_lock();
594 		opt = ireq->ipv6_opt;
595 		if (!opt)
596 			opt = rcu_dereference(np->opt);
597 		err = ip6_xmit(sk, skb, fl6, skb->mark ? : READ_ONCE(sk->sk_mark),
598 			       opt, tclass, READ_ONCE(sk->sk_priority));
599 		rcu_read_unlock();
600 		err = net_xmit_eval(err);
601 	}
602 
603 done:
604 	return err;
605 }
606 
607 
608 static void tcp_v6_reqsk_destructor(struct request_sock *req)
609 {
610 	kfree_rcu(inet_rsk(req)->ipv6_opt, rcu);
611 	consume_skb(inet_rsk(req)->pktopts);
612 }
613 
614 #ifdef CONFIG_TCP_MD5SIG
615 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(const struct sock *sk,
616 						   const struct in6_addr *addr,
617 						   int l3index)
618 {
619 	return tcp_md5_do_lookup(sk, l3index,
620 				 (union tcp_md5_addr *)addr, AF_INET6);
621 }
622 
623 static struct tcp_md5sig_key *tcp_v6_md5_lookup(const struct sock *sk,
624 						const struct sock *addr_sk)
625 {
626 	int l3index;
627 
628 	l3index = l3mdev_master_ifindex_by_index(sock_net(sk),
629 						 addr_sk->sk_bound_dev_if);
630 	return tcp_v6_md5_do_lookup(sk, &addr_sk->sk_v6_daddr,
631 				    l3index);
632 }
633 
634 static int tcp_v6_parse_md5_keys(struct sock *sk, int optname,
635 				 sockptr_t optval, int optlen)
636 {
637 	struct tcp_md5sig cmd;
638 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
639 	union tcp_ao_addr *addr;
640 	int l3index = 0;
641 	u8 prefixlen;
642 	bool l3flag;
643 	u8 flags;
644 
645 	if (optlen < sizeof(cmd))
646 		return -EINVAL;
647 
648 	if (copy_from_sockptr(&cmd, optval, sizeof(cmd)))
649 		return -EFAULT;
650 
651 	if (sin6->sin6_family != AF_INET6)
652 		return -EINVAL;
653 
654 	flags = cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX;
655 	l3flag = cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX;
656 
657 	if (optname == TCP_MD5SIG_EXT &&
658 	    cmd.tcpm_flags & TCP_MD5SIG_FLAG_PREFIX) {
659 		prefixlen = cmd.tcpm_prefixlen;
660 		if (prefixlen > 128 || (ipv6_addr_v4mapped(&sin6->sin6_addr) &&
661 					prefixlen > 32))
662 			return -EINVAL;
663 	} else {
664 		prefixlen = ipv6_addr_v4mapped(&sin6->sin6_addr) ? 32 : 128;
665 	}
666 
667 	if (optname == TCP_MD5SIG_EXT && cmd.tcpm_ifindex &&
668 	    cmd.tcpm_flags & TCP_MD5SIG_FLAG_IFINDEX) {
669 		struct net_device *dev;
670 
671 		rcu_read_lock();
672 		dev = dev_get_by_index_rcu(sock_net(sk), cmd.tcpm_ifindex);
673 		if (dev && netif_is_l3_master(dev))
674 			l3index = dev->ifindex;
675 		rcu_read_unlock();
676 
677 		/* ok to reference set/not set outside of rcu;
678 		 * right now device MUST be an L3 master
679 		 */
680 		if (!dev || !l3index)
681 			return -EINVAL;
682 	}
683 
684 	if (!cmd.tcpm_keylen) {
685 		if (ipv6_addr_v4mapped(&sin6->sin6_addr))
686 			return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3],
687 					      AF_INET, prefixlen,
688 					      l3index, flags);
689 		return tcp_md5_do_del(sk, (union tcp_md5_addr *)&sin6->sin6_addr,
690 				      AF_INET6, prefixlen, l3index, flags);
691 	}
692 
693 	if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
694 		return -EINVAL;
695 
696 	if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
697 		addr = (union tcp_md5_addr *)&sin6->sin6_addr.s6_addr32[3];
698 
699 		/* Don't allow keys for peers that have a matching TCP-AO key.
700 		 * See the comment in tcp_ao_add_cmd()
701 		 */
702 		if (tcp_ao_required(sk, addr, AF_INET,
703 				    l3flag ? l3index : -1, false))
704 			return -EKEYREJECTED;
705 		return tcp_md5_do_add(sk, addr,
706 				      AF_INET, prefixlen, l3index, flags,
707 				      cmd.tcpm_key, cmd.tcpm_keylen);
708 	}
709 
710 	addr = (union tcp_md5_addr *)&sin6->sin6_addr;
711 
712 	/* Don't allow keys for peers that have a matching TCP-AO key.
713 	 * See the comment in tcp_ao_add_cmd()
714 	 */
715 	if (tcp_ao_required(sk, addr, AF_INET6, l3flag ? l3index : -1, false))
716 		return -EKEYREJECTED;
717 
718 	return tcp_md5_do_add(sk, addr, AF_INET6, prefixlen, l3index, flags,
719 			      cmd.tcpm_key, cmd.tcpm_keylen);
720 }
721 
722 static void tcp_v6_md5_hash_headers(struct md5_ctx *ctx,
723 				    const struct in6_addr *daddr,
724 				    const struct in6_addr *saddr,
725 				    const struct tcphdr *th, int nbytes)
726 {
727 	struct {
728 		struct tcp6_pseudohdr ip; /* TCP pseudo-header (RFC2460) */
729 		struct tcphdr tcp;
730 	} h;
731 
732 	h.ip.saddr = *saddr;
733 	h.ip.daddr = *daddr;
734 	h.ip.protocol = cpu_to_be32(IPPROTO_TCP);
735 	h.ip.len = cpu_to_be32(nbytes);
736 	h.tcp = *th;
737 	h.tcp.check = 0;
738 	md5_update(ctx, (const u8 *)&h, sizeof(h.ip) + sizeof(h.tcp));
739 }
740 
741 static noinline_for_stack void
742 tcp_v6_md5_hash_hdr(char *md5_hash, const struct tcp_md5sig_key *key,
743 		    const struct in6_addr *daddr, struct in6_addr *saddr,
744 		    const struct tcphdr *th)
745 {
746 	struct md5_ctx ctx;
747 
748 	md5_init(&ctx);
749 	tcp_v6_md5_hash_headers(&ctx, daddr, saddr, th, th->doff << 2);
750 	tcp_md5_hash_key(&ctx, key);
751 	md5_final(&ctx, md5_hash);
752 }
753 
754 static noinline_for_stack void
755 tcp_v6_md5_hash_skb(char *md5_hash, const struct tcp_md5sig_key *key,
756 		    const struct sock *sk, const struct sk_buff *skb)
757 {
758 	const struct tcphdr *th = tcp_hdr(skb);
759 	const struct in6_addr *saddr, *daddr;
760 	struct md5_ctx ctx;
761 
762 	if (sk) { /* valid for establish/request sockets */
763 		saddr = &sk->sk_v6_rcv_saddr;
764 		daddr = &sk->sk_v6_daddr;
765 	} else {
766 		const struct ipv6hdr *ip6h = ipv6_hdr(skb);
767 		saddr = &ip6h->saddr;
768 		daddr = &ip6h->daddr;
769 	}
770 
771 	md5_init(&ctx);
772 	tcp_v6_md5_hash_headers(&ctx, daddr, saddr, th, skb->len);
773 	tcp_md5_hash_skb_data(&ctx, skb, th->doff << 2);
774 	tcp_md5_hash_key(&ctx, key);
775 	md5_final(&ctx, md5_hash);
776 }
777 #endif
778 
779 static void tcp_v6_init_req(struct request_sock *req,
780 			    const struct sock *sk_listener,
781 			    struct sk_buff *skb,
782 			    u32 tw_isn)
783 {
784 	bool l3_slave = ipv6_l3mdev_skb(TCP_SKB_CB(skb)->header.h6.flags);
785 	struct inet_request_sock *ireq = inet_rsk(req);
786 	const struct ipv6_pinfo *np = tcp_inet6_sk(sk_listener);
787 
788 	ireq->ir_v6_rmt_addr = ipv6_hdr(skb)->saddr;
789 	ireq->ir_v6_loc_addr = ipv6_hdr(skb)->daddr;
790 	ireq->ir_rmt_addr = LOOPBACK4_IPV6;
791 	ireq->ir_loc_addr = LOOPBACK4_IPV6;
792 	tcp_rsk(req)->syn_flowlabel = ip6_flowlabel(ipv6_hdr(skb));
793 
794 	/* So that link locals have meaning */
795 	if ((!sk_listener->sk_bound_dev_if || l3_slave) &&
796 	    ipv6_addr_type(&ireq->ir_v6_rmt_addr) & IPV6_ADDR_LINKLOCAL)
797 		ireq->ir_iif = tcp_v6_iif(skb);
798 
799 	if (!tw_isn &&
800 	    (ipv6_opt_accepted(sk_listener, skb, &TCP_SKB_CB(skb)->header.h6) ||
801 	     np->rxopt.bits.rxinfo ||
802 	     np->rxopt.bits.rxoinfo || np->rxopt.bits.rxhlim ||
803 	     np->rxopt.bits.rxohlim || inet6_test_bit(REPFLOW, sk_listener))) {
804 		refcount_inc(&skb->users);
805 		ireq->pktopts = skb;
806 	}
807 }
808 
809 static struct dst_entry *tcp_v6_route_req(const struct sock *sk,
810 					  struct sk_buff *skb,
811 					  struct flowi *fl,
812 					  struct request_sock *req,
813 					  u32 tw_isn)
814 {
815 	tcp_v6_init_req(req, sk, skb, tw_isn);
816 
817 	if (security_inet_conn_request(sk, skb, req))
818 		return NULL;
819 
820 	return inet6_csk_route_req(sk, NULL, &fl->u.ip6, req, IPPROTO_TCP);
821 }
822 
823 struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
824 	.family		=	AF_INET6,
825 	.obj_size	=	sizeof(struct tcp6_request_sock),
826 	.send_ack	=	tcp_v6_reqsk_send_ack,
827 	.destructor	=	tcp_v6_reqsk_destructor,
828 	.send_reset	=	tcp_v6_send_reset,
829 };
830 
831 const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
832 	.mss_clamp	=	IPV6_MIN_MTU - sizeof(struct tcphdr) -
833 				sizeof(struct ipv6hdr),
834 #ifdef CONFIG_TCP_MD5SIG
835 	.req_md5_lookup	=	tcp_v6_md5_lookup,
836 	.calc_md5_hash	=	tcp_v6_md5_hash_skb,
837 #endif
838 #ifdef CONFIG_TCP_AO
839 	.ao_lookup	=	tcp_v6_ao_lookup_rsk,
840 	.ao_calc_key	=	tcp_v6_ao_calc_key_rsk,
841 	.ao_synack_hash =	tcp_v6_ao_synack_hash,
842 #endif
843 #ifdef CONFIG_SYN_COOKIES
844 	.cookie_init_seq =	cookie_v6_init_sequence,
845 #endif
846 	.route_req	=	tcp_v6_route_req,
847 	.init_seq_and_ts_off	= tcp_v6_init_seq_and_ts_off,
848 	.send_synack	=	tcp_v6_send_synack,
849 };
850 
851 static void tcp_v6_send_response(const struct sock *sk, struct sk_buff *skb, u32 seq,
852 				 u32 ack, u32 win, u32 tsval, u32 tsecr,
853 				 int oif, int rst, u8 tclass, __be32 label,
854 				 u32 priority, u32 txhash, struct tcp_key *key)
855 {
856 	struct net *net = sk ? sock_net(sk) : skb_dst_dev_net_rcu(skb);
857 	unsigned int tot_len = sizeof(struct tcphdr);
858 	struct sock *ctl_sk = net->ipv6.tcp_sk;
859 	const struct tcphdr *th = tcp_hdr(skb);
860 	__be32 mrst = 0, *topt;
861 	struct dst_entry *dst;
862 	struct sk_buff *buff;
863 	struct tcphdr *t1;
864 	struct flowi6 fl6;
865 	u32 mark = 0;
866 
867 	if (tsecr)
868 		tot_len += TCPOLEN_TSTAMP_ALIGNED;
869 	if (tcp_key_is_md5(key))
870 		tot_len += TCPOLEN_MD5SIG_ALIGNED;
871 	if (tcp_key_is_ao(key))
872 		tot_len += tcp_ao_len_aligned(key->ao_key);
873 
874 #ifdef CONFIG_MPTCP
875 	if (rst && !tcp_key_is_md5(key)) {
876 		mrst = mptcp_reset_option(skb);
877 
878 		if (mrst)
879 			tot_len += sizeof(__be32);
880 	}
881 #endif
882 
883 	buff = alloc_skb(MAX_TCP_HEADER, GFP_ATOMIC);
884 	if (!buff)
885 		return;
886 
887 	skb_reserve(buff, MAX_TCP_HEADER);
888 
889 	t1 = skb_push(buff, tot_len);
890 	skb_reset_transport_header(buff);
891 
892 	/* Swap the send and the receive. */
893 	memset(t1, 0, sizeof(*t1));
894 	t1->dest = th->source;
895 	t1->source = th->dest;
896 	t1->doff = tot_len / 4;
897 	t1->seq = htonl(seq);
898 	t1->ack_seq = htonl(ack);
899 	t1->ack = !rst || !th->ack;
900 	t1->rst = rst;
901 	t1->window = htons(win);
902 
903 	topt = (__be32 *)(t1 + 1);
904 
905 	if (tsecr) {
906 		*topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
907 				(TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
908 		*topt++ = htonl(tsval);
909 		*topt++ = htonl(tsecr);
910 	}
911 
912 	if (mrst)
913 		*topt++ = mrst;
914 
915 #ifdef CONFIG_TCP_MD5SIG
916 	if (tcp_key_is_md5(key)) {
917 		*topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
918 				(TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
919 		tcp_v6_md5_hash_hdr((__u8 *)topt, key->md5_key,
920 				    &ipv6_hdr(skb)->saddr,
921 				    &ipv6_hdr(skb)->daddr, t1);
922 	}
923 #endif
924 #ifdef CONFIG_TCP_AO
925 	if (tcp_key_is_ao(key)) {
926 		*topt++ = htonl((TCPOPT_AO << 24) |
927 				(tcp_ao_len(key->ao_key) << 16) |
928 				(key->ao_key->sndid << 8) |
929 				(key->rcv_next));
930 		memset((u8 *)topt + tcp_ao_maclen(key->ao_key), TCPOPT_NOP,
931 		       tcp_ao_len_aligned(key->ao_key) - tcp_ao_len(key->ao_key));
932 
933 		tcp_ao_hash_hdr(AF_INET6, (char *)topt, key->ao_key,
934 				key->traffic_key,
935 				(union tcp_ao_addr *)&ipv6_hdr(skb)->saddr,
936 				(union tcp_ao_addr *)&ipv6_hdr(skb)->daddr,
937 				t1, key->sne);
938 	}
939 #endif
940 
941 	memset(&fl6, 0, sizeof(fl6));
942 	fl6.daddr = ipv6_hdr(skb)->saddr;
943 	fl6.saddr = ipv6_hdr(skb)->daddr;
944 	fl6.flowlabel = label;
945 
946 	buff->ip_summed = CHECKSUM_PARTIAL;
947 
948 	__tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
949 
950 	fl6.flowi6_proto = IPPROTO_TCP;
951 	if (rt6_need_strict(&fl6.daddr) && !oif)
952 		fl6.flowi6_oif = tcp_v6_iif(skb);
953 	else {
954 		if (!oif && netif_index_is_l3_master(net, skb->skb_iif))
955 			oif = skb->skb_iif;
956 
957 		fl6.flowi6_oif = oif;
958 	}
959 
960 	if (sk) {
961 		/* unconstify the socket only to attach it to buff with care. */
962 		skb_set_owner_edemux(buff, (struct sock *)sk);
963 		psp_reply_set_decrypted(sk, buff);
964 
965 		if (sk->sk_state == TCP_TIME_WAIT)
966 			mark = inet_twsk(sk)->tw_mark;
967 		else
968 			mark = READ_ONCE(sk->sk_mark);
969 		skb_set_delivery_time(buff, tcp_transmit_time(sk), SKB_CLOCK_MONOTONIC);
970 	}
971 	if (txhash) {
972 		/* autoflowlabel/skb_get_hash_flowi6 rely on buff->hash */
973 		skb_set_hash(buff, txhash, PKT_HASH_TYPE_L4);
974 
975 		/* Select the local ECMP path from the connection's txhash,
976 		 * so a control packet (RST, or ACK from a time-wait socket)
977 		 * uses the same nexthop as the data.  Only policy 0 uses
978 		 * mp_hash; policies 1-3 derive a deterministic hash.
979 		 */
980 		ip6_ecmp_set_mp_hash(net, &fl6, txhash);
981 	}
982 	fl6.flowi6_mark = IP6_REPLY_MARK(net, skb->mark) ?: mark;
983 	fl6.fl6_dport = t1->dest;
984 	fl6.fl6_sport = t1->source;
985 	fl6.flowi6_uid = sock_net_uid(net, sk && sk_fullsock(sk) ? sk : NULL);
986 	security_skb_classify_flow(skb, flowi6_to_flowi_common(&fl6));
987 
988 	/* Pass a socket to ip6_dst_lookup either it is for RST
989 	 * Underlying function will use this to retrieve the network
990 	 * namespace
991 	 */
992 	if (sk && sk->sk_state != TCP_TIME_WAIT)
993 		dst = ip6_dst_lookup_flow(net, sk, &fl6, NULL); /*sk's xfrm_policy can be referred*/
994 	else
995 		dst = ip6_dst_lookup_flow(net, ctl_sk, &fl6, NULL);
996 	if (!IS_ERR(dst)) {
997 		skb_dst_set(buff, dst);
998 		ip6_xmit(ctl_sk, buff, &fl6, fl6.flowi6_mark, NULL,
999 			 tclass, priority);
1000 		TCP_INC_STATS(net, TCP_MIB_OUTSEGS);
1001 		if (rst)
1002 			TCP_INC_STATS(net, TCP_MIB_OUTRSTS);
1003 		return;
1004 	}
1005 
1006 	sk_skb_reason_drop(sk, buff, SKB_DROP_REASON_IP_OUTNOROUTES);
1007 }
1008 
1009 static void tcp_v6_send_reset(const struct sock *sk, struct sk_buff *skb,
1010 			      enum sk_rst_reason reason)
1011 {
1012 	const struct tcphdr *th = tcp_hdr(skb);
1013 	struct ipv6hdr *ipv6h = ipv6_hdr(skb);
1014 	const __u8 *md5_hash_location = NULL;
1015 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
1016 	bool allocated_traffic_key = false;
1017 #endif
1018 	const struct tcp_ao_hdr *aoh;
1019 	struct tcp_key key = {};
1020 	u32 seq = 0, ack_seq = 0;
1021 	__be32 label = 0;
1022 	u32 priority = 0;
1023 	struct net *net;
1024 	u32 txhash = 0;
1025 	int oif = 0;
1026 #ifdef CONFIG_TCP_MD5SIG
1027 	unsigned char newhash[16];
1028 	struct sock *sk1 = NULL;
1029 #endif
1030 
1031 	if (th->rst)
1032 		return;
1033 
1034 	/* If sk not NULL, it means we did a successful lookup and incoming
1035 	 * route had to be correct. prequeue might have dropped our dst.
1036 	 */
1037 	if (!sk && !ipv6_unicast_destination(skb))
1038 		return;
1039 
1040 	net = sk ? sock_net(sk) : skb_dst_dev_net_rcu(skb);
1041 	/* Invalid TCP option size or twice included auth */
1042 	if (tcp_parse_auth_options(th, &md5_hash_location, &aoh))
1043 		return;
1044 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
1045 	rcu_read_lock();
1046 #endif
1047 #ifdef CONFIG_TCP_MD5SIG
1048 	if (sk && sk_fullsock(sk)) {
1049 		int l3index;
1050 
1051 		/* sdif set, means packet ingressed via a device
1052 		 * in an L3 domain and inet_iif is set to it.
1053 		 */
1054 		l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
1055 		key.md5_key = tcp_v6_md5_do_lookup(sk, &ipv6h->saddr, l3index);
1056 		if (key.md5_key)
1057 			key.type = TCP_KEY_MD5;
1058 	} else if (md5_hash_location) {
1059 		int dif = tcp_v6_iif_l3_slave(skb);
1060 		int sdif = tcp_v6_sdif(skb);
1061 		int l3index;
1062 
1063 		/*
1064 		 * active side is lost. Try to find listening socket through
1065 		 * source port, and then find md5 key through listening socket.
1066 		 * we are not loose security here:
1067 		 * Incoming packet is checked with md5 hash with finding key,
1068 		 * no RST generated if md5 hash doesn't match.
1069 		 */
1070 		sk1 = inet6_lookup_listener(net, NULL, 0, &ipv6h->saddr, th->source,
1071 					    &ipv6h->daddr, ntohs(th->source),
1072 					    dif, sdif);
1073 		if (!sk1)
1074 			goto out;
1075 
1076 		/* sdif set, means packet ingressed via a device
1077 		 * in an L3 domain and dif is set to it.
1078 		 */
1079 		l3index = tcp_v6_sdif(skb) ? dif : 0;
1080 
1081 		key.md5_key = tcp_v6_md5_do_lookup(sk1, &ipv6h->saddr, l3index);
1082 		if (!key.md5_key)
1083 			goto out;
1084 		key.type = TCP_KEY_MD5;
1085 
1086 		tcp_v6_md5_hash_skb(newhash, key.md5_key, NULL, skb);
1087 		if (crypto_memneq(md5_hash_location, newhash, 16))
1088 			goto out;
1089 	}
1090 #endif
1091 
1092 	if (th->ack)
1093 		seq = ntohl(th->ack_seq);
1094 	else
1095 		ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
1096 			  (th->doff << 2);
1097 
1098 #ifdef CONFIG_TCP_AO
1099 	if (aoh) {
1100 		int l3index;
1101 
1102 		l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
1103 		if (tcp_ao_prepare_reset(sk, skb, aoh, l3index, seq,
1104 					 &key.ao_key, &key.traffic_key,
1105 					 &allocated_traffic_key,
1106 					 &key.rcv_next, &key.sne))
1107 			goto out;
1108 		key.type = TCP_KEY_AO;
1109 	}
1110 #endif
1111 
1112 	if (sk) {
1113 		oif = sk->sk_bound_dev_if;
1114 		if (sk_fullsock(sk)) {
1115 			if (inet6_test_bit(REPFLOW, sk))
1116 				label = ip6_flowlabel(ipv6h);
1117 			priority = READ_ONCE(sk->sk_priority);
1118 			txhash = sk->sk_txhash;
1119 		}
1120 		if (sk->sk_state == TCP_TIME_WAIT) {
1121 			label = cpu_to_be32(inet_twsk(sk)->tw_flowlabel);
1122 			priority = inet_twsk(sk)->tw_priority;
1123 			txhash = inet_twsk(sk)->tw_txhash;
1124 		}
1125 	} else {
1126 		if (READ_ONCE(net->ipv6.sysctl.flowlabel_reflect) &
1127 		    FLOWLABEL_REFLECT_TCP_RESET)
1128 			label = ip6_flowlabel(ipv6h);
1129 	}
1130 
1131 	trace_tcp_send_reset(sk, skb, reason);
1132 
1133 	tcp_v6_send_response(sk, skb, seq, ack_seq, 0, 0, 0, oif, 1,
1134 			     ipv6_get_dsfield(ipv6h) & ~INET_ECN_MASK,
1135 			     label, priority, txhash,
1136 			     &key);
1137 
1138 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
1139 out:
1140 	if (allocated_traffic_key)
1141 		kfree(key.traffic_key);
1142 	rcu_read_unlock();
1143 #endif
1144 }
1145 
1146 static void tcp_v6_send_ack(const struct sock *sk, struct sk_buff *skb, u32 seq,
1147 			    u32 ack, u32 win, u32 tsval, u32 tsecr, int oif,
1148 			    struct tcp_key *key, u8 tclass,
1149 			    __be32 label, u32 priority, u32 txhash)
1150 {
1151 	tcp_v6_send_response(sk, skb, seq, ack, win, tsval, tsecr, oif, 0,
1152 			     tclass, label, priority, txhash, key);
1153 }
1154 
1155 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb,
1156 				enum tcp_tw_status tw_status)
1157 {
1158 	struct inet_timewait_sock *tw = inet_twsk(sk);
1159 	struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
1160 	u8 tclass = tw->tw_tclass;
1161 	struct tcp_key key = {};
1162 
1163 	if (tw_status == TCP_TW_ACK_OOW)
1164 		tclass &= ~INET_ECN_MASK;
1165 #ifdef CONFIG_TCP_AO
1166 	struct tcp_ao_info *ao_info;
1167 
1168 	if (static_branch_unlikely(&tcp_ao_needed.key)) {
1169 
1170 		/* FIXME: the segment to-be-acked is not verified yet */
1171 		ao_info = rcu_dereference(tcptw->ao_info);
1172 		if (ao_info) {
1173 			const struct tcp_ao_hdr *aoh;
1174 
1175 			/* Invalid TCP option size or twice included auth */
1176 			if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
1177 				goto out;
1178 			if (aoh)
1179 				key.ao_key = tcp_ao_established_key(sk, ao_info,
1180 								    aoh->rnext_keyid, -1);
1181 		}
1182 	}
1183 	if (key.ao_key) {
1184 		struct tcp_ao_key *rnext_key;
1185 
1186 		key.traffic_key = snd_other_key(key.ao_key);
1187 		/* rcv_next switches to our rcv_next */
1188 		rnext_key = READ_ONCE(ao_info->rnext_key);
1189 		key.rcv_next = rnext_key->rcvid;
1190 		key.sne = READ_ONCE(ao_info->snd_sne);
1191 		key.type = TCP_KEY_AO;
1192 #else
1193 	if (0) {
1194 #endif
1195 #ifdef CONFIG_TCP_MD5SIG
1196 	} else if (static_branch_unlikely(&tcp_md5_needed.key)) {
1197 		key.md5_key = tcp_twsk_md5_key(tcptw);
1198 		if (key.md5_key)
1199 			key.type = TCP_KEY_MD5;
1200 #endif
1201 	}
1202 
1203 	tcp_v6_send_ack(sk, skb, tcptw->tw_snd_nxt,
1204 			READ_ONCE(tcptw->tw_rcv_nxt),
1205 			tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
1206 			tcp_tw_tsval(tcptw),
1207 			READ_ONCE(tcptw->tw_ts_recent), tw->tw_bound_dev_if,
1208 			&key, tclass, cpu_to_be32(tw->tw_flowlabel),
1209 			tw->tw_priority, tw->tw_txhash);
1210 
1211 #ifdef CONFIG_TCP_AO
1212 out:
1213 #endif
1214 	inet_twsk_put(tw);
1215 }
1216 
1217 static void tcp_v6_reqsk_send_ack(const struct sock *sk, struct sk_buff *skb,
1218 				  struct request_sock *req)
1219 {
1220 	struct tcp_key key = {};
1221 
1222 #ifdef CONFIG_TCP_AO
1223 	if (static_branch_unlikely(&tcp_ao_needed.key) &&
1224 	    tcp_rsk_used_ao(req)) {
1225 		const struct in6_addr *addr = &ipv6_hdr(skb)->saddr;
1226 		const struct tcp_ao_hdr *aoh;
1227 		int l3index;
1228 
1229 		l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
1230 		/* Invalid TCP option size or twice included auth */
1231 		if (tcp_parse_auth_options(tcp_hdr(skb), NULL, &aoh))
1232 			return;
1233 		if (!aoh)
1234 			return;
1235 		key.ao_key = tcp_ao_do_lookup(sk, l3index,
1236 					      (union tcp_ao_addr *)addr,
1237 					      AF_INET6, aoh->rnext_keyid, -1);
1238 		if (unlikely(!key.ao_key)) {
1239 			/* Send ACK with any matching MKT for the peer */
1240 			key.ao_key = tcp_ao_do_lookup(sk, l3index,
1241 						      (union tcp_ao_addr *)addr,
1242 						      AF_INET6, -1, -1);
1243 			/* Matching key disappeared (user removed the key?)
1244 			 * let the handshake timeout.
1245 			 */
1246 			if (!key.ao_key) {
1247 				net_info_ratelimited("TCP-AO key for (%pI6, %d)->(%pI6, %d) suddenly disappeared, won't ACK new connection\n",
1248 						     addr,
1249 						     ntohs(tcp_hdr(skb)->source),
1250 						     &ipv6_hdr(skb)->daddr,
1251 						     ntohs(tcp_hdr(skb)->dest));
1252 				return;
1253 			}
1254 		}
1255 		key.traffic_key = kmalloc(tcp_ao_digest_size(key.ao_key), GFP_ATOMIC);
1256 		if (!key.traffic_key)
1257 			return;
1258 
1259 		key.type = TCP_KEY_AO;
1260 		key.rcv_next = aoh->keyid;
1261 		tcp_v6_ao_calc_key_rsk(key.ao_key, key.traffic_key, req);
1262 #else
1263 	if (0) {
1264 #endif
1265 #ifdef CONFIG_TCP_MD5SIG
1266 	} else if (static_branch_unlikely(&tcp_md5_needed.key)) {
1267 		int l3index = tcp_v6_sdif(skb) ? tcp_v6_iif_l3_slave(skb) : 0;
1268 
1269 		key.md5_key = tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr,
1270 						   l3index);
1271 		if (key.md5_key)
1272 			key.type = TCP_KEY_MD5;
1273 #endif
1274 	}
1275 
1276 	/* sk->sk_state == TCP_LISTEN -> for regular TCP_SYN_RECV
1277 	 * sk->sk_state == TCP_SYN_RECV -> for Fast Open.
1278 	 */
1279 	tcp_v6_send_ack(sk, skb, (sk->sk_state == TCP_LISTEN) ?
1280 			tcp_rsk(req)->snt_isn + 1 : tcp_sk(sk)->snd_nxt,
1281 			tcp_rsk(req)->rcv_nxt,
1282 			tcp_synack_window(req) >> inet_rsk(req)->rcv_wscale,
1283 			tcp_rsk_tsval(tcp_rsk(req)),
1284 			req->ts_recent, sk->sk_bound_dev_if,
1285 			&key, ipv6_get_dsfield(ipv6_hdr(skb)) & ~INET_ECN_MASK,
1286 			0,
1287 			READ_ONCE(sk->sk_priority),
1288 			READ_ONCE(tcp_rsk(req)->txhash));
1289 	if (tcp_key_is_ao(&key))
1290 		kfree(key.traffic_key);
1291 }
1292 
1293 
1294 static struct sock *tcp_v6_cookie_check(struct sock *sk, struct sk_buff *skb)
1295 {
1296 #ifdef CONFIG_SYN_COOKIES
1297 	const struct tcphdr *th = tcp_hdr(skb);
1298 
1299 	if (!th->syn)
1300 		sk = cookie_v6_check(sk, skb);
1301 #endif
1302 	return sk;
1303 }
1304 
1305 u16 tcp_v6_get_syncookie(struct sock *sk, struct ipv6hdr *iph,
1306 			 struct tcphdr *th, u32 *cookie)
1307 {
1308 	u16 mss = 0;
1309 #ifdef CONFIG_SYN_COOKIES
1310 	mss = tcp_get_syncookie_mss(&tcp6_request_sock_ops,
1311 				    &tcp_request_sock_ipv6_ops, sk, th);
1312 	if (mss) {
1313 		*cookie = __cookie_v6_init_sequence(iph, th, &mss);
1314 		tcp_synq_overflow(sk);
1315 	}
1316 #endif
1317 	return mss;
1318 }
1319 
1320 static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1321 {
1322 	if (skb->protocol == htons(ETH_P_IP))
1323 		return tcp_v4_conn_request(sk, skb);
1324 
1325 	if (!ipv6_unicast_destination(skb))
1326 		goto drop;
1327 
1328 	if (ipv6_addr_v4mapped(&ipv6_hdr(skb)->saddr)) {
1329 		__IP6_INC_STATS(sock_net(sk), NULL, IPSTATS_MIB_INHDRERRORS);
1330 		return 0;
1331 	}
1332 
1333 	return tcp_conn_request(&tcp6_request_sock_ops,
1334 				&tcp_request_sock_ipv6_ops, sk, skb);
1335 
1336 drop:
1337 	tcp_listendrop(sk);
1338 	return 0; /* don't send reset */
1339 }
1340 
1341 static void tcp_v6_restore_cb(struct sk_buff *skb)
1342 {
1343 	/* We need to move header back to the beginning if xfrm6_policy_check()
1344 	 * and tcp_v6_fill_cb() are going to be called again.
1345 	 * ip6_datagram_recv_specific_ctl() also expects IP6CB to be there.
1346 	 */
1347 	memmove(IP6CB(skb), &TCP_SKB_CB(skb)->header.h6,
1348 		sizeof(struct inet6_skb_parm));
1349 }
1350 
1351 /* Called from tcp_v4_syn_recv_sock() for v6_mapped children. */
1352 static void tcp_v6_mapped_child_init(struct sock *newsk, const struct sock *sk)
1353 {
1354 	struct inet_sock *newinet = inet_sk(newsk);
1355 	struct ipv6_pinfo *newnp;
1356 
1357 	newinet->pinet6 = newnp = tcp_inet6_sk(newsk);
1358 	newinet->ipv6_fl_list = NULL;
1359 
1360 	memcpy(newnp, tcp_inet6_sk(sk), sizeof(struct ipv6_pinfo));
1361 
1362 	newnp->saddr = newsk->sk_v6_rcv_saddr;
1363 
1364 	inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1365 	if (sk_is_mptcp(newsk))
1366 		mptcpv6_handle_mapped(newsk, true);
1367 	newsk->sk_backlog_rcv = tcp_v4_do_rcv;
1368 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
1369 	tcp_sk(newsk)->af_specific = &tcp_sock_ipv6_mapped_specific;
1370 #endif
1371 
1372 	newnp->ipv6_mc_list = NULL;
1373 	newnp->ipv6_ac_list = NULL;
1374 	newnp->pktoptions  = NULL;
1375 	newnp->opt	   = NULL;
1376 
1377 	/* tcp_v4_syn_recv_sock() has initialized newinet->mc_{index,ttl} */
1378 	newnp->mcast_oif   = newinet->mc_index;
1379 	newnp->mcast_hops  = newinet->mc_ttl;
1380 
1381 	newnp->rcv_flowinfo = 0;
1382 	if (inet6_test_bit(REPFLOW, sk))
1383 		newnp->flow_label = 0;
1384 }
1385 
1386 static struct sock *tcp_v6_syn_recv_sock(const struct sock *sk, struct sk_buff *skb,
1387 					 struct request_sock *req,
1388 					 struct dst_entry *dst,
1389 					 struct request_sock *req_unhash,
1390 					 bool *own_req,
1391 					 void (*opt_child_init)(struct sock *newsk,
1392 								const struct sock *sk))
1393 {
1394 	const struct ipv6_pinfo *np = tcp_inet6_sk(sk);
1395 	struct inet_request_sock *ireq;
1396 	struct ipv6_txoptions *opt;
1397 	struct inet_sock *newinet;
1398 	bool found_dup_sk = false;
1399 	struct ipv6_pinfo *newnp;
1400 	struct tcp_sock *newtp;
1401 	struct sock *newsk;
1402 #ifdef CONFIG_TCP_MD5SIG
1403 	struct tcp_md5sig_key *key;
1404 	int l3index;
1405 #endif
1406 	struct flowi6 fl6;
1407 
1408 	if (skb->protocol == htons(ETH_P_IP))
1409 		return tcp_v4_syn_recv_sock(sk, skb, req, dst,
1410 					    req_unhash, own_req,
1411 					    tcp_v6_mapped_child_init);
1412 	ireq = inet_rsk(req);
1413 
1414 	if (sk_acceptq_is_full(sk))
1415 		goto exit_overflow;
1416 
1417 	dst = inet6_csk_route_req(sk, dst, &fl6, req, IPPROTO_TCP);
1418 	if (!dst)
1419 		goto exit;
1420 
1421 	newsk = tcp_create_openreq_child(sk, req, skb);
1422 	if (!newsk)
1423 		goto exit_nonewsk;
1424 
1425 	/*
1426 	 * No need to charge this sock to the relevant IPv6 refcnt debug socks
1427 	 * count here, tcp_create_openreq_child now does this for us, see the
1428 	 * comment in that function for the gory details. -acme
1429 	 */
1430 
1431 	newsk->sk_gso_type = SKB_GSO_TCPV6;
1432 	inet6_sk_rx_dst_set(newsk, skb);
1433 
1434 	newinet = inet_sk(newsk);
1435 	newinet->cork.fl.u.ip6 = fl6;
1436 	newinet->pinet6 = tcp_inet6_sk(newsk);
1437 	newinet->ipv6_fl_list = NULL;
1438 	newinet->inet_opt = NULL;
1439 
1440 	newtp = tcp_sk(newsk);
1441 	newnp = tcp_inet6_sk(newsk);
1442 
1443 	memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1444 
1445 	ip6_dst_store(newsk, dst, false, false);
1446 
1447 	newnp->saddr = ireq->ir_v6_loc_addr;
1448 
1449 	/* Now IPv6 options...
1450 
1451 	   First: no IPv4 options.
1452 	 */
1453 	newnp->ipv6_mc_list = NULL;
1454 	newnp->ipv6_ac_list = NULL;
1455 
1456 	/* Clone RX bits */
1457 	newnp->rxopt.all = np->rxopt.all;
1458 
1459 	newnp->pktoptions = NULL;
1460 	newnp->opt	  = NULL;
1461 	newnp->mcast_oif  = tcp_v6_iif(skb);
1462 	newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1463 	newnp->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(skb));
1464 	if (inet6_test_bit(REPFLOW, sk))
1465 		newnp->flow_label = ip6_flowlabel(ipv6_hdr(skb));
1466 
1467 	/* Set ToS of the new socket based upon the value of incoming SYN.
1468 	 * ECT bits are set later in tcp_init_transfer().
1469 	 */
1470 	if (READ_ONCE(sock_net(sk)->ipv4.sysctl_tcp_reflect_tos))
1471 		newnp->tclass = tcp_rsk(req)->syn_tos & ~INET_ECN_MASK;
1472 
1473 	/* Clone native IPv6 options from listening socket (if any)
1474 
1475 	   Yes, keeping reference count would be much more clever,
1476 	   but we make one more one thing there: reattach optmem
1477 	   to newsk.
1478 	 */
1479 	opt = ireq->ipv6_opt;
1480 	if (!opt)
1481 		opt = rcu_dereference(np->opt);
1482 	if (opt) {
1483 		opt = ipv6_dup_options(newsk, opt);
1484 		RCU_INIT_POINTER(newnp->opt, opt);
1485 	}
1486 	inet_csk(newsk)->icsk_ext_hdr_len = 0;
1487 	if (opt)
1488 		inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
1489 						    opt->opt_flen;
1490 
1491 	tcp_ca_openreq_child(newsk, dst);
1492 
1493 	tcp_sync_mss(newsk, dst6_mtu(dst));
1494 	newtp->advmss = tcp_mss_clamp(tcp_sk(sk), tcp_dst_advmss(dst));
1495 
1496 	tcp_initialize_rcv_mss(newsk);
1497 
1498 #ifdef CONFIG_TCP_MD5SIG
1499 	l3index = l3mdev_master_ifindex_by_index(sock_net(sk), ireq->ir_iif);
1500 
1501 	if (!tcp_rsk_used_ao(req)) {
1502 		/* Copy over the MD5 key from the original socket */
1503 		key = tcp_v6_md5_do_lookup(sk, &newsk->sk_v6_daddr, l3index);
1504 		if (key) {
1505 			const union tcp_md5_addr *addr;
1506 
1507 			addr = (union tcp_md5_addr *)&newsk->sk_v6_daddr;
1508 			if (tcp_md5_key_copy(newsk, addr, AF_INET6, 128, l3index, key))
1509 				goto put_and_exit;
1510 		}
1511 	}
1512 #endif
1513 #ifdef CONFIG_TCP_AO
1514 	/* Copy over tcp_ao_info if any */
1515 	if (tcp_ao_copy_all_matching(sk, newsk, req, skb, AF_INET6))
1516 		goto put_and_exit; /* OOM */
1517 #endif
1518 
1519 	if (__inet_inherit_port(sk, newsk) < 0)
1520 		goto put_and_exit;
1521 	*own_req = inet_ehash_nolisten(newsk, req_to_sk(req_unhash),
1522 				       &found_dup_sk);
1523 	if (*own_req) {
1524 		tcp_move_syn(newtp, req);
1525 
1526 		/* Clone pktoptions received with SYN, if we own the req */
1527 		if (ireq->pktopts) {
1528 			newnp->pktoptions = skb_clone_and_charge_r(ireq->pktopts, newsk);
1529 			consume_skb(ireq->pktopts);
1530 			ireq->pktopts = NULL;
1531 			if (newnp->pktoptions)
1532 				tcp_v6_restore_cb(newnp->pktoptions);
1533 		}
1534 	} else {
1535 		if (!req_unhash && found_dup_sk) {
1536 			/* This code path should only be executed in the
1537 			 * syncookie case only
1538 			 */
1539 			bh_unlock_sock(newsk);
1540 			sock_put(newsk);
1541 			newsk = NULL;
1542 		}
1543 	}
1544 
1545 	return newsk;
1546 
1547 exit_overflow:
1548 	__NET_INC_STATS(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1549 exit_nonewsk:
1550 	dst_release(dst);
1551 exit:
1552 	tcp_listendrop(sk);
1553 	return NULL;
1554 put_and_exit:
1555 	inet_csk_prepare_forced_close(newsk);
1556 	tcp_done(newsk);
1557 	goto exit;
1558 }
1559 
1560 INDIRECT_CALLABLE_DECLARE(struct dst_entry *ipv4_dst_check(struct dst_entry *,
1561 							   u32));
1562 /* The socket must have it's spinlock held when we get
1563  * here, unless it is a TCP_LISTEN socket.
1564  *
1565  * We have a potential double-lock case here, so even when
1566  * doing backlog processing we use the BH locking scheme.
1567  * This is because we cannot sleep with the original spinlock
1568  * held.
1569  */
1570 INDIRECT_CALLABLE_SCOPE
1571 int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1572 {
1573 	struct ipv6_pinfo *np = tcp_inet6_sk(sk);
1574 	struct sk_buff *opt_skb = NULL;
1575 	enum skb_drop_reason reason;
1576 	struct tcp_sock *tp;
1577 
1578 	/* Imagine: socket is IPv6. IPv4 packet arrives,
1579 	   goes to IPv4 receive handler and backlogged.
1580 	   From backlog it always goes here. Kerboom...
1581 	   Fortunately, tcp_rcv_established and rcv_established
1582 	   handle them correctly, but it is not case with
1583 	   tcp_v6_hnd_req and tcp_v6_send_reset().   --ANK
1584 	 */
1585 
1586 	if (skb->protocol == htons(ETH_P_IP))
1587 		return tcp_v4_do_rcv(sk, skb);
1588 
1589 	reason = psp_sk_rx_policy_check(sk, skb);
1590 	if (reason)
1591 		goto err_discard;
1592 
1593 	/*
1594 	 *	socket locking is here for SMP purposes as backlog rcv
1595 	 *	is currently called with bh processing disabled.
1596 	 */
1597 
1598 	/* Do Stevens' IPV6_PKTOPTIONS.
1599 
1600 	   Yes, guys, it is the only place in our code, where we
1601 	   may make it not affecting IPv4.
1602 	   The rest of code is protocol independent,
1603 	   and I do not like idea to uglify IPv4.
1604 
1605 	   Actually, all the idea behind IPV6_PKTOPTIONS
1606 	   looks not very well thought. For now we latch
1607 	   options, received in the last packet, enqueued
1608 	   by tcp. Feel free to propose better solution.
1609 					       --ANK (980728)
1610 	 */
1611 	if (np->rxopt.all &&
1612 	    !((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)))
1613 		opt_skb = skb_clone_and_charge_r(skb, sk);
1614 
1615 	if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1616 		struct dst_entry *dst;
1617 
1618 		dst = rcu_dereference_protected(sk->sk_rx_dst,
1619 						lockdep_sock_is_held(sk));
1620 
1621 		sock_rps_save_rxhash(sk, skb);
1622 		sk_mark_napi_id(sk, skb);
1623 		if (dst && unlikely(dst != skb_dst(skb))) {
1624 			if (sk->sk_rx_dst_ifindex != skb->skb_iif ||
1625 			    INDIRECT_CALL_1(dst->ops->check, ip6_dst_check,
1626 					    dst, sk->sk_rx_dst_cookie) == NULL) {
1627 				RCU_INIT_POINTER(sk->sk_rx_dst, NULL);
1628 				dst_release(dst);
1629 			}
1630 		}
1631 
1632 		tcp_rcv_established(sk, skb);
1633 		if (opt_skb)
1634 			goto ipv6_pktoptions;
1635 		return 0;
1636 	}
1637 
1638 	if (tcp_checksum_complete(skb))
1639 		goto csum_err;
1640 
1641 	if (sk->sk_state == TCP_LISTEN) {
1642 		struct sock *nsk = tcp_v6_cookie_check(sk, skb);
1643 
1644 		if (!nsk)
1645 			return 0;
1646 		if (nsk != sk) {
1647 			reason = tcp_child_process(sk, nsk, skb);
1648 			sock_put(nsk);
1649 			if (reason)
1650 				goto reset;
1651 			return 0;
1652 		}
1653 	} else
1654 		sock_rps_save_rxhash(sk, skb);
1655 
1656 	reason = tcp_rcv_state_process(sk, skb);
1657 	if (reason)
1658 		goto reset;
1659 	if (opt_skb)
1660 		goto ipv6_pktoptions;
1661 	return 0;
1662 
1663 reset:
1664 	tcp_v6_send_reset(sk, skb, sk_rst_convert_drop_reason(reason));
1665 discard:
1666 	if (opt_skb)
1667 		__kfree_skb(opt_skb);
1668 	sk_skb_reason_drop(sk, skb, reason);
1669 	return 0;
1670 csum_err:
1671 	reason = SKB_DROP_REASON_TCP_CSUM;
1672 	trace_tcp_bad_csum(skb);
1673 	TCP_INC_STATS(sock_net(sk), TCP_MIB_CSUMERRORS);
1674 err_discard:
1675 	TCP_INC_STATS(sock_net(sk), TCP_MIB_INERRS);
1676 	goto discard;
1677 
1678 
1679 ipv6_pktoptions:
1680 	/* Do you ask, what is it?
1681 
1682 	   1. skb was enqueued by tcp.
1683 	   2. skb is added to tail of read queue, rather than out of order.
1684 	   3. socket is not in passive state.
1685 	   4. Finally, it really contains options, which user wants to receive.
1686 	 */
1687 	tp = tcp_sk(sk);
1688 	if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1689 	    !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1690 		if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
1691 			WRITE_ONCE(np->mcast_oif, tcp_v6_iif(opt_skb));
1692 		if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
1693 			WRITE_ONCE(np->mcast_hops,
1694 				   ipv6_hdr(opt_skb)->hop_limit);
1695 		if (np->rxopt.bits.rxflow || np->rxopt.bits.rxtclass)
1696 			np->rcv_flowinfo = ip6_flowinfo(ipv6_hdr(opt_skb));
1697 		if (inet6_test_bit(REPFLOW, sk))
1698 			np->flow_label = ip6_flowlabel(ipv6_hdr(opt_skb));
1699 		if (ipv6_opt_accepted(sk, opt_skb, &TCP_SKB_CB(opt_skb)->header.h6)) {
1700 			tcp_v6_restore_cb(opt_skb);
1701 			opt_skb = xchg(&np->pktoptions, opt_skb);
1702 		} else {
1703 			__kfree_skb(opt_skb);
1704 			opt_skb = xchg(&np->pktoptions, NULL);
1705 		}
1706 	}
1707 
1708 	consume_skb(opt_skb);
1709 	return 0;
1710 }
1711 
1712 static void tcp_v6_fill_cb(struct sk_buff *skb, const struct ipv6hdr *hdr,
1713 			   const struct tcphdr *th)
1714 {
1715 	/* This is tricky: we move IP6CB at its correct location into
1716 	 * TCP_SKB_CB(). It must be done after xfrm6_policy_check(), because
1717 	 * _decode_session6() uses IP6CB().
1718 	 * barrier() makes sure compiler won't play aliasing games.
1719 	 */
1720 	memmove(&TCP_SKB_CB(skb)->header.h6, IP6CB(skb),
1721 		sizeof(struct inet6_skb_parm));
1722 	barrier();
1723 
1724 	TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1725 	TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1726 				    skb->len - th->doff*4);
1727 	TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1728 	TCP_SKB_CB(skb)->tcp_flags = tcp_flags_ntohs(th);
1729 	TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
1730 	TCP_SKB_CB(skb)->sacked = 0;
1731 	TCP_SKB_CB(skb)->has_rxtstamp =
1732 			skb->tstamp || skb_hwtstamps(skb)->hwtstamp;
1733 }
1734 
1735 INDIRECT_CALLABLE_SCOPE int tcp_v6_rcv(struct sk_buff *skb)
1736 {
1737 	struct net *net = dev_net_rcu(skb->dev);
1738 	enum skb_drop_reason drop_reason;
1739 	enum tcp_tw_status tw_status;
1740 	int sdif = inet6_sdif(skb);
1741 	int dif = inet6_iif(skb);
1742 	const struct tcphdr *th;
1743 	const struct ipv6hdr *hdr;
1744 	struct sock *sk = NULL;
1745 	bool refcounted;
1746 	int ret;
1747 	u32 isn;
1748 
1749 	drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
1750 	if (skb->pkt_type != PACKET_HOST)
1751 		goto discard_it;
1752 
1753 	/*
1754 	 *	Count it even if it's bad.
1755 	 */
1756 	__TCP_INC_STATS(net, TCP_MIB_INSEGS);
1757 
1758 	if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1759 		goto discard_it;
1760 
1761 	th = (const struct tcphdr *)skb->data;
1762 
1763 	if (unlikely(th->doff < sizeof(struct tcphdr) / 4)) {
1764 		drop_reason = SKB_DROP_REASON_PKT_TOO_SMALL;
1765 		goto bad_packet;
1766 	}
1767 	if (!pskb_may_pull(skb, th->doff*4))
1768 		goto discard_it;
1769 
1770 	if (skb_checksum_init(skb, IPPROTO_TCP, ip6_compute_pseudo))
1771 		goto csum_error;
1772 
1773 	th = (const struct tcphdr *)skb->data;
1774 	hdr = ipv6_hdr(skb);
1775 
1776 lookup:
1777 	sk = __inet6_lookup_skb(skb, __tcp_hdrlen(th),
1778 				th->source, th->dest, inet6_iif(skb), sdif,
1779 				&refcounted);
1780 	if (!sk)
1781 		goto no_tcp_socket;
1782 
1783 	if (sk->sk_state == TCP_TIME_WAIT)
1784 		goto do_time_wait;
1785 
1786 	if (sk->sk_state == TCP_NEW_SYN_RECV) {
1787 		struct request_sock *req = inet_reqsk(sk);
1788 		bool req_stolen = false;
1789 		struct sock *nsk;
1790 
1791 		sk = req->rsk_listener;
1792 		if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1793 			drop_reason = SKB_DROP_REASON_XFRM_POLICY;
1794 		else
1795 			drop_reason = tcp_inbound_hash(sk, req, skb,
1796 						       &hdr->saddr, &hdr->daddr,
1797 						       AF_INET6, dif, sdif);
1798 		if (drop_reason) {
1799 			sk_drops_skbadd(sk, skb);
1800 			reqsk_put(req);
1801 			goto discard_it;
1802 		}
1803 		if (tcp_checksum_complete(skb)) {
1804 			reqsk_put(req);
1805 			goto csum_error;
1806 		}
1807 		if (unlikely(sk->sk_state != TCP_LISTEN)) {
1808 			nsk = reuseport_migrate_sock(sk, req_to_sk(req), skb);
1809 			if (!nsk) {
1810 				inet_csk_reqsk_queue_drop_and_put(sk, req);
1811 				goto lookup;
1812 			}
1813 			sk = nsk;
1814 			/* reuseport_migrate_sock() has already held one sk_refcnt
1815 			 * before returning.
1816 			 */
1817 		} else {
1818 			sock_hold(sk);
1819 		}
1820 		refcounted = true;
1821 		nsk = NULL;
1822 		drop_reason = tcp_filter(sk, skb);
1823 		if (!drop_reason) {
1824 			th = (const struct tcphdr *)skb->data;
1825 			hdr = ipv6_hdr(skb);
1826 			tcp_v6_fill_cb(skb, hdr, th);
1827 			nsk = tcp_check_req(sk, skb, req, false, &req_stolen,
1828 					    &drop_reason);
1829 		}
1830 		if (!nsk) {
1831 			reqsk_put(req);
1832 			if (req_stolen) {
1833 				/* Another cpu got exclusive access to req
1834 				 * and created a full blown socket.
1835 				 * Try to feed this packet to this socket
1836 				 * instead of discarding it.
1837 				 */
1838 				tcp_v6_restore_cb(skb);
1839 				sock_put(sk);
1840 				goto lookup;
1841 			}
1842 			goto discard_and_relse;
1843 		}
1844 		nf_reset_ct(skb);
1845 		if (nsk == sk) {
1846 			reqsk_put(req);
1847 			tcp_v6_restore_cb(skb);
1848 		} else {
1849 			drop_reason = tcp_child_process(sk, nsk, skb);
1850 			if (drop_reason) {
1851 				enum sk_rst_reason rst_reason;
1852 
1853 				rst_reason = sk_rst_convert_drop_reason(drop_reason);
1854 				tcp_v6_send_reset(nsk, skb, rst_reason);
1855 				sock_put(nsk);
1856 				goto discard_and_relse;
1857 			}
1858 			sock_put(nsk);
1859 			sock_put(sk);
1860 			return 0;
1861 		}
1862 	}
1863 
1864 	isn = 0;
1865 process:
1866 	if (static_branch_unlikely(&ip6_min_hopcount)) {
1867 		/* min_hopcount can be changed concurrently from do_ipv6_setsockopt() */
1868 		if (unlikely(hdr->hop_limit < READ_ONCE(tcp_inet6_sk(sk)->min_hopcount))) {
1869 			__NET_INC_STATS(net, LINUX_MIB_TCPMINTTLDROP);
1870 			drop_reason = SKB_DROP_REASON_TCP_MINTTL;
1871 			goto discard_and_relse;
1872 		}
1873 	}
1874 
1875 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
1876 		drop_reason = SKB_DROP_REASON_XFRM_POLICY;
1877 		goto discard_and_relse;
1878 	}
1879 
1880 	drop_reason = tcp_inbound_hash(sk, NULL, skb, &hdr->saddr, &hdr->daddr,
1881 				       AF_INET6, dif, sdif);
1882 	if (drop_reason)
1883 		goto discard_and_relse;
1884 
1885 	nf_reset_ct(skb);
1886 
1887 	drop_reason = tcp_filter(sk, skb);
1888 	if (drop_reason)
1889 		goto discard_and_relse;
1890 
1891 	th = (const struct tcphdr *)skb->data;
1892 	hdr = ipv6_hdr(skb);
1893 	tcp_v6_fill_cb(skb, hdr, th);
1894 	TCP_SKB_CB(skb)->tcp_tw_isn = isn;
1895 
1896 	skb->dev = NULL;
1897 
1898 	if (sk->sk_state == TCP_LISTEN) {
1899 		ret = tcp_v6_do_rcv(sk, skb);
1900 		goto put_and_return;
1901 	}
1902 
1903 	sk_incoming_cpu_update(sk);
1904 
1905 	bh_lock_sock_nested(sk);
1906 	tcp_segs_in(tcp_sk(sk), skb);
1907 	ret = 0;
1908 	if (!sock_owned_by_user(sk)) {
1909 		ret = tcp_v6_do_rcv(sk, skb);
1910 	} else {
1911 		drop_reason = tcp_add_backlog(sk, skb);
1912 		if (drop_reason)
1913 			goto discard_and_relse;
1914 	}
1915 	bh_unlock_sock(sk);
1916 put_and_return:
1917 	if (refcounted)
1918 		sock_put(sk);
1919 	return ret ? -1 : 0;
1920 
1921 no_tcp_socket:
1922 	drop_reason = SKB_DROP_REASON_NO_SOCKET;
1923 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1924 		goto discard_it;
1925 
1926 	tcp_v6_fill_cb(skb, hdr, th);
1927 
1928 	if (tcp_checksum_complete(skb)) {
1929 csum_error:
1930 		drop_reason = SKB_DROP_REASON_TCP_CSUM;
1931 		trace_tcp_bad_csum(skb);
1932 		__TCP_INC_STATS(net, TCP_MIB_CSUMERRORS);
1933 bad_packet:
1934 		__TCP_INC_STATS(net, TCP_MIB_INERRS);
1935 	} else {
1936 		tcp_v6_send_reset(NULL, skb, sk_rst_convert_drop_reason(drop_reason));
1937 	}
1938 
1939 discard_it:
1940 	SKB_DR_OR(drop_reason, NOT_SPECIFIED);
1941 	sk_skb_reason_drop(sk, skb, drop_reason);
1942 	return 0;
1943 
1944 discard_and_relse:
1945 	sk_drops_skbadd(sk, skb);
1946 	if (refcounted)
1947 		sock_put(sk);
1948 	goto discard_it;
1949 
1950 do_time_wait:
1951 	if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1952 		drop_reason = SKB_DROP_REASON_XFRM_POLICY;
1953 		inet_twsk_put(inet_twsk(sk));
1954 		goto discard_it;
1955 	}
1956 
1957 	tcp_v6_fill_cb(skb, hdr, th);
1958 
1959 	if (tcp_checksum_complete(skb)) {
1960 		inet_twsk_put(inet_twsk(sk));
1961 		goto csum_error;
1962 	}
1963 
1964 	tw_status = tcp_timewait_state_process(inet_twsk(sk), skb, th, &isn,
1965 					       &drop_reason);
1966 	switch (tw_status) {
1967 	case TCP_TW_SYN:
1968 	{
1969 		struct sock *sk2;
1970 
1971 		sk2 = inet6_lookup_listener(net, skb, __tcp_hdrlen(th),
1972 					    &ipv6_hdr(skb)->saddr, th->source,
1973 					    &ipv6_hdr(skb)->daddr,
1974 					    ntohs(th->dest),
1975 					    tcp_v6_iif_l3_slave(skb),
1976 					    sdif);
1977 		if (sk2) {
1978 			struct inet_timewait_sock *tw = inet_twsk(sk);
1979 			inet_twsk_deschedule_put(tw);
1980 			sk = sk2;
1981 			tcp_v6_restore_cb(skb);
1982 			refcounted = false;
1983 			goto process;
1984 		}
1985 
1986 		drop_reason = psp_twsk_rx_policy_check(inet_twsk(sk), skb);
1987 		if (drop_reason) {
1988 			inet_twsk_put(inet_twsk(sk));
1989 			goto discard_it;
1990 		}
1991 	}
1992 		/* to ACK */
1993 		fallthrough;
1994 	case TCP_TW_ACK:
1995 	case TCP_TW_ACK_OOW:
1996 		tcp_v6_timewait_ack(sk, skb, tw_status);
1997 		break;
1998 	case TCP_TW_RST:
1999 		tcp_v6_send_reset(sk, skb, SK_RST_REASON_TCP_TIMEWAIT_SOCKET);
2000 		inet_twsk_deschedule_put(inet_twsk(sk));
2001 		goto discard_it;
2002 	case TCP_TW_SUCCESS:
2003 		;
2004 	}
2005 	goto discard_it;
2006 }
2007 
2008 static struct timewait_sock_ops tcp6_timewait_sock_ops = {
2009 	.twsk_obj_size	= sizeof(struct tcp6_timewait_sock),
2010 };
2011 
2012 const struct inet_connection_sock_af_ops ipv6_specific = {
2013 	.queue_xmit	   = inet6_csk_xmit,
2014 	.rebuild_header	   = inet6_sk_rebuild_header,
2015 	.sk_rx_dst_set	   = inet6_sk_rx_dst_set,
2016 	.conn_request	   = tcp_v6_conn_request,
2017 	.syn_recv_sock	   = tcp_v6_syn_recv_sock,
2018 	.net_header_len	   = sizeof(struct ipv6hdr),
2019 	.setsockopt	   = ipv6_setsockopt,
2020 	.getsockopt	   = ipv6_getsockopt,
2021 	.mtu_reduced	   = tcp_v6_mtu_reduced,
2022 };
2023 
2024 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
2025 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
2026 #ifdef CONFIG_TCP_MD5SIG
2027 	.md5_lookup	=	tcp_v6_md5_lookup,
2028 	.calc_md5_hash	=	tcp_v6_md5_hash_skb,
2029 	.md5_parse	=	tcp_v6_parse_md5_keys,
2030 #endif
2031 #ifdef CONFIG_TCP_AO
2032 	.ao_lookup	=	tcp_v6_ao_lookup,
2033 	.calc_ao_hash	=	tcp_v6_ao_hash_skb,
2034 	.ao_parse	=	tcp_v6_parse_ao,
2035 	.ao_calc_key_sk	=	tcp_v6_ao_calc_key_sk,
2036 #endif
2037 };
2038 #endif
2039 
2040 /*
2041  *	TCP over IPv4 via INET6 API
2042  */
2043 static const struct inet_connection_sock_af_ops ipv6_mapped = {
2044 	.queue_xmit	   = ip_queue_xmit,
2045 	.rebuild_header	   = inet_sk_rebuild_header,
2046 	.sk_rx_dst_set	   = inet_sk_rx_dst_set,
2047 	.conn_request	   = tcp_v6_conn_request,
2048 	.syn_recv_sock	   = tcp_v6_syn_recv_sock,
2049 	.net_header_len	   = sizeof(struct iphdr),
2050 	.setsockopt	   = ipv6_setsockopt,
2051 	.getsockopt	   = ipv6_getsockopt,
2052 	.mtu_reduced	   = tcp_v4_mtu_reduced,
2053 };
2054 
2055 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
2056 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
2057 #ifdef CONFIG_TCP_MD5SIG
2058 	.md5_lookup	=	tcp_v4_md5_lookup,
2059 	.calc_md5_hash	=	tcp_v4_md5_hash_skb,
2060 	.md5_parse	=	tcp_v6_parse_md5_keys,
2061 #endif
2062 #ifdef CONFIG_TCP_AO
2063 	.ao_lookup	=	tcp_v6_ao_lookup,
2064 	.calc_ao_hash	=	tcp_v4_ao_hash_skb,
2065 	.ao_parse	=	tcp_v6_parse_ao,
2066 	.ao_calc_key_sk	=	tcp_v4_ao_calc_key_sk,
2067 #endif
2068 };
2069 
2070 static void tcp6_destruct_sock(struct sock *sk)
2071 {
2072 	tcp_md5_destruct_sock(sk);
2073 	tcp_ao_destroy_sock(sk, false);
2074 	inet6_sock_destruct(sk);
2075 }
2076 #endif
2077 
2078 /* NOTE: A lot of things set to zero explicitly by call to
2079  *       sk_alloc() so need not be done here.
2080  */
2081 static int tcp_v6_init_sock(struct sock *sk)
2082 {
2083 	struct inet_connection_sock *icsk = inet_csk(sk);
2084 
2085 	tcp_init_sock(sk);
2086 
2087 	icsk->icsk_af_ops = &ipv6_specific;
2088 
2089 #if defined(CONFIG_TCP_MD5SIG) || defined(CONFIG_TCP_AO)
2090 	tcp_sk(sk)->af_specific = &tcp_sock_ipv6_specific;
2091 	sk->sk_destruct = tcp6_destruct_sock;
2092 #endif
2093 
2094 	return 0;
2095 }
2096 
2097 #ifdef CONFIG_PROC_FS
2098 /* Proc filesystem TCPv6 sock list dumping. */
2099 static void get_openreq6(struct seq_file *seq,
2100 			 const struct request_sock *req, int i)
2101 {
2102 	long ttd = req->rsk_timer.expires - jiffies;
2103 	const struct in6_addr *src = &inet_rsk(req)->ir_v6_loc_addr;
2104 	const struct in6_addr *dest = &inet_rsk(req)->ir_v6_rmt_addr;
2105 
2106 	if (ttd < 0)
2107 		ttd = 0;
2108 
2109 	seq_printf(seq,
2110 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2111 		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %d %d 0\n",
2112 		   i,
2113 		   src->s6_addr32[0], src->s6_addr32[1],
2114 		   src->s6_addr32[2], src->s6_addr32[3],
2115 		   inet_rsk(req)->ir_num,
2116 		   dest->s6_addr32[0], dest->s6_addr32[1],
2117 		   dest->s6_addr32[2], dest->s6_addr32[3],
2118 		   ntohs(inet_rsk(req)->ir_rmt_port),
2119 		   TCP_SYN_RECV,
2120 		   0, 0, /* could print option size, but that is af dependent. */
2121 		   1,   /* timers active (only the expire timer) */
2122 		   jiffies_to_clock_t(ttd),
2123 		   req->num_timeout,
2124 		   from_kuid_munged(seq_user_ns(seq),
2125 				    sk_uid(req->rsk_listener)),
2126 		   0,  /* non standard timer */
2127 		   0, /* open_requests have no inode */
2128 		   0);
2129 }
2130 
2131 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
2132 {
2133 	const struct in6_addr *dest, *src;
2134 	__u16 destp, srcp;
2135 	int timer_active;
2136 	unsigned long timer_expires;
2137 	const struct inet_sock *inet = inet_sk(sp);
2138 	const struct tcp_sock *tp = tcp_sk(sp);
2139 	const struct inet_connection_sock *icsk = inet_csk(sp);
2140 	const struct fastopen_queue *fastopenq = &icsk->icsk_accept_queue.fastopenq;
2141 	u8 icsk_pending;
2142 	int rx_queue;
2143 	int state;
2144 
2145 	dest  = &sp->sk_v6_daddr;
2146 	src   = &sp->sk_v6_rcv_saddr;
2147 	destp = ntohs(inet->inet_dport);
2148 	srcp  = ntohs(inet->inet_sport);
2149 
2150 	icsk_pending = smp_load_acquire(&icsk->icsk_pending);
2151 	if (icsk_pending == ICSK_TIME_RETRANS ||
2152 	    icsk_pending == ICSK_TIME_REO_TIMEOUT ||
2153 	    icsk_pending == ICSK_TIME_LOSS_PROBE) {
2154 		timer_active	= 1;
2155 		timer_expires	= tcp_timeout_expires(sp);
2156 	} else if (icsk_pending == ICSK_TIME_PROBE0) {
2157 		timer_active	= 4;
2158 		timer_expires	= tcp_timeout_expires(sp);
2159 	} else if (timer_pending(&icsk->icsk_keepalive_timer)) {
2160 		timer_active	= 2;
2161 		timer_expires	= icsk->icsk_keepalive_timer.expires;
2162 	} else {
2163 		timer_active	= 0;
2164 		timer_expires = jiffies;
2165 	}
2166 
2167 	state = inet_sk_state_load(sp);
2168 	if (state == TCP_LISTEN)
2169 		rx_queue = READ_ONCE(sp->sk_ack_backlog);
2170 	else
2171 		/* Because we don't lock the socket,
2172 		 * we might find a transient negative value.
2173 		 */
2174 		rx_queue = max_t(int, READ_ONCE(tp->rcv_nxt) -
2175 				      READ_ONCE(tp->copied_seq), 0);
2176 
2177 	seq_printf(seq,
2178 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2179 		   "%02X %08X:%08X %02X:%08lX %08X %5u %8d %llu %d 0 %lu %lu %u %u %d\n",
2180 		   i,
2181 		   src->s6_addr32[0], src->s6_addr32[1],
2182 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
2183 		   dest->s6_addr32[0], dest->s6_addr32[1],
2184 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
2185 		   state,
2186 		   READ_ONCE(tp->write_seq) - tp->snd_una,
2187 		   rx_queue,
2188 		   timer_active,
2189 		   jiffies_delta_to_clock_t(timer_expires - jiffies),
2190 		   READ_ONCE(icsk->icsk_retransmits),
2191 		   from_kuid_munged(seq_user_ns(seq), sk_uid(sp)),
2192 		   READ_ONCE(icsk->icsk_probes_out),
2193 		   sock_i_ino(sp),
2194 		   refcount_read(&sp->sk_refcnt),
2195 		   jiffies_to_clock_t(icsk->icsk_rto),
2196 		   jiffies_to_clock_t(icsk->icsk_ack.ato),
2197 		   (icsk->icsk_ack.quick << 1) | inet_csk_in_pingpong_mode(sp),
2198 		   tcp_snd_cwnd(tp),
2199 		   state == TCP_LISTEN ?
2200 			fastopenq->max_qlen :
2201 			(tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh)
2202 		   );
2203 }
2204 
2205 static void get_timewait6_sock(struct seq_file *seq,
2206 			       struct inet_timewait_sock *tw, int i)
2207 {
2208 	long delta = tw->tw_timer.expires - jiffies;
2209 	const struct in6_addr *dest, *src;
2210 	__u16 destp, srcp;
2211 
2212 	dest = &tw->tw_v6_daddr;
2213 	src  = &tw->tw_v6_rcv_saddr;
2214 	destp = ntohs(tw->tw_dport);
2215 	srcp  = ntohs(tw->tw_sport);
2216 
2217 	seq_printf(seq,
2218 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2219 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d 0\n",
2220 		   i,
2221 		   src->s6_addr32[0], src->s6_addr32[1],
2222 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
2223 		   dest->s6_addr32[0], dest->s6_addr32[1],
2224 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
2225 		   READ_ONCE(tw->tw_substate), 0, 0,
2226 		   3, jiffies_delta_to_clock_t(delta), 0, 0, 0, 0,
2227 		   refcount_read(&tw->tw_refcnt));
2228 }
2229 
2230 static int tcp6_seq_show(struct seq_file *seq, void *v)
2231 {
2232 	struct tcp_iter_state *st;
2233 	struct sock *sk = v;
2234 
2235 	if (v == SEQ_START_TOKEN) {
2236 		seq_puts(seq,
2237 			 "  sl  "
2238 			 "local_address                         "
2239 			 "remote_address                        "
2240 			 "st tx_queue rx_queue tr tm->when retrnsmt"
2241 			 "   uid  timeout inode\n");
2242 		goto out;
2243 	}
2244 	st = seq->private;
2245 
2246 	if (sk->sk_state == TCP_TIME_WAIT)
2247 		get_timewait6_sock(seq, v, st->num);
2248 	else if (sk->sk_state == TCP_NEW_SYN_RECV)
2249 		get_openreq6(seq, v, st->num);
2250 	else
2251 		get_tcp6_sock(seq, v, st->num);
2252 out:
2253 	return 0;
2254 }
2255 
2256 static const struct seq_operations tcp6_seq_ops = {
2257 	.show		= tcp6_seq_show,
2258 	.start		= tcp_seq_start,
2259 	.next		= tcp_seq_next,
2260 	.stop		= tcp_seq_stop,
2261 };
2262 
2263 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2264 	.family		= AF_INET6,
2265 };
2266 
2267 int __net_init tcp6_proc_init(struct net *net)
2268 {
2269 	if (!proc_create_net_data("tcp6", 0444, net->proc_net, &tcp6_seq_ops,
2270 			sizeof(struct tcp_iter_state), &tcp6_seq_afinfo))
2271 		return -ENOMEM;
2272 	return 0;
2273 }
2274 
2275 void tcp6_proc_exit(struct net *net)
2276 {
2277 	remove_proc_entry("tcp6", net->proc_net);
2278 }
2279 #endif
2280 
2281 struct proto tcpv6_prot = {
2282 	.name			= "TCPv6",
2283 	.owner			= THIS_MODULE,
2284 	.close			= tcp_close,
2285 	.pre_connect		= tcp_v6_pre_connect,
2286 	.connect		= tcp_v6_connect,
2287 	.disconnect		= tcp_disconnect,
2288 	.accept			= inet_csk_accept,
2289 	.ioctl			= tcp_ioctl,
2290 	.init			= tcp_v6_init_sock,
2291 	.destroy		= tcp_v4_destroy_sock,
2292 	.shutdown		= tcp_shutdown,
2293 	.setsockopt		= tcp_setsockopt,
2294 	.getsockopt		= tcp_getsockopt,
2295 	.bpf_bypass_getsockopt	= tcp_bpf_bypass_getsockopt,
2296 	.keepalive		= tcp_set_keepalive,
2297 	.recvmsg		= tcp_recvmsg,
2298 	.sendmsg		= tcp_sendmsg,
2299 	.splice_eof		= tcp_splice_eof,
2300 	.backlog_rcv		= tcp_v6_do_rcv,
2301 	.release_cb		= tcp_release_cb,
2302 	.hash			= inet_hash,
2303 	.unhash			= inet_unhash,
2304 	.get_port		= inet_csk_get_port,
2305 	.put_port		= inet_put_port,
2306 #ifdef CONFIG_BPF_SYSCALL
2307 	.psock_update_sk_prot	= tcp_bpf_update_proto,
2308 #endif
2309 	.enter_memory_pressure	= tcp_enter_memory_pressure,
2310 	.leave_memory_pressure	= tcp_leave_memory_pressure,
2311 	.stream_memory_free	= tcp_stream_memory_free,
2312 	.sockets_allocated	= &tcp_sockets_allocated,
2313 
2314 	.memory_allocated	= &net_aligned_data.tcp_memory_allocated,
2315 	.per_cpu_fw_alloc	= &tcp_memory_per_cpu_fw_alloc,
2316 
2317 	.memory_pressure	= &tcp_memory_pressure,
2318 	.sysctl_mem		= sysctl_tcp_mem,
2319 	.sysctl_wmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_wmem),
2320 	.sysctl_rmem_offset	= offsetof(struct net, ipv4.sysctl_tcp_rmem),
2321 	.max_header		= MAX_TCP_HEADER,
2322 	.obj_size		= sizeof(struct tcp6_sock),
2323 	.freeptr_offset		= offsetof(struct tcp6_sock,
2324 					   tcp.inet_conn.icsk_inet.sk.sk_freeptr),
2325 	.ipv6_pinfo_offset = offsetof(struct tcp6_sock, inet6),
2326 	.slab_flags		= SLAB_TYPESAFE_BY_RCU,
2327 	.twsk_prot		= &tcp6_timewait_sock_ops,
2328 	.rsk_prot		= &tcp6_request_sock_ops,
2329 	.h.hashinfo		= NULL,
2330 	.no_autobind		= true,
2331 	.diag_destroy		= tcp_abort,
2332 };
2333 EXPORT_SYMBOL_GPL(tcpv6_prot);
2334 
2335 
2336 static struct inet_protosw tcpv6_protosw = {
2337 	.type		=	SOCK_STREAM,
2338 	.protocol	=	IPPROTO_TCP,
2339 	.prot		=	&tcpv6_prot,
2340 	.ops		=	&inet6_stream_ops,
2341 	.flags		=	INET_PROTOSW_PERMANENT |
2342 				INET_PROTOSW_ICSK,
2343 };
2344 
2345 static int __net_init tcpv6_net_init(struct net *net)
2346 {
2347 	int res;
2348 
2349 	res = inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2350 				   SOCK_RAW, IPPROTO_TCP, net);
2351 	if (!res)
2352 		net->ipv6.tcp_sk->sk_clockid = CLOCK_MONOTONIC;
2353 
2354 	return res;
2355 }
2356 
2357 static void __net_exit tcpv6_net_exit(struct net *net)
2358 {
2359 	inet_ctl_sock_destroy(net->ipv6.tcp_sk);
2360 }
2361 
2362 static struct pernet_operations tcpv6_net_ops = {
2363 	.init	    = tcpv6_net_init,
2364 	.exit	    = tcpv6_net_exit,
2365 };
2366 
2367 int __init tcpv6_init(void)
2368 {
2369 	int ret;
2370 
2371 	net_hotdata.tcpv6_protocol = (struct inet6_protocol) {
2372 		.handler     = tcp_v6_rcv,
2373 		.err_handler = tcp_v6_err,
2374 		.flags	     = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
2375 	};
2376 	ret = inet6_add_protocol(&net_hotdata.tcpv6_protocol, IPPROTO_TCP);
2377 	if (ret)
2378 		goto out;
2379 
2380 	/* register inet6 protocol */
2381 	ret = inet6_register_protosw(&tcpv6_protosw);
2382 	if (ret)
2383 		goto out_tcpv6_protocol;
2384 
2385 	ret = register_pernet_subsys(&tcpv6_net_ops);
2386 	if (ret)
2387 		goto out_tcpv6_protosw;
2388 
2389 	ret = mptcpv6_init();
2390 	if (ret)
2391 		goto out_tcpv6_pernet_subsys;
2392 
2393 out:
2394 	return ret;
2395 
2396 out_tcpv6_pernet_subsys:
2397 	unregister_pernet_subsys(&tcpv6_net_ops);
2398 out_tcpv6_protosw:
2399 	inet6_unregister_protosw(&tcpv6_protosw);
2400 out_tcpv6_protocol:
2401 	inet6_del_protocol(&net_hotdata.tcpv6_protocol, IPPROTO_TCP);
2402 	goto out;
2403 }
2404 
2405 void tcpv6_exit(void)
2406 {
2407 	unregister_pernet_subsys(&tcpv6_net_ops);
2408 	inet6_unregister_protosw(&tcpv6_protosw);
2409 	inet6_del_protocol(&net_hotdata.tcpv6_protocol, IPPROTO_TCP);
2410 }
2411