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