hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/ipv6/udp.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * UDP over IPv6
34 * Linux INET6 implementation
....@@ -14,11 +15,6 @@
1415 * a single port at the same time.
1516 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
1617 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17
- *
18
- * This program is free software; you can redistribute it and/or
19
- * modify it under the terms of the GNU General Public License
20
- * as published by the Free Software Foundation; either version
21
- * 2 of the License, or (at your option) any later version.
2218 */
2319
2420 #include <linux/errno.h>
....@@ -36,6 +32,7 @@
3632 #include <linux/skbuff.h>
3733 #include <linux/slab.h>
3834 #include <linux/uaccess.h>
35
+#include <linux/indirect_call_wrapper.h>
3936
4037 #include <net/addrconf.h>
4138 #include <net/ndisc.h>
....@@ -45,6 +42,7 @@
4542 #include <net/raw.h>
4643 #include <net/tcp_states.h>
4744 #include <net/ip6_checksum.h>
45
+#include <net/ip6_tunnel.h>
4846 #include <net/xfrm.h>
4947 #include <net/inet_hashtables.h>
5048 #include <net/inet6_hashtables.h>
....@@ -56,14 +54,17 @@
5654 #include <trace/events/skb.h>
5755 #include "udp_impl.h"
5856
59
-static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
57
+static void udpv6_destruct_sock(struct sock *sk)
6058 {
61
-#if defined(CONFIG_NET_L3_MASTER_DEV)
62
- if (!net->ipv4.sysctl_udp_l3mdev_accept &&
63
- skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
64
- return true;
65
-#endif
66
- return false;
59
+ udp_destruct_common(sk);
60
+ inet6_sock_destruct(sk);
61
+}
62
+
63
+int udpv6_init_sock(struct sock *sk)
64
+{
65
+ skb_queue_head_init(&udp_sk(sk)->reader_queue);
66
+ sk->sk_destruct = udpv6_destruct_sock;
67
+ return 0;
6768 }
6869
6970 static u32 udp6_ehashfn(const struct net *net,
....@@ -86,7 +87,7 @@
8687 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
8788
8889 return __inet6_ehashfn(lhash, lport, fhash, fport,
89
- udp_ipv6_hash_secret + net_hash_mix(net));
90
+ udp6_ehash_secret + net_hash_mix(net));
9091 }
9192
9293 int udp_v6_get_port(struct sock *sk, unsigned short snum)
....@@ -101,7 +102,7 @@
101102 return udp_lib_get_port(sk, snum, hash2_nulladdr);
102103 }
103104
104
-static void udp_v6_rehash(struct sock *sk)
105
+void udp_v6_rehash(struct sock *sk)
105106 {
106107 u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
107108 &sk->sk_v6_rcv_saddr,
....@@ -113,14 +114,18 @@
113114 static int compute_score(struct sock *sk, struct net *net,
114115 const struct in6_addr *saddr, __be16 sport,
115116 const struct in6_addr *daddr, unsigned short hnum,
116
- int dif, int sdif, bool exact_dif)
117
+ int dif, int sdif)
117118 {
118119 int score;
119120 struct inet_sock *inet;
121
+ bool dev_match;
120122
121123 if (!net_eq(sock_net(sk), net) ||
122124 udp_sk(sk)->udp_port_hash != hnum ||
123125 sk->sk_family != PF_INET6)
126
+ return -1;
127
+
128
+ if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
124129 return -1;
125130
126131 score = 0;
....@@ -132,27 +137,17 @@
132137 score++;
133138 }
134139
135
- if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
136
- if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
137
- return -1;
138
- score++;
139
- }
140
-
141140 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
142141 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
143142 return -1;
144143 score++;
145144 }
146145
147
- if (sk->sk_bound_dev_if || exact_dif) {
148
- bool dev_match = (sk->sk_bound_dev_if == dif ||
149
- sk->sk_bound_dev_if == sdif);
150
-
151
- if (!dev_match)
152
- return -1;
153
- if (sk->sk_bound_dev_if)
154
- score++;
155
- }
146
+ dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
147
+ if (!dev_match)
148
+ return -1;
149
+ if (sk->sk_bound_dev_if)
150
+ score++;
156151
157152 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
158153 score++;
....@@ -160,41 +155,85 @@
160155 return score;
161156 }
162157
158
+static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
159
+ struct sk_buff *skb,
160
+ const struct in6_addr *saddr,
161
+ __be16 sport,
162
+ const struct in6_addr *daddr,
163
+ unsigned int hnum)
164
+{
165
+ struct sock *reuse_sk = NULL;
166
+ u32 hash;
167
+
168
+ if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
169
+ hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
170
+ reuse_sk = reuseport_select_sock(sk, hash, skb,
171
+ sizeof(struct udphdr));
172
+ }
173
+ return reuse_sk;
174
+}
175
+
163176 /* called with rcu_read_lock() */
164177 static struct sock *udp6_lib_lookup2(struct net *net,
165178 const struct in6_addr *saddr, __be16 sport,
166179 const struct in6_addr *daddr, unsigned int hnum,
167
- int dif, int sdif, bool exact_dif,
168
- struct udp_hslot *hslot2, struct sk_buff *skb)
180
+ int dif, int sdif, struct udp_hslot *hslot2,
181
+ struct sk_buff *skb)
169182 {
170
- struct sock *sk, *result, *reuseport_result;
183
+ struct sock *sk, *result;
171184 int score, badness;
172
- u32 hash = 0;
173185
174186 result = NULL;
175187 badness = -1;
176188 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
177189 score = compute_score(sk, net, saddr, sport,
178
- daddr, hnum, dif, sdif, exact_dif);
190
+ daddr, hnum, dif, sdif);
179191 if (score > badness) {
180
- reuseport_result = NULL;
181
-
182
- if (sk->sk_reuseport &&
183
- sk->sk_state != TCP_ESTABLISHED) {
184
- hash = udp6_ehashfn(net, daddr, hnum,
185
- saddr, sport);
186
-
187
- reuseport_result = reuseport_select_sock(sk, hash, skb,
188
- sizeof(struct udphdr));
189
- if (reuseport_result && !reuseport_has_conns(sk, false))
190
- return reuseport_result;
192
+ badness = score;
193
+ result = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
194
+ if (!result) {
195
+ result = sk;
196
+ continue;
191197 }
192198
193
- result = reuseport_result ? : sk;
194
- badness = score;
199
+ /* Fall back to scoring if group has connections */
200
+ if (!reuseport_has_conns(sk))
201
+ return result;
202
+
203
+ /* Reuseport logic returned an error, keep original score. */
204
+ if (IS_ERR(result))
205
+ continue;
206
+
207
+ badness = compute_score(sk, net, saddr, sport,
208
+ daddr, hnum, dif, sdif);
195209 }
196210 }
197211 return result;
212
+}
213
+
214
+static inline struct sock *udp6_lookup_run_bpf(struct net *net,
215
+ struct udp_table *udptable,
216
+ struct sk_buff *skb,
217
+ const struct in6_addr *saddr,
218
+ __be16 sport,
219
+ const struct in6_addr *daddr,
220
+ u16 hnum)
221
+{
222
+ struct sock *sk, *reuse_sk;
223
+ bool no_reuseport;
224
+
225
+ if (udptable != &udp_table)
226
+ return NULL; /* only UDP is supported */
227
+
228
+ no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
229
+ saddr, sport, daddr, hnum, &sk);
230
+ if (no_reuseport || IS_ERR_OR_NULL(sk))
231
+ return sk;
232
+
233
+ reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
234
+ if (reuse_sk)
235
+ sk = reuse_sk;
236
+ return sk;
198237 }
199238
200239 /* rcu_read_lock() must be held */
....@@ -204,66 +243,47 @@
204243 int dif, int sdif, struct udp_table *udptable,
205244 struct sk_buff *skb)
206245 {
207
- struct sock *sk, *result;
208246 unsigned short hnum = ntohs(dport);
209
- unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
210
- struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
211
- bool exact_dif = udp6_lib_exact_dif_match(net, skb);
212
- int score, badness;
213
- u32 hash = 0;
247
+ unsigned int hash2, slot2;
248
+ struct udp_hslot *hslot2;
249
+ struct sock *result, *sk;
214250
215
- if (hslot->count > 10) {
216
- hash2 = ipv6_portaddr_hash(net, daddr, hnum);
217
- slot2 = hash2 & udptable->mask;
218
- hslot2 = &udptable->hash2[slot2];
219
- if (hslot->count < hslot2->count)
220
- goto begin;
251
+ hash2 = ipv6_portaddr_hash(net, daddr, hnum);
252
+ slot2 = hash2 & udptable->mask;
253
+ hslot2 = &udptable->hash2[slot2];
221254
222
- result = udp6_lib_lookup2(net, saddr, sport,
223
- daddr, hnum, dif, sdif, exact_dif,
224
- hslot2, skb);
225
- if (!result) {
226
- unsigned int old_slot2 = slot2;
227
- hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
228
- slot2 = hash2 & udptable->mask;
229
- /* avoid searching the same slot again. */
230
- if (unlikely(slot2 == old_slot2))
231
- return result;
255
+ /* Lookup connected or non-wildcard sockets */
256
+ result = udp6_lib_lookup2(net, saddr, sport,
257
+ daddr, hnum, dif, sdif,
258
+ hslot2, skb);
259
+ if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
260
+ goto done;
232261
233
- hslot2 = &udptable->hash2[slot2];
234
- if (hslot->count < hslot2->count)
235
- goto begin;
236
-
237
- result = udp6_lib_lookup2(net, saddr, sport,
238
- daddr, hnum, dif, sdif,
239
- exact_dif, hslot2,
240
- skb);
241
- }
242
- if (unlikely(IS_ERR(result)))
243
- return NULL;
244
- return result;
245
- }
246
-begin:
247
- result = NULL;
248
- badness = -1;
249
- sk_for_each_rcu(sk, &hslot->head) {
250
- score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
251
- sdif, exact_dif);
252
- if (score > badness) {
253
- if (sk->sk_reuseport) {
254
- hash = udp6_ehashfn(net, daddr, hnum,
255
- saddr, sport);
256
- result = reuseport_select_sock(sk, hash, skb,
257
- sizeof(struct udphdr));
258
- if (unlikely(IS_ERR(result)))
259
- return NULL;
260
- if (result)
261
- return result;
262
- }
262
+ /* Lookup redirect from BPF */
263
+ if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
264
+ sk = udp6_lookup_run_bpf(net, udptable, skb,
265
+ saddr, sport, daddr, hnum);
266
+ if (sk) {
263267 result = sk;
264
- badness = score;
268
+ goto done;
265269 }
266270 }
271
+
272
+ /* Got non-wildcard socket or error on first lookup */
273
+ if (result)
274
+ goto done;
275
+
276
+ /* Lookup wildcard sockets */
277
+ hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
278
+ slot2 = hash2 & udptable->mask;
279
+ hslot2 = &udptable->hash2[slot2];
280
+
281
+ result = udp6_lib_lookup2(net, saddr, sport,
282
+ &in6addr_any, hnum, dif, sdif,
283
+ hslot2, skb);
284
+done:
285
+ if (IS_ERR(result))
286
+ return NULL;
267287 return result;
268288 }
269289 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
....@@ -329,9 +349,9 @@
329349 struct inet_sock *inet = inet_sk(sk);
330350 struct sk_buff *skb;
331351 unsigned int ulen, copied;
332
- int peeked, peeking, off;
333
- int err;
352
+ int off, err, peeking = flags & MSG_PEEK;
334353 int is_udplite = IS_UDPLITE(sk);
354
+ struct udp_mib __percpu *mib;
335355 bool checksum_valid = false;
336356 int is_udp4;
337357
....@@ -342,9 +362,8 @@
342362 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
343363
344364 try_again:
345
- peeking = flags & MSG_PEEK;
346365 off = sk_peek_offset(sk, flags);
347
- skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
366
+ skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
348367 if (!skb)
349368 return err;
350369
....@@ -356,6 +375,7 @@
356375 msg->msg_flags |= MSG_TRUNC;
357376
358377 is_udp4 = (skb->protocol == htons(ETH_P_IP));
378
+ mib = __UDPX_MIB(sk, is_udp4);
359379
360380 /*
361381 * If checksum is needed at all, try to do it while copying the
....@@ -382,26 +402,15 @@
382402 goto csum_copy_err;
383403 }
384404 if (unlikely(err)) {
385
- if (!peeked) {
405
+ if (!peeking) {
386406 atomic_inc(&sk->sk_drops);
387
- if (is_udp4)
388
- UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
389
- is_udplite);
390
- else
391
- UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
392
- is_udplite);
407
+ SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
393408 }
394409 kfree_skb(skb);
395410 return err;
396411 }
397
- if (!peeked) {
398
- if (is_udp4)
399
- UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
400
- is_udplite);
401
- else
402
- UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
403
- is_udplite);
404
- }
412
+ if (!peeking)
413
+ SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
405414
406415 sock_recv_ts_and_drops(msg, sk, skb);
407416
....@@ -429,6 +438,9 @@
429438 (struct sockaddr *)sin6);
430439 }
431440
441
+ if (udp_sk(sk)->gro_enabled)
442
+ udp_cmsg_recv(msg, sk, skb);
443
+
432444 if (np->rxopt.all)
433445 ip6_datagram_recv_common_ctl(sk, msg, skb);
434446
....@@ -451,17 +463,8 @@
451463 csum_copy_err:
452464 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
453465 udp_skb_destructor)) {
454
- if (is_udp4) {
455
- UDP_INC_STATS(sock_net(sk),
456
- UDP_MIB_CSUMERRORS, is_udplite);
457
- UDP_INC_STATS(sock_net(sk),
458
- UDP_MIB_INERRORS, is_udplite);
459
- } else {
460
- UDP6_INC_STATS(sock_net(sk),
461
- UDP_MIB_CSUMERRORS, is_udplite);
462
- UDP6_INC_STATS(sock_net(sk),
463
- UDP_MIB_INERRORS, is_udplite);
464
- }
466
+ SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
467
+ SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
465468 }
466469 kfree_skb(skb);
467470
....@@ -471,26 +474,133 @@
471474 goto try_again;
472475 }
473476
474
-void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
475
- u8 type, u8 code, int offset, __be32 info,
476
- struct udp_table *udptable)
477
+DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
478
+void udpv6_encap_enable(void)
479
+{
480
+ static_branch_inc(&udpv6_encap_needed_key);
481
+}
482
+EXPORT_SYMBOL(udpv6_encap_enable);
483
+
484
+/* Handler for tunnels with arbitrary destination ports: no socket lookup, go
485
+ * through error handlers in encapsulations looking for a match.
486
+ */
487
+static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
488
+ struct inet6_skb_parm *opt,
489
+ u8 type, u8 code, int offset, __be32 info)
490
+{
491
+ int i;
492
+
493
+ for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
494
+ int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
495
+ u8 type, u8 code, int offset, __be32 info);
496
+ const struct ip6_tnl_encap_ops *encap;
497
+
498
+ encap = rcu_dereference(ip6tun_encaps[i]);
499
+ if (!encap)
500
+ continue;
501
+ handler = encap->err_handler;
502
+ if (handler && !handler(skb, opt, type, code, offset, info))
503
+ return 0;
504
+ }
505
+
506
+ return -ENOENT;
507
+}
508
+
509
+/* Try to match ICMP errors to UDP tunnels by looking up a socket without
510
+ * reversing source and destination port: this will match tunnels that force the
511
+ * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
512
+ * lwtunnels might actually break this assumption by being configured with
513
+ * different destination ports on endpoints, in this case we won't be able to
514
+ * trace ICMP messages back to them.
515
+ *
516
+ * If this doesn't match any socket, probe tunnels with arbitrary destination
517
+ * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
518
+ * we've sent packets to won't necessarily match the local destination port.
519
+ *
520
+ * Then ask the tunnel implementation to match the error against a valid
521
+ * association.
522
+ *
523
+ * Return an error if we can't find a match, the socket if we need further
524
+ * processing, zero otherwise.
525
+ */
526
+static struct sock *__udp6_lib_err_encap(struct net *net,
527
+ const struct ipv6hdr *hdr, int offset,
528
+ struct udphdr *uh,
529
+ struct udp_table *udptable,
530
+ struct sk_buff *skb,
531
+ struct inet6_skb_parm *opt,
532
+ u8 type, u8 code, __be32 info)
533
+{
534
+ int network_offset, transport_offset;
535
+ struct sock *sk;
536
+
537
+ network_offset = skb_network_offset(skb);
538
+ transport_offset = skb_transport_offset(skb);
539
+
540
+ /* Network header needs to point to the outer IPv6 header inside ICMP */
541
+ skb_reset_network_header(skb);
542
+
543
+ /* Transport header needs to point to the UDP header */
544
+ skb_set_transport_header(skb, offset);
545
+
546
+ sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
547
+ &hdr->saddr, uh->dest,
548
+ inet6_iif(skb), 0, udptable, skb);
549
+ if (sk) {
550
+ int (*lookup)(struct sock *sk, struct sk_buff *skb);
551
+ struct udp_sock *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
+ if (!sk) {
559
+ sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
560
+ offset, info));
561
+ }
562
+
563
+ skb_set_transport_header(skb, transport_offset);
564
+ skb_set_network_header(skb, network_offset);
565
+
566
+ return sk;
567
+}
568
+
569
+int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
570
+ u8 type, u8 code, int offset, __be32 info,
571
+ struct udp_table *udptable)
477572 {
478573 struct ipv6_pinfo *np;
479574 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
480575 const struct in6_addr *saddr = &hdr->saddr;
481576 const struct in6_addr *daddr = &hdr->daddr;
482577 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
578
+ bool tunnel = false;
483579 struct sock *sk;
484580 int harderr;
485581 int err;
486582 struct net *net = dev_net(skb->dev);
487583
488584 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
489
- inet6_iif(skb), 0, udptable, NULL);
585
+ inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
490586 if (!sk) {
491
- __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
492
- ICMP6_MIB_INERRORS);
493
- return;
587
+ /* No socket for error: try tunnels before discarding */
588
+ sk = ERR_PTR(-ENOENT);
589
+ if (static_branch_unlikely(&udpv6_encap_needed_key)) {
590
+ sk = __udp6_lib_err_encap(net, hdr, offset, uh,
591
+ udptable, skb,
592
+ opt, type, code, info);
593
+ if (!sk)
594
+ return 0;
595
+ }
596
+
597
+ if (IS_ERR(sk)) {
598
+ __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
599
+ ICMP6_MIB_INERRORS);
600
+ return PTR_ERR(sk);
601
+ }
602
+
603
+ tunnel = true;
494604 }
495605
496606 harderr = icmpv6_err_convert(type, code, &err);
....@@ -504,9 +614,18 @@
504614 harderr = 1;
505615 }
506616 if (type == NDISC_REDIRECT) {
507
- ip6_sk_redirect(skb, sk);
617
+ if (tunnel) {
618
+ ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
619
+ sk->sk_mark, sk->sk_uid);
620
+ } else {
621
+ ip6_sk_redirect(skb, sk);
622
+ }
508623 goto out;
509624 }
625
+
626
+ /* Tunnels don't have an application socket: don't pass errors back */
627
+ if (tunnel)
628
+ goto out;
510629
511630 if (!np->recverr) {
512631 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
....@@ -518,7 +637,7 @@
518637 sk->sk_err = err;
519638 sk->sk_error_report(sk);
520639 out:
521
- return;
640
+ return 0;
522641 }
523642
524643 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
....@@ -549,21 +668,14 @@
549668 return 0;
550669 }
551670
552
-static __inline__ void udpv6_err(struct sk_buff *skb,
553
- struct inet6_skb_parm *opt, u8 type,
554
- u8 code, int offset, __be32 info)
671
+static __inline__ int udpv6_err(struct sk_buff *skb,
672
+ struct inet6_skb_parm *opt, u8 type,
673
+ u8 code, int offset, __be32 info)
555674 {
556
- __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
675
+ return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
557676 }
558677
559
-static DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
560
-void udpv6_encap_enable(void)
561
-{
562
- static_branch_enable(&udpv6_encap_needed_key);
563
-}
564
-EXPORT_SYMBOL(udpv6_encap_enable);
565
-
566
-static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
678
+static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
567679 {
568680 struct udp_sock *up = udp_sk(sk);
569681 int is_udplite = IS_UDPLITE(sk);
....@@ -646,10 +758,31 @@
646758 return -1;
647759 }
648760
761
+static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
762
+{
763
+ struct sk_buff *next, *segs;
764
+ int ret;
765
+
766
+ if (likely(!udp_unexpected_gso(sk, skb)))
767
+ return udpv6_queue_rcv_one_skb(sk, skb);
768
+
769
+ __skb_push(skb, -skb_mac_offset(skb));
770
+ segs = udp_rcv_segment(sk, skb, false);
771
+ skb_list_walk_safe(segs, skb, next) {
772
+ __skb_pull(skb, skb_transport_offset(skb));
773
+
774
+ ret = udpv6_queue_rcv_one_skb(sk, skb);
775
+ if (ret > 0)
776
+ ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
777
+ true);
778
+ }
779
+ return 0;
780
+}
781
+
649782 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
650783 __be16 loc_port, const struct in6_addr *loc_addr,
651784 __be16 rmt_port, const struct in6_addr *rmt_addr,
652
- int dif, unsigned short hnum)
785
+ int dif, int sdif, unsigned short hnum)
653786 {
654787 struct inet_sock *inet = inet_sk(sk);
655788
....@@ -661,7 +794,7 @@
661794 (inet->inet_dport && inet->inet_dport != rmt_port) ||
662795 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
663796 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
664
- (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
797
+ !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
665798 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
666799 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
667800 return false;
....@@ -695,6 +828,7 @@
695828 unsigned int offset = offsetof(typeof(*sk), sk_node);
696829 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
697830 int dif = inet6_iif(skb);
831
+ int sdif = inet6_sdif(skb);
698832 struct hlist_node *node;
699833 struct sk_buff *nskb;
700834
....@@ -709,7 +843,8 @@
709843
710844 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
711845 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
712
- uh->source, saddr, dif, hnum))
846
+ uh->source, saddr, dif, sdif,
847
+ hnum))
713848 continue;
714849 /* If zero checksum and no_check is not on for
715850 * the socket then skip it.
....@@ -769,8 +904,7 @@
769904 int ret;
770905
771906 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
772
- skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
773
- ip6_compute_pseudo);
907
+ skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
774908
775909 ret = udpv6_queue_rcv_skb(sk, skb);
776910
....@@ -787,6 +921,7 @@
787921 struct net *net = dev_net(skb->dev);
788922 struct udphdr *uh;
789923 struct sock *sk;
924
+ bool refcounted;
790925 u32 ulen = 0;
791926
792927 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
....@@ -823,21 +958,23 @@
823958 goto csum_error;
824959
825960 /* Check if the socket is already available, e.g. due to early demux */
826
- sk = skb_steal_sock(skb);
961
+ sk = skb_steal_sock(skb, &refcounted);
827962 if (sk) {
828963 struct dst_entry *dst = skb_dst(skb);
829964 int ret;
830965
831
- if (unlikely(sk->sk_rx_dst != dst))
966
+ if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
832967 udp6_sk_rx_dst_set(sk, dst);
833968
834969 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
835
- sock_put(sk);
970
+ if (refcounted)
971
+ sock_put(sk);
836972 goto report_csum_error;
837973 }
838974
839975 ret = udp6_unicast_rcv_skb(sk, skb, uh);
840
- sock_put(sk);
976
+ if (refcounted)
977
+ sock_put(sk);
841978 return ret;
842979 }
843980
....@@ -904,7 +1041,7 @@
9041041
9051042 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
9061043 if (sk->sk_state == TCP_ESTABLISHED &&
907
- INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
1044
+ inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
9081045 return sk;
9091046 /* Only check first socket in chain */
9101047 break;
....@@ -912,7 +1049,7 @@
9121049 return NULL;
9131050 }
9141051
915
-static void udp_v6_early_demux(struct sk_buff *skb)
1052
+void udp_v6_early_demux(struct sk_buff *skb)
9161053 {
9171054 struct net *net = dev_net(skb->dev);
9181055 const struct udphdr *uh;
....@@ -940,7 +1077,7 @@
9401077
9411078 skb->sk = sk;
9421079 skb->destructor = sock_efree;
943
- dst = READ_ONCE(sk->sk_rx_dst);
1080
+ dst = rcu_dereference(sk->sk_rx_dst);
9441081
9451082 if (dst)
9461083 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
....@@ -953,7 +1090,7 @@
9531090 }
9541091 }
9551092
956
-static __inline__ int udpv6_rcv(struct sk_buff *skb)
1093
+INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
9571094 {
9581095 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
9591096 }
....@@ -977,6 +1114,8 @@
9771114 static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
9781115 int addr_len)
9791116 {
1117
+ if (addr_len < offsetofend(struct sockaddr, sa_family))
1118
+ return -EINVAL;
9801119 /* The following checks are replicated from __ip6_datagram_connect()
9811120 * and intended to prevent BPF program called below from accessing
9821121 * bytes that are out of the bound specified by user in addr_len.
....@@ -998,6 +1137,9 @@
9981137 * @sk: socket we are sending on
9991138 * @skb: sk_buff containing the filled-in UDP header
10001139 * (checksum field must be zeroed out)
1140
+ * @saddr: source address
1141
+ * @daddr: destination address
1142
+ * @len: length of packet
10011143 */
10021144 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
10031145 const struct in6_addr *saddr,
....@@ -1177,6 +1319,7 @@
11771319 ipcm6_init(&ipc6);
11781320 ipc6.gso_size = READ_ONCE(up->gso_size);
11791321 ipc6.sockc.tsflags = sk->sk_tsflags;
1322
+ ipc6.sockc.mark = sk->sk_mark;
11801323
11811324 /* destination address check */
11821325 if (sin6) {
....@@ -1219,9 +1362,11 @@
12191362 msg->msg_name = &sin;
12201363 msg->msg_namelen = sizeof(sin);
12211364 do_udp_sendmsg:
1222
- if (__ipv6_only_sock(sk))
1223
- return -ENETUNREACH;
1224
- return udp_sendmsg(sk, msg, len);
1365
+ err = __ipv6_only_sock(sk) ?
1366
+ -ENETUNREACH : udp_sendmsg(sk, msg, len);
1367
+ msg->msg_name = sin6;
1368
+ msg->msg_namelen = addr_len;
1369
+ return err;
12251370 }
12261371 }
12271372
....@@ -1266,7 +1411,7 @@
12661411 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
12671412 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
12681413 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1269
- if (!flowlabel)
1414
+ if (IS_ERR(flowlabel))
12701415 return -EINVAL;
12711416 }
12721417 }
....@@ -1299,7 +1444,6 @@
12991444 if (!fl6.flowi6_oif)
13001445 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
13011446
1302
- fl6.flowi6_mark = sk->sk_mark;
13031447 fl6.flowi6_uid = sk->sk_uid;
13041448
13051449 if (msg->msg_controllen) {
....@@ -1318,7 +1462,7 @@
13181462 }
13191463 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
13201464 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1321
- if (!flowlabel)
1465
+ if (IS_ERR(flowlabel))
13221466 return -EINVAL;
13231467 }
13241468 if (!(opt->opt_nflen|opt->opt_flen))
....@@ -1335,6 +1479,7 @@
13351479 ipc6.opt = opt;
13361480
13371481 fl6.flowi6_proto = sk->sk_protocol;
1482
+ fl6.flowi6_mark = ipc6.sockc.mark;
13381483 fl6.daddr = *daddr;
13391484 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
13401485 fl6.saddr = np->saddr;
....@@ -1376,7 +1521,7 @@
13761521 } else if (!fl6.flowi6_oif)
13771522 fl6.flowi6_oif = np->ucast_oif;
13781523
1379
- security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1524
+ security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
13801525
13811526 if (ipc6.tclass < 0)
13821527 ipc6.tclass = np->tclass;
....@@ -1482,38 +1627,32 @@
14821627 udp_v6_flush_pending_frames(sk);
14831628 release_sock(sk);
14841629
1485
- if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
1486
- void (*encap_destroy)(struct sock *sk);
1487
- encap_destroy = READ_ONCE(up->encap_destroy);
1488
- if (encap_destroy)
1489
- encap_destroy(sk);
1630
+ if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1631
+ if (up->encap_type) {
1632
+ void (*encap_destroy)(struct sock *sk);
1633
+ encap_destroy = READ_ONCE(up->encap_destroy);
1634
+ if (encap_destroy)
1635
+ encap_destroy(sk);
1636
+ }
1637
+ if (up->encap_enabled) {
1638
+ static_branch_dec(&udpv6_encap_needed_key);
1639
+ udp_encap_disable();
1640
+ }
14901641 }
1491
-
1492
- inet6_destroy_sock(sk);
14931642 }
14941643
14951644 /*
14961645 * Socket option code for UDP
14971646 */
1498
-int udpv6_setsockopt(struct sock *sk, int level, int optname,
1499
- char __user *optval, unsigned int optlen)
1647
+int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1648
+ unsigned int optlen)
15001649 {
15011650 if (level == SOL_UDP || level == SOL_UDPLITE)
1502
- return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1651
+ return udp_lib_setsockopt(sk, level, optname,
1652
+ optval, optlen,
15031653 udp_v6_push_pending_frames);
15041654 return ipv6_setsockopt(sk, level, optname, optval, optlen);
15051655 }
1506
-
1507
-#ifdef CONFIG_COMPAT
1508
-int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1509
- char __user *optval, unsigned int optlen)
1510
-{
1511
- if (level == SOL_UDP || level == SOL_UDPLITE)
1512
- return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1513
- udp_v6_push_pending_frames);
1514
- return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1515
-}
1516
-#endif
15171656
15181657 int udpv6_getsockopt(struct sock *sk, int level, int optname,
15191658 char __user *optval, int __user *optlen)
....@@ -1523,22 +1662,7 @@
15231662 return ipv6_getsockopt(sk, level, optname, optval, optlen);
15241663 }
15251664
1526
-#ifdef CONFIG_COMPAT
1527
-int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1528
- char __user *optval, int __user *optlen)
1529
-{
1530
- if (level == SOL_UDP || level == SOL_UDPLITE)
1531
- return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1532
- return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1533
-}
1534
-#endif
1535
-
1536
-/* thinking of making this const? Don't.
1537
- * early_demux can change based on sysctl.
1538
- */
1539
-static struct inet6_protocol udpv6_protocol = {
1540
- .early_demux = udp_v6_early_demux,
1541
- .early_demux_handler = udp_v6_early_demux,
1665
+static const struct inet6_protocol udpv6_protocol = {
15421666 .handler = udpv6_rcv,
15431667 .err_handler = udpv6_err,
15441668 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
....@@ -1598,7 +1722,7 @@
15981722 .connect = ip6_datagram_connect,
15991723 .disconnect = udp_disconnect,
16001724 .ioctl = udp_ioctl,
1601
- .init = udp_init_sock,
1725
+ .init = udpv6_init_sock,
16021726 .destroy = udpv6_destroy_sock,
16031727 .setsockopt = udpv6_setsockopt,
16041728 .getsockopt = udpv6_getsockopt,
....@@ -1615,10 +1739,6 @@
16151739 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
16161740 .obj_size = sizeof(struct udp6_sock),
16171741 .h.udp_table = &udp_table,
1618
-#ifdef CONFIG_COMPAT
1619
- .compat_setsockopt = compat_udpv6_setsockopt,
1620
- .compat_getsockopt = compat_udpv6_getsockopt,
1621
-#endif
16221742 .diag_destroy = udp_abort,
16231743 };
16241744