2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * Copyright (c) 2010-2011 Juniper Networks, Inc.
6 * Copyright (c) 2014 Kevin Lo
9 * Portions of this software were developed by Robert N. M. Watson under
10 * contract to Juniper Networks, Inc.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the project nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
37 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
41 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
42 * The Regents of the University of California.
43 * All rights reserved.
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. Neither the name of the University nor the names of its contributors
54 * may be used to endorse or promote products derived from this software
55 * without specific prior written permission.
57 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
72 #include <sys/cdefs.h>
73 __FBSDID("$FreeBSD$");
76 #include "opt_inet6.h"
77 #include "opt_ipsec.h"
80 #include <sys/param.h>
82 #include <sys/kernel.h>
87 #include <sys/protosw.h>
89 #include <sys/signalvar.h>
90 #include <sys/socket.h>
91 #include <sys/socketvar.h>
93 #include <sys/sysctl.h>
94 #include <sys/syslog.h>
95 #include <sys/systm.h>
98 #include <net/if_var.h>
99 #include <net/if_types.h>
100 #include <net/route.h>
101 #include <net/rss_config.h>
103 #include <netinet/in.h>
104 #include <netinet/in_kdtrace.h>
105 #include <netinet/in_pcb.h>
106 #include <netinet/in_systm.h>
107 #include <netinet/in_var.h>
108 #include <netinet/ip.h>
109 #include <netinet/ip6.h>
110 #include <netinet/icmp6.h>
111 #include <netinet/ip_var.h>
112 #include <netinet/udp.h>
113 #include <netinet/udp_var.h>
114 #include <netinet/udplite.h>
116 #include <netinet6/ip6protosw.h>
117 #include <netinet6/ip6_var.h>
118 #include <netinet6/in6_pcb.h>
119 #include <netinet6/in6_rss.h>
120 #include <netinet6/udp6_var.h>
121 #include <netinet6/scope6_var.h>
123 #include <netipsec/ipsec_support.h>
125 #include <security/mac/mac_framework.h>
128 * UDP protocol implementation.
129 * Per RFC 768, August, 1980.
132 extern struct protosw inetsw[];
133 static void udp6_detach(struct socket *so);
136 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
137 struct sockaddr_in6 *fromsa)
140 struct mbuf *opts = NULL, *tmp_opts;
143 INP_LOCK_ASSERT(inp);
146 * Engage the tunneling protocol.
149 if (up->u_tun_func != NULL) {
152 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&fromsa[0],
155 return (in_pcbrele_rlocked(inp));
157 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
158 /* Check AH/ESP integrity. */
159 if (IPSEC_ENABLED(ipv6)) {
160 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
167 if (mac_inpcb_check_deliver(inp, n) != 0) {
173 if (inp->inp_flags & INP_CONTROLOPTS ||
174 inp->inp_socket->so_options & SO_TIMESTAMP)
175 ip6_savecontrol(inp, n, &opts);
176 if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
177 tmp_opts = sbcreatecontrol((caddr_t)&fromsa[1],
178 sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR, IPPROTO_IPV6);
181 tmp_opts->m_next = opts;
188 m_adj(n, off + sizeof(struct udphdr));
190 so = inp->inp_socket;
191 SOCKBUF_LOCK(&so->so_rcv);
192 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
194 SOCKBUF_UNLOCK(&so->so_rcv);
198 UDPSTAT_INC(udps_fullsock);
200 sorwakeup_locked(so);
205 udp6_input(struct mbuf **mp, int *offp, int proto)
207 struct mbuf *m = *mp;
212 struct inpcbinfo *pcbinfo;
217 struct sockaddr_in6 fromsa[2];
218 struct m_tag *fwd_tag;
222 ifp = m->m_pkthdr.rcvif;
223 ip6 = mtod(m, struct ip6_hdr *);
225 #ifndef PULLDOWN_TEST
226 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
227 ip6 = mtod(m, struct ip6_hdr *);
228 uh = (struct udphdr *)((caddr_t)ip6 + off);
230 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
232 return (IPPROTO_DONE);
235 UDPSTAT_INC(udps_ipackets);
238 * Destination port of 0 is illegal, based on RFC768.
240 if (uh->uh_dport == 0)
243 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
244 ulen = ntohs((u_short)uh->uh_ulen);
247 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
248 if (nxt == IPPROTO_UDPLITE) {
249 /* Zero means checksum over the complete packet. */
254 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
255 /* XXX: What is the right UDPLite MIB counter? */
258 if (uh->uh_sum == 0) {
259 /* XXX: What is the right UDPLite MIB counter? */
263 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
264 UDPSTAT_INC(udps_badlen);
267 if (uh->uh_sum == 0) {
268 UDPSTAT_INC(udps_nosum);
273 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
275 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
276 uh_sum = m->m_pkthdr.csum_data;
278 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
279 m->m_pkthdr.csum_data);
282 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
285 UDPSTAT_INC(udps_badsum);
290 * Construct sockaddr format source address.
292 init_sin6(&fromsa[0], m, 0);
293 fromsa[0].sin6_port = uh->uh_sport;
294 init_sin6(&fromsa[1], m, 1);
295 fromsa[1].sin6_port = uh->uh_dport;
297 pcbinfo = udp_get_inpcbinfo(nxt);
298 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
300 struct inpcbhead *pcblist;
301 struct ip6_moptions *imo;
303 INP_INFO_RLOCK(pcbinfo);
305 * In the event that laddr should be set to the link-local
306 * address (this happens in RIPng), the multicast address
307 * specified in the received packet will not match laddr. To
308 * handle this situation, matching is relaxed if the
309 * receiving interface is the same as one specified in the
310 * socket and if the destination multicast address matches
311 * one of the multicast groups specified in the socket.
315 * KAME note: traditionally we dropped udpiphdr from mbuf
316 * here. We need udphdr for IPsec processing so we do that
319 pcblist = udp_get_pcblist(nxt);
321 LIST_FOREACH(inp, pcblist, inp_list) {
322 if ((inp->inp_vflag & INP_IPV6) == 0)
324 if (inp->inp_lport != uh->uh_dport)
326 if (inp->inp_fport != 0 &&
327 inp->inp_fport != uh->uh_sport)
329 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
330 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
334 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
335 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
337 inp->inp_fport != uh->uh_sport)
342 * XXXRW: Because we weren't holding either the inpcb
343 * or the hash lock when we checked for a match
344 * before, we should probably recheck now that the
345 * inpcb lock is (supposed to be) held.
349 * Handle socket delivery policy for any-source
350 * and source-specific multicast. [RFC3678]
352 imo = inp->in6p_moptions;
353 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
354 struct sockaddr_in6 mcaddr;
359 bzero(&mcaddr, sizeof(struct sockaddr_in6));
360 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
361 mcaddr.sin6_family = AF_INET6;
362 mcaddr.sin6_addr = ip6->ip6_dst;
364 blocked = im6o_mc_filter(imo, ifp,
365 (struct sockaddr *)&mcaddr,
366 (struct sockaddr *)&fromsa[0]);
367 if (blocked != MCAST_PASS) {
368 if (blocked == MCAST_NOTGMEMBER)
369 IP6STAT_INC(ip6s_notmember);
370 if (blocked == MCAST_NOTSMEMBER ||
371 blocked == MCAST_MUTED)
372 UDPSTAT_INC(udps_filtermcast);
373 INP_RUNLOCK(inp); /* XXX */
382 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
385 UDP_PROBE(receive, NULL, last, ip6,
387 if (udp6_append(last, n, off, fromsa))
394 * Don't look for additional matches if this one does
395 * not have either the SO_REUSEPORT or SO_REUSEADDR
396 * socket options set. This heuristic avoids
397 * searching through all pcbs in the common case of a
398 * non-shared port. It assumes that an application
399 * will never clear these options after setting them.
401 if ((last->inp_socket->so_options &
402 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
408 * No matching pcb found; discard datagram. (No need
409 * to send an ICMP Port Unreachable for a broadcast
410 * or multicast datgram.)
412 UDPSTAT_INC(udps_noport);
413 UDPSTAT_INC(udps_noportmcast);
417 INP_INFO_RUNLOCK(pcbinfo);
418 UDP_PROBE(receive, NULL, last, ip6, last, uh);
419 if (udp6_append(last, m, off, fromsa) == 0)
422 return (IPPROTO_DONE);
425 * Locate pcb for datagram.
429 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
431 if ((m->m_flags & M_IP6_NEXTHOP) &&
432 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
433 struct sockaddr_in6 *next_hop6;
435 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
438 * Transparently forwarded. Pretend to be the destination.
439 * Already got one like this?
441 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
442 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
443 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
446 * It's new. Try to find the ambushing socket.
447 * Because we've rewritten the destination address,
448 * any hardware-generated hash is ignored.
450 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
451 uh->uh_sport, &next_hop6->sin6_addr,
452 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
453 uh->uh_dport, INPLOOKUP_WILDCARD |
454 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
456 /* Remove the tag from the packet. We don't need it anymore. */
457 m_tag_delete(m, fwd_tag);
458 m->m_flags &= ~M_IP6_NEXTHOP;
460 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
461 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
462 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
463 m->m_pkthdr.rcvif, m);
465 if (udp_log_in_vain) {
466 char ip6bufs[INET6_ADDRSTRLEN];
467 char ip6bufd[INET6_ADDRSTRLEN];
470 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
471 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
473 ip6_sprintf(ip6bufs, &ip6->ip6_src),
474 ntohs(uh->uh_sport));
476 UDPSTAT_INC(udps_noport);
477 if (m->m_flags & M_MCAST) {
478 printf("UDP6: M_MCAST is set in a unicast packet.\n");
479 UDPSTAT_INC(udps_noportmcast);
484 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
485 return (IPPROTO_DONE);
487 INP_RLOCK_ASSERT(inp);
490 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
493 return (IPPROTO_DONE);
496 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
497 if (udp6_append(inp, m, off, fromsa) == 0)
499 return (IPPROTO_DONE);
502 INP_INFO_RUNLOCK(pcbinfo);
506 return (IPPROTO_DONE);
510 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
511 struct inpcbinfo *pcbinfo)
517 struct ip6ctlparam *ip6cp = NULL;
518 const struct sockaddr_in6 *sa6_src = NULL;
520 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
521 struct udp_portonly {
526 if (sa->sa_family != AF_INET6 ||
527 sa->sa_len != sizeof(struct sockaddr_in6))
530 if ((unsigned)cmd >= PRC_NCMDS)
532 if (PRC_IS_REDIRECT(cmd))
533 notify = in6_rtchange, d = NULL;
534 else if (cmd == PRC_HOSTDEAD)
536 else if (inet6ctlerrmap[cmd] == 0)
539 /* if the parameter is from icmp6, decode it. */
541 ip6cp = (struct ip6ctlparam *)d;
543 ip6 = ip6cp->ip6c_ip6;
544 off = ip6cp->ip6c_off;
545 cmdarg = ip6cp->ip6c_cmdarg;
546 sa6_src = ip6cp->ip6c_src;
556 * XXX: We assume that when IPV6 is non NULL,
557 * M and OFF are valid.
560 /* Check if we can safely examine src and dst ports. */
561 if (m->m_pkthdr.len < off + sizeof(*uhp))
564 bzero(&uh, sizeof(uh));
565 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
567 if (!PRC_IS_REDIRECT(cmd)) {
568 /* Check to see if its tunneled */
570 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
571 uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
572 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
573 m->m_pkthdr.rcvif, m);
578 if (up->u_icmp_func) {
581 (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
590 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
591 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
594 (void)in6_pcbnotify(pcbinfo, sa, 0,
595 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
599 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
602 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
606 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
609 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
613 udp6_getcred(SYSCTL_HANDLER_ARGS)
616 struct sockaddr_in6 addrs[2];
620 error = priv_check(req->td, PRIV_NETINET_GETCRED);
624 if (req->newlen != sizeof(addrs))
626 if (req->oldlen != sizeof(struct xucred))
628 error = SYSCTL_IN(req, addrs, sizeof(addrs));
631 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
632 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
635 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
636 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
637 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
639 INP_RLOCK_ASSERT(inp);
640 if (inp->inp_socket == NULL)
643 error = cr_canseesocket(req->td->td_ucred,
646 cru2x(inp->inp_cred, &xuc);
651 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
655 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
656 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
659 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
660 struct mbuf *control, struct thread *td)
662 u_int32_t ulen = m->m_pkthdr.len;
663 u_int32_t plen = sizeof(struct udphdr) + ulen;
666 struct in6_addr *laddr, *faddr, in6a;
667 struct sockaddr_in6 *sin6 = NULL;
668 int cscov_partial = 0;
669 int scope_ambiguous = 0;
674 struct ip6_pktopts *optp, opt;
675 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
677 struct sockaddr_in6 tmp;
679 INP_WLOCK_ASSERT(inp);
680 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
683 /* addr6 has been validated in udp6_send(). */
684 sin6 = (struct sockaddr_in6 *)addr6;
686 /* protect *sin6 from overwrites */
691 * Application should provide a proper zone ID or the use of
692 * default zone IDs should be enabled. Unfortunately, some
693 * applications do not behave as it should, so we need a
694 * workaround. Even if an appropriate ID is not determined,
695 * we'll see if we can determine the outgoing interface. If we
696 * can, determine the zone ID based on the interface below.
698 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
700 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
704 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
705 IPPROTO_UDP : IPPROTO_UDPLITE;
707 if ((error = ip6_setpktopts(control, &opt,
708 inp->in6p_outputopts, td->td_ucred, nxt)) != 0)
712 optp = inp->in6p_outputopts;
715 faddr = &sin6->sin6_addr;
718 * Since we saw no essential reason for calling in_pcbconnect,
719 * we get rid of such kind of logic, and call in6_selectsrc
720 * and in6_pcbsetport in order to fill in the local address
721 * and the local port.
723 if (sin6->sin6_port == 0) {
724 error = EADDRNOTAVAIL;
728 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
729 /* how about ::ffff:0.0.0.0 case? */
734 fport = sin6->sin6_port; /* allow 0 port */
736 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
737 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
739 * I believe we should explicitly discard the
740 * packet when mapped addresses are disabled,
741 * rather than send the packet as an IPv6 one.
742 * If we chose the latter approach, the packet
743 * might be sent out on the wire based on the
744 * default route, the situation which we'd
745 * probably want to avoid.
746 * (20010421 jinmei@kame.net)
751 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
752 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
754 * when remote addr is an IPv4-mapped address,
755 * local addr should not be an IPv6 address,
756 * since you cannot determine how to map IPv6
757 * source address to IPv4.
766 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
767 error = in6_selectsrc_socket(sin6, optp, inp,
768 td->td_ucred, scope_ambiguous, &in6a, NULL);
773 laddr = &inp->in6p_laddr; /* XXX */
776 error = EADDRNOTAVAIL;
779 if (inp->inp_lport == 0 &&
780 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
781 /* Undo an address bind that may have occurred. */
782 inp->in6p_laddr = in6addr_any;
786 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
790 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
791 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
793 * XXX: this case would happen when the
794 * application sets the V6ONLY flag after
795 * connecting the foreign address.
796 * Such applications should be fixed,
799 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
800 "option was set for a connected socket\n");
806 laddr = &inp->in6p_laddr;
807 faddr = &inp->in6p_faddr;
808 fport = inp->inp_fport;
812 hlen = sizeof(struct ip);
815 * Calculate data length and get a mbuf
816 * for UDP and IP6 headers.
818 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
825 * Stuff checksum and output datagram.
827 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
828 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
829 udp6->uh_dport = fport;
830 if (nxt == IPPROTO_UDPLITE) {
834 cscov = up->u_txcslen;
837 udp6->uh_ulen = htons(cscov);
839 * For UDP-Lite, checksum coverage length of zero means
840 * the entire UDPLite packet is covered by the checksum.
842 cscov_partial = (cscov == 0) ? 0 : 1;
843 } else if (plen <= 0xffff)
844 udp6->uh_ulen = htons((u_short)plen);
851 ip6 = mtod(m, struct ip6_hdr *);
852 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
853 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
854 ip6->ip6_vfc |= IPV6_VERSION;
855 ip6->ip6_plen = htons((u_short)plen);
857 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
858 ip6->ip6_src = *laddr;
859 ip6->ip6_dst = *faddr;
862 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
863 sizeof(struct ip6_hdr), plen, cscov)) == 0)
864 udp6->uh_sum = 0xffff;
866 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
867 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
868 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
873 uint32_t hash_val, hash_type;
876 pr = inp->inp_socket->so_proto->pr_protocol;
878 * Calculate an appropriate RSS hash for UDP and
881 * The called function will take care of figuring out
882 * whether a 2-tuple or 4-tuple hash is required based
883 * on the currently configured scheme.
885 * Later later on connected socket values should be
886 * cached in the inpcb and reused, rather than constantly
889 * UDP Lite is a different protocol number and will
890 * likely end up being hashed as a 2-tuple until
891 * RSS / NICs grow UDP Lite protocol awareness.
893 if (rss_proto_software_hash_v6(faddr, laddr, fport,
894 inp->inp_lport, pr, &hash_val, &hash_type) == 0) {
895 m->m_pkthdr.flowid = hash_val;
896 M_HASHTYPE_SET(m, hash_type);
903 * Don't override with the inp cached flowid.
905 * Until the whole UDP path is vetted, it may actually
908 flags |= IP_NODEFAULTFLOWID;
911 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
912 UDPSTAT_INC(udps_opackets);
913 error = ip6_output(m, optp, &inp->inp_route6, flags,
914 inp->in6p_moptions, NULL, inp);
917 error = EAFNOSUPPORT;
927 ip6_clearpktopts(&opt, -1);
934 udp6_abort(struct socket *so)
937 struct inpcbinfo *pcbinfo;
939 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
941 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
945 if (inp->inp_vflag & INP_IPV4) {
946 struct pr_usrreqs *pru;
949 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
950 IPPROTO_UDP : IPPROTO_UDPLITE;
952 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
953 (*pru->pru_abort)(so);
958 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
959 INP_HASH_WLOCK(pcbinfo);
960 in6_pcbdisconnect(inp);
961 inp->in6p_laddr = in6addr_any;
962 INP_HASH_WUNLOCK(pcbinfo);
963 soisdisconnected(so);
969 udp6_attach(struct socket *so, int proto, struct thread *td)
972 struct inpcbinfo *pcbinfo;
975 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
977 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
979 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
980 error = soreserve(so, udp_sendspace, udp_recvspace);
984 INP_INFO_WLOCK(pcbinfo);
985 error = in_pcballoc(so, pcbinfo);
987 INP_INFO_WUNLOCK(pcbinfo);
990 inp = (struct inpcb *)so->so_pcb;
991 inp->inp_vflag |= INP_IPV6;
992 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
993 inp->inp_vflag |= INP_IPV4;
994 inp->in6p_hops = -1; /* use kernel default */
995 inp->in6p_cksum = -1; /* just to be sure */
998 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
999 * because the socket may be bound to an IPv6 wildcard address,
1000 * which may match an IPv4-mapped IPv6 address.
1002 inp->inp_ip_ttl = V_ip_defttl;
1004 error = udp_newudpcb(inp);
1008 INP_INFO_WUNLOCK(pcbinfo);
1012 INP_INFO_WUNLOCK(pcbinfo);
1017 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1020 struct inpcbinfo *pcbinfo;
1023 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1024 inp = sotoinpcb(so);
1025 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1028 INP_HASH_WLOCK(pcbinfo);
1029 inp->inp_vflag &= ~INP_IPV4;
1030 inp->inp_vflag |= INP_IPV6;
1031 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1032 struct sockaddr_in6 *sin6_p;
1034 sin6_p = (struct sockaddr_in6 *)nam;
1036 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1037 inp->inp_vflag |= INP_IPV4;
1039 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1040 struct sockaddr_in sin;
1042 in6_sin6_2_sin(&sin, sin6_p);
1043 inp->inp_vflag |= INP_IPV4;
1044 inp->inp_vflag &= ~INP_IPV6;
1045 error = in_pcbbind(inp, (struct sockaddr *)&sin,
1052 error = in6_pcbbind(inp, nam, td->td_ucred);
1056 INP_HASH_WUNLOCK(pcbinfo);
1062 udp6_close(struct socket *so)
1065 struct inpcbinfo *pcbinfo;
1067 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1068 inp = sotoinpcb(so);
1069 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1073 if (inp->inp_vflag & INP_IPV4) {
1074 struct pr_usrreqs *pru;
1077 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1078 IPPROTO_UDP : IPPROTO_UDPLITE;
1080 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1081 (*pru->pru_disconnect)(so);
1085 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1086 INP_HASH_WLOCK(pcbinfo);
1087 in6_pcbdisconnect(inp);
1088 inp->in6p_laddr = in6addr_any;
1089 INP_HASH_WUNLOCK(pcbinfo);
1090 soisdisconnected(so);
1096 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1099 struct inpcbinfo *pcbinfo;
1100 struct sockaddr_in6 *sin6;
1103 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1104 inp = sotoinpcb(so);
1105 sin6 = (struct sockaddr_in6 *)nam;
1106 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1109 * XXXRW: Need to clarify locking of v4/v6 flags.
1113 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1114 struct sockaddr_in sin;
1116 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1120 if ((inp->inp_vflag & INP_IPV4) == 0) {
1121 error = EAFNOSUPPORT;
1124 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1128 in6_sin6_2_sin(&sin, sin6);
1129 inp->inp_vflag |= INP_IPV4;
1130 inp->inp_vflag &= ~INP_IPV6;
1131 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1134 INP_HASH_WLOCK(pcbinfo);
1135 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1137 INP_HASH_WUNLOCK(pcbinfo);
1142 if ((inp->inp_vflag & INP_IPV6) == 0) {
1143 error = EAFNOSUPPORT;
1148 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1152 inp->inp_vflag &= ~INP_IPV4;
1153 inp->inp_vflag |= INP_IPV6;
1154 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1157 INP_HASH_WLOCK(pcbinfo);
1158 error = in6_pcbconnect(inp, nam, td->td_ucred);
1159 INP_HASH_WUNLOCK(pcbinfo);
1168 udp6_detach(struct socket *so)
1171 struct inpcbinfo *pcbinfo;
1174 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1175 inp = sotoinpcb(so);
1176 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1178 INP_INFO_WLOCK(pcbinfo);
1180 up = intoudpcb(inp);
1181 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1184 INP_INFO_WUNLOCK(pcbinfo);
1189 udp6_disconnect(struct socket *so)
1192 struct inpcbinfo *pcbinfo;
1195 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1196 inp = sotoinpcb(so);
1197 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1201 if (inp->inp_vflag & INP_IPV4) {
1202 struct pr_usrreqs *pru;
1205 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1206 IPPROTO_UDP : IPPROTO_UDPLITE;
1208 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1209 (void)(*pru->pru_disconnect)(so);
1214 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1219 INP_HASH_WLOCK(pcbinfo);
1220 in6_pcbdisconnect(inp);
1221 inp->in6p_laddr = in6addr_any;
1222 INP_HASH_WUNLOCK(pcbinfo);
1224 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1232 udp6_send(struct socket *so, int flags, struct mbuf *m,
1233 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1236 struct inpcbinfo *pcbinfo;
1239 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1240 inp = sotoinpcb(so);
1241 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1245 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1249 if (addr->sa_family != AF_INET6) {
1250 error = EAFNOSUPPORT;
1256 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1258 struct sockaddr_in6 *sin6 = NULL;
1261 hasv4addr = (inp->inp_vflag & INP_IPV4);
1263 sin6 = (struct sockaddr_in6 *)addr;
1264 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1268 struct pr_usrreqs *pru;
1271 nxt = (inp->inp_socket->so_proto->pr_protocol ==
1272 IPPROTO_UDP) ? IPPROTO_UDP : IPPROTO_UDPLITE;
1274 * XXXRW: We release UDP-layer locks before calling
1275 * udp_send() in order to avoid recursion. However,
1276 * this does mean there is a short window where inp's
1277 * fields are unstable. Could this lead to a
1278 * potential race in which the factors causing us to
1279 * select the UDPv4 output routine are invalidated?
1283 in6_sin6_2_sin_in_sock(addr);
1284 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1285 /* addr will just be freed in sendit(). */
1286 return ((*pru->pru_send)(so, flags, m, addr, control,
1292 mac_inpcb_create_mbuf(inp, m);
1294 INP_HASH_WLOCK(pcbinfo);
1295 error = udp6_output(inp, m, addr, control, td);
1296 INP_HASH_WUNLOCK(pcbinfo);
1306 struct pr_usrreqs udp6_usrreqs = {
1307 .pru_abort = udp6_abort,
1308 .pru_attach = udp6_attach,
1309 .pru_bind = udp6_bind,
1310 .pru_connect = udp6_connect,
1311 .pru_control = in6_control,
1312 .pru_detach = udp6_detach,
1313 .pru_disconnect = udp6_disconnect,
1314 .pru_peeraddr = in6_mapped_peeraddr,
1315 .pru_send = udp6_send,
1316 .pru_shutdown = udp_shutdown,
1317 .pru_sockaddr = in6_mapped_sockaddr,
1318 .pru_soreceive = soreceive_dgram,
1319 .pru_sosend = sosend_dgram,
1320 .pru_sosetlabel = in_pcbsosetlabel,
1321 .pru_close = udp6_close