xref: /freebsd/sys/netinet/udp_usrreq.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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 <stddef.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/malloc.h>
45 #include <sys/mbuf.h>
46 #include <sys/domain.h>
47 #include <sys/proc.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.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_RD,
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 	save_ip = *ip;
213 
214 	/*
215 	 * Checksum extended UDP header and data.
216 	 */
217 	if (uh->uh_sum) {
218 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
219 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
220 				uh->uh_sum = m->m_pkthdr.csum_data;
221 			else
222 	                	uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
223 				    ip->ip_dst.s_addr, htonl(ip->ip_len +
224 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
225 			uh->uh_sum ^= 0xffff;
226 		} else {
227 			bzero(((struct ipovly *)ip)->ih_x1, 9);
228 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
229 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
230 		}
231 		if (uh->uh_sum) {
232 			udpstat.udps_badsum++;
233 			m_freem(m);
234 			return;
235 		}
236 	}
237 
238 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
239 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
240 		struct inpcb *last;
241 		/*
242 		 * Deliver a multicast or broadcast datagram to *all* sockets
243 		 * for which the local and remote addresses and ports match
244 		 * those of the incoming datagram.  This allows more than
245 		 * one process to receive multi/broadcasts on the same port.
246 		 * (This really ought to be done for unicast datagrams as
247 		 * well, but that would cause problems with existing
248 		 * applications that open both address-specific sockets and
249 		 * a wildcard socket listening to the same port -- they would
250 		 * end up receiving duplicates of every unicast datagram.
251 		 * Those applications open the multiple sockets to overcome an
252 		 * inadequacy of the UDP socket interface, but for backwards
253 		 * compatibility we avoid the problem here rather than
254 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
255 		 */
256 
257 		/*
258 		 * Construct sockaddr format source address.
259 		 */
260 		udp_in.sin_port = uh->uh_sport;
261 		udp_in.sin_addr = ip->ip_src;
262 		/*
263 		 * Locate pcb(s) for datagram.
264 		 * (Algorithm copied from raw_intr().)
265 		 */
266 		last = NULL;
267 #ifdef INET6
268 		udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
269 #endif
270 		LIST_FOREACH(inp, &udb, inp_list) {
271 #ifdef INET6
272 			if ((inp->inp_vflag & INP_IPV4) == 0)
273 				continue;
274 #endif
275 			if (inp->inp_lport != uh->uh_dport)
276 				continue;
277 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
278 				if (inp->inp_laddr.s_addr !=
279 				    ip->ip_dst.s_addr)
280 					continue;
281 			}
282 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
283 				if (inp->inp_faddr.s_addr !=
284 				    ip->ip_src.s_addr ||
285 				    inp->inp_fport != uh->uh_sport)
286 					continue;
287 			}
288 
289 			if (last != NULL) {
290 				struct mbuf *n;
291 
292 #ifdef IPSEC
293 				/* check AH/ESP integrity. */
294 				if (ipsec4_in_reject_so(m, last->inp_socket))
295 					ipsecstat.in_polvio++;
296 					/* do not inject data to pcb */
297 				else
298 #endif /*IPSEC*/
299 				if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
300 					udp_append(last, ip, n,
301 						   iphlen +
302 						   sizeof(struct udphdr));
303 			}
304 			last = inp;
305 			/*
306 			 * Don't look for additional matches if this one does
307 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
308 			 * socket options set.  This heuristic avoids searching
309 			 * through all pcbs in the common case of a non-shared
310 			 * port.  It * assumes that an application will never
311 			 * clear these options after setting them.
312 			 */
313 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
314 				break;
315 		}
316 
317 		if (last == NULL) {
318 			/*
319 			 * No matching pcb found; discard datagram.
320 			 * (No need to send an ICMP Port Unreachable
321 			 * for a broadcast or multicast datgram.)
322 			 */
323 			udpstat.udps_noportbcast++;
324 			goto bad;
325 		}
326 #ifdef IPSEC
327 		/* check AH/ESP integrity. */
328 		if (ipsec4_in_reject_so(m, last->inp_socket)) {
329 			ipsecstat.in_polvio++;
330 			goto bad;
331 		}
332 #endif /*IPSEC*/
333 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
334 		return;
335 	}
336 	/*
337 	 * Locate pcb for datagram.
338 	 */
339 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
340 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
341 	if (inp == NULL) {
342 		if (log_in_vain) {
343 			char buf[4*sizeof "123"];
344 
345 			strcpy(buf, inet_ntoa(ip->ip_dst));
346 			log(LOG_INFO,
347 			    "Connection attempt to UDP %s:%d from %s:%d\n",
348 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
349 			    ntohs(uh->uh_sport));
350 		}
351 		udpstat.udps_noport++;
352 		if (m->m_flags & (M_BCAST | M_MCAST)) {
353 			udpstat.udps_noportbcast++;
354 			goto bad;
355 		}
356 		*ip = save_ip;
357 		if (badport_bandlim(0) < 0)
358 			goto bad;
359 		if (blackhole)
360 			goto bad;
361 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
362 		return;
363 	}
364 #ifdef IPSEC
365 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
366 		ipsecstat.in_polvio++;
367 		goto bad;
368 	}
369 #endif /*IPSEC*/
370 
371 	/*
372 	 * Construct sockaddr format source address.
373 	 * Stuff source address and datagram in user buffer.
374 	 */
375 	udp_in.sin_port = uh->uh_sport;
376 	udp_in.sin_addr = ip->ip_src;
377 	if (inp->inp_flags & INP_CONTROLOPTS
378 	    || inp->inp_socket->so_options & SO_TIMESTAMP) {
379 #ifdef INET6
380 		if (inp->inp_vflag & INP_IPV6) {
381 			int savedflags;
382 
383 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
384 			savedflags = inp->inp_flags;
385 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
386 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
387 			inp->inp_flags = savedflags;
388 		} else
389 #endif
390 		ip_savecontrol(inp, &opts, ip, m);
391 	}
392 	iphlen += sizeof(struct udphdr);
393 	m_adj(m, iphlen);
394 #ifdef INET6
395 	if (inp->inp_vflag & INP_IPV6) {
396 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
397 		append_sa = (struct sockaddr *)&udp_in6;
398 	} else
399 #endif
400 	append_sa = (struct sockaddr *)&udp_in;
401 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
402 		udpstat.udps_fullsock++;
403 		goto bad;
404 	}
405 	sorwakeup(inp->inp_socket);
406 	return;
407 bad:
408 	m_freem(m);
409 	if (opts)
410 		m_freem(opts);
411 	return;
412 }
413 
414 #ifdef INET6
415 static void
416 ip_2_ip6_hdr(ip6, ip)
417 	struct ip6_hdr *ip6;
418 	struct ip *ip;
419 {
420 	bzero(ip6, sizeof(*ip6));
421 
422 	ip6->ip6_vfc = IPV6_VERSION;
423 	ip6->ip6_plen = ip->ip_len;
424 	ip6->ip6_nxt = ip->ip_p;
425 	ip6->ip6_hlim = ip->ip_ttl;
426 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
427 		IPV6_ADDR_INT32_SMP;
428 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
429 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
430 }
431 #endif
432 
433 /*
434  * subroutine of udp_input(), mainly for source code readability.
435  * caller must properly init udp_ip6 and udp_in6 beforehand.
436  */
437 static void
438 udp_append(last, ip, n, off)
439 	struct inpcb *last;
440 	struct ip *ip;
441 	struct mbuf *n;
442 	int off;
443 {
444 	struct sockaddr *append_sa;
445 	struct mbuf *opts = 0;
446 
447 	if (last->inp_flags & INP_CONTROLOPTS ||
448 	    last->inp_socket->so_options & SO_TIMESTAMP) {
449 #ifdef INET6
450 		if (last->inp_vflag & INP_IPV6) {
451 			int savedflags;
452 
453 			if (udp_ip6.uip6_init_done == 0) {
454 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
455 				udp_ip6.uip6_init_done = 1;
456 			}
457 			savedflags = last->inp_flags;
458 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
459 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
460 			last->inp_flags = savedflags;
461 		} else
462 #endif
463 		ip_savecontrol(last, &opts, ip, n);
464 	}
465 #ifdef INET6
466 	if (last->inp_vflag & INP_IPV6) {
467 		if (udp_in6.uin6_init_done == 0) {
468 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
469 			udp_in6.uin6_init_done = 1;
470 		}
471 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
472 	} else
473 #endif
474 	append_sa = (struct sockaddr *)&udp_in;
475 	m_adj(n, off);
476 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
477 		m_freem(n);
478 		if (opts)
479 			m_freem(opts);
480 		udpstat.udps_fullsock++;
481 	} else
482 		sorwakeup(last->inp_socket);
483 }
484 
485 /*
486  * Notify a udp user of an asynchronous error;
487  * just wake up so that he can collect error status.
488  */
489 void
490 udp_notify(inp, errno)
491 	register struct inpcb *inp;
492 	int errno;
493 {
494 	inp->inp_socket->so_error = errno;
495 	sorwakeup(inp->inp_socket);
496 	sowwakeup(inp->inp_socket);
497 }
498 
499 void
500 udp_ctlinput(cmd, sa, vip)
501 	int cmd;
502 	struct sockaddr *sa;
503 	void *vip;
504 {
505 	register struct ip *ip = vip;
506 	register struct udphdr *uh;
507 
508 	if (!PRC_IS_REDIRECT(cmd) &&
509 	    ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
510 		return;
511 	if (ip) {
512 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
513 		in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
514 			cmd, udp_notify);
515 	} else
516 		in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
517 }
518 
519 static int
520 udp_pcblist(SYSCTL_HANDLER_ARGS)
521 {
522 	int error, i, n, s;
523 	struct inpcb *inp, **inp_list;
524 	inp_gen_t gencnt;
525 	struct xinpgen xig;
526 
527 	/*
528 	 * The process of preparing the TCB list is too time-consuming and
529 	 * resource-intensive to repeat twice on every request.
530 	 */
531 	if (req->oldptr == 0) {
532 		n = udbinfo.ipi_count;
533 		req->oldidx = 2 * (sizeof xig)
534 			+ (n + n/8) * sizeof(struct xinpcb);
535 		return 0;
536 	}
537 
538 	if (req->newptr != 0)
539 		return EPERM;
540 
541 	/*
542 	 * OK, now we're committed to doing something.
543 	 */
544 	s = splnet();
545 	gencnt = udbinfo.ipi_gencnt;
546 	n = udbinfo.ipi_count;
547 	splx(s);
548 
549 	xig.xig_len = sizeof xig;
550 	xig.xig_count = n;
551 	xig.xig_gen = gencnt;
552 	xig.xig_sogen = so_gencnt;
553 	error = SYSCTL_OUT(req, &xig, sizeof xig);
554 	if (error)
555 		return error;
556 
557 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
558 	if (inp_list == 0)
559 		return ENOMEM;
560 
561 	s = splnet();
562 	for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
563 	     inp = inp->inp_list.le_next) {
564 		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
565 			inp_list[i++] = inp;
566 	}
567 	splx(s);
568 	n = i;
569 
570 	error = 0;
571 	for (i = 0; i < n; i++) {
572 		inp = inp_list[i];
573 		if (inp->inp_gencnt <= gencnt) {
574 			struct xinpcb xi;
575 			xi.xi_len = sizeof xi;
576 			/* XXX should avoid extra copy */
577 			bcopy(inp, &xi.xi_inp, sizeof *inp);
578 			if (inp->inp_socket)
579 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
580 			error = SYSCTL_OUT(req, &xi, sizeof xi);
581 		}
582 	}
583 	if (!error) {
584 		/*
585 		 * Give the user an updated idea of our state.
586 		 * If the generation differs from what we told
587 		 * her before, she knows that something happened
588 		 * while we were processing this request, and it
589 		 * might be necessary to retry.
590 		 */
591 		s = splnet();
592 		xig.xig_gen = udbinfo.ipi_gencnt;
593 		xig.xig_sogen = so_gencnt;
594 		xig.xig_count = udbinfo.ipi_count;
595 		splx(s);
596 		error = SYSCTL_OUT(req, &xig, sizeof xig);
597 	}
598 	free(inp_list, M_TEMP);
599 	return error;
600 }
601 
602 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
603 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
604 
605 static int
606 udp_getcred(SYSCTL_HANDLER_ARGS)
607 {
608 	struct sockaddr_in addrs[2];
609 	struct inpcb *inp;
610 	int error, s;
611 
612 	error = suser(req->p);
613 	if (error)
614 		return (error);
615 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
616 	if (error)
617 		return (error);
618 	s = splnet();
619 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
620 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
621 	if (inp == NULL || inp->inp_socket == NULL) {
622 		error = ENOENT;
623 		goto out;
624 	}
625 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
626 out:
627 	splx(s);
628 	return (error);
629 }
630 
631 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
632     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
633 
634 static int
635 udp_output(inp, m, addr, control, p)
636 	register struct inpcb *inp;
637 	struct mbuf *m;
638 	struct sockaddr *addr;
639 	struct mbuf *control;
640 	struct proc *p;
641 {
642 	register struct udpiphdr *ui;
643 	register int len = m->m_pkthdr.len;
644 	struct in_addr laddr;
645 	struct sockaddr_in *sin;
646 	int s = 0, error = 0;
647 
648 	if (control)
649 		m_freem(control);		/* XXX */
650 
651 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
652 		error = EMSGSIZE;
653 		goto release;
654 	}
655 
656 	if (addr) {
657 		sin = (struct sockaddr_in *)addr;
658 		prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
659 		laddr = inp->inp_laddr;
660 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
661 			error = EISCONN;
662 			goto release;
663 		}
664 		/*
665 		 * Must block input while temporarily connected.
666 		 */
667 		s = splnet();
668 		error = in_pcbconnect(inp, addr, p);
669 		if (error) {
670 			splx(s);
671 			goto release;
672 		}
673 	} else {
674 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
675 			error = ENOTCONN;
676 			goto release;
677 		}
678 	}
679 	/*
680 	 * Calculate data length and get a mbuf
681 	 * for UDP and IP headers.
682 	 */
683 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
684 	if (m == 0) {
685 		error = ENOBUFS;
686 		if (addr)
687 			splx(s);
688 		goto release;
689 	}
690 
691 	/*
692 	 * Fill in mbuf with extended UDP header
693 	 * and addresses and length put into network format.
694 	 */
695 	ui = mtod(m, struct udpiphdr *);
696 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
697 	ui->ui_pr = IPPROTO_UDP;
698 	ui->ui_src = inp->inp_laddr;
699 	ui->ui_dst = inp->inp_faddr;
700 	ui->ui_sport = inp->inp_lport;
701 	ui->ui_dport = inp->inp_fport;
702 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
703 
704 	/*
705 	 * Set up checksum and output datagram.
706 	 */
707 	if (udpcksum) {
708         	ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
709 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
710 		m->m_pkthdr.csum_flags = CSUM_UDP;
711 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
712 	} else {
713 		ui->ui_sum = 0;
714 	}
715 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
716 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
717 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
718 	udpstat.udps_opackets++;
719 
720 #ifdef IPSEC
721 	ipsec_setsocket(m, inp->inp_socket);
722 #endif /*IPSEC*/
723 	error = ip_output(m, inp->inp_options, &inp->inp_route,
724 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
725 	    inp->inp_moptions);
726 
727 	if (addr) {
728 		in_pcbdisconnect(inp);
729 		inp->inp_laddr = laddr;	/* XXX rehash? */
730 		splx(s);
731 	}
732 	return (error);
733 
734 release:
735 	m_freem(m);
736 	return (error);
737 }
738 
739 u_long	udp_sendspace = 9216;		/* really max datagram size */
740 					/* 40 1K datagrams */
741 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
742     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
743 
744 u_long	udp_recvspace = 40 * (1024 +
745 #ifdef INET6
746 				      sizeof(struct sockaddr_in6)
747 #else
748 				      sizeof(struct sockaddr_in)
749 #endif
750 				      );
751 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
752     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
753 
754 static int
755 udp_abort(struct socket *so)
756 {
757 	struct inpcb *inp;
758 	int s;
759 
760 	inp = sotoinpcb(so);
761 	if (inp == 0)
762 		return EINVAL;	/* ??? possible? panic instead? */
763 	soisdisconnected(so);
764 	s = splnet();
765 	in_pcbdetach(inp);
766 	splx(s);
767 	return 0;
768 }
769 
770 static int
771 udp_attach(struct socket *so, int proto, struct proc *p)
772 {
773 	struct inpcb *inp;
774 	int s, error;
775 
776 	inp = sotoinpcb(so);
777 	if (inp != 0)
778 		return EINVAL;
779 
780 	error = soreserve(so, udp_sendspace, udp_recvspace);
781 	if (error)
782 		return error;
783 	s = splnet();
784 	error = in_pcballoc(so, &udbinfo, p);
785 	splx(s);
786 	if (error)
787 		return error;
788 
789 	inp = (struct inpcb *)so->so_pcb;
790 	inp->inp_vflag |= INP_IPV4;
791 	inp->inp_ip_ttl = ip_defttl;
792 #ifdef IPSEC
793 	error = ipsec_init_policy(so, &inp->inp_sp);
794 	if (error != 0) {
795 		in_pcbdetach(inp);
796 		return error;
797 	}
798 #endif /*IPSEC*/
799 	return 0;
800 }
801 
802 static int
803 udp_bind(struct socket *so, struct sockaddr *nam, 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 	s = splnet();
812 	error = in_pcbbind(inp, nam, p);
813 	splx(s);
814 	return error;
815 }
816 
817 static int
818 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
819 {
820 	struct inpcb *inp;
821 	int s, error;
822 	struct sockaddr_in *sin;
823 
824 	inp = sotoinpcb(so);
825 	if (inp == 0)
826 		return EINVAL;
827 	if (inp->inp_faddr.s_addr != INADDR_ANY)
828 		return EISCONN;
829 	s = splnet();
830 	sin = (struct sockaddr_in *)nam;
831 	prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
832 	error = in_pcbconnect(inp, nam, p);
833 	splx(s);
834 	if (error == 0)
835 		soisconnected(so);
836 	return error;
837 }
838 
839 static int
840 udp_detach(struct socket *so)
841 {
842 	struct inpcb *inp;
843 	int s;
844 
845 	inp = sotoinpcb(so);
846 	if (inp == 0)
847 		return EINVAL;
848 	s = splnet();
849 	in_pcbdetach(inp);
850 	splx(s);
851 	return 0;
852 }
853 
854 static int
855 udp_disconnect(struct socket *so)
856 {
857 	struct inpcb *inp;
858 	int s;
859 
860 	inp = sotoinpcb(so);
861 	if (inp == 0)
862 		return EINVAL;
863 	if (inp->inp_faddr.s_addr == INADDR_ANY)
864 		return ENOTCONN;
865 
866 	s = splnet();
867 	in_pcbdisconnect(inp);
868 	inp->inp_laddr.s_addr = INADDR_ANY;
869 	splx(s);
870 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
871 	return 0;
872 }
873 
874 static int
875 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
876 	    struct mbuf *control, struct proc *p)
877 {
878 	struct inpcb *inp;
879 
880 	inp = sotoinpcb(so);
881 	if (inp == 0) {
882 		m_freem(m);
883 		return EINVAL;
884 	}
885 	return udp_output(inp, m, addr, control, p);
886 }
887 
888 int
889 udp_shutdown(struct socket *so)
890 {
891 	struct inpcb *inp;
892 
893 	inp = sotoinpcb(so);
894 	if (inp == 0)
895 		return EINVAL;
896 	socantsendmore(so);
897 	return 0;
898 }
899 
900 struct pr_usrreqs udp_usrreqs = {
901 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
902 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
903 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
904 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
905 	in_setsockaddr, sosend, soreceive, sopoll
906 };
907