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

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

Version: ~ [ linux-6.4-rc3 ] ~ [ linux-6.3.4 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.30 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.113 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.180 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.243 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.283 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.315 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ 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 <linux/uaccess.h>
 39 
 40 #include <net/addrconf.h>
 41 #include <net/ndisc.h>
 42 #include <net/protocol.h>
 43 #include <net/transp_v6.h>
 44 #include <net/ip6_route.h>
 45 #include <net/raw.h>
 46 #include <net/tcp_states.h>
 47 #include <net/ip6_checksum.h>
 48 #include <net/xfrm.h>
 49 #include <net/inet_hashtables.h>
 50 #include <net/inet6_hashtables.h>
 51 #include <net/busy_poll.h>
 52 #include <net/sock_reuseport.h>
 53 
 54 #include <linux/proc_fs.h>
 55 #include <linux/seq_file.h>
 56 #include <trace/events/skb.h>
 57 #include "udp_impl.h"
 58 
 59 static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
 60 {
 61 #if defined(CONFIG_NET_L3_MASTER_DEV)
 62         if (!net->ipv4.sysctl_udp_l3mdev_accept &&
 63             skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
 64                 return true;
 65 #endif
 66         return false;
 67 }
 68 
 69 static u32 udp6_ehashfn(const struct net *net,
 70                         const struct in6_addr *laddr,
 71                         const u16 lport,
 72                         const struct in6_addr *faddr,
 73                         const __be16 fport)
 74 {
 75         static u32 udp6_ehash_secret __read_mostly;
 76         static u32 udp_ipv6_hash_secret __read_mostly;
 77 
 78         u32 lhash, fhash;
 79 
 80         net_get_random_once(&udp6_ehash_secret,
 81                             sizeof(udp6_ehash_secret));
 82         net_get_random_once(&udp_ipv6_hash_secret,
 83                             sizeof(udp_ipv6_hash_secret));
 84 
 85         lhash = (__force u32)laddr->s6_addr32[3];
 86         fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
 87 
 88         return __inet6_ehashfn(lhash, lport, fhash, fport,
 89                                udp_ipv6_hash_secret + net_hash_mix(net));
 90 }
 91 
 92 static u32 udp6_portaddr_hash(const struct net *net,
 93                               const struct in6_addr *addr6,
 94                               unsigned int port)
 95 {
 96         unsigned int hash, mix = net_hash_mix(net);
 97 
 98         if (ipv6_addr_any(addr6))
 99                 hash = jhash_1word(0, mix);
100         else if (ipv6_addr_v4mapped(addr6))
101                 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
102         else
103                 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
104 
105         return hash ^ port;
106 }
107 
108 int udp_v6_get_port(struct sock *sk, unsigned short snum)
109 {
110         unsigned int hash2_nulladdr =
111                 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
112         unsigned int hash2_partial =
113                 udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
114 
115         /* precompute partial secondary hash */
116         udp_sk(sk)->udp_portaddr_hash = hash2_partial;
117         return udp_lib_get_port(sk, snum, hash2_nulladdr);
118 }
119 
120 static void udp_v6_rehash(struct sock *sk)
121 {
122         u16 new_hash = udp6_portaddr_hash(sock_net(sk),
123                                           &sk->sk_v6_rcv_saddr,
124                                           inet_sk(sk)->inet_num);
125 
126         udp_lib_rehash(sk, new_hash);
127 }
128 
129 static int compute_score(struct sock *sk, struct net *net,
130                          const struct in6_addr *saddr, __be16 sport,
131                          const struct in6_addr *daddr, unsigned short hnum,
132                          int dif, int sdif, bool exact_dif)
133 {
134         int score;
135         struct inet_sock *inet;
136 
137         if (!net_eq(sock_net(sk), net) ||
138             udp_sk(sk)->udp_port_hash != hnum ||
139             sk->sk_family != PF_INET6)
140                 return -1;
141 
142         score = 0;
143         inet = inet_sk(sk);
144 
145         if (inet->inet_dport) {
146                 if (inet->inet_dport != sport)
147                         return -1;
148                 score++;
149         }
150 
151         if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
152                 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
153                         return -1;
154                 score++;
155         }
156 
157         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
158                 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
159                         return -1;
160                 score++;
161         }
162 
163         if (sk->sk_bound_dev_if || exact_dif) {
164                 bool dev_match = (sk->sk_bound_dev_if == dif ||
165                                   sk->sk_bound_dev_if == sdif);
166 
167                 if (exact_dif && !dev_match)
168                         return -1;
169                 if (sk->sk_bound_dev_if && dev_match)
170                         score++;
171         }
172 
173         if (sk->sk_incoming_cpu == raw_smp_processor_id())
174                 score++;
175 
176         return score;
177 }
178 
179 /* called with rcu_read_lock() */
180 static struct sock *udp6_lib_lookup2(struct net *net,
181                 const struct in6_addr *saddr, __be16 sport,
182                 const struct in6_addr *daddr, unsigned int hnum,
183                 int dif, int sdif, bool exact_dif,
184                 struct udp_hslot *hslot2, struct sk_buff *skb)
185 {
186         struct sock *sk, *result;
187         int score, badness, matches = 0, reuseport = 0;
188         u32 hash = 0;
189 
190         result = NULL;
191         badness = -1;
192         udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
193                 score = compute_score(sk, net, saddr, sport,
194                                       daddr, hnum, dif, sdif, exact_dif);
195                 if (score > badness) {
196                         reuseport = sk->sk_reuseport;
197                         if (reuseport) {
198                                 hash = udp6_ehashfn(net, daddr, hnum,
199                                                     saddr, sport);
200 
201                                 result = reuseport_select_sock(sk, hash, skb,
202                                                         sizeof(struct udphdr));
203                                 if (result)
204                                         return result;
205                                 matches = 1;
206                         }
207                         result = sk;
208                         badness = score;
209                 } else if (score == badness && reuseport) {
210                         matches++;
211                         if (reciprocal_scale(hash, matches) == 0)
212                                 result = sk;
213                         hash = next_pseudo_random32(hash);
214                 }
215         }
216         return result;
217 }
218 
219 /* rcu_read_lock() must be held */
220 struct sock *__udp6_lib_lookup(struct net *net,
221                                const struct in6_addr *saddr, __be16 sport,
222                                const struct in6_addr *daddr, __be16 dport,
223                                int dif, int sdif, struct udp_table *udptable,
224                                struct sk_buff *skb)
225 {
226         struct sock *sk, *result;
227         unsigned short hnum = ntohs(dport);
228         unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
229         struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
230         bool exact_dif = udp6_lib_exact_dif_match(net, skb);
231         int score, badness, matches = 0, reuseport = 0;
232         u32 hash = 0;
233 
234         if (hslot->count > 10) {
235                 hash2 = udp6_portaddr_hash(net, daddr, hnum);
236                 slot2 = hash2 & udptable->mask;
237                 hslot2 = &udptable->hash2[slot2];
238                 if (hslot->count < hslot2->count)
239                         goto begin;
240 
241                 result = udp6_lib_lookup2(net, saddr, sport,
242                                           daddr, hnum, dif, sdif, exact_dif,
243                                           hslot2, skb);
244                 if (!result) {
245                         unsigned int old_slot2 = slot2;
246                         hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
247                         slot2 = hash2 & udptable->mask;
248                         /* avoid searching the same slot again. */
249                         if (unlikely(slot2 == old_slot2))
250                                 return result;
251 
252                         hslot2 = &udptable->hash2[slot2];
253                         if (hslot->count < hslot2->count)
254                                 goto begin;
255 
256                         result = udp6_lib_lookup2(net, saddr, sport,
257                                                   daddr, hnum, dif, sdif,
258                                                   exact_dif, hslot2,
259                                                   skb);
260                 }
261                 return result;
262         }
263 begin:
264         result = NULL;
265         badness = -1;
266         sk_for_each_rcu(sk, &hslot->head) {
267                 score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
268                                       sdif, exact_dif);
269                 if (score > badness) {
270                         reuseport = sk->sk_reuseport;
271                         if (reuseport) {
272                                 hash = udp6_ehashfn(net, daddr, hnum,
273                                                     saddr, sport);
274                                 result = reuseport_select_sock(sk, hash, skb,
275                                                         sizeof(struct udphdr));
276                                 if (result)
277                                         return result;
278                                 matches = 1;
279                         }
280                         result = sk;
281                         badness = score;
282                 } else if (score == badness && reuseport) {
283                         matches++;
284                         if (reciprocal_scale(hash, matches) == 0)
285                                 result = sk;
286                         hash = next_pseudo_random32(hash);
287                 }
288         }
289         return result;
290 }
291 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
292 
293 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
294                                           __be16 sport, __be16 dport,
295                                           struct udp_table *udptable)
296 {
297         const struct ipv6hdr *iph = ipv6_hdr(skb);
298 
299         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
300                                  &iph->daddr, dport, inet6_iif(skb),
301                                  inet6_sdif(skb), udptable, skb);
302 }
303 
304 struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
305                                  __be16 sport, __be16 dport)
306 {
307         const struct ipv6hdr *iph = ipv6_hdr(skb);
308 
309         return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
310                                  &iph->daddr, dport, inet6_iif(skb),
311                                  inet6_sdif(skb), &udp_table, skb);
312 }
313 EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
314 
315 /* Must be called under rcu_read_lock().
316  * Does increment socket refcount.
317  */
318 #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
319     IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY) || \
320     IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
321 struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
322                              const struct in6_addr *daddr, __be16 dport, int dif)
323 {
324         struct sock *sk;
325 
326         sk =  __udp6_lib_lookup(net, saddr, sport, daddr, dport,
327                                 dif, 0, &udp_table, NULL);
328         if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
329                 sk = NULL;
330         return sk;
331 }
332 EXPORT_SYMBOL_GPL(udp6_lib_lookup);
333 #endif
334 
335 /* do not use the scratch area len for jumbogram: their length execeeds the
336  * scratch area space; note that the IP6CB flags is still in the first
337  * cacheline, so checking for jumbograms is cheap
338  */
339 static int udp6_skb_len(struct sk_buff *skb)
340 {
341         return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
342 }
343 
344 /*
345  *      This should be easy, if there is something there we
346  *      return it, otherwise we block.
347  */
348 
349 int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
350                   int noblock, int flags, int *addr_len)
351 {
352         struct ipv6_pinfo *np = inet6_sk(sk);
353         struct inet_sock *inet = inet_sk(sk);
354         struct sk_buff *skb;
355         unsigned int ulen, copied;
356         int peeked, peeking, off;
357         int err;
358         int is_udplite = IS_UDPLITE(sk);
359         bool checksum_valid = false;
360         int is_udp4;
361 
362         if (flags & MSG_ERRQUEUE)
363                 return ipv6_recv_error(sk, msg, len, addr_len);
364 
365         if (np->rxpmtu && np->rxopt.bits.rxpmtu)
366                 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
367 
368 try_again:
369         peeking = flags & MSG_PEEK;
370         off = sk_peek_offset(sk, flags);
371         skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
372         if (!skb)
373                 return err;
374         if (ccs_socket_post_recvmsg_permission(sk, skb, flags))
375                 return -EAGAIN; /* Hope less harmful than -EPERM. */
376 
377         ulen = udp6_skb_len(skb);
378         copied = len;
379         if (copied > ulen - off)
380                 copied = ulen - off;
381         else if (copied < ulen)
382                 msg->msg_flags |= MSG_TRUNC;
383 
384         is_udp4 = (skb->protocol == htons(ETH_P_IP));
385 
386         /*
387          * If checksum is needed at all, try to do it while copying the
388          * data.  If the data is truncated, or if we only want a partial
389          * coverage checksum (UDP-Lite), do it before the copy.
390          */
391 
392         if (copied < ulen || peeking ||
393             (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
394                 checksum_valid = udp_skb_csum_unnecessary(skb) ||
395                                 !__udp_lib_checksum_complete(skb);
396                 if (!checksum_valid)
397                         goto csum_copy_err;
398         }
399 
400         if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
401                 if (udp_skb_is_linear(skb))
402                         err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
403                 else
404                         err = skb_copy_datagram_msg(skb, off, msg, copied);
405         } else {
406                 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
407                 if (err == -EINVAL)
408                         goto csum_copy_err;
409         }
410         if (unlikely(err)) {
411                 if (!peeked) {
412                         atomic_inc(&sk->sk_drops);
413                         if (is_udp4)
414                                 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
415                                               is_udplite);
416                         else
417                                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
418                                                is_udplite);
419                 }
420                 kfree_skb(skb);
421                 return err;
422         }
423         if (!peeked) {
424                 if (is_udp4)
425                         UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
426                                       is_udplite);
427                 else
428                         UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
429                                        is_udplite);
430         }
431 
432         sock_recv_ts_and_drops(msg, sk, skb);
433 
434         /* Copy the address. */
435         if (msg->msg_name) {
436                 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
437                 sin6->sin6_family = AF_INET6;
438                 sin6->sin6_port = udp_hdr(skb)->source;
439                 sin6->sin6_flowinfo = 0;
440 
441                 if (is_udp4) {
442                         ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
443                                                &sin6->sin6_addr);
444                         sin6->sin6_scope_id = 0;
445                 } else {
446                         sin6->sin6_addr = ipv6_hdr(skb)->saddr;
447                         sin6->sin6_scope_id =
448                                 ipv6_iface_scope_id(&sin6->sin6_addr,
449                                                     inet6_iif(skb));
450                 }
451                 *addr_len = sizeof(*sin6);
452         }
453 
454         if (np->rxopt.all)
455                 ip6_datagram_recv_common_ctl(sk, msg, skb);
456 
457         if (is_udp4) {
458                 if (inet->cmsg_flags)
459                         ip_cmsg_recv_offset(msg, sk, skb,
460                                             sizeof(struct udphdr), off);
461         } else {
462                 if (np->rxopt.all)
463                         ip6_datagram_recv_specific_ctl(sk, msg, skb);
464         }
465 
466         err = copied;
467         if (flags & MSG_TRUNC)
468                 err = ulen;
469 
470         skb_consume_udp(sk, skb, peeking ? -err : err);
471         return err;
472 
473 csum_copy_err:
474         if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
475                                  udp_skb_destructor)) {
476                 if (is_udp4) {
477                         UDP_INC_STATS(sock_net(sk),
478                                       UDP_MIB_CSUMERRORS, is_udplite);
479                         UDP_INC_STATS(sock_net(sk),
480                                       UDP_MIB_INERRORS, is_udplite);
481                 } else {
482                         UDP6_INC_STATS(sock_net(sk),
483                                        UDP_MIB_CSUMERRORS, is_udplite);
484                         UDP6_INC_STATS(sock_net(sk),
485                                        UDP_MIB_INERRORS, is_udplite);
486                 }
487         }
488         kfree_skb(skb);
489 
490         /* starting over for a new packet, but check if we need to yield */
491         cond_resched();
492         msg->msg_flags &= ~MSG_TRUNC;
493         goto try_again;
494 }
495 
496 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
497                     u8 type, u8 code, int offset, __be32 info,
498                     struct udp_table *udptable)
499 {
500         struct ipv6_pinfo *np;
501         const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
502         const struct in6_addr *saddr = &hdr->saddr;
503         const struct in6_addr *daddr = &hdr->daddr;
504         struct udphdr *uh = (struct udphdr *)(skb->data+offset);
505         struct sock *sk;
506         int harderr;
507         int err;
508         struct net *net = dev_net(skb->dev);
509 
510         sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
511                                inet6_iif(skb), 0, udptable, skb);
512         if (!sk) {
513                 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
514                                   ICMP6_MIB_INERRORS);
515                 return;
516         }
517 
518         harderr = icmpv6_err_convert(type, code, &err);
519         np = inet6_sk(sk);
520 
521         if (type == ICMPV6_PKT_TOOBIG) {
522                 if (!ip6_sk_accept_pmtu(sk))
523                         goto out;
524                 ip6_sk_update_pmtu(skb, sk, info);
525                 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
526                         harderr = 1;
527         }
528         if (type == NDISC_REDIRECT) {
529                 ip6_sk_redirect(skb, sk);
530                 goto out;
531         }
532 
533         if (!np->recverr) {
534                 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
535                         goto out;
536         } else {
537                 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
538         }
539 
540         sk->sk_err = err;
541         sk->sk_error_report(sk);
542 out:
543         return;
544 }
545 
546 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
547 {
548         int rc;
549 
550         if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
551                 sock_rps_save_rxhash(sk, skb);
552                 sk_mark_napi_id(sk, skb);
553                 sk_incoming_cpu_update(sk);
554         } else {
555                 sk_mark_napi_id_once(sk, skb);
556         }
557 
558         rc = __udp_enqueue_schedule_skb(sk, skb);
559         if (rc < 0) {
560                 int is_udplite = IS_UDPLITE(sk);
561 
562                 /* Note that an ENOMEM error is charged twice */
563                 if (rc == -ENOMEM)
564                         UDP6_INC_STATS(sock_net(sk),
565                                          UDP_MIB_RCVBUFERRORS, is_udplite);
566                 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
567                 kfree_skb(skb);
568                 return -1;
569         }
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         static_key_enable(&udpv6_encap_needed);
585 }
586 EXPORT_SYMBOL(udpv6_encap_enable);
587 
588 static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
589 {
590         struct udp_sock *up = udp_sk(sk);
591         int is_udplite = IS_UDPLITE(sk);
592 
593         if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
594                 goto drop;
595 
596         if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
597                 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
598 
599                 /*
600                  * This is an encapsulation socket so pass the skb to
601                  * the socket's udp_encap_rcv() hook. Otherwise, just
602                  * fall through and pass this up the UDP socket.
603                  * up->encap_rcv() returns the following value:
604                  * =0 if skb was successfully passed to the encap
605                  *    handler or was discarded by it.
606                  * >0 if skb should be passed on to UDP.
607                  * <0 if skb should be resubmitted as proto -N
608                  */
609 
610                 /* if we're overly short, let UDP handle it */
611                 encap_rcv = READ_ONCE(up->encap_rcv);
612                 if (encap_rcv) {
613                         int ret;
614 
615                         /* Verify checksum before giving to encap */
616                         if (udp_lib_checksum_complete(skb))
617                                 goto csum_error;
618 
619                         ret = encap_rcv(sk, skb);
620                         if (ret <= 0) {
621                                 __UDP_INC_STATS(sock_net(sk),
622                                                 UDP_MIB_INDATAGRAMS,
623                                                 is_udplite);
624                                 return -ret;
625                         }
626                 }
627 
628                 /* FALLTHROUGH -- it's a UDP Packet */
629         }
630 
631         /*
632          * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
633          */
634         if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
635 
636                 if (up->pcrlen == 0) {          /* full coverage was set  */
637                         net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
638                                             UDP_SKB_CB(skb)->cscov, skb->len);
639                         goto drop;
640                 }
641                 if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
642                         net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
643                                             UDP_SKB_CB(skb)->cscov, up->pcrlen);
644                         goto drop;
645                 }
646         }
647 
648         prefetch(&sk->sk_rmem_alloc);
649         if (rcu_access_pointer(sk->sk_filter) &&
650             udp_lib_checksum_complete(skb))
651                 goto csum_error;
652 
653         if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
654                 goto drop;
655 
656         udp_csum_pull_header(skb);
657 
658         skb_dst_drop(skb);
659 
660         return __udpv6_queue_rcv_skb(sk, skb);
661 
662 csum_error:
663         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
664 drop:
665         __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
666         atomic_inc(&sk->sk_drops);
667         kfree_skb(skb);
668         return -1;
669 }
670 
671 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
672                                    __be16 loc_port, const struct in6_addr *loc_addr,
673                                    __be16 rmt_port, const struct in6_addr *rmt_addr,
674                                    int dif, unsigned short hnum)
675 {
676         struct inet_sock *inet = inet_sk(sk);
677 
678         if (!net_eq(sock_net(sk), net))
679                 return false;
680 
681         if (udp_sk(sk)->udp_port_hash != hnum ||
682             sk->sk_family != PF_INET6 ||
683             (inet->inet_dport && inet->inet_dport != rmt_port) ||
684             (!ipv6_addr_any(&sk->sk_v6_daddr) &&
685                     !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
686             (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
687             (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
688                     !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
689                 return false;
690         if (!inet6_mc_check(sk, loc_addr, rmt_addr))
691                 return false;
692         return true;
693 }
694 
695 static void udp6_csum_zero_error(struct sk_buff *skb)
696 {
697         /* RFC 2460 section 8.1 says that we SHOULD log
698          * this error. Well, it is reasonable.
699          */
700         net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
701                             &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
702                             &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
703 }
704 
705 /*
706  * Note: called only from the BH handler context,
707  * so we don't need to lock the hashes.
708  */
709 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
710                 const struct in6_addr *saddr, const struct in6_addr *daddr,
711                 struct udp_table *udptable, int proto)
712 {
713         struct sock *sk, *first = NULL;
714         const struct udphdr *uh = udp_hdr(skb);
715         unsigned short hnum = ntohs(uh->dest);
716         struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
717         unsigned int offset = offsetof(typeof(*sk), sk_node);
718         unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
719         int dif = inet6_iif(skb);
720         struct hlist_node *node;
721         struct sk_buff *nskb;
722 
723         if (use_hash2) {
724                 hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
725                             udptable->mask;
726                 hash2 = udp6_portaddr_hash(net, daddr, hnum) & udptable->mask;
727 start_lookup:
728                 hslot = &udptable->hash2[hash2];
729                 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
730         }
731 
732         sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
733                 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
734                                             uh->source, saddr, dif, hnum))
735                         continue;
736                 /* If zero checksum and no_check is not on for
737                  * the socket then skip it.
738                  */
739                 if (!uh->check && !udp_sk(sk)->no_check6_rx)
740                         continue;
741                 if (!first) {
742                         first = sk;
743                         continue;
744                 }
745                 nskb = skb_clone(skb, GFP_ATOMIC);
746                 if (unlikely(!nskb)) {
747                         atomic_inc(&sk->sk_drops);
748                         __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
749                                          IS_UDPLITE(sk));
750                         __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
751                                          IS_UDPLITE(sk));
752                         continue;
753                 }
754 
755                 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
756                         consume_skb(nskb);
757         }
758 
759         /* Also lookup *:port if we are using hash2 and haven't done so yet. */
760         if (use_hash2 && hash2 != hash2_any) {
761                 hash2 = hash2_any;
762                 goto start_lookup;
763         }
764 
765         if (first) {
766                 if (udpv6_queue_rcv_skb(first, skb) > 0)
767                         consume_skb(skb);
768         } else {
769                 kfree_skb(skb);
770                 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
771                                  proto == IPPROTO_UDPLITE);
772         }
773         return 0;
774 }
775 
776 static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
777 {
778         if (udp_sk_rx_dst_set(sk, dst)) {
779                 const struct rt6_info *rt = (const struct rt6_info *)dst;
780 
781                 inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
782         }
783 }
784 
785 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
786                    int proto)
787 {
788         const struct in6_addr *saddr, *daddr;
789         struct net *net = dev_net(skb->dev);
790         struct udphdr *uh;
791         struct sock *sk;
792         u32 ulen = 0;
793 
794         if (!pskb_may_pull(skb, sizeof(struct udphdr)))
795                 goto discard;
796 
797         saddr = &ipv6_hdr(skb)->saddr;
798         daddr = &ipv6_hdr(skb)->daddr;
799         uh = udp_hdr(skb);
800 
801         ulen = ntohs(uh->len);
802         if (ulen > skb->len)
803                 goto short_packet;
804 
805         if (proto == IPPROTO_UDP) {
806                 /* UDP validates ulen. */
807 
808                 /* Check for jumbo payload */
809                 if (ulen == 0)
810                         ulen = skb->len;
811 
812                 if (ulen < sizeof(*uh))
813                         goto short_packet;
814 
815                 if (ulen < skb->len) {
816                         if (pskb_trim_rcsum(skb, ulen))
817                                 goto short_packet;
818                         saddr = &ipv6_hdr(skb)->saddr;
819                         daddr = &ipv6_hdr(skb)->daddr;
820                         uh = udp_hdr(skb);
821                 }
822         }
823 
824         if (udp6_csum_init(skb, uh, proto))
825                 goto csum_error;
826 
827         /* Check if the socket is already available, e.g. due to early demux */
828         sk = skb_steal_sock(skb);
829         if (sk) {
830                 struct dst_entry *dst = skb_dst(skb);
831                 int ret;
832 
833                 if (unlikely(sk->sk_rx_dst != dst))
834                         udp6_sk_rx_dst_set(sk, dst);
835 
836                 ret = udpv6_queue_rcv_skb(sk, skb);
837                 sock_put(sk);
838 
839                 /* a return value > 0 means to resubmit the input */
840                 if (ret > 0)
841                         return ret;
842                 return 0;
843         }
844 
845         /*
846          *      Multicast receive code
847          */
848         if (ipv6_addr_is_multicast(daddr))
849                 return __udp6_lib_mcast_deliver(net, skb,
850                                 saddr, daddr, udptable, proto);
851 
852         /* Unicast */
853         sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
854         if (sk) {
855                 int ret;
856 
857                 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
858                         udp6_csum_zero_error(skb);
859                         goto csum_error;
860                 }
861 
862                 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
863                         skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
864                                                  ip6_compute_pseudo);
865 
866                 ret = udpv6_queue_rcv_skb(sk, skb);
867 
868                 /* a return value > 0 means to resubmit the input */
869                 if (ret > 0)
870                         return ret;
871 
872                 return 0;
873         }
874 
875         if (!uh->check) {
876                 udp6_csum_zero_error(skb);
877                 goto csum_error;
878         }
879 
880         if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
881                 goto discard;
882 
883         if (udp_lib_checksum_complete(skb))
884                 goto csum_error;
885 
886         __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
887         icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
888 
889         kfree_skb(skb);
890         return 0;
891 
892 short_packet:
893         net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
894                             proto == IPPROTO_UDPLITE ? "-Lite" : "",
895                             saddr, ntohs(uh->source),
896                             ulen, skb->len,
897                             daddr, ntohs(uh->dest));
898         goto discard;
899 csum_error:
900         __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
901 discard:
902         __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
903         kfree_skb(skb);
904         return 0;
905 }
906 
907 
908 static struct sock *__udp6_lib_demux_lookup(struct net *net,
909                         __be16 loc_port, const struct in6_addr *loc_addr,
910                         __be16 rmt_port, const struct in6_addr *rmt_addr,
911                         int dif, int sdif)
912 {
913         unsigned short hnum = ntohs(loc_port);
914         unsigned int hash2 = udp6_portaddr_hash(net, loc_addr, hnum);
915         unsigned int slot2 = hash2 & udp_table.mask;
916         struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
917         const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
918         struct sock *sk;
919 
920         udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
921                 if (sk->sk_state == TCP_ESTABLISHED &&
922                     INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
923                         return sk;
924                 /* Only check first socket in chain */
925                 break;
926         }
927         return NULL;
928 }
929 
930 static void udp_v6_early_demux(struct sk_buff *skb)
931 {
932         struct net *net = dev_net(skb->dev);
933         const struct udphdr *uh;
934         struct sock *sk;
935         struct dst_entry *dst;
936         int dif = skb->dev->ifindex;
937         int sdif = inet6_sdif(skb);
938 
939         if (!pskb_may_pull(skb, skb_transport_offset(skb) +
940             sizeof(struct udphdr)))
941                 return;
942 
943         uh = udp_hdr(skb);
944 
945         if (skb->pkt_type == PACKET_HOST)
946                 sk = __udp6_lib_demux_lookup(net, uh->dest,
947                                              &ipv6_hdr(skb)->daddr,
948                                              uh->source, &ipv6_hdr(skb)->saddr,
949                                              dif, sdif);
950         else
951                 return;
952 
953         if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
954                 return;
955 
956         skb->sk = sk;
957         skb->destructor = sock_efree;
958         dst = READ_ONCE(sk->sk_rx_dst);
959 
960         if (dst)
961                 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
962         if (dst) {
963                 /* set noref for now.
964                  * any place which wants to hold dst has to call
965                  * dst_hold_safe()
966                  */
967                 skb_dst_set_noref(skb, dst);
968         }
969 }
970 
971 static __inline__ int udpv6_rcv(struct sk_buff *skb)
972 {
973         return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
974 }
975 
976 /*
977  * Throw away all pending data and cancel the corking. Socket is locked.
978  */
979 static void udp_v6_flush_pending_frames(struct sock *sk)
980 {
981         struct udp_sock *up = udp_sk(sk);
982 
983         if (up->pending == AF_INET)
984                 udp_flush_pending_frames(sk);
985         else if (up->pending) {
986                 up->len = 0;
987                 up->pending = 0;
988                 ip6_flush_pending_frames(sk);
989         }
990 }
991 
992 /**
993  *      udp6_hwcsum_outgoing  -  handle outgoing HW checksumming
994  *      @sk:    socket we are sending on
995  *      @skb:   sk_buff containing the filled-in UDP header
996  *              (checksum field must be zeroed out)
997  */
998 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
999                                  const struct in6_addr *saddr,
1000                                  const struct in6_addr *daddr, int len)
1001 {
1002         unsigned int offset;
1003         struct udphdr *uh = udp_hdr(skb);
1004         struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1005         __wsum csum = 0;
1006 
1007         if (!frags) {
1008                 /* Only one fragment on the socket.  */
1009                 skb->csum_start = skb_transport_header(skb) - skb->head;
1010                 skb->csum_offset = offsetof(struct udphdr, check);
1011                 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1012         } else {
1013                 /*
1014                  * HW-checksum won't work as there are two or more
1015                  * fragments on the socket so that all csums of sk_buffs
1016                  * should be together
1017                  */
1018                 offset = skb_transport_offset(skb);
1019                 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
1020                 csum = skb->csum;
1021 
1022                 skb->ip_summed = CHECKSUM_NONE;
1023 
1024                 do {
1025                         csum = csum_add(csum, frags->csum);
1026                 } while ((frags = frags->next));
1027 
1028                 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1029                                             csum);
1030                 if (uh->check == 0)
1031                         uh->check = CSUM_MANGLED_0;
1032         }
1033 }
1034 
1035 /*
1036  *      Sending
1037  */
1038 
1039 static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
1040 {
1041         struct sock *sk = skb->sk;
1042         struct udphdr *uh;
1043         int err = 0;
1044         int is_udplite = IS_UDPLITE(sk);
1045         __wsum csum = 0;
1046         int offset = skb_transport_offset(skb);
1047         int len = skb->len - offset;
1048 
1049         /*
1050          * Create a UDP header
1051          */
1052         uh = udp_hdr(skb);
1053         uh->source = fl6->fl6_sport;
1054         uh->dest = fl6->fl6_dport;
1055         uh->len = htons(len);
1056         uh->check = 0;
1057 
1058         if (is_udplite)
1059                 csum = udplite_csum(skb);
1060         else if (udp_sk(sk)->no_check6_tx) {   /* UDP csum disabled */
1061                 skb->ip_summed = CHECKSUM_NONE;
1062                 goto send;
1063         } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1064                 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1065                 goto send;
1066         } else
1067                 csum = udp_csum(skb);
1068 
1069         /* add protocol-dependent pseudo-header */
1070         uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1071                                     len, fl6->flowi6_proto, csum);
1072         if (uh->check == 0)
1073                 uh->check = CSUM_MANGLED_0;
1074 
1075 send:
1076         err = ip6_send_skb(skb);
1077         if (err) {
1078                 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
1079                         UDP6_INC_STATS(sock_net(sk),
1080                                        UDP_MIB_SNDBUFERRORS, is_udplite);
1081                         err = 0;
1082                 }
1083         } else {
1084                 UDP6_INC_STATS(sock_net(sk),
1085                                UDP_MIB_OUTDATAGRAMS, is_udplite);
1086         }
1087         return err;
1088 }
1089 
1090 static int udp_v6_push_pending_frames(struct sock *sk)
1091 {
1092         struct sk_buff *skb;
1093         struct udp_sock  *up = udp_sk(sk);
1094         struct flowi6 fl6;
1095         int err = 0;
1096 
1097         if (up->pending == AF_INET)
1098                 return udp_push_pending_frames(sk);
1099 
1100         /* ip6_finish_skb will release the cork, so make a copy of
1101          * fl6 here.
1102          */
1103         fl6 = inet_sk(sk)->cork.fl.u.ip6;
1104 
1105         skb = ip6_finish_skb(sk);
1106         if (!skb)
1107                 goto out;
1108 
1109         err = udp_v6_send_skb(skb, &fl6);
1110 
1111 out:
1112         up->len = 0;
1113         up->pending = 0;
1114         return err;
1115 }
1116 
1117 int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1118 {
1119         struct ipv6_txoptions opt_space;
1120         struct udp_sock *up = udp_sk(sk);
1121         struct inet_sock *inet = inet_sk(sk);
1122         struct ipv6_pinfo *np = inet6_sk(sk);
1123         DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1124         struct in6_addr *daddr, *final_p, final;
1125         struct ipv6_txoptions *opt = NULL;
1126         struct ipv6_txoptions *opt_to_free = NULL;
1127         struct ip6_flowlabel *flowlabel = NULL;
1128         struct flowi6 fl6;
1129         struct dst_entry *dst;
1130         struct ipcm6_cookie ipc6;
1131         int addr_len = msg->msg_namelen;
1132         int ulen = len;
1133         int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1134         int err;
1135         int connected = 0;
1136         int is_udplite = IS_UDPLITE(sk);
1137         int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1138         struct sockcm_cookie sockc;
1139 
1140         ipc6.hlimit = -1;
1141         ipc6.tclass = -1;
1142         ipc6.dontfrag = -1;
1143         sockc.tsflags = sk->sk_tsflags;
1144 
1145         /* destination address check */
1146         if (sin6) {
1147                 if (addr_len < offsetof(struct sockaddr, sa_data))
1148                         return -EINVAL;
1149 
1150                 switch (sin6->sin6_family) {
1151                 case AF_INET6:
1152                         if (addr_len < SIN6_LEN_RFC2133)
1153                                 return -EINVAL;
1154                         daddr = &sin6->sin6_addr;
1155                         if (ipv6_addr_any(daddr) &&
1156                             ipv6_addr_v4mapped(&np->saddr))
1157                                 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1158                                                        daddr);
1159                         break;
1160                 case AF_INET:
1161                         goto do_udp_sendmsg;
1162                 case AF_UNSPEC:
1163                         msg->msg_name = sin6 = NULL;
1164                         msg->msg_namelen = addr_len = 0;
1165                         daddr = NULL;
1166                         break;
1167                 default:
1168                         return -EINVAL;
1169                 }
1170         } else if (!up->pending) {
1171                 if (sk->sk_state != TCP_ESTABLISHED)
1172                         return -EDESTADDRREQ;
1173                 daddr = &sk->sk_v6_daddr;
1174         } else
1175                 daddr = NULL;
1176 
1177         if (daddr) {
1178                 if (ipv6_addr_v4mapped(daddr)) {
1179                         struct sockaddr_in sin;
1180                         sin.sin_family = AF_INET;
1181                         sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1182                         sin.sin_addr.s_addr = daddr->s6_addr32[3];
1183                         msg->msg_name = &sin;
1184                         msg->msg_namelen = sizeof(sin);
1185 do_udp_sendmsg:
1186                         if (__ipv6_only_sock(sk))
1187                                 return -ENETUNREACH;
1188                         return udp_sendmsg(sk, msg, len);
1189                 }
1190         }
1191 
1192         if (up->pending == AF_INET)
1193                 return udp_sendmsg(sk, msg, len);
1194 
1195         /* Rough check on arithmetic overflow,
1196            better check is made in ip6_append_data().
1197            */
1198         if (len > INT_MAX - sizeof(struct udphdr))
1199                 return -EMSGSIZE;
1200 
1201         getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
1202         if (up->pending) {
1203                 /*
1204                  * There are pending frames.
1205                  * The socket lock must be held while it's corked.
1206                  */
1207                 lock_sock(sk);
1208                 if (likely(up->pending)) {
1209                         if (unlikely(up->pending != AF_INET6)) {
1210                                 release_sock(sk);
1211                                 return -EAFNOSUPPORT;
1212                         }
1213                         dst = NULL;
1214                         goto do_append_data;
1215                 }
1216                 release_sock(sk);
1217         }
1218         ulen += sizeof(struct udphdr);
1219 
1220         memset(&fl6, 0, sizeof(fl6));
1221 
1222         if (sin6) {
1223                 if (sin6->sin6_port == 0)
1224                         return -EINVAL;
1225 
1226                 fl6.fl6_dport = sin6->sin6_port;
1227                 daddr = &sin6->sin6_addr;
1228 
1229                 if (np->sndflow) {
1230                         fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1231                         if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1232                                 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1233                                 if (!flowlabel)
1234                                         return -EINVAL;
1235                         }
1236                 }
1237 
1238                 /*
1239                  * Otherwise it will be difficult to maintain
1240                  * sk->sk_dst_cache.
1241                  */
1242                 if (sk->sk_state == TCP_ESTABLISHED &&
1243                     ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1244                         daddr = &sk->sk_v6_daddr;
1245 
1246                 if (addr_len >= sizeof(struct sockaddr_in6) &&
1247                     sin6->sin6_scope_id &&
1248                     __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
1249                         fl6.flowi6_oif = sin6->sin6_scope_id;
1250         } else {
1251                 if (sk->sk_state != TCP_ESTABLISHED)
1252                         return -EDESTADDRREQ;
1253 
1254                 fl6.fl6_dport = inet->inet_dport;
1255                 daddr = &sk->sk_v6_daddr;
1256                 fl6.flowlabel = np->flow_label;
1257                 connected = 1;
1258         }
1259 
1260         if (!fl6.flowi6_oif)
1261                 fl6.flowi6_oif = sk->sk_bound_dev_if;
1262 
1263         if (!fl6.flowi6_oif)
1264                 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1265 
1266         fl6.flowi6_mark = sk->sk_mark;
1267         fl6.flowi6_uid = sk->sk_uid;
1268 
1269         if (msg->msg_controllen) {
1270                 opt = &opt_space;
1271                 memset(opt, 0, sizeof(struct ipv6_txoptions));
1272                 opt->tot_len = sizeof(*opt);
1273                 ipc6.opt = opt;
1274 
1275                 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
1276                 if (err < 0) {
1277                         fl6_sock_release(flowlabel);
1278                         return err;
1279                 }
1280                 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1281                         flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1282                         if (!flowlabel)
1283                                 return -EINVAL;
1284                 }
1285                 if (!(opt->opt_nflen|opt->opt_flen))
1286                         opt = NULL;
1287                 connected = 0;
1288         }
1289         if (!opt) {
1290                 opt = txopt_get(np);
1291                 opt_to_free = opt;
1292         }
1293         if (flowlabel)
1294                 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1295         opt = ipv6_fixup_options(&opt_space, opt);
1296         ipc6.opt = opt;
1297 
1298         fl6.flowi6_proto = sk->sk_protocol;
1299         if (!ipv6_addr_any(daddr))
1300                 fl6.daddr = *daddr;
1301         else
1302                 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1303         if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1304                 fl6.saddr = np->saddr;
1305         fl6.fl6_sport = inet->inet_sport;
1306 
1307         final_p = fl6_update_dst(&fl6, opt, &final);
1308         if (final_p)
1309                 connected = 0;
1310 
1311         if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1312                 fl6.flowi6_oif = np->mcast_oif;
1313                 connected = 0;
1314         } else if (!fl6.flowi6_oif)
1315                 fl6.flowi6_oif = np->ucast_oif;
1316 
1317         security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1318 
1319         if (ipc6.tclass < 0)
1320                 ipc6.tclass = np->tclass;
1321 
1322         fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1323 
1324         dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
1325         if (IS_ERR(dst)) {
1326                 err = PTR_ERR(dst);
1327                 dst = NULL;
1328                 goto out;
1329         }
1330 
1331         if (ipc6.hlimit < 0)
1332                 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1333 
1334         if (msg->msg_flags&MSG_CONFIRM)
1335                 goto do_confirm;
1336 back_from_confirm:
1337 
1338         /* Lockless fast path for the non-corking case */
1339         if (!corkreq) {
1340                 struct sk_buff *skb;
1341 
1342                 skb = ip6_make_skb(sk, getfrag, msg, ulen,
1343                                    sizeof(struct udphdr), &ipc6,
1344                                    &fl6, (struct rt6_info *)dst,
1345                                    msg->msg_flags, &sockc);
1346                 err = PTR_ERR(skb);
1347                 if (!IS_ERR_OR_NULL(skb))
1348                         err = udp_v6_send_skb(skb, &fl6);
1349                 goto release_dst;
1350         }
1351 
1352         lock_sock(sk);
1353         if (unlikely(up->pending)) {
1354                 /* The socket is already corked while preparing it. */
1355                 /* ... which is an evident application bug. --ANK */
1356                 release_sock(sk);
1357 
1358                 net_dbg_ratelimited("udp cork app bug 2\n");
1359                 err = -EINVAL;
1360                 goto out;
1361         }
1362 
1363         up->pending = AF_INET6;
1364 
1365 do_append_data:
1366         if (ipc6.dontfrag < 0)
1367                 ipc6.dontfrag = np->dontfrag;
1368         up->len += ulen;
1369         err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1370                               &ipc6, &fl6, (struct rt6_info *)dst,
1371                               corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, &sockc);
1372         if (err)
1373                 udp_v6_flush_pending_frames(sk);
1374         else if (!corkreq)
1375                 err = udp_v6_push_pending_frames(sk);
1376         else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1377                 up->pending = 0;
1378 
1379         if (err > 0)
1380                 err = np->recverr ? net_xmit_errno(err) : 0;
1381         release_sock(sk);
1382 
1383 release_dst:
1384         if (dst) {
1385                 if (connected) {
1386                         ip6_dst_store(sk, dst,
1387                                       ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1388                                       &sk->sk_v6_daddr : NULL,
1389 #ifdef CONFIG_IPV6_SUBTREES
1390                                       ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1391                                       &np->saddr :
1392 #endif
1393                                       NULL);
1394                 } else {
1395                         dst_release(dst);
1396                 }
1397                 dst = NULL;
1398         }
1399 
1400 out:
1401         dst_release(dst);
1402         fl6_sock_release(flowlabel);
1403         txopt_put(opt_to_free);
1404         if (!err)
1405                 return len;
1406         /*
1407          * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
1408          * ENOBUFS might not be good (it's not tunable per se), but otherwise
1409          * we don't have a good statistic (IpOutDiscards but it can be too many
1410          * things).  We could add another new stat but at least for now that
1411          * seems like overkill.
1412          */
1413         if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1414                 UDP6_INC_STATS(sock_net(sk),
1415                                UDP_MIB_SNDBUFERRORS, is_udplite);
1416         }
1417         return err;
1418 
1419 do_confirm:
1420         if (msg->msg_flags & MSG_PROBE)
1421                 dst_confirm_neigh(dst, &fl6.daddr);
1422         if (!(msg->msg_flags&MSG_PROBE) || len)
1423                 goto back_from_confirm;
1424         err = 0;
1425         goto out;
1426 }
1427 
1428 void udpv6_destroy_sock(struct sock *sk)
1429 {
1430         struct udp_sock *up = udp_sk(sk);
1431         lock_sock(sk);
1432         udp_v6_flush_pending_frames(sk);
1433         release_sock(sk);
1434 
1435         if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
1436                 void (*encap_destroy)(struct sock *sk);
1437                 encap_destroy = READ_ONCE(up->encap_destroy);
1438                 if (encap_destroy)
1439                         encap_destroy(sk);
1440         }
1441 
1442         inet6_destroy_sock(sk);
1443 }
1444 
1445 /*
1446  *      Socket option code for UDP
1447  */
1448 int udpv6_setsockopt(struct sock *sk, int level, int optname,
1449                      char __user *optval, unsigned int optlen)
1450 {
1451         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1452                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1453                                           udp_v6_push_pending_frames);
1454         return ipv6_setsockopt(sk, level, optname, optval, optlen);
1455 }
1456 
1457 #ifdef CONFIG_COMPAT
1458 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1459                             char __user *optval, unsigned int optlen)
1460 {
1461         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1462                 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1463                                           udp_v6_push_pending_frames);
1464         return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1465 }
1466 #endif
1467 
1468 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1469                      char __user *optval, int __user *optlen)
1470 {
1471         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1472                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1473         return ipv6_getsockopt(sk, level, optname, optval, optlen);
1474 }
1475 
1476 #ifdef CONFIG_COMPAT
1477 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1478                             char __user *optval, int __user *optlen)
1479 {
1480         if (level == SOL_UDP  ||  level == SOL_UDPLITE)
1481                 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1482         return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1483 }
1484 #endif
1485 
1486 /* thinking of making this const? Don't.
1487  * early_demux can change based on sysctl.
1488  */
1489 static struct inet6_protocol udpv6_protocol = {
1490         .early_demux    =       udp_v6_early_demux,
1491         .early_demux_handler =  udp_v6_early_demux,
1492         .handler        =       udpv6_rcv,
1493         .err_handler    =       udpv6_err,
1494         .flags          =       INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1495 };
1496 
1497 /* ------------------------------------------------------------------------ */
1498 #ifdef CONFIG_PROC_FS
1499 int udp6_seq_show(struct seq_file *seq, void *v)
1500 {
1501         if (v == SEQ_START_TOKEN) {
1502                 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1503         } else {
1504                 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1505                 struct inet_sock *inet = inet_sk(v);
1506                 __u16 srcp = ntohs(inet->inet_sport);
1507                 __u16 destp = ntohs(inet->inet_dport);
1508                 ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
1509         }
1510         return 0;
1511 }
1512 
1513 static const struct file_operations udp6_afinfo_seq_fops = {
1514         .owner    = THIS_MODULE,
1515         .open     = udp_seq_open,
1516         .read     = seq_read,
1517         .llseek   = seq_lseek,
1518         .release  = seq_release_net
1519 };
1520 
1521 static struct udp_seq_afinfo udp6_seq_afinfo = {
1522         .name           = "udp6",
1523         .family         = AF_INET6,
1524         .udp_table      = &udp_table,
1525         .seq_fops       = &udp6_afinfo_seq_fops,
1526         .seq_ops        = {
1527                 .show           = udp6_seq_show,
1528         },
1529 };
1530 
1531 int __net_init udp6_proc_init(struct net *net)
1532 {
1533         return udp_proc_register(net, &udp6_seq_afinfo);
1534 }
1535 
1536 void udp6_proc_exit(struct net *net)
1537 {
1538         udp_proc_unregister(net, &udp6_seq_afinfo);
1539 }
1540 #endif /* CONFIG_PROC_FS */
1541 
1542 /* ------------------------------------------------------------------------ */
1543 
1544 struct proto udpv6_prot = {
1545         .name              = "UDPv6",
1546         .owner             = THIS_MODULE,
1547         .close             = udp_lib_close,
1548         .connect           = ip6_datagram_connect,
1549         .disconnect        = udp_disconnect,
1550         .ioctl             = udp_ioctl,
1551         .init              = udp_init_sock,
1552         .destroy           = udpv6_destroy_sock,
1553         .setsockopt        = udpv6_setsockopt,
1554         .getsockopt        = udpv6_getsockopt,
1555         .sendmsg           = udpv6_sendmsg,
1556         .recvmsg           = udpv6_recvmsg,
1557         .release_cb        = ip6_datagram_release_cb,
1558         .hash              = udp_lib_hash,
1559         .unhash            = udp_lib_unhash,
1560         .rehash            = udp_v6_rehash,
1561         .get_port          = udp_v6_get_port,
1562         .memory_allocated  = &udp_memory_allocated,
1563         .sysctl_mem        = sysctl_udp_mem,
1564         .sysctl_wmem       = &sysctl_udp_wmem_min,
1565         .sysctl_rmem       = &sysctl_udp_rmem_min,
1566         .obj_size          = sizeof(struct udp6_sock),
1567         .h.udp_table       = &udp_table,
1568 #ifdef CONFIG_COMPAT
1569         .compat_setsockopt = compat_udpv6_setsockopt,
1570         .compat_getsockopt = compat_udpv6_getsockopt,
1571 #endif
1572         .diag_destroy      = udp_abort,
1573 };
1574 
1575 static struct inet_protosw udpv6_protosw = {
1576         .type =      SOCK_DGRAM,
1577         .protocol =  IPPROTO_UDP,
1578         .prot =      &udpv6_prot,
1579         .ops =       &inet6_dgram_ops,
1580         .flags =     INET_PROTOSW_PERMANENT,
1581 };
1582 
1583 int __init udpv6_init(void)
1584 {
1585         int ret;
1586 
1587         ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1588         if (ret)
1589                 goto out;
1590 
1591         ret = inet6_register_protosw(&udpv6_protosw);
1592         if (ret)
1593                 goto out_udpv6_protocol;
1594 out:
1595         return ret;
1596 
1597 out_udpv6_protocol:
1598         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1599         goto out;
1600 }
1601 
1602 void udpv6_exit(void)
1603 {
1604         inet6_unregister_protosw(&udpv6_protosw);
1605         inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1606 }
1607 

~ [ 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