2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the project nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
30 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
34 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
35 * The Regents of the University of California.
36 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
65 #include <sys/cdefs.h>
66 __FBSDID("$FreeBSD$");
69 #include "opt_inet6.h"
70 #include "opt_ipsec.h"
73 #include <sys/param.h>
74 #include <sys/kernel.h>
79 #include <sys/protosw.h>
80 #include <sys/signalvar.h>
81 #include <sys/socket.h>
82 #include <sys/socketvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/syslog.h>
86 #include <sys/systm.h>
87 #include <sys/vimage.h>
90 #include <net/if_types.h>
91 #include <net/route.h>
93 #include <netinet/in.h>
94 #include <netinet/in_pcb.h>
95 #include <netinet/in_systm.h>
96 #include <netinet/in_var.h>
97 #include <netinet/ip.h>
98 #include <netinet/ip_icmp.h>
99 #include <netinet/ip6.h>
100 #include <netinet/icmp_var.h>
101 #include <netinet/icmp6.h>
102 #include <netinet/ip_var.h>
103 #include <netinet/udp.h>
104 #include <netinet/udp_var.h>
105 #include <netinet6/ip6protosw.h>
106 #include <netinet6/ip6_var.h>
107 #include <netinet6/in6_pcb.h>
108 #include <netinet6/udp6_var.h>
109 #include <netinet6/scope6_var.h>
112 #include <netipsec/ipsec.h>
113 #include <netipsec/ipsec6.h>
116 #include <security/mac/mac_framework.h>
119 * UDP protocol implementation.
120 * Per RFC 768, August, 1980.
123 extern struct protosw inetsw[];
124 static void udp6_detach(struct socket *so);
127 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
128 struct sockaddr_in6 *fromsa)
130 INIT_VNET_INET(inp->inp_vnet);
134 INP_LOCK_ASSERT(inp);
137 /* Check AH/ESP integrity. */
138 if (ipsec6_in_reject(n, inp)) {
139 INIT_VNET_IPSEC(inp->inp_vnet);
141 V_ipsec6stat.in_polvio++;
146 if (mac_inpcb_check_deliver(inp, n) != 0) {
152 if (inp->in6p_flags & IN6P_CONTROLOPTS ||
153 inp->inp_socket->so_options & SO_TIMESTAMP)
154 ip6_savecontrol(inp, n, &opts);
155 m_adj(n, off + sizeof(struct udphdr));
157 so = inp->inp_socket;
158 SOCKBUF_LOCK(&so->so_rcv);
159 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
161 SOCKBUF_UNLOCK(&so->so_rcv);
165 V_udpstat.udps_fullsock++;
167 sorwakeup_locked(so);
171 udp6_input(struct mbuf **mp, int *offp, int proto)
173 INIT_VNET_INET(curvnet);
174 INIT_VNET_INET6(curvnet);
175 struct mbuf *m = *mp;
181 struct sockaddr_in6 fromsa;
183 ip6 = mtod(m, struct ip6_hdr *);
185 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
186 /* XXX send icmp6 host/port unreach? */
188 return (IPPROTO_DONE);
191 #ifndef PULLDOWN_TEST
192 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
193 ip6 = mtod(m, struct ip6_hdr *);
194 uh = (struct udphdr *)((caddr_t)ip6 + off);
196 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
198 return (IPPROTO_DONE);
201 V_udpstat.udps_ipackets++;
204 * Destination port of 0 is illegal, based on RFC768.
206 if (uh->uh_dport == 0)
209 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
210 ulen = ntohs((u_short)uh->uh_ulen);
213 V_udpstat.udps_badlen++;
218 * Checksum extended UDP header and data.
220 if (uh->uh_sum == 0) {
221 V_udpstat.udps_nosum++;
224 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
225 V_udpstat.udps_badsum++;
230 * Construct sockaddr format source address.
232 init_sin6(&fromsa, m);
233 fromsa.sin6_port = uh->uh_sport;
235 INP_INFO_RLOCK(&V_udbinfo);
236 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
240 * In the event that laddr should be set to the link-local
241 * address (this happens in RIPng), the multicast address
242 * specified in the received packet will not match laddr. To
243 * handle this situation, matching is relaxed if the
244 * receiving interface is the same as one specified in the
245 * socket and if the destination multicast address matches
246 * one of the multicast groups specified in the socket.
250 * KAME note: traditionally we dropped udpiphdr from mbuf
251 * here. We need udphdr for IPsec processing so we do that
255 LIST_FOREACH(inp, &V_udb, inp_list) {
256 if ((inp->inp_vflag & INP_IPV6) == 0)
258 if (inp->in6p_lport != uh->uh_dport)
261 * XXX: Do not check source port of incoming datagram
262 * unless inp_connect() has been called to bind the
263 * fport part of the 4-tuple; the source could be
264 * trying to talk to us with an ephemeral port.
266 if (inp->inp_fport != 0 &&
267 inp->inp_fport != uh->uh_sport)
269 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
270 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
274 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
275 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
277 inp->in6p_fport != uh->uh_sport)
284 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
286 udp6_append(last, n, off, &fromsa);
292 * Don't look for additional matches if this one does
293 * not have either the SO_REUSEPORT or SO_REUSEADDR
294 * socket options set. This heuristic avoids
295 * searching through all pcbs in the common case of a
296 * non-shared port. It assumes that an application
297 * will never clear these options after setting them.
299 if ((last->inp_socket->so_options &
300 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
306 * No matching pcb found; discard datagram. (No need
307 * to send an ICMP Port Unreachable for a broadcast
308 * or multicast datgram.)
310 V_udpstat.udps_noport++;
311 V_udpstat.udps_noportmcast++;
315 INP_INFO_RUNLOCK(&V_udbinfo);
316 udp6_append(last, m, off, &fromsa);
318 return (IPPROTO_DONE);
321 * Locate pcb for datagram.
323 inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
324 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
326 if (udp_log_in_vain) {
327 char ip6bufs[INET6_ADDRSTRLEN];
328 char ip6bufd[INET6_ADDRSTRLEN];
331 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
332 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
334 ip6_sprintf(ip6bufs, &ip6->ip6_src),
335 ntohs(uh->uh_sport));
337 V_udpstat.udps_noport++;
338 if (m->m_flags & M_MCAST) {
339 printf("UDP6: M_MCAST is set in a unicast packet.\n");
340 V_udpstat.udps_noportmcast++;
343 INP_INFO_RUNLOCK(&V_udbinfo);
346 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
348 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
349 return (IPPROTO_DONE);
352 INP_INFO_RUNLOCK(&V_udbinfo);
353 udp6_append(inp, m, off, &fromsa);
355 return (IPPROTO_DONE);
358 INP_INFO_RUNLOCK(&V_udbinfo);
362 return (IPPROTO_DONE);
366 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
368 INIT_VNET_INET(curvnet);
373 struct ip6ctlparam *ip6cp = NULL;
374 const struct sockaddr_in6 *sa6_src = NULL;
376 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
377 struct udp_portonly {
382 if (sa->sa_family != AF_INET6 ||
383 sa->sa_len != sizeof(struct sockaddr_in6))
386 if ((unsigned)cmd >= PRC_NCMDS)
388 if (PRC_IS_REDIRECT(cmd))
389 notify = in6_rtchange, d = NULL;
390 else if (cmd == PRC_HOSTDEAD)
392 else if (inet6ctlerrmap[cmd] == 0)
395 /* if the parameter is from icmp6, decode it. */
397 ip6cp = (struct ip6ctlparam *)d;
399 ip6 = ip6cp->ip6c_ip6;
400 off = ip6cp->ip6c_off;
401 cmdarg = ip6cp->ip6c_cmdarg;
402 sa6_src = ip6cp->ip6c_src;
412 * XXX: We assume that when IPV6 is non NULL,
413 * M and OFF are valid.
416 /* Check if we can safely examine src and dst ports. */
417 if (m->m_pkthdr.len < off + sizeof(*uhp))
420 bzero(&uh, sizeof(uh));
421 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
423 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
424 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
427 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
428 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
432 udp6_getcred(SYSCTL_HANDLER_ARGS)
434 INIT_VNET_INET(curvnet);
435 INIT_VNET_INET6(curvnet);
437 struct sockaddr_in6 addrs[2];
441 error = priv_check(req->td, PRIV_NETINET_GETCRED);
445 if (req->newlen != sizeof(addrs))
447 if (req->oldlen != sizeof(struct xucred))
449 error = SYSCTL_IN(req, addrs, sizeof(addrs));
452 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
453 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
456 INP_INFO_RLOCK(&V_udbinfo);
457 inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
458 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
462 INP_INFO_RUNLOCK(&V_udbinfo);
463 if (inp->inp_socket == NULL)
466 error = cr_canseesocket(req->td->td_ucred,
469 cru2x(inp->inp_cred, &xuc);
472 INP_INFO_RUNLOCK(&V_udbinfo);
476 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
480 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
481 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
484 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
485 struct mbuf *control, struct thread *td)
487 INIT_VNET_INET(curvnet);
488 INIT_VNET_INET6(curvnet);
489 u_int32_t ulen = m->m_pkthdr.len;
490 u_int32_t plen = sizeof(struct udphdr) + ulen;
493 struct in6_addr *laddr, *faddr;
494 struct sockaddr_in6 *sin6 = NULL;
495 struct ifnet *oifp = NULL;
496 int scope_ambiguous = 0;
499 struct ip6_pktopts *optp, opt;
500 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
502 struct sockaddr_in6 tmp;
504 INP_WLOCK_ASSERT(inp);
507 /* addr6 has been validated in udp6_send(). */
508 sin6 = (struct sockaddr_in6 *)addr6;
510 /* protect *sin6 from overwrites */
515 * Application should provide a proper zone ID or the use of
516 * default zone IDs should be enabled. Unfortunately, some
517 * applications do not behave as it should, so we need a
518 * workaround. Even if an appropriate ID is not determined,
519 * we'll see if we can determine the outgoing interface. If we
520 * can, determine the zone ID based on the interface below.
522 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
524 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
529 if ((error = ip6_setpktopts(control, &opt,
530 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
534 optp = inp->in6p_outputopts;
537 faddr = &sin6->sin6_addr;
540 * IPv4 version of udp_output calls in_pcbconnect in this case,
541 * which needs splnet and affects performance.
542 * Since we saw no essential reason for calling in_pcbconnect,
543 * we get rid of such kind of logic, and call in6_selectsrc
544 * and in6_pcbsetport in order to fill in the local address
545 * and the local port.
547 if (sin6->sin6_port == 0) {
548 error = EADDRNOTAVAIL;
552 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
553 /* how about ::ffff:0.0.0.0 case? */
558 fport = sin6->sin6_port; /* allow 0 port */
560 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
561 if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) {
563 * I believe we should explicitly discard the
564 * packet when mapped addresses are disabled,
565 * rather than send the packet as an IPv6 one.
566 * If we chose the latter approach, the packet
567 * might be sent out on the wire based on the
568 * default route, the situation which we'd
569 * probably want to avoid.
570 * (20010421 jinmei@kame.net)
575 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
576 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
578 * when remote addr is an IPv4-mapped address,
579 * local addr should not be an IPv6 address,
580 * since you cannot determine how to map IPv6
581 * source address to IPv4.
590 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
591 laddr = in6_selectsrc(sin6, optp, inp, NULL,
592 td->td_ucred, &oifp, &error);
593 if (oifp && scope_ambiguous &&
594 (error = in6_setscope(&sin6->sin6_addr,
599 laddr = &inp->in6p_laddr; /* XXX */
602 error = EADDRNOTAVAIL;
605 if (inp->in6p_lport == 0 &&
606 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
609 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
613 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
614 if ((inp->in6p_flags & IN6P_IPV6_V6ONLY)) {
616 * XXX: this case would happen when the
617 * application sets the V6ONLY flag after
618 * connecting the foreign address.
619 * Such applications should be fixed,
622 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
623 "option was set for a connected socket\n");
629 laddr = &inp->in6p_laddr;
630 faddr = &inp->in6p_faddr;
631 fport = inp->in6p_fport;
635 hlen = sizeof(struct ip);
638 * Calculate data length and get a mbuf
639 * for UDP and IP6 headers.
641 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
648 * Stuff checksum and output datagram.
650 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
651 udp6->uh_sport = inp->in6p_lport; /* lport is always set in the PCB */
652 udp6->uh_dport = fport;
654 udp6->uh_ulen = htons((u_short)plen);
661 ip6 = mtod(m, struct ip6_hdr *);
662 ip6->ip6_flow = inp->in6p_flowinfo & IPV6_FLOWINFO_MASK;
663 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
664 ip6->ip6_vfc |= IPV6_VERSION;
665 #if 0 /* ip6_plen will be filled in ip6_output. */
666 ip6->ip6_plen = htons((u_short)plen);
668 ip6->ip6_nxt = IPPROTO_UDP;
669 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
670 ip6->ip6_src = *laddr;
671 ip6->ip6_dst = *faddr;
673 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
674 sizeof(struct ip6_hdr), plen)) == 0) {
675 udp6->uh_sum = 0xffff;
680 V_udpstat.udps_opackets++;
681 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
685 error = EAFNOSUPPORT;
695 ip6_clearpktopts(&opt, -1);
702 udp6_abort(struct socket *so)
704 INIT_VNET_INET(so->so_vnet);
708 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
711 if (inp->inp_vflag & INP_IPV4) {
712 struct pr_usrreqs *pru;
714 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
715 (*pru->pru_abort)(so);
720 INP_INFO_WLOCK(&V_udbinfo);
722 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
723 in6_pcbdisconnect(inp);
724 inp->in6p_laddr = in6addr_any;
725 soisdisconnected(so);
728 INP_INFO_WUNLOCK(&V_udbinfo);
732 udp6_attach(struct socket *so, int proto, struct thread *td)
734 INIT_VNET_INET(so->so_vnet);
739 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
741 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
742 error = soreserve(so, udp_sendspace, udp_recvspace);
746 INP_INFO_WLOCK(&V_udbinfo);
747 error = in_pcballoc(so, &V_udbinfo);
749 INP_INFO_WUNLOCK(&V_udbinfo);
752 inp = (struct inpcb *)so->so_pcb;
753 INP_INFO_WUNLOCK(&V_udbinfo);
754 inp->inp_vflag |= INP_IPV6;
755 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
756 inp->inp_vflag |= INP_IPV4;
757 inp->in6p_hops = -1; /* use kernel default */
758 inp->in6p_cksum = -1; /* just to be sure */
761 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
762 * because the socket may be bound to an IPv6 wildcard address,
763 * which may match an IPv4-mapped IPv6 address.
765 inp->inp_ip_ttl = V_ip_defttl;
771 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
773 INIT_VNET_INET(so->so_vnet);
778 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
780 INP_INFO_WLOCK(&V_udbinfo);
782 inp->inp_vflag &= ~INP_IPV4;
783 inp->inp_vflag |= INP_IPV6;
784 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
785 struct sockaddr_in6 *sin6_p;
787 sin6_p = (struct sockaddr_in6 *)nam;
789 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
790 inp->inp_vflag |= INP_IPV4;
791 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
792 struct sockaddr_in sin;
794 in6_sin6_2_sin(&sin, sin6_p);
795 inp->inp_vflag |= INP_IPV4;
796 inp->inp_vflag &= ~INP_IPV6;
797 error = in_pcbbind(inp, (struct sockaddr *)&sin,
803 error = in6_pcbbind(inp, nam, td->td_ucred);
806 INP_INFO_WUNLOCK(&V_udbinfo);
811 udp6_close(struct socket *so)
813 INIT_VNET_INET(so->so_vnet);
817 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
820 if (inp->inp_vflag & INP_IPV4) {
821 struct pr_usrreqs *pru;
823 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
824 (*pru->pru_disconnect)(so);
828 INP_INFO_WLOCK(&V_udbinfo);
830 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
831 in6_pcbdisconnect(inp);
832 inp->in6p_laddr = in6addr_any;
833 soisdisconnected(so);
836 INP_INFO_WUNLOCK(&V_udbinfo);
840 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
842 INIT_VNET_INET(so->so_vnet);
847 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
849 INP_INFO_WLOCK(&V_udbinfo);
851 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
852 struct sockaddr_in6 *sin6_p;
854 sin6_p = (struct sockaddr_in6 *)nam;
855 if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
856 struct sockaddr_in sin;
858 if (inp->inp_faddr.s_addr != INADDR_ANY) {
862 in6_sin6_2_sin(&sin, sin6_p);
863 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
866 inp->inp_vflag |= INP_IPV4;
867 inp->inp_vflag &= ~INP_IPV6;
873 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
877 error = in6_pcbconnect(inp, nam, td->td_ucred);
879 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
880 /* should be non mapped addr */
881 inp->inp_vflag &= ~INP_IPV4;
882 inp->inp_vflag |= INP_IPV6;
888 INP_INFO_WUNLOCK(&V_udbinfo);
893 udp6_detach(struct socket *so)
895 INIT_VNET_INET(so->so_vnet);
899 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
901 INP_INFO_WLOCK(&V_udbinfo);
905 INP_INFO_WUNLOCK(&V_udbinfo);
909 udp6_disconnect(struct socket *so)
911 INIT_VNET_INET(so->so_vnet);
916 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
918 INP_INFO_WLOCK(&V_udbinfo);
922 if (inp->inp_vflag & INP_IPV4) {
923 struct pr_usrreqs *pru;
925 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
926 error = (*pru->pru_disconnect)(so);
931 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
936 in6_pcbdisconnect(inp);
937 inp->in6p_laddr = in6addr_any;
939 so->so_state &= ~SS_ISCONNECTED; /* XXX */
943 INP_INFO_WUNLOCK(&V_udbinfo);
948 udp6_send(struct socket *so, int flags, struct mbuf *m,
949 struct sockaddr *addr, struct mbuf *control, struct thread *td)
951 INIT_VNET_INET(so->so_vnet);
956 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
958 INP_INFO_WLOCK(&V_udbinfo);
961 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
965 if (addr->sa_family != AF_INET6) {
966 error = EAFNOSUPPORT;
972 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
974 struct sockaddr_in6 *sin6 = 0;
977 hasv4addr = (inp->inp_vflag & INP_IPV4);
979 sin6 = (struct sockaddr_in6 *)addr;
980 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
984 struct pr_usrreqs *pru;
986 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
987 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
989 * When remote addr is IPv4-mapped address,
990 * local addr should not be an IPv6 address;
991 * since you cannot determine how to map IPv6
992 * source address to IPv4.
999 * XXXRW: We release UDP-layer locks before calling
1000 * udp_send() in order to avoid recursion. However,
1001 * this does mean there is a short window where inp's
1002 * fields are unstable. Could this lead to a
1003 * potential race in which the factors causing us to
1004 * select the UDPv4 output routine are invalidated?
1007 INP_INFO_WUNLOCK(&V_udbinfo);
1009 in6_sin6_2_sin_in_sock(addr);
1010 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1011 /* addr will just be freed in sendit(). */
1012 return ((*pru->pru_send)(so, flags, m, addr, control,
1018 mac_inpcb_create_mbuf(inp, m);
1020 error = udp6_output(inp, m, addr, control, td);
1023 INP_INFO_WUNLOCK(&V_udbinfo);
1028 INP_INFO_WUNLOCK(&V_udbinfo);
1033 struct pr_usrreqs udp6_usrreqs = {
1034 .pru_abort = udp6_abort,
1035 .pru_attach = udp6_attach,
1036 .pru_bind = udp6_bind,
1037 .pru_connect = udp6_connect,
1038 .pru_control = in6_control,
1039 .pru_detach = udp6_detach,
1040 .pru_disconnect = udp6_disconnect,
1041 .pru_peeraddr = in6_mapped_peeraddr,
1042 .pru_send = udp6_send,
1043 .pru_shutdown = udp_shutdown,
1044 .pru_sockaddr = in6_mapped_sockaddr,
1045 .pru_soreceive = soreceive_dgram,
1046 .pru_sosend = sosend_dgram,
1047 .pru_sosetlabel = in_pcbsosetlabel,
1048 .pru_close = udp6_close