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