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
)))
180 return __udp6_lib_lookup(dev_net(skb_dst(skb
)->dev
), &iph
->saddr
, sport
,
181 &iph
->daddr
, dport
, inet6_iif(skb
),
186 * This should be easy, if there is something there we
187 * return it, otherwise we block.
190 int udpv6_recvmsg(struct kiocb
*iocb
, struct sock
*sk
,
191 struct msghdr
*msg
, size_t len
,
192 int noblock
, int flags
, int *addr_len
)
194 struct ipv6_pinfo
*np
= inet6_sk(sk
);
195 struct inet_sock
*inet
= inet_sk(sk
);
197 unsigned int ulen
, copied
;
200 int is_udplite
= IS_UDPLITE(sk
);
204 *addr_len
=sizeof(struct sockaddr_in6
);
206 if (flags
& MSG_ERRQUEUE
)
207 return ipv6_recv_error(sk
, msg
, len
);
210 skb
= __skb_recv_datagram(sk
, flags
| (noblock
? MSG_DONTWAIT
: 0),
215 ulen
= skb
->len
- sizeof(struct udphdr
);
219 else if (copied
< ulen
)
220 msg
->msg_flags
|= MSG_TRUNC
;
222 is_udp4
= (skb
->protocol
== htons(ETH_P_IP
));
225 * If checksum is needed at all, try to do it while copying the
226 * data. If the data is truncated, or if we only want a partial
227 * coverage checksum (UDP-Lite), do it before the copy.
230 if (copied
< ulen
|| UDP_SKB_CB(skb
)->partial_cov
) {
231 if (udp_lib_checksum_complete(skb
))
235 if (skb_csum_unnecessary(skb
))
236 err
= skb_copy_datagram_iovec(skb
, sizeof(struct udphdr
),
237 msg
->msg_iov
, copied
);
239 err
= skb_copy_and_csum_datagram_iovec(skb
, sizeof(struct udphdr
), msg
->msg_iov
);
248 UDP_INC_STATS_USER(sock_net(sk
),
249 UDP_MIB_INDATAGRAMS
, is_udplite
);
251 UDP6_INC_STATS_USER(sock_net(sk
),
252 UDP_MIB_INDATAGRAMS
, is_udplite
);
255 sock_recv_timestamp(msg
, sk
, skb
);
257 /* Copy the address. */
259 struct sockaddr_in6
*sin6
;
261 sin6
= (struct sockaddr_in6
*) msg
->msg_name
;
262 sin6
->sin6_family
= AF_INET6
;
263 sin6
->sin6_port
= udp_hdr(skb
)->source
;
264 sin6
->sin6_flowinfo
= 0;
265 sin6
->sin6_scope_id
= 0;
268 ipv6_addr_set(&sin6
->sin6_addr
, 0, 0,
269 htonl(0xffff), ip_hdr(skb
)->saddr
);
271 ipv6_addr_copy(&sin6
->sin6_addr
,
272 &ipv6_hdr(skb
)->saddr
);
273 if (ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
274 sin6
->sin6_scope_id
= IP6CB(skb
)->iif
;
279 if (inet
->cmsg_flags
)
280 ip_cmsg_recv(msg
, skb
);
283 datagram_recv_ctl(sk
, msg
, skb
);
287 if (flags
& MSG_TRUNC
)
292 skb_free_datagram(sk
, skb
);
299 if (!skb_kill_datagram(sk
, skb
, flags
)) {
301 UDP_INC_STATS_USER(sock_net(sk
),
302 UDP_MIB_INERRORS
, is_udplite
);
304 UDP6_INC_STATS_USER(sock_net(sk
),
305 UDP_MIB_INERRORS
, is_udplite
);
309 if (flags
& MSG_DONTWAIT
)
314 void __udp6_lib_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
315 int type
, int code
, int offset
, __be32 info
,
316 struct udp_table
*udptable
)
318 struct ipv6_pinfo
*np
;
319 struct ipv6hdr
*hdr
= (struct ipv6hdr
*)skb
->data
;
320 struct in6_addr
*saddr
= &hdr
->saddr
;
321 struct in6_addr
*daddr
= &hdr
->daddr
;
322 struct udphdr
*uh
= (struct udphdr
*)(skb
->data
+offset
);
326 sk
= __udp6_lib_lookup(dev_net(skb
->dev
), daddr
, uh
->dest
,
327 saddr
, uh
->source
, inet6_iif(skb
), udptable
);
333 if (!icmpv6_err_convert(type
, code
, &err
) && !np
->recverr
)
336 if (sk
->sk_state
!= TCP_ESTABLISHED
&& !np
->recverr
)
340 ipv6_icmp_error(sk
, skb
, err
, uh
->dest
, ntohl(info
), (u8
*)(uh
+1));
343 sk
->sk_error_report(sk
);
348 static __inline__
void udpv6_err(struct sk_buff
*skb
,
349 struct inet6_skb_parm
*opt
, int type
,
350 int code
, int offset
, __be32 info
)
352 __udp6_lib_err(skb
, opt
, type
, code
, offset
, info
, &udp_table
);
355 int udpv6_queue_rcv_skb(struct sock
* sk
, struct sk_buff
*skb
)
357 struct udp_sock
*up
= udp_sk(sk
);
359 int is_udplite
= IS_UDPLITE(sk
);
361 if (!xfrm6_policy_check(sk
, XFRM_POLICY_IN
, skb
))
365 * UDP-Lite specific tests, ignored on UDP sockets (see net/ipv4/udp.c).
367 if ((is_udplite
& UDPLITE_RECV_CC
) && UDP_SKB_CB(skb
)->partial_cov
) {
369 if (up
->pcrlen
== 0) { /* full coverage was set */
370 LIMIT_NETDEBUG(KERN_WARNING
"UDPLITE6: partial coverage"
371 " %d while full coverage %d requested\n",
372 UDP_SKB_CB(skb
)->cscov
, skb
->len
);
375 if (UDP_SKB_CB(skb
)->cscov
< up
->pcrlen
) {
376 LIMIT_NETDEBUG(KERN_WARNING
"UDPLITE6: coverage %d "
377 "too small, need min %d\n",
378 UDP_SKB_CB(skb
)->cscov
, up
->pcrlen
);
384 if (udp_lib_checksum_complete(skb
))
388 if ((rc
= sock_queue_rcv_skb(sk
,skb
)) < 0) {
389 /* Note that an ENOMEM error is charged twice */
391 UDP6_INC_STATS_BH(sock_net(sk
),
392 UDP_MIB_RCVBUFERRORS
, is_udplite
);
393 atomic_inc(&sk
->sk_drops
);
400 UDP6_INC_STATS_BH(sock_net(sk
), UDP_MIB_INERRORS
, is_udplite
);
405 static struct sock
*udp_v6_mcast_next(struct net
*net
, struct sock
*sk
,
406 __be16 loc_port
, struct in6_addr
*loc_addr
,
407 __be16 rmt_port
, struct in6_addr
*rmt_addr
,
410 struct hlist_nulls_node
*node
;
412 unsigned short num
= ntohs(loc_port
);
414 sk_nulls_for_each_from(s
, node
) {
415 struct inet_sock
*inet
= inet_sk(s
);
417 if (!net_eq(sock_net(s
), net
))
420 if (s
->sk_hash
== num
&& s
->sk_family
== PF_INET6
) {
421 struct ipv6_pinfo
*np
= inet6_sk(s
);
423 if (inet
->dport
!= rmt_port
)
426 if (!ipv6_addr_any(&np
->daddr
) &&
427 !ipv6_addr_equal(&np
->daddr
, rmt_addr
))
430 if (s
->sk_bound_dev_if
&& s
->sk_bound_dev_if
!= dif
)
433 if (!ipv6_addr_any(&np
->rcv_saddr
)) {
434 if (!ipv6_addr_equal(&np
->rcv_saddr
, loc_addr
))
437 if (!inet6_mc_check(s
, loc_addr
, rmt_addr
))
446 * Note: called only from the BH handler context,
447 * so we don't need to lock the hashes.
449 static int __udp6_lib_mcast_deliver(struct net
*net
, struct sk_buff
*skb
,
450 struct in6_addr
*saddr
, struct in6_addr
*daddr
,
451 struct udp_table
*udptable
)
453 struct sock
*sk
, *sk2
;
454 const struct udphdr
*uh
= udp_hdr(skb
);
455 struct udp_hslot
*hslot
= &udptable
->hash
[udp_hashfn(net
, ntohs(uh
->dest
))];
458 spin_lock(&hslot
->lock
);
459 sk
= sk_nulls_head(&hslot
->head
);
460 dif
= inet6_iif(skb
);
461 sk
= udp_v6_mcast_next(net
, sk
, uh
->dest
, daddr
, uh
->source
, saddr
, dif
);
468 while ((sk2
= udp_v6_mcast_next(net
, sk_nulls_next(sk2
), uh
->dest
, daddr
,
469 uh
->source
, saddr
, dif
))) {
470 struct sk_buff
*buff
= skb_clone(skb
, GFP_ATOMIC
);
473 if (!sock_owned_by_user(sk2
))
474 udpv6_queue_rcv_skb(sk2
, buff
);
476 sk_add_backlog(sk2
, buff
);
481 if (!sock_owned_by_user(sk
))
482 udpv6_queue_rcv_skb(sk
, skb
);
484 sk_add_backlog(sk
, skb
);
487 spin_unlock(&hslot
->lock
);
491 static inline int udp6_csum_init(struct sk_buff
*skb
, struct udphdr
*uh
,
496 UDP_SKB_CB(skb
)->partial_cov
= 0;
497 UDP_SKB_CB(skb
)->cscov
= skb
->len
;
499 if (proto
== IPPROTO_UDPLITE
) {
500 err
= udplite_checksum_init(skb
, uh
);
505 if (uh
->check
== 0) {
506 /* RFC 2460 section 8.1 says that we SHOULD log
507 this error. Well, it is reasonable.
509 LIMIT_NETDEBUG(KERN_INFO
"IPv6: udp checksum is 0\n");
512 if (skb
->ip_summed
== CHECKSUM_COMPLETE
&&
513 !csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
, &ipv6_hdr(skb
)->daddr
,
514 skb
->len
, proto
, skb
->csum
))
515 skb
->ip_summed
= CHECKSUM_UNNECESSARY
;
517 if (!skb_csum_unnecessary(skb
))
518 skb
->csum
= ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb
)->saddr
,
519 &ipv6_hdr(skb
)->daddr
,
520 skb
->len
, proto
, 0));
525 int __udp6_lib_rcv(struct sk_buff
*skb
, struct udp_table
*udptable
,
530 struct net_device
*dev
= skb
->dev
;
531 struct in6_addr
*saddr
, *daddr
;
533 struct net
*net
= dev_net(skb
->dev
);
535 if (!pskb_may_pull(skb
, sizeof(struct udphdr
)))
538 saddr
= &ipv6_hdr(skb
)->saddr
;
539 daddr
= &ipv6_hdr(skb
)->daddr
;
542 ulen
= ntohs(uh
->len
);
546 if (proto
== IPPROTO_UDP
) {
547 /* UDP validates ulen. */
549 /* Check for jumbo payload */
553 if (ulen
< sizeof(*uh
))
556 if (ulen
< skb
->len
) {
557 if (pskb_trim_rcsum(skb
, ulen
))
559 saddr
= &ipv6_hdr(skb
)->saddr
;
560 daddr
= &ipv6_hdr(skb
)->daddr
;
565 if (udp6_csum_init(skb
, uh
, proto
))
569 * Multicast receive code
571 if (ipv6_addr_is_multicast(daddr
))
572 return __udp6_lib_mcast_deliver(net
, skb
,
573 saddr
, daddr
, udptable
);
578 * check socket cache ... must talk to Alan about his plans
579 * for sock caches... i'll skip this for now.
581 sk
= __udp6_lib_lookup_skb(skb
, uh
->source
, uh
->dest
, udptable
);
584 if (!xfrm6_policy_check(NULL
, XFRM_POLICY_IN
, skb
))
587 if (udp_lib_checksum_complete(skb
))
589 UDP6_INC_STATS_BH(net
, UDP_MIB_NOPORTS
,
590 proto
== IPPROTO_UDPLITE
);
592 icmpv6_send(skb
, ICMPV6_DEST_UNREACH
, ICMPV6_PORT_UNREACH
, 0, dev
);
601 if (!sock_owned_by_user(sk
))
602 udpv6_queue_rcv_skb(sk
, skb
);
604 sk_add_backlog(sk
, skb
);
610 LIMIT_NETDEBUG(KERN_DEBUG
"UDP%sv6: short packet: %d/%u\n",
611 proto
== IPPROTO_UDPLITE
? "-Lite" : "",
615 UDP6_INC_STATS_BH(net
, UDP_MIB_INERRORS
, proto
== IPPROTO_UDPLITE
);
620 static __inline__
int udpv6_rcv(struct sk_buff
*skb
)
622 return __udp6_lib_rcv(skb
, &udp_table
, IPPROTO_UDP
);
626 * Throw away all pending data and cancel the corking. Socket is locked.
628 static void udp_v6_flush_pending_frames(struct sock
*sk
)
630 struct udp_sock
*up
= udp_sk(sk
);
632 if (up
->pending
== AF_INET
)
633 udp_flush_pending_frames(sk
);
634 else if (up
->pending
) {
637 ip6_flush_pending_frames(sk
);
645 static int udp_v6_push_pending_frames(struct sock
*sk
)
649 struct udp_sock
*up
= udp_sk(sk
);
650 struct inet_sock
*inet
= inet_sk(sk
);
651 struct flowi
*fl
= &inet
->cork
.fl
;
653 int is_udplite
= IS_UDPLITE(sk
);
656 /* Grab the skbuff where UDP header space exists. */
657 if ((skb
= skb_peek(&sk
->sk_write_queue
)) == NULL
)
661 * Create a UDP header
664 uh
->source
= fl
->fl_ip_sport
;
665 uh
->dest
= fl
->fl_ip_dport
;
666 uh
->len
= htons(up
->len
);
670 csum
= udplite_csum_outgoing(sk
, skb
);
672 csum
= udp_csum_outgoing(sk
, skb
);
674 /* add protocol-dependent pseudo-header */
675 uh
->check
= csum_ipv6_magic(&fl
->fl6_src
, &fl
->fl6_dst
,
676 up
->len
, fl
->proto
, csum
);
678 uh
->check
= CSUM_MANGLED_0
;
680 err
= ip6_push_pending_frames(sk
);
685 UDP6_INC_STATS_USER(sock_net(sk
),
686 UDP_MIB_OUTDATAGRAMS
, is_udplite
);
690 int udpv6_sendmsg(struct kiocb
*iocb
, struct sock
*sk
,
691 struct msghdr
*msg
, size_t len
)
693 struct ipv6_txoptions opt_space
;
694 struct udp_sock
*up
= udp_sk(sk
);
695 struct inet_sock
*inet
= inet_sk(sk
);
696 struct ipv6_pinfo
*np
= inet6_sk(sk
);
697 struct sockaddr_in6
*sin6
= (struct sockaddr_in6
*) msg
->msg_name
;
698 struct in6_addr
*daddr
, *final_p
= NULL
, final
;
699 struct ipv6_txoptions
*opt
= NULL
;
700 struct ip6_flowlabel
*flowlabel
= NULL
;
702 struct dst_entry
*dst
;
703 int addr_len
= msg
->msg_namelen
;
707 int corkreq
= up
->corkflag
|| msg
->msg_flags
&MSG_MORE
;
710 int is_udplite
= IS_UDPLITE(sk
);
711 int (*getfrag
)(void *, char *, int, int, int, struct sk_buff
*);
713 /* destination address check */
715 if (addr_len
< offsetof(struct sockaddr
, sa_data
))
718 switch (sin6
->sin6_family
) {
720 if (addr_len
< SIN6_LEN_RFC2133
)
722 daddr
= &sin6
->sin6_addr
;
727 msg
->msg_name
= sin6
= NULL
;
728 msg
->msg_namelen
= addr_len
= 0;
734 } else if (!up
->pending
) {
735 if (sk
->sk_state
!= TCP_ESTABLISHED
)
736 return -EDESTADDRREQ
;
742 if (ipv6_addr_v4mapped(daddr
)) {
743 struct sockaddr_in sin
;
744 sin
.sin_family
= AF_INET
;
745 sin
.sin_port
= sin6
? sin6
->sin6_port
: inet
->dport
;
746 sin
.sin_addr
.s_addr
= daddr
->s6_addr32
[3];
747 msg
->msg_name
= &sin
;
748 msg
->msg_namelen
= sizeof(sin
);
750 if (__ipv6_only_sock(sk
))
752 return udp_sendmsg(iocb
, sk
, msg
, len
);
756 if (up
->pending
== AF_INET
)
757 return udp_sendmsg(iocb
, sk
, msg
, len
);
759 /* Rough check on arithmetic overflow,
760 better check is made in ip6_append_data().
762 if (len
> INT_MAX
- sizeof(struct udphdr
))
767 * There are pending frames.
768 * The socket lock must be held while it's corked.
771 if (likely(up
->pending
)) {
772 if (unlikely(up
->pending
!= AF_INET6
)) {
774 return -EAFNOSUPPORT
;
781 ulen
+= sizeof(struct udphdr
);
783 memset(&fl
, 0, sizeof(fl
));
786 if (sin6
->sin6_port
== 0)
789 fl
.fl_ip_dport
= sin6
->sin6_port
;
790 daddr
= &sin6
->sin6_addr
;
793 fl
.fl6_flowlabel
= sin6
->sin6_flowinfo
&IPV6_FLOWINFO_MASK
;
794 if (fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) {
795 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
796 if (flowlabel
== NULL
)
798 daddr
= &flowlabel
->dst
;
803 * Otherwise it will be difficult to maintain
806 if (sk
->sk_state
== TCP_ESTABLISHED
&&
807 ipv6_addr_equal(daddr
, &np
->daddr
))
810 if (addr_len
>= sizeof(struct sockaddr_in6
) &&
811 sin6
->sin6_scope_id
&&
812 ipv6_addr_type(daddr
)&IPV6_ADDR_LINKLOCAL
)
813 fl
.oif
= sin6
->sin6_scope_id
;
815 if (sk
->sk_state
!= TCP_ESTABLISHED
)
816 return -EDESTADDRREQ
;
818 fl
.fl_ip_dport
= inet
->dport
;
820 fl
.fl6_flowlabel
= np
->flow_label
;
825 fl
.oif
= sk
->sk_bound_dev_if
;
828 fl
.oif
= np
->sticky_pktinfo
.ipi6_ifindex
;
830 if (msg
->msg_controllen
) {
832 memset(opt
, 0, sizeof(struct ipv6_txoptions
));
833 opt
->tot_len
= sizeof(*opt
);
835 err
= datagram_send_ctl(sock_net(sk
), msg
, &fl
, opt
, &hlimit
, &tclass
);
837 fl6_sock_release(flowlabel
);
840 if ((fl
.fl6_flowlabel
&IPV6_FLOWLABEL_MASK
) && !flowlabel
) {
841 flowlabel
= fl6_sock_lookup(sk
, fl
.fl6_flowlabel
);
842 if (flowlabel
== NULL
)
845 if (!(opt
->opt_nflen
|opt
->opt_flen
))
852 opt
= fl6_merge_options(&opt_space
, flowlabel
, opt
);
853 opt
= ipv6_fixup_options(&opt_space
, opt
);
855 fl
.proto
= sk
->sk_protocol
;
856 if (!ipv6_addr_any(daddr
))
857 ipv6_addr_copy(&fl
.fl6_dst
, daddr
);
859 fl
.fl6_dst
.s6_addr
[15] = 0x1; /* :: means loopback (BSD'ism) */
860 if (ipv6_addr_any(&fl
.fl6_src
) && !ipv6_addr_any(&np
->saddr
))
861 ipv6_addr_copy(&fl
.fl6_src
, &np
->saddr
);
862 fl
.fl_ip_sport
= inet
->sport
;
864 /* merge ip6_build_xmit from ip6_output */
865 if (opt
&& opt
->srcrt
) {
866 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) opt
->srcrt
;
867 ipv6_addr_copy(&final
, &fl
.fl6_dst
);
868 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
873 if (!fl
.oif
&& ipv6_addr_is_multicast(&fl
.fl6_dst
)) {
874 fl
.oif
= np
->mcast_oif
;
878 security_sk_classify_flow(sk
, &fl
);
880 err
= ip6_sk_dst_lookup(sk
, &dst
, &fl
);
884 ipv6_addr_copy(&fl
.fl6_dst
, final_p
);
886 err
= __xfrm_lookup(sock_net(sk
), &dst
, &fl
, sk
, XFRM_LOOKUP_WAIT
);
889 err
= ip6_dst_blackhole(sk
, &dst
, &fl
);
895 if (ipv6_addr_is_multicast(&fl
.fl6_dst
))
896 hlimit
= np
->mcast_hops
;
898 hlimit
= np
->hop_limit
;
900 hlimit
= ip6_dst_hoplimit(dst
);
909 if (msg
->msg_flags
&MSG_CONFIRM
)
914 if (unlikely(up
->pending
)) {
915 /* The socket is already corked while preparing it. */
916 /* ... which is an evident application bug. --ANK */
919 LIMIT_NETDEBUG(KERN_DEBUG
"udp cork app bug 2\n");
924 up
->pending
= AF_INET6
;
928 getfrag
= is_udplite
? udplite_getfrag
: ip_generic_getfrag
;
929 err
= ip6_append_data(sk
, getfrag
, msg
->msg_iov
, ulen
,
930 sizeof(struct udphdr
), hlimit
, tclass
, opt
, &fl
,
931 (struct rt6_info
*)dst
,
932 corkreq
? msg
->msg_flags
|MSG_MORE
: msg
->msg_flags
);
934 udp_v6_flush_pending_frames(sk
);
936 err
= udp_v6_push_pending_frames(sk
);
937 else if (unlikely(skb_queue_empty(&sk
->sk_write_queue
)))
942 ip6_dst_store(sk
, dst
,
943 ipv6_addr_equal(&fl
.fl6_dst
, &np
->daddr
) ?
945 #ifdef CONFIG_IPV6_SUBTREES
946 ipv6_addr_equal(&fl
.fl6_src
, &np
->saddr
) ?
957 err
= np
->recverr
? net_xmit_errno(err
) : 0;
961 fl6_sock_release(flowlabel
);
965 * ENOBUFS = no kernel mem, SOCK_NOSPACE = no sndbuf space. Reporting
966 * ENOBUFS might not be good (it's not tunable per se), but otherwise
967 * we don't have a good statistic (IpOutDiscards but it can be too many
968 * things). We could add another new stat but at least for now that
969 * seems like overkill.
971 if (err
== -ENOBUFS
|| test_bit(SOCK_NOSPACE
, &sk
->sk_socket
->flags
)) {
972 UDP6_INC_STATS_USER(sock_net(sk
),
973 UDP_MIB_SNDBUFERRORS
, is_udplite
);
979 if (!(msg
->msg_flags
&MSG_PROBE
) || len
)
980 goto back_from_confirm
;
985 void udpv6_destroy_sock(struct sock
*sk
)
988 udp_v6_flush_pending_frames(sk
);
991 inet6_destroy_sock(sk
);
995 * Socket option code for UDP
997 int udpv6_setsockopt(struct sock
*sk
, int level
, int optname
,
998 char __user
*optval
, int optlen
)
1000 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1001 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1002 udp_v6_push_pending_frames
);
1003 return ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1006 #ifdef CONFIG_COMPAT
1007 int compat_udpv6_setsockopt(struct sock
*sk
, int level
, int optname
,
1008 char __user
*optval
, int optlen
)
1010 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1011 return udp_lib_setsockopt(sk
, level
, optname
, optval
, optlen
,
1012 udp_v6_push_pending_frames
);
1013 return compat_ipv6_setsockopt(sk
, level
, optname
, optval
, optlen
);
1017 int udpv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1018 char __user
*optval
, int __user
*optlen
)
1020 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1021 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1022 return ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1025 #ifdef CONFIG_COMPAT
1026 int compat_udpv6_getsockopt(struct sock
*sk
, int level
, int optname
,
1027 char __user
*optval
, int __user
*optlen
)
1029 if (level
== SOL_UDP
|| level
== SOL_UDPLITE
)
1030 return udp_lib_getsockopt(sk
, level
, optname
, optval
, optlen
);
1031 return compat_ipv6_getsockopt(sk
, level
, optname
, optval
, optlen
);
1035 static struct inet6_protocol udpv6_protocol
= {
1036 .handler
= udpv6_rcv
,
1037 .err_handler
= udpv6_err
,
1038 .flags
= INET6_PROTO_NOPOLICY
|INET6_PROTO_FINAL
,
1041 /* ------------------------------------------------------------------------ */
1042 #ifdef CONFIG_PROC_FS
1044 static void udp6_sock_seq_show(struct seq_file
*seq
, struct sock
*sp
, int bucket
)
1046 struct inet_sock
*inet
= inet_sk(sp
);
1047 struct ipv6_pinfo
*np
= inet6_sk(sp
);
1048 struct in6_addr
*dest
, *src
;
1052 src
= &np
->rcv_saddr
;
1053 destp
= ntohs(inet
->dport
);
1054 srcp
= ntohs(inet
->sport
);
1056 "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
1057 "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d\n",
1059 src
->s6_addr32
[0], src
->s6_addr32
[1],
1060 src
->s6_addr32
[2], src
->s6_addr32
[3], srcp
,
1061 dest
->s6_addr32
[0], dest
->s6_addr32
[1],
1062 dest
->s6_addr32
[2], dest
->s6_addr32
[3], destp
,
1064 sk_wmem_alloc_get(sp
),
1065 sk_rmem_alloc_get(sp
),
1069 atomic_read(&sp
->sk_refcnt
), sp
,
1070 atomic_read(&sp
->sk_drops
));
1073 int udp6_seq_show(struct seq_file
*seq
, void *v
)
1075 if (v
== SEQ_START_TOKEN
)
1080 "st tx_queue rx_queue tr tm->when retrnsmt"
1081 " uid timeout inode ref pointer drops\n");
1083 udp6_sock_seq_show(seq
, v
, ((struct udp_iter_state
*)seq
->private)->bucket
);
1087 static struct udp_seq_afinfo udp6_seq_afinfo
= {
1090 .udp_table
= &udp_table
,
1092 .owner
= THIS_MODULE
,
1095 .show
= udp6_seq_show
,
1099 int udp6_proc_init(struct net
*net
)
1101 return udp_proc_register(net
, &udp6_seq_afinfo
);
1104 void udp6_proc_exit(struct net
*net
) {
1105 udp_proc_unregister(net
, &udp6_seq_afinfo
);
1107 #endif /* CONFIG_PROC_FS */
1109 /* ------------------------------------------------------------------------ */
1111 struct proto udpv6_prot
= {
1113 .owner
= THIS_MODULE
,
1114 .close
= udp_lib_close
,
1115 .connect
= ip6_datagram_connect
,
1116 .disconnect
= udp_disconnect
,
1118 .destroy
= udpv6_destroy_sock
,
1119 .setsockopt
= udpv6_setsockopt
,
1120 .getsockopt
= udpv6_getsockopt
,
1121 .sendmsg
= udpv6_sendmsg
,
1122 .recvmsg
= udpv6_recvmsg
,
1123 .backlog_rcv
= udpv6_queue_rcv_skb
,
1124 .hash
= udp_lib_hash
,
1125 .unhash
= udp_lib_unhash
,
1126 .get_port
= udp_v6_get_port
,
1127 .memory_allocated
= &udp_memory_allocated
,
1128 .sysctl_mem
= sysctl_udp_mem
,
1129 .sysctl_wmem
= &sysctl_udp_wmem_min
,
1130 .sysctl_rmem
= &sysctl_udp_rmem_min
,
1131 .obj_size
= sizeof(struct udp6_sock
),
1132 .slab_flags
= SLAB_DESTROY_BY_RCU
,
1133 .h
.udp_table
= &udp_table
,
1134 #ifdef CONFIG_COMPAT
1135 .compat_setsockopt
= compat_udpv6_setsockopt
,
1136 .compat_getsockopt
= compat_udpv6_getsockopt
,
1140 static struct inet_protosw udpv6_protosw
= {
1142 .protocol
= IPPROTO_UDP
,
1143 .prot
= &udpv6_prot
,
1144 .ops
= &inet6_dgram_ops
,
1146 .no_check
= UDP_CSUM_DEFAULT
,
1147 .flags
= INET_PROTOSW_PERMANENT
,
1151 int __init
udpv6_init(void)
1155 ret
= inet6_add_protocol(&udpv6_protocol
, IPPROTO_UDP
);
1159 ret
= inet6_register_protosw(&udpv6_protosw
);
1161 goto out_udpv6_protocol
;
1166 inet6_del_protocol(&udpv6_protocol
, IPPROTO_UDP
);
1170 void udpv6_exit(void)
1172 inet6_unregister_protosw(&udpv6_protosw
);
1173 inet6_del_protocol(&udpv6_protocol
, IPPROTO_UDP
);