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