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"
72 #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>
89 #include <net/if_types.h>
90 #include <net/route.h>
92 #include <netinet/in.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/in_systm.h>
95 #include <netinet/in_var.h>
96 #include <netinet/ip.h>
97 #include <netinet/ip_icmp.h>
98 #include <netinet/ip6.h>
99 #include <netinet/icmp_var.h>
100 #include <netinet/icmp6.h>
101 #include <netinet/ip_var.h>
102 #include <netinet/udp.h>
103 #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 V_ipsec6stat.in_polvio++;
144 if (mac_inpcb_check_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_INC(udps_fullsock);
165 sorwakeup_locked(so);
169 udp6_input(struct mbuf **mp, int *offp, int proto)
171 struct mbuf *m = *mp;
179 struct sockaddr_in6 fromsa;
181 ifp = m->m_pkthdr.rcvif;
182 ip6 = mtod(m, struct ip6_hdr *);
184 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
185 /* XXX send icmp6 host/port unreach? */
187 return (IPPROTO_DONE);
190 #ifndef PULLDOWN_TEST
191 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
192 ip6 = mtod(m, struct ip6_hdr *);
193 uh = (struct udphdr *)((caddr_t)ip6 + off);
195 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
197 return (IPPROTO_DONE);
200 UDPSTAT_INC(udps_ipackets);
203 * Destination port of 0 is illegal, based on RFC768.
205 if (uh->uh_dport == 0)
208 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
209 ulen = ntohs((u_short)uh->uh_ulen);
212 UDPSTAT_INC(udps_badlen);
217 * Checksum extended UDP header and data.
219 if (uh->uh_sum == 0) {
220 UDPSTAT_INC(udps_nosum);
223 if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
224 UDPSTAT_INC(udps_badsum);
229 * Construct sockaddr format source address.
231 init_sin6(&fromsa, m);
232 fromsa.sin6_port = uh->uh_sport;
234 INP_INFO_RLOCK(&V_udbinfo);
235 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
237 struct ip6_moptions *imo;
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->inp_lport != uh->uh_dport)
260 if (inp->inp_fport != 0 &&
261 inp->inp_fport != uh->uh_sport)
263 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
264 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
268 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
269 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
271 inp->inp_fport != uh->uh_sport)
276 * Handle socket delivery policy for any-source
277 * and source-specific multicast. [RFC3678]
279 imo = inp->in6p_moptions;
280 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
281 struct sockaddr_in6 mcaddr;
286 bzero(&mcaddr, sizeof(struct sockaddr_in6));
287 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
288 mcaddr.sin6_family = AF_INET6;
289 mcaddr.sin6_addr = ip6->ip6_dst;
291 blocked = im6o_mc_filter(imo, ifp,
292 (struct sockaddr *)&mcaddr,
293 (struct sockaddr *)&fromsa);
294 if (blocked != MCAST_PASS) {
295 if (blocked == MCAST_NOTGMEMBER)
296 IP6STAT_INC(ip6s_notmember);
297 if (blocked == MCAST_NOTSMEMBER ||
298 blocked == MCAST_MUTED)
299 UDPSTAT_INC(udps_filtermcast);
300 INP_RUNLOCK(inp); /* XXX */
309 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
311 up = intoudpcb(last);
312 if (up->u_tun_func == NULL) {
313 udp6_append(last, n, off, &fromsa);
316 * Engage the tunneling
317 * protocol we will have to
318 * leave the info_lock up,
319 * since we are hunting
320 * through multiple UDP's.
323 (*up->u_tun_func)(n, off, last);
330 * Don't look for additional matches if this one does
331 * not have either the SO_REUSEPORT or SO_REUSEADDR
332 * socket options set. This heuristic avoids
333 * searching through all pcbs in the common case of a
334 * non-shared port. It assumes that an application
335 * will never clear these options after setting them.
337 if ((last->inp_socket->so_options &
338 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
344 * No matching pcb found; discard datagram. (No need
345 * to send an ICMP Port Unreachable for a broadcast
346 * or multicast datgram.)
348 UDPSTAT_INC(udps_noport);
349 UDPSTAT_INC(udps_noportmcast);
353 INP_INFO_RUNLOCK(&V_udbinfo);
354 up = intoudpcb(last);
355 if (up->u_tun_func == NULL) {
356 udp6_append(last, m, off, &fromsa);
359 * Engage the tunneling protocol.
361 (*up->u_tun_func)(m, off, last);
364 return (IPPROTO_DONE);
367 * Locate pcb for datagram.
369 inp = in6_pcblookup_hash(&V_udbinfo, &ip6->ip6_src, uh->uh_sport,
370 &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
372 if (udp_log_in_vain) {
373 char ip6bufs[INET6_ADDRSTRLEN];
374 char ip6bufd[INET6_ADDRSTRLEN];
377 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
378 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
380 ip6_sprintf(ip6bufs, &ip6->ip6_src),
381 ntohs(uh->uh_sport));
383 UDPSTAT_INC(udps_noport);
384 if (m->m_flags & M_MCAST) {
385 printf("UDP6: M_MCAST is set in a unicast packet.\n");
386 UDPSTAT_INC(udps_noportmcast);
389 INP_INFO_RUNLOCK(&V_udbinfo);
392 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
394 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
395 return (IPPROTO_DONE);
398 INP_INFO_RUNLOCK(&V_udbinfo);
400 if (up->u_tun_func == NULL) {
401 udp6_append(inp, m, off, &fromsa);
404 * Engage the tunneling protocol.
407 (*up->u_tun_func)(m, off, inp);
410 return (IPPROTO_DONE);
413 INP_INFO_RUNLOCK(&V_udbinfo);
417 return (IPPROTO_DONE);
421 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
427 struct ip6ctlparam *ip6cp = NULL;
428 const struct sockaddr_in6 *sa6_src = NULL;
430 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
431 struct udp_portonly {
436 if (sa->sa_family != AF_INET6 ||
437 sa->sa_len != sizeof(struct sockaddr_in6))
440 if ((unsigned)cmd >= PRC_NCMDS)
442 if (PRC_IS_REDIRECT(cmd))
443 notify = in6_rtchange, d = NULL;
444 else if (cmd == PRC_HOSTDEAD)
446 else if (inet6ctlerrmap[cmd] == 0)
449 /* if the parameter is from icmp6, decode it. */
451 ip6cp = (struct ip6ctlparam *)d;
453 ip6 = ip6cp->ip6c_ip6;
454 off = ip6cp->ip6c_off;
455 cmdarg = ip6cp->ip6c_cmdarg;
456 sa6_src = ip6cp->ip6c_src;
466 * XXX: We assume that when IPV6 is non NULL,
467 * M and OFF are valid.
470 /* Check if we can safely examine src and dst ports. */
471 if (m->m_pkthdr.len < off + sizeof(*uhp))
474 bzero(&uh, sizeof(uh));
475 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
477 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
478 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
481 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
482 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
486 udp6_getcred(SYSCTL_HANDLER_ARGS)
489 struct sockaddr_in6 addrs[2];
493 error = priv_check(req->td, PRIV_NETINET_GETCRED);
497 if (req->newlen != sizeof(addrs))
499 if (req->oldlen != sizeof(struct xucred))
501 error = SYSCTL_IN(req, addrs, sizeof(addrs));
504 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
505 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
508 INP_INFO_RLOCK(&V_udbinfo);
509 inp = in6_pcblookup_hash(&V_udbinfo, &addrs[1].sin6_addr,
510 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port, 1,
514 INP_INFO_RUNLOCK(&V_udbinfo);
515 if (inp->inp_socket == NULL)
518 error = cr_canseesocket(req->td->td_ucred,
521 cru2x(inp->inp_cred, &xuc);
524 INP_INFO_RUNLOCK(&V_udbinfo);
528 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
532 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
533 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
536 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
537 struct mbuf *control, struct thread *td)
539 u_int32_t ulen = m->m_pkthdr.len;
540 u_int32_t plen = sizeof(struct udphdr) + ulen;
543 struct in6_addr *laddr, *faddr, in6a;
544 struct sockaddr_in6 *sin6 = NULL;
545 struct ifnet *oifp = NULL;
546 int scope_ambiguous = 0;
549 struct ip6_pktopts *optp, opt;
550 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
552 struct sockaddr_in6 tmp;
554 INP_WLOCK_ASSERT(inp);
557 /* addr6 has been validated in udp6_send(). */
558 sin6 = (struct sockaddr_in6 *)addr6;
560 /* protect *sin6 from overwrites */
565 * Application should provide a proper zone ID or the use of
566 * default zone IDs should be enabled. Unfortunately, some
567 * applications do not behave as it should, so we need a
568 * workaround. Even if an appropriate ID is not determined,
569 * we'll see if we can determine the outgoing interface. If we
570 * can, determine the zone ID based on the interface below.
572 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
574 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
579 if ((error = ip6_setpktopts(control, &opt,
580 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
584 optp = inp->in6p_outputopts;
587 faddr = &sin6->sin6_addr;
590 * IPv4 version of udp_output calls in_pcbconnect in this case,
591 * which needs splnet and affects performance.
592 * Since we saw no essential reason for calling in_pcbconnect,
593 * we get rid of such kind of logic, and call in6_selectsrc
594 * and in6_pcbsetport in order to fill in the local address
595 * and the local port.
597 if (sin6->sin6_port == 0) {
598 error = EADDRNOTAVAIL;
602 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
603 /* how about ::ffff:0.0.0.0 case? */
608 fport = sin6->sin6_port; /* allow 0 port */
610 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
611 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
613 * I believe we should explicitly discard the
614 * packet when mapped addresses are disabled,
615 * rather than send the packet as an IPv6 one.
616 * If we chose the latter approach, the packet
617 * might be sent out on the wire based on the
618 * default route, the situation which we'd
619 * probably want to avoid.
620 * (20010421 jinmei@kame.net)
625 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
626 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
628 * when remote addr is an IPv4-mapped address,
629 * local addr should not be an IPv6 address,
630 * since you cannot determine how to map IPv6
631 * source address to IPv4.
640 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
641 error = in6_selectsrc(sin6, optp, inp, NULL,
642 td->td_ucred, &oifp, &in6a);
645 if (oifp && scope_ambiguous &&
646 (error = in6_setscope(&sin6->sin6_addr,
652 laddr = &inp->in6p_laddr; /* XXX */
655 error = EADDRNOTAVAIL;
658 if (inp->inp_lport == 0 &&
659 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0)
662 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
666 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
667 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
669 * XXX: this case would happen when the
670 * application sets the V6ONLY flag after
671 * connecting the foreign address.
672 * Such applications should be fixed,
675 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
676 "option was set for a connected socket\n");
682 laddr = &inp->in6p_laddr;
683 faddr = &inp->in6p_faddr;
684 fport = inp->inp_fport;
688 hlen = sizeof(struct ip);
691 * Calculate data length and get a mbuf
692 * for UDP and IP6 headers.
694 M_PREPEND(m, hlen + sizeof(struct udphdr), M_DONTWAIT);
701 * Stuff checksum and output datagram.
703 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
704 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
705 udp6->uh_dport = fport;
707 udp6->uh_ulen = htons((u_short)plen);
714 ip6 = mtod(m, struct ip6_hdr *);
715 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
716 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
717 ip6->ip6_vfc |= IPV6_VERSION;
718 #if 0 /* ip6_plen will be filled in ip6_output. */
719 ip6->ip6_plen = htons((u_short)plen);
721 ip6->ip6_nxt = IPPROTO_UDP;
722 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
723 ip6->ip6_src = *laddr;
724 ip6->ip6_dst = *faddr;
726 if ((udp6->uh_sum = in6_cksum(m, IPPROTO_UDP,
727 sizeof(struct ip6_hdr), plen)) == 0) {
728 udp6->uh_sum = 0xffff;
733 UDPSTAT_INC(udps_opackets);
734 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
738 error = EAFNOSUPPORT;
748 ip6_clearpktopts(&opt, -1);
755 udp6_abort(struct socket *so)
760 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
763 if (inp->inp_vflag & INP_IPV4) {
764 struct pr_usrreqs *pru;
766 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
767 (*pru->pru_abort)(so);
772 INP_INFO_WLOCK(&V_udbinfo);
774 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
775 in6_pcbdisconnect(inp);
776 inp->in6p_laddr = in6addr_any;
777 soisdisconnected(so);
780 INP_INFO_WUNLOCK(&V_udbinfo);
784 udp6_attach(struct socket *so, int proto, struct thread *td)
790 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
792 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
793 error = soreserve(so, udp_sendspace, udp_recvspace);
797 INP_INFO_WLOCK(&V_udbinfo);
798 error = in_pcballoc(so, &V_udbinfo);
800 INP_INFO_WUNLOCK(&V_udbinfo);
803 inp = (struct inpcb *)so->so_pcb;
804 inp->inp_vflag |= INP_IPV6;
805 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
806 inp->inp_vflag |= INP_IPV4;
807 inp->in6p_hops = -1; /* use kernel default */
808 inp->in6p_cksum = -1; /* just to be sure */
811 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
812 * because the socket may be bound to an IPv6 wildcard address,
813 * which may match an IPv4-mapped IPv6 address.
815 inp->inp_ip_ttl = V_ip_defttl;
817 error = udp_newudpcb(inp);
821 INP_INFO_WUNLOCK(&V_udbinfo);
825 INP_INFO_WUNLOCK(&V_udbinfo);
830 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
836 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
838 INP_INFO_WLOCK(&V_udbinfo);
840 inp->inp_vflag &= ~INP_IPV4;
841 inp->inp_vflag |= INP_IPV6;
842 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
843 struct sockaddr_in6 *sin6_p;
845 sin6_p = (struct sockaddr_in6 *)nam;
847 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
848 inp->inp_vflag |= INP_IPV4;
849 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
850 struct sockaddr_in sin;
852 in6_sin6_2_sin(&sin, sin6_p);
853 inp->inp_vflag |= INP_IPV4;
854 inp->inp_vflag &= ~INP_IPV6;
855 error = in_pcbbind(inp, (struct sockaddr *)&sin,
861 error = in6_pcbbind(inp, nam, td->td_ucred);
864 INP_INFO_WUNLOCK(&V_udbinfo);
869 udp6_close(struct socket *so)
874 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
877 if (inp->inp_vflag & INP_IPV4) {
878 struct pr_usrreqs *pru;
880 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
881 (*pru->pru_disconnect)(so);
885 INP_INFO_WLOCK(&V_udbinfo);
887 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
888 in6_pcbdisconnect(inp);
889 inp->in6p_laddr = in6addr_any;
890 soisdisconnected(so);
893 INP_INFO_WUNLOCK(&V_udbinfo);
897 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
900 struct sockaddr_in6 *sin6;
904 sin6 = (struct sockaddr_in6 *)nam;
905 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
907 INP_INFO_WLOCK(&V_udbinfo);
909 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0 &&
910 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
911 struct sockaddr_in sin;
913 if (inp->inp_faddr.s_addr != INADDR_ANY) {
917 in6_sin6_2_sin(&sin, sin6);
918 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
921 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
924 inp->inp_vflag |= INP_IPV4;
925 inp->inp_vflag &= ~INP_IPV6;
930 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
934 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
937 error = in6_pcbconnect(inp, nam, td->td_ucred);
939 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
940 /* should be non mapped addr */
941 inp->inp_vflag &= ~INP_IPV4;
942 inp->inp_vflag |= INP_IPV6;
948 INP_INFO_WUNLOCK(&V_udbinfo);
953 udp6_detach(struct socket *so)
959 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
961 INP_INFO_WLOCK(&V_udbinfo);
964 KASSERT(up != NULL, ("%s: up == NULL", __func__));
967 INP_INFO_WUNLOCK(&V_udbinfo);
972 udp6_disconnect(struct socket *so)
978 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
980 INP_INFO_WLOCK(&V_udbinfo);
984 if (inp->inp_vflag & INP_IPV4) {
985 struct pr_usrreqs *pru;
987 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
988 error = (*pru->pru_disconnect)(so);
993 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
998 in6_pcbdisconnect(inp);
999 inp->in6p_laddr = in6addr_any;
1001 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1005 INP_INFO_WUNLOCK(&V_udbinfo);
1010 udp6_send(struct socket *so, int flags, struct mbuf *m,
1011 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1016 inp = sotoinpcb(so);
1017 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1019 INP_INFO_WLOCK(&V_udbinfo);
1022 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1026 if (addr->sa_family != AF_INET6) {
1027 error = EAFNOSUPPORT;
1033 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1035 struct sockaddr_in6 *sin6 = 0;
1038 hasv4addr = (inp->inp_vflag & INP_IPV4);
1040 sin6 = (struct sockaddr_in6 *)addr;
1041 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1045 struct pr_usrreqs *pru;
1047 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1048 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
1050 * When remote addr is IPv4-mapped address,
1051 * local addr should not be an IPv6 address;
1052 * since you cannot determine how to map IPv6
1053 * source address to IPv4.
1060 * XXXRW: We release UDP-layer locks before calling
1061 * udp_send() in order to avoid recursion. However,
1062 * this does mean there is a short window where inp's
1063 * fields are unstable. Could this lead to a
1064 * potential race in which the factors causing us to
1065 * select the UDPv4 output routine are invalidated?
1068 INP_INFO_WUNLOCK(&V_udbinfo);
1070 in6_sin6_2_sin_in_sock(addr);
1071 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1072 /* addr will just be freed in sendit(). */
1073 return ((*pru->pru_send)(so, flags, m, addr, control,
1079 mac_inpcb_create_mbuf(inp, m);
1081 error = udp6_output(inp, m, addr, control, td);
1084 INP_INFO_WUNLOCK(&V_udbinfo);
1089 INP_INFO_WUNLOCK(&V_udbinfo);
1094 struct pr_usrreqs udp6_usrreqs = {
1095 .pru_abort = udp6_abort,
1096 .pru_attach = udp6_attach,
1097 .pru_bind = udp6_bind,
1098 .pru_connect = udp6_connect,
1099 .pru_control = in6_control,
1100 .pru_detach = udp6_detach,
1101 .pru_disconnect = udp6_disconnect,
1102 .pru_peeraddr = in6_mapped_peeraddr,
1103 .pru_send = udp6_send,
1104 .pru_shutdown = udp_shutdown,
1105 .pru_sockaddr = in6_mapped_sockaddr,
1106 .pru_soreceive = soreceive_dgram,
1107 .pru_sosend = sosend_dgram,
1108 .pru_sosetlabel = in_pcbsosetlabel,
1109 .pru_close = udp6_close