blob: 18328b5c37d8d1dcf02839aac25f9615cf26e124 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * UDP over IPv6
3 * Linux INET6 implementation
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * Based on linux/ipv4/udp.c
9 *
10 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * 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.
22 */
23
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
28#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
35#include <linux/module.h>
36#include <linux/skbuff.h>
37#include <linux/slab.h>
38#include <asm/uaccess.h>
39
40#include <net/ndisc.h>
41#include <net/protocol.h>
42#include <net/transp_v6.h>
43#include <net/ip6_route.h>
44#include <net/raw.h>
45#include <net/tcp_states.h>
46#include <net/ip6_checksum.h>
47#include <net/xfrm.h>
48
49#include <linux/proc_fs.h>
50#include <linux/seq_file.h>
51#include "udp_impl.h"
52
53#include <net/netfilter/nf_conntrack.h>
54#include <net/SI/fast_common.h>
55
56
57int ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
58{
59 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
60 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
61 __be32 sk1_rcv_saddr = sk_rcv_saddr(sk);
62 __be32 sk2_rcv_saddr = sk_rcv_saddr(sk2);
63 int sk_ipv6only = ipv6_only_sock(sk);
64 int sk2_ipv6only = inet_v6_ipv6only(sk2);
65 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
66 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
67
68 /* if both are mapped, treat as IPv4 */
69 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
70 return (!sk2_ipv6only &&
71 (!sk1_rcv_saddr || !sk2_rcv_saddr ||
72 sk1_rcv_saddr == sk2_rcv_saddr));
73
74 if (addr_type2 == IPV6_ADDR_ANY &&
75 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
76 return 1;
77
78 if (addr_type == IPV6_ADDR_ANY &&
79 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
80 return 1;
81
82 if (sk2_rcv_saddr6 &&
83 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
84 return 1;
85
86 return 0;
87}
88
89static unsigned int udp6_portaddr_hash(struct net *net,
90 const struct in6_addr *addr6,
91 unsigned int port)
92{
93 unsigned int hash, mix = net_hash_mix(net);
94
95 if (ipv6_addr_any(addr6))
96 hash = jhash_1word(0, mix);
97 else if (ipv6_addr_v4mapped(addr6))
98 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
99 else
100 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
101
102 return hash ^ port;
103}
104
105
106int udp_v6_get_port(struct sock *sk, unsigned short snum)
107{
108 unsigned int hash2_nulladdr =
109 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
110 unsigned int hash2_partial =
111 udp6_portaddr_hash(sock_net(sk), &inet6_sk(sk)->rcv_saddr, 0);
112
113 /* precompute partial secondary hash */
114 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
115 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
116}
117
118static void udp_v6_rehash(struct sock *sk)
119{
120 u16 new_hash = udp6_portaddr_hash(sock_net(sk),
121 &inet6_sk(sk)->rcv_saddr,
122 inet_sk(sk)->inet_num);
123
124 udp_lib_rehash(sk, new_hash);
125}
126
127static inline int compute_score(struct sock *sk, struct net *net,
128 unsigned short hnum,
129 const struct in6_addr *saddr, __be16 sport,
130 const struct in6_addr *daddr, __be16 dport,
131 int dif)
132{
133 int score = -1;
134
135 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
136 sk->sk_family == PF_INET6) {
137 struct ipv6_pinfo *np = inet6_sk(sk);
138 struct inet_sock *inet = inet_sk(sk);
139
140 score = 0;
141 if (inet->inet_dport) {
142 if (inet->inet_dport != sport)
143 return -1;
144 score++;
145 }
146 if (!ipv6_addr_any(&np->rcv_saddr)) {
147 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
148 return -1;
149 score++;
150 }
151 if (!ipv6_addr_any(&np->daddr)) {
152 if (!ipv6_addr_equal(&np->daddr, saddr))
153 return -1;
154 score++;
155 }
156 if (sk->sk_bound_dev_if) {
157 if (sk->sk_bound_dev_if != dif)
158 return -1;
159 score++;
160 }
161 }
162 return score;
163}
164
165#define SCORE2_MAX (1 + 1 + 1)
166static inline int compute_score2(struct sock *sk, struct net *net,
167 const struct in6_addr *saddr, __be16 sport,
168 const struct in6_addr *daddr, unsigned short hnum,
169 int dif)
170{
171 int score = -1;
172
173 if (net_eq(sock_net(sk), net) && udp_sk(sk)->udp_port_hash == hnum &&
174 sk->sk_family == PF_INET6) {
175 struct ipv6_pinfo *np = inet6_sk(sk);
176 struct inet_sock *inet = inet_sk(sk);
177
178 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
179 return -1;
180 score = 0;
181 if (inet->inet_dport) {
182 if (inet->inet_dport != sport)
183 return -1;
184 score++;
185 }
186 if (!ipv6_addr_any(&np->daddr)) {
187 if (!ipv6_addr_equal(&np->daddr, saddr))
188 return -1;
189 score++;
190 }
191 if (sk->sk_bound_dev_if) {
192 if (sk->sk_bound_dev_if != dif)
193 return -1;
194 score++;
195 }
196 }
197 return score;
198}
199
200
201/* called with read_rcu_lock() */
202static struct sock *udp6_lib_lookup2(struct net *net,
203 const struct in6_addr *saddr, __be16 sport,
204 const struct in6_addr *daddr, unsigned int hnum, int dif,
205 struct udp_hslot *hslot2, unsigned int slot2)
206{
207 struct sock *sk, *result;
208 struct hlist_nulls_node *node;
209 int score, badness;
210
211begin:
212 result = NULL;
213 badness = -1;
214 udp_portaddr_for_each_entry_rcu(sk, node, &hslot2->head) {
215 score = compute_score2(sk, net, saddr, sport,
216 daddr, hnum, dif);
217 if (score > badness) {
218 result = sk;
219 badness = score;
220 if (score == SCORE2_MAX)
221 goto exact_match;
222 }
223 }
224 /*
225 * if the nulls value we got at the end of this lookup is
226 * not the expected one, we must restart lookup.
227 * We probably met an item that was moved to another chain.
228 */
229 if (get_nulls_value(node) != slot2)
230 goto begin;
231
232 if (result) {
233exact_match:
234 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
235 result = NULL;
236 else if (unlikely(compute_score2(result, net, saddr, sport,
237 daddr, hnum, dif) < badness)) {
238 sock_put(result);
239 goto begin;
240 }
241 }
242 return result;
243}
244
245struct sock *__udp6_lib_lookup(struct net *net,
246 const struct in6_addr *saddr, __be16 sport,
247 const struct in6_addr *daddr, __be16 dport,
248 int dif, struct udp_table *udptable)
249{
250 struct sock *sk, *result;
251 struct hlist_nulls_node *node;
252 unsigned short hnum = ntohs(dport);
253 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
254 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
255 int score, badness;
256
257 rcu_read_lock();
258 if (hslot->count > 10) {
259 hash2 = udp6_portaddr_hash(net, daddr, hnum);
260 slot2 = hash2 & udptable->mask;
261 hslot2 = &udptable->hash2[slot2];
262 if (hslot->count < hslot2->count)
263 goto begin;
264
265 result = udp6_lib_lookup2(net, saddr, sport,
266 daddr, hnum, dif,
267 hslot2, slot2);
268 if (!result) {
269 hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
270 slot2 = hash2 & udptable->mask;
271 hslot2 = &udptable->hash2[slot2];
272 if (hslot->count < hslot2->count)
273 goto begin;
274
275 result = udp6_lib_lookup2(net, saddr, sport,
276 &in6addr_any, hnum, dif,
277 hslot2, slot2);
278 }
279 rcu_read_unlock();
280 return result;
281 }
282begin:
283 result = NULL;
284 badness = -1;
285 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
286 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
287 if (score > badness) {
288 result = sk;
289 badness = score;
290 }
291 }
292 /*
293 * if the nulls value we got at the end of this lookup is
294 * not the expected one, we must restart lookup.
295 * We probably met an item that was moved to another chain.
296 */
297 if (get_nulls_value(node) != slot)
298 goto begin;
299
300 if (result) {
301 if (unlikely(!atomic_inc_not_zero_hint(&result->sk_refcnt, 2)))
302 result = NULL;
303 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
304 daddr, dport, dif) < badness)) {
305 sock_put(result);
306 goto begin;
307 }
308 }
309 rcu_read_unlock();
310 return result;
311}
312EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
313
314static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
315 __be16 sport, __be16 dport,
316 struct udp_table *udptable)
317{
318 struct sock *sk;
319 const struct ipv6hdr *iph = ipv6_hdr(skb);
320
321 if (unlikely(sk = skb_steal_sock(skb)))
322 return sk;
323 return __udp6_lib_lookup(dev_net(skb_dst(skb)->dev), &iph->saddr, sport,
324 &iph->daddr, dport, inet6_iif(skb),
325 udptable);
326}
327
328struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
329 const struct in6_addr *daddr, __be16 dport, int dif)
330{
331 return __udp6_lib_lookup(net, saddr, sport, daddr, dport, dif, &udp_table);
332}
333EXPORT_SYMBOL_GPL(udp6_lib_lookup);
334
335
336/*
337 * This should be easy, if there is something there we
338 * return it, otherwise we block.
339 */
340
341int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
342 struct msghdr *msg, size_t len,
343 int noblock, int flags, int *addr_len)
344{
345 struct ipv6_pinfo *np = inet6_sk(sk);
346 struct inet_sock *inet = inet_sk(sk);
347 struct sk_buff *skb;
348 unsigned int ulen, copied;
349 int peeked, off = 0;
350 int err;
351 int is_udplite = IS_UDPLITE(sk);
352 bool checksum_valid = false;
353 int is_udp4;
354 bool slow;
355
356 if (flags & MSG_ERRQUEUE)
357 return ipv6_recv_error(sk, msg, len, addr_len);
358
359 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
360 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
361
362try_again:
363 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
364 &peeked, &off, &err);
365 if (!skb)
366 goto out;
367
368 ulen = skb->len - sizeof(struct udphdr);
369 copied = len;
370 if (copied > ulen)
371 copied = ulen;
372 else if (copied < ulen)
373 msg->msg_flags |= MSG_TRUNC;
374
375 is_udp4 = (skb->protocol == htons(ETH_P_IP));
376
377 /*
378 * If checksum is needed at all, try to do it while copying the
379 * data. If the data is truncated, or if we only want a partial
380 * coverage checksum (UDP-Lite), do it before the copy.
381 */
382
383 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
384 checksum_valid = !udp_lib_checksum_complete(skb);
385 if (!checksum_valid)
386 goto csum_copy_err;
387 }
388
389 if (checksum_valid || skb_csum_unnecessary(skb))
390 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
391 msg->msg_iov, copied );
392 else {
393 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
394 if (err == -EINVAL)
395 goto csum_copy_err;
396 }
397 if (err)
398 goto out_free;
399
400 if (!peeked) {
401 if (is_udp4)
402 UDP_INC_STATS_USER(sock_net(sk),
403 UDP_MIB_INDATAGRAMS, is_udplite);
404 else
405 UDP6_INC_STATS_USER(sock_net(sk),
406 UDP_MIB_INDATAGRAMS, is_udplite);
407 }
408
409 sock_recv_ts_and_drops(msg, sk, skb);
410
411 /* Copy the address. */
412 if (msg->msg_name) {
413 struct sockaddr_in6 *sin6;
414
415 sin6 = (struct sockaddr_in6 *) msg->msg_name;
416 sin6->sin6_family = AF_INET6;
417 sin6->sin6_port = udp_hdr(skb)->source;
418 sin6->sin6_flowinfo = 0;
419 sin6->sin6_scope_id = 0;
420
421 if (is_udp4)
422 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
423 &sin6->sin6_addr);
424 else {
425 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
426 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
427 sin6->sin6_scope_id = IP6CB(skb)->iif;
428 }
429 *addr_len = sizeof(*sin6);
430 }
431 if (is_udp4) {
432 if (inet->cmsg_flags)
433 ip_cmsg_recv(msg, skb);
434 } else {
435 if (np->rxopt.all)
436 datagram_recv_ctl(sk, msg, skb);
437 }
438
439 err = copied;
440 if (flags & MSG_TRUNC)
441 err = ulen;
442
443out_free:
444 skb_free_datagram_locked(sk, skb);
445out:
446 return err;
447
448csum_copy_err:
449 slow = lock_sock_fast(sk);
450 if (!skb_kill_datagram(sk, skb, flags)) {
451 if (is_udp4)
452 UDP_INC_STATS_USER(sock_net(sk),
453 UDP_MIB_INERRORS, is_udplite);
454 else
455 UDP6_INC_STATS_USER(sock_net(sk),
456 UDP_MIB_INERRORS, is_udplite);
457 }
458 unlock_sock_fast(sk, slow);
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
466void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
467 u8 type, u8 code, int offset, __be32 info,
468 struct udp_table *udptable)
469{
470 struct ipv6_pinfo *np;
471 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
472 const struct in6_addr *saddr = &hdr->saddr;
473 const struct in6_addr *daddr = &hdr->daddr;
474 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
475 struct sock *sk;
476 int err;
477
478 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
479 saddr, uh->source, inet6_iif(skb), udptable);
480 if (sk == NULL)
481 return;
482
483 np = inet6_sk(sk);
484
485 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
486 goto out;
487
488 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
489 goto out;
490
491 if (np->recverr)
492 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
493
494 sk->sk_err = err;
495 sk->sk_error_report(sk);
496out:
497 sock_put(sk);
498}
499
500static __inline__ void udpv6_err(struct sk_buff *skb,
501 struct inet6_skb_parm *opt, u8 type,
502 u8 code, int offset, __be32 info )
503{
504 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
505}
506
507int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
508{
509 struct udp_sock *up = udp_sk(sk);
510 int rc;
511 int is_udplite = IS_UDPLITE(sk);
512
513 if (!ipv6_addr_any(&inet6_sk(sk)->daddr))
514 sock_rps_save_rxhash(sk, skb);
515
516 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
517 goto drop;
518
519 /*
520 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
521 */
522 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
523
524 if (up->pcrlen == 0) { /* full coverage was set */
525 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
526 " %d while full coverage %d requested\n",
527 UDP_SKB_CB(skb)->cscov, skb->len);
528 goto drop;
529 }
530 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
531 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
532 "too small, need min %d\n",
533 UDP_SKB_CB(skb)->cscov, up->pcrlen);
534 goto drop;
535 }
536 }
537
538 if (rcu_access_pointer(sk->sk_filter)) {
539 if (udp_lib_checksum_complete(skb))
540 goto drop;
541 }
542
543 skb_dst_drop(skb);
544 rc = sock_queue_rcv_skb(sk, skb);
545 if (rc < 0) {
546 /* Note that an ENOMEM error is charged twice */
547 if (rc == -ENOMEM)
548 UDP6_INC_STATS_BH(sock_net(sk),
549 UDP_MIB_RCVBUFERRORS, is_udplite);
550 goto drop_no_sk_drops_inc;
551 }
552
553 return 0;
554drop:
555 atomic_inc(&sk->sk_drops);
556drop_no_sk_drops_inc:
557 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
558 kfree_skb(skb);
559 return -1;
560}
561
562static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
563 __be16 loc_port, const struct in6_addr *loc_addr,
564 __be16 rmt_port, const struct in6_addr *rmt_addr,
565 int dif)
566{
567 struct hlist_nulls_node *node;
568 struct sock *s = sk;
569 unsigned short num = ntohs(loc_port);
570
571 sk_nulls_for_each_from(s, node) {
572 struct inet_sock *inet = inet_sk(s);
573
574 if (!net_eq(sock_net(s), net))
575 continue;
576
577 if (udp_sk(s)->udp_port_hash == num &&
578 s->sk_family == PF_INET6) {
579 struct ipv6_pinfo *np = inet6_sk(s);
580 if (inet->inet_dport) {
581 if (inet->inet_dport != rmt_port)
582 continue;
583 }
584 if (!ipv6_addr_any(&np->daddr) &&
585 !ipv6_addr_equal(&np->daddr, rmt_addr))
586 continue;
587
588 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
589 continue;
590
591 if (!ipv6_addr_any(&np->rcv_saddr)) {
592 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
593 continue;
594 }
595 if (!inet6_mc_check(s, loc_addr, rmt_addr))
596 continue;
597 return s;
598 }
599 }
600 return NULL;
601}
602
603static void flush_stack(struct sock **stack, unsigned int count,
604 struct sk_buff *skb, unsigned int final)
605{
606 unsigned int i;
607 struct sock *sk;
608 struct sk_buff *skb1;
609
610 for (i = 0; i < count; i++) {
611 skb1 = (i == final) ? skb : skb_clone(skb, GFP_ATOMIC);
612
613 sk = stack[i];
614 if (skb1) {
615 if (sk_rcvqueues_full(sk, skb1)) {
616 kfree_skb(skb1);
617 goto drop;
618 }
619 bh_lock_sock(sk);
620 if (!sock_owned_by_user(sk))
621 udpv6_queue_rcv_skb(sk, skb1);
622 else if (sk_add_backlog(sk, skb1)) {
623 kfree_skb(skb1);
624 bh_unlock_sock(sk);
625 goto drop;
626 }
627 bh_unlock_sock(sk);
628 continue;
629 }
630drop:
631 atomic_inc(&sk->sk_drops);
632 UDP6_INC_STATS_BH(sock_net(sk),
633 UDP_MIB_RCVBUFERRORS, IS_UDPLITE(sk));
634 UDP6_INC_STATS_BH(sock_net(sk),
635 UDP_MIB_INERRORS, IS_UDPLITE(sk));
636 }
637}
638/*
639 * Note: called only from the BH handler context,
640 * so we don't need to lock the hashes.
641 */
642static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
643 const struct in6_addr *saddr, const struct in6_addr *daddr,
644 struct udp_table *udptable)
645{
646 struct sock *sk, *stack[256 / sizeof(struct sock *)];
647 const struct udphdr *uh = udp_hdr(skb);
648 struct udp_hslot *hslot = udp_hashslot(udptable, net, ntohs(uh->dest));
649 int dif;
650 unsigned int i, count = 0;
651
652 spin_lock(&hslot->lock);
653 sk = sk_nulls_head(&hslot->head);
654 dif = inet6_iif(skb);
655 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
656 while (sk) {
657 stack[count++] = sk;
658 sk = udp_v6_mcast_next(net, sk_nulls_next(sk), uh->dest, daddr,
659 uh->source, saddr, dif);
660 if (unlikely(count == ARRAY_SIZE(stack))) {
661 if (!sk)
662 break;
663 flush_stack(stack, count, skb, ~0);
664 count = 0;
665 }
666 }
667 /*
668 * before releasing the lock, we must take reference on sockets
669 */
670 for (i = 0; i < count; i++)
671 sock_hold(stack[i]);
672
673 spin_unlock(&hslot->lock);
674
675 if (count) {
676 flush_stack(stack, count, skb, count - 1);
677
678 for (i = 0; i < count; i++)
679 sock_put(stack[i]);
680 } else {
681 kfree_skb(skb);
682 }
683 return 0;
684}
685
686static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
687 int proto)
688{
689 int err;
690
691 UDP_SKB_CB(skb)->partial_cov = 0;
692 UDP_SKB_CB(skb)->cscov = skb->len;
693
694 if (proto == IPPROTO_UDPLITE) {
695 err = udplite_checksum_init(skb, uh);
696 if (err)
697 return err;
698 }
699
700 if (uh->check == 0) {
701 /* RFC 2460 section 8.1 says that we SHOULD log
702 this error. Well, it is reasonable.
703 */
704 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
705 return 1;
706 }
707 if (skb->ip_summed == CHECKSUM_COMPLETE &&
708 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
709 skb->len, proto, skb->csum))
710 skb->ip_summed = CHECKSUM_UNNECESSARY;
711
712 if (!skb_csum_unnecessary(skb))
713 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
714 &ipv6_hdr(skb)->daddr,
715 skb->len, proto, 0));
716
717 return 0;
718}
719
720extern void fast_sk_add_ct(struct sk_buff * skb,struct sock *sk);
lh758261d2023-07-13 05:52:04 -0700721int udp6_zero_csum_ctrl = 1;
722module_param(udp6_zero_csum_ctrl,int,0644);
lh9ed821d2023-04-07 01:36:19 -0700723int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
724 int proto)
725{
726 struct net *net = dev_net(skb->dev);
727 struct sock *sk;
728 struct udphdr *uh;
729 const struct in6_addr *saddr, *daddr;
730 u32 ulen = 0;
731 struct nf_conn *ct = (struct nf_conn *)skb->nfct;
732
733 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
734 goto discard;
735
736 saddr = &ipv6_hdr(skb)->saddr;
737 daddr = &ipv6_hdr(skb)->daddr;
738 uh = udp_hdr(skb);
739
740 ulen = ntohs(uh->len);
741 if (ulen > skb->len)
742 goto short_packet;
743
744 if (proto == IPPROTO_UDP) {
745 /* UDP validates ulen. */
746
747 /* Check for jumbo payload */
748 if (ulen == 0)
749 ulen = skb->len;
750
751 if (ulen < sizeof(*uh))
752 goto short_packet;
753
754 if (ulen < skb->len) {
755 if (pskb_trim_rcsum(skb, ulen))
756 goto short_packet;
757 saddr = &ipv6_hdr(skb)->saddr;
758 daddr = &ipv6_hdr(skb)->daddr;
759 uh = udp_hdr(skb);
760 }
761 }
762
lh758261d2023-07-13 05:52:04 -0700763 if (!(udp6_zero_csum_ctrl && ((IPPROTO_UDP == proto)&&(0 == uh->check))) && udp6_csum_init(skb, uh, proto))
lh9ed821d2023-04-07 01:36:19 -0700764 goto discard;
765
766 /*
767 * Multicast receive code
768 */
769 if (ipv6_addr_is_multicast(daddr))
770 return __udp6_lib_mcast_deliver(net, skb,
771 saddr, daddr, udptable);
772
773 /* Unicast */
774
775 /*
776 * check socket cache ... must talk to Alan about his plans
777 * for sock caches... i'll skip this for now.
778 */
779
780 if (skb->isFastlocal && ct && ct->fast_ct.isFast == FAST_CT_LOCAL6)
781 {
782 sk = ct->fast_ct.sk;
783 }
784 else
785 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
786
787 if (sk == NULL) {
788 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
789 goto discard;
790
791 if (fast_local6_proc && fastnat_level != FAST_CLOSE && fastnat_level != FAST_CLOSE_KEEP_LINK)
792 nf_reset(skb);
793
794 if (udp_lib_checksum_complete(skb))
795 goto discard;
796 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
797 proto == IPPROTO_UDPLITE);
798
799 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
800
801 kfree_skb(skb);
802 return 0;
803 }
804
805 if (skb->isFastlocal == 0)
806 fast_sk_add_ct(skb, sk);
807
808 /*¿½×Ôip6_input_finish,ÑÓ³ÙÊÍ·Åct*/
809 if (fast_local6_proc && fastnat_level != FAST_CLOSE && fastnat_level != FAST_CLOSE_KEEP_LINK)
810 nf_reset(skb);
811
812 /* deliver */
813 if (sk_rcvqueues_full(sk, skb)) {
814 sock_put(sk);
815 goto discard;
816 }
817 bh_lock_sock(sk);
818 if (!sock_owned_by_user(sk))
819 udpv6_queue_rcv_skb(sk, skb);
820 else if (sk_add_backlog(sk, skb)) {
821 atomic_inc(&sk->sk_drops);
822 bh_unlock_sock(sk);
823 sock_put(sk);
824 goto discard;
825 }
826 bh_unlock_sock(sk);
827 sock_put(sk);
828 return 0;
829
830short_packet:
831 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
832 proto == IPPROTO_UDPLITE ? "-Lite" : "",
833 saddr,
834 ntohs(uh->source),
835 ulen,
836 skb->len,
837 daddr,
838 ntohs(uh->dest));
839
840discard:
841 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
842 kfree_skb(skb);
843 return 0;
844}
845
846int udpv6_rcv(struct sk_buff *skb)
847{
848 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
849}
850
851/*
852 * Throw away all pending data and cancel the corking. Socket is locked.
853 */
854static void udp_v6_flush_pending_frames(struct sock *sk)
855{
856 struct udp_sock *up = udp_sk(sk);
857
858 if (up->pending == AF_INET)
859 udp_flush_pending_frames(sk);
860 else if (up->pending) {
861 up->len = 0;
862 up->pending = 0;
863 ip6_flush_pending_frames(sk);
864 }
865}
866
867/**
868 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
869 * @sk: socket we are sending on
870 * @skb: sk_buff containing the filled-in UDP header
871 * (checksum field must be zeroed out)
872 */
873static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
874 const struct in6_addr *saddr,
875 const struct in6_addr *daddr, int len)
876{
877 unsigned int offset;
878 struct udphdr *uh = udp_hdr(skb);
879 __wsum csum = 0;
880
881 if (skb_queue_len(&sk->sk_write_queue) == 1) {
882 /* Only one fragment on the socket. */
883 skb->csum_start = skb_transport_header(skb) - skb->head;
884 skb->csum_offset = offsetof(struct udphdr, check);
885 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
886 } else {
887 /*
888 * HW-checksum won't work as there are two or more
889 * fragments on the socket so that all csums of sk_buffs
890 * should be together
891 */
892 offset = skb_transport_offset(skb);
893 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
894
895 skb->ip_summed = CHECKSUM_NONE;
896
897 skb_queue_walk(&sk->sk_write_queue, skb) {
898 csum = csum_add(csum, skb->csum);
899 }
900
901 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
902 csum);
903 if (uh->check == 0)
904 uh->check = CSUM_MANGLED_0;
905 }
906}
907
908/*
909 * Sending
910 */
911
912static int udp_v6_push_pending_frames(struct sock *sk)
913{
914 struct sk_buff *skb;
915 struct udphdr *uh;
916 struct udp_sock *up = udp_sk(sk);
917 struct inet_sock *inet = inet_sk(sk);
918 struct flowi6 *fl6;
919 int err = 0;
920 int is_udplite = IS_UDPLITE(sk);
921 __wsum csum = 0;
922
923 if (up->pending == AF_INET)
924 return udp_push_pending_frames(sk);
925
926 fl6 = &inet->cork.fl.u.ip6;
927
928 /* Grab the skbuff where UDP header space exists. */
929 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
930 goto out;
931
932 /*
933 * Create a UDP header
934 */
935 uh = udp_hdr(skb);
936 uh->source = fl6->fl6_sport;
937 uh->dest = fl6->fl6_dport;
938 uh->len = htons(up->len);
939 uh->check = 0;
940
941 if (is_udplite)
942 csum = udplite_csum_outgoing(sk, skb);
943 else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
944 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr,
945 up->len);
946 goto send;
947 } else
948 csum = udp_csum_outgoing(sk, skb);
949
950 /* add protocol-dependent pseudo-header */
951 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
952 up->len, fl6->flowi6_proto, csum);
953 if (uh->check == 0)
954 uh->check = CSUM_MANGLED_0;
955
956send:
957 err = ip6_push_pending_frames(sk);
958 if (err) {
959 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
960 UDP6_INC_STATS_USER(sock_net(sk),
961 UDP_MIB_SNDBUFERRORS, is_udplite);
962 err = 0;
963 }
964 } else
965 UDP6_INC_STATS_USER(sock_net(sk),
966 UDP_MIB_OUTDATAGRAMS, is_udplite);
967out:
968 up->len = 0;
969 up->pending = 0;
970 return err;
971}
972
973int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
974 struct msghdr *msg, size_t len)
975{
976 struct ipv6_txoptions opt_space;
977 struct udp_sock *up = udp_sk(sk);
978 struct inet_sock *inet = inet_sk(sk);
979 struct ipv6_pinfo *np = inet6_sk(sk);
980 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
981 struct in6_addr *daddr, *final_p, final;
982 struct ipv6_txoptions *opt = NULL;
983 struct ipv6_txoptions *opt_to_free = NULL;//CVE-2016-3841
984 struct ip6_flowlabel *flowlabel = NULL;
985 struct flowi6 fl6;
986 struct dst_entry *dst;
987 int addr_len = msg->msg_namelen;
988 int ulen = len;
989 int hlimit = -1;
990 int tclass = -1;
991 int dontfrag = -1;
992 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
993 int err;
994 int connected = 0;
995 int is_udplite = IS_UDPLITE(sk);
996 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
997
998 /* destination address check */
999 if (sin6) {
1000 if (addr_len < offsetof(struct sockaddr, sa_data))
1001 return -EINVAL;
1002
1003 switch (sin6->sin6_family) {
1004 case AF_INET6:
1005 if (addr_len < SIN6_LEN_RFC2133)
1006 return -EINVAL;
1007 daddr = &sin6->sin6_addr;
1008 break;
1009 case AF_INET:
1010 goto do_udp_sendmsg;
1011 case AF_UNSPEC:
1012 msg->msg_name = sin6 = NULL;
1013 msg->msg_namelen = addr_len = 0;
1014 daddr = NULL;
1015 break;
1016 default:
1017 return -EINVAL;
1018 }
1019 } else if (!up->pending) {
1020 if (sk->sk_state != TCP_ESTABLISHED)
1021 return -EDESTADDRREQ;
1022 daddr = &np->daddr;
1023 } else
1024 daddr = NULL;
1025
1026 if (daddr) {
1027 if (ipv6_addr_v4mapped(daddr)) {
1028 struct sockaddr_in sin;
1029 sin.sin_family = AF_INET;
1030 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1031 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1032 msg->msg_name = &sin;
1033 msg->msg_namelen = sizeof(sin);
1034do_udp_sendmsg:
1035 if (__ipv6_only_sock(sk))
1036 return -ENETUNREACH;
1037 return udp_sendmsg(iocb, sk, msg, len);
1038 }
1039 }
1040
1041 if (up->pending == AF_INET)
1042 return udp_sendmsg(iocb, sk, msg, len);
1043
1044 /* Rough check on arithmetic overflow,
1045 better check is made in ip6_append_data().
1046 */
1047 if (len > INT_MAX - sizeof(struct udphdr))
1048 return -EMSGSIZE;
1049
1050 if (up->pending) {
1051 /*
1052 * There are pending frames.
1053 * The socket lock must be held while it's corked.
1054 */
1055 lock_sock(sk);
1056 if (likely(up->pending)) {
1057 if (unlikely(up->pending != AF_INET6)) {
1058 release_sock(sk);
1059 return -EAFNOSUPPORT;
1060 }
1061 dst = NULL;
1062 goto do_append_data;
1063 }
1064 release_sock(sk);
1065 }
1066 ulen += sizeof(struct udphdr);
1067
1068 memset(&fl6, 0, sizeof(fl6));
1069
1070 if (sin6) {
1071 if (sin6->sin6_port == 0)
1072 return -EINVAL;
1073
1074 fl6.fl6_dport = sin6->sin6_port;
1075 daddr = &sin6->sin6_addr;
1076
1077 if (np->sndflow) {
1078 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1079 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1080 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1081 if (flowlabel == NULL)
1082 return -EINVAL;
1083 daddr = &flowlabel->dst;
1084 }
1085 }
1086
1087 /*
1088 * Otherwise it will be difficult to maintain
1089 * sk->sk_dst_cache.
1090 */
1091 if (sk->sk_state == TCP_ESTABLISHED &&
1092 ipv6_addr_equal(daddr, &np->daddr))
1093 daddr = &np->daddr;
1094
1095 if (addr_len >= sizeof(struct sockaddr_in6) &&
1096 sin6->sin6_scope_id &&
1097 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
1098 fl6.flowi6_oif = sin6->sin6_scope_id;
1099 } else {
1100 if (sk->sk_state != TCP_ESTABLISHED)
1101 return -EDESTADDRREQ;
1102
1103 fl6.fl6_dport = inet->inet_dport;
1104 daddr = &np->daddr;
1105 fl6.flowlabel = np->flow_label;
1106 connected = 1;
1107 }
1108
1109 if (!fl6.flowi6_oif)
1110 fl6.flowi6_oif = sk->sk_bound_dev_if;
1111
1112 if (!fl6.flowi6_oif)
1113 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
1114
1115 fl6.flowi6_mark = sk->sk_mark;
1116
1117 if (msg->msg_controllen) {
1118 opt = &opt_space;
1119 memset(opt, 0, sizeof(struct ipv6_txoptions));
1120 opt->tot_len = sizeof(*opt);
1121
1122 err = datagram_send_ctl(sock_net(sk), sk, msg, &fl6, opt,
1123 &hlimit, &tclass, &dontfrag);
1124 if (err < 0) {
1125 fl6_sock_release(flowlabel);
1126 return err;
1127 }
1128 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1129 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
1130 if (flowlabel == NULL)
1131 return -EINVAL;
1132 }
1133 if (!(opt->opt_nflen|opt->opt_flen))
1134 opt = NULL;
1135 connected = 0;
1136 }
1137 /*//CVE-2016-3841
1138 if (opt == NULL)
1139 opt = np->opt;*/
1140 if (!opt) {
1141 opt = txopt_get(np);
1142 opt_to_free = opt;
1143 }
1144 //CVE-2016-3841
1145 if (flowlabel)
1146 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1147 opt = ipv6_fixup_options(&opt_space, opt);
1148
1149 fl6.flowi6_proto = sk->sk_protocol;
1150 if (!ipv6_addr_any(daddr))
1151 fl6.daddr = *daddr;
1152 else
1153 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1154 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
1155 fl6.saddr = np->saddr;
1156 fl6.fl6_sport = inet->inet_sport;
1157
1158 final_p = fl6_update_dst(&fl6, opt, &final);
1159 if (final_p)
1160 connected = 0;
1161
1162 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1163 fl6.flowi6_oif = np->mcast_oif;
1164 connected = 0;
1165 } else if (!fl6.flowi6_oif)
1166 fl6.flowi6_oif = np->ucast_oif;
1167
1168 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
1169
1170 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, true);
1171 if (IS_ERR(dst)) {
1172 err = PTR_ERR(dst);
1173 dst = NULL;
1174 goto out;
1175 }
1176
1177 if (hlimit < 0) {
1178 if (ipv6_addr_is_multicast(&fl6.daddr))
1179 hlimit = np->mcast_hops;
1180 else
1181 hlimit = np->hop_limit;
1182 if (hlimit < 0)
1183 hlimit = ip6_dst_hoplimit(dst);
1184 }
1185
1186 if (tclass < 0)
1187 tclass = np->tclass;
1188
1189 if (dontfrag < 0)
1190 dontfrag = np->dontfrag;
1191
1192 if (msg->msg_flags&MSG_CONFIRM)
1193 goto do_confirm;
1194back_from_confirm:
1195
1196 lock_sock(sk);
1197 if (unlikely(up->pending)) {
1198 /* The socket is already corked while preparing it. */
1199 /* ... which is an evident application bug. --ANK */
1200 release_sock(sk);
1201
1202 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
1203 err = -EINVAL;
1204 goto out;
1205 }
1206
1207 up->pending = AF_INET6;
1208
1209do_append_data:
1210 up->len += ulen;
1211 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1212 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
1213 sizeof(struct udphdr), hlimit, tclass, opt, &fl6,
1214 (struct rt6_info*)dst,
1215 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, dontfrag);
1216 if (err)
1217 udp_v6_flush_pending_frames(sk);
1218 else if (!corkreq)
1219 err = udp_v6_push_pending_frames(sk);
1220 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1221 up->pending = 0;
1222
1223 if (dst) {
1224 if (connected) {
1225 ip6_dst_store(sk, dst,
1226 ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
1227 &np->daddr : NULL,
1228#ifdef CONFIG_IPV6_SUBTREES
1229 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
1230 &np->saddr :
1231#endif
1232 NULL);
1233 } else {
1234 dst_release(dst);
1235 }
1236 dst = NULL;
1237 }
1238
1239 if (err > 0)
1240 err = np->recverr ? net_xmit_errno(err) : 0;
1241 release_sock(sk);
1242out:
1243 dst_release(dst);
1244 fl6_sock_release(flowlabel);
1245 txopt_put(opt_to_free);//CVE-2016-3841
1246 if (!err)
1247 return len;
1248 /*
1249 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1250 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1251 * we don't have a good statistic (IpOutDiscards but it can be too many
1252 * things). We could add another new stat but at least for now that
1253 * seems like overkill.
1254 */
1255 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
1256 UDP6_INC_STATS_USER(sock_net(sk),
1257 UDP_MIB_SNDBUFERRORS, is_udplite);
1258 }
1259 return err;
1260
1261do_confirm:
1262 dst_confirm(dst);
1263 if (!(msg->msg_flags&MSG_PROBE) || len)
1264 goto back_from_confirm;
1265 err = 0;
1266 goto out;
1267}
1268
1269void udpv6_destroy_sock(struct sock *sk)
1270{
1271 lock_sock(sk);
1272 udp_v6_flush_pending_frames(sk);
1273 release_sock(sk);
1274
1275 inet6_destroy_sock(sk);
1276}
1277
1278/*
1279 * Socket option code for UDP
1280 */
1281int udpv6_setsockopt(struct sock *sk, int level, int optname,
1282 char __user *optval, unsigned int optlen)
1283{
1284 if (level == SOL_UDP || level == SOL_UDPLITE)
1285 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1286 udp_v6_push_pending_frames);
1287 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1288}
1289
1290#ifdef CONFIG_COMPAT
1291int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1292 char __user *optval, unsigned int optlen)
1293{
1294 if (level == SOL_UDP || level == SOL_UDPLITE)
1295 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1296 udp_v6_push_pending_frames);
1297 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1298}
1299#endif
1300
1301int udpv6_getsockopt(struct sock *sk, int level, int optname,
1302 char __user *optval, int __user *optlen)
1303{
1304 if (level == SOL_UDP || level == SOL_UDPLITE)
1305 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1306 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1307}
1308
1309#ifdef CONFIG_COMPAT
1310int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1311 char __user *optval, int __user *optlen)
1312{
1313 if (level == SOL_UDP || level == SOL_UDPLITE)
1314 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1315 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1316}
1317#endif
1318
1319static int udp6_ufo_send_check(struct sk_buff *skb)
1320{
1321 const struct ipv6hdr *ipv6h;
1322 struct udphdr *uh;
1323
1324 if (!pskb_may_pull(skb, sizeof(*uh)))
1325 return -EINVAL;
1326
1327 ipv6h = ipv6_hdr(skb);
1328 uh = udp_hdr(skb);
1329
1330 uh->check = ~csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr, skb->len,
1331 IPPROTO_UDP, 0);
1332 skb->csum_start = skb_transport_header(skb) - skb->head;
1333 skb->csum_offset = offsetof(struct udphdr, check);
1334 skb->ip_summed = CHECKSUM_PARTIAL;
1335 return 0;
1336}
1337
1338static struct sk_buff *udp6_ufo_fragment(struct sk_buff *skb,
1339 netdev_features_t features)
1340{
1341 struct sk_buff *segs = ERR_PTR(-EINVAL);
1342 unsigned int mss;
1343 unsigned int unfrag_ip6hlen, unfrag_len;
1344 struct frag_hdr *fptr;
1345 u8 *mac_start, *prevhdr;
1346 u8 nexthdr;
1347 u8 frag_hdr_sz = sizeof(struct frag_hdr);
1348 int offset;
1349 __wsum csum;
1350
1351 mss = skb_shinfo(skb)->gso_size;
1352 if (unlikely(skb->len <= mss))
1353 goto out;
1354
1355 if (skb_gso_ok(skb, features | NETIF_F_GSO_ROBUST)) {
1356 /* Packet is from an untrusted source, reset gso_segs. */
1357 int type = skb_shinfo(skb)->gso_type;
1358
1359 if (unlikely(type & ~(SKB_GSO_UDP | SKB_GSO_DODGY) ||
1360 !(type & (SKB_GSO_UDP))))
1361 goto out;
1362
1363 skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(skb->len, mss);
1364
1365 segs = NULL;
1366 goto out;
1367 }
1368
1369 /* Do software UFO. Complete and fill in the UDP checksum as HW cannot
1370 * do checksum of UDP packets sent as multiple IP fragments.
1371 */
1372 offset = skb_checksum_start_offset(skb);
1373 csum = skb_checksum(skb, offset, skb->len- offset, 0);
1374 offset += skb->csum_offset;
1375 *(__sum16 *)(skb->data + offset) = csum_fold(csum);
1376 skb->ip_summed = CHECKSUM_NONE;
1377
1378 /* Check if there is enough headroom to insert fragment header. */
1379 if ((skb_mac_header(skb) < skb->head + frag_hdr_sz) &&
1380 pskb_expand_head(skb, frag_hdr_sz, 0, GFP_ATOMIC))
1381 goto out;
1382
1383 /* Find the unfragmentable header and shift it left by frag_hdr_sz
1384 * bytes to insert fragment header.
1385 */
1386 unfrag_ip6hlen = ip6_find_1stfragopt(skb, &prevhdr);
1387 if (unfrag_ip6hlen < 0)//CVE-2017-9074
1388 return ERR_PTR(unfrag_ip6hlen);
1389 nexthdr = *prevhdr;
1390 *prevhdr = NEXTHDR_FRAGMENT;
1391 unfrag_len = skb_network_header(skb) - skb_mac_header(skb) +
1392 unfrag_ip6hlen;
1393 mac_start = skb_mac_header(skb);
1394 memmove(mac_start-frag_hdr_sz, mac_start, unfrag_len);
1395
1396 skb->mac_header -= frag_hdr_sz;
1397 skb->network_header -= frag_hdr_sz;
1398
1399 fptr = (struct frag_hdr *)(skb_network_header(skb) + unfrag_ip6hlen);
1400 fptr->nexthdr = nexthdr;
1401 fptr->reserved = 0;
1402 fptr->identification = skb_shinfo(skb)->ip6_frag_id;
1403
1404 /* Fragment the skb. ipv6 header and the remaining fields of the
1405 * fragment header are updated in ipv6_gso_segment()
1406 */
1407 segs = skb_segment(skb, features);
1408
1409out:
1410 return segs;
1411}
1412
1413static const struct inet6_protocol udpv6_protocol = {
1414 .handler = udpv6_rcv,
1415 .err_handler = udpv6_err,
1416 .gso_send_check = udp6_ufo_send_check,
1417 .gso_segment = udp6_ufo_fragment,
1418 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1419};
1420
1421/* ------------------------------------------------------------------------ */
1422#ifdef CONFIG_PROC_FS
1423
1424static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1425{
1426 struct inet_sock *inet = inet_sk(sp);
1427 struct ipv6_pinfo *np = inet6_sk(sp);
1428 const struct in6_addr *dest, *src;
1429 __u16 destp, srcp;
1430
1431 dest = &np->daddr;
1432 src = &np->rcv_saddr;
1433 destp = ntohs(inet->inet_dport);
1434 srcp = ntohs(inet->inet_sport);
1435 seq_printf(seq,
1436 "%5d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1437 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %pK %d\n",
1438 bucket,
1439 src->s6_addr32[0], src->s6_addr32[1],
1440 src->s6_addr32[2], src->s6_addr32[3], srcp,
1441 dest->s6_addr32[0], dest->s6_addr32[1],
1442 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1443 sp->sk_state,
1444 sk_wmem_alloc_get(sp),
1445 sk_rmem_alloc_get(sp),
1446 0, 0L, 0,
1447 sock_i_uid(sp), 0,
1448 sock_i_ino(sp),
1449 atomic_read(&sp->sk_refcnt), sp,
1450 atomic_read(&sp->sk_drops));
1451}
1452
1453int udp6_seq_show(struct seq_file *seq, void *v)
1454{
1455 if (v == SEQ_START_TOKEN)
1456 seq_printf(seq,
1457 " sl "
1458 "local_address "
1459 "remote_address "
1460 "st tx_queue rx_queue tr tm->when retrnsmt"
1461 " uid timeout inode ref pointer drops\n");
1462 else
1463 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1464 return 0;
1465}
1466
1467static const struct file_operations udp6_afinfo_seq_fops = {
1468 .owner = THIS_MODULE,
1469 .open = udp_seq_open,
1470 .read = seq_read,
1471 .llseek = seq_lseek,
1472 .release = seq_release_net
1473};
1474
1475static struct udp_seq_afinfo udp6_seq_afinfo = {
1476 .name = "udp6",
1477 .family = AF_INET6,
1478 .udp_table = &udp_table,
1479 .seq_fops = &udp6_afinfo_seq_fops,
1480 .seq_ops = {
1481 .show = udp6_seq_show,
1482 },
1483};
1484
1485int __net_init udp6_proc_init(struct net *net)
1486{
1487 return udp_proc_register(net, &udp6_seq_afinfo);
1488}
1489
1490void udp6_proc_exit(struct net *net) {
1491 udp_proc_unregister(net, &udp6_seq_afinfo);
1492}
1493#endif /* CONFIG_PROC_FS */
1494
1495void udp_v6_clear_sk(struct sock *sk, int size)
1496{
1497 struct inet_sock *inet = inet_sk(sk);
1498
1499 /* we do not want to clear pinet6 field, because of RCU lookups */
1500 sk_prot_clear_portaddr_nulls(sk, offsetof(struct inet_sock, pinet6));
1501
1502 size -= offsetof(struct inet_sock, pinet6) + sizeof(inet->pinet6);
1503 memset(&inet->pinet6 + 1, 0, size);
1504}
1505
1506/* ------------------------------------------------------------------------ */
1507
1508struct proto udpv6_prot = {
1509 .name = "UDPv6",
1510 .owner = THIS_MODULE,
1511 .close = udp_lib_close,
1512 .connect = ip6_datagram_connect,
1513 .disconnect = udp_disconnect,
1514 .ioctl = udp_ioctl,
1515 .destroy = udpv6_destroy_sock,
1516 .setsockopt = udpv6_setsockopt,
1517 .getsockopt = udpv6_getsockopt,
1518 .sendmsg = udpv6_sendmsg,
1519 .recvmsg = udpv6_recvmsg,
1520 .backlog_rcv = udpv6_queue_rcv_skb,
1521 .hash = udp_lib_hash,
1522 .unhash = udp_lib_unhash,
1523 .rehash = udp_v6_rehash,
1524 .get_port = udp_v6_get_port,
1525 .memory_allocated = &udp_memory_allocated,
1526 .sysctl_mem = sysctl_udp_mem,
1527 .sysctl_wmem = &sysctl_udp_wmem_min,
1528 .sysctl_rmem = &sysctl_udp_rmem_min,
1529 .obj_size = sizeof(struct udp6_sock),
1530 .slab_flags = SLAB_DESTROY_BY_RCU,
1531 .h.udp_table = &udp_table,
1532#ifdef CONFIG_COMPAT
1533 .compat_setsockopt = compat_udpv6_setsockopt,
1534 .compat_getsockopt = compat_udpv6_getsockopt,
1535#endif
1536 .clear_sk = udp_v6_clear_sk,
1537};
1538
1539static struct inet_protosw udpv6_protosw = {
1540 .type = SOCK_DGRAM,
1541 .protocol = IPPROTO_UDP,
1542 .prot = &udpv6_prot,
1543 .ops = &inet6_dgram_ops,
1544 .no_check = UDP_CSUM_DEFAULT,
1545 .flags = INET_PROTOSW_PERMANENT,
1546};
1547
1548
1549int __init udpv6_init(void)
1550{
1551 int ret;
1552
1553 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1554 if (ret)
1555 goto out;
1556
1557 ret = inet6_register_protosw(&udpv6_protosw);
1558 if (ret)
1559 goto out_udpv6_protocol;
1560out:
1561 return ret;
1562
1563out_udpv6_protocol:
1564 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1565 goto out;
1566}
1567
1568void udpv6_exit(void)
1569{
1570 inet6_unregister_protosw(&udpv6_protosw);
1571 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1572}