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 MSGF_UDP_SEND MSGF_PROTO1
123 #define INP_DIRECT_DETACH INP_FLAG_PROTO2
125 #define UDP_KTR_STRING "inp=%p"
126 #define UDP_KTR_ARGS struct inpcb *inp
129 #define KTR_UDP KTR_ALL
132 KTR_INFO_MASTER(udp);
133 KTR_INFO(KTR_UDP, udp, send_beg, 0, UDP_KTR_STRING, UDP_KTR_ARGS);
134 KTR_INFO(KTR_UDP, udp, send_end, 1, UDP_KTR_STRING, UDP_KTR_ARGS);
135 KTR_INFO(KTR_UDP, udp, send_ipout, 2, UDP_KTR_STRING, UDP_KTR_ARGS);
136 KTR_INFO(KTR_UDP, udp, redisp_ipout_beg, 3, UDP_KTR_STRING, UDP_KTR_ARGS);
137 KTR_INFO(KTR_UDP, udp, redisp_ipout_end, 4, UDP_KTR_STRING, UDP_KTR_ARGS);
138 KTR_INFO(KTR_UDP, udp, send_redisp, 5, UDP_KTR_STRING, UDP_KTR_ARGS);
139 KTR_INFO(KTR_UDP, udp, send_inswildcard, 6, UDP_KTR_STRING, UDP_KTR_ARGS);
141 #define logudp(name, inp) KTR_LOG(udp_##name, inp)
144 * UDP protocol implementation.
145 * Per RFC 768, August, 1980.
148 static int udpcksum = 1;
150 static int udpcksum = 0; /* XXX */
152 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
153 &udpcksum, 0, "Enable checksumming of UDP packets");
156 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
157 &log_in_vain, 0, "Log all incoming UDP packets");
159 static int blackhole = 0;
160 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
161 &blackhole, 0, "Do not send port unreachables for refused connects");
163 static int strict_mcast_mship = 1;
164 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
165 &strict_mcast_mship, 0, "Only send multicast to member sockets");
167 int udp_sosend_async = 1;
168 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
169 &udp_sosend_async, 0, "UDP asynchronized pru_send");
171 int udp_sosend_prepend = 1;
172 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
173 &udp_sosend_prepend, 0,
174 "Prepend enough space for proto and link header in pru_send");
176 static int udp_reuseport_ext = 1;
177 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW,
178 &udp_reuseport_ext, 0, "SO_REUSEPORT extension");
180 struct inpcbinfo udbinfo[MAXCPU];
183 #define UDBHASHSIZE 16
186 struct udpstat udpstat_percpu[MAXCPU] __cachealign;
190 struct sockaddr_in6 uin6_sin;
191 u_char uin6_init_done : 1;
194 struct ip6_hdr uip6_ip6;
195 u_char uip6_init_done : 1;
202 static void udp_append (struct inpcb *last, struct ip *ip,
203 struct mbuf *n, int off, struct sockaddr_in *udp_in,
204 struct udp_in6 *, struct udp_ip6 *);
206 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
209 static int udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
210 struct sockaddr_in *if_sin);
212 static boolean_t udp_inswildcardhash(struct inpcb *inp,
213 struct netmsg_base *msg, int error);
214 static void udp_remwildcardhash(struct inpcb *inp);
219 struct inpcbportinfo *portinfo;
222 portinfo = kmalloc_cachealign(sizeof(*portinfo) * ncpus2, M_PCB,
225 for (cpu = 0; cpu < ncpus2; cpu++) {
226 struct inpcbinfo *uicb = &udbinfo[cpu];
230 * UDP pcb list, wildcard hash table and localgroup hash
233 in_pcbinfo_init(uicb, cpu, TRUE);
234 uicb->hashbase = hashinit(UDBHASHSIZE, M_PCB, &uicb->hashmask);
236 in_pcbportinfo_init(&portinfo[cpu], UDBHASHSIZE, TRUE, cpu);
237 uicb->portinfo = portinfo;
238 uicb->portinfo_mask = ncpus2_mask;
240 uicb->wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
241 &uicb->wildcardhashmask);
242 uicb->localgrphashbase = hashinit(UDBHASHSIZE, M_PCB,
243 &uicb->localgrphashmask);
245 uicb->ipi_size = sizeof(struct inpcb);
249 * Initialize UDP statistics counters for each CPU.
251 for (cpu = 0; cpu < ncpus; ++cpu)
252 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat));
256 sysctl_udpstat(SYSCTL_HANDLER_ARGS)
260 for (cpu = 0; cpu < ncpus; ++cpu) {
261 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu],
262 sizeof(struct udpstat))))
264 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu],
265 sizeof(struct udpstat))))
271 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW),
272 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics");
275 udp_ctloutput(netmsg_t msg)
277 struct socket *so = msg->base.nm_so;
278 struct sockopt *sopt = msg->ctloutput.nm_sopt;
279 struct inpcb *inp = so->so_pcb;
281 if (sopt->sopt_level == IPPROTO_IP) {
282 switch (sopt->sopt_name) {
283 case IP_MULTICAST_IF:
284 case IP_MULTICAST_VIF:
285 case IP_MULTICAST_TTL:
286 case IP_MULTICAST_LOOP:
287 case IP_ADD_MEMBERSHIP:
288 case IP_DROP_MEMBERSHIP:
289 if (&curthread->td_msgport != netisr_cpuport(0)) {
291 * This pr_ctloutput msg will be forwarded
292 * to netisr0 to run; we can't do direct
295 inp->inp_flags &= ~INP_DIRECT_DETACH;
300 return ip_ctloutput(msg);
304 * Check multicast packets to make sure they are only sent to sockets with
305 * multicast memberships for the packet's destination address and arrival
306 * interface. Multicast packets to multicast-unaware sockets are also
309 * Returns 0 if the packet is acceptable, -1 if it is not.
312 check_multicast_membership(const struct ip *ip, const struct inpcb *inp,
313 const struct mbuf *m)
315 const struct ip_moptions *mopt;
318 if (strict_mcast_mship == 0 ||
319 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
323 KASSERT(&curthread->td_msgport == netisr_cpuport(0),
324 ("multicast input not in netisr0"));
326 mopt = inp->inp_moptions;
329 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
330 const struct in_multi *maddr = mopt->imo_membership[mshipno];
332 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
333 m->m_pkthdr.rcvif == maddr->inm_ifp) {
340 struct udp_mcast_arg {
346 struct sockaddr_in *udp_in;
348 struct udp_in6 *udp_in6;
349 struct udp_ip6 *udp_ip6;
354 udp_mcast_input(struct udp_mcast_arg *arg)
356 struct inpcb *inp = arg->inp;
357 struct inpcb *last = arg->last;
358 struct ip *ip = arg->ip;
359 struct mbuf *m = arg->m;
361 if (check_multicast_membership(ip, inp, m) < 0)
362 return ERESTART; /* caller continue */
368 /* check AH/ESP integrity. */
369 if (ipsec4_in_reject_so(m, last->inp_socket))
370 ipsecstat.in_polvio++;
371 /* do not inject data to pcb */
375 /* check AH/ESP integrity. */
376 if (ipsec4_in_reject(m, last))
379 #endif /*FAST_IPSEC*/
380 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
381 udp_append(last, ip, n,
382 arg->iphlen + sizeof(struct udphdr),
385 arg->udp_in6, arg->udp_ip6
391 arg->last = last = inp;
394 * Don't look for additional matches if this one does
395 * not have either the SO_REUSEPORT or SO_REUSEADDR
396 * socket options set. This heuristic avoids searching
397 * through all pcbs in the common case of a non-shared
398 * port. It * assumes that an application will never
399 * clear these options after setting them.
401 if (!(last->inp_socket->so_options &
402 (SO_REUSEPORT | SO_REUSEADDR)))
403 return EJUSTRETURN; /* caller stop */
408 udp_input(struct mbuf **mp, int *offp, int proto)
410 struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
412 struct udp_in6 udp_in6 = {
413 { sizeof udp_in6.uin6_sin, AF_INET6 }, 0
415 struct udp_ip6 udp_ip6;
423 struct mbuf *opts = NULL;
426 struct sockaddr *append_sa;
427 struct inpcbinfo *pcbinfo = &udbinfo[mycpuid];
434 udp_stat.udps_ipackets++;
437 * Strip IP options, if any; should skip this,
438 * make available to user, and use on returned packets,
439 * but we don't yet have a way to check the checksum
440 * with options still present.
442 if (iphlen > sizeof(struct ip)) {
444 iphlen = sizeof(struct ip);
448 * IP and UDP headers are together in first mbuf.
449 * Already checked and pulled up in ip_demux().
451 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
452 ("UDP header not in one mbuf"));
454 ip = mtod(m, struct ip *);
455 uh = (struct udphdr *)((caddr_t)ip + iphlen);
457 /* destination port of 0 is illegal, based on RFC768. */
458 if (uh->uh_dport == 0)
462 * Make mbuf data length reflect UDP length.
463 * If not enough data to reflect UDP length, drop.
465 len = ntohs((u_short)uh->uh_ulen);
466 if (ip->ip_len != len) {
467 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
468 udp_stat.udps_badlen++;
471 m_adj(m, len - ip->ip_len);
472 /* ip->ip_len = len; */
475 * Save a copy of the IP header in case we want restore it
476 * for sending an ICMP error message in response.
481 * Checksum extended UDP header and data.
484 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
485 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
486 uh->uh_sum = m->m_pkthdr.csum_data;
488 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
489 ip->ip_dst.s_addr, htonl((u_short)len +
490 m->m_pkthdr.csum_data + IPPROTO_UDP));
491 uh->uh_sum ^= 0xffff;
495 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
496 bzero(((struct ipovly *)ip)->ih_x1, 9);
497 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
498 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
499 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
502 udp_stat.udps_badsum++;
504 return(IPPROTO_DONE);
507 udp_stat.udps_nosum++;
509 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
510 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
511 struct inpcbhead *connhead;
512 struct inpcontainer *ic, *ic_marker;
513 struct inpcontainerhead *ichead;
514 struct udp_mcast_arg arg;
519 * Deliver a multicast or broadcast datagram to *all* sockets
520 * for which the local and remote addresses and ports match
521 * those of the incoming datagram. This allows more than
522 * one process to receive multi/broadcasts on the same port.
523 * (This really ought to be done for unicast datagrams as
524 * well, but that would cause problems with existing
525 * applications that open both address-specific sockets and
526 * a wildcard socket listening to the same port -- they would
527 * end up receiving duplicates of every unicast datagram.
528 * Those applications open the multiple sockets to overcome an
529 * inadequacy of the UDP socket interface, but for backwards
530 * compatibility we avoid the problem here rather than
531 * fixing the interface. Maybe 4.5BSD will remedy this?)
535 * Construct sockaddr format source address.
537 udp_in.sin_port = uh->uh_sport;
538 udp_in.sin_addr = ip->ip_src;
539 arg.udp_in = &udp_in;
541 * Locate pcb(s) for datagram.
542 * (Algorithm copied from raw_intr().)
546 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
547 arg.udp_in6 = &udp_in6;
548 arg.udp_ip6 = &udp_ip6;
552 connhead = &pcbinfo->hashbase[
553 INP_PCBCONNHASH(ip->ip_src.s_addr, uh->uh_sport,
554 ip->ip_dst.s_addr, uh->uh_dport, pcbinfo->hashmask)];
555 LIST_FOREACH(inp, connhead, inp_hash) {
557 if (!(inp->inp_vflag & INP_IPV4))
560 if (!in_hosteq(inp->inp_faddr, ip->ip_src) ||
561 !in_hosteq(inp->inp_laddr, ip->ip_dst) ||
562 inp->inp_fport != uh->uh_sport ||
563 inp->inp_lport != uh->uh_dport)
571 error = udp_mcast_input(&arg);
572 if (error == ERESTART)
576 if (error == EJUSTRETURN)
580 ichead = &pcbinfo->wildcardhashbase[
581 INP_PCBWILDCARDHASH(uh->uh_dport,
582 pcbinfo->wildcardhashmask)];
583 ic_marker = in_pcbcontainer_marker(mycpuid);
585 GET_PCBINFO_TOKEN(pcbinfo);
586 LIST_INSERT_HEAD(ichead, ic_marker, ic_list);
587 while ((ic = LIST_NEXT(ic_marker, ic_list)) != NULL) {
588 LIST_REMOVE(ic_marker, ic_list);
589 LIST_INSERT_AFTER(ic, ic_marker, ic_list);
592 if (inp->inp_flags & INP_PLACEMARKER)
595 if (!(inp->inp_vflag & INP_IPV4))
598 if (inp->inp_lport != uh->uh_dport)
600 if (inp->inp_laddr.s_addr != INADDR_ANY &&
601 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
609 error = udp_mcast_input(&arg);
610 if (error == ERESTART)
614 if (error == EJUSTRETURN)
617 LIST_REMOVE(ic_marker, ic_list);
618 REL_PCBINFO_TOKEN(pcbinfo);
622 * No matching pcb found; discard datagram.
623 * (No need to send an ICMP Port Unreachable
624 * for a broadcast or multicast datgram.)
626 udp_stat.udps_noportbcast++;
630 /* check AH/ESP integrity. */
631 if (ipsec4_in_reject_so(m, last->inp_socket)) {
632 ipsecstat.in_polvio++;
637 /* check AH/ESP integrity. */
638 if (ipsec4_in_reject(m, last))
640 #endif /*FAST_IPSEC*/
641 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
649 return(IPPROTO_DONE);
652 * Locate pcb for datagram.
654 inp = in_pcblookup_pkthash(pcbinfo, ip->ip_src, uh->uh_sport,
655 ip->ip_dst, uh->uh_dport, TRUE, m->m_pkthdr.rcvif,
656 udp_reuseport_ext ? m : NULL);
659 char buf[sizeof "aaa.bbb.ccc.ddd"];
661 strcpy(buf, inet_ntoa(ip->ip_dst));
663 "Connection attempt to UDP %s:%d from %s:%d\n",
664 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
665 ntohs(uh->uh_sport));
667 udp_stat.udps_noport++;
668 if (m->m_flags & (M_BCAST | M_MCAST)) {
669 udp_stat.udps_noportbcast++;
675 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
679 ip->ip_len += iphlen;
680 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
681 return(IPPROTO_DONE);
684 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
685 ipsecstat.in_polvio++;
690 if (ipsec4_in_reject(m, inp))
692 #endif /*FAST_IPSEC*/
694 * Check the minimum TTL for socket.
696 if (ip->ip_ttl < inp->inp_ip_minttl)
700 * Construct sockaddr format source address.
701 * Stuff source address and datagram in user buffer.
703 udp_in.sin_port = uh->uh_sport;
704 udp_in.sin_addr = ip->ip_src;
705 if ((inp->inp_flags & INP_CONTROLOPTS) ||
706 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
708 if (inp->inp_vflag & INP_IPV6) {
711 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
712 savedflags = inp->inp_flags;
713 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
714 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
715 inp->inp_flags = savedflags;
718 ip_savecontrol(inp, &opts, ip, m);
720 m_adj(m, iphlen + sizeof(struct udphdr));
722 if (inp->inp_vflag & INP_IPV6) {
723 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
724 append_sa = (struct sockaddr *)&udp_in6;
727 append_sa = (struct sockaddr *)&udp_in;
729 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
730 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
731 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
732 udp_stat.udps_fullsock++;
735 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
736 sorwakeup(inp->inp_socket);
737 return(IPPROTO_DONE);
742 return(IPPROTO_DONE);
747 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
749 bzero(ip6, sizeof *ip6);
751 ip6->ip6_vfc = IPV6_VERSION;
752 ip6->ip6_plen = ip->ip_len;
753 ip6->ip6_nxt = ip->ip_p;
754 ip6->ip6_hlim = ip->ip_ttl;
755 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
757 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
758 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
763 * subroutine of udp_input(), mainly for source code readability.
764 * caller must properly init udp_ip6 and udp_in6 beforehand.
767 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off,
768 struct sockaddr_in *udp_in,
769 struct udp_in6 *udp_in6, struct udp_ip6 *udp_ip6)
771 struct sockaddr *append_sa;
772 struct mbuf *opts = NULL;
775 if (last->inp_flags & INP_CONTROLOPTS ||
776 last->inp_socket->so_options & SO_TIMESTAMP) {
778 if (last->inp_vflag & INP_IPV6) {
781 if (udp_ip6->uip6_init_done == 0) {
782 ip_2_ip6_hdr(&udp_ip6->uip6_ip6, ip);
783 udp_ip6->uip6_init_done = 1;
785 savedflags = last->inp_flags;
786 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
787 ip6_savecontrol(last, &opts, &udp_ip6->uip6_ip6, n);
788 last->inp_flags = savedflags;
791 ip_savecontrol(last, &opts, ip, n);
794 if (last->inp_vflag & INP_IPV6) {
795 if (udp_in6->uin6_init_done == 0) {
796 in6_sin_2_v4mapsin6(udp_in, &udp_in6->uin6_sin);
797 udp_in6->uin6_init_done = 1;
799 append_sa = (struct sockaddr *)&udp_in6->uin6_sin;
802 append_sa = (struct sockaddr *)udp_in;
805 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
806 ret = ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts);
807 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
812 udp_stat.udps_fullsock++;
814 sorwakeup(last->inp_socket);
819 * Notify a udp user of an asynchronous error;
820 * just wake up so that he can collect error status.
823 udp_notify(struct inpcb *inp, int error)
825 inp->inp_socket->so_error = error;
826 sorwakeup(inp->inp_socket);
827 sowwakeup(inp->inp_socket);
830 struct netmsg_udp_notify {
831 struct netmsg_base base;
832 inp_notify_t nm_notify;
833 struct in_addr nm_faddr;
838 udp_notifyall_oncpu(netmsg_t msg)
840 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
841 int nextcpu, cpu = mycpuid;
843 in_pcbnotifyall(&udbinfo[cpu], nm->nm_faddr, nm->nm_arg, nm->nm_notify);
846 if (nextcpu < ncpus2)
847 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg);
849 lwkt_replymsg(&nm->base.lmsg, 0);
853 udp_get_inpnotify(int cmd, const struct sockaddr *sa,
854 struct ip **ip0, int *cpuid)
856 struct in_addr faddr;
857 struct ip *ip = *ip0;
858 inp_notify_t notify = udp_notify;
860 faddr = ((const struct sockaddr_in *)sa)->sin_addr;
861 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
864 if (PRC_IS_REDIRECT(cmd)) {
866 notify = in_rtchange;
867 } else if (cmd == PRC_HOSTDEAD) {
869 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
875 /* Go through all CPUs */
878 const struct udphdr *uh;
880 uh = (const struct udphdr *)
881 ((caddr_t)ip + (ip->ip_hl << 2));
882 *cpuid = udp_addrcpu(faddr.s_addr, ip->ip_src.s_addr,
883 uh->uh_dport, uh->uh_sport);
892 udp_ctlinput(netmsg_t msg)
894 struct sockaddr *sa = msg->ctlinput.nm_arg;
895 struct ip *ip = msg->ctlinput.nm_extra;
896 int cmd = msg->ctlinput.nm_cmd;
898 struct in_addr faddr;
900 notify = udp_get_inpnotify(cmd, sa, &ip, NULL);
904 faddr = ((struct sockaddr_in *)sa)->sin_addr;
906 const struct udphdr *uh;
909 uh = (const struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
910 inp = in_pcblookup_hash(&udbinfo[mycpuid], faddr, uh->uh_dport,
911 ip->ip_src, uh->uh_sport, 0, NULL);
912 if (inp != NULL && inp->inp_socket != NULL)
913 notify(inp, inetctlerrmap[cmd]);
915 struct netmsg_udp_notify *nm;
917 KKASSERT(&curthread->td_msgport == netisr_cpuport(0));
918 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
919 netmsg_init(&nm->base, NULL, &netisr_afree_rport,
920 0, udp_notifyall_oncpu);
921 nm->nm_faddr = faddr;
922 nm->nm_arg = inetctlerrmap[cmd];
923 nm->nm_notify = notify;
924 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg);
927 lwkt_replymsg(&msg->lmsg, 0);
930 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, udbinfo, 0,
931 in_pcblist_global_ncpus2, "S,xinpcb", "List of active UDP sockets");
934 udp_getcred(SYSCTL_HANDLER_ARGS)
936 struct sockaddr_in addrs[2];
937 struct ucred cred0, *cred = NULL;
939 int error, cpu, origcpu;
941 error = priv_check(req->td, PRIV_ROOT);
944 error = SYSCTL_IN(req, addrs, sizeof addrs);
949 cpu = udp_addrcpu(addrs[1].sin_addr.s_addr, addrs[1].sin_port,
950 addrs[0].sin_addr.s_addr, addrs[0].sin_port);
952 lwkt_migratecpu(cpu);
954 inp = in_pcblookup_hash(&udbinfo[cpu],
955 addrs[1].sin_addr, addrs[1].sin_port,
956 addrs[0].sin_addr, addrs[0].sin_port, TRUE, NULL);
957 if (inp == NULL || inp->inp_socket == NULL) {
959 } else if (inp->inp_socket->so_cred != NULL) {
960 cred0 = *(inp->inp_socket->so_cred);
964 lwkt_migratecpu(origcpu);
969 return SYSCTL_OUT(req, cred, sizeof(struct ucred));
971 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
972 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
975 udp_send_redispatch(netmsg_t msg)
977 struct mbuf *m = msg->send.nm_m;
978 int pru_flags = msg->send.nm_flags;
979 struct inpcb *inp = msg->send.base.nm_so->so_pcb;
980 struct mbuf *m_opt = msg->send.nm_control; /* XXX save ipopt */
981 int flags = msg->send.nm_priv; /* ip_output flags */
984 logudp(redisp_ipout_beg, inp);
987 * - Don't use inp route cache. It should only be used in the
989 * - Access to inp_moptions should be safe, since multicast UDP
990 * datagrams are redispatched to netisr0 and inp_moptions is
991 * changed only in netisr0.
993 error = ip_output(m, m_opt, NULL, flags, inp->inp_moptions, inp);
994 if ((pru_flags & PRUS_NOREPLY) == 0)
995 lwkt_replymsg(&msg->send.base.lmsg, error);
998 /* Free saved ip options, if any */
1002 logudp(redisp_ipout_end, inp);
1006 udp_send(netmsg_t msg)
1008 struct socket *so = msg->send.base.nm_so;
1009 struct mbuf *m = msg->send.nm_m;
1010 struct sockaddr *dstaddr = msg->send.nm_addr;
1011 int pru_flags = msg->send.nm_flags;
1012 struct inpcb *inp = so->so_pcb;
1013 struct thread *td = msg->send.nm_td;
1016 struct udpiphdr *ui;
1017 int len = m->m_pkthdr.len;
1018 struct sockaddr_in *sin; /* really is initialized before use */
1021 KKASSERT(msg->send.nm_control == NULL);
1023 logudp(send_beg, inp);
1030 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
1035 if (inp->inp_lport == 0) { /* unbound socket */
1036 boolean_t forwarded;
1038 error = in_pcbbind(inp, NULL, td);
1043 * Need to call udp_send again, after this inpcb is
1044 * inserted into wildcard hash table.
1046 msg->send.base.lmsg.ms_flags |= MSGF_UDP_SEND;
1047 forwarded = udp_inswildcardhash(inp, &msg->send.base, 0);
1050 * The message is further forwarded, so we are
1053 logudp(send_inswildcard, inp);
1058 if (dstaddr != NULL) { /* destination address specified */
1059 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1060 /* already connected */
1064 sin = (struct sockaddr_in *)dstaddr;
1065 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
1066 error = EAFNOSUPPORT; /* IPv6 only jail */
1070 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1071 /* no destination specified and not already connected */
1079 * Calculate data length and get a mbuf
1080 * for UDP and IP headers.
1082 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
1089 * Fill in mbuf with extended UDP header
1090 * and addresses and length put into network format.
1092 ui = mtod(m, struct udpiphdr *);
1093 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
1094 ui->ui_pr = IPPROTO_UDP;
1097 * Set destination address.
1099 if (dstaddr != NULL) { /* use specified destination */
1100 ui->ui_dst = sin->sin_addr;
1101 ui->ui_dport = sin->sin_port;
1102 } else { /* use connected destination */
1103 ui->ui_dst = inp->inp_faddr;
1104 ui->ui_dport = inp->inp_fport;
1108 * Set source address.
1110 if (inp->inp_laddr.s_addr == INADDR_ANY ||
1111 IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) {
1112 struct sockaddr_in *if_sin;
1114 if (dstaddr == NULL) {
1116 * connect() had (or should have) failed because
1117 * the interface had no IP address, but the
1118 * application proceeded to call send() anyways.
1124 /* Look up outgoing interface. */
1125 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1);
1128 ui->ui_src = if_sin->sin_addr; /* use address of interface */
1130 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
1132 ui->ui_sport = inp->inp_lport;
1133 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
1136 * Release the original thread, since it is no longer used
1138 if (pru_flags & PRUS_HELDTD) {
1140 pru_flags &= ~PRUS_HELDTD;
1143 * Free the dest address, since it is no longer needed
1145 if (pru_flags & PRUS_FREEADDR) {
1146 kfree(dstaddr, M_SONAME);
1147 pru_flags &= ~PRUS_FREEADDR;
1150 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1153 * Set up checksum and output datagram.
1156 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
1157 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1158 m->m_pkthdr.csum_flags = CSUM_UDP;
1159 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1160 m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
1164 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
1165 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1166 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
1167 udp_stat.udps_opackets++;
1169 flags = IP_DEBUGROUTE |
1170 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST));
1171 if (pru_flags & PRUS_DONTROUTE)
1172 flags |= SO_DONTROUTE;
1174 if (inp->inp_flags & INP_CONNECTED) {
1176 * For connected socket, this datagram has already
1177 * been in the correct netisr; no need to rehash.
1182 cpu = udp_addrcpu(ui->ui_dst.s_addr, ui->ui_dport,
1183 ui->ui_src.s_addr, ui->ui_sport);
1184 if (cpu != mycpuid) {
1185 struct mbuf *m_opt = NULL;
1186 struct netmsg_pru_send *smsg;
1187 struct lwkt_port *port = netisr_cpuport(cpu);
1190 * Not on the CPU that matches this UDP datagram hash;
1191 * redispatch to the correct CPU to do the ip_output().
1193 if (inp->inp_options != NULL) {
1195 * If there are ip options, then save a copy,
1196 * since accessing inp_options on other CPUs'
1199 * XXX optimize this?
1201 m_opt = m_copym(inp->inp_options, 0, M_COPYALL,
1204 if ((pru_flags & PRUS_NOREPLY) == 0) {
1206 * Change some parts of the original netmsg and
1207 * forward it to the target netisr.
1209 * NOTE: so_port MUST NOT be checked in the target
1213 smsg->nm_priv = flags; /* ip_output flags */
1215 smsg->nm_control = m_opt; /* XXX save ipopt */
1216 smsg->base.lmsg.ms_flags |= MSGF_IGNSOPORT;
1217 smsg->base.nm_dispatch = udp_send_redispatch;
1218 lwkt_forwardmsg(port, &smsg->base.lmsg);
1221 * Recreate the netmsg, since the original mbuf
1222 * could have been changed. And send it to the
1225 * NOTE: so_port MUST NOT be checked in the target
1228 smsg = &m->m_hdr.mh_sndmsg;
1229 netmsg_init(&smsg->base, so, &netisr_apanic_rport,
1230 MSGF_IGNSOPORT, udp_send_redispatch);
1231 smsg->nm_priv = flags; /* ip_output flags */
1232 smsg->nm_flags = pru_flags;
1234 smsg->nm_control = m_opt; /* XXX save ipopt */
1235 lwkt_sendmsg(port, &smsg->base.lmsg);
1238 /* This UDP datagram is redispatched; done */
1239 logudp(send_redisp, inp);
1244 logudp(send_ipout, inp);
1245 error = ip_output(m, inp->inp_options, &inp->inp_route, flags,
1246 inp->inp_moptions, inp);
1253 if (pru_flags & PRUS_HELDTD)
1255 if (pru_flags & PRUS_FREEADDR)
1256 kfree(dstaddr, M_SONAME);
1257 if ((pru_flags & PRUS_NOREPLY) == 0)
1258 lwkt_replymsg(&msg->send.base.lmsg, error);
1260 logudp(send_end, inp);
1263 u_long udp_sendspace = 9216; /* really max datagram size */
1264 /* 40 1K datagrams */
1265 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
1266 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
1268 u_long udp_recvspace = 40 * (1024 +
1270 sizeof(struct sockaddr_in6)
1272 sizeof(struct sockaddr_in)
1275 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
1276 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
1279 * This should never happen, since UDP socket does not support
1280 * connection acception (SO_ACCEPTCONN, i.e. listen(2)).
1283 udp_abort(netmsg_t msg __unused)
1285 panic("udp_abort is called");
1289 udp_attach(netmsg_t msg)
1291 struct socket *so = msg->attach.base.nm_so;
1292 struct pru_attach_info *ai = msg->attach.nm_ai;
1301 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
1305 error = in_pcballoc(so, &udbinfo[mycpuid]);
1309 inp = (struct inpcb *)so->so_pcb;
1310 inp->inp_flags |= INP_DIRECT_DETACH;
1311 inp->inp_vflag |= INP_IPV4;
1312 inp->inp_ip_ttl = ip_defttl;
1315 lwkt_replymsg(&msg->attach.base.lmsg, error);
1319 udp_inswildcard_replymsg(netmsg_t msg)
1321 lwkt_msg_t lmsg = &msg->lmsg;
1323 if (lmsg->ms_flags & MSGF_UDP_SEND) {
1325 /* msg is replied by udp_send() */
1327 lwkt_replymsg(lmsg, lmsg->ms_error);
1332 udp_soreuseport_dispatch(netmsg_t msg)
1334 /* This inpcb has already been in the wildcard hash. */
1335 in_pcblink_flags(msg->base.nm_so->so_pcb, &udbinfo[mycpuid], 0);
1336 udp_inswildcard_replymsg(msg);
1340 udp_sosetport(struct lwkt_msg *msg, lwkt_port_t port)
1342 sosetport(((struct netmsg_base *)msg)->nm_so, port);
1346 udp_inswildcardhash_oncpu(struct inpcb *inp, struct netmsg_base *msg)
1350 KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid],
1351 ("not on owner cpu"));
1353 in_pcbinswildcardhash(inp);
1354 for (cpu = 0; cpu < ncpus2; ++cpu) {
1355 if (cpu == mycpuid) {
1357 * This inpcb has been inserted by the above
1358 * in_pcbinswildcardhash().
1362 in_pcbinswildcardhash_oncpu(inp, &udbinfo[cpu]);
1365 if (inp->inp_socket->so_options & SO_REUSEPORT) {
1367 * For SO_REUSEPORT socket, redistribute it based on its
1368 * local group index.
1370 cpu = inp->inp_lgrpindex & ncpus2_mask;
1371 if (cpu != mycpuid) {
1372 struct lwkt_port *port = netisr_cpuport(cpu);
1373 lwkt_msg_t lmsg = &msg->lmsg;
1376 * We are moving the protocol processing port the
1377 * socket is on, we have to unlink here and re-link
1378 * on the target cpu (this inpcb is still left in
1379 * the wildcard hash).
1381 in_pcbunlink_flags(inp, &udbinfo[mycpuid], 0);
1382 msg->nm_dispatch = udp_soreuseport_dispatch;
1385 * See the related comment in tcp_usrreq.c
1388 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1389 lwkt_forwardmsg(port, lmsg);
1390 return TRUE; /* forwarded */
1397 udp_inswildcardhash_dispatch(netmsg_t msg)
1399 struct inpcb *inp = msg->base.nm_so->so_pcb;
1400 boolean_t forwarded;
1402 KASSERT(inp->inp_lport != 0, ("local port not set yet"));
1403 KASSERT((ntohs(inp->inp_lport) & ncpus2_mask) == mycpuid,
1404 ("not target cpu"));
1406 in_pcblink(inp, &udbinfo[mycpuid]);
1408 forwarded = udp_inswildcardhash_oncpu(inp, &msg->base);
1410 /* The message is further forwarded, so we are done here. */
1413 udp_inswildcard_replymsg(msg);
1417 udp_inswildcardhash(struct inpcb *inp, struct netmsg_base *msg, int error)
1419 lwkt_msg_t lmsg = &msg->lmsg;
1422 ASSERT_INP_NOTINHASH(inp);
1424 /* This inpcb could no longer be directly detached */
1425 inp->inp_flags &= ~INP_DIRECT_DETACH;
1428 * Always clear the route cache, so we don't need to
1429 * worry about any owner CPU changes later.
1431 in_pcbresetroute(inp);
1433 KASSERT(inp->inp_lport != 0, ("local port not set yet"));
1434 cpu = ntohs(inp->inp_lport) & ncpus2_mask;
1436 lmsg->ms_error = error;
1437 if (cpu != mycpuid) {
1438 struct lwkt_port *port = netisr_cpuport(cpu);
1441 * We are moving the protocol processing port the socket
1442 * is on, we have to unlink here and re-link on the
1445 in_pcbunlink(inp, &udbinfo[mycpuid]);
1446 msg->nm_dispatch = udp_inswildcardhash_dispatch;
1448 /* See the related comment in tcp_usrreq.c tcp_connect() */
1449 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1450 lwkt_forwardmsg(port, lmsg);
1451 return TRUE; /* forwarded */
1454 return udp_inswildcardhash_oncpu(inp, msg);
1458 udp_bind(netmsg_t msg)
1460 struct socket *so = msg->bind.base.nm_so;
1466 struct sockaddr *nam = msg->bind.nm_nam;
1467 struct thread *td = msg->bind.nm_td;
1469 error = in_pcbbind(inp, nam, td);
1471 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1472 boolean_t forwarded;
1474 if (sin->sin_addr.s_addr != INADDR_ANY)
1475 inp->inp_flags |= INP_WASBOUND_NOTANY;
1477 forwarded = udp_inswildcardhash(inp,
1478 &msg->bind.base, 0);
1481 * The message is further forwarded, so
1490 lwkt_replymsg(&msg->bind.base.lmsg, error);
1494 udp_connect(netmsg_t msg)
1496 struct socket *so = msg->connect.base.nm_so;
1497 struct sockaddr *nam = msg->connect.nm_nam;
1498 struct thread *td = msg->connect.nm_td;
1500 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1501 struct sockaddr_in *if_sin;
1502 struct lwkt_port *port;
1505 KKASSERT(msg->connect.nm_m == NULL);
1513 if (msg->connect.nm_flags & PRUC_RECONNECT) {
1514 msg->connect.nm_flags &= ~PRUC_RECONNECT;
1515 in_pcblink(inp, &udbinfo[mycpuid]);
1518 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1525 * Bind if we have to
1527 if (inp->inp_lport == 0) {
1528 error = in_pcbbind(inp, NULL, td);
1534 * Calculate the correct protocol processing thread. The connect
1535 * operation must run there.
1537 error = in_pcbladdr(inp, nam, &if_sin, td);
1540 if (!prison_remote_ip(td, nam)) {
1541 error = EAFNOSUPPORT; /* IPv6 only jail */
1545 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1546 inp->inp_laddr.s_addr != INADDR_ANY ?
1547 inp->inp_laddr.s_addr : if_sin->sin_addr.s_addr, inp->inp_lport);
1548 if (port != &curthread->td_msgport) {
1549 lwkt_msg_t lmsg = &msg->connect.base.lmsg;
1550 int nm_flags = PRUC_RECONNECT;
1553 * in_pcbladdr() may have allocated a route entry for us
1554 * on the current CPU, but we need a route entry on the
1555 * inpcb's owner CPU, so free it here.
1557 in_pcbresetroute(inp);
1559 if (inp->inp_flags & INP_WILDCARD) {
1561 * Remove this inpcb from the wildcard hash before
1562 * the socket's msgport changes.
1564 udp_remwildcardhash(inp);
1568 * We are moving the protocol processing port the socket
1569 * is on, we have to unlink here and re-link on the
1572 in_pcbunlink(inp, &udbinfo[mycpuid]);
1573 msg->connect.nm_flags |= nm_flags;
1575 /* See the related comment in tcp_usrreq.c tcp_connect() */
1576 lwkt_setmsg_receipt(lmsg, udp_sosetport);
1577 lwkt_forwardmsg(port, lmsg);
1578 /* msg invalid now */
1581 error = udp_connect_oncpu(inp, sin, if_sin);
1583 if (error && inp != NULL && inp->inp_lport != 0 &&
1584 (inp->inp_flags & INP_WILDCARD) == 0) {
1585 boolean_t forwarded;
1587 /* Connect failed; put it to wildcard hash. */
1588 forwarded = udp_inswildcardhash(inp, &msg->connect.base,
1592 * The message is further forwarded, so we are done
1598 lwkt_replymsg(&msg->connect.base.lmsg, error);
1602 udp_remwildcardhash(struct inpcb *inp)
1606 KASSERT(inp->inp_pcbinfo == &udbinfo[mycpuid],
1607 ("not on owner cpu"));
1609 for (cpu = 0; cpu < ncpus2; ++cpu) {
1610 if (cpu == mycpuid) {
1612 * This inpcb will be removed by the later
1613 * in_pcbremwildcardhash().
1617 in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpu]);
1619 in_pcbremwildcardhash(inp);
1623 udp_connect_oncpu(struct inpcb *inp, struct sockaddr_in *sin,
1624 struct sockaddr_in *if_sin)
1626 struct socket *so = inp->inp_socket;
1629 oinp = in_pcblookup_hash(inp->inp_pcbinfo,
1630 sin->sin_addr, sin->sin_port,
1631 inp->inp_laddr.s_addr != INADDR_ANY ?
1632 inp->inp_laddr : if_sin->sin_addr, inp->inp_lport, FALSE, NULL);
1637 * No more errors can occur, finish adjusting the socket
1638 * and change the processing port to reflect the connected
1639 * socket. Once set we can no longer safely mess with the
1643 if (inp->inp_flags & INP_WILDCARD)
1644 udp_remwildcardhash(inp);
1646 if (inp->inp_laddr.s_addr == INADDR_ANY)
1647 inp->inp_laddr = if_sin->sin_addr;
1648 inp->inp_faddr = sin->sin_addr;
1649 inp->inp_fport = sin->sin_port;
1650 in_pcbinsconnhash(inp);
1658 udp_detach2(struct socket *so)
1660 in_pcbdetach(so->so_pcb);
1666 udp_detach_final_dispatch(netmsg_t msg)
1668 udp_detach2(msg->base.nm_so);
1672 udp_detach_oncpu_dispatch(netmsg_t msg)
1674 struct netmsg_base *clomsg = &msg->base;
1675 struct socket *so = clomsg->nm_so;
1676 struct inpcb *inp = so->so_pcb;
1677 struct thread *td = curthread;
1678 int nextcpu, cpuid = mycpuid;
1680 KASSERT(td->td_type == TD_TYPE_NETISR, ("not in netisr"));
1682 if (inp->inp_flags & INP_WILDCARD) {
1684 * This inp will be removed on the inp's
1685 * owner CPU later, so don't do it now.
1687 if (&td->td_msgport != so->so_port)
1688 in_pcbremwildcardhash_oncpu(inp, &udbinfo[cpuid]);
1693 * Free and clear multicast socket option,
1694 * which is only accessed in netisr0.
1696 ip_freemoptions(inp->inp_moptions);
1697 inp->inp_moptions = NULL;
1700 nextcpu = cpuid + 1;
1701 if (nextcpu < ncpus2) {
1702 lwkt_forwardmsg(netisr_cpuport(nextcpu), &clomsg->lmsg);
1705 * No one could see this inpcb now; destroy this
1706 * inpcb in its owner netisr.
1708 netmsg_init(clomsg, so, &netisr_apanic_rport, 0,
1709 udp_detach_final_dispatch);
1710 lwkt_sendmsg(so->so_port, &clomsg->lmsg);
1715 udp_detach(netmsg_t msg)
1717 struct socket *so = msg->detach.base.nm_so;
1718 struct netmsg_base *clomsg;
1723 lwkt_replymsg(&msg->detach.base.lmsg, EINVAL);
1728 * Reply EJUSTRETURN ASAP, we will call sodiscard() and
1731 lwkt_replymsg(&msg->detach.base.lmsg, EJUSTRETURN);
1734 /* Only one CPU, detach the inpcb directly. */
1740 * Remove this inpcb from the inpcb list first, so that
1741 * no one could find this inpcb from the inpcb list.
1745 if (inp->inp_flags & INP_DIRECT_DETACH) {
1747 * Direct detaching is allowed
1749 KASSERT((inp->inp_flags & INP_WILDCARD) == 0,
1750 ("in the wildcardhash"));
1751 KASSERT(inp->inp_moptions == NULL, ("has mcast options"));
1757 * Go through netisrs which process UDP to make sure
1758 * no one could find this inpcb anymore.
1760 clomsg = &so->so_clomsg;
1761 netmsg_init(clomsg, so, &netisr_apanic_rport, MSGF_IGNSOPORT,
1762 udp_detach_oncpu_dispatch);
1763 lwkt_sendmsg(netisr_cpuport(0), &clomsg->lmsg);
1767 udp_disconnect(netmsg_t msg)
1769 struct socket *so = msg->disconnect.base.nm_so;
1771 boolean_t forwarded;
1779 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1784 soclrstate(so, SS_ISCONNECTED); /* XXX */
1786 in_pcbdisconnect(inp);
1789 * Follow traditional BSD behavior and retain the local port
1790 * binding. But, fix the old misbehavior of overwriting any
1791 * previously bound local address.
1793 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1794 inp->inp_laddr.s_addr = INADDR_ANY;
1796 if (so->so_state & SS_ISCLOSING) {
1798 * If this socket is being closed, there is no need
1799 * to put this socket back into wildcard hash table.
1805 forwarded = udp_inswildcardhash(inp, &msg->disconnect.base, 0);
1808 * The message is further forwarded, so we are done
1814 lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1818 udp_shutdown(netmsg_t msg)
1820 struct socket *so = msg->shutdown.base.nm_so;
1831 lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1834 struct pr_usrreqs udp_usrreqs = {
1835 .pru_abort = udp_abort,
1836 .pru_accept = pr_generic_notsupp,
1837 .pru_attach = udp_attach,
1838 .pru_bind = udp_bind,
1839 .pru_connect = udp_connect,
1840 .pru_connect2 = pr_generic_notsupp,
1841 .pru_control = in_control_dispatch,
1842 .pru_detach = udp_detach,
1843 .pru_disconnect = udp_disconnect,
1844 .pru_listen = pr_generic_notsupp,
1845 .pru_peeraddr = in_setpeeraddr_dispatch,
1846 .pru_rcvd = pr_generic_notsupp,
1847 .pru_rcvoob = pr_generic_notsupp,
1848 .pru_send = udp_send,
1849 .pru_sense = pru_sense_null,
1850 .pru_shutdown = udp_shutdown,
1851 .pru_sockaddr = in_setsockaddr_dispatch,
1852 .pru_sosend = sosendudp,
1853 .pru_soreceive = soreceive