gro: Disable frag0 optimization on IPv6 ext headers
[pandora-kernel.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  *      Based on:
9  *      linux/net/ipv4/tcp.c
10  *      linux/net/ipv4/tcp_input.c
11  *      linux/net/ipv4/tcp_output.c
12  *
13  *      Fixes:
14  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
15  *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
16  *      Alexey Kuznetsov                allow both IPv4 and IPv6 sockets to bind
17  *                                      a single port at the same time.
18  *      YOSHIFUJI Hideaki @USAGI:       convert /proc/net/tcp6 to seq_file.
19  *
20  *      This program is free software; you can redistribute it and/or
21  *      modify it under the terms of the GNU General Public License
22  *      as published by the Free Software Foundation; either version
23  *      2 of the License, or (at your option) any later version.
24  */
25
26 #include <linux/bottom_half.h>
27 #include <linux/module.h>
28 #include <linux/errno.h>
29 #include <linux/types.h>
30 #include <linux/socket.h>
31 #include <linux/sockios.h>
32 #include <linux/net.h>
33 #include <linux/jiffies.h>
34 #include <linux/in.h>
35 #include <linux/in6.h>
36 #include <linux/netdevice.h>
37 #include <linux/init.h>
38 #include <linux/jhash.h>
39 #include <linux/ipsec.h>
40 #include <linux/times.h>
41 #include <linux/slab.h>
42
43 #include <linux/ipv6.h>
44 #include <linux/icmpv6.h>
45 #include <linux/random.h>
46
47 #include <net/tcp.h>
48 #include <net/ndisc.h>
49 #include <net/inet6_hashtables.h>
50 #include <net/inet6_connection_sock.h>
51 #include <net/ipv6.h>
52 #include <net/transp_v6.h>
53 #include <net/addrconf.h>
54 #include <net/ip6_route.h>
55 #include <net/ip6_checksum.h>
56 #include <net/inet_ecn.h>
57 #include <net/protocol.h>
58 #include <net/xfrm.h>
59 #include <net/snmp.h>
60 #include <net/dsfield.h>
61 #include <net/timewait_sock.h>
62 #include <net/netdma.h>
63 #include <net/inet_common.h>
64 #include <net/secure_seq.h>
65
66 #include <asm/uaccess.h>
67
68 #include <linux/proc_fs.h>
69 #include <linux/seq_file.h>
70
71 #include <linux/crypto.h>
72 #include <linux/scatterlist.h>
73
74 static void     tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb);
75 static void     tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
76                                       struct request_sock *req);
77
78 static int      tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb);
79 static void     __tcp_v6_send_check(struct sk_buff *skb,
80                                     const struct in6_addr *saddr,
81                                     const struct in6_addr *daddr);
82
83 static const struct inet_connection_sock_af_ops ipv6_mapped;
84 static const struct inet_connection_sock_af_ops ipv6_specific;
85 #ifdef CONFIG_TCP_MD5SIG
86 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific;
87 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific;
88 #else
89 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
90                                                    const struct in6_addr *addr)
91 {
92         return NULL;
93 }
94 #endif
95
96 static void tcp_v6_hash(struct sock *sk)
97 {
98         if (sk->sk_state != TCP_CLOSE) {
99                 if (inet_csk(sk)->icsk_af_ops == &ipv6_mapped) {
100                         tcp_prot.hash(sk);
101                         return;
102                 }
103                 local_bh_disable();
104                 __inet6_hash(sk, NULL);
105                 local_bh_enable();
106         }
107 }
108
109 static __inline__ __sum16 tcp_v6_check(int len,
110                                    const struct in6_addr *saddr,
111                                    const struct in6_addr *daddr,
112                                    __wsum base)
113 {
114         return csum_ipv6_magic(saddr, daddr, len, IPPROTO_TCP, base);
115 }
116
117 static __u32 tcp_v6_init_sequence(const struct sk_buff *skb)
118 {
119         return secure_tcpv6_sequence_number(ipv6_hdr(skb)->daddr.s6_addr32,
120                                             ipv6_hdr(skb)->saddr.s6_addr32,
121                                             tcp_hdr(skb)->dest,
122                                             tcp_hdr(skb)->source);
123 }
124
125 static int tcp_v6_connect(struct sock *sk, struct sockaddr *uaddr,
126                           int addr_len)
127 {
128         struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
129         struct inet_sock *inet = inet_sk(sk);
130         struct inet_connection_sock *icsk = inet_csk(sk);
131         struct ipv6_pinfo *np = inet6_sk(sk);
132         struct tcp_sock *tp = tcp_sk(sk);
133         struct in6_addr *saddr = NULL, *final_p, final;
134         struct ipv6_txoptions *opt;
135         struct rt6_info *rt;
136         struct flowi6 fl6;
137         struct dst_entry *dst;
138         int addr_type;
139         int err;
140
141         if (addr_len < SIN6_LEN_RFC2133)
142                 return -EINVAL;
143
144         if (usin->sin6_family != AF_INET6)
145                 return -EAFNOSUPPORT;
146
147         memset(&fl6, 0, sizeof(fl6));
148
149         if (np->sndflow) {
150                 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
151                 IP6_ECN_flow_init(fl6.flowlabel);
152                 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
153                         struct ip6_flowlabel *flowlabel;
154                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
155                         if (flowlabel == NULL)
156                                 return -EINVAL;
157                         ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
158                         fl6_sock_release(flowlabel);
159                 }
160         }
161
162         /*
163          *      connect() to INADDR_ANY means loopback (BSD'ism).
164          */
165
166         if(ipv6_addr_any(&usin->sin6_addr))
167                 usin->sin6_addr.s6_addr[15] = 0x1;
168
169         addr_type = ipv6_addr_type(&usin->sin6_addr);
170
171         if(addr_type & IPV6_ADDR_MULTICAST)
172                 return -ENETUNREACH;
173
174         if (addr_type&IPV6_ADDR_LINKLOCAL) {
175                 if (addr_len >= sizeof(struct sockaddr_in6) &&
176                     usin->sin6_scope_id) {
177                         /* If interface is set while binding, indices
178                          * must coincide.
179                          */
180                         if (sk->sk_bound_dev_if &&
181                             sk->sk_bound_dev_if != usin->sin6_scope_id)
182                                 return -EINVAL;
183
184                         sk->sk_bound_dev_if = usin->sin6_scope_id;
185                 }
186
187                 /* Connect to link-local address requires an interface */
188                 if (!sk->sk_bound_dev_if)
189                         return -EINVAL;
190         }
191
192         if (tp->rx_opt.ts_recent_stamp &&
193             !ipv6_addr_equal(&np->daddr, &usin->sin6_addr)) {
194                 tp->rx_opt.ts_recent = 0;
195                 tp->rx_opt.ts_recent_stamp = 0;
196                 tp->write_seq = 0;
197         }
198
199         ipv6_addr_copy(&np->daddr, &usin->sin6_addr);
200         np->flow_label = fl6.flowlabel;
201
202         /*
203          *      TCP over IPv4
204          */
205
206         if (addr_type == IPV6_ADDR_MAPPED) {
207                 u32 exthdrlen = icsk->icsk_ext_hdr_len;
208                 struct sockaddr_in sin;
209
210                 SOCK_DEBUG(sk, "connect: ipv4 mapped\n");
211
212                 if (__ipv6_only_sock(sk))
213                         return -ENETUNREACH;
214
215                 sin.sin_family = AF_INET;
216                 sin.sin_port = usin->sin6_port;
217                 sin.sin_addr.s_addr = usin->sin6_addr.s6_addr32[3];
218
219                 icsk->icsk_af_ops = &ipv6_mapped;
220                 sk->sk_backlog_rcv = tcp_v4_do_rcv;
221 #ifdef CONFIG_TCP_MD5SIG
222                 tp->af_specific = &tcp_sock_ipv6_mapped_specific;
223 #endif
224
225                 err = tcp_v4_connect(sk, (struct sockaddr *)&sin, sizeof(sin));
226
227                 if (err) {
228                         icsk->icsk_ext_hdr_len = exthdrlen;
229                         icsk->icsk_af_ops = &ipv6_specific;
230                         sk->sk_backlog_rcv = tcp_v6_do_rcv;
231 #ifdef CONFIG_TCP_MD5SIG
232                         tp->af_specific = &tcp_sock_ipv6_specific;
233 #endif
234                         goto failure;
235                 } else {
236                         ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
237                         ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
238                                                &np->rcv_saddr);
239                 }
240
241                 return err;
242         }
243
244         if (!ipv6_addr_any(&np->rcv_saddr))
245                 saddr = &np->rcv_saddr;
246
247         fl6.flowi6_proto = IPPROTO_TCP;
248         ipv6_addr_copy(&fl6.daddr, &np->daddr);
249         ipv6_addr_copy(&fl6.saddr,
250                        (saddr ? saddr : &np->saddr));
251         fl6.flowi6_oif = sk->sk_bound_dev_if;
252         fl6.flowi6_mark = sk->sk_mark;
253         fl6.fl6_dport = usin->sin6_port;
254         fl6.fl6_sport = inet->inet_sport;
255
256         opt = rcu_dereference_protected(np->opt, sock_owned_by_user(sk));
257         final_p = fl6_update_dst(&fl6, opt, &final);
258
259         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
260
261         dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
262         if (IS_ERR(dst)) {
263                 err = PTR_ERR(dst);
264                 goto failure;
265         }
266
267         if (saddr == NULL) {
268                 saddr = &fl6.saddr;
269                 ipv6_addr_copy(&np->rcv_saddr, saddr);
270         }
271
272         /* set the source address */
273         ipv6_addr_copy(&np->saddr, saddr);
274         inet->inet_rcv_saddr = LOOPBACK4_IPV6;
275
276         sk->sk_gso_type = SKB_GSO_TCPV6;
277         __ip6_dst_store(sk, dst, NULL, NULL);
278
279         rt = (struct rt6_info *) dst;
280         if (tcp_death_row.sysctl_tw_recycle &&
281             !tp->rx_opt.ts_recent_stamp &&
282             ipv6_addr_equal(&rt->rt6i_dst.addr, &np->daddr)) {
283                 struct inet_peer *peer = rt6_get_peer(rt);
284                 /*
285                  * VJ's idea. We save last timestamp seen from
286                  * the destination in peer table, when entering state
287                  * TIME-WAIT * and initialize rx_opt.ts_recent from it,
288                  * when trying new connection.
289                  */
290                 if (peer) {
291                         inet_peer_refcheck(peer);
292                         if ((u32)get_seconds() - peer->tcp_ts_stamp <= TCP_PAWS_MSL) {
293                                 tp->rx_opt.ts_recent_stamp = peer->tcp_ts_stamp;
294                                 tp->rx_opt.ts_recent = peer->tcp_ts;
295                         }
296                 }
297         }
298
299         icsk->icsk_ext_hdr_len = 0;
300         if (opt)
301                 icsk->icsk_ext_hdr_len = opt->opt_flen +
302                                          opt->opt_nflen;
303
304         tp->rx_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
305
306         inet->inet_dport = usin->sin6_port;
307
308         tcp_set_state(sk, TCP_SYN_SENT);
309         err = inet6_hash_connect(&tcp_death_row, sk);
310         if (err)
311                 goto late_failure;
312
313         if (!tp->write_seq)
314                 tp->write_seq = secure_tcpv6_sequence_number(np->saddr.s6_addr32,
315                                                              np->daddr.s6_addr32,
316                                                              inet->inet_sport,
317                                                              inet->inet_dport);
318
319         err = tcp_connect(sk);
320         if (err)
321                 goto late_failure;
322
323         return 0;
324
325 late_failure:
326         tcp_set_state(sk, TCP_CLOSE);
327         __sk_dst_reset(sk);
328 failure:
329         inet->inet_dport = 0;
330         sk->sk_route_caps = 0;
331         return err;
332 }
333
334 static void tcp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
335                 u8 type, u8 code, int offset, __be32 info)
336 {
337         const struct ipv6hdr *hdr = (const struct ipv6hdr*)skb->data;
338         const struct tcphdr *th = (struct tcphdr *)(skb->data+offset);
339         struct ipv6_pinfo *np;
340         struct sock *sk;
341         int err;
342         struct tcp_sock *tp;
343         __u32 seq;
344         struct net *net = dev_net(skb->dev);
345
346         sk = inet6_lookup(net, &tcp_hashinfo, &hdr->daddr,
347                         th->dest, &hdr->saddr, th->source, skb->dev->ifindex);
348
349         if (sk == NULL) {
350                 ICMP6_INC_STATS_BH(net, __in6_dev_get(skb->dev),
351                                    ICMP6_MIB_INERRORS);
352                 return;
353         }
354
355         if (sk->sk_state == TCP_TIME_WAIT) {
356                 inet_twsk_put(inet_twsk(sk));
357                 return;
358         }
359
360         bh_lock_sock(sk);
361         if (sock_owned_by_user(sk))
362                 NET_INC_STATS_BH(net, LINUX_MIB_LOCKDROPPEDICMPS);
363
364         if (sk->sk_state == TCP_CLOSE)
365                 goto out;
366
367         if (ipv6_hdr(skb)->hop_limit < inet6_sk(sk)->min_hopcount) {
368                 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
369                 goto out;
370         }
371
372         tp = tcp_sk(sk);
373         seq = ntohl(th->seq);
374         if (sk->sk_state != TCP_LISTEN &&
375             !between(seq, tp->snd_una, tp->snd_nxt)) {
376                 NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
377                 goto out;
378         }
379
380         np = inet6_sk(sk);
381
382         if (type == ICMPV6_PKT_TOOBIG) {
383                 struct dst_entry *dst;
384
385                 if (sock_owned_by_user(sk))
386                         goto out;
387                 if ((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE))
388                         goto out;
389
390                 /* icmp should have updated the destination cache entry */
391                 dst = __sk_dst_check(sk, np->dst_cookie);
392
393                 if (dst == NULL) {
394                         struct inet_sock *inet = inet_sk(sk);
395                         struct flowi6 fl6;
396
397                         /* BUGGG_FUTURE: Again, it is not clear how
398                            to handle rthdr case. Ignore this complexity
399                            for now.
400                          */
401                         memset(&fl6, 0, sizeof(fl6));
402                         fl6.flowi6_proto = IPPROTO_TCP;
403                         ipv6_addr_copy(&fl6.daddr, &np->daddr);
404                         ipv6_addr_copy(&fl6.saddr, &np->saddr);
405                         fl6.flowi6_oif = sk->sk_bound_dev_if;
406                         fl6.flowi6_mark = sk->sk_mark;
407                         fl6.fl6_dport = inet->inet_dport;
408                         fl6.fl6_sport = inet->inet_sport;
409                         security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
410
411                         dst = ip6_dst_lookup_flow(sk, &fl6, NULL, false);
412                         if (IS_ERR(dst)) {
413                                 sk->sk_err_soft = -PTR_ERR(dst);
414                                 goto out;
415                         }
416
417                 } else
418                         dst_hold(dst);
419
420                 if (inet_csk(sk)->icsk_pmtu_cookie > dst_mtu(dst)) {
421                         tcp_sync_mss(sk, dst_mtu(dst));
422                         tcp_simple_retransmit(sk);
423                 } /* else let the usual retransmit timer handle it */
424                 dst_release(dst);
425                 goto out;
426         }
427
428         icmpv6_err_convert(type, code, &err);
429
430         /* Might be for an request_sock */
431         switch (sk->sk_state) {
432                 struct request_sock *req, **prev;
433         case TCP_LISTEN:
434                 if (sock_owned_by_user(sk))
435                         goto out;
436
437                 req = inet6_csk_search_req(sk, &prev, th->dest, &hdr->daddr,
438                                            &hdr->saddr, inet6_iif(skb));
439                 if (!req)
440                         goto out;
441
442                 /* ICMPs are not backlogged, hence we cannot get
443                  * an established socket here.
444                  */
445                 WARN_ON(req->sk != NULL);
446
447                 if (seq != tcp_rsk(req)->snt_isn) {
448                         NET_INC_STATS_BH(net, LINUX_MIB_OUTOFWINDOWICMPS);
449                         goto out;
450                 }
451
452                 inet_csk_reqsk_queue_drop(sk, req, prev);
453                 goto out;
454
455         case TCP_SYN_SENT:
456         case TCP_SYN_RECV:  /* Cannot happen.
457                                It can, it SYNs are crossed. --ANK */
458                 if (!sock_owned_by_user(sk)) {
459                         sk->sk_err = err;
460                         sk->sk_error_report(sk);                /* Wake people up to see the error (see connect in sock.c) */
461
462                         tcp_done(sk);
463                 } else
464                         sk->sk_err_soft = err;
465                 goto out;
466         }
467
468         if (!sock_owned_by_user(sk) && np->recverr) {
469                 sk->sk_err = err;
470                 sk->sk_error_report(sk);
471         } else
472                 sk->sk_err_soft = err;
473
474 out:
475         bh_unlock_sock(sk);
476         sock_put(sk);
477 }
478
479
480 static int tcp_v6_send_synack(struct sock *sk, struct request_sock *req,
481                               struct request_values *rvp)
482 {
483         struct inet6_request_sock *treq = inet6_rsk(req);
484         struct ipv6_pinfo *np = inet6_sk(sk);
485         struct sk_buff * skb;
486         struct in6_addr * final_p, final;
487         struct flowi6 fl6;
488         struct dst_entry *dst;
489         int err;
490
491         memset(&fl6, 0, sizeof(fl6));
492         fl6.flowi6_proto = IPPROTO_TCP;
493         ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
494         ipv6_addr_copy(&fl6.saddr, &treq->loc_addr);
495         fl6.flowlabel = 0;
496         fl6.flowi6_oif = treq->iif;
497         fl6.flowi6_mark = sk->sk_mark;
498         fl6.fl6_dport = inet_rsk(req)->rmt_port;
499         fl6.fl6_sport = inet_rsk(req)->loc_port;
500         security_req_classify_flow(req, flowi6_to_flowi(&fl6));
501
502         final_p = fl6_update_dst(&fl6, rcu_dereference(np->opt), &final);
503
504         dst = ip6_dst_lookup_flow(sk, &fl6, final_p, false);
505         if (IS_ERR(dst)) {
506                 err = PTR_ERR(dst);
507                 dst = NULL;
508                 goto done;
509         }
510         skb = tcp_make_synack(sk, dst, req, rvp);
511         err = -ENOMEM;
512         if (skb) {
513                 __tcp_v6_send_check(skb, &treq->loc_addr, &treq->rmt_addr);
514
515                 ipv6_addr_copy(&fl6.daddr, &treq->rmt_addr);
516                 rcu_read_lock();
517                 err = ip6_xmit(sk, skb, &fl6, rcu_dereference(np->opt),
518                                np->tclass);
519                 rcu_read_unlock();
520                 err = net_xmit_eval(err);
521         }
522
523 done:
524         dst_release(dst);
525         return err;
526 }
527
528 static int tcp_v6_rtx_synack(struct sock *sk, struct request_sock *req,
529                              struct request_values *rvp)
530 {
531         TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_RETRANSSEGS);
532         return tcp_v6_send_synack(sk, req, rvp);
533 }
534
535 static void tcp_v6_reqsk_destructor(struct request_sock *req)
536 {
537         kfree_skb(inet6_rsk(req)->pktopts);
538 }
539
540 #ifdef CONFIG_TCP_MD5SIG
541 static struct tcp_md5sig_key *tcp_v6_md5_do_lookup(struct sock *sk,
542                                                    const struct in6_addr *addr)
543 {
544         struct tcp_sock *tp = tcp_sk(sk);
545         int i;
546
547         BUG_ON(tp == NULL);
548
549         if (!tp->md5sig_info || !tp->md5sig_info->entries6)
550                 return NULL;
551
552         for (i = 0; i < tp->md5sig_info->entries6; i++) {
553                 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, addr))
554                         return &tp->md5sig_info->keys6[i].base;
555         }
556         return NULL;
557 }
558
559 static struct tcp_md5sig_key *tcp_v6_md5_lookup(struct sock *sk,
560                                                 struct sock *addr_sk)
561 {
562         return tcp_v6_md5_do_lookup(sk, &inet6_sk(addr_sk)->daddr);
563 }
564
565 static struct tcp_md5sig_key *tcp_v6_reqsk_md5_lookup(struct sock *sk,
566                                                       struct request_sock *req)
567 {
568         return tcp_v6_md5_do_lookup(sk, &inet6_rsk(req)->rmt_addr);
569 }
570
571 static int tcp_v6_md5_do_add(struct sock *sk, const struct in6_addr *peer,
572                              char *newkey, u8 newkeylen)
573 {
574         /* Add key to the list */
575         struct tcp_md5sig_key *key;
576         struct tcp_sock *tp = tcp_sk(sk);
577         struct tcp6_md5sig_key *keys;
578
579         key = tcp_v6_md5_do_lookup(sk, peer);
580         if (key) {
581                 /* modify existing entry - just update that one */
582                 kfree(key->key);
583                 key->key = newkey;
584                 key->keylen = newkeylen;
585         } else {
586                 /* reallocate new list if current one is full. */
587                 if (!tp->md5sig_info) {
588                         tp->md5sig_info = kzalloc(sizeof(*tp->md5sig_info), GFP_ATOMIC);
589                         if (!tp->md5sig_info) {
590                                 kfree(newkey);
591                                 return -ENOMEM;
592                         }
593                         sk_nocaps_add(sk, NETIF_F_GSO_MASK);
594                 }
595                 if (tp->md5sig_info->entries6 == 0 &&
596                     !tcp_alloc_md5sig_pool()) {
597                         kfree(newkey);
598                         return -ENOMEM;
599                 }
600                 if (tp->md5sig_info->alloced6 == tp->md5sig_info->entries6) {
601                         keys = kmalloc((sizeof (tp->md5sig_info->keys6[0]) *
602                                        (tp->md5sig_info->entries6 + 1)), GFP_ATOMIC);
603
604                         if (!keys) {
605                                 kfree(newkey);
606                                 return -ENOMEM;
607                         }
608
609                         if (tp->md5sig_info->entries6)
610                                 memmove(keys, tp->md5sig_info->keys6,
611                                         (sizeof (tp->md5sig_info->keys6[0]) *
612                                          tp->md5sig_info->entries6));
613
614                         kfree(tp->md5sig_info->keys6);
615                         tp->md5sig_info->keys6 = keys;
616                         tp->md5sig_info->alloced6++;
617                 }
618
619                 ipv6_addr_copy(&tp->md5sig_info->keys6[tp->md5sig_info->entries6].addr,
620                                peer);
621                 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.key = newkey;
622                 tp->md5sig_info->keys6[tp->md5sig_info->entries6].base.keylen = newkeylen;
623
624                 tp->md5sig_info->entries6++;
625         }
626         return 0;
627 }
628
629 static int tcp_v6_md5_add_func(struct sock *sk, struct sock *addr_sk,
630                                u8 *newkey, __u8 newkeylen)
631 {
632         return tcp_v6_md5_do_add(sk, &inet6_sk(addr_sk)->daddr,
633                                  newkey, newkeylen);
634 }
635
636 static int tcp_v6_md5_do_del(struct sock *sk, const struct in6_addr *peer)
637 {
638         struct tcp_sock *tp = tcp_sk(sk);
639         int i;
640
641         for (i = 0; i < tp->md5sig_info->entries6; i++) {
642                 if (ipv6_addr_equal(&tp->md5sig_info->keys6[i].addr, peer)) {
643                         /* Free the key */
644                         kfree(tp->md5sig_info->keys6[i].base.key);
645                         tp->md5sig_info->entries6--;
646
647                         if (tp->md5sig_info->entries6 == 0) {
648                                 kfree(tp->md5sig_info->keys6);
649                                 tp->md5sig_info->keys6 = NULL;
650                                 tp->md5sig_info->alloced6 = 0;
651                         } else {
652                                 /* shrink the database */
653                                 if (tp->md5sig_info->entries6 != i)
654                                         memmove(&tp->md5sig_info->keys6[i],
655                                                 &tp->md5sig_info->keys6[i+1],
656                                                 (tp->md5sig_info->entries6 - i)
657                                                 * sizeof (tp->md5sig_info->keys6[0]));
658                         }
659                         return 0;
660                 }
661         }
662         return -ENOENT;
663 }
664
665 static void tcp_v6_clear_md5_list (struct sock *sk)
666 {
667         struct tcp_sock *tp = tcp_sk(sk);
668         int i;
669
670         if (tp->md5sig_info->entries6) {
671                 for (i = 0; i < tp->md5sig_info->entries6; i++)
672                         kfree(tp->md5sig_info->keys6[i].base.key);
673                 tp->md5sig_info->entries6 = 0;
674         }
675
676         kfree(tp->md5sig_info->keys6);
677         tp->md5sig_info->keys6 = NULL;
678         tp->md5sig_info->alloced6 = 0;
679
680         if (tp->md5sig_info->entries4) {
681                 for (i = 0; i < tp->md5sig_info->entries4; i++)
682                         kfree(tp->md5sig_info->keys4[i].base.key);
683                 tp->md5sig_info->entries4 = 0;
684         }
685
686         kfree(tp->md5sig_info->keys4);
687         tp->md5sig_info->keys4 = NULL;
688         tp->md5sig_info->alloced4 = 0;
689 }
690
691 static int tcp_v6_parse_md5_keys (struct sock *sk, char __user *optval,
692                                   int optlen)
693 {
694         struct tcp_md5sig cmd;
695         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&cmd.tcpm_addr;
696         u8 *newkey;
697
698         if (optlen < sizeof(cmd))
699                 return -EINVAL;
700
701         if (copy_from_user(&cmd, optval, sizeof(cmd)))
702                 return -EFAULT;
703
704         if (sin6->sin6_family != AF_INET6)
705                 return -EINVAL;
706
707         if (!cmd.tcpm_keylen) {
708                 if (!tcp_sk(sk)->md5sig_info)
709                         return -ENOENT;
710                 if (ipv6_addr_v4mapped(&sin6->sin6_addr))
711                         return tcp_v4_md5_do_del(sk, sin6->sin6_addr.s6_addr32[3]);
712                 return tcp_v6_md5_do_del(sk, &sin6->sin6_addr);
713         }
714
715         if (cmd.tcpm_keylen > TCP_MD5SIG_MAXKEYLEN)
716                 return -EINVAL;
717
718         if (!tcp_sk(sk)->md5sig_info) {
719                 struct tcp_sock *tp = tcp_sk(sk);
720                 struct tcp_md5sig_info *p;
721
722                 p = kzalloc(sizeof(struct tcp_md5sig_info), GFP_KERNEL);
723                 if (!p)
724                         return -ENOMEM;
725
726                 tp->md5sig_info = p;
727                 sk_nocaps_add(sk, NETIF_F_GSO_MASK);
728         }
729
730         newkey = kmemdup(cmd.tcpm_key, cmd.tcpm_keylen, GFP_KERNEL);
731         if (!newkey)
732                 return -ENOMEM;
733         if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
734                 return tcp_v4_md5_do_add(sk, sin6->sin6_addr.s6_addr32[3],
735                                          newkey, cmd.tcpm_keylen);
736         }
737         return tcp_v6_md5_do_add(sk, &sin6->sin6_addr, newkey, cmd.tcpm_keylen);
738 }
739
740 static int tcp_v6_md5_hash_pseudoheader(struct tcp_md5sig_pool *hp,
741                                         const struct in6_addr *daddr,
742                                         const struct in6_addr *saddr, int nbytes)
743 {
744         struct tcp6_pseudohdr *bp;
745         struct scatterlist sg;
746
747         bp = &hp->md5_blk.ip6;
748         /* 1. TCP pseudo-header (RFC2460) */
749         ipv6_addr_copy(&bp->saddr, saddr);
750         ipv6_addr_copy(&bp->daddr, daddr);
751         bp->protocol = cpu_to_be32(IPPROTO_TCP);
752         bp->len = cpu_to_be32(nbytes);
753
754         sg_init_one(&sg, bp, sizeof(*bp));
755         return crypto_hash_update(&hp->md5_desc, &sg, sizeof(*bp));
756 }
757
758 static int tcp_v6_md5_hash_hdr(char *md5_hash, struct tcp_md5sig_key *key,
759                                const struct in6_addr *daddr, struct in6_addr *saddr,
760                                const struct tcphdr *th)
761 {
762         struct tcp_md5sig_pool *hp;
763         struct hash_desc *desc;
764
765         hp = tcp_get_md5sig_pool();
766         if (!hp)
767                 goto clear_hash_noput;
768         desc = &hp->md5_desc;
769
770         if (crypto_hash_init(desc))
771                 goto clear_hash;
772         if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, th->doff << 2))
773                 goto clear_hash;
774         if (tcp_md5_hash_header(hp, th))
775                 goto clear_hash;
776         if (tcp_md5_hash_key(hp, key))
777                 goto clear_hash;
778         if (crypto_hash_final(desc, md5_hash))
779                 goto clear_hash;
780
781         tcp_put_md5sig_pool();
782         return 0;
783
784 clear_hash:
785         tcp_put_md5sig_pool();
786 clear_hash_noput:
787         memset(md5_hash, 0, 16);
788         return 1;
789 }
790
791 static int tcp_v6_md5_hash_skb(char *md5_hash, struct tcp_md5sig_key *key,
792                                const struct sock *sk,
793                                const struct request_sock *req,
794                                const struct sk_buff *skb)
795 {
796         const struct in6_addr *saddr, *daddr;
797         struct tcp_md5sig_pool *hp;
798         struct hash_desc *desc;
799         const struct tcphdr *th = tcp_hdr(skb);
800
801         if (sk) {
802                 saddr = &inet6_sk(sk)->saddr;
803                 daddr = &inet6_sk(sk)->daddr;
804         } else if (req) {
805                 saddr = &inet6_rsk(req)->loc_addr;
806                 daddr = &inet6_rsk(req)->rmt_addr;
807         } else {
808                 const struct ipv6hdr *ip6h = ipv6_hdr(skb);
809                 saddr = &ip6h->saddr;
810                 daddr = &ip6h->daddr;
811         }
812
813         hp = tcp_get_md5sig_pool();
814         if (!hp)
815                 goto clear_hash_noput;
816         desc = &hp->md5_desc;
817
818         if (crypto_hash_init(desc))
819                 goto clear_hash;
820
821         if (tcp_v6_md5_hash_pseudoheader(hp, daddr, saddr, skb->len))
822                 goto clear_hash;
823         if (tcp_md5_hash_header(hp, th))
824                 goto clear_hash;
825         if (tcp_md5_hash_skb_data(hp, skb, th->doff << 2))
826                 goto clear_hash;
827         if (tcp_md5_hash_key(hp, key))
828                 goto clear_hash;
829         if (crypto_hash_final(desc, md5_hash))
830                 goto clear_hash;
831
832         tcp_put_md5sig_pool();
833         return 0;
834
835 clear_hash:
836         tcp_put_md5sig_pool();
837 clear_hash_noput:
838         memset(md5_hash, 0, 16);
839         return 1;
840 }
841
842 static int tcp_v6_inbound_md5_hash(struct sock *sk, const struct sk_buff *skb)
843 {
844         const __u8 *hash_location = NULL;
845         struct tcp_md5sig_key *hash_expected;
846         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
847         const struct tcphdr *th = tcp_hdr(skb);
848         int genhash;
849         u8 newhash[16];
850
851         hash_expected = tcp_v6_md5_do_lookup(sk, &ip6h->saddr);
852         hash_location = tcp_parse_md5sig_option(th);
853
854         /* We've parsed the options - do we have a hash? */
855         if (!hash_expected && !hash_location)
856                 return 0;
857
858         if (hash_expected && !hash_location) {
859                 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5NOTFOUND);
860                 return 1;
861         }
862
863         if (!hash_expected && hash_location) {
864                 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_TCPMD5UNEXPECTED);
865                 return 1;
866         }
867
868         /* check the signature */
869         genhash = tcp_v6_md5_hash_skb(newhash,
870                                       hash_expected,
871                                       NULL, NULL, skb);
872
873         if (genhash || memcmp(hash_location, newhash, 16) != 0) {
874                 if (net_ratelimit()) {
875                         printk(KERN_INFO "MD5 Hash %s for [%pI6c]:%u->[%pI6c]:%u\n",
876                                genhash ? "failed" : "mismatch",
877                                &ip6h->saddr, ntohs(th->source),
878                                &ip6h->daddr, ntohs(th->dest));
879                 }
880                 return 1;
881         }
882         return 0;
883 }
884 #endif
885
886 struct request_sock_ops tcp6_request_sock_ops __read_mostly = {
887         .family         =       AF_INET6,
888         .obj_size       =       sizeof(struct tcp6_request_sock),
889         .rtx_syn_ack    =       tcp_v6_rtx_synack,
890         .send_ack       =       tcp_v6_reqsk_send_ack,
891         .destructor     =       tcp_v6_reqsk_destructor,
892         .send_reset     =       tcp_v6_send_reset,
893         .syn_ack_timeout =      tcp_syn_ack_timeout,
894 };
895
896 #ifdef CONFIG_TCP_MD5SIG
897 static const struct tcp_request_sock_ops tcp_request_sock_ipv6_ops = {
898         .md5_lookup     =       tcp_v6_reqsk_md5_lookup,
899         .calc_md5_hash  =       tcp_v6_md5_hash_skb,
900 };
901 #endif
902
903 static void __tcp_v6_send_check(struct sk_buff *skb,
904                                 const struct in6_addr *saddr, const struct in6_addr *daddr)
905 {
906         struct tcphdr *th = tcp_hdr(skb);
907
908         if (skb->ip_summed == CHECKSUM_PARTIAL) {
909                 th->check = ~tcp_v6_check(skb->len, saddr, daddr, 0);
910                 skb->csum_start = skb_transport_header(skb) - skb->head;
911                 skb->csum_offset = offsetof(struct tcphdr, check);
912         } else {
913                 th->check = tcp_v6_check(skb->len, saddr, daddr,
914                                          csum_partial(th, th->doff << 2,
915                                                       skb->csum));
916         }
917 }
918
919 static void tcp_v6_send_check(struct sock *sk, struct sk_buff *skb)
920 {
921         struct ipv6_pinfo *np = inet6_sk(sk);
922
923         __tcp_v6_send_check(skb, &np->saddr, &np->daddr);
924 }
925
926 static int tcp_v6_gso_send_check(struct sk_buff *skb)
927 {
928         const struct ipv6hdr *ipv6h;
929         struct tcphdr *th;
930
931         if (!pskb_may_pull(skb, sizeof(*th)))
932                 return -EINVAL;
933
934         ipv6h = ipv6_hdr(skb);
935         th = tcp_hdr(skb);
936
937         th->check = 0;
938         skb->ip_summed = CHECKSUM_PARTIAL;
939         __tcp_v6_send_check(skb, &ipv6h->saddr, &ipv6h->daddr);
940         return 0;
941 }
942
943 static struct sk_buff **tcp6_gro_receive(struct sk_buff **head,
944                                          struct sk_buff *skb)
945 {
946         const struct ipv6hdr *iph = skb_gro_network_header(skb);
947
948         switch (skb->ip_summed) {
949         case CHECKSUM_COMPLETE:
950                 if (!tcp_v6_check(skb_gro_len(skb), &iph->saddr, &iph->daddr,
951                                   skb->csum)) {
952                         skb->ip_summed = CHECKSUM_UNNECESSARY;
953                         break;
954                 }
955
956                 /* fall through */
957         case CHECKSUM_NONE:
958                 NAPI_GRO_CB(skb)->flush = 1;
959                 return NULL;
960         }
961
962         return tcp_gro_receive(head, skb);
963 }
964
965 static int tcp6_gro_complete(struct sk_buff *skb)
966 {
967         const struct ipv6hdr *iph = ipv6_hdr(skb);
968         struct tcphdr *th = tcp_hdr(skb);
969
970         th->check = ~tcp_v6_check(skb->len - skb_transport_offset(skb),
971                                   &iph->saddr, &iph->daddr, 0);
972         skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
973
974         return tcp_gro_complete(skb);
975 }
976
977 static void tcp_v6_send_response(struct sk_buff *skb, u32 seq, u32 ack, u32 win,
978                                  u32 ts, struct tcp_md5sig_key *key, int rst, u8 tclass)
979 {
980         const struct tcphdr *th = tcp_hdr(skb);
981         struct tcphdr *t1;
982         struct sk_buff *buff;
983         struct flowi6 fl6;
984         struct net *net = dev_net(skb_dst(skb)->dev);
985         struct sock *ctl_sk = net->ipv6.tcp_sk;
986         unsigned int tot_len = sizeof(struct tcphdr);
987         struct dst_entry *dst;
988         __be32 *topt;
989
990         if (ts)
991                 tot_len += TCPOLEN_TSTAMP_ALIGNED;
992 #ifdef CONFIG_TCP_MD5SIG
993         if (key)
994                 tot_len += TCPOLEN_MD5SIG_ALIGNED;
995 #endif
996
997         buff = alloc_skb(MAX_HEADER + sizeof(struct ipv6hdr) + tot_len,
998                          GFP_ATOMIC);
999         if (buff == NULL)
1000                 return;
1001
1002         skb_reserve(buff, MAX_HEADER + sizeof(struct ipv6hdr) + tot_len);
1003
1004         t1 = (struct tcphdr *) skb_push(buff, tot_len);
1005         skb_reset_transport_header(buff);
1006
1007         /* Swap the send and the receive. */
1008         memset(t1, 0, sizeof(*t1));
1009         t1->dest = th->source;
1010         t1->source = th->dest;
1011         t1->doff = tot_len / 4;
1012         t1->seq = htonl(seq);
1013         t1->ack_seq = htonl(ack);
1014         t1->ack = !rst || !th->ack;
1015         t1->rst = rst;
1016         t1->window = htons(win);
1017
1018         topt = (__be32 *)(t1 + 1);
1019
1020         if (ts) {
1021                 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1022                                 (TCPOPT_TIMESTAMP << 8) | TCPOLEN_TIMESTAMP);
1023                 *topt++ = htonl(tcp_time_stamp);
1024                 *topt++ = htonl(ts);
1025         }
1026
1027 #ifdef CONFIG_TCP_MD5SIG
1028         if (key) {
1029                 *topt++ = htonl((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) |
1030                                 (TCPOPT_MD5SIG << 8) | TCPOLEN_MD5SIG);
1031                 tcp_v6_md5_hash_hdr((__u8 *)topt, key,
1032                                     &ipv6_hdr(skb)->saddr,
1033                                     &ipv6_hdr(skb)->daddr, t1);
1034         }
1035 #endif
1036
1037         memset(&fl6, 0, sizeof(fl6));
1038         ipv6_addr_copy(&fl6.daddr, &ipv6_hdr(skb)->saddr);
1039         ipv6_addr_copy(&fl6.saddr, &ipv6_hdr(skb)->daddr);
1040
1041         buff->ip_summed = CHECKSUM_PARTIAL;
1042         buff->csum = 0;
1043
1044         __tcp_v6_send_check(buff, &fl6.saddr, &fl6.daddr);
1045
1046         fl6.flowi6_proto = IPPROTO_TCP;
1047         if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
1048                 fl6.flowi6_oif = inet6_iif(skb);
1049         fl6.fl6_dport = t1->dest;
1050         fl6.fl6_sport = t1->source;
1051         security_skb_classify_flow(skb, flowi6_to_flowi(&fl6));
1052
1053         /* Pass a socket to ip6_dst_lookup either it is for RST
1054          * Underlying function will use this to retrieve the network
1055          * namespace
1056          */
1057         dst = ip6_dst_lookup_flow(ctl_sk, &fl6, NULL, false);
1058         if (!IS_ERR(dst)) {
1059                 skb_dst_set(buff, dst);
1060                 ip6_xmit(ctl_sk, buff, &fl6, NULL, tclass);
1061                 TCP_INC_STATS_BH(net, TCP_MIB_OUTSEGS);
1062                 if (rst)
1063                         TCP_INC_STATS_BH(net, TCP_MIB_OUTRSTS);
1064                 return;
1065         }
1066
1067         kfree_skb(buff);
1068 }
1069
1070 static void tcp_v6_send_reset(struct sock *sk, struct sk_buff *skb)
1071 {
1072         const struct tcphdr *th = tcp_hdr(skb);
1073         u32 seq = 0, ack_seq = 0;
1074         struct tcp_md5sig_key *key = NULL;
1075
1076         if (th->rst)
1077                 return;
1078
1079         if (!ipv6_unicast_destination(skb))
1080                 return;
1081
1082 #ifdef CONFIG_TCP_MD5SIG
1083         if (sk)
1084                 key = tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->saddr);
1085 #endif
1086
1087         if (th->ack)
1088                 seq = ntohl(th->ack_seq);
1089         else
1090                 ack_seq = ntohl(th->seq) + th->syn + th->fin + skb->len -
1091                           (th->doff << 2);
1092
1093         tcp_v6_send_response(skb, seq, ack_seq, 0, 0, key, 1, 0);
1094 }
1095
1096 static void tcp_v6_send_ack(struct sk_buff *skb, u32 seq, u32 ack, u32 win, u32 ts,
1097                             struct tcp_md5sig_key *key, u8 tclass)
1098 {
1099         tcp_v6_send_response(skb, seq, ack, win, ts, key, 0, tclass);
1100 }
1101
1102 static void tcp_v6_timewait_ack(struct sock *sk, struct sk_buff *skb)
1103 {
1104         struct inet_timewait_sock *tw = inet_twsk(sk);
1105         struct tcp_timewait_sock *tcptw = tcp_twsk(sk);
1106
1107         tcp_v6_send_ack(skb, tcptw->tw_snd_nxt, tcptw->tw_rcv_nxt,
1108                         tcptw->tw_rcv_wnd >> tw->tw_rcv_wscale,
1109                         tcptw->tw_ts_recent, tcp_twsk_md5_key(tcptw),
1110                         tw->tw_tclass);
1111
1112         inet_twsk_put(tw);
1113 }
1114
1115 static void tcp_v6_reqsk_send_ack(struct sock *sk, struct sk_buff *skb,
1116                                   struct request_sock *req)
1117 {
1118         tcp_v6_send_ack(skb, tcp_rsk(req)->snt_isn + 1, tcp_rsk(req)->rcv_isn + 1, req->rcv_wnd, req->ts_recent,
1119                         tcp_v6_md5_do_lookup(sk, &ipv6_hdr(skb)->daddr), 0);
1120 }
1121
1122
1123 static struct sock *tcp_v6_hnd_req(struct sock *sk,struct sk_buff *skb)
1124 {
1125         struct request_sock *req, **prev;
1126         const struct tcphdr *th = tcp_hdr(skb);
1127         struct sock *nsk;
1128
1129         /* Find possible connection requests. */
1130         req = inet6_csk_search_req(sk, &prev, th->source,
1131                                    &ipv6_hdr(skb)->saddr,
1132                                    &ipv6_hdr(skb)->daddr, inet6_iif(skb));
1133         if (req)
1134                 return tcp_check_req(sk, skb, req, prev);
1135
1136         nsk = __inet6_lookup_established(sock_net(sk), &tcp_hashinfo,
1137                         &ipv6_hdr(skb)->saddr, th->source,
1138                         &ipv6_hdr(skb)->daddr, ntohs(th->dest), inet6_iif(skb));
1139
1140         if (nsk) {
1141                 if (nsk->sk_state != TCP_TIME_WAIT) {
1142                         bh_lock_sock(nsk);
1143                         return nsk;
1144                 }
1145                 inet_twsk_put(inet_twsk(nsk));
1146                 return NULL;
1147         }
1148
1149 #ifdef CONFIG_SYN_COOKIES
1150         if (!th->syn)
1151                 sk = cookie_v6_check(sk, skb);
1152 #endif
1153         return sk;
1154 }
1155
1156 /* FIXME: this is substantially similar to the ipv4 code.
1157  * Can some kind of merge be done? -- erics
1158  */
1159 static int tcp_v6_conn_request(struct sock *sk, struct sk_buff *skb)
1160 {
1161         struct tcp_extend_values tmp_ext;
1162         struct tcp_options_received tmp_opt;
1163         const u8 *hash_location;
1164         struct request_sock *req;
1165         struct inet6_request_sock *treq;
1166         struct ipv6_pinfo *np = inet6_sk(sk);
1167         struct tcp_sock *tp = tcp_sk(sk);
1168         __u32 isn = TCP_SKB_CB(skb)->when;
1169         struct dst_entry *dst = NULL;
1170         int want_cookie = 0;
1171
1172         if (skb->protocol == htons(ETH_P_IP))
1173                 return tcp_v4_conn_request(sk, skb);
1174
1175         if (!ipv6_unicast_destination(skb))
1176                 goto drop;
1177
1178         if (inet_csk_reqsk_queue_is_full(sk) && !isn) {
1179                 want_cookie = tcp_syn_flood_action(sk, skb, "TCPv6");
1180                 if (!want_cookie)
1181                         goto drop;
1182         }
1183
1184         if (sk_acceptq_is_full(sk) && inet_csk_reqsk_queue_young(sk) > 1)
1185                 goto drop;
1186
1187         req = inet6_reqsk_alloc(&tcp6_request_sock_ops);
1188         if (req == NULL)
1189                 goto drop;
1190
1191 #ifdef CONFIG_TCP_MD5SIG
1192         tcp_rsk(req)->af_specific = &tcp_request_sock_ipv6_ops;
1193 #endif
1194
1195         tcp_clear_options(&tmp_opt);
1196         tmp_opt.mss_clamp = IPV6_MIN_MTU - sizeof(struct tcphdr) - sizeof(struct ipv6hdr);
1197         tmp_opt.user_mss = tp->rx_opt.user_mss;
1198         tcp_parse_options(skb, &tmp_opt, &hash_location, 0);
1199
1200         if (tmp_opt.cookie_plus > 0 &&
1201             tmp_opt.saw_tstamp &&
1202             !tp->rx_opt.cookie_out_never &&
1203             (sysctl_tcp_cookie_size > 0 ||
1204              (tp->cookie_values != NULL &&
1205               tp->cookie_values->cookie_desired > 0))) {
1206                 u8 *c;
1207                 u32 *d;
1208                 u32 *mess = &tmp_ext.cookie_bakery[COOKIE_DIGEST_WORDS];
1209                 int l = tmp_opt.cookie_plus - TCPOLEN_COOKIE_BASE;
1210
1211                 if (tcp_cookie_generator(&tmp_ext.cookie_bakery[0]) != 0)
1212                         goto drop_and_free;
1213
1214                 /* Secret recipe starts with IP addresses */
1215                 d = (__force u32 *)&ipv6_hdr(skb)->daddr.s6_addr32[0];
1216                 *mess++ ^= *d++;
1217                 *mess++ ^= *d++;
1218                 *mess++ ^= *d++;
1219                 *mess++ ^= *d++;
1220                 d = (__force u32 *)&ipv6_hdr(skb)->saddr.s6_addr32[0];
1221                 *mess++ ^= *d++;
1222                 *mess++ ^= *d++;
1223                 *mess++ ^= *d++;
1224                 *mess++ ^= *d++;
1225
1226                 /* plus variable length Initiator Cookie */
1227                 c = (u8 *)mess;
1228                 while (l-- > 0)
1229                         *c++ ^= *hash_location++;
1230
1231                 want_cookie = 0;        /* not our kind of cookie */
1232                 tmp_ext.cookie_out_never = 0; /* false */
1233                 tmp_ext.cookie_plus = tmp_opt.cookie_plus;
1234         } else if (!tp->rx_opt.cookie_in_always) {
1235                 /* redundant indications, but ensure initialization. */
1236                 tmp_ext.cookie_out_never = 1; /* true */
1237                 tmp_ext.cookie_plus = 0;
1238         } else {
1239                 goto drop_and_free;
1240         }
1241         tmp_ext.cookie_in_always = tp->rx_opt.cookie_in_always;
1242
1243         if (want_cookie && !tmp_opt.saw_tstamp)
1244                 tcp_clear_options(&tmp_opt);
1245
1246         tmp_opt.tstamp_ok = tmp_opt.saw_tstamp;
1247         tcp_openreq_init(req, &tmp_opt, skb);
1248
1249         treq = inet6_rsk(req);
1250         ipv6_addr_copy(&treq->rmt_addr, &ipv6_hdr(skb)->saddr);
1251         ipv6_addr_copy(&treq->loc_addr, &ipv6_hdr(skb)->daddr);
1252         if (!want_cookie || tmp_opt.tstamp_ok)
1253                 TCP_ECN_create_request(req, skb);
1254
1255         treq->iif = sk->sk_bound_dev_if;
1256
1257         /* So that link locals have meaning */
1258         if (!sk->sk_bound_dev_if &&
1259             ipv6_addr_type(&treq->rmt_addr) & IPV6_ADDR_LINKLOCAL)
1260                 treq->iif = inet6_iif(skb);
1261
1262         if (!isn) {
1263                 struct inet_peer *peer = NULL;
1264
1265                 if (ipv6_opt_accepted(sk, skb) ||
1266                     np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo ||
1267                     np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim) {
1268                         atomic_inc(&skb->users);
1269                         treq->pktopts = skb;
1270                 }
1271
1272                 if (want_cookie) {
1273                         isn = cookie_v6_init_sequence(sk, skb, &req->mss);
1274                         req->cookie_ts = tmp_opt.tstamp_ok;
1275                         goto have_isn;
1276                 }
1277
1278                 /* VJ's idea. We save last timestamp seen
1279                  * from the destination in peer table, when entering
1280                  * state TIME-WAIT, and check against it before
1281                  * accepting new connection request.
1282                  *
1283                  * If "isn" is not zero, this request hit alive
1284                  * timewait bucket, so that all the necessary checks
1285                  * are made in the function processing timewait state.
1286                  */
1287                 if (tmp_opt.saw_tstamp &&
1288                     tcp_death_row.sysctl_tw_recycle &&
1289                     (dst = inet6_csk_route_req(sk, req)) != NULL &&
1290                     (peer = rt6_get_peer((struct rt6_info *)dst)) != NULL &&
1291                     ipv6_addr_equal((struct in6_addr *)peer->daddr.addr.a6,
1292                                     &treq->rmt_addr)) {
1293                         inet_peer_refcheck(peer);
1294                         if ((u32)get_seconds() - peer->tcp_ts_stamp < TCP_PAWS_MSL &&
1295                             (s32)(peer->tcp_ts - req->ts_recent) >
1296                                                         TCP_PAWS_WINDOW) {
1297                                 NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_PAWSPASSIVEREJECTED);
1298                                 goto drop_and_release;
1299                         }
1300                 }
1301                 /* Kill the following clause, if you dislike this way. */
1302                 else if (!sysctl_tcp_syncookies &&
1303                          (sysctl_max_syn_backlog - inet_csk_reqsk_queue_len(sk) <
1304                           (sysctl_max_syn_backlog >> 2)) &&
1305                          (!peer || !peer->tcp_ts_stamp) &&
1306                          (!dst || !dst_metric(dst, RTAX_RTT))) {
1307                         /* Without syncookies last quarter of
1308                          * backlog is filled with destinations,
1309                          * proven to be alive.
1310                          * It means that we continue to communicate
1311                          * to destinations, already remembered
1312                          * to the moment of synflood.
1313                          */
1314                         LIMIT_NETDEBUG(KERN_DEBUG "TCP: drop open request from %pI6/%u\n",
1315                                        &treq->rmt_addr, ntohs(tcp_hdr(skb)->source));
1316                         goto drop_and_release;
1317                 }
1318
1319                 isn = tcp_v6_init_sequence(skb);
1320         }
1321 have_isn:
1322         tcp_rsk(req)->snt_isn = isn;
1323         tcp_rsk(req)->snt_synack = tcp_time_stamp;
1324
1325         security_inet_conn_request(sk, skb, req);
1326
1327         if (tcp_v6_send_synack(sk, req,
1328                                (struct request_values *)&tmp_ext) ||
1329             want_cookie)
1330                 goto drop_and_free;
1331
1332         inet6_csk_reqsk_queue_hash_add(sk, req, TCP_TIMEOUT_INIT);
1333         return 0;
1334
1335 drop_and_release:
1336         dst_release(dst);
1337 drop_and_free:
1338         reqsk_free(req);
1339 drop:
1340         return 0; /* don't send reset */
1341 }
1342
1343 static struct sock * tcp_v6_syn_recv_sock(struct sock *sk, struct sk_buff *skb,
1344                                           struct request_sock *req,
1345                                           struct dst_entry *dst)
1346 {
1347         struct inet6_request_sock *treq;
1348         struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
1349         struct tcp6_sock *newtcp6sk;
1350         struct inet_sock *newinet;
1351         struct tcp_sock *newtp;
1352         struct sock *newsk;
1353         struct ipv6_txoptions *opt;
1354 #ifdef CONFIG_TCP_MD5SIG
1355         struct tcp_md5sig_key *key;
1356 #endif
1357
1358         if (skb->protocol == htons(ETH_P_IP)) {
1359                 /*
1360                  *      v6 mapped
1361                  */
1362
1363                 newsk = tcp_v4_syn_recv_sock(sk, skb, req, dst);
1364
1365                 if (newsk == NULL)
1366                         return NULL;
1367
1368                 newtcp6sk = (struct tcp6_sock *)newsk;
1369                 inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1370
1371                 newinet = inet_sk(newsk);
1372                 newnp = inet6_sk(newsk);
1373                 newtp = tcp_sk(newsk);
1374
1375                 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1376
1377                 ipv6_addr_set_v4mapped(newinet->inet_daddr, &newnp->daddr);
1378
1379                 ipv6_addr_set_v4mapped(newinet->inet_saddr, &newnp->saddr);
1380
1381                 ipv6_addr_copy(&newnp->rcv_saddr, &newnp->saddr);
1382
1383                 inet_csk(newsk)->icsk_af_ops = &ipv6_mapped;
1384                 newsk->sk_backlog_rcv = tcp_v4_do_rcv;
1385 #ifdef CONFIG_TCP_MD5SIG
1386                 newtp->af_specific = &tcp_sock_ipv6_mapped_specific;
1387 #endif
1388
1389                 newnp->ipv6_ac_list = NULL;
1390                 newnp->ipv6_fl_list = NULL;
1391                 newnp->pktoptions  = NULL;
1392                 newnp->opt         = NULL;
1393                 newnp->mcast_oif   = inet6_iif(skb);
1394                 newnp->mcast_hops  = ipv6_hdr(skb)->hop_limit;
1395
1396                 /*
1397                  * No need to charge this sock to the relevant IPv6 refcnt debug socks count
1398                  * here, tcp_create_openreq_child now does this for us, see the comment in
1399                  * that function for the gory details. -acme
1400                  */
1401
1402                 /* It is tricky place. Until this moment IPv4 tcp
1403                    worked with IPv6 icsk.icsk_af_ops.
1404                    Sync it now.
1405                  */
1406                 tcp_sync_mss(newsk, inet_csk(newsk)->icsk_pmtu_cookie);
1407
1408                 return newsk;
1409         }
1410
1411         treq = inet6_rsk(req);
1412
1413         if (sk_acceptq_is_full(sk))
1414                 goto out_overflow;
1415
1416         if (!dst) {
1417                 dst = inet6_csk_route_req(sk, req);
1418                 if (!dst)
1419                         goto out;
1420         }
1421
1422         newsk = tcp_create_openreq_child(sk, req, skb);
1423         if (newsk == NULL)
1424                 goto out_nonewsk;
1425
1426         /*
1427          * No need to charge this sock to the relevant IPv6 refcnt debug socks
1428          * count here, tcp_create_openreq_child now does this for us, see the
1429          * comment in that function for the gory details. -acme
1430          */
1431
1432         newsk->sk_gso_type = SKB_GSO_TCPV6;
1433         __ip6_dst_store(newsk, dst, NULL, NULL);
1434
1435         newtcp6sk = (struct tcp6_sock *)newsk;
1436         inet_sk(newsk)->pinet6 = &newtcp6sk->inet6;
1437
1438         newtp = tcp_sk(newsk);
1439         newinet = inet_sk(newsk);
1440         newnp = inet6_sk(newsk);
1441
1442         memcpy(newnp, np, sizeof(struct ipv6_pinfo));
1443
1444         ipv6_addr_copy(&newnp->daddr, &treq->rmt_addr);
1445         ipv6_addr_copy(&newnp->saddr, &treq->loc_addr);
1446         ipv6_addr_copy(&newnp->rcv_saddr, &treq->loc_addr);
1447         newsk->sk_bound_dev_if = treq->iif;
1448
1449         /* Now IPv6 options...
1450
1451            First: no IPv4 options.
1452          */
1453         newinet->inet_opt = NULL;
1454         newnp->ipv6_ac_list = NULL;
1455         newnp->ipv6_fl_list = NULL;
1456
1457         /* Clone RX bits */
1458         newnp->rxopt.all = np->rxopt.all;
1459
1460         /* Clone pktoptions received with SYN */
1461         newnp->pktoptions = NULL;
1462         if (treq->pktopts != NULL) {
1463                 newnp->pktoptions = skb_clone(treq->pktopts, GFP_ATOMIC);
1464                 kfree_skb(treq->pktopts);
1465                 treq->pktopts = NULL;
1466                 if (newnp->pktoptions)
1467                         skb_set_owner_r(newnp->pktoptions, newsk);
1468         }
1469         newnp->opt        = NULL;
1470         newnp->mcast_oif  = inet6_iif(skb);
1471         newnp->mcast_hops = ipv6_hdr(skb)->hop_limit;
1472
1473         /* Clone native IPv6 options from listening socket (if any)
1474
1475            Yes, keeping reference count would be much more clever,
1476            but we make one more one thing there: reattach optmem
1477            to newsk.
1478          */
1479         opt = rcu_dereference(np->opt);
1480         if (opt) {
1481                 opt = ipv6_dup_options(newsk, opt);
1482                 RCU_INIT_POINTER(newnp->opt, opt);
1483         }
1484         inet_csk(newsk)->icsk_ext_hdr_len = 0;
1485         if (opt)
1486                 inet_csk(newsk)->icsk_ext_hdr_len = opt->opt_nflen +
1487                                                     opt->opt_flen;
1488
1489         tcp_mtup_init(newsk);
1490         tcp_sync_mss(newsk, dst_mtu(dst));
1491         newtp->advmss = dst_metric_advmss(dst);
1492         if (tcp_sk(sk)->rx_opt.user_mss &&
1493             tcp_sk(sk)->rx_opt.user_mss < newtp->advmss)
1494                 newtp->advmss = tcp_sk(sk)->rx_opt.user_mss;
1495
1496         tcp_initialize_rcv_mss(newsk);
1497         if (tcp_rsk(req)->snt_synack)
1498                 tcp_valid_rtt_meas(newsk,
1499                     tcp_time_stamp - tcp_rsk(req)->snt_synack);
1500         newtp->total_retrans = req->retrans;
1501
1502         newinet->inet_daddr = newinet->inet_saddr = LOOPBACK4_IPV6;
1503         newinet->inet_rcv_saddr = LOOPBACK4_IPV6;
1504
1505 #ifdef CONFIG_TCP_MD5SIG
1506         /* Copy over the MD5 key from the original socket */
1507         if ((key = tcp_v6_md5_do_lookup(sk, &newnp->daddr)) != NULL) {
1508                 /* We're using one, so create a matching key
1509                  * on the newsk structure. If we fail to get
1510                  * memory, then we end up not copying the key
1511                  * across. Shucks.
1512                  */
1513                 char *newkey = kmemdup(key->key, key->keylen, GFP_ATOMIC);
1514                 if (newkey != NULL)
1515                         tcp_v6_md5_do_add(newsk, &newnp->daddr,
1516                                           newkey, key->keylen);
1517         }
1518 #endif
1519
1520         if (__inet_inherit_port(sk, newsk) < 0) {
1521                 inet_csk_prepare_forced_close(newsk);
1522                 tcp_done(newsk);
1523                 goto out;
1524         }
1525         __inet6_hash(newsk, NULL);
1526
1527         return newsk;
1528
1529 out_overflow:
1530         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENOVERFLOWS);
1531 out_nonewsk:
1532         dst_release(dst);
1533 out:
1534         NET_INC_STATS_BH(sock_net(sk), LINUX_MIB_LISTENDROPS);
1535         return NULL;
1536 }
1537
1538 static __sum16 tcp_v6_checksum_init(struct sk_buff *skb)
1539 {
1540         if (skb->ip_summed == CHECKSUM_COMPLETE) {
1541                 if (!tcp_v6_check(skb->len, &ipv6_hdr(skb)->saddr,
1542                                   &ipv6_hdr(skb)->daddr, skb->csum)) {
1543                         skb->ip_summed = CHECKSUM_UNNECESSARY;
1544                         return 0;
1545                 }
1546         }
1547
1548         skb->csum = ~csum_unfold(tcp_v6_check(skb->len,
1549                                               &ipv6_hdr(skb)->saddr,
1550                                               &ipv6_hdr(skb)->daddr, 0));
1551
1552         if (skb->len <= 76) {
1553                 return __skb_checksum_complete(skb);
1554         }
1555         return 0;
1556 }
1557
1558 /* The socket must have it's spinlock held when we get
1559  * here.
1560  *
1561  * We have a potential double-lock case here, so even when
1562  * doing backlog processing we use the BH locking scheme.
1563  * This is because we cannot sleep with the original spinlock
1564  * held.
1565  */
1566 static int tcp_v6_do_rcv(struct sock *sk, struct sk_buff *skb)
1567 {
1568         struct ipv6_pinfo *np = inet6_sk(sk);
1569         struct tcp_sock *tp;
1570         struct sk_buff *opt_skb = NULL;
1571
1572         /* Imagine: socket is IPv6. IPv4 packet arrives,
1573            goes to IPv4 receive handler and backlogged.
1574            From backlog it always goes here. Kerboom...
1575            Fortunately, tcp_rcv_established and rcv_established
1576            handle them correctly, but it is not case with
1577            tcp_v6_hnd_req and tcp_v6_send_reset().   --ANK
1578          */
1579
1580         if (skb->protocol == htons(ETH_P_IP))
1581                 return tcp_v4_do_rcv(sk, skb);
1582
1583 #ifdef CONFIG_TCP_MD5SIG
1584         if (tcp_v6_inbound_md5_hash (sk, skb))
1585                 goto discard;
1586 #endif
1587
1588         if (tcp_filter(sk, skb))
1589                 goto discard;
1590
1591         /*
1592          *      socket locking is here for SMP purposes as backlog rcv
1593          *      is currently called with bh processing disabled.
1594          */
1595
1596         /* Do Stevens' IPV6_PKTOPTIONS.
1597
1598            Yes, guys, it is the only place in our code, where we
1599            may make it not affecting IPv4.
1600            The rest of code is protocol independent,
1601            and I do not like idea to uglify IPv4.
1602
1603            Actually, all the idea behind IPV6_PKTOPTIONS
1604            looks not very well thought. For now we latch
1605            options, received in the last packet, enqueued
1606            by tcp. Feel free to propose better solution.
1607                                                --ANK (980728)
1608          */
1609         if (np->rxopt.all)
1610                 opt_skb = skb_clone(skb, GFP_ATOMIC);
1611
1612         if (sk->sk_state == TCP_ESTABLISHED) { /* Fast path */
1613                 sock_rps_save_rxhash(sk, skb);
1614                 if (tcp_rcv_established(sk, skb, tcp_hdr(skb), skb->len))
1615                         goto reset;
1616                 if (opt_skb)
1617                         goto ipv6_pktoptions;
1618                 return 0;
1619         }
1620
1621         if (skb->len < tcp_hdrlen(skb) || tcp_checksum_complete(skb))
1622                 goto csum_err;
1623
1624         if (sk->sk_state == TCP_LISTEN) {
1625                 struct sock *nsk = tcp_v6_hnd_req(sk, skb);
1626                 if (!nsk)
1627                         goto discard;
1628
1629                 /*
1630                  * Queue it on the new socket if the new socket is active,
1631                  * otherwise we just shortcircuit this and continue with
1632                  * the new socket..
1633                  */
1634                 if(nsk != sk) {
1635                         sock_rps_save_rxhash(nsk, skb);
1636                         if (tcp_child_process(sk, nsk, skb))
1637                                 goto reset;
1638                         if (opt_skb)
1639                                 __kfree_skb(opt_skb);
1640                         return 0;
1641                 }
1642         } else
1643                 sock_rps_save_rxhash(sk, skb);
1644
1645         if (tcp_rcv_state_process(sk, skb, tcp_hdr(skb), skb->len))
1646                 goto reset;
1647         if (opt_skb)
1648                 goto ipv6_pktoptions;
1649         return 0;
1650
1651 reset:
1652         tcp_v6_send_reset(sk, skb);
1653 discard:
1654         if (opt_skb)
1655                 __kfree_skb(opt_skb);
1656         kfree_skb(skb);
1657         return 0;
1658 csum_err:
1659         TCP_INC_STATS_BH(sock_net(sk), TCP_MIB_INERRS);
1660         goto discard;
1661
1662
1663 ipv6_pktoptions:
1664         /* Do you ask, what is it?
1665
1666            1. skb was enqueued by tcp.
1667            2. skb is added to tail of read queue, rather than out of order.
1668            3. socket is not in passive state.
1669            4. Finally, it really contains options, which user wants to receive.
1670          */
1671         tp = tcp_sk(sk);
1672         if (TCP_SKB_CB(opt_skb)->end_seq == tp->rcv_nxt &&
1673             !((1 << sk->sk_state) & (TCPF_CLOSE | TCPF_LISTEN))) {
1674                 if (np->rxopt.bits.rxinfo || np->rxopt.bits.rxoinfo)
1675                         np->mcast_oif = inet6_iif(opt_skb);
1676                 if (np->rxopt.bits.rxhlim || np->rxopt.bits.rxohlim)
1677                         np->mcast_hops = ipv6_hdr(opt_skb)->hop_limit;
1678                 if (ipv6_opt_accepted(sk, opt_skb)) {
1679                         skb_set_owner_r(opt_skb, sk);
1680                         opt_skb = xchg(&np->pktoptions, opt_skb);
1681                 } else {
1682                         __kfree_skb(opt_skb);
1683                         opt_skb = xchg(&np->pktoptions, NULL);
1684                 }
1685         }
1686
1687         kfree_skb(opt_skb);
1688         return 0;
1689 }
1690
1691 static int tcp_v6_rcv(struct sk_buff *skb)
1692 {
1693         const struct tcphdr *th;
1694         const struct ipv6hdr *hdr;
1695         struct sock *sk;
1696         int ret;
1697         struct net *net = dev_net(skb->dev);
1698
1699         if (skb->pkt_type != PACKET_HOST)
1700                 goto discard_it;
1701
1702         /*
1703          *      Count it even if it's bad.
1704          */
1705         TCP_INC_STATS_BH(net, TCP_MIB_INSEGS);
1706
1707         if (!pskb_may_pull(skb, sizeof(struct tcphdr)))
1708                 goto discard_it;
1709
1710         th = tcp_hdr(skb);
1711
1712         if (th->doff < sizeof(struct tcphdr)/4)
1713                 goto bad_packet;
1714         if (!pskb_may_pull(skb, th->doff*4))
1715                 goto discard_it;
1716
1717         if (!skb_csum_unnecessary(skb) && tcp_v6_checksum_init(skb))
1718                 goto bad_packet;
1719
1720         th = tcp_hdr(skb);
1721         hdr = ipv6_hdr(skb);
1722         TCP_SKB_CB(skb)->seq = ntohl(th->seq);
1723         TCP_SKB_CB(skb)->end_seq = (TCP_SKB_CB(skb)->seq + th->syn + th->fin +
1724                                     skb->len - th->doff*4);
1725         TCP_SKB_CB(skb)->ack_seq = ntohl(th->ack_seq);
1726         TCP_SKB_CB(skb)->when = 0;
1727         TCP_SKB_CB(skb)->ip_dsfield = ipv6_get_dsfield(hdr);
1728         TCP_SKB_CB(skb)->sacked = 0;
1729
1730         sk = __inet6_lookup_skb(&tcp_hashinfo, skb, th->source, th->dest);
1731         if (!sk)
1732                 goto no_tcp_socket;
1733
1734 process:
1735         if (sk->sk_state == TCP_TIME_WAIT)
1736                 goto do_time_wait;
1737
1738         if (hdr->hop_limit < inet6_sk(sk)->min_hopcount) {
1739                 NET_INC_STATS_BH(net, LINUX_MIB_TCPMINTTLDROP);
1740                 goto discard_and_relse;
1741         }
1742
1743         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
1744                 goto discard_and_relse;
1745
1746         if (tcp_filter(sk, skb))
1747                 goto discard_and_relse;
1748         th = (const struct tcphdr *)skb->data;
1749         hdr = ipv6_hdr(skb);
1750
1751         skb->dev = NULL;
1752
1753         bh_lock_sock_nested(sk);
1754         ret = 0;
1755         if (!sock_owned_by_user(sk)) {
1756 #ifdef CONFIG_NET_DMA
1757                 struct tcp_sock *tp = tcp_sk(sk);
1758                 if (!tp->ucopy.dma_chan && tp->ucopy.pinned_list)
1759                         tp->ucopy.dma_chan = dma_find_channel(DMA_MEMCPY);
1760                 if (tp->ucopy.dma_chan)
1761                         ret = tcp_v6_do_rcv(sk, skb);
1762                 else
1763 #endif
1764                 {
1765                         if (!tcp_prequeue(sk, skb))
1766                                 ret = tcp_v6_do_rcv(sk, skb);
1767                 }
1768         } else if (unlikely(sk_add_backlog(sk, skb))) {
1769                 bh_unlock_sock(sk);
1770                 NET_INC_STATS_BH(net, LINUX_MIB_TCPBACKLOGDROP);
1771                 goto discard_and_relse;
1772         }
1773         bh_unlock_sock(sk);
1774
1775         sock_put(sk);
1776         return ret ? -1 : 0;
1777
1778 no_tcp_socket:
1779         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1780                 goto discard_it;
1781
1782         if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1783 bad_packet:
1784                 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1785         } else {
1786                 tcp_v6_send_reset(NULL, skb);
1787         }
1788
1789 discard_it:
1790
1791         /*
1792          *      Discard frame
1793          */
1794
1795         kfree_skb(skb);
1796         return 0;
1797
1798 discard_and_relse:
1799         sock_put(sk);
1800         goto discard_it;
1801
1802 do_time_wait:
1803         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb)) {
1804                 inet_twsk_put(inet_twsk(sk));
1805                 goto discard_it;
1806         }
1807
1808         if (skb->len < (th->doff<<2) || tcp_checksum_complete(skb)) {
1809                 TCP_INC_STATS_BH(net, TCP_MIB_INERRS);
1810                 inet_twsk_put(inet_twsk(sk));
1811                 goto discard_it;
1812         }
1813
1814         switch (tcp_timewait_state_process(inet_twsk(sk), skb, th)) {
1815         case TCP_TW_SYN:
1816         {
1817                 struct sock *sk2;
1818
1819                 sk2 = inet6_lookup_listener(dev_net(skb->dev), &tcp_hashinfo,
1820                                             &ipv6_hdr(skb)->daddr,
1821                                             ntohs(th->dest), inet6_iif(skb));
1822                 if (sk2 != NULL) {
1823                         struct inet_timewait_sock *tw = inet_twsk(sk);
1824                         inet_twsk_deschedule(tw, &tcp_death_row);
1825                         inet_twsk_put(tw);
1826                         sk = sk2;
1827                         goto process;
1828                 }
1829                 /* Fall through to ACK */
1830         }
1831         case TCP_TW_ACK:
1832                 tcp_v6_timewait_ack(sk, skb);
1833                 break;
1834         case TCP_TW_RST:
1835                 goto no_tcp_socket;
1836         case TCP_TW_SUCCESS:;
1837         }
1838         goto discard_it;
1839 }
1840
1841 static struct inet_peer *tcp_v6_get_peer(struct sock *sk, bool *release_it)
1842 {
1843         struct rt6_info *rt = (struct rt6_info *) __sk_dst_get(sk);
1844         struct ipv6_pinfo *np = inet6_sk(sk);
1845         struct inet_peer *peer;
1846
1847         if (!rt ||
1848             !ipv6_addr_equal(&np->daddr, &rt->rt6i_dst.addr)) {
1849                 peer = inet_getpeer_v6(&np->daddr, 1);
1850                 *release_it = true;
1851         } else {
1852                 if (!rt->rt6i_peer)
1853                         rt6_bind_peer(rt, 1);
1854                 peer = rt->rt6i_peer;
1855                 *release_it = false;
1856         }
1857
1858         return peer;
1859 }
1860
1861 static void *tcp_v6_tw_get_peer(struct sock *sk)
1862 {
1863         const struct inet6_timewait_sock *tw6 = inet6_twsk(sk);
1864         const struct inet_timewait_sock *tw = inet_twsk(sk);
1865
1866         if (tw->tw_family == AF_INET)
1867                 return tcp_v4_tw_get_peer(sk);
1868
1869         return inet_getpeer_v6(&tw6->tw_v6_daddr, 1);
1870 }
1871
1872 static struct timewait_sock_ops tcp6_timewait_sock_ops = {
1873         .twsk_obj_size  = sizeof(struct tcp6_timewait_sock),
1874         .twsk_unique    = tcp_twsk_unique,
1875         .twsk_destructor= tcp_twsk_destructor,
1876         .twsk_getpeer   = tcp_v6_tw_get_peer,
1877 };
1878
1879 static const struct inet_connection_sock_af_ops ipv6_specific = {
1880         .queue_xmit        = inet6_csk_xmit,
1881         .send_check        = tcp_v6_send_check,
1882         .rebuild_header    = inet6_sk_rebuild_header,
1883         .conn_request      = tcp_v6_conn_request,
1884         .syn_recv_sock     = tcp_v6_syn_recv_sock,
1885         .get_peer          = tcp_v6_get_peer,
1886         .net_header_len    = sizeof(struct ipv6hdr),
1887         .setsockopt        = ipv6_setsockopt,
1888         .getsockopt        = ipv6_getsockopt,
1889         .addr2sockaddr     = inet6_csk_addr2sockaddr,
1890         .sockaddr_len      = sizeof(struct sockaddr_in6),
1891         .bind_conflict     = inet6_csk_bind_conflict,
1892 #ifdef CONFIG_COMPAT
1893         .compat_setsockopt = compat_ipv6_setsockopt,
1894         .compat_getsockopt = compat_ipv6_getsockopt,
1895 #endif
1896 };
1897
1898 #ifdef CONFIG_TCP_MD5SIG
1899 static const struct tcp_sock_af_ops tcp_sock_ipv6_specific = {
1900         .md5_lookup     =       tcp_v6_md5_lookup,
1901         .calc_md5_hash  =       tcp_v6_md5_hash_skb,
1902         .md5_add        =       tcp_v6_md5_add_func,
1903         .md5_parse      =       tcp_v6_parse_md5_keys,
1904 };
1905 #endif
1906
1907 /*
1908  *      TCP over IPv4 via INET6 API
1909  */
1910
1911 static const struct inet_connection_sock_af_ops ipv6_mapped = {
1912         .queue_xmit        = ip_queue_xmit,
1913         .send_check        = tcp_v4_send_check,
1914         .rebuild_header    = inet_sk_rebuild_header,
1915         .conn_request      = tcp_v6_conn_request,
1916         .syn_recv_sock     = tcp_v6_syn_recv_sock,
1917         .get_peer          = tcp_v4_get_peer,
1918         .net_header_len    = sizeof(struct iphdr),
1919         .setsockopt        = ipv6_setsockopt,
1920         .getsockopt        = ipv6_getsockopt,
1921         .addr2sockaddr     = inet6_csk_addr2sockaddr,
1922         .sockaddr_len      = sizeof(struct sockaddr_in6),
1923         .bind_conflict     = inet6_csk_bind_conflict,
1924 #ifdef CONFIG_COMPAT
1925         .compat_setsockopt = compat_ipv6_setsockopt,
1926         .compat_getsockopt = compat_ipv6_getsockopt,
1927 #endif
1928 };
1929
1930 #ifdef CONFIG_TCP_MD5SIG
1931 static const struct tcp_sock_af_ops tcp_sock_ipv6_mapped_specific = {
1932         .md5_lookup     =       tcp_v4_md5_lookup,
1933         .calc_md5_hash  =       tcp_v4_md5_hash_skb,
1934         .md5_add        =       tcp_v6_md5_add_func,
1935         .md5_parse      =       tcp_v6_parse_md5_keys,
1936 };
1937 #endif
1938
1939 /* NOTE: A lot of things set to zero explicitly by call to
1940  *       sk_alloc() so need not be done here.
1941  */
1942 static int tcp_v6_init_sock(struct sock *sk)
1943 {
1944         struct inet_connection_sock *icsk = inet_csk(sk);
1945         struct tcp_sock *tp = tcp_sk(sk);
1946
1947         skb_queue_head_init(&tp->out_of_order_queue);
1948         tcp_init_xmit_timers(sk);
1949         tcp_prequeue_init(tp);
1950
1951         icsk->icsk_rto = TCP_TIMEOUT_INIT;
1952         tp->mdev = TCP_TIMEOUT_INIT;
1953
1954         /* So many TCP implementations out there (incorrectly) count the
1955          * initial SYN frame in their delayed-ACK and congestion control
1956          * algorithms that we must have the following bandaid to talk
1957          * efficiently to them.  -DaveM
1958          */
1959         tp->snd_cwnd = 2;
1960
1961         /* See draft-stevens-tcpca-spec-01 for discussion of the
1962          * initialization of these values.
1963          */
1964         tp->snd_ssthresh = TCP_INFINITE_SSTHRESH;
1965         tp->snd_cwnd_clamp = ~0;
1966         tp->mss_cache = TCP_MSS_DEFAULT;
1967
1968         tp->reordering = sysctl_tcp_reordering;
1969
1970         sk->sk_state = TCP_CLOSE;
1971
1972         icsk->icsk_af_ops = &ipv6_specific;
1973         icsk->icsk_ca_ops = &tcp_init_congestion_ops;
1974         icsk->icsk_sync_mss = tcp_sync_mss;
1975         sk->sk_write_space = sk_stream_write_space;
1976         sock_set_flag(sk, SOCK_USE_WRITE_QUEUE);
1977
1978 #ifdef CONFIG_TCP_MD5SIG
1979         tp->af_specific = &tcp_sock_ipv6_specific;
1980 #endif
1981
1982         /* TCP Cookie Transactions */
1983         if (sysctl_tcp_cookie_size > 0) {
1984                 /* Default, cookies without s_data_payload. */
1985                 tp->cookie_values =
1986                         kzalloc(sizeof(*tp->cookie_values),
1987                                 sk->sk_allocation);
1988                 if (tp->cookie_values != NULL)
1989                         kref_init(&tp->cookie_values->kref);
1990         }
1991         /* Presumed zeroed, in order of appearance:
1992          *      cookie_in_always, cookie_out_never,
1993          *      s_data_constant, s_data_in, s_data_out
1994          */
1995         sk->sk_sndbuf = sysctl_tcp_wmem[1];
1996         sk->sk_rcvbuf = sysctl_tcp_rmem[1];
1997
1998         local_bh_disable();
1999         percpu_counter_inc(&tcp_sockets_allocated);
2000         local_bh_enable();
2001
2002         return 0;
2003 }
2004
2005 static void tcp_v6_destroy_sock(struct sock *sk)
2006 {
2007 #ifdef CONFIG_TCP_MD5SIG
2008         /* Clean up the MD5 key list */
2009         if (tcp_sk(sk)->md5sig_info)
2010                 tcp_v6_clear_md5_list(sk);
2011 #endif
2012         tcp_v4_destroy_sock(sk);
2013         inet6_destroy_sock(sk);
2014 }
2015
2016 #ifdef CONFIG_PROC_FS
2017 /* Proc filesystem TCPv6 sock list dumping. */
2018 static void get_openreq6(struct seq_file *seq,
2019                          const struct sock *sk, struct request_sock *req, int i, int uid)
2020 {
2021         int ttd = req->expires - jiffies;
2022         const struct in6_addr *src = &inet6_rsk(req)->loc_addr;
2023         const struct in6_addr *dest = &inet6_rsk(req)->rmt_addr;
2024
2025         if (ttd < 0)
2026                 ttd = 0;
2027
2028         seq_printf(seq,
2029                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2030                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2031                    i,
2032                    src->s6_addr32[0], src->s6_addr32[1],
2033                    src->s6_addr32[2], src->s6_addr32[3],
2034                    ntohs(inet_rsk(req)->loc_port),
2035                    dest->s6_addr32[0], dest->s6_addr32[1],
2036                    dest->s6_addr32[2], dest->s6_addr32[3],
2037                    ntohs(inet_rsk(req)->rmt_port),
2038                    TCP_SYN_RECV,
2039                    0,0, /* could print option size, but that is af dependent. */
2040                    1,   /* timers active (only the expire timer) */
2041                    jiffies_to_clock_t(ttd),
2042                    req->retrans,
2043                    uid,
2044                    0,  /* non standard timer */
2045                    0, /* open_requests have no inode */
2046                    0, req);
2047 }
2048
2049 static void get_tcp6_sock(struct seq_file *seq, struct sock *sp, int i)
2050 {
2051         const struct in6_addr *dest, *src;
2052         __u16 destp, srcp;
2053         int timer_active;
2054         unsigned long timer_expires;
2055         const struct inet_sock *inet = inet_sk(sp);
2056         const struct tcp_sock *tp = tcp_sk(sp);
2057         const struct inet_connection_sock *icsk = inet_csk(sp);
2058         const struct ipv6_pinfo *np = inet6_sk(sp);
2059
2060         dest  = &np->daddr;
2061         src   = &np->rcv_saddr;
2062         destp = ntohs(inet->inet_dport);
2063         srcp  = ntohs(inet->inet_sport);
2064
2065         if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
2066                 timer_active    = 1;
2067                 timer_expires   = icsk->icsk_timeout;
2068         } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
2069                 timer_active    = 4;
2070                 timer_expires   = icsk->icsk_timeout;
2071         } else if (timer_pending(&sp->sk_timer)) {
2072                 timer_active    = 2;
2073                 timer_expires   = sp->sk_timer.expires;
2074         } else {
2075                 timer_active    = 0;
2076                 timer_expires = jiffies;
2077         }
2078
2079         seq_printf(seq,
2080                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2081                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %lu %lu %u %u %d\n",
2082                    i,
2083                    src->s6_addr32[0], src->s6_addr32[1],
2084                    src->s6_addr32[2], src->s6_addr32[3], srcp,
2085                    dest->s6_addr32[0], dest->s6_addr32[1],
2086                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
2087                    sp->sk_state,
2088                    tp->write_seq-tp->snd_una,
2089                    (sp->sk_state == TCP_LISTEN) ? sp->sk_ack_backlog : (tp->rcv_nxt - tp->copied_seq),
2090                    timer_active,
2091                    jiffies_to_clock_t(timer_expires - jiffies),
2092                    icsk->icsk_retransmits,
2093                    sock_i_uid(sp),
2094                    icsk->icsk_probes_out,
2095                    sock_i_ino(sp),
2096                    atomic_read(&sp->sk_refcnt), sp,
2097                    jiffies_to_clock_t(icsk->icsk_rto),
2098                    jiffies_to_clock_t(icsk->icsk_ack.ato),
2099                    (icsk->icsk_ack.quick << 1 ) | icsk->icsk_ack.pingpong,
2100                    tp->snd_cwnd,
2101                    tcp_in_initial_slowstart(tp) ? -1 : tp->snd_ssthresh
2102                    );
2103 }
2104
2105 static void get_timewait6_sock(struct seq_file *seq,
2106                                struct inet_timewait_sock *tw, int i)
2107 {
2108         const struct in6_addr *dest, *src;
2109         __u16 destp, srcp;
2110         const struct inet6_timewait_sock *tw6 = inet6_twsk((struct sock *)tw);
2111         int ttd = tw->tw_ttd - jiffies;
2112
2113         if (ttd < 0)
2114                 ttd = 0;
2115
2116         dest = &tw6->tw_v6_daddr;
2117         src  = &tw6->tw_v6_rcv_saddr;
2118         destp = ntohs(tw->tw_dport);
2119         srcp  = ntohs(tw->tw_sport);
2120
2121         seq_printf(seq,
2122                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
2123                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %d %d %pK\n",
2124                    i,
2125                    src->s6_addr32[0], src->s6_addr32[1],
2126                    src->s6_addr32[2], src->s6_addr32[3], srcp,
2127                    dest->s6_addr32[0], dest->s6_addr32[1],
2128                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
2129                    tw->tw_substate, 0, 0,
2130                    3, jiffies_to_clock_t(ttd), 0, 0, 0, 0,
2131                    atomic_read(&tw->tw_refcnt), tw);
2132 }
2133
2134 static int tcp6_seq_show(struct seq_file *seq, void *v)
2135 {
2136         struct tcp_iter_state *st;
2137
2138         if (v == SEQ_START_TOKEN) {
2139                 seq_puts(seq,
2140                          "  sl  "
2141                          "local_address                         "
2142                          "remote_address                        "
2143                          "st tx_queue rx_queue tr tm->when retrnsmt"
2144                          "   uid  timeout inode\n");
2145                 goto out;
2146         }
2147         st = seq->private;
2148
2149         switch (st->state) {
2150         case TCP_SEQ_STATE_LISTENING:
2151         case TCP_SEQ_STATE_ESTABLISHED:
2152                 get_tcp6_sock(seq, v, st->num);
2153                 break;
2154         case TCP_SEQ_STATE_OPENREQ:
2155                 get_openreq6(seq, st->syn_wait_sk, v, st->num, st->uid);
2156                 break;
2157         case TCP_SEQ_STATE_TIME_WAIT:
2158                 get_timewait6_sock(seq, v, st->num);
2159                 break;
2160         }
2161 out:
2162         return 0;
2163 }
2164
2165 static const struct file_operations tcp6_afinfo_seq_fops = {
2166         .owner   = THIS_MODULE,
2167         .open    = tcp_seq_open,
2168         .read    = seq_read,
2169         .llseek  = seq_lseek,
2170         .release = seq_release_net
2171 };
2172
2173 static struct tcp_seq_afinfo tcp6_seq_afinfo = {
2174         .name           = "tcp6",
2175         .family         = AF_INET6,
2176         .seq_fops       = &tcp6_afinfo_seq_fops,
2177         .seq_ops        = {
2178                 .show           = tcp6_seq_show,
2179         },
2180 };
2181
2182 int __net_init tcp6_proc_init(struct net *net)
2183 {
2184         return tcp_proc_register(net, &tcp6_seq_afinfo);
2185 }
2186
2187 void tcp6_proc_exit(struct net *net)
2188 {
2189         tcp_proc_unregister(net, &tcp6_seq_afinfo);
2190 }
2191 #endif
2192
2193 static void tcp_v6_clear_sk(struct sock *sk, int size)
2194 {
2195         struct inet_sock *inet = inet_sk(sk);
2196
2197         /* we do not want to clear pinet6 field, because of RCU lookups */
2198         sk_prot_clear_nulls(sk, offsetof(struct inet_sock, pinet6));
2199
2200         size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
2201         memset(&inet->pinet6 + 1, 0, size);
2202 }
2203
2204 struct proto tcpv6_prot = {
2205         .name                   = "TCPv6",
2206         .owner                  = THIS_MODULE,
2207         .close                  = tcp_close,
2208         .connect                = tcp_v6_connect,
2209         .disconnect             = tcp_disconnect,
2210         .accept                 = inet_csk_accept,
2211         .ioctl                  = tcp_ioctl,
2212         .init                   = tcp_v6_init_sock,
2213         .destroy                = tcp_v6_destroy_sock,
2214         .shutdown               = tcp_shutdown,
2215         .setsockopt             = tcp_setsockopt,
2216         .getsockopt             = tcp_getsockopt,
2217         .recvmsg                = tcp_recvmsg,
2218         .sendmsg                = tcp_sendmsg,
2219         .sendpage               = tcp_sendpage,
2220         .backlog_rcv            = tcp_v6_do_rcv,
2221         .hash                   = tcp_v6_hash,
2222         .unhash                 = inet_unhash,
2223         .get_port               = inet_csk_get_port,
2224         .enter_memory_pressure  = tcp_enter_memory_pressure,
2225         .sockets_allocated      = &tcp_sockets_allocated,
2226         .memory_allocated       = &tcp_memory_allocated,
2227         .memory_pressure        = &tcp_memory_pressure,
2228         .orphan_count           = &tcp_orphan_count,
2229         .sysctl_mem             = sysctl_tcp_mem,
2230         .sysctl_wmem            = sysctl_tcp_wmem,
2231         .sysctl_rmem            = sysctl_tcp_rmem,
2232         .max_header             = MAX_TCP_HEADER,
2233         .obj_size               = sizeof(struct tcp6_sock),
2234         .slab_flags             = SLAB_DESTROY_BY_RCU,
2235         .twsk_prot              = &tcp6_timewait_sock_ops,
2236         .rsk_prot               = &tcp6_request_sock_ops,
2237         .h.hashinfo             = &tcp_hashinfo,
2238         .no_autobind            = true,
2239 #ifdef CONFIG_COMPAT
2240         .compat_setsockopt      = compat_tcp_setsockopt,
2241         .compat_getsockopt      = compat_tcp_getsockopt,
2242 #endif
2243         .clear_sk               = tcp_v6_clear_sk,
2244 };
2245
2246 static const struct inet6_protocol tcpv6_protocol = {
2247         .handler        =       tcp_v6_rcv,
2248         .err_handler    =       tcp_v6_err,
2249         .gso_send_check =       tcp_v6_gso_send_check,
2250         .gso_segment    =       tcp_tso_segment,
2251         .gro_receive    =       tcp6_gro_receive,
2252         .gro_complete   =       tcp6_gro_complete,
2253         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
2254 };
2255
2256 static struct inet_protosw tcpv6_protosw = {
2257         .type           =       SOCK_STREAM,
2258         .protocol       =       IPPROTO_TCP,
2259         .prot           =       &tcpv6_prot,
2260         .ops            =       &inet6_stream_ops,
2261         .no_check       =       0,
2262         .flags          =       INET_PROTOSW_PERMANENT |
2263                                 INET_PROTOSW_ICSK,
2264 };
2265
2266 static int __net_init tcpv6_net_init(struct net *net)
2267 {
2268         return inet_ctl_sock_create(&net->ipv6.tcp_sk, PF_INET6,
2269                                     SOCK_RAW, IPPROTO_TCP, net);
2270 }
2271
2272 static void __net_exit tcpv6_net_exit(struct net *net)
2273 {
2274         inet_ctl_sock_destroy(net->ipv6.tcp_sk);
2275 }
2276
2277 static void __net_exit tcpv6_net_exit_batch(struct list_head *net_exit_list)
2278 {
2279         inet_twsk_purge(&tcp_hashinfo, &tcp_death_row, AF_INET6);
2280 }
2281
2282 static struct pernet_operations tcpv6_net_ops = {
2283         .init       = tcpv6_net_init,
2284         .exit       = tcpv6_net_exit,
2285         .exit_batch = tcpv6_net_exit_batch,
2286 };
2287
2288 int __init tcpv6_init(void)
2289 {
2290         int ret;
2291
2292         ret = inet6_add_protocol(&tcpv6_protocol, IPPROTO_TCP);
2293         if (ret)
2294                 goto out;
2295
2296         /* register inet6 protocol */
2297         ret = inet6_register_protosw(&tcpv6_protosw);
2298         if (ret)
2299                 goto out_tcpv6_protocol;
2300
2301         ret = register_pernet_subsys(&tcpv6_net_ops);
2302         if (ret)
2303                 goto out_tcpv6_protosw;
2304 out:
2305         return ret;
2306
2307 out_tcpv6_protocol:
2308         inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2309 out_tcpv6_protosw:
2310         inet6_unregister_protosw(&tcpv6_protosw);
2311         goto out;
2312 }
2313
2314 void tcpv6_exit(void)
2315 {
2316         unregister_pernet_subsys(&tcpv6_net_ops);
2317         inet6_unregister_protosw(&tcpv6_protosw);
2318         inet6_del_protocol(&tcpv6_protocol, IPPROTO_TCP);
2319 }