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

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

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

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