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