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 ipv6_rcv_saddr_equal(const struct sock *sk, const struct sock *sk2)
54 const struct in6_addr *sk_rcv_saddr6 = &inet6_sk(sk)->rcv_saddr;
55 const struct in6_addr *sk2_rcv_saddr6 = inet6_rcv_saddr(sk2);
56 __be32 sk_rcv_saddr = inet_sk(sk)->rcv_saddr;
57 __be32 sk2_rcv_saddr = inet_rcv_saddr(sk2);
58 int sk_ipv6only = ipv6_only_sock(sk);
59 int sk2_ipv6only = inet_v6_ipv6only(sk2);
60 int addr_type = ipv6_addr_type(sk_rcv_saddr6);
61 int addr_type2 = sk2_rcv_saddr6 ? ipv6_addr_type(sk2_rcv_saddr6) : IPV6_ADDR_MAPPED;
63 /* if both are mapped, treat as IPv4 */
64 if (addr_type == IPV6_ADDR_MAPPED && addr_type2 == IPV6_ADDR_MAPPED)
65 return (!sk2_ipv6only &&
66 (!sk_rcv_saddr || !sk2_rcv_saddr ||
67 sk_rcv_saddr == sk2_rcv_saddr));
69 if (addr_type2 == IPV6_ADDR_ANY &&
70 !(sk2_ipv6only && addr_type == IPV6_ADDR_MAPPED))
73 if (addr_type == IPV6_ADDR_ANY &&
74 !(sk_ipv6only && addr_type2 == IPV6_ADDR_MAPPED))
78 ipv6_addr_equal(sk_rcv_saddr6, sk2_rcv_saddr6))
84 int udp_v6_get_port(struct sock *sk, unsigned short snum)
86 return udp_lib_get_port(sk, snum, ipv6_rcv_saddr_equal);
89 static inline int compute_score(struct sock *sk, struct net *net,
91 struct in6_addr *saddr, __be16 sport,
92 struct in6_addr *daddr, __be16 dport,
97 if (net_eq(sock_net(sk), net) && sk->sk_hash == hnum &&
98 sk->sk_family == PF_INET6) {
99 struct ipv6_pinfo *np = inet6_sk(sk);
100 struct inet_sock *inet = inet_sk(sk);
104 if (inet->dport != sport)
108 if (!ipv6_addr_any(&np->rcv_saddr)) {
109 if (!ipv6_addr_equal(&np->rcv_saddr, daddr))
113 if (!ipv6_addr_any(&np->daddr)) {
114 if (!ipv6_addr_equal(&np->daddr, saddr))
118 if (sk->sk_bound_dev_if) {
119 if (sk->sk_bound_dev_if != dif)
127 static struct sock *__udp6_lib_lookup(struct net *net,
128 struct in6_addr *saddr, __be16 sport,
129 struct in6_addr *daddr, __be16 dport,
130 int dif, struct udp_table *udptable)
132 struct sock *sk, *result;
133 struct hlist_nulls_node *node;
134 unsigned short hnum = ntohs(dport);
135 unsigned int hash = udp_hashfn(net, hnum);
136 struct udp_hslot *hslot = &udptable->hash[hash];
143 sk_nulls_for_each_rcu(sk, node, &hslot->head) {
144 score = compute_score(sk, net, hnum, saddr, sport, daddr, dport, dif);
145 if (score > badness) {
151 * if the nulls value we got at the end of this lookup is
152 * not the expected one, we must restart lookup.
153 * We probably met an item that was moved to another chain.
155 if (get_nulls_value(node) != hash)
159 if (unlikely(!atomic_inc_not_zero(&result->sk_refcnt)))
161 else if (unlikely(compute_score(result, net, hnum, saddr, sport,
162 daddr, dport, dif) < badness)) {
171 static struct sock *__udp6_lib_lookup_skb(struct sk_buff *skb,
172 __be16 sport, __be16 dport,
173 struct udp_table *udptable)
176 struct ipv6hdr *iph = ipv6_hdr(skb);
178 if (unlikely(sk = skb_steal_sock(skb)))
181 return __udp6_lib_lookup(dev_net(skb->dst->dev), &iph->saddr, sport,
182 &iph->daddr, dport, inet6_iif(skb),
187 * This should be easy, if there is something there we
188 * return it, otherwise we block.
191 int udpv6_recvmsg(struct kiocb *iocb, struct sock *sk,
192 struct msghdr *msg, size_t len,
193 int noblock, int flags, int *addr_len)
195 struct ipv6_pinfo *np = inet6_sk(sk);
196 struct inet_sock *inet = inet_sk(sk);
198 unsigned int ulen, copied;
201 int is_udplite = IS_UDPLITE(sk);
205 *addr_len=sizeof(struct sockaddr_in6);
207 if (flags & MSG_ERRQUEUE)
208 return ipv6_recv_error(sk, msg, len);
211 skb = __skb_recv_datagram(sk, flags | (noblock ? MSG_DONTWAIT : 0),
216 ulen = skb->len - sizeof(struct udphdr);
220 else if (copied < ulen)
221 msg->msg_flags |= MSG_TRUNC;
223 is_udp4 = (skb->protocol == htons(ETH_P_IP));
226 * If checksum is needed at all, try to do it while copying the
227 * data. If the data is truncated, or if we only want a partial
228 * coverage checksum (UDP-Lite), do it before the copy.
231 if (copied < ulen || UDP_SKB_CB(skb)->partial_cov) {
232 if (udp_lib_checksum_complete(skb))
236 if (skb_csum_unnecessary(skb))
237 err = skb_copy_datagram_iovec(skb, sizeof(struct udphdr),
238 msg->msg_iov, copied );
240 err = skb_copy_and_csum_datagram_iovec(skb, sizeof(struct udphdr), msg->msg_iov);
249 UDP_INC_STATS_USER(sock_net(sk),
250 UDP_MIB_INDATAGRAMS, is_udplite);
252 UDP6_INC_STATS_USER(sock_net(sk),
253 UDP_MIB_INDATAGRAMS, is_udplite);
256 sock_recv_timestamp(msg, sk, skb);
258 /* Copy the address. */
260 struct sockaddr_in6 *sin6;
262 sin6 = (struct sockaddr_in6 *) msg->msg_name;
263 sin6->sin6_family = AF_INET6;
264 sin6->sin6_port = udp_hdr(skb)->source;
265 sin6->sin6_flowinfo = 0;
266 sin6->sin6_scope_id = 0;
269 ipv6_addr_set(&sin6->sin6_addr, 0, 0,
270 htonl(0xffff), ip_hdr(skb)->saddr);
272 ipv6_addr_copy(&sin6->sin6_addr,
273 &ipv6_hdr(skb)->saddr);
274 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
275 sin6->sin6_scope_id = IP6CB(skb)->iif;
280 if (inet->cmsg_flags)
281 ip_cmsg_recv(msg, skb);
284 datagram_recv_ctl(sk, msg, skb);
288 if (flags & MSG_TRUNC)
293 skb_free_datagram(sk, skb);
300 if (!skb_kill_datagram(sk, skb, flags)) {
302 UDP_INC_STATS_USER(sock_net(sk),
303 UDP_MIB_INERRORS, is_udplite);
305 UDP6_INC_STATS_USER(sock_net(sk),
306 UDP_MIB_INERRORS, is_udplite);
310 if (flags & MSG_DONTWAIT)
315 void __udp6_lib_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
316 int type, int code, int offset, __be32 info,
317 struct udp_table *udptable)
319 struct ipv6_pinfo *np;
320 struct ipv6hdr *hdr = (struct ipv6hdr*)skb->data;
321 struct in6_addr *saddr = &hdr->saddr;
322 struct in6_addr *daddr = &hdr->daddr;
323 struct udphdr *uh = (struct udphdr*)(skb->data+offset);
327 sk = __udp6_lib_lookup(dev_net(skb->dev), daddr, uh->dest,
328 saddr, uh->source, inet6_iif(skb), udptable);
334 if (!icmpv6_err_convert(type, code, &err) && !np->recverr)
337 if (sk->sk_state != TCP_ESTABLISHED && !np->recverr)
341 ipv6_icmp_error(sk, skb, err, uh->dest, ntohl(info), (u8 *)(uh+1));
344 sk->sk_error_report(sk);
349 static __inline__ void udpv6_err(struct sk_buff *skb,
350 struct inet6_skb_parm *opt, int type,
351 int code, int offset, __be32 info )
353 __udp6_lib_err(skb, opt, type, code, offset, info, &udp_table);
356 int udpv6_queue_rcv_skb(struct sock * sk, struct sk_buff *skb)
358 struct udp_sock *up = udp_sk(sk);
360 int is_udplite = IS_UDPLITE(sk);
362 if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb))
366 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
368 if ((is_udplite & UDPLITE_RECV_CC) && UDP_SKB_CB(skb)->partial_cov) {
370 if (up->pcrlen == 0) { /* full coverage was set */
371 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: partial coverage"
372 " %d while full coverage %d requested\n",
373 UDP_SKB_CB(skb)->cscov, skb->len);
376 if (UDP_SKB_CB(skb)->cscov < up->pcrlen) {
377 LIMIT_NETDEBUG(KERN_WARNING "UDPLITE6: coverage %d "
378 "too small, need min %d\n",
379 UDP_SKB_CB(skb)->cscov, up->pcrlen);
385 if (udp_lib_checksum_complete(skb))
389 if ((rc = sock_queue_rcv_skb(sk,skb)) < 0) {
390 /* Note that an ENOMEM error is charged twice */
392 UDP6_INC_STATS_BH(sock_net(sk),
393 UDP_MIB_RCVBUFERRORS, is_udplite);
394 atomic_inc(&sk->sk_drops);
401 UDP6_INC_STATS_BH(sock_net(sk), UDP_MIB_INERRORS, is_udplite);
406 static struct sock *udp_v6_mcast_next(struct net *net, struct sock *sk,
407 __be16 loc_port, struct in6_addr *loc_addr,
408 __be16 rmt_port, struct in6_addr *rmt_addr,
411 struct hlist_nulls_node *node;
413 unsigned short num = ntohs(loc_port);
415 sk_nulls_for_each_from(s, node) {
416 struct inet_sock *inet = inet_sk(s);
418 if (!net_eq(sock_net(s), net))
421 if (s->sk_hash == num && s->sk_family == PF_INET6) {
422 struct ipv6_pinfo *np = inet6_sk(s);
424 if (inet->dport != rmt_port)
427 if (!ipv6_addr_any(&np->daddr) &&
428 !ipv6_addr_equal(&np->daddr, rmt_addr))
431 if (s->sk_bound_dev_if && s->sk_bound_dev_if != dif)
434 if (!ipv6_addr_any(&np->rcv_saddr)) {
435 if (!ipv6_addr_equal(&np->rcv_saddr, loc_addr))
438 if (!inet6_mc_check(s, loc_addr, rmt_addr))
447 * Note: called only from the BH handler context,
448 * so we don't need to lock the hashes.
450 static int __udp6_lib_mcast_deliver(struct net *net, struct sk_buff *skb,
451 struct in6_addr *saddr, struct in6_addr *daddr,
452 struct udp_table *udptable)
454 struct sock *sk, *sk2;
455 const struct udphdr *uh = udp_hdr(skb);
456 struct udp_hslot *hslot = &udptable->hash[udp_hashfn(net, ntohs(uh->dest))];
459 spin_lock(&hslot->lock);
460 sk = sk_nulls_head(&hslot->head);
461 dif = inet6_iif(skb);
462 sk = udp_v6_mcast_next(net, sk, uh->dest, daddr, uh->source, saddr, dif);
469 while ((sk2 = udp_v6_mcast_next(net, sk_nulls_next(sk2), uh->dest, daddr,
470 uh->source, saddr, dif))) {
471 struct sk_buff *buff = skb_clone(skb, GFP_ATOMIC);
474 if (!sock_owned_by_user(sk2))
475 udpv6_queue_rcv_skb(sk2, buff);
477 sk_add_backlog(sk2, buff);
482 if (!sock_owned_by_user(sk))
483 udpv6_queue_rcv_skb(sk, skb);
485 sk_add_backlog(sk, skb);
488 spin_unlock(&hslot->lock);
492 static inline int udp6_csum_init(struct sk_buff *skb, struct udphdr *uh,
497 UDP_SKB_CB(skb)->partial_cov = 0;
498 UDP_SKB_CB(skb)->cscov = skb->len;
500 if (proto == IPPROTO_UDPLITE) {
501 err = udplite_checksum_init(skb, uh);
506 if (uh->check == 0) {
507 /* RFC 2460 section 8.1 says that we SHOULD log
508 this error. Well, it is reasonable.
510 LIMIT_NETDEBUG(KERN_INFO "IPv6: udp checksum is 0\n");
513 if (skb->ip_summed == CHECKSUM_COMPLETE &&
514 !csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
515 skb->len, proto, skb->csum))
516 skb->ip_summed = CHECKSUM_UNNECESSARY;
518 if (!skb_csum_unnecessary(skb))
519 skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
520 &ipv6_hdr(skb)->daddr,
521 skb->len, proto, 0));
526 int __udp6_lib_rcv(struct sk_buff *skb, struct udp_table *udptable,
531 struct net_device *dev = skb->dev;
532 struct in6_addr *saddr, *daddr;
534 struct net *net = dev_net(skb->dev);
536 if (!pskb_may_pull(skb, sizeof(struct udphdr)))
539 saddr = &ipv6_hdr(skb)->saddr;
540 daddr = &ipv6_hdr(skb)->daddr;
543 ulen = ntohs(uh->len);
547 if (proto == IPPROTO_UDP) {
548 /* UDP validates ulen. */
550 /* Check for jumbo payload */
554 if (ulen < sizeof(*uh))
557 if (ulen < skb->len) {
558 if (pskb_trim_rcsum(skb, ulen))
560 saddr = &ipv6_hdr(skb)->saddr;
561 daddr = &ipv6_hdr(skb)->daddr;
566 if (udp6_csum_init(skb, uh, proto))
570 * Multicast receive code
572 if (ipv6_addr_is_multicast(daddr))
573 return __udp6_lib_mcast_deliver(net, skb,
574 saddr, daddr, udptable);
579 * check socket cache ... must talk to Alan about his plans
580 * for sock caches... i'll skip this for now.
582 sk = __udp6_lib_lookup_skb(skb, uh->source, uh->dest, udptable);
585 if (!xfrm6_policy_check(NULL, XFRM_POLICY_IN, skb))
588 if (udp_lib_checksum_complete(skb))
590 UDP6_INC_STATS_BH(net, UDP_MIB_NOPORTS,
591 proto == IPPROTO_UDPLITE);
593 icmpv6_send(skb, ICMPV6_DEST_UNREACH, ICMPV6_PORT_UNREACH, 0, dev);
602 if (!sock_owned_by_user(sk))
603 udpv6_queue_rcv_skb(sk, skb);
605 sk_add_backlog(sk, skb);
611 LIMIT_NETDEBUG(KERN_DEBUG "UDP%sv6: short packet: %d/%u\n",
612 proto == IPPROTO_UDPLITE ? "-Lite" : "",
616 UDP6_INC_STATS_BH(net, UDP_MIB_INERRORS, proto == IPPROTO_UDPLITE);
621 static __inline__ int udpv6_rcv(struct sk_buff *skb)
623 return __udp6_lib_rcv(skb, &udp_table, IPPROTO_UDP);
627 * Throw away all pending data and cancel the corking. Socket is locked.
629 static void udp_v6_flush_pending_frames(struct sock *sk)
631 struct udp_sock *up = udp_sk(sk);
633 if (up->pending == AF_INET)
634 udp_flush_pending_frames(sk);
635 else if (up->pending) {
638 ip6_flush_pending_frames(sk);
646 static int udp_v6_push_pending_frames(struct sock *sk)
650 struct udp_sock *up = udp_sk(sk);
651 struct inet_sock *inet = inet_sk(sk);
652 struct flowi *fl = &inet->cork.fl;
654 int is_udplite = IS_UDPLITE(sk);
657 /* Grab the skbuff where UDP header space exists. */
658 if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
662 * Create a UDP header
665 uh->source = fl->fl_ip_sport;
666 uh->dest = fl->fl_ip_dport;
667 uh->len = htons(up->len);
671 csum = udplite_csum_outgoing(sk, skb);
673 csum = udp_csum_outgoing(sk, skb);
675 /* add protocol-dependent pseudo-header */
676 uh->check = csum_ipv6_magic(&fl->fl6_src, &fl->fl6_dst,
677 up->len, fl->proto, csum );
679 uh->check = CSUM_MANGLED_0;
681 err = ip6_push_pending_frames(sk);
686 UDP6_INC_STATS_USER(sock_net(sk),
687 UDP_MIB_OUTDATAGRAMS, is_udplite);
691 int udpv6_sendmsg(struct kiocb *iocb, struct sock *sk,
692 struct msghdr *msg, size_t len)
694 struct ipv6_txoptions opt_space;
695 struct udp_sock *up = udp_sk(sk);
696 struct inet_sock *inet = inet_sk(sk);
697 struct ipv6_pinfo *np = inet6_sk(sk);
698 struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) msg->msg_name;
699 struct in6_addr *daddr, *final_p = NULL, final;
700 struct ipv6_txoptions *opt = NULL;
701 struct ip6_flowlabel *flowlabel = NULL;
703 struct dst_entry *dst;
704 int addr_len = msg->msg_namelen;
708 int corkreq = up->corkflag || msg->msg_flags&MSG_MORE;
711 int is_udplite = IS_UDPLITE(sk);
712 int (*getfrag)(void *, char *, int, int, int, struct sk_buff *);
714 /* destination address check */
716 if (addr_len < offsetof(struct sockaddr, sa_data))
719 switch (sin6->sin6_family) {
721 if (addr_len < SIN6_LEN_RFC2133)
723 daddr = &sin6->sin6_addr;
728 msg->msg_name = sin6 = NULL;
729 msg->msg_namelen = addr_len = 0;
735 } else if (!up->pending) {
736 if (sk->sk_state != TCP_ESTABLISHED)
737 return -EDESTADDRREQ;
743 if (ipv6_addr_v4mapped(daddr)) {
744 struct sockaddr_in sin;
745 sin.sin_family = AF_INET;
746 sin.sin_port = sin6 ? sin6->sin6_port : inet->dport;
747 sin.sin_addr.s_addr = daddr->s6_addr32[3];
748 msg->msg_name = &sin;
749 msg->msg_namelen = sizeof(sin);
751 if (__ipv6_only_sock(sk))
753 return udp_sendmsg(iocb, sk, msg, len);
757 if (up->pending == AF_INET)
758 return udp_sendmsg(iocb, sk, msg, len);
760 /* Rough check on arithmetic overflow,
761 better check is made in ip6_append_data().
763 if (len > INT_MAX - sizeof(struct udphdr))
768 * There are pending frames.
769 * The socket lock must be held while it's corked.
772 if (likely(up->pending)) {
773 if (unlikely(up->pending != AF_INET6)) {
775 return -EAFNOSUPPORT;
782 ulen += sizeof(struct udphdr);
784 memset(&fl, 0, sizeof(fl));
787 if (sin6->sin6_port == 0)
790 fl.fl_ip_dport = sin6->sin6_port;
791 daddr = &sin6->sin6_addr;
794 fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
795 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
796 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
797 if (flowlabel == NULL)
799 daddr = &flowlabel->dst;
804 * Otherwise it will be difficult to maintain
807 if (sk->sk_state == TCP_ESTABLISHED &&
808 ipv6_addr_equal(daddr, &np->daddr))
811 if (addr_len >= sizeof(struct sockaddr_in6) &&
812 sin6->sin6_scope_id &&
813 ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
814 fl.oif = sin6->sin6_scope_id;
816 if (sk->sk_state != TCP_ESTABLISHED)
817 return -EDESTADDRREQ;
819 fl.fl_ip_dport = inet->dport;
821 fl.fl6_flowlabel = np->flow_label;
826 fl.oif = sk->sk_bound_dev_if;
829 fl.oif = np->sticky_pktinfo.ipi6_ifindex;
831 if (msg->msg_controllen) {
833 memset(opt, 0, sizeof(struct ipv6_txoptions));
834 opt->tot_len = sizeof(*opt);
836 err = datagram_send_ctl(sock_net(sk), msg, &fl, opt, &hlimit, &tclass);
838 fl6_sock_release(flowlabel);
841 if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
842 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
843 if (flowlabel == NULL)
846 if (!(opt->opt_nflen|opt->opt_flen))
853 opt = fl6_merge_options(&opt_space, flowlabel, opt);
854 opt = ipv6_fixup_options(&opt_space, opt);
856 fl.proto = sk->sk_protocol;
857 if (!ipv6_addr_any(daddr))
858 ipv6_addr_copy(&fl.fl6_dst, daddr);
860 fl.fl6_dst.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
861 if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
862 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
863 fl.fl_ip_sport = inet->sport;
865 /* merge ip6_build_xmit from ip6_output */
866 if (opt && opt->srcrt) {
867 struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
868 ipv6_addr_copy(&final, &fl.fl6_dst);
869 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
874 if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst)) {
875 fl.oif = np->mcast_oif;
879 security_sk_classify_flow(sk, &fl);
881 err = ip6_sk_dst_lookup(sk, &dst, &fl);
885 ipv6_addr_copy(&fl.fl6_dst, final_p);
887 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
890 err = ip6_dst_blackhole(sk, &dst, &fl);
896 if (ipv6_addr_is_multicast(&fl.fl6_dst))
897 hlimit = np->mcast_hops;
899 hlimit = np->hop_limit;
901 hlimit = ip6_dst_hoplimit(dst);
910 if (msg->msg_flags&MSG_CONFIRM)
915 if (unlikely(up->pending)) {
916 /* The socket is already corked while preparing it. */
917 /* ... which is an evident application bug. --ANK */
920 LIMIT_NETDEBUG(KERN_DEBUG "udp cork app bug 2\n");
925 up->pending = AF_INET6;
929 getfrag = is_udplite ? udplite_getfrag : ip_generic_getfrag;
930 err = ip6_append_data(sk, getfrag, msg->msg_iov, ulen,
931 sizeof(struct udphdr), hlimit, tclass, opt, &fl,
932 (struct rt6_info*)dst,
933 corkreq ? msg->msg_flags|MSG_MORE : msg->msg_flags);
935 udp_v6_flush_pending_frames(sk);
937 err = udp_v6_push_pending_frames(sk);
938 else if (unlikely(skb_queue_empty(&sk->sk_write_queue)))
943 ip6_dst_store(sk, dst,
944 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
946 #ifdef CONFIG_IPV6_SUBTREES
947 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
958 err = np->recverr ? net_xmit_errno(err) : 0;
962 fl6_sock_release(flowlabel);
966 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
967 * ENOBUFS might not be good (it's not tunable per se), but otherwise
968 * we don't have a good statistic (IpOutDiscards but it can be too many
969 * things). We could add another new stat but at least for now that
970 * seems like overkill.
972 if (err == -ENOBUFS || test_bit(SOCK_NOSPACE, &sk->sk_socket->flags)) {
973 UDP6_INC_STATS_USER(sock_net(sk),
974 UDP_MIB_SNDBUFERRORS, is_udplite);
980 if (!(msg->msg_flags&MSG_PROBE) || len)
981 goto back_from_confirm;
986 void udpv6_destroy_sock(struct sock *sk)
989 udp_v6_flush_pending_frames(sk);
992 inet6_destroy_sock(sk);
996 * Socket option code for UDP
998 int udpv6_setsockopt(struct sock *sk, int level, int optname,
999 char __user *optval, int optlen)
1001 if (level == SOL_UDP || level == SOL_UDPLITE)
1002 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1003 udp_v6_push_pending_frames);
1004 return ipv6_setsockopt(sk, level, optname, optval, optlen);
1007 #ifdef CONFIG_COMPAT
1008 int compat_udpv6_setsockopt(struct sock *sk, int level, int optname,
1009 char __user *optval, int optlen)
1011 if (level == SOL_UDP || level == SOL_UDPLITE)
1012 return udp_lib_setsockopt(sk, level, optname, optval, optlen,
1013 udp_v6_push_pending_frames);
1014 return compat_ipv6_setsockopt(sk, level, optname, optval, optlen);
1018 int udpv6_getsockopt(struct sock *sk, int level, int optname,
1019 char __user *optval, int __user *optlen)
1021 if (level == SOL_UDP || level == SOL_UDPLITE)
1022 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1023 return ipv6_getsockopt(sk, level, optname, optval, optlen);
1026 #ifdef CONFIG_COMPAT
1027 int compat_udpv6_getsockopt(struct sock *sk, int level, int optname,
1028 char __user *optval, int __user *optlen)
1030 if (level == SOL_UDP || level == SOL_UDPLITE)
1031 return udp_lib_getsockopt(sk, level, optname, optval, optlen);
1032 return compat_ipv6_getsockopt(sk, level, optname, optval, optlen);
1036 static struct inet6_protocol udpv6_protocol = {
1037 .handler = udpv6_rcv,
1038 .err_handler = udpv6_err,
1039 .flags = INET6_PROTO_NOPOLICY|INET6_PROTO_FINAL,
1042 /* ------------------------------------------------------------------------ */
1043 #ifdef CONFIG_PROC_FS
1045 static void udp6_sock_seq_show(struct seq_file *seq, struct sock *sp, int bucket)
1047 struct inet_sock *inet = inet_sk(sp);
1048 struct ipv6_pinfo *np = inet6_sk(sp);
1049 struct in6_addr *dest, *src;
1053 src = &np->rcv_saddr;
1054 destp = ntohs(inet->dport);
1055 srcp = ntohs(inet->sport);
1057 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1058 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1060 src->s6_addr32[0], src->s6_addr32[1],
1061 src->s6_addr32[2], src->s6_addr32[3], srcp,
1062 dest->s6_addr32[0], dest->s6_addr32[1],
1063 dest->s6_addr32[2], dest->s6_addr32[3], destp,
1065 atomic_read(&sp->sk_wmem_alloc),
1066 atomic_read(&sp->sk_rmem_alloc),
1070 atomic_read(&sp->sk_refcnt), sp,
1071 atomic_read(&sp->sk_drops));
1074 int udp6_seq_show(struct seq_file *seq, void *v)
1076 if (v == SEQ_START_TOKEN)
1081 "st tx_queue rx_queue tr tm->when retrnsmt"
1082 " uid timeout inode ref pointer drops\n");
1084 udp6_sock_seq_show(seq, v, ((struct udp_iter_state *)seq->private)->bucket);
1088 static struct udp_seq_afinfo udp6_seq_afinfo = {
1091 .udp_table = &udp_table,
1093 .owner = THIS_MODULE,
1096 .show = udp6_seq_show,
1100 int udp6_proc_init(struct net *net)
1102 return udp_proc_register(net, &udp6_seq_afinfo);
1105 void udp6_proc_exit(struct net *net) {
1106 udp_proc_unregister(net, &udp6_seq_afinfo);
1108 #endif /* CONFIG_PROC_FS */
1110 /* ------------------------------------------------------------------------ */
1112 struct proto udpv6_prot = {
1114 .owner = THIS_MODULE,
1115 .close = udp_lib_close,
1116 .connect = ip6_datagram_connect,
1117 .disconnect = udp_disconnect,
1119 .destroy = udpv6_destroy_sock,
1120 .setsockopt = udpv6_setsockopt,
1121 .getsockopt = udpv6_getsockopt,
1122 .sendmsg = udpv6_sendmsg,
1123 .recvmsg = udpv6_recvmsg,
1124 .backlog_rcv = udpv6_queue_rcv_skb,
1125 .hash = udp_lib_hash,
1126 .unhash = udp_lib_unhash,
1127 .get_port = udp_v6_get_port,
1128 .memory_allocated = &udp_memory_allocated,
1129 .sysctl_mem = sysctl_udp_mem,
1130 .sysctl_wmem = &sysctl_udp_wmem_min,
1131 .sysctl_rmem = &sysctl_udp_rmem_min,
1132 .obj_size = sizeof(struct udp6_sock),
1133 .slab_flags = SLAB_DESTROY_BY_RCU,
1134 .h.udp_table = &udp_table,
1135 #ifdef CONFIG_COMPAT
1136 .compat_setsockopt = compat_udpv6_setsockopt,
1137 .compat_getsockopt = compat_udpv6_getsockopt,
1141 static struct inet_protosw udpv6_protosw = {
1143 .protocol = IPPROTO_UDP,
1144 .prot = &udpv6_prot,
1145 .ops = &inet6_dgram_ops,
1147 .no_check = UDP_CSUM_DEFAULT,
1148 .flags = INET_PROTOSW_PERMANENT,
1152 int __init udpv6_init(void)
1156 ret = inet6_add_protocol(&udpv6_protocol, IPPROTO_UDP);
1160 ret = inet6_register_protosw(&udpv6_protosw);
1162 goto out_udpv6_protocol;
1167 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);
1171 void udpv6_exit(void)
1173 inet6_unregister_protosw(&udpv6_protosw);
1174 inet6_del_protocol(&udpv6_protocol, IPPROTO_UDP);