3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/udp.c
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.
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.
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>
40 #include <net/addrconf.h>
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_route.h>
46 #include <net/tcp_states.h>
47 #include <net/ip6_checksum.h>
49 #include <net/inet6_hashtables.h>
50 #include <net/busy_poll.h>
51 #include <net/sock_reuseport.h>
53 #include <linux/proc_fs.h>
54 #include <linux/seq_file.h>
55 #include <trace/events/skb.h>
58 static u32
udp6_ehashfn(const struct net
*net
,
59 const struct in6_addr
*laddr
,
61 const struct in6_addr
*faddr
,
64 static u32 udp6_ehash_secret __read_mostly
;
65 static u32 udp_ipv6_hash_secret __read_mostly
;
69 net_get_random_once(&udp6_ehash_secret
,
70 sizeof(udp6_ehash_secret
));
71 net_get_random_once(&udp_ipv6_hash_secret
,
72 sizeof(udp_ipv6_hash_secret
));
74 lhash
= (__force u32
)laddr
->s6_addr32
[3];
75 fhash
= __ipv6_addr_jhash(faddr
, udp_ipv6_hash_secret
);
77 return __inet6_ehashfn(lhash
, lport
, fhash
, fport
,
78 udp_ipv6_hash_secret
+ net_hash_mix(net
));
81 static u32
udp6_portaddr_hash(const struct net
*net
,
82 const struct in6_addr
*addr6
,
85 unsigned int hash
, mix
= net_hash_mix(net
);
87 if (ipv6_addr_any(addr6
))
88 hash
= jhash_1word(0, mix
);
89 else if (ipv6_addr_v4mapped(addr6
))
90 hash
= jhash_1word((__force u32
)addr6
->s6_addr32
[3], mix
);
92 hash
= jhash2((__force u32
*)addr6
->s6_addr32
, 4, mix
);
97 int udp_v6_get_port(struct sock
*sk
, unsigned short snum
)
99 unsigned int hash2_nulladdr
=
100 udp6_portaddr_hash(sock_net(sk
), &in6addr_any
, snum
);
101 unsigned int hash2_partial
=
102 udp6_portaddr_hash(sock_net(sk
), &sk
->sk_v6_rcv_saddr
, 0);
104 /* precompute partial secondary hash */
105 udp_sk(sk
)->udp_portaddr_hash
= hash2_partial
;
106 return udp_lib_get_port(sk
, snum
, ipv6_rcv_saddr_equal
, hash2_nulladdr
);
109 static void udp_v6_rehash(struct sock
*sk
)
111 u16 new_hash
= udp6_portaddr_hash(sock_net(sk
),
112 &sk
->sk_v6_rcv_saddr
,
113 inet_sk(sk
)->inet_num
);
115 udp_lib_rehash(sk
, new_hash
);
118 static int compute_score(struct sock
*sk
, struct net
*net
,
119 const struct in6_addr
*saddr
, __be16 sport
,
120 const struct in6_addr
*daddr
, unsigned short hnum
,
124 struct inet_sock
*inet
;
126 if (!net_eq(sock_net(sk
), net
) ||
127 udp_sk(sk
)->udp_port_hash
!= hnum
||
128 sk
->sk_family
!= PF_INET6
)
134 if (inet
->inet_dport
) {
135 if (inet
->inet_dport
!= sport
)
140 if (!ipv6_addr_any(&sk
->sk_v6_rcv_saddr
)) {
141 if (!ipv6_addr_equal(&sk
->sk_v6_rcv_saddr
, daddr
))
146 if (!ipv6_addr_any(&sk
->sk_v6_daddr
)) {
147 if (!ipv6_addr_equal(&sk
->sk_v6_daddr
, saddr
))
152 if (sk
->sk_bound_dev_if
) {
153 if (sk
->sk_bound_dev_if
!= dif
)
158 if (sk
->sk_incoming_cpu
== raw_smp_processor_id())
164 /* called with rcu_read_lock() */
165 static struct sock
*udp6_lib_lookup2(struct net
*net
,
166 const struct in6_addr
*saddr
, __be16 sport
,
167 const struct in6_addr
*daddr
, unsigned int hnum
, int dif
,
168 struct udp_hslot
*hslot2
,
171 struct sock
*sk
, *result
;
172 int score
, badness
, matches
= 0, reuseport
= 0;
177 udp_portaddr_for_each_entry_rcu(sk
, &hslot2
->head
) {
178 score
= compute_score(sk
, net
, saddr
, sport
,
180 if (score
> badness
) {
181 reuseport
= sk
->sk_reuseport
;
183 hash
= udp6_ehashfn(net
, daddr
, hnum
,
186 result
= reuseport_select_sock(sk
, hash
, skb
,
187 sizeof(struct udphdr
));
194 } else if (score
== badness
&& reuseport
) {
196 if (reciprocal_scale(hash
, matches
) == 0)
198 hash
= next_pseudo_random32(hash
);
204 /* rcu_read_lock() must be held */
205 struct sock
*__udp6_lib_lookup(struct net
*net
,
206 const struct in6_addr
*saddr
, __be16 sport
,
207 const struct in6_addr
*daddr
, __be16 dport
,
208 int dif
, struct udp_table
*udptable
,
211 struct sock
*sk
, *result
;
212 unsigned short hnum
= ntohs(dport
);
213 unsigned int hash2
, slot2
, slot
= udp_hashfn(net
, hnum
, udptable
->mask
);
214 struct udp_hslot
*hslot2
, *hslot
= &udptable
->hash
[slot
];
215 int score
, badness
, matches
= 0, reuseport
= 0;
218 if (hslot
->count
> 10) {
219 hash2
= udp6_portaddr_hash(net
, daddr
, hnum
);
220 slot2
= hash2
& udptable
->mask
;
221 hslot2
= &udptable
->hash2
[slot2
];
222 if (hslot
->count
< hslot2
->count
)
225 result
= udp6_lib_lookup2(net
, saddr
, sport
,
229 unsigned int old_slot2
= slot2
;
230 hash2
= udp6_portaddr_hash(net
, &in6addr_any
, hnum
);
231 slot2
= hash2
& udptable
->mask
;
232 /* avoid searching the same slot again. */
233 if (unlikely(slot2
== old_slot2
))
236 hslot2
= &udptable
->hash2
[slot2
];
237 if (hslot
->count
< hslot2
->count
)
240 result
= udp6_lib_lookup2(net
, saddr
, sport
,
249 sk_for_each_rcu(sk
, &hslot
->head
) {
250 score
= compute_score(sk
, net
, saddr
, sport
, daddr
, hnum
, dif
);
251 if (score
> badness
) {
252 reuseport
= sk
->sk_reuseport
;
254 hash
= udp6_ehashfn(net
, daddr
, hnum
,
256 result
= reuseport_select_sock(sk
, hash
, skb
,
257 sizeof(struct udphdr
));
264 } else if (score
== badness
&& reuseport
) {
266 if (reciprocal_scale(hash
, matches
) == 0)
268 hash
= next_pseudo_random32(hash
);
273 EXPORT_SYMBOL_GPL(__udp6_lib_lookup
);
275 static struct sock
*__udp6_lib_lookup_skb(struct sk_buff
*skb
,
276 __be16 sport
, __be16 dport
,
277 struct udp_table
*udptable
)
279 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
282 sk
= skb_steal_sock(skb
);
285 return __udp6_lib_lookup(dev_net(skb
->dev
), &iph
->saddr
, sport
,
286 &iph
->daddr
, dport
, inet6_iif(skb
),
290 struct sock
*udp6_lib_lookup_skb(struct sk_buff
*skb
,
291 __be16 sport
, __be16 dport
)
293 const struct ipv6hdr
*iph
= ipv6_hdr(skb
);
295 return __udp6_lib_lookup(dev_net(skb
->dev
), &iph
->saddr
, sport
,
296 &iph
->daddr
, dport
, inet6_iif(skb
),
299 EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb
);
301 /* Must be called under rcu_read_lock().
302 * Does increment socket refcount.
304 #if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
305 IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
306 struct sock
*udp6_lib_lookup(struct net
*net
, const struct in6_addr
*saddr
, __be16 sport
,
307 const struct in6_addr
*daddr
, __be16 dport
, int dif
)
311 sk
= __udp6_lib_lookup(net
, saddr
, sport
, daddr
, dport
,
312 dif
, &udp_table
, NULL
);
313 if (sk
&& !atomic_inc_not_zero(&sk
->sk_refcnt
))
317 EXPORT_SYMBOL_GPL(udp6_lib_lookup
);
321 * This should be easy, if there is something there we
322 * return it, otherwise we block.
325 int udpv6_recvmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
,
326 int noblock
, int flags
, int *addr_len
)
328 struct ipv6_pinfo
*np
= inet6_sk(sk
);
329 struct inet_sock
*inet
= inet_sk(sk
);
331 unsigned int ulen
, copied
;
332 int peeked
, peeking
, off
;
334 int is_udplite
= IS_UDPLITE(sk
);
335 bool checksum_valid
= false;
338 if (flags
& MSG_ERRQUEUE
)
339 return ipv6_recv_error(sk
, msg
, len
, addr_len
);
341 if (np
->rxpmtu
&& np
->rxopt
.bits
.rxpmtu
)
342 return ipv6_recv_rxpmtu(sk
, msg
, len
, addr_len
);
345 peeking
= off
= sk_peek_offset(sk
, flags
);
346 skb
= __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
347 &peeked
, &off
, &err
);
353 if (copied
> ulen
- off
)
355 else if (copied
< ulen
)
356 msg
->msg_flags
|= MSG_TRUNC
;
358 is_udp4
= (skb
->protocol
== htons(ETH_P_IP
));
361 * If checksum is needed at all, try to do it while copying the
362 * data. If the data is truncated, or if we only want a partial
363 * coverage checksum (UDP-Lite), do it before the copy.
366 if (copied
< ulen
|| UDP_SKB_CB(skb
)->partial_cov
|| peeking
) {
367 checksum_valid
= !udp_lib_checksum_complete(skb
);
372 if (checksum_valid
|| skb_csum_unnecessary(skb
))
373 err
= skb_copy_datagram_msg(skb
, off
, msg
, copied
);
375 err
= skb_copy_and_csum_datagram_msg(skb
, off
, msg
);
381 atomic_inc(&sk
->sk_drops
);
383 UDP_INC_STATS(sock_net(sk
), UDP_MIB_INERRORS
,
386 UDP6_INC_STATS(sock_net(sk
), UDP_MIB_INERRORS
,
394 UDP_INC_STATS(sock_net(sk
), UDP_MIB_INDATAGRAMS
,
397 UDP6_INC_STATS(sock_net(sk
), UDP_MIB_INDATAGRAMS
,
401 sock_recv_ts_and_drops(msg
, sk
, skb
);
403 /* Copy the address. */
405 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, msg
->msg_name
);
406 sin6
->sin6_family
= AF_INET6
;
407 sin6
->sin6_port
= udp_hdr(skb
)->source
;
408 sin6
->sin6_flowinfo
= 0;
411 ipv6_addr_set_v4mapped(ip_hdr(skb
)->saddr
,
413 sin6
->sin6_scope_id
= 0;
415 sin6
->sin6_addr
= ipv6_hdr(skb
)->saddr
;
416 sin6
->sin6_scope_id
=
417 ipv6_iface_scope_id(&sin6
->sin6_addr
,
420 *addr_len
= sizeof(*sin6
);
424 ip6_datagram_recv_common_ctl(sk
, msg
, skb
);
427 if (inet
->cmsg_flags
)
428 ip_cmsg_recv_offset(msg
, sk
, skb
,
429 sizeof(struct udphdr
), off
);
432 ip6_datagram_recv_specific_ctl(sk
, msg
, skb
);
436 if (flags
& MSG_TRUNC
)
439 skb_consume_udp(sk
, skb
, peeking
? -err
: err
);
443 if (!__sk_queue_drop_skb(sk
, skb
, flags
)) {
445 UDP_INC_STATS(sock_net(sk
),
446 UDP_MIB_CSUMERRORS
, is_udplite
);
447 UDP_INC_STATS(sock_net(sk
),
448 UDP_MIB_INERRORS
, is_udplite
);
450 UDP6_INC_STATS(sock_net(sk
),
451 UDP_MIB_CSUMERRORS
, is_udplite
);
452 UDP6_INC_STATS(sock_net(sk
),
453 UDP_MIB_INERRORS
, is_udplite
);
458 /* starting over for a new packet, but check if we need to yield */
460 msg
->msg_flags
&= ~MSG_TRUNC
;
464 void __udp6_lib_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
465 u8 type
, u8 code
, int offset
, __be32 info
,
466 struct udp_table
*udptable
)
468 struct ipv6_pinfo
*np
;
469 const struct ipv6hdr
*hdr
= (const struct ipv6hdr
*)skb
->data
;
470 const struct in6_addr
*saddr
= &hdr
->saddr
;
471 const struct in6_addr
*daddr
= &hdr
->daddr
;
472 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+offset
);
476 struct net
*net
= dev_net(skb
->dev
);
478 sk
= __udp6_lib_lookup(net
, daddr
, uh
->dest
, saddr
, uh
->source
,
479 inet6_iif(skb
), udptable
, skb
);
481 __ICMP6_INC_STATS(net
, __in6_dev_get(skb
->dev
),
486 harderr
= icmpv6_err_convert(type
, code
, &err
);
489 if (type
== ICMPV6_PKT_TOOBIG
) {
490 if (!ip6_sk_accept_pmtu(sk
))
492 ip6_sk_update_pmtu(skb
, sk
, info
);
493 if (np
->pmtudisc
!= IPV6_PMTUDISC_DONT
)
496 if (type
== NDISC_REDIRECT
) {
497 ip6_sk_redirect(skb
, sk
);
502 if (!harderr
|| sk
->sk_state
!= TCP_ESTABLISHED
)
505 ipv6_icmp_error(sk
, skb
, err
, uh
->dest
, ntohl(info
), (u8
*)(uh
+1));
509 sk
->sk_error_report(sk
);
514 static int __udpv6_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
518 if (!ipv6_addr_any(&sk
->sk_v6_daddr
)) {
519 sock_rps_save_rxhash(sk
, skb
);
520 sk_mark_napi_id(sk
, skb
);
521 sk_incoming_cpu_update(sk
);
524 rc
= __udp_enqueue_schedule_skb(sk
, skb
);
526 int is_udplite
= IS_UDPLITE(sk
);
528 /* Note that an ENOMEM error is charged twice */
530 UDP6_INC_STATS(sock_net(sk
),
531 UDP_MIB_RCVBUFERRORS
, is_udplite
);
532 UDP6_INC_STATS(sock_net(sk
), UDP_MIB_INERRORS
, is_udplite
);
540 static __inline__
void udpv6_err(struct sk_buff
*skb
,
541 struct inet6_skb_parm
*opt
, u8 type
,
542 u8 code
, int offset
, __be32 info
)
544 __udp6_lib_err(skb
, opt
, type
, code
, offset
, info
, &udp_table
);
547 static struct static_key udpv6_encap_needed __read_mostly
;
548 void udpv6_encap_enable(void)
550 if (!static_key_enabled(&udpv6_encap_needed
))
551 static_key_slow_inc(&udpv6_encap_needed
);
553 EXPORT_SYMBOL(udpv6_encap_enable
);
555 int udpv6_queue_rcv_skb(struct sock
*sk
, struct sk_buff
*skb
)
557 struct udp_sock
*up
= udp_sk(sk
);
558 int is_udplite
= IS_UDPLITE(sk
);
560 if (!xfrm6_policy_check(sk
, XFRM_POLICY_IN
, skb
))
563 if (static_key_false(&udpv6_encap_needed
) && up
->encap_type
) {
564 int (*encap_rcv
)(struct sock
*sk
, struct sk_buff
*skb
);
567 * This is an encapsulation socket so pass the skb to
568 * the socket's udp_encap_rcv() hook. Otherwise, just
569 * fall through and pass this up the UDP socket.
570 * up->encap_rcv() returns the following value:
571 * =0 if skb was successfully passed to the encap
572 * handler or was discarded by it.
573 * >0 if skb should be passed on to UDP.
574 * <0 if skb should be resubmitted as proto -N
577 /* if we're overly short, let UDP handle it */
578 encap_rcv
= ACCESS_ONCE(up
->encap_rcv
);
582 /* Verify checksum before giving to encap */
583 if (udp_lib_checksum_complete(skb
))
586 ret
= encap_rcv(sk
, skb
);
588 __UDP_INC_STATS(sock_net(sk
),
595 /* FALLTHROUGH -- it's a UDP Packet */
599 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
601 if ((is_udplite
& UDPLITE_RECV_CC
) && UDP_SKB_CB(skb
)->partial_cov
) {
603 if (up
->pcrlen
== 0) { /* full coverage was set */
604 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
605 UDP_SKB_CB(skb
)->cscov
, skb
->len
);
608 if (UDP_SKB_CB(skb
)->cscov
< up
->pcrlen
) {
609 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
610 UDP_SKB_CB(skb
)->cscov
, up
->pcrlen
);
615 if (rcu_access_pointer(sk
->sk_filter
) &&
616 udp_lib_checksum_complete(skb
))
619 if (sk_filter_trim_cap(sk
, skb
, sizeof(struct udphdr
)))
622 udp_csum_pull_header(skb
);
626 return __udpv6_queue_rcv_skb(sk
, skb
);
629 __UDP6_INC_STATS(sock_net(sk
), UDP_MIB_CSUMERRORS
, is_udplite
);
631 __UDP6_INC_STATS(sock_net(sk
), UDP_MIB_INERRORS
, is_udplite
);
632 atomic_inc(&sk
->sk_drops
);
637 static bool __udp_v6_is_mcast_sock(struct net
*net
, struct sock
*sk
,
638 __be16 loc_port
, const struct in6_addr
*loc_addr
,
639 __be16 rmt_port
, const struct in6_addr
*rmt_addr
,
640 int dif
, unsigned short hnum
)
642 struct inet_sock
*inet
= inet_sk(sk
);
644 if (!net_eq(sock_net(sk
), net
))
647 if (udp_sk(sk
)->udp_port_hash
!= hnum
||
648 sk
->sk_family
!= PF_INET6
||
649 (inet
->inet_dport
&& inet
->inet_dport
!= rmt_port
) ||
650 (!ipv6_addr_any(&sk
->sk_v6_daddr
) &&
651 !ipv6_addr_equal(&sk
->sk_v6_daddr
, rmt_addr
)) ||
652 (sk
->sk_bound_dev_if
&& sk
->sk_bound_dev_if
!= dif
) ||
653 (!ipv6_addr_any(&sk
->sk_v6_rcv_saddr
) &&
654 !ipv6_addr_equal(&sk
->sk_v6_rcv_saddr
, loc_addr
)))
656 if (!inet6_mc_check(sk
, loc_addr
, rmt_addr
))
661 static void udp6_csum_zero_error(struct sk_buff
*skb
)
663 /* RFC 2460 section 8.1 says that we SHOULD log
664 * this error. Well, it is reasonable.
666 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
667 &ipv6_hdr(skb
)->saddr
, ntohs(udp_hdr(skb
)->source
),
668 &ipv6_hdr(skb
)->daddr
, ntohs(udp_hdr(skb
)->dest
));
672 * Note: called only from the BH handler context,
673 * so we don't need to lock the hashes.
675 static int __udp6_lib_mcast_deliver(struct net
*net
, struct sk_buff
*skb
,
676 const struct in6_addr
*saddr
, const struct in6_addr
*daddr
,
677 struct udp_table
*udptable
, int proto
)
679 struct sock
*sk
, *first
= NULL
;
680 const struct udphdr
*uh
= udp_hdr(skb
);
681 unsigned short hnum
= ntohs(uh
->dest
);
682 struct udp_hslot
*hslot
= udp_hashslot(udptable
, net
, hnum
);
683 unsigned int offset
= offsetof(typeof(*sk
), sk_node
);
684 unsigned int hash2
= 0, hash2_any
= 0, use_hash2
= (hslot
->count
> 10);
685 int dif
= inet6_iif(skb
);
686 struct hlist_node
*node
;
687 struct sk_buff
*nskb
;
690 hash2_any
= udp6_portaddr_hash(net
, &in6addr_any
, hnum
) &
692 hash2
= udp6_portaddr_hash(net
, daddr
, hnum
) & udp_table
.mask
;
694 hslot
= &udp_table
.hash2
[hash2
];
695 offset
= offsetof(typeof(*sk
), __sk_common
.skc_portaddr_node
);
698 sk_for_each_entry_offset_rcu(sk
, node
, &hslot
->head
, offset
) {
699 if (!__udp_v6_is_mcast_sock(net
, sk
, uh
->dest
, daddr
,
700 uh
->source
, saddr
, dif
, hnum
))
702 /* If zero checksum and no_check is not on for
703 * the socket then skip it.
705 if (!uh
->check
&& !udp_sk(sk
)->no_check6_rx
)
711 nskb
= skb_clone(skb
, GFP_ATOMIC
);
712 if (unlikely(!nskb
)) {
713 atomic_inc(&sk
->sk_drops
);
714 __UDP6_INC_STATS(net
, UDP_MIB_RCVBUFERRORS
,
716 __UDP6_INC_STATS(net
, UDP_MIB_INERRORS
,
721 if (udpv6_queue_rcv_skb(sk
, nskb
) > 0)
725 /* Also lookup *:port if we are using hash2 and haven't done so yet. */
726 if (use_hash2
&& hash2
!= hash2_any
) {
732 if (udpv6_queue_rcv_skb(first
, skb
) > 0)
736 __UDP6_INC_STATS(net
, UDP_MIB_IGNOREDMULTI
,
737 proto
== IPPROTO_UDPLITE
);
742 int __udp6_lib_rcv(struct sk_buff
*skb
, struct udp_table
*udptable
,
745 const struct in6_addr
*saddr
, *daddr
;
746 struct net
*net
= dev_net(skb
->dev
);
751 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
754 saddr
= &ipv6_hdr(skb
)->saddr
;
755 daddr
= &ipv6_hdr(skb
)->daddr
;
758 ulen
= ntohs(uh
->len
);
762 if (proto
== IPPROTO_UDP
) {
763 /* UDP validates ulen. */
765 /* Check for jumbo payload */
769 if (ulen
< sizeof(*uh
))
772 if (ulen
< skb
->len
) {
773 if (pskb_trim_rcsum(skb
, ulen
))
775 saddr
= &ipv6_hdr(skb
)->saddr
;
776 daddr
= &ipv6_hdr(skb
)->daddr
;
781 if (udp6_csum_init(skb
, uh
, proto
))
785 * Multicast receive code
787 if (ipv6_addr_is_multicast(daddr
))
788 return __udp6_lib_mcast_deliver(net
, skb
,
789 saddr
, daddr
, udptable
, proto
);
794 * check socket cache ... must talk to Alan about his plans
795 * for sock caches... i'll skip this for now.
797 sk
= __udp6_lib_lookup_skb(skb
, uh
->source
, uh
->dest
, udptable
);
801 if (!uh
->check
&& !udp_sk(sk
)->no_check6_rx
) {
802 udp6_csum_zero_error(skb
);
806 if (inet_get_convert_csum(sk
) && uh
->check
&& !IS_UDPLITE(sk
))
807 skb_checksum_try_convert(skb
, IPPROTO_UDP
, uh
->check
,
810 ret
= udpv6_queue_rcv_skb(sk
, skb
);
812 /* a return value > 0 means to resubmit the input */
820 udp6_csum_zero_error(skb
);
824 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
827 if (udp_lib_checksum_complete(skb
))
830 __UDP6_INC_STATS(net
, UDP_MIB_NOPORTS
, proto
== IPPROTO_UDPLITE
);
831 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0);
837 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
838 proto
== IPPROTO_UDPLITE
? "-Lite" : "",
839 saddr
, ntohs(uh
->source
),
841 daddr
, ntohs(uh
->dest
));
844 __UDP6_INC_STATS(net
, UDP_MIB_CSUMERRORS
, proto
== IPPROTO_UDPLITE
);
846 __UDP6_INC_STATS(net
, UDP_MIB_INERRORS
, proto
== IPPROTO_UDPLITE
);
851 static __inline__
int udpv6_rcv(struct sk_buff
*skb
)
853 return __udp6_lib_rcv(skb
, &udp_table
, IPPROTO_UDP
);
857 * Throw away all pending data and cancel the corking. Socket is locked.
859 static void udp_v6_flush_pending_frames(struct sock
*sk
)
861 struct udp_sock
*up
= udp_sk(sk
);
863 if (up
->pending
== AF_INET
)
864 udp_flush_pending_frames(sk
);
865 else if (up
->pending
) {
868 ip6_flush_pending_frames(sk
);
873 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
874 * @sk: socket we are sending on
875 * @skb: sk_buff containing the filled-in UDP header
876 * (checksum field must be zeroed out)
878 static void udp6_hwcsum_outgoing(struct sock
*sk
, struct sk_buff
*skb
,
879 const struct in6_addr
*saddr
,
880 const struct in6_addr
*daddr
, int len
)
883 struct udphdr
*uh
= udp_hdr(skb
);
884 struct sk_buff
*frags
= skb_shinfo(skb
)->frag_list
;
888 /* Only one fragment on the socket. */
889 skb
->csum_start
= skb_transport_header(skb
) - skb
->head
;
890 skb
->csum_offset
= offsetof(struct udphdr
, check
);
891 uh
->check
= ~csum_ipv6_magic(saddr
, daddr
, len
, IPPROTO_UDP
, 0);
894 * HW-checksum won't work as there are two or more
895 * fragments on the socket so that all csums of sk_buffs
898 offset
= skb_transport_offset(skb
);
899 skb
->csum
= skb_checksum(skb
, offset
, skb
->len
- offset
, 0);
901 skb
->ip_summed
= CHECKSUM_NONE
;
904 csum
= csum_add(csum
, frags
->csum
);
905 } while ((frags
= frags
->next
));
907 uh
->check
= csum_ipv6_magic(saddr
, daddr
, len
, IPPROTO_UDP
,
910 uh
->check
= CSUM_MANGLED_0
;
918 static int udp_v6_send_skb(struct sk_buff
*skb
, struct flowi6
*fl6
)
920 struct sock
*sk
= skb
->sk
;
923 int is_udplite
= IS_UDPLITE(sk
);
925 int offset
= skb_transport_offset(skb
);
926 int len
= skb
->len
- offset
;
929 * Create a UDP header
932 uh
->source
= fl6
->fl6_sport
;
933 uh
->dest
= fl6
->fl6_dport
;
934 uh
->len
= htons(len
);
938 csum
= udplite_csum(skb
);
939 else if (udp_sk(sk
)->no_check6_tx
) { /* UDP csum disabled */
940 skb
->ip_summed
= CHECKSUM_NONE
;
942 } else if (skb
->ip_summed
== CHECKSUM_PARTIAL
) { /* UDP hardware csum */
943 udp6_hwcsum_outgoing(sk
, skb
, &fl6
->saddr
, &fl6
->daddr
, len
);
946 csum
= udp_csum(skb
);
948 /* add protocol-dependent pseudo-header */
949 uh
->check
= csum_ipv6_magic(&fl6
->saddr
, &fl6
->daddr
,
950 len
, fl6
->flowi6_proto
, csum
);
952 uh
->check
= CSUM_MANGLED_0
;
955 err
= ip6_send_skb(skb
);
957 if (err
== -ENOBUFS
&& !inet6_sk(sk
)->recverr
) {
958 UDP6_INC_STATS(sock_net(sk
),
959 UDP_MIB_SNDBUFERRORS
, is_udplite
);
963 UDP6_INC_STATS(sock_net(sk
),
964 UDP_MIB_OUTDATAGRAMS
, is_udplite
);
969 static int udp_v6_push_pending_frames(struct sock
*sk
)
972 struct udp_sock
*up
= udp_sk(sk
);
976 if (up
->pending
== AF_INET
)
977 return udp_push_pending_frames(sk
);
979 /* ip6_finish_skb will release the cork, so make a copy of
982 fl6
= inet_sk(sk
)->cork
.fl
.u
.ip6
;
984 skb
= ip6_finish_skb(sk
);
988 err
= udp_v6_send_skb(skb
, &fl6
);
996 int udpv6_sendmsg(struct sock
*sk
, struct msghdr
*msg
, size_t len
)
998 struct ipv6_txoptions opt_space
;
999 struct udp_sock
*up
= udp_sk(sk
);
1000 struct inet_sock
*inet
= inet_sk(sk
);
1001 struct ipv6_pinfo
*np
= inet6_sk(sk
);
1002 DECLARE_SOCKADDR(struct sockaddr_in6
*, sin6
, msg
->msg_name
);
1003 struct in6_addr
*daddr
, *final_p
, final
;
1004 struct ipv6_txoptions
*opt
= NULL
;
1005 struct ipv6_txoptions
*opt_to_free
= NULL
;
1006 struct ip6_flowlabel
*flowlabel
= NULL
;
1008 struct dst_entry
*dst
;
1009 struct ipcm6_cookie ipc6
;
1010 int addr_len
= msg
->msg_namelen
;
1012 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
1015 int is_udplite
= IS_UDPLITE(sk
);
1016 int (*getfrag
)(void *, char *, int, int, int, struct sk_buff
*);
1017 struct sockcm_cookie sockc
;
1023 /* destination address check */
1025 if (addr_len
< offsetof(struct sockaddr
, sa_data
))
1028 switch (sin6
->sin6_family
) {
1030 if (addr_len
< SIN6_LEN_RFC2133
)
1032 daddr
= &sin6
->sin6_addr
;
1035 goto do_udp_sendmsg
;
1037 msg
->msg_name
= sin6
= NULL
;
1038 msg
->msg_namelen
= addr_len
= 0;
1044 } else if (!up
->pending
) {
1045 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1046 return -EDESTADDRREQ
;
1047 daddr
= &sk
->sk_v6_daddr
;
1052 if (ipv6_addr_v4mapped(daddr
)) {
1053 struct sockaddr_in sin
;
1054 sin
.sin_family
= AF_INET
;
1055 sin
.sin_port
= sin6
? sin6
->sin6_port
: inet
->inet_dport
;
1056 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
1057 msg
->msg_name
= &sin
;
1058 msg
->msg_namelen
= sizeof(sin
);
1060 if (__ipv6_only_sock(sk
))
1061 return -ENETUNREACH
;
1062 return udp_sendmsg(sk
, msg
, len
);
1066 if (up
->pending
== AF_INET
)
1067 return udp_sendmsg(sk
, msg
, len
);
1069 /* Rough check on arithmetic overflow,
1070 better check is made in ip6_append_data().
1072 if (len
> INT_MAX
- sizeof(struct udphdr
))
1075 getfrag
= is_udplite
? udplite_getfrag
: ip_generic_getfrag
;
1078 * There are pending frames.
1079 * The socket lock must be held while it's corked.
1082 if (likely(up
->pending
)) {
1083 if (unlikely(up
->pending
!= AF_INET6
)) {
1085 return -EAFNOSUPPORT
;
1088 goto do_append_data
;
1092 ulen
+= sizeof(struct udphdr
);
1094 memset(&fl6
, 0, sizeof(fl6
));
1097 if (sin6
->sin6_port
== 0)
1100 fl6
.fl6_dport
= sin6
->sin6_port
;
1101 daddr
= &sin6
->sin6_addr
;
1104 fl6
.flowlabel
= sin6
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
1105 if (fl6
.flowlabel
&IPV6_FLOWLABEL_MASK
) {
1106 flowlabel
= fl6_sock_lookup(sk
, fl6
.flowlabel
);
1113 * Otherwise it will be difficult to maintain
1116 if (sk
->sk_state
== TCP_ESTABLISHED
&&
1117 ipv6_addr_equal(daddr
, &sk
->sk_v6_daddr
))
1118 daddr
= &sk
->sk_v6_daddr
;
1120 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
1121 sin6
->sin6_scope_id
&&
1122 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr
)))
1123 fl6
.flowi6_oif
= sin6
->sin6_scope_id
;
1125 if (sk
->sk_state
!= TCP_ESTABLISHED
)
1126 return -EDESTADDRREQ
;
1128 fl6
.fl6_dport
= inet
->inet_dport
;
1129 daddr
= &sk
->sk_v6_daddr
;
1130 fl6
.flowlabel
= np
->flow_label
;
1134 if (!fl6
.flowi6_oif
)
1135 fl6
.flowi6_oif
= sk
->sk_bound_dev_if
;
1137 if (!fl6
.flowi6_oif
)
1138 fl6
.flowi6_oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
1140 fl6
.flowi6_mark
= sk
->sk_mark
;
1141 fl6
.flowi6_uid
= sk
->sk_uid
;
1142 sockc
.tsflags
= sk
->sk_tsflags
;
1144 if (msg
->msg_controllen
) {
1146 memset(opt
, 0, sizeof(struct ipv6_txoptions
));
1147 opt
->tot_len
= sizeof(*opt
);
1150 err
= ip6_datagram_send_ctl(sock_net(sk
), sk
, msg
, &fl6
, &ipc6
, &sockc
);
1152 fl6_sock_release(flowlabel
);
1155 if ((fl6
.flowlabel
&IPV6_FLOWLABEL_MASK
) && !flowlabel
) {
1156 flowlabel
= fl6_sock_lookup(sk
, fl6
.flowlabel
);
1160 if (!(opt
->opt_nflen
|opt
->opt_flen
))
1165 opt
= txopt_get(np
);
1169 opt
= fl6_merge_options(&opt_space
, flowlabel
, opt
);
1170 opt
= ipv6_fixup_options(&opt_space
, opt
);
1173 fl6
.flowi6_proto
= sk
->sk_protocol
;
1174 if (!ipv6_addr_any(daddr
))
1177 fl6
.daddr
.s6_addr
[15] = 0x1; /* :: means loopback (BSD'ism) */
1178 if (ipv6_addr_any(&fl6
.saddr
) && !ipv6_addr_any(&np
->saddr
))
1179 fl6
.saddr
= np
->saddr
;
1180 fl6
.fl6_sport
= inet
->inet_sport
;
1182 final_p
= fl6_update_dst(&fl6
, opt
, &final
);
1186 if (!fl6
.flowi6_oif
&& ipv6_addr_is_multicast(&fl6
.daddr
)) {
1187 fl6
.flowi6_oif
= np
->mcast_oif
;
1189 } else if (!fl6
.flowi6_oif
)
1190 fl6
.flowi6_oif
= np
->ucast_oif
;
1192 security_sk_classify_flow(sk
, flowi6_to_flowi(&fl6
));
1194 if (ipc6
.tclass
< 0)
1195 ipc6
.tclass
= np
->tclass
;
1197 fl6
.flowlabel
= ip6_make_flowinfo(ipc6
.tclass
, fl6
.flowlabel
);
1199 dst
= ip6_sk_dst_lookup_flow(sk
, &fl6
, final_p
);
1206 if (ipc6
.hlimit
< 0)
1207 ipc6
.hlimit
= ip6_sk_dst_hoplimit(np
, &fl6
, dst
);
1209 if (msg
->msg_flags
&MSG_CONFIRM
)
1213 /* Lockless fast path for the non-corking case */
1215 struct sk_buff
*skb
;
1217 skb
= ip6_make_skb(sk
, getfrag
, msg
, ulen
,
1218 sizeof(struct udphdr
), &ipc6
,
1219 &fl6
, (struct rt6_info
*)dst
,
1220 msg
->msg_flags
, &sockc
);
1222 if (!IS_ERR_OR_NULL(skb
))
1223 err
= udp_v6_send_skb(skb
, &fl6
);
1228 if (unlikely(up
->pending
)) {
1229 /* The socket is already corked while preparing it. */
1230 /* ... which is an evident application bug. --ANK */
1233 net_dbg_ratelimited("udp cork app bug 2\n");
1238 up
->pending
= AF_INET6
;
1241 if (ipc6
.dontfrag
< 0)
1242 ipc6
.dontfrag
= np
->dontfrag
;
1244 err
= ip6_append_data(sk
, getfrag
, msg
, ulen
, sizeof(struct udphdr
),
1245 &ipc6
, &fl6
, (struct rt6_info
*)dst
,
1246 corkreq
? msg
->msg_flags
|MSG_MORE
: msg
->msg_flags
, &sockc
);
1248 udp_v6_flush_pending_frames(sk
);
1250 err
= udp_v6_push_pending_frames(sk
);
1251 else if (unlikely(skb_queue_empty(&sk
->sk_write_queue
)))
1255 err
= np
->recverr
? net_xmit_errno(err
) : 0;
1261 ip6_dst_store(sk
, dst
,
1262 ipv6_addr_equal(&fl6
.daddr
, &sk
->sk_v6_daddr
) ?
1263 &sk
->sk_v6_daddr
: NULL
,
1264 #ifdef CONFIG_IPV6_SUBTREES
1265 ipv6_addr_equal(&fl6
.saddr
, &np
->saddr
) ?
1277 fl6_sock_release(flowlabel
);
1278 txopt_put(opt_to_free
);
1282 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1283 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1284 * we don't have a good statistic (IpOutDiscards but it can be too many
1285 * things). We could add another new stat but at least for now that
1286 * seems like overkill.
1288 if (err
== -ENOBUFS
|| test_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
)) {
1289 UDP6_INC_STATS(sock_net(sk
),
1290 UDP_MIB_SNDBUFERRORS
, is_udplite
);
1296 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
1297 goto back_from_confirm
;
1302 void udpv6_destroy_sock(struct sock
*sk
)
1304 struct udp_sock
*up
= udp_sk(sk
);
1306 udp_v6_flush_pending_frames(sk
);
1309 if (static_key_false(&udpv6_encap_needed
) && up
->encap_type
) {
1310 void (*encap_destroy
)(struct sock
*sk
);
1311 encap_destroy
= ACCESS_ONCE(up
->encap_destroy
);
1316 inet6_destroy_sock(sk
);
1320 * Socket option code for UDP
1322 int udpv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1323 char __user
*optval
, unsigned int optlen
)
1325 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1326 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1327 udp_v6_push_pending_frames
);
1328 return ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1331 #ifdef CONFIG_COMPAT
1332 int compat_udpv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1333 char __user
*optval
, unsigned int optlen
)
1335 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1336 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1337 udp_v6_push_pending_frames
);
1338 return compat_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1342 int udpv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1343 char __user
*optval
, int __user
*optlen
)
1345 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1346 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1347 return ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1350 #ifdef CONFIG_COMPAT
1351 int compat_udpv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1352 char __user
*optval
, int __user
*optlen
)
1354 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1355 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1356 return compat_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1360 static const struct inet6_protocol udpv6_protocol
= {
1361 .handler
= udpv6_rcv
,
1362 .err_handler
= udpv6_err
,
1363 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1366 /* ------------------------------------------------------------------------ */
1367 #ifdef CONFIG_PROC_FS
1368 int udp6_seq_show(struct seq_file
*seq
, void *v
)
1370 if (v
== SEQ_START_TOKEN
) {
1371 seq_puts(seq
, IPV6_SEQ_DGRAM_HEADER
);
1373 int bucket
= ((struct udp_iter_state
*)seq
->private)->bucket
;
1374 struct inet_sock
*inet
= inet_sk(v
);
1375 __u16 srcp
= ntohs(inet
->inet_sport
);
1376 __u16 destp
= ntohs(inet
->inet_dport
);
1377 ip6_dgram_sock_seq_show(seq
, v
, srcp
, destp
, bucket
);
1382 static const struct file_operations udp6_afinfo_seq_fops
= {
1383 .owner
= THIS_MODULE
,
1384 .open
= udp_seq_open
,
1386 .llseek
= seq_lseek
,
1387 .release
= seq_release_net
1390 static struct udp_seq_afinfo udp6_seq_afinfo
= {
1393 .udp_table
= &udp_table
,
1394 .seq_fops
= &udp6_afinfo_seq_fops
,
1396 .show
= udp6_seq_show
,
1400 int __net_init
udp6_proc_init(struct net
*net
)
1402 return udp_proc_register(net
, &udp6_seq_afinfo
);
1405 void udp6_proc_exit(struct net
*net
)
1407 udp_proc_unregister(net
, &udp6_seq_afinfo
);
1409 #endif /* CONFIG_PROC_FS */
1411 /* ------------------------------------------------------------------------ */
1413 struct proto udpv6_prot
= {
1415 .owner
= THIS_MODULE
,
1416 .close
= udp_lib_close
,
1417 .connect
= ip6_datagram_connect
,
1418 .disconnect
= udp_disconnect
,
1420 .init
= udp_init_sock
,
1421 .destroy
= udpv6_destroy_sock
,
1422 .setsockopt
= udpv6_setsockopt
,
1423 .getsockopt
= udpv6_getsockopt
,
1424 .sendmsg
= udpv6_sendmsg
,
1425 .recvmsg
= udpv6_recvmsg
,
1426 .release_cb
= ip6_datagram_release_cb
,
1427 .hash
= udp_lib_hash
,
1428 .unhash
= udp_lib_unhash
,
1429 .rehash
= udp_v6_rehash
,
1430 .get_port
= udp_v6_get_port
,
1431 .memory_allocated
= &udp_memory_allocated
,
1432 .sysctl_mem
= sysctl_udp_mem
,
1433 .sysctl_wmem
= &sysctl_udp_wmem_min
,
1434 .sysctl_rmem
= &sysctl_udp_rmem_min
,
1435 .obj_size
= sizeof(struct udp6_sock
),
1436 .h
.udp_table
= &udp_table
,
1437 #ifdef CONFIG_COMPAT
1438 .compat_setsockopt
= compat_udpv6_setsockopt
,
1439 .compat_getsockopt
= compat_udpv6_getsockopt
,
1441 .diag_destroy
= udp_abort
,
1444 static struct inet_protosw udpv6_protosw
= {
1446 .protocol
= IPPROTO_UDP
,
1447 .prot
= &udpv6_prot
,
1448 .ops
= &inet6_dgram_ops
,
1449 .flags
= INET_PROTOSW_PERMANENT
,
1452 int __init
udpv6_init(void)
1456 ret
= inet6_add_protocol(&udpv6_protocol
, IPPROTO_UDP
);
1460 ret
= inet6_register_protosw(&udpv6_protosw
);
1462 goto out_udpv6_protocol
;
1467 inet6_del_protocol(&udpv6_protocol
, IPPROTO_UDP
);
1471 void udpv6_exit(void)
1473 inet6_unregister_protosw(&udpv6_protosw
);
1474 inet6_del_protocol(&udpv6_protocol
, IPPROTO_UDP
);