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