2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * Portions of this software were developed by Robert N. M. Watson under
7 * contract to Juniper Networks, Inc.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
34 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
38 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
39 * The Regents of the University of California.
40 * All rights reserved.
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 4. Neither the name of the University nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
54 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
69 #include <sys/cdefs.h>
70 __FBSDID("$FreeBSD$");
73 #include "opt_inet6.h"
75 #include "opt_ipsec.h"
76 #include "opt_kdtrace.h"
78 #include <sys/param.h>
80 #include <sys/kernel.h>
85 #include <sys/protosw.h>
87 #include <sys/signalvar.h>
88 #include <sys/socket.h>
89 #include <sys/socketvar.h>
91 #include <sys/sysctl.h>
92 #include <sys/syslog.h>
93 #include <sys/systm.h>
96 #include <net/if_types.h>
97 #include <net/route.h>
99 #include <netinet/in.h>
100 #include <netinet/in_kdtrace.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/in_systm.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip.h>
105 #include <netinet/ip_icmp.h>
106 #include <netinet/ip6.h>
107 #include <netinet/icmp_var.h>
108 #include <netinet/icmp6.h>
109 #include <netinet/ip_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
113 #include <netinet6/ip6protosw.h>
114 #include <netinet6/ip6_var.h>
115 #include <netinet6/in6_pcb.h>
116 #include <netinet6/udp6_var.h>
117 #include <netinet6/scope6_var.h>
120 #include <netipsec/ipsec.h>
121 #include <netipsec/ipsec6.h>
124 #include <security/mac/mac_framework.h>
127 * UDP protocol implementation.
128 * Per RFC 768, August, 1980.
131 extern struct protosw inetsw[];
132 static void udp6_detach(struct socket *so);
135 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
136 struct sockaddr_in6 *fromsa)
141 INP_LOCK_ASSERT(inp);
144 /* Check AH/ESP integrity. */
145 if (ipsec6_in_reject(n, inp)) {
147 IPSEC6STAT_INC(ips_in_polvio);
152 if (mac_inpcb_check_deliver(inp, n) != 0) {
158 if (inp->inp_flags & INP_CONTROLOPTS ||
159 inp->inp_socket->so_options & SO_TIMESTAMP)
160 ip6_savecontrol(inp, n, &opts);
161 m_adj(n, off + sizeof(struct udphdr));
163 so = inp->inp_socket;
164 SOCKBUF_LOCK(&so->so_rcv);
165 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
167 SOCKBUF_UNLOCK(&so->so_rcv);
171 UDPSTAT_INC(udps_fullsock);
173 sorwakeup_locked(so);
177 udp6_input(struct mbuf **mp, int *offp, int proto)
179 struct mbuf *m = *mp;
187 struct sockaddr_in6 fromsa;
188 struct m_tag *fwd_tag;
191 ifp = m->m_pkthdr.rcvif;
192 ip6 = mtod(m, struct ip6_hdr *);
194 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
195 /* XXX send icmp6 host/port unreach? */
197 return (IPPROTO_DONE);
200 #ifndef PULLDOWN_TEST
201 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
202 ip6 = mtod(m, struct ip6_hdr *);
203 uh = (struct udphdr *)((caddr_t)ip6 + off);
205 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
207 return (IPPROTO_DONE);
210 UDPSTAT_INC(udps_ipackets);
213 * Destination port of 0 is illegal, based on RFC768.
215 if (uh->uh_dport == 0)
218 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
219 ulen = ntohs((u_short)uh->uh_ulen);
222 UDPSTAT_INC(udps_badlen);
227 * Checksum extended UDP header and data.
229 if (uh->uh_sum == 0) {
230 UDPSTAT_INC(udps_nosum);
234 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) {
235 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
236 uh_sum = m->m_pkthdr.csum_data;
238 uh_sum = in6_cksum_pseudo(ip6, ulen,
239 IPPROTO_UDP, m->m_pkthdr.csum_data);
242 uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen);
245 UDPSTAT_INC(udps_badsum);
250 * Construct sockaddr format source address.
252 init_sin6(&fromsa, m);
253 fromsa.sin6_port = uh->uh_sport;
255 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
257 struct ip6_moptions *imo;
259 INP_INFO_RLOCK(&V_udbinfo);
261 * In the event that laddr should be set to the link-local
262 * address (this happens in RIPng), the multicast address
263 * specified in the received packet will not match laddr. To
264 * handle this situation, matching is relaxed if the
265 * receiving interface is the same as one specified in the
266 * socket and if the destination multicast address matches
267 * one of the multicast groups specified in the socket.
271 * KAME note: traditionally we dropped udpiphdr from mbuf
272 * here. We need udphdr for IPsec processing so we do that
276 LIST_FOREACH(inp, &V_udb, inp_list) {
277 if ((inp->inp_vflag & INP_IPV6) == 0)
279 if (inp->inp_lport != uh->uh_dport)
281 if (inp->inp_fport != 0 &&
282 inp->inp_fport != uh->uh_sport)
284 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
285 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
289 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
290 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
292 inp->inp_fport != uh->uh_sport)
297 * XXXRW: Because we weren't holding either the inpcb
298 * or the hash lock when we checked for a match
299 * before, we should probably recheck now that the
300 * inpcb lock is (supposed to be) held.
304 * Handle socket delivery policy for any-source
305 * and source-specific multicast. [RFC3678]
307 imo = inp->in6p_moptions;
308 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
309 struct sockaddr_in6 mcaddr;
314 bzero(&mcaddr, sizeof(struct sockaddr_in6));
315 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
316 mcaddr.sin6_family = AF_INET6;
317 mcaddr.sin6_addr = ip6->ip6_dst;
319 blocked = im6o_mc_filter(imo, ifp,
320 (struct sockaddr *)&mcaddr,
321 (struct sockaddr *)&fromsa);
322 if (blocked != MCAST_PASS) {
323 if (blocked == MCAST_NOTGMEMBER)
324 IP6STAT_INC(ip6s_notmember);
325 if (blocked == MCAST_NOTSMEMBER ||
326 blocked == MCAST_MUTED)
327 UDPSTAT_INC(udps_filtermcast);
328 INP_RUNLOCK(inp); /* XXX */
337 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
339 up = intoudpcb(last);
340 if (up->u_tun_func == NULL) {
341 udp6_append(last, n, off, &fromsa);
344 * Engage the tunneling
345 * protocol we will have to
346 * leave the info_lock up,
347 * since we are hunting
348 * through multiple UDP's.
351 (*up->u_tun_func)(n, off, last);
358 * Don't look for additional matches if this one does
359 * not have either the SO_REUSEPORT or SO_REUSEADDR
360 * socket options set. This heuristic avoids
361 * searching through all pcbs in the common case of a
362 * non-shared port. It assumes that an application
363 * will never clear these options after setting them.
365 if ((last->inp_socket->so_options &
366 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
372 * No matching pcb found; discard datagram. (No need
373 * to send an ICMP Port Unreachable for a broadcast
374 * or multicast datgram.)
376 UDPSTAT_INC(udps_noport);
377 UDPSTAT_INC(udps_noportmcast);
381 INP_INFO_RUNLOCK(&V_udbinfo);
382 up = intoudpcb(last);
383 UDP_PROBE(receive, NULL, last, ip6, last, uh);
384 if (up->u_tun_func == NULL) {
385 udp6_append(last, m, off, &fromsa);
388 * Engage the tunneling protocol.
390 (*up->u_tun_func)(m, off, last);
393 return (IPPROTO_DONE);
396 * Locate pcb for datagram.
400 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
402 if ((m->m_flags & M_IP6_NEXTHOP) &&
403 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
404 struct sockaddr_in6 *next_hop6;
406 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
409 * Transparently forwarded. Pretend to be the destination.
410 * Already got one like this?
412 inp = in6_pcblookup_mbuf(&V_udbinfo,
413 &ip6->ip6_src, uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
414 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
417 * It's new. Try to find the ambushing socket.
418 * Because we've rewritten the destination address,
419 * any hardware-generated hash is ignored.
421 inp = in6_pcblookup(&V_udbinfo, &ip6->ip6_src,
422 uh->uh_sport, &next_hop6->sin6_addr,
423 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
424 uh->uh_dport, INPLOOKUP_WILDCARD |
425 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
427 /* Remove the tag from the packet. We don't need it anymore. */
428 m_tag_delete(m, fwd_tag);
429 m->m_flags &= ~M_IP6_NEXTHOP;
431 inp = in6_pcblookup_mbuf(&V_udbinfo, &ip6->ip6_src,
432 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
433 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
434 m->m_pkthdr.rcvif, m);
436 if (udp_log_in_vain) {
437 char ip6bufs[INET6_ADDRSTRLEN];
438 char ip6bufd[INET6_ADDRSTRLEN];
441 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
442 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
444 ip6_sprintf(ip6bufs, &ip6->ip6_src),
445 ntohs(uh->uh_sport));
447 UDPSTAT_INC(udps_noport);
448 if (m->m_flags & M_MCAST) {
449 printf("UDP6: M_MCAST is set in a unicast packet.\n");
450 UDPSTAT_INC(udps_noportmcast);
455 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
457 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
458 return (IPPROTO_DONE);
460 INP_RLOCK_ASSERT(inp);
462 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
463 if (up->u_tun_func == NULL) {
464 udp6_append(inp, m, off, &fromsa);
467 * Engage the tunneling protocol.
470 (*up->u_tun_func)(m, off, inp);
473 return (IPPROTO_DONE);
476 INP_INFO_RUNLOCK(&V_udbinfo);
480 return (IPPROTO_DONE);
484 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
490 struct ip6ctlparam *ip6cp = NULL;
491 const struct sockaddr_in6 *sa6_src = NULL;
493 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
494 struct udp_portonly {
499 if (sa->sa_family != AF_INET6 ||
500 sa->sa_len != sizeof(struct sockaddr_in6))
503 if ((unsigned)cmd >= PRC_NCMDS)
505 if (PRC_IS_REDIRECT(cmd))
506 notify = in6_rtchange, d = NULL;
507 else if (cmd == PRC_HOSTDEAD)
509 else if (inet6ctlerrmap[cmd] == 0)
512 /* if the parameter is from icmp6, decode it. */
514 ip6cp = (struct ip6ctlparam *)d;
516 ip6 = ip6cp->ip6c_ip6;
517 off = ip6cp->ip6c_off;
518 cmdarg = ip6cp->ip6c_cmdarg;
519 sa6_src = ip6cp->ip6c_src;
529 * XXX: We assume that when IPV6 is non NULL,
530 * M and OFF are valid.
533 /* Check if we can safely examine src and dst ports. */
534 if (m->m_pkthdr.len < off + sizeof(*uhp))
537 bzero(&uh, sizeof(uh));
538 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
540 (void) in6_pcbnotify(&V_udbinfo, sa, uh.uh_dport,
541 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
544 (void) in6_pcbnotify(&V_udbinfo, sa, 0,
545 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
549 udp6_getcred(SYSCTL_HANDLER_ARGS)
552 struct sockaddr_in6 addrs[2];
556 error = priv_check(req->td, PRIV_NETINET_GETCRED);
560 if (req->newlen != sizeof(addrs))
562 if (req->oldlen != sizeof(struct xucred))
564 error = SYSCTL_IN(req, addrs, sizeof(addrs));
567 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
568 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
571 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
572 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
573 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
575 INP_RLOCK_ASSERT(inp);
576 if (inp->inp_socket == NULL)
579 error = cr_canseesocket(req->td->td_ucred,
582 cru2x(inp->inp_cred, &xuc);
587 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
591 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
592 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
595 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
596 struct mbuf *control, struct thread *td)
598 u_int32_t ulen = m->m_pkthdr.len;
599 u_int32_t plen = sizeof(struct udphdr) + ulen;
602 struct in6_addr *laddr, *faddr, in6a;
603 struct sockaddr_in6 *sin6 = NULL;
604 struct ifnet *oifp = NULL;
605 int scope_ambiguous = 0;
608 struct ip6_pktopts *optp, opt;
609 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
611 struct sockaddr_in6 tmp;
613 INP_WLOCK_ASSERT(inp);
614 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
617 /* addr6 has been validated in udp6_send(). */
618 sin6 = (struct sockaddr_in6 *)addr6;
620 /* protect *sin6 from overwrites */
625 * Application should provide a proper zone ID or the use of
626 * default zone IDs should be enabled. Unfortunately, some
627 * applications do not behave as it should, so we need a
628 * workaround. Even if an appropriate ID is not determined,
629 * we'll see if we can determine the outgoing interface. If we
630 * can, determine the zone ID based on the interface below.
632 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
634 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
639 if ((error = ip6_setpktopts(control, &opt,
640 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
644 optp = inp->in6p_outputopts;
647 faddr = &sin6->sin6_addr;
650 * Since we saw no essential reason for calling in_pcbconnect,
651 * we get rid of such kind of logic, and call in6_selectsrc
652 * and in6_pcbsetport in order to fill in the local address
653 * and the local port.
655 if (sin6->sin6_port == 0) {
656 error = EADDRNOTAVAIL;
660 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
661 /* how about ::ffff:0.0.0.0 case? */
666 fport = sin6->sin6_port; /* allow 0 port */
668 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
669 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
671 * I believe we should explicitly discard the
672 * packet when mapped addresses are disabled,
673 * rather than send the packet as an IPv6 one.
674 * If we chose the latter approach, the packet
675 * might be sent out on the wire based on the
676 * default route, the situation which we'd
677 * probably want to avoid.
678 * (20010421 jinmei@kame.net)
683 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
684 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
686 * when remote addr is an IPv4-mapped address,
687 * local addr should not be an IPv6 address,
688 * since you cannot determine how to map IPv6
689 * source address to IPv4.
698 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
699 error = in6_selectsrc(sin6, optp, inp, NULL,
700 td->td_ucred, &oifp, &in6a);
703 if (oifp && scope_ambiguous &&
704 (error = in6_setscope(&sin6->sin6_addr,
710 laddr = &inp->in6p_laddr; /* XXX */
713 error = EADDRNOTAVAIL;
716 if (inp->inp_lport == 0 &&
717 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
718 /* Undo an address bind that may have occurred. */
719 inp->in6p_laddr = in6addr_any;
723 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
727 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
728 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
730 * XXX: this case would happen when the
731 * application sets the V6ONLY flag after
732 * connecting the foreign address.
733 * Such applications should be fixed,
736 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
737 "option was set for a connected socket\n");
743 laddr = &inp->in6p_laddr;
744 faddr = &inp->in6p_faddr;
745 fport = inp->inp_fport;
749 hlen = sizeof(struct ip);
752 * Calculate data length and get a mbuf
753 * for UDP and IP6 headers.
755 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
762 * Stuff checksum and output datagram.
764 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
765 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
766 udp6->uh_dport = fport;
768 udp6->uh_ulen = htons((u_short)plen);
775 ip6 = mtod(m, struct ip6_hdr *);
776 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
777 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
778 ip6->ip6_vfc |= IPV6_VERSION;
779 ip6->ip6_plen = htons((u_short)plen);
780 ip6->ip6_nxt = IPPROTO_UDP;
781 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
782 ip6->ip6_src = *laddr;
783 ip6->ip6_dst = *faddr;
785 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, IPPROTO_UDP, 0);
786 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
787 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
791 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
792 UDPSTAT_INC(udps_opackets);
793 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
797 error = EAFNOSUPPORT;
807 ip6_clearpktopts(&opt, -1);
814 udp6_abort(struct socket *so)
819 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
822 if (inp->inp_vflag & INP_IPV4) {
823 struct pr_usrreqs *pru;
825 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
826 (*pru->pru_abort)(so);
832 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
833 INP_HASH_WLOCK(&V_udbinfo);
834 in6_pcbdisconnect(inp);
835 inp->in6p_laddr = in6addr_any;
836 INP_HASH_WUNLOCK(&V_udbinfo);
837 soisdisconnected(so);
843 udp6_attach(struct socket *so, int proto, struct thread *td)
849 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
851 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
852 error = soreserve(so, udp_sendspace, udp_recvspace);
856 INP_INFO_WLOCK(&V_udbinfo);
857 error = in_pcballoc(so, &V_udbinfo);
859 INP_INFO_WUNLOCK(&V_udbinfo);
862 inp = (struct inpcb *)so->so_pcb;
863 inp->inp_vflag |= INP_IPV6;
864 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
865 inp->inp_vflag |= INP_IPV4;
866 inp->in6p_hops = -1; /* use kernel default */
867 inp->in6p_cksum = -1; /* just to be sure */
870 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
871 * because the socket may be bound to an IPv6 wildcard address,
872 * which may match an IPv4-mapped IPv6 address.
874 inp->inp_ip_ttl = V_ip_defttl;
876 error = udp_newudpcb(inp);
880 INP_INFO_WUNLOCK(&V_udbinfo);
884 INP_INFO_WUNLOCK(&V_udbinfo);
889 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
895 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
898 INP_HASH_WLOCK(&V_udbinfo);
899 inp->inp_vflag &= ~INP_IPV4;
900 inp->inp_vflag |= INP_IPV6;
901 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
902 struct sockaddr_in6 *sin6_p;
904 sin6_p = (struct sockaddr_in6 *)nam;
906 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
907 inp->inp_vflag |= INP_IPV4;
909 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
910 struct sockaddr_in sin;
912 in6_sin6_2_sin(&sin, sin6_p);
913 inp->inp_vflag |= INP_IPV4;
914 inp->inp_vflag &= ~INP_IPV6;
915 error = in_pcbbind(inp, (struct sockaddr *)&sin,
922 error = in6_pcbbind(inp, nam, td->td_ucred);
926 INP_HASH_WUNLOCK(&V_udbinfo);
932 udp6_close(struct socket *so)
937 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
940 if (inp->inp_vflag & INP_IPV4) {
941 struct pr_usrreqs *pru;
943 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
944 (*pru->pru_disconnect)(so);
949 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
950 INP_HASH_WLOCK(&V_udbinfo);
951 in6_pcbdisconnect(inp);
952 inp->in6p_laddr = in6addr_any;
953 INP_HASH_WUNLOCK(&V_udbinfo);
954 soisdisconnected(so);
960 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
963 struct sockaddr_in6 *sin6;
967 sin6 = (struct sockaddr_in6 *)nam;
968 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
971 * XXXRW: Need to clarify locking of v4/v6 flags.
975 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
976 struct sockaddr_in sin;
978 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
982 if (inp->inp_faddr.s_addr != INADDR_ANY) {
986 in6_sin6_2_sin(&sin, sin6);
987 inp->inp_vflag |= INP_IPV4;
988 inp->inp_vflag &= ~INP_IPV6;
989 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
992 INP_HASH_WLOCK(&V_udbinfo);
993 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
995 INP_HASH_WUNLOCK(&V_udbinfo);
1001 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1005 inp->inp_vflag &= ~INP_IPV4;
1006 inp->inp_vflag |= INP_IPV6;
1007 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1010 INP_HASH_WLOCK(&V_udbinfo);
1011 error = in6_pcbconnect(inp, nam, td->td_ucred);
1012 INP_HASH_WUNLOCK(&V_udbinfo);
1021 udp6_detach(struct socket *so)
1026 inp = sotoinpcb(so);
1027 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1029 INP_INFO_WLOCK(&V_udbinfo);
1031 up = intoudpcb(inp);
1032 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1035 INP_INFO_WUNLOCK(&V_udbinfo);
1040 udp6_disconnect(struct socket *so)
1045 inp = sotoinpcb(so);
1046 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1049 if (inp->inp_vflag & INP_IPV4) {
1050 struct pr_usrreqs *pru;
1052 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1053 (void)(*pru->pru_disconnect)(so);
1060 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1065 INP_HASH_WLOCK(&V_udbinfo);
1066 in6_pcbdisconnect(inp);
1067 inp->in6p_laddr = in6addr_any;
1068 INP_HASH_WUNLOCK(&V_udbinfo);
1070 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1078 udp6_send(struct socket *so, int flags, struct mbuf *m,
1079 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1084 inp = sotoinpcb(so);
1085 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1089 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1093 if (addr->sa_family != AF_INET6) {
1094 error = EAFNOSUPPORT;
1100 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1102 struct sockaddr_in6 *sin6 = 0;
1105 hasv4addr = (inp->inp_vflag & INP_IPV4);
1107 sin6 = (struct sockaddr_in6 *)addr;
1108 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1112 struct pr_usrreqs *pru;
1115 * XXXRW: We release UDP-layer locks before calling
1116 * udp_send() in order to avoid recursion. However,
1117 * this does mean there is a short window where inp's
1118 * fields are unstable. Could this lead to a
1119 * potential race in which the factors causing us to
1120 * select the UDPv4 output routine are invalidated?
1124 in6_sin6_2_sin_in_sock(addr);
1125 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1126 /* addr will just be freed in sendit(). */
1127 return ((*pru->pru_send)(so, flags, m, addr, control,
1133 mac_inpcb_create_mbuf(inp, m);
1135 INP_HASH_WLOCK(&V_udbinfo);
1136 error = udp6_output(inp, m, addr, control, td);
1137 INP_HASH_WUNLOCK(&V_udbinfo);
1149 struct pr_usrreqs udp6_usrreqs = {
1150 .pru_abort = udp6_abort,
1151 .pru_attach = udp6_attach,
1152 .pru_bind = udp6_bind,
1153 .pru_connect = udp6_connect,
1154 .pru_control = in6_control,
1155 .pru_detach = udp6_detach,
1156 .pru_disconnect = udp6_disconnect,
1157 .pru_peeraddr = in6_mapped_peeraddr,
1158 .pru_send = udp6_send,
1159 .pru_shutdown = udp_shutdown,
1160 .pru_sockaddr = in6_mapped_sockaddr,
1161 .pru_soreceive = soreceive_dgram,
1162 .pru_sosend = sosend_dgram,
1163 .pru_sosetlabel = in_pcbsosetlabel,
1164 .pru_close = udp6_close