[PATCH] Generic ioremap_page_range: x86_64 conversion
[pv_ops_mirror.git] / net / ipv6 / udp.c
blob9662561701d1c8463b53f0f1fe73dd89a2ba44d7
1 /*
2 * UDP over IPv6
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/udp.c
10 * $Id: udp.c,v 1.65 2002/02/01 22:01:04 davem Exp $
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.
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.
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 static inline int udp_v6_get_port(struct sock *sk, unsigned short snum)
66 return udp_get_port(sk, snum, ipv6_rcv_saddr_equal);
69 static void udp_v6_hash(struct sock *sk)
71 BUG();
74 static void udp_v6_unhash(struct sock *sk)
76 write_lock_bh(&udp_hash_lock);
77 if (sk_del_node_init(sk)) {
78 inet_sk(sk)->num = 0;
79 sock_prot_dec_use(sk->sk_prot);
81 write_unlock_bh(&udp_hash_lock);
84 static struct sock *udp_v6_lookup(struct in6_addr *saddr, u16 sport,
85 struct in6_addr *daddr, u16 dport, int dif)
87 struct sock *sk, *result = NULL;
88 struct hlist_node *node;
89 unsigned short hnum = ntohs(dport);
90 int badness = -1;
92 read_lock(&udp_hash_lock);
93 sk_for_each(sk, node, &udp_hash[hnum & (UDP_HTABLE_SIZE - 1)]) {
94 struct inet_sock *inet = inet_sk(sk);
96 if (inet->num == hnum && sk->sk_family == PF_INET6) {
97 struct ipv6_pinfo *np = inet6_sk(sk);
98 int score = 0;
99 if (inet->dport) {
100 if (inet->dport != sport)
101 continue;
102 score++;
104 if (!ipv6_addr_any(&np->rcv_saddr)) {
105 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
106 continue;
107 score++;
109 if (!ipv6_addr_any(&np->daddr)) {
110 if (!ipv6_addr_equal(&np->daddr, saddr))
111 continue;
112 score++;
114 if (sk->sk_bound_dev_if) {
115 if (sk->sk_bound_dev_if != dif)
116 continue;
117 score++;
119 if(score == 4) {
120 result = sk;
121 break;
122 } else if(score > badness) {
123 result = sk;
124 badness = score;
128 if (result)
129 sock_hold(result);
130 read_unlock(&udp_hash_lock);
131 return result;
138 static void udpv6_close(struct sock *sk, long timeout)
140 sk_common_release(sk);
144 * This should be easy, if there is something there we
145 * return it, otherwise we block.
148 static int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
149 struct msghdr *msg, size_t len,
150 int noblock, int flags, int *addr_len)
152 struct ipv6_pinfo *np = inet6_sk(sk);
153 struct inet_sock *inet = inet_sk(sk);
154 struct sk_buff *skb;
155 size_t copied;
156 int err;
158 if (addr_len)
159 *addr_len=sizeof(struct sockaddr_in6);
161 if (flags & MSG_ERRQUEUE)
162 return ipv6_recv_error(sk, msg, len);
164 try_again:
165 skb = skb_recv_datagram(sk, flags, noblock, &err);
166 if (!skb)
167 goto out;
169 copied = skb->len - sizeof(struct udphdr);
170 if (copied > len) {
171 copied = len;
172 msg->msg_flags |= MSG_TRUNC;
175 if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
176 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
177 copied);
178 } else if (msg->msg_flags&MSG_TRUNC) {
179 if (__skb_checksum_complete(skb))
180 goto csum_copy_err;
181 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov,
182 copied);
183 } else {
184 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
185 if (err == -EINVAL)
186 goto csum_copy_err;
188 if (err)
189 goto out_free;
191 sock_recv_timestamp(msg, sk, skb);
193 /* Copy the address. */
194 if (msg->msg_name) {
195 struct sockaddr_in6 *sin6;
197 sin6 = (struct sockaddr_in6 *) msg->msg_name;
198 sin6->sin6_family = AF_INET6;
199 sin6->sin6_port = skb->h.uh->source;
200 sin6->sin6_flowinfo = 0;
201 sin6->sin6_scope_id = 0;
203 if (skb->protocol == htons(ETH_P_IP))
204 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
205 htonl(0xffff), skb->nh.iph->saddr);
206 else {
207 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
208 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
209 sin6->sin6_scope_id = IP6CB(skb)->iif;
213 if (skb->protocol == htons(ETH_P_IP)) {
214 if (inet->cmsg_flags)
215 ip_cmsg_recv(msg, skb);
216 } else {
217 if (np->rxopt.all)
218 datagram_recv_ctl(sk, msg, skb);
221 err = copied;
222 if (flags & MSG_TRUNC)
223 err = skb->len - sizeof(struct udphdr);
225 out_free:
226 skb_free_datagram(sk, skb);
227 out:
228 return err;
230 csum_copy_err:
231 skb_kill_datagram(sk, skb, flags);
233 if (flags & MSG_DONTWAIT) {
234 UDP6_INC_STATS_USER(UDP_MIB_INERRORS);
235 return -EAGAIN;
237 goto try_again;
240 static void udpv6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
241 int type, int code, int offset, __u32 info)
243 struct ipv6_pinfo *np;
244 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
245 struct net_device *dev = skb->dev;
246 struct in6_addr *saddr = &hdr->saddr;
247 struct in6_addr *daddr = &hdr->daddr;
248 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
249 struct sock *sk;
250 int err;
252 sk = udp_v6_lookup(daddr, uh->dest, saddr, uh->source, dev->ifindex);
254 if (sk == NULL)
255 return;
257 np = inet6_sk(sk);
259 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
260 goto out;
262 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
263 goto out;
265 if (np->recverr)
266 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
268 sk->sk_err = err;
269 sk->sk_error_report(sk);
270 out:
271 sock_put(sk);
274 static inline int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
276 int rc;
278 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
279 kfree_skb(skb);
280 return -1;
283 if (skb_checksum_complete(skb)) {
284 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
285 kfree_skb(skb);
286 return 0;
289 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
290 /* Note that an ENOMEM error is charged twice */
291 if (rc == -ENOMEM)
292 UDP6_INC_STATS_BH(UDP_MIB_RCVBUFERRORS);
293 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
294 kfree_skb(skb);
295 return 0;
297 UDP6_INC_STATS_BH(UDP_MIB_INDATAGRAMS);
298 return 0;
301 static struct sock *udp_v6_mcast_next(struct sock *sk,
302 u16 loc_port, struct in6_addr *loc_addr,
303 u16 rmt_port, struct in6_addr *rmt_addr,
304 int dif)
306 struct hlist_node *node;
307 struct sock *s = sk;
308 unsigned short num = ntohs(loc_port);
310 sk_for_each_from(s, node) {
311 struct inet_sock *inet = inet_sk(s);
313 if (inet->num == num && s->sk_family == PF_INET6) {
314 struct ipv6_pinfo *np = inet6_sk(s);
315 if (inet->dport) {
316 if (inet->dport != rmt_port)
317 continue;
319 if (!ipv6_addr_any(&np->daddr) &&
320 !ipv6_addr_equal(&np->daddr, rmt_addr))
321 continue;
323 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
324 continue;
326 if (!ipv6_addr_any(&np->rcv_saddr)) {
327 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
328 continue;
330 if(!inet6_mc_check(s, loc_addr, rmt_addr))
331 continue;
332 return s;
335 return NULL;
339 * Note: called only from the BH handler context,
340 * so we don't need to lock the hashes.
342 static void udpv6_mcast_deliver(struct udphdr *uh,
343 struct in6_addr *saddr, struct in6_addr *daddr,
344 struct sk_buff *skb)
346 struct sock *sk, *sk2;
347 int dif;
349 read_lock(&udp_hash_lock);
350 sk = sk_head(&udp_hash[ntohs(uh->dest) & (UDP_HTABLE_SIZE - 1)]);
351 dif = skb->dev->ifindex;
352 sk = udp_v6_mcast_next(sk, uh->dest, daddr, uh->source, saddr, dif);
353 if (!sk) {
354 kfree_skb(skb);
355 goto out;
358 sk2 = sk;
359 while ((sk2 = udp_v6_mcast_next(sk_next(sk2), uh->dest, daddr,
360 uh->source, saddr, dif))) {
361 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
362 if (buff)
363 udpv6_queue_rcv_skb(sk2, buff);
365 udpv6_queue_rcv_skb(sk, skb);
366 out:
367 read_unlock(&udp_hash_lock);
370 static int udpv6_rcv(struct sk_buff **pskb)
372 struct sk_buff *skb = *pskb;
373 struct sock *sk;
374 struct udphdr *uh;
375 struct net_device *dev = skb->dev;
376 struct in6_addr *saddr, *daddr;
377 u32 ulen = 0;
379 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
380 goto short_packet;
382 saddr = &skb->nh.ipv6h->saddr;
383 daddr = &skb->nh.ipv6h->daddr;
384 uh = skb->h.uh;
386 ulen = ntohs(uh->len);
388 /* Check for jumbo payload */
389 if (ulen == 0)
390 ulen = skb->len;
392 if (ulen > skb->len || ulen < sizeof(*uh))
393 goto short_packet;
395 if (uh->check == 0) {
396 /* RFC 2460 section 8.1 says that we SHOULD log
397 this error. Well, it is reasonable.
399 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
400 goto discard;
403 if (ulen < skb->len) {
404 if (pskb_trim_rcsum(skb, ulen))
405 goto discard;
406 saddr = &skb->nh.ipv6h->saddr;
407 daddr = &skb->nh.ipv6h->daddr;
408 uh = skb->h.uh;
411 if (skb->ip_summed == CHECKSUM_COMPLETE &&
412 !csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, skb->csum))
413 skb->ip_summed = CHECKSUM_UNNECESSARY;
415 if (skb->ip_summed != CHECKSUM_UNNECESSARY)
416 skb->csum = ~csum_ipv6_magic(saddr, daddr, ulen, IPPROTO_UDP, 0);
419 * Multicast receive code
421 if (ipv6_addr_is_multicast(daddr)) {
422 udpv6_mcast_deliver(uh, saddr, daddr, skb);
423 return 0;
426 /* Unicast */
429 * check socket cache ... must talk to Alan about his plans
430 * for sock caches... i'll skip this for now.
432 sk = udp_v6_lookup(saddr, uh->source, daddr, uh->dest, dev->ifindex);
434 if (sk == NULL) {
435 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
436 goto discard;
438 if (skb_checksum_complete(skb))
439 goto discard;
440 UDP6_INC_STATS_BH(UDP_MIB_NOPORTS);
442 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
444 kfree_skb(skb);
445 return(0);
448 /* deliver */
450 udpv6_queue_rcv_skb(sk, skb);
451 sock_put(sk);
452 return(0);
454 short_packet:
455 if (net_ratelimit())
456 printk(KERN_DEBUG "UDP: short packet: %d/%u\n", ulen, skb->len);
458 discard:
459 UDP6_INC_STATS_BH(UDP_MIB_INERRORS);
460 kfree_skb(skb);
461 return(0);
464 * Throw away all pending data and cancel the corking. Socket is locked.
466 static void udp_v6_flush_pending_frames(struct sock *sk)
468 struct udp_sock *up = udp_sk(sk);
470 if (up->pending) {
471 up->len = 0;
472 up->pending = 0;
473 ip6_flush_pending_frames(sk);
478 * Sending
481 static int udp_v6_push_pending_frames(struct sock *sk, struct udp_sock *up)
483 struct sk_buff *skb;
484 struct udphdr *uh;
485 struct inet_sock *inet = inet_sk(sk);
486 struct flowi *fl = &inet->cork.fl;
487 int err = 0;
489 /* Grab the skbuff where UDP header space exists. */
490 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
491 goto out;
494 * Create a UDP header
496 uh = skb->h.uh;
497 uh->source = fl->fl_ip_sport;
498 uh->dest = fl->fl_ip_dport;
499 uh->len = htons(up->len);
500 uh->check = 0;
502 if (sk->sk_no_check == UDP_CSUM_NOXMIT) {
503 skb->ip_summed = CHECKSUM_NONE;
504 goto send;
507 if (skb_queue_len(&sk->sk_write_queue) == 1) {
508 skb->csum = csum_partial((char *)uh,
509 sizeof(struct udphdr), skb->csum);
510 uh->check = csum_ipv6_magic(&fl->fl6_src,
511 &fl->fl6_dst,
512 up->len, fl->proto, skb->csum);
513 } else {
514 u32 tmp_csum = 0;
516 skb_queue_walk(&sk->sk_write_queue, skb) {
517 tmp_csum = csum_add(tmp_csum, skb->csum);
519 tmp_csum = csum_partial((char *)uh,
520 sizeof(struct udphdr), tmp_csum);
521 tmp_csum = csum_ipv6_magic(&fl->fl6_src,
522 &fl->fl6_dst,
523 up->len, fl->proto, tmp_csum);
524 uh->check = tmp_csum;
527 if (uh->check == 0)
528 uh->check = -1;
530 send:
531 err = ip6_push_pending_frames(sk);
532 out:
533 up->len = 0;
534 up->pending = 0;
535 return err;
538 static int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
539 struct msghdr *msg, size_t len)
541 struct ipv6_txoptions opt_space;
542 struct udp_sock *up = udp_sk(sk);
543 struct inet_sock *inet = inet_sk(sk);
544 struct ipv6_pinfo *np = inet6_sk(sk);
545 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
546 struct in6_addr *daddr, *final_p = NULL, final;
547 struct ipv6_txoptions *opt = NULL;
548 struct ip6_flowlabel *flowlabel = NULL;
549 struct flowi *fl = &inet->cork.fl;
550 struct dst_entry *dst;
551 int addr_len = msg->msg_namelen;
552 int ulen = len;
553 int hlimit = -1;
554 int tclass = -1;
555 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
556 int err;
557 int connected = 0;
559 /* destination address check */
560 if (sin6) {
561 if (addr_len < offsetof(struct sockaddr, sa_data))
562 return -EINVAL;
564 switch (sin6->sin6_family) {
565 case AF_INET6:
566 if (addr_len < SIN6_LEN_RFC2133)
567 return -EINVAL;
568 daddr = &sin6->sin6_addr;
569 break;
570 case AF_INET:
571 goto do_udp_sendmsg;
572 case AF_UNSPEC:
573 msg->msg_name = sin6 = NULL;
574 msg->msg_namelen = addr_len = 0;
575 daddr = NULL;
576 break;
577 default:
578 return -EINVAL;
580 } else if (!up->pending) {
581 if (sk->sk_state != TCP_ESTABLISHED)
582 return -EDESTADDRREQ;
583 daddr = &np->daddr;
584 } else
585 daddr = NULL;
587 if (daddr) {
588 if (ipv6_addr_type(daddr) == IPV6_ADDR_MAPPED) {
589 struct sockaddr_in sin;
590 sin.sin_family = AF_INET;
591 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
592 sin.sin_addr.s_addr = daddr->s6_addr32[3];
593 msg->msg_name = &sin;
594 msg->msg_namelen = sizeof(sin);
595 do_udp_sendmsg:
596 if (__ipv6_only_sock(sk))
597 return -ENETUNREACH;
598 return udp_sendmsg(iocb, sk, msg, len);
602 if (up->pending == AF_INET)
603 return udp_sendmsg(iocb, sk, msg, len);
605 /* Rough check on arithmetic overflow,
606 better check is made in ip6_build_xmit
608 if (len > INT_MAX - sizeof(struct udphdr))
609 return -EMSGSIZE;
611 if (up->pending) {
613 * There are pending frames.
614 * The socket lock must be held while it's corked.
616 lock_sock(sk);
617 if (likely(up->pending)) {
618 if (unlikely(up->pending != AF_INET6)) {
619 release_sock(sk);
620 return -EAFNOSUPPORT;
622 dst = NULL;
623 goto do_append_data;
625 release_sock(sk);
627 ulen += sizeof(struct udphdr);
629 memset(fl, 0, sizeof(*fl));
631 if (sin6) {
632 if (sin6->sin6_port == 0)
633 return -EINVAL;
635 fl->fl_ip_dport = sin6->sin6_port;
636 daddr = &sin6->sin6_addr;
638 if (np->sndflow) {
639 fl->fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
640 if (fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
641 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
642 if (flowlabel == NULL)
643 return -EINVAL;
644 daddr = &flowlabel->dst;
649 * Otherwise it will be difficult to maintain
650 * sk->sk_dst_cache.
652 if (sk->sk_state == TCP_ESTABLISHED &&
653 ipv6_addr_equal(daddr, &np->daddr))
654 daddr = &np->daddr;
656 if (addr_len >= sizeof(struct sockaddr_in6) &&
657 sin6->sin6_scope_id &&
658 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
659 fl->oif = sin6->sin6_scope_id;
660 } else {
661 if (sk->sk_state != TCP_ESTABLISHED)
662 return -EDESTADDRREQ;
664 fl->fl_ip_dport = inet->dport;
665 daddr = &np->daddr;
666 fl->fl6_flowlabel = np->flow_label;
667 connected = 1;
670 if (!fl->oif)
671 fl->oif = sk->sk_bound_dev_if;
673 if (msg->msg_controllen) {
674 opt = &opt_space;
675 memset(opt, 0, sizeof(struct ipv6_txoptions));
676 opt->tot_len = sizeof(*opt);
678 err = datagram_send_ctl(msg, fl, opt, &hlimit, &tclass);
679 if (err < 0) {
680 fl6_sock_release(flowlabel);
681 return err;
683 if ((fl->fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
684 flowlabel = fl6_sock_lookup(sk, fl->fl6_flowlabel);
685 if (flowlabel == NULL)
686 return -EINVAL;
688 if (!(opt->opt_nflen|opt->opt_flen))
689 opt = NULL;
690 connected = 0;
692 if (opt == NULL)
693 opt = np->opt;
694 if (flowlabel)
695 opt = fl6_merge_options(&opt_space, flowlabel, opt);
696 opt = ipv6_fixup_options(&opt_space, opt);
698 fl->proto = IPPROTO_UDP;
699 ipv6_addr_copy(&fl->fl6_dst, daddr);
700 if (ipv6_addr_any(&fl->fl6_src) && !ipv6_addr_any(&np->saddr))
701 ipv6_addr_copy(&fl->fl6_src, &np->saddr);
702 fl->fl_ip_sport = inet->sport;
704 /* merge ip6_build_xmit from ip6_output */
705 if (opt && opt->srcrt) {
706 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
707 ipv6_addr_copy(&final, &fl->fl6_dst);
708 ipv6_addr_copy(&fl->fl6_dst, rt0->addr);
709 final_p = &final;
710 connected = 0;
713 if (!fl->oif && ipv6_addr_is_multicast(&fl->fl6_dst)) {
714 fl->oif = np->mcast_oif;
715 connected = 0;
718 security_sk_classify_flow(sk, fl);
720 err = ip6_sk_dst_lookup(sk, &dst, fl);
721 if (err)
722 goto out;
723 if (final_p)
724 ipv6_addr_copy(&fl->fl6_dst, final_p);
726 if ((err = xfrm_lookup(&dst, fl, sk, 0)) < 0)
727 goto out;
729 if (hlimit < 0) {
730 if (ipv6_addr_is_multicast(&fl->fl6_dst))
731 hlimit = np->mcast_hops;
732 else
733 hlimit = np->hop_limit;
734 if (hlimit < 0)
735 hlimit = dst_metric(dst, RTAX_HOPLIMIT);
736 if (hlimit < 0)
737 hlimit = ipv6_get_hoplimit(dst->dev);
740 if (tclass < 0) {
741 tclass = np->tclass;
742 if (tclass < 0)
743 tclass = 0;
746 if (msg->msg_flags&MSG_CONFIRM)
747 goto do_confirm;
748 back_from_confirm:
750 lock_sock(sk);
751 if (unlikely(up->pending)) {
752 /* The socket is already corked while preparing it. */
753 /* ... which is an evident application bug. --ANK */
754 release_sock(sk);
756 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
757 err = -EINVAL;
758 goto out;
761 up->pending = AF_INET6;
763 do_append_data:
764 up->len += ulen;
765 err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov, ulen,
766 sizeof(struct udphdr), hlimit, tclass, opt, fl,
767 (struct rt6_info*)dst,
768 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
769 if (err)
770 udp_v6_flush_pending_frames(sk);
771 else if (!corkreq)
772 err = udp_v6_push_pending_frames(sk, up);
774 if (dst) {
775 if (connected) {
776 ip6_dst_store(sk, dst,
777 ipv6_addr_equal(&fl->fl6_dst, &np->daddr) ?
778 &np->daddr : NULL,
779 #ifdef CONFIG_IPV6_SUBTREES
780 ipv6_addr_equal(&fl->fl6_src, &np->saddr) ?
781 &np->saddr :
782 #endif
783 NULL);
784 } else {
785 dst_release(dst);
789 if (err > 0)
790 err = np->recverr ? net_xmit_errno(err) : 0;
791 release_sock(sk);
792 out:
793 fl6_sock_release(flowlabel);
794 if (!err) {
795 UDP6_INC_STATS_USER(UDP_MIB_OUTDATAGRAMS);
796 return len;
799 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
800 * ENOBUFS might not be good (it's not tunable per se), but otherwise
801 * we don't have a good statistic (IpOutDiscards but it can be too many
802 * things). We could add another new stat but at least for now that
803 * seems like overkill.
805 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
806 UDP6_INC_STATS_USER(UDP_MIB_SNDBUFERRORS);
808 return err;
810 do_confirm:
811 dst_confirm(dst);
812 if (!(msg->msg_flags&MSG_PROBE) || len)
813 goto back_from_confirm;
814 err = 0;
815 goto out;
818 static int udpv6_destroy_sock(struct sock *sk)
820 lock_sock(sk);
821 udp_v6_flush_pending_frames(sk);
822 release_sock(sk);
824 inet6_destroy_sock(sk);
826 return 0;
830 * Socket option code for UDP
832 static int do_udpv6_setsockopt(struct sock *sk, int level, int optname,
833 char __user *optval, int optlen)
835 struct udp_sock *up = udp_sk(sk);
836 int val;
837 int err = 0;
839 if(optlen<sizeof(int))
840 return -EINVAL;
842 if (get_user(val, (int __user *)optval))
843 return -EFAULT;
845 switch(optname) {
846 case UDP_CORK:
847 if (val != 0) {
848 up->corkflag = 1;
849 } else {
850 up->corkflag = 0;
851 lock_sock(sk);
852 udp_v6_push_pending_frames(sk, up);
853 release_sock(sk);
855 break;
857 case UDP_ENCAP:
858 switch (val) {
859 case 0:
860 up->encap_type = val;
861 break;
862 default:
863 err = -ENOPROTOOPT;
864 break;
866 break;
868 default:
869 err = -ENOPROTOOPT;
870 break;
873 return err;
876 static int udpv6_setsockopt(struct sock *sk, int level, int optname,
877 char __user *optval, int optlen)
879 if (level != SOL_UDP)
880 return ipv6_setsockopt(sk, level, optname, optval, optlen);
881 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
884 #ifdef CONFIG_COMPAT
885 static int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
886 char __user *optval, int optlen)
888 if (level != SOL_UDP)
889 return compat_ipv6_setsockopt(sk, level, optname,
890 optval, optlen);
891 return do_udpv6_setsockopt(sk, level, optname, optval, optlen);
893 #endif
895 static int do_udpv6_getsockopt(struct sock *sk, int level, int optname,
896 char __user *optval, int __user *optlen)
898 struct udp_sock *up = udp_sk(sk);
899 int val, len;
901 if(get_user(len,optlen))
902 return -EFAULT;
904 len = min_t(unsigned int, len, sizeof(int));
906 if(len < 0)
907 return -EINVAL;
909 switch(optname) {
910 case UDP_CORK:
911 val = up->corkflag;
912 break;
914 case UDP_ENCAP:
915 val = up->encap_type;
916 break;
918 default:
919 return -ENOPROTOOPT;
922 if(put_user(len, optlen))
923 return -EFAULT;
924 if(copy_to_user(optval, &val,len))
925 return -EFAULT;
926 return 0;
929 static int udpv6_getsockopt(struct sock *sk, int level, int optname,
930 char __user *optval, int __user *optlen)
932 if (level != SOL_UDP)
933 return ipv6_getsockopt(sk, level, optname, optval, optlen);
934 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
937 #ifdef CONFIG_COMPAT
938 static int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
939 char __user *optval, int __user *optlen)
941 if (level != SOL_UDP)
942 return compat_ipv6_getsockopt(sk, level, optname,
943 optval, optlen);
944 return do_udpv6_getsockopt(sk, level, optname, optval, optlen);
946 #endif
948 static struct inet6_protocol udpv6_protocol = {
949 .handler = udpv6_rcv,
950 .err_handler = udpv6_err,
951 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
954 /* ------------------------------------------------------------------------ */
955 #ifdef CONFIG_PROC_FS
957 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
959 struct inet_sock *inet = inet_sk(sp);
960 struct ipv6_pinfo *np = inet6_sk(sp);
961 struct in6_addr *dest, *src;
962 __u16 destp, srcp;
964 dest = &np->daddr;
965 src = &np->rcv_saddr;
966 destp = ntohs(inet->dport);
967 srcp = ntohs(inet->sport);
968 seq_printf(seq,
969 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
970 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
971 bucket,
972 src->s6_addr32[0], src->s6_addr32[1],
973 src->s6_addr32[2], src->s6_addr32[3], srcp,
974 dest->s6_addr32[0], dest->s6_addr32[1],
975 dest->s6_addr32[2], dest->s6_addr32[3], destp,
976 sp->sk_state,
977 atomic_read(&sp->sk_wmem_alloc),
978 atomic_read(&sp->sk_rmem_alloc),
979 0, 0L, 0,
980 sock_i_uid(sp), 0,
981 sock_i_ino(sp),
982 atomic_read(&sp->sk_refcnt), sp);
985 static int udp6_seq_show(struct seq_file *seq, void *v)
987 if (v == SEQ_START_TOKEN)
988 seq_printf(seq,
989 " sl "
990 "local_address "
991 "remote_address "
992 "st tx_queue rx_queue tr tm->when retrnsmt"
993 " uid timeout inode\n");
994 else
995 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
996 return 0;
999 static struct file_operations udp6_seq_fops;
1000 static struct udp_seq_afinfo udp6_seq_afinfo = {
1001 .owner = THIS_MODULE,
1002 .name = "udp6",
1003 .family = AF_INET6,
1004 .seq_show = udp6_seq_show,
1005 .seq_fops = &udp6_seq_fops,
1008 int __init udp6_proc_init(void)
1010 return udp_proc_register(&udp6_seq_afinfo);
1013 void udp6_proc_exit(void) {
1014 udp_proc_unregister(&udp6_seq_afinfo);
1016 #endif /* CONFIG_PROC_FS */
1018 /* ------------------------------------------------------------------------ */
1020 struct proto udpv6_prot = {
1021 .name = "UDPv6",
1022 .owner = THIS_MODULE,
1023 .close = udpv6_close,
1024 .connect = ip6_datagram_connect,
1025 .disconnect = udp_disconnect,
1026 .ioctl = udp_ioctl,
1027 .destroy = udpv6_destroy_sock,
1028 .setsockopt = udpv6_setsockopt,
1029 .getsockopt = udpv6_getsockopt,
1030 .sendmsg = udpv6_sendmsg,
1031 .recvmsg = udpv6_recvmsg,
1032 .backlog_rcv = udpv6_queue_rcv_skb,
1033 .hash = udp_v6_hash,
1034 .unhash = udp_v6_unhash,
1035 .get_port = udp_v6_get_port,
1036 .obj_size = sizeof(struct udp6_sock),
1037 #ifdef CONFIG_COMPAT
1038 .compat_setsockopt = compat_udpv6_setsockopt,
1039 .compat_getsockopt = compat_udpv6_getsockopt,
1040 #endif
1043 static struct inet_protosw udpv6_protosw = {
1044 .type = SOCK_DGRAM,
1045 .protocol = IPPROTO_UDP,
1046 .prot = &udpv6_prot,
1047 .ops = &inet6_dgram_ops,
1048 .capability =-1,
1049 .no_check = UDP_CSUM_DEFAULT,
1050 .flags = INET_PROTOSW_PERMANENT,
1054 void __init udpv6_init(void)
1056 if (inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP) < 0)
1057 printk(KERN_ERR "udpv6_init: Could not register protocol\n");
1058 inet6_register_protosw(&udpv6_protosw);