3 * Linux INET6 implementation
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * Based on linux/ipv4/udp.c
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.
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.
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>
39 #include <net/ndisc.h>
40 #include <net/protocol.h>
41 #include <net/transp_v6.h>
42 #include <net/ip6_route.h>
44 #include <net/tcp_states.h>
45 #include <net/ip6_checksum.h>
48 #include <linux/proc_fs.h>
49 #include <linux/seq_file.h>
52 int udp_v6_get_port(struct sock *sk, unsigned short snum)
54 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
57 static inline int compute_score(struct sock *sk, struct net *net,
59 struct in6_addr *saddr, __be16 sport,
60 struct in6_addr *daddr, __be16 dport,
65 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
66 sk->sk_family == PF_INET6) {
67 struct ipv6_pinfo *np = inet6_sk(sk);
68 struct inet_sock *inet = inet_sk(sk);
72 if (inet->dport != sport)
76 if (!ipv6_addr_any(&np->rcv_saddr)) {
77 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
81 if (!ipv6_addr_any(&np->daddr)) {
82 if (!ipv6_addr_equal(&np->daddr, saddr))
86 if (sk->sk_bound_dev_if) {
87 if (sk->sk_bound_dev_if != dif)
95 static struct sock *__udp6_lib_lookup(struct net *net,
96 struct in6_addr *saddr, __be16 sport,
97 struct in6_addr *daddr, __be16 dport,
98 int dif, struct udp_table *udptable)
100 struct sock *sk, *result;
101 struct hlist_node *node, *next;
102 unsigned short hnum = ntohs(dport);
103 unsigned int hash = udp_hashfn(net, hnum);
104 struct udp_hslot *hslot = &udptable->hash[hash];
111 sk_for_each_rcu_safenext(sk, node, &hslot->head, next) {
113 * lockless reader, and SLAB_DESTROY_BY_RCU items:
114 * We must check this item was not moved to another chain
116 if (udp_hashfn(net, sk->sk_hash) != hash)
118 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
119 if (score > badness) {
125 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
127 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
128 daddr, dport, dif) < badness)) {
137 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
138 __be16 sport, __be16 dport,
139 struct udp_table *udptable)
142 struct ipv6hdr *iph = ipv6_hdr(skb);
144 if (unlikely(sk = skb_steal_sock(skb)))
147 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
148 &iph->daddr, dport, inet6_iif(skb),
153 * This should be easy, if there is something there we
154 * return it, otherwise we block.
157 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
158 struct msghdr *msg, size_t len,
159 int noblock, int flags, int *addr_len)
161 struct ipv6_pinfo *np = inet6_sk(sk);
162 struct inet_sock *inet = inet_sk(sk);
164 unsigned int ulen, copied;
167 int is_udplite = IS_UDPLITE(sk);
171 *addr_len=sizeof(struct sockaddr_in6);
173 if (flags & MSG_ERRQUEUE)
174 return ipv6_recv_error(sk, msg, len);
177 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
182 ulen = skb->len - sizeof(struct udphdr);
186 else if (copied < ulen)
187 msg->msg_flags |= MSG_TRUNC;
189 is_udp4 = (skb->protocol == htons(ETH_P_IP));
192 * If checksum is needed at all, try to do it while copying the
193 * data. If the data is truncated, or if we only want a partial
194 * coverage checksum (UDP-Lite), do it before the copy.
197 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
198 if (udp_lib_checksum_complete(skb))
202 if (skb_csum_unnecessary(skb))
203 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
204 msg->msg_iov, copied );
206 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
215 UDP_INC_STATS_USER(sock_net(sk),
216 UDP_MIB_INDATAGRAMS, is_udplite);
218 UDP6_INC_STATS_USER(sock_net(sk),
219 UDP_MIB_INDATAGRAMS, is_udplite);
222 sock_recv_timestamp(msg, sk, skb);
224 /* Copy the address. */
226 struct sockaddr_in6 *sin6;
228 sin6 = (struct sockaddr_in6 *) msg->msg_name;
229 sin6->sin6_family = AF_INET6;
230 sin6->sin6_port = udp_hdr(skb)->source;
231 sin6->sin6_flowinfo = 0;
232 sin6->sin6_scope_id = 0;
235 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
236 htonl(0xffff), ip_hdr(skb)->saddr);
238 ipv6_addr_copy(&sin6->sin6_addr,
239 &ipv6_hdr(skb)->saddr);
240 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
241 sin6->sin6_scope_id = IP6CB(skb)->iif;
246 if (inet->cmsg_flags)
247 ip_cmsg_recv(msg, skb);
250 datagram_recv_ctl(sk, msg, skb);
254 if (flags & MSG_TRUNC)
259 skb_free_datagram(sk, skb);
266 if (!skb_kill_datagram(sk, skb, flags)) {
268 UDP_INC_STATS_USER(sock_net(sk),
269 UDP_MIB_INERRORS, is_udplite);
271 UDP6_INC_STATS_USER(sock_net(sk),
272 UDP_MIB_INERRORS, is_udplite);
276 if (flags & MSG_DONTWAIT)
281 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
282 int type, int code, int offset, __be32 info,
283 struct udp_table *udptable)
285 struct ipv6_pinfo *np;
286 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
287 struct in6_addr *saddr = &hdr->saddr;
288 struct in6_addr *daddr = &hdr->daddr;
289 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
293 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
294 saddr, uh->source, inet6_iif(skb), udptable);
300 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
303 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
307 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
310 sk->sk_error_report(sk);
315 static __inline__ void udpv6_err(struct sk_buff *skb,
316 struct inet6_skb_parm *opt, int type,
317 int code, int offset, __be32 info )
319 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
322 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
324 struct udp_sock *up = udp_sk(sk);
326 int is_udplite = IS_UDPLITE(sk);
328 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
332 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
334 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
336 if (up->pcrlen == 0) { /* full coverage was set */
337 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
338 " %d while full coverage %d requested\n",
339 UDP_SKB_CB(skb)->cscov, skb->len);
342 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
343 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
344 "too small, need min %d\n",
345 UDP_SKB_CB(skb)->cscov, up->pcrlen);
351 if (udp_lib_checksum_complete(skb))
355 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
356 /* Note that an ENOMEM error is charged twice */
358 UDP6_INC_STATS_BH(sock_net(sk),
359 UDP_MIB_RCVBUFERRORS, is_udplite);
360 atomic_inc(&sk->sk_drops);
367 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
372 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
373 __be16 loc_port, struct in6_addr *loc_addr,
374 __be16 rmt_port, struct in6_addr *rmt_addr,
377 struct hlist_node *node;
379 unsigned short num = ntohs(loc_port);
381 sk_for_each_from(s, node) {
382 struct inet_sock *inet = inet_sk(s);
384 if (!net_eq(sock_net(s), net))
387 if (s->sk_hash == num && s->sk_family == PF_INET6) {
388 struct ipv6_pinfo *np = inet6_sk(s);
390 if (inet->dport != rmt_port)
393 if (!ipv6_addr_any(&np->daddr) &&
394 !ipv6_addr_equal(&np->daddr, rmt_addr))
397 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
400 if (!ipv6_addr_any(&np->rcv_saddr)) {
401 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
404 if (!inet6_mc_check(s, loc_addr, rmt_addr))
413 * Note: called only from the BH handler context,
414 * so we don't need to lock the hashes.
416 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
417 struct in6_addr *saddr, struct in6_addr *daddr,
418 struct udp_table *udptable)
420 struct sock *sk, *sk2;
421 const struct udphdr *uh = udp_hdr(skb);
422 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
425 spin_lock(&hslot->lock);
426 sk = sk_head(&hslot->head);
427 dif = inet6_iif(skb);
428 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
435 while ((sk2 = udp_v6_mcast_next(net, sk_next(sk2), uh->dest, daddr,
436 uh->source, saddr, dif))) {
437 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
440 if (!sock_owned_by_user(sk2))
441 udpv6_queue_rcv_skb(sk2, buff);
443 sk_add_backlog(sk2, buff);
448 if (!sock_owned_by_user(sk))
449 udpv6_queue_rcv_skb(sk, skb);
451 sk_add_backlog(sk, skb);
454 spin_unlock(&hslot->lock);
458 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
463 UDP_SKB_CB(skb)->partial_cov = 0;
464 UDP_SKB_CB(skb)->cscov = skb->len;
466 if (proto == IPPROTO_UDPLITE) {
467 err = udplite_checksum_init(skb, uh);
472 if (uh->check == 0) {
473 /* RFC 2460 section 8.1 says that we SHOULD log
474 this error. Well, it is reasonable.
476 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
479 if (skb->ip_summed == CHECKSUM_COMPLETE &&
480 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
481 skb->len, proto, skb->csum))
482 skb->ip_summed = CHECKSUM_UNNECESSARY;
484 if (!skb_csum_unnecessary(skb))
485 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
486 &ipv6_hdr(skb)->daddr,
487 skb->len, proto, 0));
492 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
497 struct net_device *dev = skb->dev;
498 struct in6_addr *saddr, *daddr;
500 struct net *net = dev_net(skb->dev);
502 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
505 saddr = &ipv6_hdr(skb)->saddr;
506 daddr = &ipv6_hdr(skb)->daddr;
509 ulen = ntohs(uh->len);
513 if (proto == IPPROTO_UDP) {
514 /* UDP validates ulen. */
516 /* Check for jumbo payload */
520 if (ulen < sizeof(*uh))
523 if (ulen < skb->len) {
524 if (pskb_trim_rcsum(skb, ulen))
526 saddr = &ipv6_hdr(skb)->saddr;
527 daddr = &ipv6_hdr(skb)->daddr;
532 if (udp6_csum_init(skb, uh, proto))
536 * Multicast receive code
538 if (ipv6_addr_is_multicast(daddr))
539 return __udp6_lib_mcast_deliver(net, skb,
540 saddr, daddr, udptable);
545 * check socket cache ... must talk to Alan about his plans
546 * for sock caches... i'll skip this for now.
548 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
551 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
554 if (udp_lib_checksum_complete(skb))
556 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
557 proto == IPPROTO_UDPLITE);
559 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
568 if (!sock_owned_by_user(sk))
569 udpv6_queue_rcv_skb(sk, skb);
571 sk_add_backlog(sk, skb);
577 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
578 proto == IPPROTO_UDPLITE ? "-Lite" : "",
582 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
587 static __inline__ int udpv6_rcv(struct sk_buff *skb)
589 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
593 * Throw away all pending data and cancel the corking. Socket is locked.
595 static void udp_v6_flush_pending_frames(struct sock *sk)
597 struct udp_sock *up = udp_sk(sk);
599 if (up->pending == AF_INET)
600 udp_flush_pending_frames(sk);
601 else if (up->pending) {
604 ip6_flush_pending_frames(sk);
612 static int udp_v6_push_pending_frames(struct sock *sk)
616 struct udp_sock *up = udp_sk(sk);
617 struct inet_sock *inet = inet_sk(sk);
618 struct flowi *fl = &inet->cork.fl;
620 int is_udplite = IS_UDPLITE(sk);
623 /* Grab the skbuff where UDP header space exists. */
624 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
628 * Create a UDP header
631 uh->source = fl->fl_ip_sport;
632 uh->dest = fl->fl_ip_dport;
633 uh->len = htons(up->len);
637 csum = udplite_csum_outgoing(sk, skb);
639 csum = udp_csum_outgoing(sk, skb);
641 /* add protocol-dependent pseudo-header */
642 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
643 up->len, fl->proto, csum );
645 uh->check = CSUM_MANGLED_0;
647 err = ip6_push_pending_frames(sk);
652 UDP6_INC_STATS_USER(sock_net(sk),
653 UDP_MIB_OUTDATAGRAMS, is_udplite);
657 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
658 struct msghdr *msg, size_t len)
660 struct ipv6_txoptions opt_space;
661 struct udp_sock *up = udp_sk(sk);
662 struct inet_sock *inet = inet_sk(sk);
663 struct ipv6_pinfo *np = inet6_sk(sk);
664 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
665 struct in6_addr *daddr, *final_p = NULL, final;
666 struct ipv6_txoptions *opt = NULL;
667 struct ip6_flowlabel *flowlabel = NULL;
669 struct dst_entry *dst;
670 int addr_len = msg->msg_namelen;
674 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
677 int is_udplite = IS_UDPLITE(sk);
678 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
680 /* destination address check */
682 if (addr_len < offsetof(struct sockaddr, sa_data))
685 switch (sin6->sin6_family) {
687 if (addr_len < SIN6_LEN_RFC2133)
689 daddr = &sin6->sin6_addr;
694 msg->msg_name = sin6 = NULL;
695 msg->msg_namelen = addr_len = 0;
701 } else if (!up->pending) {
702 if (sk->sk_state != TCP_ESTABLISHED)
703 return -EDESTADDRREQ;
709 if (ipv6_addr_v4mapped(daddr)) {
710 struct sockaddr_in sin;
711 sin.sin_family = AF_INET;
712 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
713 sin.sin_addr.s_addr = daddr->s6_addr32[3];
714 msg->msg_name = &sin;
715 msg->msg_namelen = sizeof(sin);
717 if (__ipv6_only_sock(sk))
719 return udp_sendmsg(iocb, sk, msg, len);
723 if (up->pending == AF_INET)
724 return udp_sendmsg(iocb, sk, msg, len);
726 /* Rough check on arithmetic overflow,
727 better check is made in ip6_append_data().
729 if (len > INT_MAX - sizeof(struct udphdr))
734 * There are pending frames.
735 * The socket lock must be held while it's corked.
738 if (likely(up->pending)) {
739 if (unlikely(up->pending != AF_INET6)) {
741 return -EAFNOSUPPORT;
748 ulen += sizeof(struct udphdr);
750 memset(&fl, 0, sizeof(fl));
753 if (sin6->sin6_port == 0)
756 fl.fl_ip_dport = sin6->sin6_port;
757 daddr = &sin6->sin6_addr;
760 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
761 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
762 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
763 if (flowlabel == NULL)
765 daddr = &flowlabel->dst;
770 * Otherwise it will be difficult to maintain
773 if (sk->sk_state == TCP_ESTABLISHED &&
774 ipv6_addr_equal(daddr, &np->daddr))
777 if (addr_len >= sizeof(struct sockaddr_in6) &&
778 sin6->sin6_scope_id &&
779 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
780 fl.oif = sin6->sin6_scope_id;
782 if (sk->sk_state != TCP_ESTABLISHED)
783 return -EDESTADDRREQ;
785 fl.fl_ip_dport = inet->dport;
787 fl.fl6_flowlabel = np->flow_label;
792 fl.oif = sk->sk_bound_dev_if;
794 if (msg->msg_controllen) {
796 memset(opt, 0, sizeof(struct ipv6_txoptions));
797 opt->tot_len = sizeof(*opt);
799 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
801 fl6_sock_release(flowlabel);
804 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
805 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
806 if (flowlabel == NULL)
809 if (!(opt->opt_nflen|opt->opt_flen))
816 opt = fl6_merge_options(&opt_space, flowlabel, opt);
817 opt = ipv6_fixup_options(&opt_space, opt);
819 fl.proto = sk->sk_protocol;
820 if (!ipv6_addr_any(daddr))
821 ipv6_addr_copy(&fl.fl6_dst, daddr);
823 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
824 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
825 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
826 fl.fl_ip_sport = inet->sport;
828 /* merge ip6_build_xmit from ip6_output */
829 if (opt && opt->srcrt) {
830 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
831 ipv6_addr_copy(&final, &fl.fl6_dst);
832 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
837 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
838 fl.oif = np->mcast_oif;
842 security_sk_classify_flow(sk, &fl);
844 err = ip6_sk_dst_lookup(sk, &dst, &fl);
848 ipv6_addr_copy(&fl.fl6_dst, final_p);
850 if ((err = __xfrm_lookup(&dst, &fl, sk, XFRM_LOOKUP_WAIT)) < 0) {
852 err = ip6_dst_blackhole(sk, &dst, &fl);
858 if (ipv6_addr_is_multicast(&fl.fl6_dst))
859 hlimit = np->mcast_hops;
861 hlimit = np->hop_limit;
863 hlimit = ip6_dst_hoplimit(dst);
872 if (msg->msg_flags&MSG_CONFIRM)
877 if (unlikely(up->pending)) {
878 /* The socket is already corked while preparing it. */
879 /* ... which is an evident application bug. --ANK */
882 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
887 up->pending = AF_INET6;
891 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
892 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
893 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
894 (struct rt6_info*)dst,
895 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
897 udp_v6_flush_pending_frames(sk);
899 err = udp_v6_push_pending_frames(sk);
900 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
905 ip6_dst_store(sk, dst,
906 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
908 #ifdef CONFIG_IPV6_SUBTREES
909 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
920 err = np->recverr ? net_xmit_errno(err) : 0;
924 fl6_sock_release(flowlabel);
928 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
929 * ENOBUFS might not be good (it's not tunable per se), but otherwise
930 * we don't have a good statistic (IpOutDiscards but it can be too many
931 * things). We could add another new stat but at least for now that
932 * seems like overkill.
934 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
935 UDP6_INC_STATS_USER(sock_net(sk),
936 UDP_MIB_SNDBUFERRORS, is_udplite);
942 if (!(msg->msg_flags&MSG_PROBE) || len)
943 goto back_from_confirm;
948 void udpv6_destroy_sock(struct sock *sk)
951 udp_v6_flush_pending_frames(sk);
954 inet6_destroy_sock(sk);
958 * Socket option code for UDP
960 int udpv6_setsockopt(struct sock *sk, int level, int optname,
961 char __user *optval, int optlen)
963 if (level == SOL_UDP || level == SOL_UDPLITE)
964 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
965 udp_v6_push_pending_frames);
966 return ipv6_setsockopt(sk, level, optname, optval, optlen);
970 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
971 char __user *optval, int optlen)
973 if (level == SOL_UDP || level == SOL_UDPLITE)
974 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
975 udp_v6_push_pending_frames);
976 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
980 int udpv6_getsockopt(struct sock *sk, int level, int optname,
981 char __user *optval, int __user *optlen)
983 if (level == SOL_UDP || level == SOL_UDPLITE)
984 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
985 return ipv6_getsockopt(sk, level, optname, optval, optlen);
989 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
990 char __user *optval, int __user *optlen)
992 if (level == SOL_UDP || level == SOL_UDPLITE)
993 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
994 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
998 static struct inet6_protocol udpv6_protocol = {
999 .handler = udpv6_rcv,
1000 .err_handler = udpv6_err,
1001 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1004 /* ------------------------------------------------------------------------ */
1005 #ifdef CONFIG_PROC_FS
1007 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1009 struct inet_sock *inet = inet_sk(sp);
1010 struct ipv6_pinfo *np = inet6_sk(sp);
1011 struct in6_addr *dest, *src;
1015 src = &np->rcv_saddr;
1016 destp = ntohs(inet->dport);
1017 srcp = ntohs(inet->sport);
1019 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1020 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1022 src->s6_addr32[0], src->s6_addr32[1],
1023 src->s6_addr32[2], src->s6_addr32[3], srcp,
1024 dest->s6_addr32[0], dest->s6_addr32[1],
1025 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1027 atomic_read(&sp->sk_wmem_alloc),
1028 atomic_read(&sp->sk_rmem_alloc),
1032 atomic_read(&sp->sk_refcnt), sp,
1033 atomic_read(&sp->sk_drops));
1036 int udp6_seq_show(struct seq_file *seq, void *v)
1038 if (v == SEQ_START_TOKEN)
1043 "st tx_queue rx_queue tr tm->when retrnsmt"
1044 " uid timeout inode ref pointer drops\n");
1046 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1050 static struct udp_seq_afinfo udp6_seq_afinfo = {
1053 .udp_table = &udp_table,
1055 .owner = THIS_MODULE,
1058 .show = udp6_seq_show,
1062 int udp6_proc_init(struct net *net)
1064 return udp_proc_register(net, &udp6_seq_afinfo);
1067 void udp6_proc_exit(struct net *net) {
1068 udp_proc_unregister(net, &udp6_seq_afinfo);
1070 #endif /* CONFIG_PROC_FS */
1072 /* ------------------------------------------------------------------------ */
1074 struct proto udpv6_prot = {
1076 .owner = THIS_MODULE,
1077 .close = udp_lib_close,
1078 .connect = ip6_datagram_connect,
1079 .disconnect = udp_disconnect,
1081 .destroy = udpv6_destroy_sock,
1082 .setsockopt = udpv6_setsockopt,
1083 .getsockopt = udpv6_getsockopt,
1084 .sendmsg = udpv6_sendmsg,
1085 .recvmsg = udpv6_recvmsg,
1086 .backlog_rcv = udpv6_queue_rcv_skb,
1087 .hash = udp_lib_hash,
1088 .unhash = udp_lib_unhash,
1089 .get_port = udp_v6_get_port,
1090 .memory_allocated = &udp_memory_allocated,
1091 .sysctl_mem = sysctl_udp_mem,
1092 .sysctl_wmem = &sysctl_udp_wmem_min,
1093 .sysctl_rmem = &sysctl_udp_rmem_min,
1094 .obj_size = sizeof(struct udp6_sock),
1095 .slab_flags = SLAB_DESTROY_BY_RCU,
1096 .h.udp_table = &udp_table,
1097 #ifdef CONFIG_COMPAT
1098 .compat_setsockopt = compat_udpv6_setsockopt,
1099 .compat_getsockopt = compat_udpv6_getsockopt,
1103 static struct inet_protosw udpv6_protosw = {
1105 .protocol = IPPROTO_UDP,
1106 .prot = &udpv6_prot,
1107 .ops = &inet6_dgram_ops,
1109 .no_check = UDP_CSUM_DEFAULT,
1110 .flags = INET_PROTOSW_PERMANENT,
1114 int __init udpv6_init(void)
1118 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1122 ret = inet6_register_protosw(&udpv6_protosw);
1124 goto out_udpv6_protocol;
1129 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1133 void udpv6_exit(void)
1135 inet6_unregister_protosw(&udpv6_protosw);
1136 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);