~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/udp.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp