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>
84 #include <sys/thread2.h>
85 #include <sys/socketvar2.h>
86 #include <sys/serialize.h>
88 #include <machine/stdarg.h>
91 #include <net/route.h>
92 #include <net/netmsg2.h>
93 #include <net/netisr2.h>
95 #include <netinet/in.h>
96 #include <netinet/in_systm.h>
97 #include <netinet/ip.h>
99 #include <netinet/ip6.h>
101 #include <netinet/in_pcb.h>
102 #include <netinet/in_var.h>
103 #include <netinet/ip_var.h>
105 #include <netinet6/ip6_var.h>
107 #include <netinet/ip_icmp.h>
108 #include <netinet/icmp_var.h>
109 #include <netinet/udp.h>
110 #include <netinet/udp_var.h>
113 #include <netproto/ipsec/ipsec.h>
117 #include <netinet6/ipsec.h>
121 * UDP protocol implementation.
122 * Per RFC 768, August, 1980.
125 static int udpcksum = 1;
127 static int udpcksum = 0; /* XXX */
129 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
130 &udpcksum, 0, "Enable checksumming of UDP packets");
133 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
134 &log_in_vain, 0, "Log all incoming UDP packets");
136 static int blackhole = 0;
137 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
138 &blackhole, 0, "Do not send port unreachables for refused connects");
140 static int strict_mcast_mship = 1;
141 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
142 &strict_mcast_mship, 0, "Only send multicast to member sockets");
144 int udp_sosend_async = 1;
145 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
146 &udp_sosend_async, 0, "UDP asynchronized pru_send");
148 int udp_sosend_prepend = 1;
149 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
150 &udp_sosend_prepend, 0,
151 "Prepend enough space for proto and link header in pru_send");
153 static int udp_reuseport_ext = 1;
154 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
155 &udp_reuseport_ext, 0, "SO_REUSEPORT extension");
157 struct inpcbinfo udbinfo;
159 static struct netisr_barrier *udbinfo_br;
160 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
163 #define UDBHASHSIZE 16
166 struct udpstat udpstat_percpu[MAXCPU] __cachealign;
170 struct sockaddr_in6 uin6_sin;
171 u_char uin6_init_done : 1;
174 struct ip6_hdr uip6_ip6;
175 u_char uip6_init_done : 1;
182 static void udp_append (struct inpcb *last, struct ip *ip,
183 struct mbuf *n, int off, struct sockaddr_in *udp_in,
184 struct udp_in6 *, struct udp_ip6 *);
186 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
189 static int udp_connect_oncpu(struct socket *so, struct thread *td,
190 struct sockaddr_in *sin, struct sockaddr_in *if_sin);
191 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
192 struct thread *, int, boolean_t);
199 in_pcbinfo_init(&udbinfo);
200 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
201 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
202 &udbinfo.porthashmask);
203 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
204 &udbinfo.wildcardhashmask);
205 udbinfo.localgrphashbase = hashinit(UDBHASHSIZE, M_PCB,
206 &udbinfo.localgrphashmask);
207 udbinfo.ipi_size = sizeof(struct inpcb);
209 udbinfo_br = netisr_barrier_create();
212 * Initialize UDP statistics counters for each CPU.
214 for (cpu = 0; cpu < ncpus; ++cpu)
215 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat));
219 sysctl_udpstat(SYSCTL_HANDLER_ARGS)
223 for (cpu = 0; cpu < ncpus; ++cpu) {
224 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu],
225 sizeof(struct udpstat))))
227 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu],
228 sizeof(struct udpstat))))
234 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
235 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics");
238 * Check multicast packets to make sure they are only sent to sockets with
239 * multicast memberships for the packet's destination address and arrival
240 * interface. Multicast packets to multicast-unaware sockets are also
243 * Returns 0 if the packet is acceptable, -1 if it is not.
246 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
249 struct ip_moptions *mopt;
251 if (strict_mcast_mship == 0 ||
252 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
255 mopt = inp->inp_moptions;
258 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
259 struct in_multi *maddr = mopt->imo_membership[mshipno];
261 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
262 m->m_pkthdr.rcvif == maddr->inm_ifp) {
270 udp_input(struct mbuf **mp, int *offp, int proto)
272 struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
274 struct udp_in6 udp_in6 = {
275 { sizeof udp_in6.uin6_sin, AF_INET6 }, 0
277 struct udp_ip6 udp_ip6;
285 struct mbuf *opts = NULL;
288 struct sockaddr *append_sa;
295 udp_stat.udps_ipackets++;
298 * Strip IP options, if any; should skip this,
299 * make available to user, and use on returned packets,
300 * but we don't yet have a way to check the checksum
301 * with options still present.
303 if (iphlen > sizeof(struct ip)) {
305 iphlen = sizeof(struct ip);
309 * IP and UDP headers are together in first mbuf.
310 * Already checked and pulled up in ip_demux().
312 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
313 ("UDP header not in one mbuf"));
315 ip = mtod(m, struct ip *);
316 uh = (struct udphdr *)((caddr_t)ip + iphlen);
318 /* destination port of 0 is illegal, based on RFC768. */
319 if (uh->uh_dport == 0)
323 * Make mbuf data length reflect UDP length.
324 * If not enough data to reflect UDP length, drop.
326 len = ntohs((u_short)uh->uh_ulen);
327 if (ip->ip_len != len) {
328 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
329 udp_stat.udps_badlen++;
332 m_adj(m, len - ip->ip_len);
333 /* ip->ip_len = len; */
336 * Save a copy of the IP header in case we want restore it
337 * for sending an ICMP error message in response.
342 * Checksum extended UDP header and data.
345 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
346 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
347 uh->uh_sum = m->m_pkthdr.csum_data;
349 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
350 ip->ip_dst.s_addr, htonl((u_short)len +
351 m->m_pkthdr.csum_data + IPPROTO_UDP));
352 uh->uh_sum ^= 0xffff;
356 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
357 bzero(((struct ipovly *)ip)->ih_x1, 9);
358 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
359 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
360 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
363 udp_stat.udps_badsum++;
365 return(IPPROTO_DONE);
368 udp_stat.udps_nosum++;
370 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
371 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
375 * Deliver a multicast or broadcast datagram to *all* sockets
376 * for which the local and remote addresses and ports match
377 * those of the incoming datagram. This allows more than
378 * one process to receive multi/broadcasts on the same port.
379 * (This really ought to be done for unicast datagrams as
380 * well, but that would cause problems with existing
381 * applications that open both address-specific sockets and
382 * a wildcard socket listening to the same port -- they would
383 * end up receiving duplicates of every unicast datagram.
384 * Those applications open the multiple sockets to overcome an
385 * inadequacy of the UDP socket interface, but for backwards
386 * compatibility we avoid the problem here rather than
387 * fixing the interface. Maybe 4.5BSD will remedy this?)
391 * Construct sockaddr format source address.
393 udp_in.sin_port = uh->uh_sport;
394 udp_in.sin_addr = ip->ip_src;
396 * Locate pcb(s) for datagram.
397 * (Algorithm copied from raw_intr().)
401 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
403 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
404 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
406 if (!(inp->inp_vflag & INP_IPV4))
409 if (inp->inp_lport != uh->uh_dport)
411 if (inp->inp_laddr.s_addr != INADDR_ANY) {
412 if (inp->inp_laddr.s_addr !=
416 if (inp->inp_faddr.s_addr != INADDR_ANY) {
417 if (inp->inp_faddr.s_addr !=
419 inp->inp_fport != uh->uh_sport)
423 if (check_multicast_membership(ip, inp, m) < 0)
430 /* check AH/ESP integrity. */
431 if (ipsec4_in_reject_so(m, last->inp_socket))
432 ipsecstat.in_polvio++;
433 /* do not inject data to pcb */
437 /* check AH/ESP integrity. */
438 if (ipsec4_in_reject(m, last))
441 #endif /*FAST_IPSEC*/
442 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
443 udp_append(last, ip, n,
444 iphlen + sizeof(struct udphdr),
455 * Don't look for additional matches if this one does
456 * not have either the SO_REUSEPORT or SO_REUSEADDR
457 * socket options set. This heuristic avoids searching
458 * through all pcbs in the common case of a non-shared
459 * port. It * assumes that an application will never
460 * clear these options after setting them.
462 if (!(last->inp_socket->so_options &
463 (SO_REUSEPORT | SO_REUSEADDR)))
469 * No matching pcb found; discard datagram.
470 * (No need to send an ICMP Port Unreachable
471 * for a broadcast or multicast datgram.)
473 udp_stat.udps_noportbcast++;
477 /* check AH/ESP integrity. */
478 if (ipsec4_in_reject_so(m, last->inp_socket)) {
479 ipsecstat.in_polvio++;
484 /* check AH/ESP integrity. */
485 if (ipsec4_in_reject(m, last))
487 #endif /*FAST_IPSEC*/
488 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
496 return(IPPROTO_DONE);
499 * Locate pcb for datagram.
501 inp = in_pcblookup_pkthash(&udbinfo, ip->ip_src, uh->uh_sport,
502 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif,
503 udp_reuseport_ext ? m : NULL);
506 char buf[sizeof "aaa.bbb.ccc.ddd"];
508 strcpy(buf, inet_ntoa(ip->ip_dst));
510 "Connection attempt to UDP %s:%d from %s:%d\n",
511 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
512 ntohs(uh->uh_sport));
514 udp_stat.udps_noport++;
515 if (m->m_flags & (M_BCAST | M_MCAST)) {
516 udp_stat.udps_noportbcast++;
522 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
526 ip->ip_len += iphlen;
527 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
528 return(IPPROTO_DONE);
531 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
532 ipsecstat.in_polvio++;
537 if (ipsec4_in_reject(m, inp))
539 #endif /*FAST_IPSEC*/
541 * Check the minimum TTL for socket.
543 if (ip->ip_ttl < inp->inp_ip_minttl)
547 * Construct sockaddr format source address.
548 * Stuff source address and datagram in user buffer.
550 udp_in.sin_port = uh->uh_sport;
551 udp_in.sin_addr = ip->ip_src;
552 if ((inp->inp_flags & INP_CONTROLOPTS) ||
553 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
555 if (inp->inp_vflag & INP_IPV6) {
558 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
559 savedflags = inp->inp_flags;
560 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
561 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
562 inp->inp_flags = savedflags;
565 ip_savecontrol(inp, &opts, ip, m);
567 m_adj(m, iphlen + sizeof(struct udphdr));
569 if (inp->inp_vflag & INP_IPV6) {
570 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
571 append_sa = (struct sockaddr *)&udp_in6;
574 append_sa = (struct sockaddr *)&udp_in;
576 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
577 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
578 udp_stat.udps_fullsock++;
579 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
582 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
583 sorwakeup(inp->inp_socket);
584 return(IPPROTO_DONE);
589 return(IPPROTO_DONE);
594 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
596 bzero(ip6, sizeof *ip6);
598 ip6->ip6_vfc = IPV6_VERSION;
599 ip6->ip6_plen = ip->ip_len;
600 ip6->ip6_nxt = ip->ip_p;
601 ip6->ip6_hlim = ip->ip_ttl;
602 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
604 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
605 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
610 * subroutine of udp_input(), mainly for source code readability.
611 * caller must properly init udp_ip6 and udp_in6 beforehand.
614 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
615 struct sockaddr_in *udp_in,
616 struct udp_in6 *udp_in6, struct udp_ip6 *udp_ip6)
618 struct sockaddr *append_sa;
619 struct mbuf *opts = NULL;
621 if (last->inp_flags & INP_CONTROLOPTS ||
622 last->inp_socket->so_options & SO_TIMESTAMP) {
624 if (last->inp_vflag & INP_IPV6) {
627 if (udp_ip6->uip6_init_done == 0) {
628 ip_2_ip6_hdr(&udp_ip6->uip6_ip6, ip);
629 udp_ip6->uip6_init_done = 1;
631 savedflags = last->inp_flags;
632 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
633 ip6_savecontrol(last, &opts, &udp_ip6->uip6_ip6, n);
634 last->inp_flags = savedflags;
637 ip_savecontrol(last, &opts, ip, n);
640 if (last->inp_vflag & INP_IPV6) {
641 if (udp_in6->uin6_init_done == 0) {
642 in6_sin_2_v4mapsin6(udp_in, &udp_in6->uin6_sin);
643 udp_in6->uin6_init_done = 1;
645 append_sa = (struct sockaddr *)&udp_in6->uin6_sin;
648 append_sa = (struct sockaddr *)udp_in;
650 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
651 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
655 udp_stat.udps_fullsock++;
657 sorwakeup(last->inp_socket);
659 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
663 * Notify a udp user of an asynchronous error;
664 * just wake up so that he can collect error status.
667 udp_notify(struct inpcb *inp, int error)
669 inp->inp_socket->so_error = error;
670 sorwakeup(inp->inp_socket);
671 sowwakeup(inp->inp_socket);
674 struct netmsg_udp_notify {
675 struct netmsg_base base;
676 void (*nm_notify)(struct inpcb *, int);
677 struct in_addr nm_faddr;
682 udp_notifyall_oncpu(netmsg_t msg)
684 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
689 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
690 nm->nm_arg, nm->nm_notify);
691 lwkt_replymsg(&nm->base.lmsg, 0);
694 /* XXX currently udp only runs on cpu 0 */
695 nextcpu = mycpuid + 1;
696 if (nextcpu < ncpus2)
697 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
699 lwkt_replymsg(&nmsg->base.lmsg, 0);
704 udp_rtchange(struct inpcb *inp, int err)
706 /* XXX Nuke this, once UDP inpcbs are CPU localized */
707 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
708 rtfree(inp->inp_route.ro_rt);
709 inp->inp_route.ro_rt = NULL;
711 * A new route can be allocated the next time
712 * output is attempted.
718 udp_ctlinput(netmsg_t msg)
720 struct sockaddr *sa = msg->ctlinput.nm_arg;
721 struct ip *ip = msg->ctlinput.nm_extra;
722 int cmd = msg->ctlinput.nm_cmd;
724 void (*notify) (struct inpcb *, int) = udp_notify;
725 struct in_addr faddr;
728 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
730 faddr = ((struct sockaddr_in *)sa)->sin_addr;
731 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
734 if (PRC_IS_REDIRECT(cmd)) {
736 notify = udp_rtchange;
737 } else if (cmd == PRC_HOSTDEAD) {
739 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
744 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
745 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
746 ip->ip_src, uh->uh_sport, 0, NULL);
747 if (inp != NULL && inp->inp_socket != NULL)
748 (*notify)(inp, inetctlerrmap[cmd]);
749 } else if (PRC_IS_REDIRECT(cmd)) {
750 struct netmsg_udp_notify *nm;
752 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
753 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
754 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
755 0, udp_notifyall_oncpu);
756 nm->nm_faddr = faddr;
757 nm->nm_arg = inetctlerrmap[cmd];
758 nm->nm_notify = notify;
759 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg);
762 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
763 * once UDP inpcbs are CPU localized
765 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
766 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
770 lwkt_replymsg(&msg->lmsg, 0);
774 udp_pcblist(SYSCTL_HANDLER_ARGS)
780 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
784 KKASSERT(xi == NULL);
788 KKASSERT(xi == NULL);
792 for (i = 0; i < nxi; ++i) {
793 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
801 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
802 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
805 udp_getcred(SYSCTL_HANDLER_ARGS)
807 struct sockaddr_in addrs[2];
808 struct ucred cred0, *cred = NULL;
812 error = priv_check(req->td, PRIV_ROOT);
815 error = SYSCTL_IN(req, addrs, sizeof addrs);
820 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
821 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
822 if (inp == NULL || inp->inp_socket == NULL) {
825 if (inp->inp_socket->so_cred != NULL) {
826 cred0 = *(inp->inp_socket->so_cred);
835 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
838 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
839 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
842 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
843 struct thread *td, int flags, boolean_t held_td)
846 int len = m->m_pkthdr.len;
847 struct sockaddr_in *sin; /* really is initialized before use */
848 int error = 0, lport_any = 0;
850 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
855 if (inp->inp_lport == 0) { /* unbound socket */
856 error = in_pcbbind(inp, NULL, td);
860 udbinfo_barrier_set();
861 in_pcbinswildcardhash(inp);
862 udbinfo_barrier_rem();
866 if (dstaddr != NULL) { /* destination address specified */
867 if (inp->inp_faddr.s_addr != INADDR_ANY) {
868 /* already connected */
872 sin = (struct sockaddr_in *)dstaddr;
873 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
874 error = EAFNOSUPPORT; /* IPv6 only jail */
878 if (inp->inp_faddr.s_addr == INADDR_ANY) {
879 /* no destination specified and not already connected */
887 * Calculate data length and get a mbuf
888 * for UDP and IP headers.
890 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
897 * Fill in mbuf with extended UDP header
898 * and addresses and length put into network format.
900 ui = mtod(m, struct udpiphdr *);
901 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
902 ui->ui_pr = IPPROTO_UDP;
905 * Set destination address.
907 if (dstaddr != NULL) { /* use specified destination */
908 ui->ui_dst = sin->sin_addr;
909 ui->ui_dport = sin->sin_port;
910 } else { /* use connected destination */
911 ui->ui_dst = inp->inp_faddr;
912 ui->ui_dport = inp->inp_fport;
916 * Set source address.
918 if (inp->inp_laddr.s_addr == INADDR_ANY ||
919 IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) {
920 struct sockaddr_in *if_sin;
922 if (dstaddr == NULL) {
924 * connect() had (or should have) failed because
925 * the interface had no IP address, but the
926 * application proceeded to call send() anyways.
932 /* Look up outgoing interface. */
933 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1);
936 ui->ui_src = if_sin->sin_addr; /* use address of interface */
938 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
940 ui->ui_sport = inp->inp_lport;
941 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
944 * Release the original thread, since it is no longer used
951 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
954 * Set up checksum and output datagram.
957 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
958 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
959 m->m_pkthdr.csum_flags = CSUM_UDP;
960 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
961 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
965 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
966 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
967 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
968 udp_stat.udps_opackets++;
970 error = ip_output(m, inp->inp_options, &inp->inp_route,
971 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
972 flags | IP_DEBUGROUTE,
973 inp->inp_moptions, inp);
976 * If this is the first data gram sent on an unbound and unconnected
977 * UDP socket, lport will be changed in this function. If target
978 * CPU after this lport changing is no longer the current CPU, then
979 * free the route entry allocated on the current CPU.
982 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
983 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
985 struct route *ro = &inp->inp_route;
987 if (ro->ro_rt != NULL)
989 bzero(ro, sizeof(*ro));
991 panic("UDP activity should only be in netisr0");
1004 u_long udp_sendspace = 9216; /* really max datagram size */
1005 /* 40 1K datagrams */
1006 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
1007 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
1009 u_long udp_recvspace = 40 * (1024 +
1011 sizeof(struct sockaddr_in6)
1013 sizeof(struct sockaddr_in)
1016 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1017 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
1020 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
1021 * will sofree() it when we return.
1024 udp_abort(netmsg_t msg)
1026 struct socket *so = msg->abort.base.nm_so;
1030 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1034 soisdisconnected(so);
1036 udbinfo_barrier_set();
1038 udbinfo_barrier_rem();
1043 lwkt_replymsg(&msg->abort.base.lmsg, error);
1047 udp_attach(netmsg_t msg)
1049 struct socket *so = msg->attach.base.nm_so;
1050 struct pru_attach_info *ai = msg->attach.nm_ai;
1054 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1061 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
1065 udbinfo_barrier_set();
1066 error = in_pcballoc(so, &udbinfo);
1067 udbinfo_barrier_rem();
1073 * Set default port for protocol processing prior to bind/connect.
1075 sosetport(so, netisr_cpuport(0));
1077 inp = (struct inpcb *)so->so_pcb;
1078 inp->inp_vflag |= INP_IPV4;
1079 inp->inp_ip_ttl = ip_defttl;
1082 lwkt_replymsg(&msg->attach.base.lmsg, error);
1086 udp_bind(netmsg_t msg)
1088 struct socket *so = msg->bind.base.nm_so;
1089 struct sockaddr *nam = msg->bind.nm_nam;
1090 struct thread *td = msg->bind.nm_td;
1091 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1097 error = in_pcbbind(inp, nam, td);
1099 if (sin->sin_addr.s_addr != INADDR_ANY)
1100 inp->inp_flags |= INP_WASBOUND_NOTANY;
1102 udbinfo_barrier_set();
1103 in_pcbinswildcardhash(inp);
1104 udbinfo_barrier_rem();
1109 lwkt_replymsg(&msg->bind.base.lmsg, error);
1113 udp_connect(netmsg_t msg)
1115 struct socket *so = msg->connect.base.nm_so;
1116 struct sockaddr *nam = msg->connect.nm_nam;
1117 struct thread *td = msg->connect.nm_td;
1119 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1120 struct sockaddr_in *if_sin;
1124 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1132 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1133 panic("UDP does not support RECONNECT");
1135 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1136 in_pcblink(inp, &udbinfo);
1140 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1147 * Bind if we have to
1149 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1150 inp->inp_laddr.s_addr == INADDR_ANY) {
1151 error = in_pcbbind(inp, NULL, td);
1157 * Calculate the correct protocol processing thread. The connect
1158 * operation must run there.
1160 error = in_pcbladdr(inp, nam, &if_sin, td);
1163 if (!prison_remote_ip(td, nam)) {
1164 error = EAFNOSUPPORT; /* IPv6 only jail */
1168 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1169 inp->inp_laddr.s_addr, inp->inp_lport);
1170 if (port != &curthread->td_msgport) {
1172 struct route *ro = &inp->inp_route;
1175 * in_pcbladdr() may have allocated a route entry for us
1176 * on the current CPU, but we need a route entry on the
1177 * inpcb's owner CPU, so free it here.
1179 if (ro->ro_rt != NULL)
1181 bzero(ro, sizeof(*ro));
1184 * We are moving the protocol processing port the socket
1185 * is on, we have to unlink here and re-link on the
1188 in_pcbunlink(so->so_pcb, &udbinfo);
1189 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1190 sosetport(so, port);
1191 msg->connect.nm_flags |= PRUC_RECONNECT;
1192 msg->connect.base.nm_dispatch = udp_connect;
1194 lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1195 /* msg invalid now */
1198 panic("UDP activity should only be in netisr0");
1201 KKASSERT(port == &curthread->td_msgport);
1202 error = udp_connect_oncpu(so, td, sin, if_sin);
1204 KKASSERT(msg->connect.nm_m == NULL);
1205 lwkt_replymsg(&msg->connect.base.lmsg, error);
1209 udp_connect_oncpu(struct socket *so, struct thread *td,
1210 struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1215 udbinfo_barrier_set();
1218 if (inp->inp_flags & INP_WILDCARD)
1219 in_pcbremwildcardhash(inp);
1220 error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1224 * No more errors can occur, finish adjusting the socket
1225 * and change the processing port to reflect the connected
1226 * socket. Once set we can no longer safely mess with the
1230 } else if (error == EAFNOSUPPORT) { /* connection dissolved */
1232 * Follow traditional BSD behavior and retain
1233 * the local port binding. But, fix the old misbehavior
1234 * of overwriting any previously bound local address.
1236 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1237 inp->inp_laddr.s_addr = INADDR_ANY;
1238 in_pcbinswildcardhash(inp);
1241 udbinfo_barrier_rem();
1246 udp_detach(netmsg_t msg)
1248 struct socket *so = msg->detach.base.nm_so;
1252 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1256 udbinfo_barrier_set();
1258 udbinfo_barrier_rem();
1263 lwkt_replymsg(&msg->detach.base.lmsg, error);
1267 udp_disconnect(netmsg_t msg)
1269 struct socket *so = msg->disconnect.base.nm_so;
1274 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1281 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1288 udbinfo_barrier_set();
1289 in_pcbdisconnect(inp);
1290 udbinfo_barrier_rem();
1292 soclrstate(so, SS_ISCONNECTED); /* XXX */
1295 ro = &inp->inp_route;
1296 if (ro->ro_rt != NULL)
1298 bzero(ro, sizeof(*ro));
1301 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1305 udp_send(netmsg_t msg)
1307 struct socket *so = msg->send.base.nm_so;
1308 struct mbuf *m = msg->send.nm_m;
1309 struct sockaddr *addr = msg->send.nm_addr;
1310 int pru_flags = msg->send.nm_flags;
1314 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1315 KKASSERT(msg->send.nm_control == NULL);
1319 struct thread *td = msg->send.nm_td;
1322 if (pru_flags & PRUS_DONTROUTE)
1323 flags |= SO_DONTROUTE;
1324 error = udp_output(inp, m, addr, td, flags,
1325 (pru_flags & PRUS_HELDTD) ? TRUE : FALSE);
1327 if (pru_flags & PRUS_HELDTD)
1328 lwkt_rele(msg->send.nm_td);
1333 if (pru_flags & PRUS_FREEADDR)
1334 kfree(addr, M_SONAME);
1336 if ((pru_flags & PRUS_NOREPLY) == 0)
1337 lwkt_replymsg(&msg->send.base.lmsg, error);
1341 udp_shutdown(netmsg_t msg)
1343 struct socket *so = msg->shutdown.base.nm_so;
1347 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
1356 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1362 lwkt_serialize_enter(&udbinfo_slize);
1366 udbinfo_unlock(void)
1368 lwkt_serialize_exit(&udbinfo_slize);
1372 udbinfo_barrier_set(void)
1374 netisr_barrier_set(udbinfo_br);
1379 udbinfo_barrier_rem(void)
1382 netisr_barrier_rem(udbinfo_br);
1385 struct pr_usrreqs udp_usrreqs = {
1386 .pru_abort = udp_abort,
1387 .pru_accept = pr_generic_notsupp,
1388 .pru_attach = udp_attach,
1389 .pru_bind = udp_bind,
1390 .pru_connect = udp_connect,
1391 .pru_connect2 = pr_generic_notsupp,
1392 .pru_control = in_control_dispatch,
1393 .pru_detach = udp_detach,
1394 .pru_disconnect = udp_disconnect,
1395 .pru_listen = pr_generic_notsupp,
1396 .pru_peeraddr = in_setpeeraddr_dispatch,
1397 .pru_rcvd = pr_generic_notsupp,
1398 .pru_rcvoob = pr_generic_notsupp,
1399 .pru_send = udp_send,
1400 .pru_sense = pru_sense_null,
1401 .pru_shutdown = udp_shutdown,
1402 .pru_sockaddr = in_setsockaddr_dispatch,
1403 .pru_sosend = sosendudp,
1404 .pru_soreceive = soreceive