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