2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
5 * The Regents of the University of California.
6 * Copyright (c) 2008 Robert N. M. Watson
7 * Copyright (c) 2010-2011 Juniper Networks, Inc.
8 * Copyright (c) 2014 Kevin Lo
11 * Portions of this software were developed by Robert N. M. Watson under
12 * contract to Juniper Networks, Inc.
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
41 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
45 #include "opt_inet6.h"
46 #include "opt_ipsec.h"
49 #include <sys/param.h>
50 #include <sys/domain.h>
51 #include <sys/eventhandler.h>
53 #include <sys/kernel.h>
55 #include <sys/malloc.h>
59 #include <sys/protosw.h>
61 #include <sys/signalvar.h>
62 #include <sys/socket.h>
63 #include <sys/socketvar.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/systm.h>
72 #include <net/if_var.h>
73 #include <net/route.h>
74 #include <net/rss_config.h>
76 #include <netinet/in.h>
77 #include <netinet/in_kdtrace.h>
78 #include <netinet/in_pcb.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip.h>
83 #include <netinet/ip6.h>
85 #include <netinet/ip_icmp.h>
86 #include <netinet/icmp_var.h>
87 #include <netinet/ip_var.h>
88 #include <netinet/ip_options.h>
90 #include <netinet6/ip6_var.h>
92 #include <netinet/udp.h>
93 #include <netinet/udp_var.h>
94 #include <netinet/udplite.h>
95 #include <netinet/in_rss.h>
97 #include <netipsec/ipsec_support.h>
99 #include <machine/in_cksum.h>
101 #include <security/mac/mac_framework.h>
104 * UDP and UDP-Lite protocols implementation.
105 * Per RFC 768, August, 1980.
106 * Per RFC 3828, July, 2004.
110 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums
111 * removes the only data integrity mechanism for packets and malformed
112 * packets that would otherwise be discarded due to bad checksums, and may
113 * cause problems (especially for NFS data blocks).
115 VNET_DEFINE(int, udp_cksum) = 1;
116 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_VNET | CTLFLAG_RW,
117 &VNET_NAME(udp_cksum), 0, "compute udp checksum");
119 VNET_DEFINE(int, udp_log_in_vain) = 0;
120 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_VNET | CTLFLAG_RW,
121 &VNET_NAME(udp_log_in_vain), 0, "Log all incoming UDP packets");
123 VNET_DEFINE(int, udp_blackhole) = 0;
124 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_VNET | CTLFLAG_RW,
125 &VNET_NAME(udp_blackhole), 0,
126 "Do not send port unreachables for refused connects");
128 u_long udp_sendspace = 9216; /* really max datagram size */
129 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
130 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
132 u_long udp_recvspace = 40 * (1024 +
134 sizeof(struct sockaddr_in6)
136 sizeof(struct sockaddr_in)
138 ); /* 40 1K datagrams */
140 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
141 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
143 VNET_DEFINE(struct inpcbhead, udb); /* from udp_var.h */
144 VNET_DEFINE(struct inpcbinfo, udbinfo);
145 VNET_DEFINE(struct inpcbhead, ulitecb);
146 VNET_DEFINE(struct inpcbinfo, ulitecbinfo);
147 VNET_DEFINE_STATIC(uma_zone_t, udpcb_zone);
148 #define V_udpcb_zone VNET(udpcb_zone)
151 #define UDBHASHSIZE 128
154 VNET_PCPUSTAT_DEFINE(struct udpstat, udpstat); /* from udp_var.h */
155 VNET_PCPUSTAT_SYSINIT(udpstat);
156 SYSCTL_VNET_PCPUSTAT(_net_inet_udp, UDPCTL_STATS, stats, struct udpstat,
157 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
160 VNET_PCPUSTAT_SYSUNINIT(udpstat);
163 static void udp_detach(struct socket *so);
164 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
165 struct mbuf *, struct thread *);
169 udp_zone_change(void *tag)
172 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
173 uma_zone_set_max(V_udpcb_zone, maxsockets);
177 udp_inpcb_init(void *mem, int size, int flags)
182 INP_LOCK_INIT(inp, "inp", "udpinp");
187 udplite_inpcb_init(void *mem, int size, int flags)
192 INP_LOCK_INIT(inp, "inp", "udpliteinp");
201 * For now default to 2-tuple UDP hashing - until the fragment
202 * reassembly code can also update the flowid.
204 * Once we can calculate the flowid that way and re-establish
205 * a 4-tuple, flip this to 4-tuple.
207 in_pcbinfo_init(&V_udbinfo, "udp", &V_udb, UDBHASHSIZE, UDBHASHSIZE,
208 "udp_inpcb", udp_inpcb_init, IPI_HASHFIELDS_2TUPLE);
209 V_udpcb_zone = uma_zcreate("udpcb", sizeof(struct udpcb),
210 NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
211 uma_zone_set_max(V_udpcb_zone, maxsockets);
212 uma_zone_set_warning(V_udpcb_zone, "kern.ipc.maxsockets limit reached");
213 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
214 EVENTHANDLER_PRI_ANY);
221 in_pcbinfo_init(&V_ulitecbinfo, "udplite", &V_ulitecb, UDBHASHSIZE,
222 UDBHASHSIZE, "udplite_inpcb", udplite_inpcb_init,
223 IPI_HASHFIELDS_2TUPLE);
227 * Kernel module interface for updating udpstat. The argument is an index
228 * into udpstat treated as an array of u_long. While this encodes the
229 * general layout of udpstat into the caller, it doesn't encode its location,
230 * so that future changes to add, for example, per-CPU stats support won't
231 * cause binary compatibility problems for kernel modules.
234 kmod_udpstat_inc(int statnum)
237 counter_u64_add(VNET(udpstat)[statnum], 1);
241 udp_newudpcb(struct inpcb *inp)
245 up = uma_zalloc(V_udpcb_zone, M_NOWAIT | M_ZERO);
253 udp_discardcb(struct udpcb *up)
256 uma_zfree(V_udpcb_zone, up);
261 udp_destroy(void *unused __unused)
264 in_pcbinfo_destroy(&V_udbinfo);
265 uma_zdestroy(V_udpcb_zone);
267 VNET_SYSUNINIT(udp, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udp_destroy, NULL);
270 udplite_destroy(void *unused __unused)
273 in_pcbinfo_destroy(&V_ulitecbinfo);
275 VNET_SYSUNINIT(udplite, SI_SUB_PROTO_DOMAIN, SI_ORDER_FOURTH, udplite_destroy,
281 * Subroutine of udp_input(), which appends the provided mbuf chain to the
282 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
283 * contains the source address. If the socket ends up being an IPv6 socket,
284 * udp_append() will convert to a sockaddr_in6 before passing the address
285 * into the socket code.
287 * In the normal case udp_append() will return 0, indicating that you
288 * must unlock the inp. However if a tunneling protocol is in place we increment
289 * the inpcb refcnt and unlock the inp, on return from the tunneling protocol we
290 * then decrement the reference count. If the inp_rele returns 1, indicating the
291 * inp is gone, we return that to the caller to tell them *not* to unlock
292 * the inp. In the case of multi-cast this will cause the distribution
293 * to stop (though most tunneling protocols known currently do *not* use
297 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
298 struct sockaddr_in *udp_in)
300 struct sockaddr *append_sa;
302 struct mbuf *tmpopts, *opts = NULL;
304 struct sockaddr_in6 udp_in6;
308 INP_LOCK_ASSERT(inp);
311 * Engage the tunneling protocol.
314 if (up->u_tun_func != NULL) {
317 (*up->u_tun_func)(n, off, inp, (struct sockaddr *)&udp_in[0],
320 return (in_pcbrele_rlocked(inp));
323 off += sizeof(struct udphdr);
325 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
326 /* Check AH/ESP integrity. */
327 if (IPSEC_ENABLED(ipv4) &&
328 IPSEC_CHECK_POLICY(ipv4, n, inp) != 0) {
332 if (up->u_flags & UF_ESPINUDP) {/* IPSec UDP encaps. */
333 if (IPSEC_ENABLED(ipv4) &&
334 UDPENCAP_INPUT(n, off, AF_INET) != 0)
335 return (0); /* Consumed. */
339 if (mac_inpcb_check_deliver(inp, n) != 0) {
344 if (inp->inp_flags & INP_CONTROLOPTS ||
345 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
347 if (inp->inp_vflag & INP_IPV6)
348 (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
351 ip_savecontrol(inp, &opts, ip, n);
353 if ((inp->inp_vflag & INP_IPV4) && (inp->inp_flags2 & INP_ORIGDSTADDR)) {
354 tmpopts = sbcreatecontrol((caddr_t)&udp_in[1],
355 sizeof(struct sockaddr_in), IP_ORIGDSTADDR, IPPROTO_IP);
358 tmpopts->m_next = opts;
365 if (inp->inp_vflag & INP_IPV6) {
366 bzero(&udp_in6, sizeof(udp_in6));
367 udp_in6.sin6_len = sizeof(udp_in6);
368 udp_in6.sin6_family = AF_INET6;
369 in6_sin_2_v4mapsin6(&udp_in[0], &udp_in6);
370 append_sa = (struct sockaddr *)&udp_in6;
373 append_sa = (struct sockaddr *)&udp_in[0];
376 so = inp->inp_socket;
377 SOCKBUF_LOCK(&so->so_rcv);
378 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
379 SOCKBUF_UNLOCK(&so->so_rcv);
383 UDPSTAT_INC(udps_fullsock);
385 sorwakeup_locked(so);
390 udp_input(struct mbuf **mp, int *offp, int proto)
396 uint16_t len, ip_len;
397 struct inpcbinfo *pcbinfo;
399 struct sockaddr_in udp_in[2];
401 struct m_tag *fwd_tag;
402 struct epoch_tracker et;
403 int cscov_partial, iphlen;
407 ifp = m->m_pkthdr.rcvif;
409 UDPSTAT_INC(udps_ipackets);
412 * Strip IP options, if any; should skip this, make available to
413 * user, and use on returned packets, but we don't yet have a way to
414 * check the checksum with options still present.
416 if (iphlen > sizeof (struct ip)) {
418 iphlen = sizeof(struct ip);
422 * Get IP and UDP header together in first mbuf.
424 if (m->m_len < iphlen + sizeof(struct udphdr)) {
425 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == NULL) {
426 UDPSTAT_INC(udps_hdrops);
427 return (IPPROTO_DONE);
430 ip = mtod(m, struct ip *);
431 uh = (struct udphdr *)((caddr_t)ip + iphlen);
432 cscov_partial = (proto == IPPROTO_UDPLITE) ? 1 : 0;
435 * Destination port of 0 is illegal, based on RFC768.
437 if (uh->uh_dport == 0)
441 * Construct sockaddr format source address. Stuff source address
442 * and datagram in user buffer.
444 bzero(&udp_in[0], sizeof(struct sockaddr_in) * 2);
445 udp_in[0].sin_len = sizeof(struct sockaddr_in);
446 udp_in[0].sin_family = AF_INET;
447 udp_in[0].sin_port = uh->uh_sport;
448 udp_in[0].sin_addr = ip->ip_src;
449 udp_in[1].sin_len = sizeof(struct sockaddr_in);
450 udp_in[1].sin_family = AF_INET;
451 udp_in[1].sin_port = uh->uh_dport;
452 udp_in[1].sin_addr = ip->ip_dst;
455 * Make mbuf data length reflect UDP length. If not enough data to
456 * reflect UDP length, drop.
458 len = ntohs((u_short)uh->uh_ulen);
459 ip_len = ntohs(ip->ip_len) - iphlen;
460 if (proto == IPPROTO_UDPLITE && (len == 0 || len == ip_len)) {
461 /* Zero means checksum over the complete packet. */
467 if (len > ip_len || len < sizeof(struct udphdr)) {
468 UDPSTAT_INC(udps_badlen);
471 if (proto == IPPROTO_UDP)
472 m_adj(m, len - ip_len);
476 * Save a copy of the IP header in case we want restore it for
477 * sending an ICMP error message in response.
479 if (!V_udp_blackhole)
482 memset(&save_ip, 0, sizeof(save_ip));
485 * Checksum extended UDP header and data.
490 if ((m->m_pkthdr.csum_flags & CSUM_DATA_VALID) &&
492 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
493 uh_sum = m->m_pkthdr.csum_data;
495 uh_sum = in_pseudo(ip->ip_src.s_addr,
496 ip->ip_dst.s_addr, htonl((u_short)len +
497 m->m_pkthdr.csum_data + proto));
502 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
503 bzero(((struct ipovly *)ip)->ih_x1, 9);
504 ((struct ipovly *)ip)->ih_len = (proto == IPPROTO_UDP) ?
505 uh->uh_ulen : htons(ip_len);
506 uh_sum = in_cksum(m, len + sizeof (struct ip));
507 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
510 UDPSTAT_INC(udps_badsum);
512 return (IPPROTO_DONE);
515 if (proto == IPPROTO_UDP) {
516 UDPSTAT_INC(udps_nosum);
518 /* UDPLite requires a checksum */
519 /* XXX: What is the right UDPLite MIB counter here? */
521 return (IPPROTO_DONE);
525 pcbinfo = udp_get_inpcbinfo(proto);
526 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
527 in_broadcast(ip->ip_dst, ifp)) {
529 struct inpcbhead *pcblist;
531 INP_INFO_RLOCK_ET(pcbinfo, et);
532 pcblist = udp_get_pcblist(proto);
534 CK_LIST_FOREACH(inp, pcblist, inp_list) {
535 if (inp->inp_lport != uh->uh_dport)
538 if ((inp->inp_vflag & INP_IPV4) == 0)
541 if (inp->inp_laddr.s_addr != INADDR_ANY &&
542 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
544 if (inp->inp_faddr.s_addr != INADDR_ANY &&
545 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
547 if (inp->inp_fport != 0 &&
548 inp->inp_fport != uh->uh_sport)
553 if (__predict_false(inp->inp_flags2 & INP_FREED)) {
559 * XXXRW: Because we weren't holding either the inpcb
560 * or the hash lock when we checked for a match
561 * before, we should probably recheck now that the
562 * inpcb lock is held.
566 * Handle socket delivery policy for any-source
567 * and source-specific multicast. [RFC3678]
569 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
570 struct ip_moptions *imo;
571 struct sockaddr_in group;
574 imo = inp->inp_moptions;
579 bzero(&group, sizeof(struct sockaddr_in));
580 group.sin_len = sizeof(struct sockaddr_in);
581 group.sin_family = AF_INET;
582 group.sin_addr = ip->ip_dst;
584 blocked = imo_multi_filter(imo, ifp,
585 (struct sockaddr *)&group,
586 (struct sockaddr *)&udp_in[0]);
587 if (blocked != MCAST_PASS) {
588 if (blocked == MCAST_NOTGMEMBER)
589 IPSTAT_INC(ips_notmember);
590 if (blocked == MCAST_NOTSMEMBER ||
591 blocked == MCAST_MUTED)
592 UDPSTAT_INC(udps_filtermcast);
600 if ((n = m_copym(m, 0, M_COPYALL, M_NOWAIT)) !=
602 if (proto == IPPROTO_UDPLITE)
603 UDPLITE_PROBE(receive, NULL, last, ip,
606 UDP_PROBE(receive, NULL, last, ip, last,
608 if (udp_append(last, ip, n, iphlen,
617 * Don't look for additional matches if this one does
618 * not have either the SO_REUSEPORT or SO_REUSEADDR
619 * socket options set. This heuristic avoids
620 * searching through all pcbs in the common case of a
621 * non-shared port. It assumes that an application
622 * will never clear these options after setting them.
624 if ((last->inp_socket->so_options &
625 (SO_REUSEPORT|SO_REUSEPORT_LB|SO_REUSEADDR)) == 0)
631 * No matching pcb found; discard datagram. (No need
632 * to send an ICMP Port Unreachable for a broadcast
633 * or multicast datgram.)
635 UDPSTAT_INC(udps_noportbcast);
638 INP_INFO_RUNLOCK_ET(pcbinfo, et);
641 if (proto == IPPROTO_UDPLITE)
642 UDPLITE_PROBE(receive, NULL, last, ip, last, uh);
644 UDP_PROBE(receive, NULL, last, ip, last, uh);
645 if (udp_append(last, ip, m, iphlen, udp_in) == 0)
648 INP_INFO_RUNLOCK_ET(pcbinfo, et);
649 return (IPPROTO_DONE);
653 * Locate pcb for datagram.
657 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
659 if ((m->m_flags & M_IP_NEXTHOP) &&
660 (fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL)) != NULL) {
661 struct sockaddr_in *next_hop;
663 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
666 * Transparently forwarded. Pretend to be the destination.
667 * Already got one like this?
669 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
670 ip->ip_dst, uh->uh_dport, INPLOOKUP_RLOCKPCB, ifp, m);
673 * It's new. Try to find the ambushing socket.
674 * Because we've rewritten the destination address,
675 * any hardware-generated hash is ignored.
677 inp = in_pcblookup(pcbinfo, ip->ip_src,
678 uh->uh_sport, next_hop->sin_addr,
679 next_hop->sin_port ? htons(next_hop->sin_port) :
680 uh->uh_dport, INPLOOKUP_WILDCARD |
681 INPLOOKUP_RLOCKPCB, ifp);
683 /* Remove the tag from the packet. We don't need it anymore. */
684 m_tag_delete(m, fwd_tag);
685 m->m_flags &= ~M_IP_NEXTHOP;
687 inp = in_pcblookup_mbuf(pcbinfo, ip->ip_src, uh->uh_sport,
688 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD |
689 INPLOOKUP_RLOCKPCB, ifp, m);
691 if (V_udp_log_in_vain) {
692 char src[INET_ADDRSTRLEN];
693 char dst[INET_ADDRSTRLEN];
696 "Connection attempt to UDP %s:%d from %s:%d\n",
697 inet_ntoa_r(ip->ip_dst, dst), ntohs(uh->uh_dport),
698 inet_ntoa_r(ip->ip_src, src), ntohs(uh->uh_sport));
700 if (proto == IPPROTO_UDPLITE)
701 UDPLITE_PROBE(receive, NULL, NULL, ip, NULL, uh);
703 UDP_PROBE(receive, NULL, NULL, ip, NULL, uh);
704 UDPSTAT_INC(udps_noport);
705 if (m->m_flags & (M_BCAST | M_MCAST)) {
706 UDPSTAT_INC(udps_noportbcast);
711 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
714 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
715 return (IPPROTO_DONE);
719 * Check the minimum TTL for socket.
721 INP_RLOCK_ASSERT(inp);
722 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
723 if (proto == IPPROTO_UDPLITE)
724 UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
726 UDP_PROBE(receive, NULL, inp, ip, inp, uh);
729 return (IPPROTO_DONE);
735 if (up->u_rxcslen == 0 || up->u_rxcslen > len) {
738 return (IPPROTO_DONE);
742 if (proto == IPPROTO_UDPLITE)
743 UDPLITE_PROBE(receive, NULL, inp, ip, inp, uh);
745 UDP_PROBE(receive, NULL, inp, ip, inp, uh);
746 if (udp_append(inp, ip, m, iphlen, udp_in) == 0)
748 return (IPPROTO_DONE);
752 return (IPPROTO_DONE);
757 * Notify a udp user of an asynchronous error; just wake up so that they can
758 * collect error status.
761 udp_notify(struct inpcb *inp, int errno)
764 INP_WLOCK_ASSERT(inp);
765 if ((errno == EHOSTUNREACH || errno == ENETUNREACH ||
766 errno == EHOSTDOWN) && inp->inp_route.ro_rt) {
767 RTFREE(inp->inp_route.ro_rt);
768 inp->inp_route.ro_rt = (struct rtentry *)NULL;
771 inp->inp_socket->so_error = errno;
772 sorwakeup(inp->inp_socket);
773 sowwakeup(inp->inp_socket);
779 udp_common_ctlinput(int cmd, struct sockaddr *sa, void *vip,
780 struct inpcbinfo *pcbinfo)
784 struct in_addr faddr;
787 faddr = ((struct sockaddr_in *)sa)->sin_addr;
788 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
791 if (PRC_IS_REDIRECT(cmd)) {
792 /* signal EHOSTDOWN, as it flushes the cached route */
793 in_pcbnotifyall(&V_udbinfo, faddr, EHOSTDOWN, udp_notify);
798 * Hostdead is ugly because it goes linearly through all PCBs.
800 * XXX: We never get this from ICMP, otherwise it makes an excellent
801 * DoS attack on machines with many connections.
803 if (cmd == PRC_HOSTDEAD)
805 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
808 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
809 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
810 ip->ip_src, uh->uh_sport, INPLOOKUP_WLOCKPCB, NULL);
812 INP_WLOCK_ASSERT(inp);
813 if (inp->inp_socket != NULL) {
814 udp_notify(inp, inetctlerrmap[cmd]);
818 inp = in_pcblookup(pcbinfo, faddr, uh->uh_dport,
819 ip->ip_src, uh->uh_sport,
820 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
828 func = up->u_icmp_func;
831 (*func)(cmd, sa, vip, ctx);
835 in_pcbnotifyall(pcbinfo, faddr, inetctlerrmap[cmd],
839 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
842 return (udp_common_ctlinput(cmd, sa, vip, &V_udbinfo));
846 udplite_ctlinput(int cmd, struct sockaddr *sa, void *vip)
849 return (udp_common_ctlinput(cmd, sa, vip, &V_ulitecbinfo));
854 udp_pcblist(SYSCTL_HANDLER_ARGS)
857 struct inpcb *inp, **inp_list;
860 struct epoch_tracker et;
863 * The process of preparing the PCB list is too time-consuming and
864 * resource-intensive to repeat twice on every request.
866 if (req->oldptr == 0) {
867 n = V_udbinfo.ipi_count;
868 n += imax(n / 8, 10);
869 req->oldidx = 2 * (sizeof xig) + n * sizeof(struct xinpcb);
873 if (req->newptr != 0)
877 * OK, now we're committed to doing something.
879 INP_INFO_RLOCK_ET(&V_udbinfo, et);
880 gencnt = V_udbinfo.ipi_gencnt;
881 n = V_udbinfo.ipi_count;
882 INP_INFO_RUNLOCK_ET(&V_udbinfo, et);
884 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
885 + n * sizeof(struct xinpcb));
889 bzero(&xig, sizeof(xig));
890 xig.xig_len = sizeof xig;
892 xig.xig_gen = gencnt;
893 xig.xig_sogen = so_gencnt;
894 error = SYSCTL_OUT(req, &xig, sizeof xig);
898 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
899 if (inp_list == NULL)
902 INP_INFO_RLOCK_ET(&V_udbinfo, et);
903 for (inp = CK_LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
904 inp = CK_LIST_NEXT(inp, inp_list)) {
906 if (inp->inp_gencnt <= gencnt &&
907 cr_canseeinpcb(req->td->td_ucred, inp) == 0) {
913 INP_INFO_RUNLOCK_ET(&V_udbinfo, et);
917 for (i = 0; i < n; i++) {
920 if (inp->inp_gencnt <= gencnt) {
923 in_pcbtoxinpcb(inp, &xi);
925 error = SYSCTL_OUT(req, &xi, sizeof xi);
929 INP_INFO_WLOCK(&V_udbinfo);
930 for (i = 0; i < n; i++) {
933 if (!in_pcbrele_rlocked(inp))
936 INP_INFO_WUNLOCK(&V_udbinfo);
940 * Give the user an updated idea of our state. If the
941 * generation differs from what we told her before, she knows
942 * that something happened while we were processing this
943 * request, and it might be necessary to retry.
945 INP_INFO_RLOCK_ET(&V_udbinfo, et);
946 xig.xig_gen = V_udbinfo.ipi_gencnt;
947 xig.xig_sogen = so_gencnt;
948 xig.xig_count = V_udbinfo.ipi_count;
949 INP_INFO_RUNLOCK_ET(&V_udbinfo, et);
950 error = SYSCTL_OUT(req, &xig, sizeof xig);
952 free(inp_list, M_TEMP);
956 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist,
957 CTLTYPE_OPAQUE | CTLFLAG_RD, NULL, 0,
958 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
962 udp_getcred(SYSCTL_HANDLER_ARGS)
965 struct sockaddr_in addrs[2];
969 error = priv_check(req->td, PRIV_NETINET_GETCRED);
972 error = SYSCTL_IN(req, addrs, sizeof(addrs));
975 inp = in_pcblookup(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
976 addrs[0].sin_addr, addrs[0].sin_port,
977 INPLOOKUP_WILDCARD | INPLOOKUP_RLOCKPCB, NULL);
979 INP_RLOCK_ASSERT(inp);
980 if (inp->inp_socket == NULL)
983 error = cr_canseeinpcb(req->td->td_ucred, inp);
985 cru2x(inp->inp_cred, &xuc);
990 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
994 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
995 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
996 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
1000 udp_ctloutput(struct socket *so, struct sockopt *sopt)
1004 int isudplite, error, optval;
1007 isudplite = (so->so_proto->pr_protocol == IPPROTO_UDPLITE) ? 1 : 0;
1008 inp = sotoinpcb(so);
1009 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1011 if (sopt->sopt_level != so->so_proto->pr_protocol) {
1013 if (INP_CHECK_SOCKAF(so, AF_INET6)) {
1015 error = ip6_ctloutput(so, sopt);
1018 #if defined(INET) && defined(INET6)
1024 error = ip_ctloutput(so, sopt);
1030 switch (sopt->sopt_dir) {
1032 switch (sopt->sopt_name) {
1033 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1036 if (!IPSEC_ENABLED(ipv4)) {
1038 return (ENOPROTOOPT);
1040 error = UDPENCAP_PCBCTL(inp, sopt);
1044 case UDPLITE_SEND_CSCOV:
1045 case UDPLITE_RECV_CSCOV:
1048 error = ENOPROTOOPT;
1052 error = sooptcopyin(sopt, &optval, sizeof(optval),
1056 inp = sotoinpcb(so);
1057 KASSERT(inp != NULL, ("%s: inp == NULL", __func__));
1059 up = intoudpcb(inp);
1060 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1061 if ((optval != 0 && optval < 8) || (optval > 65535)) {
1066 if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1067 up->u_txcslen = optval;
1069 up->u_rxcslen = optval;
1074 error = ENOPROTOOPT;
1079 switch (sopt->sopt_name) {
1080 #if defined(IPSEC) || defined(IPSEC_SUPPORT)
1083 if (!IPSEC_ENABLED(ipv4)) {
1085 return (ENOPROTOOPT);
1087 error = UDPENCAP_PCBCTL(inp, sopt);
1091 case UDPLITE_SEND_CSCOV:
1092 case UDPLITE_RECV_CSCOV:
1095 error = ENOPROTOOPT;
1098 up = intoudpcb(inp);
1099 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1100 if (sopt->sopt_name == UDPLITE_SEND_CSCOV)
1101 optval = up->u_txcslen;
1103 optval = up->u_rxcslen;
1105 error = sooptcopyout(sopt, &optval, sizeof(optval));
1109 error = ENOPROTOOPT;
1118 #define UH_WLOCKED 2
1119 #define UH_RLOCKED 1
1120 #define UH_UNLOCKED 0
1122 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
1123 struct mbuf *control, struct thread *td)
1125 struct udpiphdr *ui;
1126 int len = m->m_pkthdr.len;
1127 struct in_addr faddr, laddr;
1129 struct inpcbinfo *pcbinfo;
1130 struct sockaddr_in *sin, src;
1131 struct epoch_tracker et;
1132 int cscov_partial = 0;
1135 u_short fport, lport;
1136 int unlock_udbinfo, unlock_inp;
1140 uint32_t flowid = 0;
1141 uint8_t flowtype = M_HASHTYPE_NONE;
1144 * udp_output() may need to temporarily bind or connect the current
1145 * inpcb. As such, we don't know up front whether we will need the
1146 * pcbinfo lock or not. Do any work to decide what is needed up
1147 * front before acquiring any locks.
1149 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1157 sin = (struct sockaddr_in *)addr;
1160 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1163 * In case we lost a race and another thread bound addr/port
1164 * on the inp we cannot keep the wlock (which still would be
1165 * fine) as further down, based on these values we make
1166 * decisions for the pcbinfo lock. If the locks are not in
1167 * synch the assertions on unlock will fire, hence we go for
1170 if (sin != NULL && (inp->inp_laddr.s_addr != INADDR_ANY ||
1171 inp->inp_lport != 0)) {
1175 unlock_inp = UH_WLOCKED;
1178 unlock_inp = UH_RLOCKED;
1180 tos = inp->inp_ip_tos;
1181 if (control != NULL) {
1183 * XXX: Currently, we assume all the optional information is
1184 * stored in a single mbuf.
1186 if (control->m_next) {
1187 if (unlock_inp == UH_WLOCKED)
1195 for (; control->m_len > 0;
1196 control->m_data += CMSG_ALIGN(cm->cmsg_len),
1197 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
1198 cm = mtod(control, struct cmsghdr *);
1199 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
1200 || cm->cmsg_len > control->m_len) {
1204 if (cm->cmsg_level != IPPROTO_IP)
1207 switch (cm->cmsg_type) {
1208 case IP_SENDSRCADDR:
1210 CMSG_LEN(sizeof(struct in_addr))) {
1214 bzero(&src, sizeof(src));
1215 src.sin_family = AF_INET;
1216 src.sin_len = sizeof(src);
1217 src.sin_port = inp->inp_lport;
1219 *(struct in_addr *)CMSG_DATA(cm);
1223 if (cm->cmsg_len != CMSG_LEN(sizeof(u_char))) {
1227 tos = *(u_char *)CMSG_DATA(cm);
1231 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1235 flowid = *(uint32_t *) CMSG_DATA(cm);
1239 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1243 flowtype = *(uint32_t *) CMSG_DATA(cm);
1247 case IP_RSSBUCKETID:
1248 if (cm->cmsg_len != CMSG_LEN(sizeof(uint32_t))) {
1252 /* This is just a placeholder for now */
1256 error = ENOPROTOOPT;
1265 if (unlock_inp == UH_WLOCKED)
1274 * In the old days, depending on whether or not the application had
1275 * bound or connected the socket, we had to do varying levels of work.
1276 * The optimal case was for a connected UDP socket, as a global lock
1277 * wasn't required at all.
1278 * In order to decide which we need, we required stability of the
1279 * inpcb binding, which we ensured by acquiring a read lock on the
1280 * inpcb. This didn't strictly follow the lock order, so we played
1281 * the trylock and retry game.
1282 * With the re-introduction of the route-cache in some cases, we started
1283 * to acquire an early inp wlock and a possible race during re-lock
1284 * went away. With the introduction of epoch(9) some read locking
1285 * became epoch(9) and the lock-order issues also went away.
1286 * Due to route-cache we may now hold more conservative locks than
1287 * otherwise required and have split up the 2nd case in case 2 and 3
1288 * in order to keep the udpinfo lock level in sync with the inp one
1289 * for the IP_SENDSRCADDR case below.
1291 pr = inp->inp_socket->so_proto->pr_protocol;
1292 pcbinfo = udp_get_inpcbinfo(pr);
1294 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
1295 INP_HASH_WLOCK(pcbinfo);
1296 unlock_udbinfo = UH_WLOCKED;
1297 } else if (sin != NULL &&
1298 (sin->sin_addr.s_addr == INADDR_ANY ||
1299 sin->sin_addr.s_addr == INADDR_BROADCAST ||
1300 inp->inp_laddr.s_addr == INADDR_ANY ||
1301 inp->inp_lport == 0)) {
1302 INP_HASH_RLOCK_ET(pcbinfo, et);
1303 unlock_udbinfo = UH_RLOCKED;
1304 } else if (src.sin_family == AF_INET) {
1305 if (unlock_inp == UH_WLOCKED) {
1306 INP_HASH_WLOCK(pcbinfo);
1307 unlock_udbinfo = UH_WLOCKED;
1309 INP_HASH_RLOCK_ET(pcbinfo, et);
1310 unlock_udbinfo = UH_RLOCKED;
1313 unlock_udbinfo = UH_UNLOCKED;
1316 * If the IP_SENDSRCADDR control message was specified, override the
1317 * source address for this datagram. Its use is invalidated if the
1318 * address thus specified is incomplete or clobbers other inpcbs.
1320 laddr = inp->inp_laddr;
1321 lport = inp->inp_lport;
1322 if (src.sin_family == AF_INET) {
1323 INP_HASH_LOCK_ASSERT(pcbinfo);
1325 (laddr.s_addr == INADDR_ANY &&
1326 src.sin_addr.s_addr == INADDR_ANY)) {
1330 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
1331 &laddr.s_addr, &lport, td->td_ucred);
1337 * If a UDP socket has been connected, then a local address/port will
1338 * have been selected and bound.
1340 * If a UDP socket has not been connected to, then an explicit
1341 * destination address must be used, in which case a local
1342 * address/port may not have been selected and bound.
1345 INP_LOCK_ASSERT(inp);
1346 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1352 * Jail may rewrite the destination address, so let it do
1353 * that before we use it.
1355 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1360 * If a local address or port hasn't yet been selected, or if
1361 * the destination address needs to be rewritten due to using
1362 * a special INADDR_ constant, invoke in_pcbconnect_setup()
1363 * to do the heavy lifting. Once a port is selected, we
1364 * commit the binding back to the socket; we also commit the
1365 * binding of the address if in jail.
1367 * If we already have a valid binding and we're not
1368 * requesting a destination address rewrite, use a fast path.
1370 if (inp->inp_laddr.s_addr == INADDR_ANY ||
1371 inp->inp_lport == 0 ||
1372 sin->sin_addr.s_addr == INADDR_ANY ||
1373 sin->sin_addr.s_addr == INADDR_BROADCAST) {
1374 INP_HASH_LOCK_ASSERT(pcbinfo);
1375 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
1376 &lport, &faddr.s_addr, &fport, NULL,
1382 * XXXRW: Why not commit the port if the address is
1385 /* Commit the local port if newly assigned. */
1386 if (inp->inp_laddr.s_addr == INADDR_ANY &&
1387 inp->inp_lport == 0) {
1388 INP_WLOCK_ASSERT(inp);
1389 INP_HASH_WLOCK_ASSERT(pcbinfo);
1391 * Remember addr if jailed, to prevent
1394 if (prison_flag(td->td_ucred, PR_IP4))
1395 inp->inp_laddr = laddr;
1396 inp->inp_lport = lport;
1397 if (in_pcbinshash(inp) != 0) {
1402 inp->inp_flags |= INP_ANONPORT;
1405 faddr = sin->sin_addr;
1406 fport = sin->sin_port;
1409 INP_LOCK_ASSERT(inp);
1410 faddr = inp->inp_faddr;
1411 fport = inp->inp_fport;
1412 if (faddr.s_addr == INADDR_ANY) {
1419 * Calculate data length and get a mbuf for UDP, IP, and possible
1420 * link-layer headers. Immediate slide the data pointer back forward
1421 * since we won't use that space at this layer.
1423 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_NOWAIT);
1428 m->m_data += max_linkhdr;
1429 m->m_len -= max_linkhdr;
1430 m->m_pkthdr.len -= max_linkhdr;
1433 * Fill in mbuf with extended UDP header and addresses and length put
1434 * into network format.
1436 ui = mtod(m, struct udpiphdr *);
1437 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
1438 ui->ui_v = IPVERSION << 4;
1442 ui->ui_sport = lport;
1443 ui->ui_dport = fport;
1444 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1445 if (pr == IPPROTO_UDPLITE) {
1449 up = intoudpcb(inp);
1450 cscov = up->u_txcslen;
1451 plen = (u_short)len + sizeof(struct udphdr);
1454 ui->ui_len = htons(plen);
1455 ui->ui_ulen = htons(cscov);
1457 * For UDP-Lite, checksum coverage length of zero means
1458 * the entire UDPLite packet is covered by the checksum.
1460 cscov_partial = (cscov == 0) ? 0 : 1;
1464 * Set the Don't Fragment bit in the IP header.
1466 if (inp->inp_flags & INP_DONTFRAG) {
1469 ip = (struct ip *)&ui->ui_i;
1470 ip->ip_off |= htons(IP_DF);
1474 if (inp->inp_socket->so_options & SO_DONTROUTE)
1475 ipflags |= IP_ROUTETOIF;
1476 if (inp->inp_socket->so_options & SO_BROADCAST)
1477 ipflags |= IP_ALLOWBROADCAST;
1478 if (inp->inp_flags & INP_ONESBCAST)
1479 ipflags |= IP_SENDONES;
1482 mac_inpcb_create_mbuf(inp, m);
1486 * Set up checksum and output datagram.
1489 if (pr == IPPROTO_UDPLITE) {
1490 if (inp->inp_flags & INP_ONESBCAST)
1491 faddr.s_addr = INADDR_BROADCAST;
1492 if (cscov_partial) {
1493 if ((ui->ui_sum = in_cksum(m, sizeof(struct ip) + cscov)) == 0)
1494 ui->ui_sum = 0xffff;
1496 if ((ui->ui_sum = in_cksum(m, sizeof(struct udpiphdr) + len)) == 0)
1497 ui->ui_sum = 0xffff;
1499 } else if (V_udp_cksum) {
1500 if (inp->inp_flags & INP_ONESBCAST)
1501 faddr.s_addr = INADDR_BROADCAST;
1502 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1503 htons((u_short)len + sizeof(struct udphdr) + pr));
1504 m->m_pkthdr.csum_flags = CSUM_UDP;
1505 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1507 ((struct ip *)ui)->ip_len = htons(sizeof(struct udpiphdr) + len);
1508 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1509 ((struct ip *)ui)->ip_tos = tos; /* XXX */
1510 UDPSTAT_INC(udps_opackets);
1513 * Setup flowid / RSS information for outbound socket.
1515 * Once the UDP code decides to set a flowid some other way,
1516 * this allows the flowid to be overridden by userland.
1518 if (flowtype != M_HASHTYPE_NONE) {
1519 m->m_pkthdr.flowid = flowid;
1520 M_HASHTYPE_SET(m, flowtype);
1524 uint32_t hash_val, hash_type;
1526 * Calculate an appropriate RSS hash for UDP and
1529 * The called function will take care of figuring out
1530 * whether a 2-tuple or 4-tuple hash is required based
1531 * on the currently configured scheme.
1533 * Later later on connected socket values should be
1534 * cached in the inpcb and reused, rather than constantly
1535 * re-calculating it.
1537 * UDP Lite is a different protocol number and will
1538 * likely end up being hashed as a 2-tuple until
1539 * RSS / NICs grow UDP Lite protocol awareness.
1541 if (rss_proto_software_hash_v4(faddr, laddr, fport, lport,
1542 pr, &hash_val, &hash_type) == 0) {
1543 m->m_pkthdr.flowid = hash_val;
1544 M_HASHTYPE_SET(m, hash_type);
1549 * Don't override with the inp cached flowid value.
1551 * Depending upon the kind of send being done, the inp
1552 * flowid/flowtype values may actually not be appropriate
1553 * for this particular socket send.
1555 * We should either leave the flowid at zero (which is what is
1556 * currently done) or set it to some software generated
1557 * hash value based on the packet contents.
1559 ipflags |= IP_NODEFAULTFLOWID;
1562 if (unlock_udbinfo == UH_WLOCKED)
1563 INP_HASH_WUNLOCK(pcbinfo);
1564 else if (unlock_udbinfo == UH_RLOCKED)
1565 INP_HASH_RUNLOCK_ET(pcbinfo, et);
1566 if (pr == IPPROTO_UDPLITE)
1567 UDPLITE_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1569 UDP_PROBE(send, NULL, inp, &ui->ui_i, inp, &ui->ui_u);
1570 error = ip_output(m, inp->inp_options,
1571 (unlock_inp == UH_WLOCKED ? &inp->inp_route : NULL), ipflags,
1572 inp->inp_moptions, inp);
1573 if (unlock_inp == UH_WLOCKED)
1580 if (unlock_udbinfo == UH_WLOCKED) {
1581 KASSERT(unlock_inp == UH_WLOCKED,
1582 ("%s: excl udbinfo lock %#03x, shared inp lock %#03x, "
1583 "sin %p daddr %#010x inp %p laddr %#010x lport %#06x "
1585 __func__, unlock_udbinfo, unlock_inp, sin,
1586 (sin != NULL) ? sin->sin_addr.s_addr : 0xfefefefe, inp,
1587 inp->inp_laddr.s_addr, inp->inp_lport, src.sin_family));
1588 INP_HASH_WUNLOCK(pcbinfo);
1590 } else if (unlock_udbinfo == UH_RLOCKED) {
1591 KASSERT(unlock_inp == UH_RLOCKED,
1592 ("%s: shared udbinfo lock %#03x, excl inp lock %#03x, "
1593 "sin %p daddr %#010x inp %p laddr %#010x lport %#06x "
1595 __func__, unlock_udbinfo, unlock_inp, sin,
1596 (sin != NULL) ? sin->sin_addr.s_addr : 0xfefefefe, inp,
1597 inp->inp_laddr.s_addr, inp->inp_lport, src.sin_family));
1598 INP_HASH_RUNLOCK_ET(pcbinfo, et);
1600 } else if (unlock_inp == UH_WLOCKED)
1609 udp_abort(struct socket *so)
1612 struct inpcbinfo *pcbinfo;
1614 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1615 inp = sotoinpcb(so);
1616 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1618 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1619 INP_HASH_WLOCK(pcbinfo);
1620 in_pcbdisconnect(inp);
1621 inp->inp_laddr.s_addr = INADDR_ANY;
1622 INP_HASH_WUNLOCK(pcbinfo);
1623 soisdisconnected(so);
1629 udp_attach(struct socket *so, int proto, struct thread *td)
1631 static uint32_t udp_flowid;
1633 struct inpcbinfo *pcbinfo;
1636 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1637 inp = sotoinpcb(so);
1638 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1639 error = soreserve(so, udp_sendspace, udp_recvspace);
1642 INP_INFO_WLOCK(pcbinfo);
1643 error = in_pcballoc(so, pcbinfo);
1645 INP_INFO_WUNLOCK(pcbinfo);
1649 inp = sotoinpcb(so);
1650 inp->inp_vflag |= INP_IPV4;
1651 inp->inp_ip_ttl = V_ip_defttl;
1652 inp->inp_flowid = atomic_fetchadd_int(&udp_flowid, 1);
1653 inp->inp_flowtype = M_HASHTYPE_OPAQUE;
1655 error = udp_newudpcb(inp);
1659 INP_INFO_WUNLOCK(pcbinfo);
1664 INP_INFO_WUNLOCK(pcbinfo);
1670 udp_set_kernel_tunneling(struct socket *so, udp_tun_func_t f, udp_tun_icmp_t i, void *ctx)
1675 KASSERT(so->so_type == SOCK_DGRAM,
1676 ("udp_set_kernel_tunneling: !dgram"));
1677 inp = sotoinpcb(so);
1678 KASSERT(inp != NULL, ("udp_set_kernel_tunneling: inp == NULL"));
1680 up = intoudpcb(inp);
1681 if ((up->u_tun_func != NULL) ||
1682 (up->u_icmp_func != NULL)) {
1687 up->u_icmp_func = i;
1688 up->u_tun_ctx = ctx;
1695 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1698 struct inpcbinfo *pcbinfo;
1701 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1702 inp = sotoinpcb(so);
1703 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1705 INP_HASH_WLOCK(pcbinfo);
1706 error = in_pcbbind(inp, nam, td->td_ucred);
1707 INP_HASH_WUNLOCK(pcbinfo);
1713 udp_close(struct socket *so)
1716 struct inpcbinfo *pcbinfo;
1718 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1719 inp = sotoinpcb(so);
1720 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1722 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1723 INP_HASH_WLOCK(pcbinfo);
1724 in_pcbdisconnect(inp);
1725 inp->inp_laddr.s_addr = INADDR_ANY;
1726 INP_HASH_WUNLOCK(pcbinfo);
1727 soisdisconnected(so);
1733 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1736 struct inpcbinfo *pcbinfo;
1737 struct sockaddr_in *sin;
1740 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1741 inp = sotoinpcb(so);
1742 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1744 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1748 sin = (struct sockaddr_in *)nam;
1749 error = prison_remote_ip4(td->td_ucred, &sin->sin_addr);
1754 INP_HASH_WLOCK(pcbinfo);
1755 error = in_pcbconnect(inp, nam, td->td_ucred);
1756 INP_HASH_WUNLOCK(pcbinfo);
1764 udp_detach(struct socket *so)
1767 struct inpcbinfo *pcbinfo;
1770 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1771 inp = sotoinpcb(so);
1772 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1773 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1774 ("udp_detach: not disconnected"));
1775 INP_INFO_WLOCK(pcbinfo);
1777 up = intoudpcb(inp);
1778 KASSERT(up != NULL, ("%s: up == NULL", __func__));
1779 inp->inp_ppcb = NULL;
1782 INP_INFO_WUNLOCK(pcbinfo);
1787 udp_disconnect(struct socket *so)
1790 struct inpcbinfo *pcbinfo;
1792 pcbinfo = udp_get_inpcbinfo(so->so_proto->pr_protocol);
1793 inp = sotoinpcb(so);
1794 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1796 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1800 INP_HASH_WLOCK(pcbinfo);
1801 in_pcbdisconnect(inp);
1802 inp->inp_laddr.s_addr = INADDR_ANY;
1803 INP_HASH_WUNLOCK(pcbinfo);
1805 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1812 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1813 struct mbuf *control, struct thread *td)
1817 inp = sotoinpcb(so);
1818 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1819 return (udp_output(inp, m, addr, control, td));
1824 udp_shutdown(struct socket *so)
1828 inp = sotoinpcb(so);
1829 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1837 struct pr_usrreqs udp_usrreqs = {
1838 .pru_abort = udp_abort,
1839 .pru_attach = udp_attach,
1840 .pru_bind = udp_bind,
1841 .pru_connect = udp_connect,
1842 .pru_control = in_control,
1843 .pru_detach = udp_detach,
1844 .pru_disconnect = udp_disconnect,
1845 .pru_peeraddr = in_getpeeraddr,
1846 .pru_send = udp_send,
1847 .pru_soreceive = soreceive_dgram,
1848 .pru_sosend = sosend_dgram,
1849 .pru_shutdown = udp_shutdown,
1850 .pru_sockaddr = in_getsockaddr,
1851 .pru_sosetlabel = in_pcbsosetlabel,
1852 .pru_close = udp_close,