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)
144 INP_LOCK_ASSERT(inp);
147 * Engage the tunneling protocol.
150 if (up->u_tun_func != NULL) {
151 (*up->u_tun_func)(n, off, inp);
155 /* Check AH/ESP integrity. */
156 if (ipsec6_in_reject(n, inp)) {
158 IPSEC6STAT_INC(ips_in_polvio);
163 if (mac_inpcb_check_deliver(inp, n) != 0) {
169 if (inp->inp_flags & INP_CONTROLOPTS ||
170 inp->inp_socket->so_options & SO_TIMESTAMP)
171 ip6_savecontrol(inp, n, &opts);
172 m_adj(n, off + sizeof(struct udphdr));
174 so = inp->inp_socket;
175 SOCKBUF_LOCK(&so->so_rcv);
176 if (sbappendaddr_locked(&so->so_rcv, (struct sockaddr *)fromsa, n,
178 SOCKBUF_UNLOCK(&so->so_rcv);
182 UDPSTAT_INC(udps_fullsock);
184 sorwakeup_locked(so);
188 udp6_input(struct mbuf **mp, int *offp, int proto)
190 struct mbuf *m = *mp;
195 struct inpcbinfo *pcbinfo;
200 struct sockaddr_in6 fromsa;
201 struct m_tag *fwd_tag;
205 ifp = m->m_pkthdr.rcvif;
206 ip6 = mtod(m, struct ip6_hdr *);
208 if (faithprefix_p != NULL && (*faithprefix_p)(&ip6->ip6_dst)) {
209 /* XXX send icmp6 host/port unreach? */
211 return (IPPROTO_DONE);
214 #ifndef PULLDOWN_TEST
215 IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
216 ip6 = mtod(m, struct ip6_hdr *);
217 uh = (struct udphdr *)((caddr_t)ip6 + off);
219 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(*uh));
221 return (IPPROTO_DONE);
224 UDPSTAT_INC(udps_ipackets);
227 * Destination port of 0 is illegal, based on RFC768.
229 if (uh->uh_dport == 0)
232 plen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6);
233 ulen = ntohs((u_short)uh->uh_ulen);
236 cscov_partial = (nxt == IPPROTO_UDPLITE) ? 1 : 0;
237 if (nxt == IPPROTO_UDPLITE) {
238 /* Zero means checksum over the complete packet. */
243 if ((ulen < sizeof(struct udphdr)) || (ulen > plen)) {
244 /* XXX: What is the right UDPLite MIB counter? */
247 if (uh->uh_sum == 0) {
248 /* XXX: What is the right UDPLite MIB counter? */
252 if ((ulen < sizeof(struct udphdr)) || (plen != ulen)) {
253 UDPSTAT_INC(udps_badlen);
256 if (uh->uh_sum == 0) {
257 UDPSTAT_INC(udps_nosum);
262 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID_IPV6) &&
264 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
265 uh_sum = m->m_pkthdr.csum_data;
267 uh_sum = in6_cksum_pseudo(ip6, ulen, nxt,
268 m->m_pkthdr.csum_data);
271 uh_sum = in6_cksum_partial(m, nxt, off, plen, ulen);
274 UDPSTAT_INC(udps_badsum);
279 * Construct sockaddr format source address.
281 init_sin6(&fromsa, m);
282 fromsa.sin6_port = uh->uh_sport;
284 pcbinfo = get_inpcbinfo(nxt);
285 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
287 struct inpcbhead *pcblist;
288 struct ip6_moptions *imo;
290 INP_INFO_RLOCK(pcbinfo);
292 * In the event that laddr should be set to the link-local
293 * address (this happens in RIPng), the multicast address
294 * specified in the received packet will not match laddr. To
295 * handle this situation, matching is relaxed if the
296 * receiving interface is the same as one specified in the
297 * socket and if the destination multicast address matches
298 * one of the multicast groups specified in the socket.
302 * KAME note: traditionally we dropped udpiphdr from mbuf
303 * here. We need udphdr for IPsec processing so we do that
306 pcblist = get_pcblist(nxt);
308 LIST_FOREACH(inp, pcblist, inp_list) {
309 if ((inp->inp_vflag & INP_IPV6) == 0)
311 if (inp->inp_lport != uh->uh_dport)
313 if (inp->inp_fport != 0 &&
314 inp->inp_fport != uh->uh_sport)
316 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) {
317 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_laddr,
321 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
322 if (!IN6_ARE_ADDR_EQUAL(&inp->in6p_faddr,
324 inp->inp_fport != uh->uh_sport)
329 * XXXRW: Because we weren't holding either the inpcb
330 * or the hash lock when we checked for a match
331 * before, we should probably recheck now that the
332 * inpcb lock is (supposed to be) held.
336 * Handle socket delivery policy for any-source
337 * and source-specific multicast. [RFC3678]
339 imo = inp->in6p_moptions;
340 if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
341 struct sockaddr_in6 mcaddr;
346 bzero(&mcaddr, sizeof(struct sockaddr_in6));
347 mcaddr.sin6_len = sizeof(struct sockaddr_in6);
348 mcaddr.sin6_family = AF_INET6;
349 mcaddr.sin6_addr = ip6->ip6_dst;
351 blocked = im6o_mc_filter(imo, ifp,
352 (struct sockaddr *)&mcaddr,
353 (struct sockaddr *)&fromsa);
354 if (blocked != MCAST_PASS) {
355 if (blocked == MCAST_NOTGMEMBER)
356 IP6STAT_INC(ip6s_notmember);
357 if (blocked == MCAST_NOTSMEMBER ||
358 blocked == MCAST_MUTED)
359 UDPSTAT_INC(udps_filtermcast);
360 INP_RUNLOCK(inp); /* XXX */
369 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
371 UDP_PROBE(receive, NULL, last, ip6,
373 udp6_append(last, n, off, &fromsa);
379 * Don't look for additional matches if this one does
380 * not have either the SO_REUSEPORT or SO_REUSEADDR
381 * socket options set. This heuristic avoids
382 * searching through all pcbs in the common case of a
383 * non-shared port. It assumes that an application
384 * will never clear these options after setting them.
386 if ((last->inp_socket->so_options &
387 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
393 * No matching pcb found; discard datagram. (No need
394 * to send an ICMP Port Unreachable for a broadcast
395 * or multicast datgram.)
397 UDPSTAT_INC(udps_noport);
398 UDPSTAT_INC(udps_noportmcast);
402 INP_INFO_RUNLOCK(pcbinfo);
403 UDP_PROBE(receive, NULL, last, ip6, last, uh);
404 udp6_append(last, m, off, &fromsa);
406 return (IPPROTO_DONE);
409 * Locate pcb for datagram.
413 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
415 if ((m->m_flags & M_IP6_NEXTHOP) &&
416 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
417 struct sockaddr_in6 *next_hop6;
419 next_hop6 = (struct sockaddr_in6 *)(fwd_tag + 1);
422 * Transparently forwarded. Pretend to be the destination.
423 * Already got one like this?
425 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
426 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
427 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif, m);
430 * It's new. Try to find the ambushing socket.
431 * Because we've rewritten the destination address,
432 * any hardware-generated hash is ignored.
434 inp = in6_pcblookup(pcbinfo, &ip6->ip6_src,
435 uh->uh_sport, &next_hop6->sin6_addr,
436 next_hop6->sin6_port ? htons(next_hop6->sin6_port) :
437 uh->uh_dport, INPLOOKUP_WILDCARD |
438 INPLOOKUP_RLOCKPCB, m->m_pkthdr.rcvif);
440 /* Remove the tag from the packet. We don't need it anymore. */
441 m_tag_delete(m, fwd_tag);
442 m->m_flags &= ~M_IP6_NEXTHOP;
444 inp = in6_pcblookup_mbuf(pcbinfo, &ip6->ip6_src,
445 uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
446 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB,
447 m->m_pkthdr.rcvif, m);
449 if (udp_log_in_vain) {
450 char ip6bufs[INET6_ADDRSTRLEN];
451 char ip6bufd[INET6_ADDRSTRLEN];
454 "Connection attempt to UDP [%s]:%d from [%s]:%d\n",
455 ip6_sprintf(ip6bufd, &ip6->ip6_dst),
457 ip6_sprintf(ip6bufs, &ip6->ip6_src),
458 ntohs(uh->uh_sport));
460 UDPSTAT_INC(udps_noport);
461 if (m->m_flags & M_MCAST) {
462 printf("UDP6: M_MCAST is set in a unicast packet.\n");
463 UDPSTAT_INC(udps_noportmcast);
468 if (badport_bandlim(BANDLIM_ICMP6_UNREACH) < 0)
470 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
471 return (IPPROTO_DONE);
473 INP_RLOCK_ASSERT(inp);
476 if (up->u_rxcslen == 0 || up->u_rxcslen > ulen) {
479 return (IPPROTO_DONE);
482 UDP_PROBE(receive, NULL, inp, ip6, inp, uh);
483 udp6_append(inp, m, off, &fromsa);
485 return (IPPROTO_DONE);
488 INP_INFO_RUNLOCK(pcbinfo);
492 return (IPPROTO_DONE);
496 udp6_common_ctlinput(int cmd, struct sockaddr *sa, void *d,
497 struct inpcbinfo *pcbinfo)
503 struct ip6ctlparam *ip6cp = NULL;
504 const struct sockaddr_in6 *sa6_src = NULL;
506 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
507 struct udp_portonly {
512 if (sa->sa_family != AF_INET6 ||
513 sa->sa_len != sizeof(struct sockaddr_in6))
516 if ((unsigned)cmd >= PRC_NCMDS)
518 if (PRC_IS_REDIRECT(cmd))
519 notify = in6_rtchange, d = NULL;
520 else if (cmd == PRC_HOSTDEAD)
522 else if (inet6ctlerrmap[cmd] == 0)
525 /* if the parameter is from icmp6, decode it. */
527 ip6cp = (struct ip6ctlparam *)d;
529 ip6 = ip6cp->ip6c_ip6;
530 off = ip6cp->ip6c_off;
531 cmdarg = ip6cp->ip6c_cmdarg;
532 sa6_src = ip6cp->ip6c_src;
542 * XXX: We assume that when IPV6 is non NULL,
543 * M and OFF are valid.
546 /* Check if we can safely examine src and dst ports. */
547 if (m->m_pkthdr.len < off + sizeof(*uhp))
550 bzero(&uh, sizeof(uh));
551 m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
553 (void)in6_pcbnotify(pcbinfo, sa, uh.uh_dport,
554 (struct sockaddr *)ip6cp->ip6c_src, uh.uh_sport, cmd,
557 (void)in6_pcbnotify(pcbinfo, sa, 0,
558 (const struct sockaddr *)sa6_src, 0, cmd, cmdarg, notify);
562 udp6_ctlinput(int cmd, struct sockaddr *sa, void *d)
565 return (udp6_common_ctlinput(cmd, sa, d, &V_udbinfo));
569 udplite6_ctlinput(int cmd, struct sockaddr *sa, void *d)
572 return (udp6_common_ctlinput(cmd, sa, d, &V_ulitecbinfo));
576 udp6_getcred(SYSCTL_HANDLER_ARGS)
579 struct sockaddr_in6 addrs[2];
583 error = priv_check(req->td, PRIV_NETINET_GETCRED);
587 if (req->newlen != sizeof(addrs))
589 if (req->oldlen != sizeof(struct xucred))
591 error = SYSCTL_IN(req, addrs, sizeof(addrs));
594 if ((error = sa6_embedscope(&addrs[0], V_ip6_use_defzone)) != 0 ||
595 (error = sa6_embedscope(&addrs[1], V_ip6_use_defzone)) != 0) {
598 inp = in6_pcblookup(&V_udbinfo, &addrs[1].sin6_addr,
599 addrs[1].sin6_port, &addrs[0].sin6_addr, addrs[0].sin6_port,
600 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
602 INP_RLOCK_ASSERT(inp);
603 if (inp->inp_socket == NULL)
606 error = cr_canseesocket(req->td->td_ucred,
609 cru2x(inp->inp_cred, &xuc);
614 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
618 SYSCTL_PROC(_net_inet6_udp6, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 0,
619 0, udp6_getcred, "S,xucred", "Get the xucred of a UDP6 connection");
622 udp6_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr6,
623 struct mbuf *control, struct thread *td)
625 u_int32_t ulen = m->m_pkthdr.len;
626 u_int32_t plen = sizeof(struct udphdr) + ulen;
629 struct in6_addr *laddr, *faddr, in6a;
630 struct sockaddr_in6 *sin6 = NULL;
631 struct ifnet *oifp = NULL;
632 int cscov_partial = 0;
633 int scope_ambiguous = 0;
638 struct ip6_pktopts *optp, opt;
639 int af = AF_INET6, hlen = sizeof(struct ip6_hdr);
641 struct sockaddr_in6 tmp;
643 INP_WLOCK_ASSERT(inp);
644 INP_HASH_WLOCK_ASSERT(inp->inp_pcbinfo);
647 /* addr6 has been validated in udp6_send(). */
648 sin6 = (struct sockaddr_in6 *)addr6;
650 /* protect *sin6 from overwrites */
655 * Application should provide a proper zone ID or the use of
656 * default zone IDs should be enabled. Unfortunately, some
657 * applications do not behave as it should, so we need a
658 * workaround. Even if an appropriate ID is not determined,
659 * we'll see if we can determine the outgoing interface. If we
660 * can, determine the zone ID based on the interface below.
662 if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
664 if ((error = sa6_embedscope(sin6, V_ip6_use_defzone)) != 0)
669 if ((error = ip6_setpktopts(control, &opt,
670 inp->in6p_outputopts, td->td_ucred, IPPROTO_UDP)) != 0)
674 optp = inp->in6p_outputopts;
677 faddr = &sin6->sin6_addr;
680 * Since we saw no essential reason for calling in_pcbconnect,
681 * we get rid of such kind of logic, and call in6_selectsrc
682 * and in6_pcbsetport in order to fill in the local address
683 * and the local port.
685 if (sin6->sin6_port == 0) {
686 error = EADDRNOTAVAIL;
690 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
691 /* how about ::ffff:0.0.0.0 case? */
696 fport = sin6->sin6_port; /* allow 0 port */
698 if (IN6_IS_ADDR_V4MAPPED(faddr)) {
699 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
701 * I believe we should explicitly discard the
702 * packet when mapped addresses are disabled,
703 * rather than send the packet as an IPv6 one.
704 * If we chose the latter approach, the packet
705 * might be sent out on the wire based on the
706 * default route, the situation which we'd
707 * probably want to avoid.
708 * (20010421 jinmei@kame.net)
713 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) &&
714 !IN6_IS_ADDR_V4MAPPED(&inp->in6p_laddr)) {
716 * when remote addr is an IPv4-mapped address,
717 * local addr should not be an IPv6 address,
718 * since you cannot determine how to map IPv6
719 * source address to IPv4.
728 if (!IN6_IS_ADDR_V4MAPPED(faddr)) {
729 error = in6_selectsrc(sin6, optp, inp, NULL,
730 td->td_ucred, &oifp, &in6a);
733 if (oifp && scope_ambiguous &&
734 (error = in6_setscope(&sin6->sin6_addr,
740 laddr = &inp->in6p_laddr; /* XXX */
743 error = EADDRNOTAVAIL;
746 if (inp->inp_lport == 0 &&
747 (error = in6_pcbsetport(laddr, inp, td->td_ucred)) != 0) {
748 /* Undo an address bind that may have occurred. */
749 inp->in6p_laddr = in6addr_any;
753 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
757 if (IN6_IS_ADDR_V4MAPPED(&inp->in6p_faddr)) {
758 if ((inp->inp_flags & IN6P_IPV6_V6ONLY)) {
760 * XXX: this case would happen when the
761 * application sets the V6ONLY flag after
762 * connecting the foreign address.
763 * Such applications should be fixed,
766 log(LOG_INFO, "udp6_output: IPV6_V6ONLY "
767 "option was set for a connected socket\n");
773 laddr = &inp->in6p_laddr;
774 faddr = &inp->in6p_faddr;
775 fport = inp->inp_fport;
779 hlen = sizeof(struct ip);
782 * Calculate data length and get a mbuf
783 * for UDP and IP6 headers.
785 M_PREPEND(m, hlen + sizeof(struct udphdr), M_NOWAIT);
792 * Stuff checksum and output datagram.
794 nxt = (inp->inp_socket->so_proto->pr_protocol == IPPROTO_UDP) ?
795 IPPROTO_UDP : IPPROTO_UDPLITE;
796 udp6 = (struct udphdr *)(mtod(m, caddr_t) + hlen);
797 udp6->uh_sport = inp->inp_lport; /* lport is always set in the PCB */
798 udp6->uh_dport = fport;
799 if (nxt == IPPROTO_UDPLITE) {
803 cscov = up->u_txcslen;
806 udp6->uh_ulen = htons(cscov);
808 * For UDP-Lite, checksum coverage length of zero means
809 * the entire UDPLite packet is covered by the checksum.
811 cscov_partial = (cscov == 0) ? 0 : 1;
812 } else if (plen <= 0xffff)
813 udp6->uh_ulen = htons((u_short)plen);
820 ip6 = mtod(m, struct ip6_hdr *);
821 ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK;
822 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
823 ip6->ip6_vfc |= IPV6_VERSION;
824 ip6->ip6_plen = htons((u_short)plen);
826 ip6->ip6_hlim = in6_selecthlim(inp, NULL);
827 ip6->ip6_src = *laddr;
828 ip6->ip6_dst = *faddr;
831 if ((udp6->uh_sum = in6_cksum_partial(m, nxt,
832 sizeof(struct ip6_hdr), plen, cscov)) == 0)
833 udp6->uh_sum = 0xffff;
835 udp6->uh_sum = in6_cksum_pseudo(ip6, plen, nxt, 0);
836 m->m_pkthdr.csum_flags = CSUM_UDP_IPV6;
837 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
842 UDP_PROBE(send, NULL, inp, ip6, inp, udp6);
843 UDPSTAT_INC(udps_opackets);
844 error = ip6_output(m, optp, NULL, flags, inp->in6p_moptions,
848 error = EAFNOSUPPORT;
858 ip6_clearpktopts(&opt, -1);
865 udp6_abort(struct socket *so)
868 struct inpcbinfo *pcbinfo;
870 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
872 KASSERT(inp != NULL, ("udp6_abort: inp == NULL"));
875 if (inp->inp_vflag & INP_IPV4) {
876 struct pr_usrreqs *pru;
878 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
879 (*pru->pru_abort)(so);
885 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
886 INP_HASH_WLOCK(pcbinfo);
887 in6_pcbdisconnect(inp);
888 inp->in6p_laddr = in6addr_any;
889 INP_HASH_WUNLOCK(pcbinfo);
890 soisdisconnected(so);
896 udp6_attach(struct socket *so, int proto, struct thread *td)
899 struct inpcbinfo *pcbinfo;
902 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
904 KASSERT(inp == NULL, ("udp6_attach: inp != NULL"));
906 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
907 error = soreserve(so, udp_sendspace, udp_recvspace);
911 INP_INFO_WLOCK(pcbinfo);
912 error = in_pcballoc(so, pcbinfo);
914 INP_INFO_WUNLOCK(pcbinfo);
917 inp = (struct inpcb *)so->so_pcb;
918 inp->inp_vflag |= INP_IPV6;
919 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0)
920 inp->inp_vflag |= INP_IPV4;
921 inp->in6p_hops = -1; /* use kernel default */
922 inp->in6p_cksum = -1; /* just to be sure */
925 * IPv4 TTL initialization is necessary for an IPv6 socket as well,
926 * because the socket may be bound to an IPv6 wildcard address,
927 * which may match an IPv4-mapped IPv6 address.
929 inp->inp_ip_ttl = V_ip_defttl;
931 error = udp_newudpcb(inp);
935 INP_INFO_WUNLOCK(pcbinfo);
939 INP_INFO_WUNLOCK(pcbinfo);
944 udp6_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
947 struct inpcbinfo *pcbinfo;
950 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
952 KASSERT(inp != NULL, ("udp6_bind: inp == NULL"));
955 INP_HASH_WLOCK(pcbinfo);
956 inp->inp_vflag &= ~INP_IPV4;
957 inp->inp_vflag |= INP_IPV6;
958 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
959 struct sockaddr_in6 *sin6_p;
961 sin6_p = (struct sockaddr_in6 *)nam;
963 if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr))
964 inp->inp_vflag |= INP_IPV4;
966 else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) {
967 struct sockaddr_in sin;
969 in6_sin6_2_sin(&sin, sin6_p);
970 inp->inp_vflag |= INP_IPV4;
971 inp->inp_vflag &= ~INP_IPV6;
972 error = in_pcbbind(inp, (struct sockaddr *)&sin,
979 error = in6_pcbbind(inp, nam, td->td_ucred);
983 INP_HASH_WUNLOCK(pcbinfo);
989 udp6_close(struct socket *so)
992 struct inpcbinfo *pcbinfo;
994 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
996 KASSERT(inp != NULL, ("udp6_close: inp == NULL"));
999 if (inp->inp_vflag & INP_IPV4) {
1000 struct pr_usrreqs *pru;
1002 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1003 (*pru->pru_disconnect)(so);
1008 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1009 INP_HASH_WLOCK(pcbinfo);
1010 in6_pcbdisconnect(inp);
1011 inp->in6p_laddr = in6addr_any;
1012 INP_HASH_WUNLOCK(pcbinfo);
1013 soisdisconnected(so);
1019 udp6_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1022 struct inpcbinfo *pcbinfo;
1023 struct sockaddr_in6 *sin6;
1026 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1027 inp = sotoinpcb(so);
1028 sin6 = (struct sockaddr_in6 *)nam;
1029 KASSERT(inp != NULL, ("udp6_connect: inp == NULL"));
1032 * XXXRW: Need to clarify locking of v4/v6 flags.
1036 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
1037 struct sockaddr_in sin;
1039 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) != 0) {
1043 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1047 in6_sin6_2_sin(&sin, sin6);
1048 inp->inp_vflag |= INP_IPV4;
1049 inp->inp_vflag &= ~INP_IPV6;
1050 error = prison_remote_ip4(td->td_ucred, &sin.sin_addr);
1053 INP_HASH_WLOCK(pcbinfo);
1054 error = in_pcbconnect(inp, (struct sockaddr *)&sin,
1056 INP_HASH_WUNLOCK(pcbinfo);
1062 if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1066 inp->inp_vflag &= ~INP_IPV4;
1067 inp->inp_vflag |= INP_IPV6;
1068 error = prison_remote_ip6(td->td_ucred, &sin6->sin6_addr);
1071 INP_HASH_WLOCK(pcbinfo);
1072 error = in6_pcbconnect(inp, nam, td->td_ucred);
1073 INP_HASH_WUNLOCK(pcbinfo);
1082 udp6_detach(struct socket *so)
1085 struct inpcbinfo *pcbinfo;
1088 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1089 inp = sotoinpcb(so);
1090 KASSERT(inp != NULL, ("udp6_detach: inp == NULL"));
1092 INP_INFO_WLOCK(pcbinfo);
1094 up = intoudpcb(inp);
1095 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1098 INP_INFO_WUNLOCK(pcbinfo);
1103 udp6_disconnect(struct socket *so)
1106 struct inpcbinfo *pcbinfo;
1109 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1110 inp = sotoinpcb(so);
1111 KASSERT(inp != NULL, ("udp6_disconnect: inp == NULL"));
1114 if (inp->inp_vflag & INP_IPV4) {
1115 struct pr_usrreqs *pru;
1117 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1118 (void)(*pru->pru_disconnect)(so);
1125 if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) {
1130 INP_HASH_WLOCK(pcbinfo);
1131 in6_pcbdisconnect(inp);
1132 inp->in6p_laddr = in6addr_any;
1133 INP_HASH_WUNLOCK(pcbinfo);
1135 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1143 udp6_send(struct socket *so, int flags, struct mbuf *m,
1144 struct sockaddr *addr, struct mbuf *control, struct thread *td)
1147 struct inpcbinfo *pcbinfo;
1150 pcbinfo = get_inpcbinfo(so->so_proto->pr_protocol);
1151 inp = sotoinpcb(so);
1152 KASSERT(inp != NULL, ("udp6_send: inp == NULL"));
1156 if (addr->sa_len != sizeof(struct sockaddr_in6)) {
1160 if (addr->sa_family != AF_INET6) {
1161 error = EAFNOSUPPORT;
1167 if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) {
1169 struct sockaddr_in6 *sin6 = 0;
1172 hasv4addr = (inp->inp_vflag & INP_IPV4);
1174 sin6 = (struct sockaddr_in6 *)addr;
1175 hasv4addr = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)
1179 struct pr_usrreqs *pru;
1182 * XXXRW: We release UDP-layer locks before calling
1183 * udp_send() in order to avoid recursion. However,
1184 * this does mean there is a short window where inp's
1185 * fields are unstable. Could this lead to a
1186 * potential race in which the factors causing us to
1187 * select the UDPv4 output routine are invalidated?
1191 in6_sin6_2_sin_in_sock(addr);
1192 pru = inetsw[ip_protox[IPPROTO_UDP]].pr_usrreqs;
1193 /* addr will just be freed in sendit(). */
1194 return ((*pru->pru_send)(so, flags, m, addr, control,
1200 mac_inpcb_create_mbuf(inp, m);
1202 INP_HASH_WLOCK(pcbinfo);
1203 error = udp6_output(inp, m, addr, control, td);
1204 INP_HASH_WUNLOCK(pcbinfo);
1216 struct pr_usrreqs udp6_usrreqs = {
1217 .pru_abort = udp6_abort,
1218 .pru_attach = udp6_attach,
1219 .pru_bind = udp6_bind,
1220 .pru_connect = udp6_connect,
1221 .pru_control = in6_control,
1222 .pru_detach = udp6_detach,
1223 .pru_disconnect = udp6_disconnect,
1224 .pru_peeraddr = in6_mapped_peeraddr,
1225 .pru_send = udp6_send,
1226 .pru_shutdown = udp_shutdown,
1227 .pru_sockaddr = in6_mapped_sockaddr,
1228 .pru_soreceive = soreceive_dgram,
1229 .pru_sosend = sosend_dgram,
1230 .pru_sosetlabel = in_pcbsosetlabel,
1231 .pru_close = udp6_close