2 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3 * Copyright (c) 2010-2011 Juniper Networks, Inc.
4 * Copyright (c) 2014 Kevin Lo
7 * Portions of this software were developed by Robert N. M. Watson under
8 * contract to Juniper Networks, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the project nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $
35 * $KAME: udp6_output.c,v 1.31 2001/05/21 16:39:15 jinmei Exp $
39 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
40 * The Regents of the University of California.
41 * All rights reserved.
43 * Redistribution and use in source and binary forms, with or without
44 * modification, are permitted provided that the following conditions
46 * 1. Redistributions of source code must retain the above copyright
47 * notice, this list of conditions and the following disclaimer.
48 * 2. Redistributions in binary form must reproduce the above copyright
49 * notice, this list of conditions and the following disclaimer in the
50 * documentation and/or other materials provided with the distribution.
51 * 4. Neither the name of the University nor the names of its contributors
52 * may be used to endorse or promote products derived from this software
53 * without specific prior written permission.
55 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
56 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
57 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
58 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
59 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
60 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
61 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
63 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
64 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
70 #include <sys/cdefs.h>
71 __FBSDID("$FreeBSD$");
74 #include "opt_inet6.h"
76 #include "opt_ipsec.h"
79 #include <sys/param.h>
81 #include <sys/kernel.h>
86 #include <sys/protosw.h>
88 #include <sys/signalvar.h>
89 #include <sys/socket.h>
90 #include <sys/socketvar.h>
92 #include <sys/sysctl.h>
93 #include <sys/syslog.h>
94 #include <sys/systm.h>
97 #include <net/if_var.h>
98 #include <net/if_types.h>
99 #include <net/route.h>
100 #include <net/rss_config.h>
102 #include <netinet/in.h>
103 #include <netinet/in_kdtrace.h>
104 #include <netinet/in_pcb.h>
105 #include <netinet/in_systm.h>
106 #include <netinet/in_var.h>
107 #include <netinet/ip.h>
108 #include <netinet/ip_icmp.h>
109 #include <netinet/ip6.h>
110 #include <netinet/icmp_var.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_pcb.h>
120 #include <netinet6/in6_rss.h>
121 #include <netinet6/udp6_var.h>
122 #include <netinet6/scope6_var.h>
125 #include <netipsec/ipsec.h>
126 #include <netipsec/ipsec6.h>
129 #include <security/mac/mac_framework.h>
132 * UDP protocol implementation.
133 * Per RFC 768, August, 1980.
136 extern struct protosw inetsw[];
137 static void udp6_detach(struct socket *so);
140 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
141 struct sockaddr_in6 *fromsa)
147 INP_LOCK_ASSERT(inp);
150 * Engage the tunneling protocol.
153 if (up->u_tun_func != NULL) {
156 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)fromsa,
159 return (in_pcbrele_rlocked(inp));
162 /* Check AH/ESP integrity. */
163 if (ipsec6_in_reject(n, inp)) {
169 if (mac_inpcb_check_deliver(inp, n) != 0) {
175 if (inp->inp_flags & INP_CONTROLOPTS ||
176 inp->inp_socket->so_options & SO_TIMESTAMP)
177 ip6_savecontrol(inp, n, &opts);
178 m_adj(n, off + sizeof(struct udphdr));
180 so = inp->inp_socket;
181 SOCKBUF_LOCK(&so->so_rcv);
182 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
184 SOCKBUF_UNLOCK(&so->so_rcv);
188 UDPSTAT_INC(udps_fullsock);
190 sorwakeup_locked(so);
195 udp6_input(struct mbuf **mp, int *offp, int proto)
197 struct mbuf *m = *mp;
202 struct inpcbinfo *pcbinfo;
207 struct sockaddr_in6 fromsa;
208 struct m_tag *fwd_tag;
212 ifp = m->m_pkthdr.rcvif;
213 ip6 = mtod(m, struct ip6_hdr *);
215 #ifndef PULLDOWN_TEST
216 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
217 ip6 = mtod(m, struct ip6_hdr *);
218 uh = (struct udphdr *)((caddr_t)ip6 + off);
220 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
222 return (IPPROTO_DONE);
225 UDPSTAT_INC(udps_ipackets);
228 * Destination port of 0 is illegal, based on RFC768.
230 if (uh->uh_dport == 0)
233 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
234 ulen = ntohs((u_short)uh->uh_ulen);
237 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
238 if (nxt == IPPROTO_UDPLITE) {
239 /* Zero means checksum over the complete packet. */
244 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
245 /* XXX: What is the right UDPLite MIB counter? */
248 if (uh->uh_sum == 0) {
249 /* XXX: What is the right UDPLite MIB counter? */
253 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
254 UDPSTAT_INC(udps_badlen);
257 if (uh->uh_sum == 0) {
258 UDPSTAT_INC(udps_nosum);
263 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
265 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
266 uh_sum = m->m_pkthdr.csum_data;
268 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
269 m->m_pkthdr.csum_data);
272 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
275 UDPSTAT_INC(udps_badsum);
280 * Construct sockaddr format source address.
282 init_sin6(&fromsa, m);
283 fromsa.sin6_port = uh->uh_sport;
285 pcbinfo = udp_get_inpcbinfo(nxt);
286 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
288 struct inpcbhead *pcblist;
289 struct ip6_moptions *imo;
291 INP_INFO_RLOCK(pcbinfo);
293 * In the event that laddr should be set to the link-local
294 * address (this happens in RIPng), the multicast address
295 * specified in the received packet will not match laddr. To
296 * handle this situation, matching is relaxed if the
297 * receiving interface is the same as one specified in the
298 * socket and if the destination multicast address matches
299 * one of the multicast groups specified in the socket.
303 * KAME note: traditionally we dropped udpiphdr from mbuf
304 * here. We need udphdr for IPsec processing so we do that
307 pcblist = udp_get_pcblist(nxt);
309 LIST_FOREACH(inp, pcblist, inp_list) {
310 if ((inp->inp_vflag & INP_IPV6) == 0)
312 if (inp->inp_lport != uh->uh_dport)
314 if (inp->inp_fport != 0 &&
315 inp->inp_fport != uh->uh_sport)
317 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
318 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
322 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
323 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
325 inp->inp_fport != uh->uh_sport)
330 * XXXRW: Because we weren't holding either the inpcb
331 * or the hash lock when we checked for a match
332 * before, we should probably recheck now that the
333 * inpcb lock is (supposed to be) held.
337 * Handle socket delivery policy for any-source
338 * and source-specific multicast. [RFC3678]
340 imo = inp->in6p_moptions;
341 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
342 struct sockaddr_in6 mcaddr;
347 bzero(&mcaddr, sizeof(struct sockaddr_in6));
348 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
349 mcaddr.sin6_family = AF_INET6;
350 mcaddr.sin6_addr = ip6->ip6_dst;
352 blocked = im6o_mc_filter(imo, ifp,
353 (struct sockaddr *)&mcaddr,
354 (struct sockaddr *)&fromsa);
355 if (blocked != MCAST_PASS) {
356 if (blocked == MCAST_NOTGMEMBER)
357 IP6STAT_INC(ip6s_notmember);
358 if (blocked == MCAST_NOTSMEMBER ||
359 blocked == MCAST_MUTED)
360 UDPSTAT_INC(udps_filtermcast);
361 INP_RUNLOCK(inp); /* XXX */
370 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
372 UDP_PROBE(receive, NULL, last, ip6,
374 if (udp6_append(last, n, off, &fromsa))
381 * Don't look for additional matches if this one does
382 * not have either the SO_REUSEPORT or SO_REUSEADDR
383 * socket options set. This heuristic avoids
384 * searching through all pcbs in the common case of a
385 * non-shared port. It assumes that an application
386 * will never clear these options after setting them.
388 if ((last->inp_socket->so_options &
389 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
395 * No matching pcb found; discard datagram. (No need
396 * to send an ICMP Port Unreachable for a broadcast
397 * or multicast datgram.)
399 UDPSTAT_INC(udps_noport);
400 UDPSTAT_INC(udps_noportmcast);
404 INP_INFO_RUNLOCK(pcbinfo);
405 UDP_PROBE(receive, NULL, last, ip6, last, uh);
406 if (udp6_append(last, m, off, &fromsa) == 0)
409 return (IPPROTO_DONE);
412 * Locate pcb for datagram.
416 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
418 if ((m->m_flags & M_IP6_NEXTHOP) &&
419 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
420 struct sockaddr_in6 *next_hop6;
422 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
425 * Transparently forwarded. Pretend to be the destination.
426 * Already got one like this?
428 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
429 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
430 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
433 * It's new. Try to find the ambushing socket.
434 * Because we've rewritten the destination address,
435 * any hardware-generated hash is ignored.
437 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
438 uh->uh_sport, &next_hop6->sin6_addr,
439 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
440 uh->uh_dport, INPLOOKUP_WILDCARD |
441 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
443 /* Remove the tag from the packet. We don't need it anymore. */
444 m_tag_delete(m, fwd_tag);
445 m->m_flags &= ~M_IP6_NEXTHOP;
447 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
448 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
449 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
450 m->m_pkthdr.rcvif, m);
452 if (udp_log_in_vain) {
453 char ip6bufs[INET6_ADDRSTRLEN];
454 char ip6bufd[INET6_ADDRSTRLEN];
457 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
458 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
460 ip6_sprintf(ip6bufs, &ip6->ip6_src),
461 ntohs(uh->uh_sport));
463 UDPSTAT_INC(udps_noport);
464 if (m->m_flags & M_MCAST) {
465 printf("UDP6: M_MCAST is set in a unicast packet.\n");
466 UDPSTAT_INC(udps_noportmcast);
471 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
473 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
474 return (IPPROTO_DONE);
476 INP_RLOCK_ASSERT(inp);
479 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
482 return (IPPROTO_DONE);
485 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
486 if (udp6_append(inp, m, off, &fromsa) == 0)
488 return (IPPROTO_DONE);
491 INP_INFO_RUNLOCK(pcbinfo);
495 return (IPPROTO_DONE);
499 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
500 struct inpcbinfo *pcbinfo)
506 struct ip6ctlparam *ip6cp = NULL;
507 const struct sockaddr_in6 *sa6_src = NULL;
509 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
510 struct udp_portonly {
515 if (sa->sa_family != AF_INET6 ||
516 sa->sa_len != sizeof(struct sockaddr_in6))
519 if ((unsigned)cmd >= PRC_NCMDS)
521 if (PRC_IS_REDIRECT(cmd))
522 notify = in6_rtchange, d = NULL;
523 else if (cmd == PRC_HOSTDEAD)
525 else if (inet6ctlerrmap[cmd] == 0)
528 /* if the parameter is from icmp6, decode it. */
530 ip6cp = (struct ip6ctlparam *)d;
532 ip6 = ip6cp->ip6c_ip6;
533 off = ip6cp->ip6c_off;
534 cmdarg = ip6cp->ip6c_cmdarg;
535 sa6_src = ip6cp->ip6c_src;
545 * XXX: We assume that when IPV6 is non NULL,
546 * M and OFF are valid.
549 /* Check if we can safely examine src and dst ports. */
550 if (m->m_pkthdr.len < off + sizeof(*uhp))
553 bzero(&uh, sizeof(uh));
554 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
556 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
557 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
560 (void)in6_pcbnotify(pcbinfo, sa, 0,
561 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
565 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
568 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
572 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
575 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
579 udp6_getcred(SYSCTL_HANDLER_ARGS)
582 struct sockaddr_in6 addrs[2];
586 error = priv_check(req->td, PRIV_NETINET_GETCRED);
590 if (req->newlen != sizeof(addrs))
592 if (req->oldlen != sizeof(struct xucred))
594 error = SYSCTL_IN(req, addrs, sizeof(addrs));
597 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
598 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
601 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
602 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
603 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
605 INP_RLOCK_ASSERT(inp);
606 if (inp->inp_socket == NULL)
609 error = cr_canseesocket(req->td->td_ucred,
612 cru2x(inp->inp_cred, &xuc);
617 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
621 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
622 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
625 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
626 struct mbuf *control, struct thread *td)
628 u_int32_t ulen = m->m_pkthdr.len;
629 u_int32_t plen = sizeof(struct udphdr) + ulen;
632 struct in6_addr *laddr, *faddr, in6a;
633 struct sockaddr_in6 *sin6 = NULL;
634 int cscov_partial = 0;
635 int scope_ambiguous = 0;
640 struct ip6_pktopts *optp, opt;
641 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
643 struct sockaddr_in6 tmp;
645 INP_WLOCK_ASSERT(inp);
646 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
649 /* addr6 has been validated in udp6_send(). */
650 sin6 = (struct sockaddr_in6 *)addr6;
652 /* protect *sin6 from overwrites */
657 * Application should provide a proper zone ID or the use of
658 * default zone IDs should be enabled. Unfortunately, some
659 * applications do not behave as it should, so we need a
660 * workaround. Even if an appropriate ID is not determined,
661 * we'll see if we can determine the outgoing interface. If we
662 * can, determine the zone ID based on the interface below.
664 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
666 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
670 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
671 IPPROTO_UDP : IPPROTO_UDPLITE;
673 if ((error = ip6_setpktopts(control, &opt,
674 inp->in6p_outputopts, td->td_ucred, nxt)) != 0)
678 optp = inp->in6p_outputopts;
681 faddr = &sin6->sin6_addr;
684 * Since we saw no essential reason for calling in_pcbconnect,
685 * we get rid of such kind of logic, and call in6_selectsrc
686 * and in6_pcbsetport in order to fill in the local address
687 * and the local port.
689 if (sin6->sin6_port == 0) {
690 error = EADDRNOTAVAIL;
694 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
695 /* how about ::ffff:0.0.0.0 case? */
700 fport = sin6->sin6_port; /* allow 0 port */
702 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
703 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
705 * I believe we should explicitly discard the
706 * packet when mapped addresses are disabled,
707 * rather than send the packet as an IPv6 one.
708 * If we chose the latter approach, the packet
709 * might be sent out on the wire based on the
710 * default route, the situation which we'd
711 * probably want to avoid.
712 * (20010421 jinmei@kame.net)
717 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
718 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
720 * when remote addr is an IPv4-mapped address,
721 * local addr should not be an IPv6 address,
722 * since you cannot determine how to map IPv6
723 * source address to IPv4.
732 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
733 error = in6_selectsrc_socket(sin6, optp, inp,
734 td->td_ucred, scope_ambiguous, &in6a, NULL);
739 laddr = &inp->in6p_laddr; /* XXX */
742 error = EADDRNOTAVAIL;
745 if (inp->inp_lport == 0 &&
746 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
747 /* Undo an address bind that may have occurred. */
748 inp->in6p_laddr = in6addr_any;
752 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
756 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
757 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
759 * XXX: this case would happen when the
760 * application sets the V6ONLY flag after
761 * connecting the foreign address.
762 * Such applications should be fixed,
765 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
766 "option was set for a connected socket\n");
772 laddr = &inp->in6p_laddr;
773 faddr = &inp->in6p_faddr;
774 fport = inp->inp_fport;
778 hlen = sizeof(struct ip);
781 * Calculate data length and get a mbuf
782 * for UDP and IP6 headers.
784 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
791 * Stuff checksum and output datagram.
793 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
794 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
795 udp6->uh_dport = fport;
796 if (nxt == IPPROTO_UDPLITE) {
800 cscov = up->u_txcslen;
803 udp6->uh_ulen = htons(cscov);
805 * For UDP-Lite, checksum coverage length of zero means
806 * the entire UDPLite packet is covered by the checksum.
808 cscov_partial = (cscov == 0) ? 0 : 1;
809 } else if (plen <= 0xffff)
810 udp6->uh_ulen = htons((u_short)plen);
817 ip6 = mtod(m, struct ip6_hdr *);
818 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
819 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
820 ip6->ip6_vfc |= IPV6_VERSION;
821 ip6->ip6_plen = htons((u_short)plen);
823 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
824 ip6->ip6_src = *laddr;
825 ip6->ip6_dst = *faddr;
828 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
829 sizeof(struct ip6_hdr), plen, cscov)) == 0)
830 udp6->uh_sum = 0xffff;
832 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
833 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
834 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
839 uint32_t hash_val, hash_type;
842 pr = inp->inp_socket->so_proto->pr_protocol;
844 * Calculate an appropriate RSS hash for UDP and
847 * The called function will take care of figuring out
848 * whether a 2-tuple or 4-tuple hash is required based
849 * on the currently configured scheme.
851 * Later later on connected socket values should be
852 * cached in the inpcb and reused, rather than constantly
855 * UDP Lite is a different protocol number and will
856 * likely end up being hashed as a 2-tuple until
857 * RSS / NICs grow UDP Lite protocol awareness.
859 if (rss_proto_software_hash_v6(faddr, laddr, fport,
860 inp->inp_lport, pr, &hash_val, &hash_type) == 0) {
861 m->m_pkthdr.flowid = hash_val;
862 M_HASHTYPE_SET(m, hash_type);
869 * Don't override with the inp cached flowid.
871 * Until the whole UDP path is vetted, it may actually
874 flags |= IP_NODEFAULTFLOWID;
877 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
878 UDPSTAT_INC(udps_opackets);
879 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
883 error = EAFNOSUPPORT;
893 ip6_clearpktopts(&opt, -1);
900 udp6_abort(struct socket *so)
903 struct inpcbinfo *pcbinfo;
905 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
907 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
911 if (inp->inp_vflag & INP_IPV4) {
912 struct pr_usrreqs *pru;
915 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
916 IPPROTO_UDP : IPPROTO_UDPLITE;
918 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
919 (*pru->pru_abort)(so);
924 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
925 INP_HASH_WLOCK(pcbinfo);
926 in6_pcbdisconnect(inp);
927 inp->in6p_laddr = in6addr_any;
928 INP_HASH_WUNLOCK(pcbinfo);
929 soisdisconnected(so);
935 udp6_attach(struct socket *so, int proto, struct thread *td)
938 struct inpcbinfo *pcbinfo;
941 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
943 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
945 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
946 error = soreserve(so, udp_sendspace, udp_recvspace);
950 INP_INFO_WLOCK(pcbinfo);
951 error = in_pcballoc(so, pcbinfo);
953 INP_INFO_WUNLOCK(pcbinfo);
956 inp = (struct inpcb *)so->so_pcb;
957 inp->inp_vflag |= INP_IPV6;
958 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
959 inp->inp_vflag |= INP_IPV4;
960 inp->in6p_hops = -1; /* use kernel default */
961 inp->in6p_cksum = -1; /* just to be sure */
964 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
965 * because the socket may be bound to an IPv6 wildcard address,
966 * which may match an IPv4-mapped IPv6 address.
968 inp->inp_ip_ttl = V_ip_defttl;
970 error = udp_newudpcb(inp);
974 INP_INFO_WUNLOCK(pcbinfo);
978 INP_INFO_WUNLOCK(pcbinfo);
983 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
986 struct inpcbinfo *pcbinfo;
989 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
991 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
994 INP_HASH_WLOCK(pcbinfo);
995 inp->inp_vflag &= ~INP_IPV4;
996 inp->inp_vflag |= INP_IPV6;
997 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
998 struct sockaddr_in6 *sin6_p;
1000 sin6_p = (struct sockaddr_in6 *)nam;
1002 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
1003 inp->inp_vflag |= INP_IPV4;
1005 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
1006 struct sockaddr_in sin;
1008 in6_sin6_2_sin(&sin, sin6_p);
1009 inp->inp_vflag |= INP_IPV4;
1010 inp->inp_vflag &= ~INP_IPV6;
1011 error = in_pcbbind(inp, (struct sockaddr *)&sin,
1018 error = in6_pcbbind(inp, nam, td->td_ucred);
1022 INP_HASH_WUNLOCK(pcbinfo);
1028 udp6_close(struct socket *so)
1031 struct inpcbinfo *pcbinfo;
1033 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1034 inp = sotoinpcb(so);
1035 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1039 if (inp->inp_vflag & INP_IPV4) {
1040 struct pr_usrreqs *pru;
1043 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1044 IPPROTO_UDP : IPPROTO_UDPLITE;
1046 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1047 (*pru->pru_disconnect)(so);
1051 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1052 INP_HASH_WLOCK(pcbinfo);
1053 in6_pcbdisconnect(inp);
1054 inp->in6p_laddr = in6addr_any;
1055 INP_HASH_WUNLOCK(pcbinfo);
1056 soisdisconnected(so);
1062 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1065 struct inpcbinfo *pcbinfo;
1066 struct sockaddr_in6 *sin6;
1069 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1070 inp = sotoinpcb(so);
1071 sin6 = (struct sockaddr_in6 *)nam;
1072 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1075 * XXXRW: Need to clarify locking of v4/v6 flags.
1079 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1080 struct sockaddr_in sin;
1082 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1086 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1090 in6_sin6_2_sin(&sin, sin6);
1091 inp->inp_vflag |= INP_IPV4;
1092 inp->inp_vflag &= ~INP_IPV6;
1093 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1096 INP_HASH_WLOCK(pcbinfo);
1097 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1099 INP_HASH_WUNLOCK(pcbinfo);
1105 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1109 inp->inp_vflag &= ~INP_IPV4;
1110 inp->inp_vflag |= INP_IPV6;
1111 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1114 INP_HASH_WLOCK(pcbinfo);
1115 error = in6_pcbconnect(inp, nam, td->td_ucred);
1116 INP_HASH_WUNLOCK(pcbinfo);
1125 udp6_detach(struct socket *so)
1128 struct inpcbinfo *pcbinfo;
1131 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1132 inp = sotoinpcb(so);
1133 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1135 INP_INFO_WLOCK(pcbinfo);
1137 up = intoudpcb(inp);
1138 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1141 INP_INFO_WUNLOCK(pcbinfo);
1146 udp6_disconnect(struct socket *so)
1149 struct inpcbinfo *pcbinfo;
1152 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1153 inp = sotoinpcb(so);
1154 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1158 if (inp->inp_vflag & INP_IPV4) {
1159 struct pr_usrreqs *pru;
1162 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
1163 IPPROTO_UDP : IPPROTO_UDPLITE;
1165 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1166 (void)(*pru->pru_disconnect)(so);
1171 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1176 INP_HASH_WLOCK(pcbinfo);
1177 in6_pcbdisconnect(inp);
1178 inp->in6p_laddr = in6addr_any;
1179 INP_HASH_WUNLOCK(pcbinfo);
1181 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1189 udp6_send(struct socket *so, int flags, struct mbuf *m,
1190 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1193 struct inpcbinfo *pcbinfo;
1196 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1197 inp = sotoinpcb(so);
1198 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1202 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1206 if (addr->sa_family != AF_INET6) {
1207 error = EAFNOSUPPORT;
1213 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1215 struct sockaddr_in6 *sin6 = 0;
1218 hasv4addr = (inp->inp_vflag & INP_IPV4);
1220 sin6 = (struct sockaddr_in6 *)addr;
1221 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1225 struct pr_usrreqs *pru;
1228 nxt = (inp->inp_socket->so_proto->pr_protocol ==
1229 IPPROTO_UDP) ? IPPROTO_UDP : IPPROTO_UDPLITE;
1231 * XXXRW: We release UDP-layer locks before calling
1232 * udp_send() in order to avoid recursion. However,
1233 * this does mean there is a short window where inp's
1234 * fields are unstable. Could this lead to a
1235 * potential race in which the factors causing us to
1236 * select the UDPv4 output routine are invalidated?
1240 in6_sin6_2_sin_in_sock(addr);
1241 pru = inetsw[ip_protox[nxt]].pr_usrreqs;
1242 /* addr will just be freed in sendit(). */
1243 return ((*pru->pru_send)(so, flags, m, addr, control,
1249 mac_inpcb_create_mbuf(inp, m);
1251 INP_HASH_WLOCK(pcbinfo);
1252 error = udp6_output(inp, m, addr, control, td);
1253 INP_HASH_WUNLOCK(pcbinfo);
1263 struct pr_usrreqs udp6_usrreqs = {
1264 .pru_abort = udp6_abort,
1265 .pru_attach = udp6_attach,
1266 .pru_bind = udp6_bind,
1267 .pru_connect = udp6_connect,
1268 .pru_control = in6_control,
1269 .pru_detach = udp6_detach,
1270 .pru_disconnect = udp6_disconnect,
1271 .pru_peeraddr = in6_mapped_peeraddr,
1272 .pru_send = udp6_send,
1273 .pru_shutdown = udp_shutdown,
1274 .pru_sockaddr = in6_mapped_sockaddr,
1275 .pru_soreceive = soreceive_dgram,
1276 .pru_sosend = sosend_dgram,
1277 .pru_sosetlabel = in_pcbsosetlabel,
1278 .pru_close = udp6_close