ia64/linux-2.6.18-xen.hg

view net/ipv6/udp.c @ 673:3161879fdf22

[IA64] xencomm: support XENMEM_add_to_physmap and XENMEM_remove_from_phsymap

support XENMEM_add_to_physmap and XENMEM_remove_from_phsymap.

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author Isaku Yamahata <yamahata@valinux.co.jp>
date Tue Sep 16 21:26:15 2008 +0900 (2008-09-16)
parents 3e8752eb6d9c
children fba34c7b1c97
line source
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 */
26 #include <linux/errno.h>
27 #include <linux/types.h>
28 #include <linux/socket.h>
29 #include <linux/sockios.h>
30 #include <linux/sched.h>
31 #include <linux/net.h>
32 #include <linux/in6.h>
33 #include <linux/netdevice.h>
34 #include <linux/if_arp.h>
35 #include <linux/ipv6.h>
36 #include <linux/icmpv6.h>
37 #include <linux/init.h>
38 #include <linux/skbuff.h>
39 #include <asm/uaccess.h>
41 #include <net/sock.h>
42 #include <net/snmp.h>
44 #include <net/ipv6.h>
45 #include <net/ndisc.h>
46 #include <net/protocol.h>
47 #include <net/transp_v6.h>
48 #include <net/ip6_route.h>
49 #include <net/addrconf.h>
50 #include <net/ip.h>
51 #include <net/udp.h>
52 #include <net/raw.h>
53 #include <net/inet_common.h>
54 #include <net/tcp_states.h>
56 #include <net/ip6_checksum.h>
57 #include <net/xfrm.h>
59 #include <linux/proc_fs.h>
60 #include <linux/seq_file.h>
62 DEFINE_SNMP_STAT(struct udp_mib, udp_stats_in6) __read_mostly;
64 /* Grrr, addr_type already calculated by caller, but I don't want
65 * to add some silly "cookie" argument to this method just for that.
66 */
67 static int udp_v6_get_port(struct sock *sk, unsigned short snum)
68 {
69 struct sock *sk2;
70 struct hlist_node *node;
72 write_lock_bh(&udp_hash_lock);
73 if (snum == 0) {
74 int best_size_so_far, best, result, i;
76 if (udp_port_rover > sysctl_local_port_range[1] ||
77 udp_port_rover < sysctl_local_port_range[0])
78 udp_port_rover = sysctl_local_port_range[0];
79 best_size_so_far = 32767;
80 best = result = udp_port_rover;
81 for (i = 0; i < UDP_HTABLE_SIZE; i++, result++) {
82 int size;
83 struct hlist_head *list;
85 list = &udp_hash[result & (UDP_HTABLE_SIZE - 1)];
86 if (hlist_empty(list)) {
87 if (result > sysctl_local_port_range[1])
88 result = sysctl_local_port_range[0] +
89 ((result - sysctl_local_port_range[0]) &
90 (UDP_HTABLE_SIZE - 1));
91 goto gotit;
92 }
93 size = 0;
94 sk_for_each(sk2, node, list)
95 if (++size >= best_size_so_far)
96 goto next;
97 best_size_so_far = size;
98 best = result;
99 next:;
100 }
101 result = best;
102 for(i = 0; i < (1 << 16) / UDP_HTABLE_SIZE; i++, result += UDP_HTABLE_SIZE) {
103 if (result > sysctl_local_port_range[1])
104 result = sysctl_local_port_range[0]
105 + ((result - sysctl_local_port_range[0]) &
106 (UDP_HTABLE_SIZE - 1));
107 if (!udp_lport_inuse(result))
108 break;
109 }
110 if (i >= (1 << 16) / UDP_HTABLE_SIZE)
111 goto fail;
112 gotit:
113 udp_port_rover = snum = result;
114 } else {
115 sk_for_each(sk2, node,
116 &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]) {
117 if (inet_sk(sk2)->num == snum &&
118 sk2 != sk &&
119 (!sk2->sk_bound_dev_if ||
120 !sk->sk_bound_dev_if ||
121 sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
122 (!sk2->sk_reuse || !sk->sk_reuse) &&
123 ipv6_rcv_saddr_equal(sk, sk2))
124 goto fail;
125 }
126 }
128 inet_sk(sk)->num = snum;
129 if (sk_unhashed(sk)) {
130 sk_add_node(sk, &udp_hash[snum & (UDP_HTABLE_SIZE - 1)]);
131 sock_prot_inc_use(sk->sk_prot);
132 }
133 write_unlock_bh(&udp_hash_lock);
134 return 0;
136 fail:
137 write_unlock_bh(&udp_hash_lock);
138 return 1;
139 }
141 static void udp_v6_hash(struct sock *sk)
142 {
143 BUG();
144 }
146 static void udp_v6_unhash(struct sock *sk)
147 {
148 write_lock_bh(&udp_hash_lock);
149 if (sk_del_node_init(sk)) {
150 inet_sk(sk)->num = 0;
151 sock_prot_dec_use(sk->sk_prot);
152 }
153 write_unlock_bh(&udp_hash_lock);
154 }
156 static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport,
157 struct in6_addr *daddr, u16 dport, int dif)
158 {
159 struct sock *sk, *result = NULL;
160 struct hlist_node *node;
161 unsigned short hnum = ntohs(dport);
162 int badness = -1;
164 read_lock(&udp_hash_lock);
165 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
166 struct inet_sock *inet = inet_sk(sk);
168 if (inet->num == hnum && sk->sk_family == PF_INET6) {
169 struct ipv6_pinfo *np = inet6_sk(sk);
170 int score = 0;
171 if (inet->dport) {
172 if (inet->dport != sport)
173 continue;
174 score++;
175 }
176 if (!ipv6_addr_any(&np->rcv_saddr)) {
177 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
178 continue;
179 score++;
180 }
181 if (!ipv6_addr_any(&np->daddr)) {
182 if (!ipv6_addr_equal(&np->daddr, saddr))
183 continue;
184 score++;
185 }
186 if (sk->sk_bound_dev_if) {
187 if (sk->sk_bound_dev_if != dif)
188 continue;
189 score++;
190 }
191 if(score == 4) {
192 result = sk;
193 break;
194 } else if(score > badness) {
195 result = sk;
196 badness = score;
197 }
198 }
199 }
200 if (result)
201 sock_hold(result);
202 read_unlock(&udp_hash_lock);
203 return result;
204 }
206 /*
207 *
208 */
210 static void udpv6_close(struct sock *sk, long timeout)
211 {
212 sk_common_release(sk);
213 }
215 /*
216 * This should be easy, if there is something there we
217 * return it, otherwise we block.
218 */
220 static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
221 struct msghdr *msg, size_t len,
222 int noblock, int flags, int *addr_len)
223 {
224 struct ipv6_pinfo *np = inet6_sk(sk);
225 struct inet_sock *inet = inet_sk(sk);
226 struct sk_buff *skb;
227 size_t copied;
228 int err;
230 if (addr_len)
231 *addr_len=sizeof(struct sockaddr_in6);
233 if (flags & MSG_ERRQUEUE)
234 return ipv6_recv_error(sk, msg, len);
236 try_again:
237 skb = skb_recv_datagram(sk, flags, noblock, &err);
238 if (!skb)
239 goto out;
241 copied = skb->len - sizeof(struct udphdr);
242 if (copied > len) {
243 copied = len;
244 msg->msg_flags |= MSG_TRUNC;
245 }
247 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
248 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
249 copied);
250 } else if (msg->msg_flags&MSG_TRUNC) {
251 if (__skb_checksum_complete(skb))
252 goto csum_copy_err;
253 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
254 copied);
255 } else {
256 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
257 if (err == -EINVAL)
258 goto csum_copy_err;
259 }
260 if (err)
261 goto out_free;
263 sock_recv_timestamp(msg, sk, skb);
265 /* Copy the address. */
266 if (msg->msg_name) {
267 struct sockaddr_in6 *sin6;
269 sin6 = (struct sockaddr_in6 *) msg->msg_name;
270 sin6->sin6_family = AF_INET6;
271 sin6->sin6_port = skb->h.uh->source;
272 sin6->sin6_flowinfo = 0;
273 sin6->sin6_scope_id = 0;
275 if (skb->protocol == htons(ETH_P_IP))
276 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
277 htonl(0xffff), skb->nh.iph->saddr);
278 else {
279 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
280 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
281 sin6->sin6_scope_id = IP6CB(skb)->iif;
282 }
284 }
285 if (skb->protocol == htons(ETH_P_IP)) {
286 if (inet->cmsg_flags)
287 ip_cmsg_recv(msg, skb);
288 } else {
289 if (np->rxopt.all)
290 datagram_recv_ctl(sk, msg, skb);
291 }
293 err = copied;
294 if (flags & MSG_TRUNC)
295 err = skb->len - sizeof(struct udphdr);
297 out_free:
298 skb_free_datagram(sk, skb);
299 out:
300 return err;
302 csum_copy_err:
303 skb_kill_datagram(sk, skb, flags);
305 if (flags & MSG_DONTWAIT) {
306 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
307 return -EAGAIN;
308 }
309 goto try_again;
310 }
312 static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
313 int type, int code, int offset, __u32 info)
314 {
315 struct ipv6_pinfo *np;
316 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
317 struct in6_addr *saddr = &hdr->saddr;
318 struct in6_addr *daddr = &hdr->daddr;
319 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
320 struct sock *sk;
321 int err;
323 sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, inet6_iif(skb));
325 if (sk == NULL)
326 return;
328 np = inet6_sk(sk);
330 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
331 goto out;
333 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
334 goto out;
336 if (np->recverr)
337 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
339 sk->sk_err = err;
340 sk->sk_error_report(sk);
341 out:
342 sock_put(sk);
343 }
345 static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
346 {
347 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
348 kfree_skb(skb);
349 return -1;
350 }
352 if (skb_checksum_complete(skb)) {
353 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
354 kfree_skb(skb);
355 return 0;
356 }
358 if (sock_queue_rcv_skb(sk,skb)<0) {
359 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
360 kfree_skb(skb);
361 return 0;
362 }
363 UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
364 return 0;
365 }
367 static struct sock *udp_v6_mcast_next(struct sock *sk,
368 u16 loc_port, struct in6_addr *loc_addr,
369 u16 rmt_port, struct in6_addr *rmt_addr,
370 int dif)
371 {
372 struct hlist_node *node;
373 struct sock *s = sk;
374 unsigned short num = ntohs(loc_port);
376 sk_for_each_from(s, node) {
377 struct inet_sock *inet = inet_sk(s);
379 if (inet->num == num && s->sk_family == PF_INET6) {
380 struct ipv6_pinfo *np = inet6_sk(s);
381 if (inet->dport) {
382 if (inet->dport != rmt_port)
383 continue;
384 }
385 if (!ipv6_addr_any(&np->daddr) &&
386 !ipv6_addr_equal(&np->daddr, rmt_addr))
387 continue;
389 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
390 continue;
392 if (!ipv6_addr_any(&np->rcv_saddr)) {
393 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
394 continue;
395 }
396 if(!inet6_mc_check(s, loc_addr, rmt_addr))
397 continue;
398 return s;
399 }
400 }
401 return NULL;
402 }
404 /*
405 * Note: called only from the BH handler context,
406 * so we don't need to lock the hashes.
407 */
408 static void udpv6_mcast_deliver(struct udphdr *uh,
409 struct in6_addr *saddr, struct in6_addr *daddr,
410 struct sk_buff *skb)
411 {
412 struct sock *sk, *sk2;
413 int dif;
415 read_lock(&udp_hash_lock);
416 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
417 dif = inet6_iif(skb);
418 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
419 if (!sk) {
420 kfree_skb(skb);
421 goto out;
422 }
424 sk2 = sk;
425 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
426 uh->source, saddr, dif))) {
427 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
428 if (buff)
429 udpv6_queue_rcv_skb(sk2, buff);
430 }
431 udpv6_queue_rcv_skb(sk, skb);
432 out:
433 read_unlock(&udp_hash_lock);
434 }
436 static int udpv6_rcv(struct sk_buff **pskb)
437 {
438 struct sk_buff *skb = *pskb;
439 struct sock *sk;
440 struct udphdr *uh;
441 struct net_device *dev = skb->dev;
442 struct in6_addr *saddr, *daddr;
443 u32 ulen = 0;
445 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
446 goto short_packet;
448 saddr = &skb->nh.ipv6h->saddr;
449 daddr = &skb->nh.ipv6h->daddr;
450 uh = skb->h.uh;
452 ulen = ntohs(uh->len);
454 /* Check for jumbo payload */
455 if (ulen == 0)
456 ulen = skb->len;
458 if (ulen > skb->len || ulen < sizeof(*uh))
459 goto short_packet;
461 if (uh->check == 0) {
462 /* RFC 2460 section 8.1 says that we SHOULD log
463 this error. Well, it is reasonable.
464 */
465 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
466 goto discard;
467 }
469 if (ulen < skb->len) {
470 if (pskb_trim_rcsum(skb, ulen))
471 goto discard;
472 saddr = &skb->nh.ipv6h->saddr;
473 daddr = &skb->nh.ipv6h->daddr;
474 uh = skb->h.uh;
475 }
477 if (skb->ip_summed == CHECKSUM_HW &&
478 !csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum))
479 skb->ip_summed = CHECKSUM_UNNECESSARY;
481 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
482 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
484 /*
485 * Multicast receive code
486 */
487 if (ipv6_addr_is_multicast(daddr)) {
488 udpv6_mcast_deliver(uh, saddr, daddr, skb);
489 return 0;
490 }
492 /* Unicast */
494 /*
495 * check socket cache ... must talk to Alan about his plans
496 * for sock caches... i'll skip this for now.
497 */
498 sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, inet6_iif(skb));
500 if (sk == NULL) {
501 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
502 goto discard;
504 if (skb_checksum_complete(skb))
505 goto discard;
506 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
508 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
510 kfree_skb(skb);
511 return(0);
512 }
514 /* deliver */
516 udpv6_queue_rcv_skb(sk, skb);
517 sock_put(sk);
518 return(0);
520 short_packet:
521 if (net_ratelimit())
522 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
524 discard:
525 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
526 kfree_skb(skb);
527 return(0);
528 }
529 /*
530 * Throw away all pending data and cancel the corking. Socket is locked.
531 */
532 static void udp_v6_flush_pending_frames(struct sock *sk)
533 {
534 struct udp_sock *up = udp_sk(sk);
536 if (up->pending) {
537 up->len = 0;
538 up->pending = 0;
539 ip6_flush_pending_frames(sk);
540 }
541 }
543 /*
544 * Sending
545 */
547 static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up)
548 {
549 struct sk_buff *skb;
550 struct udphdr *uh;
551 struct inet_sock *inet = inet_sk(sk);
552 struct flowi *fl = &inet->cork.fl;
553 int err = 0;
555 /* Grab the skbuff where UDP header space exists. */
556 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
557 goto out;
559 /*
560 * Create a UDP header
561 */
562 uh = skb->h.uh;
563 uh->source = fl->fl_ip_sport;
564 uh->dest = fl->fl_ip_dport;
565 uh->len = htons(up->len);
566 uh->check = 0;
568 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
569 skb->ip_summed = CHECKSUM_NONE;
570 goto send;
571 }
573 if (skb_queue_len(&sk->sk_write_queue) == 1) {
574 skb->csum = csum_partial((char *)uh,
575 sizeof(struct udphdr), skb->csum);
576 uh->check = csum_ipv6_magic(&fl->fl6_src,
577 &fl->fl6_dst,
578 up->len, fl->proto, skb->csum);
579 } else {
580 u32 tmp_csum = 0;
582 skb_queue_walk(&sk->sk_write_queue, skb) {
583 tmp_csum = csum_add(tmp_csum, skb->csum);
584 }
585 tmp_csum = csum_partial((char *)uh,
586 sizeof(struct udphdr), tmp_csum);
587 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
588 &fl->fl6_dst,
589 up->len, fl->proto, tmp_csum);
590 uh->check = tmp_csum;
592 }
593 if (uh->check == 0)
594 uh->check = -1;
596 send:
597 err = ip6_push_pending_frames(sk);
598 out:
599 up->len = 0;
600 up->pending = 0;
601 return err;
602 }
604 static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
605 struct msghdr *msg, size_t len)
606 {
607 struct ipv6_txoptions opt_space;
608 struct udp_sock *up = udp_sk(sk);
609 struct inet_sock *inet = inet_sk(sk);
610 struct ipv6_pinfo *np = inet6_sk(sk);
611 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
612 struct in6_addr *daddr, *final_p = NULL, final;
613 struct ipv6_txoptions *opt = NULL;
614 struct ip6_flowlabel *flowlabel = NULL;
615 struct flowi *fl = &inet->cork.fl;
616 struct dst_entry *dst;
617 int addr_len = msg->msg_namelen;
618 int ulen = len;
619 int hlimit = -1;
620 int tclass = -1;
621 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
622 int err;
623 int connected = 0;
625 /* destination address check */
626 if (sin6) {
627 if (addr_len < offsetof(struct sockaddr, sa_data))
628 return -EINVAL;
630 switch (sin6->sin6_family) {
631 case AF_INET6:
632 if (addr_len < SIN6_LEN_RFC2133)
633 return -EINVAL;
634 daddr = &sin6->sin6_addr;
635 break;
636 case AF_INET:
637 goto do_udp_sendmsg;
638 case AF_UNSPEC:
639 msg->msg_name = sin6 = NULL;
640 msg->msg_namelen = addr_len = 0;
641 daddr = NULL;
642 break;
643 default:
644 return -EINVAL;
645 }
646 } else if (!up->pending) {
647 if (sk->sk_state != TCP_ESTABLISHED)
648 return -EDESTADDRREQ;
649 daddr = &np->daddr;
650 } else
651 daddr = NULL;
653 if (daddr) {
654 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
655 struct sockaddr_in sin;
656 sin.sin_family = AF_INET;
657 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
658 sin.sin_addr.s_addr = daddr->s6_addr32[3];
659 msg->msg_name = &sin;
660 msg->msg_namelen = sizeof(sin);
661 do_udp_sendmsg:
662 if (__ipv6_only_sock(sk))
663 return -ENETUNREACH;
664 return udp_sendmsg(iocb, sk, msg, len);
665 }
666 }
668 if (up->pending == AF_INET)
669 return udp_sendmsg(iocb, sk, msg, len);
671 /* Rough check on arithmetic overflow,
672 better check is made in ip6_build_xmit
673 */
674 if (len > INT_MAX - sizeof(struct udphdr))
675 return -EMSGSIZE;
677 if (up->pending) {
678 /*
679 * There are pending frames.
680 * The socket lock must be held while it's corked.
681 */
682 lock_sock(sk);
683 if (likely(up->pending)) {
684 if (unlikely(up->pending != AF_INET6)) {
685 release_sock(sk);
686 return -EAFNOSUPPORT;
687 }
688 dst = NULL;
689 goto do_append_data;
690 }
691 release_sock(sk);
692 }
693 ulen += sizeof(struct udphdr);
695 memset(fl, 0, sizeof(*fl));
697 if (sin6) {
698 if (sin6->sin6_port == 0)
699 return -EINVAL;
701 fl->fl_ip_dport = sin6->sin6_port;
702 daddr = &sin6->sin6_addr;
704 if (np->sndflow) {
705 fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
706 if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
707 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
708 if (flowlabel == NULL)
709 return -EINVAL;
710 daddr = &flowlabel->dst;
711 }
712 }
714 /*
715 * Otherwise it will be difficult to maintain
716 * sk->sk_dst_cache.
717 */
718 if (sk->sk_state == TCP_ESTABLISHED &&
719 ipv6_addr_equal(daddr, &np->daddr))
720 daddr = &np->daddr;
722 if (addr_len >= sizeof(struct sockaddr_in6) &&
723 sin6->sin6_scope_id &&
724 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
725 fl->oif = sin6->sin6_scope_id;
726 } else {
727 if (sk->sk_state != TCP_ESTABLISHED)
728 return -EDESTADDRREQ;
730 fl->fl_ip_dport = inet->dport;
731 daddr = &np->daddr;
732 fl->fl6_flowlabel = np->flow_label;
733 connected = 1;
734 }
736 if (!fl->oif)
737 fl->oif = sk->sk_bound_dev_if;
739 if (msg->msg_controllen) {
740 opt = &opt_space;
741 memset(opt, 0, sizeof(struct ipv6_txoptions));
742 opt->tot_len = sizeof(*opt);
744 err = datagram_send_ctl(msg, fl, opt, &hlimit, &tclass);
745 if (err < 0) {
746 fl6_sock_release(flowlabel);
747 return err;
748 }
749 if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
750 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
751 if (flowlabel == NULL)
752 return -EINVAL;
753 }
754 if (!(opt->opt_nflen|opt->opt_flen))
755 opt = NULL;
756 connected = 0;
757 }
758 if (opt == NULL)
759 opt = np->opt;
760 if (flowlabel)
761 opt = fl6_merge_options(&opt_space, flowlabel, opt);
762 opt = ipv6_fixup_options(&opt_space, opt);
764 fl->proto = IPPROTO_UDP;
765 ipv6_addr_copy(&fl->fl6_dst, daddr);
766 if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
767 ipv6_addr_copy(&fl->fl6_src, &np->saddr);
768 fl->fl_ip_sport = inet->sport;
770 /* merge ip6_build_xmit from ip6_output */
771 if (opt && opt->srcrt) {
772 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
773 ipv6_addr_copy(&final, &fl->fl6_dst);
774 ipv6_addr_copy(&fl->fl6_dst, rt0->addr);
775 final_p = &final;
776 connected = 0;
777 }
779 if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst)) {
780 fl->oif = np->mcast_oif;
781 connected = 0;
782 }
784 err = ip6_sk_dst_lookup(sk, &dst, fl);
785 if (err)
786 goto out;
787 if (final_p)
788 ipv6_addr_copy(&fl->fl6_dst, final_p);
790 if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0)
791 goto out;
793 if (hlimit < 0) {
794 if (ipv6_addr_is_multicast(&fl->fl6_dst))
795 hlimit = np->mcast_hops;
796 else
797 hlimit = np->hop_limit;
798 if (hlimit < 0)
799 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
800 if (hlimit < 0)
801 hlimit = ipv6_get_hoplimit(dst->dev);
802 }
804 if (tclass < 0) {
805 tclass = np->tclass;
806 if (tclass < 0)
807 tclass = 0;
808 }
810 if (msg->msg_flags&MSG_CONFIRM)
811 goto do_confirm;
812 back_from_confirm:
814 lock_sock(sk);
815 if (unlikely(up->pending)) {
816 /* The socket is already corked while preparing it. */
817 /* ... which is an evident application bug. --ANK */
818 release_sock(sk);
820 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
821 err = -EINVAL;
822 goto out;
823 }
825 up->pending = AF_INET6;
827 do_append_data:
828 up->len += ulen;
829 err = ip6_append_data(sk, ip_generic_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, up);
838 if (dst) {
839 if (connected) {
840 ip6_dst_store(sk, dst,
841 ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ?
842 &np->daddr : NULL);
843 } else {
844 dst_release(dst);
845 }
846 }
848 if (err > 0)
849 err = np->recverr ? net_xmit_errno(err) : 0;
850 release_sock(sk);
851 out:
852 fl6_sock_release(flowlabel);
853 if (!err) {
854 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
855 return len;
856 }
857 return err;
859 do_confirm:
860 dst_confirm(dst);
861 if (!(msg->msg_flags&MSG_PROBE) || len)
862 goto back_from_confirm;
863 err = 0;
864 goto out;
865 }
867 static int udpv6_destroy_sock(struct sock *sk)
868 {
869 lock_sock(sk);
870 udp_v6_flush_pending_frames(sk);
871 release_sock(sk);
873 inet6_destroy_sock(sk);
875 return 0;
876 }
878 /*
879 * Socket option code for UDP
880 */
881 static int do_udpv6_setsockopt(struct sock *sk, int level, int optname,
882 char __user *optval, int optlen)
883 {
884 struct udp_sock *up = udp_sk(sk);
885 int val;
886 int err = 0;
888 if(optlen<sizeof(int))
889 return -EINVAL;
891 if (get_user(val, (int __user *)optval))
892 return -EFAULT;
894 switch(optname) {
895 case UDP_CORK:
896 if (val != 0) {
897 up->corkflag = 1;
898 } else {
899 up->corkflag = 0;
900 lock_sock(sk);
901 udp_v6_push_pending_frames(sk, up);
902 release_sock(sk);
903 }
904 break;
906 case UDP_ENCAP:
907 switch (val) {
908 case 0:
909 up->encap_type = val;
910 break;
911 default:
912 err = -ENOPROTOOPT;
913 break;
914 }
915 break;
917 default:
918 err = -ENOPROTOOPT;
919 break;
920 };
922 return err;
923 }
925 static int udpv6_setsockopt(struct sock *sk, int level, int optname,
926 char __user *optval, int optlen)
927 {
928 if (level != SOL_UDP)
929 return ipv6_setsockopt(sk, level, optname, optval, optlen);
930 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
931 }
933 #ifdef CONFIG_COMPAT
934 static int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
935 char __user *optval, int optlen)
936 {
937 if (level != SOL_UDP)
938 return compat_ipv6_setsockopt(sk, level, optname,
939 optval, optlen);
940 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
941 }
942 #endif
944 static int do_udpv6_getsockopt(struct sock *sk, int level, int optname,
945 char __user *optval, int __user *optlen)
946 {
947 struct udp_sock *up = udp_sk(sk);
948 int val, len;
950 if(get_user(len,optlen))
951 return -EFAULT;
953 len = min_t(unsigned int, len, sizeof(int));
955 if(len < 0)
956 return -EINVAL;
958 switch(optname) {
959 case UDP_CORK:
960 val = up->corkflag;
961 break;
963 case UDP_ENCAP:
964 val = up->encap_type;
965 break;
967 default:
968 return -ENOPROTOOPT;
969 };
971 if(put_user(len, optlen))
972 return -EFAULT;
973 if(copy_to_user(optval, &val,len))
974 return -EFAULT;
975 return 0;
976 }
978 static int udpv6_getsockopt(struct sock *sk, int level, int optname,
979 char __user *optval, int __user *optlen)
980 {
981 if (level != SOL_UDP)
982 return ipv6_getsockopt(sk, level, optname, optval, optlen);
983 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
984 }
986 #ifdef CONFIG_COMPAT
987 static int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
988 char __user *optval, int __user *optlen)
989 {
990 if (level != SOL_UDP)
991 return compat_ipv6_getsockopt(sk, level, optname,
992 optval, optlen);
993 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
994 }
995 #endif
997 static struct inet6_protocol udpv6_protocol = {
998 .handler = udpv6_rcv,
999 .err_handler = udpv6_err,
1000 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1001 };
1003 /* ------------------------------------------------------------------------ */
1004 #ifdef CONFIG_PROC_FS
1006 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1008 struct inet_sock *inet = inet_sk(sp);
1009 struct ipv6_pinfo *np = inet6_sk(sp);
1010 struct in6_addr *dest, *src;
1011 __u16 destp, srcp;
1013 dest = &np->daddr;
1014 src = &np->rcv_saddr;
1015 destp = ntohs(inet->dport);
1016 srcp = ntohs(inet->sport);
1017 seq_printf(seq,
1018 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1019 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
1020 bucket,
1021 src->s6_addr32[0], src->s6_addr32[1],
1022 src->s6_addr32[2], src->s6_addr32[3], srcp,
1023 dest->s6_addr32[0], dest->s6_addr32[1],
1024 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1025 sp->sk_state,
1026 atomic_read(&sp->sk_wmem_alloc),
1027 atomic_read(&sp->sk_rmem_alloc),
1028 0, 0L, 0,
1029 sock_i_uid(sp), 0,
1030 sock_i_ino(sp),
1031 atomic_read(&sp->sk_refcnt), sp);
1034 static int udp6_seq_show(struct seq_file *seq, void *v)
1036 if (v == SEQ_START_TOKEN)
1037 seq_printf(seq,
1038 " sl "
1039 "local_address "
1040 "remote_address "
1041 "st tx_queue rx_queue tr tm->when retrnsmt"
1042 " uid timeout inode\n");
1043 else
1044 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1045 return 0;
1048 static struct file_operations udp6_seq_fops;
1049 static struct udp_seq_afinfo udp6_seq_afinfo = {
1050 .owner = THIS_MODULE,
1051 .name = "udp6",
1052 .family = AF_INET6,
1053 .seq_show = udp6_seq_show,
1054 .seq_fops = &udp6_seq_fops,
1055 };
1057 int __init udp6_proc_init(void)
1059 return udp_proc_register(&udp6_seq_afinfo);
1062 void udp6_proc_exit(void) {
1063 udp_proc_unregister(&udp6_seq_afinfo);
1065 #endif /* CONFIG_PROC_FS */
1067 /* ------------------------------------------------------------------------ */
1069 struct proto udpv6_prot = {
1070 .name = "UDPv6",
1071 .owner = THIS_MODULE,
1072 .close = udpv6_close,
1073 .connect = ip6_datagram_connect,
1074 .disconnect = udp_disconnect,
1075 .ioctl = udp_ioctl,
1076 .destroy = udpv6_destroy_sock,
1077 .setsockopt = udpv6_setsockopt,
1078 .getsockopt = udpv6_getsockopt,
1079 .sendmsg = udpv6_sendmsg,
1080 .recvmsg = udpv6_recvmsg,
1081 .backlog_rcv = udpv6_queue_rcv_skb,
1082 .hash = udp_v6_hash,
1083 .unhash = udp_v6_unhash,
1084 .get_port = udp_v6_get_port,
1085 .obj_size = sizeof(struct udp6_sock),
1086 #ifdef CONFIG_COMPAT
1087 .compat_setsockopt = compat_udpv6_setsockopt,
1088 .compat_getsockopt = compat_udpv6_getsockopt,
1089 #endif
1090 };
1092 static struct inet_protosw udpv6_protosw = {
1093 .type = SOCK_DGRAM,
1094 .protocol = IPPROTO_UDP,
1095 .prot = &udpv6_prot,
1096 .ops = &inet6_dgram_ops,
1097 .capability =-1,
1098 .no_check = UDP_CSUM_DEFAULT,
1099 .flags = INET_PROTOSW_PERMANENT,
1100 };
1103 void __init udpv6_init(void)
1105 if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1106 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1107 inet6_register_protosw(&udpv6_protosw);