ipv6: Fix NULL pointer dereference with time-wait sockets
[safe/jmp/linux-2.6] / 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 <asm/uaccess.h>
38
39 #include <net/ndisc.h>
40 #include <net/protocol.h>
41 #include <net/transp_v6.h>
42 #include <net/ip6_route.h>
43 #include <net/raw.h>
44 #include <net/tcp_states.h>
45 #include <net/ip6_checksum.h>
46 #include <net/xfrm.h>
47
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
50 #include "udp_impl.h"
51
52 int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
53 {
54         const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
55         const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
56         __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
57         __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
58         int sk_ipv6only = ipv6_only_sock(sk);
59         int sk2_ipv6only = inet_v6_ipv6only(sk2);
60         int addr_type = ipv6_addr_type(sk_rcv_saddr6);
61         int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
62
63         /* if both are mapped, treat as IPv4 */
64         if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
65                 return (!sk2_ipv6only &&
66                         (!sk_rcv_saddr || !sk2_rcv_saddr ||
67                           sk_rcv_saddr == sk2_rcv_saddr));
68
69         if (addr_type2 == IPV6_ADDR_ANY &&
70             !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
71                 return 1;
72
73         if (addr_type == IPV6_ADDR_ANY &&
74             !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
75                 return 1;
76
77         if (sk2_rcv_saddr6 &&
78             ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
79                 return 1;
80
81         return 0;
82 }
83
84 int udp_v6_get_port(struct sock *sk, unsigned short snum)
85 {
86         return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
87 }
88
89 static inline int compute_score(struct sock *sk, struct net *net,
90                                 unsigned short hnum,
91                                 struct in6_addr *saddr, __be16 sport,
92                                 struct in6_addr *daddr, __be16 dport,
93                                 int dif)
94 {
95         int score = -1;
96
97         if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
98                         sk->sk_family == PF_INET6) {
99                 struct ipv6_pinfo *np = inet6_sk(sk);
100                 struct inet_sock *inet = inet_sk(sk);
101
102                 score = 0;
103                 if (inet->dport) {
104                         if (inet->dport != sport)
105                                 return -1;
106                         score++;
107                 }
108                 if (!ipv6_addr_any(&np->rcv_saddr)) {
109                         if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
110                                 return -1;
111                         score++;
112                 }
113                 if (!ipv6_addr_any(&np->daddr)) {
114                         if (!ipv6_addr_equal(&np->daddr, saddr))
115                                 return -1;
116                         score++;
117                 }
118                 if (sk->sk_bound_dev_if) {
119                         if (sk->sk_bound_dev_if != dif)
120                                 return -1;
121                         score++;
122                 }
123         }
124         return score;
125 }
126
127 static struct sock *__udp6_lib_lookup(struct net *net,
128                                       struct in6_addr *saddr, __be16 sport,
129                                       struct in6_addr *daddr, __be16 dport,
130                                       int dif, struct udp_table *udptable)
131 {
132         struct sock *sk, *result;
133         struct hlist_nulls_node *node;
134         unsigned short hnum = ntohs(dport);
135         unsigned int hash = udp_hashfn(net, hnum);
136         struct udp_hslot *hslot = &udptable->hash[hash];
137         int score, badness;
138
139         rcu_read_lock();
140 begin:
141         result = NULL;
142         badness = -1;
143         sk_nulls_for_each_rcu(sk, node, &hslot->head) {
144                 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
145                 if (score > badness) {
146                         result = sk;
147                         badness = score;
148                 }
149         }
150         /*
151          * if the nulls value we got at the end of this lookup is
152          * not the expected one, we must restart lookup.
153          * We probably met an item that was moved to another chain.
154          */
155         if (get_nulls_value(node) != hash)
156                 goto begin;
157
158         if (result) {
159                 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
160                         result = NULL;
161                 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
162                                         daddr, dport, dif) < badness)) {
163                         sock_put(result);
164                         goto begin;
165                 }
166         }
167         rcu_read_unlock();
168         return result;
169 }
170
171 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
172                                           __be16 sport, __be16 dport,
173                                           struct udp_table *udptable)
174 {
175         struct sock *sk;
176         struct ipv6hdr *iph = ipv6_hdr(skb);
177
178         if (unlikely(sk = skb_steal_sock(skb)))
179                 return sk;
180         else
181                 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
182                                          &iph->daddr, dport, inet6_iif(skb),
183                                          udptable);
184 }
185
186 /*
187  *      This should be easy, if there is something there we
188  *      return it, otherwise we block.
189  */
190
191 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
192                   struct msghdr *msg, size_t len,
193                   int noblock, int flags, int *addr_len)
194 {
195         struct ipv6_pinfo *np = inet6_sk(sk);
196         struct inet_sock *inet = inet_sk(sk);
197         struct sk_buff *skb;
198         unsigned int ulen, copied;
199         int peeked;
200         int err;
201         int is_udplite = IS_UDPLITE(sk);
202         int is_udp4;
203
204         if (addr_len)
205                 *addr_len=sizeof(struct sockaddr_in6);
206
207         if (flags & MSG_ERRQUEUE)
208                 return ipv6_recv_error(sk, msg, len);
209
210 try_again:
211         skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
212                                   &peeked, &err);
213         if (!skb)
214                 goto out;
215
216         ulen = skb->len - sizeof(struct udphdr);
217         copied = len;
218         if (copied > ulen)
219                 copied = ulen;
220         else if (copied < ulen)
221                 msg->msg_flags |= MSG_TRUNC;
222
223         is_udp4 = (skb->protocol == htons(ETH_P_IP));
224
225         /*
226          * If checksum is needed at all, try to do it while copying the
227          * data.  If the data is truncated, or if we only want a partial
228          * coverage checksum (UDP-Lite), do it before the copy.
229          */
230
231         if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
232                 if (udp_lib_checksum_complete(skb))
233                         goto csum_copy_err;
234         }
235
236         if (skb_csum_unnecessary(skb))
237                 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
238                                               msg->msg_iov, copied       );
239         else {
240                 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
241                 if (err == -EINVAL)
242                         goto csum_copy_err;
243         }
244         if (err)
245                 goto out_free;
246
247         if (!peeked) {
248                 if (is_udp4)
249                         UDP_INC_STATS_USER(sock_net(sk),
250                                         UDP_MIB_INDATAGRAMS, is_udplite);
251                 else
252                         UDP6_INC_STATS_USER(sock_net(sk),
253                                         UDP_MIB_INDATAGRAMS, is_udplite);
254         }
255
256         sock_recv_timestamp(msg, sk, skb);
257
258         /* Copy the address. */
259         if (msg->msg_name) {
260                 struct sockaddr_in6 *sin6;
261
262                 sin6 = (struct sockaddr_in6 *) msg->msg_name;
263                 sin6->sin6_family = AF_INET6;
264                 sin6->sin6_port = udp_hdr(skb)->source;
265                 sin6->sin6_flowinfo = 0;
266                 sin6->sin6_scope_id = 0;
267
268                 if (is_udp4)
269                         ipv6_addr_set(&sin6->sin6_addr, 0, 0,
270                                       htonl(0xffff), ip_hdr(skb)->saddr);
271                 else {
272                         ipv6_addr_copy(&sin6->sin6_addr,
273                                        &ipv6_hdr(skb)->saddr);
274                         if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
275                                 sin6->sin6_scope_id = IP6CB(skb)->iif;
276                 }
277
278         }
279         if (is_udp4) {
280                 if (inet->cmsg_flags)
281                         ip_cmsg_recv(msg, skb);
282         } else {
283                 if (np->rxopt.all)
284                         datagram_recv_ctl(sk, msg, skb);
285         }
286
287         err = copied;
288         if (flags & MSG_TRUNC)
289                 err = ulen;
290
291 out_free:
292         lock_sock(sk);
293         skb_free_datagram(sk, skb);
294         release_sock(sk);
295 out:
296         return err;
297
298 csum_copy_err:
299         lock_sock(sk);
300         if (!skb_kill_datagram(sk, skb, flags)) {
301                 if (is_udp4)
302                         UDP_INC_STATS_USER(sock_net(sk),
303                                         UDP_MIB_INERRORS, is_udplite);
304                 else
305                         UDP6_INC_STATS_USER(sock_net(sk),
306                                         UDP_MIB_INERRORS, is_udplite);
307         }
308         release_sock(sk);
309
310         if (flags & MSG_DONTWAIT)
311                 return -EAGAIN;
312         goto try_again;
313 }
314
315 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
316                     int type, int code, int offset, __be32 info,
317                     struct udp_table *udptable)
318 {
319         struct ipv6_pinfo *np;
320         struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
321         struct in6_addr *saddr = &hdr->saddr;
322         struct in6_addr *daddr = &hdr->daddr;
323         struct udphdr *uh = (struct udphdr*)(skb->data+offset);
324         struct sock *sk;
325         int err;
326
327         sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
328                                saddr, uh->source, inet6_iif(skb), udptable);
329         if (sk == NULL)
330                 return;
331
332         np = inet6_sk(sk);
333
334         if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
335                 goto out;
336
337         if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
338                 goto out;
339
340         if (np->recverr)
341                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
342
343         sk->sk_err = err;
344         sk->sk_error_report(sk);
345 out:
346         sock_put(sk);
347 }
348
349 static __inline__ void udpv6_err(struct sk_buff *skb,
350                                  struct inet6_skb_parm *opt, int type,
351                                  int code, int offset, __be32 info     )
352 {
353         __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
354 }
355
356 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
357 {
358         struct udp_sock *up = udp_sk(sk);
359         int rc;
360         int is_udplite = IS_UDPLITE(sk);
361
362         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
363                 goto drop;
364
365         /*
366          * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
367          */
368         if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
369
370                 if (up->pcrlen == 0) {          /* full coverage was set  */
371                         LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
372                                 " %d while full coverage %d requested\n",
373                                 UDP_SKB_CB(skb)->cscov, skb->len);
374                         goto drop;
375                 }
376                 if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
377                         LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
378                                                     "too small, need min %d\n",
379                                        UDP_SKB_CB(skb)->cscov, up->pcrlen);
380                         goto drop;
381                 }
382         }
383
384         if (sk->sk_filter) {
385                 if (udp_lib_checksum_complete(skb))
386                         goto drop;
387         }
388
389         if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
390                 /* Note that an ENOMEM error is charged twice */
391                 if (rc == -ENOMEM) {
392                         UDP6_INC_STATS_BH(sock_net(sk),
393                                         UDP_MIB_RCVBUFERRORS, is_udplite);
394                         atomic_inc(&sk->sk_drops);
395                 }
396                 goto drop;
397         }
398
399         return 0;
400 drop:
401         UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
402         kfree_skb(skb);
403         return -1;
404 }
405
406 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
407                                       __be16 loc_port, struct in6_addr *loc_addr,
408                                       __be16 rmt_port, struct in6_addr *rmt_addr,
409                                       int dif)
410 {
411         struct hlist_nulls_node *node;
412         struct sock *s = sk;
413         unsigned short num = ntohs(loc_port);
414
415         sk_nulls_for_each_from(s, node) {
416                 struct inet_sock *inet = inet_sk(s);
417
418                 if (!net_eq(sock_net(s), net))
419                         continue;
420
421                 if (s->sk_hash == num && s->sk_family == PF_INET6) {
422                         struct ipv6_pinfo *np = inet6_sk(s);
423                         if (inet->dport) {
424                                 if (inet->dport != rmt_port)
425                                         continue;
426                         }
427                         if (!ipv6_addr_any(&np->daddr) &&
428                             !ipv6_addr_equal(&np->daddr, rmt_addr))
429                                 continue;
430
431                         if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
432                                 continue;
433
434                         if (!ipv6_addr_any(&np->rcv_saddr)) {
435                                 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
436                                         continue;
437                         }
438                         if (!inet6_mc_check(s, loc_addr, rmt_addr))
439                                 continue;
440                         return s;
441                 }
442         }
443         return NULL;
444 }
445
446 /*
447  * Note: called only from the BH handler context,
448  * so we don't need to lock the hashes.
449  */
450 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
451                 struct in6_addr *saddr, struct in6_addr *daddr,
452                 struct udp_table *udptable)
453 {
454         struct sock *sk, *sk2;
455         const struct udphdr *uh = udp_hdr(skb);
456         struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
457         int dif;
458
459         spin_lock(&hslot->lock);
460         sk = sk_nulls_head(&hslot->head);
461         dif = inet6_iif(skb);
462         sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
463         if (!sk) {
464                 kfree_skb(skb);
465                 goto out;
466         }
467
468         sk2 = sk;
469         while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
470                                         uh->source, saddr, dif))) {
471                 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
472                 if (buff) {
473                         bh_lock_sock(sk2);
474                         if (!sock_owned_by_user(sk2))
475                                 udpv6_queue_rcv_skb(sk2, buff);
476                         else
477                                 sk_add_backlog(sk2, buff);
478                         bh_unlock_sock(sk2);
479                 }
480         }
481         bh_lock_sock(sk);
482         if (!sock_owned_by_user(sk))
483                 udpv6_queue_rcv_skb(sk, skb);
484         else
485                 sk_add_backlog(sk, skb);
486         bh_unlock_sock(sk);
487 out:
488         spin_unlock(&hslot->lock);
489         return 0;
490 }
491
492 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
493                                  int proto)
494 {
495         int err;
496
497         UDP_SKB_CB(skb)->partial_cov = 0;
498         UDP_SKB_CB(skb)->cscov = skb->len;
499
500         if (proto == IPPROTO_UDPLITE) {
501                 err = udplite_checksum_init(skb, uh);
502                 if (err)
503                         return err;
504         }
505
506         if (uh->check == 0) {
507                 /* RFC 2460 section 8.1 says that we SHOULD log
508                    this error. Well, it is reasonable.
509                  */
510                 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
511                 return 1;
512         }
513         if (skb->ip_summed == CHECKSUM_COMPLETE &&
514             !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
515                              skb->len, proto, skb->csum))
516                 skb->ip_summed = CHECKSUM_UNNECESSARY;
517
518         if (!skb_csum_unnecessary(skb))
519                 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
520                                                          &ipv6_hdr(skb)->daddr,
521                                                          skb->len, proto, 0));
522
523         return 0;
524 }
525
526 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
527                    int proto)
528 {
529         struct sock *sk;
530         struct udphdr *uh;
531         struct net_device *dev = skb->dev;
532         struct in6_addr *saddr, *daddr;
533         u32 ulen = 0;
534         struct net *net = dev_net(skb->dev);
535
536         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
537                 goto short_packet;
538
539         saddr = &ipv6_hdr(skb)->saddr;
540         daddr = &ipv6_hdr(skb)->daddr;
541         uh = udp_hdr(skb);
542
543         ulen = ntohs(uh->len);
544         if (ulen > skb->len)
545                 goto short_packet;
546
547         if (proto == IPPROTO_UDP) {
548                 /* UDP validates ulen. */
549
550                 /* Check for jumbo payload */
551                 if (ulen == 0)
552                         ulen = skb->len;
553
554                 if (ulen < sizeof(*uh))
555                         goto short_packet;
556
557                 if (ulen < skb->len) {
558                         if (pskb_trim_rcsum(skb, ulen))
559                                 goto short_packet;
560                         saddr = &ipv6_hdr(skb)->saddr;
561                         daddr = &ipv6_hdr(skb)->daddr;
562                         uh = udp_hdr(skb);
563                 }
564         }
565
566         if (udp6_csum_init(skb, uh, proto))
567                 goto discard;
568
569         /*
570          *      Multicast receive code
571          */
572         if (ipv6_addr_is_multicast(daddr))
573                 return __udp6_lib_mcast_deliver(net, skb,
574                                 saddr, daddr, udptable);
575
576         /* Unicast */
577
578         /*
579          * check socket cache ... must talk to Alan about his plans
580          * for sock caches... i'll skip this for now.
581          */
582         sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
583
584         if (sk == NULL) {
585                 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
586                         goto discard;
587
588                 if (udp_lib_checksum_complete(skb))
589                         goto discard;
590                 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
591                                 proto == IPPROTO_UDPLITE);
592
593                 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
594
595                 kfree_skb(skb);
596                 return 0;
597         }
598
599         /* deliver */
600
601         bh_lock_sock(sk);
602         if (!sock_owned_by_user(sk))
603                 udpv6_queue_rcv_skb(sk, skb);
604         else
605                 sk_add_backlog(sk, skb);
606         bh_unlock_sock(sk);
607         sock_put(sk);
608         return 0;
609
610 short_packet:
611         LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
612                        proto == IPPROTO_UDPLITE ? "-Lite" : "",
613                        ulen, skb->len);
614
615 discard:
616         UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
617         kfree_skb(skb);
618         return 0;
619 }
620
621 static __inline__ int udpv6_rcv(struct sk_buff *skb)
622 {
623         return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
624 }
625
626 /*
627  * Throw away all pending data and cancel the corking. Socket is locked.
628  */
629 static void udp_v6_flush_pending_frames(struct sock *sk)
630 {
631         struct udp_sock *up = udp_sk(sk);
632
633         if (up->pending == AF_INET)
634                 udp_flush_pending_frames(sk);
635         else if (up->pending) {
636                 up->len = 0;
637                 up->pending = 0;
638                 ip6_flush_pending_frames(sk);
639         }
640 }
641
642 /*
643  *      Sending
644  */
645
646 static int udp_v6_push_pending_frames(struct sock *sk)
647 {
648         struct sk_buff *skb;
649         struct udphdr *uh;
650         struct udp_sock  *up = udp_sk(sk);
651         struct inet_sock *inet = inet_sk(sk);
652         struct flowi *fl = &inet->cork.fl;
653         int err = 0;
654         int is_udplite = IS_UDPLITE(sk);
655         __wsum csum = 0;
656
657         /* Grab the skbuff where UDP header space exists. */
658         if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
659                 goto out;
660
661         /*
662          * Create a UDP header
663          */
664         uh = udp_hdr(skb);
665         uh->source = fl->fl_ip_sport;
666         uh->dest = fl->fl_ip_dport;
667         uh->len = htons(up->len);
668         uh->check = 0;
669
670         if (is_udplite)
671                 csum = udplite_csum_outgoing(sk, skb);
672          else
673                 csum = udp_csum_outgoing(sk, skb);
674
675         /* add protocol-dependent pseudo-header */
676         uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
677                                     up->len, fl->proto, csum   );
678         if (uh->check == 0)
679                 uh->check = CSUM_MANGLED_0;
680
681         err = ip6_push_pending_frames(sk);
682 out:
683         up->len = 0;
684         up->pending = 0;
685         if (!err)
686                 UDP6_INC_STATS_USER(sock_net(sk),
687                                 UDP_MIB_OUTDATAGRAMS, is_udplite);
688         return err;
689 }
690
691 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
692                   struct msghdr *msg, size_t len)
693 {
694         struct ipv6_txoptions opt_space;
695         struct udp_sock *up = udp_sk(sk);
696         struct inet_sock *inet = inet_sk(sk);
697         struct ipv6_pinfo *np = inet6_sk(sk);
698         struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
699         struct in6_addr *daddr, *final_p = NULL, final;
700         struct ipv6_txoptions *opt = NULL;
701         struct ip6_flowlabel *flowlabel = NULL;
702         struct flowi fl;
703         struct dst_entry *dst;
704         int addr_len = msg->msg_namelen;
705         int ulen = len;
706         int hlimit = -1;
707         int tclass = -1;
708         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
709         int err;
710         int connected = 0;
711         int is_udplite = IS_UDPLITE(sk);
712         int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
713
714         /* destination address check */
715         if (sin6) {
716                 if (addr_len < offsetof(struct sockaddr, sa_data))
717                         return -EINVAL;
718
719                 switch (sin6->sin6_family) {
720                 case AF_INET6:
721                         if (addr_len < SIN6_LEN_RFC2133)
722                                 return -EINVAL;
723                         daddr = &sin6->sin6_addr;
724                         break;
725                 case AF_INET:
726                         goto do_udp_sendmsg;
727                 case AF_UNSPEC:
728                         msg->msg_name = sin6 = NULL;
729                         msg->msg_namelen = addr_len = 0;
730                         daddr = NULL;
731                         break;
732                 default:
733                         return -EINVAL;
734                 }
735         } else if (!up->pending) {
736                 if (sk->sk_state != TCP_ESTABLISHED)
737                         return -EDESTADDRREQ;
738                 daddr = &np->daddr;
739         } else
740                 daddr = NULL;
741
742         if (daddr) {
743                 if (ipv6_addr_v4mapped(daddr)) {
744                         struct sockaddr_in sin;
745                         sin.sin_family = AF_INET;
746                         sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
747                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
748                         msg->msg_name = &sin;
749                         msg->msg_namelen = sizeof(sin);
750 do_udp_sendmsg:
751                         if (__ipv6_only_sock(sk))
752                                 return -ENETUNREACH;
753                         return udp_sendmsg(iocb, sk, msg, len);
754                 }
755         }
756
757         if (up->pending == AF_INET)
758                 return udp_sendmsg(iocb, sk, msg, len);
759
760         /* Rough check on arithmetic overflow,
761            better check is made in ip6_append_data().
762            */
763         if (len > INT_MAX - sizeof(struct udphdr))
764                 return -EMSGSIZE;
765
766         if (up->pending) {
767                 /*
768                  * There are pending frames.
769                  * The socket lock must be held while it's corked.
770                  */
771                 lock_sock(sk);
772                 if (likely(up->pending)) {
773                         if (unlikely(up->pending != AF_INET6)) {
774                                 release_sock(sk);
775                                 return -EAFNOSUPPORT;
776                         }
777                         dst = NULL;
778                         goto do_append_data;
779                 }
780                 release_sock(sk);
781         }
782         ulen += sizeof(struct udphdr);
783
784         memset(&fl, 0, sizeof(fl));
785
786         if (sin6) {
787                 if (sin6->sin6_port == 0)
788                         return -EINVAL;
789
790                 fl.fl_ip_dport = sin6->sin6_port;
791                 daddr = &sin6->sin6_addr;
792
793                 if (np->sndflow) {
794                         fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
795                         if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
796                                 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
797                                 if (flowlabel == NULL)
798                                         return -EINVAL;
799                                 daddr = &flowlabel->dst;
800                         }
801                 }
802
803                 /*
804                  * Otherwise it will be difficult to maintain
805                  * sk->sk_dst_cache.
806                  */
807                 if (sk->sk_state == TCP_ESTABLISHED &&
808                     ipv6_addr_equal(daddr, &np->daddr))
809                         daddr = &np->daddr;
810
811                 if (addr_len >= sizeof(struct sockaddr_in6) &&
812                     sin6->sin6_scope_id &&
813                     ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
814                         fl.oif = sin6->sin6_scope_id;
815         } else {
816                 if (sk->sk_state != TCP_ESTABLISHED)
817                         return -EDESTADDRREQ;
818
819                 fl.fl_ip_dport = inet->dport;
820                 daddr = &np->daddr;
821                 fl.fl6_flowlabel = np->flow_label;
822                 connected = 1;
823         }
824
825         if (!fl.oif)
826                 fl.oif = sk->sk_bound_dev_if;
827
828         if (!fl.oif)
829                 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
830
831         if (msg->msg_controllen) {
832                 opt = &opt_space;
833                 memset(opt, 0, sizeof(struct ipv6_txoptions));
834                 opt->tot_len = sizeof(*opt);
835
836                 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
837                 if (err < 0) {
838                         fl6_sock_release(flowlabel);
839                         return err;
840                 }
841                 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
842                         flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
843                         if (flowlabel == NULL)
844                                 return -EINVAL;
845                 }
846                 if (!(opt->opt_nflen|opt->opt_flen))
847                         opt = NULL;
848                 connected = 0;
849         }
850         if (opt == NULL)
851                 opt = np->opt;
852         if (flowlabel)
853                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
854         opt = ipv6_fixup_options(&opt_space, opt);
855
856         fl.proto = sk->sk_protocol;
857         if (!ipv6_addr_any(daddr))
858                 ipv6_addr_copy(&fl.fl6_dst, daddr);
859         else
860                 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
861         if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
862                 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
863         fl.fl_ip_sport = inet->sport;
864
865         /* merge ip6_build_xmit from ip6_output */
866         if (opt && opt->srcrt) {
867                 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
868                 ipv6_addr_copy(&final, &fl.fl6_dst);
869                 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
870                 final_p = &final;
871                 connected = 0;
872         }
873
874         if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
875                 fl.oif = np->mcast_oif;
876                 connected = 0;
877         }
878
879         security_sk_classify_flow(sk, &fl);
880
881         err = ip6_sk_dst_lookup(sk, &dst, &fl);
882         if (err)
883                 goto out;
884         if (final_p)
885                 ipv6_addr_copy(&fl.fl6_dst, final_p);
886
887         err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
888         if (err < 0) {
889                 if (err == -EREMOTE)
890                         err = ip6_dst_blackhole(sk, &dst, &fl);
891                 if (err < 0)
892                         goto out;
893         }
894
895         if (hlimit < 0) {
896                 if (ipv6_addr_is_multicast(&fl.fl6_dst))
897                         hlimit = np->mcast_hops;
898                 else
899                         hlimit = np->hop_limit;
900                 if (hlimit < 0)
901                         hlimit = ip6_dst_hoplimit(dst);
902         }
903
904         if (tclass < 0) {
905                 tclass = np->tclass;
906                 if (tclass < 0)
907                         tclass = 0;
908         }
909
910         if (msg->msg_flags&MSG_CONFIRM)
911                 goto do_confirm;
912 back_from_confirm:
913
914         lock_sock(sk);
915         if (unlikely(up->pending)) {
916                 /* The socket is already corked while preparing it. */
917                 /* ... which is an evident application bug. --ANK */
918                 release_sock(sk);
919
920                 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
921                 err = -EINVAL;
922                 goto out;
923         }
924
925         up->pending = AF_INET6;
926
927 do_append_data:
928         up->len += ulen;
929         getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
930         err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
931                 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
932                 (struct rt6_info*)dst,
933                 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
934         if (err)
935                 udp_v6_flush_pending_frames(sk);
936         else if (!corkreq)
937                 err = udp_v6_push_pending_frames(sk);
938         else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
939                 up->pending = 0;
940
941         if (dst) {
942                 if (connected) {
943                         ip6_dst_store(sk, dst,
944                                       ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
945                                       &np->daddr : NULL,
946 #ifdef CONFIG_IPV6_SUBTREES
947                                       ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
948                                       &np->saddr :
949 #endif
950                                       NULL);
951                 } else {
952                         dst_release(dst);
953                 }
954                 dst = NULL;
955         }
956
957         if (err > 0)
958                 err = np->recverr ? net_xmit_errno(err) : 0;
959         release_sock(sk);
960 out:
961         dst_release(dst);
962         fl6_sock_release(flowlabel);
963         if (!err)
964                 return len;
965         /*
966          * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
967          * ENOBUFS might not be good (it's not tunable per se), but otherwise
968          * we don't have a good statistic (IpOutDiscards but it can be too many
969          * things).  We could add another new stat but at least for now that
970          * seems like overkill.
971          */
972         if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
973                 UDP6_INC_STATS_USER(sock_net(sk),
974                                 UDP_MIB_SNDBUFERRORS, is_udplite);
975         }
976         return err;
977
978 do_confirm:
979         dst_confirm(dst);
980         if (!(msg->msg_flags&MSG_PROBE) || len)
981                 goto back_from_confirm;
982         err = 0;
983         goto out;
984 }
985
986 void udpv6_destroy_sock(struct sock *sk)
987 {
988         lock_sock(sk);
989         udp_v6_flush_pending_frames(sk);
990         release_sock(sk);
991
992         inet6_destroy_sock(sk);
993 }
994
995 /*
996  *      Socket option code for UDP
997  */
998 int udpv6_setsockopt(struct sock *sk, int level, int optname,
999                      char __user *optval, int optlen)
1000 {
1001         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1002                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1003                                           udp_v6_push_pending_frames);
1004         return ipv6_setsockopt(sk, level, optname, optval, optlen);
1005 }
1006
1007 #ifdef CONFIG_COMPAT
1008 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1009                             char __user *optval, int optlen)
1010 {
1011         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1012                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1013                                           udp_v6_push_pending_frames);
1014         return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1015 }
1016 #endif
1017
1018 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1019                      char __user *optval, int __user *optlen)
1020 {
1021         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1022                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1023         return ipv6_getsockopt(sk, level, optname, optval, optlen);
1024 }
1025
1026 #ifdef CONFIG_COMPAT
1027 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1028                             char __user *optval, int __user *optlen)
1029 {
1030         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1031                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1032         return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1033 }
1034 #endif
1035
1036 static struct inet6_protocol udpv6_protocol = {
1037         .handler        =       udpv6_rcv,
1038         .err_handler    =       udpv6_err,
1039         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1040 };
1041
1042 /* ------------------------------------------------------------------------ */
1043 #ifdef CONFIG_PROC_FS
1044
1045 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1046 {
1047         struct inet_sock *inet = inet_sk(sp);
1048         struct ipv6_pinfo *np = inet6_sk(sp);
1049         struct in6_addr *dest, *src;
1050         __u16 destp, srcp;
1051
1052         dest  = &np->daddr;
1053         src   = &np->rcv_saddr;
1054         destp = ntohs(inet->dport);
1055         srcp  = ntohs(inet->sport);
1056         seq_printf(seq,
1057                    "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1058                    "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1059                    bucket,
1060                    src->s6_addr32[0], src->s6_addr32[1],
1061                    src->s6_addr32[2], src->s6_addr32[3], srcp,
1062                    dest->s6_addr32[0], dest->s6_addr32[1],
1063                    dest->s6_addr32[2], dest->s6_addr32[3], destp,
1064                    sp->sk_state,
1065                    atomic_read(&sp->sk_wmem_alloc),
1066                    atomic_read(&sp->sk_rmem_alloc),
1067                    0, 0L, 0,
1068                    sock_i_uid(sp), 0,
1069                    sock_i_ino(sp),
1070                    atomic_read(&sp->sk_refcnt), sp,
1071                    atomic_read(&sp->sk_drops));
1072 }
1073
1074 int udp6_seq_show(struct seq_file *seq, void *v)
1075 {
1076         if (v == SEQ_START_TOKEN)
1077                 seq_printf(seq,
1078                            "  sl  "
1079                            "local_address                         "
1080                            "remote_address                        "
1081                            "st tx_queue rx_queue tr tm->when retrnsmt"
1082                            "   uid  timeout inode ref pointer drops\n");
1083         else
1084                 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1085         return 0;
1086 }
1087
1088 static struct udp_seq_afinfo udp6_seq_afinfo = {
1089         .name           = "udp6",
1090         .family         = AF_INET6,
1091         .udp_table      = &udp_table,
1092         .seq_fops       = {
1093                 .owner  =       THIS_MODULE,
1094         },
1095         .seq_ops        = {
1096                 .show           = udp6_seq_show,
1097         },
1098 };
1099
1100 int udp6_proc_init(struct net *net)
1101 {
1102         return udp_proc_register(net, &udp6_seq_afinfo);
1103 }
1104
1105 void udp6_proc_exit(struct net *net) {
1106         udp_proc_unregister(net, &udp6_seq_afinfo);
1107 }
1108 #endif /* CONFIG_PROC_FS */
1109
1110 /* ------------------------------------------------------------------------ */
1111
1112 struct proto udpv6_prot = {
1113         .name              = "UDPv6",
1114         .owner             = THIS_MODULE,
1115         .close             = udp_lib_close,
1116         .connect           = ip6_datagram_connect,
1117         .disconnect        = udp_disconnect,
1118         .ioctl             = udp_ioctl,
1119         .destroy           = udpv6_destroy_sock,
1120         .setsockopt        = udpv6_setsockopt,
1121         .getsockopt        = udpv6_getsockopt,
1122         .sendmsg           = udpv6_sendmsg,
1123         .recvmsg           = udpv6_recvmsg,
1124         .backlog_rcv       = udpv6_queue_rcv_skb,
1125         .hash              = udp_lib_hash,
1126         .unhash            = udp_lib_unhash,
1127         .get_port          = udp_v6_get_port,
1128         .memory_allocated  = &udp_memory_allocated,
1129         .sysctl_mem        = sysctl_udp_mem,
1130         .sysctl_wmem       = &sysctl_udp_wmem_min,
1131         .sysctl_rmem       = &sysctl_udp_rmem_min,
1132         .obj_size          = sizeof(struct udp6_sock),
1133         .slab_flags        = SLAB_DESTROY_BY_RCU,
1134         .h.udp_table       = &udp_table,
1135 #ifdef CONFIG_COMPAT
1136         .compat_setsockopt = compat_udpv6_setsockopt,
1137         .compat_getsockopt = compat_udpv6_getsockopt,
1138 #endif
1139 };
1140
1141 static struct inet_protosw udpv6_protosw = {
1142         .type =      SOCK_DGRAM,
1143         .protocol =  IPPROTO_UDP,
1144         .prot =      &udpv6_prot,
1145         .ops =       &inet6_dgram_ops,
1146         .capability =-1,
1147         .no_check =  UDP_CSUM_DEFAULT,
1148         .flags =     INET_PROTOSW_PERMANENT,
1149 };
1150
1151
1152 int __init udpv6_init(void)
1153 {
1154         int ret;
1155
1156         ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1157         if (ret)
1158                 goto out;
1159
1160         ret = inet6_register_protosw(&udpv6_protosw);
1161         if (ret)
1162                 goto out_udpv6_protocol;
1163 out:
1164         return ret;
1165
1166 out_udpv6_protocol:
1167         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1168         goto out;
1169 }
1170
1171 void udpv6_exit(void)
1172 {
1173         inet6_unregister_protosw(&udpv6_protosw);
1174         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1175 }