forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
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>
....@@ -55,16 +53,6 @@
5553 #include <linux/seq_file.h>
5654 #include <trace/events/skb.h>
5755 #include "udp_impl.h"
58
-
59
-static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
60
-{
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;
67
-}
6856
6957 static u32 udp6_ehashfn(const struct net *net,
7058 const struct in6_addr *laddr,
....@@ -101,7 +89,7 @@
10189 return udp_lib_get_port(sk, snum, hash2_nulladdr);
10290 }
10391
104
-static void udp_v6_rehash(struct sock *sk)
92
+void udp_v6_rehash(struct sock *sk)
10593 {
10694 u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
10795 &sk->sk_v6_rcv_saddr,
....@@ -113,14 +101,18 @@
113101 static int compute_score(struct sock *sk, struct net *net,
114102 const struct in6_addr *saddr, __be16 sport,
115103 const struct in6_addr *daddr, unsigned short hnum,
116
- int dif, int sdif, bool exact_dif)
104
+ int dif, int sdif)
117105 {
118106 int score;
119107 struct inet_sock *inet;
108
+ bool dev_match;
120109
121110 if (!net_eq(sock_net(sk), net) ||
122111 udp_sk(sk)->udp_port_hash != hnum ||
123112 sk->sk_family != PF_INET6)
113
+ return -1;
114
+
115
+ if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
124116 return -1;
125117
126118 score = 0;
....@@ -132,27 +124,17 @@
132124 score++;
133125 }
134126
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
-
141127 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
142128 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
143129 return -1;
144130 score++;
145131 }
146132
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
- }
133
+ dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
134
+ if (!dev_match)
135
+ return -1;
136
+ if (sk->sk_bound_dev_if)
137
+ score++;
156138
157139 if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
158140 score++;
....@@ -160,41 +142,76 @@
160142 return score;
161143 }
162144
145
+static struct sock *lookup_reuseport(struct net *net, struct sock *sk,
146
+ struct sk_buff *skb,
147
+ const struct in6_addr *saddr,
148
+ __be16 sport,
149
+ const struct in6_addr *daddr,
150
+ unsigned int hnum)
151
+{
152
+ struct sock *reuse_sk = NULL;
153
+ u32 hash;
154
+
155
+ if (sk->sk_reuseport && sk->sk_state != TCP_ESTABLISHED) {
156
+ hash = udp6_ehashfn(net, daddr, hnum, saddr, sport);
157
+ reuse_sk = reuseport_select_sock(sk, hash, skb,
158
+ sizeof(struct udphdr));
159
+ }
160
+ return reuse_sk;
161
+}
162
+
163163 /* called with rcu_read_lock() */
164164 static struct sock *udp6_lib_lookup2(struct net *net,
165165 const struct in6_addr *saddr, __be16 sport,
166166 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)
167
+ int dif, int sdif, struct udp_hslot *hslot2,
168
+ struct sk_buff *skb)
169169 {
170
- struct sock *sk, *result, *reuseport_result;
170
+ struct sock *sk, *result;
171171 int score, badness;
172
- u32 hash = 0;
173172
174173 result = NULL;
175174 badness = -1;
176175 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
177176 score = compute_score(sk, net, saddr, sport,
178
- daddr, hnum, dif, sdif, exact_dif);
177
+ daddr, hnum, dif, sdif);
179178 if (score > badness) {
180
- reuseport_result = NULL;
179
+ result = lookup_reuseport(net, sk, skb,
180
+ saddr, sport, daddr, hnum);
181
+ /* Fall back to scoring if group has connections */
182
+ if (result && !reuseport_has_conns(sk))
183
+ return result;
181184
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;
191
- }
192
-
193
- result = reuseport_result ? : sk;
185
+ result = result ? : sk;
194186 badness = score;
195187 }
196188 }
197189 return result;
190
+}
191
+
192
+static inline struct sock *udp6_lookup_run_bpf(struct net *net,
193
+ struct udp_table *udptable,
194
+ struct sk_buff *skb,
195
+ const struct in6_addr *saddr,
196
+ __be16 sport,
197
+ const struct in6_addr *daddr,
198
+ u16 hnum)
199
+{
200
+ struct sock *sk, *reuse_sk;
201
+ bool no_reuseport;
202
+
203
+ if (udptable != &udp_table)
204
+ return NULL; /* only UDP is supported */
205
+
206
+ no_reuseport = bpf_sk_lookup_run_v6(net, IPPROTO_UDP,
207
+ saddr, sport, daddr, hnum, &sk);
208
+ if (no_reuseport || IS_ERR_OR_NULL(sk))
209
+ return sk;
210
+
211
+ reuse_sk = lookup_reuseport(net, sk, skb, saddr, sport, daddr, hnum);
212
+ if (reuse_sk)
213
+ sk = reuse_sk;
214
+ return sk;
198215 }
199216
200217 /* rcu_read_lock() must be held */
....@@ -204,66 +221,47 @@
204221 int dif, int sdif, struct udp_table *udptable,
205222 struct sk_buff *skb)
206223 {
207
- struct sock *sk, *result;
208224 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;
225
+ unsigned int hash2, slot2;
226
+ struct udp_hslot *hslot2;
227
+ struct sock *result, *sk;
214228
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;
229
+ hash2 = ipv6_portaddr_hash(net, daddr, hnum);
230
+ slot2 = hash2 & udptable->mask;
231
+ hslot2 = &udptable->hash2[slot2];
221232
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;
233
+ /* Lookup connected or non-wildcard sockets */
234
+ result = udp6_lib_lookup2(net, saddr, sport,
235
+ daddr, hnum, dif, sdif,
236
+ hslot2, skb);
237
+ if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
238
+ goto done;
232239
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
- }
240
+ /* Lookup redirect from BPF */
241
+ if (static_branch_unlikely(&bpf_sk_lookup_enabled)) {
242
+ sk = udp6_lookup_run_bpf(net, udptable, skb,
243
+ saddr, sport, daddr, hnum);
244
+ if (sk) {
263245 result = sk;
264
- badness = score;
246
+ goto done;
265247 }
266248 }
249
+
250
+ /* Got non-wildcard socket or error on first lookup */
251
+ if (result)
252
+ goto done;
253
+
254
+ /* Lookup wildcard sockets */
255
+ hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
256
+ slot2 = hash2 & udptable->mask;
257
+ hslot2 = &udptable->hash2[slot2];
258
+
259
+ result = udp6_lib_lookup2(net, saddr, sport,
260
+ &in6addr_any, hnum, dif, sdif,
261
+ hslot2, skb);
262
+done:
263
+ if (IS_ERR(result))
264
+ return NULL;
267265 return result;
268266 }
269267 EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
....@@ -329,9 +327,9 @@
329327 struct inet_sock *inet = inet_sk(sk);
330328 struct sk_buff *skb;
331329 unsigned int ulen, copied;
332
- int peeked, peeking, off;
333
- int err;
330
+ int off, err, peeking = flags & MSG_PEEK;
334331 int is_udplite = IS_UDPLITE(sk);
332
+ struct udp_mib __percpu *mib;
335333 bool checksum_valid = false;
336334 int is_udp4;
337335
....@@ -342,9 +340,8 @@
342340 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
343341
344342 try_again:
345
- peeking = flags & MSG_PEEK;
346343 off = sk_peek_offset(sk, flags);
347
- skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
344
+ skb = __skb_recv_udp(sk, flags, noblock, &off, &err);
348345 if (!skb)
349346 return err;
350347
....@@ -356,6 +353,7 @@
356353 msg->msg_flags |= MSG_TRUNC;
357354
358355 is_udp4 = (skb->protocol == htons(ETH_P_IP));
356
+ mib = __UDPX_MIB(sk, is_udp4);
359357
360358 /*
361359 * If checksum is needed at all, try to do it while copying the
....@@ -382,26 +380,15 @@
382380 goto csum_copy_err;
383381 }
384382 if (unlikely(err)) {
385
- if (!peeked) {
383
+ if (!peeking) {
386384 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);
385
+ SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
393386 }
394387 kfree_skb(skb);
395388 return err;
396389 }
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
- }
390
+ if (!peeking)
391
+ SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
405392
406393 sock_recv_ts_and_drops(msg, sk, skb);
407394
....@@ -429,6 +416,9 @@
429416 (struct sockaddr *)sin6);
430417 }
431418
419
+ if (udp_sk(sk)->gro_enabled)
420
+ udp_cmsg_recv(msg, sk, skb);
421
+
432422 if (np->rxopt.all)
433423 ip6_datagram_recv_common_ctl(sk, msg, skb);
434424
....@@ -451,17 +441,8 @@
451441 csum_copy_err:
452442 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
453443 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
- }
444
+ SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
445
+ SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
465446 }
466447 kfree_skb(skb);
467448
....@@ -471,26 +452,133 @@
471452 goto try_again;
472453 }
473454
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)
455
+DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
456
+void udpv6_encap_enable(void)
457
+{
458
+ static_branch_inc(&udpv6_encap_needed_key);
459
+}
460
+EXPORT_SYMBOL(udpv6_encap_enable);
461
+
462
+/* Handler for tunnels with arbitrary destination ports: no socket lookup, go
463
+ * through error handlers in encapsulations looking for a match.
464
+ */
465
+static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
466
+ struct inet6_skb_parm *opt,
467
+ u8 type, u8 code, int offset, __be32 info)
468
+{
469
+ int i;
470
+
471
+ for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
472
+ int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
473
+ u8 type, u8 code, int offset, __be32 info);
474
+ const struct ip6_tnl_encap_ops *encap;
475
+
476
+ encap = rcu_dereference(ip6tun_encaps[i]);
477
+ if (!encap)
478
+ continue;
479
+ handler = encap->err_handler;
480
+ if (handler && !handler(skb, opt, type, code, offset, info))
481
+ return 0;
482
+ }
483
+
484
+ return -ENOENT;
485
+}
486
+
487
+/* Try to match ICMP errors to UDP tunnels by looking up a socket without
488
+ * reversing source and destination port: this will match tunnels that force the
489
+ * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
490
+ * lwtunnels might actually break this assumption by being configured with
491
+ * different destination ports on endpoints, in this case we won't be able to
492
+ * trace ICMP messages back to them.
493
+ *
494
+ * If this doesn't match any socket, probe tunnels with arbitrary destination
495
+ * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
496
+ * we've sent packets to won't necessarily match the local destination port.
497
+ *
498
+ * Then ask the tunnel implementation to match the error against a valid
499
+ * association.
500
+ *
501
+ * Return an error if we can't find a match, the socket if we need further
502
+ * processing, zero otherwise.
503
+ */
504
+static struct sock *__udp6_lib_err_encap(struct net *net,
505
+ const struct ipv6hdr *hdr, int offset,
506
+ struct udphdr *uh,
507
+ struct udp_table *udptable,
508
+ struct sk_buff *skb,
509
+ struct inet6_skb_parm *opt,
510
+ u8 type, u8 code, __be32 info)
511
+{
512
+ int network_offset, transport_offset;
513
+ struct sock *sk;
514
+
515
+ network_offset = skb_network_offset(skb);
516
+ transport_offset = skb_transport_offset(skb);
517
+
518
+ /* Network header needs to point to the outer IPv6 header inside ICMP */
519
+ skb_reset_network_header(skb);
520
+
521
+ /* Transport header needs to point to the UDP header */
522
+ skb_set_transport_header(skb, offset);
523
+
524
+ sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
525
+ &hdr->saddr, uh->dest,
526
+ inet6_iif(skb), 0, udptable, skb);
527
+ if (sk) {
528
+ int (*lookup)(struct sock *sk, struct sk_buff *skb);
529
+ struct udp_sock *up = udp_sk(sk);
530
+
531
+ lookup = READ_ONCE(up->encap_err_lookup);
532
+ if (!lookup || lookup(sk, skb))
533
+ sk = NULL;
534
+ }
535
+
536
+ if (!sk) {
537
+ sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
538
+ offset, info));
539
+ }
540
+
541
+ skb_set_transport_header(skb, transport_offset);
542
+ skb_set_network_header(skb, network_offset);
543
+
544
+ return sk;
545
+}
546
+
547
+int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
548
+ u8 type, u8 code, int offset, __be32 info,
549
+ struct udp_table *udptable)
477550 {
478551 struct ipv6_pinfo *np;
479552 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
480553 const struct in6_addr *saddr = &hdr->saddr;
481554 const struct in6_addr *daddr = &hdr->daddr;
482555 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
556
+ bool tunnel = false;
483557 struct sock *sk;
484558 int harderr;
485559 int err;
486560 struct net *net = dev_net(skb->dev);
487561
488562 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
489
- inet6_iif(skb), 0, udptable, NULL);
563
+ inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
490564 if (!sk) {
491
- __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
492
- ICMP6_MIB_INERRORS);
493
- return;
565
+ /* No socket for error: try tunnels before discarding */
566
+ sk = ERR_PTR(-ENOENT);
567
+ if (static_branch_unlikely(&udpv6_encap_needed_key)) {
568
+ sk = __udp6_lib_err_encap(net, hdr, offset, uh,
569
+ udptable, skb,
570
+ opt, type, code, info);
571
+ if (!sk)
572
+ return 0;
573
+ }
574
+
575
+ if (IS_ERR(sk)) {
576
+ __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
577
+ ICMP6_MIB_INERRORS);
578
+ return PTR_ERR(sk);
579
+ }
580
+
581
+ tunnel = true;
494582 }
495583
496584 harderr = icmpv6_err_convert(type, code, &err);
....@@ -504,9 +592,18 @@
504592 harderr = 1;
505593 }
506594 if (type == NDISC_REDIRECT) {
507
- ip6_sk_redirect(skb, sk);
595
+ if (tunnel) {
596
+ ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
597
+ sk->sk_mark, sk->sk_uid);
598
+ } else {
599
+ ip6_sk_redirect(skb, sk);
600
+ }
508601 goto out;
509602 }
603
+
604
+ /* Tunnels don't have an application socket: don't pass errors back */
605
+ if (tunnel)
606
+ goto out;
510607
511608 if (!np->recverr) {
512609 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
....@@ -518,7 +615,7 @@
518615 sk->sk_err = err;
519616 sk->sk_error_report(sk);
520617 out:
521
- return;
618
+ return 0;
522619 }
523620
524621 static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
....@@ -549,21 +646,14 @@
549646 return 0;
550647 }
551648
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)
649
+static __inline__ int udpv6_err(struct sk_buff *skb,
650
+ struct inet6_skb_parm *opt, u8 type,
651
+ u8 code, int offset, __be32 info)
555652 {
556
- __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
653
+ return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
557654 }
558655
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)
656
+static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
567657 {
568658 struct udp_sock *up = udp_sk(sk);
569659 int is_udplite = IS_UDPLITE(sk);
....@@ -646,10 +736,31 @@
646736 return -1;
647737 }
648738
739
+static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
740
+{
741
+ struct sk_buff *next, *segs;
742
+ int ret;
743
+
744
+ if (likely(!udp_unexpected_gso(sk, skb)))
745
+ return udpv6_queue_rcv_one_skb(sk, skb);
746
+
747
+ __skb_push(skb, -skb_mac_offset(skb));
748
+ segs = udp_rcv_segment(sk, skb, false);
749
+ skb_list_walk_safe(segs, skb, next) {
750
+ __skb_pull(skb, skb_transport_offset(skb));
751
+
752
+ ret = udpv6_queue_rcv_one_skb(sk, skb);
753
+ if (ret > 0)
754
+ ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
755
+ true);
756
+ }
757
+ return 0;
758
+}
759
+
649760 static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
650761 __be16 loc_port, const struct in6_addr *loc_addr,
651762 __be16 rmt_port, const struct in6_addr *rmt_addr,
652
- int dif, unsigned short hnum)
763
+ int dif, int sdif, unsigned short hnum)
653764 {
654765 struct inet_sock *inet = inet_sk(sk);
655766
....@@ -661,7 +772,7 @@
661772 (inet->inet_dport && inet->inet_dport != rmt_port) ||
662773 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
663774 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
664
- (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
775
+ !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
665776 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
666777 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
667778 return false;
....@@ -695,6 +806,7 @@
695806 unsigned int offset = offsetof(typeof(*sk), sk_node);
696807 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
697808 int dif = inet6_iif(skb);
809
+ int sdif = inet6_sdif(skb);
698810 struct hlist_node *node;
699811 struct sk_buff *nskb;
700812
....@@ -709,7 +821,8 @@
709821
710822 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
711823 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
712
- uh->source, saddr, dif, hnum))
824
+ uh->source, saddr, dif, sdif,
825
+ hnum))
713826 continue;
714827 /* If zero checksum and no_check is not on for
715828 * the socket then skip it.
....@@ -769,8 +882,7 @@
769882 int ret;
770883
771884 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);
885
+ skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
774886
775887 ret = udpv6_queue_rcv_skb(sk, skb);
776888
....@@ -787,6 +899,7 @@
787899 struct net *net = dev_net(skb->dev);
788900 struct udphdr *uh;
789901 struct sock *sk;
902
+ bool refcounted;
790903 u32 ulen = 0;
791904
792905 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
....@@ -823,21 +936,23 @@
823936 goto csum_error;
824937
825938 /* Check if the socket is already available, e.g. due to early demux */
826
- sk = skb_steal_sock(skb);
939
+ sk = skb_steal_sock(skb, &refcounted);
827940 if (sk) {
828941 struct dst_entry *dst = skb_dst(skb);
829942 int ret;
830943
831
- if (unlikely(sk->sk_rx_dst != dst))
944
+ if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
832945 udp6_sk_rx_dst_set(sk, dst);
833946
834947 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
835
- sock_put(sk);
948
+ if (refcounted)
949
+ sock_put(sk);
836950 goto report_csum_error;
837951 }
838952
839953 ret = udp6_unicast_rcv_skb(sk, skb, uh);
840
- sock_put(sk);
954
+ if (refcounted)
955
+ sock_put(sk);
841956 return ret;
842957 }
843958
....@@ -904,7 +1019,7 @@
9041019
9051020 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
9061021 if (sk->sk_state == TCP_ESTABLISHED &&
907
- INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
1022
+ inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
9081023 return sk;
9091024 /* Only check first socket in chain */
9101025 break;
....@@ -912,7 +1027,7 @@
9121027 return NULL;
9131028 }
9141029
915
-static void udp_v6_early_demux(struct sk_buff *skb)
1030
+void udp_v6_early_demux(struct sk_buff *skb)
9161031 {
9171032 struct net *net = dev_net(skb->dev);
9181033 const struct udphdr *uh;
....@@ -940,7 +1055,7 @@
9401055
9411056 skb->sk = sk;
9421057 skb->destructor = sock_efree;
943
- dst = READ_ONCE(sk->sk_rx_dst);
1058
+ dst = rcu_dereference(sk->sk_rx_dst);
9441059
9451060 if (dst)
9461061 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
....@@ -953,7 +1068,7 @@
9531068 }
9541069 }
9551070
956
-static __inline__ int udpv6_rcv(struct sk_buff *skb)
1071
+INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
9571072 {
9581073 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
9591074 }
....@@ -977,6 +1092,8 @@
9771092 static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
9781093 int addr_len)
9791094 {
1095
+ if (addr_len < offsetofend(struct sockaddr, sa_family))
1096
+ return -EINVAL;
9801097 /* The following checks are replicated from __ip6_datagram_connect()
9811098 * and intended to prevent BPF program called below from accessing
9821099 * bytes that are out of the bound specified by user in addr_len.
....@@ -998,6 +1115,9 @@
9981115 * @sk: socket we are sending on
9991116 * @skb: sk_buff containing the filled-in UDP header
10001117 * (checksum field must be zeroed out)
1118
+ * @saddr: source address
1119
+ * @daddr: destination address
1120
+ * @len: length of packet
10011121 */
10021122 static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
10031123 const struct in6_addr *saddr,
....@@ -1177,6 +1297,7 @@
11771297 ipcm6_init(&ipc6);
11781298 ipc6.gso_size = READ_ONCE(up->gso_size);
11791299 ipc6.sockc.tsflags = sk->sk_tsflags;
1300
+ ipc6.sockc.mark = sk->sk_mark;
11801301
11811302 /* destination address check */
11821303 if (sin6) {
....@@ -1266,7 +1387,7 @@
12661387 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
12671388 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
12681389 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1269
- if (!flowlabel)
1390
+ if (IS_ERR(flowlabel))
12701391 return -EINVAL;
12711392 }
12721393 }
....@@ -1299,7 +1420,6 @@
12991420 if (!fl6.flowi6_oif)
13001421 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
13011422
1302
- fl6.flowi6_mark = sk->sk_mark;
13031423 fl6.flowi6_uid = sk->sk_uid;
13041424
13051425 if (msg->msg_controllen) {
....@@ -1318,7 +1438,7 @@
13181438 }
13191439 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
13201440 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1321
- if (!flowlabel)
1441
+ if (IS_ERR(flowlabel))
13221442 return -EINVAL;
13231443 }
13241444 if (!(opt->opt_nflen|opt->opt_flen))
....@@ -1335,6 +1455,7 @@
13351455 ipc6.opt = opt;
13361456
13371457 fl6.flowi6_proto = sk->sk_protocol;
1458
+ fl6.flowi6_mark = ipc6.sockc.mark;
13381459 fl6.daddr = *daddr;
13391460 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
13401461 fl6.saddr = np->saddr;
....@@ -1376,7 +1497,7 @@
13761497 } else if (!fl6.flowi6_oif)
13771498 fl6.flowi6_oif = np->ucast_oif;
13781499
1379
- security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1500
+ security_sk_classify_flow(sk, flowi6_to_flowi_common(&fl6));
13801501
13811502 if (ipc6.tclass < 0)
13821503 ipc6.tclass = np->tclass;
....@@ -1482,11 +1603,17 @@
14821603 udp_v6_flush_pending_frames(sk);
14831604 release_sock(sk);
14841605
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);
1606
+ if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1607
+ if (up->encap_type) {
1608
+ void (*encap_destroy)(struct sock *sk);
1609
+ encap_destroy = READ_ONCE(up->encap_destroy);
1610
+ if (encap_destroy)
1611
+ encap_destroy(sk);
1612
+ }
1613
+ if (up->encap_enabled) {
1614
+ static_branch_dec(&udpv6_encap_needed_key);
1615
+ udp_encap_disable();
1616
+ }
14901617 }
14911618
14921619 inet6_destroy_sock(sk);
....@@ -1495,25 +1622,15 @@
14951622 /*
14961623 * Socket option code for UDP
14971624 */
1498
-int udpv6_setsockopt(struct sock *sk, int level, int optname,
1499
- char __user *optval, unsigned int optlen)
1625
+int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1626
+ unsigned int optlen)
15001627 {
15011628 if (level == SOL_UDP || level == SOL_UDPLITE)
1502
- return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1629
+ return udp_lib_setsockopt(sk, level, optname,
1630
+ optval, optlen,
15031631 udp_v6_push_pending_frames);
15041632 return ipv6_setsockopt(sk, level, optname, optval, optlen);
15051633 }
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
15171634
15181635 int udpv6_getsockopt(struct sock *sk, int level, int optname,
15191636 char __user *optval, int __user *optlen)
....@@ -1523,22 +1640,7 @@
15231640 return ipv6_getsockopt(sk, level, optname, optval, optlen);
15241641 }
15251642
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,
1643
+static const struct inet6_protocol udpv6_protocol = {
15421644 .handler = udpv6_rcv,
15431645 .err_handler = udpv6_err,
15441646 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
....@@ -1615,10 +1717,6 @@
16151717 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
16161718 .obj_size = sizeof(struct udp6_sock),
16171719 .h.udp_table = &udp_table,
1618
-#ifdef CONFIG_COMPAT
1619
- .compat_setsockopt = compat_udpv6_setsockopt,
1620
- .compat_getsockopt = compat_udpv6_getsockopt,
1621
-#endif
16221720 .diag_destroy = udp_abort,
16231721 };
16241722