Merge to Fedora kernel-2.6.7-1.492
[linux-2.6.git] / net / ipv6 / tcp_ipv6.c
1 /*
2  *      TCP over IPv6
3  *      Linux INET6 implementation 
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>     
7  *
8  *      $Id: tcp_ipv6.c,v 1.144 2002/02/01 22:01:04 davem Exp $
9  *
10  *      Based on: 
11  *      linux/net/ipv4/tcp.c
12  *      linux/net/ipv4/tcp_input.c
13  *      linux/net/ipv4/tcp_output.c
14  *
15  *      Fixes:
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.
21  *
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.
26  */
27
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>
36 #include <linux/in.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>
42 #include <linux/times.h>
43
44 #include <linux/ipv6.h>
45 #include <linux/icmpv6.h>
46 #include <linux/random.h>
47
48 #include <net/tcp.h>
49 #include <net/ndisc.h>
50 #include <net/ipv6.h>
51 #include <net/transp_v6.h>
52 #include <net/addrconf.h>
53 #include <net/ip6_route.h>
54 #include <net/ip6_checksum.h>
55 #include <net/inet_ecn.h>
56 #include <net/protocol.h>
57 #include <net/xfrm.h>
58
59 #include <asm/uaccess.h>
60
61 #include <linux/proc_fs.h>
62 #include <linux/seq_file.h>
63
64 static void     tcp_v6_send_reset(struct sk_buff *skb);
65 static void     tcp_v6_or_send_ack(struct sk_buff *skb, struct open_request *req);
66 static void     tcp_v6_send_check(struct sock *sk, struct tcphdr *th, int len, 
67                                   struct sk_buff *skb);
68
69 static int      tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
70 static int      tcp_v6_xmit(struct sk_buff *skb, int ipfragok);
71
72 static struct tcp_func ipv6_mapped;
73 static struct tcp_func ipv6_specific;
74
75 /* I have no idea if this is a good hash for v6 or not. -DaveM */
76 static __inline__ int tcp_v6_hashfn(struct in6_addr *laddr, u16 lport,
77                                     struct in6_addr *faddr, u16 fport)
78 {
79         int hashent = (lport ^ fport);
80
81         hashent ^= (laddr->s6_addr32[3] ^ faddr->s6_addr32[3]);
82         hashent ^= hashent>>16;
83         hashent ^= hashent>>8;
84         return (hashent & (tcp_ehash_size - 1));
85 }
86
87 static __inline__ int tcp_v6_sk_hashfn(struct sock *sk)
88 {
89         struct inet_opt *inet = inet_sk(sk);
90         struct ipv6_pinfo *np = inet6_sk(sk);
91         struct in6_addr *laddr = &np->rcv_saddr;
92         struct in6_addr *faddr = &np->daddr;
93         __u16 lport = inet->num;
94         __u16 fport = inet->dport;
95         return tcp_v6_hashfn(laddr, lport, faddr, fport);
96 }
97
98 static inline int tcp_v6_bind_conflict(struct sock *sk,
99                                        struct tcp_bind_bucket *tb)
100 {
101         struct sock *sk2;
102         struct hlist_node *node;
103
104         /* We must walk the whole port owner list in this case. -DaveM */
105         sk_for_each_bound(sk2, node, &tb->owners) {
106                 if (sk != sk2 &&
107                     (!sk->sk_bound_dev_if ||
108                      !sk2->sk_bound_dev_if ||
109                      sk->sk_bound_dev_if == sk2->sk_bound_dev_if) &&
110                     (!sk->sk_reuse || !sk2->sk_reuse ||
111                      sk2->sk_state == TCP_LISTEN) &&
112                      ipv6_rcv_saddr_equal(sk, sk2))
113                         break;
114         }
115
116         return node != NULL;
117 }
118
119 /* Grrr, addr_type already calculated by caller, but I don't want
120  * to add some silly "cookie" argument to this method just for that.
121  * But it doesn't matter, the recalculation is in the rarest path
122  * this function ever takes.
123  */
124 static int tcp_v6_get_port(struct sock *sk, unsigned short snum)
125 {
126         struct tcp_bind_hashbucket *head;
127         struct tcp_bind_bucket *tb;
128         struct hlist_node *node;
129         int ret;
130
131         local_bh_disable();
132         if (snum == 0) {
133                 int low = sysctl_local_port_range[0];
134                 int high = sysctl_local_port_range[1];
135                 int remaining = (high - low) + 1;
136                 int rover;
137
138                 spin_lock(&tcp_portalloc_lock);
139                 rover = tcp_port_rover;
140                 do {    rover++;
141                         if ((rover < low) || (rover > high))
142                                 rover = low;
143                         head = &tcp_bhash[tcp_bhashfn(rover)];
144                         spin_lock(&head->lock);
145                         tb_for_each(tb, node, &head->chain)
146                                 if (tb->port == rover)
147                                         goto next;
148                         break;
149                 next:
150                         spin_unlock(&head->lock);
151                 } while (--remaining > 0);
152                 tcp_port_rover = rover;
153                 spin_unlock(&tcp_portalloc_lock);
154
155                 /* Exhausted local port range during search? */
156                 ret = 1;
157                 if (remaining <= 0)
158                         goto fail;
159
160                 /* OK, here is the one we will use. */
161                 snum = rover;
162         } else {
163                 head = &tcp_bhash[tcp_bhashfn(snum)];
164                 spin_lock(&head->lock);
165                 tb_for_each(tb, node, &head->chain)
166                         if (tb->port == snum)
167                                 goto tb_found;
168         }
169         tb = NULL;
170         goto tb_not_found;
171 tb_found:
172         if (tb && !hlist_empty(&tb->owners)) {
173                 if (tb->fastreuse > 0 && sk->sk_reuse &&
174                     sk->sk_state != TCP_LISTEN) {
175                         goto success;
176                 } else {
177                         ret = 1;
178                         if (tcp_v6_bind_conflict(sk, tb))
179                                 goto fail_unlock;
180                 }
181         }
182 tb_not_found:
183         ret = 1;
184         if (!tb && (tb = tcp_bucket_create(head, snum)) == NULL)
185                 goto fail_unlock;
186         if (hlist_empty(&tb->owners)) {
187                 if (sk->sk_reuse && sk->sk_state != TCP_LISTEN)
188                         tb->fastreuse = 1;
189                 else
190                         tb->fastreuse = 0;
191         } else if (tb->fastreuse &&
192                    (!sk->sk_reuse || sk->sk_state == TCP_LISTEN))
193                 tb->fastreuse = 0;
194
195 success:
196         if (!tcp_sk(sk)->bind_hash)
197                 tcp_bind_hash(sk, tb, snum);
198         BUG_TRAP(tcp_sk(sk)->bind_hash == tb);
199         ret = 0;
200
201 fail_unlock:
202         spin_unlock(&head->lock);
203 fail:
204         local_bh_enable();
205         return ret;
206 }
207
208 static __inline__ void __tcp_v6_hash(struct sock *sk)
209 {
210         struct hlist_head *list;
211         rwlock_t *lock;
212
213         BUG_TRAP(sk_unhashed(sk));
214
215         if (sk->sk_state == TCP_LISTEN) {
216                 list = &tcp_listening_hash[tcp_sk_listen_hashfn(sk)];
217                 lock = &tcp_lhash_lock;
218                 tcp_listen_wlock();
219         } else {
220                 sk->sk_hashent = tcp_v6_sk_hashfn(sk);
221                 list = &tcp_ehash[sk->sk_hashent].chain;
222                 lock = &tcp_ehash[sk->sk_hashent].lock;
223                 write_lock(lock);
224         }
225
226         __sk_add_node(sk, list);
227         sock_prot_inc_use(sk->sk_prot);
228         write_unlock(lock);
229 }
230
231
232 static void tcp_v6_hash(struct sock *sk)
233 {
234         if (sk->sk_state != TCP_CLOSE) {
235                 struct tcp_opt *tp = tcp_sk(sk);
236
237                 if (tp->af_specific == &ipv6_mapped) {
238                         tcp_prot.hash(sk);
239                         return;
240                 }
241                 local_bh_disable();
242                 __tcp_v6_hash(sk);
243                 local_bh_enable();
244         }
245 }
246
247 static struct sock *tcp_v6_lookup_listener(struct in6_addr *daddr, unsigned short hnum, int dif)
248 {
249         struct sock *sk;
250         struct hlist_node *node;
251         struct sock *result = NULL;
252         int score, hiscore;
253
254         hiscore=0;
255         read_lock(&tcp_lhash_lock);
256         sk_for_each(sk, node, &tcp_listening_hash[tcp_lhashfn(hnum)]) {
257                 if (inet_sk(sk)->num == hnum && sk->sk_family == PF_INET6) {
258                         struct ipv6_pinfo *np = inet6_sk(sk);
259                         
260                         score = 1;
261                         if (!ipv6_addr_any(&np->rcv_saddr)) {
262                                 if (ipv6_addr_cmp(&np->rcv_saddr, daddr))
263                                         continue;
264                                 score++;
265                         }
266                         if (sk->sk_bound_dev_if) {
267                                 if (sk->sk_bound_dev_if != dif)
268                                         continue;
269                                 score++;
270                         }
271                         if (score == 3) {
272                                 result = sk;
273                                 break;
274                         }
275                         if (score > hiscore) {
276                                 hiscore = score;
277                                 result = sk;
278                         }
279                 }
280         }
281         if (result)
282                 sock_hold(result);
283         read_unlock(&tcp_lhash_lock);
284         return result;
285 }
286
287 /* Sockets in TCP_CLOSE state are _always_ taken out of the hash, so
288  * we need not check it for TCP lookups anymore, thanks Alexey. -DaveM
289  *
290  * The sockhash lock must be held as a reader here.
291  */
292
293 static inline struct sock *__tcp_v6_lookup_established(struct in6_addr *saddr, u16 sport,
294                                                        struct in6_addr *daddr, u16 hnum,
295                                                        int dif)
296 {
297         struct tcp_ehash_bucket *head;
298         struct sock *sk;
299         struct hlist_node *node;
300         __u32 ports = TCP_COMBINED_PORTS(sport, hnum);
301         int hash;
302
303         /* Optimize here for direct hit, only listening connections can
304          * have wildcards anyways.
305          */
306         hash = tcp_v6_hashfn(daddr, hnum, saddr, sport);
307         head = &tcp_ehash[hash];
308         read_lock(&head->lock);
309         sk_for_each(sk, node, &head->chain) {
310                 /* For IPV6 do the cheaper port and family tests first. */
311                 if(TCP_IPV6_MATCH(sk, saddr, daddr, ports, dif))
312                         goto hit; /* You sunk my battleship! */
313         }
314         /* Must check for a TIME_WAIT'er before going to listener hash. */
315         sk_for_each(sk, node, &(head + tcp_ehash_size)->chain) {
316                 /* FIXME: acme: check this... */
317                 struct tcp_tw_bucket *tw = (struct tcp_tw_bucket *)sk;
318
319                 if(*((__u32 *)&(tw->tw_dport))  == ports        &&
320                    sk->sk_family                == PF_INET6) {
321                         if(!ipv6_addr_cmp(&tw->tw_v6_daddr, saddr)      &&
322                            !ipv6_addr_cmp(&tw->tw_v6_rcv_saddr, daddr)  &&
323                            (!sk->sk_bound_dev_if || sk->sk_bound_dev_if == dif))
324                                 goto hit;
325                 }
326         }
327         read_unlock(&head->lock);
328         return NULL;
329
330 hit:
331         sock_hold(sk);
332         read_unlock(&head->lock);
333         return sk;
334 }
335
336
337 static inline struct sock *__tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
338                                            struct in6_addr *daddr, u16 hnum,
339                                            int dif)
340 {
341         struct sock *sk;
342
343         sk = __tcp_v6_lookup_established(saddr, sport, daddr, hnum, dif);
344
345         if (sk)
346                 return sk;
347
348         return tcp_v6_lookup_listener(daddr, hnum, dif);
349 }
350
351 inline struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
352                                   struct in6_addr *daddr, u16 dport,
353                                   int dif)
354 {
355         struct sock *sk;
356
357         local_bh_disable();
358         sk = __tcp_v6_lookup(saddr, sport, daddr, ntohs(dport), dif);
359         local_bh_enable();
360
361         return sk;
362 }
363
364
365 /*
366  * Open request hash tables.
367  */
368
369 static u32 tcp_v6_synq_hash(struct in6_addr *raddr, u16 rport, u32 rnd)
370 {
371         u32 a, b, c;
372
373         a = raddr->s6_addr32[0];
374         b = raddr->s6_addr32[1];
375         c = raddr->s6_addr32[2];
376
377         a += JHASH_GOLDEN_RATIO;
378         b += JHASH_GOLDEN_RATIO;
379         c += rnd;
380         __jhash_mix(a, b, c);
381
382         a += raddr->s6_addr32[3];
383         b += (u32) rport;
384         __jhash_mix(a, b, c);
385
386         return c & (TCP_SYNQ_HSIZE - 1);
387 }
388
389 static struct open_request *tcp_v6_search_req(struct tcp_opt *tp,
390                                               struct open_request ***prevp,
391                                               __u16 rport,
392                                               struct in6_addr *raddr,
393                                               struct in6_addr *laddr,
394                                               int iif)
395 {
396         struct tcp_listen_opt *lopt = tp->listen_opt;
397         struct open_request *req, **prev;  
398
399         for (prev = &lopt->syn_table[tcp_v6_synq_hash(raddr, rport, lopt->hash_rnd)];
400              (req = *prev) != NULL;
401              prev = &req->dl_next) {
402                 if (req->rmt_port == rport &&
403                     req->class->family == AF_INET6 &&
404                     !ipv6_addr_cmp(&req->af.v6_req.rmt_addr, raddr) &&
405                     !ipv6_addr_cmp(&req->af.v6_req.loc_addr, laddr) &&
406                     (!req->af.v6_req.iif || req->af.v6_req.iif == iif)) {
407                         BUG_TRAP(req->sk == NULL);
408                         *prevp = prev;
409                         return req;
410                 }
411         }
412
413         return NULL;
414 }
415
416 static __inline__ u16 tcp_v6_check(struct tcphdr *th, int len,
417                                    struct in6_addr *saddr, 
418                                    struct in6_addr *daddr, 
419                                    unsigned long base)
420 {
421         return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
422 }
423
424 static __u32 tcp_v6_init_sequence(struct sock *sk, struct sk_buff *skb)
425 {
426         if (skb->protocol == htons(ETH_P_IPV6)) {
427                 return secure_tcpv6_sequence_number(skb->nh.ipv6h->daddr.s6_addr32,
428                                                     skb->nh.ipv6h->saddr.s6_addr32,
429                                                     skb->h.th->dest,
430                                                     skb->h.th->source);
431         } else {
432                 return secure_tcp_sequence_number(skb->nh.iph->daddr,
433                                                   skb->nh.iph->saddr,
434                                                   skb->h.th->dest,
435                                                   skb->h.th->source);
436         }
437 }
438
439 static int tcp_v6_check_established(struct sock *sk)
440 {
441         struct inet_opt *inet = inet_sk(sk);
442         struct ipv6_pinfo *np = inet6_sk(sk);
443         struct in6_addr *daddr = &np->rcv_saddr;
444         struct in6_addr *saddr = &np->daddr;
445         int dif = sk->sk_bound_dev_if;
446         u32 ports = TCP_COMBINED_PORTS(inet->dport, inet->num);
447         int hash = tcp_v6_hashfn(daddr, inet->num, saddr, inet->dport);
448         struct tcp_ehash_bucket *head = &tcp_ehash[hash];
449         struct sock *sk2;
450         struct hlist_node *node;
451         struct tcp_tw_bucket *tw;
452
453         write_lock_bh(&head->lock);
454
455         /* Check TIME-WAIT sockets first. */
456         sk_for_each(sk2, node, &(head + tcp_ehash_size)->chain) {
457                 tw = (struct tcp_tw_bucket*)sk2;
458
459                 if(*((__u32 *)&(tw->tw_dport))  == ports        &&
460                    sk2->sk_family               == PF_INET6     &&
461                    !ipv6_addr_cmp(&tw->tw_v6_daddr, saddr)      &&
462                    !ipv6_addr_cmp(&tw->tw_v6_rcv_saddr, daddr)  &&
463                    sk2->sk_bound_dev_if == sk->sk_bound_dev_if) {
464                         struct tcp_opt *tp = tcp_sk(sk);
465
466                         if (tw->tw_ts_recent_stamp) {
467                                 /* See comment in tcp_ipv4.c */
468                                 tp->write_seq = tw->tw_snd_nxt + 65535 + 2;
469                                 if (!tp->write_seq)
470                                         tp->write_seq = 1;
471                                 tp->ts_recent = tw->tw_ts_recent;
472                                 tp->ts_recent_stamp = tw->tw_ts_recent_stamp;
473                                 sock_hold(sk2);
474                                 goto unique;
475                         } else
476                                 goto not_unique;
477                 }
478         }
479         tw = NULL;
480
481         /* And established part... */
482         sk_for_each(sk2, node, &head->chain) {
483                 if(TCP_IPV6_MATCH(sk2, saddr, daddr, ports, dif))
484                         goto not_unique;
485         }
486
487 unique:
488         BUG_TRAP(sk_unhashed(sk));
489         __sk_add_node(sk, &head->chain);
490         sk->sk_hashent = hash;
491         sock_prot_inc_use(sk->sk_prot);
492         write_unlock_bh(&head->lock);
493
494         if (tw) {
495                 /* Silly. Should hash-dance instead... */
496                 local_bh_disable();
497                 tcp_tw_deschedule(tw);
498                 NET_INC_STATS_BH(TimeWaitRecycled);
499                 local_bh_enable();
500
501                 tcp_tw_put(tw);
502         }
503         return 0;
504
505 not_unique:
506         write_unlock_bh(&head->lock);
507         return -EADDRNOTAVAIL;
508 }
509
510 static int tcp_v6_hash_connect(struct sock *sk)
511 {
512         struct tcp_bind_hashbucket *head;
513         struct tcp_bind_bucket *tb;
514
515         /* XXX */
516         if (inet_sk(sk)->num == 0) { 
517                 int err = tcp_v6_get_port(sk, inet_sk(sk)->num);
518                 if (err)
519                         return err;
520                 inet_sk(sk)->sport = htons(inet_sk(sk)->num);
521         }
522
523         head = &tcp_bhash[tcp_bhashfn(inet_sk(sk)->num)];
524         tb = tb_head(head);
525
526         spin_lock_bh(&head->lock);
527
528         if (sk_head(&tb->owners) == sk && !sk->sk_bind_node.next) {
529                 __tcp_v6_hash(sk);
530                 spin_unlock_bh(&head->lock);
531                 return 0;
532         } else {
533                 spin_unlock_bh(&head->lock);
534                 return tcp_v6_check_established(sk);
535         }
536 }
537
538 static __inline__ int tcp_v6_iif(struct sk_buff *skb)
539 {
540         return IP6CB(skb)->iif;
541 }
542
543 static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr, 
544                           int addr_len)
545 {
546         struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
547         struct inet_opt *inet = inet_sk(sk);
548         struct ipv6_pinfo *np = inet6_sk(sk);
549         struct tcp_opt *tp = tcp_sk(sk);
550         struct in6_addr *saddr = NULL;
551         struct flowi fl;
552         struct dst_entry *dst;
553         int addr_type;
554         int err;
555
556         if (addr_len < SIN6_LEN_RFC2133) 
557                 return -EINVAL;
558
559         if (usin->sin6_family != AF_INET6) 
560                 return(-EAFNOSUPPORT);
561
562         memset(&fl, 0, sizeof(fl));
563
564         if (np->sndflow) {
565                 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
566                 IP6_ECN_flow_init(fl.fl6_flowlabel);
567                 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
568                         struct ip6_flowlabel *flowlabel;
569                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
570                         if (flowlabel == NULL)
571                                 return -EINVAL;
572                         ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
573                         fl6_sock_release(flowlabel);
574                 }
575         }
576
577         /*
578          *      connect() to INADDR_ANY means loopback (BSD'ism).
579          */
580         
581         if(ipv6_addr_any(&usin->sin6_addr))
582                 usin->sin6_addr.s6_addr[15] = 0x1; 
583
584         addr_type = ipv6_addr_type(&usin->sin6_addr);
585
586         if(addr_type & IPV6_ADDR_MULTICAST)
587                 return -ENETUNREACH;
588
589         if (addr_type&IPV6_ADDR_LINKLOCAL) {
590                 if (addr_len >= sizeof(struct sockaddr_in6) &&
591                     usin->sin6_scope_id) {
592                         /* If interface is set while binding, indices
593                          * must coincide.
594                          */
595                         if (sk->sk_bound_dev_if &&
596                             sk->sk_bound_dev_if != usin->sin6_scope_id)
597                                 return -EINVAL;
598
599                         sk->sk_bound_dev_if = usin->sin6_scope_id;
600                 }
601
602                 /* Connect to link-local address requires an interface */
603                 if (!sk->sk_bound_dev_if)
604                         return -EINVAL;
605         }
606
607         if (tp->ts_recent_stamp &&
608             ipv6_addr_cmp(&np->daddr, &usin->sin6_addr)) {
609                 tp->ts_recent = 0;
610                 tp->ts_recent_stamp = 0;
611                 tp->write_seq = 0;
612         }
613
614         ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
615         np->flow_label = fl.fl6_flowlabel;
616
617         /*
618          *      TCP over IPv4
619          */
620
621         if (addr_type == IPV6_ADDR_MAPPED) {
622                 u32 exthdrlen = tp->ext_header_len;
623                 struct sockaddr_in sin;
624
625                 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
626
627                 if (__ipv6_only_sock(sk))
628                         return -ENETUNREACH;
629
630                 sin.sin_family = AF_INET;
631                 sin.sin_port = usin->sin6_port;
632                 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
633
634                 tp->af_specific = &ipv6_mapped;
635                 sk->sk_backlog_rcv = tcp_v4_do_rcv;
636
637                 err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
638
639                 if (err) {
640                         tp->ext_header_len = exthdrlen;
641                         tp->af_specific = &ipv6_specific;
642                         sk->sk_backlog_rcv = tcp_v6_do_rcv;
643                         goto failure;
644                 } else {
645                         ipv6_addr_set(&np->saddr, 0, 0, htonl(0x0000FFFF),
646                                       inet->saddr);
647                         ipv6_addr_set(&np->rcv_saddr, 0, 0, htonl(0x0000FFFF),
648                                       inet->rcv_saddr);
649                 }
650
651                 return err;
652         }
653
654         if (!ipv6_addr_any(&np->rcv_saddr))
655                 saddr = &np->rcv_saddr;
656
657         fl.proto = IPPROTO_TCP;
658         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
659         ipv6_addr_copy(&fl.fl6_src,
660                        (saddr ? saddr : &np->saddr));
661         fl.oif = sk->sk_bound_dev_if;
662         fl.fl_ip_dport = usin->sin6_port;
663         fl.fl_ip_sport = inet->sport;
664
665         if (np->opt && np->opt->srcrt) {
666                 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
667                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
668         }
669
670         err = ip6_dst_lookup(sk, &dst, &fl);
671
672         if (err)
673                 goto failure;
674
675         if (saddr == NULL) {
676                 saddr = &fl.fl6_src;
677                 ipv6_addr_copy(&np->rcv_saddr, saddr);
678         }
679
680         /* set the source address */
681         ipv6_addr_copy(&np->saddr, saddr);
682         inet->rcv_saddr = LOOPBACK4_IPV6;
683
684         ip6_dst_store(sk, dst, NULL);
685         sk->sk_route_caps = dst->dev->features &
686                 ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
687
688         tp->ext_header_len = 0;
689         if (np->opt)
690                 tp->ext_header_len = np->opt->opt_flen + np->opt->opt_nflen;
691         tp->ext2_header_len = dst->header_len;
692
693         tp->mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
694
695         inet->dport = usin->sin6_port;
696
697         tcp_set_state(sk, TCP_SYN_SENT);
698         err = tcp_v6_hash_connect(sk);
699         if (err)
700                 goto late_failure;
701
702         if (!tp->write_seq)
703                 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
704                                                              np->daddr.s6_addr32,
705                                                              inet->sport,
706                                                              inet->dport);
707
708         err = tcp_connect(sk);
709         if (err)
710                 goto late_failure;
711
712         return 0;
713
714 late_failure:
715         tcp_set_state(sk, TCP_CLOSE);
716         __sk_dst_reset(sk);
717 failure:
718         inet->dport = 0;
719         sk->sk_route_caps = 0;
720         return err;
721 }
722
723 static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
724                 int type, int code, int offset, __u32 info)
725 {
726         struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
727         struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
728         struct ipv6_pinfo *np;
729         struct sock *sk;
730         int err;
731         struct tcp_opt *tp; 
732         __u32 seq;
733
734         sk = tcp_v6_lookup(&hdr->daddr, th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
735
736         if (sk == NULL) {
737                 ICMP6_INC_STATS_BH(__in6_dev_get(skb->dev), Icmp6InErrors);
738                 return;
739         }
740
741         if (sk->sk_state == TCP_TIME_WAIT) {
742                 tcp_tw_put((struct tcp_tw_bucket*)sk);
743                 return;
744         }
745
746         bh_lock_sock(sk);
747         if (sock_owned_by_user(sk))
748                 NET_INC_STATS_BH(LockDroppedIcmps);
749
750         if (sk->sk_state == TCP_CLOSE)
751                 goto out;
752
753         tp = tcp_sk(sk);
754         seq = ntohl(th->seq); 
755         if (sk->sk_state != TCP_LISTEN &&
756             !between(seq, tp->snd_una, tp->snd_nxt)) {
757                 NET_INC_STATS_BH(OutOfWindowIcmps);
758                 goto out;
759         }
760
761         np = inet6_sk(sk);
762
763         if (type == ICMPV6_PKT_TOOBIG) {
764                 struct dst_entry *dst = NULL;
765
766                 if (sock_owned_by_user(sk))
767                         goto out;
768                 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
769                         goto out;
770
771                 /* icmp should have updated the destination cache entry */
772                 dst = __sk_dst_check(sk, np->dst_cookie);
773
774                 if (dst == NULL) {
775                         struct inet_opt *inet = inet_sk(sk);
776                         struct flowi fl;
777
778                         /* BUGGG_FUTURE: Again, it is not clear how
779                            to handle rthdr case. Ignore this complexity
780                            for now.
781                          */
782                         memset(&fl, 0, sizeof(fl));
783                         fl.proto = IPPROTO_TCP;
784                         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
785                         ipv6_addr_copy(&fl.fl6_src, &np->saddr);
786                         fl.oif = sk->sk_bound_dev_if;
787                         fl.fl_ip_dport = inet->dport;
788                         fl.fl_ip_sport = inet->sport;
789
790                         if ((err = ip6_dst_lookup(sk, &dst, &fl))) {
791                                 sk->sk_err_soft = -err;
792                                 goto out;
793                         }
794                 } else
795                         dst_hold(dst);
796
797                 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 */
801                 dst_release(dst);
802                 goto out;
803         }
804
805         icmpv6_err_convert(type, code, &err);
806
807         /* Might be for an open_request */
808         switch (sk->sk_state) {
809                 struct open_request *req, **prev;
810         case TCP_LISTEN:
811                 if (sock_owned_by_user(sk))
812                         goto out;
813
814                 req = tcp_v6_search_req(tp, &prev, th->dest, &hdr->daddr,
815                                         &hdr->saddr, tcp_v6_iif(skb));
816                 if (!req)
817                         goto out;
818
819                 /* ICMPs are not backlogged, hence we cannot get
820                  * an established socket here.
821                  */
822                 BUG_TRAP(req->sk == NULL);
823
824                 if (seq != req->snt_isn) {
825                         NET_INC_STATS_BH(OutOfWindowIcmps);
826                         goto out;
827                 }
828
829                 tcp_synq_drop(sk, req, prev);
830                 goto out;
831
832         case TCP_SYN_SENT:
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);
837                         sk->sk_err = err;
838                         sk->sk_error_report(sk);                /* Wake people up to see the error (see connect in sock.c) */
839
840                         tcp_done(sk);
841                 } else
842                         sk->sk_err_soft = err;
843                 goto out;
844         }
845
846         if (!sock_owned_by_user(sk) && np->recverr) {
847                 sk->sk_err = err;
848                 sk->sk_error_report(sk);
849         } else
850                 sk->sk_err_soft = err;
851
852 out:
853         bh_unlock_sock(sk);
854         sock_put(sk);
855 }
856
857
858 static int tcp_v6_send_synack(struct sock *sk, struct open_request *req,
859                               struct dst_entry *dst)
860 {
861         struct ipv6_pinfo *np = inet6_sk(sk);
862         struct sk_buff * skb;
863         struct ipv6_txoptions *opt = NULL;
864         struct flowi fl;
865         int err = -1;
866
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;
875
876         if (dst == NULL) {
877                 opt = np->opt;
878                 if (opt == NULL &&
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 = IP6CB(pktopts);
883                         if (rxopt->srcrt)
884                                 opt = ipv6_invert_rthdr(sk, (struct ipv6_rt_hdr*)(pktopts->nh.raw + rxopt->srcrt));
885                 }
886
887                 if (opt && opt->srcrt) {
888                         struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
889                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
890                 }
891
892                 err = ip6_dst_lookup(sk, &dst, &fl);
893                 if (err)
894                         goto done;
895         }
896
897         skb = tcp_make_synack(sk, dst, req);
898         if (skb) {
899                 struct tcphdr *th = skb->h.th;
900
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));
904
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)
908                         err = 0;
909         }
910
911 done:
912         dst_release(dst);
913         if (opt && opt != np->opt)
914                 sock_kfree_s(sk, opt, opt->tot_len);
915         return err;
916 }
917
918 static void tcp_v6_or_free(struct open_request *req)
919 {
920         if (req->af.v6_req.pktopts)
921                 kfree_skb(req->af.v6_req.pktopts);
922 }
923
924 static struct or_calltable or_ipv6 = {
925         .family         =       AF_INET6,
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
930 };
931
932 static int ipv6_opt_accepted(struct sock *sk, struct sk_buff *skb)
933 {
934         struct ipv6_pinfo *np = inet6_sk(sk);
935         struct inet6_skb_parm *opt = IP6CB(skb);
936
937         if (np->rxopt.all) {
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))
943                         return 1;
944         }
945         return 0;
946 }
947
948
949 static void tcp_v6_send_check(struct sock *sk, struct tcphdr *th, int len, 
950                               struct sk_buff *skb)
951 {
952         struct ipv6_pinfo *np = inet6_sk(sk);
953
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);
957         } else {
958                 th->check = csum_ipv6_magic(&np->saddr, &np->daddr, len, IPPROTO_TCP, 
959                                             csum_partial((char *)th, th->doff<<2, 
960                                                          skb->csum));
961         }
962 }
963
964
965 static void tcp_v6_send_reset(struct sk_buff *skb)
966 {
967         struct tcphdr *th = skb->h.th, *t1; 
968         struct sk_buff *buff;
969         struct flowi fl;
970
971         if (th->rst)
972                 return;
973
974         if (!ipv6_unicast_destination(skb))
975                 return; 
976
977         /*
978          * We need to grab some memory, and put together an RST,
979          * and then put it into the queue to be sent.
980          */
981
982         buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr), GFP_ATOMIC);
983         if (buff == NULL) 
984                 return;
985
986         skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr));
987
988         t1 = (struct tcphdr *) skb_push(buff,sizeof(struct tcphdr));
989
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;
995         t1->rst = 1;
996   
997         if(th->ack) {
998                 t1->seq = th->ack_seq;
999         } else {
1000                 t1->ack = 1;
1001                 t1->ack_seq = htonl(ntohl(th->seq) + th->syn + th->fin
1002                                     + skb->len - (th->doff<<2));
1003         }
1004
1005         buff->csum = csum_partial((char *)t1, sizeof(*t1), 0);
1006
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);
1010
1011         t1->check = csum_ipv6_magic(&fl.fl6_src, &fl.fl6_dst,
1012                                     sizeof(*t1), IPPROTO_TCP,
1013                                     buff->csum);
1014
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;
1019
1020         /* sk = NULL, but it is safe for now. RST socket required. */
1021         if (!ip6_dst_lookup(NULL, &buff->dst, &fl)) {
1022                 ip6_xmit(NULL, buff, &fl, NULL, 0);
1023                 TCP_INC_STATS_BH(TcpOutSegs);
1024                 TCP_INC_STATS_BH(TcpOutRsts);
1025                 return;
1026         }
1027
1028         kfree_skb(buff);
1029 }
1030
1031 static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts)
1032 {
1033         struct tcphdr *th = skb->h.th, *t1;
1034         struct sk_buff *buff;
1035         struct flowi fl;
1036         int tot_len = sizeof(struct tcphdr);
1037
1038         buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr), GFP_ATOMIC);
1039         if (buff == NULL)
1040                 return;
1041
1042         skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr));
1043
1044         if (ts)
1045                 tot_len += 3*4;
1046
1047         t1 = (struct tcphdr *) skb_push(buff,tot_len);
1048
1049         /* Swap the send and the receive. */
1050         memset(t1, 0, sizeof(*t1));
1051         t1->dest = th->source;
1052         t1->source = th->dest;
1053         t1->doff = tot_len/4;
1054         t1->seq = htonl(seq);
1055         t1->ack_seq = htonl(ack);
1056         t1->ack = 1;
1057         t1->window = htons(win);
1058         
1059         if (ts) {
1060                 u32 *ptr = (u32*)(t1 + 1);
1061                 *ptr++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1062                                (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1063                 *ptr++ = htonl(tcp_time_stamp);
1064                 *ptr = htonl(ts);
1065         }
1066
1067         buff->csum = csum_partial((char *)t1, tot_len, 0);
1068
1069         memset(&fl, 0, sizeof(fl));
1070         ipv6_addr_copy(&fl.fl6_dst, &skb->nh.ipv6h->saddr);
1071         ipv6_addr_copy(&fl.fl6_src, &skb->nh.ipv6h->daddr);
1072
1073         t1->check = csum_ipv6_magic(&fl.fl6_src, &fl.fl6_dst,
1074                                     tot_len, IPPROTO_TCP,
1075                                     buff->csum);
1076
1077         fl.proto = IPPROTO_TCP;
1078         fl.oif = tcp_v6_iif(skb);
1079         fl.fl_ip_dport = t1->dest;
1080         fl.fl_ip_sport = t1->source;
1081
1082         if (!ip6_dst_lookup(NULL, &buff->dst, &fl)) {
1083                 ip6_xmit(NULL, buff, &fl, NULL, 0);
1084                 TCP_INC_STATS_BH(TcpOutSegs);
1085                 return;
1086         }
1087
1088         kfree_skb(buff);
1089 }
1090
1091 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
1092 {
1093         struct tcp_tw_bucket *tw = (struct tcp_tw_bucket *)sk;
1094
1095         tcp_v6_send_ack(skb, tw->tw_snd_nxt, tw->tw_rcv_nxt,
1096                         tw->tw_rcv_wnd >> tw->tw_rcv_wscale, tw->tw_ts_recent);
1097
1098         tcp_tw_put(tw);
1099 }
1100
1101 static void tcp_v6_or_send_ack(struct sk_buff *skb, struct open_request *req)
1102 {
1103         tcp_v6_send_ack(skb, req->snt_isn+1, req->rcv_isn+1, req->rcv_wnd, req->ts_recent);
1104 }
1105
1106
1107 static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
1108 {
1109         struct open_request *req, **prev;
1110         struct tcphdr *th = skb->h.th;
1111         struct tcp_opt *tp = tcp_sk(sk);
1112         struct sock *nsk;
1113
1114         /* Find possible connection requests. */
1115         req = tcp_v6_search_req(tp, &prev, th->source, &skb->nh.ipv6h->saddr,
1116                                 &skb->nh.ipv6h->daddr, tcp_v6_iif(skb));
1117         if (req)
1118                 return tcp_check_req(sk, skb, req, prev);
1119
1120         nsk = __tcp_v6_lookup_established(&skb->nh.ipv6h->saddr,
1121                                           th->source,
1122                                           &skb->nh.ipv6h->daddr,
1123                                           ntohs(th->dest),
1124                                           tcp_v6_iif(skb));
1125
1126         if (nsk) {
1127                 if (nsk->sk_state != TCP_TIME_WAIT) {
1128                         bh_lock_sock(nsk);
1129                         return nsk;
1130                 }
1131                 tcp_tw_put((struct tcp_tw_bucket*)nsk);
1132                 return NULL;
1133         }
1134
1135 #if 0 /*def CONFIG_SYN_COOKIES*/
1136         if (!th->rst && !th->syn && th->ack)
1137                 sk = cookie_v6_check(sk, skb, &(IPCB(skb)->opt));
1138 #endif
1139         return sk;
1140 }
1141
1142 static void tcp_v6_synq_add(struct sock *sk, struct open_request *req)
1143 {
1144         struct tcp_opt *tp = tcp_sk(sk);
1145         struct tcp_listen_opt *lopt = tp->listen_opt;
1146         u32 h = tcp_v6_synq_hash(&req->af.v6_req.rmt_addr, req->rmt_port, lopt->hash_rnd);
1147
1148         req->sk = NULL;
1149         req->expires = jiffies + TCP_TIMEOUT_INIT;
1150         req->retrans = 0;
1151         req->dl_next = lopt->syn_table[h];
1152
1153         write_lock(&tp->syn_wait_lock);
1154         lopt->syn_table[h] = req;
1155         write_unlock(&tp->syn_wait_lock);
1156
1157 #ifdef CONFIG_ACCEPT_QUEUES
1158         tcp_synq_added(sk, req);
1159 #else
1160         tcp_synq_added(sk);
1161 #endif
1162 }
1163
1164
1165 /* FIXME: this is substantially similar to the ipv4 code.
1166  * Can some kind of merge be done? -- erics
1167  */
1168 static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1169 {
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;
1174 #ifdef CONFIG_ACCEPT_QUEUES
1175         int class = 0;
1176 #endif
1177
1178         if (skb->protocol == htons(ETH_P_IP))
1179                 return tcp_v4_conn_request(sk, skb);
1180
1181         if (!ipv6_unicast_destination(skb))
1182                 goto drop; 
1183
1184
1185         /*
1186          *      There are no SYN attacks on IPv6, yet...        
1187          */
1188         if (tcp_synq_is_full(sk) && !isn) {
1189                 if (net_ratelimit())
1190                         printk(KERN_INFO "TCPv6: dropping request, synflood is possible\n");
1191                 goto drop;              
1192         }
1193
1194 #ifdef CONFIG_ACCEPT_QUEUES
1195         class = (skb->nfmark <= 0) ? 0 :
1196                         ((skb->nfmark >= NUM_ACCEPT_QUEUES) ? 0: skb->nfmark);
1197         /*
1198          * Accept only if the class has shares set or if the default class
1199          * i.e. class 0 has shares
1200          */
1201         if (!(tcp_sk(sk)->acceptq[class].aq_ratio)) {
1202                 if (tcp_sk(sk)->acceptq[0].aq_ratio) 
1203                         class = 0; 
1204                 else 
1205                         goto drop;
1206         }
1207
1208         if (sk_acceptq_is_full(sk, class) && tcp_synq_young(sk, class) > 1)
1209 #else
1210         if (sk_acceptq_is_full(sk) && tcp_synq_young(sk) > 1)
1211 #endif
1212                 goto drop;
1213
1214
1215         req = tcp_openreq_alloc();
1216         if (req == NULL)
1217                 goto drop;
1218
1219         tcp_clear_options(&tmptp);
1220         tmptp.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
1221         tmptp.user_mss = tp->user_mss;
1222
1223         tcp_parse_options(skb, &tmptp, 0);
1224
1225         tmptp.tstamp_ok = tmptp.saw_tstamp;
1226         tcp_openreq_init(req, &tmptp, skb);
1227 #ifdef CONFIG_ACCEPT_QUEUES
1228         req->acceptq_class = class;
1229         req->acceptq_time_stamp = jiffies;
1230 #endif
1231         req->class = &or_ipv6;
1232         ipv6_addr_copy(&req->af.v6_req.rmt_addr, &skb->nh.ipv6h->saddr);
1233         ipv6_addr_copy(&req->af.v6_req.loc_addr, &skb->nh.ipv6h->daddr);
1234         TCP_ECN_create_request(req, skb->h.th);
1235         req->af.v6_req.pktopts = NULL;
1236         if (ipv6_opt_accepted(sk, skb) ||
1237             np->rxopt.bits.rxinfo ||
1238             np->rxopt.bits.rxhlim) {
1239                 atomic_inc(&skb->users);
1240                 req->af.v6_req.pktopts = skb;
1241         }
1242         req->af.v6_req.iif = sk->sk_bound_dev_if;
1243
1244         /* So that link locals have meaning */
1245         if (!sk->sk_bound_dev_if &&
1246             ipv6_addr_type(&req->af.v6_req.rmt_addr) & IPV6_ADDR_LINKLOCAL)
1247                 req->af.v6_req.iif = tcp_v6_iif(skb);
1248
1249         if (isn == 0) 
1250                 isn = tcp_v6_init_sequence(sk,skb);
1251
1252         req->snt_isn = isn;
1253
1254         if (tcp_v6_send_synack(sk, req, NULL))
1255                 goto drop;
1256
1257         tcp_v6_synq_add(sk, req);
1258
1259         return 0;
1260
1261 drop:
1262         if (req)
1263                 tcp_openreq_free(req);
1264
1265         TCP_INC_STATS_BH(TcpAttemptFails);
1266         return 0; /* don't send reset */
1267 }
1268
1269 static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1270                                           struct open_request *req,
1271                                           struct dst_entry *dst)
1272 {
1273         struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1274         struct tcp6_sock *newtcp6sk;
1275         struct inet_opt *newinet;
1276         struct tcp_opt *newtp;
1277         struct sock *newsk;
1278         struct ipv6_txoptions *opt;
1279
1280         if (skb->protocol == htons(ETH_P_IP)) {
1281                 /*
1282                  *      v6 mapped
1283                  */
1284
1285                 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1286
1287                 if (newsk == NULL) 
1288                         return NULL;
1289
1290                 newtcp6sk = (struct tcp6_sock *)newsk;
1291                 newtcp6sk->pinet6 = &newtcp6sk->inet6;
1292
1293                 newinet = inet_sk(newsk);
1294                 newnp = inet6_sk(newsk);
1295                 newtp = tcp_sk(newsk);
1296
1297                 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1298
1299                 ipv6_addr_set(&newnp->daddr, 0, 0, htonl(0x0000FFFF),
1300                               newinet->daddr);
1301
1302                 ipv6_addr_set(&newnp->saddr, 0, 0, htonl(0x0000FFFF),
1303                               newinet->saddr);
1304
1305                 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
1306
1307                 newtp->af_specific = &ipv6_mapped;
1308                 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
1309                 newnp->pktoptions  = NULL;
1310                 newnp->opt         = NULL;
1311                 newnp->mcast_oif   = tcp_v6_iif(skb);
1312                 newnp->mcast_hops  = skb->nh.ipv6h->hop_limit;
1313
1314                 /* Charge newly allocated IPv6 socket. Though it is mapped,
1315                  * it is IPv6 yet.
1316                  */
1317 #ifdef INET_REFCNT_DEBUG
1318                 atomic_inc(&inet6_sock_nr);
1319 #endif
1320
1321                 /* It is tricky place. Until this moment IPv4 tcp
1322                    worked with IPv6 af_tcp.af_specific.
1323                    Sync it now.
1324                  */
1325                 tcp_sync_mss(newsk, newtp->pmtu_cookie);
1326
1327                 return newsk;
1328         }
1329
1330         opt = np->opt;
1331
1332 #ifdef CONFIG_ACCEPT_QUEUES
1333         if (sk_acceptq_is_full(sk, req->acceptq_class))
1334 #else
1335         if (sk_acceptq_is_full(sk))
1336 #endif
1337                 goto out_overflow;
1338
1339         if (np->rxopt.bits.srcrt == 2 &&
1340             opt == NULL && req->af.v6_req.pktopts) {
1341                 struct inet6_skb_parm *rxopt = IP6CB(req->af.v6_req.pktopts);
1342                 if (rxopt->srcrt)
1343                         opt = ipv6_invert_rthdr(sk, (struct ipv6_rt_hdr*)(req->af.v6_req.pktopts->nh.raw+rxopt->srcrt));
1344         }
1345
1346         if (dst == NULL) {
1347                 struct flowi fl;
1348
1349                 memset(&fl, 0, sizeof(fl));
1350                 fl.proto = IPPROTO_TCP;
1351                 ipv6_addr_copy(&fl.fl6_dst, &req->af.v6_req.rmt_addr);
1352                 if (opt && opt->srcrt) {
1353                         struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
1354                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1355                 }
1356                 ipv6_addr_copy(&fl.fl6_src, &req->af.v6_req.loc_addr);
1357                 fl.oif = sk->sk_bound_dev_if;
1358                 fl.fl_ip_dport = req->rmt_port;
1359                 fl.fl_ip_sport = inet_sk(sk)->sport;
1360
1361                 if (ip6_dst_lookup(sk, &dst, &fl))
1362                         goto out;
1363         } 
1364
1365         newsk = tcp_create_openreq_child(sk, req, skb);
1366         if (newsk == NULL)
1367                 goto out;
1368
1369         /* Charge newly allocated IPv6 socket */
1370 #ifdef INET_REFCNT_DEBUG
1371         atomic_inc(&inet6_sock_nr);
1372 #endif
1373
1374         ip6_dst_store(newsk, dst, NULL);
1375         newsk->sk_route_caps = dst->dev->features &
1376                 ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
1377
1378         newtcp6sk = (struct tcp6_sock *)newsk;
1379         newtcp6sk->pinet6 = &newtcp6sk->inet6;
1380
1381         newtp = tcp_sk(newsk);
1382         newinet = inet_sk(newsk);
1383         newnp = inet6_sk(newsk);
1384
1385         memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1386
1387         ipv6_addr_copy(&newnp->daddr, &req->af.v6_req.rmt_addr);
1388         ipv6_addr_copy(&newnp->saddr, &req->af.v6_req.loc_addr);
1389         ipv6_addr_copy(&newnp->rcv_saddr, &req->af.v6_req.loc_addr);
1390         newsk->sk_bound_dev_if = req->af.v6_req.iif;
1391
1392         /* Now IPv6 options... 
1393
1394            First: no IPv4 options.
1395          */
1396         newinet->opt = NULL;
1397
1398         /* Clone RX bits */
1399         newnp->rxopt.all = np->rxopt.all;
1400
1401         /* Clone pktoptions received with SYN */
1402         newnp->pktoptions = NULL;
1403         if (req->af.v6_req.pktopts) {
1404                 newnp->pktoptions = skb_clone(req->af.v6_req.pktopts,
1405                                               GFP_ATOMIC);
1406                 kfree_skb(req->af.v6_req.pktopts);
1407                 req->af.v6_req.pktopts = NULL;
1408                 if (newnp->pktoptions)
1409                         skb_set_owner_r(newnp->pktoptions, newsk);
1410         }
1411         newnp->opt        = NULL;
1412         newnp->mcast_oif  = tcp_v6_iif(skb);
1413         newnp->mcast_hops = skb->nh.ipv6h->hop_limit;
1414
1415         /* Clone native IPv6 options from listening socket (if any)
1416
1417            Yes, keeping reference count would be much more clever,
1418            but we make one more one thing there: reattach optmem
1419            to newsk.
1420          */
1421         if (opt) {
1422                 newnp->opt = ipv6_dup_options(newsk, opt);
1423                 if (opt != np->opt)
1424                         sock_kfree_s(sk, opt, opt->tot_len);
1425         }
1426
1427         newtp->ext_header_len = 0;
1428         if (newnp->opt)
1429                 newtp->ext_header_len = newnp->opt->opt_nflen +
1430                                         newnp->opt->opt_flen;
1431         newtp->ext2_header_len = dst->header_len;
1432
1433         tcp_sync_mss(newsk, dst_pmtu(dst));
1434         newtp->advmss = dst_metric(dst, RTAX_ADVMSS);
1435         tcp_initialize_rcv_mss(newsk);
1436
1437         newinet->daddr = newinet->saddr = newinet->rcv_saddr = LOOPBACK4_IPV6;
1438
1439         __tcp_v6_hash(newsk);
1440         tcp_inherit_port(sk, newsk);
1441
1442         return newsk;
1443
1444 out_overflow:
1445         NET_INC_STATS_BH(ListenOverflows);
1446 out:
1447         NET_INC_STATS_BH(ListenDrops);
1448         if (opt && opt != np->opt)
1449                 sock_kfree_s(sk, opt, opt->tot_len);
1450         dst_release(dst);
1451         return NULL;
1452 }
1453
1454 static int tcp_v6_checksum_init(struct sk_buff *skb)
1455 {
1456         if (skb->ip_summed == CHECKSUM_HW) {
1457                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1458                 if (!tcp_v6_check(skb->h.th,skb->len,&skb->nh.ipv6h->saddr,
1459                                   &skb->nh.ipv6h->daddr,skb->csum))
1460                         return 0;
1461                 LIMIT_NETDEBUG(printk(KERN_DEBUG "hw tcp v6 csum failed\n"));
1462         }
1463         if (skb->len <= 76) {
1464                 if (tcp_v6_check(skb->h.th,skb->len,&skb->nh.ipv6h->saddr,
1465                                  &skb->nh.ipv6h->daddr,skb_checksum(skb, 0, skb->len, 0)))
1466                         return -1;
1467                 skb->ip_summed = CHECKSUM_UNNECESSARY;
1468         } else {
1469                 skb->csum = ~tcp_v6_check(skb->h.th,skb->len,&skb->nh.ipv6h->saddr,
1470                                           &skb->nh.ipv6h->daddr,0);
1471         }
1472         return 0;
1473 }
1474
1475 /* The socket must have it's spinlock held when we get
1476  * here.
1477  *
1478  * We have a potential double-lock case here, so even when
1479  * doing backlog processing we use the BH locking scheme.
1480  * This is because we cannot sleep with the original spinlock
1481  * held.
1482  */
1483 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1484 {
1485         struct ipv6_pinfo *np = inet6_sk(sk);
1486         struct tcp_opt *tp;
1487         struct sk_buff *opt_skb = NULL;
1488
1489         /* Imagine: socket is IPv6. IPv4 packet arrives,
1490            goes to IPv4 receive handler and backlogged.
1491            From backlog it always goes here. Kerboom...
1492            Fortunately, tcp_rcv_established and rcv_established
1493            handle them correctly, but it is not case with
1494            tcp_v6_hnd_req and tcp_v6_send_reset().   --ANK
1495          */
1496
1497         if (skb->protocol == htons(ETH_P_IP))
1498                 return tcp_v4_do_rcv(sk, skb);
1499
1500         if (sk_filter(sk, skb, 0))
1501                 goto discard;
1502
1503         /*
1504          *      socket locking is here for SMP purposes as backlog rcv
1505          *      is currently called with bh processing disabled.
1506          */
1507
1508         /* Do Stevens' IPV6_PKTOPTIONS.
1509
1510            Yes, guys, it is the only place in our code, where we
1511            may make it not affecting IPv4.
1512            The rest of code is protocol independent,
1513            and I do not like idea to uglify IPv4.
1514
1515            Actually, all the idea behind IPV6_PKTOPTIONS
1516            looks not very well thought. For now we latch
1517            options, received in the last packet, enqueued
1518            by tcp. Feel free to propose better solution.
1519                                                --ANK (980728)
1520          */
1521         if (np->rxopt.all)
1522                 opt_skb = skb_clone(skb, GFP_ATOMIC);
1523
1524         if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1525                 TCP_CHECK_TIMER(sk);
1526                 if (tcp_rcv_established(sk, skb, skb->h.th, skb->len))
1527                         goto reset;
1528                 TCP_CHECK_TIMER(sk);
1529                 if (opt_skb)
1530                         goto ipv6_pktoptions;
1531                 return 0;
1532         }
1533
1534         if (skb->len < (skb->h.th->doff<<2) || tcp_checksum_complete(skb))
1535                 goto csum_err;
1536
1537         if (sk->sk_state == TCP_LISTEN) { 
1538                 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1539                 if (!nsk)
1540                         goto discard;
1541
1542                 /*
1543                  * Queue it on the new socket if the new socket is active,
1544                  * otherwise we just shortcircuit this and continue with
1545                  * the new socket..
1546                  */
1547                 if(nsk != sk) {
1548                         if (tcp_child_process(sk, nsk, skb))
1549                                 goto reset;
1550                         if (opt_skb)
1551                                 __kfree_skb(opt_skb);
1552                         return 0;
1553                 }
1554         }
1555
1556         TCP_CHECK_TIMER(sk);
1557         if (tcp_rcv_state_process(sk, skb, skb->h.th, skb->len))
1558                 goto reset;
1559         TCP_CHECK_TIMER(sk);
1560         if (opt_skb)
1561                 goto ipv6_pktoptions;
1562         return 0;
1563
1564 reset:
1565         tcp_v6_send_reset(skb);
1566 discard:
1567         if (opt_skb)
1568                 __kfree_skb(opt_skb);
1569         kfree_skb(skb);
1570         return 0;
1571 csum_err:
1572         TCP_INC_STATS_BH(TcpInErrs);
1573         goto discard;
1574
1575
1576 ipv6_pktoptions:
1577         /* Do you ask, what is it?
1578
1579            1. skb was enqueued by tcp.
1580            2. skb is added to tail of read queue, rather than out of order.
1581            3. socket is not in passive state.
1582            4. Finally, it really contains options, which user wants to receive.
1583          */
1584         tp = tcp_sk(sk);
1585         if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1586             !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1587                 if (np->rxopt.bits.rxinfo)
1588                         np->mcast_oif = tcp_v6_iif(opt_skb);
1589                 if (np->rxopt.bits.rxhlim)
1590                         np->mcast_hops = opt_skb->nh.ipv6h->hop_limit;
1591                 if (ipv6_opt_accepted(sk, opt_skb)) {
1592                         skb_set_owner_r(opt_skb, sk);
1593                         opt_skb = xchg(&np->pktoptions, opt_skb);
1594                 } else {
1595                         __kfree_skb(opt_skb);
1596                         opt_skb = xchg(&np->pktoptions, NULL);
1597                 }
1598         }
1599
1600         if (opt_skb)
1601                 kfree_skb(opt_skb);
1602         return 0;
1603 }
1604
1605 static int tcp_v6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
1606 {
1607         struct sk_buff *skb = *pskb;
1608         struct tcphdr *th;      
1609         struct sock *sk;
1610         int ret;
1611
1612         if (skb->pkt_type != PACKET_HOST)
1613                 goto discard_it;
1614
1615         /*
1616          *      Count it even if it's bad.
1617          */
1618         TCP_INC_STATS_BH(TcpInSegs);
1619
1620         if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1621                 goto discard_it;
1622
1623         th = skb->h.th;
1624
1625         if (th->doff < sizeof(struct tcphdr)/4)
1626                 goto bad_packet;
1627         if (!pskb_may_pull(skb, th->doff*4))
1628                 goto discard_it;
1629
1630         if ((skb->ip_summed != CHECKSUM_UNNECESSARY &&
1631              tcp_v6_checksum_init(skb) < 0))
1632                 goto bad_packet;
1633
1634         th = skb->h.th;
1635         TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1636         TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1637                                     skb->len - th->doff*4);
1638         TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1639         TCP_SKB_CB(skb)->when = 0;
1640         TCP_SKB_CB(skb)->flags = ip6_get_dsfield(skb->nh.ipv6h);
1641         TCP_SKB_CB(skb)->sacked = 0;
1642
1643         sk = __tcp_v6_lookup(&skb->nh.ipv6h->saddr, th->source,
1644                              &skb->nh.ipv6h->daddr, ntohs(th->dest), tcp_v6_iif(skb));
1645
1646         if (!sk)
1647                 goto no_tcp_socket;
1648
1649 process:
1650         if (sk->sk_state == TCP_TIME_WAIT)
1651                 goto do_time_wait;
1652
1653         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1654                 goto discard_and_relse;
1655
1656         if (sk_filter(sk, skb, 0))
1657                 goto discard_and_relse;
1658
1659         skb->dev = NULL;
1660
1661         bh_lock_sock(sk);
1662         ret = 0;
1663         if (!sock_owned_by_user(sk)) {
1664                 if (!tcp_prequeue(sk, skb))
1665                         ret = tcp_v6_do_rcv(sk, skb);
1666         } else
1667                 sk_add_backlog(sk, skb);
1668         bh_unlock_sock(sk);
1669
1670         sock_put(sk);
1671         return ret ? -1 : 0;
1672
1673 no_tcp_socket:
1674         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1675                 goto discard_it;
1676
1677         if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1678 bad_packet:
1679                 TCP_INC_STATS_BH(TcpInErrs);
1680         } else {
1681                 tcp_v6_send_reset(skb);
1682         }
1683
1684 discard_it:
1685
1686         /*
1687          *      Discard frame
1688          */
1689
1690         kfree_skb(skb);
1691         return 0;
1692
1693 discard_and_relse:
1694         sock_put(sk);
1695         goto discard_it;
1696
1697 do_time_wait:
1698         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1699                 tcp_tw_put((struct tcp_tw_bucket *) sk);
1700                 goto discard_it;
1701         }
1702
1703         if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1704                 TCP_INC_STATS_BH(TcpInErrs);
1705                 tcp_tw_put((struct tcp_tw_bucket *) sk);
1706                 goto discard_it;
1707         }
1708
1709         switch(tcp_timewait_state_process((struct tcp_tw_bucket *)sk,
1710                                           skb, th, skb->len)) {
1711         case TCP_TW_SYN:
1712         {
1713                 struct sock *sk2;
1714
1715                 sk2 = tcp_v6_lookup_listener(&skb->nh.ipv6h->daddr, ntohs(th->dest), tcp_v6_iif(skb));
1716                 if (sk2 != NULL) {
1717                         tcp_tw_deschedule((struct tcp_tw_bucket *)sk);
1718                         tcp_tw_put((struct tcp_tw_bucket *)sk);
1719                         sk = sk2;
1720                         goto process;
1721                 }
1722                 /* Fall through to ACK */
1723         }
1724         case TCP_TW_ACK:
1725                 tcp_v6_timewait_ack(sk, skb);
1726                 break;
1727         case TCP_TW_RST:
1728                 goto no_tcp_socket;
1729         case TCP_TW_SUCCESS:;
1730         }
1731         goto discard_it;
1732 }
1733
1734 static int tcp_v6_rebuild_header(struct sock *sk)
1735 {
1736         int err;
1737         struct dst_entry *dst;
1738         struct ipv6_pinfo *np = inet6_sk(sk);
1739
1740         dst = __sk_dst_check(sk, np->dst_cookie);
1741
1742         if (dst == NULL) {
1743                 struct inet_opt *inet = inet_sk(sk);
1744                 struct flowi fl;
1745
1746                 memset(&fl, 0, sizeof(fl));
1747                 fl.proto = IPPROTO_TCP;
1748                 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
1749                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
1750                 fl.fl6_flowlabel = np->flow_label;
1751                 fl.oif = sk->sk_bound_dev_if;
1752                 fl.fl_ip_dport = inet->dport;
1753                 fl.fl_ip_sport = inet->sport;
1754
1755                 if (np->opt && np->opt->srcrt) {
1756                         struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
1757                         ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1758                 }
1759
1760                 err = ip6_dst_lookup(sk, &dst, &fl);
1761
1762                 if (err) {
1763                         sk->sk_route_caps = 0;
1764                         return err;
1765                 }
1766
1767                 ip6_dst_store(sk, dst, NULL);
1768                 sk->sk_route_caps = dst->dev->features &
1769                         ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
1770                 tcp_sk(sk)->ext2_header_len = dst->header_len;
1771         }
1772
1773         return 0;
1774 }
1775
1776 static int tcp_v6_xmit(struct sk_buff *skb, int ipfragok)
1777 {
1778         struct sock *sk = skb->sk;
1779         struct inet_opt *inet = inet_sk(sk);
1780         struct ipv6_pinfo *np = inet6_sk(sk);
1781         struct flowi fl;
1782         struct dst_entry *dst;
1783
1784         memset(&fl, 0, sizeof(fl));
1785         fl.proto = IPPROTO_TCP;
1786         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
1787         ipv6_addr_copy(&fl.fl6_src, &np->saddr);
1788         fl.fl6_flowlabel = np->flow_label;
1789         IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
1790         fl.oif = sk->sk_bound_dev_if;
1791         fl.fl_ip_sport = inet->sport;
1792         fl.fl_ip_dport = inet->dport;
1793
1794         if (np->opt && np->opt->srcrt) {
1795                 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
1796                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
1797         }
1798
1799         dst = __sk_dst_check(sk, np->dst_cookie);
1800
1801         if (dst == NULL) {
1802                 int err = ip6_dst_lookup(sk, &dst, &fl);
1803
1804                 if (err) {
1805                         sk->sk_err_soft = -err;
1806                         return err;
1807                 }
1808
1809                 ip6_dst_store(sk, dst, NULL);
1810                 sk->sk_route_caps = dst->dev->features &
1811                         ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
1812                 tcp_sk(sk)->ext2_header_len = dst->header_len;
1813         }
1814
1815         skb->dst = dst_clone(dst);
1816
1817         /* Restore final destination back after routing done */
1818         ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
1819
1820         return ip6_xmit(sk, skb, &fl, np->opt, 0);
1821 }
1822
1823 static void v6_addr2sockaddr(struct sock *sk, struct sockaddr * uaddr)
1824 {
1825         struct ipv6_pinfo *np = inet6_sk(sk);
1826         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) uaddr;
1827
1828         sin6->sin6_family = AF_INET6;
1829         ipv6_addr_copy(&sin6->sin6_addr, &np->daddr);
1830         sin6->sin6_port = inet_sk(sk)->dport;
1831         /* We do not store received flowlabel for TCP */
1832         sin6->sin6_flowinfo = 0;
1833         sin6->sin6_scope_id = 0;
1834         if (sk->sk_bound_dev_if &&
1835             ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
1836                 sin6->sin6_scope_id = sk->sk_bound_dev_if;
1837 }
1838
1839 static int tcp_v6_remember_stamp(struct sock *sk)
1840 {
1841         /* Alas, not yet... */
1842         return 0;
1843 }
1844
1845 static struct tcp_func ipv6_specific = {
1846         .queue_xmit     =       tcp_v6_xmit,
1847         .send_check     =       tcp_v6_send_check,
1848         .rebuild_header =       tcp_v6_rebuild_header,
1849         .conn_request   =       tcp_v6_conn_request,
1850         .syn_recv_sock  =       tcp_v6_syn_recv_sock,
1851         .remember_stamp =       tcp_v6_remember_stamp,
1852         .net_header_len =       sizeof(struct ipv6hdr),
1853
1854         .setsockopt     =       ipv6_setsockopt,
1855         .getsockopt     =       ipv6_getsockopt,
1856         .addr2sockaddr  =       v6_addr2sockaddr,
1857         .sockaddr_len   =       sizeof(struct sockaddr_in6)
1858 };
1859
1860 /*
1861  *      TCP over IPv4 via INET6 API
1862  */
1863
1864 static struct tcp_func ipv6_mapped = {
1865         .queue_xmit     =       ip_queue_xmit,
1866         .send_check     =       tcp_v4_send_check,
1867         .rebuild_header =       tcp_v4_rebuild_header,
1868         .conn_request   =       tcp_v6_conn_request,
1869         .syn_recv_sock  =       tcp_v6_syn_recv_sock,
1870         .remember_stamp =       tcp_v4_remember_stamp,
1871         .net_header_len =       sizeof(struct iphdr),
1872
1873         .setsockopt     =       ipv6_setsockopt,
1874         .getsockopt     =       ipv6_getsockopt,
1875         .addr2sockaddr  =       v6_addr2sockaddr,
1876         .sockaddr_len   =       sizeof(struct sockaddr_in6)
1877 };
1878
1879
1880
1881 /* NOTE: A lot of things set to zero explicitly by call to
1882  *       sk_alloc() so need not be done here.
1883  */
1884 static int tcp_v6_init_sock(struct sock *sk)
1885 {
1886         struct tcp_opt *tp = tcp_sk(sk);
1887
1888         skb_queue_head_init(&tp->out_of_order_queue);
1889         tcp_init_xmit_timers(sk);
1890         tcp_prequeue_init(tp);
1891
1892         tp->rto  = TCP_TIMEOUT_INIT;
1893         tp->mdev = TCP_TIMEOUT_INIT;
1894
1895         /* So many TCP implementations out there (incorrectly) count the
1896          * initial SYN frame in their delayed-ACK and congestion control
1897          * algorithms that we must have the following bandaid to talk
1898          * efficiently to them.  -DaveM
1899          */
1900         tp->snd_cwnd = 2;
1901
1902         /* See draft-stevens-tcpca-spec-01 for discussion of the
1903          * initialization of these values.
1904          */
1905         tp->snd_ssthresh = 0x7fffffff;
1906         tp->snd_cwnd_clamp = ~0;
1907         tp->mss_cache = 536;
1908
1909         tp->reordering = sysctl_tcp_reordering;
1910
1911         sk->sk_state = TCP_CLOSE;
1912
1913         tp->af_specific = &ipv6_specific;
1914
1915         sk->sk_write_space = sk_stream_write_space;
1916         sk->sk_use_write_queue = 1;
1917
1918         sk->sk_sndbuf = sysctl_tcp_wmem[1];
1919         sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1920
1921         atomic_inc(&tcp_sockets_allocated);
1922
1923         return 0;
1924 }
1925
1926 static int tcp_v6_destroy_sock(struct sock *sk)
1927 {
1928         extern int tcp_v4_destroy_sock(struct sock *sk);
1929
1930         tcp_v4_destroy_sock(sk);
1931         return inet6_destroy_sock(sk);
1932 }
1933
1934 /* Proc filesystem TCPv6 sock list dumping. */
1935 static void get_openreq6(struct seq_file *seq, 
1936                          struct sock *sk, struct open_request *req, int i, int uid)
1937 {
1938         struct in6_addr *dest, *src;
1939         int ttd = req->expires - jiffies;
1940
1941         if (ttd < 0)
1942                 ttd = 0;
1943
1944         src = &req->af.v6_req.loc_addr;
1945         dest = &req->af.v6_req.rmt_addr;
1946         seq_printf(seq,
1947                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1948                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p\n",
1949                    i,
1950                    src->s6_addr32[0], src->s6_addr32[1],
1951                    src->s6_addr32[2], src->s6_addr32[3],
1952                    ntohs(inet_sk(sk)->sport),
1953                    dest->s6_addr32[0], dest->s6_addr32[1],
1954                    dest->s6_addr32[2], dest->s6_addr32[3],
1955                    ntohs(req->rmt_port),
1956                    TCP_SYN_RECV,
1957                    0,0, /* could print option size, but that is af dependent. */
1958                    1,   /* timers active (only the expire timer) */  
1959                    jiffies_to_clock_t(ttd), 
1960                    req->retrans,
1961                    uid,
1962                    0,  /* non standard timer */  
1963                    0, /* open_requests have no inode */
1964                    0, req);
1965 }
1966
1967 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
1968 {
1969         struct in6_addr *dest, *src;
1970         __u16 destp, srcp;
1971         int timer_active;
1972         unsigned long timer_expires;
1973         struct inet_opt *inet = inet_sk(sp);
1974         struct tcp_opt *tp = tcp_sk(sp);
1975         struct ipv6_pinfo *np = inet6_sk(sp);
1976
1977         dest  = &np->daddr;
1978         src   = &np->rcv_saddr;
1979         destp = ntohs(inet->dport);
1980         srcp  = ntohs(inet->sport);
1981         if (tp->pending == TCP_TIME_RETRANS) {
1982                 timer_active    = 1;
1983                 timer_expires   = tp->timeout;
1984         } else if (tp->pending == TCP_TIME_PROBE0) {
1985                 timer_active    = 4;
1986                 timer_expires   = tp->timeout;
1987         } else if (timer_pending(&sp->sk_timer)) {
1988                 timer_active    = 2;
1989                 timer_expires   = sp->sk_timer.expires;
1990         } else {
1991                 timer_active    = 0;
1992                 timer_expires = jiffies;
1993         }
1994
1995         seq_printf(seq,
1996                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1997                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %u %u %u %u %d\n",
1998                    i,
1999                    src->s6_addr32[0], src->s6_addr32[1],
2000                    src->s6_addr32[2], src->s6_addr32[3], srcp,
2001                    dest->s6_addr32[0], dest->s6_addr32[1],
2002                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
2003                    sp->sk_state, 
2004                    tp->write_seq-tp->snd_una, tp->rcv_nxt-tp->copied_seq,
2005                    timer_active,
2006                    jiffies_to_clock_t(timer_expires - jiffies),
2007                    tp->retransmits,
2008                    sock_i_uid(sp),
2009                    tp->probes_out,
2010                    sock_i_ino(sp),
2011                    atomic_read(&sp->sk_refcnt), sp,
2012                    tp->rto, tp->ack.ato, (tp->ack.quick<<1)|tp->ack.pingpong,
2013                    tp->snd_cwnd, tp->snd_ssthresh>=0xFFFF?-1:tp->snd_ssthresh
2014                    );
2015 }
2016
2017 static void get_timewait6_sock(struct seq_file *seq, 
2018                                struct tcp_tw_bucket *tw, int i)
2019 {
2020         struct in6_addr *dest, *src;
2021         __u16 destp, srcp;
2022         int ttd = tw->tw_ttd - jiffies;
2023
2024         if (ttd < 0)
2025                 ttd = 0;
2026
2027         dest  = &tw->tw_v6_daddr;
2028         src   = &tw->tw_v6_rcv_saddr;
2029         destp = ntohs(tw->tw_dport);
2030         srcp  = ntohs(tw->tw_sport);
2031
2032         seq_printf(seq,
2033                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2034                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %p\n",
2035                    i,
2036                    src->s6_addr32[0], src->s6_addr32[1],
2037                    src->s6_addr32[2], src->s6_addr32[3], srcp,
2038                    dest->s6_addr32[0], dest->s6_addr32[1],
2039                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
2040                    tw->tw_substate, 0, 0,
2041                    3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
2042                    atomic_read(&tw->tw_refcnt), tw);
2043 }
2044
2045 #ifdef CONFIG_PROC_FS
2046 static int tcp6_seq_show(struct seq_file *seq, void *v)
2047 {
2048         struct tcp_iter_state *st;
2049
2050         if (v == SEQ_START_TOKEN) {
2051                 seq_printf(seq,
2052                            "  sl  "
2053                            "local_address                         "
2054                            "remote_address                        "
2055                            "st tx_queue rx_queue tr tm->when retrnsmt"
2056                            "   uid  timeout inode\n");
2057                 goto out;
2058         }
2059         st = seq->private;
2060
2061         switch (st->state) {
2062         case TCP_SEQ_STATE_LISTENING:
2063         case TCP_SEQ_STATE_ESTABLISHED:
2064                 get_tcp6_sock(seq, v, st->num);
2065                 break;
2066         case TCP_SEQ_STATE_OPENREQ:
2067                 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
2068                 break;
2069         case TCP_SEQ_STATE_TIME_WAIT:
2070                 get_timewait6_sock(seq, v, st->num);
2071                 break;
2072         }
2073 out:
2074         return 0;
2075 }
2076
2077 static struct file_operations tcp6_seq_fops;
2078 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2079         .owner          = THIS_MODULE,
2080         .name           = "tcp6",
2081         .family         = AF_INET6,
2082         .seq_show       = tcp6_seq_show,
2083         .seq_fops       = &tcp6_seq_fops,
2084 };
2085
2086 int __init tcp6_proc_init(void)
2087 {
2088         return tcp_proc_register(&tcp6_seq_afinfo);
2089 }
2090
2091 void tcp6_proc_exit(void)
2092 {
2093         tcp_proc_unregister(&tcp6_seq_afinfo);
2094 }
2095 #endif
2096
2097 struct proto tcpv6_prot = {
2098         .name                   = "TCPv6",
2099         .close                  = tcp_close,
2100         .connect                = tcp_v6_connect,
2101         .disconnect             = tcp_disconnect,
2102         .accept                 = tcp_accept,
2103         .ioctl                  = tcp_ioctl,
2104         .init                   = tcp_v6_init_sock,
2105         .destroy                = tcp_v6_destroy_sock,
2106         .shutdown               = tcp_shutdown,
2107         .setsockopt             = tcp_setsockopt,
2108         .getsockopt             = tcp_getsockopt,
2109         .sendmsg                = tcp_sendmsg,
2110         .recvmsg                = tcp_recvmsg,
2111         .backlog_rcv            = tcp_v6_do_rcv,
2112         .hash                   = tcp_v6_hash,
2113         .unhash                 = tcp_unhash,
2114         .get_port               = tcp_v6_get_port,
2115         .enter_memory_pressure  = tcp_enter_memory_pressure,
2116         .sockets_allocated      = &tcp_sockets_allocated,
2117         .memory_allocated       = &tcp_memory_allocated,
2118         .memory_pressure        = &tcp_memory_pressure,
2119         .sysctl_mem             = sysctl_tcp_mem,
2120         .sysctl_wmem            = sysctl_tcp_wmem,
2121         .sysctl_rmem            = sysctl_tcp_rmem,
2122         .max_header             = MAX_TCP_HEADER,
2123 };
2124
2125 static struct inet6_protocol tcpv6_protocol = {
2126         .handler        =       tcp_v6_rcv,
2127         .err_handler    =       tcp_v6_err,
2128         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2129 };
2130
2131 extern struct proto_ops inet6_stream_ops;
2132
2133 static struct inet_protosw tcpv6_protosw = {
2134         .type           =       SOCK_STREAM,
2135         .protocol       =       IPPROTO_TCP,
2136         .prot           =       &tcpv6_prot,
2137         .ops            =       &inet6_stream_ops,
2138         .capability     =       -1,
2139         .no_check       =       0,
2140         .flags          =       INET_PROTOSW_PERMANENT,
2141 };
2142
2143 void __init tcpv6_init(void)
2144 {
2145         /* register inet6 protocol */
2146         if (inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP) < 0)
2147                 printk(KERN_ERR "tcpv6_init: Could not register protocol\n");
2148         inet6_register_protosw(&tcpv6_protosw);
2149 }