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