12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2db8dac20SDavid S. Miller /*
3db8dac20SDavid S. Miller * UDP over IPv6
4db8dac20SDavid S. Miller * Linux INET6 implementation
5db8dac20SDavid S. Miller *
6db8dac20SDavid S. Miller * Authors:
7db8dac20SDavid S. Miller * Pedro Roque <roque@di.fc.ul.pt>
8db8dac20SDavid S. Miller *
9db8dac20SDavid S. Miller * Based on linux/ipv4/udp.c
10db8dac20SDavid S. Miller *
11db8dac20SDavid S. Miller * Fixes:
12db8dac20SDavid S. Miller * Hideaki YOSHIFUJI : sin6_scope_id support
13db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
14db8dac20SDavid S. Miller * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
15db8dac20SDavid S. Miller * a single port at the same time.
16db8dac20SDavid S. Miller * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
17db8dac20SDavid S. Miller * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
18db8dac20SDavid S. Miller */
19db8dac20SDavid S. Miller
20aef2fedaSJakub Kicinski #include <linux/bpf-cgroup.h>
21db8dac20SDavid S. Miller #include <linux/errno.h>
22db8dac20SDavid S. Miller #include <linux/types.h>
23db8dac20SDavid S. Miller #include <linux/socket.h>
24db8dac20SDavid S. Miller #include <linux/sockios.h>
25db8dac20SDavid S. Miller #include <linux/net.h>
26db8dac20SDavid S. Miller #include <linux/in6.h>
27db8dac20SDavid S. Miller #include <linux/netdevice.h>
28db8dac20SDavid S. Miller #include <linux/if_arp.h>
29db8dac20SDavid S. Miller #include <linux/ipv6.h>
30db8dac20SDavid S. Miller #include <linux/icmpv6.h>
31db8dac20SDavid S. Miller #include <linux/init.h>
32db8dac20SDavid S. Miller #include <linux/module.h>
33db8dac20SDavid S. Miller #include <linux/skbuff.h>
345a0e3ad6STejun Heo #include <linux/slab.h>
357c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
360e219ae4SPaolo Abeni #include <linux/indirect_call_wrapper.h>
37db8dac20SDavid S. Miller
38496611d7SCraig Gallek #include <net/addrconf.h>
39db8dac20SDavid S. Miller #include <net/ndisc.h>
40db8dac20SDavid S. Miller #include <net/protocol.h>
41db8dac20SDavid S. Miller #include <net/transp_v6.h>
42db8dac20SDavid S. Miller #include <net/ip6_route.h>
43db8dac20SDavid S. Miller #include <net/raw.h>
44222a011eSAndrew Lunn #include <net/seg6.h>
45db8dac20SDavid S. Miller #include <net/tcp_states.h>
46db8dac20SDavid S. Miller #include <net/ip6_checksum.h>
47e7cc0824SStefano Brivio #include <net/ip6_tunnel.h>
488139dccdSIvan Babrou #include <trace/events/udp.h>
49db8dac20SDavid S. Miller #include <net/xfrm.h>
500bd84065Ssubashab@codeaurora.org #include <net/inet_hashtables.h>
5172289b96STom Herbert #include <net/inet6_hashtables.h>
52076bb0c8SEliezer Tamir #include <net/busy_poll.h>
53e32ea7e7SCraig Gallek #include <net/sock_reuseport.h>
547938cd15SRichard Gobert #include <net/gro.h>
55db8dac20SDavid S. Miller
56db8dac20SDavid S. Miller #include <linux/proc_fs.h>
57db8dac20SDavid S. Miller #include <linux/seq_file.h>
5822911fc5SEric Dumazet #include <trace/events/skb.h>
59db8dac20SDavid S. Miller #include "udp_impl.h"
60db8dac20SDavid S. Miller
udpv6_destruct_sock(struct sock * sk)61d38afeecSKuniyuki Iwashima static void udpv6_destruct_sock(struct sock *sk)
62d38afeecSKuniyuki Iwashima {
63d38afeecSKuniyuki Iwashima udp_destruct_common(sk);
64d38afeecSKuniyuki Iwashima inet6_sock_destruct(sk);
65d38afeecSKuniyuki Iwashima }
66d38afeecSKuniyuki Iwashima
udpv6_init_sock(struct sock * sk)67d38afeecSKuniyuki Iwashima int udpv6_init_sock(struct sock *sk)
68d38afeecSKuniyuki Iwashima {
698a3854c7SPaolo Abeni udp_lib_init_sock(sk);
70d38afeecSKuniyuki Iwashima sk->sk_destruct = udpv6_destruct_sock;
718f279fb0SPavel Begunkov set_bit(SOCK_SUPPORT_ZC, &sk->sk_socket->flags);
72d38afeecSKuniyuki Iwashima return 0;
73d38afeecSKuniyuki Iwashima }
74d38afeecSKuniyuki Iwashima
750f495f76SLorenz Bauer INDIRECT_CALLABLE_SCOPE
udp6_ehashfn(const struct net * net,const struct in6_addr * laddr,const u16 lport,const struct in6_addr * faddr,const __be16 fport)760f495f76SLorenz Bauer u32 udp6_ehashfn(const struct net *net,
77b50026b5SHannes Frederic Sowa const struct in6_addr *laddr,
78b50026b5SHannes Frederic Sowa const u16 lport,
79b50026b5SHannes Frederic Sowa const struct in6_addr *faddr,
80b50026b5SHannes Frederic Sowa const __be16 fport)
81b50026b5SHannes Frederic Sowa {
821bbdceefSHannes Frederic Sowa static u32 udp6_ehash_secret __read_mostly;
831bbdceefSHannes Frederic Sowa static u32 udp_ipv6_hash_secret __read_mostly;
841bbdceefSHannes Frederic Sowa
851bbdceefSHannes Frederic Sowa u32 lhash, fhash;
861bbdceefSHannes Frederic Sowa
871bbdceefSHannes Frederic Sowa net_get_random_once(&udp6_ehash_secret,
881bbdceefSHannes Frederic Sowa sizeof(udp6_ehash_secret));
891bbdceefSHannes Frederic Sowa net_get_random_once(&udp_ipv6_hash_secret,
901bbdceefSHannes Frederic Sowa sizeof(udp_ipv6_hash_secret));
911bbdceefSHannes Frederic Sowa
921bbdceefSHannes Frederic Sowa lhash = (__force u32)laddr->s6_addr32[3];
931bbdceefSHannes Frederic Sowa fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
941bbdceefSHannes Frederic Sowa
95b50026b5SHannes Frederic Sowa return __inet6_ehashfn(lhash, lport, fhash, fport,
9651d03e2fSEric Dumazet udp6_ehash_secret + net_hash_mix(net));
97b50026b5SHannes Frederic Sowa }
98b50026b5SHannes Frederic Sowa
udp_v6_get_port(struct sock * sk,unsigned short snum)996ba5a3c5SPavel Emelyanov int udp_v6_get_port(struct sock *sk, unsigned short snum)
100db8dac20SDavid S. Miller {
10130fff923SEric Dumazet unsigned int hash2_nulladdr =
102f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
10330fff923SEric Dumazet unsigned int hash2_partial =
104f0b1e64cSMartin KaFai Lau ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
10530fff923SEric Dumazet
106d4cada4aSEric Dumazet /* precompute partial secondary hash */
10730fff923SEric Dumazet udp_sk(sk)->udp_portaddr_hash = hash2_partial;
108fe38d2a1SJosef Bacik return udp_lib_get_port(sk, snum, hash2_nulladdr);
109db8dac20SDavid S. Miller }
110db8dac20SDavid S. Miller
udp_v6_rehash(struct sock * sk)111f7c46156SAlexey Kodanev void udp_v6_rehash(struct sock *sk)
112719f8358SEric Dumazet {
113f0b1e64cSMartin KaFai Lau u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
114efe4208fSEric Dumazet &sk->sk_v6_rcv_saddr,
115719f8358SEric Dumazet inet_sk(sk)->inet_num);
116719f8358SEric Dumazet
117719f8358SEric Dumazet udp_lib_rehash(sk, new_hash);
118719f8358SEric Dumazet }
119719f8358SEric Dumazet
compute_score(struct sock * sk,struct net * net,const struct in6_addr * saddr,__be16 sport,const struct in6_addr * daddr,unsigned short hnum,int dif,int sdif)120d1e37288SSu, Xuemin static int compute_score(struct sock *sk, struct net *net,
12188440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport,
122d1e37288SSu, Xuemin const struct in6_addr *daddr, unsigned short hnum,
12373545373STim Beale int dif, int sdif)
124db8dac20SDavid S. Miller {
1254c971d2fSEric Dumazet int bound_dev_if, score;
12660c04aecSJoe Perches struct inet_sock *inet;
1276da5b0f0SMike Manning bool dev_match;
128db8dac20SDavid S. Miller
12960c04aecSJoe Perches if (!net_eq(sock_net(sk), net) ||
13060c04aecSJoe Perches udp_sk(sk)->udp_port_hash != hnum ||
13160c04aecSJoe Perches sk->sk_family != PF_INET6)
13260c04aecSJoe Perches return -1;
133645ca708SEric Dumazet
13423b0269eSPeter Oskolkov if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
13523b0269eSPeter Oskolkov return -1;
13623b0269eSPeter Oskolkov
137645ca708SEric Dumazet score = 0;
13860c04aecSJoe Perches inet = inet_sk(sk);
13960c04aecSJoe Perches
140c720c7e8SEric Dumazet if (inet->inet_dport) {
141c720c7e8SEric Dumazet if (inet->inet_dport != sport)
142645ca708SEric Dumazet return -1;
143db8dac20SDavid S. Miller score++;
144db8dac20SDavid S. Miller }
14560c04aecSJoe Perches
146efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
147efe4208fSEric Dumazet if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
148645ca708SEric Dumazet return -1;
149db8dac20SDavid S. Miller score++;
150db8dac20SDavid S. Miller }
15160c04aecSJoe Perches
1524c971d2fSEric Dumazet bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
1534c971d2fSEric Dumazet dev_match = udp_sk_bound_dev_eq(net, bound_dev_if, dif, sdif);
15469678bcdSPaolo Abeni if (!dev_match)
155645ca708SEric Dumazet return -1;
1564c971d2fSEric Dumazet if (bound_dev_if)
157db8dac20SDavid S. Miller score++;
15860c04aecSJoe Perches
1597170a977SEric Dumazet if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
16070da268bSEric Dumazet score++;
16170da268bSEric Dumazet
162645ca708SEric Dumazet return score;
163645ca708SEric Dumazet }
164645ca708SEric Dumazet
165d1e37288SSu, Xuemin /* called with rcu_read_lock() */
udp6_lib_lookup2(struct net * net,const struct in6_addr * saddr,__be16 sport,const struct in6_addr * daddr,unsigned int hnum,int dif,int sdif,struct udp_hslot * hslot2,struct sk_buff * skb)166fddc17deSEric Dumazet static struct sock *udp6_lib_lookup2(struct net *net,
167fddc17deSEric Dumazet const struct in6_addr *saddr, __be16 sport,
1681801b570SDavid Ahern const struct in6_addr *daddr, unsigned int hnum,
16973545373STim Beale int dif, int sdif, struct udp_hslot *hslot2,
17073545373STim Beale struct sk_buff *skb)
171fddc17deSEric Dumazet {
172fddc17deSEric Dumazet struct sock *sk, *result;
173e94a62f5SPaolo Abeni int score, badness;
17471d865beSGabriel Krisman Bertazi bool need_rescore;
175fddc17deSEric Dumazet
176fddc17deSEric Dumazet result = NULL;
177fddc17deSEric Dumazet badness = -1;
178ca065d0cSEric Dumazet udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
17971d865beSGabriel Krisman Bertazi need_rescore = false;
18071d865beSGabriel Krisman Bertazi rescore:
18171d865beSGabriel Krisman Bertazi score = compute_score(need_rescore ? result : sk, net, saddr,
18271d865beSGabriel Krisman Bertazi sport, daddr, hnum, dif, sdif);
183fddc17deSEric Dumazet if (score > badness) {
184ca065d0cSEric Dumazet badness = score;
1850f495f76SLorenz Bauer
18671d865beSGabriel Krisman Bertazi if (need_rescore)
18771d865beSGabriel Krisman Bertazi continue;
18871d865beSGabriel Krisman Bertazi
1890f495f76SLorenz Bauer if (sk->sk_state == TCP_ESTABLISHED) {
1900f495f76SLorenz Bauer result = sk;
1910f495f76SLorenz Bauer continue;
1920f495f76SLorenz Bauer }
1930f495f76SLorenz Bauer
1940f495f76SLorenz Bauer result = inet6_lookup_reuseport(net, sk, skb, sizeof(struct udphdr),
1950f495f76SLorenz Bauer saddr, sport, daddr, hnum, udp6_ehashfn);
196f0ea27e7SLorenz Bauer if (!result) {
197f0ea27e7SLorenz Bauer result = sk;
198f0ea27e7SLorenz Bauer continue;
199f0ea27e7SLorenz Bauer }
200f0ea27e7SLorenz Bauer
201e32ea7e7SCraig Gallek /* Fall back to scoring if group has connections */
202f0ea27e7SLorenz Bauer if (!reuseport_has_conns(sk))
203e32ea7e7SCraig Gallek return result;
204ed0dfffdSEric Dumazet
205f0ea27e7SLorenz Bauer /* Reuseport logic returned an error, keep original score. */
206f0ea27e7SLorenz Bauer if (IS_ERR(result))
207f0ea27e7SLorenz Bauer continue;
208f0ea27e7SLorenz Bauer
20971d865beSGabriel Krisman Bertazi /* compute_score is too long of a function to be
21071d865beSGabriel Krisman Bertazi * inlined, and calling it again here yields
21171d865beSGabriel Krisman Bertazi * measureable overhead for some
21271d865beSGabriel Krisman Bertazi * workloads. Work around it by jumping
21371d865beSGabriel Krisman Bertazi * backwards to rescore 'result'.
21471d865beSGabriel Krisman Bertazi */
21571d865beSGabriel Krisman Bertazi need_rescore = true;
21671d865beSGabriel Krisman Bertazi goto rescore;
217fddc17deSEric Dumazet }
218fddc17deSEric Dumazet }
219fddc17deSEric Dumazet return result;
220fddc17deSEric Dumazet }
221fddc17deSEric Dumazet
222ca065d0cSEric Dumazet /* rcu_read_lock() must be held */
__udp6_lib_lookup(struct net * net,const struct in6_addr * saddr,__be16 sport,const struct in6_addr * daddr,__be16 dport,int dif,int sdif,struct udp_table * udptable,struct sk_buff * skb)223fce82338SPavel Emelyanov struct sock *__udp6_lib_lookup(struct net *net,
22488440ae7SBalazs Scheidler const struct in6_addr *saddr, __be16 sport,
22588440ae7SBalazs Scheidler const struct in6_addr *daddr, __be16 dport,
2261801b570SDavid Ahern int dif, int sdif, struct udp_table *udptable,
227538950a1SCraig Gallek struct sk_buff *skb)
228645ca708SEric Dumazet {
229645ca708SEric Dumazet unsigned short hnum = ntohs(dport);
23023b0269eSPeter Oskolkov unsigned int hash2, slot2;
23123b0269eSPeter Oskolkov struct udp_hslot *hslot2;
2326d4201b1SJakub Sitnicki struct sock *result, *sk;
233645ca708SEric Dumazet
234f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum);
235fddc17deSEric Dumazet slot2 = hash2 & udptable->mask;
236fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2];
237fddc17deSEric Dumazet
2386d4201b1SJakub Sitnicki /* Lookup connected or non-wildcard sockets */
239fddc17deSEric Dumazet result = udp6_lib_lookup2(net, saddr, sport,
24073545373STim Beale daddr, hnum, dif, sdif,
241d1e37288SSu, Xuemin hslot2, skb);
2426d4201b1SJakub Sitnicki if (!IS_ERR_OR_NULL(result) && result->sk_state == TCP_ESTABLISHED)
2436d4201b1SJakub Sitnicki goto done;
2446d4201b1SJakub Sitnicki
2456d4201b1SJakub Sitnicki /* Lookup redirect from BPF */
2466c886db2SLorenz Bauer if (static_branch_unlikely(&bpf_sk_lookup_enabled) &&
2476c886db2SLorenz Bauer udptable == net->ipv4.udp_table) {
2486c886db2SLorenz Bauer sk = inet6_lookup_run_sk_lookup(net, IPPROTO_UDP, skb, sizeof(struct udphdr),
2496c886db2SLorenz Bauer saddr, sport, daddr, hnum, dif,
2506c886db2SLorenz Bauer udp6_ehashfn);
2516d4201b1SJakub Sitnicki if (sk) {
2526d4201b1SJakub Sitnicki result = sk;
2536d4201b1SJakub Sitnicki goto done;
2546d4201b1SJakub Sitnicki }
2556d4201b1SJakub Sitnicki }
2566d4201b1SJakub Sitnicki
2576d4201b1SJakub Sitnicki /* Got non-wildcard socket or error on first lookup */
2586d4201b1SJakub Sitnicki if (result)
2596d4201b1SJakub Sitnicki goto done;
2606d4201b1SJakub Sitnicki
2616d4201b1SJakub Sitnicki /* Lookup wildcard sockets */
262f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
263fddc17deSEric Dumazet slot2 = hash2 & udptable->mask;
264fddc17deSEric Dumazet hslot2 = &udptable->hash2[slot2];
265fddc17deSEric Dumazet
2661223c67cSJorge Boncompte [DTI2] result = udp6_lib_lookup2(net, saddr, sport,
26723b0269eSPeter Oskolkov &in6addr_any, hnum, dif, sdif,
26873545373STim Beale hslot2, skb);
2696d4201b1SJakub Sitnicki done:
27026f8113cSEnrico Weigelt if (IS_ERR(result))
2718217ca65SMartin KaFai Lau return NULL;
272fddc17deSEric Dumazet return result;
273fddc17deSEric Dumazet }
274fce82338SPavel Emelyanov EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
275db8dac20SDavid S. Miller
__udp6_lib_lookup_skb(struct sk_buff * skb,__be16 sport,__be16 dport,struct udp_table * udptable)276607c4aafSKOVACS Krisztian static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
277607c4aafSKOVACS Krisztian __be16 sport, __be16 dport,
278645ca708SEric Dumazet struct udp_table *udptable)
279607c4aafSKOVACS Krisztian {
280b71d1d42SEric Dumazet const struct ipv6hdr *iph = ipv6_hdr(skb);
281607c4aafSKOVACS Krisztian
282ed7cbbceSAlexander Duyck return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
283607c4aafSKOVACS Krisztian &iph->daddr, dport, inet6_iif(skb),
2841801b570SDavid Ahern inet6_sdif(skb), udptable, skb);
285607c4aafSKOVACS Krisztian }
286607c4aafSKOVACS Krisztian
udp6_lib_lookup_skb(const struct sk_buff * skb,__be16 sport,__be16 dport)2877b58e63eSEric Dumazet struct sock *udp6_lib_lookup_skb(const struct sk_buff *skb,
28863058308STom Herbert __be16 sport, __be16 dport)
28963058308STom Herbert {
290af276a5aSRichard Gobert const u16 offset = NAPI_GRO_CB(skb)->network_offsets[skb->encapsulation];
291af276a5aSRichard Gobert const struct ipv6hdr *iph = (struct ipv6hdr *)(skb->data + offset);
292ba6aac15SKuniyuki Iwashima struct net *net = dev_net(skb->dev);
2937938cd15SRichard Gobert int iif, sdif;
2947938cd15SRichard Gobert
2957938cd15SRichard Gobert inet6_get_iif_sdif(skb, &iif, &sdif);
29663058308STom Herbert
297ba6aac15SKuniyuki Iwashima return __udp6_lib_lookup(net, &iph->saddr, sport,
2987938cd15SRichard Gobert &iph->daddr, dport, iif,
2997938cd15SRichard Gobert sdif, net->ipv4.udp_table, NULL);
30063058308STom Herbert }
30163058308STom Herbert
302ca065d0cSEric Dumazet /* Must be called under rcu_read_lock().
303ca065d0cSEric Dumazet * Does increment socket refcount.
304ca065d0cSEric Dumazet */
3056e86000cSArnd Bergmann #if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
udp6_lib_lookup(struct net * net,const struct in6_addr * saddr,__be16 sport,const struct in6_addr * daddr,__be16 dport,int dif)306aa976fc0SBalazs Scheidler struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
307aa976fc0SBalazs Scheidler const struct in6_addr *daddr, __be16 dport, int dif)
308aa976fc0SBalazs Scheidler {
309ca065d0cSEric Dumazet struct sock *sk;
310ca065d0cSEric Dumazet
311ca065d0cSEric Dumazet sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
312ba6aac15SKuniyuki Iwashima dif, 0, net->ipv4.udp_table, NULL);
31341c6d650SReshetova, Elena if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
314ca065d0cSEric Dumazet sk = NULL;
315ca065d0cSEric Dumazet return sk;
316aa976fc0SBalazs Scheidler }
317aa976fc0SBalazs Scheidler EXPORT_SYMBOL_GPL(udp6_lib_lookup);
318ca065d0cSEric Dumazet #endif
319aa976fc0SBalazs Scheidler
320cb891fa6SPaolo Abeni /* do not use the scratch area len for jumbogram: their length execeeds the
321cb891fa6SPaolo Abeni * scratch area space; note that the IP6CB flags is still in the first
322cb891fa6SPaolo Abeni * cacheline, so checking for jumbograms is cheap
323cb891fa6SPaolo Abeni */
udp6_skb_len(struct sk_buff * skb)324cb891fa6SPaolo Abeni static int udp6_skb_len(struct sk_buff *skb)
325cb891fa6SPaolo Abeni {
326cb891fa6SPaolo Abeni return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
327cb891fa6SPaolo Abeni }
328cb891fa6SPaolo Abeni
329db8dac20SDavid S. Miller /*
330db8dac20SDavid S. Miller * This should be easy, if there is something there we
331db8dac20SDavid S. Miller * return it, otherwise we block.
332db8dac20SDavid S. Miller */
333db8dac20SDavid S. Miller
udpv6_recvmsg(struct sock * sk,struct msghdr * msg,size_t len,int flags,int * addr_len)3341b784140SYing Xue int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
335ec095263SOliver Hartkopp int flags, int *addr_len)
336db8dac20SDavid S. Miller {
337db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk);
338db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk);
339db8dac20SDavid S. Miller struct sk_buff *skb;
34059c2cdaeSDavid S. Miller unsigned int ulen, copied;
341fd69c399SPaolo Abeni int off, err, peeking = flags & MSG_PEEK;
342db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk);
343543fc3fbSPaolo Abeni struct udp_mib __percpu *mib;
344197c949eSEric Dumazet bool checksum_valid = false;
345f26ba175SWei Yongjun int is_udp4;
346db8dac20SDavid S. Miller
347db8dac20SDavid S. Miller if (flags & MSG_ERRQUEUE)
34885fbaa75SHannes Frederic Sowa return ipv6_recv_error(sk, msg, len, addr_len);
349db8dac20SDavid S. Miller
3504b340ae2SBrian Haley if (np->rxpmtu && np->rxopt.bits.rxpmtu)
35185fbaa75SHannes Frederic Sowa return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
3524b340ae2SBrian Haley
353db8dac20SDavid S. Miller try_again:
354a0917e0bSMatthew Dawson off = sk_peek_offset(sk, flags);
355ec095263SOliver Hartkopp skb = __skb_recv_udp(sk, flags, &off, &err);
356db8dac20SDavid S. Miller if (!skb)
357627d2d6bSsamanthakumar return err;
358db8dac20SDavid S. Miller
359cb891fa6SPaolo Abeni ulen = udp6_skb_len(skb);
36059c2cdaeSDavid S. Miller copied = len;
361627d2d6bSsamanthakumar if (copied > ulen - off)
362627d2d6bSsamanthakumar copied = ulen - off;
36359c2cdaeSDavid S. Miller else if (copied < ulen)
364db8dac20SDavid S. Miller msg->msg_flags |= MSG_TRUNC;
365db8dac20SDavid S. Miller
366f26ba175SWei Yongjun is_udp4 = (skb->protocol == htons(ETH_P_IP));
367029a3743SPaolo Abeni mib = __UDPX_MIB(sk, is_udp4);
368f26ba175SWei Yongjun
369db8dac20SDavid S. Miller /*
370db8dac20SDavid S. Miller * If checksum is needed at all, try to do it while copying the
371db8dac20SDavid S. Miller * data. If the data is truncated, or if we only want a partial
372db8dac20SDavid S. Miller * coverage checksum (UDP-Lite), do it before the copy.
373db8dac20SDavid S. Miller */
374db8dac20SDavid S. Miller
375d21dbdfeSEric Dumazet if (copied < ulen || peeking ||
376d21dbdfeSEric Dumazet (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
37767a51780SPaolo Abeni checksum_valid = udp_skb_csum_unnecessary(skb) ||
37867a51780SPaolo Abeni !__udp_lib_checksum_complete(skb);
379197c949eSEric Dumazet if (!checksum_valid)
380db8dac20SDavid S. Miller goto csum_copy_err;
381db8dac20SDavid S. Miller }
382db8dac20SDavid S. Miller
38367a51780SPaolo Abeni if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
38467a51780SPaolo Abeni if (udp_skb_is_linear(skb))
38567a51780SPaolo Abeni err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
38667a51780SPaolo Abeni else
387627d2d6bSsamanthakumar err = skb_copy_datagram_msg(skb, off, msg, copied);
38867a51780SPaolo Abeni } else {
389627d2d6bSsamanthakumar err = skb_copy_and_csum_datagram_msg(skb, off, msg);
390db8dac20SDavid S. Miller if (err == -EINVAL)
391db8dac20SDavid S. Miller goto csum_copy_err;
392db8dac20SDavid S. Miller }
39322911fc5SEric Dumazet if (unlikely(err)) {
394fd69c399SPaolo Abeni if (!peeking) {
395979402b1SEric Dumazet atomic_inc(&sk->sk_drops);
396029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
397979402b1SEric Dumazet }
398850cbaddSPaolo Abeni kfree_skb(skb);
399627d2d6bSsamanthakumar return err;
40022911fc5SEric Dumazet }
401fd69c399SPaolo Abeni if (!peeking)
402029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
403db8dac20SDavid S. Miller
4046fd1d51cSErin MacNeil sock_recv_cmsgs(msg, sk, skb);
405db8dac20SDavid S. Miller
406db8dac20SDavid S. Miller /* Copy the address. */
407db8dac20SDavid S. Miller if (msg->msg_name) {
408342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
409db8dac20SDavid S. Miller sin6->sin6_family = AF_INET6;
410db8dac20SDavid S. Miller sin6->sin6_port = udp_hdr(skb)->source;
411db8dac20SDavid S. Miller sin6->sin6_flowinfo = 0;
412db8dac20SDavid S. Miller
413842df073SHannes Frederic Sowa if (is_udp4) {
414b301e82cSBrian Haley ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
415b301e82cSBrian Haley &sin6->sin6_addr);
416842df073SHannes Frederic Sowa sin6->sin6_scope_id = 0;
417842df073SHannes Frederic Sowa } else {
4184e3fd7a0SAlexey Dobriyan sin6->sin6_addr = ipv6_hdr(skb)->saddr;
419842df073SHannes Frederic Sowa sin6->sin6_scope_id =
420842df073SHannes Frederic Sowa ipv6_iface_scope_id(&sin6->sin6_addr,
4214330487aSDuan Jiong inet6_iif(skb));
422db8dac20SDavid S. Miller }
423bceaa902SHannes Frederic Sowa *addr_len = sizeof(*sin6);
424983695faSDaniel Borkmann
425983695faSDaniel Borkmann BPF_CGROUP_RUN_PROG_UDP6_RECVMSG_LOCK(sk,
4266d71331eSDaan De Meyer (struct sockaddr *)sin6,
4276d71331eSDaan De Meyer addr_len);
428db8dac20SDavid S. Miller }
4294b261c75SHannes Frederic Sowa
43067188b2eSEric Dumazet if (udp_test_bit(GRO_ENABLED, sk))
431bcd1665eSPaolo Abeni udp_cmsg_recv(msg, sk, skb);
432bcd1665eSPaolo Abeni
4334b261c75SHannes Frederic Sowa if (np->rxopt.all)
4344b261c75SHannes Frederic Sowa ip6_datagram_recv_common_ctl(sk, msg, skb);
4354b261c75SHannes Frederic Sowa
436f26ba175SWei Yongjun if (is_udp4) {
437c274af22SEric Dumazet if (inet_cmsg_flags(inet))
438ad959036SPaolo Abeni ip_cmsg_recv_offset(msg, sk, skb,
43910df8e61SEric Dumazet sizeof(struct udphdr), off);
440db8dac20SDavid S. Miller } else {
441db8dac20SDavid S. Miller if (np->rxopt.all)
4424b261c75SHannes Frederic Sowa ip6_datagram_recv_specific_ctl(sk, msg, skb);
443db8dac20SDavid S. Miller }
444db8dac20SDavid S. Miller
44559c2cdaeSDavid S. Miller err = copied;
446db8dac20SDavid S. Miller if (flags & MSG_TRUNC)
447db8dac20SDavid S. Miller err = ulen;
448db8dac20SDavid S. Miller
449850cbaddSPaolo Abeni skb_consume_udp(sk, skb, peeking ? -err : err);
450db8dac20SDavid S. Miller return err;
451db8dac20SDavid S. Miller
452db8dac20SDavid S. Miller csum_copy_err:
4532276f58aSPaolo Abeni if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
4542276f58aSPaolo Abeni udp_skb_destructor)) {
455029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
456029a3743SPaolo Abeni SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
4576a5dc9e5SEric Dumazet }
458850cbaddSPaolo Abeni kfree_skb(skb);
459db8dac20SDavid S. Miller
460beb39db5SEric Dumazet /* starting over for a new packet, but check if we need to yield */
461beb39db5SEric Dumazet cond_resched();
4629cfaa8deSXufeng Zhang msg->msg_flags &= ~MSG_TRUNC;
463db8dac20SDavid S. Miller goto try_again;
464db8dac20SDavid S. Miller }
465db8dac20SDavid S. Miller
4663001e7aaSAntoine Tenart DECLARE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
udpv6_encap_enable(void)467a36e185eSStefano Brivio void udpv6_encap_enable(void)
468a36e185eSStefano Brivio {
4699c480601SPaolo Abeni static_branch_inc(&udpv6_encap_needed_key);
470a36e185eSStefano Brivio }
471a36e185eSStefano Brivio EXPORT_SYMBOL(udpv6_encap_enable);
472a36e185eSStefano Brivio
473e7cc0824SStefano Brivio /* Handler for tunnels with arbitrary destination ports: no socket lookup, go
474e7cc0824SStefano Brivio * through error handlers in encapsulations looking for a match.
475e7cc0824SStefano Brivio */
__udp6_lib_err_encap_no_sk(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)476e7cc0824SStefano Brivio static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
477e7cc0824SStefano Brivio struct inet6_skb_parm *opt,
478424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info)
479e7cc0824SStefano Brivio {
480e7cc0824SStefano Brivio int i;
481e7cc0824SStefano Brivio
482e7cc0824SStefano Brivio for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
483e7cc0824SStefano Brivio int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
484424a7cd0SPaolo Abeni u8 type, u8 code, int offset, __be32 info);
485424a7cd0SPaolo Abeni const struct ip6_tnl_encap_ops *encap;
486e7cc0824SStefano Brivio
487424a7cd0SPaolo Abeni encap = rcu_dereference(ip6tun_encaps[i]);
488424a7cd0SPaolo Abeni if (!encap)
489e7cc0824SStefano Brivio continue;
490424a7cd0SPaolo Abeni handler = encap->err_handler;
491e7cc0824SStefano Brivio if (handler && !handler(skb, opt, type, code, offset, info))
492e7cc0824SStefano Brivio return 0;
493e7cc0824SStefano Brivio }
494e7cc0824SStefano Brivio
495e7cc0824SStefano Brivio return -ENOENT;
496e7cc0824SStefano Brivio }
497e7cc0824SStefano Brivio
498a36e185eSStefano Brivio /* Try to match ICMP errors to UDP tunnels by looking up a socket without
499a36e185eSStefano Brivio * reversing source and destination port: this will match tunnels that force the
500a36e185eSStefano Brivio * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
501a36e185eSStefano Brivio * lwtunnels might actually break this assumption by being configured with
502a36e185eSStefano Brivio * different destination ports on endpoints, in this case we won't be able to
503a36e185eSStefano Brivio * trace ICMP messages back to them.
504a36e185eSStefano Brivio *
505e7cc0824SStefano Brivio * If this doesn't match any socket, probe tunnels with arbitrary destination
506e7cc0824SStefano Brivio * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
507e7cc0824SStefano Brivio * we've sent packets to won't necessarily match the local destination port.
508e7cc0824SStefano Brivio *
509a36e185eSStefano Brivio * Then ask the tunnel implementation to match the error against a valid
510a36e185eSStefano Brivio * association.
511a36e185eSStefano Brivio *
512e7cc0824SStefano Brivio * Return an error if we can't find a match, the socket if we need further
513e7cc0824SStefano Brivio * processing, zero otherwise.
514a36e185eSStefano Brivio */
__udp6_lib_err_encap(struct net * net,const struct ipv6hdr * hdr,int offset,struct udphdr * uh,struct udp_table * udptable,struct sock * sk,struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,__be32 info)515a36e185eSStefano Brivio static struct sock *__udp6_lib_err_encap(struct net *net,
516a36e185eSStefano Brivio const struct ipv6hdr *hdr, int offset,
517a36e185eSStefano Brivio struct udphdr *uh,
518a36e185eSStefano Brivio struct udp_table *udptable,
5199bfce73cSVadim Fedorenko struct sock *sk,
520e7cc0824SStefano Brivio struct sk_buff *skb,
521e7cc0824SStefano Brivio struct inet6_skb_parm *opt,
522e7cc0824SStefano Brivio u8 type, u8 code, __be32 info)
523a36e185eSStefano Brivio {
5249bfce73cSVadim Fedorenko int (*lookup)(struct sock *sk, struct sk_buff *skb);
525a36e185eSStefano Brivio int network_offset, transport_offset;
5269bfce73cSVadim Fedorenko struct udp_sock *up;
527a36e185eSStefano Brivio
528a36e185eSStefano Brivio network_offset = skb_network_offset(skb);
529a36e185eSStefano Brivio transport_offset = skb_transport_offset(skb);
530a36e185eSStefano Brivio
531a36e185eSStefano Brivio /* Network header needs to point to the outer IPv6 header inside ICMP */
532a36e185eSStefano Brivio skb_reset_network_header(skb);
533a36e185eSStefano Brivio
534a36e185eSStefano Brivio /* Transport header needs to point to the UDP header */
535a36e185eSStefano Brivio skb_set_transport_header(skb, offset);
536a36e185eSStefano Brivio
5379bfce73cSVadim Fedorenko if (sk) {
5389bfce73cSVadim Fedorenko up = udp_sk(sk);
5399bfce73cSVadim Fedorenko
5409bfce73cSVadim Fedorenko lookup = READ_ONCE(up->encap_err_lookup);
5419bfce73cSVadim Fedorenko if (lookup && lookup(sk, skb))
5429bfce73cSVadim Fedorenko sk = NULL;
5439bfce73cSVadim Fedorenko
5449bfce73cSVadim Fedorenko goto out;
5459bfce73cSVadim Fedorenko }
5469bfce73cSVadim Fedorenko
547e7cc0824SStefano Brivio sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
548e7cc0824SStefano Brivio &hdr->saddr, uh->dest,
549e7cc0824SStefano Brivio inet6_iif(skb), 0, udptable, skb);
550e7cc0824SStefano Brivio if (sk) {
5519bfce73cSVadim Fedorenko up = udp_sk(sk);
552e7cc0824SStefano Brivio
553a36e185eSStefano Brivio lookup = READ_ONCE(up->encap_err_lookup);
554a36e185eSStefano Brivio if (!lookup || lookup(sk, skb))
555a36e185eSStefano Brivio sk = NULL;
556e7cc0824SStefano Brivio }
557e7cc0824SStefano Brivio
5589bfce73cSVadim Fedorenko out:
559e7cc0824SStefano Brivio if (!sk) {
560e7cc0824SStefano Brivio sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
561e7cc0824SStefano Brivio offset, info));
562e7cc0824SStefano Brivio }
563a36e185eSStefano Brivio
564a36e185eSStefano Brivio skb_set_transport_header(skb, transport_offset);
565a36e185eSStefano Brivio skb_set_network_header(skb, network_offset);
566e7cc0824SStefano Brivio
567a36e185eSStefano Brivio return sk;
568a36e185eSStefano Brivio }
569a36e185eSStefano Brivio
__udp6_lib_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info,struct udp_table * udptable)57032bbd879SStefano Brivio int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
571d5fdd6baSBrian Haley u8 type, u8 code, int offset, __be32 info,
572645ca708SEric Dumazet struct udp_table *udptable)
573db8dac20SDavid S. Miller {
574db8dac20SDavid S. Miller struct ipv6_pinfo *np;
575b71d1d42SEric Dumazet const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
576b71d1d42SEric Dumazet const struct in6_addr *saddr = &hdr->saddr;
577222a011eSAndrew Lunn const struct in6_addr *daddr = seg6_get_daddr(skb, opt) ? : &hdr->daddr;
578db8dac20SDavid S. Miller struct udphdr *uh = (struct udphdr *)(skb->data+offset);
579a36e185eSStefano Brivio bool tunnel = false;
580db8dac20SDavid S. Miller struct sock *sk;
581e0d8c1b7SWei Wang int harderr;
582db8dac20SDavid S. Miller int err;
5837304fe46SDuan Jiong struct net *net = dev_net(skb->dev);
584db8dac20SDavid S. Miller
585e32ea7e7SCraig Gallek sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
5864ac30c4bSMartin KaFai Lau inet6_iif(skb), inet6_sdif(skb), udptable, NULL);
5879bfce73cSVadim Fedorenko
5884781a75dSEric Dumazet if (!sk || READ_ONCE(udp_sk(sk)->encap_type)) {
589a36e185eSStefano Brivio /* No socket for error: try tunnels before discarding */
590a36e185eSStefano Brivio if (static_branch_unlikely(&udpv6_encap_needed_key)) {
591a36e185eSStefano Brivio sk = __udp6_lib_err_encap(net, hdr, offset, uh,
5929bfce73cSVadim Fedorenko udptable, sk, skb,
593e7cc0824SStefano Brivio opt, type, code, info);
594e7cc0824SStefano Brivio if (!sk)
595e7cc0824SStefano Brivio return 0;
5969bfce73cSVadim Fedorenko } else
5979bfce73cSVadim Fedorenko sk = ERR_PTR(-ENOENT);
598a36e185eSStefano Brivio
599e7cc0824SStefano Brivio if (IS_ERR(sk)) {
600a16292a0SEric Dumazet __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
6017304fe46SDuan Jiong ICMP6_MIB_INERRORS);
602e7cc0824SStefano Brivio return PTR_ERR(sk);
6037304fe46SDuan Jiong }
604e7cc0824SStefano Brivio
605a36e185eSStefano Brivio tunnel = true;
606a36e185eSStefano Brivio }
607db8dac20SDavid S. Miller
608e0d8c1b7SWei Wang harderr = icmpv6_err_convert(type, code, &err);
609e0d8c1b7SWei Wang np = inet6_sk(sk);
610e0d8c1b7SWei Wang
61193b36cf3SHannes Frederic Sowa if (type == ICMPV6_PKT_TOOBIG) {
61293b36cf3SHannes Frederic Sowa if (!ip6_sk_accept_pmtu(sk))
61393b36cf3SHannes Frederic Sowa goto out;
61481aded24SDavid S. Miller ip6_sk_update_pmtu(skb, sk, info);
615e0d8c1b7SWei Wang if (np->pmtudisc != IPV6_PMTUDISC_DONT)
616e0d8c1b7SWei Wang harderr = 1;
61793b36cf3SHannes Frederic Sowa }
6181a462d18SDuan Jiong if (type == NDISC_REDIRECT) {
619a36e185eSStefano Brivio if (tunnel) {
620a36e185eSStefano Brivio ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
6213c5b4d69SEric Dumazet READ_ONCE(sk->sk_mark), sk->sk_uid);
622a36e185eSStefano Brivio } else {
623ec18d9a2SDavid S. Miller ip6_sk_redirect(skb, sk);
624a36e185eSStefano Brivio }
6251a462d18SDuan Jiong goto out;
6261a462d18SDuan Jiong }
62781aded24SDavid S. Miller
628a36e185eSStefano Brivio /* Tunnels don't have an application socket: don't pass errors back */
629ac56a0b4SDavid Howells if (tunnel) {
630ac56a0b4SDavid Howells if (udp_sk(sk)->encap_err_rcv)
63142fb06b3SDavid Howells udp_sk(sk)->encap_err_rcv(sk, skb, err, uh->dest,
63242fb06b3SDavid Howells ntohl(info), (u8 *)(uh+1));
633a36e185eSStefano Brivio goto out;
634ac56a0b4SDavid Howells }
635a36e185eSStefano Brivio
636e0d8c1b7SWei Wang if (!np->recverr) {
637e0d8c1b7SWei Wang if (!harderr || sk->sk_state != TCP_ESTABLISHED)
638db8dac20SDavid S. Miller goto out;
639e0d8c1b7SWei Wang } else {
640db8dac20SDavid S. Miller ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
641e0d8c1b7SWei Wang }
642b1faf566SEric Dumazet
643db8dac20SDavid S. Miller sk->sk_err = err;
644e3ae2365SAlexander Aring sk_error_report(sk);
645db8dac20SDavid S. Miller out:
64632bbd879SStefano Brivio return 0;
647db8dac20SDavid S. Miller }
648db8dac20SDavid S. Miller
__udpv6_queue_rcv_skb(struct sock * sk,struct sk_buff * skb)649a3f96c47SPaolo Abeni static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
650f7ad74feSBenjamin LaHaise {
651f7ad74feSBenjamin LaHaise int rc;
652f7ad74feSBenjamin LaHaise
653efe4208fSEric Dumazet if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
654f7ad74feSBenjamin LaHaise sock_rps_save_rxhash(sk, skb);
655005ec974SShawn Bohrer sk_mark_napi_id(sk, skb);
6562c8c56e1SEric Dumazet sk_incoming_cpu_update(sk);
657e68b6e50SEric Dumazet } else {
658e68b6e50SEric Dumazet sk_mark_napi_id_once(sk, skb);
659005ec974SShawn Bohrer }
660f7ad74feSBenjamin LaHaise
661850cbaddSPaolo Abeni rc = __udp_enqueue_schedule_skb(sk, skb);
662f7ad74feSBenjamin LaHaise if (rc < 0) {
663f7ad74feSBenjamin LaHaise int is_udplite = IS_UDPLITE(sk);
6640d92efdeSDonald Hunter enum skb_drop_reason drop_reason;
665f7ad74feSBenjamin LaHaise
666f7ad74feSBenjamin LaHaise /* Note that an ENOMEM error is charged twice */
6670d92efdeSDonald Hunter if (rc == -ENOMEM) {
668e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk),
669f7ad74feSBenjamin LaHaise UDP_MIB_RCVBUFERRORS, is_udplite);
6700d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_RCVBUFF;
6710d92efdeSDonald Hunter } else {
672a3ce2b10SMenglong Dong UDP6_INC_STATS(sock_net(sk),
673a3ce2b10SMenglong Dong UDP_MIB_MEMERRORS, is_udplite);
6740d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_PROTO_MEM;
6750d92efdeSDonald Hunter }
676e61da9e2SEric Dumazet UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
6770d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason);
6788139dccdSIvan Babrou trace_udp_fail_queue_rcv_skb(rc, sk);
679f7ad74feSBenjamin LaHaise return -1;
680f7ad74feSBenjamin LaHaise }
681850cbaddSPaolo Abeni
682f7ad74feSBenjamin LaHaise return 0;
683f7ad74feSBenjamin LaHaise }
684f7ad74feSBenjamin LaHaise
udpv6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,u8 type,u8 code,int offset,__be32 info)68532bbd879SStefano Brivio static __inline__ int udpv6_err(struct sk_buff *skb,
686d5fdd6baSBrian Haley struct inet6_skb_parm *opt, u8 type,
687d5fdd6baSBrian Haley u8 code, int offset, __be32 info)
688db8dac20SDavid S. Miller {
689ba6aac15SKuniyuki Iwashima return __udp6_lib_err(skb, opt, type, code, offset, info,
690ba6aac15SKuniyuki Iwashima dev_net(skb->dev)->ipv4.udp_table);
691db8dac20SDavid S. Miller }
692db8dac20SDavid S. Miller
udpv6_queue_rcv_one_skb(struct sock * sk,struct sk_buff * skb)693cf329aa4SPaolo Abeni static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
694db8dac20SDavid S. Miller {
6950d92efdeSDonald Hunter enum skb_drop_reason drop_reason = SKB_DROP_REASON_NOT_SPECIFIED;
696db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk);
697db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk);
698db8dac20SDavid S. Miller
6990d92efdeSDonald Hunter if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
7000d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_XFRM_POLICY;
701db8dac20SDavid S. Miller goto drop;
7020d92efdeSDonald Hunter }
703b0e214d2SMadhu Koriginja nf_reset_ct(skb);
704db8dac20SDavid S. Miller
7054781a75dSEric Dumazet if (static_branch_unlikely(&udpv6_encap_needed_key) &&
7064781a75dSEric Dumazet READ_ONCE(up->encap_type)) {
707d7f3f621SBenjamin LaHaise int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
708d7f3f621SBenjamin LaHaise
709d7f3f621SBenjamin LaHaise /*
710d7f3f621SBenjamin LaHaise * This is an encapsulation socket so pass the skb to
711d7f3f621SBenjamin LaHaise * the socket's udp_encap_rcv() hook. Otherwise, just
712d7f3f621SBenjamin LaHaise * fall through and pass this up the UDP socket.
713d7f3f621SBenjamin LaHaise * up->encap_rcv() returns the following value:
714d7f3f621SBenjamin LaHaise * =0 if skb was successfully passed to the encap
715d7f3f621SBenjamin LaHaise * handler or was discarded by it.
716d7f3f621SBenjamin LaHaise * >0 if skb should be passed on to UDP.
717d7f3f621SBenjamin LaHaise * <0 if skb should be resubmitted as proto -N
718d7f3f621SBenjamin LaHaise */
719d7f3f621SBenjamin LaHaise
720d7f3f621SBenjamin LaHaise /* if we're overly short, let UDP handle it */
7216aa7de05SMark Rutland encap_rcv = READ_ONCE(up->encap_rcv);
722e5aed006SHannes Frederic Sowa if (encap_rcv) {
723d7f3f621SBenjamin LaHaise int ret;
724d7f3f621SBenjamin LaHaise
7250a80966bSTom Herbert /* Verify checksum before giving to encap */
7260a80966bSTom Herbert if (udp_lib_checksum_complete(skb))
7270a80966bSTom Herbert goto csum_error;
7280a80966bSTom Herbert
729d7f3f621SBenjamin LaHaise ret = encap_rcv(sk, skb);
730d7f3f621SBenjamin LaHaise if (ret <= 0) {
731250962e4SMenglong Dong __UDP6_INC_STATS(sock_net(sk),
732d7f3f621SBenjamin LaHaise UDP_MIB_INDATAGRAMS,
733d7f3f621SBenjamin LaHaise is_udplite);
734d7f3f621SBenjamin LaHaise return -ret;
735d7f3f621SBenjamin LaHaise }
736d7f3f621SBenjamin LaHaise }
737d7f3f621SBenjamin LaHaise
738d7f3f621SBenjamin LaHaise /* FALLTHROUGH -- it's a UDP Packet */
739d7f3f621SBenjamin LaHaise }
740d7f3f621SBenjamin LaHaise
741db8dac20SDavid S. Miller /*
742db8dac20SDavid S. Miller * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
743db8dac20SDavid S. Miller */
744257e8d2eSEric Dumazet if (udp_test_bit(UDPLITE_RECV_CC, sk) && UDP_SKB_CB(skb)->partial_cov) {
745257e8d2eSEric Dumazet u16 pcrlen = READ_ONCE(up->pcrlen);
746db8dac20SDavid S. Miller
747257e8d2eSEric Dumazet if (pcrlen == 0) { /* full coverage was set */
748ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
749db8dac20SDavid S. Miller UDP_SKB_CB(skb)->cscov, skb->len);
750db8dac20SDavid S. Miller goto drop;
751db8dac20SDavid S. Miller }
752257e8d2eSEric Dumazet if (UDP_SKB_CB(skb)->cscov < pcrlen) {
753ba7a46f1SJoe Perches net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
754257e8d2eSEric Dumazet UDP_SKB_CB(skb)->cscov, pcrlen);
755db8dac20SDavid S. Miller goto drop;
756db8dac20SDavid S. Miller }
757db8dac20SDavid S. Miller }
758db8dac20SDavid S. Miller
7594b943faeSPaolo Abeni prefetch(&sk->sk_rmem_alloc);
760ce25d66aSEric Dumazet if (rcu_access_pointer(sk->sk_filter) &&
761ce25d66aSEric Dumazet udp_lib_checksum_complete(skb))
7626a5dc9e5SEric Dumazet goto csum_error;
763ce25d66aSEric Dumazet
7640d92efdeSDonald Hunter if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr))) {
7650d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
766a6127697SMichal Kubeček goto drop;
7670d92efdeSDonald Hunter }
768db8dac20SDavid S. Miller
769e6afc8acSsamanthakumar udp_csum_pull_header(skb);
770cb80ef46SBenjamin LaHaise
771d826eb14SEric Dumazet skb_dst_drop(skb);
772db8dac20SDavid S. Miller
773850cbaddSPaolo Abeni return __udpv6_queue_rcv_skb(sk, skb);
7743e215c8dSJames M Leddy
7756a5dc9e5SEric Dumazet csum_error:
7760d92efdeSDonald Hunter drop_reason = SKB_DROP_REASON_UDP_CSUM;
77702c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
778db8dac20SDavid S. Miller drop:
77902c22347SEric Dumazet __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
780cb80ef46SBenjamin LaHaise atomic_inc(&sk->sk_drops);
7810d92efdeSDonald Hunter kfree_skb_reason(skb, drop_reason);
782db8dac20SDavid S. Miller return -1;
783db8dac20SDavid S. Miller }
784db8dac20SDavid S. Miller
udpv6_queue_rcv_skb(struct sock * sk,struct sk_buff * skb)785cf329aa4SPaolo Abeni static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
786cf329aa4SPaolo Abeni {
787cf329aa4SPaolo Abeni struct sk_buff *next, *segs;
788cf329aa4SPaolo Abeni int ret;
789cf329aa4SPaolo Abeni
790cf329aa4SPaolo Abeni if (likely(!udp_unexpected_gso(sk, skb)))
791cf329aa4SPaolo Abeni return udpv6_queue_rcv_one_skb(sk, skb);
792cf329aa4SPaolo Abeni
793cf329aa4SPaolo Abeni __skb_push(skb, -skb_mac_offset(skb));
794cf329aa4SPaolo Abeni segs = udp_rcv_segment(sk, skb, false);
7951a186c14SJason A. Donenfeld skb_list_walk_safe(segs, skb, next) {
796cf329aa4SPaolo Abeni __skb_pull(skb, skb_transport_offset(skb));
797cf329aa4SPaolo Abeni
798000ac44dSPaolo Abeni udp_post_segment_fix_csum(skb);
799cf329aa4SPaolo Abeni ret = udpv6_queue_rcv_one_skb(sk, skb);
800cf329aa4SPaolo Abeni if (ret > 0)
801cf329aa4SPaolo Abeni ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
802cf329aa4SPaolo Abeni true);
803cf329aa4SPaolo Abeni }
804cf329aa4SPaolo Abeni return 0;
805cf329aa4SPaolo Abeni }
806cf329aa4SPaolo Abeni
__udp_v6_is_mcast_sock(struct net * net,const struct sock * sk,__be16 loc_port,const struct in6_addr * loc_addr,__be16 rmt_port,const struct in6_addr * rmt_addr,int dif,int sdif,unsigned short hnum)807dc3731baSEric Dumazet static bool __udp_v6_is_mcast_sock(struct net *net, const struct sock *sk,
808b71d1d42SEric Dumazet __be16 loc_port, const struct in6_addr *loc_addr,
809b71d1d42SEric Dumazet __be16 rmt_port, const struct in6_addr *rmt_addr,
8107bd2db40SDewi Morgan int dif, int sdif, unsigned short hnum)
811db8dac20SDavid S. Miller {
812dc3731baSEric Dumazet const struct inet_sock *inet = inet_sk(sk);
813db8dac20SDavid S. Miller
8149e89fd8bSSven Wegener if (!net_eq(sock_net(sk), net))
8155cf3d461SDavid Held return false;
816b8ad0cbcSDaniel Lezcano
8175cf3d461SDavid Held if (udp_sk(sk)->udp_port_hash != hnum ||
8185cf3d461SDavid Held sk->sk_family != PF_INET6 ||
8195cf3d461SDavid Held (inet->inet_dport && inet->inet_dport != rmt_port) ||
8205cf3d461SDavid Held (!ipv6_addr_any(&sk->sk_v6_daddr) &&
8215cf3d461SDavid Held !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
8224c971d2fSEric Dumazet !udp_sk_bound_dev_eq(net, READ_ONCE(sk->sk_bound_dev_if), dif, sdif) ||
82333b4b015SHenning Rogge (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
82433b4b015SHenning Rogge !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
8255cf3d461SDavid Held return false;
8269e89fd8bSSven Wegener if (!inet6_mc_check(sk, loc_addr, rmt_addr))
8275cf3d461SDavid Held return false;
8285cf3d461SDavid Held return true;
829db8dac20SDavid S. Miller }
830db8dac20SDavid S. Miller
udp6_csum_zero_error(struct sk_buff * skb)8314068579eSTom Herbert static void udp6_csum_zero_error(struct sk_buff *skb)
8324068579eSTom Herbert {
8334068579eSTom Herbert /* RFC 2460 section 8.1 says that we SHOULD log
8344068579eSTom Herbert * this error. Well, it is reasonable.
8354068579eSTom Herbert */
836ba7a46f1SJoe Perches net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
8374068579eSTom Herbert &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
8384068579eSTom Herbert &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
8394068579eSTom Herbert }
8404068579eSTom Herbert
841db8dac20SDavid S. Miller /*
842db8dac20SDavid S. Miller * Note: called only from the BH handler context,
843db8dac20SDavid S. Miller * so we don't need to lock the hashes.
844db8dac20SDavid S. Miller */
__udp6_lib_mcast_deliver(struct net * net,struct sk_buff * skb,const struct in6_addr * saddr,const struct in6_addr * daddr,struct udp_table * udptable,int proto)845e3163493SPavel Emelyanov static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
846b71d1d42SEric Dumazet const struct in6_addr *saddr, const struct in6_addr *daddr,
84736cbb245SRick Jones struct udp_table *udptable, int proto)
848db8dac20SDavid S. Miller {
849ca065d0cSEric Dumazet struct sock *sk, *first = NULL;
850db8dac20SDavid S. Miller const struct udphdr *uh = udp_hdr(skb);
8515cf3d461SDavid Held unsigned short hnum = ntohs(uh->dest);
8525cf3d461SDavid Held struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
853ca065d0cSEric Dumazet unsigned int offset = offsetof(typeof(*sk), sk_node);
8542dc41cffSDavid Held unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
855ca065d0cSEric Dumazet int dif = inet6_iif(skb);
8567bd2db40SDewi Morgan int sdif = inet6_sdif(skb);
857ca065d0cSEric Dumazet struct hlist_node *node;
858ca065d0cSEric Dumazet struct sk_buff *nskb;
8592dc41cffSDavid Held
8602dc41cffSDavid Held if (use_hash2) {
861f0b1e64cSMartin KaFai Lau hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
86273e2d5e3SPablo Neira udptable->mask;
863f0b1e64cSMartin KaFai Lau hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
8642dc41cffSDavid Held start_lookup:
86573e2d5e3SPablo Neira hslot = &udptable->hash2[hash2];
8662dc41cffSDavid Held offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
8672dc41cffSDavid Held }
868db8dac20SDavid S. Miller
869ca065d0cSEric Dumazet sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
870ca065d0cSEric Dumazet if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
8717bd2db40SDewi Morgan uh->source, saddr, dif, sdif,
8727bd2db40SDewi Morgan hnum))
873ca065d0cSEric Dumazet continue;
8741c19448cSTom Herbert /* If zero checksum and no_check is not on for
8754068579eSTom Herbert * the socket then skip it.
8764068579eSTom Herbert */
87706de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk))
878ca065d0cSEric Dumazet continue;
879ca065d0cSEric Dumazet if (!first) {
880ca065d0cSEric Dumazet first = sk;
881ca065d0cSEric Dumazet continue;
882db8dac20SDavid S. Miller }
883ca065d0cSEric Dumazet nskb = skb_clone(skb, GFP_ATOMIC);
884ca065d0cSEric Dumazet if (unlikely(!nskb)) {
885ca065d0cSEric Dumazet atomic_inc(&sk->sk_drops);
88602c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
887ca065d0cSEric Dumazet IS_UDPLITE(sk));
88802c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
889ca065d0cSEric Dumazet IS_UDPLITE(sk));
890ca065d0cSEric Dumazet continue;
891a1ab77f9SEric Dumazet }
892db8dac20SDavid S. Miller
893ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(sk, nskb) > 0)
894ca065d0cSEric Dumazet consume_skb(nskb);
895ca065d0cSEric Dumazet }
896a1ab77f9SEric Dumazet
8972dc41cffSDavid Held /* Also lookup *:port if we are using hash2 and haven't done so yet. */
8982dc41cffSDavid Held if (use_hash2 && hash2 != hash2_any) {
8992dc41cffSDavid Held hash2 = hash2_any;
9002dc41cffSDavid Held goto start_lookup;
9012dc41cffSDavid Held }
9022dc41cffSDavid Held
903ca065d0cSEric Dumazet if (first) {
904ca065d0cSEric Dumazet if (udpv6_queue_rcv_skb(first, skb) > 0)
905ca065d0cSEric Dumazet consume_skb(skb);
906a1ab77f9SEric Dumazet } else {
907ca065d0cSEric Dumazet kfree_skb(skb);
90802c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
90936cbb245SRick Jones proto == IPPROTO_UDPLITE);
910a1ab77f9SEric Dumazet }
911db8dac20SDavid S. Miller return 0;
912db8dac20SDavid S. Miller }
913db8dac20SDavid S. Miller
udp6_sk_rx_dst_set(struct sock * sk,struct dst_entry * dst)91464f0f5d1SPaolo Abeni static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
91564f0f5d1SPaolo Abeni {
916797a4c1fSEric Dumazet if (udp_sk_rx_dst_set(sk, dst))
917797a4c1fSEric Dumazet sk->sk_rx_dst_cookie = rt6_get_cookie(dst_rt6_info(dst));
91864f0f5d1SPaolo Abeni }
91964f0f5d1SPaolo Abeni
920eb63f296SPaolo Abeni /* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
921eb63f296SPaolo Abeni * return code conversion for ip layer consumption
922eb63f296SPaolo Abeni */
udp6_unicast_rcv_skb(struct sock * sk,struct sk_buff * skb,struct udphdr * uh)923eb63f296SPaolo Abeni static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
924eb63f296SPaolo Abeni struct udphdr *uh)
925eb63f296SPaolo Abeni {
926eb63f296SPaolo Abeni int ret;
927eb63f296SPaolo Abeni
928eb63f296SPaolo Abeni if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
929e4aa33adSLi RongQing skb_checksum_try_convert(skb, IPPROTO_UDP, ip6_compute_pseudo);
930eb63f296SPaolo Abeni
931eb63f296SPaolo Abeni ret = udpv6_queue_rcv_skb(sk, skb);
932eb63f296SPaolo Abeni
93384dad559SPaolo Abeni /* a return value > 0 means to resubmit the input */
934eb63f296SPaolo Abeni if (ret > 0)
93584dad559SPaolo Abeni return ret;
936eb63f296SPaolo Abeni return 0;
937eb63f296SPaolo Abeni }
938eb63f296SPaolo Abeni
__udp6_lib_rcv(struct sk_buff * skb,struct udp_table * udptable,int proto)939645ca708SEric Dumazet int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
940db8dac20SDavid S. Miller int proto)
941db8dac20SDavid S. Miller {
9424cf91f82SDavid Ahern enum skb_drop_reason reason = SKB_DROP_REASON_NOT_SPECIFIED;
943b71d1d42SEric Dumazet const struct in6_addr *saddr, *daddr;
944ca065d0cSEric Dumazet struct net *net = dev_net(skb->dev);
945ca065d0cSEric Dumazet struct udphdr *uh;
946ca065d0cSEric Dumazet struct sock *sk;
94771489e21SJoe Stringer bool refcounted;
948db8dac20SDavid S. Miller u32 ulen = 0;
949db8dac20SDavid S. Miller
950db8dac20SDavid S. Miller if (!pskb_may_pull(skb, sizeof(struct udphdr)))
951d6bc0149SBjørn Mork goto discard;
952db8dac20SDavid S. Miller
953db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr;
954db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr;
955db8dac20SDavid S. Miller uh = udp_hdr(skb);
956db8dac20SDavid S. Miller
957db8dac20SDavid S. Miller ulen = ntohs(uh->len);
958db8dac20SDavid S. Miller if (ulen > skb->len)
959db8dac20SDavid S. Miller goto short_packet;
960db8dac20SDavid S. Miller
961db8dac20SDavid S. Miller if (proto == IPPROTO_UDP) {
962db8dac20SDavid S. Miller /* UDP validates ulen. */
963db8dac20SDavid S. Miller
964db8dac20SDavid S. Miller /* Check for jumbo payload */
965db8dac20SDavid S. Miller if (ulen == 0)
966db8dac20SDavid S. Miller ulen = skb->len;
967db8dac20SDavid S. Miller
968db8dac20SDavid S. Miller if (ulen < sizeof(*uh))
969db8dac20SDavid S. Miller goto short_packet;
970db8dac20SDavid S. Miller
971db8dac20SDavid S. Miller if (ulen < skb->len) {
972db8dac20SDavid S. Miller if (pskb_trim_rcsum(skb, ulen))
973db8dac20SDavid S. Miller goto short_packet;
974db8dac20SDavid S. Miller saddr = &ipv6_hdr(skb)->saddr;
975db8dac20SDavid S. Miller daddr = &ipv6_hdr(skb)->daddr;
976db8dac20SDavid S. Miller uh = udp_hdr(skb);
977db8dac20SDavid S. Miller }
978db8dac20SDavid S. Miller }
979db8dac20SDavid S. Miller
980db8dac20SDavid S. Miller if (udp6_csum_init(skb, uh, proto))
9816a5dc9e5SEric Dumazet goto csum_error;
982db8dac20SDavid S. Miller
983c9f2c1aeSPaolo Abeni /* Check if the socket is already available, e.g. due to early demux */
9849c02bec9SLorenz Bauer sk = inet6_steal_sock(net, skb, sizeof(struct udphdr), saddr, uh->source, daddr, uh->dest,
9859c02bec9SLorenz Bauer &refcounted, udp6_ehashfn);
9869c02bec9SLorenz Bauer if (IS_ERR(sk))
9879c02bec9SLorenz Bauer goto no_sk;
9889c02bec9SLorenz Bauer
989c9f2c1aeSPaolo Abeni if (sk) {
990c9f2c1aeSPaolo Abeni struct dst_entry *dst = skb_dst(skb);
991c9f2c1aeSPaolo Abeni int ret;
992c9f2c1aeSPaolo Abeni
9938f905c0eSEric Dumazet if (unlikely(rcu_dereference(sk->sk_rx_dst) != dst))
99464f0f5d1SPaolo Abeni udp6_sk_rx_dst_set(sk, dst);
995c9f2c1aeSPaolo Abeni
99606de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk)) {
99771489e21SJoe Stringer if (refcounted)
998c9f2c1aeSPaolo Abeni sock_put(sk);
999eb63f296SPaolo Abeni goto report_csum_error;
1000eb63f296SPaolo Abeni }
1001c9f2c1aeSPaolo Abeni
1002eb63f296SPaolo Abeni ret = udp6_unicast_rcv_skb(sk, skb, uh);
100371489e21SJoe Stringer if (refcounted)
1004eb63f296SPaolo Abeni sock_put(sk);
1005c9f2c1aeSPaolo Abeni return ret;
1006c9f2c1aeSPaolo Abeni }
1007c9f2c1aeSPaolo Abeni
1008db8dac20SDavid S. Miller /*
1009db8dac20SDavid S. Miller * Multicast receive code
1010db8dac20SDavid S. Miller */
1011db8dac20SDavid S. Miller if (ipv6_addr_is_multicast(daddr))
1012e3163493SPavel Emelyanov return __udp6_lib_mcast_deliver(net, skb,
101336cbb245SRick Jones saddr, daddr, udptable, proto);
1014db8dac20SDavid S. Miller
1015db8dac20SDavid S. Miller /* Unicast */
1016607c4aafSKOVACS Krisztian sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
101753b24b8fSIan Morris if (sk) {
101806de37fcSEric Dumazet if (!uh->check && !udp_get_no_check6_rx(sk))
1019eb63f296SPaolo Abeni goto report_csum_error;
1020eb63f296SPaolo Abeni return udp6_unicast_rcv_skb(sk, skb, uh);
10214068579eSTom Herbert }
10229c02bec9SLorenz Bauer no_sk:
10234cf91f82SDavid Ahern reason = SKB_DROP_REASON_NO_SOCKET;
10244cf91f82SDavid Ahern
1025eb63f296SPaolo Abeni if (!uh->check)
1026eb63f296SPaolo Abeni goto report_csum_error;
10274068579eSTom Herbert
1028db8dac20SDavid S. Miller if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
1029db8dac20SDavid S. Miller goto discard;
1030b0e214d2SMadhu Koriginja nf_reset_ct(skb);
1031db8dac20SDavid S. Miller
1032db8dac20SDavid S. Miller if (udp_lib_checksum_complete(skb))
10336a5dc9e5SEric Dumazet goto csum_error;
1034db8dac20SDavid S. Miller
103502c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
10363ffe533cSAlexey Dobriyan icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
1037db8dac20SDavid S. Miller
10384cf91f82SDavid Ahern kfree_skb_reason(skb, reason);
1039db8dac20SDavid S. Miller return 0;
1040db8dac20SDavid S. Miller
1041db8dac20SDavid S. Miller short_packet:
10424cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
10434cf91f82SDavid Ahern reason = SKB_DROP_REASON_PKT_TOO_SMALL;
1044ba7a46f1SJoe Perches net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
1045db8dac20SDavid S. Miller proto == IPPROTO_UDPLITE ? "-Lite" : "",
1046ba7a46f1SJoe Perches saddr, ntohs(uh->source),
1047ba7a46f1SJoe Perches ulen, skb->len,
1048ba7a46f1SJoe Perches daddr, ntohs(uh->dest));
10496a5dc9e5SEric Dumazet goto discard;
1050eb63f296SPaolo Abeni
1051eb63f296SPaolo Abeni report_csum_error:
1052eb63f296SPaolo Abeni udp6_csum_zero_error(skb);
10536a5dc9e5SEric Dumazet csum_error:
10544cf91f82SDavid Ahern if (reason == SKB_DROP_REASON_NOT_SPECIFIED)
10554cf91f82SDavid Ahern reason = SKB_DROP_REASON_UDP_CSUM;
105602c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1057db8dac20SDavid S. Miller discard:
105802c22347SEric Dumazet __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
10594cf91f82SDavid Ahern kfree_skb_reason(skb, reason);
1060db8dac20SDavid S. Miller return 0;
1061db8dac20SDavid S. Miller }
1062db8dac20SDavid S. Miller
10630bd84065Ssubashab@codeaurora.org
__udp6_lib_demux_lookup(struct net * net,__be16 loc_port,const struct in6_addr * loc_addr,__be16 rmt_port,const struct in6_addr * rmt_addr,int dif,int sdif)10645425077dSsubashab@codeaurora.org static struct sock *__udp6_lib_demux_lookup(struct net *net,
10655425077dSsubashab@codeaurora.org __be16 loc_port, const struct in6_addr *loc_addr,
10665425077dSsubashab@codeaurora.org __be16 rmt_port, const struct in6_addr *rmt_addr,
10674297a0efSDavid Ahern int dif, int sdif)
10685425077dSsubashab@codeaurora.org {
1069ba6aac15SKuniyuki Iwashima struct udp_table *udptable = net->ipv4.udp_table;
10700bd84065Ssubashab@codeaurora.org unsigned short hnum = ntohs(loc_port);
1071919dfa0bSKuniyuki Iwashima unsigned int hash2, slot2;
1072919dfa0bSKuniyuki Iwashima struct udp_hslot *hslot2;
1073919dfa0bSKuniyuki Iwashima __portpair ports;
10745425077dSsubashab@codeaurora.org struct sock *sk;
10755425077dSsubashab@codeaurora.org
1076919dfa0bSKuniyuki Iwashima hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
1077ba6aac15SKuniyuki Iwashima slot2 = hash2 & udptable->mask;
1078ba6aac15SKuniyuki Iwashima hslot2 = &udptable->hash2[slot2];
1079919dfa0bSKuniyuki Iwashima ports = INET_COMBINED_PORTS(rmt_port, hnum);
1080919dfa0bSKuniyuki Iwashima
10810bd84065Ssubashab@codeaurora.org udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
108285cb73ffSWei Wang if (sk->sk_state == TCP_ESTABLISHED &&
10835d368f03SEric Dumazet inet6_match(net, sk, rmt_addr, loc_addr, ports, dif, sdif))
10845425077dSsubashab@codeaurora.org return sk;
10850bd84065Ssubashab@codeaurora.org /* Only check first socket in chain */
10860bd84065Ssubashab@codeaurora.org break;
10870bd84065Ssubashab@codeaurora.org }
10880bd84065Ssubashab@codeaurora.org return NULL;
10895425077dSsubashab@codeaurora.org }
10905425077dSsubashab@codeaurora.org
udp_v6_early_demux(struct sk_buff * skb)109111052589SKuniyuki Iwashima void udp_v6_early_demux(struct sk_buff *skb)
10925425077dSsubashab@codeaurora.org {
10935425077dSsubashab@codeaurora.org struct net *net = dev_net(skb->dev);
10945425077dSsubashab@codeaurora.org const struct udphdr *uh;
10955425077dSsubashab@codeaurora.org struct sock *sk;
10965425077dSsubashab@codeaurora.org struct dst_entry *dst;
10975425077dSsubashab@codeaurora.org int dif = skb->dev->ifindex;
10984297a0efSDavid Ahern int sdif = inet6_sdif(skb);
10995425077dSsubashab@codeaurora.org
11005425077dSsubashab@codeaurora.org if (!pskb_may_pull(skb, skb_transport_offset(skb) +
11015425077dSsubashab@codeaurora.org sizeof(struct udphdr)))
11025425077dSsubashab@codeaurora.org return;
11035425077dSsubashab@codeaurora.org
11045425077dSsubashab@codeaurora.org uh = udp_hdr(skb);
11055425077dSsubashab@codeaurora.org
11065425077dSsubashab@codeaurora.org if (skb->pkt_type == PACKET_HOST)
11075425077dSsubashab@codeaurora.org sk = __udp6_lib_demux_lookup(net, uh->dest,
11085425077dSsubashab@codeaurora.org &ipv6_hdr(skb)->daddr,
11095425077dSsubashab@codeaurora.org uh->source, &ipv6_hdr(skb)->saddr,
11104297a0efSDavid Ahern dif, sdif);
11115425077dSsubashab@codeaurora.org else
11125425077dSsubashab@codeaurora.org return;
11135425077dSsubashab@codeaurora.org
111441c6d650SReshetova, Elena if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
11155425077dSsubashab@codeaurora.org return;
11165425077dSsubashab@codeaurora.org
11175425077dSsubashab@codeaurora.org skb->sk = sk;
11185425077dSsubashab@codeaurora.org skb->destructor = sock_efree;
11198f905c0eSEric Dumazet dst = rcu_dereference(sk->sk_rx_dst);
11205425077dSsubashab@codeaurora.org
11215425077dSsubashab@codeaurora.org if (dst)
1122ef57c161SEric Dumazet dst = dst_check(dst, sk->sk_rx_dst_cookie);
11235425077dSsubashab@codeaurora.org if (dst) {
1124d24406c8SWei Wang /* set noref for now.
1125d24406c8SWei Wang * any place which wants to hold dst has to call
1126d24406c8SWei Wang * dst_hold_safe()
1127d24406c8SWei Wang */
11285425077dSsubashab@codeaurora.org skb_dst_set_noref(skb, dst);
11295425077dSsubashab@codeaurora.org }
11305425077dSsubashab@codeaurora.org }
11315425077dSsubashab@codeaurora.org
udpv6_rcv(struct sk_buff * skb)11320e219ae4SPaolo Abeni INDIRECT_CALLABLE_SCOPE int udpv6_rcv(struct sk_buff *skb)
1133db8dac20SDavid S. Miller {
1134ba6aac15SKuniyuki Iwashima return __udp6_lib_rcv(skb, dev_net(skb->dev)->ipv4.udp_table, IPPROTO_UDP);
1135db8dac20SDavid S. Miller }
1136db8dac20SDavid S. Miller
1137db8dac20SDavid S. Miller /*
1138db8dac20SDavid S. Miller * Throw away all pending data and cancel the corking. Socket is locked.
1139db8dac20SDavid S. Miller */
udp_v6_flush_pending_frames(struct sock * sk)1140db8dac20SDavid S. Miller static void udp_v6_flush_pending_frames(struct sock *sk)
1141db8dac20SDavid S. Miller {
1142db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk);
1143db8dac20SDavid S. Miller
114436d926b9SDenis V. Lunev if (up->pending == AF_INET)
114536d926b9SDenis V. Lunev udp_flush_pending_frames(sk);
114636d926b9SDenis V. Lunev else if (up->pending) {
1147db8dac20SDavid S. Miller up->len = 0;
1148d2e919b6SEric Dumazet WRITE_ONCE(up->pending, 0);
1149db8dac20SDavid S. Miller ip6_flush_pending_frames(sk);
1150db8dac20SDavid S. Miller }
1151db8dac20SDavid S. Miller }
1152db8dac20SDavid S. Miller
udpv6_pre_connect(struct sock * sk,struct sockaddr * uaddr,int addr_len)1153d74bad4eSAndrey Ignatov static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1154d74bad4eSAndrey Ignatov int addr_len)
1155d74bad4eSAndrey Ignatov {
1156bddc028aSTetsuo Handa if (addr_len < offsetofend(struct sockaddr, sa_family))
1157bddc028aSTetsuo Handa return -EINVAL;
1158d74bad4eSAndrey Ignatov /* The following checks are replicated from __ip6_datagram_connect()
1159d74bad4eSAndrey Ignatov * and intended to prevent BPF program called below from accessing
1160d74bad4eSAndrey Ignatov * bytes that are out of the bound specified by user in addr_len.
1161d74bad4eSAndrey Ignatov */
1162d74bad4eSAndrey Ignatov if (uaddr->sa_family == AF_INET) {
116389e9c728SKuniyuki Iwashima if (ipv6_only_sock(sk))
1164d74bad4eSAndrey Ignatov return -EAFNOSUPPORT;
1165d74bad4eSAndrey Ignatov return udp_pre_connect(sk, uaddr, addr_len);
1166d74bad4eSAndrey Ignatov }
1167d74bad4eSAndrey Ignatov
1168d74bad4eSAndrey Ignatov if (addr_len < SIN6_LEN_RFC2133)
1169d74bad4eSAndrey Ignatov return -EINVAL;
1170d74bad4eSAndrey Ignatov
11716d71331eSDaan De Meyer return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr, &addr_len);
1172d74bad4eSAndrey Ignatov }
1173d74bad4eSAndrey Ignatov
1174493c6be3SSridhar Samudrala /**
1175493c6be3SSridhar Samudrala * udp6_hwcsum_outgoing - handle outgoing HW checksumming
1176493c6be3SSridhar Samudrala * @sk: socket we are sending on
1177493c6be3SSridhar Samudrala * @skb: sk_buff containing the filled-in UDP header
1178493c6be3SSridhar Samudrala * (checksum field must be zeroed out)
1179b51cd7c8SAndrew Lunn * @saddr: source address
1180b51cd7c8SAndrew Lunn * @daddr: destination address
1181b51cd7c8SAndrew Lunn * @len: length of packet
1182493c6be3SSridhar Samudrala */
udp6_hwcsum_outgoing(struct sock * sk,struct sk_buff * skb,const struct in6_addr * saddr,const struct in6_addr * daddr,int len)1183493c6be3SSridhar Samudrala static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1184493c6be3SSridhar Samudrala const struct in6_addr *saddr,
1185493c6be3SSridhar Samudrala const struct in6_addr *daddr, int len)
1186493c6be3SSridhar Samudrala {
1187493c6be3SSridhar Samudrala unsigned int offset;
1188493c6be3SSridhar Samudrala struct udphdr *uh = udp_hdr(skb);
1189d39d938cSVlad Yasevich struct sk_buff *frags = skb_shinfo(skb)->frag_list;
1190493c6be3SSridhar Samudrala __wsum csum = 0;
1191493c6be3SSridhar Samudrala
1192d39d938cSVlad Yasevich if (!frags) {
1193493c6be3SSridhar Samudrala /* Only one fragment on the socket. */
1194493c6be3SSridhar Samudrala skb->csum_start = skb_transport_header(skb) - skb->head;
1195493c6be3SSridhar Samudrala skb->csum_offset = offsetof(struct udphdr, check);
1196493c6be3SSridhar Samudrala uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1197493c6be3SSridhar Samudrala } else {
1198493c6be3SSridhar Samudrala /*
1199493c6be3SSridhar Samudrala * HW-checksum won't work as there are two or more
1200493c6be3SSridhar Samudrala * fragments on the socket so that all csums of sk_buffs
1201493c6be3SSridhar Samudrala * should be together
1202493c6be3SSridhar Samudrala */
1203493c6be3SSridhar Samudrala offset = skb_transport_offset(skb);
1204493c6be3SSridhar Samudrala skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
120563ecc3d9SSubash Abhinov Kasiviswanathan csum = skb->csum;
1206493c6be3SSridhar Samudrala
1207493c6be3SSridhar Samudrala skb->ip_summed = CHECKSUM_NONE;
1208493c6be3SSridhar Samudrala
1209d39d938cSVlad Yasevich do {
1210d39d938cSVlad Yasevich csum = csum_add(csum, frags->csum);
1211d39d938cSVlad Yasevich } while ((frags = frags->next));
1212493c6be3SSridhar Samudrala
1213493c6be3SSridhar Samudrala uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1214493c6be3SSridhar Samudrala csum);
1215493c6be3SSridhar Samudrala if (uh->check == 0)
1216493c6be3SSridhar Samudrala uh->check = CSUM_MANGLED_0;
1217493c6be3SSridhar Samudrala }
1218493c6be3SSridhar Samudrala }
1219493c6be3SSridhar Samudrala
1220db8dac20SDavid S. Miller /*
1221db8dac20SDavid S. Miller * Sending
1222db8dac20SDavid S. Miller */
1223db8dac20SDavid S. Miller
udp_v6_send_skb(struct sk_buff * skb,struct flowi6 * fl6,struct inet_cork * cork)1224bec1f6f6SWillem de Bruijn static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1225bec1f6f6SWillem de Bruijn struct inet_cork *cork)
1226db8dac20SDavid S. Miller {
1227d39d938cSVlad Yasevich struct sock *sk = skb->sk;
1228db8dac20SDavid S. Miller struct udphdr *uh;
1229db8dac20SDavid S. Miller int err = 0;
1230db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk);
1231db8dac20SDavid S. Miller __wsum csum = 0;
1232d39d938cSVlad Yasevich int offset = skb_transport_offset(skb);
1233d39d938cSVlad Yasevich int len = skb->len - offset;
12344094871dSJosh Hunt int datalen = len - sizeof(*uh);
1235db8dac20SDavid S. Miller
1236db8dac20SDavid S. Miller /*
1237db8dac20SDavid S. Miller * Create a UDP header
1238db8dac20SDavid S. Miller */
1239db8dac20SDavid S. Miller uh = udp_hdr(skb);
12401958b856SDavid S. Miller uh->source = fl6->fl6_sport;
12411958b856SDavid S. Miller uh->dest = fl6->fl6_dport;
1242d39d938cSVlad Yasevich uh->len = htons(len);
1243db8dac20SDavid S. Miller uh->check = 0;
1244db8dac20SDavid S. Miller
1245bec1f6f6SWillem de Bruijn if (cork->gso_size) {
1246bec1f6f6SWillem de Bruijn const int hlen = skb_network_header_len(skb) +
1247bec1f6f6SWillem de Bruijn sizeof(struct udphdr);
1248bec1f6f6SWillem de Bruijn
1249*eaf42688SYan Zhai if (hlen + min(datalen, cork->gso_size) > cork->fragsize) {
12500f149c9fSWillem de Bruijn kfree_skb(skb);
1251*eaf42688SYan Zhai return -EMSGSIZE;
12520f149c9fSWillem de Bruijn }
1253736ef37fSCoco Li if (datalen > cork->gso_size * UDP_MAX_SEGMENTS) {
12540f149c9fSWillem de Bruijn kfree_skb(skb);
1255bec1f6f6SWillem de Bruijn return -EINVAL;
12560f149c9fSWillem de Bruijn }
125739fc39a0SEric Dumazet if (udp_get_no_check6_tx(sk)) {
12580f149c9fSWillem de Bruijn kfree_skb(skb);
1259a8c744a8SWillem de Bruijn return -EINVAL;
12600f149c9fSWillem de Bruijn }
1261ff06342cSWillem de Bruijn if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
12620f149c9fSWillem de Bruijn dst_xfrm(skb_dst(skb))) {
12630f149c9fSWillem de Bruijn kfree_skb(skb);
1264bec1f6f6SWillem de Bruijn return -EIO;
12650f149c9fSWillem de Bruijn }
1266bec1f6f6SWillem de Bruijn
12674094871dSJosh Hunt if (datalen > cork->gso_size) {
1268bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_size = cork->gso_size;
1269bec1f6f6SWillem de Bruijn skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
12704094871dSJosh Hunt skb_shinfo(skb)->gso_segs = DIV_ROUND_UP(datalen,
127144b321e5SJosh Hunt cork->gso_size);
12724094871dSJosh Hunt }
1273a8c744a8SWillem de Bruijn goto csum_partial;
1274bec1f6f6SWillem de Bruijn }
1275bec1f6f6SWillem de Bruijn
1276db8dac20SDavid S. Miller if (is_udplite)
1277d39d938cSVlad Yasevich csum = udplite_csum(skb);
127839fc39a0SEric Dumazet else if (udp_get_no_check6_tx(sk)) { /* UDP csum disabled */
12794068579eSTom Herbert skb->ip_summed = CHECKSUM_NONE;
12804068579eSTom Herbert goto send;
12814068579eSTom Herbert } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
1282a8c744a8SWillem de Bruijn csum_partial:
1283d39d938cSVlad Yasevich udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
1284493c6be3SSridhar Samudrala goto send;
1285493c6be3SSridhar Samudrala } else
1286d39d938cSVlad Yasevich csum = udp_csum(skb);
1287db8dac20SDavid S. Miller
1288db8dac20SDavid S. Miller /* add protocol-dependent pseudo-header */
12894c9483b2SDavid S. Miller uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
1290d39d938cSVlad Yasevich len, fl6->flowi6_proto, csum);
1291db8dac20SDavid S. Miller if (uh->check == 0)
1292db8dac20SDavid S. Miller uh->check = CSUM_MANGLED_0;
1293db8dac20SDavid S. Miller
1294493c6be3SSridhar Samudrala send:
1295d39d938cSVlad Yasevich err = ip6_send_skb(skb);
12966ce9e7b5SEric Dumazet if (err) {
12976ce9e7b5SEric Dumazet if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
12986aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk),
12996ce9e7b5SEric Dumazet UDP_MIB_SNDBUFERRORS, is_udplite);
13006ce9e7b5SEric Dumazet err = 0;
13016ce9e7b5SEric Dumazet }
13026aef70a8SEric Dumazet } else {
13036aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk),
13046ce9e7b5SEric Dumazet UDP_MIB_OUTDATAGRAMS, is_udplite);
13056aef70a8SEric Dumazet }
1306d39d938cSVlad Yasevich return err;
1307d39d938cSVlad Yasevich }
1308d39d938cSVlad Yasevich
udp_v6_push_pending_frames(struct sock * sk)1309d39d938cSVlad Yasevich static int udp_v6_push_pending_frames(struct sock *sk)
1310d39d938cSVlad Yasevich {
1311d39d938cSVlad Yasevich struct sk_buff *skb;
1312d39d938cSVlad Yasevich struct udp_sock *up = udp_sk(sk);
1313d39d938cSVlad Yasevich int err = 0;
1314d39d938cSVlad Yasevich
1315d39d938cSVlad Yasevich if (up->pending == AF_INET)
1316d39d938cSVlad Yasevich return udp_push_pending_frames(sk);
1317d39d938cSVlad Yasevich
1318d39d938cSVlad Yasevich skb = ip6_finish_skb(sk);
1319d39d938cSVlad Yasevich if (!skb)
1320d39d938cSVlad Yasevich goto out;
1321d39d938cSVlad Yasevich
1322940ea00bSPavel Begunkov err = udp_v6_send_skb(skb, &inet_sk(sk)->cork.fl.u.ip6,
1323940ea00bSPavel Begunkov &inet_sk(sk)->cork.base);
1324db8dac20SDavid S. Miller out:
1325db8dac20SDavid S. Miller up->len = 0;
1326d2e919b6SEric Dumazet WRITE_ONCE(up->pending, 0);
1327db8dac20SDavid S. Miller return err;
1328db8dac20SDavid S. Miller }
1329db8dac20SDavid S. Miller
udpv6_sendmsg(struct sock * sk,struct msghdr * msg,size_t len)13301b784140SYing Xue int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1331db8dac20SDavid S. Miller {
1332db8dac20SDavid S. Miller struct ipv6_txoptions opt_space;
1333db8dac20SDavid S. Miller struct udp_sock *up = udp_sk(sk);
1334db8dac20SDavid S. Miller struct inet_sock *inet = inet_sk(sk);
1335db8dac20SDavid S. Miller struct ipv6_pinfo *np = inet6_sk(sk);
1336342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
133720c59de2SArnaud Ebalard struct in6_addr *daddr, *final_p, final;
1338db8dac20SDavid S. Miller struct ipv6_txoptions *opt = NULL;
133945f6fad8SEric Dumazet struct ipv6_txoptions *opt_to_free = NULL;
1340db8dac20SDavid S. Miller struct ip6_flowlabel *flowlabel = NULL;
13415298953eSPavel Begunkov struct inet_cork_full cork;
13425298953eSPavel Begunkov struct flowi6 *fl6 = &cork.fl.u.ip6;
1343db8dac20SDavid S. Miller struct dst_entry *dst;
134426879da5SWei Wang struct ipcm6_cookie ipc6;
1345db8dac20SDavid S. Miller int addr_len = msg->msg_namelen;
13469f542f61SAlexey Kodanev bool connected = false;
1347db8dac20SDavid S. Miller int ulen = len;
13486965d1d8SEric Dumazet int corkreq = udp_test_bit(CORK, sk) || msg->msg_flags & MSG_MORE;
1349db8dac20SDavid S. Miller int err;
1350db8dac20SDavid S. Miller int is_udplite = IS_UDPLITE(sk);
1351db8dac20SDavid S. Miller int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
1352db8dac20SDavid S. Miller
1353b515430aSWillem de Bruijn ipcm6_init(&ipc6);
135418a419baSEric Dumazet ipc6.gso_size = READ_ONCE(up->gso_size);
1355e3390b30SEric Dumazet ipc6.sockc.tsflags = READ_ONCE(sk->sk_tsflags);
13563c5b4d69SEric Dumazet ipc6.sockc.mark = READ_ONCE(sk->sk_mark);
135726879da5SWei Wang
1358db8dac20SDavid S. Miller /* destination address check */
1359db8dac20SDavid S. Miller if (sin6) {
1360db8dac20SDavid S. Miller if (addr_len < offsetof(struct sockaddr, sa_data))
1361db8dac20SDavid S. Miller return -EINVAL;
1362db8dac20SDavid S. Miller
1363db8dac20SDavid S. Miller switch (sin6->sin6_family) {
1364db8dac20SDavid S. Miller case AF_INET6:
1365db8dac20SDavid S. Miller if (addr_len < SIN6_LEN_RFC2133)
1366db8dac20SDavid S. Miller return -EINVAL;
1367db8dac20SDavid S. Miller daddr = &sin6->sin6_addr;
1368052d2369SJonathan T. Leighton if (ipv6_addr_any(daddr) &&
1369052d2369SJonathan T. Leighton ipv6_addr_v4mapped(&np->saddr))
1370052d2369SJonathan T. Leighton ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1371052d2369SJonathan T. Leighton daddr);
1372db8dac20SDavid S. Miller break;
1373db8dac20SDavid S. Miller case AF_INET:
1374db8dac20SDavid S. Miller goto do_udp_sendmsg;
1375db8dac20SDavid S. Miller case AF_UNSPEC:
1376db8dac20SDavid S. Miller msg->msg_name = sin6 = NULL;
1377db8dac20SDavid S. Miller msg->msg_namelen = addr_len = 0;
1378db8dac20SDavid S. Miller daddr = NULL;
1379db8dac20SDavid S. Miller break;
1380db8dac20SDavid S. Miller default:
1381db8dac20SDavid S. Miller return -EINVAL;
1382db8dac20SDavid S. Miller }
1383d2e919b6SEric Dumazet } else if (!READ_ONCE(up->pending)) {
1384db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED)
1385db8dac20SDavid S. Miller return -EDESTADDRREQ;
1386efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr;
1387db8dac20SDavid S. Miller } else
1388db8dac20SDavid S. Miller daddr = NULL;
1389db8dac20SDavid S. Miller
1390db8dac20SDavid S. Miller if (daddr) {
1391db8dac20SDavid S. Miller if (ipv6_addr_v4mapped(daddr)) {
1392db8dac20SDavid S. Miller struct sockaddr_in sin;
1393db8dac20SDavid S. Miller sin.sin_family = AF_INET;
1394c720c7e8SEric Dumazet sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1395db8dac20SDavid S. Miller sin.sin_addr.s_addr = daddr->s6_addr32[3];
1396db8dac20SDavid S. Miller msg->msg_name = &sin;
1397db8dac20SDavid S. Miller msg->msg_namelen = sizeof(sin);
1398db8dac20SDavid S. Miller do_udp_sendmsg:
13991c5950fcSEric Dumazet err = ipv6_only_sock(sk) ?
14001c5950fcSEric Dumazet -ENETUNREACH : udp_sendmsg(sk, msg, len);
14011c5950fcSEric Dumazet msg->msg_name = sin6;
14021c5950fcSEric Dumazet msg->msg_namelen = addr_len;
14031c5950fcSEric Dumazet return err;
1404db8dac20SDavid S. Miller }
1405db8dac20SDavid S. Miller }
1406db8dac20SDavid S. Miller
1407db8dac20SDavid S. Miller /* Rough check on arithmetic overflow,
1408db8dac20SDavid S. Miller better check is made in ip6_append_data().
1409db8dac20SDavid S. Miller */
1410db8dac20SDavid S. Miller if (len > INT_MAX - sizeof(struct udphdr))
1411db8dac20SDavid S. Miller return -EMSGSIZE;
1412db8dac20SDavid S. Miller
141303485f2aSVlad Yasevich getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1414d2e919b6SEric Dumazet if (READ_ONCE(up->pending)) {
1415d2e919b6SEric Dumazet if (READ_ONCE(up->pending) == AF_INET)
1416406c4a0aSPavel Begunkov return udp_sendmsg(sk, msg, len);
1417db8dac20SDavid S. Miller /*
1418db8dac20SDavid S. Miller * There are pending frames.
1419db8dac20SDavid S. Miller * The socket lock must be held while it's corked.
1420db8dac20SDavid S. Miller */
1421db8dac20SDavid S. Miller lock_sock(sk);
1422db8dac20SDavid S. Miller if (likely(up->pending)) {
1423db8dac20SDavid S. Miller if (unlikely(up->pending != AF_INET6)) {
1424db8dac20SDavid S. Miller release_sock(sk);
1425db8dac20SDavid S. Miller return -EAFNOSUPPORT;
1426db8dac20SDavid S. Miller }
1427db8dac20SDavid S. Miller dst = NULL;
1428db8dac20SDavid S. Miller goto do_append_data;
1429db8dac20SDavid S. Miller }
1430db8dac20SDavid S. Miller release_sock(sk);
1431db8dac20SDavid S. Miller }
1432db8dac20SDavid S. Miller ulen += sizeof(struct udphdr);
1433db8dac20SDavid S. Miller
14345298953eSPavel Begunkov memset(fl6, 0, sizeof(*fl6));
1435db8dac20SDavid S. Miller
1436db8dac20SDavid S. Miller if (sin6) {
1437db8dac20SDavid S. Miller if (sin6->sin6_port == 0)
1438db8dac20SDavid S. Miller return -EINVAL;
1439db8dac20SDavid S. Miller
14405298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port;
1441db8dac20SDavid S. Miller daddr = &sin6->sin6_addr;
1442db8dac20SDavid S. Miller
1443db8dac20SDavid S. Miller if (np->sndflow) {
14445298953eSPavel Begunkov fl6->flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
14455298953eSPavel Begunkov if (fl6->flowlabel & IPV6_FLOWLABEL_MASK) {
14465298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
144759c820b2SWillem de Bruijn if (IS_ERR(flowlabel))
1448db8dac20SDavid S. Miller return -EINVAL;
1449db8dac20SDavid S. Miller }
1450db8dac20SDavid S. Miller }
1451db8dac20SDavid S. Miller
1452db8dac20SDavid S. Miller /*
1453db8dac20SDavid S. Miller * Otherwise it will be difficult to maintain
1454db8dac20SDavid S. Miller * sk->sk_dst_cache.
1455db8dac20SDavid S. Miller */
1456db8dac20SDavid S. Miller if (sk->sk_state == TCP_ESTABLISHED &&
1457efe4208fSEric Dumazet ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1458efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr;
1459db8dac20SDavid S. Miller
1460db8dac20SDavid S. Miller if (addr_len >= sizeof(struct sockaddr_in6) &&
1461db8dac20SDavid S. Miller sin6->sin6_scope_id &&
1462842df073SHannes Frederic Sowa __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
14635298953eSPavel Begunkov fl6->flowi6_oif = sin6->sin6_scope_id;
1464db8dac20SDavid S. Miller } else {
1465db8dac20SDavid S. Miller if (sk->sk_state != TCP_ESTABLISHED)
1466db8dac20SDavid S. Miller return -EDESTADDRREQ;
1467db8dac20SDavid S. Miller
14685298953eSPavel Begunkov fl6->fl6_dport = inet->inet_dport;
1469efe4208fSEric Dumazet daddr = &sk->sk_v6_daddr;
14705298953eSPavel Begunkov fl6->flowlabel = np->flow_label;
14719f542f61SAlexey Kodanev connected = true;
1472db8dac20SDavid S. Miller }
1473db8dac20SDavid S. Miller
14745298953eSPavel Begunkov if (!fl6->flowi6_oif)
14754c971d2fSEric Dumazet fl6->flowi6_oif = READ_ONCE(sk->sk_bound_dev_if);
1476db8dac20SDavid S. Miller
14775298953eSPavel Begunkov if (!fl6->flowi6_oif)
14785298953eSPavel Begunkov fl6->flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
14799f690db7SYang Hongyang
14805298953eSPavel Begunkov fl6->flowi6_uid = sk->sk_uid;
148151953d5bSBrian Haley
1482db8dac20SDavid S. Miller if (msg->msg_controllen) {
1483db8dac20SDavid S. Miller opt = &opt_space;
1484db8dac20SDavid S. Miller memset(opt, 0, sizeof(struct ipv6_txoptions));
1485db8dac20SDavid S. Miller opt->tot_len = sizeof(*opt);
148626879da5SWei Wang ipc6.opt = opt;
1487db8dac20SDavid S. Miller
14882e8de857SWillem de Bruijn err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
14891d2809e5SYick Xie if (err > 0) {
14905298953eSPavel Begunkov err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, fl6,
14915fdaa88dSWillem de Bruijn &ipc6);
14921d2809e5SYick Xie connected = false;
14931d2809e5SYick Xie }
1494db8dac20SDavid S. Miller if (err < 0) {
1495db8dac20SDavid S. Miller fl6_sock_release(flowlabel);
1496db8dac20SDavid S. Miller return err;
1497db8dac20SDavid S. Miller }
14985298953eSPavel Begunkov if ((fl6->flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
14995298953eSPavel Begunkov flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
150059c820b2SWillem de Bruijn if (IS_ERR(flowlabel))
1501db8dac20SDavid S. Miller return -EINVAL;
1502db8dac20SDavid S. Miller }
1503db8dac20SDavid S. Miller if (!(opt->opt_nflen|opt->opt_flen))
1504db8dac20SDavid S. Miller opt = NULL;
1505db8dac20SDavid S. Miller }
150645f6fad8SEric Dumazet if (!opt) {
150745f6fad8SEric Dumazet opt = txopt_get(np);
150845f6fad8SEric Dumazet opt_to_free = opt;
150945f6fad8SEric Dumazet }
1510db8dac20SDavid S. Miller if (flowlabel)
1511db8dac20SDavid S. Miller opt = fl6_merge_options(&opt_space, flowlabel, opt);
1512db8dac20SDavid S. Miller opt = ipv6_fixup_options(&opt_space, opt);
151326879da5SWei Wang ipc6.opt = opt;
1514db8dac20SDavid S. Miller
15155298953eSPavel Begunkov fl6->flowi6_proto = sk->sk_protocol;
15165298953eSPavel Begunkov fl6->flowi6_mark = ipc6.sockc.mark;
15175298953eSPavel Begunkov fl6->daddr = *daddr;
15185298953eSPavel Begunkov if (ipv6_addr_any(&fl6->saddr) && !ipv6_addr_any(&np->saddr))
15195298953eSPavel Begunkov fl6->saddr = np->saddr;
15205298953eSPavel Begunkov fl6->fl6_sport = inet->inet_sport;
1521db8dac20SDavid S. Miller
15226fc88c35SDave Marchevsky if (cgroup_bpf_enabled(CGROUP_UDP6_SENDMSG) && !connected) {
15231cedee13SAndrey Ignatov err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
15245298953eSPavel Begunkov (struct sockaddr *)sin6,
15256d71331eSDaan De Meyer &addr_len,
15265298953eSPavel Begunkov &fl6->saddr);
15271cedee13SAndrey Ignatov if (err)
15281cedee13SAndrey Ignatov goto out_no_dst;
15291cedee13SAndrey Ignatov if (sin6) {
15301cedee13SAndrey Ignatov if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
15311cedee13SAndrey Ignatov /* BPF program rewrote IPv6-only by IPv4-mapped
15321cedee13SAndrey Ignatov * IPv6. It's currently unsupported.
15331cedee13SAndrey Ignatov */
15341cedee13SAndrey Ignatov err = -ENOTSUPP;
15351cedee13SAndrey Ignatov goto out_no_dst;
15361cedee13SAndrey Ignatov }
15371cedee13SAndrey Ignatov if (sin6->sin6_port == 0) {
15381cedee13SAndrey Ignatov /* BPF program set invalid port. Reject it. */
15391cedee13SAndrey Ignatov err = -EINVAL;
15401cedee13SAndrey Ignatov goto out_no_dst;
15411cedee13SAndrey Ignatov }
15425298953eSPavel Begunkov fl6->fl6_dport = sin6->sin6_port;
15435298953eSPavel Begunkov fl6->daddr = sin6->sin6_addr;
15441cedee13SAndrey Ignatov }
15451cedee13SAndrey Ignatov }
15461cedee13SAndrey Ignatov
15475298953eSPavel Begunkov if (ipv6_addr_any(&fl6->daddr))
15485298953eSPavel Begunkov fl6->daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1549e8e36984SAndrey Ignatov
15505298953eSPavel Begunkov final_p = fl6_update_dst(fl6, opt, &final);
155120c59de2SArnaud Ebalard if (final_p)
15529f542f61SAlexey Kodanev connected = false;
1553db8dac20SDavid S. Miller
15545298953eSPavel Begunkov if (!fl6->flowi6_oif && ipv6_addr_is_multicast(&fl6->daddr)) {
15555298953eSPavel Begunkov fl6->flowi6_oif = np->mcast_oif;
15569f542f61SAlexey Kodanev connected = false;
15575298953eSPavel Begunkov } else if (!fl6->flowi6_oif)
15585298953eSPavel Begunkov fl6->flowi6_oif = np->ucast_oif;
1559db8dac20SDavid S. Miller
15605298953eSPavel Begunkov security_sk_classify_flow(sk, flowi6_to_flowi_common(fl6));
1561db8dac20SDavid S. Miller
156238b7097bSHannes Frederic Sowa if (ipc6.tclass < 0)
156338b7097bSHannes Frederic Sowa ipc6.tclass = np->tclass;
156438b7097bSHannes Frederic Sowa
15655298953eSPavel Begunkov fl6->flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6->flowlabel);
156638b7097bSHannes Frederic Sowa
15675298953eSPavel Begunkov dst = ip6_sk_dst_lookup_flow(sk, fl6, final_p, connected);
156868d0c6d3SDavid S. Miller if (IS_ERR(dst)) {
156968d0c6d3SDavid S. Miller err = PTR_ERR(dst);
157068d0c6d3SDavid S. Miller dst = NULL;
1571db8dac20SDavid S. Miller goto out;
1572db8dac20SDavid S. Miller }
1573db8dac20SDavid S. Miller
157426879da5SWei Wang if (ipc6.hlimit < 0)
15755298953eSPavel Begunkov ipc6.hlimit = ip6_sk_dst_hoplimit(np, fl6, dst);
1576db8dac20SDavid S. Miller
1577db8dac20SDavid S. Miller if (msg->msg_flags&MSG_CONFIRM)
1578db8dac20SDavid S. Miller goto do_confirm;
1579db8dac20SDavid S. Miller back_from_confirm:
1580db8dac20SDavid S. Miller
158103485f2aSVlad Yasevich /* Lockless fast path for the non-corking case */
158203485f2aSVlad Yasevich if (!corkreq) {
158303485f2aSVlad Yasevich struct sk_buff *skb;
158403485f2aSVlad Yasevich
158503485f2aSVlad Yasevich skb = ip6_make_skb(sk, getfrag, msg, ulen,
158626879da5SWei Wang sizeof(struct udphdr), &ipc6,
1587797a4c1fSEric Dumazet dst_rt6_info(dst),
15885fdaa88dSWillem de Bruijn msg->msg_flags, &cork);
158903485f2aSVlad Yasevich err = PTR_ERR(skb);
159003485f2aSVlad Yasevich if (!IS_ERR_OR_NULL(skb))
15915298953eSPavel Begunkov err = udp_v6_send_skb(skb, fl6, &cork.base);
159240ac240cSPavel Begunkov /* ip6_make_skb steals dst reference */
159340ac240cSPavel Begunkov goto out_no_dst;
159403485f2aSVlad Yasevich }
159503485f2aSVlad Yasevich
1596db8dac20SDavid S. Miller lock_sock(sk);
1597db8dac20SDavid S. Miller if (unlikely(up->pending)) {
1598db8dac20SDavid S. Miller /* The socket is already corked while preparing it. */
1599db8dac20SDavid S. Miller /* ... which is an evident application bug. --ANK */
1600db8dac20SDavid S. Miller release_sock(sk);
1601db8dac20SDavid S. Miller
1602ba7a46f1SJoe Perches net_dbg_ratelimited("udp cork app bug 2\n");
1603db8dac20SDavid S. Miller err = -EINVAL;
1604db8dac20SDavid S. Miller goto out;
1605db8dac20SDavid S. Miller }
1606db8dac20SDavid S. Miller
1607d2e919b6SEric Dumazet WRITE_ONCE(up->pending, AF_INET6);
1608db8dac20SDavid S. Miller
1609db8dac20SDavid S. Miller do_append_data:
161026879da5SWei Wang if (ipc6.dontfrag < 0)
161126879da5SWei Wang ipc6.dontfrag = np->dontfrag;
1612db8dac20SDavid S. Miller up->len += ulen;
161326879da5SWei Wang err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1614797a4c1fSEric Dumazet &ipc6, fl6, dst_rt6_info(dst),
16155fdaa88dSWillem de Bruijn corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
1616db8dac20SDavid S. Miller if (err)
1617db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk);
1618db8dac20SDavid S. Miller else if (!corkreq)
1619db8dac20SDavid S. Miller err = udp_v6_push_pending_frames(sk);
1620db8dac20SDavid S. Miller else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1621d2e919b6SEric Dumazet WRITE_ONCE(up->pending, 0);
1622db8dac20SDavid S. Miller
162303485f2aSVlad Yasevich if (err > 0)
162403485f2aSVlad Yasevich err = np->recverr ? net_xmit_errno(err) : 0;
162503485f2aSVlad Yasevich release_sock(sk);
162603485f2aSVlad Yasevich
1627db8dac20SDavid S. Miller out:
1628a3c96089SYOSHIFUJI Hideaki dst_release(dst);
16291cedee13SAndrey Ignatov out_no_dst:
1630db8dac20SDavid S. Miller fl6_sock_release(flowlabel);
163145f6fad8SEric Dumazet txopt_put(opt_to_free);
1632db8dac20SDavid S. Miller if (!err)
1633db8dac20SDavid S. Miller return len;
1634db8dac20SDavid S. Miller /*
1635db8dac20SDavid S. Miller * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1636db8dac20SDavid S. Miller * ENOBUFS might not be good (it's not tunable per se), but otherwise
1637db8dac20SDavid S. Miller * we don't have a good statistic (IpOutDiscards but it can be too many
1638db8dac20SDavid S. Miller * things). We could add another new stat but at least for now that
1639db8dac20SDavid S. Miller * seems like overkill.
1640db8dac20SDavid S. Miller */
1641db8dac20SDavid S. Miller if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
16426aef70a8SEric Dumazet UDP6_INC_STATS(sock_net(sk),
1643235b9f7aSPavel Emelyanov UDP_MIB_SNDBUFERRORS, is_udplite);
1644db8dac20SDavid S. Miller }
1645db8dac20SDavid S. Miller return err;
1646db8dac20SDavid S. Miller
1647db8dac20SDavid S. Miller do_confirm:
16480dec879fSJulian Anastasov if (msg->msg_flags & MSG_PROBE)
16495298953eSPavel Begunkov dst_confirm_neigh(dst, &fl6->daddr);
1650db8dac20SDavid S. Miller if (!(msg->msg_flags&MSG_PROBE) || len)
1651db8dac20SDavid S. Miller goto back_from_confirm;
1652db8dac20SDavid S. Miller err = 0;
1653db8dac20SDavid S. Miller goto out;
1654db8dac20SDavid S. Miller }
1655ed472b0cSDavid Howells EXPORT_SYMBOL(udpv6_sendmsg);
1656db8dac20SDavid S. Miller
udpv6_splice_eof(struct socket * sock)16571d7e4538SDavid Howells static void udpv6_splice_eof(struct socket *sock)
16581d7e4538SDavid Howells {
16591d7e4538SDavid Howells struct sock *sk = sock->sk;
16601d7e4538SDavid Howells struct udp_sock *up = udp_sk(sk);
16611d7e4538SDavid Howells
1662d2e919b6SEric Dumazet if (!READ_ONCE(up->pending) || udp_test_bit(CORK, sk))
16631d7e4538SDavid Howells return;
16641d7e4538SDavid Howells
16651d7e4538SDavid Howells lock_sock(sk);
16666965d1d8SEric Dumazet if (up->pending && !udp_test_bit(CORK, sk))
16671d7e4538SDavid Howells udp_v6_push_pending_frames(sk);
16681d7e4538SDavid Howells release_sock(sk);
16691d7e4538SDavid Howells }
16701d7e4538SDavid Howells
udpv6_destroy_sock(struct sock * sk)16717d06b2e0SBrian Haley void udpv6_destroy_sock(struct sock *sk)
1672db8dac20SDavid S. Miller {
167344046a59STom Parkin struct udp_sock *up = udp_sk(sk);
1674db8dac20SDavid S. Miller lock_sock(sk);
1675a8b897c7SPaolo Abeni
1676a8b897c7SPaolo Abeni /* protects from races with udp_abort() */
1677a8b897c7SPaolo Abeni sock_set_flag(sk, SOCK_DEAD);
1678db8dac20SDavid S. Miller udp_v6_flush_pending_frames(sk);
1679db8dac20SDavid S. Miller release_sock(sk);
1680db8dac20SDavid S. Miller
168160fb9567SPaolo Abeni if (static_branch_unlikely(&udpv6_encap_needed_key)) {
168260fb9567SPaolo Abeni if (up->encap_type) {
168344046a59STom Parkin void (*encap_destroy)(struct sock *sk);
16846aa7de05SMark Rutland encap_destroy = READ_ONCE(up->encap_destroy);
168544046a59STom Parkin if (encap_destroy)
168644046a59STom Parkin encap_destroy(sk);
168744046a59STom Parkin }
1688f1f1f5ccSEric Dumazet if (udp_test_bit(ENCAP_ENABLED, sk)) {
16899c480601SPaolo Abeni static_branch_dec(&udpv6_encap_needed_key);
1690a4a600ddSXin Long udp_encap_disable();
1691a4a600ddSXin Long }
169260fb9567SPaolo Abeni }
1693db8dac20SDavid S. Miller }
1694db8dac20SDavid S. Miller
1695db8dac20SDavid S. Miller /*
1696db8dac20SDavid S. Miller * Socket option code for UDP
1697db8dac20SDavid S. Miller */
udpv6_setsockopt(struct sock * sk,int level,int optname,sockptr_t optval,unsigned int optlen)1698a7b75c5aSChristoph Hellwig int udpv6_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval,
1699a7b75c5aSChristoph Hellwig unsigned int optlen)
1700db8dac20SDavid S. Miller {
17018a3854c7SPaolo Abeni if (level == SOL_UDP || level == SOL_UDPLITE || level == SOL_SOCKET)
170291ac1ccaSChristoph Hellwig return udp_lib_setsockopt(sk, level, optname,
1703a7b75c5aSChristoph Hellwig optval, optlen,
1704db8dac20SDavid S. Miller udp_v6_push_pending_frames);
1705db8dac20SDavid S. Miller return ipv6_setsockopt(sk, level, optname, optval, optlen);
1706db8dac20SDavid S. Miller }
1707db8dac20SDavid S. Miller
udpv6_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1708db8dac20SDavid S. Miller int udpv6_getsockopt(struct sock *sk, int level, int optname,
1709db8dac20SDavid S. Miller char __user *optval, int __user *optlen)
1710db8dac20SDavid S. Miller {
1711db8dac20SDavid S. Miller if (level == SOL_UDP || level == SOL_UDPLITE)
1712db8dac20SDavid S. Miller return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1713db8dac20SDavid S. Miller return ipv6_getsockopt(sk, level, optname, optval, optlen);
1714db8dac20SDavid S. Miller }
1715db8dac20SDavid S. Miller
171611052589SKuniyuki Iwashima static const struct inet6_protocol udpv6_protocol = {
1717db8dac20SDavid S. Miller .handler = udpv6_rcv,
1718db8dac20SDavid S. Miller .err_handler = udpv6_err,
1719db8dac20SDavid S. Miller .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1720db8dac20SDavid S. Miller };
1721db8dac20SDavid S. Miller
1722db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1723db8dac20SDavid S. Miller #ifdef CONFIG_PROC_FS
udp6_seq_show(struct seq_file * seq,void * v)1724db8dac20SDavid S. Miller int udp6_seq_show(struct seq_file *seq, void *v)
1725db8dac20SDavid S. Miller {
172617ef66afSLorenzo Colitti if (v == SEQ_START_TOKEN) {
172717ef66afSLorenzo Colitti seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
172817ef66afSLorenzo Colitti } else {
172917ef66afSLorenzo Colitti int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1730abc17a11SEric Dumazet const struct inet_sock *inet = inet_sk((const struct sock *)v);
173117ef66afSLorenzo Colitti __u16 srcp = ntohs(inet->inet_sport);
173217ef66afSLorenzo Colitti __u16 destp = ntohs(inet->inet_dport);
17336c206b20SPaolo Abeni __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
17346c206b20SPaolo Abeni udp_rqueue_get(v), bucket);
173517ef66afSLorenzo Colitti }
1736db8dac20SDavid S. Miller return 0;
1737db8dac20SDavid S. Miller }
1738db8dac20SDavid S. Miller
1739c3506372SChristoph Hellwig const struct seq_operations udp6_seq_ops = {
1740a3d2599bSChristoph Hellwig .start = udp_seq_start,
1741a3d2599bSChristoph Hellwig .next = udp_seq_next,
1742a3d2599bSChristoph Hellwig .stop = udp_seq_stop,
1743a3d2599bSChristoph Hellwig .show = udp6_seq_show,
1744a3d2599bSChristoph Hellwig };
1745c3506372SChristoph Hellwig EXPORT_SYMBOL(udp6_seq_ops);
174673cb88ecSArjan van de Ven
1747db8dac20SDavid S. Miller static struct udp_seq_afinfo udp6_seq_afinfo = {
1748db8dac20SDavid S. Miller .family = AF_INET6,
1749478aee5dSKuniyuki Iwashima .udp_table = NULL,
1750db8dac20SDavid S. Miller };
1751db8dac20SDavid S. Miller
udp6_proc_init(struct net * net)17522c8c1e72SAlexey Dobriyan int __net_init udp6_proc_init(struct net *net)
1753db8dac20SDavid S. Miller {
1754c3506372SChristoph Hellwig if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1755c3506372SChristoph Hellwig sizeof(struct udp_iter_state), &udp6_seq_afinfo))
1756a3d2599bSChristoph Hellwig return -ENOMEM;
1757a3d2599bSChristoph Hellwig return 0;
1758db8dac20SDavid S. Miller }
1759db8dac20SDavid S. Miller
udp6_proc_exit(struct net * net)1760ec120da6SIan Morris void udp6_proc_exit(struct net *net)
1761ec120da6SIan Morris {
1762a3d2599bSChristoph Hellwig remove_proc_entry("udp6", net->proc_net);
1763db8dac20SDavid S. Miller }
1764db8dac20SDavid S. Miller #endif /* CONFIG_PROC_FS */
1765db8dac20SDavid S. Miller
1766db8dac20SDavid S. Miller /* ------------------------------------------------------------------------ */
1767db8dac20SDavid S. Miller
1768db8dac20SDavid S. Miller struct proto udpv6_prot = {
1769db8dac20SDavid S. Miller .name = "UDPv6",
1770db8dac20SDavid S. Miller .owner = THIS_MODULE,
1771db8dac20SDavid S. Miller .close = udp_lib_close,
1772d74bad4eSAndrey Ignatov .pre_connect = udpv6_pre_connect,
1773db8dac20SDavid S. Miller .connect = ip6_datagram_connect,
1774db8dac20SDavid S. Miller .disconnect = udp_disconnect,
1775db8dac20SDavid S. Miller .ioctl = udp_ioctl,
1776d38afeecSKuniyuki Iwashima .init = udpv6_init_sock,
1777db8dac20SDavid S. Miller .destroy = udpv6_destroy_sock,
1778db8dac20SDavid S. Miller .setsockopt = udpv6_setsockopt,
1779db8dac20SDavid S. Miller .getsockopt = udpv6_getsockopt,
1780db8dac20SDavid S. Miller .sendmsg = udpv6_sendmsg,
1781db8dac20SDavid S. Miller .recvmsg = udpv6_recvmsg,
17821d7e4538SDavid Howells .splice_eof = udpv6_splice_eof,
1783e646b657SMartin KaFai Lau .release_cb = ip6_datagram_release_cb,
1784db8dac20SDavid S. Miller .hash = udp_lib_hash,
1785db8dac20SDavid S. Miller .unhash = udp_lib_unhash,
1786719f8358SEric Dumazet .rehash = udp_v6_rehash,
1787db8dac20SDavid S. Miller .get_port = udp_v6_get_port,
178891a760b2SMenglong Dong .put_port = udp_lib_unhash,
17898a59f9d1SCong Wang #ifdef CONFIG_BPF_SYSCALL
17908a59f9d1SCong Wang .psock_update_sk_prot = udp_bpf_update_proto,
17918a59f9d1SCong Wang #endif
17920defbb0aSEric Dumazet
1793db8dac20SDavid S. Miller .memory_allocated = &udp_memory_allocated,
17940defbb0aSEric Dumazet .per_cpu_fw_alloc = &udp_memory_per_cpu_fw_alloc,
17950defbb0aSEric Dumazet
1796db8dac20SDavid S. Miller .sysctl_mem = sysctl_udp_mem,
17971e802951STonghao Zhang .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
17981e802951STonghao Zhang .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1799db8dac20SDavid S. Miller .obj_size = sizeof(struct udp6_sock),
1800f5f80e32SEric Dumazet .ipv6_pinfo_offset = offsetof(struct udp6_sock, inet6),
180167fb4330SKuniyuki Iwashima .h.udp_table = NULL,
18025d77dca8SDavid Ahern .diag_destroy = udp_abort,
1803db8dac20SDavid S. Miller };
1804db8dac20SDavid S. Miller
1805db8dac20SDavid S. Miller static struct inet_protosw udpv6_protosw = {
1806db8dac20SDavid S. Miller .type = SOCK_DGRAM,
1807db8dac20SDavid S. Miller .protocol = IPPROTO_UDP,
1808db8dac20SDavid S. Miller .prot = &udpv6_prot,
1809db8dac20SDavid S. Miller .ops = &inet6_dgram_ops,
1810db8dac20SDavid S. Miller .flags = INET_PROTOSW_PERMANENT,
1811db8dac20SDavid S. Miller };
1812db8dac20SDavid S. Miller
udpv6_init(void)1813db8dac20SDavid S. Miller int __init udpv6_init(void)
1814db8dac20SDavid S. Miller {
1815db8dac20SDavid S. Miller int ret;
1816db8dac20SDavid S. Miller
18173336288aSVlad Yasevich ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
18183336288aSVlad Yasevich if (ret)
1819c6b641a4SVlad Yasevich goto out;
18203336288aSVlad Yasevich
1821db8dac20SDavid S. Miller ret = inet6_register_protosw(&udpv6_protosw);
1822db8dac20SDavid S. Miller if (ret)
1823db8dac20SDavid S. Miller goto out_udpv6_protocol;
1824db8dac20SDavid S. Miller out:
1825db8dac20SDavid S. Miller return ret;
1826db8dac20SDavid S. Miller
1827db8dac20SDavid S. Miller out_udpv6_protocol:
1828db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1829db8dac20SDavid S. Miller goto out;
1830db8dac20SDavid S. Miller }
1831db8dac20SDavid S. Miller
udpv6_exit(void)1832db8dac20SDavid S. Miller void udpv6_exit(void)
1833db8dac20SDavid S. Miller {
1834db8dac20SDavid S. Miller inet6_unregister_protosw(&udpv6_protosw);
1835db8dac20SDavid S. Miller inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1836db8dac20SDavid S. Miller }
1837