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