xref: /freebsd/sys/netinet/udp_usrreq.c (revision acd3428b7d3e94cef0e1881c868cb4b131d4ff41)
1 /*-
2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_ipfw.h"
34 #include "opt_ipsec.h"
35 #include "opt_inet6.h"
36 #include "opt_mac.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/eventhandler.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mbuf.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sx.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 
57 #include <vm/uma.h>
58 
59 #include <net/if.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #endif
70 #include <netinet/ip_icmp.h>
71 #include <netinet/icmp_var.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_options.h>
74 #ifdef INET6
75 #include <netinet6/ip6_var.h>
76 #endif
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
79 
80 #ifdef FAST_IPSEC
81 #include <netipsec/ipsec.h>
82 #endif /*FAST_IPSEC*/
83 
84 #ifdef IPSEC
85 #include <netinet6/ipsec.h>
86 #endif /*IPSEC*/
87 
88 #include <machine/in_cksum.h>
89 
90 #include <security/mac/mac_framework.h>
91 
92 /*
93  * UDP protocol implementation.
94  * Per RFC 768, August, 1980.
95  */
96 #ifndef	COMPAT_42
97 static int	udpcksum = 1;
98 #else
99 static int	udpcksum = 0;		/* XXX */
100 #endif
101 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
102 		&udpcksum, 0, "");
103 
104 int	log_in_vain = 0;
105 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
106     &log_in_vain, 0, "Log all incoming UDP packets");
107 
108 static int	blackhole = 0;
109 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
110 	&blackhole, 0, "Do not send port unreachables for refused connects");
111 
112 static int	strict_mcast_mship = 0;
113 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
114 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
115 
116 struct	inpcbhead udb;		/* from udp_var.h */
117 #define	udb6	udb  /* for KAME src sync over BSD*'s */
118 struct	inpcbinfo udbinfo;
119 
120 #ifndef UDBHASHSIZE
121 #define UDBHASHSIZE 16
122 #endif
123 
124 struct	udpstat udpstat;	/* from udp_var.h */
125 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
126     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
127 
128 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
129 		int off, struct sockaddr_in *udp_in);
130 
131 static void udp_detach(struct socket *so);
132 static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
133 		struct mbuf *, struct thread *);
134 
135 static void
136 udp_zone_change(void *tag)
137 {
138 
139 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
140 }
141 
142 static int
143 udp_inpcb_init(void *mem, int size, int flags)
144 {
145 	struct inpcb *inp = (struct inpcb *) mem;
146 	INP_LOCK_INIT(inp, "inp", "udpinp");
147 	return (0);
148 }
149 
150 void
151 udp_init()
152 {
153 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
154 	LIST_INIT(&udb);
155 	udbinfo.listhead = &udb;
156 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
157 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
158 					&udbinfo.porthashmask);
159 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
160 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
161 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
162 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
163 		EVENTHANDLER_PRI_ANY);
164 }
165 
166 void
167 udp_input(m, off)
168 	register struct mbuf *m;
169 	int off;
170 {
171 	int iphlen = off;
172 	register struct ip *ip;
173 	register struct udphdr *uh;
174 	register struct inpcb *inp;
175 	int len;
176 	struct ip save_ip;
177 	struct sockaddr_in udp_in;
178 #ifdef IPFIREWALL_FORWARD
179 	struct m_tag *fwd_tag;
180 #endif
181 
182 	udpstat.udps_ipackets++;
183 
184 	/*
185 	 * Strip IP options, if any; should skip this,
186 	 * make available to user, and use on returned packets,
187 	 * but we don't yet have a way to check the checksum
188 	 * with options still present.
189 	 */
190 	if (iphlen > sizeof (struct ip)) {
191 		ip_stripoptions(m, (struct mbuf *)0);
192 		iphlen = sizeof(struct ip);
193 	}
194 
195 	/*
196 	 * Get IP and UDP header together in first mbuf.
197 	 */
198 	ip = mtod(m, struct ip *);
199 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
200 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
201 			udpstat.udps_hdrops++;
202 			return;
203 		}
204 		ip = mtod(m, struct ip *);
205 	}
206 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
207 
208 	/* destination port of 0 is illegal, based on RFC768. */
209 	if (uh->uh_dport == 0)
210 		goto badunlocked;
211 
212 	/*
213 	 * Construct sockaddr format source address.
214 	 * Stuff source address and datagram in user buffer.
215 	 */
216 	bzero(&udp_in, sizeof(udp_in));
217 	udp_in.sin_len = sizeof(udp_in);
218 	udp_in.sin_family = AF_INET;
219 	udp_in.sin_port = uh->uh_sport;
220 	udp_in.sin_addr = ip->ip_src;
221 
222 	/*
223 	 * Make mbuf data length reflect UDP length.
224 	 * If not enough data to reflect UDP length, drop.
225 	 */
226 	len = ntohs((u_short)uh->uh_ulen);
227 	if (ip->ip_len != len) {
228 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
229 			udpstat.udps_badlen++;
230 			goto badunlocked;
231 		}
232 		m_adj(m, len - ip->ip_len);
233 		/* ip->ip_len = len; */
234 	}
235 	/*
236 	 * Save a copy of the IP header in case we want restore it
237 	 * for sending an ICMP error message in response.
238 	 */
239 	if (!blackhole)
240 		save_ip = *ip;
241 
242 	/*
243 	 * Checksum extended UDP header and data.
244 	 */
245 	if (uh->uh_sum) {
246 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
247 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
248 				uh->uh_sum = m->m_pkthdr.csum_data;
249 			else
250 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
251 				    ip->ip_dst.s_addr, htonl((u_short)len +
252 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
253 			uh->uh_sum ^= 0xffff;
254 		} else {
255 			char b[9];
256 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
257 			bzero(((struct ipovly *)ip)->ih_x1, 9);
258 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
259 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
260 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
261 		}
262 		if (uh->uh_sum) {
263 			udpstat.udps_badsum++;
264 			m_freem(m);
265 			return;
266 		}
267 	} else
268 		udpstat.udps_nosum++;
269 
270 #ifdef IPFIREWALL_FORWARD
271 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
272 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
273 
274 	if (fwd_tag != NULL) {
275 		struct sockaddr_in *next_hop;
276 
277 		/* Do the hack. */
278 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
279 		ip->ip_dst = next_hop->sin_addr;
280 		uh->uh_dport = ntohs(next_hop->sin_port);
281 		/* Remove the tag from the packet.  We don't need it anymore. */
282 		m_tag_delete(m, fwd_tag);
283 	}
284 #endif
285 
286 	INP_INFO_RLOCK(&udbinfo);
287 
288 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
289 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
290 		struct inpcb *last;
291 		/*
292 		 * Deliver a multicast or broadcast datagram to *all* sockets
293 		 * for which the local and remote addresses and ports match
294 		 * those of the incoming datagram.  This allows more than
295 		 * one process to receive multi/broadcasts on the same port.
296 		 * (This really ought to be done for unicast datagrams as
297 		 * well, but that would cause problems with existing
298 		 * applications that open both address-specific sockets and
299 		 * a wildcard socket listening to the same port -- they would
300 		 * end up receiving duplicates of every unicast datagram.
301 		 * Those applications open the multiple sockets to overcome an
302 		 * inadequacy of the UDP socket interface, but for backwards
303 		 * compatibility we avoid the problem here rather than
304 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
305 		 */
306 
307 		/*
308 		 * Locate pcb(s) for datagram.
309 		 * (Algorithm copied from raw_intr().)
310 		 */
311 		last = NULL;
312 		LIST_FOREACH(inp, &udb, inp_list) {
313 			if (inp->inp_lport != uh->uh_dport)
314 				continue;
315 #ifdef INET6
316 			if ((inp->inp_vflag & INP_IPV4) == 0)
317 				continue;
318 #endif
319 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
320 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
321 					continue;
322 			}
323 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
324 				if (inp->inp_faddr.s_addr !=
325 				    ip->ip_src.s_addr ||
326 				    inp->inp_fport != uh->uh_sport)
327 					continue;
328 			}
329 			INP_LOCK(inp);
330 
331 			/*
332 			 * Check multicast packets to make sure they are only
333 			 * sent to sockets with multicast memberships for the
334 			 * packet's destination address and arrival interface
335 			 */
336 #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
337 #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
338 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
339 				int mship, foundmship = 0;
340 
341 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
342 					if (MSHIP(inp, mship)->inm_addr.s_addr
343 					    == ip->ip_dst.s_addr &&
344 					    MSHIP(inp, mship)->inm_ifp
345 					    == m->m_pkthdr.rcvif) {
346 						foundmship = 1;
347 						break;
348 					}
349 				}
350 				if (foundmship == 0) {
351 					INP_UNLOCK(inp);
352 					continue;
353 				}
354 			}
355 #undef NMSHIPS
356 #undef MSHIP
357 			if (last != NULL) {
358 				struct mbuf *n;
359 
360 				n = m_copy(m, 0, M_COPYALL);
361 				if (n != NULL)
362 					udp_append(last, ip, n,
363 						   iphlen +
364 						   sizeof(struct udphdr),
365 						   &udp_in);
366 				INP_UNLOCK(last);
367 			}
368 			last = inp;
369 			/*
370 			 * Don't look for additional matches if this one does
371 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
372 			 * socket options set.  This heuristic avoids searching
373 			 * through all pcbs in the common case of a non-shared
374 			 * port.  It assumes that an application will never
375 			 * clear these options after setting them.
376 			 */
377 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
378 				break;
379 		}
380 
381 		if (last == NULL) {
382 			/*
383 			 * No matching pcb found; discard datagram.
384 			 * (No need to send an ICMP Port Unreachable
385 			 * for a broadcast or multicast datgram.)
386 			 */
387 			udpstat.udps_noportbcast++;
388 			goto badheadlocked;
389 		}
390 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
391 		    &udp_in);
392 		INP_UNLOCK(last);
393 		INP_INFO_RUNLOCK(&udbinfo);
394 		return;
395 	}
396 	/*
397 	 * Locate pcb for datagram.
398 	 */
399 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
400 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
401 	if (inp == NULL) {
402 		if (log_in_vain) {
403 			char buf[4*sizeof "123"];
404 
405 			strcpy(buf, inet_ntoa(ip->ip_dst));
406 			log(LOG_INFO,
407 			    "Connection attempt to UDP %s:%d from %s:%d\n",
408 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
409 			    ntohs(uh->uh_sport));
410 		}
411 		udpstat.udps_noport++;
412 		if (m->m_flags & (M_BCAST | M_MCAST)) {
413 			udpstat.udps_noportbcast++;
414 			goto badheadlocked;
415 		}
416 		if (blackhole)
417 			goto badheadlocked;
418 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
419 			goto badheadlocked;
420 		*ip = save_ip;
421 		ip->ip_len += iphlen;
422 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
423 		INP_INFO_RUNLOCK(&udbinfo);
424 		return;
425 	}
426 	INP_LOCK(inp);
427 	/* Check the minimum TTL for socket. */
428 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
429 		goto badheadlocked;
430 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
431 	INP_UNLOCK(inp);
432 	INP_INFO_RUNLOCK(&udbinfo);
433 	return;
434 
435 badheadlocked:
436 	if (inp)
437 		INP_UNLOCK(inp);
438 	INP_INFO_RUNLOCK(&udbinfo);
439 badunlocked:
440 	m_freem(m);
441 	return;
442 }
443 
444 /*
445  * Subroutine of udp_input(), which appends the provided mbuf chain to the
446  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
447  * contains the source address.  If the socket ends up being an IPv6 socket,
448  * udp_append() will convert to a sockaddr_in6 before passing the address
449  * into the socket code.
450  */
451 static void
452 udp_append(inp, ip, n, off, udp_in)
453 	struct inpcb *inp;
454 	struct ip *ip;
455 	struct mbuf *n;
456 	int off;
457 	struct sockaddr_in *udp_in;
458 {
459 	struct sockaddr *append_sa;
460 	struct socket *so;
461 	struct mbuf *opts = 0;
462 #ifdef INET6
463 	struct sockaddr_in6 udp_in6;
464 #endif
465 
466 	INP_LOCK_ASSERT(inp);
467 
468 #if defined(IPSEC) || defined(FAST_IPSEC)
469 	/* check AH/ESP integrity. */
470 	if (ipsec4_in_reject(n, inp)) {
471 #ifdef IPSEC
472 		ipsecstat.in_polvio++;
473 #endif /*IPSEC*/
474 		m_freem(n);
475 		return;
476 	}
477 #endif /*IPSEC || FAST_IPSEC*/
478 #ifdef MAC
479 	if (mac_check_inpcb_deliver(inp, n) != 0) {
480 		m_freem(n);
481 		return;
482 	}
483 #endif
484 	if (inp->inp_flags & INP_CONTROLOPTS ||
485 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
486 #ifdef INET6
487 		if (inp->inp_vflag & INP_IPV6) {
488 			int savedflags;
489 
490 			savedflags = inp->inp_flags;
491 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
492 			ip6_savecontrol(inp, n, &opts);
493 			inp->inp_flags = savedflags;
494 		} else
495 #endif
496 		ip_savecontrol(inp, &opts, ip, n);
497 	}
498 #ifdef INET6
499 	if (inp->inp_vflag & INP_IPV6) {
500 		bzero(&udp_in6, sizeof(udp_in6));
501 		udp_in6.sin6_len = sizeof(udp_in6);
502 		udp_in6.sin6_family = AF_INET6;
503 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
504 		append_sa = (struct sockaddr *)&udp_in6;
505 	} else
506 #endif
507 	append_sa = (struct sockaddr *)udp_in;
508 	m_adj(n, off);
509 
510 	so = inp->inp_socket;
511 	SOCKBUF_LOCK(&so->so_rcv);
512 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
513 		m_freem(n);
514 		if (opts)
515 			m_freem(opts);
516 		udpstat.udps_fullsock++;
517 		SOCKBUF_UNLOCK(&so->so_rcv);
518 	} else
519 		sorwakeup_locked(so);
520 }
521 
522 /*
523  * Notify a udp user of an asynchronous error;
524  * just wake up so that he can collect error status.
525  */
526 struct inpcb *
527 udp_notify(inp, errno)
528 	register struct inpcb *inp;
529 	int errno;
530 {
531 	inp->inp_socket->so_error = errno;
532 	sorwakeup(inp->inp_socket);
533 	sowwakeup(inp->inp_socket);
534 	return inp;
535 }
536 
537 void
538 udp_ctlinput(cmd, sa, vip)
539 	int cmd;
540 	struct sockaddr *sa;
541 	void *vip;
542 {
543 	struct ip *ip = vip;
544 	struct udphdr *uh;
545 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
546 	struct in_addr faddr;
547 	struct inpcb *inp;
548 
549 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
550 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
551 		return;
552 
553 	/*
554 	 * Redirects don't need to be handled up here.
555 	 */
556 	if (PRC_IS_REDIRECT(cmd))
557 		return;
558 	/*
559 	 * Hostdead is ugly because it goes linearly through all PCBs.
560 	 * XXX: We never get this from ICMP, otherwise it makes an
561 	 * excellent DoS attack on machines with many connections.
562 	 */
563 	if (cmd == PRC_HOSTDEAD)
564 		ip = 0;
565 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
566 		return;
567 	if (ip) {
568 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
569 		INP_INFO_RLOCK(&udbinfo);
570 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
571 		    ip->ip_src, uh->uh_sport, 0, NULL);
572 		if (inp != NULL) {
573 			INP_LOCK(inp);
574 			if (inp->inp_socket != NULL) {
575 				(*notify)(inp, inetctlerrmap[cmd]);
576 			}
577 			INP_UNLOCK(inp);
578 		}
579 		INP_INFO_RUNLOCK(&udbinfo);
580 	} else
581 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
582 }
583 
584 static int
585 udp_pcblist(SYSCTL_HANDLER_ARGS)
586 {
587 	int error, i, n;
588 	struct inpcb *inp, **inp_list;
589 	inp_gen_t gencnt;
590 	struct xinpgen xig;
591 
592 	/*
593 	 * The process of preparing the TCB list is too time-consuming and
594 	 * resource-intensive to repeat twice on every request.
595 	 */
596 	if (req->oldptr == 0) {
597 		n = udbinfo.ipi_count;
598 		req->oldidx = 2 * (sizeof xig)
599 			+ (n + n/8) * sizeof(struct xinpcb);
600 		return 0;
601 	}
602 
603 	if (req->newptr != 0)
604 		return EPERM;
605 
606 	/*
607 	 * OK, now we're committed to doing something.
608 	 */
609 	INP_INFO_RLOCK(&udbinfo);
610 	gencnt = udbinfo.ipi_gencnt;
611 	n = udbinfo.ipi_count;
612 	INP_INFO_RUNLOCK(&udbinfo);
613 
614 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
615 		+ n * sizeof(struct xinpcb));
616 	if (error != 0)
617 		return (error);
618 
619 	xig.xig_len = sizeof xig;
620 	xig.xig_count = n;
621 	xig.xig_gen = gencnt;
622 	xig.xig_sogen = so_gencnt;
623 	error = SYSCTL_OUT(req, &xig, sizeof xig);
624 	if (error)
625 		return error;
626 
627 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
628 	if (inp_list == 0)
629 		return ENOMEM;
630 
631 	INP_INFO_RLOCK(&udbinfo);
632 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
633 	     inp = LIST_NEXT(inp, inp_list)) {
634 		INP_LOCK(inp);
635 		if (inp->inp_gencnt <= gencnt &&
636 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
637 			inp_list[i++] = inp;
638 		INP_UNLOCK(inp);
639 	}
640 	INP_INFO_RUNLOCK(&udbinfo);
641 	n = i;
642 
643 	error = 0;
644 	for (i = 0; i < n; i++) {
645 		inp = inp_list[i];
646 		INP_LOCK(inp);
647 		if (inp->inp_gencnt <= gencnt) {
648 			struct xinpcb xi;
649 			bzero(&xi, sizeof(xi));
650 			xi.xi_len = sizeof xi;
651 			/* XXX should avoid extra copy */
652 			bcopy(inp, &xi.xi_inp, sizeof *inp);
653 			if (inp->inp_socket)
654 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
655 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
656 			INP_UNLOCK(inp);
657 			error = SYSCTL_OUT(req, &xi, sizeof xi);
658 		} else
659 			INP_UNLOCK(inp);
660 	}
661 	if (!error) {
662 		/*
663 		 * Give the user an updated idea of our state.
664 		 * If the generation differs from what we told
665 		 * her before, she knows that something happened
666 		 * while we were processing this request, and it
667 		 * might be necessary to retry.
668 		 */
669 		INP_INFO_RLOCK(&udbinfo);
670 		xig.xig_gen = udbinfo.ipi_gencnt;
671 		xig.xig_sogen = so_gencnt;
672 		xig.xig_count = udbinfo.ipi_count;
673 		INP_INFO_RUNLOCK(&udbinfo);
674 		error = SYSCTL_OUT(req, &xig, sizeof xig);
675 	}
676 	free(inp_list, M_TEMP);
677 	return error;
678 }
679 
680 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
681 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
682 
683 static int
684 udp_getcred(SYSCTL_HANDLER_ARGS)
685 {
686 	struct xucred xuc;
687 	struct sockaddr_in addrs[2];
688 	struct inpcb *inp;
689 	int error;
690 
691 	error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
692 	    SUSER_ALLOWJAIL);
693 	if (error)
694 		return (error);
695 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
696 	if (error)
697 		return (error);
698 	INP_INFO_RLOCK(&udbinfo);
699 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
700 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
701 	if (inp == NULL || inp->inp_socket == NULL) {
702 		error = ENOENT;
703 		goto out;
704 	}
705 	error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
706 	if (error)
707 		goto out;
708 	cru2x(inp->inp_socket->so_cred, &xuc);
709 out:
710 	INP_INFO_RUNLOCK(&udbinfo);
711 	if (error == 0)
712 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
713 	return (error);
714 }
715 
716 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
717     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
718     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
719 
720 static int
721 udp_output(inp, m, addr, control, td)
722 	register struct inpcb *inp;
723 	struct mbuf *m;
724 	struct sockaddr *addr;
725 	struct mbuf *control;
726 	struct thread *td;
727 {
728 	register struct udpiphdr *ui;
729 	register int len = m->m_pkthdr.len;
730 	struct in_addr faddr, laddr;
731 	struct cmsghdr *cm;
732 	struct sockaddr_in *sin, src;
733 	int error = 0;
734 	int ipflags;
735 	u_short fport, lport;
736 	int unlock_udbinfo;
737 
738 	/*
739 	 * udp_output() may need to temporarily bind or connect the current
740 	 * inpcb.  As such, we don't know up front what inpcb locks we will
741 	 * need.  Do any work to decide what is needed up front before
742 	 * acquiring locks.
743 	 */
744 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
745 		if (control)
746 			m_freem(control);
747 		m_freem(m);
748 		return EMSGSIZE;
749 	}
750 
751 	src.sin_addr.s_addr = INADDR_ANY;
752 	if (control != NULL) {
753 		/*
754 		 * XXX: Currently, we assume all the optional information
755 		 * is stored in a single mbuf.
756 		 */
757 		if (control->m_next) {
758 			m_freem(control);
759 			m_freem(m);
760 			return EINVAL;
761 		}
762 		for (; control->m_len > 0;
763 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
764 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
765 			cm = mtod(control, struct cmsghdr *);
766 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
767 			    cm->cmsg_len > control->m_len) {
768 				error = EINVAL;
769 				break;
770 			}
771 			if (cm->cmsg_level != IPPROTO_IP)
772 				continue;
773 
774 			switch (cm->cmsg_type) {
775 			case IP_SENDSRCADDR:
776 				if (cm->cmsg_len !=
777 				    CMSG_LEN(sizeof(struct in_addr))) {
778 					error = EINVAL;
779 					break;
780 				}
781 				bzero(&src, sizeof(src));
782 				src.sin_family = AF_INET;
783 				src.sin_len = sizeof(src);
784 				src.sin_port = inp->inp_lport;
785 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
786 				break;
787 			default:
788 				error = ENOPROTOOPT;
789 				break;
790 			}
791 			if (error)
792 				break;
793 		}
794 		m_freem(control);
795 	}
796 	if (error) {
797 		m_freem(m);
798 		return error;
799 	}
800 
801 	if (src.sin_addr.s_addr != INADDR_ANY ||
802 	    addr != NULL) {
803 		INP_INFO_WLOCK(&udbinfo);
804 		unlock_udbinfo = 1;
805 	} else
806 		unlock_udbinfo = 0;
807 	INP_LOCK(inp);
808 
809 #ifdef MAC
810 	mac_create_mbuf_from_inpcb(inp, m);
811 #endif
812 
813 	laddr = inp->inp_laddr;
814 	lport = inp->inp_lport;
815 	if (src.sin_addr.s_addr != INADDR_ANY) {
816 		if (lport == 0) {
817 			error = EINVAL;
818 			goto release;
819 		}
820 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
821 		    &laddr.s_addr, &lport, td->td_ucred);
822 		if (error)
823 			goto release;
824 	}
825 
826 	if (addr) {
827 		sin = (struct sockaddr_in *)addr;
828 		if (jailed(td->td_ucred))
829 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
830 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
831 			error = EISCONN;
832 			goto release;
833 		}
834 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
835 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
836 		if (error)
837 			goto release;
838 
839 		/* Commit the local port if newly assigned. */
840 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
841 		    inp->inp_lport == 0) {
842 			/*
843 			 * Remember addr if jailed, to prevent rebinding.
844 			 */
845 			if (jailed(td->td_ucred))
846 				inp->inp_laddr = laddr;
847 			inp->inp_lport = lport;
848 			if (in_pcbinshash(inp) != 0) {
849 				inp->inp_lport = 0;
850 				error = EAGAIN;
851 				goto release;
852 			}
853 			inp->inp_flags |= INP_ANONPORT;
854 		}
855 	} else {
856 		faddr = inp->inp_faddr;
857 		fport = inp->inp_fport;
858 		if (faddr.s_addr == INADDR_ANY) {
859 			error = ENOTCONN;
860 			goto release;
861 		}
862 	}
863 
864 	/*
865 	 * Calculate data length and get a mbuf for UDP, IP, and possible
866 	 * link-layer headers.  Immediate slide the data pointer back forward
867 	 * since we won't use that space at this layer.
868 	 */
869 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
870 	if (m == NULL) {
871 		error = ENOBUFS;
872 		goto release;
873 	}
874 	m->m_data += max_linkhdr;
875 	m->m_len -= max_linkhdr;
876 	m->m_pkthdr.len -= max_linkhdr;
877 
878 	/*
879 	 * Fill in mbuf with extended UDP header
880 	 * and addresses and length put into network format.
881 	 */
882 	ui = mtod(m, struct udpiphdr *);
883 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
884 	ui->ui_pr = IPPROTO_UDP;
885 	ui->ui_src = laddr;
886 	ui->ui_dst = faddr;
887 	ui->ui_sport = lport;
888 	ui->ui_dport = fport;
889 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
890 
891 	/*
892 	 * Set the Don't Fragment bit in the IP header.
893 	 */
894 	if (inp->inp_flags & INP_DONTFRAG) {
895 		struct ip *ip;
896 		ip = (struct ip *)&ui->ui_i;
897 		ip->ip_off |= IP_DF;
898 	}
899 
900 	ipflags = 0;
901 	if (inp->inp_socket->so_options & SO_DONTROUTE)
902 		ipflags |= IP_ROUTETOIF;
903 	if (inp->inp_socket->so_options & SO_BROADCAST)
904 		ipflags |= IP_ALLOWBROADCAST;
905 	if (inp->inp_flags & INP_ONESBCAST)
906 		ipflags |= IP_SENDONES;
907 
908 	/*
909 	 * Set up checksum and output datagram.
910 	 */
911 	if (udpcksum) {
912 		if (inp->inp_flags & INP_ONESBCAST)
913 			faddr.s_addr = INADDR_BROADCAST;
914 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
915 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
916 		m->m_pkthdr.csum_flags = CSUM_UDP;
917 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
918 	} else {
919 		ui->ui_sum = 0;
920 	}
921 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
922 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
923 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
924 	udpstat.udps_opackets++;
925 
926 	if (unlock_udbinfo)
927 		INP_INFO_WUNLOCK(&udbinfo);
928 	error = ip_output(m, inp->inp_options, NULL, ipflags,
929 	    inp->inp_moptions, inp);
930 	INP_UNLOCK(inp);
931 	return (error);
932 
933 release:
934 	INP_UNLOCK(inp);
935 	if (unlock_udbinfo)
936 		INP_INFO_WUNLOCK(&udbinfo);
937 	m_freem(m);
938 	return (error);
939 }
940 
941 u_long	udp_sendspace = 9216;		/* really max datagram size */
942 					/* 40 1K datagrams */
943 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
944     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
945 
946 u_long	udp_recvspace = 40 * (1024 +
947 #ifdef INET6
948 				      sizeof(struct sockaddr_in6)
949 #else
950 				      sizeof(struct sockaddr_in)
951 #endif
952 				      );
953 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
954     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
955 
956 static void
957 udp_abort(struct socket *so)
958 {
959 	struct inpcb *inp;
960 
961 	inp = sotoinpcb(so);
962 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
963 	INP_INFO_WLOCK(&udbinfo);
964 	INP_LOCK(inp);
965 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
966 		in_pcbdisconnect(inp);
967 		inp->inp_laddr.s_addr = INADDR_ANY;
968 		soisdisconnected(so);
969 	}
970 	INP_UNLOCK(inp);
971 	INP_INFO_WUNLOCK(&udbinfo);
972 }
973 
974 static int
975 udp_attach(struct socket *so, int proto, struct thread *td)
976 {
977 	struct inpcb *inp;
978 	int error;
979 
980 	inp = sotoinpcb(so);
981 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
982 	error = soreserve(so, udp_sendspace, udp_recvspace);
983 	if (error)
984 		return error;
985 	INP_INFO_WLOCK(&udbinfo);
986 	error = in_pcballoc(so, &udbinfo);
987 	if (error) {
988 		INP_INFO_WUNLOCK(&udbinfo);
989 		return error;
990 	}
991 
992 	inp = (struct inpcb *)so->so_pcb;
993 	INP_INFO_WUNLOCK(&udbinfo);
994 	inp->inp_vflag |= INP_IPV4;
995 	inp->inp_ip_ttl = ip_defttl;
996 	INP_UNLOCK(inp);
997 	return 0;
998 }
999 
1000 static int
1001 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1002 {
1003 	struct inpcb *inp;
1004 	int error;
1005 
1006 	inp = sotoinpcb(so);
1007 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1008 	INP_INFO_WLOCK(&udbinfo);
1009 	INP_LOCK(inp);
1010 	error = in_pcbbind(inp, nam, td->td_ucred);
1011 	INP_UNLOCK(inp);
1012 	INP_INFO_WUNLOCK(&udbinfo);
1013 	return error;
1014 }
1015 
1016 static void
1017 udp_close(struct socket *so)
1018 {
1019 	struct inpcb *inp;
1020 
1021 	inp = sotoinpcb(so);
1022 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1023 	INP_INFO_WLOCK(&udbinfo);
1024 	INP_LOCK(inp);
1025 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1026 		in_pcbdisconnect(inp);
1027 		inp->inp_laddr.s_addr = INADDR_ANY;
1028 		soisdisconnected(so);
1029 	}
1030 	INP_UNLOCK(inp);
1031 	INP_INFO_WUNLOCK(&udbinfo);
1032 }
1033 
1034 static int
1035 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1036 {
1037 	struct inpcb *inp;
1038 	int error;
1039 	struct sockaddr_in *sin;
1040 
1041 	inp = sotoinpcb(so);
1042 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1043 	INP_INFO_WLOCK(&udbinfo);
1044 	INP_LOCK(inp);
1045 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1046 		INP_UNLOCK(inp);
1047 		INP_INFO_WUNLOCK(&udbinfo);
1048 		return EISCONN;
1049 	}
1050 	sin = (struct sockaddr_in *)nam;
1051 	if (jailed(td->td_ucred))
1052 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1053 	error = in_pcbconnect(inp, nam, td->td_ucred);
1054 	if (error == 0)
1055 		soisconnected(so);
1056 	INP_UNLOCK(inp);
1057 	INP_INFO_WUNLOCK(&udbinfo);
1058 	return error;
1059 }
1060 
1061 static void
1062 udp_detach(struct socket *so)
1063 {
1064 	struct inpcb *inp;
1065 
1066 	inp = sotoinpcb(so);
1067 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1068 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1069 	    ("udp_detach: not disconnected"));
1070 	INP_INFO_WLOCK(&udbinfo);
1071 	INP_LOCK(inp);
1072 	in_pcbdetach(inp);
1073 	in_pcbfree(inp);
1074 	INP_INFO_WUNLOCK(&udbinfo);
1075 }
1076 
1077 static int
1078 udp_disconnect(struct socket *so)
1079 {
1080 	struct inpcb *inp;
1081 
1082 	inp = sotoinpcb(so);
1083 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1084 	INP_INFO_WLOCK(&udbinfo);
1085 	INP_LOCK(inp);
1086 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1087 		INP_INFO_WUNLOCK(&udbinfo);
1088 		INP_UNLOCK(inp);
1089 		return ENOTCONN;
1090 	}
1091 
1092 	in_pcbdisconnect(inp);
1093 	inp->inp_laddr.s_addr = INADDR_ANY;
1094 	SOCK_LOCK(so);
1095 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1096 	SOCK_UNLOCK(so);
1097 	INP_UNLOCK(inp);
1098 	INP_INFO_WUNLOCK(&udbinfo);
1099 	return 0;
1100 }
1101 
1102 static int
1103 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1104 	    struct mbuf *control, struct thread *td)
1105 {
1106 	struct inpcb *inp;
1107 
1108 	inp = sotoinpcb(so);
1109 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1110 	return udp_output(inp, m, addr, control, td);
1111 }
1112 
1113 int
1114 udp_shutdown(struct socket *so)
1115 {
1116 	struct inpcb *inp;
1117 
1118 	inp = sotoinpcb(so);
1119 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1120 	INP_LOCK(inp);
1121 	socantsendmore(so);
1122 	INP_UNLOCK(inp);
1123 	return 0;
1124 }
1125 
1126 /*
1127  * This is the wrapper function for in_setsockaddr.  We just pass down
1128  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1129  * here because in_setsockaddr will call malloc and might block.
1130  */
1131 static int
1132 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1133 {
1134 	return (in_setsockaddr(so, nam, &udbinfo));
1135 }
1136 
1137 /*
1138  * This is the wrapper function for in_setpeeraddr.  We just pass down
1139  * the pcbinfo for in_setpeeraddr to lock.
1140  */
1141 static int
1142 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1143 {
1144 	return (in_setpeeraddr(so, nam, &udbinfo));
1145 }
1146 
1147 struct pr_usrreqs udp_usrreqs = {
1148 	.pru_abort =		udp_abort,
1149 	.pru_attach =		udp_attach,
1150 	.pru_bind =		udp_bind,
1151 	.pru_connect =		udp_connect,
1152 	.pru_control =		in_control,
1153 	.pru_detach =		udp_detach,
1154 	.pru_disconnect =	udp_disconnect,
1155 	.pru_peeraddr =		udp_peeraddr,
1156 	.pru_send =		udp_send,
1157 	.pru_sosend =		sosend_dgram,
1158 	.pru_shutdown =		udp_shutdown,
1159 	.pru_sockaddr =		udp_sockaddr,
1160 	.pru_sosetlabel =	in_pcbsosetlabel,
1161 	.pru_close =		udp_close,
1162 };
1163