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"
78 #include "opt_route.h"
81 #include <sys/param.h>
83 #include <sys/kernel.h>
88 #include <sys/protosw.h>
90 #include <sys/signalvar.h>
91 #include <sys/socket.h>
92 #include <sys/socketvar.h>
94 #include <sys/sysctl.h>
95 #include <sys/syslog.h>
96 #include <sys/systm.h>
99 #include <net/if_var.h>
100 #include <net/if_types.h>
101 #include <net/route.h>
102 #include <net/rss_config.h>
104 #include <netinet/in.h>
105 #include <netinet/in_kdtrace.h>
106 #include <netinet/in_pcb.h>
107 #include <netinet/in_systm.h>
108 #include <netinet/in_var.h>
109 #include <netinet/ip.h>
110 #include <netinet/ip6.h>
111 #include <netinet/icmp6.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/udp.h>
114 #include <netinet/udp_var.h>
115 #include <netinet/udplite.h>
117 #include <netinet6/ip6protosw.h>
118 #include <netinet6/ip6_var.h>
119 #include <netinet6/in6_fib.h>
120 #include <netinet6/in6_pcb.h>
121 #include <netinet6/in6_rss.h>
122 #include <netinet6/udp6_var.h>
123 #include <netinet6/scope6_var.h>
125 #include <netipsec/ipsec_support.h>
127 #include <security/mac/mac_framework.h>
129 VNET_DEFINE(int, zero_checksum_port) = 0;
130 #define V_zero_checksum_port VNET(zero_checksum_port)
131 SYSCTL_INT(_net_inet6_udp6, OID_AUTO, rfc6935_port, CTLFLAG_VNET | CTLFLAG_RW,
132 &VNET_NAME(zero_checksum_port), 0,
133 "Zero UDP checksum allowed for traffic to/from this port.");
135 * UDP protocol implementation.
136 * Per RFC 768, August, 1980.
139 extern struct protosw inetsw[];
140 static void udp6_detach(struct socket *so);
143 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
144 struct sockaddr_in6 *fromsa)
147 struct mbuf *opts = NULL, *tmp_opts;
151 INP_LOCK_ASSERT(inp);
154 * Engage the tunneling protocol.
157 if (up->u_tun_func != NULL) {
160 filtered = (*up->u_tun_func)(n, off, inp,
161 (struct sockaddr *)&fromsa[0], up->u_tun_ctx);
164 return (in_pcbrele_rlocked(inp));
166 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
167 /* Check AH/ESP integrity. */
168 if (IPSEC_ENABLED(ipv6)) {
169 if (IPSEC_CHECK_POLICY(ipv6, n, inp) != 0) {
176 if (mac_inpcb_check_deliver(inp, n) != 0) {
182 if (inp->inp_flags & INP_CONTROLOPTS ||
183 inp->inp_socket->so_options & SO_TIMESTAMP)
184 ip6_savecontrol(inp, n, &opts);
185 if ((inp->inp_vflag & INP_IPV6) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
186 tmp_opts = sbcreatecontrol(&fromsa[1],
187 sizeof(struct sockaddr_in6), IPV6_ORIGDSTADDR,
188 IPPROTO_IPV6, M_NOWAIT);
191 tmp_opts->m_next = opts;
197 m_adj(n, off + sizeof(struct udphdr));
199 so = inp->inp_socket;
200 SOCKBUF_LOCK(&so->so_rcv);
201 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)&fromsa[0], n,
203 soroverflow_locked(so);
207 UDPSTAT_INC(udps_fullsock);
209 sorwakeup_locked(so);
213 struct udp6_multi_match_ctx {
219 udp6_multi_match(const struct inpcb *inp, void *v)
221 struct udp6_multi_match_ctx *ctx = v;
223 if ((inp->inp_vflag & INP_IPV6) == 0)
225 if (inp->inp_lport != ctx->uh->uh_dport)
227 if (inp->inp_fport != 0 && inp->inp_fport != ctx->uh->uh_sport)
229 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
230 !IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr, &ctx->ip6->ip6_dst))
232 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr) &&
233 (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr, &ctx->ip6->ip6_src) ||
234 inp->inp_fport != ctx->uh->uh_sport))
241 udp6_multi_input(struct mbuf *m, int off, int proto,
242 struct sockaddr_in6 *fromsa)
244 struct udp6_multi_match_ctx ctx;
245 struct inpcb_iterator inpi = INP_ITERATOR(udp_get_inpcbinfo(proto),
246 INPLOOKUP_RLOCKPCB, udp6_multi_match, &ctx);
248 struct ip6_moptions *imo;
253 * In the event that laddr should be set to the link-local
254 * address (this happens in RIPng), the multicast address
255 * specified in the received packet will not match laddr. To
256 * handle this situation, matching is relaxed if the
257 * receiving interface is the same as one specified in the
258 * socket and if the destination multicast address matches
259 * one of the multicast groups specified in the socket.
263 * KAME note: traditionally we dropped udpiphdr from mbuf
264 * here. We need udphdr for IPsec processing so we do that
267 ctx.ip6 = mtod(m, struct ip6_hdr *);
268 ctx.uh = (struct udphdr *)((char *)ctx.ip6 + off);
269 while ((inp = inp_next(&inpi)) != NULL) {
270 INP_RLOCK_ASSERT(inp);
272 * XXXRW: Because we weren't holding either the inpcb
273 * or the hash lock when we checked for a match
274 * before, we should probably recheck now that the
275 * inpcb lock is (supposed to be) held.
278 * Handle socket delivery policy for any-source
279 * and source-specific multicast. [RFC3678]
281 if ((imo = inp->in6p_moptions) != NULL) {
282 struct sockaddr_in6 mcaddr;
285 bzero(&mcaddr, sizeof(struct sockaddr_in6));
286 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
287 mcaddr.sin6_family = AF_INET6;
288 mcaddr.sin6_addr = ctx.ip6->ip6_dst;
290 blocked = im6o_mc_filter(imo, m->m_pkthdr.rcvif,
291 (struct sockaddr *)&mcaddr,
292 (struct sockaddr *)&fromsa[0]);
293 if (blocked != MCAST_PASS) {
294 if (blocked == MCAST_NOTGMEMBER)
295 IP6STAT_INC(ip6s_notmember);
296 if (blocked == MCAST_NOTSMEMBER ||
297 blocked == MCAST_MUTED)
298 UDPSTAT_INC(udps_filtermcast);
302 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) != NULL) {
303 if (proto == IPPROTO_UDPLITE)
304 UDPLITE_PROBE(receive, NULL, inp, ctx.ip6,
307 UDP_PROBE(receive, NULL, inp, ctx.ip6, inp,
309 if (udp6_append(inp, n, off, fromsa)) {
316 * Don't look for additional matches if this one does
317 * not have either the SO_REUSEPORT or SO_REUSEADDR
318 * socket options set. This heuristic avoids
319 * searching through all pcbs in the common case of a
320 * non-shared port. It assumes that an application
321 * will never clear these options after setting them.
323 if ((inp->inp_socket->so_options &
324 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0) {
333 * No matching pcb found; discard datagram. (No need
334 * to send an ICMP Port Unreachable for a broadcast
335 * or multicast datgram.)
337 UDPSTAT_INC(udps_noport);
338 UDPSTAT_INC(udps_noportmcast);
341 return (IPPROTO_DONE);
345 udp6_input(struct mbuf **mp, int *offp, int proto)
347 struct mbuf *m = *mp;
351 struct inpcbinfo *pcbinfo;
356 struct sockaddr_in6 fromsa[2];
357 struct m_tag *fwd_tag;
363 if (m->m_len < off + sizeof(struct udphdr)) {
364 m = m_pullup(m, off + sizeof(struct udphdr));
366 IP6STAT_INC(ip6s_exthdrtoolong);
368 return (IPPROTO_DONE);
371 ip6 = mtod(m, struct ip6_hdr *);
372 uh = (struct udphdr *)((caddr_t)ip6 + off);
374 UDPSTAT_INC(udps_ipackets);
377 * Destination port of 0 is illegal, based on RFC768.
379 if (uh->uh_dport == 0)
382 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
383 ulen = ntohs((u_short)uh->uh_ulen);
386 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
387 if (nxt == IPPROTO_UDPLITE) {
388 /* Zero means checksum over the complete packet. */
393 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
394 /* XXX: What is the right UDPLite MIB counter? */
397 if (uh->uh_sum == 0) {
398 /* XXX: What is the right UDPLite MIB counter? */
402 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
403 UDPSTAT_INC(udps_badlen);
406 if (uh->uh_sum == 0) {
407 UDPSTAT_INC(udps_nosum);
409 * dport 0 was rejected earlier so this is OK even if
410 * zero_checksum_port is 0 (which is its default value).
412 if (ntohs(uh->uh_dport) == V_zero_checksum_port)
419 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
421 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
422 uh_sum = m->m_pkthdr.csum_data;
424 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
425 m->m_pkthdr.csum_data);
428 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
431 UDPSTAT_INC(udps_badsum);
437 * Construct sockaddr format source address.
439 init_sin6(&fromsa[0], m, 0);
440 fromsa[0].sin6_port = uh->uh_sport;
441 init_sin6(&fromsa[1], m, 1);
442 fromsa[1].sin6_port = uh->uh_dport;
444 pcbinfo = udp_get_inpcbinfo(nxt);
445 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
447 return (udp6_multi_input(m, off, proto, fromsa));
451 * Locate pcb for datagram.
455 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
457 if ((m->m_flags & M_IP6_NEXTHOP) &&
458 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
459 struct sockaddr_in6 *next_hop6;
461 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
464 * Transparently forwarded. Pretend to be the destination.
465 * Already got one like this?
467 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
468 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
469 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
472 * It's new. Try to find the ambushing socket.
473 * Because we've rewritten the destination address,
474 * any hardware-generated hash is ignored.
476 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
477 uh->uh_sport, &next_hop6->sin6_addr,
478 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
479 uh->uh_dport, INPLOOKUP_WILDCARD |
480 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
482 /* Remove the tag from the packet. We don't need it anymore. */
483 m_tag_delete(m, fwd_tag);
484 m->m_flags &= ~M_IP6_NEXTHOP;
486 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
487 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
488 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
489 m->m_pkthdr.rcvif, m);
491 if (V_udp_log_in_vain) {
492 char ip6bufs[INET6_ADDRSTRLEN];
493 char ip6bufd[INET6_ADDRSTRLEN];
496 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
497 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
499 ip6_sprintf(ip6bufs, &ip6->ip6_src),
500 ntohs(uh->uh_sport));
502 if (nxt == IPPROTO_UDPLITE)
503 UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
505 UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
506 UDPSTAT_INC(udps_noport);
507 if (m->m_flags & M_MCAST) {
508 printf("UDP6: M_MCAST is set in a unicast packet.\n");
509 UDPSTAT_INC(udps_noportmcast);
512 if (V_udp_blackhole && (V_udp_blackhole_local ||
513 !in6_localaddr(&ip6->ip6_src)))
515 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
517 return (IPPROTO_DONE);
519 INP_RLOCK_ASSERT(inp);
522 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
526 return (IPPROTO_DONE);
529 if (nxt == IPPROTO_UDPLITE)
530 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
532 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
533 if (udp6_append(inp, m, off, fromsa) == 0)
536 return (IPPROTO_DONE);
541 return (IPPROTO_DONE);
545 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
546 struct inpcbinfo *pcbinfo)
552 struct ip6ctlparam *ip6cp = NULL;
553 const struct sockaddr_in6 *sa6_src = NULL;
555 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
556 struct udp_portonly {
561 if (sa->sa_family != AF_INET6 ||
562 sa->sa_len != sizeof(struct sockaddr_in6))
565 if ((unsigned)cmd >= PRC_NCMDS)
567 if (PRC_IS_REDIRECT(cmd))
568 notify = in6_rtchange, d = NULL;
569 else if (cmd == PRC_HOSTDEAD)
571 else if (inet6ctlerrmap[cmd] == 0)
574 /* if the parameter is from icmp6, decode it. */
576 ip6cp = (struct ip6ctlparam *)d;
578 ip6 = ip6cp->ip6c_ip6;
579 off = ip6cp->ip6c_off;
580 cmdarg = ip6cp->ip6c_cmdarg;
581 sa6_src = ip6cp->ip6c_src;
591 * XXX: We assume that when IPV6 is non NULL,
592 * M and OFF are valid.
595 /* Check if we can safely examine src and dst ports. */
596 if (m->m_pkthdr.len < off + sizeof(*uhp))
599 bzero(&uh, sizeof(uh));
600 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
602 if (!PRC_IS_REDIRECT(cmd)) {
603 /* Check to see if its tunneled */
605 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
606 uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
607 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
608 m->m_pkthdr.rcvif, m);
613 if (up->u_icmp_func) {
616 (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
625 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
626 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
629 (void)in6_pcbnotify(pcbinfo, sa, 0,
630 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
634 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
637 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
641 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
644 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
648 udp6_getcred(SYSCTL_HANDLER_ARGS)
651 struct sockaddr_in6 addrs[2];
652 struct epoch_tracker et;
656 error = priv_check(req->td, PRIV_NETINET_GETCRED);
660 if (req->newlen != sizeof(addrs))
662 if (req->oldlen != sizeof(struct xucred))
664 error = SYSCTL_IN(req, addrs, sizeof(addrs));
667 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
668 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
672 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
673 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
674 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
677 INP_RLOCK_ASSERT(inp);
678 if (inp->inp_socket == NULL)
681 error = cr_canseesocket(req->td->td_ucred,
684 cru2x(inp->inp_cred, &xuc);
689 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
693 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred,
694 CTLTYPE_OPAQUE | CTLFLAG_RW | CTLFLAG_MPSAFE,
695 0, 0, udp6_getcred, "S,xucred",
696 "Get the xucred of a UDP6 connection");
699 udp6_output(struct socket *so, int flags_arg, struct mbuf *m,
700 struct sockaddr *addr6, struct mbuf *control, struct thread *td)
705 struct in6_addr *laddr, *faddr, in6a;
706 struct ip6_pktopts *optp, opt;
707 struct sockaddr_in6 *sin6, tmp;
708 struct epoch_tracker et;
709 int cscov_partial, error, flags, hlen, scope_ambiguous;
710 u_int32_t ulen, plen;
715 /* addr6 has been validated in udp6_send(). */
716 sin6 = (struct sockaddr_in6 *)addr6;
719 * In contrast to to IPv4 we do not validate the max. packet length
720 * here due to IPv6 Jumbograms (RFC2675).
725 /* Protect *addr6 from overwrites. */
730 * Application should provide a proper zone ID or the use of
731 * default zone IDs should be enabled. Unfortunately, some
732 * applications do not behave as it should, so we need a
733 * workaround. Even if an appropriate ID is not determined,
734 * we'll see if we can determine the outgoing interface. If we
735 * can, determine the zone ID based on the interface below.
737 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
739 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0) {
748 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
750 * In the following cases we want a write lock on the inp for either
751 * local operations or for possible route cache updates in the IPv6
753 * - on connected sockets (sin6 is NULL) for route cache updates,
754 * - when we are not bound to an address and source port (it is
755 * in6_pcbsetport() which will require the write lock).
757 * We check the inp fields before actually locking the inp, so
758 * here exists a race, and we may WLOCK the inp and end with already
759 * bound one by other thread. This is fine.
761 if (sin6 == NULL || (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
762 inp->inp_lport == 0))
767 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
768 IPPROTO_UDP : IPPROTO_UDPLITE;
771 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
775 hasv4addr = (inp->inp_vflag & INP_IPV4);
777 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
780 struct pr_usrreqs *pru;
783 * XXXRW: We release UDP-layer locks before calling
784 * udp_send() in order to avoid recursion. However,
785 * this does mean there is a short window where inp's
786 * fields are unstable. Could this lead to a
787 * potential race in which the factors causing us to
788 * select the UDPv4 output routine are invalidated?
792 in6_sin6_2_sin_in_sock((struct sockaddr *)sin6);
793 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
794 /* addr will just be freed in sendit(). */
795 return ((*pru->pru_send)(so, flags_arg | PRUS_IPV6, m,
796 (struct sockaddr *)sin6, control, td));
800 if (sin6 && IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
802 * Given this is either an IPv6-only socket or no INET is
803 * supported we will fail the send if the given destination
804 * address is a v4mapped address.
806 * XXXGL: do we leak m and control?
814 if ((error = ip6_setpktopts(control, &opt,
815 inp->in6p_outputopts, td->td_ucred, nxt)) != 0) {
820 optp = inp->in6p_outputopts;
824 * Since we saw no essential reason for calling in_pcbconnect,
825 * we get rid of such kind of logic, and call in6_selectsrc
826 * and in6_pcbsetport in order to fill in the local address
827 * and the local port.
829 if (sin6->sin6_port == 0) {
830 error = EADDRNOTAVAIL;
834 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
835 /* how about ::ffff:0.0.0.0 case? */
841 * Given we handle the v4mapped case in the INET block above
842 * assert here that it must not happen anymore.
844 KASSERT(!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr),
845 ("%s: sin6(%p)->sin6_addr is v4mapped which we "
846 "should have handled.", __func__, sin6));
848 /* This only requires read-locking. */
849 error = in6_selectsrc_socket(sin6, optp, inp,
850 td->td_ucred, scope_ambiguous, &in6a, NULL);
855 if (inp->inp_lport == 0) {
856 struct inpcbinfo *pcbinfo;
858 INP_WLOCK_ASSERT(inp);
860 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
861 INP_HASH_WLOCK(pcbinfo);
862 error = in6_pcbsetport(laddr, inp, td->td_ucred);
863 INP_HASH_WUNLOCK(pcbinfo);
865 /* Undo an address bind that may have occurred. */
866 inp->in6p_laddr = in6addr_any;
870 faddr = &sin6->sin6_addr;
871 fport = sin6->sin6_port; /* allow 0 port */
874 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
878 laddr = &inp->in6p_laddr;
879 faddr = &inp->in6p_faddr;
880 fport = inp->inp_fport;
883 ulen = m->m_pkthdr.len;
884 plen = sizeof(struct udphdr) + ulen;
885 hlen = sizeof(struct ip6_hdr);
888 * Calculate data length and get a mbuf
889 * for UDP and IP6 headers.
891 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
898 * Stuff checksum and output datagram.
900 cscov = cscov_partial = 0;
901 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
902 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
903 udp6->uh_dport = fport;
904 if (nxt == IPPROTO_UDPLITE) {
908 cscov = up->u_txcslen;
911 udp6->uh_ulen = htons(cscov);
913 * For UDP-Lite, checksum coverage length of zero means
914 * the entire UDPLite packet is covered by the checksum.
916 cscov_partial = (cscov == 0) ? 0 : 1;
917 } else if (plen <= 0xffff)
918 udp6->uh_ulen = htons((u_short)plen);
923 ip6 = mtod(m, struct ip6_hdr *);
924 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
925 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
926 ip6->ip6_vfc |= IPV6_VERSION;
927 ip6->ip6_plen = htons((u_short)plen);
929 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
930 ip6->ip6_src = *laddr;
931 ip6->ip6_dst = *faddr;
934 mac_inpcb_create_mbuf(inp, m);
938 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
939 sizeof(struct ip6_hdr), plen, cscov)) == 0)
940 udp6->uh_sum = 0xffff;
942 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
943 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
944 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
948 #if defined(ROUTE_MPATH) || defined(RSS)
949 if (CALC_FLOWID_OUTBOUND_SENDTO) {
950 uint32_t hash_type, hash_val;
953 pr = inp->inp_socket->so_proto->pr_protocol;
955 hash_val = fib6_calc_packet_hash(laddr, faddr,
956 inp->inp_lport, fport, pr, &hash_type);
957 m->m_pkthdr.flowid = hash_val;
958 M_HASHTYPE_SET(m, hash_type);
960 /* do not use inp flowid */
961 flags |= IP_NODEFAULTFLOWID;
964 UDPSTAT_INC(udps_opackets);
965 if (nxt == IPPROTO_UDPLITE)
966 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
968 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
969 error = ip6_output(m, optp,
970 INP_WLOCKED(inp) ? &inp->inp_route6 : NULL, flags,
971 inp->in6p_moptions, NULL, inp);
976 ip6_clearpktopts(&opt, -1);
985 ip6_clearpktopts(&opt, -1);
994 udp6_abort(struct socket *so)
997 struct inpcbinfo *pcbinfo;
999 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1000 inp = sotoinpcb(so);
1001 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
1005 if (inp->inp_vflag & INP_IPV4) {
1006 struct pr_usrreqs *pru;
1009 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1010 IPPROTO_UDP : IPPROTO_UDPLITE;
1012 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1013 (*pru->pru_abort)(so);
1018 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1019 INP_HASH_WLOCK(pcbinfo);
1020 in6_pcbdisconnect(inp);
1021 inp->in6p_laddr = in6addr_any;
1022 INP_HASH_WUNLOCK(pcbinfo);
1023 soisdisconnected(so);
1029 udp6_attach(struct socket *so, int proto, struct thread *td)
1032 struct inpcbinfo *pcbinfo;
1035 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1036 inp = sotoinpcb(so);
1037 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1039 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1040 error = soreserve(so, udp_sendspace, udp_recvspace);
1044 error = in_pcballoc(so, pcbinfo);
1047 inp = (struct inpcb *)so->so_pcb;
1048 inp->in6p_cksum = -1; /* just to be sure */
1051 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1052 * because the socket may be bound to an IPv6 wildcard address,
1053 * which may match an IPv4-mapped IPv6 address.
1055 inp->inp_ip_ttl = V_ip_defttl;
1057 error = udp_newudpcb(inp);
1068 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1071 struct inpcbinfo *pcbinfo;
1075 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1076 inp = sotoinpcb(so);
1077 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1079 if (nam->sa_family != AF_INET6)
1080 return (EAFNOSUPPORT);
1081 if (nam->sa_len != sizeof(struct sockaddr_in6))
1085 INP_HASH_WLOCK(pcbinfo);
1086 vflagsav = inp->inp_vflag;
1087 inp->inp_vflag &= ~INP_IPV4;
1088 inp->inp_vflag |= INP_IPV6;
1089 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1090 struct sockaddr_in6 *sin6_p;
1092 sin6_p = (struct sockaddr_in6 *)nam;
1094 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1095 inp->inp_vflag |= INP_IPV4;
1097 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1098 struct sockaddr_in sin;
1100 in6_sin6_2_sin(&sin, sin6_p);
1101 inp->inp_vflag |= INP_IPV4;
1102 inp->inp_vflag &= ~INP_IPV6;
1103 error = in_pcbbind(inp, (struct sockaddr *)&sin,
1110 error = in6_pcbbind(inp, nam, td->td_ucred);
1115 inp->inp_vflag = vflagsav;
1116 INP_HASH_WUNLOCK(pcbinfo);
1122 udp6_close(struct socket *so)
1125 struct inpcbinfo *pcbinfo;
1127 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1128 inp = sotoinpcb(so);
1129 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1133 if (inp->inp_vflag & INP_IPV4) {
1134 struct pr_usrreqs *pru;
1137 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1138 IPPROTO_UDP : IPPROTO_UDPLITE;
1140 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1141 (*pru->pru_disconnect)(so);
1145 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1146 INP_HASH_WLOCK(pcbinfo);
1147 in6_pcbdisconnect(inp);
1148 inp->in6p_laddr = in6addr_any;
1149 INP_HASH_WUNLOCK(pcbinfo);
1150 soisdisconnected(so);
1156 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1159 struct epoch_tracker et;
1162 struct inpcbinfo *pcbinfo;
1163 struct sockaddr_in6 *sin6;
1167 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1168 inp = sotoinpcb(so);
1169 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1171 sin6 = (struct sockaddr_in6 *)nam;
1172 if (sin6->sin6_family != AF_INET6)
1173 return (EAFNOSUPPORT);
1174 if (sin6->sin6_len != sizeof(*sin6))
1178 * XXXRW: Need to clarify locking of v4/v6 flags.
1182 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1183 struct sockaddr_in sin;
1185 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1189 if ((inp->inp_vflag & INP_IPV4) == 0) {
1190 error = EAFNOSUPPORT;
1193 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1197 in6_sin6_2_sin(&sin, sin6);
1198 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1201 vflagsav = inp->inp_vflag;
1202 inp->inp_vflag |= INP_IPV4;
1203 inp->inp_vflag &= ~INP_IPV6;
1204 NET_EPOCH_ENTER(et);
1205 INP_HASH_WLOCK(pcbinfo);
1206 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1207 td->td_ucred, true);
1208 INP_HASH_WUNLOCK(pcbinfo);
1211 * If connect succeeds, mark socket as connected. If
1212 * connect fails and socket is unbound, reset inp_vflag
1217 else if (inp->inp_laddr.s_addr == INADDR_ANY &&
1218 inp->inp_lport == 0)
1219 inp->inp_vflag = vflagsav;
1222 if ((inp->inp_vflag & INP_IPV6) == 0) {
1223 error = EAFNOSUPPORT;
1228 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1232 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1235 vflagsav = inp->inp_vflag;
1236 inp->inp_vflag &= ~INP_IPV4;
1237 inp->inp_vflag |= INP_IPV6;
1238 INP_HASH_WLOCK(pcbinfo);
1239 error = in6_pcbconnect(inp, nam, td->td_ucred);
1240 INP_HASH_WUNLOCK(pcbinfo);
1242 * If connect succeeds, mark socket as connected. If
1243 * connect fails and socket is unbound, reset inp_vflag
1248 else if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
1249 inp->inp_lport == 0)
1250 inp->inp_vflag = vflagsav;
1257 udp6_detach(struct socket *so)
1262 inp = sotoinpcb(so);
1263 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1266 up = intoudpcb(inp);
1267 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1274 udp6_disconnect(struct socket *so)
1277 struct inpcbinfo *pcbinfo;
1279 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1280 inp = sotoinpcb(so);
1281 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1285 if (inp->inp_vflag & INP_IPV4) {
1286 struct pr_usrreqs *pru;
1289 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1290 IPPROTO_UDP : IPPROTO_UDPLITE;
1292 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1293 (void)(*pru->pru_disconnect)(so);
1298 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1303 INP_HASH_WLOCK(pcbinfo);
1304 in6_pcbdisconnect(inp);
1305 inp->in6p_laddr = in6addr_any;
1306 INP_HASH_WUNLOCK(pcbinfo);
1308 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1315 udp6_send(struct socket *so, int flags, struct mbuf *m,
1316 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1321 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1325 if (addr->sa_family != AF_INET6) {
1326 error = EAFNOSUPPORT;
1331 return (udp6_output(so, flags, m, addr, control, td));
1340 struct pr_usrreqs udp6_usrreqs = {
1341 .pru_abort = udp6_abort,
1342 .pru_attach = udp6_attach,
1343 .pru_bind = udp6_bind,
1344 .pru_connect = udp6_connect,
1345 .pru_control = in6_control,
1346 .pru_detach = udp6_detach,
1347 .pru_disconnect = udp6_disconnect,
1348 .pru_peeraddr = in6_mapped_peeraddr,
1349 .pru_send = udp6_send,
1350 .pru_shutdown = udp_shutdown,
1351 .pru_sockaddr = in6_mapped_sockaddr,
1352 .pru_soreceive = soreceive_dgram,
1353 .pru_sosend = sosend_dgram,
1354 .pru_sosetlabel = in_pcbsosetlabel,
1355 .pru_close = udp6_close