3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * $Id: tcp_ipv6.c,v 1.144 2002/02/01 22:01:04 davem Exp $
11 * linux/net/ipv4/tcp.c
12 * linux/net/ipv4/tcp_input.c
13 * linux/net/ipv4/tcp_output.c
16 * Hideaki YOSHIFUJI : sin6_scope_id support
17 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
18 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
19 * a single port at the same time.
20 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/tcp6 to seq_file.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * as published by the Free Software Foundation; either version
25 * 2 of the License, or (at your option) any later version.
28 #include <linux/module.h>
29 #include <linux/config.h>
30 #include <linux/errno.h>
31 #include <linux/types.h>
32 #include <linux/socket.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
35 #include <linux/jiffies.h>
37 #include <linux/in6.h>
38 #include <linux/netdevice.h>
39 #include <linux/init.h>
40 #include <linux/jhash.h>
41 #include <linux/ipsec.h>
43 #include <linux/ipv6.h>
44 #include <linux/icmpv6.h>
45 #include <linux/random.h>
48 #include <net/ndisc.h>
50 #include <net/transp_v6.h>
51 #include <net/addrconf.h>
52 #include <net/ip6_route.h>
53 #include <net/inet_ecn.h>
54 #include <net/protocol.h>
57 #include <asm/uaccess.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
62 static void tcp_v6_send_reset(struct sk_buff
*skb
);
63 static void tcp_v6_or_send_ack(struct sk_buff
*skb
, struct open_request
*req
);
64 static void tcp_v6_send_check(struct sock
*sk
, struct tcphdr
*th
, int len
,
67 static int tcp_v6_do_rcv(struct sock
*sk
, struct sk_buff
*skb
);
68 static int tcp_v6_xmit(struct sk_buff
*skb
, int ipfragok
);
70 static struct tcp_func ipv6_mapped
;
71 static struct tcp_func ipv6_specific
;
73 /* I have no idea if this is a good hash for v6 or not. -DaveM */
74 static __inline__
int tcp_v6_hashfn(struct in6_addr
*laddr
, u16 lport
,
75 struct in6_addr
*faddr
, u16 fport
)
77 int hashent
= (lport
^ fport
);
79 hashent
^= (laddr
->s6_addr32
[3] ^ faddr
->s6_addr32
[3]);
80 hashent
^= hashent
>>16;
81 hashent
^= hashent
>>8;
82 return (hashent
& (tcp_ehash_size
- 1));
85 static __inline__
int tcp_v6_sk_hashfn(struct sock
*sk
)
87 struct inet_opt
*inet
= inet_sk(sk
);
88 struct ipv6_pinfo
*np
= inet6_sk(sk
);
89 struct in6_addr
*laddr
= &np
->rcv_saddr
;
90 struct in6_addr
*faddr
= &np
->daddr
;
91 __u16 lport
= inet
->num
;
92 __u16 fport
= inet
->dport
;
93 return tcp_v6_hashfn(laddr
, lport
, faddr
, fport
);
96 static inline int tcp_v6_bind_conflict(struct sock
*sk
,
97 struct tcp_bind_bucket
*tb
)
100 struct hlist_node
*node
;
102 /* We must walk the whole port owner list in this case. -DaveM */
103 sk_for_each_bound(sk2
, node
, &tb
->owners
) {
104 if (sk
!= sk2
&& sk
->sk_bound_dev_if
== sk2
->sk_bound_dev_if
&&
105 (!sk
->sk_reuse
|| !sk2
->sk_reuse
||
106 sk2
->sk_state
== TCP_LISTEN
) &&
107 ipv6_rcv_saddr_equal(sk
, sk2
))
114 /* Grrr, addr_type already calculated by caller, but I don't want
115 * to add some silly "cookie" argument to this method just for that.
116 * But it doesn't matter, the recalculation is in the rarest path
117 * this function ever takes.
119 static int tcp_v6_get_port(struct sock
*sk
, unsigned short snum
)
121 struct tcp_bind_hashbucket
*head
;
122 struct tcp_bind_bucket
*tb
;
123 struct hlist_node
*node
;
128 int low
= sysctl_local_port_range
[0];
129 int high
= sysctl_local_port_range
[1];
130 int remaining
= (high
- low
) + 1;
133 spin_lock(&tcp_portalloc_lock
);
134 rover
= tcp_port_rover
;
136 if ((rover
< low
) || (rover
> high
))
138 head
= &tcp_bhash
[tcp_bhashfn(rover
)];
139 spin_lock(&head
->lock
);
140 tb_for_each(tb
, node
, &head
->chain
)
141 if (tb
->port
== rover
)
145 spin_unlock(&head
->lock
);
146 } while (--remaining
> 0);
147 tcp_port_rover
= rover
;
148 spin_unlock(&tcp_portalloc_lock
);
150 /* Exhausted local port range during search? */
155 /* OK, here is the one we will use. */
158 head
= &tcp_bhash
[tcp_bhashfn(snum
)];
159 spin_lock(&head
->lock
);
160 tb_for_each(tb
, node
, &head
->chain
)
161 if (tb
->port
== snum
)
167 if (tb
&& !hlist_empty(&tb
->owners
)) {
168 if (tb
->fastreuse
> 0 && sk
->sk_reuse
&&
169 sk
->sk_state
!= TCP_LISTEN
) {
173 if (tcp_v6_bind_conflict(sk
, tb
))
179 if (!tb
&& (tb
= tcp_bucket_create(head
, snum
)) == NULL
)
181 if (hlist_empty(&tb
->owners
)) {
182 if (sk
->sk_reuse
&& sk
->sk_state
!= TCP_LISTEN
)
186 } else if (tb
->fastreuse
&&
187 (!sk
->sk_reuse
|| sk
->sk_state
== TCP_LISTEN
))
191 if (!tcp_sk(sk
)->bind_hash
)
192 tcp_bind_hash(sk
, tb
, snum
);
193 BUG_TRAP(tcp_sk(sk
)->bind_hash
== tb
);
197 spin_unlock(&head
->lock
);
203 static __inline__
void __tcp_v6_hash(struct sock
*sk
)
205 struct hlist_head
*list
;
208 BUG_TRAP(sk_unhashed(sk
));
210 if (sk
->sk_state
== TCP_LISTEN
) {
211 list
= &tcp_listening_hash
[tcp_sk_listen_hashfn(sk
)];
212 lock
= &tcp_lhash_lock
;
215 sk
->sk_hashent
= tcp_v6_sk_hashfn(sk
);
216 list
= &tcp_ehash
[sk
->sk_hashent
].chain
;
217 lock
= &tcp_ehash
[sk
->sk_hashent
].lock
;
221 sk_add_node(sk
, list
);
222 sock_prot_inc_use(sk
->sk_prot
);
227 static void tcp_v6_hash(struct sock
*sk
)
229 if (sk
->sk_state
!= TCP_CLOSE
) {
230 struct tcp_opt
*tp
= tcp_sk(sk
);
232 if (tp
->af_specific
== &ipv6_mapped
) {
242 static struct sock
*tcp_v6_lookup_listener(struct in6_addr
*daddr
, unsigned short hnum
, int dif
)
245 struct hlist_node
*node
;
246 struct sock
*result
= NULL
;
250 read_lock(&tcp_lhash_lock
);
251 sk_for_each(sk
, node
, &tcp_listening_hash
[tcp_lhashfn(hnum
)]) {
252 if (inet_sk(sk
)->num
== hnum
&& sk
->sk_family
== PF_INET6
) {
253 struct ipv6_pinfo
*np
= inet6_sk(sk
);
256 if (!ipv6_addr_any(&np
->rcv_saddr
)) {
257 if (ipv6_addr_cmp(&np
->rcv_saddr
, daddr
))
261 if (sk
->sk_bound_dev_if
) {
262 if (sk
->sk_bound_dev_if
!= dif
)
270 if (score
> hiscore
) {
278 read_unlock(&tcp_lhash_lock
);
282 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
283 * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
285 * The sockhash lock must be held as a reader here.
288 static inline struct sock
*__tcp_v6_lookup_established(struct in6_addr
*saddr
, u16 sport
,
289 struct in6_addr
*daddr
, u16 hnum
,
292 struct tcp_ehash_bucket
*head
;
294 struct hlist_node
*node
;
295 __u32 ports
= TCP_COMBINED_PORTS(sport
, hnum
);
298 /* Optimize here for direct hit, only listening connections can
299 * have wildcards anyways.
301 hash
= tcp_v6_hashfn(daddr
, hnum
, saddr
, sport
);
302 head
= &tcp_ehash
[hash
];
303 read_lock(&head
->lock
);
304 sk_for_each(sk
, node
, &head
->chain
) {
305 /* For IPV6 do the cheaper port and family tests first. */
306 if(TCP_IPV6_MATCH(sk
, saddr
, daddr
, ports
, dif
))
307 goto hit
; /* You sunk my battleship! */
309 /* Must check for a TIME_WAIT'er before going to listener hash. */
310 sk_for_each(sk
, node
, &(head
+ tcp_ehash_size
)->chain
) {
311 /* FIXME: acme: check this... */
312 struct tcp_tw_bucket
*tw
= (struct tcp_tw_bucket
*)sk
;
314 if(*((__u32
*)&(tw
->tw_dport
)) == ports
&&
315 sk
->sk_family
== PF_INET6
) {
316 if(!ipv6_addr_cmp(&tw
->tw_v6_daddr
, saddr
) &&
317 !ipv6_addr_cmp(&tw
->tw_v6_rcv_saddr
, daddr
) &&
318 (!sk
->sk_bound_dev_if
|| sk
->sk_bound_dev_if
== dif
))
322 read_unlock(&head
->lock
);
327 read_unlock(&head
->lock
);
332 static inline struct sock
*__tcp_v6_lookup(struct in6_addr
*saddr
, u16 sport
,
333 struct in6_addr
*daddr
, u16 hnum
,
338 sk
= __tcp_v6_lookup_established(saddr
, sport
, daddr
, hnum
, dif
);
343 return tcp_v6_lookup_listener(daddr
, hnum
, dif
);
346 inline struct sock
*tcp_v6_lookup(struct in6_addr
*saddr
, u16 sport
,
347 struct in6_addr
*daddr
, u16 dport
,
353 sk
= __tcp_v6_lookup(saddr
, sport
, daddr
, ntohs(dport
), dif
);
361 * Open request hash tables.
364 static u32
tcp_v6_synq_hash(struct in6_addr
*raddr
, u16 rport
, u32 rnd
)
368 a
= raddr
->s6_addr32
[0];
369 b
= raddr
->s6_addr32
[1];
370 c
= raddr
->s6_addr32
[2];
372 a
+= JHASH_GOLDEN_RATIO
;
373 b
+= JHASH_GOLDEN_RATIO
;
375 __jhash_mix(a
, b
, c
);
377 a
+= raddr
->s6_addr32
[3];
379 __jhash_mix(a
, b
, c
);
381 return c
& (TCP_SYNQ_HSIZE
- 1);
384 static struct open_request
*tcp_v6_search_req(struct tcp_opt
*tp
,
385 struct open_request
***prevp
,
387 struct in6_addr
*raddr
,
388 struct in6_addr
*laddr
,
391 struct tcp_listen_opt
*lopt
= tp
->listen_opt
;
392 struct open_request
*req
, **prev
;
394 for (prev
= &lopt
->syn_table
[tcp_v6_synq_hash(raddr
, rport
, lopt
->hash_rnd
)];
395 (req
= *prev
) != NULL
;
396 prev
= &req
->dl_next
) {
397 if (req
->rmt_port
== rport
&&
398 req
->class->family
== AF_INET6
&&
399 !ipv6_addr_cmp(&req
->af
.v6_req
.rmt_addr
, raddr
) &&
400 !ipv6_addr_cmp(&req
->af
.v6_req
.loc_addr
, laddr
) &&
401 (!req
->af
.v6_req
.iif
|| req
->af
.v6_req
.iif
== iif
)) {
402 BUG_TRAP(req
->sk
== NULL
);
411 static __inline__ u16
tcp_v6_check(struct tcphdr
*th
, int len
,
412 struct in6_addr
*saddr
,
413 struct in6_addr
*daddr
,
416 return csum_ipv6_magic(saddr
, daddr
, len
, IPPROTO_TCP
, base
);
419 static __u32
tcp_v6_init_sequence(struct sock
*sk
, struct sk_buff
*skb
)
421 if (skb
->protocol
== htons(ETH_P_IPV6
)) {
422 return secure_tcpv6_sequence_number(skb
->nh
.ipv6h
->daddr
.s6_addr32
,
423 skb
->nh
.ipv6h
->saddr
.s6_addr32
,
427 return secure_tcp_sequence_number(skb
->nh
.iph
->daddr
,
434 static int tcp_v6_check_established(struct sock
*sk
)
436 struct inet_opt
*inet
= inet_sk(sk
);
437 struct ipv6_pinfo
*np
= inet6_sk(sk
);
438 struct in6_addr
*daddr
= &np
->rcv_saddr
;
439 struct in6_addr
*saddr
= &np
->daddr
;
440 int dif
= sk
->sk_bound_dev_if
;
441 u32 ports
= TCP_COMBINED_PORTS(inet
->dport
, inet
->num
);
442 int hash
= tcp_v6_hashfn(daddr
, inet
->num
, saddr
, inet
->dport
);
443 struct tcp_ehash_bucket
*head
= &tcp_ehash
[hash
];
445 struct hlist_node
*node
;
446 struct tcp_tw_bucket
*tw
;
448 write_lock_bh(&head
->lock
);
450 /* Check TIME-WAIT sockets first. */
451 sk_for_each(sk2
, node
, &(head
+ tcp_ehash_size
)->chain
) {
452 tw
= (struct tcp_tw_bucket
*)sk2
;
454 if(*((__u32
*)&(tw
->tw_dport
)) == ports
&&
455 sk2
->sk_family
== PF_INET6
&&
456 !ipv6_addr_cmp(&tw
->tw_v6_daddr
, saddr
) &&
457 !ipv6_addr_cmp(&tw
->tw_v6_rcv_saddr
, daddr
) &&
458 sk2
->sk_bound_dev_if
== sk
->sk_bound_dev_if
) {
459 struct tcp_opt
*tp
= tcp_sk(sk
);
461 if (tw
->tw_ts_recent_stamp
) {
462 /* See comment in tcp_ipv4.c */
463 tp
->write_seq
= tw
->tw_snd_nxt
+ 65535 + 2;
466 tp
->ts_recent
= tw
->tw_ts_recent
;
467 tp
->ts_recent_stamp
= tw
->tw_ts_recent_stamp
;
476 /* And established part... */
477 sk_for_each(sk2
, node
, &head
->chain
) {
478 if(TCP_IPV6_MATCH(sk
, saddr
, daddr
, ports
, dif
))
483 BUG_TRAP(sk_unhashed(sk
));
484 sk_add_node(sk
, &head
->chain
);
485 sk
->sk_hashent
= hash
;
486 sock_prot_inc_use(sk
->sk_prot
);
487 write_unlock_bh(&head
->lock
);
490 /* Silly. Should hash-dance instead... */
492 tcp_tw_deschedule(tw
);
493 NET_INC_STATS_BH(TimeWaitRecycled
);
501 write_unlock_bh(&head
->lock
);
502 return -EADDRNOTAVAIL
;
505 static int tcp_v6_hash_connect(struct sock
*sk
)
507 struct tcp_bind_hashbucket
*head
;
508 struct tcp_bind_bucket
*tb
;
511 if (inet_sk(sk
)->num
== 0) {
512 int err
= tcp_v6_get_port(sk
, inet_sk(sk
)->num
);
515 inet_sk(sk
)->sport
= htons(inet_sk(sk
)->num
);
518 head
= &tcp_bhash
[tcp_bhashfn(inet_sk(sk
)->num
)];
521 spin_lock_bh(&head
->lock
);
523 if (sk_head(&tb
->owners
) == sk
&& !sk
->sk_bind_node
.next
) {
525 spin_unlock_bh(&head
->lock
);
528 spin_unlock_bh(&head
->lock
);
529 return tcp_v6_check_established(sk
);
533 static __inline__
int tcp_v6_iif(struct sk_buff
*skb
)
535 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*) skb
->cb
;
539 static int tcp_v6_connect(struct sock
*sk
, struct sockaddr
*uaddr
,
542 struct sockaddr_in6
*usin
= (struct sockaddr_in6
*) uaddr
;
543 struct inet_opt
*inet
= inet_sk(sk
);
544 struct ipv6_pinfo
*np
= inet6_sk(sk
);
545 struct tcp_opt
*tp
= tcp_sk(sk
);
546 struct in6_addr
*saddr
= NULL
;
547 struct in6_addr saddr_buf
;
549 struct dst_entry
*dst
;
553 if (addr_len
< SIN6_LEN_RFC2133
)
556 if (usin
->sin6_family
!= AF_INET6
)
557 return(-EAFNOSUPPORT
);
559 memset(&fl
, 0, sizeof(fl
));
562 fl
.fl6_flowlabel
= usin
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
563 IP6_ECN_flow_init(fl
.fl6_flowlabel
);
564 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
565 struct ip6_flowlabel
*flowlabel
;
566 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
567 if (flowlabel
== NULL
)
569 ipv6_addr_copy(&usin
->sin6_addr
, &flowlabel
->dst
);
570 fl6_sock_release(flowlabel
);
575 * connect() to INADDR_ANY means loopback (BSD'ism).
578 if(ipv6_addr_any(&usin
->sin6_addr
))
579 usin
->sin6_addr
.s6_addr
[15] = 0x1;
581 addr_type
= ipv6_addr_type(&usin
->sin6_addr
);
583 if(addr_type
& IPV6_ADDR_MULTICAST
)
586 if (addr_type
&IPV6_ADDR_LINKLOCAL
) {
587 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
588 usin
->sin6_scope_id
) {
589 /* If interface is set while binding, indices
592 if (sk
->sk_bound_dev_if
&&
593 sk
->sk_bound_dev_if
!= usin
->sin6_scope_id
)
596 sk
->sk_bound_dev_if
= usin
->sin6_scope_id
;
599 /* Connect to link-local address requires an interface */
600 if (!sk
->sk_bound_dev_if
)
604 if (tp
->ts_recent_stamp
&&
605 ipv6_addr_cmp(&np
->daddr
, &usin
->sin6_addr
)) {
607 tp
->ts_recent_stamp
= 0;
611 ipv6_addr_copy(&np
->daddr
, &usin
->sin6_addr
);
612 np
->flow_label
= fl
.fl6_flowlabel
;
618 if (addr_type
== IPV6_ADDR_MAPPED
) {
619 u32 exthdrlen
= tp
->ext_header_len
;
620 struct sockaddr_in sin
;
622 SOCK_DEBUG(sk
, "connect: ipv4 mapped\n");
624 if (__ipv6_only_sock(sk
))
627 sin
.sin_family
= AF_INET
;
628 sin
.sin_port
= usin
->sin6_port
;
629 sin
.sin_addr
.s_addr
= usin
->sin6_addr
.s6_addr32
[3];
631 tp
->af_specific
= &ipv6_mapped
;
632 sk
->sk_backlog_rcv
= tcp_v4_do_rcv
;
634 err
= tcp_v4_connect(sk
, (struct sockaddr
*)&sin
, sizeof(sin
));
637 tp
->ext_header_len
= exthdrlen
;
638 tp
->af_specific
= &ipv6_specific
;
639 sk
->sk_backlog_rcv
= tcp_v6_do_rcv
;
642 ipv6_addr_set(&np
->saddr
, 0, 0, htonl(0x0000FFFF),
644 ipv6_addr_set(&np
->rcv_saddr
, 0, 0, htonl(0x0000FFFF),
651 if (!ipv6_addr_any(&np
->rcv_saddr
))
652 saddr
= &np
->rcv_saddr
;
654 fl
.proto
= IPPROTO_TCP
;
655 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
656 ipv6_addr_copy(&fl
.fl6_src
,
657 (saddr
? saddr
: &np
->saddr
));
658 fl
.oif
= sk
->sk_bound_dev_if
;
659 fl
.fl_ip_dport
= usin
->sin6_port
;
660 fl
.fl_ip_sport
= inet
->sport
;
662 if (np
->opt
&& np
->opt
->srcrt
) {
663 struct rt0_hdr
*rt0
= (struct rt0_hdr
*)np
->opt
->srcrt
;
664 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
667 dst
= ip6_route_output(sk
, &fl
);
669 if ((err
= dst
->error
) != 0) {
674 ip6_dst_store(sk
, dst
, NULL
);
675 sk
->sk_route_caps
= dst
->dev
->features
&
676 ~(NETIF_F_IP_CSUM
| NETIF_F_TSO
);
679 err
= ipv6_get_saddr(dst
, &np
->daddr
, &saddr_buf
);
686 /* set the source address */
687 ipv6_addr_copy(&np
->rcv_saddr
, saddr
);
688 ipv6_addr_copy(&np
->saddr
, saddr
);
689 inet
->rcv_saddr
= LOOPBACK4_IPV6
;
691 tp
->ext_header_len
= 0;
693 tp
->ext_header_len
= np
->opt
->opt_flen
+ np
->opt
->opt_nflen
;
694 tp
->mss_clamp
= IPV6_MIN_MTU
- sizeof(struct tcphdr
) - sizeof(struct ipv6hdr
);
696 inet
->dport
= usin
->sin6_port
;
698 tcp_set_state(sk
, TCP_SYN_SENT
);
699 err
= tcp_v6_hash_connect(sk
);
704 tp
->write_seq
= secure_tcpv6_sequence_number(np
->saddr
.s6_addr32
,
709 err
= tcp_connect(sk
);
716 tcp_set_state(sk
, TCP_CLOSE
);
720 sk
->sk_route_caps
= 0;
724 static void tcp_v6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
725 int type
, int code
, int offset
, __u32 info
)
727 struct ipv6hdr
*hdr
= (struct ipv6hdr
*)skb
->data
;
728 struct tcphdr
*th
= (struct tcphdr
*)(skb
->data
+offset
);
729 struct ipv6_pinfo
*np
;
735 sk
= tcp_v6_lookup(&hdr
->daddr
, th
->dest
, &hdr
->saddr
, th
->source
, skb
->dev
->ifindex
);
738 ICMP6_INC_STATS_BH(__in6_dev_get(skb
->dev
), Icmp6InErrors
);
742 if (sk
->sk_state
== TCP_TIME_WAIT
) {
743 tcp_tw_put((struct tcp_tw_bucket
*)sk
);
748 if (sock_owned_by_user(sk
))
749 NET_INC_STATS_BH(LockDroppedIcmps
);
751 if (sk
->sk_state
== TCP_CLOSE
)
755 seq
= ntohl(th
->seq
);
756 if (sk
->sk_state
!= TCP_LISTEN
&&
757 !between(seq
, tp
->snd_una
, tp
->snd_nxt
)) {
758 NET_INC_STATS_BH(OutOfWindowIcmps
);
764 if (type
== ICMPV6_PKT_TOOBIG
) {
765 struct dst_entry
*dst
= NULL
;
767 if (sock_owned_by_user(sk
))
769 if ((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
))
772 /* icmp should have updated the destination cache entry */
773 dst
= __sk_dst_check(sk
, np
->dst_cookie
);
776 struct inet_opt
*inet
= inet_sk(sk
);
779 /* BUGGG_FUTURE: Again, it is not clear how
780 to handle rthdr case. Ignore this complexity
783 memset(&fl
, 0, sizeof(fl
));
784 fl
.proto
= IPPROTO_TCP
;
785 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
786 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
787 fl
.oif
= sk
->sk_bound_dev_if
;
788 fl
.fl_ip_dport
= inet
->dport
;
789 fl
.fl_ip_sport
= inet
->sport
;
791 dst
= ip6_route_output(sk
, &fl
);
796 sk
->sk_err_soft
= -dst
->error
;
797 } else if (tp
->pmtu_cookie
> dst_pmtu(dst
)) {
798 tcp_sync_mss(sk
, dst_pmtu(dst
));
799 tcp_simple_retransmit(sk
);
800 } /* else let the usual retransmit timer handle it */
805 icmpv6_err_convert(type
, code
, &err
);
807 /* Might be for an open_request */
808 switch (sk
->sk_state
) {
809 struct open_request
*req
, **prev
;
811 if (sock_owned_by_user(sk
))
814 req
= tcp_v6_search_req(tp
, &prev
, th
->dest
, &hdr
->daddr
,
815 &hdr
->saddr
, tcp_v6_iif(skb
));
819 /* ICMPs are not backlogged, hence we cannot get
820 * an established socket here.
822 BUG_TRAP(req
->sk
== NULL
);
824 if (seq
!= req
->snt_isn
) {
825 NET_INC_STATS_BH(OutOfWindowIcmps
);
829 tcp_synq_drop(sk
, req
, prev
);
833 case TCP_SYN_RECV
: /* Cannot happen.
834 It can, it SYNs are crossed. --ANK */
835 if (!sock_owned_by_user(sk
)) {
836 TCP_INC_STATS_BH(TcpAttemptFails
);
838 sk
->sk_error_report(sk
); /* Wake people up to see the error (see connect in sock.c) */
842 sk
->sk_err_soft
= err
;
846 if (!sock_owned_by_user(sk
) && np
->recverr
) {
848 sk
->sk_error_report(sk
);
850 sk
->sk_err_soft
= err
;
858 static int tcp_v6_send_synack(struct sock
*sk
, struct open_request
*req
,
859 struct dst_entry
*dst
)
861 struct ipv6_pinfo
*np
= inet6_sk(sk
);
862 struct sk_buff
* skb
;
863 struct ipv6_txoptions
*opt
= NULL
;
867 memset(&fl
, 0, sizeof(fl
));
868 fl
.proto
= IPPROTO_TCP
;
869 ipv6_addr_copy(&fl
.fl6_dst
, &req
->af
.v6_req
.rmt_addr
);
870 ipv6_addr_copy(&fl
.fl6_src
, &req
->af
.v6_req
.loc_addr
);
871 fl
.fl6_flowlabel
= 0;
872 fl
.oif
= req
->af
.v6_req
.iif
;
873 fl
.fl_ip_dport
= req
->rmt_port
;
874 fl
.fl_ip_sport
= inet_sk(sk
)->sport
;
879 np
->rxopt
.bits
.srcrt
== 2 &&
880 req
->af
.v6_req
.pktopts
) {
881 struct sk_buff
*pktopts
= req
->af
.v6_req
.pktopts
;
882 struct inet6_skb_parm
*rxopt
= (struct inet6_skb_parm
*)pktopts
->cb
;
884 opt
= ipv6_invert_rthdr(sk
, (struct ipv6_rt_hdr
*)(pktopts
->nh
.raw
+ rxopt
->srcrt
));
887 if (opt
&& opt
->srcrt
) {
888 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
889 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
892 dst
= ip6_route_output(sk
, &fl
);
897 skb
= tcp_make_synack(sk
, dst
, req
);
899 struct tcphdr
*th
= skb
->h
.th
;
901 th
->check
= tcp_v6_check(th
, skb
->len
,
902 &req
->af
.v6_req
.loc_addr
, &req
->af
.v6_req
.rmt_addr
,
903 csum_partial((char *)th
, skb
->len
, skb
->csum
));
905 ipv6_addr_copy(&fl
.fl6_dst
, &req
->af
.v6_req
.rmt_addr
);
906 err
= ip6_xmit(sk
, skb
, &fl
, opt
, 0);
907 if (err
== NET_XMIT_CN
)
913 if (opt
&& opt
!= np
->opt
)
914 sock_kfree_s(sk
, opt
, opt
->tot_len
);
918 static void tcp_v6_or_free(struct open_request
*req
)
920 if (req
->af
.v6_req
.pktopts
)
921 kfree_skb(req
->af
.v6_req
.pktopts
);
924 static struct or_calltable or_ipv6
= {
926 .rtx_syn_ack
= tcp_v6_send_synack
,
927 .send_ack
= tcp_v6_or_send_ack
,
928 .destructor
= tcp_v6_or_free
,
929 .send_reset
= tcp_v6_send_reset
932 static int ipv6_opt_accepted(struct sock
*sk
, struct sk_buff
*skb
)
934 struct ipv6_pinfo
*np
= inet6_sk(sk
);
935 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*)skb
->cb
;
938 if ((opt
->hop
&& np
->rxopt
.bits
.hopopts
) ||
939 ((IPV6_FLOWINFO_MASK
&*(u32
*)skb
->nh
.raw
) &&
940 np
->rxopt
.bits
.rxflow
) ||
941 (opt
->srcrt
&& np
->rxopt
.bits
.srcrt
) ||
942 ((opt
->dst1
|| opt
->dst0
) && np
->rxopt
.bits
.dstopts
))
949 static void tcp_v6_send_check(struct sock
*sk
, struct tcphdr
*th
, int len
,
952 struct ipv6_pinfo
*np
= inet6_sk(sk
);
954 if (skb
->ip_summed
== CHECKSUM_HW
) {
955 th
->check
= ~csum_ipv6_magic(&np
->saddr
, &np
->daddr
, len
, IPPROTO_TCP
, 0);
956 skb
->csum
= offsetof(struct tcphdr
, check
);
958 th
->check
= csum_ipv6_magic(&np
->saddr
, &np
->daddr
, len
, IPPROTO_TCP
,
959 csum_partial((char *)th
, th
->doff
<<2,
965 static void tcp_v6_send_reset(struct sk_buff
*skb
)
967 struct tcphdr
*th
= skb
->h
.th
, *t1
;
968 struct sk_buff
*buff
;
974 if (ipv6_addr_is_multicast(&skb
->nh
.ipv6h
->daddr
))
978 * We need to grab some memory, and put together an RST,
979 * and then put it into the queue to be sent.
982 buff
= alloc_skb(MAX_HEADER
+ sizeof(struct ipv6hdr
), GFP_ATOMIC
);
986 skb_reserve(buff
, MAX_HEADER
+ sizeof(struct ipv6hdr
));
988 t1
= (struct tcphdr
*) skb_push(buff
,sizeof(struct tcphdr
));
990 /* Swap the send and the receive. */
991 memset(t1
, 0, sizeof(*t1
));
992 t1
->dest
= th
->source
;
993 t1
->source
= th
->dest
;
994 t1
->doff
= sizeof(*t1
)/4;
998 t1
->seq
= th
->ack_seq
;
1001 t1
->ack_seq
= htonl(ntohl(th
->seq
) + th
->syn
+ th
->fin
1002 + skb
->len
- (th
->doff
<<2));
1005 buff
->csum
= csum_partial((char *)t1
, sizeof(*t1
), 0);
1007 memset(&fl
, 0, sizeof(fl
));
1008 ipv6_addr_copy(&fl
.fl6_dst
, &skb
->nh
.ipv6h
->saddr
);
1009 ipv6_addr_copy(&fl
.fl6_src
, &skb
->nh
.ipv6h
->daddr
);
1011 t1
->check
= csum_ipv6_magic(&fl
.fl6_src
, &fl
.fl6_dst
,
1012 sizeof(*t1
), IPPROTO_TCP
,
1015 fl
.proto
= IPPROTO_TCP
;
1016 fl
.oif
= tcp_v6_iif(skb
);
1017 fl
.fl_ip_dport
= t1
->dest
;
1018 fl
.fl_ip_sport
= t1
->source
;
1020 /* sk = NULL, but it is safe for now. RST socket required. */
1021 buff
->dst
= ip6_route_output(NULL
, &fl
);
1023 if (buff
->dst
->error
== 0) {
1024 ip6_xmit(NULL
, buff
, &fl
, NULL
, 0);
1025 TCP_INC_STATS_BH(TcpOutSegs
);
1026 TCP_INC_STATS_BH(TcpOutRsts
);
1033 static void tcp_v6_send_ack(struct sk_buff
*skb
, u32 seq
, u32 ack
, u32 win
, u32 ts
)
1035 struct tcphdr
*th
= skb
->h
.th
, *t1
;
1036 struct sk_buff
*buff
;
1038 int tot_len
= sizeof(struct tcphdr
);
1040 buff
= alloc_skb(MAX_HEADER
+ sizeof(struct ipv6hdr
), GFP_ATOMIC
);
1044 skb_reserve(buff
, MAX_HEADER
+ sizeof(struct ipv6hdr
));
1049 t1
= (struct tcphdr
*) skb_push(buff
,tot_len
);
1051 /* Swap the send and the receive. */
1052 memset(t1
, 0, sizeof(*t1
));
1053 t1
->dest
= th
->source
;
1054 t1
->source
= th
->dest
;
1055 t1
->doff
= tot_len
/4;
1056 t1
->seq
= htonl(seq
);
1057 t1
->ack_seq
= htonl(ack
);
1059 t1
->window
= htons(win
);
1062 u32
*ptr
= (u32
*)(t1
+ 1);
1063 *ptr
++ = htonl((TCPOPT_NOP
<< 24) | (TCPOPT_NOP
<< 16) |
1064 (TCPOPT_TIMESTAMP
<< 8) | TCPOLEN_TIMESTAMP
);
1065 *ptr
++ = htonl(tcp_time_stamp
);
1069 buff
->csum
= csum_partial((char *)t1
, tot_len
, 0);
1071 memset(&fl
, 0, sizeof(fl
));
1072 ipv6_addr_copy(&fl
.fl6_dst
, &skb
->nh
.ipv6h
->saddr
);
1073 ipv6_addr_copy(&fl
.fl6_src
, &skb
->nh
.ipv6h
->daddr
);
1075 t1
->check
= csum_ipv6_magic(&fl
.fl6_src
, &fl
.fl6_dst
,
1076 tot_len
, IPPROTO_TCP
,
1079 fl
.proto
= IPPROTO_TCP
;
1080 fl
.oif
= tcp_v6_iif(skb
);
1081 fl
.fl_ip_dport
= t1
->dest
;
1082 fl
.fl_ip_sport
= t1
->source
;
1084 buff
->dst
= ip6_route_output(NULL
, &fl
);
1086 if (buff
->dst
->error
== 0) {
1087 ip6_xmit(NULL
, buff
, &fl
, NULL
, 0);
1088 TCP_INC_STATS_BH(TcpOutSegs
);
1095 static void tcp_v6_timewait_ack(struct sock
*sk
, struct sk_buff
*skb
)
1097 struct tcp_tw_bucket
*tw
= (struct tcp_tw_bucket
*)sk
;
1099 tcp_v6_send_ack(skb
, tw
->tw_snd_nxt
, tw
->tw_rcv_nxt
,
1100 tw
->tw_rcv_wnd
>> tw
->tw_rcv_wscale
, tw
->tw_ts_recent
);
1105 static void tcp_v6_or_send_ack(struct sk_buff
*skb
, struct open_request
*req
)
1107 tcp_v6_send_ack(skb
, req
->snt_isn
+1, req
->rcv_isn
+1, req
->rcv_wnd
, req
->ts_recent
);
1111 static struct sock
*tcp_v6_hnd_req(struct sock
*sk
,struct sk_buff
*skb
)
1113 struct open_request
*req
, **prev
;
1114 struct tcphdr
*th
= skb
->h
.th
;
1115 struct tcp_opt
*tp
= tcp_sk(sk
);
1118 /* Find possible connection requests. */
1119 req
= tcp_v6_search_req(tp
, &prev
, th
->source
, &skb
->nh
.ipv6h
->saddr
,
1120 &skb
->nh
.ipv6h
->daddr
, tcp_v6_iif(skb
));
1122 return tcp_check_req(sk
, skb
, req
, prev
);
1124 nsk
= __tcp_v6_lookup_established(&skb
->nh
.ipv6h
->saddr
,
1126 &skb
->nh
.ipv6h
->daddr
,
1131 if (nsk
->sk_state
!= TCP_TIME_WAIT
) {
1135 tcp_tw_put((struct tcp_tw_bucket
*)nsk
);
1139 #if 0 /*def CONFIG_SYN_COOKIES*/
1140 if (!th
->rst
&& !th
->syn
&& th
->ack
)
1141 sk
= cookie_v6_check(sk
, skb
, &(IPCB(skb
)->opt
));
1146 static void tcp_v6_synq_add(struct sock
*sk
, struct open_request
*req
)
1148 struct tcp_opt
*tp
= tcp_sk(sk
);
1149 struct tcp_listen_opt
*lopt
= tp
->listen_opt
;
1150 u32 h
= tcp_v6_synq_hash(&req
->af
.v6_req
.rmt_addr
, req
->rmt_port
, lopt
->hash_rnd
);
1153 req
->expires
= jiffies
+ TCP_TIMEOUT_INIT
;
1155 req
->dl_next
= lopt
->syn_table
[h
];
1157 write_lock(&tp
->syn_wait_lock
);
1158 lopt
->syn_table
[h
] = req
;
1159 write_unlock(&tp
->syn_wait_lock
);
1165 /* FIXME: this is substantially similar to the ipv4 code.
1166 * Can some kind of merge be done? -- erics
1168 static int tcp_v6_conn_request(struct sock
*sk
, struct sk_buff
*skb
)
1170 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1171 struct tcp_opt tmptp
, *tp
= tcp_sk(sk
);
1172 struct open_request
*req
= NULL
;
1173 __u32 isn
= TCP_SKB_CB(skb
)->when
;
1175 if (skb
->protocol
== htons(ETH_P_IP
))
1176 return tcp_v4_conn_request(sk
, skb
);
1178 /* FIXME: do the same check for anycast */
1179 if (ipv6_addr_is_multicast(&skb
->nh
.ipv6h
->daddr
))
1183 * There are no SYN attacks on IPv6, yet...
1185 if (tcp_synq_is_full(sk
) && !isn
) {
1186 if (net_ratelimit())
1187 printk(KERN_INFO
"TCPv6: dropping request, synflood is possible\n");
1191 if (tcp_acceptq_is_full(sk
) && tcp_synq_young(sk
) > 1)
1194 req
= tcp_openreq_alloc();
1198 tcp_clear_options(&tmptp
);
1199 tmptp
.mss_clamp
= IPV6_MIN_MTU
- sizeof(struct tcphdr
) - sizeof(struct ipv6hdr
);
1200 tmptp
.user_mss
= tp
->user_mss
;
1202 tcp_parse_options(skb
, &tmptp
, 0);
1204 tmptp
.tstamp_ok
= tmptp
.saw_tstamp
;
1205 tcp_openreq_init(req
, &tmptp
, skb
);
1207 req
->class = &or_ipv6
;
1208 ipv6_addr_copy(&req
->af
.v6_req
.rmt_addr
, &skb
->nh
.ipv6h
->saddr
);
1209 ipv6_addr_copy(&req
->af
.v6_req
.loc_addr
, &skb
->nh
.ipv6h
->daddr
);
1210 TCP_ECN_create_request(req
, skb
->h
.th
);
1211 req
->af
.v6_req
.pktopts
= NULL
;
1212 if (ipv6_opt_accepted(sk
, skb
) ||
1213 np
->rxopt
.bits
.rxinfo
||
1214 np
->rxopt
.bits
.rxhlim
) {
1215 atomic_inc(&skb
->users
);
1216 req
->af
.v6_req
.pktopts
= skb
;
1218 req
->af
.v6_req
.iif
= sk
->sk_bound_dev_if
;
1220 /* So that link locals have meaning */
1221 if (!sk
->sk_bound_dev_if
&&
1222 ipv6_addr_type(&req
->af
.v6_req
.rmt_addr
) & IPV6_ADDR_LINKLOCAL
)
1223 req
->af
.v6_req
.iif
= tcp_v6_iif(skb
);
1226 isn
= tcp_v6_init_sequence(sk
,skb
);
1230 if (tcp_v6_send_synack(sk
, req
, NULL
))
1233 tcp_v6_synq_add(sk
, req
);
1239 tcp_openreq_free(req
);
1241 TCP_INC_STATS_BH(TcpAttemptFails
);
1242 return 0; /* don't send reset */
1245 static struct sock
* tcp_v6_syn_recv_sock(struct sock
*sk
, struct sk_buff
*skb
,
1246 struct open_request
*req
,
1247 struct dst_entry
*dst
)
1249 struct ipv6_pinfo
*newnp
, *np
= inet6_sk(sk
);
1250 struct tcp6_sock
*newtcp6sk
;
1251 struct inet_opt
*newinet
;
1252 struct tcp_opt
*newtp
;
1254 struct ipv6_txoptions
*opt
;
1256 if (skb
->protocol
== htons(ETH_P_IP
)) {
1261 newsk
= tcp_v4_syn_recv_sock(sk
, skb
, req
, dst
);
1266 newtcp6sk
= (struct tcp6_sock
*)newsk
;
1267 newtcp6sk
->pinet6
= &newtcp6sk
->inet6
;
1269 newinet
= inet_sk(newsk
);
1270 newnp
= inet6_sk(newsk
);
1271 newtp
= tcp_sk(newsk
);
1273 memcpy(newnp
, np
, sizeof(struct ipv6_pinfo
));
1275 ipv6_addr_set(&newnp
->daddr
, 0, 0, htonl(0x0000FFFF),
1278 ipv6_addr_set(&newnp
->saddr
, 0, 0, htonl(0x0000FFFF),
1281 ipv6_addr_copy(&newnp
->rcv_saddr
, &newnp
->saddr
);
1283 newtp
->af_specific
= &ipv6_mapped
;
1284 newsk
->sk_backlog_rcv
= tcp_v4_do_rcv
;
1285 newnp
->pktoptions
= NULL
;
1287 newnp
->mcast_oif
= tcp_v6_iif(skb
);
1288 newnp
->mcast_hops
= skb
->nh
.ipv6h
->hop_limit
;
1290 /* Charge newly allocated IPv6 socket. Though it is mapped,
1293 #ifdef INET_REFCNT_DEBUG
1294 atomic_inc(&inet6_sock_nr
);
1297 /* It is tricky place. Until this moment IPv4 tcp
1298 worked with IPv6 af_tcp.af_specific.
1301 tcp_sync_mss(newsk
, newtp
->pmtu_cookie
);
1308 if (tcp_acceptq_is_full(sk
))
1311 if (np
->rxopt
.bits
.srcrt
== 2 &&
1312 opt
== NULL
&& req
->af
.v6_req
.pktopts
) {
1313 struct inet6_skb_parm
*rxopt
= (struct inet6_skb_parm
*)req
->af
.v6_req
.pktopts
->cb
;
1315 opt
= ipv6_invert_rthdr(sk
, (struct ipv6_rt_hdr
*)(req
->af
.v6_req
.pktopts
->nh
.raw
+rxopt
->srcrt
));
1321 memset(&fl
, 0, sizeof(fl
));
1322 fl
.proto
= IPPROTO_TCP
;
1323 ipv6_addr_copy(&fl
.fl6_dst
, &req
->af
.v6_req
.rmt_addr
);
1324 if (opt
&& opt
->srcrt
) {
1325 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
1326 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
1328 ipv6_addr_copy(&fl
.fl6_src
, &req
->af
.v6_req
.loc_addr
);
1329 fl
.oif
= sk
->sk_bound_dev_if
;
1330 fl
.fl_ip_dport
= req
->rmt_port
;
1331 fl
.fl_ip_sport
= inet_sk(sk
)->sport
;
1333 dst
= ip6_route_output(sk
, &fl
);
1339 newsk
= tcp_create_openreq_child(sk
, req
, skb
);
1343 /* Charge newly allocated IPv6 socket */
1344 #ifdef INET_REFCNT_DEBUG
1345 atomic_inc(&inet6_sock_nr
);
1348 ip6_dst_store(newsk
, dst
, NULL
);
1349 sk
->sk_route_caps
= dst
->dev
->features
&
1350 ~(NETIF_F_IP_CSUM
| NETIF_F_TSO
);
1352 newtcp6sk
= (struct tcp6_sock
*)newsk
;
1353 newtcp6sk
->pinet6
= &newtcp6sk
->inet6
;
1355 newtp
= tcp_sk(newsk
);
1356 newinet
= inet_sk(newsk
);
1357 newnp
= inet6_sk(newsk
);
1359 memcpy(newnp
, np
, sizeof(struct ipv6_pinfo
));
1361 ipv6_addr_copy(&newnp
->daddr
, &req
->af
.v6_req
.rmt_addr
);
1362 ipv6_addr_copy(&newnp
->saddr
, &req
->af
.v6_req
.loc_addr
);
1363 ipv6_addr_copy(&newnp
->rcv_saddr
, &req
->af
.v6_req
.loc_addr
);
1364 newsk
->sk_bound_dev_if
= req
->af
.v6_req
.iif
;
1366 /* Now IPv6 options...
1368 First: no IPv4 options.
1370 newinet
->opt
= NULL
;
1373 newnp
->rxopt
.all
= np
->rxopt
.all
;
1375 /* Clone pktoptions received with SYN */
1376 newnp
->pktoptions
= NULL
;
1377 if (req
->af
.v6_req
.pktopts
) {
1378 newnp
->pktoptions
= skb_clone(req
->af
.v6_req
.pktopts
,
1380 kfree_skb(req
->af
.v6_req
.pktopts
);
1381 req
->af
.v6_req
.pktopts
= NULL
;
1382 if (newnp
->pktoptions
)
1383 skb_set_owner_r(newnp
->pktoptions
, newsk
);
1386 newnp
->mcast_oif
= tcp_v6_iif(skb
);
1387 newnp
->mcast_hops
= skb
->nh
.ipv6h
->hop_limit
;
1389 /* Clone native IPv6 options from listening socket (if any)
1391 Yes, keeping reference count would be much more clever,
1392 but we make one more one thing there: reattach optmem
1396 newnp
->opt
= ipv6_dup_options(newsk
, opt
);
1398 sock_kfree_s(sk
, opt
, opt
->tot_len
);
1401 newtp
->ext_header_len
= 0;
1403 newtp
->ext_header_len
= newnp
->opt
->opt_nflen
+
1404 newnp
->opt
->opt_flen
;
1406 tcp_sync_mss(newsk
, dst_pmtu(dst
));
1407 newtp
->advmss
= dst_metric(dst
, RTAX_ADVMSS
);
1408 tcp_initialize_rcv_mss(newsk
);
1410 newinet
->daddr
= newinet
->saddr
= newinet
->rcv_saddr
= LOOPBACK4_IPV6
;
1412 __tcp_v6_hash(newsk
);
1413 tcp_inherit_port(sk
, newsk
);
1418 NET_INC_STATS_BH(ListenOverflows
);
1420 NET_INC_STATS_BH(ListenDrops
);
1421 if (opt
&& opt
!= np
->opt
)
1422 sock_kfree_s(sk
, opt
, opt
->tot_len
);
1427 static int tcp_v6_checksum_init(struct sk_buff
*skb
)
1429 if (skb
->ip_summed
== CHECKSUM_HW
) {
1430 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1431 if (!tcp_v6_check(skb
->h
.th
,skb
->len
,&skb
->nh
.ipv6h
->saddr
,
1432 &skb
->nh
.ipv6h
->daddr
,skb
->csum
))
1434 NETDEBUG(if (net_ratelimit()) printk(KERN_DEBUG
"hw tcp v6 csum failed\n"));
1436 if (skb
->len
<= 76) {
1437 if (tcp_v6_check(skb
->h
.th
,skb
->len
,&skb
->nh
.ipv6h
->saddr
,
1438 &skb
->nh
.ipv6h
->daddr
,skb_checksum(skb
, 0, skb
->len
, 0)))
1440 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
1442 skb
->csum
= ~tcp_v6_check(skb
->h
.th
,skb
->len
,&skb
->nh
.ipv6h
->saddr
,
1443 &skb
->nh
.ipv6h
->daddr
,0);
1448 /* The socket must have it's spinlock held when we get
1451 * We have a potential double-lock case here, so even when
1452 * doing backlog processing we use the BH locking scheme.
1453 * This is because we cannot sleep with the original spinlock
1456 static int tcp_v6_do_rcv(struct sock
*sk
, struct sk_buff
*skb
)
1458 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1460 struct sk_buff
*opt_skb
= NULL
;
1462 /* Imagine: socket is IPv6. IPv4 packet arrives,
1463 goes to IPv4 receive handler and backlogged.
1464 From backlog it always goes here. Kerboom...
1465 Fortunately, tcp_rcv_established and rcv_established
1466 handle them correctly, but it is not case with
1467 tcp_v6_hnd_req and tcp_v6_send_reset(). --ANK
1470 if (skb
->protocol
== htons(ETH_P_IP
))
1471 return tcp_v4_do_rcv(sk
, skb
);
1473 if (sk_filter(sk
, skb
, 0))
1477 * socket locking is here for SMP purposes as backlog rcv
1478 * is currently called with bh processing disabled.
1481 /* Do Stevens' IPV6_PKTOPTIONS.
1483 Yes, guys, it is the only place in our code, where we
1484 may make it not affecting IPv4.
1485 The rest of code is protocol independent,
1486 and I do not like idea to uglify IPv4.
1488 Actually, all the idea behind IPV6_PKTOPTIONS
1489 looks not very well thought. For now we latch
1490 options, received in the last packet, enqueued
1491 by tcp. Feel free to propose better solution.
1495 opt_skb
= skb_clone(skb
, GFP_ATOMIC
);
1497 if (sk
->sk_state
== TCP_ESTABLISHED
) { /* Fast path */
1498 TCP_CHECK_TIMER(sk
);
1499 if (tcp_rcv_established(sk
, skb
, skb
->h
.th
, skb
->len
))
1501 TCP_CHECK_TIMER(sk
);
1503 goto ipv6_pktoptions
;
1507 if (skb
->len
< (skb
->h
.th
->doff
<<2) || tcp_checksum_complete(skb
))
1510 if (sk
->sk_state
== TCP_LISTEN
) {
1511 struct sock
*nsk
= tcp_v6_hnd_req(sk
, skb
);
1516 * Queue it on the new socket if the new socket is active,
1517 * otherwise we just shortcircuit this and continue with
1521 if (tcp_child_process(sk
, nsk
, skb
))
1524 __kfree_skb(opt_skb
);
1529 TCP_CHECK_TIMER(sk
);
1530 if (tcp_rcv_state_process(sk
, skb
, skb
->h
.th
, skb
->len
))
1532 TCP_CHECK_TIMER(sk
);
1534 goto ipv6_pktoptions
;
1538 tcp_v6_send_reset(skb
);
1541 __kfree_skb(opt_skb
);
1545 TCP_INC_STATS_BH(TcpInErrs
);
1550 /* Do you ask, what is it?
1552 1. skb was enqueued by tcp.
1553 2. skb is added to tail of read queue, rather than out of order.
1554 3. socket is not in passive state.
1555 4. Finally, it really contains options, which user wants to receive.
1558 if (TCP_SKB_CB(opt_skb
)->end_seq
== tp
->rcv_nxt
&&
1559 !((1 << sk
->sk_state
) & (TCPF_CLOSE
| TCPF_LISTEN
))) {
1560 if (np
->rxopt
.bits
.rxinfo
)
1561 np
->mcast_oif
= tcp_v6_iif(opt_skb
);
1562 if (np
->rxopt
.bits
.rxhlim
)
1563 np
->mcast_hops
= opt_skb
->nh
.ipv6h
->hop_limit
;
1564 if (ipv6_opt_accepted(sk
, opt_skb
)) {
1565 skb_set_owner_r(opt_skb
, sk
);
1566 opt_skb
= xchg(&np
->pktoptions
, opt_skb
);
1568 __kfree_skb(opt_skb
);
1569 opt_skb
= xchg(&np
->pktoptions
, NULL
);
1578 static int tcp_v6_rcv(struct sk_buff
**pskb
, unsigned int *nhoffp
)
1580 struct sk_buff
*skb
= *pskb
;
1585 if (skb
->pkt_type
!= PACKET_HOST
)
1589 * Count it even if it's bad.
1591 TCP_INC_STATS_BH(TcpInSegs
);
1593 if (!pskb_may_pull(skb
, sizeof(struct tcphdr
)))
1598 if (th
->doff
< sizeof(struct tcphdr
)/4)
1600 if (!pskb_may_pull(skb
, th
->doff
*4))
1603 if ((skb
->ip_summed
!= CHECKSUM_UNNECESSARY
&&
1604 tcp_v6_checksum_init(skb
) < 0))
1608 TCP_SKB_CB(skb
)->seq
= ntohl(th
->seq
);
1609 TCP_SKB_CB(skb
)->end_seq
= (TCP_SKB_CB(skb
)->seq
+ th
->syn
+ th
->fin
+
1610 skb
->len
- th
->doff
*4);
1611 TCP_SKB_CB(skb
)->ack_seq
= ntohl(th
->ack_seq
);
1612 TCP_SKB_CB(skb
)->when
= 0;
1613 TCP_SKB_CB(skb
)->flags
= ip6_get_dsfield(skb
->nh
.ipv6h
);
1614 TCP_SKB_CB(skb
)->sacked
= 0;
1616 sk
= __tcp_v6_lookup(&skb
->nh
.ipv6h
->saddr
, th
->source
,
1617 &skb
->nh
.ipv6h
->daddr
, ntohs(th
->dest
), tcp_v6_iif(skb
));
1623 if (sk
->sk_state
== TCP_TIME_WAIT
)
1626 if (!xfrm6_policy_check(sk
, XFRM_POLICY_IN
, skb
))
1627 goto discard_and_relse
;
1629 if (sk_filter(sk
, skb
, 0))
1630 goto discard_and_relse
;
1636 if (!sock_owned_by_user(sk
)) {
1637 if (!tcp_prequeue(sk
, skb
))
1638 ret
= tcp_v6_do_rcv(sk
, skb
);
1640 sk_add_backlog(sk
, skb
);
1644 return ret
? -1 : 0;
1647 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1648 goto discard_and_relse
;
1650 if (skb
->len
< (th
->doff
<<2) || tcp_checksum_complete(skb
)) {
1652 TCP_INC_STATS_BH(TcpInErrs
);
1654 tcp_v6_send_reset(skb
);
1671 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
1672 goto discard_and_relse
;
1674 if (skb
->len
< (th
->doff
<<2) || tcp_checksum_complete(skb
)) {
1675 TCP_INC_STATS_BH(TcpInErrs
);
1680 switch(tcp_timewait_state_process((struct tcp_tw_bucket
*)sk
,
1681 skb
, th
, skb
->len
)) {
1686 sk2
= tcp_v6_lookup_listener(&skb
->nh
.ipv6h
->daddr
, ntohs(th
->dest
), tcp_v6_iif(skb
));
1688 tcp_tw_deschedule((struct tcp_tw_bucket
*)sk
);
1689 tcp_tw_put((struct tcp_tw_bucket
*)sk
);
1693 /* Fall through to ACK */
1696 tcp_v6_timewait_ack(sk
, skb
);
1700 case TCP_TW_SUCCESS
:;
1705 static int tcp_v6_rebuild_header(struct sock
*sk
)
1708 struct dst_entry
*dst
;
1709 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1711 dst
= __sk_dst_check(sk
, np
->dst_cookie
);
1714 struct inet_opt
*inet
= inet_sk(sk
);
1717 memset(&fl
, 0, sizeof(fl
));
1718 fl
.proto
= IPPROTO_TCP
;
1719 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
1720 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
1721 fl
.fl6_flowlabel
= np
->flow_label
;
1722 fl
.oif
= sk
->sk_bound_dev_if
;
1723 fl
.fl_ip_dport
= inet
->dport
;
1724 fl
.fl_ip_sport
= inet
->sport
;
1726 if (np
->opt
&& np
->opt
->srcrt
) {
1727 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) np
->opt
->srcrt
;
1728 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
1731 dst
= ip6_route_output(sk
, &fl
);
1736 sk
->sk_route_caps
= 0;
1740 ip6_dst_store(sk
, dst
, NULL
);
1741 sk
->sk_route_caps
= dst
->dev
->features
&
1742 ~(NETIF_F_IP_CSUM
| NETIF_F_TSO
);
1748 static int tcp_v6_xmit(struct sk_buff
*skb
, int ipfragok
)
1750 struct sock
*sk
= skb
->sk
;
1751 struct inet_opt
*inet
= inet_sk(sk
);
1752 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1754 struct dst_entry
*dst
;
1756 memset(&fl
, 0, sizeof(fl
));
1757 fl
.proto
= IPPROTO_TCP
;
1758 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
1759 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
1760 fl
.fl6_flowlabel
= np
->flow_label
;
1761 IP6_ECN_flow_xmit(sk
, fl
.fl6_flowlabel
);
1762 fl
.oif
= sk
->sk_bound_dev_if
;
1763 fl
.fl_ip_sport
= inet
->sport
;
1764 fl
.fl_ip_dport
= inet
->dport
;
1766 if (np
->opt
&& np
->opt
->srcrt
) {
1767 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) np
->opt
->srcrt
;
1768 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
1771 dst
= __sk_dst_check(sk
, np
->dst_cookie
);
1774 dst
= ip6_route_output(sk
, &fl
);
1777 sk
->sk_err_soft
= -dst
->error
;
1779 return -sk
->sk_err_soft
;
1782 ip6_dst_store(sk
, dst
, NULL
);
1785 skb
->dst
= dst_clone(dst
);
1787 /* Restore final destination back after routing done */
1788 ipv6_addr_copy(&fl
.fl6_dst
, &np
->daddr
);
1790 return ip6_xmit(sk
, skb
, &fl
, np
->opt
, 0);
1793 static void v6_addr2sockaddr(struct sock
*sk
, struct sockaddr
* uaddr
)
1795 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1796 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) uaddr
;
1798 sin6
->sin6_family
= AF_INET6
;
1799 ipv6_addr_copy(&sin6
->sin6_addr
, &np
->daddr
);
1800 sin6
->sin6_port
= inet_sk(sk
)->dport
;
1801 /* We do not store received flowlabel for TCP */
1802 sin6
->sin6_flowinfo
= 0;
1803 sin6
->sin6_scope_id
= 0;
1804 if (sk
->sk_bound_dev_if
&&
1805 ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
1806 sin6
->sin6_scope_id
= sk
->sk_bound_dev_if
;
1809 static int tcp_v6_remember_stamp(struct sock
*sk
)
1811 /* Alas, not yet... */
1815 static struct tcp_func ipv6_specific
= {
1816 .queue_xmit
= tcp_v6_xmit
,
1817 .send_check
= tcp_v6_send_check
,
1818 .rebuild_header
= tcp_v6_rebuild_header
,
1819 .conn_request
= tcp_v6_conn_request
,
1820 .syn_recv_sock
= tcp_v6_syn_recv_sock
,
1821 .remember_stamp
= tcp_v6_remember_stamp
,
1822 .net_header_len
= sizeof(struct ipv6hdr
),
1824 .setsockopt
= ipv6_setsockopt
,
1825 .getsockopt
= ipv6_getsockopt
,
1826 .addr2sockaddr
= v6_addr2sockaddr
,
1827 .sockaddr_len
= sizeof(struct sockaddr_in6
)
1831 * TCP over IPv4 via INET6 API
1834 static struct tcp_func ipv6_mapped
= {
1835 .queue_xmit
= ip_queue_xmit
,
1836 .send_check
= tcp_v4_send_check
,
1837 .rebuild_header
= tcp_v4_rebuild_header
,
1838 .conn_request
= tcp_v6_conn_request
,
1839 .syn_recv_sock
= tcp_v6_syn_recv_sock
,
1840 .remember_stamp
= tcp_v4_remember_stamp
,
1841 .net_header_len
= sizeof(struct iphdr
),
1843 .setsockopt
= ipv6_setsockopt
,
1844 .getsockopt
= ipv6_getsockopt
,
1845 .addr2sockaddr
= v6_addr2sockaddr
,
1846 .sockaddr_len
= sizeof(struct sockaddr_in6
)
1851 /* NOTE: A lot of things set to zero explicitly by call to
1852 * sk_alloc() so need not be done here.
1854 static int tcp_v6_init_sock(struct sock
*sk
)
1856 struct tcp_opt
*tp
= tcp_sk(sk
);
1858 skb_queue_head_init(&tp
->out_of_order_queue
);
1859 tcp_init_xmit_timers(sk
);
1860 tcp_prequeue_init(tp
);
1862 tp
->rto
= TCP_TIMEOUT_INIT
;
1863 tp
->mdev
= TCP_TIMEOUT_INIT
;
1865 /* So many TCP implementations out there (incorrectly) count the
1866 * initial SYN frame in their delayed-ACK and congestion control
1867 * algorithms that we must have the following bandaid to talk
1868 * efficiently to them. -DaveM
1872 /* See draft-stevens-tcpca-spec-01 for discussion of the
1873 * initialization of these values.
1875 tp
->snd_ssthresh
= 0x7fffffff;
1876 tp
->snd_cwnd_clamp
= ~0;
1877 tp
->mss_cache
= 536;
1879 tp
->reordering
= sysctl_tcp_reordering
;
1881 sk
->sk_state
= TCP_CLOSE
;
1883 tp
->af_specific
= &ipv6_specific
;
1885 sk
->sk_write_space
= tcp_write_space
;
1886 sk
->sk_use_write_queue
= 1;
1888 sk
->sk_sndbuf
= sysctl_tcp_wmem
[1];
1889 sk
->sk_rcvbuf
= sysctl_tcp_rmem
[1];
1891 atomic_inc(&tcp_sockets_allocated
);
1896 static int tcp_v6_destroy_sock(struct sock
*sk
)
1898 struct tcp_opt
*tp
= tcp_sk(sk
);
1899 struct inet_opt
*inet
= inet_sk(sk
);
1901 tcp_clear_xmit_timers(sk
);
1903 /* Cleanup up the write buffer. */
1904 tcp_writequeue_purge(sk
);
1906 /* Cleans up our, hopefully empty, out_of_order_queue. */
1907 __skb_queue_purge(&tp
->out_of_order_queue
);
1909 /* Clean prequeue, it must be empty really */
1910 __skb_queue_purge(&tp
->ucopy
.prequeue
);
1912 /* Clean up a referenced TCP bind bucket. */
1913 if (tcp_sk(sk
)->bind_hash
)
1916 /* If sendmsg cached page exists, toss it. */
1917 if (inet
->sndmsg_page
!= NULL
)
1918 __free_page(inet
->sndmsg_page
);
1920 atomic_dec(&tcp_sockets_allocated
);
1922 return inet6_destroy_sock(sk
);
1925 /* Proc filesystem TCPv6 sock list dumping. */
1926 static void get_openreq6(struct seq_file
*seq
,
1927 struct sock
*sk
, struct open_request
*req
, int i
, int uid
)
1929 struct in6_addr
*dest
, *src
;
1930 int ttd
= req
->expires
- jiffies
;
1935 src
= &req
->af
.v6_req
.loc_addr
;
1936 dest
= &req
->af
.v6_req
.rmt_addr
;
1938 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1939 "%02X %08X:%08X %02X:%08X %08X %5d %8d %d %d %p\n",
1941 src
->s6_addr32
[0], src
->s6_addr32
[1],
1942 src
->s6_addr32
[2], src
->s6_addr32
[3],
1943 ntohs(inet_sk(sk
)->sport
),
1944 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1945 dest
->s6_addr32
[2], dest
->s6_addr32
[3],
1946 ntohs(req
->rmt_port
),
1948 0,0, /* could print option size, but that is af dependent. */
1949 1, /* timers active (only the expire timer) */
1953 0, /* non standard timer */
1954 0, /* open_requests have no inode */
1958 static void get_tcp6_sock(struct seq_file
*seq
, struct sock
*sp
, int i
)
1960 struct in6_addr
*dest
, *src
;
1963 unsigned long timer_expires
;
1964 struct inet_opt
*inet
= inet_sk(sp
);
1965 struct tcp_opt
*tp
= tcp_sk(sp
);
1966 struct ipv6_pinfo
*np
= inet6_sk(sp
);
1969 src
= &np
->rcv_saddr
;
1970 destp
= ntohs(inet
->dport
);
1971 srcp
= ntohs(inet
->sport
);
1972 if (tp
->pending
== TCP_TIME_RETRANS
) {
1974 timer_expires
= tp
->timeout
;
1975 } else if (tp
->pending
== TCP_TIME_PROBE0
) {
1977 timer_expires
= tp
->timeout
;
1978 } else if (timer_pending(&sp
->sk_timer
)) {
1980 timer_expires
= sp
->sk_timer
.expires
;
1983 timer_expires
= jiffies
;
1987 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1988 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %u %u %u %u %d\n",
1990 src
->s6_addr32
[0], src
->s6_addr32
[1],
1991 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1992 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1993 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1995 tp
->write_seq
-tp
->snd_una
, tp
->rcv_nxt
-tp
->copied_seq
,
1996 timer_active
, timer_expires
-jiffies
,
2001 atomic_read(&sp
->sk_refcnt
), sp
,
2002 tp
->rto
, tp
->ack
.ato
, (tp
->ack
.quick
<<1)|tp
->ack
.pingpong
,
2003 tp
->snd_cwnd
, tp
->snd_ssthresh
>=0xFFFF?-1:tp
->snd_ssthresh
2007 static void get_timewait6_sock(struct seq_file
*seq
,
2008 struct tcp_tw_bucket
*tw
, int i
)
2010 struct in6_addr
*dest
, *src
;
2012 int ttd
= tw
->tw_ttd
- jiffies
;
2017 dest
= &tw
->tw_v6_daddr
;
2018 src
= &tw
->tw_v6_rcv_saddr
;
2019 destp
= ntohs(tw
->tw_dport
);
2020 srcp
= ntohs(tw
->tw_sport
);
2023 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2024 "%02X %08X:%08X %02X:%08X %08X %5d %8d %d %d %p\n",
2026 src
->s6_addr32
[0], src
->s6_addr32
[1],
2027 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
2028 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
2029 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
2030 tw
->tw_substate
, 0, 0,
2032 atomic_read(&tw
->tw_refcnt
), tw
);
2035 static int tcp6_seq_show(struct seq_file
*seq
, void *v
)
2037 struct tcp_iter_state
*st
;
2039 if (v
== (void *)1) {
2044 "st tx_queue rx_queue tr tm->when retrnsmt"
2045 " uid timeout inode\n");
2050 switch (st
->state
) {
2051 case TCP_SEQ_STATE_LISTENING
:
2052 case TCP_SEQ_STATE_ESTABLISHED
:
2053 get_tcp6_sock(seq
, v
, st
->num
);
2055 case TCP_SEQ_STATE_OPENREQ
:
2056 get_openreq6(seq
, st
->syn_wait_sk
, v
, st
->num
, st
->uid
);
2058 case TCP_SEQ_STATE_TIME_WAIT
:
2059 get_timewait6_sock(seq
, v
, st
->num
);
2066 static struct file_operations tcp6_seq_fops
;
2067 static struct tcp_seq_afinfo tcp6_seq_afinfo
= {
2068 .owner
= THIS_MODULE
,
2071 .seq_show
= tcp6_seq_show
,
2072 .seq_fops
= &tcp6_seq_fops
,
2075 int __init
tcp6_proc_init(void)
2077 return tcp_proc_register(&tcp6_seq_afinfo
);
2080 void tcp6_proc_exit(void)
2082 tcp_proc_unregister(&tcp6_seq_afinfo
);
2085 struct proto tcpv6_prot
= {
2088 .connect
= tcp_v6_connect
,
2089 .disconnect
= tcp_disconnect
,
2090 .accept
= tcp_accept
,
2092 .init
= tcp_v6_init_sock
,
2093 .destroy
= tcp_v6_destroy_sock
,
2094 .shutdown
= tcp_shutdown
,
2095 .setsockopt
= tcp_setsockopt
,
2096 .getsockopt
= tcp_getsockopt
,
2097 .sendmsg
= tcp_sendmsg
,
2098 .recvmsg
= tcp_recvmsg
,
2099 .backlog_rcv
= tcp_v6_do_rcv
,
2100 .hash
= tcp_v6_hash
,
2101 .unhash
= tcp_unhash
,
2102 .get_port
= tcp_v6_get_port
,
2105 static struct inet6_protocol tcpv6_protocol
= {
2106 .handler
= tcp_v6_rcv
,
2107 .err_handler
= tcp_v6_err
,
2108 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
2111 extern struct proto_ops inet6_stream_ops
;
2113 static struct inet_protosw tcpv6_protosw
= {
2114 .type
= SOCK_STREAM
,
2115 .protocol
= IPPROTO_TCP
,
2116 .prot
= &tcpv6_prot
,
2117 .ops
= &inet6_stream_ops
,
2120 .flags
= INET_PROTOSW_PERMANENT
,
2123 void __init
tcpv6_init(void)
2125 /* register inet6 protocol */
2126 if (inet6_add_protocol(&tcpv6_protocol
, IPPROTO_TCP
) < 0)
2127 printk(KERN_ERR
"tcpv6_init: Could not register protocol\n");
2128 inet6_register_protosw(&tcpv6_protosw
);