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

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

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ 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.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

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