]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - net/ipv6/udp.c
net/sock: add an explicit sk argument for ip_cmsg_recv_offset()
[mirror_ubuntu-artful-kernel.git] / net / ipv6 / udp.c
CommitLineData
1da177e4
LT
1/*
2 * UDP over IPv6
1ab1457c 3 * Linux INET6 implementation
1da177e4
LT
4 *
5 * Authors:
1ab1457c 6 * Pedro Roque <roque@di.fc.ul.pt>
1da177e4
LT
7 *
8 * Based on linux/ipv4/udp.c
9 *
1da177e4
LT
10 * Fixes:
11 * Hideaki YOSHIFUJI : sin6_scope_id support
12 * YOSHIFUJI Hideaki @USAGI and: Support IPV6_V6ONLY socket option, which
13 * Alexey Kuznetsov allow both IPv4 and IPv6 sockets to bind
14 * a single port at the same time.
15 * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
16 * YOSHIFUJI Hideaki @USAGI: convert /proc/net/udp6 to seq_file.
17 *
18 * This program is free software; you can redistribute it and/or
19 * modify it under the terms of the GNU General Public License
20 * as published by the Free Software Foundation; either version
21 * 2 of the License, or (at your option) any later version.
22 */
23
1da177e4
LT
24#include <linux/errno.h>
25#include <linux/types.h>
26#include <linux/socket.h>
27#include <linux/sockios.h>
1da177e4
LT
28#include <linux/net.h>
29#include <linux/in6.h>
30#include <linux/netdevice.h>
31#include <linux/if_arp.h>
32#include <linux/ipv6.h>
33#include <linux/icmpv6.h>
34#include <linux/init.h>
1781f7f5 35#include <linux/module.h>
3305b80c 36#include <linux/skbuff.h>
5a0e3ad6 37#include <linux/slab.h>
1da177e4
LT
38#include <asm/uaccess.h>
39
496611d7 40#include <net/addrconf.h>
1da177e4
LT
41#include <net/ndisc.h>
42#include <net/protocol.h>
43#include <net/transp_v6.h>
44#include <net/ip6_route.h>
1da177e4 45#include <net/raw.h>
c752f073 46#include <net/tcp_states.h>
1da177e4
LT
47#include <net/ip6_checksum.h>
48#include <net/xfrm.h>
72289b96 49#include <net/inet6_hashtables.h>
076bb0c8 50#include <net/busy_poll.h>
e32ea7e7 51#include <net/sock_reuseport.h>
1da177e4
LT
52
53#include <linux/proc_fs.h>
54#include <linux/seq_file.h>
22911fc5 55#include <trace/events/skb.h>
ba4e58ec 56#include "udp_impl.h"
1da177e4 57
6eada011
ED
58static u32 udp6_ehashfn(const struct net *net,
59 const struct in6_addr *laddr,
60 const u16 lport,
61 const struct in6_addr *faddr,
62 const __be16 fport)
b50026b5 63{
1bbdceef
HFS
64 static u32 udp6_ehash_secret __read_mostly;
65 static u32 udp_ipv6_hash_secret __read_mostly;
66
67 u32 lhash, fhash;
68
69 net_get_random_once(&udp6_ehash_secret,
70 sizeof(udp6_ehash_secret));
71 net_get_random_once(&udp_ipv6_hash_secret,
72 sizeof(udp_ipv6_hash_secret));
73
74 lhash = (__force u32)laddr->s6_addr32[3];
75 fhash = __ipv6_addr_jhash(faddr, udp_ipv6_hash_secret);
76
b50026b5 77 return __inet6_ehashfn(lhash, lport, fhash, fport,
1bbdceef 78 udp_ipv6_hash_secret + net_hash_mix(net));
b50026b5
HFS
79}
80
6eada011
ED
81static u32 udp6_portaddr_hash(const struct net *net,
82 const struct in6_addr *addr6,
83 unsigned int port)
d4cada4a
ED
84{
85 unsigned int hash, mix = net_hash_mix(net);
86
87 if (ipv6_addr_any(addr6))
88 hash = jhash_1word(0, mix);
856540ee 89 else if (ipv6_addr_v4mapped(addr6))
0eae88f3 90 hash = jhash_1word((__force u32)addr6->s6_addr32[3], mix);
d4cada4a 91 else
0eae88f3 92 hash = jhash2((__force u32 *)addr6->s6_addr32, 4, mix);
d4cada4a
ED
93
94 return hash ^ port;
95}
96
6ba5a3c5 97int udp_v6_get_port(struct sock *sk, unsigned short snum)
1da177e4 98{
30fff923
ED
99 unsigned int hash2_nulladdr =
100 udp6_portaddr_hash(sock_net(sk), &in6addr_any, snum);
9a52e97e 101 unsigned int hash2_partial =
efe4208f 102 udp6_portaddr_hash(sock_net(sk), &sk->sk_v6_rcv_saddr, 0);
30fff923 103
d4cada4a 104 /* precompute partial secondary hash */
30fff923
ED
105 udp_sk(sk)->udp_portaddr_hash = hash2_partial;
106 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal, hash2_nulladdr);
1da177e4
LT
107}
108
719f8358
ED
109static void udp_v6_rehash(struct sock *sk)
110{
111 u16 new_hash = udp6_portaddr_hash(sock_net(sk),
efe4208f 112 &sk->sk_v6_rcv_saddr,
719f8358
ED
113 inet_sk(sk)->inet_num);
114
115 udp_lib_rehash(sk, new_hash);
116}
117
d1e37288
SX
118static int compute_score(struct sock *sk, struct net *net,
119 const struct in6_addr *saddr, __be16 sport,
120 const struct in6_addr *daddr, unsigned short hnum,
121 int dif)
645ca708 122{
60c04aec
JP
123 int score;
124 struct inet_sock *inet;
125
126 if (!net_eq(sock_net(sk), net) ||
127 udp_sk(sk)->udp_port_hash != hnum ||
128 sk->sk_family != PF_INET6)
129 return -1;
130
131 score = 0;
132 inet = inet_sk(sk);
133
134 if (inet->inet_dport) {
135 if (inet->inet_dport != sport)
136 return -1;
137 score++;
138 }
139
140 if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
141 if (!ipv6_addr_equal(&sk->sk_v6_rcv_saddr, daddr))
142 return -1;
143 score++;
144 }
145
146 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
147 if (!ipv6_addr_equal(&sk->sk_v6_daddr, saddr))
148 return -1;
149 score++;
150 }
151
152 if (sk->sk_bound_dev_if) {
153 if (sk->sk_bound_dev_if != dif)
154 return -1;
155 score++;
645ca708 156 }
60c04aec 157
70da268b
ED
158 if (sk->sk_incoming_cpu == raw_smp_processor_id())
159 score++;
160
645ca708
ED
161 return score;
162}
163
d1e37288 164/* called with rcu_read_lock() */
fddc17de
ED
165static struct sock *udp6_lib_lookup2(struct net *net,
166 const struct in6_addr *saddr, __be16 sport,
167 const struct in6_addr *daddr, unsigned int hnum, int dif,
d1e37288 168 struct udp_hslot *hslot2,
1134158b 169 struct sk_buff *skb)
fddc17de
ED
170{
171 struct sock *sk, *result;
72289b96
TH
172 int score, badness, matches = 0, reuseport = 0;
173 u32 hash = 0;
fddc17de 174
fddc17de
ED
175 result = NULL;
176 badness = -1;
ca065d0c 177 udp_portaddr_for_each_entry_rcu(sk, &hslot2->head) {
d1e37288 178 score = compute_score(sk, net, saddr, sport,
fddc17de
ED
179 daddr, hnum, dif);
180 if (score > badness) {
72289b96
TH
181 reuseport = sk->sk_reuseport;
182 if (reuseport) {
b50026b5
HFS
183 hash = udp6_ehashfn(net, daddr, hnum,
184 saddr, sport);
ed0dfffd 185
ca065d0c 186 result = reuseport_select_sock(sk, hash, skb,
ed0dfffd 187 sizeof(struct udphdr));
ca065d0c
ED
188 if (result)
189 return result;
72289b96 190 matches = 1;
70da268b 191 }
ca065d0c
ED
192 result = sk;
193 badness = score;
72289b96
TH
194 } else if (score == badness && reuseport) {
195 matches++;
8fc54f68 196 if (reciprocal_scale(hash, matches) == 0)
72289b96
TH
197 result = sk;
198 hash = next_pseudo_random32(hash);
fddc17de
ED
199 }
200 }
fddc17de
ED
201 return result;
202}
203
ca065d0c 204/* rcu_read_lock() must be held */
fce82338 205struct sock *__udp6_lib_lookup(struct net *net,
88440ae7
BS
206 const struct in6_addr *saddr, __be16 sport,
207 const struct in6_addr *daddr, __be16 dport,
538950a1
CG
208 int dif, struct udp_table *udptable,
209 struct sk_buff *skb)
1da177e4 210{
271b72c7 211 struct sock *sk, *result;
1da177e4 212 unsigned short hnum = ntohs(dport);
fddc17de
ED
213 unsigned int hash2, slot2, slot = udp_hashfn(net, hnum, udptable->mask);
214 struct udp_hslot *hslot2, *hslot = &udptable->hash[slot];
72289b96
TH
215 int score, badness, matches = 0, reuseport = 0;
216 u32 hash = 0;
645ca708 217
fddc17de
ED
218 if (hslot->count > 10) {
219 hash2 = udp6_portaddr_hash(net, daddr, hnum);
220 slot2 = hash2 & udptable->mask;
221 hslot2 = &udptable->hash2[slot2];
222 if (hslot->count < hslot2->count)
223 goto begin;
224
225 result = udp6_lib_lookup2(net, saddr, sport,
226 daddr, hnum, dif,
d1e37288 227 hslot2, skb);
fddc17de 228 if (!result) {
d1e37288 229 unsigned int old_slot2 = slot2;
fddc17de
ED
230 hash2 = udp6_portaddr_hash(net, &in6addr_any, hnum);
231 slot2 = hash2 & udptable->mask;
d1e37288
SX
232 /* avoid searching the same slot again. */
233 if (unlikely(slot2 == old_slot2))
234 return result;
235
fddc17de
ED
236 hslot2 = &udptable->hash2[slot2];
237 if (hslot->count < hslot2->count)
238 goto begin;
239
1223c67c 240 result = udp6_lib_lookup2(net, saddr, sport,
d1e37288
SX
241 daddr, hnum, dif,
242 hslot2, skb);
fddc17de 243 }
fddc17de
ED
244 return result;
245 }
271b72c7
ED
246begin:
247 result = NULL;
248 badness = -1;
ca065d0c 249 sk_for_each_rcu(sk, &hslot->head) {
d1e37288 250 score = compute_score(sk, net, saddr, sport, daddr, hnum, dif);
645ca708 251 if (score > badness) {
72289b96
TH
252 reuseport = sk->sk_reuseport;
253 if (reuseport) {
b50026b5
HFS
254 hash = udp6_ehashfn(net, daddr, hnum,
255 saddr, sport);
ca065d0c 256 result = reuseport_select_sock(sk, hash, skb,
538950a1 257 sizeof(struct udphdr));
ca065d0c
ED
258 if (result)
259 return result;
72289b96
TH
260 matches = 1;
261 }
ca065d0c
ED
262 result = sk;
263 badness = score;
72289b96
TH
264 } else if (score == badness && reuseport) {
265 matches++;
8fc54f68 266 if (reciprocal_scale(hash, matches) == 0)
72289b96
TH
267 result = sk;
268 hash = next_pseudo_random32(hash);
1da177e4
LT
269 }
270 }
1da177e4
LT
271 return result;
272}
fce82338 273EXPORT_SYMBOL_GPL(__udp6_lib_lookup);
1da177e4 274
607c4aaf
KK
275static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
276 __be16 sport, __be16 dport,
645ca708 277 struct udp_table *udptable)
607c4aaf 278{
b71d1d42 279 const struct ipv6hdr *iph = ipv6_hdr(skb);
ed7cbbce 280 struct sock *sk;
607c4aaf 281
e5d08d71
IM
282 sk = skb_steal_sock(skb);
283 if (unlikely(sk))
23542618 284 return sk;
ed7cbbce 285 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
adf30907 286 &iph->daddr, dport, inet6_iif(skb),
538950a1 287 udptable, skb);
607c4aaf
KK
288}
289
63058308
TH
290struct sock *udp6_lib_lookup_skb(struct sk_buff *skb,
291 __be16 sport, __be16 dport)
292{
293 const struct ipv6hdr *iph = ipv6_hdr(skb);
63058308 294
ed7cbbce 295 return __udp6_lib_lookup(dev_net(skb->dev), &iph->saddr, sport,
63058308
TH
296 &iph->daddr, dport, inet6_iif(skb),
297 &udp_table, skb);
298}
299EXPORT_SYMBOL_GPL(udp6_lib_lookup_skb);
300
ca065d0c
ED
301/* Must be called under rcu_read_lock().
302 * Does increment socket refcount.
303 */
304#if IS_ENABLED(CONFIG_NETFILTER_XT_MATCH_SOCKET) || \
305 IS_ENABLED(CONFIG_NETFILTER_XT_TARGET_TPROXY)
aa976fc0
BS
306struct sock *udp6_lib_lookup(struct net *net, const struct in6_addr *saddr, __be16 sport,
307 const struct in6_addr *daddr, __be16 dport, int dif)
308{
ca065d0c
ED
309 struct sock *sk;
310
311 sk = __udp6_lib_lookup(net, saddr, sport, daddr, dport,
312 dif, &udp_table, NULL);
313 if (sk && !atomic_inc_not_zero(&sk->sk_refcnt))
314 sk = NULL;
315 return sk;
aa976fc0
BS
316}
317EXPORT_SYMBOL_GPL(udp6_lib_lookup);
ca065d0c 318#endif
aa976fc0 319
1da177e4 320/*
67ba4152
IM
321 * This should be easy, if there is something there we
322 * return it, otherwise we block.
1da177e4
LT
323 */
324
1b784140 325int udpv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
1da177e4
LT
326 int noblock, int flags, int *addr_len)
327{
328 struct ipv6_pinfo *np = inet6_sk(sk);
329 struct inet_sock *inet = inet_sk(sk);
1ab1457c 330 struct sk_buff *skb;
59c2cdae 331 unsigned int ulen, copied;
627d2d6b 332 int peeked, peeking, off;
759e5d00
HX
333 int err;
334 int is_udplite = IS_UDPLITE(sk);
197c949e 335 bool checksum_valid = false;
f26ba175 336 int is_udp4;
1da177e4 337
1da177e4 338 if (flags & MSG_ERRQUEUE)
85fbaa75 339 return ipv6_recv_error(sk, msg, len, addr_len);
1da177e4 340
4b340ae2 341 if (np->rxpmtu && np->rxopt.bits.rxpmtu)
85fbaa75 342 return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
4b340ae2 343
1da177e4 344try_again:
627d2d6b 345 peeking = off = sk_peek_offset(sk, flags);
a59322be 346 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
3f518bf7 347 &peeked, &off, &err);
1da177e4 348 if (!skb)
627d2d6b 349 return err;
1da177e4 350
e6afc8ac 351 ulen = skb->len;
59c2cdae 352 copied = len;
627d2d6b 353 if (copied > ulen - off)
354 copied = ulen - off;
59c2cdae 355 else if (copied < ulen)
1ab1457c 356 msg->msg_flags |= MSG_TRUNC;
1da177e4 357
f26ba175
WY
358 is_udp4 = (skb->protocol == htons(ETH_P_IP));
359
ba4e58ec 360 /*
759e5d00
HX
361 * If checksum is needed at all, try to do it while copying the
362 * data. If the data is truncated, or if we only want a partial
363 * coverage checksum (UDP-Lite), do it before the copy.
ba4e58ec 364 */
ba4e58ec 365
627d2d6b 366 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov || peeking) {
197c949e
ED
367 checksum_valid = !udp_lib_checksum_complete(skb);
368 if (!checksum_valid)
1da177e4 369 goto csum_copy_err;
ba4e58ec
GR
370 }
371
197c949e 372 if (checksum_valid || skb_csum_unnecessary(skb))
627d2d6b 373 err = skb_copy_datagram_msg(skb, off, msg, copied);
ba4e58ec 374 else {
627d2d6b 375 err = skb_copy_and_csum_datagram_msg(skb, off, msg);
1da177e4
LT
376 if (err == -EINVAL)
377 goto csum_copy_err;
378 }
22911fc5 379 if (unlikely(err)) {
979402b1
ED
380 if (!peeked) {
381 atomic_inc(&sk->sk_drops);
382 if (is_udp4)
6aef70a8
ED
383 UDP_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
384 is_udplite);
979402b1 385 else
6aef70a8
ED
386 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS,
387 is_udplite);
979402b1 388 }
850cbadd 389 kfree_skb(skb);
627d2d6b 390 return err;
22911fc5 391 }
f26ba175
WY
392 if (!peeked) {
393 if (is_udp4)
6aef70a8
ED
394 UDP_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
395 is_udplite);
f26ba175 396 else
6aef70a8
ED
397 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INDATAGRAMS,
398 is_udplite);
f26ba175 399 }
cb75994e 400
3b885787 401 sock_recv_ts_and_drops(msg, sk, skb);
1da177e4
LT
402
403 /* Copy the address. */
404 if (msg->msg_name) {
342dfc30 405 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
1da177e4 406 sin6->sin6_family = AF_INET6;
4bedb452 407 sin6->sin6_port = udp_hdr(skb)->source;
1da177e4 408 sin6->sin6_flowinfo = 0;
1da177e4 409
842df073 410 if (is_udp4) {
b301e82c
BH
411 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
412 &sin6->sin6_addr);
842df073
HFS
413 sin6->sin6_scope_id = 0;
414 } else {
4e3fd7a0 415 sin6->sin6_addr = ipv6_hdr(skb)->saddr;
842df073
HFS
416 sin6->sin6_scope_id =
417 ipv6_iface_scope_id(&sin6->sin6_addr,
4330487a 418 inet6_iif(skb));
1da177e4 419 }
bceaa902 420 *addr_len = sizeof(*sin6);
1da177e4 421 }
4b261c75
HFS
422
423 if (np->rxopt.all)
424 ip6_datagram_recv_common_ctl(sk, msg, skb);
425
f26ba175 426 if (is_udp4) {
1da177e4 427 if (inet->cmsg_flags)
ad959036 428 ip_cmsg_recv_offset(msg, sk, skb,
10df8e61 429 sizeof(struct udphdr), off);
1da177e4
LT
430 } else {
431 if (np->rxopt.all)
4b261c75 432 ip6_datagram_recv_specific_ctl(sk, msg, skb);
1ab1457c 433 }
1da177e4 434
59c2cdae 435 err = copied;
1da177e4 436 if (flags & MSG_TRUNC)
759e5d00 437 err = ulen;
1da177e4 438
850cbadd 439 skb_consume_udp(sk, skb, peeking ? -err : err);
1da177e4
LT
440 return err;
441
442csum_copy_err:
850cbadd 443 if (!__sk_queue_drop_skb(sk, skb, flags)) {
6a5dc9e5 444 if (is_udp4) {
6aef70a8
ED
445 UDP_INC_STATS(sock_net(sk),
446 UDP_MIB_CSUMERRORS, is_udplite);
447 UDP_INC_STATS(sock_net(sk),
448 UDP_MIB_INERRORS, is_udplite);
6a5dc9e5 449 } else {
6aef70a8
ED
450 UDP6_INC_STATS(sock_net(sk),
451 UDP_MIB_CSUMERRORS, is_udplite);
452 UDP6_INC_STATS(sock_net(sk),
453 UDP_MIB_INERRORS, is_udplite);
6a5dc9e5 454 }
0856f939 455 }
850cbadd 456 kfree_skb(skb);
1da177e4 457
beb39db5
ED
458 /* starting over for a new packet, but check if we need to yield */
459 cond_resched();
9cfaa8de 460 msg->msg_flags &= ~MSG_TRUNC;
1da177e4
LT
461 goto try_again;
462}
463
ba4e58ec 464void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
d5fdd6ba 465 u8 type, u8 code, int offset, __be32 info,
645ca708 466 struct udp_table *udptable)
1da177e4
LT
467{
468 struct ipv6_pinfo *np;
b71d1d42
ED
469 const struct ipv6hdr *hdr = (const struct ipv6hdr *)skb->data;
470 const struct in6_addr *saddr = &hdr->saddr;
471 const struct in6_addr *daddr = &hdr->daddr;
67ba4152 472 struct udphdr *uh = (struct udphdr *)(skb->data+offset);
1da177e4 473 struct sock *sk;
e0d8c1b7 474 int harderr;
1da177e4 475 int err;
7304fe46 476 struct net *net = dev_net(skb->dev);
1da177e4 477
e32ea7e7 478 sk = __udp6_lib_lookup(net, daddr, uh->dest, saddr, uh->source,
538950a1 479 inet6_iif(skb), udptable, skb);
63159f29 480 if (!sk) {
a16292a0
ED
481 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev),
482 ICMP6_MIB_INERRORS);
1da177e4 483 return;
7304fe46 484 }
1da177e4 485
e0d8c1b7
WW
486 harderr = icmpv6_err_convert(type, code, &err);
487 np = inet6_sk(sk);
488
93b36cf3
HFS
489 if (type == ICMPV6_PKT_TOOBIG) {
490 if (!ip6_sk_accept_pmtu(sk))
491 goto out;
81aded24 492 ip6_sk_update_pmtu(skb, sk, info);
e0d8c1b7
WW
493 if (np->pmtudisc != IPV6_PMTUDISC_DONT)
494 harderr = 1;
93b36cf3 495 }
1a462d18 496 if (type == NDISC_REDIRECT) {
ec18d9a2 497 ip6_sk_redirect(skb, sk);
1a462d18
DJ
498 goto out;
499 }
81aded24 500
e0d8c1b7
WW
501 if (!np->recverr) {
502 if (!harderr || sk->sk_state != TCP_ESTABLISHED)
503 goto out;
504 } else {
1da177e4 505 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
e0d8c1b7 506 }
b1faf566 507
1da177e4
LT
508 sk->sk_err = err;
509 sk->sk_error_report(sk);
510out:
ca065d0c 511 return;
1da177e4
LT
512}
513
f7ad74fe
BL
514static int __udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
515{
516 int rc;
517
efe4208f 518 if (!ipv6_addr_any(&sk->sk_v6_daddr)) {
f7ad74fe 519 sock_rps_save_rxhash(sk, skb);
005ec974 520 sk_mark_napi_id(sk, skb);
2c8c56e1 521 sk_incoming_cpu_update(sk);
005ec974 522 }
f7ad74fe 523
850cbadd 524 rc = __udp_enqueue_schedule_skb(sk, skb);
f7ad74fe
BL
525 if (rc < 0) {
526 int is_udplite = IS_UDPLITE(sk);
527
528 /* Note that an ENOMEM error is charged twice */
529 if (rc == -ENOMEM)
e61da9e2 530 UDP6_INC_STATS(sock_net(sk),
02c22347 531 UDP_MIB_RCVBUFERRORS, is_udplite);
e61da9e2 532 UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
f7ad74fe
BL
533 kfree_skb(skb);
534 return -1;
535 }
850cbadd 536
f7ad74fe
BL
537 return 0;
538}
539
ba4e58ec 540static __inline__ void udpv6_err(struct sk_buff *skb,
d5fdd6ba 541 struct inet6_skb_parm *opt, u8 type,
67ba4152 542 u8 code, int offset, __be32 info)
ba4e58ec 543{
645ca708 544 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
ba4e58ec
GR
545}
546
d7f3f621
BL
547static struct static_key udpv6_encap_needed __read_mostly;
548void udpv6_encap_enable(void)
549{
550 if (!static_key_enabled(&udpv6_encap_needed))
551 static_key_slow_inc(&udpv6_encap_needed);
552}
553EXPORT_SYMBOL(udpv6_encap_enable);
554
f7ad74fe 555int udpv6_queue_rcv_skb(struct sock *sk, struct sk_buff *skb)
1da177e4 556{
ba4e58ec 557 struct udp_sock *up = udp_sk(sk);
b2bf1e26 558 int is_udplite = IS_UDPLITE(sk);
a18135eb 559
ba4e58ec
GR
560 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
561 goto drop;
1da177e4 562
d7f3f621
BL
563 if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
564 int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
565
566 /*
567 * This is an encapsulation socket so pass the skb to
568 * the socket's udp_encap_rcv() hook. Otherwise, just
569 * fall through and pass this up the UDP socket.
570 * up->encap_rcv() returns the following value:
571 * =0 if skb was successfully passed to the encap
572 * handler or was discarded by it.
573 * >0 if skb should be passed on to UDP.
574 * <0 if skb should be resubmitted as proto -N
575 */
576
577 /* if we're overly short, let UDP handle it */
578 encap_rcv = ACCESS_ONCE(up->encap_rcv);
e5aed006 579 if (encap_rcv) {
d7f3f621
BL
580 int ret;
581
0a80966b
TH
582 /* Verify checksum before giving to encap */
583 if (udp_lib_checksum_complete(skb))
584 goto csum_error;
585
d7f3f621
BL
586 ret = encap_rcv(sk, skb);
587 if (ret <= 0) {
02c22347
ED
588 __UDP_INC_STATS(sock_net(sk),
589 UDP_MIB_INDATAGRAMS,
590 is_udplite);
d7f3f621
BL
591 return -ret;
592 }
593 }
594
595 /* FALLTHROUGH -- it's a UDP Packet */
596 }
597
ba4e58ec
GR
598 /*
599 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
600 */
b2bf1e26 601 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
ba4e58ec
GR
602
603 if (up->pcrlen == 0) { /* full coverage was set */
ba7a46f1
JP
604 net_dbg_ratelimited("UDPLITE6: partial coverage %d while full coverage %d requested\n",
605 UDP_SKB_CB(skb)->cscov, skb->len);
ba4e58ec
GR
606 goto drop;
607 }
608 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
ba7a46f1
JP
609 net_dbg_ratelimited("UDPLITE6: coverage %d too small, need min %d\n",
610 UDP_SKB_CB(skb)->cscov, up->pcrlen);
ba4e58ec
GR
611 goto drop;
612 }
1da177e4
LT
613 }
614
ce25d66a
ED
615 if (rcu_access_pointer(sk->sk_filter) &&
616 udp_lib_checksum_complete(skb))
617 goto csum_error;
618
ba66bbe5 619 if (sk_filter_trim_cap(sk, skb, sizeof(struct udphdr)))
a6127697 620 goto drop;
ba4e58ec 621
e6afc8ac 622 udp_csum_pull_header(skb);
cb80ef46 623
d826eb14 624 skb_dst_drop(skb);
cb75994e 625
850cbadd 626 return __udpv6_queue_rcv_skb(sk, skb);
3e215c8d 627
6a5dc9e5 628csum_error:
02c22347 629 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_CSUMERRORS, is_udplite);
ba4e58ec 630drop:
02c22347 631 __UDP6_INC_STATS(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
cb80ef46 632 atomic_inc(&sk->sk_drops);
ba4e58ec
GR
633 kfree_skb(skb);
634 return -1;
1da177e4
LT
635}
636
5cf3d461
DH
637static bool __udp_v6_is_mcast_sock(struct net *net, struct sock *sk,
638 __be16 loc_port, const struct in6_addr *loc_addr,
639 __be16 rmt_port, const struct in6_addr *rmt_addr,
640 int dif, unsigned short hnum)
1da177e4 641{
5cf3d461 642 struct inet_sock *inet = inet_sk(sk);
1da177e4 643
5cf3d461
DH
644 if (!net_eq(sock_net(sk), net))
645 return false;
646
647 if (udp_sk(sk)->udp_port_hash != hnum ||
648 sk->sk_family != PF_INET6 ||
649 (inet->inet_dport && inet->inet_dport != rmt_port) ||
650 (!ipv6_addr_any(&sk->sk_v6_daddr) &&
651 !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr)) ||
33b4b015
HR
652 (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif) ||
653 (!ipv6_addr_any(&sk->sk_v6_rcv_saddr) &&
654 !ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr)))
5cf3d461
DH
655 return false;
656 if (!inet6_mc_check(sk, loc_addr, rmt_addr))
657 return false;
658 return true;
1da177e4
LT
659}
660
4068579e
TH
661static void udp6_csum_zero_error(struct sk_buff *skb)
662{
663 /* RFC 2460 section 8.1 says that we SHOULD log
664 * this error. Well, it is reasonable.
665 */
ba7a46f1
JP
666 net_dbg_ratelimited("IPv6: udp checksum is 0 for [%pI6c]:%u->[%pI6c]:%u\n",
667 &ipv6_hdr(skb)->saddr, ntohs(udp_hdr(skb)->source),
668 &ipv6_hdr(skb)->daddr, ntohs(udp_hdr(skb)->dest));
4068579e
TH
669}
670
1da177e4
LT
671/*
672 * Note: called only from the BH handler context,
673 * so we don't need to lock the hashes.
674 */
e3163493 675static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
b71d1d42 676 const struct in6_addr *saddr, const struct in6_addr *daddr,
36cbb245 677 struct udp_table *udptable, int proto)
1da177e4 678{
ca065d0c 679 struct sock *sk, *first = NULL;
4bedb452 680 const struct udphdr *uh = udp_hdr(skb);
5cf3d461
DH
681 unsigned short hnum = ntohs(uh->dest);
682 struct udp_hslot *hslot = udp_hashslot(udptable, net, hnum);
ca065d0c 683 unsigned int offset = offsetof(typeof(*sk), sk_node);
2dc41cff 684 unsigned int hash2 = 0, hash2_any = 0, use_hash2 = (hslot->count > 10);
ca065d0c
ED
685 int dif = inet6_iif(skb);
686 struct hlist_node *node;
687 struct sk_buff *nskb;
2dc41cff
DH
688
689 if (use_hash2) {
690 hash2_any = udp6_portaddr_hash(net, &in6addr_any, hnum) &
691 udp_table.mask;
692 hash2 = udp6_portaddr_hash(net, daddr, hnum) & udp_table.mask;
693start_lookup:
694 hslot = &udp_table.hash2[hash2];
695 offset = offsetof(typeof(*sk), __sk_common.skc_portaddr_node);
696 }
1da177e4 697
ca065d0c
ED
698 sk_for_each_entry_offset_rcu(sk, node, &hslot->head, offset) {
699 if (!__udp_v6_is_mcast_sock(net, sk, uh->dest, daddr,
700 uh->source, saddr, dif, hnum))
701 continue;
702 /* If zero checksum and no_check is not on for
703 * the socket then skip it.
704 */
705 if (!uh->check && !udp_sk(sk)->no_check6_rx)
706 continue;
707 if (!first) {
708 first = sk;
709 continue;
710 }
711 nskb = skb_clone(skb, GFP_ATOMIC);
712 if (unlikely(!nskb)) {
713 atomic_inc(&sk->sk_drops);
02c22347
ED
714 __UDP6_INC_STATS(net, UDP_MIB_RCVBUFERRORS,
715 IS_UDPLITE(sk));
716 __UDP6_INC_STATS(net, UDP_MIB_INERRORS,
717 IS_UDPLITE(sk));
ca065d0c 718 continue;
95766fff 719 }
a1ab77f9 720
ca065d0c
ED
721 if (udpv6_queue_rcv_skb(sk, nskb) > 0)
722 consume_skb(nskb);
723 }
a1ab77f9 724
2dc41cff
DH
725 /* Also lookup *:port if we are using hash2 and haven't done so yet. */
726 if (use_hash2 && hash2 != hash2_any) {
727 hash2 = hash2_any;
728 goto start_lookup;
729 }
730
ca065d0c
ED
731 if (first) {
732 if (udpv6_queue_rcv_skb(first, skb) > 0)
733 consume_skb(skb);
a1ab77f9 734 } else {
ca065d0c 735 kfree_skb(skb);
02c22347
ED
736 __UDP6_INC_STATS(net, UDP_MIB_IGNOREDMULTI,
737 proto == IPPROTO_UDPLITE);
a1ab77f9 738 }
ba4e58ec 739 return 0;
1da177e4
LT
740}
741
645ca708 742int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
759e5d00 743 int proto)
1da177e4 744{
ca065d0c 745 const struct in6_addr *saddr, *daddr;
3ffe533c 746 struct net *net = dev_net(skb->dev);
1ab1457c 747 struct udphdr *uh;
ca065d0c 748 struct sock *sk;
1da177e4
LT
749 u32 ulen = 0;
750
751 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
d6bc0149 752 goto discard;
1da177e4 753
0660e03f
ACM
754 saddr = &ipv6_hdr(skb)->saddr;
755 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 756 uh = udp_hdr(skb);
1da177e4
LT
757
758 ulen = ntohs(uh->len);
ba4e58ec
GR
759 if (ulen > skb->len)
760 goto short_packet;
1da177e4 761
759e5d00
HX
762 if (proto == IPPROTO_UDP) {
763 /* UDP validates ulen. */
1da177e4 764
ba4e58ec
GR
765 /* Check for jumbo payload */
766 if (ulen == 0)
767 ulen = skb->len;
1da177e4 768
ba4e58ec
GR
769 if (ulen < sizeof(*uh))
770 goto short_packet;
1da177e4 771
ba4e58ec
GR
772 if (ulen < skb->len) {
773 if (pskb_trim_rcsum(skb, ulen))
774 goto short_packet;
0660e03f
ACM
775 saddr = &ipv6_hdr(skb)->saddr;
776 daddr = &ipv6_hdr(skb)->daddr;
4bedb452 777 uh = udp_hdr(skb);
ba4e58ec 778 }
ba4e58ec 779 }
1da177e4 780
759e5d00 781 if (udp6_csum_init(skb, uh, proto))
6a5dc9e5 782 goto csum_error;
759e5d00 783
1ab1457c
YH
784 /*
785 * Multicast receive code
1da177e4 786 */
ba4e58ec 787 if (ipv6_addr_is_multicast(daddr))
e3163493 788 return __udp6_lib_mcast_deliver(net, skb,
36cbb245 789 saddr, daddr, udptable, proto);
1da177e4
LT
790
791 /* Unicast */
792
1ab1457c 793 /*
1da177e4
LT
794 * check socket cache ... must talk to Alan about his plans
795 * for sock caches... i'll skip this for now.
796 */
607c4aaf 797 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
53b24b8f 798 if (sk) {
a5b50476
ET
799 int ret;
800
1c19448c 801 if (!uh->check && !udp_sk(sk)->no_check6_rx) {
4068579e
TH
802 udp6_csum_zero_error(skb);
803 goto csum_error;
804 }
805
224d019c 806 if (inet_get_convert_csum(sk) && uh->check && !IS_UDPLITE(sk))
2abb7cdc
TH
807 skb_checksum_try_convert(skb, IPPROTO_UDP, uh->check,
808 ip6_compute_pseudo);
809
a5b50476 810 ret = udpv6_queue_rcv_skb(sk, skb);
1da177e4 811
59dca1d8 812 /* a return value > 0 means to resubmit the input */
cb80ef46 813 if (ret > 0)
59dca1d8 814 return ret;
1da177e4 815
add459aa 816 return 0;
1da177e4 817 }
1ab1457c 818
4068579e
TH
819 if (!uh->check) {
820 udp6_csum_zero_error(skb);
821 goto csum_error;
822 }
823
cb80ef46 824 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
c377411f 825 goto discard;
cb80ef46
BL
826
827 if (udp_lib_checksum_complete(skb))
6a5dc9e5 828 goto csum_error;
cb80ef46 829
02c22347 830 __UDP6_INC_STATS(net, UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
cb80ef46
BL
831 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0);
832
833 kfree_skb(skb);
add459aa 834 return 0;
1da177e4 835
1ab1457c 836short_packet:
ba7a46f1
JP
837 net_dbg_ratelimited("UDP%sv6: short packet: From [%pI6c]:%u %d/%d to [%pI6c]:%u\n",
838 proto == IPPROTO_UDPLITE ? "-Lite" : "",
839 saddr, ntohs(uh->source),
840 ulen, skb->len,
841 daddr, ntohs(uh->dest));
6a5dc9e5
ED
842 goto discard;
843csum_error:
02c22347 844 __UDP6_INC_STATS(net, UDP_MIB_CSUMERRORS, proto == IPPROTO_UDPLITE);
1da177e4 845discard:
02c22347 846 __UDP6_INC_STATS(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
1da177e4 847 kfree_skb(skb);
add459aa 848 return 0;
1da177e4 849}
ba4e58ec 850
e5bbef20 851static __inline__ int udpv6_rcv(struct sk_buff *skb)
ba4e58ec 852{
645ca708 853 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
ba4e58ec
GR
854}
855
1da177e4
LT
856/*
857 * Throw away all pending data and cancel the corking. Socket is locked.
858 */
859static void udp_v6_flush_pending_frames(struct sock *sk)
860{
861 struct udp_sock *up = udp_sk(sk);
862
36d926b9
DL
863 if (up->pending == AF_INET)
864 udp_flush_pending_frames(sk);
865 else if (up->pending) {
1da177e4
LT
866 up->len = 0;
867 up->pending = 0;
868 ip6_flush_pending_frames(sk);
1ab1457c 869 }
1da177e4
LT
870}
871
493c6be3 872/**
67ba4152
IM
873 * udp6_hwcsum_outgoing - handle outgoing HW checksumming
874 * @sk: socket we are sending on
875 * @skb: sk_buff containing the filled-in UDP header
876 * (checksum field must be zeroed out)
493c6be3
SS
877 */
878static void udp6_hwcsum_outgoing(struct sock *sk, struct sk_buff *skb,
879 const struct in6_addr *saddr,
880 const struct in6_addr *daddr, int len)
881{
882 unsigned int offset;
883 struct udphdr *uh = udp_hdr(skb);
d39d938c 884 struct sk_buff *frags = skb_shinfo(skb)->frag_list;
493c6be3
SS
885 __wsum csum = 0;
886
d39d938c 887 if (!frags) {
493c6be3
SS
888 /* Only one fragment on the socket. */
889 skb->csum_start = skb_transport_header(skb) - skb->head;
890 skb->csum_offset = offsetof(struct udphdr, check);
891 uh->check = ~csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP, 0);
892 } else {
893 /*
894 * HW-checksum won't work as there are two or more
895 * fragments on the socket so that all csums of sk_buffs
896 * should be together
897 */
898 offset = skb_transport_offset(skb);
899 skb->csum = skb_checksum(skb, offset, skb->len - offset, 0);
900
901 skb->ip_summed = CHECKSUM_NONE;
902
d39d938c
VY
903 do {
904 csum = csum_add(csum, frags->csum);
905 } while ((frags = frags->next));
493c6be3
SS
906
907 uh->check = csum_ipv6_magic(saddr, daddr, len, IPPROTO_UDP,
908 csum);
909 if (uh->check == 0)
910 uh->check = CSUM_MANGLED_0;
911 }
912}
913
1da177e4
LT
914/*
915 * Sending
916 */
917
d39d938c 918static int udp_v6_send_skb(struct sk_buff *skb, struct flowi6 *fl6)
1da177e4 919{
d39d938c 920 struct sock *sk = skb->sk;
1da177e4 921 struct udphdr *uh;
1da177e4 922 int err = 0;
b2bf1e26 923 int is_udplite = IS_UDPLITE(sk);
868c86bc 924 __wsum csum = 0;
d39d938c
VY
925 int offset = skb_transport_offset(skb);
926 int len = skb->len - offset;
1da177e4
LT
927
928 /*
929 * Create a UDP header
930 */
4bedb452 931 uh = udp_hdr(skb);
1958b856
DM
932 uh->source = fl6->fl6_sport;
933 uh->dest = fl6->fl6_dport;
d39d938c 934 uh->len = htons(len);
1da177e4
LT
935 uh->check = 0;
936
b2bf1e26 937 if (is_udplite)
d39d938c
VY
938 csum = udplite_csum(skb);
939 else if (udp_sk(sk)->no_check6_tx) { /* UDP csum disabled */
4068579e
TH
940 skb->ip_summed = CHECKSUM_NONE;
941 goto send;
942 } else if (skb->ip_summed == CHECKSUM_PARTIAL) { /* UDP hardware csum */
d39d938c 943 udp6_hwcsum_outgoing(sk, skb, &fl6->saddr, &fl6->daddr, len);
493c6be3
SS
944 goto send;
945 } else
d39d938c 946 csum = udp_csum(skb);
1da177e4 947
ba4e58ec 948 /* add protocol-dependent pseudo-header */
4c9483b2 949 uh->check = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
d39d938c 950 len, fl6->flowi6_proto, csum);
1da177e4 951 if (uh->check == 0)
f6ab0288 952 uh->check = CSUM_MANGLED_0;
1da177e4 953
493c6be3 954send:
d39d938c 955 err = ip6_send_skb(skb);
6ce9e7b5
ED
956 if (err) {
957 if (err == -ENOBUFS && !inet6_sk(sk)->recverr) {
6aef70a8
ED
958 UDP6_INC_STATS(sock_net(sk),
959 UDP_MIB_SNDBUFERRORS, is_udplite);
6ce9e7b5
ED
960 err = 0;
961 }
6aef70a8
ED
962 } else {
963 UDP6_INC_STATS(sock_net(sk),
964 UDP_MIB_OUTDATAGRAMS, is_udplite);
965 }
d39d938c
VY
966 return err;
967}
968
969static int udp_v6_push_pending_frames(struct sock *sk)
970{
971 struct sk_buff *skb;
972 struct udp_sock *up = udp_sk(sk);
973 struct flowi6 fl6;
974 int err = 0;
975
976 if (up->pending == AF_INET)
977 return udp_push_pending_frames(sk);
978
979 /* ip6_finish_skb will release the cork, so make a copy of
980 * fl6 here.
981 */
982 fl6 = inet_sk(sk)->cork.fl.u.ip6;
983
984 skb = ip6_finish_skb(sk);
985 if (!skb)
986 goto out;
987
988 err = udp_v6_send_skb(skb, &fl6);
989
1da177e4
LT
990out:
991 up->len = 0;
992 up->pending = 0;
993 return err;
994}
995
1b784140 996int udpv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
1da177e4
LT
997{
998 struct ipv6_txoptions opt_space;
999 struct udp_sock *up = udp_sk(sk);
1000 struct inet_sock *inet = inet_sk(sk);
1001 struct ipv6_pinfo *np = inet6_sk(sk);
342dfc30 1002 DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
20c59de2 1003 struct in6_addr *daddr, *final_p, final;
1da177e4 1004 struct ipv6_txoptions *opt = NULL;
45f6fad8 1005 struct ipv6_txoptions *opt_to_free = NULL;
1da177e4 1006 struct ip6_flowlabel *flowlabel = NULL;
4c9483b2 1007 struct flowi6 fl6;
1da177e4 1008 struct dst_entry *dst;
26879da5 1009 struct ipcm6_cookie ipc6;
1da177e4
LT
1010 int addr_len = msg->msg_namelen;
1011 int ulen = len;
1da177e4
LT
1012 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
1013 int err;
987905de 1014 int connected = 0;
b2bf1e26 1015 int is_udplite = IS_UDPLITE(sk);
ba4e58ec 1016 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
ad1e46a8 1017 struct sockcm_cookie sockc;
1da177e4 1018
26879da5
WW
1019 ipc6.hlimit = -1;
1020 ipc6.tclass = -1;
1021 ipc6.dontfrag = -1;
1022
1da177e4
LT
1023 /* destination address check */
1024 if (sin6) {
1025 if (addr_len < offsetof(struct sockaddr, sa_data))
1026 return -EINVAL;
1027
1028 switch (sin6->sin6_family) {
1029 case AF_INET6:
1030 if (addr_len < SIN6_LEN_RFC2133)
1031 return -EINVAL;
1032 daddr = &sin6->sin6_addr;
1033 break;
1034 case AF_INET:
1035 goto do_udp_sendmsg;
1036 case AF_UNSPEC:
1037 msg->msg_name = sin6 = NULL;
1038 msg->msg_namelen = addr_len = 0;
1039 daddr = NULL;
1040 break;
1041 default:
1042 return -EINVAL;
1043 }
1044 } else if (!up->pending) {
1045 if (sk->sk_state != TCP_ESTABLISHED)
1046 return -EDESTADDRREQ;
efe4208f 1047 daddr = &sk->sk_v6_daddr;
1ab1457c 1048 } else
1da177e4
LT
1049 daddr = NULL;
1050
1051 if (daddr) {
e773e4fa 1052 if (ipv6_addr_v4mapped(daddr)) {
1da177e4
LT
1053 struct sockaddr_in sin;
1054 sin.sin_family = AF_INET;
c720c7e8 1055 sin.sin_port = sin6 ? sin6->sin6_port : inet->inet_dport;
1da177e4
LT
1056 sin.sin_addr.s_addr = daddr->s6_addr32[3];
1057 msg->msg_name = &sin;
1058 msg->msg_namelen = sizeof(sin);
1059do_udp_sendmsg:
1060 if (__ipv6_only_sock(sk))
1061 return -ENETUNREACH;
1b784140 1062 return udp_sendmsg(sk, msg, len);
1da177e4
LT
1063 }
1064 }
1065
1066 if (up->pending == AF_INET)
1b784140 1067 return udp_sendmsg(sk, msg, len);
1da177e4
LT
1068
1069 /* Rough check on arithmetic overflow,
b59e139b 1070 better check is made in ip6_append_data().
1da177e4
LT
1071 */
1072 if (len > INT_MAX - sizeof(struct udphdr))
1073 return -EMSGSIZE;
1ab1457c 1074
03485f2a 1075 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
1da177e4
LT
1076 if (up->pending) {
1077 /*
1078 * There are pending frames.
1079 * The socket lock must be held while it's corked.
1080 */
1081 lock_sock(sk);
1082 if (likely(up->pending)) {
1083 if (unlikely(up->pending != AF_INET6)) {
1084 release_sock(sk);
1085 return -EAFNOSUPPORT;
1086 }
1087 dst = NULL;
1088 goto do_append_data;
1089 }
1090 release_sock(sk);
1091 }
1092 ulen += sizeof(struct udphdr);
1093
4c9483b2 1094 memset(&fl6, 0, sizeof(fl6));
1da177e4
LT
1095
1096 if (sin6) {
1097 if (sin6->sin6_port == 0)
1098 return -EINVAL;
1099
1958b856 1100 fl6.fl6_dport = sin6->sin6_port;
1da177e4
LT
1101 daddr = &sin6->sin6_addr;
1102
1103 if (np->sndflow) {
4c9483b2
DM
1104 fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
1105 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
1106 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
63159f29 1107 if (!flowlabel)
1da177e4 1108 return -EINVAL;
1da177e4
LT
1109 }
1110 }
1111
1112 /*
1113 * Otherwise it will be difficult to maintain
1114 * sk->sk_dst_cache.
1115 */
1116 if (sk->sk_state == TCP_ESTABLISHED &&
efe4208f
ED
1117 ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
1118 daddr = &sk->sk_v6_daddr;
1da177e4
LT
1119
1120 if (addr_len >= sizeof(struct sockaddr_in6) &&
1121 sin6->sin6_scope_id &&
842df073 1122 __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
4c9483b2 1123 fl6.flowi6_oif = sin6->sin6_scope_id;
1da177e4
LT
1124 } else {
1125 if (sk->sk_state != TCP_ESTABLISHED)
1126 return -EDESTADDRREQ;
1127
1958b856 1128 fl6.fl6_dport = inet->inet_dport;
efe4208f 1129 daddr = &sk->sk_v6_daddr;
4c9483b2 1130 fl6.flowlabel = np->flow_label;
987905de 1131 connected = 1;
1da177e4
LT
1132 }
1133
4c9483b2
DM
1134 if (!fl6.flowi6_oif)
1135 fl6.flowi6_oif = sk->sk_bound_dev_if;
1da177e4 1136
4c9483b2
DM
1137 if (!fl6.flowi6_oif)
1138 fl6.flowi6_oif = np->sticky_pktinfo.ipi6_ifindex;
9f690db7 1139
4c9483b2 1140 fl6.flowi6_mark = sk->sk_mark;
e2d118a1 1141 fl6.flowi6_uid = sk->sk_uid;
c14ac945 1142 sockc.tsflags = sk->sk_tsflags;
51953d5b 1143
1da177e4
LT
1144 if (msg->msg_controllen) {
1145 opt = &opt_space;
1146 memset(opt, 0, sizeof(struct ipv6_txoptions));
1147 opt->tot_len = sizeof(*opt);
26879da5 1148 ipc6.opt = opt;
1da177e4 1149
26879da5 1150 err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
1da177e4
LT
1151 if (err < 0) {
1152 fl6_sock_release(flowlabel);
1153 return err;
1154 }
4c9483b2
DM
1155 if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
1156 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
63159f29 1157 if (!flowlabel)
1da177e4
LT
1158 return -EINVAL;
1159 }
1160 if (!(opt->opt_nflen|opt->opt_flen))
1161 opt = NULL;
987905de 1162 connected = 0;
1da177e4 1163 }
45f6fad8
ED
1164 if (!opt) {
1165 opt = txopt_get(np);
1166 opt_to_free = opt;
1167 }
df9890c3
YH
1168 if (flowlabel)
1169 opt = fl6_merge_options(&opt_space, flowlabel, opt);
1170 opt = ipv6_fixup_options(&opt_space, opt);
26879da5 1171 ipc6.opt = opt;
1da177e4 1172
4c9483b2 1173 fl6.flowi6_proto = sk->sk_protocol;
876c7f41 1174 if (!ipv6_addr_any(daddr))
4e3fd7a0 1175 fl6.daddr = *daddr;
876c7f41 1176 else
4c9483b2
DM
1177 fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
1178 if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
4e3fd7a0 1179 fl6.saddr = np->saddr;
1958b856 1180 fl6.fl6_sport = inet->inet_sport;
1ab1457c 1181
4c9483b2 1182 final_p = fl6_update_dst(&fl6, opt, &final);
20c59de2 1183 if (final_p)
987905de 1184 connected = 0;
1da177e4 1185
4c9483b2
DM
1186 if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr)) {
1187 fl6.flowi6_oif = np->mcast_oif;
987905de 1188 connected = 0;
c4062dfc
EH
1189 } else if (!fl6.flowi6_oif)
1190 fl6.flowi6_oif = np->ucast_oif;
1da177e4 1191
4c9483b2 1192 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
beb8d13b 1193
38b7097b
HFS
1194 if (ipc6.tclass < 0)
1195 ipc6.tclass = np->tclass;
1196
1197 fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
1198
0e0d44ab 1199 dst = ip6_sk_dst_lookup_flow(sk, &fl6, final_p);
68d0c6d3
DM
1200 if (IS_ERR(dst)) {
1201 err = PTR_ERR(dst);
1202 dst = NULL;
1da177e4 1203 goto out;
14e50e57 1204 }
1da177e4 1205
26879da5
WW
1206 if (ipc6.hlimit < 0)
1207 ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
1da177e4
LT
1208
1209 if (msg->msg_flags&MSG_CONFIRM)
1210 goto do_confirm;
1211back_from_confirm:
1212
03485f2a
VY
1213 /* Lockless fast path for the non-corking case */
1214 if (!corkreq) {
1215 struct sk_buff *skb;
1216
1217 skb = ip6_make_skb(sk, getfrag, msg, ulen,
26879da5 1218 sizeof(struct udphdr), &ipc6,
03485f2a 1219 &fl6, (struct rt6_info *)dst,
26879da5 1220 msg->msg_flags, &sockc);
03485f2a
VY
1221 err = PTR_ERR(skb);
1222 if (!IS_ERR_OR_NULL(skb))
1223 err = udp_v6_send_skb(skb, &fl6);
1224 goto release_dst;
1225 }
1226
1da177e4
LT
1227 lock_sock(sk);
1228 if (unlikely(up->pending)) {
1229 /* The socket is already corked while preparing it. */
1230 /* ... which is an evident application bug. --ANK */
1231 release_sock(sk);
1232
ba7a46f1 1233 net_dbg_ratelimited("udp cork app bug 2\n");
1da177e4
LT
1234 err = -EINVAL;
1235 goto out;
1236 }
1237
1238 up->pending = AF_INET6;
1239
1240do_append_data:
26879da5
WW
1241 if (ipc6.dontfrag < 0)
1242 ipc6.dontfrag = np->dontfrag;
1da177e4 1243 up->len += ulen;
26879da5
WW
1244 err = ip6_append_data(sk, getfrag, msg, ulen, sizeof(struct udphdr),
1245 &ipc6, &fl6, (struct rt6_info *)dst,
1246 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags, &sockc);
1da177e4
LT
1247 if (err)
1248 udp_v6_flush_pending_frames(sk);
1249 else if (!corkreq)
4c0a6cb0 1250 err = udp_v6_push_pending_frames(sk);
1e0c14f4
HX
1251 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
1252 up->pending = 0;
1da177e4 1253
03485f2a
VY
1254 if (err > 0)
1255 err = np->recverr ? net_xmit_errno(err) : 0;
1256 release_sock(sk);
1257
1258release_dst:
a5e7c210
DM
1259 if (dst) {
1260 if (connected) {
1261 ip6_dst_store(sk, dst,
efe4208f
ED
1262 ipv6_addr_equal(&fl6.daddr, &sk->sk_v6_daddr) ?
1263 &sk->sk_v6_daddr : NULL,
8e1ef0a9 1264#ifdef CONFIG_IPV6_SUBTREES
4c9483b2 1265 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
8e1ef0a9
YH
1266 &np->saddr :
1267#endif
1268 NULL);
a5e7c210
DM
1269 } else {
1270 dst_release(dst);
1271 }
a3c96089 1272 dst = NULL;
a5e7c210
DM
1273 }
1274
1da177e4 1275out:
a3c96089 1276 dst_release(dst);
1da177e4 1277 fl6_sock_release(flowlabel);
45f6fad8 1278 txopt_put(opt_to_free);
cd562c98 1279 if (!err)
1da177e4 1280 return len;
a18135eb
DM
1281 /*
1282 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
1283 * ENOBUFS might not be good (it's not tunable per se), but otherwise
1284 * we don't have a good statistic (IpOutDiscards but it can be too many
1285 * things). We could add another new stat but at least for now that
1286 * seems like overkill.
1287 */
1288 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
6aef70a8
ED
1289 UDP6_INC_STATS(sock_net(sk),
1290 UDP_MIB_SNDBUFERRORS, is_udplite);
a18135eb 1291 }
1da177e4
LT
1292 return err;
1293
1294do_confirm:
1295 dst_confirm(dst);
1296 if (!(msg->msg_flags&MSG_PROBE) || len)
1297 goto back_from_confirm;
1298 err = 0;
1299 goto out;
1300}
1301
7d06b2e0 1302void udpv6_destroy_sock(struct sock *sk)
1da177e4 1303{
44046a59 1304 struct udp_sock *up = udp_sk(sk);
1da177e4
LT
1305 lock_sock(sk);
1306 udp_v6_flush_pending_frames(sk);
1307 release_sock(sk);
1308
44046a59
TP
1309 if (static_key_false(&udpv6_encap_needed) && up->encap_type) {
1310 void (*encap_destroy)(struct sock *sk);
1311 encap_destroy = ACCESS_ONCE(up->encap_destroy);
1312 if (encap_destroy)
1313 encap_destroy(sk);
1314 }
1315
1da177e4 1316 inet6_destroy_sock(sk);
1da177e4
LT
1317}
1318
1319/*
1320 * Socket option code for UDP
1321 */
ba4e58ec 1322int udpv6_setsockopt(struct sock *sk, int level, int optname,
b7058842 1323 char __user *optval, unsigned int optlen)
3fdadf7d 1324{
db8dac20 1325 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0
GR
1326 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1327 udp_v6_push_pending_frames);
ba4e58ec 1328 return ipv6_setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1329}
1330
1331#ifdef CONFIG_COMPAT
ba4e58ec 1332int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
b7058842 1333 char __user *optval, unsigned int optlen)
3fdadf7d 1334{
db8dac20 1335 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0
GR
1336 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1337 udp_v6_push_pending_frames);
ba4e58ec 1338 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1339}
1340#endif
1341
ba4e58ec
GR
1342int udpv6_getsockopt(struct sock *sk, int level, int optname,
1343 char __user *optval, int __user *optlen)
3fdadf7d 1344{
db8dac20 1345 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0 1346 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
ba4e58ec 1347 return ipv6_getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1348}
1349
1350#ifdef CONFIG_COMPAT
ba4e58ec
GR
1351int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1352 char __user *optval, int __user *optlen)
3fdadf7d 1353{
db8dac20 1354 if (level == SOL_UDP || level == SOL_UDPLITE)
4c0a6cb0 1355 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
ba4e58ec 1356 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
3fdadf7d
DM
1357}
1358#endif
1359
41135cc8 1360static const struct inet6_protocol udpv6_protocol = {
1da177e4
LT
1361 .handler = udpv6_rcv,
1362 .err_handler = udpv6_err,
1363 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1364};
1365
1366/* ------------------------------------------------------------------------ */
1367#ifdef CONFIG_PROC_FS
ba4e58ec 1368int udp6_seq_show(struct seq_file *seq, void *v)
1da177e4 1369{
17ef66af
LC
1370 if (v == SEQ_START_TOKEN) {
1371 seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
1372 } else {
1373 int bucket = ((struct udp_iter_state *)seq->private)->bucket;
1374 struct inet_sock *inet = inet_sk(v);
1375 __u16 srcp = ntohs(inet->inet_sport);
1376 __u16 destp = ntohs(inet->inet_dport);
1377 ip6_dgram_sock_seq_show(seq, v, srcp, destp, bucket);
1378 }
1da177e4
LT
1379 return 0;
1380}
1381
73cb88ec
AV
1382static const struct file_operations udp6_afinfo_seq_fops = {
1383 .owner = THIS_MODULE,
1384 .open = udp_seq_open,
1385 .read = seq_read,
1386 .llseek = seq_lseek,
1387 .release = seq_release_net
1388};
1389
1da177e4 1390static struct udp_seq_afinfo udp6_seq_afinfo = {
1da177e4
LT
1391 .name = "udp6",
1392 .family = AF_INET6,
645ca708 1393 .udp_table = &udp_table,
73cb88ec 1394 .seq_fops = &udp6_afinfo_seq_fops,
dda61925
DL
1395 .seq_ops = {
1396 .show = udp6_seq_show,
1397 },
1da177e4
LT
1398};
1399
2c8c1e72 1400int __net_init udp6_proc_init(struct net *net)
1da177e4 1401{
0c96d8c5 1402 return udp_proc_register(net, &udp6_seq_afinfo);
1da177e4
LT
1403}
1404
ec120da6
IM
1405void udp6_proc_exit(struct net *net)
1406{
0c96d8c5 1407 udp_proc_unregister(net, &udp6_seq_afinfo);
1da177e4
LT
1408}
1409#endif /* CONFIG_PROC_FS */
1410
1411/* ------------------------------------------------------------------------ */
1412
1413struct proto udpv6_prot = {
543d9cfe
ACM
1414 .name = "UDPv6",
1415 .owner = THIS_MODULE,
ba4e58ec 1416 .close = udp_lib_close,
543d9cfe
ACM
1417 .connect = ip6_datagram_connect,
1418 .disconnect = udp_disconnect,
1419 .ioctl = udp_ioctl,
850cbadd 1420 .init = udp_init_sock,
543d9cfe
ACM
1421 .destroy = udpv6_destroy_sock,
1422 .setsockopt = udpv6_setsockopt,
1423 .getsockopt = udpv6_getsockopt,
1424 .sendmsg = udpv6_sendmsg,
1425 .recvmsg = udpv6_recvmsg,
e646b657 1426 .release_cb = ip6_datagram_release_cb,
ba4e58ec
GR
1427 .hash = udp_lib_hash,
1428 .unhash = udp_lib_unhash,
719f8358 1429 .rehash = udp_v6_rehash,
543d9cfe 1430 .get_port = udp_v6_get_port,
95766fff
HA
1431 .memory_allocated = &udp_memory_allocated,
1432 .sysctl_mem = sysctl_udp_mem,
1433 .sysctl_wmem = &sysctl_udp_wmem_min,
1434 .sysctl_rmem = &sysctl_udp_rmem_min,
543d9cfe 1435 .obj_size = sizeof(struct udp6_sock),
645ca708 1436 .h.udp_table = &udp_table,
3fdadf7d 1437#ifdef CONFIG_COMPAT
543d9cfe
ACM
1438 .compat_setsockopt = compat_udpv6_setsockopt,
1439 .compat_getsockopt = compat_udpv6_getsockopt,
3fdadf7d 1440#endif
5d77dca8 1441 .diag_destroy = udp_abort,
1da177e4
LT
1442};
1443
1da177e4
LT
1444static struct inet_protosw udpv6_protosw = {
1445 .type = SOCK_DGRAM,
1446 .protocol = IPPROTO_UDP,
1447 .prot = &udpv6_prot,
1448 .ops = &inet6_dgram_ops,
1da177e4
LT
1449 .flags = INET_PROTOSW_PERMANENT,
1450};
1451
7f4e4868 1452int __init udpv6_init(void)
1da177e4 1453{
7f4e4868
DL
1454 int ret;
1455
3336288a
VY
1456 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1457 if (ret)
c6b641a4 1458 goto out;
3336288a 1459
7f4e4868
DL
1460 ret = inet6_register_protosw(&udpv6_protosw);
1461 if (ret)
1462 goto out_udpv6_protocol;
1463out:
1464 return ret;
1465
1466out_udpv6_protocol:
1467 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1468 goto out;
1469}
1470
09f7709f 1471void udpv6_exit(void)
7f4e4868
DL
1472{
1473 inet6_unregister_protosw(&udpv6_protosw);
1474 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1da177e4 1475}