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