blob: 09cba4cfe31ff5ab571eb2ec7fe9a6c9c793b111 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * UDP over IPv6
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Based on linux/ipv4/udp.c
9 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
Herbert Xu1781f7f2007-12-11 11:30:32 -080035#include <linux/module.h>
Herbert Xu3305b802005-12-13 23:16:37 -080036#include <linux/skbuff.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090037#include <linux/slab.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080038#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Craig Gallek496611d2016-02-10 11:50:36 -050040#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <net/ndisc.h>
42#include <net/protocol.h>
43#include <net/transp_v6.h>
44#include <net/ip6_route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <net/raw.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070046#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <net/ip6_checksum.h>
Stefano Brivioe7cc0822018-11-08 12:19:22 +010048#include <net/ip6_tunnel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <net/xfrm.h>
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -060050#include <net/inet_hashtables.h>
Tom Herbert72289b92013-01-22 09:50:44 +000051#include <net/inet6_hashtables.h>
Eliezer Tamir076bb0c2013-07-10 17:13:17 +030052#include <net/busy_poll.h>
Craig Galleke32ea7e2016-01-04 17:41:46 -050053#include <net/sock_reuseport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55#include <linux/proc_fs.h>
56#include <linux/seq_file.h>
Eric Dumazet22911fc2012-06-27 00:23:44 +000057#include <trace/events/skb.h>
Gerrit Renkerba4e58e2006-11-27 11:10:57 -080058#include "udp_impl.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Robert Shearman63a6fff2017-01-26 18:02:24 +000060static bool udp6_lib_exact_dif_match(struct net *net, struct sk_buff *skb)
61{
62#if defined(CONFIG_NET_L3_MASTER_DEV)
63 if (!net->ipv4.sysctl_udp_l3mdev_accept &&
64 skb && ipv6_l3mdev_skb(IP6CB(skb)->flags))
65 return true;
66#endif
67 return false;
68}
69
Eric Dumazet6eada012015-03-18 14:05:33 -070070static u32 udp6_ehashfn(const struct net *net,
71 const struct in6_addr *laddr,
72 const u16 lport,
73 const struct in6_addr *faddr,
74 const __be16 fport)
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020075{
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020076 static u32 udp6_ehash_secret __read_mostly;
77 static u32 udp_ipv6_hash_secret __read_mostly;
78
79 u32 lhash, fhash;
80
81 net_get_random_once(&udp6_ehash_secret,
82 sizeof(udp6_ehash_secret));
83 net_get_random_once(&udp_ipv6_hash_secret,
84 sizeof(udp_ipv6_hash_secret));
85
86 lhash = (__force u32)laddr->s6_addr32[3];
87 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
88
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020089 return __inet6_ehashfn(lhash, lport, fhash, fport,
Hannes Frederic Sowa1bbdcee2013-10-19 21:48:57 +020090 udp_ipv6_hash_secret + net_hash_mix(net));
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +020091}
92
Pavel Emelyanov6ba5a3c2008-03-22 16:51:21 -070093int udp_v6_get_port(struct sock *sk, unsigned short snum)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094{
Eric Dumazet30fff922009-11-09 05:26:33 +000095 unsigned int hash2_nulladdr =
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -080096 ipv6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
Jeffrin Jose9a52e972012-05-19 01:45:21 +000097 unsigned int hash2_partial =
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -080098 ipv6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
Eric Dumazet30fff922009-11-09 05:26:33 +000099
Eric Dumazetd4cada42009-11-08 10:17:30 +0000100 /* precompute partial secondary hash */
Eric Dumazet30fff922009-11-09 05:26:33 +0000101 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
Josef Bacikfe38d2a2017-01-17 07:51:01 -0800102 return udp_lib_get_port(sk, snum, hash2_nulladdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Eric Dumazet719f8352010-09-08 05:08:44 +0000105static void udp_v6_rehash(struct sock *sk)
106{
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800107 u16 new_hash = ipv6_portaddr_hash(sock_net(sk),
Eric Dumazetefe42082013-10-03 15:42:29 -0700108 &sk->sk_v6_rcv_saddr,
Eric Dumazet719f8352010-09-08 05:08:44 +0000109 inet_sk(sk)->inet_num);
110
111 udp_lib_rehash(sk, new_hash);
112}
113
Su, Xuemind1e37282016-06-13 11:02:50 +0800114static int compute_score(struct sock *sk, struct net *net,
115 const struct in6_addr *saddr, __be16 sport,
116 const struct in6_addr *daddr, unsigned short hnum,
David Ahern1801b572017-08-07 08:44:20 -0700117 int dif, int sdif, bool exact_dif)
Eric Dumazet645ca702008-10-29 01:41:45 -0700118{
Joe Perches60c04ae2014-12-01 20:29:06 -0800119 int score;
120 struct inet_sock *inet;
Mike Manning6da5b0f2018-11-07 15:36:04 +0000121 bool dev_match;
Eric Dumazet645ca702008-10-29 01:41:45 -0700122
Joe Perches60c04ae2014-12-01 20:29:06 -0800123 if (!net_eq(sock_net(sk), net) ||
124 udp_sk(sk)->udp_port_hash != hnum ||
125 sk->sk_family != PF_INET6)
126 return -1;
Eric Dumazet645ca702008-10-29 01:41:45 -0700127
Joe Perches60c04ae2014-12-01 20:29:06 -0800128 score = 0;
129 inet = inet_sk(sk);
130
131 if (inet->inet_dport) {
132 if (inet->inet_dport != sport)
133 return -1;
134 score++;
Eric Dumazet645ca702008-10-29 01:41:45 -0700135 }
Joe Perches60c04ae2014-12-01 20:29:06 -0800136
137 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
138 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
139 return -1;
140 score++;
141 }
142
143 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
144 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
145 return -1;
146 score++;
147 }
148
Mike Manning6da5b0f2018-11-07 15:36:04 +0000149 dev_match = udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif);
150 if (!dev_match)
151 return -1;
152 score++;
Joe Perches60c04ae2014-12-01 20:29:06 -0800153
Eric Dumazet70da2682015-10-08 19:33:21 -0700154 if (sk->sk_incoming_cpu == raw_smp_processor_id())
155 score++;
156
Eric Dumazet645ca702008-10-29 01:41:45 -0700157 return score;
158}
159
Su, Xuemind1e37282016-06-13 11:02:50 +0800160/* called with rcu_read_lock() */
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000161static struct sock *udp6_lib_lookup2(struct net *net,
162 const struct in6_addr *saddr, __be16 sport,
David Ahern1801b572017-08-07 08:44:20 -0700163 const struct in6_addr *daddr, unsigned int hnum,
164 int dif, int sdif, bool exact_dif,
165 struct udp_hslot *hslot2, struct sk_buff *skb)
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000166{
167 struct sock *sk, *result;
Paolo Abenie94a62f2017-11-30 15:39:34 +0100168 int score, badness;
Tom Herbert72289b92013-01-22 09:50:44 +0000169 u32 hash = 0;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000170
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000171 result = NULL;
172 badness = -1;
Eric Dumazetca065d02016-04-01 08:52:13 -0700173 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
Su, Xuemind1e37282016-06-13 11:02:50 +0800174 score = compute_score(sk, net, saddr, sport,
David Ahern1801b572017-08-07 08:44:20 -0700175 daddr, hnum, dif, sdif, exact_dif);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000176 if (score > badness) {
Paolo Abenie94a62f2017-11-30 15:39:34 +0100177 if (sk->sk_reuseport) {
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +0200178 hash = udp6_ehashfn(net, daddr, hnum,
179 saddr, sport);
Eric Dumazeted0dfff2016-01-19 08:36:43 -0800180
Eric Dumazetca065d02016-04-01 08:52:13 -0700181 result = reuseport_select_sock(sk, hash, skb,
Eric Dumazeted0dfff2016-01-19 08:36:43 -0800182 sizeof(struct udphdr));
Eric Dumazetca065d02016-04-01 08:52:13 -0700183 if (result)
184 return result;
Eric Dumazet70da2682015-10-08 19:33:21 -0700185 }
Eric Dumazetca065d02016-04-01 08:52:13 -0700186 result = sk;
187 badness = score;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000188 }
189 }
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000190 return result;
191}
192
Eric Dumazetca065d02016-04-01 08:52:13 -0700193/* rcu_read_lock() must be held */
Pavel Emelyanovfce82332011-12-09 06:23:34 +0000194struct sock *__udp6_lib_lookup(struct net *net,
David Ahern1801b572017-08-07 08:44:20 -0700195 const struct in6_addr *saddr, __be16 sport,
196 const struct in6_addr *daddr, __be16 dport,
197 int dif, int sdif, struct udp_table *udptable,
198 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Eric Dumazet271b72c2008-10-29 02:11:14 -0700200 struct sock *sk, *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned short hnum = ntohs(dport);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000202 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
203 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
Robert Shearman63a6fff2017-01-26 18:02:24 +0000204 bool exact_dif = udp6_lib_exact_dif_match(net, skb);
Paolo Abenie94a62f2017-11-30 15:39:34 +0100205 int score, badness;
Tom Herbert72289b92013-01-22 09:50:44 +0000206 u32 hash = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000208 if (hslot->count > 10) {
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800209 hash2 = ipv6_portaddr_hash(net, daddr, hnum);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000210 slot2 = hash2 & udptable->mask;
211 hslot2 = &udptable->hash2[slot2];
212 if (hslot->count < hslot2->count)
213 goto begin;
214
215 result = udp6_lib_lookup2(net, saddr, sport,
David Ahern1801b572017-08-07 08:44:20 -0700216 daddr, hnum, dif, sdif, exact_dif,
Su, Xuemind1e37282016-06-13 11:02:50 +0800217 hslot2, skb);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000218 if (!result) {
Su, Xuemind1e37282016-06-13 11:02:50 +0800219 unsigned int old_slot2 = slot2;
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800220 hash2 = ipv6_portaddr_hash(net, &in6addr_any, hnum);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000221 slot2 = hash2 & udptable->mask;
Su, Xuemind1e37282016-06-13 11:02:50 +0800222 /* avoid searching the same slot again. */
223 if (unlikely(slot2 == old_slot2))
224 return result;
225
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000226 hslot2 = &udptable->hash2[slot2];
227 if (hslot->count < hslot2->count)
228 goto begin;
229
Jorge Boncompte [DTI2]1223c672010-04-08 04:56:48 +0000230 result = udp6_lib_lookup2(net, saddr, sport,
David Ahern1801b572017-08-07 08:44:20 -0700231 daddr, hnum, dif, sdif,
Robert Shearman63a6fff2017-01-26 18:02:24 +0000232 exact_dif, hslot2,
233 skb);
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000234 }
Martin KaFai Lau8217ca62018-08-08 01:01:26 -0700235 if (unlikely(IS_ERR(result)))
236 return NULL;
Eric Dumazetfddc17d2009-11-08 10:18:30 +0000237 return result;
238 }
Eric Dumazet271b72c2008-10-29 02:11:14 -0700239begin:
240 result = NULL;
241 badness = -1;
Eric Dumazetca065d02016-04-01 08:52:13 -0700242 sk_for_each_rcu(sk, &hslot->head) {
Robert Shearman63a6fff2017-01-26 18:02:24 +0000243 score = compute_score(sk, net, saddr, sport, daddr, hnum, dif,
David Ahern1801b572017-08-07 08:44:20 -0700244 sdif, exact_dif);
Eric Dumazet645ca702008-10-29 01:41:45 -0700245 if (score > badness) {
Paolo Abenie94a62f2017-11-30 15:39:34 +0100246 if (sk->sk_reuseport) {
Hannes Frederic Sowab50026b2013-10-19 21:48:52 +0200247 hash = udp6_ehashfn(net, daddr, hnum,
248 saddr, sport);
Eric Dumazetca065d02016-04-01 08:52:13 -0700249 result = reuseport_select_sock(sk, hash, skb,
Craig Gallek538950a2016-01-04 17:41:47 -0500250 sizeof(struct udphdr));
Martin KaFai Lau8217ca62018-08-08 01:01:26 -0700251 if (unlikely(IS_ERR(result)))
252 return NULL;
Eric Dumazetca065d02016-04-01 08:52:13 -0700253 if (result)
254 return result;
Tom Herbert72289b92013-01-22 09:50:44 +0000255 }
Eric Dumazetca065d02016-04-01 08:52:13 -0700256 result = sk;
257 badness = score;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 return result;
261}
Pavel Emelyanovfce82332011-12-09 06:23:34 +0000262EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700264static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
265 __be16 sport, __be16 dport,
Eric Dumazet645ca702008-10-29 01:41:45 -0700266 struct udp_table *udptable)
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700267{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000268 const struct ipv6hdr *iph = ipv6_hdr(skb);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700269
Alexander Duycked7cbbc2016-05-12 16:23:44 -0700270 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
Eric Dumazetadf30902009-06-02 05:19:30 +0000271 &iph->daddr, dport, inet6_iif(skb),
David Ahern1801b572017-08-07 08:44:20 -0700272 inet6_sdif(skb), udptable, skb);
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700273}
274
Tom Herbert63058302016-04-05 08:22:50 -0700275struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
276 __be16 sport, __be16 dport)
277{
278 const struct ipv6hdr *iph = ipv6_hdr(skb);
Tom Herbert63058302016-04-05 08:22:50 -0700279
Alexander Duycked7cbbc2016-05-12 16:23:44 -0700280 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
Tom Herbert63058302016-04-05 08:22:50 -0700281 &iph->daddr, dport, inet6_iif(skb),
David Ahern1801b572017-08-07 08:44:20 -0700282 inet6_sdif(skb), &udp_table, skb);
Tom Herbert63058302016-04-05 08:22:50 -0700283}
284EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
285
Eric Dumazetca065d02016-04-01 08:52:13 -0700286/* Must be called under rcu_read_lock().
287 * Does increment socket refcount.
288 */
Arnd Bergmann6e860002018-06-05 13:40:34 +0200289#if IS_ENABLED(CONFIG_NF_TPROXY_IPV6) || IS_ENABLED(CONFIG_NF_SOCKET_IPV6)
Balazs Scheidleraa976fc2010-10-21 16:05:41 +0200290struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
291 const struct in6_addr *daddr, __be16 dport, int dif)
292{
Eric Dumazetca065d02016-04-01 08:52:13 -0700293 struct sock *sk;
294
295 sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
David Ahern1801b572017-08-07 08:44:20 -0700296 dif, 0, &udp_table, NULL);
Reshetova, Elena41c6d652017-06-30 13:08:01 +0300297 if (sk && !refcount_inc_not_zero(&sk->sk_refcnt))
Eric Dumazetca065d02016-04-01 08:52:13 -0700298 sk = NULL;
299 return sk;
Balazs Scheidleraa976fc2010-10-21 16:05:41 +0200300}
301EXPORT_SYMBOL_GPL(udp6_lib_lookup);
Eric Dumazetca065d02016-04-01 08:52:13 -0700302#endif
Balazs Scheidleraa976fc2010-10-21 16:05:41 +0200303
Paolo Abenicb891fa2017-07-31 16:52:36 +0200304/* do not use the scratch area len for jumbogram: their length execeeds the
305 * scratch area space; note that the IP6CB flags is still in the first
306 * cacheline, so checking for jumbograms is cheap
307 */
308static int udp6_skb_len(struct sk_buff *skb)
309{
310 return unlikely(inet6_is_jumbogram(skb)) ? skb->len : udp_skb_len(skb);
311}
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313/*
Ian Morris67ba4152014-08-24 21:53:10 +0100314 * This should be easy, if there is something there we
315 * return it, otherwise we block.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317
Ying Xue1b784142015-03-02 15:37:48 +0800318int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int noblock, int flags, int *addr_len)
320{
321 struct ipv6_pinfo *np = inet6_sk(sk);
322 struct inet_sock *inet = inet_sk(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900323 struct sk_buff *skb;
David S. Miller59c2cda2011-12-01 14:12:55 -0500324 unsigned int ulen, copied;
samanthakumar627d2d62016-04-05 12:41:16 -0400325 int peeked, peeking, off;
Herbert Xu759e5d02007-03-25 20:10:56 -0700326 int err;
327 int is_udplite = IS_UDPLITE(sk);
Eric Dumazet197c9492015-12-30 08:51:12 -0500328 bool checksum_valid = false;
Paolo Abeni029a3742018-11-09 15:52:45 +0100329 struct udp_mib *mib;
Wei Yongjunf26ba172008-11-02 16:11:01 +0000330 int is_udp4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (flags & MSG_ERRQUEUE)
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100333 return ipv6_recv_error(sk, msg, len, addr_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Brian Haley4b340ae2010-04-23 11:26:09 +0000335 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
Hannes Frederic Sowa85fbaa72013-11-23 00:46:12 +0100336 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
Brian Haley4b340ae2010-04-23 11:26:09 +0000337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338try_again:
Matthew Dawsona0917e02017-08-18 15:04:54 -0400339 peeking = flags & MSG_PEEK;
340 off = sk_peek_offset(sk, flags);
Paolo Abeni7c13f972016-11-04 11:28:59 +0100341 skb = __skb_recv_udp(sk, flags, noblock, &peeked, &off, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 if (!skb)
samanthakumar627d2d62016-04-05 12:41:16 -0400343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Paolo Abenicb891fa2017-07-31 16:52:36 +0200345 ulen = udp6_skb_len(skb);
David S. Miller59c2cda2011-12-01 14:12:55 -0500346 copied = len;
samanthakumar627d2d62016-04-05 12:41:16 -0400347 if (copied > ulen - off)
348 copied = ulen - off;
David S. Miller59c2cda2011-12-01 14:12:55 -0500349 else if (copied < ulen)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900350 msg->msg_flags |= MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Wei Yongjunf26ba172008-11-02 16:11:01 +0000352 is_udp4 = (skb->protocol == htons(ETH_P_IP));
Paolo Abeni029a3742018-11-09 15:52:45 +0100353 mib = __UDPX_MIB(sk, is_udp4);
Wei Yongjunf26ba172008-11-02 16:11:01 +0000354
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800355 /*
Herbert Xu759e5d02007-03-25 20:10:56 -0700356 * If checksum is needed at all, try to do it while copying the
357 * data. If the data is truncated, or if we only want a partial
358 * coverage checksum (UDP-Lite), do it before the copy.
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800359 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800360
Eric Dumazetd21dbdf2016-11-18 17:18:03 -0800361 if (copied < ulen || peeking ||
362 (is_udplite && UDP_SKB_CB(skb)->partial_cov)) {
Paolo Abeni67a51782017-06-26 19:01:51 +0200363 checksum_valid = udp_skb_csum_unnecessary(skb) ||
364 !__udp_lib_checksum_complete(skb);
Eric Dumazet197c9492015-12-30 08:51:12 -0500365 if (!checksum_valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 goto csum_copy_err;
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800367 }
368
Paolo Abeni67a51782017-06-26 19:01:51 +0200369 if (checksum_valid || udp_skb_csum_unnecessary(skb)) {
370 if (udp_skb_is_linear(skb))
371 err = copy_linear_skb(skb, copied, off, &msg->msg_iter);
372 else
373 err = skb_copy_datagram_msg(skb, off, msg, copied);
374 } else {
samanthakumar627d2d62016-04-05 12:41:16 -0400375 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (err == -EINVAL)
377 goto csum_copy_err;
378 }
Eric Dumazet22911fc2012-06-27 00:23:44 +0000379 if (unlikely(err)) {
Eric Dumazet979402b2012-09-05 23:34:44 +0000380 if (!peeked) {
381 atomic_inc(&sk->sk_drops);
Paolo Abeni029a3742018-11-09 15:52:45 +0100382 SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
Eric Dumazet979402b2012-09-05 23:34:44 +0000383 }
Paolo Abeni850cbad2016-10-21 13:55:47 +0200384 kfree_skb(skb);
samanthakumar627d2d62016-04-05 12:41:16 -0400385 return err;
Eric Dumazet22911fc2012-06-27 00:23:44 +0000386 }
Paolo Abeni029a3742018-11-09 15:52:45 +0100387 if (!peeked)
388 SNMP_INC_STATS(mib, UDP_MIB_INDATAGRAMS);
Wang Chencb759942007-12-03 22:33:28 +1100389
Neil Horman3b885782009-10-12 13:26:31 -0700390 sock_recv_ts_and_drops(msg, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 /* Copy the address. */
393 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +0100394 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 sin6->sin6_family = AF_INET6;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300396 sin6->sin6_port = udp_hdr(skb)->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 sin6->sin6_flowinfo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000399 if (is_udp4) {
Brian Haleyb301e822009-10-07 13:58:25 -0700400 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
401 &sin6->sin6_addr);
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000402 sin6->sin6_scope_id = 0;
403 } else {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000404 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
Hannes Frederic Sowa842df072013-03-08 02:07:19 +0000405 sin6->sin6_scope_id =
406 ipv6_iface_scope_id(&sin6->sin6_addr,
Duan Jiong43304872014-08-01 09:52:58 +0800407 inet6_iif(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
Hannes Frederic Sowabceaa902013-11-18 04:20:45 +0100409 *addr_len = sizeof(*sin6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100411
Paolo Abenibcd16652018-11-07 12:38:30 +0100412 if (udp_sk(sk)->gro_enabled)
413 udp_cmsg_recv(msg, sk, skb);
414
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100415 if (np->rxopt.all)
416 ip6_datagram_recv_common_ctl(sk, msg, skb);
417
Wei Yongjunf26ba172008-11-02 16:11:01 +0000418 if (is_udp4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (inet->cmsg_flags)
Paolo Abeniad959032016-11-04 11:28:58 +0100420 ip_cmsg_recv_offset(msg, sk, skb,
Eric Dumazet10df8e62016-10-23 18:03:06 -0700421 sizeof(struct udphdr), off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 } else {
423 if (np->rxopt.all)
Hannes Frederic Sowa4b261c752014-01-20 03:43:08 +0100424 ip6_datagram_recv_specific_ctl(sk, msg, skb);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
David S. Miller59c2cda2011-12-01 14:12:55 -0500427 err = copied;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (flags & MSG_TRUNC)
Herbert Xu759e5d02007-03-25 20:10:56 -0700429 err = ulen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Paolo Abeni850cbad2016-10-21 13:55:47 +0200431 skb_consume_udp(sk, skb, peeking ? -err : err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return err;
433
434csum_copy_err:
Paolo Abeni2276f582017-05-16 11:20:14 +0200435 if (!__sk_queue_drop_skb(sk, &udp_sk(sk)->reader_queue, skb, flags,
436 udp_skb_destructor)) {
Paolo Abeni029a3742018-11-09 15:52:45 +0100437 SNMP_INC_STATS(mib, UDP_MIB_CSUMERRORS);
438 SNMP_INC_STATS(mib, UDP_MIB_INERRORS);
Wei Yongjun0856f932008-11-02 16:14:27 +0000439 }
Paolo Abeni850cbad2016-10-21 13:55:47 +0200440 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric Dumazetbeb39db2015-05-30 09:16:53 -0700442 /* starting over for a new packet, but check if we need to yield */
443 cond_resched();
Xufeng Zhang9cfaa8d2011-06-21 10:43:40 +0000444 msg->msg_flags &= ~MSG_TRUNC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 goto try_again;
446}
447
Stefano Brivioa36e1852018-11-08 12:19:14 +0100448DEFINE_STATIC_KEY_FALSE(udpv6_encap_needed_key);
449void udpv6_encap_enable(void)
450{
Paolo Abeni9c480602018-11-15 02:34:50 +0100451 static_branch_inc(&udpv6_encap_needed_key);
Stefano Brivioa36e1852018-11-08 12:19:14 +0100452}
453EXPORT_SYMBOL(udpv6_encap_enable);
454
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100455/* Handler for tunnels with arbitrary destination ports: no socket lookup, go
456 * through error handlers in encapsulations looking for a match.
457 */
458static int __udp6_lib_err_encap_no_sk(struct sk_buff *skb,
459 struct inet6_skb_parm *opt,
460 u8 type, u8 code, int offset, u32 info)
461{
462 int i;
463
464 for (i = 0; i < MAX_IPTUN_ENCAP_OPS; i++) {
465 int (*handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
466 u8 type, u8 code, int offset, u32 info);
467
468 if (!ip6tun_encaps[i])
469 continue;
470 handler = rcu_dereference(ip6tun_encaps[i]->err_handler);
471 if (handler && !handler(skb, opt, type, code, offset, info))
472 return 0;
473 }
474
475 return -ENOENT;
476}
477
Stefano Brivioa36e1852018-11-08 12:19:14 +0100478/* Try to match ICMP errors to UDP tunnels by looking up a socket without
479 * reversing source and destination port: this will match tunnels that force the
480 * same destination port on both endpoints (e.g. VXLAN, GENEVE). Note that
481 * lwtunnels might actually break this assumption by being configured with
482 * different destination ports on endpoints, in this case we won't be able to
483 * trace ICMP messages back to them.
484 *
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100485 * If this doesn't match any socket, probe tunnels with arbitrary destination
486 * ports (e.g. FoU, GUE): there, the receiving socket is useless, as the port
487 * we've sent packets to won't necessarily match the local destination port.
488 *
Stefano Brivioa36e1852018-11-08 12:19:14 +0100489 * Then ask the tunnel implementation to match the error against a valid
490 * association.
491 *
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100492 * Return an error if we can't find a match, the socket if we need further
493 * processing, zero otherwise.
Stefano Brivioa36e1852018-11-08 12:19:14 +0100494 */
495static struct sock *__udp6_lib_err_encap(struct net *net,
496 const struct ipv6hdr *hdr, int offset,
497 struct udphdr *uh,
498 struct udp_table *udptable,
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100499 struct sk_buff *skb,
500 struct inet6_skb_parm *opt,
501 u8 type, u8 code, __be32 info)
Stefano Brivioa36e1852018-11-08 12:19:14 +0100502{
Stefano Brivioa36e1852018-11-08 12:19:14 +0100503 int network_offset, transport_offset;
Stefano Brivioa36e1852018-11-08 12:19:14 +0100504 struct sock *sk;
505
Stefano Brivioa36e1852018-11-08 12:19:14 +0100506 network_offset = skb_network_offset(skb);
507 transport_offset = skb_transport_offset(skb);
508
509 /* Network header needs to point to the outer IPv6 header inside ICMP */
510 skb_reset_network_header(skb);
511
512 /* Transport header needs to point to the UDP header */
513 skb_set_transport_header(skb, offset);
514
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100515 sk = __udp6_lib_lookup(net, &hdr->daddr, uh->source,
516 &hdr->saddr, uh->dest,
517 inet6_iif(skb), 0, udptable, skb);
518 if (sk) {
519 int (*lookup)(struct sock *sk, struct sk_buff *skb);
520 struct udp_sock *up = udp_sk(sk);
521
522 lookup = READ_ONCE(up->encap_err_lookup);
523 if (!lookup || lookup(sk, skb))
524 sk = NULL;
525 }
526
527 if (!sk) {
528 sk = ERR_PTR(__udp6_lib_err_encap_no_sk(skb, opt, type, code,
529 offset, info));
530 }
Stefano Brivioa36e1852018-11-08 12:19:14 +0100531
532 skb_set_transport_header(skb, transport_offset);
533 skb_set_network_header(skb, network_offset);
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100534
Stefano Brivioa36e1852018-11-08 12:19:14 +0100535 return sk;
536}
537
Stefano Brivio32bbd872018-11-08 12:19:21 +0100538int __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
539 u8 type, u8 code, int offset, __be32 info,
540 struct udp_table *udptable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 struct ipv6_pinfo *np;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000543 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
544 const struct in6_addr *saddr = &hdr->saddr;
545 const struct in6_addr *daddr = &hdr->daddr;
Ian Morris67ba4152014-08-24 21:53:10 +0100546 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
Stefano Brivioa36e1852018-11-08 12:19:14 +0100547 bool tunnel = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 struct sock *sk;
Wei Wange0d8c1b2016-02-17 13:58:22 -0800549 int harderr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 int err;
Duan Jiong7304fe42014-07-31 17:54:32 +0800551 struct net *net = dev_net(skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Craig Galleke32ea7e2016-01-04 17:41:46 -0500553 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
Mike Manningf64bf6b82018-10-26 12:24:35 +0100554 inet6_iif(skb), inet6_sdif(skb), udptable, skb);
Ian Morris63159f22015-03-29 14:00:04 +0100555 if (!sk) {
Stefano Brivioa36e1852018-11-08 12:19:14 +0100556 /* No socket for error: try tunnels before discarding */
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100557 sk = ERR_PTR(-ENOENT);
Stefano Brivioa36e1852018-11-08 12:19:14 +0100558 if (static_branch_unlikely(&udpv6_encap_needed_key)) {
559 sk = __udp6_lib_err_encap(net, hdr, offset, uh,
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100560 udptable, skb,
561 opt, type, code, info);
562 if (!sk)
563 return 0;
Stefano Brivioa36e1852018-11-08 12:19:14 +0100564 }
565
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100566 if (IS_ERR(sk)) {
Stefano Brivioa36e1852018-11-08 12:19:14 +0100567 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
568 ICMP6_MIB_INERRORS);
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100569 return PTR_ERR(sk);
Stefano Brivioa36e1852018-11-08 12:19:14 +0100570 }
Stefano Brivioe7cc0822018-11-08 12:19:22 +0100571
Stefano Brivioa36e1852018-11-08 12:19:14 +0100572 tunnel = true;
Duan Jiong7304fe42014-07-31 17:54:32 +0800573 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Wei Wange0d8c1b2016-02-17 13:58:22 -0800575 harderr = icmpv6_err_convert(type, code, &err);
576 np = inet6_sk(sk);
577
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100578 if (type == ICMPV6_PKT_TOOBIG) {
579 if (!ip6_sk_accept_pmtu(sk))
580 goto out;
David S. Miller81aded22012-06-15 14:54:11 -0700581 ip6_sk_update_pmtu(skb, sk, info);
Wei Wange0d8c1b2016-02-17 13:58:22 -0800582 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
583 harderr = 1;
Hannes Frederic Sowa93b36cf2013-12-15 03:41:14 +0100584 }
Duan Jiong1a462d12013-09-20 18:20:28 +0800585 if (type == NDISC_REDIRECT) {
Stefano Brivioa36e1852018-11-08 12:19:14 +0100586 if (tunnel) {
587 ip6_redirect(skb, sock_net(sk), inet6_iif(skb),
588 sk->sk_mark, sk->sk_uid);
589 } else {
590 ip6_sk_redirect(skb, sk);
591 }
Duan Jiong1a462d12013-09-20 18:20:28 +0800592 goto out;
593 }
David S. Miller81aded22012-06-15 14:54:11 -0700594
Stefano Brivioa36e1852018-11-08 12:19:14 +0100595 /* Tunnels don't have an application socket: don't pass errors back */
596 if (tunnel)
597 goto out;
598
Wei Wange0d8c1b2016-02-17 13:58:22 -0800599 if (!np->recverr) {
600 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
601 goto out;
602 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
Wei Wange0d8c1b2016-02-17 13:58:22 -0800604 }
Eric Dumazetb1faf562010-05-31 23:44:05 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 sk->sk_err = err;
607 sk->sk_error_report(sk);
608out:
Stefano Brivio32bbd872018-11-08 12:19:21 +0100609 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
611
Paolo Abenia3f96c42017-05-17 14:52:16 +0200612static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000613{
614 int rc;
615
Eric Dumazetefe42082013-10-03 15:42:29 -0700616 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000617 sock_rps_save_rxhash(sk, skb);
Shawn Bohrer005ec972013-10-07 11:01:38 -0500618 sk_mark_napi_id(sk, skb);
Eric Dumazet2c8c56e2014-11-11 05:54:28 -0800619 sk_incoming_cpu_update(sk);
Eric Dumazete68b6e52016-11-16 09:10:42 -0800620 } else {
621 sk_mark_napi_id_once(sk, skb);
Shawn Bohrer005ec972013-10-07 11:01:38 -0500622 }
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000623
Paolo Abeni850cbad2016-10-21 13:55:47 +0200624 rc = __udp_enqueue_schedule_skb(sk, skb);
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000625 if (rc < 0) {
626 int is_udplite = IS_UDPLITE(sk);
627
628 /* Note that an ENOMEM error is charged twice */
629 if (rc == -ENOMEM)
Eric Dumazete61da9e2016-04-29 14:16:50 -0700630 UDP6_INC_STATS(sock_net(sk),
Eric Dumazet02c22342016-04-27 16:44:30 -0700631 UDP_MIB_RCVBUFERRORS, is_udplite);
Eric Dumazete61da9e2016-04-29 14:16:50 -0700632 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000633 kfree_skb(skb);
634 return -1;
635 }
Paolo Abeni850cbad2016-10-21 13:55:47 +0200636
Benjamin LaHaisef7ad74f2012-04-27 08:23:21 +0000637 return 0;
638}
639
Stefano Brivio32bbd872018-11-08 12:19:21 +0100640static __inline__ int udpv6_err(struct sk_buff *skb,
641 struct inet6_skb_parm *opt, u8 type,
642 u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Stefano Brivio32bbd872018-11-08 12:19:21 +0100644 return __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800645}
646
Paolo Abenicf329aa2018-11-07 12:38:33 +0100647static int udpv6_queue_rcv_one_skb(struct sock *sk, struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800648{
649 struct udp_sock *up = udp_sk(sk);
Wang Chenb2bf1e22007-12-03 22:34:16 +1100650 int is_udplite = IS_UDPLITE(sk);
David S. Millera18135e2006-08-15 00:00:09 -0700651
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800652 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
653 goto drop;
654
Davidlohr Bueso88ab3102018-05-08 09:07:03 -0700655 if (static_branch_unlikely(&udpv6_encap_needed_key) && up->encap_type) {
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000656 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
657
658 /*
659 * This is an encapsulation socket so pass the skb to
660 * the socket's udp_encap_rcv() hook. Otherwise, just
661 * fall through and pass this up the UDP socket.
662 * up->encap_rcv() returns the following value:
663 * =0 if skb was successfully passed to the encap
664 * handler or was discarded by it.
665 * >0 if skb should be passed on to UDP.
666 * <0 if skb should be resubmitted as proto -N
667 */
668
669 /* if we're overly short, let UDP handle it */
Mark Rutland6aa7de02017-10-23 14:07:29 -0700670 encap_rcv = READ_ONCE(up->encap_rcv);
Hannes Frederic Sowae5aed002016-05-19 15:58:33 +0200671 if (encap_rcv) {
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000672 int ret;
673
Tom Herbert0a809662014-05-07 16:52:39 -0700674 /* Verify checksum before giving to encap */
675 if (udp_lib_checksum_complete(skb))
676 goto csum_error;
677
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000678 ret = encap_rcv(sk, skb);
679 if (ret <= 0) {
Eric Dumazet02c22342016-04-27 16:44:30 -0700680 __UDP_INC_STATS(sock_net(sk),
681 UDP_MIB_INDATAGRAMS,
682 is_udplite);
Benjamin LaHaised7f3f622012-04-27 08:24:08 +0000683 return -ret;
684 }
685 }
686
687 /* FALLTHROUGH -- it's a UDP Packet */
688 }
689
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800690 /*
691 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
692 */
Wang Chenb2bf1e22007-12-03 22:34:16 +1100693 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800694
695 if (up->pcrlen == 0) { /* full coverage was set */
Joe Perchesba7a46f2014-11-11 10:59:17 -0800696 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
697 UDP_SKB_CB(skb)->cscov, skb->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800698 goto drop;
699 }
700 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
Joe Perchesba7a46f2014-11-11 10:59:17 -0800701 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
702 UDP_SKB_CB(skb)->cscov, up->pcrlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800703 goto drop;
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
706
Paolo Abeni4b943fa2017-06-22 15:01:22 +0200707 prefetch(&sk->sk_rmem_alloc);
Eric Dumazetce25d662016-06-02 14:52:43 -0700708 if (rcu_access_pointer(sk->sk_filter) &&
709 udp_lib_checksum_complete(skb))
710 goto csum_error;
711
Daniel Borkmannba66bbe2016-07-25 18:06:12 +0200712 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
Michal Kubečeka6127692016-07-08 17:52:33 +0200713 goto drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
samanthakumare6afc8a2016-04-05 12:41:15 -0400715 udp_csum_pull_header(skb);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000716
Eric Dumazetd826eb12011-11-09 07:24:35 +0000717 skb_dst_drop(skb);
Wang Chencb759942007-12-03 22:33:28 +1100718
Paolo Abeni850cbad2016-10-21 13:55:47 +0200719 return __udpv6_queue_rcv_skb(sk, skb);
James M Leddy3e215c82014-06-25 17:38:13 -0400720
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000721csum_error:
Eric Dumazet02c22342016-04-27 16:44:30 -0700722 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800723drop:
Eric Dumazet02c22342016-04-27 16:44:30 -0700724 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000725 atomic_inc(&sk->sk_drops);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800726 kfree_skb(skb);
727 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Paolo Abenicf329aa2018-11-07 12:38:33 +0100730static int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
731{
732 struct sk_buff *next, *segs;
733 int ret;
734
735 if (likely(!udp_unexpected_gso(sk, skb)))
736 return udpv6_queue_rcv_one_skb(sk, skb);
737
738 __skb_push(skb, -skb_mac_offset(skb));
739 segs = udp_rcv_segment(sk, skb, false);
740 for (skb = segs; skb; skb = next) {
741 next = skb->next;
742 __skb_pull(skb, skb_transport_offset(skb));
743
744 ret = udpv6_queue_rcv_one_skb(sk, skb);
745 if (ret > 0)
746 ip6_protocol_deliver_rcu(dev_net(skb->dev), skb, ret,
747 true);
748 }
749 return 0;
750}
751
David Held5cf3d462014-07-15 23:28:31 -0400752static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
753 __be16 loc_port, const struct in6_addr *loc_addr,
754 __be16 rmt_port, const struct in6_addr *rmt_addr,
Dewi Morgan7bd2db42018-11-07 15:36:10 +0000755 int dif, int sdif, unsigned short hnum)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756{
David Held5cf3d462014-07-15 23:28:31 -0400757 struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
David Held5cf3d462014-07-15 23:28:31 -0400759 if (!net_eq(sock_net(sk), net))
760 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
David Held5cf3d462014-07-15 23:28:31 -0400762 if (udp_sk(sk)->udp_port_hash != hnum ||
763 sk->sk_family != PF_INET6 ||
764 (inet->inet_dport && inet->inet_dport != rmt_port) ||
765 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
766 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
Dewi Morgan7bd2db42018-11-07 15:36:10 +0000767 !udp_sk_bound_dev_eq(net, sk->sk_bound_dev_if, dif, sdif) ||
Henning Rogge33b4b012015-05-18 21:08:49 +0200768 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
769 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
David Held5cf3d462014-07-15 23:28:31 -0400770 return false;
771 if (!inet6_mc_check(sk, loc_addr, rmt_addr))
772 return false;
773 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774}
775
Tom Herbert40685792014-05-02 16:29:58 -0700776static void udp6_csum_zero_error(struct sk_buff *skb)
777{
778 /* RFC 2460 section 8.1 says that we SHOULD log
779 * this error. Well, it is reasonable.
780 */
Joe Perchesba7a46f2014-11-11 10:59:17 -0800781 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
782 &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
783 &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
Tom Herbert40685792014-05-02 16:29:58 -0700784}
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786/*
787 * Note: called only from the BH handler context,
788 * so we don't need to lock the hashes.
789 */
Pavel Emelyanove3163492008-06-16 17:12:11 -0700790static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000791 const struct in6_addr *saddr, const struct in6_addr *daddr,
Rick Jones36cbb242014-11-06 10:37:54 -0800792 struct udp_table *udptable, int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Eric Dumazetca065d02016-04-01 08:52:13 -0700794 struct sock *sk, *first = NULL;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300795 const struct udphdr *uh = udp_hdr(skb);
David Held5cf3d462014-07-15 23:28:31 -0400796 unsigned short hnum = ntohs(uh->dest);
797 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
Eric Dumazetca065d02016-04-01 08:52:13 -0700798 unsigned int offset = offsetof(typeof(*sk), sk_node);
David Held2dc41cf2014-07-15 23:28:32 -0400799 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
Eric Dumazetca065d02016-04-01 08:52:13 -0700800 int dif = inet6_iif(skb);
Dewi Morgan7bd2db42018-11-07 15:36:10 +0000801 int sdif = inet6_sdif(skb);
Eric Dumazetca065d02016-04-01 08:52:13 -0700802 struct hlist_node *node;
803 struct sk_buff *nskb;
David Held2dc41cf2014-07-15 23:28:32 -0400804
805 if (use_hash2) {
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800806 hash2_any = ipv6_portaddr_hash(net, &in6addr_any, hnum) &
Pablo Neira73e2d5e2016-11-14 23:40:30 +0100807 udptable->mask;
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -0800808 hash2 = ipv6_portaddr_hash(net, daddr, hnum) & udptable->mask;
David Held2dc41cf2014-07-15 23:28:32 -0400809start_lookup:
Pablo Neira73e2d5e2016-11-14 23:40:30 +0100810 hslot = &udptable->hash2[hash2];
David Held2dc41cf2014-07-15 23:28:32 -0400811 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Eric Dumazetca065d02016-04-01 08:52:13 -0700814 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
815 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
Dewi Morgan7bd2db42018-11-07 15:36:10 +0000816 uh->source, saddr, dif, sdif,
817 hnum))
Eric Dumazetca065d02016-04-01 08:52:13 -0700818 continue;
819 /* If zero checksum and no_check is not on for
820 * the socket then skip it.
821 */
822 if (!uh->check && !udp_sk(sk)->no_check6_rx)
823 continue;
824 if (!first) {
825 first = sk;
826 continue;
Hideo Aoki95766ff2007-12-31 00:29:24 -0800827 }
Eric Dumazetca065d02016-04-01 08:52:13 -0700828 nskb = skb_clone(skb, GFP_ATOMIC);
829 if (unlikely(!nskb)) {
830 atomic_inc(&sk->sk_drops);
Eric Dumazet02c22342016-04-27 16:44:30 -0700831 __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
832 IS_UDPLITE(sk));
833 __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
834 IS_UDPLITE(sk));
Eric Dumazetca065d02016-04-01 08:52:13 -0700835 continue;
836 }
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000837
Eric Dumazetca065d02016-04-01 08:52:13 -0700838 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
839 consume_skb(nskb);
840 }
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000841
David Held2dc41cf2014-07-15 23:28:32 -0400842 /* Also lookup *:port if we are using hash2 and haven't done so yet. */
843 if (use_hash2 && hash2 != hash2_any) {
844 hash2 = hash2_any;
845 goto start_lookup;
846 }
847
Eric Dumazetca065d02016-04-01 08:52:13 -0700848 if (first) {
849 if (udpv6_queue_rcv_skb(first, skb) > 0)
850 consume_skb(skb);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000851 } else {
Eric Dumazetca065d02016-04-01 08:52:13 -0700852 kfree_skb(skb);
Eric Dumazet02c22342016-04-27 16:44:30 -0700853 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
854 proto == IPPROTO_UDPLITE);
Eric Dumazeta1ab77f2009-11-08 10:18:52 +0000855 }
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800856 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Paolo Abeni64f0f5d2017-08-25 14:31:01 +0200859static void udp6_sk_rx_dst_set(struct sock *sk, struct dst_entry *dst)
860{
861 if (udp_sk_rx_dst_set(sk, dst)) {
862 const struct rt6_info *rt = (const struct rt6_info *)dst;
863
864 inet6_sk(sk)->rx_dst_cookie = rt6_get_cookie(rt);
865 }
866}
867
Paolo Abenieb63f292018-09-13 16:27:21 +0200868/* wrapper for udp_queue_rcv_skb tacking care of csum conversion and
869 * return code conversion for ip layer consumption
870 */
871static int udp6_unicast_rcv_skb(struct sock *sk, struct sk_buff *skb,
872 struct udphdr *uh)
873{
874 int ret;
875
876 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
877 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
878 ip6_compute_pseudo);
879
880 ret = udpv6_queue_rcv_skb(sk, skb);
881
Paolo Abeni84dad552018-10-17 11:44:04 +0200882 /* a return value > 0 means to resubmit the input */
Paolo Abenieb63f292018-09-13 16:27:21 +0200883 if (ret > 0)
Paolo Abeni84dad552018-10-17 11:44:04 +0200884 return ret;
Paolo Abenieb63f292018-09-13 16:27:21 +0200885 return 0;
886}
887
Eric Dumazet645ca702008-10-29 01:41:45 -0700888int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
Herbert Xu759e5d02007-03-25 20:10:56 -0700889 int proto)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000891 const struct in6_addr *saddr, *daddr;
Eric Dumazetca065d02016-04-01 08:52:13 -0700892 struct net *net = dev_net(skb->dev);
893 struct udphdr *uh;
894 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 u32 ulen = 0;
896
897 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
Bjørn Morkd6bc0142010-05-06 03:44:35 +0000898 goto discard;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700900 saddr = &ipv6_hdr(skb)->saddr;
901 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300902 uh = udp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 ulen = ntohs(uh->len);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800905 if (ulen > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 goto short_packet;
907
Herbert Xu759e5d02007-03-25 20:10:56 -0700908 if (proto == IPPROTO_UDP) {
909 /* UDP validates ulen. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800911 /* Check for jumbo payload */
912 if (ulen == 0)
913 ulen = skb->len;
914
915 if (ulen < sizeof(*uh))
916 goto short_packet;
917
918 if (ulen < skb->len) {
919 if (pskb_trim_rcsum(skb, ulen))
920 goto short_packet;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700921 saddr = &ipv6_hdr(skb)->saddr;
922 daddr = &ipv6_hdr(skb)->daddr;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -0300923 uh = udp_hdr(skb);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926
Herbert Xu759e5d02007-03-25 20:10:56 -0700927 if (udp6_csum_init(skb, uh, proto))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000928 goto csum_error;
Herbert Xu759e5d02007-03-25 20:10:56 -0700929
Paolo Abenic9f2c1a2017-07-27 14:45:09 +0200930 /* Check if the socket is already available, e.g. due to early demux */
931 sk = skb_steal_sock(skb);
932 if (sk) {
933 struct dst_entry *dst = skb_dst(skb);
934 int ret;
935
936 if (unlikely(sk->sk_rx_dst != dst))
Paolo Abeni64f0f5d2017-08-25 14:31:01 +0200937 udp6_sk_rx_dst_set(sk, dst);
Paolo Abenic9f2c1a2017-07-27 14:45:09 +0200938
Paolo Abenieb63f292018-09-13 16:27:21 +0200939 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
940 sock_put(sk);
941 goto report_csum_error;
942 }
Paolo Abenic9f2c1a2017-07-27 14:45:09 +0200943
Paolo Abenieb63f292018-09-13 16:27:21 +0200944 ret = udp6_unicast_rcv_skb(sk, skb, uh);
945 sock_put(sk);
946 return ret;
Paolo Abenic9f2c1a2017-07-27 14:45:09 +0200947 }
948
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900949 /*
950 * Multicast receive code
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800952 if (ipv6_addr_is_multicast(daddr))
Pavel Emelyanove3163492008-06-16 17:12:11 -0700953 return __udp6_lib_mcast_deliver(net, skb,
Rick Jones36cbb242014-11-06 10:37:54 -0800954 saddr, daddr, udptable, proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
956 /* Unicast */
KOVACS Krisztian607c4aa2008-10-07 12:38:32 -0700957 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
Ian Morris53b24b82015-03-29 14:00:05 +0100958 if (sk) {
Paolo Abenieb63f292018-09-13 16:27:21 +0200959 if (!uh->check && !udp_sk(sk)->no_check6_rx)
960 goto report_csum_error;
961 return udp6_unicast_rcv_skb(sk, skb, uh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900963
Paolo Abenieb63f292018-09-13 16:27:21 +0200964 if (!uh->check)
965 goto report_csum_error;
Tom Herbert40685792014-05-02 16:29:58 -0700966
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000967 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
968 goto discard;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900969
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000970 if (udp_lib_checksum_complete(skb))
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000971 goto csum_error;
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000972
Eric Dumazet02c22342016-04-27 16:44:30 -0700973 __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
Benjamin LaHaisecb80ef42012-04-27 08:23:59 +0000974 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
975
976 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800977 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900979short_packet:
Joe Perchesba7a46f2014-11-11 10:59:17 -0800980 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
981 proto == IPPROTO_UDPLITE ? "-Lite" : "",
982 saddr, ntohs(uh->source),
983 ulen, skb->len,
984 daddr, ntohs(uh->dest));
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000985 goto discard;
Paolo Abenieb63f292018-09-13 16:27:21 +0200986
987report_csum_error:
988 udp6_csum_zero_error(skb);
Eric Dumazet6a5dc9e2013-04-29 08:39:56 +0000989csum_error:
Eric Dumazet02c22342016-04-27 16:44:30 -0700990 __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991discard:
Eric Dumazet02c22342016-04-27 16:44:30 -0700992 __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 kfree_skb(skb);
Stephen Hemmingeradd459a2007-03-08 20:42:35 -0800994 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
Gerrit Renkerba4e58e2006-11-27 11:10:57 -0800996
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -0600997
subashab@codeaurora.org54250772017-03-08 16:36:49 -0700998static struct sock *__udp6_lib_demux_lookup(struct net *net,
999 __be16 loc_port, const struct in6_addr *loc_addr,
1000 __be16 rmt_port, const struct in6_addr *rmt_addr,
David Ahern4297a0e2017-08-07 08:44:21 -07001001 int dif, int sdif)
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001002{
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -06001003 unsigned short hnum = ntohs(loc_port);
Martin KaFai Lauf0b1e642017-12-01 12:52:30 -08001004 unsigned int hash2 = ipv6_portaddr_hash(net, loc_addr, hnum);
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -06001005 unsigned int slot2 = hash2 & udp_table.mask;
1006 struct udp_hslot *hslot2 = &udp_table.hash2[slot2];
1007 const __portpair ports = INET_COMBINED_PORTS(rmt_port, hnum);
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001008 struct sock *sk;
1009
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -06001010 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
Wei Wang85cb73f2017-06-23 15:25:37 -07001011 if (sk->sk_state == TCP_ESTABLISHED &&
David Ahern4297a0e2017-08-07 08:44:21 -07001012 INET6_MATCH(sk, net, rmt_addr, loc_addr, ports, dif, sdif))
subashab@codeaurora.org0bd84062017-04-18 11:39:41 -06001013 return sk;
1014 /* Only check first socket in chain */
1015 break;
1016 }
1017 return NULL;
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001018}
1019
1020static void udp_v6_early_demux(struct sk_buff *skb)
1021{
1022 struct net *net = dev_net(skb->dev);
1023 const struct udphdr *uh;
1024 struct sock *sk;
1025 struct dst_entry *dst;
1026 int dif = skb->dev->ifindex;
David Ahern4297a0e2017-08-07 08:44:21 -07001027 int sdif = inet6_sdif(skb);
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001028
1029 if (!pskb_may_pull(skb, skb_transport_offset(skb) +
1030 sizeof(struct udphdr)))
1031 return;
1032
1033 uh = udp_hdr(skb);
1034
1035 if (skb->pkt_type == PACKET_HOST)
1036 sk = __udp6_lib_demux_lookup(net, uh->dest,
1037 &ipv6_hdr(skb)->daddr,
1038 uh->source, &ipv6_hdr(skb)->saddr,
David Ahern4297a0e2017-08-07 08:44:21 -07001039 dif, sdif);
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001040 else
1041 return;
1042
Reshetova, Elena41c6d652017-06-30 13:08:01 +03001043 if (!sk || !refcount_inc_not_zero(&sk->sk_refcnt))
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001044 return;
1045
1046 skb->sk = sk;
1047 skb->destructor = sock_efree;
1048 dst = READ_ONCE(sk->sk_rx_dst);
1049
1050 if (dst)
1051 dst = dst_check(dst, inet6_sk(sk)->rx_dst_cookie);
1052 if (dst) {
Wei Wangd24406c2017-06-17 10:42:25 -07001053 /* set noref for now.
1054 * any place which wants to hold dst has to call
1055 * dst_hold_safe()
1056 */
1057 skb_dst_set_noref(skb, dst);
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001058 }
1059}
1060
Herbert Xue5bbef22007-10-15 12:50:28 -07001061static __inline__ int udpv6_rcv(struct sk_buff *skb)
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001062{
Eric Dumazet645ca702008-10-29 01:41:45 -07001063 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001064}
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066/*
1067 * Throw away all pending data and cancel the corking. Socket is locked.
1068 */
1069static void udp_v6_flush_pending_frames(struct sock *sk)
1070{
1071 struct udp_sock *up = udp_sk(sk);
1072
Denis V. Lunev36d926b2008-06-04 15:49:07 +04001073 if (up->pending == AF_INET)
1074 udp_flush_pending_frames(sk);
1075 else if (up->pending) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 up->len = 0;
1077 up->pending = 0;
1078 ip6_flush_pending_frames(sk);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}
1081
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001082static int udpv6_pre_connect(struct sock *sk, struct sockaddr *uaddr,
1083 int addr_len)
1084{
1085 /* The following checks are replicated from __ip6_datagram_connect()
1086 * and intended to prevent BPF program called below from accessing
1087 * bytes that are out of the bound specified by user in addr_len.
1088 */
1089 if (uaddr->sa_family == AF_INET) {
1090 if (__ipv6_only_sock(sk))
1091 return -EAFNOSUPPORT;
1092 return udp_pre_connect(sk, uaddr, addr_len);
1093 }
1094
1095 if (addr_len < SIN6_LEN_RFC2133)
1096 return -EINVAL;
1097
1098 return BPF_CGROUP_RUN_PROG_INET6_CONNECT_LOCK(sk, uaddr);
1099}
1100
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001101/**
Ian Morris67ba4152014-08-24 21:53:10 +01001102 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
1103 * @sk: socket we are sending on
1104 * @skb: sk_buff containing the filled-in UDP header
1105 * (checksum field must be zeroed out)
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001106 */
1107static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
1108 const struct in6_addr *saddr,
1109 const struct in6_addr *daddr, int len)
1110{
1111 unsigned int offset;
1112 struct udphdr *uh = udp_hdr(skb);
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001113 struct sk_buff *frags = skb_shinfo(skb)->frag_list;
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001114 __wsum csum = 0;
1115
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001116 if (!frags) {
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001117 /* Only one fragment on the socket. */
1118 skb->csum_start = skb_transport_header(skb) - skb->head;
1119 skb->csum_offset = offsetof(struct udphdr, check);
1120 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
1121 } else {
1122 /*
1123 * HW-checksum won't work as there are two or more
1124 * fragments on the socket so that all csums of sk_buffs
1125 * should be together
1126 */
1127 offset = skb_transport_offset(skb);
1128 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
Subash Abhinov Kasiviswanathan63ecc3d2017-09-13 19:30:51 -06001129 csum = skb->csum;
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001130
1131 skb->ip_summed = CHECKSUM_NONE;
1132
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001133 do {
1134 csum = csum_add(csum, frags->csum);
1135 } while ((frags = frags->next));
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001136
1137 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
1138 csum);
1139 if (uh->check == 0)
1140 uh->check = CSUM_MANGLED_0;
1141 }
1142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144/*
1145 * Sending
1146 */
1147
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001148static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6,
1149 struct inet_cork *cork)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150{
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001151 struct sock *sk = skb->sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct udphdr *uh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 int err = 0;
Wang Chenb2bf1e22007-12-03 22:34:16 +11001154 int is_udplite = IS_UDPLITE(sk);
Al Viro868c86b2006-11-14 21:35:48 -08001155 __wsum csum = 0;
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001156 int offset = skb_transport_offset(skb);
1157 int len = skb->len - offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 /*
1160 * Create a UDP header
1161 */
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03001162 uh = udp_hdr(skb);
David S. Miller1958b852011-03-12 16:36:19 -05001163 uh->source = fl6->fl6_sport;
1164 uh->dest = fl6->fl6_dport;
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001165 uh->len = htons(len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 uh->check = 0;
1167
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001168 if (cork->gso_size) {
1169 const int hlen = skb_network_header_len(skb) +
1170 sizeof(struct udphdr);
1171
1172 if (hlen + cork->gso_size > cork->fragsize)
1173 return -EINVAL;
1174 if (skb->len > cork->gso_size * UDP_MAX_SEGMENTS)
1175 return -EINVAL;
Willem de Bruijna8c744a2018-04-30 15:58:36 -04001176 if (udp_sk(sk)->no_check6_tx)
1177 return -EINVAL;
Willem de Bruijnff063422018-05-22 11:34:39 -04001178 if (skb->ip_summed != CHECKSUM_PARTIAL || is_udplite ||
1179 dst_xfrm(skb_dst(skb)))
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001180 return -EIO;
1181
1182 skb_shinfo(skb)->gso_size = cork->gso_size;
1183 skb_shinfo(skb)->gso_type = SKB_GSO_UDP_L4;
Willem de Bruijna8c744a2018-04-30 15:58:36 -04001184 goto csum_partial;
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001185 }
1186
Wang Chenb2bf1e22007-12-03 22:34:16 +11001187 if (is_udplite)
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001188 csum = udplite_csum(skb);
1189 else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */
Tom Herbert40685792014-05-02 16:29:58 -07001190 skb->ip_summed = CHECKSUM_NONE;
1191 goto send;
1192 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
Willem de Bruijna8c744a2018-04-30 15:58:36 -04001193csum_partial:
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001194 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001195 goto send;
1196 } else
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001197 csum = udp_csum(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001199 /* add protocol-dependent pseudo-header */
David S. Miller4c9483b2011-03-12 16:22:43 -05001200 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001201 len, fl6->flowi6_proto, csum);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (uh->check == 0)
Al Virof6ab0282006-11-16 02:36:50 -08001203 uh->check = CSUM_MANGLED_0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Sridhar Samudrala493c6be2009-07-09 08:09:54 +00001205send:
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001206 err = ip6_send_skb(skb);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001207 if (err) {
1208 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
Eric Dumazet6aef70a2016-04-27 16:44:27 -07001209 UDP6_INC_STATS(sock_net(sk),
1210 UDP_MIB_SNDBUFERRORS, is_udplite);
Eric Dumazet6ce9e7b2009-09-02 18:05:33 -07001211 err = 0;
1212 }
Eric Dumazet6aef70a2016-04-27 16:44:27 -07001213 } else {
1214 UDP6_INC_STATS(sock_net(sk),
1215 UDP_MIB_OUTDATAGRAMS, is_udplite);
1216 }
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001217 return err;
1218}
1219
1220static int udp_v6_push_pending_frames(struct sock *sk)
1221{
1222 struct sk_buff *skb;
1223 struct udp_sock *up = udp_sk(sk);
1224 struct flowi6 fl6;
1225 int err = 0;
1226
1227 if (up->pending == AF_INET)
1228 return udp_push_pending_frames(sk);
1229
1230 /* ip6_finish_skb will release the cork, so make a copy of
1231 * fl6 here.
1232 */
1233 fl6 = inet_sk(sk)->cork.fl.u.ip6;
1234
1235 skb = ip6_finish_skb(sk);
1236 if (!skb)
1237 goto out;
1238
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001239 err = udp_v6_send_skb(skb, &fl6, &inet_sk(sk)->cork.base);
Vlad Yasevichd39d9382015-01-31 10:40:16 -05001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241out:
1242 up->len = 0;
1243 up->pending = 0;
1244 return err;
1245}
1246
Ying Xue1b784142015-03-02 15:37:48 +08001247int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248{
1249 struct ipv6_txoptions opt_space;
1250 struct udp_sock *up = udp_sk(sk);
1251 struct inet_sock *inet = inet_sk(sk);
1252 struct ipv6_pinfo *np = inet6_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01001253 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001254 struct in6_addr *daddr, *final_p, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 struct ipv6_txoptions *opt = NULL;
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001256 struct ipv6_txoptions *opt_to_free = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 struct ip6_flowlabel *flowlabel = NULL;
David S. Miller4c9483b2011-03-12 16:22:43 -05001258 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct dst_entry *dst;
Wei Wang26879da2016-05-02 21:40:07 -07001260 struct ipcm6_cookie ipc6;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 int addr_len = msg->msg_namelen;
Alexey Kodanev9f542f62018-04-03 15:00:09 +03001262 bool connected = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 int ulen = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1265 int err;
Wang Chenb2bf1e22007-12-03 22:34:16 +11001266 int is_udplite = IS_UDPLITE(sk);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001267 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Willem de Bruijnb515430a2018-07-06 10:12:55 -04001269 ipcm6_init(&ipc6);
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001270 ipc6.gso_size = up->gso_size;
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001271 ipc6.sockc.tsflags = sk->sk_tsflags;
Wei Wang26879da2016-05-02 21:40:07 -07001272
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 /* destination address check */
1274 if (sin6) {
1275 if (addr_len < offsetof(struct sockaddr, sa_data))
1276 return -EINVAL;
1277
1278 switch (sin6->sin6_family) {
1279 case AF_INET6:
1280 if (addr_len < SIN6_LEN_RFC2133)
1281 return -EINVAL;
1282 daddr = &sin6->sin6_addr;
Jonathan T. Leighton052d2362017-02-12 17:26:07 -05001283 if (ipv6_addr_any(daddr) &&
1284 ipv6_addr_v4mapped(&np->saddr))
1285 ipv6_addr_set_v4mapped(htonl(INADDR_LOOPBACK),
1286 daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 break;
1288 case AF_INET:
1289 goto do_udp_sendmsg;
1290 case AF_UNSPEC:
1291 msg->msg_name = sin6 = NULL;
1292 msg->msg_namelen = addr_len = 0;
1293 daddr = NULL;
1294 break;
1295 default:
1296 return -EINVAL;
1297 }
1298 } else if (!up->pending) {
1299 if (sk->sk_state != TCP_ESTABLISHED)
1300 return -EDESTADDRREQ;
Eric Dumazetefe42082013-10-03 15:42:29 -07001301 daddr = &sk->sk_v6_daddr;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001302 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 daddr = NULL;
1304
1305 if (daddr) {
Brian Haleye773e4f2007-08-24 23:16:08 -07001306 if (ipv6_addr_v4mapped(daddr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 struct sockaddr_in sin;
1308 sin.sin_family = AF_INET;
Eric Dumazetc720c7e82009-10-15 06:30:45 +00001309 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1311 msg->msg_name = &sin;
1312 msg->msg_namelen = sizeof(sin);
1313do_udp_sendmsg:
1314 if (__ipv6_only_sock(sk))
1315 return -ENETUNREACH;
Ying Xue1b784142015-03-02 15:37:48 +08001316 return udp_sendmsg(sk, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318 }
1319
1320 if (up->pending == AF_INET)
Ying Xue1b784142015-03-02 15:37:48 +08001321 return udp_sendmsg(sk, msg, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 /* Rough check on arithmetic overflow,
YOSHIFUJI Hideakib59e1392007-03-30 14:45:35 -07001324 better check is made in ip6_append_data().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
1326 if (len > INT_MAX - sizeof(struct udphdr))
1327 return -EMSGSIZE;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001328
Vlad Yasevich03485f22015-01-31 10:40:17 -05001329 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 if (up->pending) {
1331 /*
1332 * There are pending frames.
1333 * The socket lock must be held while it's corked.
1334 */
1335 lock_sock(sk);
1336 if (likely(up->pending)) {
1337 if (unlikely(up->pending != AF_INET6)) {
1338 release_sock(sk);
1339 return -EAFNOSUPPORT;
1340 }
1341 dst = NULL;
1342 goto do_append_data;
1343 }
1344 release_sock(sk);
1345 }
1346 ulen += sizeof(struct udphdr);
1347
David S. Miller4c9483b2011-03-12 16:22:43 -05001348 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
1350 if (sin6) {
1351 if (sin6->sin6_port == 0)
1352 return -EINVAL;
1353
David S. Miller1958b852011-03-12 16:36:19 -05001354 fl6.fl6_dport = sin6->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 daddr = &sin6->sin6_addr;
1356
1357 if (np->sndflow) {
David S. Miller4c9483b2011-03-12 16:22:43 -05001358 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1359 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1360 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Ian Morris63159f22015-03-29 14:00:04 +01001361 if (!flowlabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
1364 }
1365
1366 /*
1367 * Otherwise it will be difficult to maintain
1368 * sk->sk_dst_cache.
1369 */
1370 if (sk->sk_state == TCP_ESTABLISHED &&
Eric Dumazetefe42082013-10-03 15:42:29 -07001371 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1372 daddr = &sk->sk_v6_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 if (addr_len >= sizeof(struct sockaddr_in6) &&
1375 sin6->sin6_scope_id &&
Hannes Frederic Sowa842df072013-03-08 02:07:19 +00001376 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
David S. Miller4c9483b2011-03-12 16:22:43 -05001377 fl6.flowi6_oif = sin6->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 } else {
1379 if (sk->sk_state != TCP_ESTABLISHED)
1380 return -EDESTADDRREQ;
1381
David S. Miller1958b852011-03-12 16:36:19 -05001382 fl6.fl6_dport = inet->inet_dport;
Eric Dumazetefe42082013-10-03 15:42:29 -07001383 daddr = &sk->sk_v6_daddr;
David S. Miller4c9483b2011-03-12 16:22:43 -05001384 fl6.flowlabel = np->flow_label;
Alexey Kodanev9f542f62018-04-03 15:00:09 +03001385 connected = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 }
1387
David S. Miller4c9483b2011-03-12 16:22:43 -05001388 if (!fl6.flowi6_oif)
1389 fl6.flowi6_oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
David S. Miller4c9483b2011-03-12 16:22:43 -05001391 if (!fl6.flowi6_oif)
1392 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
Yang Hongyang9f690db2008-12-16 02:08:29 -08001393
David S. Miller4c9483b2011-03-12 16:22:43 -05001394 fl6.flowi6_mark = sk->sk_mark;
Lorenzo Colittie2d118a2016-11-04 02:23:43 +09001395 fl6.flowi6_uid = sk->sk_uid;
Brian Haley51953d52009-10-05 08:24:16 +00001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 if (msg->msg_controllen) {
1398 opt = &opt_space;
1399 memset(opt, 0, sizeof(struct ipv6_txoptions));
1400 opt->tot_len = sizeof(*opt);
Wei Wang26879da2016-05-02 21:40:07 -07001401 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Willem de Bruijn2e8de852018-04-26 13:42:20 -04001403 err = udp_cmsg_send(sk, msg, &ipc6.gso_size);
1404 if (err > 0)
1405 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001406 &ipc6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (err < 0) {
1408 fl6_sock_release(flowlabel);
1409 return err;
1410 }
David S. Miller4c9483b2011-03-12 16:22:43 -05001411 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1412 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Ian Morris63159f22015-03-29 14:00:04 +01001413 if (!flowlabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return -EINVAL;
1415 }
1416 if (!(opt->opt_nflen|opt->opt_flen))
1417 opt = NULL;
Alexey Kodanev9f542f62018-04-03 15:00:09 +03001418 connected = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 }
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001420 if (!opt) {
1421 opt = txopt_get(np);
1422 opt_to_free = opt;
1423 }
YOSHIFUJI Hideakidf9890c2005-11-20 12:23:18 +09001424 if (flowlabel)
1425 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1426 opt = ipv6_fixup_options(&opt_space, opt);
Wei Wang26879da2016-05-02 21:40:07 -07001427 ipc6.opt = opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
David S. Miller4c9483b2011-03-12 16:22:43 -05001429 fl6.flowi6_proto = sk->sk_protocol;
Brian Haley876c7f42008-04-11 00:38:24 -04001430 if (!ipv6_addr_any(daddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001431 fl6.daddr = *daddr;
Brian Haley876c7f42008-04-11 00:38:24 -04001432 else
David S. Miller4c9483b2011-03-12 16:22:43 -05001433 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1434 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00001435 fl6.saddr = np->saddr;
David S. Miller1958b852011-03-12 16:36:19 -05001436 fl6.fl6_sport = inet->inet_sport;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09001437
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001438 if (cgroup_bpf_enabled && !connected) {
1439 err = BPF_CGROUP_RUN_PROG_UDP6_SENDMSG_LOCK(sk,
1440 (struct sockaddr *)sin6, &fl6.saddr);
1441 if (err)
1442 goto out_no_dst;
1443 if (sin6) {
1444 if (ipv6_addr_v4mapped(&sin6->sin6_addr)) {
1445 /* BPF program rewrote IPv6-only by IPv4-mapped
1446 * IPv6. It's currently unsupported.
1447 */
1448 err = -ENOTSUPP;
1449 goto out_no_dst;
1450 }
1451 if (sin6->sin6_port == 0) {
1452 /* BPF program set invalid port. Reject it. */
1453 err = -EINVAL;
1454 goto out_no_dst;
1455 }
1456 fl6.fl6_dport = sin6->sin6_port;
1457 fl6.daddr = sin6->sin6_addr;
1458 }
1459 }
1460
David S. Miller4c9483b2011-03-12 16:22:43 -05001461 final_p = fl6_update_dst(&fl6, opt, &final);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +00001462 if (final_p)
Alexey Kodanev9f542f62018-04-03 15:00:09 +03001463 connected = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
David S. Miller4c9483b2011-03-12 16:22:43 -05001465 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1466 fl6.flowi6_oif = np->mcast_oif;
Alexey Kodanev9f542f62018-04-03 15:00:09 +03001467 connected = false;
Erich E. Hooverc4062df2012-02-08 09:11:08 +00001468 } else if (!fl6.flowi6_oif)
1469 fl6.flowi6_oif = np->ucast_oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470
David S. Miller4c9483b2011-03-12 16:22:43 -05001471 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -07001472
Hannes Frederic Sowa38b70972016-06-11 20:08:19 +02001473 if (ipc6.tclass < 0)
1474 ipc6.tclass = np->tclass;
1475
1476 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1477
Alexey Kodanev4f858c52018-04-03 15:00:10 +03001478 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p, connected);
David S. Miller68d0c6d2011-03-01 13:19:07 -08001479 if (IS_ERR(dst)) {
1480 err = PTR_ERR(dst);
1481 dst = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -07001483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
Wei Wang26879da2016-05-02 21:40:07 -07001485 if (ipc6.hlimit < 0)
1486 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 if (msg->msg_flags&MSG_CONFIRM)
1489 goto do_confirm;
1490back_from_confirm:
1491
Vlad Yasevich03485f22015-01-31 10:40:17 -05001492 /* Lockless fast path for the non-corking case */
1493 if (!corkreq) {
Willem de Bruijn1cd78842018-04-26 13:42:15 -04001494 struct inet_cork_full cork;
Vlad Yasevich03485f22015-01-31 10:40:17 -05001495 struct sk_buff *skb;
1496
1497 skb = ip6_make_skb(sk, getfrag, msg, ulen,
Wei Wang26879da2016-05-02 21:40:07 -07001498 sizeof(struct udphdr), &ipc6,
Vlad Yasevich03485f22015-01-31 10:40:17 -05001499 &fl6, (struct rt6_info *)dst,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001500 msg->msg_flags, &cork);
Vlad Yasevich03485f22015-01-31 10:40:17 -05001501 err = PTR_ERR(skb);
1502 if (!IS_ERR_OR_NULL(skb))
Willem de Bruijnbec1f6f2018-04-26 13:42:17 -04001503 err = udp_v6_send_skb(skb, &fl6, &cork.base);
Alexey Kodanev4f858c52018-04-03 15:00:10 +03001504 goto out;
Vlad Yasevich03485f22015-01-31 10:40:17 -05001505 }
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 lock_sock(sk);
1508 if (unlikely(up->pending)) {
1509 /* The socket is already corked while preparing it. */
1510 /* ... which is an evident application bug. --ANK */
1511 release_sock(sk);
1512
Joe Perchesba7a46f2014-11-11 10:59:17 -08001513 net_dbg_ratelimited("udp cork app bug 2\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 err = -EINVAL;
1515 goto out;
1516 }
1517
1518 up->pending = AF_INET6;
1519
1520do_append_data:
Wei Wang26879da2016-05-02 21:40:07 -07001521 if (ipc6.dontfrag < 0)
1522 ipc6.dontfrag = np->dontfrag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 up->len += ulen;
Wei Wang26879da2016-05-02 21:40:07 -07001524 err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1525 &ipc6, &fl6, (struct rt6_info *)dst,
Willem de Bruijn5fdaa882018-07-06 10:12:57 -04001526 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 if (err)
1528 udp_v6_flush_pending_frames(sk);
1529 else if (!corkreq)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001530 err = udp_v6_push_pending_frames(sk);
Herbert Xu1e0c14f2006-10-03 14:35:49 -07001531 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1532 up->pending = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Vlad Yasevich03485f22015-01-31 10:40:17 -05001534 if (err > 0)
1535 err = np->recverr ? net_xmit_errno(err) : 0;
1536 release_sock(sk);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538out:
YOSHIFUJI Hideakia3c96082008-06-04 01:30:25 +09001539 dst_release(dst);
Andrey Ignatov1cedee12018-05-25 08:55:23 -07001540out_no_dst:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 fl6_sock_release(flowlabel);
Eric Dumazet45f6fad2015-11-29 19:37:57 -08001542 txopt_put(opt_to_free);
YOSHIFUJI Hideakicd562c92007-09-14 17:15:01 -07001543 if (!err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 return len;
David S. Millera18135e2006-08-15 00:00:09 -07001545 /*
1546 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1547 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1548 * we don't have a good statistic (IpOutDiscards but it can be too many
1549 * things). We could add another new stat but at least for now that
1550 * seems like overkill.
1551 */
1552 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
Eric Dumazet6aef70a2016-04-27 16:44:27 -07001553 UDP6_INC_STATS(sock_net(sk),
1554 UDP_MIB_SNDBUFERRORS, is_udplite);
David S. Millera18135e2006-08-15 00:00:09 -07001555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 return err;
1557
1558do_confirm:
Julian Anastasov0dec8792017-02-06 23:14:16 +02001559 if (msg->msg_flags & MSG_PROBE)
1560 dst_confirm_neigh(dst, &fl6.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 if (!(msg->msg_flags&MSG_PROBE) || len)
1562 goto back_from_confirm;
1563 err = 0;
1564 goto out;
1565}
1566
Brian Haley7d06b2e2008-06-14 17:04:49 -07001567void udpv6_destroy_sock(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Tom Parkin44046a52013-03-19 06:11:12 +00001569 struct udp_sock *up = udp_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 lock_sock(sk);
1571 udp_v6_flush_pending_frames(sk);
1572 release_sock(sk);
1573
Paolo Abeni60fb9562018-11-07 12:38:28 +01001574 if (static_branch_unlikely(&udpv6_encap_needed_key)) {
1575 if (up->encap_type) {
1576 void (*encap_destroy)(struct sock *sk);
1577 encap_destroy = READ_ONCE(up->encap_destroy);
1578 if (encap_destroy)
1579 encap_destroy(sk);
1580 }
1581 if (up->encap_enabled)
Paolo Abeni9c480602018-11-15 02:34:50 +01001582 static_branch_dec(&udpv6_encap_needed_key);
Tom Parkin44046a52013-03-19 06:11:12 +00001583 }
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 inet6_destroy_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588/*
1589 * Socket option code for UDP
1590 */
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001591int udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001592 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001593{
David S. Millerdb8dac22008-03-06 16:22:02 -08001594 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001595 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1596 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001597 return ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001598}
1599
1600#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001601int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001602 char __user *optval, unsigned int optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001603{
David S. Millerdb8dac22008-03-06 16:22:02 -08001604 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001605 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1606 udp_v6_push_pending_frames);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001607 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001608}
1609#endif
1610
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001611int udpv6_getsockopt(struct sock *sk, int level, int optname,
1612 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001613{
David S. Millerdb8dac22008-03-06 16:22:02 -08001614 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001615 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001616 return ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001617}
1618
1619#ifdef CONFIG_COMPAT
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001620int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1621 char __user *optval, int __user *optlen)
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001622{
David S. Millerdb8dac22008-03-06 16:22:02 -08001623 if (level == SOL_UDP || level == SOL_UDPLITE)
Gerrit Renker4c0a6cb2006-11-27 09:29:59 -08001624 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001625 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001626}
1627#endif
1628
David Aherna8e3bb32017-08-28 15:14:20 -07001629/* thinking of making this const? Don't.
1630 * early_demux can change based on sysctl.
1631 */
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -06001632static struct inet6_protocol udpv6_protocol = {
subashab@codeaurora.org54250772017-03-08 16:36:49 -07001633 .early_demux = udp_v6_early_demux,
subashab@codeaurora.orgdddb64b2017-03-23 13:34:16 -06001634 .early_demux_handler = udp_v6_early_demux,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 .handler = udpv6_rcv,
1636 .err_handler = udpv6_err,
1637 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1638};
1639
1640/* ------------------------------------------------------------------------ */
1641#ifdef CONFIG_PROC_FS
Gerrit Renkerba4e58e2006-11-27 11:10:57 -08001642int udp6_seq_show(struct seq_file *seq, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001644 if (v == SEQ_START_TOKEN) {
1645 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1646 } else {
1647 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1648 struct inet_sock *inet = inet_sk(v);
1649 __u16 srcp = ntohs(inet->inet_sport);
1650 __u16 destp = ntohs(inet->inet_dport);
Paolo Abeni6c206b22018-06-08 11:35:40 +02001651 __ip6_dgram_sock_seq_show(seq, v, srcp, destp,
1652 udp_rqueue_get(v), bucket);
Lorenzo Colitti17ef66af2013-05-31 15:05:48 +00001653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 return 0;
1655}
1656
Christoph Hellwigc3506372018-04-10 19:42:55 +02001657const struct seq_operations udp6_seq_ops = {
Christoph Hellwiga3d25992018-04-10 21:31:50 +02001658 .start = udp_seq_start,
1659 .next = udp_seq_next,
1660 .stop = udp_seq_stop,
1661 .show = udp6_seq_show,
1662};
Christoph Hellwigc3506372018-04-10 19:42:55 +02001663EXPORT_SYMBOL(udp6_seq_ops);
Arjan van de Ven73cb88e2011-10-30 06:46:30 +00001664
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665static struct udp_seq_afinfo udp6_seq_afinfo = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 .family = AF_INET6,
Eric Dumazet645ca702008-10-29 01:41:45 -07001667 .udp_table = &udp_table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668};
1669
Alexey Dobriyan2c8c1e72010-01-17 03:35:32 +00001670int __net_init udp6_proc_init(struct net *net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671{
Christoph Hellwigc3506372018-04-10 19:42:55 +02001672 if (!proc_create_net_data("udp6", 0444, net->proc_net, &udp6_seq_ops,
1673 sizeof(struct udp_iter_state), &udp6_seq_afinfo))
Christoph Hellwiga3d25992018-04-10 21:31:50 +02001674 return -ENOMEM;
1675 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Ian Morrisec120da2015-08-14 22:43:38 +01001678void udp6_proc_exit(struct net *net)
1679{
Christoph Hellwiga3d25992018-04-10 21:31:50 +02001680 remove_proc_entry("udp6", net->proc_net);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682#endif /* CONFIG_PROC_FS */
1683
1684/* ------------------------------------------------------------------------ */
1685
1686struct proto udpv6_prot = {
Tonghao Zhang1e802952018-03-13 21:57:16 -07001687 .name = "UDPv6",
1688 .owner = THIS_MODULE,
1689 .close = udp_lib_close,
Andrey Ignatovd74bad42018-03-30 15:08:05 -07001690 .pre_connect = udpv6_pre_connect,
Tonghao Zhang1e802952018-03-13 21:57:16 -07001691 .connect = ip6_datagram_connect,
1692 .disconnect = udp_disconnect,
1693 .ioctl = udp_ioctl,
1694 .init = udp_init_sock,
1695 .destroy = udpv6_destroy_sock,
1696 .setsockopt = udpv6_setsockopt,
1697 .getsockopt = udpv6_getsockopt,
1698 .sendmsg = udpv6_sendmsg,
1699 .recvmsg = udpv6_recvmsg,
1700 .release_cb = ip6_datagram_release_cb,
1701 .hash = udp_lib_hash,
1702 .unhash = udp_lib_unhash,
1703 .rehash = udp_v6_rehash,
1704 .get_port = udp_v6_get_port,
1705 .memory_allocated = &udp_memory_allocated,
1706 .sysctl_mem = sysctl_udp_mem,
1707 .sysctl_wmem_offset = offsetof(struct net, ipv4.sysctl_udp_wmem_min),
1708 .sysctl_rmem_offset = offsetof(struct net, ipv4.sysctl_udp_rmem_min),
1709 .obj_size = sizeof(struct udp6_sock),
1710 .h.udp_table = &udp_table,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001711#ifdef CONFIG_COMPAT
Tonghao Zhang1e802952018-03-13 21:57:16 -07001712 .compat_setsockopt = compat_udpv6_setsockopt,
1713 .compat_getsockopt = compat_udpv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001714#endif
Tonghao Zhang1e802952018-03-13 21:57:16 -07001715 .diag_destroy = udp_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716};
1717
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718static struct inet_protosw udpv6_protosw = {
1719 .type = SOCK_DGRAM,
1720 .protocol = IPPROTO_UDP,
1721 .prot = &udpv6_prot,
1722 .ops = &inet6_dgram_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 .flags = INET_PROTOSW_PERMANENT,
1724};
1725
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001726int __init udpv6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001728 int ret;
1729
Vlad Yasevich33362882012-11-15 08:49:15 +00001730 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1731 if (ret)
Vlad Yasevichc6b641a2012-11-15 08:49:22 +00001732 goto out;
Vlad Yasevich33362882012-11-15 08:49:15 +00001733
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001734 ret = inet6_register_protosw(&udpv6_protosw);
1735 if (ret)
1736 goto out_udpv6_protocol;
1737out:
1738 return ret;
1739
1740out_udpv6_protocol:
1741 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1742 goto out;
1743}
1744
Daniel Lezcano09f77092007-12-13 05:34:58 -08001745void udpv6_exit(void)
Daniel Lezcano7f4e4862007-12-11 02:25:35 -08001746{
1747 inet6_unregister_protosw(&udpv6_protosw);
1748 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749}