xref: /openbmc/linux/net/ipv6/udp.c (revision 732a675a)
1 /*
2  *	UDP over IPv6
3  *	Linux INET6 implementation
4  *
5  *	Authors:
6  *	Pedro Roque		<roque@di.fc.ul.pt>
7  *
8  *	Based on linux/ipv4/udp.c
9  *
10  *	$Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $
11  *
12  *	Fixes:
13  *	Hideaki YOSHIFUJI	:	sin6_scope_id support
14  *	YOSHIFUJI Hideaki @USAGI and:	Support IPV6_V6ONLY socket option, which
15  *	Alexey Kuznetsov		allow both IPv4 and IPv6 sockets to bind
16  *					a single port at the same time.
17  *      Kazunori MIYAZAWA @USAGI:       change process style to use ip6_append_data
18  *      YOSHIFUJI Hideaki @USAGI:	convert /proc/net/udp6 to seq_file.
19  *
20  *	This program is free software; you can redistribute it and/or
21  *      modify it under the terms of the GNU General Public License
22  *      as published by the Free Software Foundation; either version
23  *      2 of the License, or (at your option) any later version.
24  */
25 
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/socket.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/in6.h>
32 #include <linux/netdevice.h>
33 #include <linux/if_arp.h>
34 #include <linux/ipv6.h>
35 #include <linux/icmpv6.h>
36 #include <linux/init.h>
37 #include <linux/module.h>
38 #include <linux/skbuff.h>
39 #include <asm/uaccess.h>
40 
41 #include <net/ndisc.h>
42 #include <net/protocol.h>
43 #include <net/transp_v6.h>
44 #include <net/ip6_route.h>
45 #include <net/raw.h>
46 #include <net/tcp_states.h>
47 #include <net/ip6_checksum.h>
48 #include <net/xfrm.h>
49 
50 #include <linux/proc_fs.h>
51 #include <linux/seq_file.h>
52 #include "udp_impl.h"
53 
54 int udp_v6_get_port(struct sock *sk, unsigned short snum)
55 {
56 	return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
57 }
58 
59 static struct sock *__udp6_lib_lookup(struct net *net,
60 				      struct in6_addr *saddr, __be16 sport,
61 				      struct in6_addr *daddr, __be16 dport,
62 				      int dif, struct hlist_head udptable[])
63 {
64 	struct sock *sk, *result = NULL;
65 	struct hlist_node *node;
66 	unsigned short hnum = ntohs(dport);
67 	int badness = -1;
68 
69 	read_lock(&udp_hash_lock);
70 	sk_for_each(sk, node, &udptable[hnum & (UDP_HTABLE_SIZE - 1)]) {
71 		struct inet_sock *inet = inet_sk(sk);
72 
73 		if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
74 				sk->sk_family == PF_INET6) {
75 			struct ipv6_pinfo *np = inet6_sk(sk);
76 			int score = 0;
77 			if (inet->dport) {
78 				if (inet->dport != sport)
79 					continue;
80 				score++;
81 			}
82 			if (!ipv6_addr_any(&np->rcv_saddr)) {
83 				if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
84 					continue;
85 				score++;
86 			}
87 			if (!ipv6_addr_any(&np->daddr)) {
88 				if (!ipv6_addr_equal(&np->daddr, saddr))
89 					continue;
90 				score++;
91 			}
92 			if (sk->sk_bound_dev_if) {
93 				if (sk->sk_bound_dev_if != dif)
94 					continue;
95 				score++;
96 			}
97 			if (score == 4) {
98 				result = sk;
99 				break;
100 			} else if (score > badness) {
101 				result = sk;
102 				badness = score;
103 			}
104 		}
105 	}
106 	if (result)
107 		sock_hold(result);
108 	read_unlock(&udp_hash_lock);
109 	return result;
110 }
111 
112 /*
113  * 	This should be easy, if there is something there we
114  * 	return it, otherwise we block.
115  */
116 
117 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
118 		  struct msghdr *msg, size_t len,
119 		  int noblock, int flags, int *addr_len)
120 {
121 	struct ipv6_pinfo *np = inet6_sk(sk);
122 	struct inet_sock *inet = inet_sk(sk);
123 	struct sk_buff *skb;
124 	unsigned int ulen, copied;
125 	int peeked;
126 	int err;
127 	int is_udplite = IS_UDPLITE(sk);
128 
129 	if (addr_len)
130 		*addr_len=sizeof(struct sockaddr_in6);
131 
132 	if (flags & MSG_ERRQUEUE)
133 		return ipv6_recv_error(sk, msg, len);
134 
135 try_again:
136 	skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
137 				  &peeked, &err);
138 	if (!skb)
139 		goto out;
140 
141 	ulen = skb->len - sizeof(struct udphdr);
142 	copied = len;
143 	if (copied > ulen)
144 		copied = ulen;
145 	else if (copied < ulen)
146 		msg->msg_flags |= MSG_TRUNC;
147 
148 	/*
149 	 * If checksum is needed at all, try to do it while copying the
150 	 * data.  If the data is truncated, or if we only want a partial
151 	 * coverage checksum (UDP-Lite), do it before the copy.
152 	 */
153 
154 	if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
155 		if (udp_lib_checksum_complete(skb))
156 			goto csum_copy_err;
157 	}
158 
159 	if (skb_csum_unnecessary(skb))
160 		err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
161 					      msg->msg_iov, copied       );
162 	else {
163 		err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
164 		if (err == -EINVAL)
165 			goto csum_copy_err;
166 	}
167 	if (err)
168 		goto out_free;
169 
170 	if (!peeked)
171 		UDP6_INC_STATS_USER(UDP_MIB_INDATAGRAMS, is_udplite);
172 
173 	sock_recv_timestamp(msg, sk, skb);
174 
175 	/* Copy the address. */
176 	if (msg->msg_name) {
177 		struct sockaddr_in6 *sin6;
178 
179 		sin6 = (struct sockaddr_in6 *) msg->msg_name;
180 		sin6->sin6_family = AF_INET6;
181 		sin6->sin6_port = udp_hdr(skb)->source;
182 		sin6->sin6_flowinfo = 0;
183 		sin6->sin6_scope_id = 0;
184 
185 		if (skb->protocol == htons(ETH_P_IP))
186 			ipv6_addr_set(&sin6->sin6_addr, 0, 0,
187 				      htonl(0xffff), ip_hdr(skb)->saddr);
188 		else {
189 			ipv6_addr_copy(&sin6->sin6_addr,
190 				       &ipv6_hdr(skb)->saddr);
191 			if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
192 				sin6->sin6_scope_id = IP6CB(skb)->iif;
193 		}
194 
195 	}
196 	if (skb->protocol == htons(ETH_P_IP)) {
197 		if (inet->cmsg_flags)
198 			ip_cmsg_recv(msg, skb);
199 	} else {
200 		if (np->rxopt.all)
201 			datagram_recv_ctl(sk, msg, skb);
202 	}
203 
204 	err = copied;
205 	if (flags & MSG_TRUNC)
206 		err = ulen;
207 
208 out_free:
209 	lock_sock(sk);
210 	skb_free_datagram(sk, skb);
211 	release_sock(sk);
212 out:
213 	return err;
214 
215 csum_copy_err:
216 	lock_sock(sk);
217 	if (!skb_kill_datagram(sk, skb, flags))
218 		UDP6_INC_STATS_USER(UDP_MIB_INERRORS, is_udplite);
219 	release_sock(sk);
220 
221 	if (flags & MSG_DONTWAIT)
222 		return -EAGAIN;
223 	goto try_again;
224 }
225 
226 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
227 		    int type, int code, int offset, __be32 info,
228 		    struct hlist_head udptable[]                    )
229 {
230 	struct ipv6_pinfo *np;
231 	struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
232 	struct in6_addr *saddr = &hdr->saddr;
233 	struct in6_addr *daddr = &hdr->daddr;
234 	struct udphdr *uh = (struct udphdr*)(skb->data+offset);
235 	struct sock *sk;
236 	int err;
237 
238 	sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
239 			       saddr, uh->source, inet6_iif(skb), udptable);
240 	if (sk == NULL)
241 		return;
242 
243 	np = inet6_sk(sk);
244 
245 	if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
246 		goto out;
247 
248 	if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
249 		goto out;
250 
251 	if (np->recverr)
252 		ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
253 
254 	sk->sk_err = err;
255 	sk->sk_error_report(sk);
256 out:
257 	sock_put(sk);
258 }
259 
260 static __inline__ void udpv6_err(struct sk_buff *skb,
261 				 struct inet6_skb_parm *opt, int type,
262 				 int code, int offset, __be32 info     )
263 {
264 	__udp6_lib_err(skb, opt, type, code, offset, info, udp_hash);
265 }
266 
267 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
268 {
269 	struct udp_sock *up = udp_sk(sk);
270 	int rc;
271 	int is_udplite = IS_UDPLITE(sk);
272 
273 	if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
274 		goto drop;
275 
276 	/*
277 	 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
278 	 */
279 	if ((is_udplite & UDPLITE_RECV_CC)  &&  UDP_SKB_CB(skb)->partial_cov) {
280 
281 		if (up->pcrlen == 0) {          /* full coverage was set  */
282 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
283 				" %d while full coverage %d requested\n",
284 				UDP_SKB_CB(skb)->cscov, skb->len);
285 			goto drop;
286 		}
287 		if (UDP_SKB_CB(skb)->cscov  <  up->pcrlen) {
288 			LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
289 						    "too small, need min %d\n",
290 				       UDP_SKB_CB(skb)->cscov, up->pcrlen);
291 			goto drop;
292 		}
293 	}
294 
295 	if (sk->sk_filter) {
296 		if (udp_lib_checksum_complete(skb))
297 			goto drop;
298 	}
299 
300 	if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
301 		/* Note that an ENOMEM error is charged twice */
302 		if (rc == -ENOMEM)
303 			UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS, is_udplite);
304 		goto drop;
305 	}
306 
307 	return 0;
308 drop:
309 	UDP6_INC_STATS_BH(UDP_MIB_INERRORS, is_udplite);
310 	kfree_skb(skb);
311 	return -1;
312 }
313 
314 static struct sock *udp_v6_mcast_next(struct sock *sk,
315 				      __be16 loc_port, struct in6_addr *loc_addr,
316 				      __be16 rmt_port, struct in6_addr *rmt_addr,
317 				      int dif)
318 {
319 	struct hlist_node *node;
320 	struct sock *s = sk;
321 	unsigned short num = ntohs(loc_port);
322 
323 	sk_for_each_from(s, node) {
324 		struct inet_sock *inet = inet_sk(s);
325 
326 		if (sock_net(s) != sock_net(sk))
327 			continue;
328 
329 		if (s->sk_hash == num && s->sk_family == PF_INET6) {
330 			struct ipv6_pinfo *np = inet6_sk(s);
331 			if (inet->dport) {
332 				if (inet->dport != rmt_port)
333 					continue;
334 			}
335 			if (!ipv6_addr_any(&np->daddr) &&
336 			    !ipv6_addr_equal(&np->daddr, rmt_addr))
337 				continue;
338 
339 			if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
340 				continue;
341 
342 			if (!ipv6_addr_any(&np->rcv_saddr)) {
343 				if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
344 					continue;
345 			}
346 			if (!inet6_mc_check(s, loc_addr, rmt_addr))
347 				continue;
348 			return s;
349 		}
350 	}
351 	return NULL;
352 }
353 
354 /*
355  * Note: called only from the BH handler context,
356  * so we don't need to lock the hashes.
357  */
358 static int __udp6_lib_mcast_deliver(struct sk_buff *skb, struct in6_addr *saddr,
359 			   struct in6_addr *daddr, struct hlist_head udptable[])
360 {
361 	struct sock *sk, *sk2;
362 	const struct udphdr *uh = udp_hdr(skb);
363 	int dif;
364 
365 	read_lock(&udp_hash_lock);
366 	sk = sk_head(&udptable[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
367 	dif = inet6_iif(skb);
368 	sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
369 	if (!sk) {
370 		kfree_skb(skb);
371 		goto out;
372 	}
373 
374 	sk2 = sk;
375 	while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
376 					uh->source, saddr, dif))) {
377 		struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
378 		if (buff) {
379 			bh_lock_sock_nested(sk2);
380 			if (!sock_owned_by_user(sk2))
381 				udpv6_queue_rcv_skb(sk2, buff);
382 			else
383 				sk_add_backlog(sk2, buff);
384 			bh_unlock_sock(sk2);
385 		}
386 	}
387 	bh_lock_sock_nested(sk);
388 	if (!sock_owned_by_user(sk))
389 		udpv6_queue_rcv_skb(sk, skb);
390 	else
391 		sk_add_backlog(sk, skb);
392 	bh_unlock_sock(sk);
393 out:
394 	read_unlock(&udp_hash_lock);
395 	return 0;
396 }
397 
398 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
399 				 int proto)
400 {
401 	int err;
402 
403 	UDP_SKB_CB(skb)->partial_cov = 0;
404 	UDP_SKB_CB(skb)->cscov = skb->len;
405 
406 	if (proto == IPPROTO_UDPLITE) {
407 		err = udplite_checksum_init(skb, uh);
408 		if (err)
409 			return err;
410 	}
411 
412 	if (uh->check == 0) {
413 		/* RFC 2460 section 8.1 says that we SHOULD log
414 		   this error. Well, it is reasonable.
415 		 */
416 		LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
417 		return 1;
418 	}
419 	if (skb->ip_summed == CHECKSUM_COMPLETE &&
420 	    !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
421 			     skb->len, proto, skb->csum))
422 		skb->ip_summed = CHECKSUM_UNNECESSARY;
423 
424 	if (!skb_csum_unnecessary(skb))
425 		skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
426 							 &ipv6_hdr(skb)->daddr,
427 							 skb->len, proto, 0));
428 
429 	return 0;
430 }
431 
432 int __udp6_lib_rcv(struct sk_buff *skb, struct hlist_head udptable[],
433 		   int proto)
434 {
435 	struct sock *sk;
436 	struct udphdr *uh;
437 	struct net_device *dev = skb->dev;
438 	struct in6_addr *saddr, *daddr;
439 	u32 ulen = 0;
440 
441 	if (!pskb_may_pull(skb, sizeof(struct udphdr)))
442 		goto short_packet;
443 
444 	saddr = &ipv6_hdr(skb)->saddr;
445 	daddr = &ipv6_hdr(skb)->daddr;
446 	uh = udp_hdr(skb);
447 
448 	ulen = ntohs(uh->len);
449 	if (ulen > skb->len)
450 		goto short_packet;
451 
452 	if (proto == IPPROTO_UDP) {
453 		/* UDP validates ulen. */
454 
455 		/* Check for jumbo payload */
456 		if (ulen == 0)
457 			ulen = skb->len;
458 
459 		if (ulen < sizeof(*uh))
460 			goto short_packet;
461 
462 		if (ulen < skb->len) {
463 			if (pskb_trim_rcsum(skb, ulen))
464 				goto short_packet;
465 			saddr = &ipv6_hdr(skb)->saddr;
466 			daddr = &ipv6_hdr(skb)->daddr;
467 			uh = udp_hdr(skb);
468 		}
469 	}
470 
471 	if (udp6_csum_init(skb, uh, proto))
472 		goto discard;
473 
474 	/*
475 	 *	Multicast receive code
476 	 */
477 	if (ipv6_addr_is_multicast(daddr))
478 		return __udp6_lib_mcast_deliver(skb, saddr, daddr, udptable);
479 
480 	/* Unicast */
481 
482 	/*
483 	 * check socket cache ... must talk to Alan about his plans
484 	 * for sock caches... i'll skip this for now.
485 	 */
486 	sk = __udp6_lib_lookup(dev_net(skb->dev), saddr, uh->source,
487 			       daddr, uh->dest, inet6_iif(skb), udptable);
488 
489 	if (sk == NULL) {
490 		if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
491 			goto discard;
492 
493 		if (udp_lib_checksum_complete(skb))
494 			goto discard;
495 		UDP6_INC_STATS_BH(UDP_MIB_NOPORTS, proto == IPPROTO_UDPLITE);
496 
497 		icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
498 
499 		kfree_skb(skb);
500 		return 0;
501 	}
502 
503 	/* deliver */
504 
505 	bh_lock_sock_nested(sk);
506 	if (!sock_owned_by_user(sk))
507 		udpv6_queue_rcv_skb(sk, skb);
508 	else
509 		sk_add_backlog(sk, skb);
510 	bh_unlock_sock(sk);
511 	sock_put(sk);
512 	return 0;
513 
514 short_packet:
515 	LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
516 		       proto == IPPROTO_UDPLITE ? "-Lite" : "",
517 		       ulen, skb->len);
518 
519 discard:
520 	UDP6_INC_STATS_BH(UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
521 	kfree_skb(skb);
522 	return 0;
523 }
524 
525 static __inline__ int udpv6_rcv(struct sk_buff *skb)
526 {
527 	return __udp6_lib_rcv(skb, udp_hash, IPPROTO_UDP);
528 }
529 
530 /*
531  * Throw away all pending data and cancel the corking. Socket is locked.
532  */
533 static void udp_v6_flush_pending_frames(struct sock *sk)
534 {
535 	struct udp_sock *up = udp_sk(sk);
536 
537 	if (up->pending == AF_INET)
538 		udp_flush_pending_frames(sk);
539 	else if (up->pending) {
540 		up->len = 0;
541 		up->pending = 0;
542 		ip6_flush_pending_frames(sk);
543 	}
544 }
545 
546 /*
547  *	Sending
548  */
549 
550 static int udp_v6_push_pending_frames(struct sock *sk)
551 {
552 	struct sk_buff *skb;
553 	struct udphdr *uh;
554 	struct udp_sock  *up = udp_sk(sk);
555 	struct inet_sock *inet = inet_sk(sk);
556 	struct flowi *fl = &inet->cork.fl;
557 	int err = 0;
558 	int is_udplite = IS_UDPLITE(sk);
559 	__wsum csum = 0;
560 
561 	/* Grab the skbuff where UDP header space exists. */
562 	if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
563 		goto out;
564 
565 	/*
566 	 * Create a UDP header
567 	 */
568 	uh = udp_hdr(skb);
569 	uh->source = fl->fl_ip_sport;
570 	uh->dest = fl->fl_ip_dport;
571 	uh->len = htons(up->len);
572 	uh->check = 0;
573 
574 	if (is_udplite)
575 		csum = udplite_csum_outgoing(sk, skb);
576 	 else
577 		csum = udp_csum_outgoing(sk, skb);
578 
579 	/* add protocol-dependent pseudo-header */
580 	uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
581 				    up->len, fl->proto, csum   );
582 	if (uh->check == 0)
583 		uh->check = CSUM_MANGLED_0;
584 
585 	err = ip6_push_pending_frames(sk);
586 out:
587 	up->len = 0;
588 	up->pending = 0;
589 	if (!err)
590 		UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS, is_udplite);
591 	return err;
592 }
593 
594 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
595 		  struct msghdr *msg, size_t len)
596 {
597 	struct ipv6_txoptions opt_space;
598 	struct udp_sock *up = udp_sk(sk);
599 	struct inet_sock *inet = inet_sk(sk);
600 	struct ipv6_pinfo *np = inet6_sk(sk);
601 	struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
602 	struct in6_addr *daddr, *final_p = NULL, final;
603 	struct ipv6_txoptions *opt = NULL;
604 	struct ip6_flowlabel *flowlabel = NULL;
605 	struct flowi fl;
606 	struct dst_entry *dst;
607 	int addr_len = msg->msg_namelen;
608 	int ulen = len;
609 	int hlimit = -1;
610 	int tclass = -1;
611 	int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
612 	int err;
613 	int connected = 0;
614 	int is_udplite = IS_UDPLITE(sk);
615 	int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
616 
617 	/* destination address check */
618 	if (sin6) {
619 		if (addr_len < offsetof(struct sockaddr, sa_data))
620 			return -EINVAL;
621 
622 		switch (sin6->sin6_family) {
623 		case AF_INET6:
624 			if (addr_len < SIN6_LEN_RFC2133)
625 				return -EINVAL;
626 			daddr = &sin6->sin6_addr;
627 			break;
628 		case AF_INET:
629 			goto do_udp_sendmsg;
630 		case AF_UNSPEC:
631 			msg->msg_name = sin6 = NULL;
632 			msg->msg_namelen = addr_len = 0;
633 			daddr = NULL;
634 			break;
635 		default:
636 			return -EINVAL;
637 		}
638 	} else if (!up->pending) {
639 		if (sk->sk_state != TCP_ESTABLISHED)
640 			return -EDESTADDRREQ;
641 		daddr = &np->daddr;
642 	} else
643 		daddr = NULL;
644 
645 	if (daddr) {
646 		if (ipv6_addr_v4mapped(daddr)) {
647 			struct sockaddr_in sin;
648 			sin.sin_family = AF_INET;
649 			sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
650 			sin.sin_addr.s_addr = daddr->s6_addr32[3];
651 			msg->msg_name = &sin;
652 			msg->msg_namelen = sizeof(sin);
653 do_udp_sendmsg:
654 			if (__ipv6_only_sock(sk))
655 				return -ENETUNREACH;
656 			return udp_sendmsg(iocb, sk, msg, len);
657 		}
658 	}
659 
660 	if (up->pending == AF_INET)
661 		return udp_sendmsg(iocb, sk, msg, len);
662 
663 	/* Rough check on arithmetic overflow,
664 	   better check is made in ip6_append_data().
665 	   */
666 	if (len > INT_MAX - sizeof(struct udphdr))
667 		return -EMSGSIZE;
668 
669 	if (up->pending) {
670 		/*
671 		 * There are pending frames.
672 		 * The socket lock must be held while it's corked.
673 		 */
674 		lock_sock(sk);
675 		if (likely(up->pending)) {
676 			if (unlikely(up->pending != AF_INET6)) {
677 				release_sock(sk);
678 				return -EAFNOSUPPORT;
679 			}
680 			dst = NULL;
681 			goto do_append_data;
682 		}
683 		release_sock(sk);
684 	}
685 	ulen += sizeof(struct udphdr);
686 
687 	memset(&fl, 0, sizeof(fl));
688 
689 	if (sin6) {
690 		if (sin6->sin6_port == 0)
691 			return -EINVAL;
692 
693 		fl.fl_ip_dport = sin6->sin6_port;
694 		daddr = &sin6->sin6_addr;
695 
696 		if (np->sndflow) {
697 			fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
698 			if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
699 				flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
700 				if (flowlabel == NULL)
701 					return -EINVAL;
702 				daddr = &flowlabel->dst;
703 			}
704 		}
705 
706 		/*
707 		 * Otherwise it will be difficult to maintain
708 		 * sk->sk_dst_cache.
709 		 */
710 		if (sk->sk_state == TCP_ESTABLISHED &&
711 		    ipv6_addr_equal(daddr, &np->daddr))
712 			daddr = &np->daddr;
713 
714 		if (addr_len >= sizeof(struct sockaddr_in6) &&
715 		    sin6->sin6_scope_id &&
716 		    ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
717 			fl.oif = sin6->sin6_scope_id;
718 	} else {
719 		if (sk->sk_state != TCP_ESTABLISHED)
720 			return -EDESTADDRREQ;
721 
722 		fl.fl_ip_dport = inet->dport;
723 		daddr = &np->daddr;
724 		fl.fl6_flowlabel = np->flow_label;
725 		connected = 1;
726 	}
727 
728 	if (!fl.oif)
729 		fl.oif = sk->sk_bound_dev_if;
730 
731 	if (msg->msg_controllen) {
732 		opt = &opt_space;
733 		memset(opt, 0, sizeof(struct ipv6_txoptions));
734 		opt->tot_len = sizeof(*opt);
735 
736 		err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
737 		if (err < 0) {
738 			fl6_sock_release(flowlabel);
739 			return err;
740 		}
741 		if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
742 			flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
743 			if (flowlabel == NULL)
744 				return -EINVAL;
745 		}
746 		if (!(opt->opt_nflen|opt->opt_flen))
747 			opt = NULL;
748 		connected = 0;
749 	}
750 	if (opt == NULL)
751 		opt = np->opt;
752 	if (flowlabel)
753 		opt = fl6_merge_options(&opt_space, flowlabel, opt);
754 	opt = ipv6_fixup_options(&opt_space, opt);
755 
756 	fl.proto = sk->sk_protocol;
757 	if (!ipv6_addr_any(daddr))
758 		ipv6_addr_copy(&fl.fl6_dst, daddr);
759 	else
760 		fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
761 	if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
762 		ipv6_addr_copy(&fl.fl6_src, &np->saddr);
763 	fl.fl_ip_sport = inet->sport;
764 
765 	/* merge ip6_build_xmit from ip6_output */
766 	if (opt && opt->srcrt) {
767 		struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
768 		ipv6_addr_copy(&final, &fl.fl6_dst);
769 		ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
770 		final_p = &final;
771 		connected = 0;
772 	}
773 
774 	if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
775 		fl.oif = np->mcast_oif;
776 		connected = 0;
777 	}
778 
779 	security_sk_classify_flow(sk, &fl);
780 
781 	err = ip6_sk_dst_lookup(sk, &dst, &fl);
782 	if (err)
783 		goto out;
784 	if (final_p)
785 		ipv6_addr_copy(&fl.fl6_dst, final_p);
786 
787 	if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
788 		if (err == -EREMOTE)
789 			err = ip6_dst_blackhole(sk, &dst, &fl);
790 		if (err < 0)
791 			goto out;
792 	}
793 
794 	if (hlimit < 0) {
795 		if (ipv6_addr_is_multicast(&fl.fl6_dst))
796 			hlimit = np->mcast_hops;
797 		else
798 			hlimit = np->hop_limit;
799 		if (hlimit < 0)
800 			hlimit = ip6_dst_hoplimit(dst);
801 	}
802 
803 	if (tclass < 0) {
804 		tclass = np->tclass;
805 		if (tclass < 0)
806 			tclass = 0;
807 	}
808 
809 	if (msg->msg_flags&MSG_CONFIRM)
810 		goto do_confirm;
811 back_from_confirm:
812 
813 	lock_sock(sk);
814 	if (unlikely(up->pending)) {
815 		/* The socket is already corked while preparing it. */
816 		/* ... which is an evident application bug. --ANK */
817 		release_sock(sk);
818 
819 		LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
820 		err = -EINVAL;
821 		goto out;
822 	}
823 
824 	up->pending = AF_INET6;
825 
826 do_append_data:
827 	up->len += ulen;
828 	getfrag  =  is_udplite ?  udplite_getfrag : ip_generic_getfrag;
829 	err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
830 		sizeof(struct udphdr), hlimit, tclass, opt, &fl,
831 		(struct rt6_info*)dst,
832 		corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
833 	if (err)
834 		udp_v6_flush_pending_frames(sk);
835 	else if (!corkreq)
836 		err = udp_v6_push_pending_frames(sk);
837 	else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
838 		up->pending = 0;
839 
840 	if (dst) {
841 		if (connected) {
842 			ip6_dst_store(sk, dst,
843 				      ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
844 				      &np->daddr : NULL,
845 #ifdef CONFIG_IPV6_SUBTREES
846 				      ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
847 				      &np->saddr :
848 #endif
849 				      NULL);
850 		} else {
851 			dst_release(dst);
852 		}
853 		dst = NULL;
854 	}
855 
856 	if (err > 0)
857 		err = np->recverr ? net_xmit_errno(err) : 0;
858 	release_sock(sk);
859 out:
860 	dst_release(dst);
861 	fl6_sock_release(flowlabel);
862 	if (!err)
863 		return len;
864 	/*
865 	 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space.  Reporting
866 	 * ENOBUFS might not be good (it's not tunable per se), but otherwise
867 	 * we don't have a good statistic (IpOutDiscards but it can be too many
868 	 * things).  We could add another new stat but at least for now that
869 	 * seems like overkill.
870 	 */
871 	if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
872 		UDP6_INC_STATS_USER(UDP_MIB_SNDBUFERRORS, is_udplite);
873 	}
874 	return err;
875 
876 do_confirm:
877 	dst_confirm(dst);
878 	if (!(msg->msg_flags&MSG_PROBE) || len)
879 		goto back_from_confirm;
880 	err = 0;
881 	goto out;
882 }
883 
884 int udpv6_destroy_sock(struct sock *sk)
885 {
886 	lock_sock(sk);
887 	udp_v6_flush_pending_frames(sk);
888 	release_sock(sk);
889 
890 	inet6_destroy_sock(sk);
891 
892 	return 0;
893 }
894 
895 /*
896  *	Socket option code for UDP
897  */
898 int udpv6_setsockopt(struct sock *sk, int level, int optname,
899 		     char __user *optval, int optlen)
900 {
901 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
902 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
903 					  udp_v6_push_pending_frames);
904 	return ipv6_setsockopt(sk, level, optname, optval, optlen);
905 }
906 
907 #ifdef CONFIG_COMPAT
908 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
909 			    char __user *optval, int optlen)
910 {
911 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
912 		return udp_lib_setsockopt(sk, level, optname, optval, optlen,
913 					  udp_v6_push_pending_frames);
914 	return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
915 }
916 #endif
917 
918 int udpv6_getsockopt(struct sock *sk, int level, int optname,
919 		     char __user *optval, int __user *optlen)
920 {
921 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
922 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
923 	return ipv6_getsockopt(sk, level, optname, optval, optlen);
924 }
925 
926 #ifdef CONFIG_COMPAT
927 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
928 			    char __user *optval, int __user *optlen)
929 {
930 	if (level == SOL_UDP  ||  level == SOL_UDPLITE)
931 		return udp_lib_getsockopt(sk, level, optname, optval, optlen);
932 	return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
933 }
934 #endif
935 
936 static struct inet6_protocol udpv6_protocol = {
937 	.handler	=	udpv6_rcv,
938 	.err_handler	=	udpv6_err,
939 	.flags		=	INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
940 };
941 
942 /* ------------------------------------------------------------------------ */
943 #ifdef CONFIG_PROC_FS
944 
945 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
946 {
947 	struct inet_sock *inet = inet_sk(sp);
948 	struct ipv6_pinfo *np = inet6_sk(sp);
949 	struct in6_addr *dest, *src;
950 	__u16 destp, srcp;
951 
952 	dest  = &np->daddr;
953 	src   = &np->rcv_saddr;
954 	destp = ntohs(inet->dport);
955 	srcp  = ntohs(inet->sport);
956 	seq_printf(seq,
957 		   "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
958 		   "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
959 		   bucket,
960 		   src->s6_addr32[0], src->s6_addr32[1],
961 		   src->s6_addr32[2], src->s6_addr32[3], srcp,
962 		   dest->s6_addr32[0], dest->s6_addr32[1],
963 		   dest->s6_addr32[2], dest->s6_addr32[3], destp,
964 		   sp->sk_state,
965 		   atomic_read(&sp->sk_wmem_alloc),
966 		   atomic_read(&sp->sk_rmem_alloc),
967 		   0, 0L, 0,
968 		   sock_i_uid(sp), 0,
969 		   sock_i_ino(sp),
970 		   atomic_read(&sp->sk_refcnt), sp);
971 }
972 
973 int udp6_seq_show(struct seq_file *seq, void *v)
974 {
975 	if (v == SEQ_START_TOKEN)
976 		seq_printf(seq,
977 			   "  sl  "
978 			   "local_address                         "
979 			   "remote_address                        "
980 			   "st tx_queue rx_queue tr tm->when retrnsmt"
981 			   "   uid  timeout inode\n");
982 	else
983 		udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
984 	return 0;
985 }
986 
987 static struct udp_seq_afinfo udp6_seq_afinfo = {
988 	.name		= "udp6",
989 	.family		= AF_INET6,
990 	.hashtable	= udp_hash,
991 	.seq_fops	= {
992 		.owner	=	THIS_MODULE,
993 	},
994 	.seq_ops	= {
995 		.show		= udp6_seq_show,
996 	},
997 };
998 
999 int udp6_proc_init(struct net *net)
1000 {
1001 	return udp_proc_register(net, &udp6_seq_afinfo);
1002 }
1003 
1004 void udp6_proc_exit(struct net *net) {
1005 	udp_proc_unregister(net, &udp6_seq_afinfo);
1006 }
1007 #endif /* CONFIG_PROC_FS */
1008 
1009 /* ------------------------------------------------------------------------ */
1010 
1011 struct proto udpv6_prot = {
1012 	.name		   = "UDPv6",
1013 	.owner		   = THIS_MODULE,
1014 	.close		   = udp_lib_close,
1015 	.connect	   = ip6_datagram_connect,
1016 	.disconnect	   = udp_disconnect,
1017 	.ioctl		   = udp_ioctl,
1018 	.destroy	   = udpv6_destroy_sock,
1019 	.setsockopt	   = udpv6_setsockopt,
1020 	.getsockopt	   = udpv6_getsockopt,
1021 	.sendmsg	   = udpv6_sendmsg,
1022 	.recvmsg	   = udpv6_recvmsg,
1023 	.backlog_rcv	   = udpv6_queue_rcv_skb,
1024 	.hash		   = udp_lib_hash,
1025 	.unhash		   = udp_lib_unhash,
1026 	.get_port	   = udp_v6_get_port,
1027 	.memory_allocated  = &udp_memory_allocated,
1028 	.sysctl_mem	   = sysctl_udp_mem,
1029 	.sysctl_wmem	   = &sysctl_udp_wmem_min,
1030 	.sysctl_rmem	   = &sysctl_udp_rmem_min,
1031 	.obj_size	   = sizeof(struct udp6_sock),
1032 	.h.udp_hash	   = udp_hash,
1033 #ifdef CONFIG_COMPAT
1034 	.compat_setsockopt = compat_udpv6_setsockopt,
1035 	.compat_getsockopt = compat_udpv6_getsockopt,
1036 #endif
1037 };
1038 
1039 static struct inet_protosw udpv6_protosw = {
1040 	.type =      SOCK_DGRAM,
1041 	.protocol =  IPPROTO_UDP,
1042 	.prot =      &udpv6_prot,
1043 	.ops =       &inet6_dgram_ops,
1044 	.capability =-1,
1045 	.no_check =  UDP_CSUM_DEFAULT,
1046 	.flags =     INET_PROTOSW_PERMANENT,
1047 };
1048 
1049 
1050 int __init udpv6_init(void)
1051 {
1052 	int ret;
1053 
1054 	ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1055 	if (ret)
1056 		goto out;
1057 
1058 	ret = inet6_register_protosw(&udpv6_protosw);
1059 	if (ret)
1060 		goto out_udpv6_protocol;
1061 out:
1062 	return ret;
1063 
1064 out_udpv6_protocol:
1065 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1066 	goto out;
1067 }
1068 
1069 void udpv6_exit(void)
1070 {
1071 	inet6_unregister_protosw(&udpv6_protosw);
1072 	inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1073 }
1074