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_ac_list = NULL;
1390 newnp->ipv6_fl_list = NULL;
1391 newnp->pktoptions = NULL;
1393 newnp->mcast_oif = inet6_iif(skb);
1394 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1397 * No need to charge this sock to the relevant IPv6 refcnt debug socks count
1398 * here, tcp_create_openreq_child now does this for us, see the comment in
1399 * that function for the gory details. -acme
1402 /* It is tricky place. Until this moment IPv4 tcp
1403 worked with IPv6 icsk.icsk_af_ops.
1406 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
1411 treq = inet6_rsk(req);
1413 if (sk_acceptq_is_full(sk))
1417 dst = inet6_csk_route_req(sk, req);
1422 newsk = tcp_create_openreq_child(sk, req, skb);
1427 * No need to charge this sock to the relevant IPv6 refcnt debug socks
1428 * count here, tcp_create_openreq_child now does this for us, see the
1429 * comment in that function for the gory details. -acme
1432 newsk->sk_gso_type = SKB_GSO_TCPV6;
1433 __ip6_dst_store(newsk, dst, NULL, NULL);
1435 newtcp6sk = (struct tcp6_sock *)newsk;
1436 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1438 newtp = tcp_sk(newsk);
1439 newinet = inet_sk(newsk);
1440 newnp = inet6_sk(newsk);
1442 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1444 ipv6_addr_copy(&newnp->daddr, &treq->rmt_addr);
1445 ipv6_addr_copy(&newnp->saddr, &treq->loc_addr);
1446 ipv6_addr_copy(&newnp->rcv_saddr, &treq->loc_addr);
1447 newsk->sk_bound_dev_if = treq->iif;
1449 /* Now IPv6 options...
1451 First: no IPv4 options.
1453 newinet->inet_opt = NULL;
1454 newnp->ipv6_ac_list = NULL;
1455 newnp->ipv6_fl_list = NULL;
1458 newnp->rxopt.all = np->rxopt.all;
1460 /* Clone pktoptions received with SYN */
1461 newnp->pktoptions = NULL;
1462 if (treq->pktopts != NULL) {
1463 newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
1464 kfree_skb(treq->pktopts);
1465 treq->pktopts = NULL;
1466 if (newnp->pktoptions)
1467 skb_set_owner_r(newnp->pktoptions, newsk);
1470 newnp->mcast_oif = inet6_iif(skb);
1471 newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1473 /* Clone native IPv6 options from listening socket (if any)
1475 Yes, keeping reference count would be much more clever,
1476 but we make one more one thing there: reattach optmem
1479 opt = rcu_dereference(np->opt);
1481 opt = ipv6_dup_options(newsk, opt);
1482 RCU_INIT_POINTER(newnp->opt, opt);
1484 inet_csk(newsk)->icsk_ext_hdr_len = 0;
1486 inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
1489 tcp_mtup_init(newsk);
1490 tcp_sync_mss(newsk, dst_mtu(dst));
1491 newtp->advmss = dst_metric_advmss(dst);
1492 if (tcp_sk(sk)->rx_opt.user_mss &&
1493 tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1494 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1496 tcp_initialize_rcv_mss(newsk);
1497 if (tcp_rsk(req)->snt_synack)
1498 tcp_valid_rtt_meas(newsk,
1499 tcp_time_stamp - tcp_rsk(req)->snt_synack);
1500 newtp->total_retrans = req->retrans;
1502 newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1503 newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
1505 #ifdef CONFIG_TCP_MD5SIG
1506 /* Copy over the MD5 key from the original socket */
1507 if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
1508 /* We're using one, so create a matching key
1509 * on the newsk structure. If we fail to get
1510 * memory, then we end up not copying the key
1513 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1515 tcp_v6_md5_do_add(newsk, &newnp->daddr,
1516 newkey, key->keylen);
1520 if (__inet_inherit_port(sk, newsk) < 0) {
1521 inet_csk_prepare_forced_close(newsk);
1525 __inet6_hash(newsk, NULL);
1530 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1534 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
1538 static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
1540 if (skb->ip_summed == CHECKSUM_COMPLETE) {
1541 if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
1542 &ipv6_hdr(skb)->daddr, skb->csum)) {
1543 skb->ip_summed = CHECKSUM_UNNECESSARY;
1548 skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
1549 &ipv6_hdr(skb)->saddr,
1550 &ipv6_hdr(skb)->daddr, 0));
1552 if (skb->len <= 76) {
1553 return __skb_checksum_complete(skb);
1558 /* The socket must have it's spinlock held when we get
1561 * We have a potential double-lock case here, so even when
1562 * doing backlog processing we use the BH locking scheme.
1563 * This is because we cannot sleep with the original spinlock
1566 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1568 struct ipv6_pinfo *np = inet6_sk(sk);
1569 struct tcp_sock *tp;
1570 struct sk_buff *opt_skb = NULL;
1572 /* Imagine: socket is IPv6. IPv4 packet arrives,
1573 goes to IPv4 receive handler and backlogged.
1574 From backlog it always goes here. Kerboom...
1575 Fortunately, tcp_rcv_established and rcv_established
1576 handle them correctly, but it is not case with
1577 tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
1580 if (skb->protocol == htons(ETH_P_IP))
1581 return tcp_v4_do_rcv(sk, skb);
1583 #ifdef CONFIG_TCP_MD5SIG
1584 if (tcp_v6_inbound_md5_hash (sk, skb))
1588 if (tcp_filter(sk, skb))
1592 * socket locking is here for SMP purposes as backlog rcv
1593 * is currently called with bh processing disabled.
1596 /* Do Stevens' IPV6_PKTOPTIONS.
1598 Yes, guys, it is the only place in our code, where we
1599 may make it not affecting IPv4.
1600 The rest of code is protocol independent,
1601 and I do not like idea to uglify IPv4.
1603 Actually, all the idea behind IPV6_PKTOPTIONS
1604 looks not very well thought. For now we latch
1605 options, received in the last packet, enqueued
1606 by tcp. Feel free to propose better solution.
1610 opt_skb = skb_clone(skb, GFP_ATOMIC);
1612 if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1613 sock_rps_save_rxhash(sk, skb);
1614 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
1617 goto ipv6_pktoptions;
1621 if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
1624 if (sk->sk_state == TCP_LISTEN) {
1625 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1630 * Queue it on the new socket if the new socket is active,
1631 * otherwise we just shortcircuit this and continue with
1635 sock_rps_save_rxhash(nsk, skb);
1636 if (tcp_child_process(sk, nsk, skb))
1639 __kfree_skb(opt_skb);
1643 sock_rps_save_rxhash(sk, skb);
1645 if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
1648 goto ipv6_pktoptions;
1652 tcp_v6_send_reset(sk, skb);
1655 __kfree_skb(opt_skb);
1659 TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
1664 /* Do you ask, what is it?
1666 1. skb was enqueued by tcp.
1667 2. skb is added to tail of read queue, rather than out of order.
1668 3. socket is not in passive state.
1669 4. Finally, it really contains options, which user wants to receive.
1672 if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1673 !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1674 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
1675 np->mcast_oif = inet6_iif(opt_skb);
1676 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
1677 np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
1678 if (ipv6_opt_accepted(sk, opt_skb)) {
1679 skb_set_owner_r(opt_skb, sk);
1680 opt_skb = xchg(&np->pktoptions, opt_skb);
1682 __kfree_skb(opt_skb);
1683 opt_skb = xchg(&np->pktoptions, NULL);
1691 static int tcp_v6_rcv(struct sk_buff *skb)
1693 const struct tcphdr *th;
1694 const struct ipv6hdr *hdr;
1697 struct net *net = dev_net(skb->dev);
1699 if (skb->pkt_type != PACKET_HOST)
1703 * Count it even if it's bad.
1705 TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
1707 if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1712 if (th->doff < sizeof(struct tcphdr)/4)
1714 if (!pskb_may_pull(skb, th->doff*4))
1717 if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
1721 hdr = ipv6_hdr(skb);
1722 TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1723 TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1724 skb->len - th->doff*4);
1725 TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1726 TCP_SKB_CB(skb)->when = 0;
1727 TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
1728 TCP_SKB_CB(skb)->sacked = 0;
1730 sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
1735 if (sk->sk_state == TCP_TIME_WAIT)
1738 if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
1739 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
1740 goto discard_and_relse;
1743 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1744 goto discard_and_relse;
1746 if (tcp_filter(sk, skb))
1747 goto discard_and_relse;
1748 th = (const struct tcphdr *)skb->data;
1749 hdr = ipv6_hdr(skb);
1753 bh_lock_sock_nested(sk);
1755 if (!sock_owned_by_user(sk)) {
1756 #ifdef CONFIG_NET_DMA
1757 struct tcp_sock *tp = tcp_sk(sk);
1758 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1759 tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
1760 if (tp->ucopy.dma_chan)
1761 ret = tcp_v6_do_rcv(sk, skb);
1765 if (!tcp_prequeue(sk, skb))
1766 ret = tcp_v6_do_rcv(sk, skb);
1768 } else if (unlikely(sk_add_backlog(sk, skb))) {
1770 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
1771 goto discard_and_relse;
1776 return ret ? -1 : 0;
1779 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1782 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1784 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1786 tcp_v6_send_reset(NULL, skb);
1803 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1804 inet_twsk_put(inet_twsk(sk));
1808 if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1809 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1810 inet_twsk_put(inet_twsk(sk));
1814 switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
1819 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
1820 &ipv6_hdr(skb)->daddr,
1821 ntohs(th->dest), inet6_iif(skb));
1823 struct inet_timewait_sock *tw = inet_twsk(sk);
1824 inet_twsk_deschedule(tw, &tcp_death_row);
1829 /* Fall through to ACK */
1832 tcp_v6_timewait_ack(sk, skb);
1836 case TCP_TW_SUCCESS:;
1841 static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
1843 struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
1844 struct ipv6_pinfo *np = inet6_sk(sk);
1845 struct inet_peer *peer;
1848 !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
1849 peer = inet_getpeer_v6(&np->daddr, 1);
1853 rt6_bind_peer(rt, 1);
1854 peer = rt->rt6i_peer;
1855 *release_it = false;
1861 static void *tcp_v6_tw_get_peer(struct sock *sk)
1863 const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
1864 const struct inet_timewait_sock *tw = inet_twsk(sk);
1866 if (tw->tw_family == AF_INET)
1867 return tcp_v4_tw_get_peer(sk);
1869 return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
1872 static struct timewait_sock_ops tcp6_timewait_sock_ops = {
1873 .twsk_obj_size = sizeof(struct tcp6_timewait_sock),
1874 .twsk_unique = tcp_twsk_unique,
1875 .twsk_destructor= tcp_twsk_destructor,
1876 .twsk_getpeer = tcp_v6_tw_get_peer,
1879 static const struct inet_connection_sock_af_ops ipv6_specific = {
1880 .queue_xmit = inet6_csk_xmit,
1881 .send_check = tcp_v6_send_check,
1882 .rebuild_header = inet6_sk_rebuild_header,
1883 .conn_request = tcp_v6_conn_request,
1884 .syn_recv_sock = tcp_v6_syn_recv_sock,
1885 .get_peer = tcp_v6_get_peer,
1886 .net_header_len = sizeof(struct ipv6hdr),
1887 .setsockopt = ipv6_setsockopt,
1888 .getsockopt = ipv6_getsockopt,
1889 .addr2sockaddr = inet6_csk_addr2sockaddr,
1890 .sockaddr_len = sizeof(struct sockaddr_in6),
1891 .bind_conflict = inet6_csk_bind_conflict,
1892 #ifdef CONFIG_COMPAT
1893 .compat_setsockopt = compat_ipv6_setsockopt,
1894 .compat_getsockopt = compat_ipv6_getsockopt,
1898 #ifdef CONFIG_TCP_MD5SIG
1899 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
1900 .md5_lookup = tcp_v6_md5_lookup,
1901 .calc_md5_hash = tcp_v6_md5_hash_skb,
1902 .md5_add = tcp_v6_md5_add_func,
1903 .md5_parse = tcp_v6_parse_md5_keys,
1908 * TCP over IPv4 via INET6 API
1911 static const struct inet_connection_sock_af_ops ipv6_mapped = {
1912 .queue_xmit = ip_queue_xmit,
1913 .send_check = tcp_v4_send_check,
1914 .rebuild_header = inet_sk_rebuild_header,
1915 .conn_request = tcp_v6_conn_request,
1916 .syn_recv_sock = tcp_v6_syn_recv_sock,
1917 .get_peer = tcp_v4_get_peer,
1918 .net_header_len = sizeof(struct iphdr),
1919 .setsockopt = ipv6_setsockopt,
1920 .getsockopt = ipv6_getsockopt,
1921 .addr2sockaddr = inet6_csk_addr2sockaddr,
1922 .sockaddr_len = sizeof(struct sockaddr_in6),
1923 .bind_conflict = inet6_csk_bind_conflict,
1924 #ifdef CONFIG_COMPAT
1925 .compat_setsockopt = compat_ipv6_setsockopt,
1926 .compat_getsockopt = compat_ipv6_getsockopt,
1930 #ifdef CONFIG_TCP_MD5SIG
1931 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
1932 .md5_lookup = tcp_v4_md5_lookup,
1933 .calc_md5_hash = tcp_v4_md5_hash_skb,
1934 .md5_add = tcp_v6_md5_add_func,
1935 .md5_parse = tcp_v6_parse_md5_keys,
1939 /* NOTE: A lot of things set to zero explicitly by call to
1940 * sk_alloc() so need not be done here.
1942 static int tcp_v6_init_sock(struct sock *sk)
1944 struct inet_connection_sock *icsk = inet_csk(sk);
1945 struct tcp_sock *tp = tcp_sk(sk);
1947 skb_queue_head_init(&tp->out_of_order_queue);
1948 tcp_init_xmit_timers(sk);
1949 tcp_prequeue_init(tp);
1951 icsk->icsk_rto = TCP_TIMEOUT_INIT;
1952 tp->mdev = TCP_TIMEOUT_INIT;
1954 /* So many TCP implementations out there (incorrectly) count the
1955 * initial SYN frame in their delayed-ACK and congestion control
1956 * algorithms that we must have the following bandaid to talk
1957 * efficiently to them. -DaveM
1961 /* See draft-stevens-tcpca-spec-01 for discussion of the
1962 * initialization of these values.
1964 tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1965 tp->snd_cwnd_clamp = ~0;
1966 tp->mss_cache = TCP_MSS_DEFAULT;
1968 tp->reordering = sysctl_tcp_reordering;
1970 sk->sk_state = TCP_CLOSE;
1972 icsk->icsk_af_ops = &ipv6_specific;
1973 icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1974 icsk->icsk_sync_mss = tcp_sync_mss;
1975 sk->sk_write_space = sk_stream_write_space;
1976 sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1978 #ifdef CONFIG_TCP_MD5SIG
1979 tp->af_specific = &tcp_sock_ipv6_specific;
1982 /* TCP Cookie Transactions */
1983 if (sysctl_tcp_cookie_size > 0) {
1984 /* Default, cookies without s_data_payload. */
1986 kzalloc(sizeof(*tp->cookie_values),
1988 if (tp->cookie_values != NULL)
1989 kref_init(&tp->cookie_values->kref);
1991 /* Presumed zeroed, in order of appearance:
1992 * cookie_in_always, cookie_out_never,
1993 * s_data_constant, s_data_in, s_data_out
1995 sk->sk_sndbuf = sysctl_tcp_wmem[1];
1996 sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1999 percpu_counter_inc(&tcp_sockets_allocated);
2005 static void tcp_v6_destroy_sock(struct sock *sk)
2007 #ifdef CONFIG_TCP_MD5SIG
2008 /* Clean up the MD5 key list */
2009 if (tcp_sk(sk)->md5sig_info)
2010 tcp_v6_clear_md5_list(sk);
2012 tcp_v4_destroy_sock(sk);
2013 inet6_destroy_sock(sk);
2016 #ifdef CONFIG_PROC_FS
2017 /* Proc filesystem TCPv6 sock list dumping. */
2018 static void get_openreq6(struct seq_file *seq,
2019 const struct sock *sk, struct request_sock *req, int i, int uid)
2021 int ttd = req->expires - jiffies;
2022 const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
2023 const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
2029 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2030 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2032 src->s6_addr32[0], src->s6_addr32[1],
2033 src->s6_addr32[2], src->s6_addr32[3],
2034 ntohs(inet_rsk(req)->loc_port),
2035 dest->s6_addr32[0], dest->s6_addr32[1],
2036 dest->s6_addr32[2], dest->s6_addr32[3],
2037 ntohs(inet_rsk(req)->rmt_port),
2039 0,0, /* could print option size, but that is af dependent. */
2040 1, /* timers active (only the expire timer) */
2041 jiffies_to_clock_t(ttd),
2044 0, /* non standard timer */
2045 0, /* open_requests have no inode */
2049 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
2051 const struct in6_addr *dest, *src;
2054 unsigned long timer_expires;
2055 const struct inet_sock *inet = inet_sk(sp);
2056 const struct tcp_sock *tp = tcp_sk(sp);
2057 const struct inet_connection_sock *icsk = inet_csk(sp);
2058 const struct ipv6_pinfo *np = inet6_sk(sp);
2061 src = &np->rcv_saddr;
2062 destp = ntohs(inet->inet_dport);
2063 srcp = ntohs(inet->inet_sport);
2065 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
2067 timer_expires = icsk->icsk_timeout;
2068 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2070 timer_expires = icsk->icsk_timeout;
2071 } else if (timer_pending(&sp->sk_timer)) {
2073 timer_expires = sp->sk_timer.expires;
2076 timer_expires = jiffies;
2080 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2081 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
2083 src->s6_addr32[0], src->s6_addr32[1],
2084 src->s6_addr32[2], src->s6_addr32[3], srcp,
2085 dest->s6_addr32[0], dest->s6_addr32[1],
2086 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2088 tp->write_seq-tp->snd_una,
2089 (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
2091 jiffies_to_clock_t(timer_expires - jiffies),
2092 icsk->icsk_retransmits,
2094 icsk->icsk_probes_out,
2096 atomic_read(&sp->sk_refcnt), sp,
2097 jiffies_to_clock_t(icsk->icsk_rto),
2098 jiffies_to_clock_t(icsk->icsk_ack.ato),
2099 (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
2101 tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
2105 static void get_timewait6_sock(struct seq_file *seq,
2106 struct inet_timewait_sock *tw, int i)
2108 const struct in6_addr *dest, *src;
2110 const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
2111 int ttd = tw->tw_ttd - jiffies;
2116 dest = &tw6->tw_v6_daddr;
2117 src = &tw6->tw_v6_rcv_saddr;
2118 destp = ntohs(tw->tw_dport);
2119 srcp = ntohs(tw->tw_sport);
2122 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2123 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2125 src->s6_addr32[0], src->s6_addr32[1],
2126 src->s6_addr32[2], src->s6_addr32[3], srcp,
2127 dest->s6_addr32[0], dest->s6_addr32[1],
2128 dest->s6_addr32[2], dest->s6_addr32[3], destp,
2129 tw->tw_substate, 0, 0,
2130 3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
2131 atomic_read(&tw->tw_refcnt), tw);
2134 static int tcp6_seq_show(struct seq_file *seq, void *v)
2136 struct tcp_iter_state *st;
2138 if (v == SEQ_START_TOKEN) {
2143 "st tx_queue rx_queue tr tm->when retrnsmt"
2144 " uid timeout inode\n");
2149 switch (st->state) {
2150 case TCP_SEQ_STATE_LISTENING:
2151 case TCP_SEQ_STATE_ESTABLISHED:
2152 get_tcp6_sock(seq, v, st->num);
2154 case TCP_SEQ_STATE_OPENREQ:
2155 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
2157 case TCP_SEQ_STATE_TIME_WAIT:
2158 get_timewait6_sock(seq, v, st->num);
2165 static const struct file_operations tcp6_afinfo_seq_fops = {
2166 .owner = THIS_MODULE,
2167 .open = tcp_seq_open,
2169 .llseek = seq_lseek,
2170 .release = seq_release_net
2173 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2176 .seq_fops = &tcp6_afinfo_seq_fops,
2178 .show = tcp6_seq_show,
2182 int __net_init tcp6_proc_init(struct net *net)
2184 return tcp_proc_register(net, &tcp6_seq_afinfo);
2187 void tcp6_proc_exit(struct net *net)
2189 tcp_proc_unregister(net, &tcp6_seq_afinfo);
2193 static void tcp_v6_clear_sk(struct sock *sk, int size)
2195 struct inet_sock *inet = inet_sk(sk);
2197 /* we do not want to clear pinet6 field, because of RCU lookups */
2198 sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
2200 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
2201 memset(&inet->pinet6 + 1, 0, size);
2204 struct proto tcpv6_prot = {
2206 .owner = THIS_MODULE,
2208 .connect = tcp_v6_connect,
2209 .disconnect = tcp_disconnect,
2210 .accept = inet_csk_accept,
2212 .init = tcp_v6_init_sock,
2213 .destroy = tcp_v6_destroy_sock,
2214 .shutdown = tcp_shutdown,
2215 .setsockopt = tcp_setsockopt,
2216 .getsockopt = tcp_getsockopt,
2217 .recvmsg = tcp_recvmsg,
2218 .sendmsg = tcp_sendmsg,
2219 .sendpage = tcp_sendpage,
2220 .backlog_rcv = tcp_v6_do_rcv,
2221 .hash = tcp_v6_hash,
2222 .unhash = inet_unhash,
2223 .get_port = inet_csk_get_port,
2224 .enter_memory_pressure = tcp_enter_memory_pressure,
2225 .sockets_allocated = &tcp_sockets_allocated,
2226 .memory_allocated = &tcp_memory_allocated,
2227 .memory_pressure = &tcp_memory_pressure,
2228 .orphan_count = &tcp_orphan_count,
2229 .sysctl_mem = sysctl_tcp_mem,
2230 .sysctl_wmem = sysctl_tcp_wmem,
2231 .sysctl_rmem = sysctl_tcp_rmem,
2232 .max_header = MAX_TCP_HEADER,
2233 .obj_size = sizeof(struct tcp6_sock),
2234 .slab_flags = SLAB_DESTROY_BY_RCU,
2235 .twsk_prot = &tcp6_timewait_sock_ops,
2236 .rsk_prot = &tcp6_request_sock_ops,
2237 .h.hashinfo = &tcp_hashinfo,
2238 .no_autobind = true,
2239 #ifdef CONFIG_COMPAT
2240 .compat_setsockopt = compat_tcp_setsockopt,
2241 .compat_getsockopt = compat_tcp_getsockopt,
2243 .clear_sk = tcp_v6_clear_sk,
2246 static const struct inet6_protocol tcpv6_protocol = {
2247 .handler = tcp_v6_rcv,
2248 .err_handler = tcp_v6_err,
2249 .gso_send_check = tcp_v6_gso_send_check,
2250 .gso_segment = tcp_tso_segment,
2251 .gro_receive = tcp6_gro_receive,
2252 .gro_complete = tcp6_gro_complete,
2253 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2256 static struct inet_protosw tcpv6_protosw = {
2257 .type = SOCK_STREAM,
2258 .protocol = IPPROTO_TCP,
2259 .prot = &tcpv6_prot,
2260 .ops = &inet6_stream_ops,
2262 .flags = INET_PROTOSW_PERMANENT |
2266 static int __net_init tcpv6_net_init(struct net *net)
2268 return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2269 SOCK_RAW, IPPROTO_TCP, net);
2272 static void __net_exit tcpv6_net_exit(struct net *net)
2274 inet_ctl_sock_destroy(net->ipv6.tcp_sk);
2277 static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
2279 inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
2282 static struct pernet_operations tcpv6_net_ops = {
2283 .init = tcpv6_net_init,
2284 .exit = tcpv6_net_exit,
2285 .exit_batch = tcpv6_net_exit_batch,
2288 int __init tcpv6_init(void)
2292 ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
2296 /* register inet6 protocol */
2297 ret = inet6_register_protosw(&tcpv6_protosw);
2299 goto out_tcpv6_protocol;
2301 ret = register_pernet_subsys(&tcpv6_net_ops);
2303 goto out_tcpv6_protosw;
2308 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2310 inet6_unregister_protosw(&tcpv6_protosw);
2314 void tcpv6_exit(void)
2316 unregister_pernet_subsys(&tcpv6_net_ops);
2317 inet6_unregister_protosw(&tcpv6_protosw);
2318 inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);