2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36 * The Regents of the University of California. All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
63 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
66 #include "opt_ipsec.h"
67 #include "opt_inet6.h"
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/kernel.h>
72 #include <sys/malloc.h>
74 #include <sys/domain.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/sysctl.h>
81 #include <sys/syslog.h>
82 #include <sys/in_cksum.h>
85 #include <sys/thread2.h>
86 #include <sys/socketvar2.h>
87 #include <sys/serialize.h>
89 #include <machine/stdarg.h>
92 #include <net/route.h>
93 #include <net/netmsg2.h>
94 #include <net/netisr2.h>
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
100 #include <netinet/ip6.h>
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
106 #include <netinet6/ip6_var.h>
108 #include <netinet/ip_icmp.h>
109 #include <netinet/icmp_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
114 #include <netproto/ipsec/ipsec.h>
118 #include <netinet6/ipsec.h>
121 #define UDP_KTR_STRING "inp=%p"
122 #define UDP_KTR_ARGS struct inpcb *inp
125 #define KTR_UDP KTR_ALL
128 KTR_INFO_MASTER(udp);
129 KTR_INFO(KTR_UDP, udp, send_beg, 0, UDP_KTR_STRING, UDP_KTR_ARGS);
130 KTR_INFO(KTR_UDP, udp, send_end, 1, UDP_KTR_STRING, UDP_KTR_ARGS);
131 KTR_INFO(KTR_UDP, udp, send_ipout, 2, UDP_KTR_STRING, UDP_KTR_ARGS);
132 KTR_INFO(KTR_UDP, udp, redisp_ipout_beg, 3, UDP_KTR_STRING, UDP_KTR_ARGS);
133 KTR_INFO(KTR_UDP, udp, redisp_ipout_end, 4, UDP_KTR_STRING, UDP_KTR_ARGS);
134 KTR_INFO(KTR_UDP, udp, send_redisp, 5, UDP_KTR_STRING, UDP_KTR_ARGS);
136 #define logudp(name, inp) KTR_LOG(udp_##name, inp)
139 * UDP protocol implementation.
140 * Per RFC 768, August, 1980.
143 static int udpcksum = 1;
145 static int udpcksum = 0; /* XXX */
147 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
148 &udpcksum, 0, "Enable checksumming of UDP packets");
151 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
152 &log_in_vain, 0, "Log all incoming UDP packets");
154 static int blackhole = 0;
155 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
156 &blackhole, 0, "Do not send port unreachables for refused connects");
158 static int strict_mcast_mship = 1;
159 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
160 &strict_mcast_mship, 0, "Only send multicast to member sockets");
162 int udp_sosend_async = 1;
163 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
164 &udp_sosend_async, 0, "UDP asynchronized pru_send");
166 int udp_sosend_prepend = 1;
167 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
168 &udp_sosend_prepend, 0,
169 "Prepend enough space for proto and link header in pru_send");
171 static int udp_reuseport_ext = 1;
172 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
173 &udp_reuseport_ext, 0, "SO_REUSEPORT extension");
175 struct inpcbinfo udbinfo;
176 struct inpcbportinfo udbportinfo;
178 static struct netisr_barrier *udbinfo_br;
179 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
182 #define UDBHASHSIZE 16
185 struct udpstat udpstat_percpu[MAXCPU] __cachealign;
189 struct sockaddr_in6 uin6_sin;
190 u_char uin6_init_done : 1;
193 struct ip6_hdr uip6_ip6;
194 u_char uip6_init_done : 1;
201 static void udp_append (struct inpcb *last, struct ip *ip,
202 struct mbuf *n, int off, struct sockaddr_in *udp_in,
203 struct udp_in6 *, struct udp_ip6 *);
205 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
208 static int udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
209 struct sockaddr_in *if_sin);
216 in_pcbinfo_init(&udbinfo);
217 in_pcbportinfo_init(&udbportinfo, UDBHASHSIZE, FALSE, 0);
219 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
220 udbinfo.portinfo = &udbportinfo;
221 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
222 &udbinfo.wildcardhashmask);
223 udbinfo.localgrphashbase = hashinit(UDBHASHSIZE, M_PCB,
224 &udbinfo.localgrphashmask);
225 udbinfo.ipi_size = sizeof(struct inpcb);
227 udbinfo_br = netisr_barrier_create();
230 * Initialize UDP statistics counters for each CPU.
232 for (cpu = 0; cpu < ncpus; ++cpu)
233 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat));
237 sysctl_udpstat(SYSCTL_HANDLER_ARGS)
241 for (cpu = 0; cpu < ncpus; ++cpu) {
242 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu],
243 sizeof(struct udpstat))))
245 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu],
246 sizeof(struct udpstat))))
252 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
253 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics");
256 * Check multicast packets to make sure they are only sent to sockets with
257 * multicast memberships for the packet's destination address and arrival
258 * interface. Multicast packets to multicast-unaware sockets are also
261 * Returns 0 if the packet is acceptable, -1 if it is not.
264 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
267 struct ip_moptions *mopt;
269 if (strict_mcast_mship == 0 ||
270 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
273 mopt = inp->inp_moptions;
276 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
277 struct in_multi *maddr = mopt->imo_membership[mshipno];
279 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
280 m->m_pkthdr.rcvif == maddr->inm_ifp) {
288 udp_input(struct mbuf **mp, int *offp, int proto)
290 struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
292 struct udp_in6 udp_in6 = {
293 { sizeof udp_in6.uin6_sin, AF_INET6 }, 0
295 struct udp_ip6 udp_ip6;
303 struct mbuf *opts = NULL;
306 struct sockaddr *append_sa;
313 udp_stat.udps_ipackets++;
316 * Strip IP options, if any; should skip this,
317 * make available to user, and use on returned packets,
318 * but we don't yet have a way to check the checksum
319 * with options still present.
321 if (iphlen > sizeof(struct ip)) {
323 iphlen = sizeof(struct ip);
327 * IP and UDP headers are together in first mbuf.
328 * Already checked and pulled up in ip_demux().
330 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
331 ("UDP header not in one mbuf"));
333 ip = mtod(m, struct ip *);
334 uh = (struct udphdr *)((caddr_t)ip + iphlen);
336 /* destination port of 0 is illegal, based on RFC768. */
337 if (uh->uh_dport == 0)
341 * Make mbuf data length reflect UDP length.
342 * If not enough data to reflect UDP length, drop.
344 len = ntohs((u_short)uh->uh_ulen);
345 if (ip->ip_len != len) {
346 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
347 udp_stat.udps_badlen++;
350 m_adj(m, len - ip->ip_len);
351 /* ip->ip_len = len; */
354 * Save a copy of the IP header in case we want restore it
355 * for sending an ICMP error message in response.
360 * Checksum extended UDP header and data.
363 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
364 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
365 uh->uh_sum = m->m_pkthdr.csum_data;
367 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
368 ip->ip_dst.s_addr, htonl((u_short)len +
369 m->m_pkthdr.csum_data + IPPROTO_UDP));
370 uh->uh_sum ^= 0xffff;
374 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
375 bzero(((struct ipovly *)ip)->ih_x1, 9);
376 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
377 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
378 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
381 udp_stat.udps_badsum++;
383 return(IPPROTO_DONE);
386 udp_stat.udps_nosum++;
388 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
389 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
393 * Deliver a multicast or broadcast datagram to *all* sockets
394 * for which the local and remote addresses and ports match
395 * those of the incoming datagram. This allows more than
396 * one process to receive multi/broadcasts on the same port.
397 * (This really ought to be done for unicast datagrams as
398 * well, but that would cause problems with existing
399 * applications that open both address-specific sockets and
400 * a wildcard socket listening to the same port -- they would
401 * end up receiving duplicates of every unicast datagram.
402 * Those applications open the multiple sockets to overcome an
403 * inadequacy of the UDP socket interface, but for backwards
404 * compatibility we avoid the problem here rather than
405 * fixing the interface. Maybe 4.5BSD will remedy this?)
409 * Construct sockaddr format source address.
411 udp_in.sin_port = uh->uh_sport;
412 udp_in.sin_addr = ip->ip_src;
414 * Locate pcb(s) for datagram.
415 * (Algorithm copied from raw_intr().)
419 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
421 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
422 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
424 if (!(inp->inp_vflag & INP_IPV4))
427 if (inp->inp_lport != uh->uh_dport)
429 if (inp->inp_laddr.s_addr != INADDR_ANY) {
430 if (inp->inp_laddr.s_addr !=
434 if (inp->inp_faddr.s_addr != INADDR_ANY) {
435 if (inp->inp_faddr.s_addr !=
437 inp->inp_fport != uh->uh_sport)
441 if (check_multicast_membership(ip, inp, m) < 0)
448 /* check AH/ESP integrity. */
449 if (ipsec4_in_reject_so(m, last->inp_socket))
450 ipsecstat.in_polvio++;
451 /* do not inject data to pcb */
455 /* check AH/ESP integrity. */
456 if (ipsec4_in_reject(m, last))
459 #endif /*FAST_IPSEC*/
460 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
461 udp_append(last, ip, n,
462 iphlen + sizeof(struct udphdr),
473 * Don't look for additional matches if this one does
474 * not have either the SO_REUSEPORT or SO_REUSEADDR
475 * socket options set. This heuristic avoids searching
476 * through all pcbs in the common case of a non-shared
477 * port. It * assumes that an application will never
478 * clear these options after setting them.
480 if (!(last->inp_socket->so_options &
481 (SO_REUSEPORT | SO_REUSEADDR)))
487 * No matching pcb found; discard datagram.
488 * (No need to send an ICMP Port Unreachable
489 * for a broadcast or multicast datgram.)
491 udp_stat.udps_noportbcast++;
495 /* check AH/ESP integrity. */
496 if (ipsec4_in_reject_so(m, last->inp_socket)) {
497 ipsecstat.in_polvio++;
502 /* check AH/ESP integrity. */
503 if (ipsec4_in_reject(m, last))
505 #endif /*FAST_IPSEC*/
506 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
514 return(IPPROTO_DONE);
517 * Locate pcb for datagram.
519 inp = in_pcblookup_pkthash(&udbinfo, ip->ip_src, uh->uh_sport,
520 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif,
521 udp_reuseport_ext ? m : NULL);
524 char buf[sizeof "aaa.bbb.ccc.ddd"];
526 strcpy(buf, inet_ntoa(ip->ip_dst));
528 "Connection attempt to UDP %s:%d from %s:%d\n",
529 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
530 ntohs(uh->uh_sport));
532 udp_stat.udps_noport++;
533 if (m->m_flags & (M_BCAST | M_MCAST)) {
534 udp_stat.udps_noportbcast++;
540 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
544 ip->ip_len += iphlen;
545 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
546 return(IPPROTO_DONE);
549 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
550 ipsecstat.in_polvio++;
555 if (ipsec4_in_reject(m, inp))
557 #endif /*FAST_IPSEC*/
559 * Check the minimum TTL for socket.
561 if (ip->ip_ttl < inp->inp_ip_minttl)
565 * Construct sockaddr format source address.
566 * Stuff source address and datagram in user buffer.
568 udp_in.sin_port = uh->uh_sport;
569 udp_in.sin_addr = ip->ip_src;
570 if ((inp->inp_flags & INP_CONTROLOPTS) ||
571 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
573 if (inp->inp_vflag & INP_IPV6) {
576 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
577 savedflags = inp->inp_flags;
578 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
579 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
580 inp->inp_flags = savedflags;
583 ip_savecontrol(inp, &opts, ip, m);
585 m_adj(m, iphlen + sizeof(struct udphdr));
587 if (inp->inp_vflag & INP_IPV6) {
588 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
589 append_sa = (struct sockaddr *)&udp_in6;
592 append_sa = (struct sockaddr *)&udp_in;
594 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
595 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
596 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
597 udp_stat.udps_fullsock++;
600 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
601 sorwakeup(inp->inp_socket);
602 return(IPPROTO_DONE);
607 return(IPPROTO_DONE);
612 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
614 bzero(ip6, sizeof *ip6);
616 ip6->ip6_vfc = IPV6_VERSION;
617 ip6->ip6_plen = ip->ip_len;
618 ip6->ip6_nxt = ip->ip_p;
619 ip6->ip6_hlim = ip->ip_ttl;
620 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
622 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
623 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
628 * subroutine of udp_input(), mainly for source code readability.
629 * caller must properly init udp_ip6 and udp_in6 beforehand.
632 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
633 struct sockaddr_in *udp_in,
634 struct udp_in6 *udp_in6, struct udp_ip6 *udp_ip6)
636 struct sockaddr *append_sa;
637 struct mbuf *opts = NULL;
640 if (last->inp_flags & INP_CONTROLOPTS ||
641 last->inp_socket->so_options & SO_TIMESTAMP) {
643 if (last->inp_vflag & INP_IPV6) {
646 if (udp_ip6->uip6_init_done == 0) {
647 ip_2_ip6_hdr(&udp_ip6->uip6_ip6, ip);
648 udp_ip6->uip6_init_done = 1;
650 savedflags = last->inp_flags;
651 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
652 ip6_savecontrol(last, &opts, &udp_ip6->uip6_ip6, n);
653 last->inp_flags = savedflags;
656 ip_savecontrol(last, &opts, ip, n);
659 if (last->inp_vflag & INP_IPV6) {
660 if (udp_in6->uin6_init_done == 0) {
661 in6_sin_2_v4mapsin6(udp_in, &udp_in6->uin6_sin);
662 udp_in6->uin6_init_done = 1;
664 append_sa = (struct sockaddr *)&udp_in6->uin6_sin;
667 append_sa = (struct sockaddr *)udp_in;
670 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
671 ret = ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts);
672 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
677 udp_stat.udps_fullsock++;
679 sorwakeup(last->inp_socket);
684 * Notify a udp user of an asynchronous error;
685 * just wake up so that he can collect error status.
688 udp_notify(struct inpcb *inp, int error)
690 inp->inp_socket->so_error = error;
691 sorwakeup(inp->inp_socket);
692 sowwakeup(inp->inp_socket);
695 struct netmsg_udp_notify {
696 struct netmsg_base base;
697 void (*nm_notify)(struct inpcb *, int);
698 struct in_addr nm_faddr;
703 udp_notifyall_oncpu(netmsg_t msg)
705 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
710 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
711 nm->nm_arg, nm->nm_notify);
712 lwkt_replymsg(&nm->base.lmsg, 0);
715 /* XXX currently udp only runs on cpu 0 */
716 nextcpu = mycpuid + 1;
717 if (nextcpu < ncpus2)
718 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
720 lwkt_replymsg(&nmsg->base.lmsg, 0);
725 udp_ctlinput(netmsg_t msg)
727 struct sockaddr *sa = msg->ctlinput.nm_arg;
728 struct ip *ip = msg->ctlinput.nm_extra;
729 int cmd = msg->ctlinput.nm_cmd;
731 void (*notify) (struct inpcb *, int) = udp_notify;
732 struct in_addr faddr;
735 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
737 faddr = ((struct sockaddr_in *)sa)->sin_addr;
738 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
741 if (PRC_IS_REDIRECT(cmd)) {
743 notify = in_rtchange;
744 } else if (cmd == PRC_HOSTDEAD) {
746 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
751 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
752 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
753 ip->ip_src, uh->uh_sport, 0, NULL);
754 if (inp != NULL && inp->inp_socket != NULL)
755 (*notify)(inp, inetctlerrmap[cmd]);
757 struct netmsg_udp_notify *nm;
759 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
760 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
761 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
762 0, udp_notifyall_oncpu);
763 nm->nm_faddr = faddr;
764 nm->nm_arg = inetctlerrmap[cmd];
765 nm->nm_notify = notify;
766 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg);
769 lwkt_replymsg(&msg->lmsg, 0);
773 udp_pcblist(SYSCTL_HANDLER_ARGS)
779 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
783 KKASSERT(xi == NULL);
787 KKASSERT(xi == NULL);
791 for (i = 0; i < nxi; ++i) {
792 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
800 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
801 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
804 udp_getcred(SYSCTL_HANDLER_ARGS)
806 struct sockaddr_in addrs[2];
807 struct ucred cred0, *cred = NULL;
811 error = priv_check(req->td, PRIV_ROOT);
814 error = SYSCTL_IN(req, addrs, sizeof addrs);
819 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
820 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
821 if (inp == NULL || inp->inp_socket == NULL) {
824 if (inp->inp_socket->so_cred != NULL) {
825 cred0 = *(inp->inp_socket->so_cred);
834 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
837 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
838 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
841 udp_send_redispatch(netmsg_t msg)
843 struct mbuf *m = msg->send.nm_m;
844 int pru_flags = msg->send.nm_flags;
845 struct inpcb *inp = msg->send.base.nm_so->so_pcb;
846 struct mbuf *m_opt = msg->send.nm_control; /* XXX save ipopt */
847 int flags = msg->send.nm_priv; /* ip_output flags */
850 logudp(redisp_ipout_beg, inp);
853 * - Don't use inp route cache. It should only be used in the
855 * - Access to inp_moptions should be safe, since multicast UDP
856 * datagrams are redispatched to netisr0 and inp_moptions is
857 * changed only in netisr0.
859 error = ip_output(m, m_opt, NULL, flags, inp->inp_moptions, inp);
860 if ((pru_flags & PRUS_NOREPLY) == 0)
861 lwkt_replymsg(&msg->send.base.lmsg, error);
864 /* Free saved ip options, if any */
868 logudp(redisp_ipout_end, inp);
872 udp_send(netmsg_t msg)
874 struct socket *so = msg->send.base.nm_so;
875 struct mbuf *m = msg->send.nm_m;
876 struct sockaddr *dstaddr = msg->send.nm_addr;
877 int pru_flags = msg->send.nm_flags;
878 struct inpcb *inp = so->so_pcb;
879 struct thread *td = msg->send.nm_td;
883 int len = m->m_pkthdr.len;
884 struct sockaddr_in *sin; /* really is initialized before use */
887 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
888 KKASSERT(msg->send.nm_control == NULL);
890 logudp(send_beg, inp);
897 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
902 if (inp->inp_lport == 0) { /* unbound socket */
903 error = in_pcbbind(inp, NULL, td);
907 udbinfo_barrier_set();
908 in_pcbinswildcardhash(inp);
909 udbinfo_barrier_rem();
912 if (dstaddr != NULL) { /* destination address specified */
913 if (inp->inp_faddr.s_addr != INADDR_ANY) {
914 /* already connected */
918 sin = (struct sockaddr_in *)dstaddr;
919 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
920 error = EAFNOSUPPORT; /* IPv6 only jail */
924 if (inp->inp_faddr.s_addr == INADDR_ANY) {
925 /* no destination specified and not already connected */
933 * Calculate data length and get a mbuf
934 * for UDP and IP headers.
936 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
943 * Fill in mbuf with extended UDP header
944 * and addresses and length put into network format.
946 ui = mtod(m, struct udpiphdr *);
947 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
948 ui->ui_pr = IPPROTO_UDP;
951 * Set destination address.
953 if (dstaddr != NULL) { /* use specified destination */
954 ui->ui_dst = sin->sin_addr;
955 ui->ui_dport = sin->sin_port;
956 } else { /* use connected destination */
957 ui->ui_dst = inp->inp_faddr;
958 ui->ui_dport = inp->inp_fport;
962 * Set source address.
964 if (inp->inp_laddr.s_addr == INADDR_ANY ||
965 IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) {
966 struct sockaddr_in *if_sin;
968 if (dstaddr == NULL) {
970 * connect() had (or should have) failed because
971 * the interface had no IP address, but the
972 * application proceeded to call send() anyways.
978 /* Look up outgoing interface. */
979 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1);
982 ui->ui_src = if_sin->sin_addr; /* use address of interface */
984 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
986 ui->ui_sport = inp->inp_lport;
987 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
990 * Release the original thread, since it is no longer used
992 if (pru_flags & PRUS_HELDTD) {
994 pru_flags &= ~PRUS_HELDTD;
997 * Free the dest address, since it is no longer needed
999 if (pru_flags & PRUS_FREEADDR) {
1000 kfree(dstaddr, M_SONAME);
1001 pru_flags &= ~PRUS_FREEADDR;
1004 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1007 * Set up checksum and output datagram.
1010 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
1011 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1012 m->m_pkthdr.csum_flags = CSUM_UDP;
1013 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1014 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
1018 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
1019 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1020 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
1021 udp_stat.udps_opackets++;
1023 flags = IP_DEBUGROUTE |
1024 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1025 if (pru_flags & PRUS_DONTROUTE)
1026 flags |= SO_DONTROUTE;
1028 cpu = udp_addrcpu_pkt(ui->ui_dst.s_addr, ui->ui_dport,
1029 ui->ui_src.s_addr, ui->ui_sport);
1030 if (cpu != mycpuid) {
1031 struct mbuf *m_opt = NULL;
1032 struct netmsg_pru_send *smsg;
1033 struct lwkt_port *port = netisr_cpuport(cpu);
1036 * Not on the CPU that matches this UDP datagram hash;
1037 * redispatch to the correct CPU to do the ip_output().
1039 if (inp->inp_options != NULL) {
1041 * If there are ip options, then save a copy,
1042 * since accessing inp_options on other CPUs'
1045 * XXX optimize this?
1047 m_opt = m_copym(inp->inp_options, 0, M_COPYALL,
1050 if ((pru_flags & PRUS_NOREPLY) == 0) {
1052 * Change some parts of the original netmsg and
1053 * forward it to the target netisr.
1055 * NOTE: so_port MUST NOT be checked in the target
1059 smsg->nm_priv = flags; /* ip_output flags */
1061 smsg->nm_control = m_opt; /* XXX save ipopt */
1062 smsg->base.lmsg.ms_flags |= MSGF_IGNSOPORT;
1063 smsg->base.nm_dispatch = udp_send_redispatch;
1064 lwkt_forwardmsg(port, &smsg->base.lmsg);
1067 * Recreate the netmsg, since the original mbuf
1068 * could have been changed. And send it to the
1071 * NOTE: so_port MUST NOT be checked in the target
1074 smsg = &m->m_hdr.mh_sndmsg;
1075 netmsg_init(&smsg->base, so, &netisr_apanic_rport,
1076 MSGF_IGNSOPORT, udp_send_redispatch);
1077 smsg->nm_priv = flags; /* ip_output flags */
1078 smsg->nm_flags = pru_flags;
1080 smsg->nm_control = m_opt; /* XXX save ipopt */
1081 lwkt_sendmsg(port, &smsg->base.lmsg);
1084 /* This UDP datagram is redispatched; done */
1085 logudp(send_redisp, inp);
1089 logudp(send_ipout, inp);
1090 error = ip_output(m, inp->inp_options, &inp->inp_route, flags,
1091 inp->inp_moptions, inp);
1098 if (pru_flags & PRUS_HELDTD)
1100 if (pru_flags & PRUS_FREEADDR)
1101 kfree(dstaddr, M_SONAME);
1102 if ((pru_flags & PRUS_NOREPLY) == 0)
1103 lwkt_replymsg(&msg->send.base.lmsg, error);
1105 logudp(send_end, inp);
1108 u_long udp_sendspace = 9216; /* really max datagram size */
1109 /* 40 1K datagrams */
1110 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
1111 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
1113 u_long udp_recvspace = 40 * (1024 +
1115 sizeof(struct sockaddr_in6)
1117 sizeof(struct sockaddr_in)
1120 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1121 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
1124 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
1125 * will sofree() it when we return.
1128 udp_abort(netmsg_t msg)
1130 struct socket *so = msg->abort.base.nm_so;
1134 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1138 soisdisconnected(so);
1140 udbinfo_barrier_set();
1142 udbinfo_barrier_rem();
1147 lwkt_replymsg(&msg->abort.base.lmsg, error);
1151 udp_attach(netmsg_t msg)
1153 struct socket *so = msg->attach.base.nm_so;
1154 struct pru_attach_info *ai = msg->attach.nm_ai;
1158 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1165 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
1169 udbinfo_barrier_set();
1170 error = in_pcballoc(so, &udbinfo);
1171 udbinfo_barrier_rem();
1176 inp = (struct inpcb *)so->so_pcb;
1177 inp->inp_vflag |= INP_IPV4;
1178 inp->inp_ip_ttl = ip_defttl;
1181 lwkt_replymsg(&msg->attach.base.lmsg, error);
1185 udp_bind(netmsg_t msg)
1187 struct socket *so = msg->bind.base.nm_so;
1188 struct sockaddr *nam = msg->bind.nm_nam;
1189 struct thread *td = msg->bind.nm_td;
1190 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1196 error = in_pcbbind(inp, nam, td);
1198 if (sin->sin_addr.s_addr != INADDR_ANY)
1199 inp->inp_flags |= INP_WASBOUND_NOTANY;
1201 udbinfo_barrier_set();
1202 in_pcbinswildcardhash(inp);
1203 udbinfo_barrier_rem();
1208 lwkt_replymsg(&msg->bind.base.lmsg, error);
1212 udp_connect(netmsg_t msg)
1214 struct socket *so = msg->connect.base.nm_so;
1215 struct sockaddr *nam = msg->connect.nm_nam;
1216 struct thread *td = msg->connect.nm_td;
1218 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1219 struct sockaddr_in *if_sin;
1223 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1224 KKASSERT(msg->connect.nm_m == NULL);
1232 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1233 panic("UDP does not support RECONNECT");
1235 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1236 in_pcblink(inp, &udbinfo);
1240 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1247 * Bind if we have to
1249 if (inp->inp_lport == 0 ||
1250 (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1251 inp->inp_laddr.s_addr == INADDR_ANY)) {
1252 error = in_pcbbind(inp, NULL, td);
1258 * Calculate the correct protocol processing thread. The connect
1259 * operation must run there.
1261 error = in_pcbladdr(inp, nam, &if_sin, td);
1264 if (!prison_remote_ip(td, nam)) {
1265 error = EAFNOSUPPORT; /* IPv6 only jail */
1269 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1270 inp->inp_laddr.s_addr != INADDR_ANY ?
1271 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr, inp->inp_lport);
1272 if (port != &curthread->td_msgport) {
1274 struct route *ro = &inp->inp_route;
1277 * in_pcbladdr() may have allocated a route entry for us
1278 * on the current CPU, but we need a route entry on the
1279 * inpcb's owner CPU, so free it here.
1281 if (ro->ro_rt != NULL)
1283 bzero(ro, sizeof(*ro));
1286 * We are moving the protocol processing port the socket
1287 * is on, we have to unlink here and re-link on the
1290 in_pcbunlink(so->so_pcb, &udbinfo);
1291 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1292 sosetport(so, port);
1293 msg->connect.nm_flags |= PRUC_RECONNECT;
1294 msg->connect.base.nm_dispatch = udp_connect;
1296 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1297 /* msg invalid now */
1300 panic("UDP activity should only be in netisr0");
1303 KKASSERT(port == &curthread->td_msgport);
1304 error = udp_connect_oncpu(inp, sin, if_sin);
1306 lwkt_replymsg(&msg->connect.base.lmsg, error);
1310 udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
1311 struct sockaddr_in *if_sin)
1313 struct socket *so = inp->inp_socket;
1316 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1317 sin->sin_addr, sin->sin_port,
1318 inp->inp_laddr.s_addr != INADDR_ANY ?
1319 inp->inp_laddr : if_sin->sin_addr, inp->inp_lport, FALSE, NULL);
1323 udbinfo_barrier_set();
1325 if (inp->inp_flags & INP_WILDCARD)
1326 in_pcbremwildcardhash(inp);
1328 if (inp->inp_laddr.s_addr == INADDR_ANY)
1329 inp->inp_laddr = if_sin->sin_addr;
1330 inp->inp_faddr = sin->sin_addr;
1331 inp->inp_fport = sin->sin_port;
1332 in_pcbinsconnhash(inp);
1335 * No more errors can occur, finish adjusting the socket
1336 * and change the processing port to reflect the connected
1337 * socket. Once set we can no longer safely mess with the
1342 udbinfo_barrier_rem();
1348 udp_detach(netmsg_t msg)
1350 struct socket *so = msg->detach.base.nm_so;
1354 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1358 udbinfo_barrier_set();
1360 udbinfo_barrier_rem();
1365 lwkt_replymsg(&msg->detach.base.lmsg, error);
1369 udp_disconnect(netmsg_t msg)
1371 struct socket *so = msg->disconnect.base.nm_so;
1376 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1383 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1388 udbinfo_barrier_set();
1390 in_pcbdisconnect(inp);
1393 * Follow traditional BSD behavior and retain the local port
1394 * binding. But, fix the old misbehavior of overwriting any
1395 * previously bound local address.
1397 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1398 inp->inp_laddr.s_addr = INADDR_ANY;
1399 in_pcbinswildcardhash(inp);
1401 udbinfo_barrier_rem();
1403 soclrstate(so, SS_ISCONNECTED); /* XXX */
1405 ro = &inp->inp_route;
1406 if (ro->ro_rt != NULL)
1408 bzero(ro, sizeof(*ro));
1411 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1415 udp_shutdown(netmsg_t msg)
1417 struct socket *so = msg->shutdown.base.nm_so;
1421 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1430 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1436 lwkt_serialize_enter(&udbinfo_slize);
1440 udbinfo_unlock(void)
1442 lwkt_serialize_exit(&udbinfo_slize);
1446 udbinfo_barrier_set(void)
1448 netisr_barrier_set(udbinfo_br);
1453 udbinfo_barrier_rem(void)
1456 netisr_barrier_rem(udbinfo_br);
1459 struct pr_usrreqs udp_usrreqs = {
1460 .pru_abort = udp_abort,
1461 .pru_accept = pr_generic_notsupp,
1462 .pru_attach = udp_attach,
1463 .pru_bind = udp_bind,
1464 .pru_connect = udp_connect,
1465 .pru_connect2 = pr_generic_notsupp,
1466 .pru_control = in_control_dispatch,
1467 .pru_detach = udp_detach,
1468 .pru_disconnect = udp_disconnect,
1469 .pru_listen = pr_generic_notsupp,
1470 .pru_peeraddr = in_setpeeraddr_dispatch,
1471 .pru_rcvd = pr_generic_notsupp,
1472 .pru_rcvoob = pr_generic_notsupp,
1473 .pru_send = udp_send,
1474 .pru_sense = pru_sense_null,
1475 .pru_shutdown = udp_shutdown,
1476 .pru_sockaddr = in_setsockaddr_dispatch,
1477 .pru_sosend = sosendudp,
1478 .pru_soreceive = soreceive