xref: /dragonfly/sys/netinet/udp_usrreq.c (revision 36a3d1d6)
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  * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.47 2008/11/11 10:46:58 sephe Exp $
69  */
70 
71 #include "opt_ipsec.h"
72 #include "opt_inet6.h"
73 
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/kernel.h>
77 #include <sys/malloc.h>
78 #include <sys/mbuf.h>
79 #include <sys/domain.h>
80 #include <sys/proc.h>
81 #include <sys/priv.h>
82 #include <sys/protosw.h>
83 #include <sys/socket.h>
84 #include <sys/socketvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/syslog.h>
87 #include <sys/thread2.h>
88 #include <sys/in_cksum.h>
89 
90 #include <machine/stdarg.h>
91 
92 #include <net/if.h>
93 #include <net/route.h>
94 #include <net/netmsg2.h>
95 
96 #include <netinet/in.h>
97 #include <netinet/in_systm.h>
98 #include <netinet/ip.h>
99 #ifdef INET6
100 #include <netinet/ip6.h>
101 #endif
102 #include <netinet/in_pcb.h>
103 #include <netinet/in_var.h>
104 #include <netinet/ip_var.h>
105 #ifdef INET6
106 #include <netinet6/ip6_var.h>
107 #endif
108 #include <netinet/ip_icmp.h>
109 #include <netinet/icmp_var.h>
110 #include <netinet/udp.h>
111 #include <netinet/udp_var.h>
112 
113 #ifdef FAST_IPSEC
114 #include <netproto/ipsec/ipsec.h>
115 #endif
116 
117 #ifdef IPSEC
118 #include <netinet6/ipsec.h>
119 #endif
120 
121 int	udp_mpsafe_proto = 0;
122 TUNABLE_INT("net.inet.udp.mpsafe_proto", &udp_mpsafe_proto);
123 
124 int	udp_mpsafe_thread = NETMSG_SERVICE_ADAPTIVE;
125 TUNABLE_INT("net.inet.udp.mpsafe_thread", &udp_mpsafe_thread);
126 SYSCTL_INT(_net_inet_udp, OID_AUTO, mpsafe_thread, CTLFLAG_RW,
127 	   &udp_mpsafe_thread, 0,
128 	   "0:BGL, 1:Adaptive BGL, 2:No BGL(experimental)");
129 
130 /*
131  * UDP protocol implementation.
132  * Per RFC 768, August, 1980.
133  */
134 #ifndef	COMPAT_42
135 static int	udpcksum = 1;
136 #else
137 static int	udpcksum = 0;		/* XXX */
138 #endif
139 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
140 		&udpcksum, 0, "");
141 
142 int	log_in_vain = 0;
143 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
144     &log_in_vain, 0, "Log all incoming UDP packets");
145 
146 static int	blackhole = 0;
147 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
148 	&blackhole, 0, "Do not send port unreachables for refused connects");
149 
150 static int	strict_mcast_mship = 1;
151 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
152 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
153 
154 struct	inpcbinfo udbinfo;
155 
156 #ifndef UDBHASHSIZE
157 #define UDBHASHSIZE 16
158 #endif
159 
160 struct	udpstat udpstat;	/* from udp_var.h */
161 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
162     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
163 
164 static struct	sockaddr_in udp_in = { sizeof udp_in, AF_INET };
165 #ifdef INET6
166 struct udp_in6 {
167 	struct sockaddr_in6	uin6_sin;
168 	u_char			uin6_init_done : 1;
169 } udp_in6 = {
170 	{ sizeof udp_in6.uin6_sin, AF_INET6 },
171 	0
172 };
173 struct udp_ip6 {
174 	struct ip6_hdr		uip6_ip6;
175 	u_char			uip6_init_done : 1;
176 } udp_ip6;
177 #endif /* INET6 */
178 
179 static void udp_append (struct inpcb *last, struct ip *ip,
180 			    struct mbuf *n, int off);
181 #ifdef INET6
182 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
183 #endif
184 
185 static int udp_connect_oncpu(struct socket *so, struct thread *td,
186 			struct sockaddr_in *sin, struct sockaddr_in *if_sin);
187 static int udp_detach (struct socket *so);
188 static	int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
189 			    struct mbuf *, struct thread *);
190 
191 void
192 udp_init(void)
193 {
194 	in_pcbinfo_init(&udbinfo);
195 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
196 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
197 					&udbinfo.porthashmask);
198 	udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
199 					    &udbinfo.wildcardhashmask);
200 	udbinfo.ipi_size = sizeof(struct inpcb);
201 	udp_thread_init();
202 }
203 
204 /*
205  * Check multicast packets to make sure they are only sent to sockets with
206  * multicast memberships for the packet's destination address and arrival
207  * interface.  Multicast packets to multicast-unaware sockets are also
208  * disallowed.
209  *
210  * Returns 0 if the packet is acceptable, -1 if it is not.
211  */
212 static __inline int
213 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
214 {
215 	int mshipno;
216 	struct ip_moptions *mopt;
217 
218 	if (strict_mcast_mship == 0 ||
219 	    !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
220 		return (0);
221 	}
222 	mopt = inp->inp_moptions;
223 	if (mopt == NULL)
224 		return (-1);
225 	for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
226 		struct in_multi *maddr = mopt->imo_membership[mshipno];
227 
228 		if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
229 		    m->m_pkthdr.rcvif == maddr->inm_ifp) {
230 			return (0);
231 		}
232 	}
233 	return (-1);
234 }
235 
236 void
237 udp_input(struct mbuf *m, ...)
238 {
239 	int iphlen;
240 	struct ip *ip;
241 	struct udphdr *uh;
242 	struct inpcb *inp;
243 	struct mbuf *opts = NULL;
244 	int len, off, proto;
245 	struct ip save_ip;
246 	struct sockaddr *append_sa;
247 	__va_list ap;
248 
249 	__va_start(ap, m);
250 	off = __va_arg(ap, int);
251 	proto = __va_arg(ap, int);
252 	__va_end(ap);
253 
254 	iphlen = off;
255 	udpstat.udps_ipackets++;
256 
257 	/*
258 	 * Strip IP options, if any; should skip this,
259 	 * make available to user, and use on returned packets,
260 	 * but we don't yet have a way to check the checksum
261 	 * with options still present.
262 	 */
263 	if (iphlen > sizeof(struct ip)) {
264 		ip_stripoptions(m);
265 		iphlen = sizeof(struct ip);
266 	}
267 
268 	/*
269 	 * IP and UDP headers are together in first mbuf.
270 	 * Already checked and pulled up in ip_demux().
271 	 */
272 	KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
273 	    ("UDP header not in one mbuf"));
274 
275 	ip = mtod(m, struct ip *);
276 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
277 
278 	/* destination port of 0 is illegal, based on RFC768. */
279 	if (uh->uh_dport == 0)
280 		goto bad;
281 
282 	/*
283 	 * Make mbuf data length reflect UDP length.
284 	 * If not enough data to reflect UDP length, drop.
285 	 */
286 	len = ntohs((u_short)uh->uh_ulen);
287 	if (ip->ip_len != len) {
288 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
289 			udpstat.udps_badlen++;
290 			goto bad;
291 		}
292 		m_adj(m, len - ip->ip_len);
293 		/* ip->ip_len = len; */
294 	}
295 	/*
296 	 * Save a copy of the IP header in case we want restore it
297 	 * for sending an ICMP error message in response.
298 	 */
299 	save_ip = *ip;
300 
301 	/*
302 	 * Checksum extended UDP header and data.
303 	 */
304 	if (uh->uh_sum) {
305 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
306 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
307 				uh->uh_sum = m->m_pkthdr.csum_data;
308 			else
309 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
310 				    ip->ip_dst.s_addr, htonl((u_short)len +
311 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
312 			uh->uh_sum ^= 0xffff;
313 		} else {
314 			char b[9];
315 
316 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
317 			bzero(((struct ipovly *)ip)->ih_x1, 9);
318 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
319 			uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
320 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
321 		}
322 		if (uh->uh_sum) {
323 			udpstat.udps_badsum++;
324 			m_freem(m);
325 			return;
326 		}
327 	} else
328 		udpstat.udps_nosum++;
329 
330 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
331 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
332 		struct inpcb *last;
333 
334 		/*
335 		 * Deliver a multicast or broadcast datagram to *all* sockets
336 		 * for which the local and remote addresses and ports match
337 		 * those of the incoming datagram.  This allows more than
338 		 * one process to receive multi/broadcasts on the same port.
339 		 * (This really ought to be done for unicast datagrams as
340 		 * well, but that would cause problems with existing
341 		 * applications that open both address-specific sockets and
342 		 * a wildcard socket listening to the same port -- they would
343 		 * end up receiving duplicates of every unicast datagram.
344 		 * Those applications open the multiple sockets to overcome an
345 		 * inadequacy of the UDP socket interface, but for backwards
346 		 * compatibility we avoid the problem here rather than
347 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
348 		 */
349 
350 		/*
351 		 * Construct sockaddr format source address.
352 		 */
353 		udp_in.sin_port = uh->uh_sport;
354 		udp_in.sin_addr = ip->ip_src;
355 		/*
356 		 * Locate pcb(s) for datagram.
357 		 * (Algorithm copied from raw_intr().)
358 		 */
359 		last = NULL;
360 #ifdef INET6
361 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
362 #endif
363 		LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
364 			if (inp->inp_flags & INP_PLACEMARKER)
365 				continue;
366 #ifdef INET6
367 			if (!(inp->inp_vflag & INP_IPV4))
368 				continue;
369 #endif
370 			if (inp->inp_lport != uh->uh_dport)
371 				continue;
372 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
373 				if (inp->inp_laddr.s_addr !=
374 				    ip->ip_dst.s_addr)
375 					continue;
376 			}
377 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
378 				if (inp->inp_faddr.s_addr !=
379 				    ip->ip_src.s_addr ||
380 				    inp->inp_fport != uh->uh_sport)
381 					continue;
382 			}
383 
384 			if (check_multicast_membership(ip, inp, m) < 0)
385 				continue;
386 
387 			if (last != NULL) {
388 				struct mbuf *n;
389 
390 #ifdef IPSEC
391 				/* check AH/ESP integrity. */
392 				if (ipsec4_in_reject_so(m, last->inp_socket))
393 					ipsecstat.in_polvio++;
394 					/* do not inject data to pcb */
395 				else
396 #endif /*IPSEC*/
397 #ifdef FAST_IPSEC
398 				/* check AH/ESP integrity. */
399 				if (ipsec4_in_reject(m, last))
400 					;
401 				else
402 #endif /*FAST_IPSEC*/
403 				if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
404 					udp_append(last, ip, n,
405 						   iphlen +
406 						   sizeof(struct udphdr));
407 			}
408 			last = inp;
409 			/*
410 			 * Don't look for additional matches if this one does
411 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
412 			 * socket options set.  This heuristic avoids searching
413 			 * through all pcbs in the common case of a non-shared
414 			 * port.  It * assumes that an application will never
415 			 * clear these options after setting them.
416 			 */
417 			if (!(last->inp_socket->so_options &
418 			    (SO_REUSEPORT | SO_REUSEADDR)))
419 				break;
420 		}
421 
422 		if (last == NULL) {
423 			/*
424 			 * No matching pcb found; discard datagram.
425 			 * (No need to send an ICMP Port Unreachable
426 			 * for a broadcast or multicast datgram.)
427 			 */
428 			udpstat.udps_noportbcast++;
429 			goto bad;
430 		}
431 #ifdef IPSEC
432 		/* check AH/ESP integrity. */
433 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
434 			ipsecstat.in_polvio++;
435 			goto bad;
436 		}
437 #endif /*IPSEC*/
438 #ifdef FAST_IPSEC
439 		/* check AH/ESP integrity. */
440 		if (ipsec4_in_reject(m, last))
441 			goto bad;
442 #endif /*FAST_IPSEC*/
443 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
444 		return;
445 	}
446 	/*
447 	 * Locate pcb for datagram.
448 	 */
449 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
450 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
451 	if (inp == NULL) {
452 		if (log_in_vain) {
453 			char buf[sizeof "aaa.bbb.ccc.ddd"];
454 
455 			strcpy(buf, inet_ntoa(ip->ip_dst));
456 			log(LOG_INFO,
457 			    "Connection attempt to UDP %s:%d from %s:%d\n",
458 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
459 			    ntohs(uh->uh_sport));
460 		}
461 		udpstat.udps_noport++;
462 		if (m->m_flags & (M_BCAST | M_MCAST)) {
463 			udpstat.udps_noportbcast++;
464 			goto bad;
465 		}
466 		if (blackhole)
467 			goto bad;
468 #ifdef ICMP_BANDLIM
469 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
470 			goto bad;
471 #endif
472 		*ip = save_ip;
473 		ip->ip_len += iphlen;
474 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
475 		return;
476 	}
477 #ifdef IPSEC
478 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
479 		ipsecstat.in_polvio++;
480 		goto bad;
481 	}
482 #endif /*IPSEC*/
483 #ifdef FAST_IPSEC
484 	if (ipsec4_in_reject(m, inp))
485 		goto bad;
486 #endif /*FAST_IPSEC*/
487 	/*
488 	 * Check the minimum TTL for socket.
489 	 */
490 	if (ip->ip_ttl < inp->inp_ip_minttl)
491 		goto bad;
492 
493 	/*
494 	 * Construct sockaddr format source address.
495 	 * Stuff source address and datagram in user buffer.
496 	 */
497 	udp_in.sin_port = uh->uh_sport;
498 	udp_in.sin_addr = ip->ip_src;
499 	if ((inp->inp_flags & INP_CONTROLOPTS) ||
500 	    (inp->inp_socket->so_options & SO_TIMESTAMP)) {
501 #ifdef INET6
502 		if (inp->inp_vflag & INP_IPV6) {
503 			int savedflags;
504 
505 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
506 			savedflags = inp->inp_flags;
507 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
508 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
509 			inp->inp_flags = savedflags;
510 		} else
511 #endif
512 		ip_savecontrol(inp, &opts, ip, m);
513 	}
514 	m_adj(m, iphlen + sizeof(struct udphdr));
515 #ifdef INET6
516 	if (inp->inp_vflag & INP_IPV6) {
517 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
518 		append_sa = (struct sockaddr *)&udp_in6;
519 	} else
520 #endif
521 		append_sa = (struct sockaddr *)&udp_in;
522 	if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
523 		udpstat.udps_fullsock++;
524 		goto bad;
525 	}
526 	sorwakeup(inp->inp_socket);
527 	return;
528 bad:
529 	m_freem(m);
530 	if (opts)
531 		m_freem(opts);
532 	return;
533 }
534 
535 #ifdef INET6
536 static void
537 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
538 {
539 	bzero(ip6, sizeof *ip6);
540 
541 	ip6->ip6_vfc = IPV6_VERSION;
542 	ip6->ip6_plen = ip->ip_len;
543 	ip6->ip6_nxt = ip->ip_p;
544 	ip6->ip6_hlim = ip->ip_ttl;
545 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
546 		IPV6_ADDR_INT32_SMP;
547 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
548 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
549 }
550 #endif
551 
552 /*
553  * subroutine of udp_input(), mainly for source code readability.
554  * caller must properly init udp_ip6 and udp_in6 beforehand.
555  */
556 static void
557 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
558 {
559 	struct sockaddr *append_sa;
560 	struct mbuf *opts = NULL;
561 
562 	if (last->inp_flags & INP_CONTROLOPTS ||
563 	    last->inp_socket->so_options & SO_TIMESTAMP) {
564 #ifdef INET6
565 		if (last->inp_vflag & INP_IPV6) {
566 			int savedflags;
567 
568 			if (udp_ip6.uip6_init_done == 0) {
569 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
570 				udp_ip6.uip6_init_done = 1;
571 			}
572 			savedflags = last->inp_flags;
573 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
574 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
575 			last->inp_flags = savedflags;
576 		} else
577 #endif
578 		ip_savecontrol(last, &opts, ip, n);
579 	}
580 #ifdef INET6
581 	if (last->inp_vflag & INP_IPV6) {
582 		if (udp_in6.uin6_init_done == 0) {
583 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
584 			udp_in6.uin6_init_done = 1;
585 		}
586 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
587 	} else
588 #endif
589 		append_sa = (struct sockaddr *)&udp_in;
590 	m_adj(n, off);
591 	if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
592 		m_freem(n);
593 		if (opts)
594 			m_freem(opts);
595 		udpstat.udps_fullsock++;
596 	} else
597 		sorwakeup(last->inp_socket);
598 }
599 
600 /*
601  * Notify a udp user of an asynchronous error;
602  * just wake up so that he can collect error status.
603  */
604 void
605 udp_notify(struct inpcb *inp, int error)
606 {
607 	inp->inp_socket->so_error = error;
608 	sorwakeup(inp->inp_socket);
609 	sowwakeup(inp->inp_socket);
610 }
611 
612 struct netmsg_udp_notify {
613 	struct netmsg	nm_nmsg;
614 	void		(*nm_notify)(struct inpcb *, int);
615 	struct in_addr	nm_faddr;
616 	int		nm_arg;
617 };
618 
619 static void
620 udp_notifyall_oncpu(struct netmsg *netmsg)
621 {
622 	struct netmsg_udp_notify *nmsg = (struct netmsg_udp_notify *)netmsg;
623 	int nextcpu;
624 
625 	in_pcbnotifyall(&udbinfo.pcblisthead, nmsg->nm_faddr, nmsg->nm_arg,
626 			nmsg->nm_notify);
627 
628 	nextcpu = mycpuid + 1;
629 	if (nextcpu < ncpus2)
630 		lwkt_forwardmsg(udp_cport(nextcpu), &netmsg->nm_lmsg);
631 	else
632 		lwkt_replymsg(&netmsg->nm_lmsg, 0);
633 }
634 
635 static void
636 udp_rtchange(struct inpcb *inp, int err)
637 {
638 #ifdef SMP
639 	/* XXX Nuke this, once UDP inpcbs are CPU localized */
640 	if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) {
641 		rtfree(inp->inp_route.ro_rt);
642 		inp->inp_route.ro_rt = NULL;
643 		/*
644 		 * A new route can be allocated the next time
645 		 * output is attempted.
646 		 */
647 	}
648 #else
649 	in_rtchange(inp, err);
650 #endif
651 }
652 
653 void
654 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
655 {
656 	struct ip *ip = vip;
657 	struct udphdr *uh;
658 	void (*notify) (struct inpcb *, int) = udp_notify;
659 	struct in_addr faddr;
660 	struct inpcb *inp;
661 
662 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
663 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
664 		return;
665 
666 	if (PRC_IS_REDIRECT(cmd)) {
667 		ip = NULL;
668 		notify = udp_rtchange;
669 	} else if (cmd == PRC_HOSTDEAD)
670 		ip = NULL;
671 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
672 		return;
673 	if (ip) {
674 		crit_enter();
675 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
676 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
677 					ip->ip_src, uh->uh_sport, 0, NULL);
678 		if (inp != NULL && inp->inp_socket != NULL)
679 			(*notify)(inp, inetctlerrmap[cmd]);
680 		crit_exit();
681 	} else if (PRC_IS_REDIRECT(cmd)) {
682 		struct netmsg_udp_notify nmsg;
683 
684 		KKASSERT(&curthread->td_msgport == cpu_portfn(0));
685 		netmsg_init(&nmsg.nm_nmsg, NULL, &curthread->td_msgport,
686 			    0, udp_notifyall_oncpu);
687 		nmsg.nm_faddr = faddr;
688 		nmsg.nm_arg = inetctlerrmap[cmd];
689 		nmsg.nm_notify = notify;
690 
691 		lwkt_domsg(udp_cport(0), &nmsg.nm_nmsg.nm_lmsg, 0);
692 	} else {
693 		/*
694 		 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL,
695 		 * once UDP inpcbs are CPU localized
696 		 */
697 		KKASSERT(&curthread->td_msgport == udp_cport(0));
698 		in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
699 				notify);
700 	}
701 }
702 
703 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
704 	    in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
705 
706 static int
707 udp_getcred(SYSCTL_HANDLER_ARGS)
708 {
709 	struct sockaddr_in addrs[2];
710 	struct inpcb *inp;
711 	int error;
712 
713 	error = priv_check(req->td, PRIV_ROOT);
714 	if (error)
715 		return (error);
716 	error = SYSCTL_IN(req, addrs, sizeof addrs);
717 	if (error)
718 		return (error);
719 	crit_enter();
720 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
721 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
722 	if (inp == NULL || inp->inp_socket == NULL) {
723 		error = ENOENT;
724 		goto out;
725 	}
726 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
727 out:
728 	crit_exit();
729 	return (error);
730 }
731 
732 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
733     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
734 
735 static int
736 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
737 	   struct mbuf *control, struct thread *td)
738 {
739 	struct udpiphdr *ui;
740 	int len = m->m_pkthdr.len;
741 	struct sockaddr_in *sin;	/* really is initialized before use */
742 	int error = 0, lport_any = 0;
743 
744 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
745 		error = EMSGSIZE;
746 		goto release;
747 	}
748 
749 	if (inp->inp_lport == 0) {	/* unbound socket */
750 		error = in_pcbbind(inp, NULL, td);
751 		if (error)
752 			goto release;
753 		in_pcbinswildcardhash(inp);
754 		lport_any = 1;
755 	}
756 
757 	if (dstaddr != NULL) {		/* destination address specified */
758 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
759 			/* already connected */
760 			error = EISCONN;
761 			goto release;
762 		}
763 		sin = (struct sockaddr_in *)dstaddr;
764 		if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
765 			error = EAFNOSUPPORT; /* IPv6 only jail */
766 			goto release;
767 		}
768 	} else {
769 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
770 			/* no destination specified and not already connected */
771 			error = ENOTCONN;
772 			goto release;
773 		}
774 		sin = NULL;
775 	}
776 
777 	/*
778 	 * Calculate data length and get a mbuf
779 	 * for UDP and IP headers.
780 	 */
781 	M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
782 	if (m == NULL) {
783 		error = ENOBUFS;
784 		goto release;
785 	}
786 
787 	/*
788 	 * Fill in mbuf with extended UDP header
789 	 * and addresses and length put into network format.
790 	 */
791 	ui = mtod(m, struct udpiphdr *);
792 	bzero(ui->ui_x1, sizeof ui->ui_x1);	/* XXX still needed? */
793 	ui->ui_pr = IPPROTO_UDP;
794 
795 	/*
796 	 * Set destination address.
797 	 */
798 	if (dstaddr != NULL) {			/* use specified destination */
799 		ui->ui_dst = sin->sin_addr;
800 		ui->ui_dport = sin->sin_port;
801 	} else {				/* use connected destination */
802 		ui->ui_dst = inp->inp_faddr;
803 		ui->ui_dport = inp->inp_fport;
804 	}
805 
806 	/*
807 	 * Set source address.
808 	 */
809 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
810 		struct sockaddr_in *if_sin;
811 
812 		if (dstaddr == NULL) {
813 			/*
814 			 * connect() had (or should have) failed because
815 			 * the interface had no IP address, but the
816 			 * application proceeded to call send() anyways.
817 			 */
818 			error = ENOTCONN;
819 			goto release;
820 		}
821 
822 		/* Look up outgoing interface. */
823 		if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
824 			goto release;
825 		ui->ui_src = if_sin->sin_addr;	/* use address of interface */
826 	} else {
827 		ui->ui_src = inp->inp_laddr;	/* use non-null bound address */
828 	}
829 	ui->ui_sport = inp->inp_lport;
830 	KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
831 
832 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
833 
834 	/*
835 	 * Set up checksum and output datagram.
836 	 */
837 	if (udpcksum) {
838 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
839 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
840 		m->m_pkthdr.csum_flags = CSUM_UDP;
841 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
842 	} else {
843 		ui->ui_sum = 0;
844 	}
845 	((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
846 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
847 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
848 	udpstat.udps_opackets++;
849 
850 	error = ip_output(m, inp->inp_options, &inp->inp_route,
851 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
852 	    IP_DEBUGROUTE,
853 	    inp->inp_moptions, inp);
854 
855 	/*
856 	 * If this is the first data gram sent on an unbound and unconnected
857 	 * UDP socket, lport will be changed in this function.  If target
858 	 * CPU after this lport changing is no longer the current CPU, then
859 	 * free the route entry allocated on the current CPU.
860 	 */
861 	if (lport_any) {
862 		if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
863 		    inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
864 			struct route *ro = &inp->inp_route;
865 
866 			if (ro->ro_rt != NULL)
867 				RTFREE(ro->ro_rt);
868 			bzero(ro, sizeof(*ro));
869 		}
870 	}
871 	return (error);
872 
873 release:
874 	m_freem(m);
875 	return (error);
876 }
877 
878 u_long	udp_sendspace = 9216;		/* really max datagram size */
879 					/* 40 1K datagrams */
880 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
881     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
882 
883 u_long	udp_recvspace = 40 * (1024 +
884 #ifdef INET6
885 				      sizeof(struct sockaddr_in6)
886 #else
887 				      sizeof(struct sockaddr_in)
888 #endif
889 				      );
890 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
891     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
892 
893 static int
894 udp_abort(struct socket *so)
895 {
896 	struct inpcb *inp;
897 
898 	inp = so->so_pcb;
899 	if (inp == NULL)
900 		return EINVAL;	/* ??? possible? panic instead? */
901 	soisdisconnected(so);
902 	crit_enter();
903 	in_pcbdetach(inp);
904 	crit_exit();
905 	return 0;
906 }
907 
908 static int
909 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
910 {
911 	struct inpcb *inp;
912 	int error;
913 
914 	inp = so->so_pcb;
915 	if (inp != NULL)
916 		return EINVAL;
917 
918 	error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
919 	if (error)
920 		return error;
921 	crit_enter();
922 	error = in_pcballoc(so, &udbinfo);
923 	crit_exit();
924 	if (error)
925 		return error;
926 	so->so_port = udp_soport_attach(so);
927 
928 	inp = (struct inpcb *)so->so_pcb;
929 	inp->inp_vflag |= INP_IPV4;
930 	inp->inp_ip_ttl = ip_defttl;
931 	return 0;
932 }
933 
934 static int
935 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
936 {
937 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
938 	struct inpcb *inp;
939 	int error;
940 
941 	inp = so->so_pcb;
942 	if (inp == NULL)
943 		return EINVAL;
944 	crit_enter();
945 	error = in_pcbbind(inp, nam, td);
946 	crit_exit();
947 	if (error == 0) {
948 		if (sin->sin_addr.s_addr != INADDR_ANY)
949 			inp->inp_flags |= INP_WASBOUND_NOTANY;
950 		in_pcbinswildcardhash(inp);
951 	}
952 	return error;
953 }
954 
955 #ifdef SMP
956 
957 struct netmsg_udp_connect {
958 	struct netmsg		nm_netmsg;
959 	struct socket		*nm_so;
960 	struct sockaddr_in	*nm_sin;
961 	struct sockaddr_in	*nm_ifsin;
962 	struct thread		*nm_td;
963 };
964 
965 static void
966 udp_connect_handler(netmsg_t netmsg)
967 {
968 	struct netmsg_udp_connect *msg = (void *)netmsg;
969 	int error;
970 
971 	error = udp_connect_oncpu(msg->nm_so, msg->nm_td,
972 				  msg->nm_sin, msg->nm_ifsin);
973 	lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error);
974 }
975 
976 #endif
977 
978 static int
979 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
980 {
981 	struct inpcb *inp;
982 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
983 	struct sockaddr_in *if_sin;
984 	lwkt_port_t port;
985 	int error;
986 
987 	inp = so->so_pcb;
988 	if (inp == NULL)
989 		return EINVAL;
990 	if (inp->inp_faddr.s_addr != INADDR_ANY)
991 		return EISCONN;
992 	error = 0;
993 
994 	/*
995 	 * Bind if we have to
996 	 */
997 	if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
998 	    inp->inp_laddr.s_addr == INADDR_ANY) {
999 		error = in_pcbbind(inp, NULL, td);
1000 		if (error)
1001 			return (error);
1002 	}
1003 
1004 	/*
1005 	 * Calculate the correct protocol processing thread.  The connect
1006 	 * operation must run there.
1007 	 */
1008 	error = in_pcbladdr(inp, nam, &if_sin, td);
1009 	if (error)
1010 		return(error);
1011 	if (!prison_remote_ip(td, nam))
1012 		return(EAFNOSUPPORT); /* IPv6 only jail */
1013 
1014 	port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port,
1015 			    inp->inp_laddr.s_addr, inp->inp_lport);
1016 #ifdef SMP
1017 	if (port != &curthread->td_msgport) {
1018 		struct netmsg_udp_connect msg;
1019 		struct route *ro = &inp->inp_route;
1020 
1021 		/*
1022 		 * in_pcbladdr() may have allocated a route entry for us
1023 		 * on the current CPU, but we need a route entry on the
1024 		 * inpcb's owner CPU, so free it here.
1025 		 */
1026 		if (ro->ro_rt != NULL)
1027 			RTFREE(ro->ro_rt);
1028 		bzero(ro, sizeof(*ro));
1029 
1030 		/*
1031 		 * NOTE: We haven't set so->so_port yet do not pass so
1032 		 *       to netmsg_init() or it will be improperly forwarded.
1033 		 */
1034 		netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport,
1035 			    0, udp_connect_handler);
1036 		msg.nm_so = so;
1037 		msg.nm_sin = sin;
1038 		msg.nm_ifsin = if_sin;
1039 		msg.nm_td = td;
1040 		error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0);
1041 	} else {
1042 		error = udp_connect_oncpu(so, td, sin, if_sin);
1043 	}
1044 #else
1045 	error = udp_connect_oncpu(so, td, sin, if_sin);
1046 #endif
1047 	return (error);
1048 }
1049 
1050 static int
1051 udp_connect_oncpu(struct socket *so, struct thread *td,
1052 		  struct sockaddr_in *sin, struct sockaddr_in *if_sin)
1053 {
1054 	struct inpcb *inp;
1055 	int error;
1056 
1057 	inp = so->so_pcb;
1058 	if (inp->inp_flags & INP_WILDCARD)
1059 		in_pcbremwildcardhash(inp);
1060 	error = in_pcbconnect(inp, (struct sockaddr *)sin, td);
1061 
1062 	if (error == 0) {
1063 		/*
1064 		 * No more errors can occur, finish adjusting the socket
1065 		 * and change the processing port to reflect the connected
1066 		 * socket.  Once set we can no longer safely mess with the
1067 		 * socket.
1068 		 */
1069 		soisconnected(so);
1070 		sosetport(so, &curthread->td_msgport);
1071 	} else if (error == EAFNOSUPPORT) {	/* connection dissolved */
1072 		/*
1073 		 * Follow traditional BSD behavior and retain
1074 		 * the local port binding.  But, fix the old misbehavior
1075 		 * of overwriting any previously bound local address.
1076 		 */
1077 		if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
1078 			inp->inp_laddr.s_addr = INADDR_ANY;
1079 		in_pcbinswildcardhash(inp);
1080 	}
1081 	return error;
1082 }
1083 
1084 static int
1085 udp_detach(struct socket *so)
1086 {
1087 	struct inpcb *inp;
1088 
1089 	inp = so->so_pcb;
1090 	if (inp == NULL)
1091 		return EINVAL;
1092 	crit_enter();
1093 	in_pcbdetach(inp);
1094 	crit_exit();
1095 	return 0;
1096 }
1097 
1098 static int
1099 udp_disconnect(struct socket *so)
1100 {
1101 	struct route *ro;
1102 	struct inpcb *inp;
1103 
1104 	inp = so->so_pcb;
1105 	if (inp == NULL)
1106 		return EINVAL;
1107 	if (inp->inp_faddr.s_addr == INADDR_ANY)
1108 		return ENOTCONN;
1109 
1110 	crit_enter();
1111 	in_pcbdisconnect(inp);
1112 	crit_exit();
1113 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1114 
1115 	ro = &inp->inp_route;
1116 	if (ro->ro_rt != NULL)
1117 		RTFREE(ro->ro_rt);
1118 	bzero(ro, sizeof(*ro));
1119 
1120 	return 0;
1121 }
1122 
1123 static int
1124 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1125 	    struct mbuf *control, struct thread *td)
1126 {
1127 	struct inpcb *inp;
1128 
1129 	inp = so->so_pcb;
1130 	if (inp == NULL) {
1131 		m_freem(m);
1132 		return EINVAL;
1133 	}
1134 	return udp_output(inp, m, addr, control, td);
1135 }
1136 
1137 int
1138 udp_shutdown(struct socket *so)
1139 {
1140 	struct inpcb *inp;
1141 
1142 	inp = so->so_pcb;
1143 	if (inp == NULL)
1144 		return EINVAL;
1145 	socantsendmore(so);
1146 	return 0;
1147 }
1148 
1149 struct pr_usrreqs udp_usrreqs = {
1150 	.pru_abort = udp_abort,
1151 	.pru_accept = pru_accept_notsupp,
1152 	.pru_attach = udp_attach,
1153 	.pru_bind = udp_bind,
1154 	.pru_connect = udp_connect,
1155 	.pru_connect2 = pru_connect2_notsupp,
1156 	.pru_control = in_control,
1157 	.pru_detach = udp_detach,
1158 	.pru_disconnect = udp_disconnect,
1159 	.pru_listen = pru_listen_notsupp,
1160 	.pru_peeraddr = in_setpeeraddr,
1161 	.pru_rcvd = pru_rcvd_notsupp,
1162 	.pru_rcvoob = pru_rcvoob_notsupp,
1163 	.pru_send = udp_send,
1164 	.pru_sense = pru_sense_null,
1165 	.pru_shutdown = udp_shutdown,
1166 	.pru_sockaddr = in_setsockaddr,
1167 	.pru_sosend = sosendudp,
1168 	.pru_soreceive = soreceive
1169 };
1170 
1171