xref: /dragonfly/sys/netinet/udp_usrreq.c (revision 52e9aa73)
1 /*
2  * Copyright (c) 2004 Jeffrey M. Hsu.  All rights reserved.
3  * Copyright (c) 2004 The DragonFly Project.  All rights reserved.
4  *
5  * This code is derived from software contributed to The DragonFly Project
6  * by Jeffrey M. Hsu.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
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.
19  *
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
31  * SUCH DAMAGE.
32  */
33 
34 /*
35  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
36  *	The Regents of the University of California.  All rights reserved.
37  *
38  * Redistribution and use in source and binary forms, with or without
39  * modification, are permitted provided that the following conditions
40  * are met:
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. All advertising materials mentioning features or use of this software
47  *    must display the following acknowledgement:
48  *	This product includes software developed by the University of
49  *	California, Berkeley and its contributors.
50  * 4. Neither the name of the University nor the names of its contributors
51  *    may be used to endorse or promote products derived from this software
52  *    without specific prior written permission.
53  *
54  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
55  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
56  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
57  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
58  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
59  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
60  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
61  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
63  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64  * SUCH DAMAGE.
65  *
66  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
67  * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
68  */
69 
70 #include "opt_ipsec.h"
71 #include "opt_inet6.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/kernel.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/domain.h>
79 #include <sys/proc.h>
80 #include <sys/priv.h>
81 #include <sys/protosw.h>
82 #include <sys/socket.h>
83 #include <sys/socketvar.h>
84 #include <sys/sysctl.h>
85 #include <sys/syslog.h>
86 #include <sys/in_cksum.h>
87 
88 #include <sys/thread2.h>
89 #include <sys/socketvar2.h>
90 #include <sys/serialize.h>
91 
92 #include <machine/stdarg.h>
93 
94 #include <net/if.h>
95 #include <net/route.h>
96 #include <net/netmsg2.h>
97 
98 #include <netinet/in.h>
99 #include <netinet/in_systm.h>
100 #include <netinet/ip.h>
101 #ifdef INET6
102 #include <netinet/ip6.h>
103 #endif
104 #include <netinet/in_pcb.h>
105 #include <netinet/in_var.h>
106 #include <netinet/ip_var.h>
107 #ifdef INET6
108 #include <netinet6/ip6_var.h>
109 #endif
110 #include <netinet/ip_icmp.h>
111 #include <netinet/icmp_var.h>
112 #include <netinet/udp.h>
113 #include <netinet/udp_var.h>
114 
115 #ifdef FAST_IPSEC
116 #include <netproto/ipsec/ipsec.h>
117 #endif
118 
119 #ifdef IPSEC
120 #include <netinet6/ipsec.h>
121 #endif
122 
123 /*
124  * UDP protocol implementation.
125  * Per RFC 768, August, 1980.
126  */
127 #ifndef	COMPAT_42
128 static int	udpcksum = 1;
129 #else
130 static int	udpcksum = 0;		/* XXX */
131 #endif
132 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
133     &udpcksum, 0, "Enable checksumming of UDP packets");
134 
135 int	log_in_vain = 0;
136 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
137     &log_in_vain, 0, "Log all incoming UDP packets");
138 
139 static int	blackhole = 0;
140 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
141 	&blackhole, 0, "Do not send port unreachables for refused connects");
142 
143 static int	strict_mcast_mship = 1;
144 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
145 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
146 
147 int	udp_sosend_async = 1;
148 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW,
149 	&udp_sosend_async, 0, "UDP asynchronized pru_send");
150 
151 int	udp_sosend_prepend = 1;
152 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW,
153 	&udp_sosend_prepend, 0,
154 	"Prepend enough space for proto and link header in pru_send");
155 
156 struct	inpcbinfo udbinfo;
157 
158 static struct netisr_barrier *udbinfo_br;
159 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER;
160 
161 #ifndef UDBHASHSIZE
162 #define UDBHASHSIZE 16
163 #endif
164 
165 struct	udpstat udpstat;	/* from udp_var.h */
166 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
167     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
168 
169 static struct	sockaddr_in udp_in = { sizeof udp_in, AF_INET };
170 #ifdef INET6
171 struct udp_in6 {
172 	struct sockaddr_in6	uin6_sin;
173 	u_char			uin6_init_done : 1;
174 } udp_in6 = {
175 	{ sizeof udp_in6.uin6_sin, AF_INET6 },
176 	0
177 };
178 struct udp_ip6 {
179 	struct ip6_hdr		uip6_ip6;
180 	u_char			uip6_init_done : 1;
181 } udp_ip6;
182 #endif /* INET6 */
183 
184 static void udp_append (struct inpcb *last, struct ip *ip,
185 			    struct mbuf *n, int off);
186 #ifdef INET6
187 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
188 #endif
189 
190 static int udp_connect_oncpu(struct socket *so, struct thread *td,
191 			struct sockaddr_in *sin, struct sockaddr_in *if_sin);
192 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
193 			struct thread *, int);
194 
195 void
196 udp_init(void)
197 {
198 	in_pcbinfo_init(&udbinfo);
199 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
200 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
201 					&udbinfo.porthashmask);
202 	udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
203 					    &udbinfo.wildcardhashmask);
204 	udbinfo.ipi_size = sizeof(struct inpcb);
205 
206 	udbinfo_br = netisr_barrier_create();
207 }
208 
209 /*
210  * Check multicast packets to make sure they are only sent to sockets with
211  * multicast memberships for the packet's destination address and arrival
212  * interface.  Multicast packets to multicast-unaware sockets are also
213  * disallowed.
214  *
215  * Returns 0 if the packet is acceptable, -1 if it is not.
216  */
217 static __inline int
218 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
219 {
220 	int mshipno;
221 	struct ip_moptions *mopt;
222 
223 	if (strict_mcast_mship == 0 ||
224 	    !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
225 		return (0);
226 	}
227 	mopt = inp->inp_moptions;
228 	if (mopt == NULL)
229 		return (-1);
230 	for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) {
231 		struct in_multi *maddr = mopt->imo_membership[mshipno];
232 
233 		if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
234 		    m->m_pkthdr.rcvif == maddr->inm_ifp) {
235 			return (0);
236 		}
237 	}
238 	return (-1);
239 }
240 
241 int
242 udp_input(struct mbuf **mp, int *offp, int proto)
243 {
244 	int iphlen;
245 	struct ip *ip;
246 	struct udphdr *uh;
247 	struct inpcb *inp;
248 	struct mbuf *m;
249 	struct mbuf *opts = NULL;
250 	int len, off;
251 	struct ip save_ip;
252 	struct sockaddr *append_sa;
253 
254 	off = *offp;
255 	m = *mp;
256 	*mp = NULL;
257 
258 	iphlen = off;
259 	udpstat.udps_ipackets++;
260 
261 	/*
262 	 * Strip IP options, if any; should skip this,
263 	 * make available to user, and use on returned packets,
264 	 * but we don't yet have a way to check the checksum
265 	 * with options still present.
266 	 */
267 	if (iphlen > sizeof(struct ip)) {
268 		ip_stripoptions(m);
269 		iphlen = sizeof(struct ip);
270 	}
271 
272 	/*
273 	 * IP and UDP headers are together in first mbuf.
274 	 * Already checked and pulled up in ip_demux().
275 	 */
276 	KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
277 	    ("UDP header not in one mbuf"));
278 
279 	ip = mtod(m, struct ip *);
280 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
281 
282 	/* destination port of 0 is illegal, based on RFC768. */
283 	if (uh->uh_dport == 0)
284 		goto bad;
285 
286 	/*
287 	 * Make mbuf data length reflect UDP length.
288 	 * If not enough data to reflect UDP length, drop.
289 	 */
290 	len = ntohs((u_short)uh->uh_ulen);
291 	if (ip->ip_len != len) {
292 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
293 			udpstat.udps_badlen++;
294 			goto bad;
295 		}
296 		m_adj(m, len - ip->ip_len);
297 		/* ip->ip_len = len; */
298 	}
299 	/*
300 	 * Save a copy of the IP header in case we want restore it
301 	 * for sending an ICMP error message in response.
302 	 */
303 	save_ip = *ip;
304 
305 	/*
306 	 * Checksum extended UDP header and data.
307 	 */
308 	if (uh->uh_sum) {
309 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
310 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
311 				uh->uh_sum = m->m_pkthdr.csum_data;
312 			else
313 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
314 				    ip->ip_dst.s_addr, htonl((u_short)len +
315 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
316 			uh->uh_sum ^= 0xffff;
317 		} else {
318 			char b[9];
319 
320 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
321 			bzero(((struct ipovly *)ip)->ih_x1, 9);
322 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
323 			uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
324 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
325 		}
326 		if (uh->uh_sum) {
327 			udpstat.udps_badsum++;
328 			m_freem(m);
329 			return(IPPROTO_DONE);
330 		}
331 	} else
332 		udpstat.udps_nosum++;
333 
334 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
335 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
336 		struct inpcb *last;
337 
338 		/*
339 		 * Deliver a multicast or broadcast datagram to *all* sockets
340 		 * for which the local and remote addresses and ports match
341 		 * those of the incoming datagram.  This allows more than
342 		 * one process to receive multi/broadcasts on the same port.
343 		 * (This really ought to be done for unicast datagrams as
344 		 * well, but that would cause problems with existing
345 		 * applications that open both address-specific sockets and
346 		 * a wildcard socket listening to the same port -- they would
347 		 * end up receiving duplicates of every unicast datagram.
348 		 * Those applications open the multiple sockets to overcome an
349 		 * inadequacy of the UDP socket interface, but for backwards
350 		 * compatibility we avoid the problem here rather than
351 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
352 		 */
353 
354 		/*
355 		 * Construct sockaddr format source address.
356 		 */
357 		udp_in.sin_port = uh->uh_sport;
358 		udp_in.sin_addr = ip->ip_src;
359 		/*
360 		 * Locate pcb(s) for datagram.
361 		 * (Algorithm copied from raw_intr().)
362 		 */
363 		last = NULL;
364 #ifdef INET6
365 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
366 #endif
367 		LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
368 			KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0);
369 #ifdef INET6
370 			if (!(inp->inp_vflag & INP_IPV4))
371 				continue;
372 #endif
373 			if (inp->inp_lport != uh->uh_dport)
374 				continue;
375 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
376 				if (inp->inp_laddr.s_addr !=
377 				    ip->ip_dst.s_addr)
378 					continue;
379 			}
380 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
381 				if (inp->inp_faddr.s_addr !=
382 				    ip->ip_src.s_addr ||
383 				    inp->inp_fport != uh->uh_sport)
384 					continue;
385 			}
386 
387 			if (check_multicast_membership(ip, inp, m) < 0)
388 				continue;
389 
390 			if (last != NULL) {
391 				struct mbuf *n;
392 
393 #ifdef IPSEC
394 				/* check AH/ESP integrity. */
395 				if (ipsec4_in_reject_so(m, last->inp_socket))
396 					ipsecstat.in_polvio++;
397 					/* do not inject data to pcb */
398 				else
399 #endif /*IPSEC*/
400 #ifdef FAST_IPSEC
401 				/* check AH/ESP integrity. */
402 				if (ipsec4_in_reject(m, last))
403 					;
404 				else
405 #endif /*FAST_IPSEC*/
406 				if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
407 					udp_append(last, ip, n,
408 						   iphlen +
409 						   sizeof(struct udphdr));
410 			}
411 			last = inp;
412 			/*
413 			 * Don't look for additional matches if this one does
414 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
415 			 * socket options set.  This heuristic avoids searching
416 			 * through all pcbs in the common case of a non-shared
417 			 * port.  It * assumes that an application will never
418 			 * clear these options after setting them.
419 			 */
420 			if (!(last->inp_socket->so_options &
421 			    (SO_REUSEPORT | SO_REUSEADDR)))
422 				break;
423 		}
424 
425 		if (last == NULL) {
426 			/*
427 			 * No matching pcb found; discard datagram.
428 			 * (No need to send an ICMP Port Unreachable
429 			 * for a broadcast or multicast datgram.)
430 			 */
431 			udpstat.udps_noportbcast++;
432 			goto bad;
433 		}
434 #ifdef IPSEC
435 		/* check AH/ESP integrity. */
436 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
437 			ipsecstat.in_polvio++;
438 			goto bad;
439 		}
440 #endif /*IPSEC*/
441 #ifdef FAST_IPSEC
442 		/* check AH/ESP integrity. */
443 		if (ipsec4_in_reject(m, last))
444 			goto bad;
445 #endif /*FAST_IPSEC*/
446 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
447 		return(IPPROTO_DONE);
448 	}
449 	/*
450 	 * Locate pcb for datagram.
451 	 */
452 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
453 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
454 	if (inp == NULL) {
455 		if (log_in_vain) {
456 			char buf[sizeof "aaa.bbb.ccc.ddd"];
457 
458 			strcpy(buf, inet_ntoa(ip->ip_dst));
459 			log(LOG_INFO,
460 			    "Connection attempt to UDP %s:%d from %s:%d\n",
461 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
462 			    ntohs(uh->uh_sport));
463 		}
464 		udpstat.udps_noport++;
465 		if (m->m_flags & (M_BCAST | M_MCAST)) {
466 			udpstat.udps_noportbcast++;
467 			goto bad;
468 		}
469 		if (blackhole)
470 			goto bad;
471 #ifdef ICMP_BANDLIM
472 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
473 			goto bad;
474 #endif
475 		*ip = save_ip;
476 		ip->ip_len += iphlen;
477 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
478 		return(IPPROTO_DONE);
479 	}
480 #ifdef IPSEC
481 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
482 		ipsecstat.in_polvio++;
483 		goto bad;
484 	}
485 #endif /*IPSEC*/
486 #ifdef FAST_IPSEC
487 	if (ipsec4_in_reject(m, inp))
488 		goto bad;
489 #endif /*FAST_IPSEC*/
490 	/*
491 	 * Check the minimum TTL for socket.
492 	 */
493 	if (ip->ip_ttl < inp->inp_ip_minttl)
494 		goto bad;
495 
496 	/*
497 	 * Construct sockaddr format source address.
498 	 * Stuff source address and datagram in user buffer.
499 	 */
500 	udp_in.sin_port = uh->uh_sport;
501 	udp_in.sin_addr = ip->ip_src;
502 	if ((inp->inp_flags & INP_CONTROLOPTS) ||
503 	    (inp->inp_socket->so_options & SO_TIMESTAMP)) {
504 #ifdef INET6
505 		if (inp->inp_vflag & INP_IPV6) {
506 			int savedflags;
507 
508 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
509 			savedflags = inp->inp_flags;
510 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
511 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
512 			inp->inp_flags = savedflags;
513 		} else
514 #endif
515 		ip_savecontrol(inp, &opts, ip, m);
516 	}
517 	m_adj(m, iphlen + sizeof(struct udphdr));
518 #ifdef INET6
519 	if (inp->inp_vflag & INP_IPV6) {
520 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
521 		append_sa = (struct sockaddr *)&udp_in6;
522 	} else
523 #endif
524 		append_sa = (struct sockaddr *)&udp_in;
525 
526 	lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token);
527 	if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
528 		udpstat.udps_fullsock++;
529 		lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
530 		goto bad;
531 	}
532 	lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token);
533 	sorwakeup(inp->inp_socket);
534 	return(IPPROTO_DONE);
535 bad:
536 	m_freem(m);
537 	if (opts)
538 		m_freem(opts);
539 	return(IPPROTO_DONE);
540 }
541 
542 #ifdef INET6
543 static void
544 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
545 {
546 	bzero(ip6, sizeof *ip6);
547 
548 	ip6->ip6_vfc = IPV6_VERSION;
549 	ip6->ip6_plen = ip->ip_len;
550 	ip6->ip6_nxt = ip->ip_p;
551 	ip6->ip6_hlim = ip->ip_ttl;
552 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
553 		IPV6_ADDR_INT32_SMP;
554 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
555 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
556 }
557 #endif
558 
559 /*
560  * subroutine of udp_input(), mainly for source code readability.
561  * caller must properly init udp_ip6 and udp_in6 beforehand.
562  */
563 static void
564 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
565 {
566 	struct sockaddr *append_sa;
567 	struct mbuf *opts = NULL;
568 
569 	if (last->inp_flags & INP_CONTROLOPTS ||
570 	    last->inp_socket->so_options & SO_TIMESTAMP) {
571 #ifdef INET6
572 		if (last->inp_vflag & INP_IPV6) {
573 			int savedflags;
574 
575 			if (udp_ip6.uip6_init_done == 0) {
576 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
577 				udp_ip6.uip6_init_done = 1;
578 			}
579 			savedflags = last->inp_flags;
580 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
581 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
582 			last->inp_flags = savedflags;
583 		} else
584 #endif
585 		ip_savecontrol(last, &opts, ip, n);
586 	}
587 #ifdef INET6
588 	if (last->inp_vflag & INP_IPV6) {
589 		if (udp_in6.uin6_init_done == 0) {
590 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
591 			udp_in6.uin6_init_done = 1;
592 		}
593 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
594 	} else
595 #endif
596 		append_sa = (struct sockaddr *)&udp_in;
597 	m_adj(n, off);
598 	lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token);
599 	if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
600 		m_freem(n);
601 		if (opts)
602 			m_freem(opts);
603 		udpstat.udps_fullsock++;
604 	} else {
605 		sorwakeup(last->inp_socket);
606 	}
607 	lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token);
608 }
609 
610 /*
611  * Notify a udp user of an asynchronous error;
612  * just wake up so that he can collect error status.
613  */
614 void
615 udp_notify(struct inpcb *inp, int error)
616 {
617 	inp->inp_socket->so_error = error;
618 	sorwakeup(inp->inp_socket);
619 	sowwakeup(inp->inp_socket);
620 }
621 
622 struct netmsg_udp_notify {
623 	struct netmsg_base base;
624 	void		(*nm_notify)(struct inpcb *, int);
625 	struct in_addr	nm_faddr;
626 	int		nm_arg;
627 };
628 
629 static void
630 udp_notifyall_oncpu(netmsg_t msg)
631 {
632 	struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg;
633 #if 0
634 	int nextcpu;
635 #endif
636 
637 	in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr,
638 			nm->nm_arg, nm->nm_notify);
639 	lwkt_replymsg(&nm->base.lmsg, 0);
640 
641 #if 0
642 	/* XXX currently udp only runs on cpu 0 */
643 	nextcpu = mycpuid + 1;
644 	if (nextcpu < ncpus2)
645 		lwkt_forwardmsg(netisr_portfn(nextcpu), &nm->base.lmsg);
646 	else
647 		lwkt_replymsg(&nmsg->base.lmsg, 0);
648 #endif
649 }
650 
651 static void
652 udp_rtchange(struct inpcb *inp, int err)
653 {
654 	/* XXX Nuke this, once UDP inpcbs are CPU localized */
655 	if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
656 		rtfree(inp->inp_route.ro_rt);
657 		inp->inp_route.ro_rt = NULL;
658 		/*
659 		 * A new route can be allocated the next time
660 		 * output is attempted.
661 		 */
662 	}
663 }
664 
665 void
666 udp_ctlinput(netmsg_t msg)
667 {
668 	struct sockaddr *sa = msg->ctlinput.nm_arg;
669 	struct ip *ip = msg->ctlinput.nm_extra;
670 	int cmd = msg->ctlinput.nm_cmd;
671 	struct udphdr *uh;
672 	void (*notify) (struct inpcb *, int) = udp_notify;
673 	struct in_addr faddr;
674 	struct inpcb *inp;
675 
676 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
677 
678 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
679 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
680 		goto done;
681 
682 	if (PRC_IS_REDIRECT(cmd)) {
683 		ip = NULL;
684 		notify = udp_rtchange;
685 	} else if (cmd == PRC_HOSTDEAD) {
686 		ip = NULL;
687 	} else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) {
688 		goto done;
689 	}
690 
691 	if (ip) {
692 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
693 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
694 					ip->ip_src, uh->uh_sport, 0, NULL);
695 		if (inp != NULL && inp->inp_socket != NULL)
696 			(*notify)(inp, inetctlerrmap[cmd]);
697 	} else if (PRC_IS_REDIRECT(cmd)) {
698 		struct netmsg_udp_notify *nm;
699 
700 		KKASSERT(&curthread->td_msgport == netisr_portfn(0));
701 		nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT);
702 		netmsg_init(&nm->base, NULL, &netisr_afree_rport,
703 			    0, udp_notifyall_oncpu);
704 		nm->nm_faddr = faddr;
705 		nm->nm_arg = inetctlerrmap[cmd];
706 		nm->nm_notify = notify;
707 		lwkt_sendmsg(netisr_portfn(0), &nm->base.lmsg);
708 	} else {
709 		/*
710 		 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
711 		 * once UDP inpcbs are CPU localized
712 		 */
713 		KKASSERT(&curthread->td_msgport == netisr_portfn(0));
714 		in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
715 				notify);
716 	}
717 done:
718 	lwkt_replymsg(&msg->lmsg, 0);
719 }
720 
721 static int
722 udp_pcblist(SYSCTL_HANDLER_ARGS)
723 {
724 	struct xinpcb *xi;
725 	int error, nxi, i;
726 
727 	udbinfo_lock();
728 	error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi);
729 	udbinfo_unlock();
730 
731 	if (error) {
732 		KKASSERT(xi == NULL);
733 		return error;
734 	}
735 	if (nxi == 0) {
736 		KKASSERT(xi == NULL);
737 		return 0;
738 	}
739 
740 	for (i = 0; i < nxi; ++i) {
741 		error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i]));
742 		if (error)
743 			break;
744 	}
745 	kfree(xi, M_TEMP);
746 
747 	return error;
748 }
749 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
750 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
751 
752 static int
753 udp_getcred(SYSCTL_HANDLER_ARGS)
754 {
755 	struct sockaddr_in addrs[2];
756 	struct ucred cred0, *cred = NULL;
757 	struct inpcb *inp;
758 	int error;
759 
760 	error = priv_check(req->td, PRIV_ROOT);
761 	if (error)
762 		return (error);
763 	error = SYSCTL_IN(req, addrs, sizeof addrs);
764 	if (error)
765 		return (error);
766 
767 	udbinfo_lock();
768 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
769 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
770 	if (inp == NULL || inp->inp_socket == NULL) {
771 		error = ENOENT;
772 	} else {
773 		if (inp->inp_socket->so_cred != NULL) {
774 			cred0 = *(inp->inp_socket->so_cred);
775 			cred = &cred0;
776 		}
777 	}
778 	udbinfo_unlock();
779 
780 	if (error)
781 		return error;
782 
783 	return SYSCTL_OUT(req, cred, sizeof(struct ucred));
784 }
785 
786 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
787     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
788 
789 static int
790 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
791 	   struct thread *td, int flags)
792 {
793 	struct udpiphdr *ui;
794 	int len = m->m_pkthdr.len;
795 	struct sockaddr_in *sin;	/* really is initialized before use */
796 	int error = 0, lport_any = 0;
797 
798 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
799 		error = EMSGSIZE;
800 		goto release;
801 	}
802 
803 	if (inp->inp_lport == 0) {	/* unbound socket */
804 		error = in_pcbbind(inp, NULL, td);
805 		if (error)
806 			goto release;
807 
808 		udbinfo_barrier_set();
809 		in_pcbinswildcardhash(inp);
810 		udbinfo_barrier_rem();
811 		lport_any = 1;
812 	}
813 
814 	if (dstaddr != NULL) {		/* destination address specified */
815 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
816 			/* already connected */
817 			error = EISCONN;
818 			goto release;
819 		}
820 		sin = (struct sockaddr_in *)dstaddr;
821 		if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
822 			error = EAFNOSUPPORT; /* IPv6 only jail */
823 			goto release;
824 		}
825 	} else {
826 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
827 			/* no destination specified and not already connected */
828 			error = ENOTCONN;
829 			goto release;
830 		}
831 		sin = NULL;
832 	}
833 
834 	/*
835 	 * Calculate data length and get a mbuf
836 	 * for UDP and IP headers.
837 	 */
838 	M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
839 	if (m == NULL) {
840 		error = ENOBUFS;
841 		goto release;
842 	}
843 
844 	/*
845 	 * Fill in mbuf with extended UDP header
846 	 * and addresses and length put into network format.
847 	 */
848 	ui = mtod(m, struct udpiphdr *);
849 	bzero(ui->ui_x1, sizeof ui->ui_x1);	/* XXX still needed? */
850 	ui->ui_pr = IPPROTO_UDP;
851 
852 	/*
853 	 * Set destination address.
854 	 */
855 	if (dstaddr != NULL) {			/* use specified destination */
856 		ui->ui_dst = sin->sin_addr;
857 		ui->ui_dport = sin->sin_port;
858 	} else {				/* use connected destination */
859 		ui->ui_dst = inp->inp_faddr;
860 		ui->ui_dport = inp->inp_fport;
861 	}
862 
863 	/*
864 	 * Set source address.
865 	 */
866 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
867 		struct sockaddr_in *if_sin;
868 
869 		if (dstaddr == NULL) {
870 			/*
871 			 * connect() had (or should have) failed because
872 			 * the interface had no IP address, but the
873 			 * application proceeded to call send() anyways.
874 			 */
875 			error = ENOTCONN;
876 			goto release;
877 		}
878 
879 		/* Look up outgoing interface. */
880 		if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
881 			goto release;
882 		ui->ui_src = if_sin->sin_addr;	/* use address of interface */
883 	} else {
884 		ui->ui_src = inp->inp_laddr;	/* use non-null bound address */
885 	}
886 	ui->ui_sport = inp->inp_lport;
887 	KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
888 
889 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
890 
891 	/*
892 	 * Set up checksum and output datagram.
893 	 */
894 	if (udpcksum) {
895 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
896 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
897 		m->m_pkthdr.csum_flags = CSUM_UDP;
898 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
899 		m->m_pkthdr.csum_thlen = sizeof(struct udphdr);
900 	} else {
901 		ui->ui_sum = 0;
902 	}
903 	((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
904 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
905 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
906 	udpstat.udps_opackets++;
907 
908 	error = ip_output(m, inp->inp_options, &inp->inp_route,
909 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
910 	    flags | IP_DEBUGROUTE,
911 	    inp->inp_moptions, inp);
912 
913 	/*
914 	 * If this is the first data gram sent on an unbound and unconnected
915 	 * UDP socket, lport will be changed in this function.  If target
916 	 * CPU after this lport changing is no longer the current CPU, then
917 	 * free the route entry allocated on the current CPU.
918 	 */
919 	if (lport_any) {
920 		if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
921 		    inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
922 #ifdef notyet
923 			struct route *ro = &inp->inp_route;
924 
925 			if (ro->ro_rt != NULL)
926 				RTFREE(ro->ro_rt);
927 			bzero(ro, sizeof(*ro));
928 #else
929 			panic("UDP activity should only be in netisr0");
930 #endif
931 		}
932 	}
933 	return (error);
934 
935 release:
936 	m_freem(m);
937 	return (error);
938 }
939 
940 u_long	udp_sendspace = 9216;		/* really max datagram size */
941 					/* 40 1K datagrams */
942 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
943     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
944 
945 u_long	udp_recvspace = 40 * (1024 +
946 #ifdef INET6
947 				      sizeof(struct sockaddr_in6)
948 #else
949 				      sizeof(struct sockaddr_in)
950 #endif
951 				      );
952 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
953     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
954 
955 /*
956  * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort()
957  *	 will sofree() it when we return.
958  */
959 static void
960 udp_abort(netmsg_t msg)
961 {
962 	struct socket *so = msg->abort.base.nm_so;
963 	struct inpcb *inp;
964 	int error;
965 
966 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
967 
968 	inp = so->so_pcb;
969 	if (inp) {
970 		soisdisconnected(so);
971 
972 		udbinfo_barrier_set();
973 		in_pcbdetach(inp);
974 		udbinfo_barrier_rem();
975 		error = 0;
976 	} else {
977 		error = EINVAL;
978 	}
979 	lwkt_replymsg(&msg->abort.base.lmsg, error);
980 }
981 
982 static void
983 udp_attach(netmsg_t msg)
984 {
985 	struct socket *so = msg->attach.base.nm_so;
986 	struct pru_attach_info *ai = msg->attach.nm_ai;
987 	struct inpcb *inp;
988 	int error;
989 
990 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
991 
992 	inp = so->so_pcb;
993 	if (inp != NULL) {
994 		error = EINVAL;
995 		goto out;
996 	}
997 	error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
998 	if (error)
999 		goto out;
1000 
1001 	udbinfo_barrier_set();
1002 	error = in_pcballoc(so, &udbinfo);
1003 	udbinfo_barrier_rem();
1004 
1005 	if (error)
1006 		goto out;
1007 
1008 	/*
1009 	 * Set default port for protocol processing prior to bind/connect.
1010 	 */
1011 	sosetport(so, netisr_portfn(0));
1012 
1013 	inp = (struct inpcb *)so->so_pcb;
1014 	inp->inp_vflag |= INP_IPV4;
1015 	inp->inp_ip_ttl = ip_defttl;
1016 	error = 0;
1017 out:
1018 	lwkt_replymsg(&msg->attach.base.lmsg, error);
1019 }
1020 
1021 static void
1022 udp_bind(netmsg_t msg)
1023 {
1024 	struct socket *so = msg->bind.base.nm_so;
1025 	struct sockaddr *nam = msg->bind.nm_nam;
1026 	struct thread *td = msg->bind.nm_td;
1027 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1028 	struct inpcb *inp;
1029 	int error;
1030 
1031 	inp = so->so_pcb;
1032 	if (inp) {
1033 		error = in_pcbbind(inp, nam, td);
1034 		if (error == 0) {
1035 			if (sin->sin_addr.s_addr != INADDR_ANY)
1036 				inp->inp_flags |= INP_WASBOUND_NOTANY;
1037 
1038 			udbinfo_barrier_set();
1039 			in_pcbinswildcardhash(inp);
1040 			udbinfo_barrier_rem();
1041 		}
1042 	} else {
1043 		error = EINVAL;
1044 	}
1045 	lwkt_replymsg(&msg->bind.base.lmsg, error);
1046 }
1047 
1048 static void
1049 udp_connect(netmsg_t msg)
1050 {
1051 	struct socket *so = msg->connect.base.nm_so;
1052 	struct sockaddr *nam = msg->connect.nm_nam;
1053 	struct thread *td = msg->connect.nm_td;
1054 	struct inpcb *inp;
1055 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
1056 	struct sockaddr_in *if_sin;
1057 	lwkt_port_t port;
1058 	int error;
1059 
1060 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
1061 
1062 	inp = so->so_pcb;
1063 	if (inp == NULL) {
1064 		error = EINVAL;
1065 		goto out;
1066 	}
1067 
1068 	if (msg->connect.nm_reconnect & NMSG_RECONNECT_RECONNECT) {
1069 		panic("UDP does not support RECONNECT");
1070 #ifdef notyet
1071 		msg->connect.nm_reconnect &= ~NMSG_RECONNECT_RECONNECT;
1072 		in_pcblink(inp, &udbinfo);
1073 #endif
1074 	}
1075 
1076 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1077 		error = EISCONN;
1078 		goto out;
1079 	}
1080 	error = 0;
1081 
1082 	/*
1083 	 * Bind if we have to
1084 	 */
1085 	if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
1086 	    inp->inp_laddr.s_addr == INADDR_ANY) {
1087 		error = in_pcbbind(inp, NULL, td);
1088 		if (error)
1089 			goto out;
1090 	}
1091 
1092 	/*
1093 	 * Calculate the correct protocol processing thread.  The connect
1094 	 * operation must run there.
1095 	 */
1096 	error = in_pcbladdr(inp, nam, &if_sin, td);
1097 	if (error)
1098 		goto out;
1099 	if (!prison_remote_ip(td, nam)) {
1100 		error = EAFNOSUPPORT; /* IPv6 only jail */
1101 		goto out;
1102 	}
1103 
1104 	port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1105 			    inp->inp_laddr.s_addr, inp->inp_lport);
1106 	if (port != &curthread->td_msgport) {
1107 #ifdef notyet
1108 		struct route *ro = &inp->inp_route;
1109 
1110 		/*
1111 		 * in_pcbladdr() may have allocated a route entry for us
1112 		 * on the current CPU, but we need a route entry on the
1113 		 * inpcb's owner CPU, so free it here.
1114 		 */
1115 		if (ro->ro_rt != NULL)
1116 			RTFREE(ro->ro_rt);
1117 		bzero(ro, sizeof(*ro));
1118 
1119 		/*
1120 		 * We are moving the protocol processing port the socket
1121 		 * is on, we have to unlink here and re-link on the
1122 		 * target cpu.
1123 		 */
1124 		in_pcbunlink(so->so_pcb, &udbinfo);
1125 		/* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */
1126 		sosetport(so, port);
1127 		msg->connect.nm_reconnect |= NMSG_RECONNECT_RECONNECT;
1128 		msg->connect.base.nm_dispatch = udp_connect;
1129 
1130 		lwkt_forwardmsg(port, &msg->connect.base.lmsg);
1131 		/* msg invalid now */
1132 		return;
1133 #else
1134 		panic("UDP activity should only be in netisr0");
1135 #endif
1136 	}
1137 	KKASSERT(port == &curthread->td_msgport);
1138 	error = udp_connect_oncpu(so, td, sin, if_sin);
1139 out:
1140 	KKASSERT(msg->connect.nm_m == NULL);
1141 	lwkt_replymsg(&msg->connect.base.lmsg, error);
1142 }
1143 
1144 static int
1145 udp_connect_oncpu(struct socket *so, struct thread *td,
1146 		  struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1147 {
1148 	struct inpcb *inp;
1149 	int error;
1150 
1151 	udbinfo_barrier_set();
1152 
1153 	inp = so->so_pcb;
1154 	if (inp->inp_flags & INP_WILDCARD)
1155 		in_pcbremwildcardhash(inp);
1156 	error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1157 
1158 	if (error == 0) {
1159 		/*
1160 		 * No more errors can occur, finish adjusting the socket
1161 		 * and change the processing port to reflect the connected
1162 		 * socket.  Once set we can no longer safely mess with the
1163 		 * socket.
1164 		 */
1165 		soisconnected(so);
1166 	} else if (error == EAFNOSUPPORT) {	/* connection dissolved */
1167 		/*
1168 		 * Follow traditional BSD behavior and retain
1169 		 * the local port binding.  But, fix the old misbehavior
1170 		 * of overwriting any previously bound local address.
1171 		 */
1172 		if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1173 			inp->inp_laddr.s_addr = INADDR_ANY;
1174 		in_pcbinswildcardhash(inp);
1175 	}
1176 
1177 	udbinfo_barrier_rem();
1178 	return error;
1179 }
1180 
1181 static void
1182 udp_detach(netmsg_t msg)
1183 {
1184 	struct socket *so = msg->detach.base.nm_so;
1185 	struct inpcb *inp;
1186 	int error;
1187 
1188 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
1189 
1190 	inp = so->so_pcb;
1191 	if (inp) {
1192 		udbinfo_barrier_set();
1193 		in_pcbdetach(inp);
1194 		udbinfo_barrier_rem();
1195 		error = 0;
1196 	} else {
1197 		error = EINVAL;
1198 	}
1199 	lwkt_replymsg(&msg->detach.base.lmsg, error);
1200 }
1201 
1202 static void
1203 udp_disconnect(netmsg_t msg)
1204 {
1205 	struct socket *so = msg->disconnect.base.nm_so;
1206 	struct route *ro;
1207 	struct inpcb *inp;
1208 	int error;
1209 
1210 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
1211 
1212 	inp = so->so_pcb;
1213 	if (inp == NULL) {
1214 		error = EINVAL;
1215 		goto out;
1216 	}
1217 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1218 		error = ENOTCONN;
1219 		goto out;
1220 	}
1221 
1222 	soreference(so);
1223 
1224 	udbinfo_barrier_set();
1225 	in_pcbdisconnect(inp);
1226 	udbinfo_barrier_rem();
1227 
1228 	soclrstate(so, SS_ISCONNECTED);		/* XXX */
1229 	sofree(so);
1230 
1231 	ro = &inp->inp_route;
1232 	if (ro->ro_rt != NULL)
1233 		RTFREE(ro->ro_rt);
1234 	bzero(ro, sizeof(*ro));
1235 	error = 0;
1236 out:
1237 	lwkt_replymsg(&msg->disconnect.base.lmsg, error);
1238 }
1239 
1240 static void
1241 udp_send(netmsg_t msg)
1242 {
1243 	struct socket *so = msg->send.base.nm_so;
1244 	struct mbuf *m = msg->send.nm_m;
1245 	struct sockaddr *addr = msg->send.nm_addr;
1246 	int pru_flags = msg->send.nm_flags;
1247 	struct inpcb *inp;
1248 	int error;
1249 
1250 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
1251 	KKASSERT(msg->send.nm_control == NULL);
1252 
1253 	inp = so->so_pcb;
1254 	if (inp) {
1255 		struct thread *td = msg->send.nm_td;
1256 		int flags = 0;
1257 
1258 		if (pru_flags & PRUS_DONTROUTE)
1259 			flags |= SO_DONTROUTE;
1260 		error = udp_output(inp, m, addr, td, flags);
1261 	} else {
1262 		m_freem(m);
1263 		error = EINVAL;
1264 	}
1265 
1266 	if (pru_flags & PRUS_FREEADDR)
1267 		kfree(addr, M_SONAME);
1268 
1269 	if ((pru_flags & PRUS_NOREPLY) == 0)
1270 		lwkt_replymsg(&msg->send.base.lmsg, error);
1271 }
1272 
1273 void
1274 udp_shutdown(netmsg_t msg)
1275 {
1276 	struct socket *so = msg->shutdown.base.nm_so;
1277 	struct inpcb *inp;
1278 	int error;
1279 
1280 	KKASSERT(&curthread->td_msgport == netisr_portfn(0));
1281 
1282 	inp = so->so_pcb;
1283 	if (inp) {
1284 		socantsendmore(so);
1285 		error = 0;
1286 	} else {
1287 		error = EINVAL;
1288 	}
1289 	lwkt_replymsg(&msg->shutdown.base.lmsg, error);
1290 }
1291 
1292 void
1293 udbinfo_lock(void)
1294 {
1295 	lwkt_serialize_enter(&udbinfo_slize);
1296 }
1297 
1298 void
1299 udbinfo_unlock(void)
1300 {
1301 	lwkt_serialize_exit(&udbinfo_slize);
1302 }
1303 
1304 void
1305 udbinfo_barrier_set(void)
1306 {
1307 	netisr_barrier_set(udbinfo_br);
1308 	udbinfo_lock();
1309 }
1310 
1311 void
1312 udbinfo_barrier_rem(void)
1313 {
1314 	udbinfo_unlock();
1315 	netisr_barrier_rem(udbinfo_br);
1316 }
1317 
1318 struct pr_usrreqs udp_usrreqs = {
1319 	.pru_abort = udp_abort,
1320 	.pru_accept = pr_generic_notsupp,
1321 	.pru_attach = udp_attach,
1322 	.pru_bind = udp_bind,
1323 	.pru_connect = udp_connect,
1324 	.pru_connect2 = pr_generic_notsupp,
1325 	.pru_control = in_control_dispatch,
1326 	.pru_detach = udp_detach,
1327 	.pru_disconnect = udp_disconnect,
1328 	.pru_listen = pr_generic_notsupp,
1329 	.pru_peeraddr = in_setpeeraddr_dispatch,
1330 	.pru_rcvd = pr_generic_notsupp,
1331 	.pru_rcvoob = pr_generic_notsupp,
1332 	.pru_send = udp_send,
1333 	.pru_sense = pru_sense_null,
1334 	.pru_shutdown = udp_shutdown,
1335 	.pru_sockaddr = in_setsockaddr_dispatch,
1336 	.pru_sosend = sosendudp,
1337 	.pru_soreceive = soreceive
1338 };
1339 
1340