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