3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
10 * linux/net/ipv4/tcp_input.c
11 * linux/net/ipv4/tcp_output.c
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.
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.
26 #include <linux/bottom_half.h>
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/jiffies.h>
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>
41 #include <linux/slab.h>
43 #include <linux/ipv6.h>
44 #include <linux/icmpv6.h>
45 #include <linux/random.h>
48 #include <net/ndisc.h>
49 #include <net/inet6_hashtables.h>
50 #include <net/inet6_connection_sock.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>
60 #include <net/dsfield.h>
61 #include <net/timewait_sock.h>
62 #include <net/netdma.h>
63 #include <net/inet_common.h>
64 #include <net/secure_seq.h>
66 #include <asm/uaccess.h>
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
71 #include <linux/crypto.h>
72 #include <linux/scatterlist.h>
74 static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
75 static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
76 struct request_sock *req);
78 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
79 static void __tcp_v6_send_check(struct sk_buff *skb,
80 const struct in6_addr *saddr,
81 const struct in6_addr *daddr);
83 static const struct inet_connection_sock_af_ops ipv6_mapped;
84 static const struct inet_connection_sock_af_ops ipv6_specific;
85 #ifdef CONFIG_TCP_MD5SIG
86 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
87 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
89 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
90 const struct in6_addr *addr)
96 static void tcp_v6_hash(struct sock *sk)
98 if (sk->sk_state != TCP_CLOSE) {
99 if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
104 __inet6_hash(sk, NULL);
109 static __inline__ __sum16 tcp_v6_check(int len,
110 const struct in6_addr *saddr,
111 const struct in6_addr *daddr,
114 return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
117 static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
119 return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
120 ipv6_hdr(skb)->saddr.s6_addr32,
122 tcp_hdr(skb)->source);
125 static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
128 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
129 struct inet_sock *inet = inet_sk(sk);
130 struct inet_connection_sock *icsk = inet_csk(sk);
131 struct ipv6_pinfo *np = inet6_sk(sk);
132 struct tcp_sock *tp = tcp_sk(sk);
133 struct in6_addr *saddr = NULL, *final_p, final;
134 struct ipv6_txoptions *opt;
137 struct dst_entry *dst;
141 if (addr_len < SIN6_LEN_RFC2133)
144 if (usin->sin6_family != AF_INET6)
145 return -EAFNOSUPPORT;
147 memset(&fl6, 0, sizeof(fl6));
150 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
151 IP6_ECN_flow_init(fl6.flowlabel);
152 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
153 struct ip6_flowlabel *flowlabel;
154 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
155 if (flowlabel == NULL)
157 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
158 fl6_sock_release(flowlabel);
163 * connect() to INADDR_ANY means loopback (BSD'ism).
166 if(ipv6_addr_any(&usin->sin6_addr))
167 usin->sin6_addr.s6_addr[15] = 0x1;
169 addr_type = ipv6_addr_type(&usin->sin6_addr);
171 if(addr_type & IPV6_ADDR_MULTICAST)
174 if (addr_type&IPV6_ADDR_LINKLOCAL) {
175 if (addr_len >= sizeof(struct sockaddr_in6) &&
176 usin->sin6_scope_id) {
177 /* If interface is set while binding, indices
180 if (sk->sk_bound_dev_if &&
181 sk->sk_bound_dev_if != usin->sin6_scope_id)
184 sk->sk_bound_dev_if = usin->sin6_scope_id;
187 /* Connect to link-local address requires an interface */
188 if (!sk->sk_bound_dev_if)
192 if (tp->rx_opt.ts_recent_stamp &&
193 !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) {
194 tp->rx_opt.ts_recent = 0;
195 tp->rx_opt.ts_recent_stamp = 0;
199 ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
200 np->flow_label = fl6.flowlabel;
206 if (addr_type == IPV6_ADDR_MAPPED) {
207 u32 exthdrlen = icsk->icsk_ext_hdr_len;
208 struct sockaddr_in sin;
210 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
212 if (__ipv6_only_sock(sk))
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];
219 icsk->icsk_af_ops = &ipv6_mapped;
220 sk->sk_backlog_rcv = tcp_v4_do_rcv;
221 #ifdef CONFIG_TCP_MD5SIG
222 tp->af_specific = &tcp_sock_ipv6_mapped_specific;
225 err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
228 icsk->icsk_ext_hdr_len = exthdrlen;
229 icsk->icsk_af_ops = &ipv6_specific;
230 sk->sk_backlog_rcv = tcp_v6_do_rcv;
231 #ifdef CONFIG_TCP_MD5SIG
232 tp->af_specific = &tcp_sock_ipv6_specific;
236 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
237 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
244 if (!ipv6_addr_any(&np->rcv_saddr))
245 saddr = &np->rcv_saddr;
247 fl6.flowi6_proto = IPPROTO_TCP;
248 ipv6_addr_copy(&fl6.daddr, &np->daddr);
249 ipv6_addr_copy(&fl6.saddr,
250 (saddr ? saddr : &np->saddr));
251 fl6.flowi6_oif = sk->sk_bound_dev_if;
252 fl6.flowi6_mark = sk->sk_mark;
253 fl6.fl6_dport = usin->sin6_port;
254 fl6.fl6_sport = inet->inet_sport;
256 opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
257 final_p = fl6_update_dst(&fl6, opt, &final);
259 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
261 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
269 ipv6_addr_copy(&np->rcv_saddr, saddr);
272 /* set the source address */
273 ipv6_addr_copy(&np->saddr, saddr);
274 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
276 sk->sk_gso_type = SKB_GSO_TCPV6;
277 __ip6_dst_store(sk, dst, NULL, NULL);
279 rt = (struct rt6_info *) dst;
280 if (tcp_death_row.sysctl_tw_recycle &&
281 !tp->rx_opt.ts_recent_stamp &&
282 ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) {
283 struct inet_peer *peer = rt6_get_peer(rt);
285 * VJ's idea. We save last timestamp seen from
286 * the destination in peer table, when entering state
287 * TIME-WAIT * and initialize rx_opt.ts_recent from it,
288 * when trying new connection.
291 inet_peer_refcheck(peer);
292 if ((u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
293 tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
294 tp->rx_opt.ts_recent = peer->tcp_ts;
299 icsk->icsk_ext_hdr_len = 0;
301 icsk->icsk_ext_hdr_len = opt->opt_flen +
304 tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
306 inet->inet_dport = usin->sin6_port;
308 tcp_set_state(sk, TCP_SYN_SENT);
309 err = inet6_hash_connect(&tcp_death_row, sk);
314 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
319 err = tcp_connect(sk);
326 tcp_set_state(sk, TCP_CLOSE);
329 inet->inet_dport = 0;
330 sk->sk_route_caps = 0;
334 static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
335 u8 type, u8 code, int offset, __be32 info)
337 const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data;
338 const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
339 struct ipv6_pinfo *np;
344 struct net *net = dev_net(skb->dev);
346 sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
347 th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
350 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
355 if (sk->sk_state == TCP_TIME_WAIT) {
356 inet_twsk_put(inet_twsk(sk));
361 if (sock_owned_by_user(sk))
362 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
364 if (sk->sk_state == TCP_CLOSE)
367 if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) {
368 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
373 seq = ntohl(th->seq);
374 if (sk->sk_state != TCP_LISTEN &&
375 !between(seq, tp->snd_una, tp->snd_nxt)) {
376 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
382 if (type == ICMPV6_PKT_TOOBIG) {
383 struct dst_entry *dst;
385 if (sock_owned_by_user(sk))
387 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
390 /* icmp should have updated the destination cache entry */
391 dst = __sk_dst_check(sk, np->dst_cookie);
394 struct inet_sock *inet = inet_sk(sk);
397 /* BUGGG_FUTURE: Again, it is not clear how
398 to handle rthdr case. Ignore this complexity
401 memset(&fl6, 0, sizeof(fl6));
402 fl6.flowi6_proto = IPPROTO_TCP;
403 ipv6_addr_copy(&fl6.daddr, &np->daddr);
404 ipv6_addr_copy(&fl6.saddr, &np->saddr);
405 fl6.flowi6_oif = sk->sk_bound_dev_if;
406 fl6.flowi6_mark = sk->sk_mark;
407 fl6.fl6_dport = inet->inet_dport;
408 fl6.fl6_sport = inet->inet_sport;
409 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
411 dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false);
413 sk->sk_err_soft = -PTR_ERR(dst);
420 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
421 tcp_sync_mss(sk, dst_mtu(dst));
422 tcp_simple_retransmit(sk);
423 } /* else let the usual retransmit timer handle it */
428 icmpv6_err_convert(type, code, &err);
430 /* Might be for an request_sock */
431 switch (sk->sk_state) {
432 struct request_sock *req, **prev;
434 if (sock_owned_by_user(sk))
437 req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
438 &hdr->saddr, inet6_iif(skb));
442 /* ICMPs are not backlogged, hence we cannot get
443 * an established socket here.
445 WARN_ON(req->sk != NULL);
447 if (seq != tcp_rsk(req)->snt_isn) {
448 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
452 inet_csk_reqsk_queue_drop(sk, req, prev);
456 case TCP_SYN_RECV: /* Cannot happen.
457 It can, it SYNs are crossed. --ANK */
458 if (!sock_owned_by_user(sk)) {
460 sk->sk_error_report(sk); /* Wake people up to see the error (see connect in sock.c) */
464 sk->sk_err_soft = err;
468 if (!sock_owned_by_user(sk) && np->recverr) {
470 sk->sk_error_report(sk);
472 sk->sk_err_soft = err;
480 static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req,
481 struct request_values *rvp)
483 struct inet6_request_sock *treq = inet6_rsk(req);
484 struct ipv6_pinfo *np = inet6_sk(sk);
485 struct sk_buff * skb;
486 struct in6_addr * final_p, final;
488 struct dst_entry *dst;
491 memset(&fl6, 0, sizeof(fl6));
492 fl6.flowi6_proto = IPPROTO_TCP;
493 ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
494 ipv6_addr_copy(&fl6.saddr, &treq->loc_addr);
496 fl6.flowi6_oif = treq->iif;
497 fl6.flowi6_mark = sk->sk_mark;
498 fl6.fl6_dport = inet_rsk(req)->rmt_port;
499 fl6.fl6_sport = inet_rsk(req)->loc_port;
500 security_req_classify_flow(req, flowi6_to_flowi(&fl6));
502 final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
504 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
510 skb = tcp_make_synack(sk, dst, req, rvp);
513 __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr);
515 ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
516 err = ip6_xmit(sk, skb, &fl6, rcu_dereference(np->opt),
518 err = net_xmit_eval(err);
526 static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
527 struct request_values *rvp)
529 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
530 return tcp_v6_send_synack(sk, req, rvp);
533 static void tcp_v6_reqsk_destructor(struct request_sock *req)
535 kfree_skb(inet6_rsk(req)->pktopts);
538 #ifdef CONFIG_TCP_MD5SIG
539 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
540 const struct in6_addr *addr)
542 struct tcp_sock *tp = tcp_sk(sk);
547 if (!tp->md5sig_info || !tp->md5sig_info->entries6)
550 for (i = 0; i < tp->md5sig_info->entries6; i++) {
551 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, addr))
552 return &tp->md5sig_info->keys6[i].base;
557 static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
558 struct sock *addr_sk)
560 return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr);
563 static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
564 struct request_sock *req)
566 return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr);
569 static int tcp_v6_md5_do_add(struct sock *sk, const struct in6_addr *peer,
570 char *newkey, u8 newkeylen)
572 /* Add key to the list */
573 struct tcp_md5sig_key *key;
574 struct tcp_sock *tp = tcp_sk(sk);
575 struct tcp6_md5sig_key *keys;
577 key = tcp_v6_md5_do_lookup(sk, peer);
579 /* modify existing entry - just update that one */
582 key->keylen = newkeylen;
584 /* reallocate new list if current one is full. */
585 if (!tp->md5sig_info) {
586 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info), GFP_ATOMIC);
587 if (!tp->md5sig_info) {
591 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
593 if (tp->md5sig_info->entries6 == 0 &&
594 !tcp_alloc_md5sig_pool()) {
598 if (tp->md5sig_info->alloced6 == tp->md5sig_info->entries6) {
599 keys = kmalloc((sizeof (tp->md5sig_info->keys6[0]) *
600 (tp->md5sig_info->entries6 + 1)), GFP_ATOMIC);
607 if (tp->md5sig_info->entries6)
608 memmove(keys, tp->md5sig_info->keys6,
609 (sizeof (tp->md5sig_info->keys6[0]) *
610 tp->md5sig_info->entries6));
612 kfree(tp->md5sig_info->keys6);
613 tp->md5sig_info->keys6 = keys;
614 tp->md5sig_info->alloced6++;
617 ipv6_addr_copy(&tp->md5sig_info->keys6[tp->md5sig_info->entries6].addr,
619 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.key = newkey;
620 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.keylen = newkeylen;
622 tp->md5sig_info->entries6++;
627 static int tcp_v6_md5_add_func(struct sock *sk, struct sock *addr_sk,
628 u8 *newkey, __u8 newkeylen)
630 return tcp_v6_md5_do_add(sk, &inet6_sk(addr_sk)->daddr,
634 static int tcp_v6_md5_do_del(struct sock *sk, const struct in6_addr *peer)
636 struct tcp_sock *tp = tcp_sk(sk);
639 for (i = 0; i < tp->md5sig_info->entries6; i++) {
640 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, peer)) {
642 kfree(tp->md5sig_info->keys6[i].base.key);
643 tp->md5sig_info->entries6--;
645 if (tp->md5sig_info->entries6 == 0) {
646 kfree(tp->md5sig_info->keys6);
647 tp->md5sig_info->keys6 = NULL;
648 tp->md5sig_info->alloced6 = 0;
650 /* shrink the database */
651 if (tp->md5sig_info->entries6 != i)
652 memmove(&tp->md5sig_info->keys6[i],
653 &tp->md5sig_info->keys6[i+1],
654 (tp->md5sig_info->entries6 - i)
655 * sizeof (tp->md5sig_info->keys6[0]));
663 static void tcp_v6_clear_md5_list (struct sock *sk)
665 struct tcp_sock *tp = tcp_sk(sk);
668 if (tp->md5sig_info->entries6) {
669 for (i = 0; i < tp->md5sig_info->entries6; i++)
670 kfree(tp->md5sig_info->keys6[i].base.key);
671 tp->md5sig_info->entries6 = 0;
674 kfree(tp->md5sig_info->keys6);
675 tp->md5sig_info->keys6 = NULL;
676 tp->md5sig_info->alloced6 = 0;
678 if (tp->md5sig_info->entries4) {
679 for (i = 0; i < tp->md5sig_info->entries4; i++)
680 kfree(tp->md5sig_info->keys4[i].base.key);
681 tp->md5sig_info->entries4 = 0;
684 kfree(tp->md5sig_info->keys4);
685 tp->md5sig_info->keys4 = NULL;
686 tp->md5sig_info->alloced4 = 0;
689 static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
692 struct tcp_md5sig cmd;
693 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
696 if (optlen < sizeof(cmd))
699 if (copy_from_user(&cmd, optval, sizeof(cmd)))
702 if (sin6->sin6_family != AF_INET6)
705 if (!cmd.tcpm_keylen) {
706 if (!tcp_sk(sk)->md5sig_info)
708 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
709 return tcp_v4_md5_do_del(sk, sin6->sin6_addr.s6_addr32[3]);
710 return tcp_v6_md5_do_del(sk, &sin6->sin6_addr);
713 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
716 if (!tcp_sk(sk)->md5sig_info) {
717 struct tcp_sock *tp = tcp_sk(sk);
718 struct tcp_md5sig_info *p;
720 p = kzalloc(sizeof(struct tcp_md5sig_info), GFP_KERNEL);
725 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
728 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
731 if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
732 return tcp_v4_md5_do_add(sk, sin6->sin6_addr.s6_addr32[3],
733 newkey, cmd.tcpm_keylen);
735 return tcp_v6_md5_do_add(sk, &sin6->sin6_addr, newkey, cmd.tcpm_keylen);
738 static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
739 const struct in6_addr *daddr,
740 const struct in6_addr *saddr, int nbytes)
742 struct tcp6_pseudohdr *bp;
743 struct scatterlist sg;
745 bp = &hp->md5_blk.ip6;
746 /* 1. TCP pseudo-header (RFC2460) */
747 ipv6_addr_copy(&bp->saddr, saddr);
748 ipv6_addr_copy(&bp->daddr, daddr);
749 bp->protocol = cpu_to_be32(IPPROTO_TCP);
750 bp->len = cpu_to_be32(nbytes);
752 sg_init_one(&sg, bp, sizeof(*bp));
753 return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
756 static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
757 const struct in6_addr *daddr, struct in6_addr *saddr,
758 const struct tcphdr *th)
760 struct tcp_md5sig_pool *hp;
761 struct hash_desc *desc;
763 hp = tcp_get_md5sig_pool();
765 goto clear_hash_noput;
766 desc = &hp->md5_desc;
768 if (crypto_hash_init(desc))
770 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
772 if (tcp_md5_hash_header(hp, th))
774 if (tcp_md5_hash_key(hp, key))
776 if (crypto_hash_final(desc, md5_hash))
779 tcp_put_md5sig_pool();
783 tcp_put_md5sig_pool();
785 memset(md5_hash, 0, 16);
789 static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
790 const struct sock *sk,
791 const struct request_sock *req,
792 const struct sk_buff *skb)
794 const struct in6_addr *saddr, *daddr;
795 struct tcp_md5sig_pool *hp;
796 struct hash_desc *desc;
797 const struct tcphdr *th = tcp_hdr(skb);
800 saddr = &inet6_sk(sk)->saddr;
801 daddr = &inet6_sk(sk)->daddr;
803 saddr = &inet6_rsk(req)->loc_addr;
804 daddr = &inet6_rsk(req)->rmt_addr;
806 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
807 saddr = &ip6h->saddr;
808 daddr = &ip6h->daddr;
811 hp = tcp_get_md5sig_pool();
813 goto clear_hash_noput;
814 desc = &hp->md5_desc;
816 if (crypto_hash_init(desc))
819 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
821 if (tcp_md5_hash_header(hp, th))
823 if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
825 if (tcp_md5_hash_key(hp, key))
827 if (crypto_hash_final(desc, md5_hash))
830 tcp_put_md5sig_pool();
834 tcp_put_md5sig_pool();
836 memset(md5_hash, 0, 16);
840 static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
842 const __u8 *hash_location = NULL;
843 struct tcp_md5sig_key *hash_expected;
844 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
845 const struct tcphdr *th = tcp_hdr(skb);
849 hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
850 hash_location = tcp_parse_md5sig_option(th);
852 /* We've parsed the options - do we have a hash? */
853 if (!hash_expected && !hash_location)
856 if (hash_expected && !hash_location) {
857 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
861 if (!hash_expected && hash_location) {
862 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
866 /* check the signature */
867 genhash = tcp_v6_md5_hash_skb(newhash,
871 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
872 if (net_ratelimit()) {
873 printk(KERN_INFO "MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
874 genhash ? "failed" : "mismatch",
875 &ip6h->saddr, ntohs(th->source),
876 &ip6h->daddr, ntohs(th->dest));
884 struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
886 .obj_size = sizeof(struct tcp6_request_sock),
887 .rtx_syn_ack = tcp_v6_rtx_synack,
888 .send_ack = tcp_v6_reqsk_send_ack,
889 .destructor = tcp_v6_reqsk_destructor,
890 .send_reset = tcp_v6_send_reset,
891 .syn_ack_timeout = tcp_syn_ack_timeout,
894 #ifdef CONFIG_TCP_MD5SIG
895 static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
896 .md5_lookup = tcp_v6_reqsk_md5_lookup,
897 .calc_md5_hash = tcp_v6_md5_hash_skb,
901 static void __tcp_v6_send_check(struct sk_buff *skb,
902 const struct in6_addr *saddr, const struct in6_addr *daddr)
904 struct tcphdr *th = tcp_hdr(skb);
906 if (skb->ip_summed == CHECKSUM_PARTIAL) {
907 th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
908 skb->csum_start = skb_transport_header(skb) - skb->head;
909 skb->csum_offset = offsetof(struct tcphdr, check);
911 th->check = tcp_v6_check(skb->len, saddr, daddr,
912 csum_partial(th, th->doff << 2,
917 static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
919 struct ipv6_pinfo *np = inet6_sk(sk);
921 __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
924 static int tcp_v6_gso_send_check(struct sk_buff *skb)
926 const struct ipv6hdr *ipv6h;
929 if (!pskb_may_pull(skb, sizeof(*th)))
932 ipv6h = ipv6_hdr(skb);
936 skb->ip_summed = CHECKSUM_PARTIAL;
937 __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
941 static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
944 const struct ipv6hdr *iph = skb_gro_network_header(skb);
946 switch (skb->ip_summed) {
947 case CHECKSUM_COMPLETE:
948 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
950 skb->ip_summed = CHECKSUM_UNNECESSARY;
956 NAPI_GRO_CB(skb)->flush = 1;
960 return tcp_gro_receive(head, skb);
963 static int tcp6_gro_complete(struct sk_buff *skb)
965 const struct ipv6hdr *iph = ipv6_hdr(skb);
966 struct tcphdr *th = tcp_hdr(skb);
968 th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
969 &iph->saddr, &iph->daddr, 0);
970 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
972 return tcp_gro_complete(skb);
975 static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
976 u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
978 const struct tcphdr *th = tcp_hdr(skb);
980 struct sk_buff *buff;
982 struct net *net = dev_net(skb_dst(skb)->dev);
983 struct sock *ctl_sk = net->ipv6.tcp_sk;
984 unsigned int tot_len = sizeof(struct tcphdr);
985 struct dst_entry *dst;
989 tot_len += TCPOLEN_TSTAMP_ALIGNED;
990 #ifdef CONFIG_TCP_MD5SIG
992 tot_len += TCPOLEN_MD5SIG_ALIGNED;
995 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
1000 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
1002 t1 = (struct tcphdr *) skb_push(buff, tot_len);
1003 skb_reset_transport_header(buff);
1005 /* Swap the send and the receive. */
1006 memset(t1, 0, sizeof(*t1));
1007 t1->dest = th->source;
1008 t1->source = th->dest;
1009 t1->doff = tot_len / 4;
1010 t1->seq = htonl(seq);
1011 t1->ack_seq = htonl(ack);
1012 t1->ack = !rst || !th->ack;
1014 t1->window = htons(win);
1016 topt = (__be32 *)(t1 + 1);
1019 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1020 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1021 *topt++ = htonl(tcp_time_stamp);
1022 *topt++ = htonl(ts);
1025 #ifdef CONFIG_TCP_MD5SIG
1027 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1028 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
1029 tcp_v6_md5_hash_hdr((__u8 *)topt, key,
1030 &ipv6_hdr(skb)->saddr,
1031 &ipv6_hdr(skb)->daddr, t1);
1035 memset(&fl6, 0, sizeof(fl6));
1036 ipv6_addr_copy(&fl6.daddr, &ipv6_hdr(skb)->saddr);
1037 ipv6_addr_copy(&fl6.saddr, &ipv6_hdr(skb)->daddr);
1039 buff->ip_summed = CHECKSUM_PARTIAL;
1042 __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
1044 fl6.flowi6_proto = IPPROTO_TCP;
1045 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
1046 fl6.flowi6_oif = inet6_iif(skb);
1047 fl6.fl6_dport = t1->dest;
1048 fl6.fl6_sport = t1->source;
1049 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
1051 /* Pass a socket to ip6_dst_lookup either it is for RST
1052 * Underlying function will use this to retrieve the network
1055 dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
1057 skb_dst_set(buff, dst);
1058 ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
1059 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
1061 TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
1068 static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
1070 const struct tcphdr *th = tcp_hdr(skb);
1071 u32 seq = 0, ack_seq = 0;
1072 struct tcp_md5sig_key *key = NULL;
1077 if (!ipv6_unicast_destination(skb))
1080 #ifdef CONFIG_TCP_MD5SIG
1082 key = tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr);
1086 seq = ntohl(th->ack_seq);
1088 ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
1091 tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
1094 static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
1095 struct tcp_md5sig_key *key, u8 tclass)
1097 tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
1100 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
1102 struct inet_timewait_sock *tw = inet_twsk(sk);
1103 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
1105 tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
1106 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
1107 tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
1113 static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
1114 struct request_sock *req)
1116 tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
1117 tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
1121 static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
1123 struct request_sock *req, **prev;
1124 const struct tcphdr *th = tcp_hdr(skb);
1127 /* Find possible connection requests. */
1128 req = inet6_csk_search_req(sk, &prev, th->source,
1129 &ipv6_hdr(skb)->saddr,
1130 &ipv6_hdr(skb)->daddr, inet6_iif(skb));
1132 return tcp_check_req(sk, skb, req, prev);
1134 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
1135 &ipv6_hdr(skb)->saddr, th->source,
1136 &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
1139 if (nsk->sk_state != TCP_TIME_WAIT) {
1143 inet_twsk_put(inet_twsk(nsk));
1147 #ifdef CONFIG_SYN_COOKIES
1149 sk = cookie_v6_check(sk, skb);
1154 /* FIXME: this is substantially similar to the ipv4 code.
1155 * Can some kind of merge be done? -- erics
1157 static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1159 struct tcp_extend_values tmp_ext;
1160 struct tcp_options_received tmp_opt;
1161 const u8 *hash_location;
1162 struct request_sock *req;
1163 struct inet6_request_sock *treq;
1164 struct ipv6_pinfo *np = inet6_sk(sk);
1165 struct tcp_sock *tp = tcp_sk(sk);
1166 __u32 isn = TCP_SKB_CB(skb)->when;
1167 struct dst_entry *dst = NULL;
1168 int want_cookie = 0;
1170 if (skb->protocol == htons(ETH_P_IP))
1171 return tcp_v4_conn_request(sk, skb);
1173 if (!ipv6_unicast_destination(skb))
1176 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
1177 want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6");
1182 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
1185 req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
1189 #ifdef CONFIG_TCP_MD5SIG
1190 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
1193 tcp_clear_options(&tmp_opt);
1194 tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
1195 tmp_opt.user_mss = tp->rx_opt.user_mss;
1196 tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
1198 if (tmp_opt.cookie_plus > 0 &&
1199 tmp_opt.saw_tstamp &&
1200 !tp->rx_opt.cookie_out_never &&
1201 (sysctl_tcp_cookie_size > 0 ||
1202 (tp->cookie_values != NULL &&
1203 tp->cookie_values->cookie_desired > 0))) {
1206 u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
1207 int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
1209 if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
1212 /* Secret recipe starts with IP addresses */
1213 d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
1218 d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
1224 /* plus variable length Initiator Cookie */
1227 *c++ ^= *hash_location++;
1229 want_cookie = 0; /* not our kind of cookie */
1230 tmp_ext.cookie_out_never = 0; /* false */
1231 tmp_ext.cookie_plus = tmp_opt.cookie_plus;
1232 } else if (!tp->rx_opt.cookie_in_always) {
1233 /* redundant indications, but ensure initialization. */
1234 tmp_ext.cookie_out_never = 1; /* true */
1235 tmp_ext.cookie_plus = 0;
1239 tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
1241 if (want_cookie && !tmp_opt.saw_tstamp)
1242 tcp_clear_options(&tmp_opt);
1244 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1245 tcp_openreq_init(req, &tmp_opt, skb);
1247 treq = inet6_rsk(req);
1248 ipv6_addr_copy(&treq->rmt_addr, &ipv6_hdr(skb)->saddr);
1249 ipv6_addr_copy(&treq->loc_addr, &ipv6_hdr(skb)->daddr);
1250 if (!want_cookie || tmp_opt.tstamp_ok)
1251 TCP_ECN_create_request(req, skb);
1253 treq->iif = sk->sk_bound_dev_if;
1255 /* So that link locals have meaning */
1256 if (!sk->sk_bound_dev_if &&
1257 ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL)
1258 treq->iif = inet6_iif(skb);
1261 struct inet_peer *peer = NULL;
1263 if (ipv6_opt_accepted(sk, skb) ||
1264 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
1265 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
1266 atomic_inc(&skb->users);
1267 treq->pktopts = skb;
1271 isn = cookie_v6_init_sequence(sk, skb, &req->mss);
1272 req->cookie_ts = tmp_opt.tstamp_ok;
1276 /* VJ's idea. We save last timestamp seen
1277 * from the destination in peer table, when entering
1278 * state TIME-WAIT, and check against it before
1279 * accepting new connection request.
1281 * If "isn" is not zero, this request hit alive
1282 * timewait bucket, so that all the necessary checks
1283 * are made in the function processing timewait state.
1285 if (tmp_opt.saw_tstamp &&
1286 tcp_death_row.sysctl_tw_recycle &&
1287 (dst = inet6_csk_route_req(sk, req)) != NULL &&
1288 (peer = rt6_get_peer((struct rt6_info *)dst)) != NULL &&
1289 ipv6_addr_equal((struct in6_addr *)peer->daddr.addr.a6,
1291 inet_peer_refcheck(peer);
1292 if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
1293 (s32)(peer->tcp_ts - req->ts_recent) >
1295 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
1296 goto drop_and_release;
1299 /* Kill the following clause, if you dislike this way. */
1300 else if (!sysctl_tcp_syncookies &&
1301 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
1302 (sysctl_max_syn_backlog >> 2)) &&
1303 (!peer || !peer->tcp_ts_stamp) &&
1304 (!dst || !dst_metric(dst, RTAX_RTT))) {
1305 /* Without syncookies last quarter of
1306 * backlog is filled with destinations,
1307 * proven to be alive.
1308 * It means that we continue to communicate
1309 * to destinations, already remembered
1310 * to the moment of synflood.
1312 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
1313 &treq->rmt_addr, ntohs(tcp_hdr(skb)->source));
1314 goto drop_and_release;
1317 isn = tcp_v6_init_sequence(skb);
1320 tcp_rsk(req)->snt_isn = isn;
1321 tcp_rsk(req)->snt_synack = tcp_time_stamp;
1323 security_inet_conn_request(sk, skb, req);
1325 if (tcp_v6_send_synack(sk, req,
1326 (struct request_values *)&tmp_ext) ||
1330 inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
1338 return 0; /* don't send reset */
1341 static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1342 struct request_sock *req,
1343 struct dst_entry *dst)
1345 struct inet6_request_sock *treq;
1346 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1347 struct tcp6_sock *newtcp6sk;
1348 struct inet_sock *newinet;
1349 struct tcp_sock *newtp;
1351 struct ipv6_txoptions *opt;
1352 #ifdef CONFIG_TCP_MD5SIG
1353 struct tcp_md5sig_key *key;
1356 if (skb->protocol == htons(ETH_P_IP)) {
1361 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1366 newtcp6sk = (struct tcp6_sock *)newsk;
1367 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1369 newinet = inet_sk(newsk);
1370 newnp = inet6_sk(newsk);
1371 newtp = tcp_sk(newsk);
1373 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1375 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
1377 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1379 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
1381 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1382 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
1383 #ifdef CONFIG_TCP_MD5SIG
1384 newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
1387 newnp->ipv6_ac_list = NULL;
1388 newnp->ipv6_fl_list = NULL;
1389 newnp->pktoptions = NULL;
1391 newnp->mcast_oif = inet6_iif(skb);
1392 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1395 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
1396 * here, tcp_create_openreq_child now does this for us, see the comment in
1397 * that function for the gory details. -acme
1400 /* It is tricky place. Until this moment IPv4 tcp
1401 worked with IPv6 icsk.icsk_af_ops.
1404 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
1409 treq = inet6_rsk(req);
1411 if (sk_acceptq_is_full(sk))
1415 dst = inet6_csk_route_req(sk, req);
1420 newsk = tcp_create_openreq_child(sk, req, skb);
1425 * No need to charge this sock to the relevant IPv6 refcnt debug socks
1426 * count here, tcp_create_openreq_child now does this for us, see the
1427 * comment in that function for the gory details. -acme
1430 newsk->sk_gso_type = SKB_GSO_TCPV6;
1431 __ip6_dst_store(newsk, dst, NULL, NULL);
1433 newtcp6sk = (struct tcp6_sock *)newsk;
1434 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1436 newtp = tcp_sk(newsk);
1437 newinet = inet_sk(newsk);
1438 newnp = inet6_sk(newsk);
1440 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1442 ipv6_addr_copy(&newnp->daddr, &treq->rmt_addr);
1443 ipv6_addr_copy(&newnp->saddr, &treq->loc_addr);
1444 ipv6_addr_copy(&newnp->rcv_saddr, &treq->loc_addr);
1445 newsk->sk_bound_dev_if = treq->iif;
1447 /* Now IPv6 options...
1449 First: no IPv4 options.
1451 newinet->inet_opt = NULL;
1452 newnp->ipv6_ac_list = NULL;
1453 newnp->ipv6_fl_list = NULL;
1456 newnp->rxopt.all = np->rxopt.all;
1458 /* Clone pktoptions received with SYN */
1459 newnp->pktoptions = NULL;
1460 if (treq->pktopts != NULL) {
1461 newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
1462 kfree_skb(treq->pktopts);
1463 treq->pktopts = NULL;
1464 if (newnp->pktoptions)
1465 skb_set_owner_r(newnp->pktoptions, newsk);
1468 newnp->mcast_oif = inet6_iif(skb);
1469 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1471 /* Clone native IPv6 options from listening socket (if any)
1473 Yes, keeping reference count would be much more clever,
1474 but we make one more one thing there: reattach optmem
1477 opt = rcu_dereference(np->opt);
1479 opt = ipv6_dup_options(newsk, opt);
1480 RCU_INIT_POINTER(newnp->opt, opt);
1482 inet_csk(newsk)->icsk_ext_hdr_len = 0;
1484 inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
1487 tcp_mtup_init(newsk);
1488 tcp_sync_mss(newsk, dst_mtu(dst));
1489 newtp->advmss = dst_metric_advmss(dst);
1490 if (tcp_sk(sk)->rx_opt.user_mss &&
1491 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1492 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1494 tcp_initialize_rcv_mss(newsk);
1495 if (tcp_rsk(req)->snt_synack)
1496 tcp_valid_rtt_meas(newsk,
1497 tcp_time_stamp - tcp_rsk(req)->snt_synack);
1498 newtp->total_retrans = req->retrans;
1500 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1501 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
1503 #ifdef CONFIG_TCP_MD5SIG
1504 /* Copy over the MD5 key from the original socket */
1505 if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
1506 /* We're using one, so create a matching key
1507 * on the newsk structure. If we fail to get
1508 * memory, then we end up not copying the key
1511 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1513 tcp_v6_md5_do_add(newsk, &newnp->daddr,
1514 newkey, key->keylen);
1518 if (__inet_inherit_port(sk, newsk) < 0) {
1519 inet_csk_prepare_forced_close(newsk);
1523 __inet6_hash(newsk, NULL);
1528 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1532 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
1536 static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
1538 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1539 if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
1540 &ipv6_hdr(skb)->daddr, skb->csum)) {
1541 skb->ip_summed = CHECKSUM_UNNECESSARY;
1546 skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
1547 &ipv6_hdr(skb)->saddr,
1548 &ipv6_hdr(skb)->daddr, 0));
1550 if (skb->len <= 76) {
1551 return __skb_checksum_complete(skb);
1556 /* The socket must have it's spinlock held when we get
1559 * We have a potential double-lock case here, so even when
1560 * doing backlog processing we use the BH locking scheme.
1561 * This is because we cannot sleep with the original spinlock
1564 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1566 struct ipv6_pinfo *np = inet6_sk(sk);
1567 struct tcp_sock *tp;
1568 struct sk_buff *opt_skb = NULL;
1570 /* Imagine: socket is IPv6. IPv4 packet arrives,
1571 goes to IPv4 receive handler and backlogged.
1572 From backlog it always goes here. Kerboom...
1573 Fortunately, tcp_rcv_established and rcv_established
1574 handle them correctly, but it is not case with
1575 tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
1578 if (skb->protocol == htons(ETH_P_IP))
1579 return tcp_v4_do_rcv(sk, skb);
1581 #ifdef CONFIG_TCP_MD5SIG
1582 if (tcp_v6_inbound_md5_hash (sk, skb))
1586 if (sk_filter(sk, skb))
1590 * socket locking is here for SMP purposes as backlog rcv
1591 * is currently called with bh processing disabled.
1594 /* Do Stevens' IPV6_PKTOPTIONS.
1596 Yes, guys, it is the only place in our code, where we
1597 may make it not affecting IPv4.
1598 The rest of code is protocol independent,
1599 and I do not like idea to uglify IPv4.
1601 Actually, all the idea behind IPV6_PKTOPTIONS
1602 looks not very well thought. For now we latch
1603 options, received in the last packet, enqueued
1604 by tcp. Feel free to propose better solution.
1608 opt_skb = skb_clone(skb, GFP_ATOMIC);
1610 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1611 sock_rps_save_rxhash(sk, skb);
1612 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
1615 goto ipv6_pktoptions;
1619 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
1622 if (sk->sk_state == TCP_LISTEN) {
1623 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1628 * Queue it on the new socket if the new socket is active,
1629 * otherwise we just shortcircuit this and continue with
1633 sock_rps_save_rxhash(nsk, skb);
1634 if (tcp_child_process(sk, nsk, skb))
1637 __kfree_skb(opt_skb);
1641 sock_rps_save_rxhash(sk, skb);
1643 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
1646 goto ipv6_pktoptions;
1650 tcp_v6_send_reset(sk, skb);
1653 __kfree_skb(opt_skb);
1657 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
1662 /* Do you ask, what is it?
1664 1. skb was enqueued by tcp.
1665 2. skb is added to tail of read queue, rather than out of order.
1666 3. socket is not in passive state.
1667 4. Finally, it really contains options, which user wants to receive.
1670 if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1671 !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1672 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
1673 np->mcast_oif = inet6_iif(opt_skb);
1674 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
1675 np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
1676 if (ipv6_opt_accepted(sk, opt_skb)) {
1677 skb_set_owner_r(opt_skb, sk);
1678 opt_skb = xchg(&np->pktoptions, opt_skb);
1680 __kfree_skb(opt_skb);
1681 opt_skb = xchg(&np->pktoptions, NULL);
1689 static int tcp_v6_rcv(struct sk_buff *skb)
1691 const struct tcphdr *th;
1692 const struct ipv6hdr *hdr;
1695 struct net *net = dev_net(skb->dev);
1697 if (skb->pkt_type != PACKET_HOST)
1701 * Count it even if it's bad.
1703 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
1705 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1710 if (th->doff < sizeof(struct tcphdr)/4)
1712 if (!pskb_may_pull(skb, th->doff*4))
1715 if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
1719 hdr = ipv6_hdr(skb);
1720 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1721 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1722 skb->len - th->doff*4);
1723 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1724 TCP_SKB_CB(skb)->when = 0;
1725 TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
1726 TCP_SKB_CB(skb)->sacked = 0;
1728 sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
1733 if (sk->sk_state == TCP_TIME_WAIT)
1736 if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
1737 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
1738 goto discard_and_relse;
1741 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1742 goto discard_and_relse;
1744 if (sk_filter(sk, skb))
1745 goto discard_and_relse;
1749 bh_lock_sock_nested(sk);
1751 if (!sock_owned_by_user(sk)) {
1752 #ifdef CONFIG_NET_DMA
1753 struct tcp_sock *tp = tcp_sk(sk);
1754 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1755 tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
1756 if (tp->ucopy.dma_chan)
1757 ret = tcp_v6_do_rcv(sk, skb);
1761 if (!tcp_prequeue(sk, skb))
1762 ret = tcp_v6_do_rcv(sk, skb);
1764 } else if (unlikely(sk_add_backlog(sk, skb))) {
1766 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
1767 goto discard_and_relse;
1772 return ret ? -1 : 0;
1775 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1778 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1780 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1782 tcp_v6_send_reset(NULL, skb);
1799 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1800 inet_twsk_put(inet_twsk(sk));
1804 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1805 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1806 inet_twsk_put(inet_twsk(sk));
1810 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
1815 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
1816 &ipv6_hdr(skb)->daddr,
1817 ntohs(th->dest), inet6_iif(skb));
1819 struct inet_timewait_sock *tw = inet_twsk(sk);
1820 inet_twsk_deschedule(tw, &tcp_death_row);
1825 /* Fall through to ACK */
1828 tcp_v6_timewait_ack(sk, skb);
1832 case TCP_TW_SUCCESS:;
1837 static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
1839 struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
1840 struct ipv6_pinfo *np = inet6_sk(sk);
1841 struct inet_peer *peer;
1844 !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
1845 peer = inet_getpeer_v6(&np->daddr, 1);
1849 rt6_bind_peer(rt, 1);
1850 peer = rt->rt6i_peer;
1851 *release_it = false;
1857 static void *tcp_v6_tw_get_peer(struct sock *sk)
1859 const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
1860 const struct inet_timewait_sock *tw = inet_twsk(sk);
1862 if (tw->tw_family == AF_INET)
1863 return tcp_v4_tw_get_peer(sk);
1865 return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
1868 static struct timewait_sock_ops tcp6_timewait_sock_ops = {
1869 .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
1870 .twsk_unique = tcp_twsk_unique,
1871 .twsk_destructor= tcp_twsk_destructor,
1872 .twsk_getpeer = tcp_v6_tw_get_peer,
1875 static const struct inet_connection_sock_af_ops ipv6_specific = {
1876 .queue_xmit = inet6_csk_xmit,
1877 .send_check = tcp_v6_send_check,
1878 .rebuild_header = inet6_sk_rebuild_header,
1879 .conn_request = tcp_v6_conn_request,
1880 .syn_recv_sock = tcp_v6_syn_recv_sock,
1881 .get_peer = tcp_v6_get_peer,
1882 .net_header_len = sizeof(struct ipv6hdr),
1883 .setsockopt = ipv6_setsockopt,
1884 .getsockopt = ipv6_getsockopt,
1885 .addr2sockaddr = inet6_csk_addr2sockaddr,
1886 .sockaddr_len = sizeof(struct sockaddr_in6),
1887 .bind_conflict = inet6_csk_bind_conflict,
1888 #ifdef CONFIG_COMPAT
1889 .compat_setsockopt = compat_ipv6_setsockopt,
1890 .compat_getsockopt = compat_ipv6_getsockopt,
1894 #ifdef CONFIG_TCP_MD5SIG
1895 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
1896 .md5_lookup = tcp_v6_md5_lookup,
1897 .calc_md5_hash = tcp_v6_md5_hash_skb,
1898 .md5_add = tcp_v6_md5_add_func,
1899 .md5_parse = tcp_v6_parse_md5_keys,
1904 * TCP over IPv4 via INET6 API
1907 static const struct inet_connection_sock_af_ops ipv6_mapped = {
1908 .queue_xmit = ip_queue_xmit,
1909 .send_check = tcp_v4_send_check,
1910 .rebuild_header = inet_sk_rebuild_header,
1911 .conn_request = tcp_v6_conn_request,
1912 .syn_recv_sock = tcp_v6_syn_recv_sock,
1913 .get_peer = tcp_v4_get_peer,
1914 .net_header_len = sizeof(struct iphdr),
1915 .setsockopt = ipv6_setsockopt,
1916 .getsockopt = ipv6_getsockopt,
1917 .addr2sockaddr = inet6_csk_addr2sockaddr,
1918 .sockaddr_len = sizeof(struct sockaddr_in6),
1919 .bind_conflict = inet6_csk_bind_conflict,
1920 #ifdef CONFIG_COMPAT
1921 .compat_setsockopt = compat_ipv6_setsockopt,
1922 .compat_getsockopt = compat_ipv6_getsockopt,
1926 #ifdef CONFIG_TCP_MD5SIG
1927 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
1928 .md5_lookup = tcp_v4_md5_lookup,
1929 .calc_md5_hash = tcp_v4_md5_hash_skb,
1930 .md5_add = tcp_v6_md5_add_func,
1931 .md5_parse = tcp_v6_parse_md5_keys,
1935 /* NOTE: A lot of things set to zero explicitly by call to
1936 * sk_alloc() so need not be done here.
1938 static int tcp_v6_init_sock(struct sock *sk)
1940 struct inet_connection_sock *icsk = inet_csk(sk);
1941 struct tcp_sock *tp = tcp_sk(sk);
1943 skb_queue_head_init(&tp->out_of_order_queue);
1944 tcp_init_xmit_timers(sk);
1945 tcp_prequeue_init(tp);
1947 icsk->icsk_rto = TCP_TIMEOUT_INIT;
1948 tp->mdev = TCP_TIMEOUT_INIT;
1950 /* So many TCP implementations out there (incorrectly) count the
1951 * initial SYN frame in their delayed-ACK and congestion control
1952 * algorithms that we must have the following bandaid to talk
1953 * efficiently to them. -DaveM
1957 /* See draft-stevens-tcpca-spec-01 for discussion of the
1958 * initialization of these values.
1960 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1961 tp->snd_cwnd_clamp = ~0;
1962 tp->mss_cache = TCP_MSS_DEFAULT;
1964 tp->reordering = sysctl_tcp_reordering;
1966 sk->sk_state = TCP_CLOSE;
1968 icsk->icsk_af_ops = &ipv6_specific;
1969 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1970 icsk->icsk_sync_mss = tcp_sync_mss;
1971 sk->sk_write_space = sk_stream_write_space;
1972 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1974 #ifdef CONFIG_TCP_MD5SIG
1975 tp->af_specific = &tcp_sock_ipv6_specific;
1978 /* TCP Cookie Transactions */
1979 if (sysctl_tcp_cookie_size > 0) {
1980 /* Default, cookies without s_data_payload. */
1982 kzalloc(sizeof(*tp->cookie_values),
1984 if (tp->cookie_values != NULL)
1985 kref_init(&tp->cookie_values->kref);
1987 /* Presumed zeroed, in order of appearance:
1988 * cookie_in_always, cookie_out_never,
1989 * s_data_constant, s_data_in, s_data_out
1991 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1992 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1995 percpu_counter_inc(&tcp_sockets_allocated);
2001 static void tcp_v6_destroy_sock(struct sock *sk)
2003 #ifdef CONFIG_TCP_MD5SIG
2004 /* Clean up the MD5 key list */
2005 if (tcp_sk(sk)->md5sig_info)
2006 tcp_v6_clear_md5_list(sk);
2008 tcp_v4_destroy_sock(sk);
2009 inet6_destroy_sock(sk);
2012 #ifdef CONFIG_PROC_FS
2013 /* Proc filesystem TCPv6 sock list dumping. */
2014 static void get_openreq6(struct seq_file *seq,
2015 const struct sock *sk, struct request_sock *req, int i, int uid)
2017 int ttd = req->expires - jiffies;
2018 const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
2019 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
2025 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2026 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2028 src->s6_addr32[0], src->s6_addr32[1],
2029 src->s6_addr32[2], src->s6_addr32[3],
2030 ntohs(inet_rsk(req)->loc_port),
2031 dest->s6_addr32[0], dest->s6_addr32[1],
2032 dest->s6_addr32[2], dest->s6_addr32[3],
2033 ntohs(inet_rsk(req)->rmt_port),
2035 0,0, /* could print option size, but that is af dependent. */
2036 1, /* timers active (only the expire timer) */
2037 jiffies_to_clock_t(ttd),
2040 0, /* non standard timer */
2041 0, /* open_requests have no inode */
2045 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
2047 const struct in6_addr *dest, *src;
2050 unsigned long timer_expires;
2051 const struct inet_sock *inet = inet_sk(sp);
2052 const struct tcp_sock *tp = tcp_sk(sp);
2053 const struct inet_connection_sock *icsk = inet_csk(sp);
2054 const struct ipv6_pinfo *np = inet6_sk(sp);
2057 src = &np->rcv_saddr;
2058 destp = ntohs(inet->inet_dport);
2059 srcp = ntohs(inet->inet_sport);
2061 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
2063 timer_expires = icsk->icsk_timeout;
2064 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2066 timer_expires = icsk->icsk_timeout;
2067 } else if (timer_pending(&sp->sk_timer)) {
2069 timer_expires = sp->sk_timer.expires;
2072 timer_expires = jiffies;
2076 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2077 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
2079 src->s6_addr32[0], src->s6_addr32[1],
2080 src->s6_addr32[2], src->s6_addr32[3], srcp,
2081 dest->s6_addr32[0], dest->s6_addr32[1],
2082 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2084 tp->write_seq-tp->snd_una,
2085 (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
2087 jiffies_to_clock_t(timer_expires - jiffies),
2088 icsk->icsk_retransmits,
2090 icsk->icsk_probes_out,
2092 atomic_read(&sp->sk_refcnt), sp,
2093 jiffies_to_clock_t(icsk->icsk_rto),
2094 jiffies_to_clock_t(icsk->icsk_ack.ato),
2095 (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
2097 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
2101 static void get_timewait6_sock(struct seq_file *seq,
2102 struct inet_timewait_sock *tw, int i)
2104 const struct in6_addr *dest, *src;
2106 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
2107 int ttd = tw->tw_ttd - jiffies;
2112 dest = &tw6->tw_v6_daddr;
2113 src = &tw6->tw_v6_rcv_saddr;
2114 destp = ntohs(tw->tw_dport);
2115 srcp = ntohs(tw->tw_sport);
2118 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2119 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2121 src->s6_addr32[0], src->s6_addr32[1],
2122 src->s6_addr32[2], src->s6_addr32[3], srcp,
2123 dest->s6_addr32[0], dest->s6_addr32[1],
2124 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2125 tw->tw_substate, 0, 0,
2126 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
2127 atomic_read(&tw->tw_refcnt), tw);
2130 static int tcp6_seq_show(struct seq_file *seq, void *v)
2132 struct tcp_iter_state *st;
2134 if (v == SEQ_START_TOKEN) {
2139 "st tx_queue rx_queue tr tm->when retrnsmt"
2140 " uid timeout inode\n");
2145 switch (st->state) {
2146 case TCP_SEQ_STATE_LISTENING:
2147 case TCP_SEQ_STATE_ESTABLISHED:
2148 get_tcp6_sock(seq, v, st->num);
2150 case TCP_SEQ_STATE_OPENREQ:
2151 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
2153 case TCP_SEQ_STATE_TIME_WAIT:
2154 get_timewait6_sock(seq, v, st->num);
2161 static const struct file_operations tcp6_afinfo_seq_fops = {
2162 .owner = THIS_MODULE,
2163 .open = tcp_seq_open,
2165 .llseek = seq_lseek,
2166 .release = seq_release_net
2169 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2172 .seq_fops = &tcp6_afinfo_seq_fops,
2174 .show = tcp6_seq_show,
2178 int __net_init tcp6_proc_init(struct net *net)
2180 return tcp_proc_register(net, &tcp6_seq_afinfo);
2183 void tcp6_proc_exit(struct net *net)
2185 tcp_proc_unregister(net, &tcp6_seq_afinfo);
2189 static void tcp_v6_clear_sk(struct sock *sk, int size)
2191 struct inet_sock *inet = inet_sk(sk);
2193 /* we do not want to clear pinet6 field, because of RCU lookups */
2194 sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
2196 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
2197 memset(&inet->pinet6 + 1, 0, size);
2200 struct proto tcpv6_prot = {
2202 .owner = THIS_MODULE,
2204 .connect = tcp_v6_connect,
2205 .disconnect = tcp_disconnect,
2206 .accept = inet_csk_accept,
2208 .init = tcp_v6_init_sock,
2209 .destroy = tcp_v6_destroy_sock,
2210 .shutdown = tcp_shutdown,
2211 .setsockopt = tcp_setsockopt,
2212 .getsockopt = tcp_getsockopt,
2213 .recvmsg = tcp_recvmsg,
2214 .sendmsg = tcp_sendmsg,
2215 .sendpage = tcp_sendpage,
2216 .backlog_rcv = tcp_v6_do_rcv,
2217 .hash = tcp_v6_hash,
2218 .unhash = inet_unhash,
2219 .get_port = inet_csk_get_port,
2220 .enter_memory_pressure = tcp_enter_memory_pressure,
2221 .sockets_allocated = &tcp_sockets_allocated,
2222 .memory_allocated = &tcp_memory_allocated,
2223 .memory_pressure = &tcp_memory_pressure,
2224 .orphan_count = &tcp_orphan_count,
2225 .sysctl_mem = sysctl_tcp_mem,
2226 .sysctl_wmem = sysctl_tcp_wmem,
2227 .sysctl_rmem = sysctl_tcp_rmem,
2228 .max_header = MAX_TCP_HEADER,
2229 .obj_size = sizeof(struct tcp6_sock),
2230 .slab_flags = SLAB_DESTROY_BY_RCU,
2231 .twsk_prot = &tcp6_timewait_sock_ops,
2232 .rsk_prot = &tcp6_request_sock_ops,
2233 .h.hashinfo = &tcp_hashinfo,
2234 .no_autobind = true,
2235 #ifdef CONFIG_COMPAT
2236 .compat_setsockopt = compat_tcp_setsockopt,
2237 .compat_getsockopt = compat_tcp_getsockopt,
2239 .clear_sk = tcp_v6_clear_sk,
2242 static const struct inet6_protocol tcpv6_protocol = {
2243 .handler = tcp_v6_rcv,
2244 .err_handler = tcp_v6_err,
2245 .gso_send_check = tcp_v6_gso_send_check,
2246 .gso_segment = tcp_tso_segment,
2247 .gro_receive = tcp6_gro_receive,
2248 .gro_complete = tcp6_gro_complete,
2249 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2252 static struct inet_protosw tcpv6_protosw = {
2253 .type = SOCK_STREAM,
2254 .protocol = IPPROTO_TCP,
2255 .prot = &tcpv6_prot,
2256 .ops = &inet6_stream_ops,
2258 .flags = INET_PROTOSW_PERMANENT |
2262 static int __net_init tcpv6_net_init(struct net *net)
2264 return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2265 SOCK_RAW, IPPROTO_TCP, net);
2268 static void __net_exit tcpv6_net_exit(struct net *net)
2270 inet_ctl_sock_destroy(net->ipv6.tcp_sk);
2273 static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
2275 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
2278 static struct pernet_operations tcpv6_net_ops = {
2279 .init = tcpv6_net_init,
2280 .exit = tcpv6_net_exit,
2281 .exit_batch = tcpv6_net_exit_batch,
2284 int __init tcpv6_init(void)
2288 ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
2292 /* register inet6 protocol */
2293 ret = inet6_register_protosw(&tcpv6_protosw);
2295 goto out_tcpv6_protocol;
2297 ret = register_pernet_subsys(&tcpv6_net_ops);
2299 goto out_tcpv6_protosw;
2304 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2306 inet6_unregister_protosw(&tcpv6_protosw);
2310 void tcpv6_exit(void)
2312 unregister_pernet_subsys(&tcpv6_net_ops);
2313 inet6_unregister_protosw(&tcpv6_protosw);
2314 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);