xref: /freebsd/sys/netinet/udp_usrreq.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
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 
53 #include <vm/vm_zone.h>
54 
55 #include <net/if.h>
56 #include <net/route.h>
57 
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/ip.h>
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #endif
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip_var.h>
67 #ifdef INET6
68 #include <netinet6/ip6_var.h>
69 #endif
70 #include <netinet/ip_icmp.h>
71 #include <netinet/icmp_var.h>
72 #include <netinet/udp.h>
73 #include <netinet/udp_var.h>
74 
75 #ifdef IPSEC
76 #include <netinet6/ipsec.h>
77 #endif /*IPSEC*/
78 
79 #include <machine/in_cksum.h>
80 
81 /*
82  * UDP protocol implementation.
83  * Per RFC 768, August, 1980.
84  */
85 #ifndef	COMPAT_42
86 static int	udpcksum = 1;
87 #else
88 static int	udpcksum = 0;		/* XXX */
89 #endif
90 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
91 		&udpcksum, 0, "");
92 
93 int	log_in_vain = 0;
94 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
95     &log_in_vain, 0, "Log all incoming UDP packets");
96 
97 static int	blackhole = 0;
98 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
99 	&blackhole, 0, "Do not send port unreachables for refused connects");
100 
101 struct	inpcbhead udb;		/* from udp_var.h */
102 #define	udb6	udb  /* for KAME src sync over BSD*'s */
103 struct	inpcbinfo udbinfo;
104 
105 #ifndef UDBHASHSIZE
106 #define UDBHASHSIZE 16
107 #endif
108 
109 struct	udpstat udpstat;	/* from udp_var.h */
110 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD,
111     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
112 
113 static struct	sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
114 #ifdef INET6
115 struct udp_in6 {
116 	struct sockaddr_in6	uin6_sin;
117 	u_char			uin6_init_done : 1;
118 } udp_in6 = {
119 	{ sizeof(udp_in6.uin6_sin), AF_INET6 },
120 	0
121 };
122 struct udp_ip6 {
123 	struct ip6_hdr		uip6_ip6;
124 	u_char			uip6_init_done : 1;
125 } udp_ip6;
126 #endif /* INET6 */
127 
128 static void udp_append __P((struct inpcb *last, struct ip *ip,
129 			    struct mbuf *n, int off));
130 #ifdef INET6
131 static void ip_2_ip6_hdr __P((struct ip6_hdr *ip6, struct ip *ip));
132 #endif
133 
134 static int udp_detach __P((struct socket *so));
135 static	int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *,
136 			    struct mbuf *, struct proc *));
137 
138 void
139 udp_init()
140 {
141 	LIST_INIT(&udb);
142 	udbinfo.listhead = &udb;
143 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
144 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
145 					&udbinfo.porthashmask);
146 	udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
147 				 ZONE_INTERRUPT, 0);
148 }
149 
150 void
151 udp_input(m, off, proto)
152 	register struct mbuf *m;
153 	int off, proto;
154 {
155 	int iphlen = off;
156 	register struct ip *ip;
157 	register struct udphdr *uh;
158 	register struct inpcb *inp;
159 	struct mbuf *opts = 0;
160 	int len;
161 	struct ip save_ip;
162 	struct sockaddr *append_sa;
163 
164 	udpstat.udps_ipackets++;
165 
166 	/*
167 	 * Strip IP options, if any; should skip this,
168 	 * make available to user, and use on returned packets,
169 	 * but we don't yet have a way to check the checksum
170 	 * with options still present.
171 	 */
172 	if (iphlen > sizeof (struct ip)) {
173 		ip_stripoptions(m, (struct mbuf *)0);
174 		iphlen = sizeof(struct ip);
175 	}
176 
177 	/*
178 	 * Get IP and UDP header together in first mbuf.
179 	 */
180 	ip = mtod(m, struct ip *);
181 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
182 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
183 			udpstat.udps_hdrops++;
184 			return;
185 		}
186 		ip = mtod(m, struct ip *);
187 	}
188 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
189 
190 	/* destination port of 0 is illegal, based on RFC768. */
191 	if (uh->uh_dport == 0)
192 		goto bad;
193 
194 	/*
195 	 * Make mbuf data length reflect UDP length.
196 	 * If not enough data to reflect UDP length, drop.
197 	 */
198 	len = ntohs((u_short)uh->uh_ulen);
199 	if (ip->ip_len != len) {
200 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
201 			udpstat.udps_badlen++;
202 			goto bad;
203 		}
204 		m_adj(m, len - ip->ip_len);
205 		/* ip->ip_len = len; */
206 	}
207 	/*
208 	 * Save a copy of the IP header in case we want restore it
209 	 * for sending an ICMP error message in response.
210 	 */
211 	if (!blackhole)
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((u_short)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 		if (badport_bandlim(0) < 0)
357 			goto bad;
358 		if (blackhole)
359 			goto bad;
360 		*ip = save_ip;
361 		ip->ip_len += iphlen;
362 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
363 		return;
364 	}
365 #ifdef IPSEC
366 	if (ipsec4_in_reject_so(m, inp->inp_socket)) {
367 		ipsecstat.in_polvio++;
368 		goto bad;
369 	}
370 #endif /*IPSEC*/
371 
372 	/*
373 	 * Construct sockaddr format source address.
374 	 * Stuff source address and datagram in user buffer.
375 	 */
376 	udp_in.sin_port = uh->uh_sport;
377 	udp_in.sin_addr = ip->ip_src;
378 	if (inp->inp_flags & INP_CONTROLOPTS
379 	    || inp->inp_socket->so_options & SO_TIMESTAMP) {
380 #ifdef INET6
381 		if (inp->inp_vflag & INP_IPV6) {
382 			int savedflags;
383 
384 			ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
385 			savedflags = inp->inp_flags;
386 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
387 			ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
388 			inp->inp_flags = savedflags;
389 		} else
390 #endif
391 		ip_savecontrol(inp, &opts, ip, m);
392 	}
393 	iphlen += sizeof(struct udphdr);
394 	m_adj(m, iphlen);
395 #ifdef INET6
396 	if (inp->inp_vflag & INP_IPV6) {
397 		in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
398 		append_sa = (struct sockaddr *)&udp_in6;
399 	} else
400 #endif
401 	append_sa = (struct sockaddr *)&udp_in;
402 	if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
403 		udpstat.udps_fullsock++;
404 		goto bad;
405 	}
406 	sorwakeup(inp->inp_socket);
407 	return;
408 bad:
409 	m_freem(m);
410 	if (opts)
411 		m_freem(opts);
412 	return;
413 }
414 
415 #ifdef INET6
416 static void
417 ip_2_ip6_hdr(ip6, ip)
418 	struct ip6_hdr *ip6;
419 	struct ip *ip;
420 {
421 	bzero(ip6, sizeof(*ip6));
422 
423 	ip6->ip6_vfc = IPV6_VERSION;
424 	ip6->ip6_plen = ip->ip_len;
425 	ip6->ip6_nxt = ip->ip_p;
426 	ip6->ip6_hlim = ip->ip_ttl;
427 	ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
428 		IPV6_ADDR_INT32_SMP;
429 	ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
430 	ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
431 }
432 #endif
433 
434 /*
435  * subroutine of udp_input(), mainly for source code readability.
436  * caller must properly init udp_ip6 and udp_in6 beforehand.
437  */
438 static void
439 udp_append(last, ip, n, off)
440 	struct inpcb *last;
441 	struct ip *ip;
442 	struct mbuf *n;
443 	int off;
444 {
445 	struct sockaddr *append_sa;
446 	struct mbuf *opts = 0;
447 
448 	if (last->inp_flags & INP_CONTROLOPTS ||
449 	    last->inp_socket->so_options & SO_TIMESTAMP) {
450 #ifdef INET6
451 		if (last->inp_vflag & INP_IPV6) {
452 			int savedflags;
453 
454 			if (udp_ip6.uip6_init_done == 0) {
455 				ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
456 				udp_ip6.uip6_init_done = 1;
457 			}
458 			savedflags = last->inp_flags;
459 			last->inp_flags &= ~INP_UNMAPPABLEOPTS;
460 			ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
461 			last->inp_flags = savedflags;
462 		} else
463 #endif
464 		ip_savecontrol(last, &opts, ip, n);
465 	}
466 #ifdef INET6
467 	if (last->inp_vflag & INP_IPV6) {
468 		if (udp_in6.uin6_init_done == 0) {
469 			in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
470 			udp_in6.uin6_init_done = 1;
471 		}
472 		append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
473 	} else
474 #endif
475 	append_sa = (struct sockaddr *)&udp_in;
476 	m_adj(n, off);
477 	if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
478 		m_freem(n);
479 		if (opts)
480 			m_freem(opts);
481 		udpstat.udps_fullsock++;
482 	} else
483 		sorwakeup(last->inp_socket);
484 }
485 
486 /*
487  * Notify a udp user of an asynchronous error;
488  * just wake up so that he can collect error status.
489  */
490 void
491 udp_notify(inp, errno)
492 	register struct inpcb *inp;
493 	int errno;
494 {
495 	inp->inp_socket->so_error = errno;
496 	sorwakeup(inp->inp_socket);
497 	sowwakeup(inp->inp_socket);
498 }
499 
500 void
501 udp_ctlinput(cmd, sa, vip)
502 	int cmd;
503 	struct sockaddr *sa;
504 	void *vip;
505 {
506 	register struct ip *ip = vip;
507 	register struct udphdr *uh;
508 
509 	if (!PRC_IS_REDIRECT(cmd) &&
510 	    ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0))
511 		return;
512 	if (ip) {
513 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
514 		in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport,
515 			cmd, udp_notify);
516 	} else
517 		in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify);
518 }
519 
520 static int
521 udp_pcblist(SYSCTL_HANDLER_ARGS)
522 {
523 	int error, i, n, s;
524 	struct inpcb *inp, **inp_list;
525 	inp_gen_t gencnt;
526 	struct xinpgen xig;
527 
528 	/*
529 	 * The process of preparing the TCB list is too time-consuming and
530 	 * resource-intensive to repeat twice on every request.
531 	 */
532 	if (req->oldptr == 0) {
533 		n = udbinfo.ipi_count;
534 		req->oldidx = 2 * (sizeof xig)
535 			+ (n + n/8) * sizeof(struct xinpcb);
536 		return 0;
537 	}
538 
539 	if (req->newptr != 0)
540 		return EPERM;
541 
542 	/*
543 	 * OK, now we're committed to doing something.
544 	 */
545 	s = splnet();
546 	gencnt = udbinfo.ipi_gencnt;
547 	n = udbinfo.ipi_count;
548 	splx(s);
549 
550 	xig.xig_len = sizeof xig;
551 	xig.xig_count = n;
552 	xig.xig_gen = gencnt;
553 	xig.xig_sogen = so_gencnt;
554 	error = SYSCTL_OUT(req, &xig, sizeof xig);
555 	if (error)
556 		return error;
557 
558 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
559 	if (inp_list == 0)
560 		return ENOMEM;
561 
562 	s = splnet();
563 	for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n;
564 	     inp = inp->inp_list.le_next) {
565 		if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp))
566 			inp_list[i++] = inp;
567 	}
568 	splx(s);
569 	n = i;
570 
571 	error = 0;
572 	for (i = 0; i < n; i++) {
573 		inp = inp_list[i];
574 		if (inp->inp_gencnt <= gencnt) {
575 			struct xinpcb xi;
576 			xi.xi_len = sizeof xi;
577 			/* XXX should avoid extra copy */
578 			bcopy(inp, &xi.xi_inp, sizeof *inp);
579 			if (inp->inp_socket)
580 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
581 			error = SYSCTL_OUT(req, &xi, sizeof xi);
582 		}
583 	}
584 	if (!error) {
585 		/*
586 		 * Give the user an updated idea of our state.
587 		 * If the generation differs from what we told
588 		 * her before, she knows that something happened
589 		 * while we were processing this request, and it
590 		 * might be necessary to retry.
591 		 */
592 		s = splnet();
593 		xig.xig_gen = udbinfo.ipi_gencnt;
594 		xig.xig_sogen = so_gencnt;
595 		xig.xig_count = udbinfo.ipi_count;
596 		splx(s);
597 		error = SYSCTL_OUT(req, &xig, sizeof xig);
598 	}
599 	free(inp_list, M_TEMP);
600 	return error;
601 }
602 
603 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
604 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
605 
606 static int
607 udp_getcred(SYSCTL_HANDLER_ARGS)
608 {
609 	struct sockaddr_in addrs[2];
610 	struct inpcb *inp;
611 	int error, s;
612 
613 	error = suser(req->p);
614 	if (error)
615 		return (error);
616 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
617 	if (error)
618 		return (error);
619 	s = splnet();
620 	inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
621 				addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
622 	if (inp == NULL || inp->inp_socket == NULL) {
623 		error = ENOENT;
624 		goto out;
625 	}
626 	error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
627 out:
628 	splx(s);
629 	return (error);
630 }
631 
632 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
633     0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
634 
635 static int
636 udp_output(inp, m, addr, control, p)
637 	register struct inpcb *inp;
638 	struct mbuf *m;
639 	struct sockaddr *addr;
640 	struct mbuf *control;
641 	struct proc *p;
642 {
643 	register struct udpiphdr *ui;
644 	register int len = m->m_pkthdr.len;
645 	struct in_addr laddr;
646 	struct sockaddr_in *sin;
647 	int s = 0, error = 0;
648 
649 	if (control)
650 		m_freem(control);		/* XXX */
651 
652 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
653 		error = EMSGSIZE;
654 		goto release;
655 	}
656 
657 	if (addr) {
658 		sin = (struct sockaddr_in *)addr;
659 		prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
660 		laddr = inp->inp_laddr;
661 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
662 			error = EISCONN;
663 			goto release;
664 		}
665 		/*
666 		 * Must block input while temporarily connected.
667 		 */
668 		s = splnet();
669 		error = in_pcbconnect(inp, addr, p);
670 		if (error) {
671 			splx(s);
672 			goto release;
673 		}
674 	} else {
675 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
676 			error = ENOTCONN;
677 			goto release;
678 		}
679 	}
680 	/*
681 	 * Calculate data length and get a mbuf
682 	 * for UDP and IP headers.
683 	 */
684 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
685 	if (m == 0) {
686 		error = ENOBUFS;
687 		if (addr)
688 			splx(s);
689 		goto release;
690 	}
691 
692 	/*
693 	 * Fill in mbuf with extended UDP header
694 	 * and addresses and length put into network format.
695 	 */
696 	ui = mtod(m, struct udpiphdr *);
697 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
698 	ui->ui_pr = IPPROTO_UDP;
699 	ui->ui_src = inp->inp_laddr;
700 	ui->ui_dst = inp->inp_faddr;
701 	ui->ui_sport = inp->inp_lport;
702 	ui->ui_dport = inp->inp_fport;
703 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
704 
705 	/*
706 	 * Set up checksum and output datagram.
707 	 */
708 	if (udpcksum) {
709         	ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
710 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
711 		m->m_pkthdr.csum_flags = CSUM_UDP;
712 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
713 	} else {
714 		ui->ui_sum = 0;
715 	}
716 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
717 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
718 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
719 	udpstat.udps_opackets++;
720 
721 #ifdef IPSEC
722 	ipsec_setsocket(m, inp->inp_socket);
723 #endif /*IPSEC*/
724 	error = ip_output(m, inp->inp_options, &inp->inp_route,
725 	    (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
726 	    inp->inp_moptions);
727 
728 	if (addr) {
729 		in_pcbdisconnect(inp);
730 		inp->inp_laddr = laddr;	/* XXX rehash? */
731 		splx(s);
732 	}
733 	return (error);
734 
735 release:
736 	m_freem(m);
737 	return (error);
738 }
739 
740 u_long	udp_sendspace = 9216;		/* really max datagram size */
741 					/* 40 1K datagrams */
742 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
743     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
744 
745 u_long	udp_recvspace = 40 * (1024 +
746 #ifdef INET6
747 				      sizeof(struct sockaddr_in6)
748 #else
749 				      sizeof(struct sockaddr_in)
750 #endif
751 				      );
752 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
753     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
754 
755 static int
756 udp_abort(struct socket *so)
757 {
758 	struct inpcb *inp;
759 	int s;
760 
761 	inp = sotoinpcb(so);
762 	if (inp == 0)
763 		return EINVAL;	/* ??? possible? panic instead? */
764 	soisdisconnected(so);
765 	s = splnet();
766 	in_pcbdetach(inp);
767 	splx(s);
768 	return 0;
769 }
770 
771 static int
772 udp_attach(struct socket *so, int proto, struct proc *p)
773 {
774 	struct inpcb *inp;
775 	int s, error;
776 
777 	inp = sotoinpcb(so);
778 	if (inp != 0)
779 		return EINVAL;
780 
781 	error = soreserve(so, udp_sendspace, udp_recvspace);
782 	if (error)
783 		return error;
784 	s = splnet();
785 	error = in_pcballoc(so, &udbinfo, p);
786 	splx(s);
787 	if (error)
788 		return error;
789 
790 	inp = (struct inpcb *)so->so_pcb;
791 	inp->inp_vflag |= INP_IPV4;
792 	inp->inp_ip_ttl = ip_defttl;
793 #ifdef IPSEC
794 	error = ipsec_init_policy(so, &inp->inp_sp);
795 	if (error != 0) {
796 		in_pcbdetach(inp);
797 		return error;
798 	}
799 #endif /*IPSEC*/
800 	return 0;
801 }
802 
803 static int
804 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
805 {
806 	struct inpcb *inp;
807 	int s, error;
808 
809 	inp = sotoinpcb(so);
810 	if (inp == 0)
811 		return EINVAL;
812 	s = splnet();
813 	error = in_pcbbind(inp, nam, p);
814 	splx(s);
815 	return error;
816 }
817 
818 static int
819 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p)
820 {
821 	struct inpcb *inp;
822 	int s, error;
823 	struct sockaddr_in *sin;
824 
825 	inp = sotoinpcb(so);
826 	if (inp == 0)
827 		return EINVAL;
828 	if (inp->inp_faddr.s_addr != INADDR_ANY)
829 		return EISCONN;
830 	s = splnet();
831 	sin = (struct sockaddr_in *)nam;
832 	prison_remote_ip(p, 0, &sin->sin_addr.s_addr);
833 	error = in_pcbconnect(inp, nam, p);
834 	splx(s);
835 	if (error == 0)
836 		soisconnected(so);
837 	return error;
838 }
839 
840 static int
841 udp_detach(struct socket *so)
842 {
843 	struct inpcb *inp;
844 	int s;
845 
846 	inp = sotoinpcb(so);
847 	if (inp == 0)
848 		return EINVAL;
849 	s = splnet();
850 	in_pcbdetach(inp);
851 	splx(s);
852 	return 0;
853 }
854 
855 static int
856 udp_disconnect(struct socket *so)
857 {
858 	struct inpcb *inp;
859 	int s;
860 
861 	inp = sotoinpcb(so);
862 	if (inp == 0)
863 		return EINVAL;
864 	if (inp->inp_faddr.s_addr == INADDR_ANY)
865 		return ENOTCONN;
866 
867 	s = splnet();
868 	in_pcbdisconnect(inp);
869 	inp->inp_laddr.s_addr = INADDR_ANY;
870 	splx(s);
871 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
872 	return 0;
873 }
874 
875 static int
876 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
877 	    struct mbuf *control, struct proc *p)
878 {
879 	struct inpcb *inp;
880 
881 	inp = sotoinpcb(so);
882 	if (inp == 0) {
883 		m_freem(m);
884 		return EINVAL;
885 	}
886 	return udp_output(inp, m, addr, control, p);
887 }
888 
889 int
890 udp_shutdown(struct socket *so)
891 {
892 	struct inpcb *inp;
893 
894 	inp = sotoinpcb(so);
895 	if (inp == 0)
896 		return EINVAL;
897 	socantsendmore(so);
898 	return 0;
899 }
900 
901 struct pr_usrreqs udp_usrreqs = {
902 	udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
903 	pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
904 	pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
905 	pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
906 	in_setsockaddr, sosend, soreceive, sopoll
907 };
908