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);
517 err = ip6_xmit(sk, skb, &fl6, rcu_dereference(np->opt),
520 err = net_xmit_eval(err);
528 static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
529 struct request_values *rvp)
531 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
532 return tcp_v6_send_synack(sk, req, rvp);
535 static void tcp_v6_reqsk_destructor(struct request_sock *req)
537 kfree_skb(inet6_rsk(req)->pktopts);
540 #ifdef CONFIG_TCP_MD5SIG
541 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
542 const struct in6_addr *addr)
544 struct tcp_sock *tp = tcp_sk(sk);
549 if (!tp->md5sig_info || !tp->md5sig_info->entries6)
552 for (i = 0; i < tp->md5sig_info->entries6; i++) {
553 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, addr))
554 return &tp->md5sig_info->keys6[i].base;
559 static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
560 struct sock *addr_sk)
562 return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr);
565 static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
566 struct request_sock *req)
568 return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr);
571 static int tcp_v6_md5_do_add(struct sock *sk, const struct in6_addr *peer,
572 char *newkey, u8 newkeylen)
574 /* Add key to the list */
575 struct tcp_md5sig_key *key;
576 struct tcp_sock *tp = tcp_sk(sk);
577 struct tcp6_md5sig_key *keys;
579 key = tcp_v6_md5_do_lookup(sk, peer);
581 /* modify existing entry - just update that one */
584 key->keylen = newkeylen;
586 /* reallocate new list if current one is full. */
587 if (!tp->md5sig_info) {
588 tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info), GFP_ATOMIC);
589 if (!tp->md5sig_info) {
593 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
595 if (tp->md5sig_info->entries6 == 0 &&
596 !tcp_alloc_md5sig_pool()) {
600 if (tp->md5sig_info->alloced6 == tp->md5sig_info->entries6) {
601 keys = kmalloc((sizeof (tp->md5sig_info->keys6[0]) *
602 (tp->md5sig_info->entries6 + 1)), GFP_ATOMIC);
609 if (tp->md5sig_info->entries6)
610 memmove(keys, tp->md5sig_info->keys6,
611 (sizeof (tp->md5sig_info->keys6[0]) *
612 tp->md5sig_info->entries6));
614 kfree(tp->md5sig_info->keys6);
615 tp->md5sig_info->keys6 = keys;
616 tp->md5sig_info->alloced6++;
619 ipv6_addr_copy(&tp->md5sig_info->keys6[tp->md5sig_info->entries6].addr,
621 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.key = newkey;
622 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.keylen = newkeylen;
624 tp->md5sig_info->entries6++;
629 static int tcp_v6_md5_add_func(struct sock *sk, struct sock *addr_sk,
630 u8 *newkey, __u8 newkeylen)
632 return tcp_v6_md5_do_add(sk, &inet6_sk(addr_sk)->daddr,
636 static int tcp_v6_md5_do_del(struct sock *sk, const struct in6_addr *peer)
638 struct tcp_sock *tp = tcp_sk(sk);
641 for (i = 0; i < tp->md5sig_info->entries6; i++) {
642 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, peer)) {
644 kfree(tp->md5sig_info->keys6[i].base.key);
645 tp->md5sig_info->entries6--;
647 if (tp->md5sig_info->entries6 == 0) {
648 kfree(tp->md5sig_info->keys6);
649 tp->md5sig_info->keys6 = NULL;
650 tp->md5sig_info->alloced6 = 0;
652 /* shrink the database */
653 if (tp->md5sig_info->entries6 != i)
654 memmove(&tp->md5sig_info->keys6[i],
655 &tp->md5sig_info->keys6[i+1],
656 (tp->md5sig_info->entries6 - i)
657 * sizeof (tp->md5sig_info->keys6[0]));
665 static void tcp_v6_clear_md5_list (struct sock *sk)
667 struct tcp_sock *tp = tcp_sk(sk);
670 if (tp->md5sig_info->entries6) {
671 for (i = 0; i < tp->md5sig_info->entries6; i++)
672 kfree(tp->md5sig_info->keys6[i].base.key);
673 tp->md5sig_info->entries6 = 0;
676 kfree(tp->md5sig_info->keys6);
677 tp->md5sig_info->keys6 = NULL;
678 tp->md5sig_info->alloced6 = 0;
680 if (tp->md5sig_info->entries4) {
681 for (i = 0; i < tp->md5sig_info->entries4; i++)
682 kfree(tp->md5sig_info->keys4[i].base.key);
683 tp->md5sig_info->entries4 = 0;
686 kfree(tp->md5sig_info->keys4);
687 tp->md5sig_info->keys4 = NULL;
688 tp->md5sig_info->alloced4 = 0;
691 static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
694 struct tcp_md5sig cmd;
695 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
698 if (optlen < sizeof(cmd))
701 if (copy_from_user(&cmd, optval, sizeof(cmd)))
704 if (sin6->sin6_family != AF_INET6)
707 if (!cmd.tcpm_keylen) {
708 if (!tcp_sk(sk)->md5sig_info)
710 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
711 return tcp_v4_md5_do_del(sk, sin6->sin6_addr.s6_addr32[3]);
712 return tcp_v6_md5_do_del(sk, &sin6->sin6_addr);
715 if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
718 if (!tcp_sk(sk)->md5sig_info) {
719 struct tcp_sock *tp = tcp_sk(sk);
720 struct tcp_md5sig_info *p;
722 p = kzalloc(sizeof(struct tcp_md5sig_info), GFP_KERNEL);
727 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
730 newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
733 if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
734 return tcp_v4_md5_do_add(sk, sin6->sin6_addr.s6_addr32[3],
735 newkey, cmd.tcpm_keylen);
737 return tcp_v6_md5_do_add(sk, &sin6->sin6_addr, newkey, cmd.tcpm_keylen);
740 static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
741 const struct in6_addr *daddr,
742 const struct in6_addr *saddr, int nbytes)
744 struct tcp6_pseudohdr *bp;
745 struct scatterlist sg;
747 bp = &hp->md5_blk.ip6;
748 /* 1. TCP pseudo-header (RFC2460) */
749 ipv6_addr_copy(&bp->saddr, saddr);
750 ipv6_addr_copy(&bp->daddr, daddr);
751 bp->protocol = cpu_to_be32(IPPROTO_TCP);
752 bp->len = cpu_to_be32(nbytes);
754 sg_init_one(&sg, bp, sizeof(*bp));
755 return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
758 static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
759 const struct in6_addr *daddr, struct in6_addr *saddr,
760 const struct tcphdr *th)
762 struct tcp_md5sig_pool *hp;
763 struct hash_desc *desc;
765 hp = tcp_get_md5sig_pool();
767 goto clear_hash_noput;
768 desc = &hp->md5_desc;
770 if (crypto_hash_init(desc))
772 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
774 if (tcp_md5_hash_header(hp, th))
776 if (tcp_md5_hash_key(hp, key))
778 if (crypto_hash_final(desc, md5_hash))
781 tcp_put_md5sig_pool();
785 tcp_put_md5sig_pool();
787 memset(md5_hash, 0, 16);
791 static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
792 const struct sock *sk,
793 const struct request_sock *req,
794 const struct sk_buff *skb)
796 const struct in6_addr *saddr, *daddr;
797 struct tcp_md5sig_pool *hp;
798 struct hash_desc *desc;
799 const struct tcphdr *th = tcp_hdr(skb);
802 saddr = &inet6_sk(sk)->saddr;
803 daddr = &inet6_sk(sk)->daddr;
805 saddr = &inet6_rsk(req)->loc_addr;
806 daddr = &inet6_rsk(req)->rmt_addr;
808 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
809 saddr = &ip6h->saddr;
810 daddr = &ip6h->daddr;
813 hp = tcp_get_md5sig_pool();
815 goto clear_hash_noput;
816 desc = &hp->md5_desc;
818 if (crypto_hash_init(desc))
821 if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
823 if (tcp_md5_hash_header(hp, th))
825 if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
827 if (tcp_md5_hash_key(hp, key))
829 if (crypto_hash_final(desc, md5_hash))
832 tcp_put_md5sig_pool();
836 tcp_put_md5sig_pool();
838 memset(md5_hash, 0, 16);
842 static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
844 const __u8 *hash_location = NULL;
845 struct tcp_md5sig_key *hash_expected;
846 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
847 const struct tcphdr *th = tcp_hdr(skb);
851 hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
852 hash_location = tcp_parse_md5sig_option(th);
854 /* We've parsed the options - do we have a hash? */
855 if (!hash_expected && !hash_location)
858 if (hash_expected && !hash_location) {
859 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
863 if (!hash_expected && hash_location) {
864 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
868 /* check the signature */
869 genhash = tcp_v6_md5_hash_skb(newhash,
873 if (genhash || memcmp(hash_location, newhash, 16) != 0) {
874 if (net_ratelimit()) {
875 printk(KERN_INFO "MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
876 genhash ? "failed" : "mismatch",
877 &ip6h->saddr, ntohs(th->source),
878 &ip6h->daddr, ntohs(th->dest));
886 struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
888 .obj_size = sizeof(struct tcp6_request_sock),
889 .rtx_syn_ack = tcp_v6_rtx_synack,
890 .send_ack = tcp_v6_reqsk_send_ack,
891 .destructor = tcp_v6_reqsk_destructor,
892 .send_reset = tcp_v6_send_reset,
893 .syn_ack_timeout = tcp_syn_ack_timeout,
896 #ifdef CONFIG_TCP_MD5SIG
897 static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
898 .md5_lookup = tcp_v6_reqsk_md5_lookup,
899 .calc_md5_hash = tcp_v6_md5_hash_skb,
903 static void __tcp_v6_send_check(struct sk_buff *skb,
904 const struct in6_addr *saddr, const struct in6_addr *daddr)
906 struct tcphdr *th = tcp_hdr(skb);
908 if (skb->ip_summed == CHECKSUM_PARTIAL) {
909 th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
910 skb->csum_start = skb_transport_header(skb) - skb->head;
911 skb->csum_offset = offsetof(struct tcphdr, check);
913 th->check = tcp_v6_check(skb->len, saddr, daddr,
914 csum_partial(th, th->doff << 2,
919 static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
921 struct ipv6_pinfo *np = inet6_sk(sk);
923 __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
926 static int tcp_v6_gso_send_check(struct sk_buff *skb)
928 const struct ipv6hdr *ipv6h;
931 if (!pskb_may_pull(skb, sizeof(*th)))
934 ipv6h = ipv6_hdr(skb);
938 skb->ip_summed = CHECKSUM_PARTIAL;
939 __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
943 static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
946 const struct ipv6hdr *iph = skb_gro_network_header(skb);
948 switch (skb->ip_summed) {
949 case CHECKSUM_COMPLETE:
950 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
952 skb->ip_summed = CHECKSUM_UNNECESSARY;
958 NAPI_GRO_CB(skb)->flush = 1;
962 return tcp_gro_receive(head, skb);
965 static int tcp6_gro_complete(struct sk_buff *skb)
967 const struct ipv6hdr *iph = ipv6_hdr(skb);
968 struct tcphdr *th = tcp_hdr(skb);
970 th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
971 &iph->saddr, &iph->daddr, 0);
972 skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
974 return tcp_gro_complete(skb);
977 static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
978 u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
980 const struct tcphdr *th = tcp_hdr(skb);
982 struct sk_buff *buff;
984 struct net *net = dev_net(skb_dst(skb)->dev);
985 struct sock *ctl_sk = net->ipv6.tcp_sk;
986 unsigned int tot_len = sizeof(struct tcphdr);
987 struct dst_entry *dst;
991 tot_len += TCPOLEN_TSTAMP_ALIGNED;
992 #ifdef CONFIG_TCP_MD5SIG
994 tot_len += TCPOLEN_MD5SIG_ALIGNED;
997 buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
1002 skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
1004 t1 = (struct tcphdr *) skb_push(buff, tot_len);
1005 skb_reset_transport_header(buff);
1007 /* Swap the send and the receive. */
1008 memset(t1, 0, sizeof(*t1));
1009 t1->dest = th->source;
1010 t1->source = th->dest;
1011 t1->doff = tot_len / 4;
1012 t1->seq = htonl(seq);
1013 t1->ack_seq = htonl(ack);
1014 t1->ack = !rst || !th->ack;
1016 t1->window = htons(win);
1018 topt = (__be32 *)(t1 + 1);
1021 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1022 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1023 *topt++ = htonl(tcp_time_stamp);
1024 *topt++ = htonl(ts);
1027 #ifdef CONFIG_TCP_MD5SIG
1029 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1030 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
1031 tcp_v6_md5_hash_hdr((__u8 *)topt, key,
1032 &ipv6_hdr(skb)->saddr,
1033 &ipv6_hdr(skb)->daddr, t1);
1037 memset(&fl6, 0, sizeof(fl6));
1038 ipv6_addr_copy(&fl6.daddr, &ipv6_hdr(skb)->saddr);
1039 ipv6_addr_copy(&fl6.saddr, &ipv6_hdr(skb)->daddr);
1041 buff->ip_summed = CHECKSUM_PARTIAL;
1044 __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
1046 fl6.flowi6_proto = IPPROTO_TCP;
1047 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
1048 fl6.flowi6_oif = inet6_iif(skb);
1049 fl6.fl6_dport = t1->dest;
1050 fl6.fl6_sport = t1->source;
1051 security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
1053 /* Pass a socket to ip6_dst_lookup either it is for RST
1054 * Underlying function will use this to retrieve the network
1057 dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
1059 skb_dst_set(buff, dst);
1060 ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
1061 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
1063 TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
1070 static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
1072 const struct tcphdr *th = tcp_hdr(skb);
1073 u32 seq = 0, ack_seq = 0;
1074 struct tcp_md5sig_key *key = NULL;
1079 if (!ipv6_unicast_destination(skb))
1082 #ifdef CONFIG_TCP_MD5SIG
1084 key = tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr);
1088 seq = ntohl(th->ack_seq);
1090 ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
1093 tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
1096 static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
1097 struct tcp_md5sig_key *key, u8 tclass)
1099 tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
1102 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
1104 struct inet_timewait_sock *tw = inet_twsk(sk);
1105 struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
1107 tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
1108 tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
1109 tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
1115 static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
1116 struct request_sock *req)
1118 tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
1119 tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
1123 static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
1125 struct request_sock *req, **prev;
1126 const struct tcphdr *th = tcp_hdr(skb);
1129 /* Find possible connection requests. */
1130 req = inet6_csk_search_req(sk, &prev, th->source,
1131 &ipv6_hdr(skb)->saddr,
1132 &ipv6_hdr(skb)->daddr, inet6_iif(skb));
1134 return tcp_check_req(sk, skb, req, prev);
1136 nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
1137 &ipv6_hdr(skb)->saddr, th->source,
1138 &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
1141 if (nsk->sk_state != TCP_TIME_WAIT) {
1145 inet_twsk_put(inet_twsk(nsk));
1149 #ifdef CONFIG_SYN_COOKIES
1151 sk = cookie_v6_check(sk, skb);
1156 /* FIXME: this is substantially similar to the ipv4 code.
1157 * Can some kind of merge be done? -- erics
1159 static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1161 struct tcp_extend_values tmp_ext;
1162 struct tcp_options_received tmp_opt;
1163 const u8 *hash_location;
1164 struct request_sock *req;
1165 struct inet6_request_sock *treq;
1166 struct ipv6_pinfo *np = inet6_sk(sk);
1167 struct tcp_sock *tp = tcp_sk(sk);
1168 __u32 isn = TCP_SKB_CB(skb)->when;
1169 struct dst_entry *dst = NULL;
1170 int want_cookie = 0;
1172 if (skb->protocol == htons(ETH_P_IP))
1173 return tcp_v4_conn_request(sk, skb);
1175 if (!ipv6_unicast_destination(skb))
1178 if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
1179 want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6");
1184 if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
1187 req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
1191 #ifdef CONFIG_TCP_MD5SIG
1192 tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
1195 tcp_clear_options(&tmp_opt);
1196 tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
1197 tmp_opt.user_mss = tp->rx_opt.user_mss;
1198 tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
1200 if (tmp_opt.cookie_plus > 0 &&
1201 tmp_opt.saw_tstamp &&
1202 !tp->rx_opt.cookie_out_never &&
1203 (sysctl_tcp_cookie_size > 0 ||
1204 (tp->cookie_values != NULL &&
1205 tp->cookie_values->cookie_desired > 0))) {
1208 u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
1209 int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
1211 if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
1214 /* Secret recipe starts with IP addresses */
1215 d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
1220 d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
1226 /* plus variable length Initiator Cookie */
1229 *c++ ^= *hash_location++;
1231 want_cookie = 0; /* not our kind of cookie */
1232 tmp_ext.cookie_out_never = 0; /* false */
1233 tmp_ext.cookie_plus = tmp_opt.cookie_plus;
1234 } else if (!tp->rx_opt.cookie_in_always) {
1235 /* redundant indications, but ensure initialization. */
1236 tmp_ext.cookie_out_never = 1; /* true */
1237 tmp_ext.cookie_plus = 0;
1241 tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
1243 if (want_cookie && !tmp_opt.saw_tstamp)
1244 tcp_clear_options(&tmp_opt);
1246 tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1247 tcp_openreq_init(req, &tmp_opt, skb);
1249 treq = inet6_rsk(req);
1250 ipv6_addr_copy(&treq->rmt_addr, &ipv6_hdr(skb)->saddr);
1251 ipv6_addr_copy(&treq->loc_addr, &ipv6_hdr(skb)->daddr);
1252 if (!want_cookie || tmp_opt.tstamp_ok)
1253 TCP_ECN_create_request(req, skb);
1255 treq->iif = sk->sk_bound_dev_if;
1257 /* So that link locals have meaning */
1258 if (!sk->sk_bound_dev_if &&
1259 ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL)
1260 treq->iif = inet6_iif(skb);
1263 struct inet_peer *peer = NULL;
1265 if (ipv6_opt_accepted(sk, skb) ||
1266 np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
1267 np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
1268 atomic_inc(&skb->users);
1269 treq->pktopts = skb;
1273 isn = cookie_v6_init_sequence(sk, skb, &req->mss);
1274 req->cookie_ts = tmp_opt.tstamp_ok;
1278 /* VJ's idea. We save last timestamp seen
1279 * from the destination in peer table, when entering
1280 * state TIME-WAIT, and check against it before
1281 * accepting new connection request.
1283 * If "isn" is not zero, this request hit alive
1284 * timewait bucket, so that all the necessary checks
1285 * are made in the function processing timewait state.
1287 if (tmp_opt.saw_tstamp &&
1288 tcp_death_row.sysctl_tw_recycle &&
1289 (dst = inet6_csk_route_req(sk, req)) != NULL &&
1290 (peer = rt6_get_peer((struct rt6_info *)dst)) != NULL &&
1291 ipv6_addr_equal((struct in6_addr *)peer->daddr.addr.a6,
1293 inet_peer_refcheck(peer);
1294 if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
1295 (s32)(peer->tcp_ts - req->ts_recent) >
1297 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
1298 goto drop_and_release;
1301 /* Kill the following clause, if you dislike this way. */
1302 else if (!sysctl_tcp_syncookies &&
1303 (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
1304 (sysctl_max_syn_backlog >> 2)) &&
1305 (!peer || !peer->tcp_ts_stamp) &&
1306 (!dst || !dst_metric(dst, RTAX_RTT))) {
1307 /* Without syncookies last quarter of
1308 * backlog is filled with destinations,
1309 * proven to be alive.
1310 * It means that we continue to communicate
1311 * to destinations, already remembered
1312 * to the moment of synflood.
1314 LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
1315 &treq->rmt_addr, ntohs(tcp_hdr(skb)->source));
1316 goto drop_and_release;
1319 isn = tcp_v6_init_sequence(skb);
1322 tcp_rsk(req)->snt_isn = isn;
1323 tcp_rsk(req)->snt_synack = tcp_time_stamp;
1325 security_inet_conn_request(sk, skb, req);
1327 if (tcp_v6_send_synack(sk, req,
1328 (struct request_values *)&tmp_ext) ||
1332 inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
1340 return 0; /* don't send reset */
1343 static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1344 struct request_sock *req,
1345 struct dst_entry *dst)
1347 struct inet6_request_sock *treq;
1348 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1349 struct tcp6_sock *newtcp6sk;
1350 struct inet_sock *newinet;
1351 struct tcp_sock *newtp;
1353 struct ipv6_txoptions *opt;
1354 #ifdef CONFIG_TCP_MD5SIG
1355 struct tcp_md5sig_key *key;
1358 if (skb->protocol == htons(ETH_P_IP)) {
1363 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1368 newtcp6sk = (struct tcp6_sock *)newsk;
1369 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1371 newinet = inet_sk(newsk);
1372 newnp = inet6_sk(newsk);
1373 newtp = tcp_sk(newsk);
1375 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1377 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
1379 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1381 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
1383 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1384 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
1385 #ifdef CONFIG_TCP_MD5SIG
1386 newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
1389 newnp->ipv6_mc_list = NULL;
1390 newnp->ipv6_ac_list = NULL;
1391 newnp->ipv6_fl_list = NULL;
1392 newnp->pktoptions = NULL;
1394 newnp->mcast_oif = inet6_iif(skb);
1395 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1398 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
1399 * here, tcp_create_openreq_child now does this for us, see the comment in
1400 * that function for the gory details. -acme
1403 /* It is tricky place. Until this moment IPv4 tcp
1404 worked with IPv6 icsk.icsk_af_ops.
1407 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
1412 treq = inet6_rsk(req);
1414 if (sk_acceptq_is_full(sk))
1418 dst = inet6_csk_route_req(sk, req);
1423 newsk = tcp_create_openreq_child(sk, req, skb);
1428 * No need to charge this sock to the relevant IPv6 refcnt debug socks
1429 * count here, tcp_create_openreq_child now does this for us, see the
1430 * comment in that function for the gory details. -acme
1433 newsk->sk_gso_type = SKB_GSO_TCPV6;
1434 __ip6_dst_store(newsk, dst, NULL, NULL);
1436 newtcp6sk = (struct tcp6_sock *)newsk;
1437 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1439 newtp = tcp_sk(newsk);
1440 newinet = inet_sk(newsk);
1441 newnp = inet6_sk(newsk);
1443 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1445 ipv6_addr_copy(&newnp->daddr, &treq->rmt_addr);
1446 ipv6_addr_copy(&newnp->saddr, &treq->loc_addr);
1447 ipv6_addr_copy(&newnp->rcv_saddr, &treq->loc_addr);
1448 newsk->sk_bound_dev_if = treq->iif;
1450 /* Now IPv6 options...
1452 First: no IPv4 options.
1454 newinet->inet_opt = NULL;
1455 newnp->ipv6_mc_list = NULL;
1456 newnp->ipv6_ac_list = NULL;
1457 newnp->ipv6_fl_list = NULL;
1460 newnp->rxopt.all = np->rxopt.all;
1462 /* Clone pktoptions received with SYN */
1463 newnp->pktoptions = NULL;
1464 if (treq->pktopts != NULL) {
1465 newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
1466 kfree_skb(treq->pktopts);
1467 treq->pktopts = NULL;
1468 if (newnp->pktoptions)
1469 skb_set_owner_r(newnp->pktoptions, newsk);
1472 newnp->mcast_oif = inet6_iif(skb);
1473 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1475 /* Clone native IPv6 options from listening socket (if any)
1477 Yes, keeping reference count would be much more clever,
1478 but we make one more one thing there: reattach optmem
1481 opt = rcu_dereference(np->opt);
1483 opt = ipv6_dup_options(newsk, opt);
1484 RCU_INIT_POINTER(newnp->opt, opt);
1486 inet_csk(newsk)->icsk_ext_hdr_len = 0;
1488 inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
1491 tcp_mtup_init(newsk);
1492 tcp_sync_mss(newsk, dst_mtu(dst));
1493 newtp->advmss = dst_metric_advmss(dst);
1494 if (tcp_sk(sk)->rx_opt.user_mss &&
1495 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1496 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1498 tcp_initialize_rcv_mss(newsk);
1499 if (tcp_rsk(req)->snt_synack)
1500 tcp_valid_rtt_meas(newsk,
1501 tcp_time_stamp - tcp_rsk(req)->snt_synack);
1502 newtp->total_retrans = req->retrans;
1504 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1505 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
1507 #ifdef CONFIG_TCP_MD5SIG
1508 /* Copy over the MD5 key from the original socket */
1509 if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
1510 /* We're using one, so create a matching key
1511 * on the newsk structure. If we fail to get
1512 * memory, then we end up not copying the key
1515 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1517 tcp_v6_md5_do_add(newsk, &newnp->daddr,
1518 newkey, key->keylen);
1522 if (__inet_inherit_port(sk, newsk) < 0) {
1523 inet_csk_prepare_forced_close(newsk);
1527 __inet6_hash(newsk, NULL);
1532 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1536 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
1540 static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
1542 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1543 if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
1544 &ipv6_hdr(skb)->daddr, skb->csum)) {
1545 skb->ip_summed = CHECKSUM_UNNECESSARY;
1550 skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
1551 &ipv6_hdr(skb)->saddr,
1552 &ipv6_hdr(skb)->daddr, 0));
1554 if (skb->len <= 76) {
1555 return __skb_checksum_complete(skb);
1560 /* The socket must have it's spinlock held when we get
1563 * We have a potential double-lock case here, so even when
1564 * doing backlog processing we use the BH locking scheme.
1565 * This is because we cannot sleep with the original spinlock
1568 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1570 struct ipv6_pinfo *np = inet6_sk(sk);
1571 struct tcp_sock *tp;
1572 struct sk_buff *opt_skb = NULL;
1574 /* Imagine: socket is IPv6. IPv4 packet arrives,
1575 goes to IPv4 receive handler and backlogged.
1576 From backlog it always goes here. Kerboom...
1577 Fortunately, tcp_rcv_established and rcv_established
1578 handle them correctly, but it is not case with
1579 tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
1582 if (skb->protocol == htons(ETH_P_IP))
1583 return tcp_v4_do_rcv(sk, skb);
1585 #ifdef CONFIG_TCP_MD5SIG
1586 if (tcp_v6_inbound_md5_hash (sk, skb))
1590 if (tcp_filter(sk, skb))
1594 * socket locking is here for SMP purposes as backlog rcv
1595 * is currently called with bh processing disabled.
1598 /* Do Stevens' IPV6_PKTOPTIONS.
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.
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.
1612 opt_skb = skb_clone(skb, GFP_ATOMIC);
1614 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1615 sock_rps_save_rxhash(sk, skb);
1616 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
1619 goto ipv6_pktoptions;
1623 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
1626 if (sk->sk_state == TCP_LISTEN) {
1627 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1632 * Queue it on the new socket if the new socket is active,
1633 * otherwise we just shortcircuit this and continue with
1637 sock_rps_save_rxhash(nsk, skb);
1638 if (tcp_child_process(sk, nsk, skb))
1641 __kfree_skb(opt_skb);
1645 sock_rps_save_rxhash(sk, skb);
1647 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
1650 goto ipv6_pktoptions;
1654 tcp_v6_send_reset(sk, skb);
1657 __kfree_skb(opt_skb);
1661 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
1666 /* Do you ask, what is it?
1668 1. skb was enqueued by tcp.
1669 2. skb is added to tail of read queue, rather than out of order.
1670 3. socket is not in passive state.
1671 4. Finally, it really contains options, which user wants to receive.
1674 if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1675 !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1676 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
1677 np->mcast_oif = inet6_iif(opt_skb);
1678 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
1679 np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
1680 if (ipv6_opt_accepted(sk, opt_skb)) {
1681 skb_set_owner_r(opt_skb, sk);
1682 opt_skb = xchg(&np->pktoptions, opt_skb);
1684 __kfree_skb(opt_skb);
1685 opt_skb = xchg(&np->pktoptions, NULL);
1693 static int tcp_v6_rcv(struct sk_buff *skb)
1695 const struct tcphdr *th;
1696 const struct ipv6hdr *hdr;
1699 struct net *net = dev_net(skb->dev);
1701 if (skb->pkt_type != PACKET_HOST)
1705 * Count it even if it's bad.
1707 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
1709 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1714 if (th->doff < sizeof(struct tcphdr)/4)
1716 if (!pskb_may_pull(skb, th->doff*4))
1719 if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
1723 hdr = ipv6_hdr(skb);
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)->when = 0;
1729 TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
1730 TCP_SKB_CB(skb)->sacked = 0;
1732 sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
1737 if (sk->sk_state == TCP_TIME_WAIT)
1740 if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
1741 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
1742 goto discard_and_relse;
1745 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1746 goto discard_and_relse;
1748 if (tcp_filter(sk, skb))
1749 goto discard_and_relse;
1750 th = (const struct tcphdr *)skb->data;
1751 hdr = ipv6_hdr(skb);
1755 bh_lock_sock_nested(sk);
1757 if (!sock_owned_by_user(sk)) {
1758 #ifdef CONFIG_NET_DMA
1759 struct tcp_sock *tp = tcp_sk(sk);
1760 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1761 tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
1762 if (tp->ucopy.dma_chan)
1763 ret = tcp_v6_do_rcv(sk, skb);
1767 if (!tcp_prequeue(sk, skb))
1768 ret = tcp_v6_do_rcv(sk, skb);
1770 } else if (unlikely(sk_add_backlog(sk, skb))) {
1772 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
1773 goto discard_and_relse;
1778 return ret ? -1 : 0;
1781 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1784 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1786 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1788 tcp_v6_send_reset(NULL, skb);
1805 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1806 inet_twsk_put(inet_twsk(sk));
1810 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1811 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1812 inet_twsk_put(inet_twsk(sk));
1816 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
1821 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
1822 &ipv6_hdr(skb)->daddr,
1823 ntohs(th->dest), inet6_iif(skb));
1825 struct inet_timewait_sock *tw = inet_twsk(sk);
1826 inet_twsk_deschedule(tw, &tcp_death_row);
1831 /* Fall through to ACK */
1834 tcp_v6_timewait_ack(sk, skb);
1838 case TCP_TW_SUCCESS:;
1843 static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
1845 struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
1846 struct ipv6_pinfo *np = inet6_sk(sk);
1847 struct inet_peer *peer;
1850 !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
1851 peer = inet_getpeer_v6(&np->daddr, 1);
1855 rt6_bind_peer(rt, 1);
1856 peer = rt->rt6i_peer;
1857 *release_it = false;
1863 static void *tcp_v6_tw_get_peer(struct sock *sk)
1865 const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
1866 const struct inet_timewait_sock *tw = inet_twsk(sk);
1868 if (tw->tw_family == AF_INET)
1869 return tcp_v4_tw_get_peer(sk);
1871 return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
1874 static struct timewait_sock_ops tcp6_timewait_sock_ops = {
1875 .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
1876 .twsk_unique = tcp_twsk_unique,
1877 .twsk_destructor= tcp_twsk_destructor,
1878 .twsk_getpeer = tcp_v6_tw_get_peer,
1881 static const struct inet_connection_sock_af_ops ipv6_specific = {
1882 .queue_xmit = inet6_csk_xmit,
1883 .send_check = tcp_v6_send_check,
1884 .rebuild_header = inet6_sk_rebuild_header,
1885 .conn_request = tcp_v6_conn_request,
1886 .syn_recv_sock = tcp_v6_syn_recv_sock,
1887 .get_peer = tcp_v6_get_peer,
1888 .net_header_len = sizeof(struct ipv6hdr),
1889 .setsockopt = ipv6_setsockopt,
1890 .getsockopt = ipv6_getsockopt,
1891 .addr2sockaddr = inet6_csk_addr2sockaddr,
1892 .sockaddr_len = sizeof(struct sockaddr_in6),
1893 .bind_conflict = inet6_csk_bind_conflict,
1894 #ifdef CONFIG_COMPAT
1895 .compat_setsockopt = compat_ipv6_setsockopt,
1896 .compat_getsockopt = compat_ipv6_getsockopt,
1900 #ifdef CONFIG_TCP_MD5SIG
1901 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
1902 .md5_lookup = tcp_v6_md5_lookup,
1903 .calc_md5_hash = tcp_v6_md5_hash_skb,
1904 .md5_add = tcp_v6_md5_add_func,
1905 .md5_parse = tcp_v6_parse_md5_keys,
1910 * TCP over IPv4 via INET6 API
1913 static const struct inet_connection_sock_af_ops ipv6_mapped = {
1914 .queue_xmit = ip_queue_xmit,
1915 .send_check = tcp_v4_send_check,
1916 .rebuild_header = inet_sk_rebuild_header,
1917 .conn_request = tcp_v6_conn_request,
1918 .syn_recv_sock = tcp_v6_syn_recv_sock,
1919 .get_peer = tcp_v4_get_peer,
1920 .net_header_len = sizeof(struct iphdr),
1921 .setsockopt = ipv6_setsockopt,
1922 .getsockopt = ipv6_getsockopt,
1923 .addr2sockaddr = inet6_csk_addr2sockaddr,
1924 .sockaddr_len = sizeof(struct sockaddr_in6),
1925 .bind_conflict = inet6_csk_bind_conflict,
1926 #ifdef CONFIG_COMPAT
1927 .compat_setsockopt = compat_ipv6_setsockopt,
1928 .compat_getsockopt = compat_ipv6_getsockopt,
1932 #ifdef CONFIG_TCP_MD5SIG
1933 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
1934 .md5_lookup = tcp_v4_md5_lookup,
1935 .calc_md5_hash = tcp_v4_md5_hash_skb,
1936 .md5_add = tcp_v6_md5_add_func,
1937 .md5_parse = tcp_v6_parse_md5_keys,
1941 /* NOTE: A lot of things set to zero explicitly by call to
1942 * sk_alloc() so need not be done here.
1944 static int tcp_v6_init_sock(struct sock *sk)
1946 struct inet_connection_sock *icsk = inet_csk(sk);
1947 struct tcp_sock *tp = tcp_sk(sk);
1949 skb_queue_head_init(&tp->out_of_order_queue);
1950 tcp_init_xmit_timers(sk);
1951 tcp_prequeue_init(tp);
1953 icsk->icsk_rto = TCP_TIMEOUT_INIT;
1954 tp->mdev = TCP_TIMEOUT_INIT;
1956 /* So many TCP implementations out there (incorrectly) count the
1957 * initial SYN frame in their delayed-ACK and congestion control
1958 * algorithms that we must have the following bandaid to talk
1959 * efficiently to them. -DaveM
1963 /* See draft-stevens-tcpca-spec-01 for discussion of the
1964 * initialization of these values.
1966 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1967 tp->snd_cwnd_clamp = ~0;
1968 tp->mss_cache = TCP_MSS_DEFAULT;
1970 tp->reordering = sysctl_tcp_reordering;
1972 sk->sk_state = TCP_CLOSE;
1974 icsk->icsk_af_ops = &ipv6_specific;
1975 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1976 icsk->icsk_sync_mss = tcp_sync_mss;
1977 sk->sk_write_space = sk_stream_write_space;
1978 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1980 #ifdef CONFIG_TCP_MD5SIG
1981 tp->af_specific = &tcp_sock_ipv6_specific;
1984 /* TCP Cookie Transactions */
1985 if (sysctl_tcp_cookie_size > 0) {
1986 /* Default, cookies without s_data_payload. */
1988 kzalloc(sizeof(*tp->cookie_values),
1990 if (tp->cookie_values != NULL)
1991 kref_init(&tp->cookie_values->kref);
1993 /* Presumed zeroed, in order of appearance:
1994 * cookie_in_always, cookie_out_never,
1995 * s_data_constant, s_data_in, s_data_out
1997 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1998 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
2001 percpu_counter_inc(&tcp_sockets_allocated);
2007 static void tcp_v6_destroy_sock(struct sock *sk)
2009 #ifdef CONFIG_TCP_MD5SIG
2010 /* Clean up the MD5 key list */
2011 if (tcp_sk(sk)->md5sig_info)
2012 tcp_v6_clear_md5_list(sk);
2014 tcp_v4_destroy_sock(sk);
2015 inet6_destroy_sock(sk);
2018 #ifdef CONFIG_PROC_FS
2019 /* Proc filesystem TCPv6 sock list dumping. */
2020 static void get_openreq6(struct seq_file *seq,
2021 const struct sock *sk, struct request_sock *req, int i, int uid)
2023 int ttd = req->expires - jiffies;
2024 const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
2025 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
2031 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2032 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2034 src->s6_addr32[0], src->s6_addr32[1],
2035 src->s6_addr32[2], src->s6_addr32[3],
2036 ntohs(inet_rsk(req)->loc_port),
2037 dest->s6_addr32[0], dest->s6_addr32[1],
2038 dest->s6_addr32[2], dest->s6_addr32[3],
2039 ntohs(inet_rsk(req)->rmt_port),
2041 0,0, /* could print option size, but that is af dependent. */
2042 1, /* timers active (only the expire timer) */
2043 jiffies_to_clock_t(ttd),
2046 0, /* non standard timer */
2047 0, /* open_requests have no inode */
2051 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
2053 const struct in6_addr *dest, *src;
2056 unsigned long timer_expires;
2057 const struct inet_sock *inet = inet_sk(sp);
2058 const struct tcp_sock *tp = tcp_sk(sp);
2059 const struct inet_connection_sock *icsk = inet_csk(sp);
2060 const struct ipv6_pinfo *np = inet6_sk(sp);
2063 src = &np->rcv_saddr;
2064 destp = ntohs(inet->inet_dport);
2065 srcp = ntohs(inet->inet_sport);
2067 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
2069 timer_expires = icsk->icsk_timeout;
2070 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2072 timer_expires = icsk->icsk_timeout;
2073 } else if (timer_pending(&sp->sk_timer)) {
2075 timer_expires = sp->sk_timer.expires;
2078 timer_expires = jiffies;
2082 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2083 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
2085 src->s6_addr32[0], src->s6_addr32[1],
2086 src->s6_addr32[2], src->s6_addr32[3], srcp,
2087 dest->s6_addr32[0], dest->s6_addr32[1],
2088 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2090 tp->write_seq-tp->snd_una,
2091 (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
2093 jiffies_to_clock_t(timer_expires - jiffies),
2094 icsk->icsk_retransmits,
2096 icsk->icsk_probes_out,
2098 atomic_read(&sp->sk_refcnt), sp,
2099 jiffies_to_clock_t(icsk->icsk_rto),
2100 jiffies_to_clock_t(icsk->icsk_ack.ato),
2101 (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
2103 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
2107 static void get_timewait6_sock(struct seq_file *seq,
2108 struct inet_timewait_sock *tw, int i)
2110 const struct in6_addr *dest, *src;
2112 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
2113 int ttd = tw->tw_ttd - jiffies;
2118 dest = &tw6->tw_v6_daddr;
2119 src = &tw6->tw_v6_rcv_saddr;
2120 destp = ntohs(tw->tw_dport);
2121 srcp = ntohs(tw->tw_sport);
2124 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2125 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2127 src->s6_addr32[0], src->s6_addr32[1],
2128 src->s6_addr32[2], src->s6_addr32[3], srcp,
2129 dest->s6_addr32[0], dest->s6_addr32[1],
2130 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2131 tw->tw_substate, 0, 0,
2132 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
2133 atomic_read(&tw->tw_refcnt), tw);
2136 static int tcp6_seq_show(struct seq_file *seq, void *v)
2138 struct tcp_iter_state *st;
2140 if (v == SEQ_START_TOKEN) {
2145 "st tx_queue rx_queue tr tm->when retrnsmt"
2146 " uid timeout inode\n");
2151 switch (st->state) {
2152 case TCP_SEQ_STATE_LISTENING:
2153 case TCP_SEQ_STATE_ESTABLISHED:
2154 get_tcp6_sock(seq, v, st->num);
2156 case TCP_SEQ_STATE_OPENREQ:
2157 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
2159 case TCP_SEQ_STATE_TIME_WAIT:
2160 get_timewait6_sock(seq, v, st->num);
2167 static const struct file_operations tcp6_afinfo_seq_fops = {
2168 .owner = THIS_MODULE,
2169 .open = tcp_seq_open,
2171 .llseek = seq_lseek,
2172 .release = seq_release_net
2175 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2178 .seq_fops = &tcp6_afinfo_seq_fops,
2180 .show = tcp6_seq_show,
2184 int __net_init tcp6_proc_init(struct net *net)
2186 return tcp_proc_register(net, &tcp6_seq_afinfo);
2189 void tcp6_proc_exit(struct net *net)
2191 tcp_proc_unregister(net, &tcp6_seq_afinfo);
2195 static void tcp_v6_clear_sk(struct sock *sk, int size)
2197 struct inet_sock *inet = inet_sk(sk);
2199 /* we do not want to clear pinet6 field, because of RCU lookups */
2200 sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
2202 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
2203 memset(&inet->pinet6 + 1, 0, size);
2206 struct proto tcpv6_prot = {
2208 .owner = THIS_MODULE,
2210 .connect = tcp_v6_connect,
2211 .disconnect = tcp_disconnect,
2212 .accept = inet_csk_accept,
2214 .init = tcp_v6_init_sock,
2215 .destroy = tcp_v6_destroy_sock,
2216 .shutdown = tcp_shutdown,
2217 .setsockopt = tcp_setsockopt,
2218 .getsockopt = tcp_getsockopt,
2219 .recvmsg = tcp_recvmsg,
2220 .sendmsg = tcp_sendmsg,
2221 .sendpage = tcp_sendpage,
2222 .backlog_rcv = tcp_v6_do_rcv,
2223 .hash = tcp_v6_hash,
2224 .unhash = inet_unhash,
2225 .get_port = inet_csk_get_port,
2226 .enter_memory_pressure = tcp_enter_memory_pressure,
2227 .sockets_allocated = &tcp_sockets_allocated,
2228 .memory_allocated = &tcp_memory_allocated,
2229 .memory_pressure = &tcp_memory_pressure,
2230 .orphan_count = &tcp_orphan_count,
2231 .sysctl_mem = sysctl_tcp_mem,
2232 .sysctl_wmem = sysctl_tcp_wmem,
2233 .sysctl_rmem = sysctl_tcp_rmem,
2234 .max_header = MAX_TCP_HEADER,
2235 .obj_size = sizeof(struct tcp6_sock),
2236 .slab_flags = SLAB_DESTROY_BY_RCU,
2237 .twsk_prot = &tcp6_timewait_sock_ops,
2238 .rsk_prot = &tcp6_request_sock_ops,
2239 .h.hashinfo = &tcp_hashinfo,
2240 .no_autobind = true,
2241 #ifdef CONFIG_COMPAT
2242 .compat_setsockopt = compat_tcp_setsockopt,
2243 .compat_getsockopt = compat_tcp_getsockopt,
2245 .clear_sk = tcp_v6_clear_sk,
2248 static const struct inet6_protocol tcpv6_protocol = {
2249 .handler = tcp_v6_rcv,
2250 .err_handler = tcp_v6_err,
2251 .gso_send_check = tcp_v6_gso_send_check,
2252 .gso_segment = tcp_tso_segment,
2253 .gro_receive = tcp6_gro_receive,
2254 .gro_complete = tcp6_gro_complete,
2255 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2258 static struct inet_protosw tcpv6_protosw = {
2259 .type = SOCK_STREAM,
2260 .protocol = IPPROTO_TCP,
2261 .prot = &tcpv6_prot,
2262 .ops = &inet6_stream_ops,
2264 .flags = INET_PROTOSW_PERMANENT |
2268 static int __net_init tcpv6_net_init(struct net *net)
2270 return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2271 SOCK_RAW, IPPROTO_TCP, net);
2274 static void __net_exit tcpv6_net_exit(struct net *net)
2276 inet_ctl_sock_destroy(net->ipv6.tcp_sk);
2279 static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
2281 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
2284 static struct pernet_operations tcpv6_net_ops = {
2285 .init = tcpv6_net_init,
2286 .exit = tcpv6_net_exit,
2287 .exit_batch = tcpv6_net_exit_batch,
2290 int __init tcpv6_init(void)
2294 ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
2298 /* register inet6 protocol */
2299 ret = inet6_register_protosw(&tcpv6_protosw);
2301 goto out_tcpv6_protocol;
2303 ret = register_pernet_subsys(&tcpv6_net_ops);
2305 goto out_tcpv6_protosw;
2310 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2312 inet6_unregister_protosw(&tcpv6_protosw);
2316 void tcpv6_exit(void)
2318 unregister_pernet_subsys(&tcpv6_net_ops);
2319 inet6_unregister_protosw(&tcpv6_protosw);
2320 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);