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>
75 #include <sys/kernel.h>
80 #include <sys/protosw.h>
81 #include <sys/signalvar.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/syslog.h>
87 #include <sys/systm.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)
133 INP_LOCK_ASSERT(inp);
136 /* Check AH/ESP integrity. */
137 if (ipsec6_in_reject(n, inp)) {
139 ipsec6stat.in_polvio++;
144 if (mac_check_inpcb_deliver(inp, n) != 0) {
150 if (inp->inp_flags & INP_CONTROLOPTS ||
151 inp->inp_socket->so_options & SO_TIMESTAMP)
152 ip6_savecontrol(inp, n, &opts);
153 m_adj(n, off + sizeof(struct udphdr));
155 so = inp->inp_socket;
156 SOCKBUF_LOCK(&so->so_rcv);
157 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
159 SOCKBUF_UNLOCK(&so->so_rcv);
163 udpstat.udps_fullsock++;
165 sorwakeup_locked(so);
169 udp6_input(struct mbuf **mp, int *offp, int proto)
171 struct mbuf *m = *mp;
177 struct sockaddr_in6 fromsa;
179 ip6 = mtod(m, struct ip6_hdr *);
181 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
182 /* XXX send icmp6 host/port unreach? */
184 return (IPPROTO_DONE);
187 #ifndef PULLDOWN_TEST
188 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
189 ip6 = mtod(m, struct ip6_hdr *);
190 uh = (struct udphdr *)((caddr_t)ip6 + off);
192 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
194 return (IPPROTO_DONE);
197 udpstat.udps_ipackets++;
200 * Destination port of 0 is illegal, based on RFC768.
202 if (uh->uh_dport == 0)
205 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
206 ulen = ntohs((u_short)uh->uh_ulen);
209 udpstat.udps_badlen++;
214 * Checksum extended UDP header and data.
216 if (uh->uh_sum == 0) {
217 udpstat.udps_nosum++;
220 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
221 udpstat.udps_badsum++;
226 * Construct sockaddr format source address.
228 init_sin6(&fromsa, m);
229 fromsa.sin6_port = uh->uh_sport;
231 INP_INFO_RLOCK(&udbinfo);
232 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
236 * In the event that laddr should be set to the link-local
237 * address (this happens in RIPng), the multicast address
238 * specified in the received packet will not match laddr. To
239 * handle this situation, matching is relaxed if the
240 * receiving interface is the same as one specified in the
241 * socket and if the destination multicast address matches
242 * one of the multicast groups specified in the socket.
246 * KAME note: traditionally we dropped udpiphdr from mbuf
247 * here. We need udphdr for IPsec processing so we do that
251 LIST_FOREACH(inp, &udb, inp_list) {
252 if ((inp->inp_vflag & INP_IPV6) == 0)
254 if (inp->inp_lport != uh->uh_dport)
257 * XXX: Do not check source port of incoming datagram
258 * unless inp_connect() has been called to bind the
259 * fport part of the 4-tuple; the source could be
260 * trying to talk to us with an ephemeral port.
262 if (inp->inp_fport != 0 &&
263 inp->inp_fport != uh->uh_sport)
265 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
266 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
270 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
271 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
273 inp->inp_fport != uh->uh_sport)
280 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
282 udp6_append(last, n, off, &fromsa);
288 * Don't look for additional matches if this one does
289 * not have either the SO_REUSEPORT or SO_REUSEADDR
290 * socket options set. This heuristic avoids
291 * searching through all pcbs in the common case of a
292 * non-shared port. It assumes that an application
293 * will never clear these options after setting them.
295 if ((last->inp_socket->so_options &
296 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
302 * No matching pcb found; discard datagram. (No need
303 * to send an ICMP Port Unreachable for a broadcast
304 * or multicast datgram.)
306 udpstat.udps_noport++;
307 udpstat.udps_noportmcast++;
311 INP_INFO_RUNLOCK(&udbinfo);
312 udp6_append(last, m, off, &fromsa);
314 return (IPPROTO_DONE);
317 * Locate pcb for datagram.
319 inp = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport,
320 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
322 if (udp_log_in_vain) {
323 char ip6bufs[INET6_ADDRSTRLEN];
324 char ip6bufd[INET6_ADDRSTRLEN];
327 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
328 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
330 ip6_sprintf(ip6bufs, &ip6->ip6_src),
331 ntohs(uh->uh_sport));
333 udpstat.udps_noport++;
334 if (m->m_flags & M_MCAST) {
335 printf("UDP6: M_MCAST is set in a unicast packet.\n");
336 udpstat.udps_noportmcast++;
339 INP_INFO_RUNLOCK(&udbinfo);
342 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
344 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
345 return (IPPROTO_DONE);
348 INP_INFO_RUNLOCK(&udbinfo);
349 udp6_append(inp, m, off, &fromsa);
351 return (IPPROTO_DONE);
354 INP_INFO_RUNLOCK(&udbinfo);
358 return (IPPROTO_DONE);
362 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
368 struct ip6ctlparam *ip6cp = NULL;
369 const struct sockaddr_in6 *sa6_src = NULL;
371 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
372 struct udp_portonly {
377 if (sa->sa_family != AF_INET6 ||
378 sa->sa_len != sizeof(struct sockaddr_in6))
381 if ((unsigned)cmd >= PRC_NCMDS)
383 if (PRC_IS_REDIRECT(cmd))
384 notify = in6_rtchange, d = NULL;
385 else if (cmd == PRC_HOSTDEAD)
387 else if (inet6ctlerrmap[cmd] == 0)
390 /* if the parameter is from icmp6, decode it. */
392 ip6cp = (struct ip6ctlparam *)d;
394 ip6 = ip6cp->ip6c_ip6;
395 off = ip6cp->ip6c_off;
396 cmdarg = ip6cp->ip6c_cmdarg;
397 sa6_src = ip6cp->ip6c_src;
407 * XXX: We assume that when IPV6 is non NULL,
408 * M and OFF are valid.
411 /* Check if we can safely examine src and dst ports. */
412 if (m->m_pkthdr.len < off + sizeof(*uhp))
415 bzero(&uh, sizeof(uh));
416 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
418 (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport,
419 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
422 (void) in6_pcbnotify(&udbinfo, sa, 0,
423 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
427 udp6_getcred(SYSCTL_HANDLER_ARGS)
430 struct sockaddr_in6 addrs[2];
434 error = priv_check(req->td, PRIV_NETINET_GETCRED);
438 if (req->newlen != sizeof(addrs))
440 if (req->oldlen != sizeof(struct xucred))
442 error = SYSCTL_IN(req, addrs, sizeof(addrs));
445 if ((error = sa6_embedscope(&addrs[0], ip6_use_defzone)) != 0 ||
446 (error = sa6_embedscope(&addrs[1], ip6_use_defzone)) != 0) {
449 INP_INFO_RLOCK(&udbinfo);
450 inp = in6_pcblookup_hash(&udbinfo, &addrs[1].sin6_addr,
451 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
455 INP_INFO_RUNLOCK(&udbinfo);
456 if (inp->inp_socket == NULL)
459 error = cr_canseesocket(req->td->td_ucred,
462 cru2x(inp->inp_cred, &xuc);
465 INP_INFO_RUNLOCK(&udbinfo);
469 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
473 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
474 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
477 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
478 struct mbuf *control, struct thread *td)
480 u_int32_t ulen = m->m_pkthdr.len;
481 u_int32_t plen = sizeof(struct udphdr) + ulen;
484 struct in6_addr *laddr, *faddr;
485 struct sockaddr_in6 *sin6 = NULL;
486 struct ifnet *oifp = NULL;
487 int scope_ambiguous = 0;
490 struct ip6_pktopts *optp, opt;
491 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
493 struct sockaddr_in6 tmp;
495 INP_WLOCK_ASSERT(inp);
498 /* addr6 has been validated in udp6_send(). */
499 sin6 = (struct sockaddr_in6 *)addr6;
501 /* protect *sin6 from overwrites */
506 * Application should provide a proper zone ID or the use of
507 * default zone IDs should be enabled. Unfortunately, some
508 * applications do not behave as it should, so we need a
509 * workaround. Even if an appropriate ID is not determined,
510 * we'll see if we can determine the outgoing interface. If we
511 * can, determine the zone ID based on the interface below.
513 if (sin6->sin6_scope_id == 0 && !ip6_use_defzone)
515 if ((error = sa6_embedscope(sin6, ip6_use_defzone)) != 0)
520 if ((error = ip6_setpktopts(control, &opt,
521 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
525 optp = inp->in6p_outputopts;
528 faddr = &sin6->sin6_addr;
531 * IPv4 version of udp_output calls in_pcbconnect in this case,
532 * which needs splnet and affects performance.
533 * Since we saw no essential reason for calling in_pcbconnect,
534 * we get rid of such kind of logic, and call in6_selectsrc
535 * and in6_pcbsetport in order to fill in the local address
536 * and the local port.
538 if (sin6->sin6_port == 0) {
539 error = EADDRNOTAVAIL;
543 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
544 /* how about ::ffff:0.0.0.0 case? */
549 fport = sin6->sin6_port; /* allow 0 port */
551 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
552 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
554 * I believe we should explicitly discard the
555 * packet when mapped addresses are disabled,
556 * rather than send the packet as an IPv6 one.
557 * If we chose the latter approach, the packet
558 * might be sent out on the wire based on the
559 * default route, the situation which we'd
560 * probably want to avoid.
561 * (20010421 jinmei@kame.net)
566 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
567 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
569 * when remote addr is an IPv4-mapped address,
570 * local addr should not be an IPv6 address,
571 * since you cannot determine how to map IPv6
572 * source address to IPv4.
581 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
582 laddr = in6_selectsrc(sin6, optp, inp, NULL,
583 td->td_ucred, &oifp, &error);
584 if (oifp && scope_ambiguous &&
585 (error = in6_setscope(&sin6->sin6_addr,
590 laddr = &inp->in6p_laddr; /* XXX */
593 error = EADDRNOTAVAIL;
596 if (inp->inp_lport == 0 &&
597 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
600 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
604 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
605 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
607 * XXX: this case would happen when the
608 * application sets the V6ONLY flag after
609 * connecting the foreign address.
610 * Such applications should be fixed,
613 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
614 "option was set for a connected socket\n");
620 laddr = &inp->in6p_laddr;
621 faddr = &inp->in6p_faddr;
622 fport = inp->inp_fport;
626 hlen = sizeof(struct ip);
629 * Calculate data length and get a mbuf
630 * for UDP and IP6 headers.
632 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
639 * Stuff checksum and output datagram.
641 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
642 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
643 udp6->uh_dport = fport;
645 udp6->uh_ulen = htons((u_short)plen);
652 ip6 = mtod(m, struct ip6_hdr *);
653 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
654 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
655 ip6->ip6_vfc |= IPV6_VERSION;
656 #if 0 /* ip6_plen will be filled in ip6_output. */
657 ip6->ip6_plen = htons((u_short)plen);
659 ip6->ip6_nxt = IPPROTO_UDP;
660 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
661 ip6->ip6_src = *laddr;
662 ip6->ip6_dst = *faddr;
664 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
665 sizeof(struct ip6_hdr), plen)) == 0) {
666 udp6->uh_sum = 0xffff;
671 udpstat.udps_opackets++;
672 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
676 error = EAFNOSUPPORT;
686 ip6_clearpktopts(&opt, -1);
693 udp6_abort(struct socket *so)
698 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
701 if (inp->inp_vflag & INP_IPV4) {
702 struct pr_usrreqs *pru;
704 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
705 (*pru->pru_abort)(so);
710 INP_INFO_WLOCK(&udbinfo);
712 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
713 in6_pcbdisconnect(inp);
714 inp->in6p_laddr = in6addr_any;
715 soisdisconnected(so);
718 INP_INFO_WUNLOCK(&udbinfo);
722 udp6_attach(struct socket *so, int proto, struct thread *td)
728 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
730 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
731 error = soreserve(so, udp_sendspace, udp_recvspace);
735 INP_INFO_WLOCK(&udbinfo);
736 error = in_pcballoc(so, &udbinfo);
738 INP_INFO_WUNLOCK(&udbinfo);
741 inp = (struct inpcb *)so->so_pcb;
742 INP_INFO_WUNLOCK(&udbinfo);
743 inp->inp_vflag |= INP_IPV6;
744 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
745 inp->inp_vflag |= INP_IPV4;
746 inp->in6p_hops = -1; /* use kernel default */
747 inp->in6p_cksum = -1; /* just to be sure */
750 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
751 * because the socket may be bound to an IPv6 wildcard address,
752 * which may match an IPv4-mapped IPv6 address.
754 inp->inp_ip_ttl = ip_defttl;
760 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
766 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
768 INP_INFO_WLOCK(&udbinfo);
770 inp->inp_vflag &= ~INP_IPV4;
771 inp->inp_vflag |= INP_IPV6;
772 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
773 struct sockaddr_in6 *sin6_p;
775 sin6_p = (struct sockaddr_in6 *)nam;
777 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
778 inp->inp_vflag |= INP_IPV4;
779 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
780 struct sockaddr_in sin;
782 in6_sin6_2_sin(&sin, sin6_p);
783 inp->inp_vflag |= INP_IPV4;
784 inp->inp_vflag &= ~INP_IPV6;
785 error = in_pcbbind(inp, (struct sockaddr *)&sin,
791 error = in6_pcbbind(inp, nam, td->td_ucred);
794 INP_INFO_WUNLOCK(&udbinfo);
799 udp6_close(struct socket *so)
804 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
807 if (inp->inp_vflag & INP_IPV4) {
808 struct pr_usrreqs *pru;
810 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
811 (*pru->pru_disconnect)(so);
815 INP_INFO_WLOCK(&udbinfo);
817 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
818 in6_pcbdisconnect(inp);
819 inp->in6p_laddr = in6addr_any;
820 soisdisconnected(so);
823 INP_INFO_WUNLOCK(&udbinfo);
827 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
830 struct sockaddr_in6 *sin6;
834 sin6 = (struct sockaddr_in6 *)nam;
835 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
837 INP_INFO_WLOCK(&udbinfo);
839 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
840 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
841 struct sockaddr_in sin;
843 if (inp->inp_faddr.s_addr != INADDR_ANY) {
847 in6_sin6_2_sin(&sin, sin6);
848 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
851 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
854 inp->inp_vflag |= INP_IPV4;
855 inp->inp_vflag &= ~INP_IPV6;
860 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
864 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
867 error = in6_pcbconnect(inp, nam, td->td_ucred);
869 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
870 /* should be non mapped addr */
871 inp->inp_vflag &= ~INP_IPV4;
872 inp->inp_vflag |= INP_IPV6;
878 INP_INFO_WUNLOCK(&udbinfo);
883 udp6_detach(struct socket *so)
888 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
890 INP_INFO_WLOCK(&udbinfo);
894 INP_INFO_WUNLOCK(&udbinfo);
898 udp6_disconnect(struct socket *so)
904 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
906 INP_INFO_WLOCK(&udbinfo);
910 if (inp->inp_vflag & INP_IPV4) {
911 struct pr_usrreqs *pru;
913 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
914 error = (*pru->pru_disconnect)(so);
919 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
924 in6_pcbdisconnect(inp);
925 inp->in6p_laddr = in6addr_any;
927 so->so_state &= ~SS_ISCONNECTED; /* XXX */
931 INP_INFO_WUNLOCK(&udbinfo);
936 udp6_send(struct socket *so, int flags, struct mbuf *m,
937 struct sockaddr *addr, struct mbuf *control, struct thread *td)
943 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
945 INP_INFO_WLOCK(&udbinfo);
948 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
952 if (addr->sa_family != AF_INET6) {
953 error = EAFNOSUPPORT;
959 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
961 struct sockaddr_in6 *sin6 = 0;
964 hasv4addr = (inp->inp_vflag & INP_IPV4);
966 sin6 = (struct sockaddr_in6 *)addr;
967 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
971 struct pr_usrreqs *pru;
973 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
974 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
976 * When remote addr is IPv4-mapped address,
977 * local addr should not be an IPv6 address;
978 * since you cannot determine how to map IPv6
979 * source address to IPv4.
986 * XXXRW: We release UDP-layer locks before calling
987 * udp_send() in order to avoid recursion. However,
988 * this does mean there is a short window where inp's
989 * fields are unstable. Could this lead to a
990 * potential race in which the factors causing us to
991 * select the UDPv4 output routine are invalidated?
994 INP_INFO_WUNLOCK(&udbinfo);
996 in6_sin6_2_sin_in_sock(addr);
997 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
998 /* addr will just be freed in sendit(). */
999 return ((*pru->pru_send)(so, flags, m, addr, control,
1005 mac_create_mbuf_from_inpcb(inp, m);
1007 error = udp6_output(inp, m, addr, control, td);
1010 INP_INFO_WUNLOCK(&udbinfo);
1015 INP_INFO_WUNLOCK(&udbinfo);
1020 struct pr_usrreqs udp6_usrreqs = {
1021 .pru_abort = udp6_abort,
1022 .pru_attach = udp6_attach,
1023 .pru_bind = udp6_bind,
1024 .pru_connect = udp6_connect,
1025 .pru_control = in6_control,
1026 .pru_detach = udp6_detach,
1027 .pru_disconnect = udp6_disconnect,
1028 .pru_peeraddr = in6_mapped_peeraddr,
1029 .pru_send = udp6_send,
1030 .pru_shutdown = udp_shutdown,
1031 .pru_sockaddr = in6_mapped_sockaddr,
1032 .pru_sosend = sosend_dgram,
1033 .pru_sosetlabel = in_pcbsosetlabel,
1034 .pru_close = udp6_close