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