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"
77 #include "opt_kdtrace.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_types.h>
98 #include <net/route.h>
100 #include <netinet/in.h>
101 #include <netinet/in_kdtrace.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_systm.h>
104 #include <netinet/in_var.h>
105 #include <netinet/ip.h>
106 #include <netinet/ip_icmp.h>
107 #include <netinet/ip6.h>
108 #include <netinet/icmp_var.h>
109 #include <netinet/icmp6.h>
110 #include <netinet/ip_var.h>
111 #include <netinet/udp.h>
112 #include <netinet/udp_var.h>
113 #include <netinet/udplite.h>
115 #include <netinet6/ip6protosw.h>
116 #include <netinet6/ip6_var.h>
117 #include <netinet6/in6_pcb.h>
118 #include <netinet6/udp6_var.h>
119 #include <netinet6/scope6_var.h>
122 #include <netipsec/ipsec.h>
123 #include <netipsec/ipsec6.h>
126 #include <security/mac/mac_framework.h>
129 * UDP protocol implementation.
130 * Per RFC 768, August, 1980.
133 extern struct protosw inetsw[];
134 static void udp6_detach(struct socket *so);
137 udp6_append(struct inpcb *inp, struct mbuf *n, int off,
138 struct sockaddr_in6 *fromsa)
143 INP_LOCK_ASSERT(inp);
146 /* Check AH/ESP integrity. */
147 if (ipsec6_in_reject(n, inp)) {
149 IPSEC6STAT_INC(ips_in_polvio);
154 if (mac_inpcb_check_deliver(inp, n) != 0) {
160 if (inp->inp_flags & INP_CONTROLOPTS ||
161 inp->inp_socket->so_options & SO_TIMESTAMP)
162 ip6_savecontrol(inp, n, &opts);
163 m_adj(n, off + sizeof(struct udphdr));
165 so = inp->inp_socket;
166 SOCKBUF_LOCK(&so->so_rcv);
167 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
169 SOCKBUF_UNLOCK(&so->so_rcv);
173 UDPSTAT_INC(udps_fullsock);
175 sorwakeup_locked(so);
179 udp6_input(struct mbuf **mp, int *offp, int proto)
181 struct mbuf *m = *mp;
186 struct inpcbinfo *pcbinfo;
191 struct sockaddr_in6 fromsa;
192 struct m_tag *fwd_tag;
196 ifp = m->m_pkthdr.rcvif;
197 ip6 = mtod(m, struct ip6_hdr *);
199 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
200 /* XXX send icmp6 host/port unreach? */
202 return (IPPROTO_DONE);
205 #ifndef PULLDOWN_TEST
206 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
207 ip6 = mtod(m, struct ip6_hdr *);
208 uh = (struct udphdr *)((caddr_t)ip6 + off);
210 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
212 return (IPPROTO_DONE);
215 UDPSTAT_INC(udps_ipackets);
218 * Destination port of 0 is illegal, based on RFC768.
220 if (uh->uh_dport == 0)
223 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
224 ulen = ntohs((u_short)uh->uh_ulen);
227 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
228 if (nxt == IPPROTO_UDPLITE && ulen == 0) {
229 /* Zero means checksum over the complete packet. */
234 UDPSTAT_INC(udps_badlen);
239 * Checksum extended UDP header and data.
241 if (uh->uh_sum == 0) {
242 if (ulen > plen || ulen < sizeof(struct udphdr)) {
243 UDPSTAT_INC(udps_nosum);
248 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
250 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
251 uh_sum = m->m_pkthdr.csum_data;
253 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
254 m->m_pkthdr.csum_data);
257 uh_sum = in6_cksum(m, nxt, off, ulen);
260 UDPSTAT_INC(udps_badsum);
265 * Construct sockaddr format source address.
267 init_sin6(&fromsa, m);
268 fromsa.sin6_port = uh->uh_sport;
270 pcbinfo = get_inpcbinfo(nxt);
271 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
273 struct inpcbhead *pcblist;
274 struct ip6_moptions *imo;
276 INP_INFO_RLOCK(pcbinfo);
278 * In the event that laddr should be set to the link-local
279 * address (this happens in RIPng), the multicast address
280 * specified in the received packet will not match laddr. To
281 * handle this situation, matching is relaxed if the
282 * receiving interface is the same as one specified in the
283 * socket and if the destination multicast address matches
284 * one of the multicast groups specified in the socket.
288 * KAME note: traditionally we dropped udpiphdr from mbuf
289 * here. We need udphdr for IPsec processing so we do that
292 pcblist = get_pcblist(nxt);
294 LIST_FOREACH(inp, pcblist, inp_list) {
295 if ((inp->inp_vflag & INP_IPV6) == 0)
297 if (inp->inp_lport != uh->uh_dport)
299 if (inp->inp_fport != 0 &&
300 inp->inp_fport != uh->uh_sport)
302 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
303 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
307 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
308 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
310 inp->inp_fport != uh->uh_sport)
315 * XXXRW: Because we weren't holding either the inpcb
316 * or the hash lock when we checked for a match
317 * before, we should probably recheck now that the
318 * inpcb lock is (supposed to be) held.
322 * Handle socket delivery policy for any-source
323 * and source-specific multicast. [RFC3678]
325 imo = inp->in6p_moptions;
326 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
327 struct sockaddr_in6 mcaddr;
332 bzero(&mcaddr, sizeof(struct sockaddr_in6));
333 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
334 mcaddr.sin6_family = AF_INET6;
335 mcaddr.sin6_addr = ip6->ip6_dst;
337 blocked = im6o_mc_filter(imo, ifp,
338 (struct sockaddr *)&mcaddr,
339 (struct sockaddr *)&fromsa);
340 if (blocked != MCAST_PASS) {
341 if (blocked == MCAST_NOTGMEMBER)
342 IP6STAT_INC(ip6s_notmember);
343 if (blocked == MCAST_NOTSMEMBER ||
344 blocked == MCAST_MUTED)
345 UDPSTAT_INC(udps_filtermcast);
346 INP_RUNLOCK(inp); /* XXX */
355 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
357 up = intoudpcb(last);
358 if (up->u_tun_func == NULL) {
359 udp6_append(last, n, off, &fromsa);
362 * Engage the tunneling
363 * protocol we will have to
364 * leave the info_lock up,
365 * since we are hunting
366 * through multiple UDP's.
369 (*up->u_tun_func)(n, off, last);
376 * Don't look for additional matches if this one does
377 * not have either the SO_REUSEPORT or SO_REUSEADDR
378 * socket options set. This heuristic avoids
379 * searching through all pcbs in the common case of a
380 * non-shared port. It assumes that an application
381 * will never clear these options after setting them.
383 if ((last->inp_socket->so_options &
384 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
390 * No matching pcb found; discard datagram. (No need
391 * to send an ICMP Port Unreachable for a broadcast
392 * or multicast datgram.)
394 UDPSTAT_INC(udps_noport);
395 UDPSTAT_INC(udps_noportmcast);
399 INP_INFO_RUNLOCK(pcbinfo);
400 up = intoudpcb(last);
401 UDP_PROBE(receive, NULL, last, ip6, last, uh);
402 if (up->u_tun_func == NULL) {
403 udp6_append(last, m, off, &fromsa);
406 * Engage the tunneling protocol.
408 (*up->u_tun_func)(m, off, last);
411 return (IPPROTO_DONE);
414 * Locate pcb for datagram.
418 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
420 if ((m->m_flags & M_IP6_NEXTHOP) &&
421 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
422 struct sockaddr_in6 *next_hop6;
424 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
427 * Transparently forwarded. Pretend to be the destination.
428 * Already got one like this?
430 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
431 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
432 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
435 * It's new. Try to find the ambushing socket.
436 * Because we've rewritten the destination address,
437 * any hardware-generated hash is ignored.
439 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
440 uh->uh_sport, &next_hop6->sin6_addr,
441 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
442 uh->uh_dport, INPLOOKUP_WILDCARD |
443 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
445 /* Remove the tag from the packet. We don't need it anymore. */
446 m_tag_delete(m, fwd_tag);
447 m->m_flags &= ~M_IP6_NEXTHOP;
449 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
450 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
451 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
452 m->m_pkthdr.rcvif, m);
454 if (udp_log_in_vain) {
455 char ip6bufs[INET6_ADDRSTRLEN];
456 char ip6bufd[INET6_ADDRSTRLEN];
459 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
460 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
462 ip6_sprintf(ip6bufs, &ip6->ip6_src),
463 ntohs(uh->uh_sport));
465 UDPSTAT_INC(udps_noport);
466 if (m->m_flags & M_MCAST) {
467 printf("UDP6: M_MCAST is set in a unicast packet.\n");
468 UDPSTAT_INC(udps_noportmcast);
473 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
475 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
476 return (IPPROTO_DONE);
478 INP_RLOCK_ASSERT(inp);
481 if (up->u_rxcslen > ulen) {
484 return (IPPROTO_DONE);
487 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
488 if (up->u_tun_func == NULL) {
489 udp6_append(inp, m, off, &fromsa);
492 * Engage the tunneling protocol.
495 (*up->u_tun_func)(m, off, inp);
498 return (IPPROTO_DONE);
501 INP_INFO_RUNLOCK(pcbinfo);
505 return (IPPROTO_DONE);
509 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
510 struct inpcbinfo *pcbinfo)
516 struct ip6ctlparam *ip6cp = NULL;
517 const struct sockaddr_in6 *sa6_src = NULL;
519 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
520 struct udp_portonly {
525 if (sa->sa_family != AF_INET6 ||
526 sa->sa_len != sizeof(struct sockaddr_in6))
529 if ((unsigned)cmd >= PRC_NCMDS)
531 if (PRC_IS_REDIRECT(cmd))
532 notify = in6_rtchange, d = NULL;
533 else if (cmd == PRC_HOSTDEAD)
535 else if (inet6ctlerrmap[cmd] == 0)
538 /* if the parameter is from icmp6, decode it. */
540 ip6cp = (struct ip6ctlparam *)d;
542 ip6 = ip6cp->ip6c_ip6;
543 off = ip6cp->ip6c_off;
544 cmdarg = ip6cp->ip6c_cmdarg;
545 sa6_src = ip6cp->ip6c_src;
555 * XXX: We assume that when IPV6 is non NULL,
556 * M and OFF are valid.
559 /* Check if we can safely examine src and dst ports. */
560 if (m->m_pkthdr.len < off + sizeof(*uhp))
563 bzero(&uh, sizeof(uh));
564 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
566 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
567 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
570 (void)in6_pcbnotify(pcbinfo, sa, 0,
571 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
575 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
578 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
582 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
585 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
589 udp6_getcred(SYSCTL_HANDLER_ARGS)
592 struct sockaddr_in6 addrs[2];
596 error = priv_check(req->td, PRIV_NETINET_GETCRED);
600 if (req->newlen != sizeof(addrs))
602 if (req->oldlen != sizeof(struct xucred))
604 error = SYSCTL_IN(req, addrs, sizeof(addrs));
607 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
608 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
611 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
612 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
613 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
615 INP_RLOCK_ASSERT(inp);
616 if (inp->inp_socket == NULL)
619 error = cr_canseesocket(req->td->td_ucred,
622 cru2x(inp->inp_cred, &xuc);
627 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
631 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
632 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
635 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
636 struct mbuf *control, struct thread *td)
638 u_int32_t ulen = m->m_pkthdr.len;
639 u_int32_t plen = sizeof(struct udphdr) + ulen;
642 struct in6_addr *laddr, *faddr, in6a;
643 struct sockaddr_in6 *sin6 = NULL;
644 struct ifnet *oifp = NULL;
645 int cscov_partial = 0;
646 int scope_ambiguous = 0;
651 struct ip6_pktopts *optp, opt;
652 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
654 struct sockaddr_in6 tmp;
656 INP_WLOCK_ASSERT(inp);
657 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
660 /* addr6 has been validated in udp6_send(). */
661 sin6 = (struct sockaddr_in6 *)addr6;
663 /* protect *sin6 from overwrites */
668 * Application should provide a proper zone ID or the use of
669 * default zone IDs should be enabled. Unfortunately, some
670 * applications do not behave as it should, so we need a
671 * workaround. Even if an appropriate ID is not determined,
672 * we'll see if we can determine the outgoing interface. If we
673 * can, determine the zone ID based on the interface below.
675 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
677 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
682 if ((error = ip6_setpktopts(control, &opt,
683 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
687 optp = inp->in6p_outputopts;
690 faddr = &sin6->sin6_addr;
693 * Since we saw no essential reason for calling in_pcbconnect,
694 * we get rid of such kind of logic, and call in6_selectsrc
695 * and in6_pcbsetport in order to fill in the local address
696 * and the local port.
698 if (sin6->sin6_port == 0) {
699 error = EADDRNOTAVAIL;
703 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
704 /* how about ::ffff:0.0.0.0 case? */
709 fport = sin6->sin6_port; /* allow 0 port */
711 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
712 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
714 * I believe we should explicitly discard the
715 * packet when mapped addresses are disabled,
716 * rather than send the packet as an IPv6 one.
717 * If we chose the latter approach, the packet
718 * might be sent out on the wire based on the
719 * default route, the situation which we'd
720 * probably want to avoid.
721 * (20010421 jinmei@kame.net)
726 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
727 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
729 * when remote addr is an IPv4-mapped address,
730 * local addr should not be an IPv6 address,
731 * since you cannot determine how to map IPv6
732 * source address to IPv4.
741 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
742 error = in6_selectsrc(sin6, optp, inp, NULL,
743 td->td_ucred, &oifp, &in6a);
746 if (oifp && scope_ambiguous &&
747 (error = in6_setscope(&sin6->sin6_addr,
753 laddr = &inp->in6p_laddr; /* XXX */
756 error = EADDRNOTAVAIL;
759 if (inp->inp_lport == 0 &&
760 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
761 /* Undo an address bind that may have occurred. */
762 inp->in6p_laddr = in6addr_any;
766 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
770 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
771 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
773 * XXX: this case would happen when the
774 * application sets the V6ONLY flag after
775 * connecting the foreign address.
776 * Such applications should be fixed,
779 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
780 "option was set for a connected socket\n");
786 laddr = &inp->in6p_laddr;
787 faddr = &inp->in6p_faddr;
788 fport = inp->inp_fport;
792 hlen = sizeof(struct ip);
795 * Calculate data length and get a mbuf
796 * for UDP and IP6 headers.
798 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
805 * Stuff checksum and output datagram.
807 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
808 IPPROTO_UDP : IPPROTO_UDPLITE;
809 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
810 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
811 udp6->uh_dport = fport;
812 if (nxt == IPPROTO_UDPLITE) {
816 cscov = up->u_txcslen;
819 udp6->uh_ulen = htons(cscov);
821 * For UDP-Lite, checksum coverage length of zero means
822 * the entire UDPLite packet is covered by the checksum.
824 cscov_partial = (cscov == 0) ? 0 : 1;
825 } else if (plen <= 0xffff)
826 udp6->uh_ulen = htons((u_short)plen);
833 ip6 = mtod(m, struct ip6_hdr *);
834 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
835 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
836 ip6->ip6_vfc |= IPV6_VERSION;
837 ip6->ip6_plen = htons((u_short)plen);
839 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
840 ip6->ip6_src = *laddr;
841 ip6->ip6_dst = *faddr;
844 if ((udp6->uh_sum = in6_cksum(m, 0,
845 sizeof(struct ip6_hdr), cscov)) == 0)
846 udp6->uh_sum = 0xffff;
848 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
849 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
850 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
855 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
856 UDPSTAT_INC(udps_opackets);
857 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
861 error = EAFNOSUPPORT;
871 ip6_clearpktopts(&opt, -1);
878 udp6_abort(struct socket *so)
881 struct inpcbinfo *pcbinfo;
883 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
885 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
888 if (inp->inp_vflag & INP_IPV4) {
889 struct pr_usrreqs *pru;
891 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
892 (*pru->pru_abort)(so);
898 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
899 INP_HASH_WLOCK(pcbinfo);
900 in6_pcbdisconnect(inp);
901 inp->in6p_laddr = in6addr_any;
902 INP_HASH_WUNLOCK(pcbinfo);
903 soisdisconnected(so);
909 udp6_attach(struct socket *so, int proto, struct thread *td)
912 struct inpcbinfo *pcbinfo;
915 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
917 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
919 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
920 error = soreserve(so, udp_sendspace, udp_recvspace);
924 INP_INFO_WLOCK(pcbinfo);
925 error = in_pcballoc(so, pcbinfo);
927 INP_INFO_WUNLOCK(pcbinfo);
930 inp = (struct inpcb *)so->so_pcb;
931 inp->inp_vflag |= INP_IPV6;
932 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
933 inp->inp_vflag |= INP_IPV4;
934 inp->in6p_hops = -1; /* use kernel default */
935 inp->in6p_cksum = -1; /* just to be sure */
938 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
939 * because the socket may be bound to an IPv6 wildcard address,
940 * which may match an IPv4-mapped IPv6 address.
942 inp->inp_ip_ttl = V_ip_defttl;
944 error = udp_newudpcb(inp);
948 INP_INFO_WUNLOCK(pcbinfo);
952 INP_INFO_WUNLOCK(pcbinfo);
957 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
960 struct inpcbinfo *pcbinfo;
963 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
965 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
968 INP_HASH_WLOCK(pcbinfo);
969 inp->inp_vflag &= ~INP_IPV4;
970 inp->inp_vflag |= INP_IPV6;
971 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
972 struct sockaddr_in6 *sin6_p;
974 sin6_p = (struct sockaddr_in6 *)nam;
976 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
977 inp->inp_vflag |= INP_IPV4;
979 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
980 struct sockaddr_in sin;
982 in6_sin6_2_sin(&sin, sin6_p);
983 inp->inp_vflag |= INP_IPV4;
984 inp->inp_vflag &= ~INP_IPV6;
985 error = in_pcbbind(inp, (struct sockaddr *)&sin,
992 error = in6_pcbbind(inp, nam, td->td_ucred);
996 INP_HASH_WUNLOCK(pcbinfo);
1002 udp6_close(struct socket *so)
1005 struct inpcbinfo *pcbinfo;
1007 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1008 inp = sotoinpcb(so);
1009 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
1012 if (inp->inp_vflag & INP_IPV4) {
1013 struct pr_usrreqs *pru;
1015 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1016 (*pru->pru_disconnect)(so);
1021 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1022 INP_HASH_WLOCK(pcbinfo);
1023 in6_pcbdisconnect(inp);
1024 inp->in6p_laddr = in6addr_any;
1025 INP_HASH_WUNLOCK(pcbinfo);
1026 soisdisconnected(so);
1032 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1035 struct inpcbinfo *pcbinfo;
1036 struct sockaddr_in6 *sin6;
1039 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1040 inp = sotoinpcb(so);
1041 sin6 = (struct sockaddr_in6 *)nam;
1042 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1045 * XXXRW: Need to clarify locking of v4/v6 flags.
1049 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1050 struct sockaddr_in sin;
1052 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1056 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1060 in6_sin6_2_sin(&sin, sin6);
1061 inp->inp_vflag |= INP_IPV4;
1062 inp->inp_vflag &= ~INP_IPV6;
1063 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1066 INP_HASH_WLOCK(pcbinfo);
1067 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1069 INP_HASH_WUNLOCK(pcbinfo);
1075 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1079 inp->inp_vflag &= ~INP_IPV4;
1080 inp->inp_vflag |= INP_IPV6;
1081 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1084 INP_HASH_WLOCK(pcbinfo);
1085 error = in6_pcbconnect(inp, nam, td->td_ucred);
1086 INP_HASH_WUNLOCK(pcbinfo);
1095 udp6_detach(struct socket *so)
1098 struct inpcbinfo *pcbinfo;
1101 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1102 inp = sotoinpcb(so);
1103 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1105 INP_INFO_WLOCK(pcbinfo);
1107 up = intoudpcb(inp);
1108 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1111 INP_INFO_WUNLOCK(pcbinfo);
1116 udp6_disconnect(struct socket *so)
1119 struct inpcbinfo *pcbinfo;
1122 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1123 inp = sotoinpcb(so);
1124 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1127 if (inp->inp_vflag & INP_IPV4) {
1128 struct pr_usrreqs *pru;
1130 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1131 (void)(*pru->pru_disconnect)(so);
1138 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1143 INP_HASH_WLOCK(pcbinfo);
1144 in6_pcbdisconnect(inp);
1145 inp->in6p_laddr = in6addr_any;
1146 INP_HASH_WUNLOCK(pcbinfo);
1148 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1156 udp6_send(struct socket *so, int flags, struct mbuf *m,
1157 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1160 struct inpcbinfo *pcbinfo;
1163 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1164 inp = sotoinpcb(so);
1165 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1169 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1173 if (addr->sa_family != AF_INET6) {
1174 error = EAFNOSUPPORT;
1180 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1182 struct sockaddr_in6 *sin6 = 0;
1185 hasv4addr = (inp->inp_vflag & INP_IPV4);
1187 sin6 = (struct sockaddr_in6 *)addr;
1188 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1192 struct pr_usrreqs *pru;
1195 * XXXRW: We release UDP-layer locks before calling
1196 * udp_send() in order to avoid recursion. However,
1197 * this does mean there is a short window where inp's
1198 * fields are unstable. Could this lead to a
1199 * potential race in which the factors causing us to
1200 * select the UDPv4 output routine are invalidated?
1204 in6_sin6_2_sin_in_sock(addr);
1205 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1206 /* addr will just be freed in sendit(). */
1207 return ((*pru->pru_send)(so, flags, m, addr, control,
1213 mac_inpcb_create_mbuf(inp, m);
1215 INP_HASH_WLOCK(pcbinfo);
1216 error = udp6_output(inp, m, addr, control, td);
1217 INP_HASH_WUNLOCK(pcbinfo);
1229 struct pr_usrreqs udp6_usrreqs = {
1230 .pru_abort = udp6_abort,
1231 .pru_attach = udp6_attach,
1232 .pru_bind = udp6_bind,
1233 .pru_connect = udp6_connect,
1234 .pru_control = in6_control,
1235 .pru_detach = udp6_detach,
1236 .pru_disconnect = udp6_disconnect,
1237 .pru_peeraddr = in6_mapped_peeraddr,
1238 .pru_send = udp6_send,
1239 .pru_shutdown = udp_shutdown,
1240 .pru_sockaddr = in6_mapped_sockaddr,
1241 .pru_soreceive = soreceive_dgram,
1242 .pru_sosend = sosend_dgram,
1243 .pru_sosetlabel = in_pcbsosetlabel,
1244 .pru_close = udp6_close