ipv6: Check ip6_find_1stfragopt() return value properly.
[pandora-kernel.git] / net / ipv6 / udp.c
1 /*
2  *      UDP over IPv6
3  *      Linux INET6 implementation
4  *
5  *      Authors:
6  *      Pedro Roque             <roque@di.fc.ul.pt>
7  *
8  *      Based on linux/ipv4/udp.c
9  *
10  *      Fixes:
11  *      Hideaki YOSHIFUJI       :       sin6_scope_id support
12  *      YOSHIFUJI Hideaki @USAGI and:   Support IPV6_V6ONLY socket option, which
13  *      Alexey Kuznetsov                allow both IPv4 and IPv6 sockets to bind
14  *                                      a single port at the same time.
15  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
16  *      YOSHIFUJI Hideaki @USAGI:       convert /proc/net/udp6 to seq_file.
17  *
18  *      This program is free software; you can redistribute it and/or
19  *      modify it under the terms of the GNU General Public License
20  *      as published by the Free Software Foundation; either version
21  *      2 of the License, or (at your option) any later version.
22  */
23
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/socket.h>
27 #include <linux/sockios.h>
28 #include <linux/net.h>
29 #include <linux/in6.h>
30 #include <linux/netdevice.h>
31 #include <linux/if_arp.h>
32 #include <linux/ipv6.h>
33 #include <linux/icmpv6.h>
34 #include <linux/init.h>
35 #include <linux/module.h>
36 #include <linux/skbuff.h>
37 #include <linux/slab.h>
38 #include <asm/uaccess.h>
39
40 #include <net/ndisc.h>
41 #include <net/protocol.h>
42 #include <net/transp_v6.h>
43 #include <net/ip6_route.h>
44 #include <net/raw.h>
45 #include <net/tcp_states.h>
46 #include <net/ip6_checksum.h>
47 #include <net/xfrm.h>
48
49 #include <linux/proc_fs.h>
50 #include <linux/seq_file.h>
51 #include "udp_impl.h"
52
53 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
54 {
55         const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
56         const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
57         __be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
58         __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
59         int sk_ipv6only = ipv6_only_sock(sk);
60         int sk2_ipv6only = inet_v6_ipv6only(sk2);
61         int addr_type = ipv6_addr_type(sk_rcv_saddr6);
62         int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
63
64         /* if both are mapped, treat as IPv4 */
65         if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
66                 return (!sk2_ipv6only &&
67                         (!sk1_rcv_saddr || !sk2_rcv_saddr ||
68                           sk1_rcv_saddr == sk2_rcv_saddr));
69
70         if (addr_type2 == IPV6_ADDR_ANY &&
71             !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
72                 return 1;
73
74         if (addr_type == IPV6_ADDR_ANY &&
75             !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
76                 return 1;
77
78         if (sk2_rcv_saddr6 &&
79             ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
80                 return 1;
81
82         return 0;
83 }
84
85 static unsigned int udp6_portaddr_hash(struct net *net,
86                                        const struct in6_addr *addr6,
87                                        unsigned int port)
88 {
89         unsigned int hash, mix = net_hash_mix(net);
90
91         if (ipv6_addr_any(addr6))
92                 hash = jhash_1word(0, mix);
93         else if (ipv6_addr_v4mapped(addr6))
94                 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
95         else
96                 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
97
98         return hash ^ port;
99 }
100
101
102 int udp_v6_get_port(struct sock *sk, unsigned short snum)
103 {
104         unsigned int hash2_nulladdr =
105                 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
106         unsigned int hash2_partial = 
107                 udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
108
109         /* precompute partial secondary hash */
110         udp_sk(sk)->udp_portaddr_hash = hash2_partial;
111         return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
112 }
113
114 static void udp_v6_rehash(struct sock *sk)
115 {
116         u16 new_hash = udp6_portaddr_hash(sock_net(sk),
117                                           &inet6_sk(sk)->rcv_saddr,
118                                           inet_sk(sk)->inet_num);
119
120         udp_lib_rehash(sk, new_hash);
121 }
122
123 static inline int compute_score(struct sock *sk, struct net *net,
124                                 unsigned short hnum,
125                                 const struct in6_addr *saddr, __be16 sport,
126                                 const struct in6_addr *daddr, __be16 dport,
127                                 int dif)
128 {
129         int score = -1;
130
131         if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
132                         sk->sk_family == PF_INET6) {
133                 struct ipv6_pinfo *np = inet6_sk(sk);
134                 struct inet_sock *inet = inet_sk(sk);
135
136                 score = 0;
137                 if (inet->inet_dport) {
138                         if (inet->inet_dport != sport)
139                                 return -1;
140                         score++;
141                 }
142                 if (!ipv6_addr_any(&np->rcv_saddr)) {
143                         if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
144                                 return -1;
145                         score++;
146                 }
147                 if (!ipv6_addr_any(&np->daddr)) {
148                         if (!ipv6_addr_equal(&np->daddr, saddr))
149                                 return -1;
150                         score++;
151                 }
152                 if (sk->sk_bound_dev_if) {
153                         if (sk->sk_bound_dev_if != dif)
154                                 return -1;
155                         score++;
156                 }
157         }
158         return score;
159 }
160
161 #define SCORE2_MAX (1 + 1 + 1)
162 static inline int compute_score2(struct sock *sk, struct net *net,
163                                 const struct in6_addr *saddr, __be16 sport,
164                                 const struct in6_addr *daddr, unsigned short hnum,
165                                 int dif)
166 {
167         int score = -1;
168
169         if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
170                         sk->sk_family == PF_INET6) {
171                 struct ipv6_pinfo *np = inet6_sk(sk);
172                 struct inet_sock *inet = inet_sk(sk);
173
174                 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
175                         return -1;
176                 score = 0;
177                 if (inet->inet_dport) {
178                         if (inet->inet_dport != sport)
179                                 return -1;
180                         score++;
181                 }
182                 if (!ipv6_addr_any(&np->daddr)) {
183                         if (!ipv6_addr_equal(&np->daddr, saddr))
184                                 return -1;
185                         score++;
186                 }
187                 if (sk->sk_bound_dev_if) {
188                         if (sk->sk_bound_dev_if != dif)
189                                 return -1;
190                         score++;
191                 }
192         }
193         return score;
194 }
195
196
197 /* called with read_rcu_lock() */
198 static struct sock *udp6_lib_lookup2(struct net *net,
199                 const struct in6_addr *saddr, __be16 sport,
200                 const struct in6_addr *daddr, unsigned int hnum, int dif,
201                 struct udp_hslot *hslot2, unsigned int slot2)
202 {
203         struct sock *sk, *result;
204         struct hlist_nulls_node *node;
205         int score, badness;
206
207 begin:
208         result = NULL;
209         badness = -1;
210         udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
211                 score = compute_score2(sk, net, saddr, sport,
212                                       daddr, hnum, dif);
213                 if (score > badness) {
214                         result = sk;
215                         badness = score;
216                         if (score == SCORE2_MAX)
217                                 goto exact_match;
218                 }
219         }
220         /*
221          * if the nulls value we got at the end of this lookup is
222          * not the expected one, we must restart lookup.
223          * We probably met an item that was moved to another chain.
224          */
225         if (get_nulls_value(node) != slot2)
226                 goto begin;
227
228         if (result) {
229 exact_match:
230                 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
231                         result = NULL;
232                 else if (unlikely(compute_score2(result, net, saddr, sport,
233                                   daddr, hnum, dif) < badness)) {
234                         sock_put(result);
235                         goto begin;
236                 }
237         }
238         return result;
239 }
240
241 static struct sock *__udp6_lib_lookup(struct net *net,
242                                       const struct in6_addr *saddr, __be16 sport,
243                                       const struct in6_addr *daddr, __be16 dport,
244                                       int dif, struct udp_table *udptable)
245 {
246         struct sock *sk, *result;
247         struct hlist_nulls_node *node;
248         unsigned short hnum = ntohs(dport);
249         unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
250         struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
251         int score, badness;
252
253         rcu_read_lock();
254         if (hslot->count > 10) {
255                 hash2 = udp6_portaddr_hash(net, daddr, hnum);
256                 slot2 = hash2 & udptable->mask;
257                 hslot2 = &udptable->hash2[slot2];
258                 if (hslot->count < hslot2->count)
259                         goto begin;
260
261                 result = udp6_lib_lookup2(net, saddr, sport,
262                                           daddr, hnum, dif,
263                                           hslot2, slot2);
264                 if (!result) {
265                         hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
266                         slot2 = hash2 & udptable->mask;
267                         hslot2 = &udptable->hash2[slot2];
268                         if (hslot->count < hslot2->count)
269                                 goto begin;
270
271                         result = udp6_lib_lookup2(net, saddr, sport,
272                                                   &in6addr_any, hnum, dif,
273                                                   hslot2, slot2);
274                 }
275                 rcu_read_unlock();
276                 return result;
277         }
278 begin:
279         result = NULL;
280         badness = -1;
281         sk_nulls_for_each_rcu(sk, node, &hslot->head) {
282                 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
283                 if (score > badness) {
284                         result = sk;
285                         badness = score;
286                 }
287         }
288         /*
289          * if the nulls value we got at the end of this lookup is
290          * not the expected one, we must restart lookup.
291          * We probably met an item that was moved to another chain.
292          */
293         if (get_nulls_value(node) != slot)
294                 goto begin;
295
296         if (result) {
297                 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
298                         result = NULL;
299                 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
300                                         daddr, dport, dif) < badness)) {
301                         sock_put(result);
302                         goto begin;
303                 }
304         }
305         rcu_read_unlock();
306         return result;
307 }
308
309 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
310                                           __be16 sport, __be16 dport,
311                                           struct udp_table *udptable)
312 {
313         struct sock *sk;
314         const struct ipv6hdr *iph = ipv6_hdr(skb);
315
316         if (unlikely(sk = skb_steal_sock(skb)))
317                 return sk;
318         return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
319                                  &iph->daddr, dport, inet6_iif(skb),
320                                  udptable);
321 }
322
323 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
324                              const struct in6_addr *daddr, __be16 dport, int dif)
325 {
326         return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
327 }
328 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
329
330
331 /*
332  *      This should be easy, if there is something there we
333  *      return it, otherwise we block.
334  */
335
336 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
337                   struct msghdr *msg, size_t len,
338                   int noblock, int flags, int *addr_len)
339 {
340         struct ipv6_pinfo *np = inet6_sk(sk);
341         struct inet_sock *inet = inet_sk(sk);
342         struct sk_buff *skb;
343         unsigned int ulen, copied;
344         int peeked;
345         int err;
346         int is_udplite = IS_UDPLITE(sk);
347         bool checksum_valid = false;
348         int is_udp4;
349         bool slow;
350
351         if (flags & MSG_ERRQUEUE)
352                 return ipv6_recv_error(sk, msg, len, addr_len);
353
354         if (np->rxpmtu && np->rxopt.bits.rxpmtu)
355                 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
356
357 try_again:
358         skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
359                                   &peeked, &err);
360         if (!skb)
361                 goto out;
362
363         ulen = skb->len - sizeof(struct udphdr);
364         copied = len;
365         if (copied > ulen)
366                 copied = ulen;
367         else if (copied < ulen)
368                 msg->msg_flags |= MSG_TRUNC;
369
370         is_udp4 = (skb->protocol == htons(ETH_P_IP));
371
372         /*
373          * If checksum is needed at all, try to do it while copying the
374          * data.  If the data is truncated, or if we only want a partial
375          * coverage checksum (UDP-Lite), do it before the copy.
376          */
377
378         if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
379                 checksum_valid = !udp_lib_checksum_complete(skb);
380                 if (!checksum_valid)
381                         goto csum_copy_err;
382         }
383
384         if (checksum_valid || skb_csum_unnecessary(skb))
385                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
386                                               msg->msg_iov, copied       );
387         else {
388                 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
389                 if (err == -EINVAL)
390                         goto csum_copy_err;
391         }
392         if (err)
393                 goto out_free;
394
395         if (!peeked) {
396                 if (is_udp4)
397                         UDP_INC_STATS_USER(sock_net(sk),
398                                         UDP_MIB_INDATAGRAMS, is_udplite);
399                 else
400                         UDP6_INC_STATS_USER(sock_net(sk),
401                                         UDP_MIB_INDATAGRAMS, is_udplite);
402         }
403
404         sock_recv_ts_and_drops(msg, sk, skb);
405
406         /* Copy the address. */
407         if (msg->msg_name) {
408                 struct sockaddr_in6 *sin6;
409
410                 sin6 = (struct sockaddr_in6 *) msg->msg_name;
411                 sin6->sin6_family = AF_INET6;
412                 sin6->sin6_port = udp_hdr(skb)->source;
413                 sin6->sin6_flowinfo = 0;
414                 sin6->sin6_scope_id = 0;
415
416                 if (is_udp4)
417                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
418                                                &sin6->sin6_addr);
419                 else {
420                         ipv6_addr_copy(&sin6->sin6_addr,
421                                        &ipv6_hdr(skb)->saddr);
422                         if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
423                                 sin6->sin6_scope_id = IP6CB(skb)->iif;
424                 }
425                 *addr_len = sizeof(*sin6);
426         }
427         if (is_udp4) {
428                 if (inet->cmsg_flags)
429                         ip_cmsg_recv(msg, skb);
430         } else {
431                 if (np->rxopt.all)
432                         datagram_recv_ctl(sk, msg, skb);
433         }
434
435         err = copied;
436         if (flags & MSG_TRUNC)
437                 err = ulen;
438
439 out_free:
440         skb_free_datagram_locked(sk, skb);
441 out:
442         return err;
443
444 csum_copy_err:
445         slow = lock_sock_fast(sk);
446         if (!skb_kill_datagram(sk, skb, flags)) {
447                 if (is_udp4)
448                         UDP_INC_STATS_USER(sock_net(sk),
449                                         UDP_MIB_INERRORS, is_udplite);
450                 else
451                         UDP6_INC_STATS_USER(sock_net(sk),
452                                         UDP_MIB_INERRORS, is_udplite);
453         }
454         unlock_sock_fast(sk, slow);
455
456         /* starting over for a new packet, but check if we need to yield */
457         cond_resched();
458         msg->msg_flags &= ~MSG_TRUNC;
459         goto try_again;
460 }
461
462 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
463                     u8 type, u8 code, int offset, __be32 info,
464                     struct udp_table *udptable)
465 {
466         struct ipv6_pinfo *np;
467         const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
468         const struct in6_addr *saddr = &hdr->saddr;
469         const struct in6_addr *daddr = &hdr->daddr;
470         struct udphdr *uh = (struct udphdr*)(skb->data+offset);
471         struct sock *sk;
472         int err;
473
474         sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
475                                saddr, uh->source, inet6_iif(skb), udptable);
476         if (sk == NULL)
477                 return;
478
479         np = inet6_sk(sk);
480
481         if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
482                 goto out;
483
484         if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
485                 goto out;
486
487         if (np->recverr)
488                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
489
490         sk->sk_err = err;
491         sk->sk_error_report(sk);
492 out:
493         sock_put(sk);
494 }
495
496 static __inline__ void udpv6_err(struct sk_buff *skb,
497                                  struct inet6_skb_parm *opt, u8 type,
498                                  u8 code, int offset, __be32 info     )
499 {
500         __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
501 }
502
503 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
504 {
505         struct udp_sock *up = udp_sk(sk);
506         int rc;
507         int is_udplite = IS_UDPLITE(sk);
508
509         if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
510                 sock_rps_save_rxhash(sk, skb);
511
512         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
513                 goto drop;
514
515         /*
516          * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
517          */
518         if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
519
520                 if (up->pcrlen == 0) {          /* full coverage was set  */
521                         LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
522                                 " %d while full coverage %d requested\n",
523                                 UDP_SKB_CB(skb)->cscov, skb->len);
524                         goto drop;
525                 }
526                 if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
527                         LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
528                                                     "too small, need min %d\n",
529                                        UDP_SKB_CB(skb)->cscov, up->pcrlen);
530                         goto drop;
531                 }
532         }
533
534         if (rcu_access_pointer(sk->sk_filter)) {
535                 if (udp_lib_checksum_complete(skb))
536                         goto drop;
537         }
538
539         if ((rc = ip_queue_rcv_skb(sk, skb)) < 0) {
540                 /* Note that an ENOMEM error is charged twice */
541                 if (rc == -ENOMEM)
542                         UDP6_INC_STATS_BH(sock_net(sk),
543                                         UDP_MIB_RCVBUFERRORS, is_udplite);
544                 goto drop_no_sk_drops_inc;
545         }
546
547         return 0;
548 drop:
549         atomic_inc(&sk->sk_drops);
550 drop_no_sk_drops_inc:
551         UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
552         kfree_skb(skb);
553         return -1;
554 }
555
556 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
557                                       __be16 loc_port, const struct in6_addr *loc_addr,
558                                       __be16 rmt_port, const struct in6_addr *rmt_addr,
559                                       int dif)
560 {
561         struct hlist_nulls_node *node;
562         struct sock *s = sk;
563         unsigned short num = ntohs(loc_port);
564
565         sk_nulls_for_each_from(s, node) {
566                 struct inet_sock *inet = inet_sk(s);
567
568                 if (!net_eq(sock_net(s), net))
569                         continue;
570
571                 if (udp_sk(s)->udp_port_hash == num &&
572                     s->sk_family == PF_INET6) {
573                         struct ipv6_pinfo *np = inet6_sk(s);
574                         if (inet->inet_dport) {
575                                 if (inet->inet_dport != rmt_port)
576                                         continue;
577                         }
578                         if (!ipv6_addr_any(&np->daddr) &&
579                             !ipv6_addr_equal(&np->daddr, rmt_addr))
580                                 continue;
581
582                         if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
583                                 continue;
584
585                         if (!ipv6_addr_any(&np->rcv_saddr)) {
586                                 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
587                                         continue;
588                         }
589                         if (!inet6_mc_check(s, loc_addr, rmt_addr))
590                                 continue;
591                         return s;
592                 }
593         }
594         return NULL;
595 }
596
597 static void flush_stack(struct sock **stack, unsigned int count,
598                         struct sk_buff *skb, unsigned int final)
599 {
600         unsigned int i;
601         struct sock *sk;
602         struct sk_buff *skb1;
603
604         for (i = 0; i < count; i++) {
605                 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
606
607                 sk = stack[i];
608                 if (skb1) {
609                         if (sk_rcvqueues_full(sk, skb1)) {
610                                 kfree_skb(skb1);
611                                 goto drop;
612                         }
613                         bh_lock_sock(sk);
614                         if (!sock_owned_by_user(sk))
615                                 udpv6_queue_rcv_skb(sk, skb1);
616                         else if (sk_add_backlog(sk, skb1)) {
617                                 kfree_skb(skb1);
618                                 bh_unlock_sock(sk);
619                                 goto drop;
620                         }
621                         bh_unlock_sock(sk);
622                         continue;
623                 }
624 drop:
625                 atomic_inc(&sk->sk_drops);
626                 UDP6_INC_STATS_BH(sock_net(sk),
627                                 UDP_MIB_RCVBUFERRORS, IS_UDPLITE(sk));
628                 UDP6_INC_STATS_BH(sock_net(sk),
629                                 UDP_MIB_INERRORS, IS_UDPLITE(sk));
630         }
631 }
632 /*
633  * Note: called only from the BH handler context,
634  * so we don't need to lock the hashes.
635  */
636 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
637                 const struct in6_addr *saddr, const struct in6_addr *daddr,
638                 struct udp_table *udptable)
639 {
640         struct sock *sk, *stack[256 / sizeof(struct sock *)];
641         const struct udphdr *uh = udp_hdr(skb);
642         struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
643         int dif;
644         unsigned int i, count = 0;
645
646         spin_lock(&hslot->lock);
647         sk = sk_nulls_head(&hslot->head);
648         dif = inet6_iif(skb);
649         sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
650         while (sk) {
651                 stack[count++] = sk;
652                 sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
653                                        uh->source, saddr, dif);
654                 if (unlikely(count == ARRAY_SIZE(stack))) {
655                         if (!sk)
656                                 break;
657                         flush_stack(stack, count, skb, ~0);
658                         count = 0;
659                 }
660         }
661         /*
662          * before releasing the lock, we must take reference on sockets
663          */
664         for (i = 0; i < count; i++)
665                 sock_hold(stack[i]);
666
667         spin_unlock(&hslot->lock);
668
669         if (count) {
670                 flush_stack(stack, count, skb, count - 1);
671
672                 for (i = 0; i < count; i++)
673                         sock_put(stack[i]);
674         } else {
675                 kfree_skb(skb);
676         }
677         return 0;
678 }
679
680 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
681                                  int proto)
682 {
683         int err;
684
685         UDP_SKB_CB(skb)->partial_cov = 0;
686         UDP_SKB_CB(skb)->cscov = skb->len;
687
688         if (proto == IPPROTO_UDPLITE) {
689                 err = udplite_checksum_init(skb, uh);
690                 if (err)
691                         return err;
692         }
693
694         if (uh->check == 0) {
695                 /* RFC 2460 section 8.1 says that we SHOULD log
696                    this error. Well, it is reasonable.
697                  */
698                 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
699                 return 1;
700         }
701         if (skb->ip_summed == CHECKSUM_COMPLETE &&
702             !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
703                              skb->len, proto, skb->csum))
704                 skb->ip_summed = CHECKSUM_UNNECESSARY;
705
706         if (!skb_csum_unnecessary(skb))
707                 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
708                                                          &ipv6_hdr(skb)->daddr,
709                                                          skb->len, proto, 0));
710
711         return 0;
712 }
713
714 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
715                    int proto)
716 {
717         struct net *net = dev_net(skb->dev);
718         struct sock *sk;
719         struct udphdr *uh;
720         const struct in6_addr *saddr, *daddr;
721         u32 ulen = 0;
722
723         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
724                 goto discard;
725
726         saddr = &ipv6_hdr(skb)->saddr;
727         daddr = &ipv6_hdr(skb)->daddr;
728         uh = udp_hdr(skb);
729
730         ulen = ntohs(uh->len);
731         if (ulen > skb->len)
732                 goto short_packet;
733
734         if (proto == IPPROTO_UDP) {
735                 /* UDP validates ulen. */
736
737                 /* Check for jumbo payload */
738                 if (ulen == 0)
739                         ulen = skb->len;
740
741                 if (ulen < sizeof(*uh))
742                         goto short_packet;
743
744                 if (ulen < skb->len) {
745                         if (pskb_trim_rcsum(skb, ulen))
746                                 goto short_packet;
747                         saddr = &ipv6_hdr(skb)->saddr;
748                         daddr = &ipv6_hdr(skb)->daddr;
749                         uh = udp_hdr(skb);
750                 }
751         }
752
753         if (udp6_csum_init(skb, uh, proto))
754                 goto discard;
755
756         /*
757          *      Multicast receive code
758          */
759         if (ipv6_addr_is_multicast(daddr))
760                 return __udp6_lib_mcast_deliver(net, skb,
761                                 saddr, daddr, udptable);
762
763         /* Unicast */
764
765         /*
766          * check socket cache ... must talk to Alan about his plans
767          * for sock caches... i'll skip this for now.
768          */
769         sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
770
771         if (sk == NULL) {
772                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
773                         goto discard;
774
775                 if (udp_lib_checksum_complete(skb))
776                         goto discard;
777                 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
778                                 proto == IPPROTO_UDPLITE);
779
780                 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
781
782                 kfree_skb(skb);
783                 return 0;
784         }
785
786         /* deliver */
787
788         if (sk_rcvqueues_full(sk, skb)) {
789                 sock_put(sk);
790                 goto discard;
791         }
792         bh_lock_sock(sk);
793         if (!sock_owned_by_user(sk))
794                 udpv6_queue_rcv_skb(sk, skb);
795         else if (sk_add_backlog(sk, skb)) {
796                 atomic_inc(&sk->sk_drops);
797                 bh_unlock_sock(sk);
798                 sock_put(sk);
799                 goto discard;
800         }
801         bh_unlock_sock(sk);
802         sock_put(sk);
803         return 0;
804
805 short_packet:
806         LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
807                        proto == IPPROTO_UDPLITE ? "-Lite" : "",
808                        saddr,
809                        ntohs(uh->source),
810                        ulen,
811                        skb->len,
812                        daddr,
813                        ntohs(uh->dest));
814
815 discard:
816         UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
817         kfree_skb(skb);
818         return 0;
819 }
820
821 static __inline__ int udpv6_rcv(struct sk_buff *skb)
822 {
823         return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
824 }
825
826 /*
827  * Throw away all pending data and cancel the corking. Socket is locked.
828  */
829 static void udp_v6_flush_pending_frames(struct sock *sk)
830 {
831         struct udp_sock *up = udp_sk(sk);
832
833         if (up->pending == AF_INET)
834                 udp_flush_pending_frames(sk);
835         else if (up->pending) {
836                 up->len = 0;
837                 up->pending = 0;
838                 ip6_flush_pending_frames(sk);
839         }
840 }
841
842 /**
843  *      udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
844  *      @sk:    socket we are sending on
845  *      @skb:   sk_buff containing the filled-in UDP header
846  *              (checksum field must be zeroed out)
847  */
848 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
849                                  const struct in6_addr *saddr,
850                                  const struct in6_addr *daddr, int len)
851 {
852         unsigned int offset;
853         struct udphdr *uh = udp_hdr(skb);
854         __wsum csum = 0;
855
856         if (skb_queue_len(&sk->sk_write_queue) == 1) {
857                 /* Only one fragment on the socket.  */
858                 skb->csum_start = skb_transport_header(skb) - skb->head;
859                 skb->csum_offset = offsetof(struct udphdr, check);
860                 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
861         } else {
862                 /*
863                  * HW-checksum won't work as there are two or more
864                  * fragments on the socket so that all csums of sk_buffs
865                  * should be together
866                  */
867                 offset = skb_transport_offset(skb);
868                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
869
870                 skb->ip_summed = CHECKSUM_NONE;
871
872                 skb_queue_walk(&sk->sk_write_queue, skb) {
873                         csum = csum_add(csum, skb->csum);
874                 }
875
876                 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
877                                             csum);
878                 if (uh->check == 0)
879                         uh->check = CSUM_MANGLED_0;
880         }
881 }
882
883 /*
884  *      Sending
885  */
886
887 static int udp_v6_push_pending_frames(struct sock *sk)
888 {
889         struct sk_buff *skb;
890         struct udphdr *uh;
891         struct udp_sock  *up = udp_sk(sk);
892         struct inet_sock *inet = inet_sk(sk);
893         struct flowi6 *fl6;
894         int err = 0;
895         int is_udplite = IS_UDPLITE(sk);
896         __wsum csum = 0;
897
898         if (up->pending == AF_INET)
899                 return udp_push_pending_frames(sk);
900
901         fl6 = &inet->cork.fl.u.ip6;
902
903         /* Grab the skbuff where UDP header space exists. */
904         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
905                 goto out;
906
907         /*
908          * Create a UDP header
909          */
910         uh = udp_hdr(skb);
911         uh->source = fl6->fl6_sport;
912         uh->dest = fl6->fl6_dport;
913         uh->len = htons(up->len);
914         uh->check = 0;
915
916         if (is_udplite)
917                 csum = udplite_csum_outgoing(sk, skb);
918         else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
919                 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
920                                      up->len);
921                 goto send;
922         } else
923                 csum = udp_csum_outgoing(sk, skb);
924
925         /* add protocol-dependent pseudo-header */
926         uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
927                                     up->len, fl6->flowi6_proto, csum);
928         if (uh->check == 0)
929                 uh->check = CSUM_MANGLED_0;
930
931 send:
932         err = ip6_push_pending_frames(sk);
933         if (err) {
934                 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
935                         UDP6_INC_STATS_USER(sock_net(sk),
936                                             UDP_MIB_SNDBUFERRORS, is_udplite);
937                         err = 0;
938                 }
939         } else
940                 UDP6_INC_STATS_USER(sock_net(sk),
941                                     UDP_MIB_OUTDATAGRAMS, is_udplite);
942 out:
943         up->len = 0;
944         up->pending = 0;
945         return err;
946 }
947
948 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
949                   struct msghdr *msg, size_t len)
950 {
951         struct ipv6_txoptions opt_space;
952         struct udp_sock *up = udp_sk(sk);
953         struct inet_sock *inet = inet_sk(sk);
954         struct ipv6_pinfo *np = inet6_sk(sk);
955         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
956         struct in6_addr *daddr, *final_p, final;
957         struct ipv6_txoptions *opt = NULL;
958         struct ipv6_txoptions *opt_to_free = NULL;
959         struct ip6_flowlabel *flowlabel = NULL;
960         struct flowi6 fl6;
961         struct dst_entry *dst;
962         int addr_len = msg->msg_namelen;
963         int ulen = len;
964         int hlimit = -1;
965         int tclass = -1;
966         int dontfrag = -1;
967         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
968         int err;
969         int connected = 0;
970         int is_udplite = IS_UDPLITE(sk);
971         int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
972
973         /* destination address check */
974         if (sin6) {
975                 if (addr_len < offsetof(struct sockaddr, sa_data))
976                         return -EINVAL;
977
978                 switch (sin6->sin6_family) {
979                 case AF_INET6:
980                         if (addr_len < SIN6_LEN_RFC2133)
981                                 return -EINVAL;
982                         daddr = &sin6->sin6_addr;
983                         break;
984                 case AF_INET:
985                         goto do_udp_sendmsg;
986                 case AF_UNSPEC:
987                         msg->msg_name = sin6 = NULL;
988                         msg->msg_namelen = addr_len = 0;
989                         daddr = NULL;
990                         break;
991                 default:
992                         return -EINVAL;
993                 }
994         } else if (!up->pending) {
995                 if (sk->sk_state != TCP_ESTABLISHED)
996                         return -EDESTADDRREQ;
997                 daddr = &np->daddr;
998         } else
999                 daddr = NULL;
1000
1001         if (daddr) {
1002                 if (ipv6_addr_v4mapped(daddr)) {
1003                         struct sockaddr_in sin;
1004                         sin.sin_family = AF_INET;
1005                         sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1006                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
1007                         msg->msg_name = &sin;
1008                         msg->msg_namelen = sizeof(sin);
1009 do_udp_sendmsg:
1010                         if (__ipv6_only_sock(sk))
1011                                 return -ENETUNREACH;
1012                         return udp_sendmsg(iocb, sk, msg, len);
1013                 }
1014         }
1015
1016         if (up->pending == AF_INET)
1017                 return udp_sendmsg(iocb, sk, msg, len);
1018
1019         /* Rough check on arithmetic overflow,
1020            better check is made in ip6_append_data().
1021            */
1022         if (len > INT_MAX - sizeof(struct udphdr))
1023                 return -EMSGSIZE;
1024
1025         if (up->pending) {
1026                 /*
1027                  * There are pending frames.
1028                  * The socket lock must be held while it's corked.
1029                  */
1030                 lock_sock(sk);
1031                 if (likely(up->pending)) {
1032                         if (unlikely(up->pending != AF_INET6)) {
1033                                 release_sock(sk);
1034                                 return -EAFNOSUPPORT;
1035                         }
1036                         dst = NULL;
1037                         goto do_append_data;
1038                 }
1039                 release_sock(sk);
1040         }
1041         ulen += sizeof(struct udphdr);
1042
1043         memset(&fl6, 0, sizeof(fl6));
1044
1045         if (sin6) {
1046                 if (sin6->sin6_port == 0)
1047                         return -EINVAL;
1048
1049                 fl6.fl6_dport = sin6->sin6_port;
1050                 daddr = &sin6->sin6_addr;
1051
1052                 if (np->sndflow) {
1053                         fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1054                         if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1055                                 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1056                                 if (flowlabel == NULL)
1057                                         return -EINVAL;
1058                                 daddr = &flowlabel->dst;
1059                         }
1060                 }
1061
1062                 /*
1063                  * Otherwise it will be difficult to maintain
1064                  * sk->sk_dst_cache.
1065                  */
1066                 if (sk->sk_state == TCP_ESTABLISHED &&
1067                     ipv6_addr_equal(daddr, &np->daddr))
1068                         daddr = &np->daddr;
1069
1070                 if (addr_len >= sizeof(struct sockaddr_in6) &&
1071                     sin6->sin6_scope_id &&
1072                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
1073                         fl6.flowi6_oif = sin6->sin6_scope_id;
1074         } else {
1075                 if (sk->sk_state != TCP_ESTABLISHED)
1076                         return -EDESTADDRREQ;
1077
1078                 fl6.fl6_dport = inet->inet_dport;
1079                 daddr = &np->daddr;
1080                 fl6.flowlabel = np->flow_label;
1081                 connected = 1;
1082         }
1083
1084         if (!fl6.flowi6_oif)
1085                 fl6.flowi6_oif = sk->sk_bound_dev_if;
1086
1087         if (!fl6.flowi6_oif)
1088                 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1089
1090         fl6.flowi6_mark = sk->sk_mark;
1091
1092         if (msg->msg_controllen) {
1093                 opt = &opt_space;
1094                 memset(opt, 0, sizeof(struct ipv6_txoptions));
1095                 opt->tot_len = sizeof(*opt);
1096
1097                 err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1098                                         &hlimit, &tclass, &dontfrag);
1099                 if (err < 0) {
1100                         fl6_sock_release(flowlabel);
1101                         return err;
1102                 }
1103                 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1104                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1105                         if (flowlabel == NULL)
1106                                 return -EINVAL;
1107                 }
1108                 if (!(opt->opt_nflen|opt->opt_flen))
1109                         opt = NULL;
1110                 connected = 0;
1111         }
1112         if (!opt) {
1113                 opt = txopt_get(np);
1114                 opt_to_free = opt;
1115         }
1116         if (flowlabel)
1117                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1118         opt = ipv6_fixup_options(&opt_space, opt);
1119
1120         fl6.flowi6_proto = sk->sk_protocol;
1121         if (!ipv6_addr_any(daddr))
1122                 ipv6_addr_copy(&fl6.daddr, daddr);
1123         else
1124                 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1125         if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1126                 ipv6_addr_copy(&fl6.saddr, &np->saddr);
1127         fl6.fl6_sport = inet->inet_sport;
1128
1129         final_p = fl6_update_dst(&fl6, opt, &final);
1130         if (final_p)
1131                 connected = 0;
1132
1133         if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1134                 fl6.flowi6_oif = np->mcast_oif;
1135                 connected = 0;
1136         }
1137
1138         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1139
1140         dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
1141         if (IS_ERR(dst)) {
1142                 err = PTR_ERR(dst);
1143                 dst = NULL;
1144                 goto out;
1145         }
1146
1147         if (hlimit < 0) {
1148                 if (ipv6_addr_is_multicast(&fl6.daddr))
1149                         hlimit = np->mcast_hops;
1150                 else
1151                         hlimit = np->hop_limit;
1152                 if (hlimit < 0)
1153                         hlimit = ip6_dst_hoplimit(dst);
1154         }
1155
1156         if (tclass < 0)
1157                 tclass = np->tclass;
1158
1159         if (dontfrag < 0)
1160                 dontfrag = np->dontfrag;
1161
1162         if (msg->msg_flags&MSG_CONFIRM)
1163                 goto do_confirm;
1164 back_from_confirm:
1165
1166         lock_sock(sk);
1167         if (unlikely(up->pending)) {
1168                 /* The socket is already corked while preparing it. */
1169                 /* ... which is an evident application bug. --ANK */
1170                 release_sock(sk);
1171
1172                 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1173                 err = -EINVAL;
1174                 goto out;
1175         }
1176
1177         up->pending = AF_INET6;
1178
1179 do_append_data:
1180         up->len += ulen;
1181         getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1182         err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
1183                 sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1184                 (struct rt6_info*)dst,
1185                 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1186         if (err)
1187                 udp_v6_flush_pending_frames(sk);
1188         else if (!corkreq)
1189                 err = udp_v6_push_pending_frames(sk);
1190         else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1191                 up->pending = 0;
1192
1193         if (dst) {
1194                 if (connected) {
1195                         ip6_dst_store(sk, dst,
1196                                       ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1197                                       &np->daddr : NULL,
1198 #ifdef CONFIG_IPV6_SUBTREES
1199                                       ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1200                                       &np->saddr :
1201 #endif
1202                                       NULL);
1203                 } else {
1204                         dst_release(dst);
1205                 }
1206                 dst = NULL;
1207         }
1208
1209         if (err > 0)
1210                 err = np->recverr ? net_xmit_errno(err) : 0;
1211         release_sock(sk);
1212 out:
1213         dst_release(dst);
1214         fl6_sock_release(flowlabel);
1215         txopt_put(opt_to_free);
1216         if (!err)
1217                 return len;
1218         /*
1219          * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1220          * ENOBUFS might not be good (it's not tunable per se), but otherwise
1221          * we don't have a good statistic (IpOutDiscards but it can be too many
1222          * things).  We could add another new stat but at least for now that
1223          * seems like overkill.
1224          */
1225         if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1226                 UDP6_INC_STATS_USER(sock_net(sk),
1227                                 UDP_MIB_SNDBUFERRORS, is_udplite);
1228         }
1229         return err;
1230
1231 do_confirm:
1232         dst_confirm(dst);
1233         if (!(msg->msg_flags&MSG_PROBE) || len)
1234                 goto back_from_confirm;
1235         err = 0;
1236         goto out;
1237 }
1238
1239 void udpv6_destroy_sock(struct sock *sk)
1240 {
1241         lock_sock(sk);
1242         udp_v6_flush_pending_frames(sk);
1243         release_sock(sk);
1244
1245         inet6_destroy_sock(sk);
1246 }
1247
1248 /*
1249  *      Socket option code for UDP
1250  */
1251 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1252                      char __user *optval, unsigned int optlen)
1253 {
1254         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1255                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1256                                           udp_v6_push_pending_frames);
1257         return ipv6_setsockopt(sk, level, optname, optval, optlen);
1258 }
1259
1260 #ifdef CONFIG_COMPAT
1261 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1262                             char __user *optval, unsigned int optlen)
1263 {
1264         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1265                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1266                                           udp_v6_push_pending_frames);
1267         return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1268 }
1269 #endif
1270
1271 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1272                      char __user *optval, int __user *optlen)
1273 {
1274         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1275                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1276         return ipv6_getsockopt(sk, level, optname, optval, optlen);
1277 }
1278
1279 #ifdef CONFIG_COMPAT
1280 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1281                             char __user *optval, int __user *optlen)
1282 {
1283         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1284                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1285         return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1286 }
1287 #endif
1288
1289 static int udp6_ufo_send_check(struct sk_buff *skb)
1290 {
1291         const struct ipv6hdr *ipv6h;
1292         struct udphdr *uh;
1293
1294         if (!pskb_may_pull(skb, sizeof(*uh)))
1295                 return -EINVAL;
1296
1297         ipv6h = ipv6_hdr(skb);
1298         uh = udp_hdr(skb);
1299
1300         uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1301                                      IPPROTO_UDP, 0);
1302         skb->csum_start = skb_transport_header(skb) - skb->head;
1303         skb->csum_offset = offsetof(struct udphdr, check);
1304         skb->ip_summed = CHECKSUM_PARTIAL;
1305         return 0;
1306 }
1307
1308 static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb, u32 features)
1309 {
1310         struct sk_buff *segs = ERR_PTR(-EINVAL);
1311         unsigned int mss;
1312         unsigned int unfrag_ip6hlen, unfrag_len;
1313         struct frag_hdr *fptr;
1314         u8 *mac_start, *prevhdr;
1315         u8 nexthdr;
1316         u8 frag_hdr_sz = sizeof(struct frag_hdr);
1317         int offset;
1318         __wsum csum;
1319         int err;
1320
1321         mss = skb_shinfo(skb)->gso_size;
1322         if (unlikely(skb->len <= mss))
1323                 goto out;
1324
1325         if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1326                 /* Packet is from an untrusted source, reset gso_segs. */
1327                 int type = skb_shinfo(skb)->gso_type;
1328
1329                 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1330                              !(type & (SKB_GSO_UDP))))
1331                         goto out;
1332
1333                 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1334
1335                 segs = NULL;
1336                 goto out;
1337         }
1338
1339         /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1340          * do checksum of UDP packets sent as multiple IP fragments.
1341          */
1342         offset = skb_checksum_start_offset(skb);
1343         csum = skb_checksum(skb, offset, skb->len- offset, 0);
1344         offset += skb->csum_offset;
1345         *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1346         skb->ip_summed = CHECKSUM_NONE;
1347
1348         /* Check if there is enough headroom to insert fragment header. */
1349         if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) &&
1350             pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1351                 goto out;
1352
1353         /* Find the unfragmentable header and shift it left by frag_hdr_sz
1354          * bytes to insert fragment header.
1355          */
1356         err = ip6_find_1stfragopt(skb, &prevhdr);
1357         if (err < 0)
1358                 return ERR_PTR(err);
1359         unfrag_ip6hlen = err;
1360         nexthdr = *prevhdr;
1361         *prevhdr = NEXTHDR_FRAGMENT;
1362         unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1363                      unfrag_ip6hlen;
1364         mac_start = skb_mac_header(skb);
1365         memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1366
1367         skb->mac_header -= frag_hdr_sz;
1368         skb->network_header -= frag_hdr_sz;
1369
1370         fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1371         fptr->nexthdr = nexthdr;
1372         fptr->reserved = 0;
1373         fptr->identification = skb_shinfo(skb)->ip6_frag_id;
1374
1375         /* Fragment the skb. ipv6 header and the remaining fields of the
1376          * fragment header are updated in ipv6_gso_segment()
1377          */
1378         segs = skb_segment(skb, features);
1379
1380 out:
1381         return segs;
1382 }
1383
1384 static const struct inet6_protocol udpv6_protocol = {
1385         .handler        =       udpv6_rcv,
1386         .err_handler    =       udpv6_err,
1387         .gso_send_check =       udp6_ufo_send_check,
1388         .gso_segment    =       udp6_ufo_fragment,
1389         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1390 };
1391
1392 /* ------------------------------------------------------------------------ */
1393 #ifdef CONFIG_PROC_FS
1394
1395 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1396 {
1397         struct inet_sock *inet = inet_sk(sp);
1398         struct ipv6_pinfo *np = inet6_sk(sp);
1399         const struct in6_addr *dest, *src;
1400         __u16 destp, srcp;
1401
1402         dest  = &np->daddr;
1403         src   = &np->rcv_saddr;
1404         destp = ntohs(inet->inet_dport);
1405         srcp  = ntohs(inet->inet_sport);
1406         seq_printf(seq,
1407                    "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1408                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1409                    bucket,
1410                    src->s6_addr32[0], src->s6_addr32[1],
1411                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1412                    dest->s6_addr32[0], dest->s6_addr32[1],
1413                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1414                    sp->sk_state,
1415                    sk_wmem_alloc_get(sp),
1416                    sk_rmem_alloc_get(sp),
1417                    0, 0L, 0,
1418                    sock_i_uid(sp), 0,
1419                    sock_i_ino(sp),
1420                    atomic_read(&sp->sk_refcnt), sp,
1421                    atomic_read(&sp->sk_drops));
1422 }
1423
1424 int udp6_seq_show(struct seq_file *seq, void *v)
1425 {
1426         if (v == SEQ_START_TOKEN)
1427                 seq_printf(seq,
1428                            "  sl  "
1429                            "local_address                         "
1430                            "remote_address                        "
1431                            "st tx_queue rx_queue tr tm->when retrnsmt"
1432                            "   uid  timeout inode ref pointer drops\n");
1433         else
1434                 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1435         return 0;
1436 }
1437
1438 static const struct file_operations udp6_afinfo_seq_fops = {
1439         .owner    = THIS_MODULE,
1440         .open     = udp_seq_open,
1441         .read     = seq_read,
1442         .llseek   = seq_lseek,
1443         .release  = seq_release_net
1444 };
1445
1446 static struct udp_seq_afinfo udp6_seq_afinfo = {
1447         .name           = "udp6",
1448         .family         = AF_INET6,
1449         .udp_table      = &udp_table,
1450         .seq_fops       = &udp6_afinfo_seq_fops,
1451         .seq_ops        = {
1452                 .show           = udp6_seq_show,
1453         },
1454 };
1455
1456 int __net_init udp6_proc_init(struct net *net)
1457 {
1458         return udp_proc_register(net, &udp6_seq_afinfo);
1459 }
1460
1461 void udp6_proc_exit(struct net *net) {
1462         udp_proc_unregister(net, &udp6_seq_afinfo);
1463 }
1464 #endif /* CONFIG_PROC_FS */
1465
1466 void udp_v6_clear_sk(struct sock *sk, int size)
1467 {
1468         struct inet_sock *inet = inet_sk(sk);
1469
1470         /* we do not want to clear pinet6 field, because of RCU lookups */
1471         sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1472
1473         size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1474         memset(&inet->pinet6 + 1, 0, size);
1475 }
1476
1477 /* ------------------------------------------------------------------------ */
1478
1479 struct proto udpv6_prot = {
1480         .name              = "UDPv6",
1481         .owner             = THIS_MODULE,
1482         .close             = udp_lib_close,
1483         .connect           = ip6_datagram_connect,
1484         .disconnect        = udp_disconnect,
1485         .ioctl             = udp_ioctl,
1486         .destroy           = udpv6_destroy_sock,
1487         .setsockopt        = udpv6_setsockopt,
1488         .getsockopt        = udpv6_getsockopt,
1489         .sendmsg           = udpv6_sendmsg,
1490         .recvmsg           = udpv6_recvmsg,
1491         .backlog_rcv       = udpv6_queue_rcv_skb,
1492         .hash              = udp_lib_hash,
1493         .unhash            = udp_lib_unhash,
1494         .rehash            = udp_v6_rehash,
1495         .get_port          = udp_v6_get_port,
1496         .memory_allocated  = &udp_memory_allocated,
1497         .sysctl_mem        = sysctl_udp_mem,
1498         .sysctl_wmem       = &sysctl_udp_wmem_min,
1499         .sysctl_rmem       = &sysctl_udp_rmem_min,
1500         .obj_size          = sizeof(struct udp6_sock),
1501         .slab_flags        = SLAB_DESTROY_BY_RCU,
1502         .h.udp_table       = &udp_table,
1503 #ifdef CONFIG_COMPAT
1504         .compat_setsockopt = compat_udpv6_setsockopt,
1505         .compat_getsockopt = compat_udpv6_getsockopt,
1506 #endif
1507         .clear_sk          = udp_v6_clear_sk,
1508 };
1509
1510 static struct inet_protosw udpv6_protosw = {
1511         .type =      SOCK_DGRAM,
1512         .protocol =  IPPROTO_UDP,
1513         .prot =      &udpv6_prot,
1514         .ops =       &inet6_dgram_ops,
1515         .no_check =  UDP_CSUM_DEFAULT,
1516         .flags =     INET_PROTOSW_PERMANENT,
1517 };
1518
1519
1520 int __init udpv6_init(void)
1521 {
1522         int ret;
1523
1524         ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1525         if (ret)
1526                 goto out;
1527
1528         ret = inet6_register_protosw(&udpv6_protosw);
1529         if (ret)
1530                 goto out_udpv6_protocol;
1531 out:
1532         return ret;
1533
1534 out_udpv6_protocol:
1535         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1536         goto out;
1537 }
1538
1539 void udpv6_exit(void)
1540 {
1541         inet6_unregister_protosw(&udpv6_protosw);
1542         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1543 }