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 epoch_tracker et;
218 struct sockaddr_in6 fromsa[2];
219 struct m_tag *fwd_tag;
223 ifp = m->m_pkthdr.rcvif;
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);
233 ip6 = mtod(m, struct ip6_hdr *);
236 UDPSTAT_INC(udps_ipackets);
239 * Destination port of 0 is illegal, based on RFC768.
241 if (uh->uh_dport == 0)
244 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
245 ulen = ntohs((u_short)uh->uh_ulen);
248 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
249 if (nxt == IPPROTO_UDPLITE) {
250 /* Zero means checksum over the complete packet. */
255 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
256 /* XXX: What is the right UDPLite MIB counter? */
259 if (uh->uh_sum == 0) {
260 /* XXX: What is the right UDPLite MIB counter? */
264 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
265 UDPSTAT_INC(udps_badlen);
268 if (uh->uh_sum == 0) {
269 UDPSTAT_INC(udps_nosum);
274 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
276 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
277 uh_sum = m->m_pkthdr.csum_data;
279 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
280 m->m_pkthdr.csum_data);
283 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
286 UDPSTAT_INC(udps_badsum);
291 * Construct sockaddr format source address.
293 init_sin6(&fromsa[0], m, 0);
294 fromsa[0].sin6_port = uh->uh_sport;
295 init_sin6(&fromsa[1], m, 1);
296 fromsa[1].sin6_port = uh->uh_dport;
298 pcbinfo = udp_get_inpcbinfo(nxt);
299 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
301 struct inpcbhead *pcblist;
302 struct ip6_moptions *imo;
304 INP_INFO_RLOCK_ET(pcbinfo, et);
306 * In the event that laddr should be set to the link-local
307 * address (this happens in RIPng), the multicast address
308 * specified in the received packet will not match laddr. To
309 * handle this situation, matching is relaxed if the
310 * receiving interface is the same as one specified in the
311 * socket and if the destination multicast address matches
312 * one of the multicast groups specified in the socket.
316 * KAME note: traditionally we dropped udpiphdr from mbuf
317 * here. We need udphdr for IPsec processing so we do that
320 pcblist = udp_get_pcblist(nxt);
322 CK_LIST_FOREACH(inp, pcblist, inp_list) {
323 if ((inp->inp_vflag & INP_IPV6) == 0)
325 if (inp->inp_lport != uh->uh_dport)
327 if (inp->inp_fport != 0 &&
328 inp->inp_fport != uh->uh_sport)
330 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
331 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
335 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
336 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
338 inp->inp_fport != uh->uh_sport)
343 * XXXRW: Because we weren't holding either the inpcb
344 * or the hash lock when we checked for a match
345 * before, we should probably recheck now that the
346 * inpcb lock is (supposed to be) held.
350 * Handle socket delivery policy for any-source
351 * and source-specific multicast. [RFC3678]
353 imo = inp->in6p_moptions;
354 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
355 struct sockaddr_in6 mcaddr;
359 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
364 bzero(&mcaddr, sizeof(struct sockaddr_in6));
365 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
366 mcaddr.sin6_family = AF_INET6;
367 mcaddr.sin6_addr = ip6->ip6_dst;
369 blocked = im6o_mc_filter(imo, ifp,
370 (struct sockaddr *)&mcaddr,
371 (struct sockaddr *)&fromsa[0]);
372 if (blocked != MCAST_PASS) {
373 if (blocked == MCAST_NOTGMEMBER)
374 IP6STAT_INC(ip6s_notmember);
375 if (blocked == MCAST_NOTSMEMBER ||
376 blocked == MCAST_MUTED)
377 UDPSTAT_INC(udps_filtermcast);
378 INP_RUNLOCK(inp); /* XXX */
387 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
390 if (__predict_true(last->inp_flags2 & INP_FREED) == 0) {
391 if (nxt == IPPROTO_UDPLITE)
392 UDPLITE_PROBE(receive, NULL, last,
395 UDP_PROBE(receive, NULL, last,
397 if (udp6_append(last, n, off, fromsa))
405 * Don't look for additional matches if this one does
406 * not have either the SO_REUSEPORT or SO_REUSEADDR
407 * socket options set. This heuristic avoids
408 * searching through all pcbs in the common case of a
409 * non-shared port. It assumes that an application
410 * will never clear these options after setting them.
412 if ((last->inp_socket->so_options &
413 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0)
419 * No matching pcb found; discard datagram. (No need
420 * to send an ICMP Port Unreachable for a broadcast
421 * or multicast datgram.)
423 UDPSTAT_INC(udps_noport);
424 UDPSTAT_INC(udps_noportmcast);
428 if (__predict_true(last->inp_flags2 & INP_FREED) == 0) {
429 if (nxt == IPPROTO_UDPLITE)
430 UDPLITE_PROBE(receive, NULL, last, ip6, last, uh);
432 UDP_PROBE(receive, NULL, last, ip6, last, uh);
433 if (udp6_append(last, m, off, fromsa) == 0)
437 INP_INFO_RUNLOCK_ET(pcbinfo, et);
439 return (IPPROTO_DONE);
442 * Locate pcb for datagram.
446 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
448 if ((m->m_flags & M_IP6_NEXTHOP) &&
449 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
450 struct sockaddr_in6 *next_hop6;
452 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
455 * Transparently forwarded. Pretend to be the destination.
456 * Already got one like this?
458 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
459 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
460 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
463 * It's new. Try to find the ambushing socket.
464 * Because we've rewritten the destination address,
465 * any hardware-generated hash is ignored.
467 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
468 uh->uh_sport, &next_hop6->sin6_addr,
469 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
470 uh->uh_dport, INPLOOKUP_WILDCARD |
471 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
473 /* Remove the tag from the packet. We don't need it anymore. */
474 m_tag_delete(m, fwd_tag);
475 m->m_flags &= ~M_IP6_NEXTHOP;
477 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
478 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
479 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
480 m->m_pkthdr.rcvif, m);
482 if (udp_log_in_vain) {
483 char ip6bufs[INET6_ADDRSTRLEN];
484 char ip6bufd[INET6_ADDRSTRLEN];
487 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
488 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
490 ip6_sprintf(ip6bufs, &ip6->ip6_src),
491 ntohs(uh->uh_sport));
493 if (nxt == IPPROTO_UDPLITE)
494 UDPLITE_PROBE(receive, NULL, NULL, ip6, NULL, uh);
496 UDP_PROBE(receive, NULL, NULL, ip6, NULL, uh);
497 UDPSTAT_INC(udps_noport);
498 if (m->m_flags & M_MCAST) {
499 printf("UDP6: M_MCAST is set in a unicast packet.\n");
500 UDPSTAT_INC(udps_noportmcast);
505 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
506 return (IPPROTO_DONE);
508 INP_RLOCK_ASSERT(inp);
511 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
514 return (IPPROTO_DONE);
517 if (nxt == IPPROTO_UDPLITE)
518 UDPLITE_PROBE(receive, NULL, inp, ip6, inp, uh);
520 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
521 if (udp6_append(inp, m, off, fromsa) == 0)
523 return (IPPROTO_DONE);
526 INP_INFO_RUNLOCK_ET(pcbinfo, et);
530 return (IPPROTO_DONE);
534 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
535 struct inpcbinfo *pcbinfo)
541 struct ip6ctlparam *ip6cp = NULL;
542 const struct sockaddr_in6 *sa6_src = NULL;
544 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
545 struct udp_portonly {
550 if (sa->sa_family != AF_INET6 ||
551 sa->sa_len != sizeof(struct sockaddr_in6))
554 if ((unsigned)cmd >= PRC_NCMDS)
556 if (PRC_IS_REDIRECT(cmd))
557 notify = in6_rtchange, d = NULL;
558 else if (cmd == PRC_HOSTDEAD)
560 else if (inet6ctlerrmap[cmd] == 0)
563 /* if the parameter is from icmp6, decode it. */
565 ip6cp = (struct ip6ctlparam *)d;
567 ip6 = ip6cp->ip6c_ip6;
568 off = ip6cp->ip6c_off;
569 cmdarg = ip6cp->ip6c_cmdarg;
570 sa6_src = ip6cp->ip6c_src;
580 * XXX: We assume that when IPV6 is non NULL,
581 * M and OFF are valid.
584 /* Check if we can safely examine src and dst ports. */
585 if (m->m_pkthdr.len < off + sizeof(*uhp))
588 bzero(&uh, sizeof(uh));
589 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
591 if (!PRC_IS_REDIRECT(cmd)) {
592 /* Check to see if its tunneled */
594 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_dst,
595 uh.uh_dport, &ip6->ip6_src, uh.uh_sport,
596 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
597 m->m_pkthdr.rcvif, m);
602 if (up->u_icmp_func) {
605 (*up->u_icmp_func)(cmd, (struct sockaddr *)ip6cp->ip6c_src,
614 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
615 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
618 (void)in6_pcbnotify(pcbinfo, sa, 0,
619 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
623 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
626 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
630 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
633 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
637 udp6_getcred(SYSCTL_HANDLER_ARGS)
640 struct sockaddr_in6 addrs[2];
644 error = priv_check(req->td, PRIV_NETINET_GETCRED);
648 if (req->newlen != sizeof(addrs))
650 if (req->oldlen != sizeof(struct xucred))
652 error = SYSCTL_IN(req, addrs, sizeof(addrs));
655 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
656 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
659 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
660 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
661 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
663 INP_RLOCK_ASSERT(inp);
664 if (inp->inp_socket == NULL)
667 error = cr_canseesocket(req->td->td_ucred,
670 cru2x(inp->inp_cred, &xuc);
675 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
679 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
680 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
683 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
684 struct mbuf *control, struct thread *td)
686 u_int32_t ulen = m->m_pkthdr.len;
687 u_int32_t plen = sizeof(struct udphdr) + ulen;
690 struct in6_addr *laddr, *faddr, in6a;
691 struct sockaddr_in6 *sin6 = NULL;
692 int cscov_partial = 0;
693 int scope_ambiguous = 0;
698 struct ip6_pktopts *optp, opt;
699 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
701 struct sockaddr_in6 tmp;
703 INP_WLOCK_ASSERT(inp);
704 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
707 /* addr6 has been validated in udp6_send(). */
708 sin6 = (struct sockaddr_in6 *)addr6;
710 /* protect *sin6 from overwrites */
715 * Application should provide a proper zone ID or the use of
716 * default zone IDs should be enabled. Unfortunately, some
717 * applications do not behave as it should, so we need a
718 * workaround. Even if an appropriate ID is not determined,
719 * we'll see if we can determine the outgoing interface. If we
720 * can, determine the zone ID based on the interface below.
722 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
724 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
728 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
729 IPPROTO_UDP : IPPROTO_UDPLITE;
731 if ((error = ip6_setpktopts(control, &opt,
732 inp->in6p_outputopts, td->td_ucred, nxt)) != 0)
736 optp = inp->in6p_outputopts;
739 faddr = &sin6->sin6_addr;
742 * Since we saw no essential reason for calling in_pcbconnect,
743 * we get rid of such kind of logic, and call in6_selectsrc
744 * and in6_pcbsetport in order to fill in the local address
745 * and the local port.
747 if (sin6->sin6_port == 0) {
748 error = EADDRNOTAVAIL;
752 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
753 /* how about ::ffff:0.0.0.0 case? */
758 fport = sin6->sin6_port; /* allow 0 port */
760 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
761 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
763 * I believe we should explicitly discard the
764 * packet when mapped addresses are disabled,
765 * rather than send the packet as an IPv6 one.
766 * If we chose the latter approach, the packet
767 * might be sent out on the wire based on the
768 * default route, the situation which we'd
769 * probably want to avoid.
770 * (20010421 jinmei@kame.net)
775 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
776 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
778 * when remote addr is an IPv4-mapped address,
779 * local addr should not be an IPv6 address,
780 * since you cannot determine how to map IPv6
781 * source address to IPv4.
790 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
791 error = in6_selectsrc_socket(sin6, optp, inp,
792 td->td_ucred, scope_ambiguous, &in6a, NULL);
797 laddr = &inp->in6p_laddr; /* XXX */
800 error = EADDRNOTAVAIL;
803 if (inp->inp_lport == 0 &&
804 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
805 /* Undo an address bind that may have occurred. */
806 inp->in6p_laddr = in6addr_any;
810 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
814 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
815 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
817 * XXX: this case would happen when the
818 * application sets the V6ONLY flag after
819 * connecting the foreign address.
820 * Such applications should be fixed,
823 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
824 "option was set for a connected socket\n");
830 laddr = &inp->in6p_laddr;
831 faddr = &inp->in6p_faddr;
832 fport = inp->inp_fport;
836 hlen = sizeof(struct ip);
839 * Calculate data length and get a mbuf
840 * for UDP and IP6 headers.
842 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
849 * Stuff checksum and output datagram.
851 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
852 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
853 udp6->uh_dport = fport;
854 if (nxt == IPPROTO_UDPLITE) {
858 cscov = up->u_txcslen;
861 udp6->uh_ulen = htons(cscov);
863 * For UDP-Lite, checksum coverage length of zero means
864 * the entire UDPLite packet is covered by the checksum.
866 cscov_partial = (cscov == 0) ? 0 : 1;
867 } else if (plen <= 0xffff)
868 udp6->uh_ulen = htons((u_short)plen);
875 ip6 = mtod(m, struct ip6_hdr *);
876 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
877 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
878 ip6->ip6_vfc |= IPV6_VERSION;
879 ip6->ip6_plen = htons((u_short)plen);
881 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
882 ip6->ip6_src = *laddr;
883 ip6->ip6_dst = *faddr;
886 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
887 sizeof(struct ip6_hdr), plen, cscov)) == 0)
888 udp6->uh_sum = 0xffff;
890 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
891 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
892 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
897 uint32_t hash_val, hash_type;
900 pr = inp->inp_socket->so_proto->pr_protocol;
902 * Calculate an appropriate RSS hash for UDP and
905 * The called function will take care of figuring out
906 * whether a 2-tuple or 4-tuple hash is required based
907 * on the currently configured scheme.
909 * Later later on connected socket values should be
910 * cached in the inpcb and reused, rather than constantly
913 * UDP Lite is a different protocol number and will
914 * likely end up being hashed as a 2-tuple until
915 * RSS / NICs grow UDP Lite protocol awareness.
917 if (rss_proto_software_hash_v6(faddr, laddr, fport,
918 inp->inp_lport, pr, &hash_val, &hash_type) == 0) {
919 m->m_pkthdr.flowid = hash_val;
920 M_HASHTYPE_SET(m, hash_type);
927 * Don't override with the inp cached flowid.
929 * Until the whole UDP path is vetted, it may actually
932 flags |= IP_NODEFAULTFLOWID;
935 if (nxt == IPPROTO_UDPLITE)
936 UDPLITE_PROBE(send, NULL, inp, ip6, inp, udp6);
938 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
939 UDPSTAT_INC(udps_opackets);
940 error = ip6_output(m, optp, &inp->inp_route6, flags,
941 inp->in6p_moptions, NULL, inp);
944 error = EAFNOSUPPORT;
954 ip6_clearpktopts(&opt, -1);
961 udp6_abort(struct socket *so)
964 struct inpcbinfo *pcbinfo;
966 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
968 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
972 if (inp->inp_vflag & INP_IPV4) {
973 struct pr_usrreqs *pru;
976 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
977 IPPROTO_UDP : IPPROTO_UDPLITE;
979 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
980 (*pru->pru_abort)(so);
985 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
986 INP_HASH_WLOCK(pcbinfo);
987 in6_pcbdisconnect(inp);
988 inp->in6p_laddr = in6addr_any;
989 INP_HASH_WUNLOCK(pcbinfo);
990 soisdisconnected(so);
996 udp6_attach(struct socket *so, int proto, struct thread *td)
999 struct inpcbinfo *pcbinfo;
1002 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1003 inp = sotoinpcb(so);
1004 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
1006 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
1007 error = soreserve(so, udp_sendspace, udp_recvspace);
1011 INP_INFO_WLOCK(pcbinfo);
1012 error = in_pcballoc(so, pcbinfo);
1014 INP_INFO_WUNLOCK(pcbinfo);
1017 inp = (struct inpcb *)so->so_pcb;
1018 inp->inp_vflag |= INP_IPV6;
1019 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
1020 inp->inp_vflag |= INP_IPV4;
1021 inp->in6p_hops = -1; /* use kernel default */
1022 inp->in6p_cksum = -1; /* just to be sure */
1025 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
1026 * because the socket may be bound to an IPv6 wildcard address,
1027 * which may match an IPv4-mapped IPv6 address.
1029 inp->inp_ip_ttl = V_ip_defttl;
1031 error = udp_newudpcb(inp);
1035 INP_INFO_WUNLOCK(pcbinfo);
1039 INP_INFO_WUNLOCK(pcbinfo);
1044 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1047 struct inpcbinfo *pcbinfo;
1050 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1051 inp = sotoinpcb(so);
1052 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
1055 INP_HASH_WLOCK(pcbinfo);
1056 inp->inp_vflag &= ~INP_IPV4;
1057 inp->inp_vflag |= INP_IPV6;
1058 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1059 struct sockaddr_in6 *sin6_p;
1061 sin6_p = (struct sockaddr_in6 *)nam;
1063 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1064 inp->inp_vflag |= INP_IPV4;
1066 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1067 struct sockaddr_in sin;
1069 in6_sin6_2_sin(&sin, sin6_p);
1070 inp->inp_vflag |= INP_IPV4;
1071 inp->inp_vflag &= ~INP_IPV6;
1072 error = in_pcbbind(inp, (struct sockaddr *)&sin,
1079 error = in6_pcbbind(inp, nam, td->td_ucred);
1083 INP_HASH_WUNLOCK(pcbinfo);
1089 udp6_close(struct socket *so)
1092 struct inpcbinfo *pcbinfo;
1094 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1095 inp = sotoinpcb(so);
1096 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1100 if (inp->inp_vflag & INP_IPV4) {
1101 struct pr_usrreqs *pru;
1104 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1105 IPPROTO_UDP : IPPROTO_UDPLITE;
1107 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1108 (*pru->pru_disconnect)(so);
1112 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1113 INP_HASH_WLOCK(pcbinfo);
1114 in6_pcbdisconnect(inp);
1115 inp->in6p_laddr = in6addr_any;
1116 INP_HASH_WUNLOCK(pcbinfo);
1117 soisdisconnected(so);
1123 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1126 struct inpcbinfo *pcbinfo;
1127 struct sockaddr_in6 *sin6;
1130 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1131 inp = sotoinpcb(so);
1132 sin6 = (struct sockaddr_in6 *)nam;
1133 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1136 * XXXRW: Need to clarify locking of v4/v6 flags.
1140 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1141 struct sockaddr_in sin;
1143 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1147 if ((inp->inp_vflag & INP_IPV4) == 0) {
1148 error = EAFNOSUPPORT;
1151 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1155 in6_sin6_2_sin(&sin, sin6);
1156 inp->inp_vflag |= INP_IPV4;
1157 inp->inp_vflag &= ~INP_IPV6;
1158 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1161 INP_HASH_WLOCK(pcbinfo);
1162 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1164 INP_HASH_WUNLOCK(pcbinfo);
1169 if ((inp->inp_vflag & INP_IPV6) == 0) {
1170 error = EAFNOSUPPORT;
1175 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1179 inp->inp_vflag &= ~INP_IPV4;
1180 inp->inp_vflag |= INP_IPV6;
1181 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1184 INP_HASH_WLOCK(pcbinfo);
1185 error = in6_pcbconnect(inp, nam, td->td_ucred);
1186 INP_HASH_WUNLOCK(pcbinfo);
1195 udp6_detach(struct socket *so)
1198 struct inpcbinfo *pcbinfo;
1201 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1202 inp = sotoinpcb(so);
1203 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1205 INP_INFO_WLOCK(pcbinfo);
1207 up = intoudpcb(inp);
1208 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1211 INP_INFO_WUNLOCK(pcbinfo);
1216 udp6_disconnect(struct socket *so)
1219 struct inpcbinfo *pcbinfo;
1221 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1222 inp = sotoinpcb(so);
1223 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1227 if (inp->inp_vflag & INP_IPV4) {
1228 struct pr_usrreqs *pru;
1231 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1232 IPPROTO_UDP : IPPROTO_UDPLITE;
1234 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1235 (void)(*pru->pru_disconnect)(so);
1240 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1245 INP_HASH_WLOCK(pcbinfo);
1246 in6_pcbdisconnect(inp);
1247 inp->in6p_laddr = in6addr_any;
1248 INP_HASH_WUNLOCK(pcbinfo);
1250 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1257 udp6_send(struct socket *so, int flags, struct mbuf *m,
1258 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1261 struct inpcbinfo *pcbinfo;
1264 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1265 inp = sotoinpcb(so);
1266 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1270 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1274 if (addr->sa_family != AF_INET6) {
1275 error = EAFNOSUPPORT;
1281 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1283 struct sockaddr_in6 *sin6 = NULL;
1286 hasv4addr = (inp->inp_vflag & INP_IPV4);
1288 sin6 = (struct sockaddr_in6 *)addr;
1289 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1293 struct pr_usrreqs *pru;
1296 nxt = (inp->inp_socket->so_proto->pr_protocol ==
1297 IPPROTO_UDP) ? IPPROTO_UDP : IPPROTO_UDPLITE;
1299 * XXXRW: We release UDP-layer locks before calling
1300 * udp_send() in order to avoid recursion. However,
1301 * this does mean there is a short window where inp's
1302 * fields are unstable. Could this lead to a
1303 * potential race in which the factors causing us to
1304 * select the UDPv4 output routine are invalidated?
1308 in6_sin6_2_sin_in_sock(addr);
1309 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1310 /* addr will just be freed in sendit(). */
1311 return ((*pru->pru_send)(so, flags, m, addr, control,
1317 mac_inpcb_create_mbuf(inp, m);
1319 INP_HASH_WLOCK(pcbinfo);
1320 error = udp6_output(inp, m, addr, control, td);
1321 INP_HASH_WUNLOCK(pcbinfo);
1331 struct pr_usrreqs udp6_usrreqs = {
1332 .pru_abort = udp6_abort,
1333 .pru_attach = udp6_attach,
1334 .pru_bind = udp6_bind,
1335 .pru_connect = udp6_connect,
1336 .pru_control = in6_control,
1337 .pru_detach = udp6_detach,
1338 .pru_disconnect = udp6_disconnect,
1339 .pru_peeraddr = in6_mapped_peeraddr,
1340 .pru_send = udp6_send,
1341 .pru_shutdown = udp_shutdown,
1342 .pru_sockaddr = in6_mapped_sockaddr,
1343 .pru_soreceive = soreceive_dgram,
1344 .pru_sosend = sosend_dgram,
1345 .pru_sosetlabel = in_pcbsosetlabel,
1346 .pru_close = udp6_close