xref: /dflybsd-src/sys/netinet/udp_usrreq.c (revision d797dc5d823699c41734581020450ed2a7edf045)
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.46 2008/09/23 11:28:49 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/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/thread2.h>
87 #include <sys/in_cksum.h>
88 
89 #include <machine/stdarg.h>
90 
91 #include <vm/vm_zone.h>
92 
93 #include <net/if.h>
94 #include <net/route.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 = 0;
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_detach (struct socket *so);
186 static	int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
187 			    struct mbuf *, struct thread *);
188 
189 void
190 udp_init(void)
191 {
192 	in_pcbinfo_init(&udbinfo);
193 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
194 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
195 					&udbinfo.porthashmask);
196 	udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
197 					    &udbinfo.wildcardhashmask);
198 	udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
199 				 ZONE_INTERRUPT, 0);
200 	udp_thread_init();
201 }
202 
203 /*
204  * Check multicast packets to make sure they are only sent to sockets with
205  * multicast memberships for the packet's destination address and arrival
206  * interface.  Multicast packets to multicast-unaware sockets are also
207  * disallowed.
208  *
209  * Returns 0 if the packet is acceptable, -1 if it is not.
210  */
211 static __inline int
212 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
213 {
214 	int mshipno;
215 	struct ip_moptions *mopt;
216 
217 	if (strict_mcast_mship == 0 ||
218 	    !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
219 		return (0);
220 	}
221 	mopt = inp->inp_moptions;
222 	if (mopt == NULL)
223 		return (-1);
224 	for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
225 		struct in_multi *maddr = mopt->imo_membership[mshipno];
226 
227 		if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
228 		    m->m_pkthdr.rcvif == maddr->inm_ifp) {
229 			return (0);
230 		}
231 	}
232 	return (-1);
233 }
234 
235 void
236 udp_input(struct mbuf *m, ...)
237 {
238 	int iphlen;
239 	struct ip *ip;
240 	struct udphdr *uh;
241 	struct inpcb *inp;
242 	struct mbuf *opts = NULL;
243 	int len, off, proto;
244 	struct ip save_ip;
245 	struct sockaddr *append_sa;
246 	__va_list ap;
247 
248 	__va_start(ap, m);
249 	off = __va_arg(ap, int);
250 	proto = __va_arg(ap, int);
251 	__va_end(ap);
252 
253 	iphlen = off;
254 	udpstat.udps_ipackets++;
255 
256 	/*
257 	 * Strip IP options, if any; should skip this,
258 	 * make available to user, and use on returned packets,
259 	 * but we don't yet have a way to check the checksum
260 	 * with options still present.
261 	 */
262 	if (iphlen > sizeof(struct ip)) {
263 		ip_stripoptions(m);
264 		iphlen = sizeof(struct ip);
265 	}
266 
267 	/*
268 	 * IP and UDP headers are together in first mbuf.
269 	 * Already checked and pulled up in ip_demux().
270 	 */
271 	KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
272 	    ("UDP header not in one mbuf"));
273 
274 	ip = mtod(m, struct ip *);
275 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
276 
277 	/* destination port of 0 is illegal, based on RFC768. */
278 	if (uh->uh_dport == 0)
279 		goto bad;
280 
281 	/*
282 	 * Make mbuf data length reflect UDP length.
283 	 * If not enough data to reflect UDP length, drop.
284 	 */
285 	len = ntohs((u_short)uh->uh_ulen);
286 	if (ip->ip_len != len) {
287 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
288 			udpstat.udps_badlen++;
289 			goto bad;
290 		}
291 		m_adj(m, len - ip->ip_len);
292 		/* ip->ip_len = len; */
293 	}
294 	/*
295 	 * Save a copy of the IP header in case we want restore it
296 	 * for sending an ICMP error message in response.
297 	 */
298 	save_ip = *ip;
299 
300 	/*
301 	 * Checksum extended UDP header and data.
302 	 */
303 	if (uh->uh_sum) {
304 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
305 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
306 				uh->uh_sum = m->m_pkthdr.csum_data;
307 			else
308 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
309 				    ip->ip_dst.s_addr, htonl((u_short)len +
310 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
311 			uh->uh_sum ^= 0xffff;
312 		} else {
313 			char b[9];
314 
315 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
316 			bzero(((struct ipovly *)ip)->ih_x1, 9);
317 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
318 			uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
319 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
320 		}
321 		if (uh->uh_sum) {
322 			udpstat.udps_badsum++;
323 			m_freem(m);
324 			return;
325 		}
326 	} else
327 		udpstat.udps_nosum++;
328 
329 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
330 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
331 		struct inpcb *last;
332 
333 		/*
334 		 * Deliver a multicast or broadcast datagram to *all* sockets
335 		 * for which the local and remote addresses and ports match
336 		 * those of the incoming datagram.  This allows more than
337 		 * one process to receive multi/broadcasts on the same port.
338 		 * (This really ought to be done for unicast datagrams as
339 		 * well, but that would cause problems with existing
340 		 * applications that open both address-specific sockets and
341 		 * a wildcard socket listening to the same port -- they would
342 		 * end up receiving duplicates of every unicast datagram.
343 		 * Those applications open the multiple sockets to overcome an
344 		 * inadequacy of the UDP socket interface, but for backwards
345 		 * compatibility we avoid the problem here rather than
346 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
347 		 */
348 
349 		/*
350 		 * Construct sockaddr format source address.
351 		 */
352 		udp_in.sin_port = uh->uh_sport;
353 		udp_in.sin_addr = ip->ip_src;
354 		/*
355 		 * Locate pcb(s) for datagram.
356 		 * (Algorithm copied from raw_intr().)
357 		 */
358 		last = NULL;
359 #ifdef INET6
360 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
361 #endif
362 		LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
363 			if (inp->inp_flags & INP_PLACEMARKER)
364 				continue;
365 #ifdef INET6
366 			if (!(inp->inp_vflag & INP_IPV4))
367 				continue;
368 #endif
369 			if (inp->inp_lport != uh->uh_dport)
370 				continue;
371 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
372 				if (inp->inp_laddr.s_addr !=
373 				    ip->ip_dst.s_addr)
374 					continue;
375 			}
376 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
377 				if (inp->inp_faddr.s_addr !=
378 				    ip->ip_src.s_addr ||
379 				    inp->inp_fport != uh->uh_sport)
380 					continue;
381 			}
382 
383 			if (check_multicast_membership(ip, inp, m) < 0)
384 				continue;
385 
386 			if (last != NULL) {
387 				struct mbuf *n;
388 
389 #ifdef IPSEC
390 				/* check AH/ESP integrity. */
391 				if (ipsec4_in_reject_so(m, last->inp_socket))
392 					ipsecstat.in_polvio++;
393 					/* do not inject data to pcb */
394 				else
395 #endif /*IPSEC*/
396 #ifdef FAST_IPSEC
397 				/* check AH/ESP integrity. */
398 				if (ipsec4_in_reject(m, last))
399 					;
400 				else
401 #endif /*FAST_IPSEC*/
402 				if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
403 					udp_append(last, ip, n,
404 						   iphlen +
405 						   sizeof(struct udphdr));
406 			}
407 			last = inp;
408 			/*
409 			 * Don't look for additional matches if this one does
410 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
411 			 * socket options set.  This heuristic avoids searching
412 			 * through all pcbs in the common case of a non-shared
413 			 * port.  It * assumes that an application will never
414 			 * clear these options after setting them.
415 			 */
416 			if (!(last->inp_socket->so_options &
417 			    (SO_REUSEPORT | SO_REUSEADDR)))
418 				break;
419 		}
420 
421 		if (last == NULL) {
422 			/*
423 			 * No matching pcb found; discard datagram.
424 			 * (No need to send an ICMP Port Unreachable
425 			 * for a broadcast or multicast datgram.)
426 			 */
427 			udpstat.udps_noportbcast++;
428 			goto bad;
429 		}
430 #ifdef IPSEC
431 		/* check AH/ESP integrity. */
432 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
433 			ipsecstat.in_polvio++;
434 			goto bad;
435 		}
436 #endif /*IPSEC*/
437 #ifdef FAST_IPSEC
438 		/* check AH/ESP integrity. */
439 		if (ipsec4_in_reject(m, last))
440 			goto bad;
441 #endif /*FAST_IPSEC*/
442 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
443 		return;
444 	}
445 	/*
446 	 * Locate pcb for datagram.
447 	 */
448 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
449 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
450 	if (inp == NULL) {
451 		if (log_in_vain) {
452 			char buf[sizeof "aaa.bbb.ccc.ddd"];
453 
454 			strcpy(buf, inet_ntoa(ip->ip_dst));
455 			log(LOG_INFO,
456 			    "Connection attempt to UDP %s:%d from %s:%d\n",
457 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
458 			    ntohs(uh->uh_sport));
459 		}
460 		udpstat.udps_noport++;
461 		if (m->m_flags & (M_BCAST | M_MCAST)) {
462 			udpstat.udps_noportbcast++;
463 			goto bad;
464 		}
465 		if (blackhole)
466 			goto bad;
467 #ifdef ICMP_BANDLIM
468 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
469 			goto bad;
470 #endif
471 		*ip = save_ip;
472 		ip->ip_len += iphlen;
473 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
474 		return;
475 	}
476 #ifdef IPSEC
477 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
478 		ipsecstat.in_polvio++;
479 		goto bad;
480 	}
481 #endif /*IPSEC*/
482 #ifdef FAST_IPSEC
483 	if (ipsec4_in_reject(m, inp))
484 		goto bad;
485 #endif /*FAST_IPSEC*/
486 	/*
487 	 * Check the minimum TTL for socket.
488 	 */
489 	if (ip->ip_ttl < inp->inp_ip_minttl)
490 		goto bad;
491 
492 	/*
493 	 * Construct sockaddr format source address.
494 	 * Stuff source address and datagram in user buffer.
495 	 */
496 	udp_in.sin_port = uh->uh_sport;
497 	udp_in.sin_addr = ip->ip_src;
498 	if ((inp->inp_flags & INP_CONTROLOPTS) ||
499 	    (inp->inp_socket->so_options & SO_TIMESTAMP)) {
500 #ifdef INET6
501 		if (inp->inp_vflag & INP_IPV6) {
502 			int savedflags;
503 
504 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
505 			savedflags = inp->inp_flags;
506 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
507 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
508 			inp->inp_flags = savedflags;
509 		} else
510 #endif
511 		ip_savecontrol(inp, &opts, ip, m);
512 	}
513 	m_adj(m, iphlen + sizeof(struct udphdr));
514 #ifdef INET6
515 	if (inp->inp_vflag & INP_IPV6) {
516 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
517 		append_sa = (struct sockaddr *)&udp_in6;
518 	} else
519 #endif
520 		append_sa = (struct sockaddr *)&udp_in;
521 	if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
522 		udpstat.udps_fullsock++;
523 		goto bad;
524 	}
525 	sorwakeup(inp->inp_socket);
526 	return;
527 bad:
528 	m_freem(m);
529 	if (opts)
530 		m_freem(opts);
531 	return;
532 }
533 
534 #ifdef INET6
535 static void
536 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
537 {
538 	bzero(ip6, sizeof *ip6);
539 
540 	ip6->ip6_vfc = IPV6_VERSION;
541 	ip6->ip6_plen = ip->ip_len;
542 	ip6->ip6_nxt = ip->ip_p;
543 	ip6->ip6_hlim = ip->ip_ttl;
544 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
545 		IPV6_ADDR_INT32_SMP;
546 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
547 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
548 }
549 #endif
550 
551 /*
552  * subroutine of udp_input(), mainly for source code readability.
553  * caller must properly init udp_ip6 and udp_in6 beforehand.
554  */
555 static void
556 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
557 {
558 	struct sockaddr *append_sa;
559 	struct mbuf *opts = NULL;
560 
561 	if (last->inp_flags & INP_CONTROLOPTS ||
562 	    last->inp_socket->so_options & SO_TIMESTAMP) {
563 #ifdef INET6
564 		if (last->inp_vflag & INP_IPV6) {
565 			int savedflags;
566 
567 			if (udp_ip6.uip6_init_done == 0) {
568 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
569 				udp_ip6.uip6_init_done = 1;
570 			}
571 			savedflags = last->inp_flags;
572 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
573 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
574 			last->inp_flags = savedflags;
575 		} else
576 #endif
577 		ip_savecontrol(last, &opts, ip, n);
578 	}
579 #ifdef INET6
580 	if (last->inp_vflag & INP_IPV6) {
581 		if (udp_in6.uin6_init_done == 0) {
582 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
583 			udp_in6.uin6_init_done = 1;
584 		}
585 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
586 	} else
587 #endif
588 		append_sa = (struct sockaddr *)&udp_in;
589 	m_adj(n, off);
590 	if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
591 		m_freem(n);
592 		if (opts)
593 			m_freem(opts);
594 		udpstat.udps_fullsock++;
595 	} else
596 		sorwakeup(last->inp_socket);
597 }
598 
599 /*
600  * Notify a udp user of an asynchronous error;
601  * just wake up so that he can collect error status.
602  */
603 void
604 udp_notify(struct inpcb *inp, int error)
605 {
606 	inp->inp_socket->so_error = error;
607 	sorwakeup(inp->inp_socket);
608 	sowwakeup(inp->inp_socket);
609 }
610 
611 void
612 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
613 {
614 	struct ip *ip = vip;
615 	struct udphdr *uh;
616 	void (*notify) (struct inpcb *, int) = udp_notify;
617 	struct in_addr faddr;
618 	struct inpcb *inp;
619 
620 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
621 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
622 		return;
623 
624 	if (PRC_IS_REDIRECT(cmd)) {
625 		ip = NULL;
626 		notify = in_rtchange;
627 	} else if (cmd == PRC_HOSTDEAD)
628 		ip = NULL;
629 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
630 		return;
631 	if (ip) {
632 		crit_enter();
633 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
634 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
635 					ip->ip_src, uh->uh_sport, 0, NULL);
636 		if (inp != NULL && inp->inp_socket != NULL)
637 			(*notify)(inp, inetctlerrmap[cmd]);
638 		crit_exit();
639 	} else
640 		in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
641 				notify);
642 }
643 
644 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
645 	    in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
646 
647 static int
648 udp_getcred(SYSCTL_HANDLER_ARGS)
649 {
650 	struct sockaddr_in addrs[2];
651 	struct inpcb *inp;
652 	int error;
653 
654 	error = suser(req->td);
655 	if (error)
656 		return (error);
657 	error = SYSCTL_IN(req, addrs, sizeof addrs);
658 	if (error)
659 		return (error);
660 	crit_enter();
661 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
662 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
663 	if (inp == NULL || inp->inp_socket == NULL) {
664 		error = ENOENT;
665 		goto out;
666 	}
667 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
668 out:
669 	crit_exit();
670 	return (error);
671 }
672 
673 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
674     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
675 
676 static int
677 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
678 	   struct mbuf *control, struct thread *td)
679 {
680 	struct udpiphdr *ui;
681 	int len = m->m_pkthdr.len;
682 	struct sockaddr_in *sin;	/* really is initialized before use */
683 	int error = 0, lport_any = 0;
684 
685 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
686 		error = EMSGSIZE;
687 		goto release;
688 	}
689 
690 	if (inp->inp_lport == 0) {	/* unbound socket */
691 		error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
692 		if (error)
693 			goto release;
694 		in_pcbinswildcardhash(inp);
695 		lport_any = 1;
696 	}
697 
698 	if (dstaddr != NULL) {		/* destination address specified */
699 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
700 			/* already connected */
701 			error = EISCONN;
702 			goto release;
703 		}
704 		sin = (struct sockaddr_in *)dstaddr;
705 		if (!prison_remote_ip(td, (struct sockaddr *)&sin)) {
706 			error = EAFNOSUPPORT; /* IPv6 only jail */
707 			goto release;
708 		}
709 	} else {
710 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
711 			/* no destination specified and not already connected */
712 			error = ENOTCONN;
713 			goto release;
714 		}
715 		sin = NULL;
716 	}
717 
718 	/*
719 	 * Calculate data length and get a mbuf
720 	 * for UDP and IP headers.
721 	 */
722 	M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
723 	if (m == NULL) {
724 		error = ENOBUFS;
725 		goto release;
726 	}
727 
728 	/*
729 	 * Fill in mbuf with extended UDP header
730 	 * and addresses and length put into network format.
731 	 */
732 	ui = mtod(m, struct udpiphdr *);
733 	bzero(ui->ui_x1, sizeof ui->ui_x1);	/* XXX still needed? */
734 	ui->ui_pr = IPPROTO_UDP;
735 
736 	/*
737 	 * Set destination address.
738 	 */
739 	if (dstaddr != NULL) {			/* use specified destination */
740 		ui->ui_dst = sin->sin_addr;
741 		ui->ui_dport = sin->sin_port;
742 	} else {				/* use connected destination */
743 		ui->ui_dst = inp->inp_faddr;
744 		ui->ui_dport = inp->inp_fport;
745 	}
746 
747 	/*
748 	 * Set source address.
749 	 */
750 	if (inp->inp_laddr.s_addr == INADDR_ANY) {
751 		struct sockaddr_in *if_sin;
752 
753 		if (dstaddr == NULL) {
754 			/*
755 			 * connect() had (or should have) failed because
756 			 * the interface had no IP address, but the
757 			 * application proceeded to call send() anyways.
758 			 */
759 			error = ENOTCONN;
760 			goto release;
761 		}
762 
763 		/* Look up outgoing interface. */
764 		if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td)))
765 			goto release;
766 		ui->ui_src = if_sin->sin_addr;	/* use address of interface */
767 	} else {
768 		ui->ui_src = inp->inp_laddr;	/* use non-null bound address */
769 	}
770 	ui->ui_sport = inp->inp_lport;
771 	KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
772 
773 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
774 
775 	/*
776 	 * Set up checksum and output datagram.
777 	 */
778 	if (udpcksum) {
779 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
780 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
781 		m->m_pkthdr.csum_flags = CSUM_UDP;
782 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
783 	} else {
784 		ui->ui_sum = 0;
785 	}
786 	((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
787 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
788 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
789 	udpstat.udps_opackets++;
790 
791 	error = ip_output(m, inp->inp_options, &inp->inp_route,
792 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) |
793 	    IP_DEBUGROUTE,
794 	    inp->inp_moptions, inp);
795 
796 	/*
797 	 * If this is the first data gram sent on an unbound and unconnected
798 	 * UDP socket, lport will be changed in this function.  If target
799 	 * CPU after this lport changing is no longer the current CPU, then
800 	 * free the route entry allocated on the current CPU.
801 	 */
802 	if (lport_any) {
803 		if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
804 		    inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
805 			struct route *ro = &inp->inp_route;
806 
807 			if (ro->ro_rt != NULL)
808 				RTFREE(ro->ro_rt);
809 			bzero(ro, sizeof(*ro));
810 		}
811 	}
812 	return (error);
813 
814 release:
815 	m_freem(m);
816 	return (error);
817 }
818 
819 u_long	udp_sendspace = 9216;		/* really max datagram size */
820 					/* 40 1K datagrams */
821 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
822     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
823 
824 u_long	udp_recvspace = 40 * (1024 +
825 #ifdef INET6
826 				      sizeof(struct sockaddr_in6)
827 #else
828 				      sizeof(struct sockaddr_in)
829 #endif
830 				      );
831 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
832     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
833 
834 static int
835 udp_abort(struct socket *so)
836 {
837 	struct inpcb *inp;
838 
839 	inp = so->so_pcb;
840 	if (inp == NULL)
841 		return EINVAL;	/* ??? possible? panic instead? */
842 	soisdisconnected(so);
843 	crit_enter();
844 	in_pcbdetach(inp);
845 	crit_exit();
846 	return 0;
847 }
848 
849 static int
850 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
851 {
852 	struct inpcb *inp;
853 	int error;
854 
855 	inp = so->so_pcb;
856 	if (inp != NULL)
857 		return EINVAL;
858 
859 	error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
860 	if (error)
861 		return error;
862 	crit_enter();
863 	error = in_pcballoc(so, &udbinfo);
864 	crit_exit();
865 	if (error)
866 		return error;
867 
868 	inp = (struct inpcb *)so->so_pcb;
869 	inp->inp_vflag |= INP_IPV4;
870 	inp->inp_ip_ttl = ip_defttl;
871 	return 0;
872 }
873 
874 static int
875 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
876 {
877 	struct sockaddr_in *sin = (struct sockaddr_in *)nam;
878 	struct inpcb *inp;
879 	int error;
880 
881 	inp = so->so_pcb;
882 	if (inp == NULL)
883 		return EINVAL;
884 	crit_enter();
885 	error = in_pcbbind(inp, nam, td);
886 	crit_exit();
887 	if (error == 0) {
888 		if (sin->sin_addr.s_addr != INADDR_ANY)
889 			inp->inp_flags |= INP_WASBOUND_NOTANY;
890 		in_pcbinswildcardhash(inp);
891 	}
892 	return error;
893 }
894 
895 static int
896 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
897 {
898 	struct inpcb *inp;
899 	int error;
900 
901 	inp = so->so_pcb;
902 	if (inp == NULL)
903 		return EINVAL;
904 	if (inp->inp_faddr.s_addr != INADDR_ANY)
905 		return EISCONN;
906 	error = 0;
907 	crit_enter();
908 	if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
909 	    inp->inp_laddr.s_addr == INADDR_ANY) {
910 		error = in_pcbbind(inp, NULL, td);
911 	}
912 	if (error == 0) {
913 		if (!prison_remote_ip(td, nam))
914 			return(EAFNOSUPPORT); /* IPv6 only jail */
915 		if (inp->inp_flags & INP_WILDCARD)
916 			in_pcbremwildcardhash(inp);
917 		error = in_pcbconnect(inp, nam, td);
918 	}
919 	crit_exit();
920 	if (error == 0) {
921 		soisconnected(so);
922 
923 		/*
924 		 * Make sure that the new target CPU is same as current CPU,
925 		 * if it is not, then we will have to free the route entry
926 		 * allocated on the current CPU.
927 		 */
928 		if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport,
929 		    inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) {
930 			struct route *ro = &inp->inp_route;
931 
932 			if (ro->ro_rt != NULL)
933 				RTFREE(ro->ro_rt);
934 			bzero(ro, sizeof(*ro));
935 		}
936 	} else if (error == EAFNOSUPPORT) {	/* connection dissolved */
937 		/*
938 		 * Follow traditional BSD behavior and retain
939 		 * the local port binding.  But, fix the old misbehavior
940 		 * of overwriting any previously bound local address.
941 		 */
942 		if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
943 			inp->inp_laddr.s_addr = INADDR_ANY;
944 		in_pcbinswildcardhash(inp);
945 	}
946 	return error;
947 }
948 
949 static int
950 udp_detach(struct socket *so)
951 {
952 	struct inpcb *inp;
953 
954 	inp = so->so_pcb;
955 	if (inp == NULL)
956 		return EINVAL;
957 	crit_enter();
958 	in_pcbdetach(inp);
959 	crit_exit();
960 	return 0;
961 }
962 
963 static int
964 udp_disconnect(struct socket *so)
965 {
966 	struct route *ro;
967 	struct inpcb *inp;
968 
969 	inp = so->so_pcb;
970 	if (inp == NULL)
971 		return EINVAL;
972 	if (inp->inp_faddr.s_addr == INADDR_ANY)
973 		return ENOTCONN;
974 
975 	crit_enter();
976 	in_pcbdisconnect(inp);
977 	crit_exit();
978 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
979 
980 	ro = &inp->inp_route;
981 	if (ro->ro_rt != NULL)
982 		RTFREE(ro->ro_rt);
983 	bzero(ro, sizeof(*ro));
984 
985 	return 0;
986 }
987 
988 static int
989 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
990 	    struct mbuf *control, struct thread *td)
991 {
992 	struct inpcb *inp;
993 
994 	inp = so->so_pcb;
995 	if (inp == NULL) {
996 		m_freem(m);
997 		return EINVAL;
998 	}
999 	return udp_output(inp, m, addr, control, td);
1000 }
1001 
1002 int
1003 udp_shutdown(struct socket *so)
1004 {
1005 	struct inpcb *inp;
1006 
1007 	inp = so->so_pcb;
1008 	if (inp == NULL)
1009 		return EINVAL;
1010 	socantsendmore(so);
1011 	return 0;
1012 }
1013 
1014 struct pr_usrreqs udp_usrreqs = {
1015 	.pru_abort = udp_abort,
1016 	.pru_accept = pru_accept_notsupp,
1017 	.pru_attach = udp_attach,
1018 	.pru_bind = udp_bind,
1019 	.pru_connect = udp_connect,
1020 	.pru_connect2 = pru_connect2_notsupp,
1021 	.pru_control = in_control,
1022 	.pru_detach = udp_detach,
1023 	.pru_disconnect = udp_disconnect,
1024 	.pru_listen = pru_listen_notsupp,
1025 	.pru_peeraddr = in_setpeeraddr,
1026 	.pru_rcvd = pru_rcvd_notsupp,
1027 	.pru_rcvoob = pru_rcvoob_notsupp,
1028 	.pru_send = udp_send,
1029 	.pru_sense = pru_sense_null,
1030 	.pru_shutdown = udp_shutdown,
1031 	.pru_sockaddr = in_setsockaddr,
1032 	.pru_sosend = sosendudp,
1033 	.pru_soreceive = soreceive,
1034 	.pru_sopoll = sopoll
1035 };
1036 
1037