xref: /freebsd/sys/netinet/udp_usrreq.c (revision adfa0adec0b5d7c19c220a85ef6ca729235ed172)
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
30  * $FreeBSD$
31  */
32 
33 #include "opt_ipfw.h"
34 #include "opt_ipsec.h"
35 #include "opt_inet6.h"
36 #include "opt_mac.h"
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/eventhandler.h>
42 #include <sys/jail.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mac.h>
46 #include <sys/malloc.h>
47 #include <sys/mbuf.h>
48 #include <sys/proc.h>
49 #include <sys/protosw.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
53 #include <sys/sx.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
56 
57 #include <vm/uma.h>
58 
59 #include <net/if.h>
60 #include <net/route.h>
61 
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
67 #ifdef INET6
68 #include <netinet/ip6.h>
69 #endif
70 #include <netinet/ip_icmp.h>
71 #include <netinet/icmp_var.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_options.h>
74 #ifdef INET6
75 #include <netinet6/ip6_var.h>
76 #endif
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
79 
80 #ifdef FAST_IPSEC
81 #include <netipsec/ipsec.h>
82 #endif /*FAST_IPSEC*/
83 
84 #ifdef IPSEC
85 #include <netinet6/ipsec.h>
86 #endif /*IPSEC*/
87 
88 #include <machine/in_cksum.h>
89 
90 /*
91  * UDP protocol implementation.
92  * Per RFC 768, August, 1980.
93  */
94 #ifndef	COMPAT_42
95 static int	udpcksum = 1;
96 #else
97 static int	udpcksum = 0;		/* XXX */
98 #endif
99 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
100 		&udpcksum, 0, "");
101 
102 int	log_in_vain = 0;
103 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
104     &log_in_vain, 0, "Log all incoming UDP packets");
105 
106 static int	blackhole = 0;
107 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
108 	&blackhole, 0, "Do not send port unreachables for refused connects");
109 
110 static int	strict_mcast_mship = 0;
111 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
112 	&strict_mcast_mship, 0, "Only send multicast to member sockets");
113 
114 struct	inpcbhead udb;		/* from udp_var.h */
115 #define	udb6	udb  /* for KAME src sync over BSD*'s */
116 struct	inpcbinfo udbinfo;
117 
118 #ifndef UDBHASHSIZE
119 #define UDBHASHSIZE 16
120 #endif
121 
122 struct	udpstat udpstat;	/* from udp_var.h */
123 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
124     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
125 
126 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
127 		int off, struct sockaddr_in *udp_in);
128 
129 static void udp_detach(struct socket *so);
130 static	int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
131 		struct mbuf *, struct thread *);
132 
133 static void
134 udp_zone_change(void *tag)
135 {
136 
137 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
138 }
139 
140 static int
141 udp_inpcb_init(void *mem, int size, int flags)
142 {
143 	struct inpcb *inp = (struct inpcb *) mem;
144 	INP_LOCK_INIT(inp, "inp", "udpinp");
145 	return (0);
146 }
147 
148 void
149 udp_init()
150 {
151 	INP_INFO_LOCK_INIT(&udbinfo, "udp");
152 	LIST_INIT(&udb);
153 	udbinfo.listhead = &udb;
154 	udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
155 	udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
156 					&udbinfo.porthashmask);
157 	udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
158 	    NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
159 	uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
160 	EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
161 		EVENTHANDLER_PRI_ANY);
162 }
163 
164 void
165 udp_input(m, off)
166 	register struct mbuf *m;
167 	int off;
168 {
169 	int iphlen = off;
170 	register struct ip *ip;
171 	register struct udphdr *uh;
172 	register struct inpcb *inp;
173 	int len;
174 	struct ip save_ip;
175 	struct sockaddr_in udp_in;
176 #ifdef IPFIREWALL_FORWARD
177 	struct m_tag *fwd_tag;
178 #endif
179 
180 	udpstat.udps_ipackets++;
181 
182 	/*
183 	 * Strip IP options, if any; should skip this,
184 	 * make available to user, and use on returned packets,
185 	 * but we don't yet have a way to check the checksum
186 	 * with options still present.
187 	 */
188 	if (iphlen > sizeof (struct ip)) {
189 		ip_stripoptions(m, (struct mbuf *)0);
190 		iphlen = sizeof(struct ip);
191 	}
192 
193 	/*
194 	 * Get IP and UDP header together in first mbuf.
195 	 */
196 	ip = mtod(m, struct ip *);
197 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
198 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
199 			udpstat.udps_hdrops++;
200 			return;
201 		}
202 		ip = mtod(m, struct ip *);
203 	}
204 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
205 
206 	/* destination port of 0 is illegal, based on RFC768. */
207 	if (uh->uh_dport == 0)
208 		goto badunlocked;
209 
210 	/*
211 	 * Construct sockaddr format source address.
212 	 * Stuff source address and datagram in user buffer.
213 	 */
214 	bzero(&udp_in, sizeof(udp_in));
215 	udp_in.sin_len = sizeof(udp_in);
216 	udp_in.sin_family = AF_INET;
217 	udp_in.sin_port = uh->uh_sport;
218 	udp_in.sin_addr = ip->ip_src;
219 
220 	/*
221 	 * Make mbuf data length reflect UDP length.
222 	 * If not enough data to reflect UDP length, drop.
223 	 */
224 	len = ntohs((u_short)uh->uh_ulen);
225 	if (ip->ip_len != len) {
226 		if (len > ip->ip_len || len < sizeof(struct udphdr)) {
227 			udpstat.udps_badlen++;
228 			goto badunlocked;
229 		}
230 		m_adj(m, len - ip->ip_len);
231 		/* ip->ip_len = len; */
232 	}
233 	/*
234 	 * Save a copy of the IP header in case we want restore it
235 	 * for sending an ICMP error message in response.
236 	 */
237 	if (!blackhole)
238 		save_ip = *ip;
239 
240 	/*
241 	 * Checksum extended UDP header and data.
242 	 */
243 	if (uh->uh_sum) {
244 		if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
245 			if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
246 				uh->uh_sum = m->m_pkthdr.csum_data;
247 			else
248 				uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
249 				    ip->ip_dst.s_addr, htonl((u_short)len +
250 				    m->m_pkthdr.csum_data + IPPROTO_UDP));
251 			uh->uh_sum ^= 0xffff;
252 		} else {
253 			char b[9];
254 			bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
255 			bzero(((struct ipovly *)ip)->ih_x1, 9);
256 			((struct ipovly *)ip)->ih_len = uh->uh_ulen;
257 			uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
258 			bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
259 		}
260 		if (uh->uh_sum) {
261 			udpstat.udps_badsum++;
262 			m_freem(m);
263 			return;
264 		}
265 	} else
266 		udpstat.udps_nosum++;
267 
268 #ifdef IPFIREWALL_FORWARD
269 	/* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
270 	fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
271 
272 	if (fwd_tag != NULL) {
273 		struct sockaddr_in *next_hop;
274 
275 		/* Do the hack. */
276 		next_hop = (struct sockaddr_in *)(fwd_tag + 1);
277 		ip->ip_dst = next_hop->sin_addr;
278 		uh->uh_dport = ntohs(next_hop->sin_port);
279 		/* Remove the tag from the packet.  We don't need it anymore. */
280 		m_tag_delete(m, fwd_tag);
281 	}
282 #endif
283 
284 	INP_INFO_RLOCK(&udbinfo);
285 
286 	if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
287 	    in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
288 		struct inpcb *last;
289 		/*
290 		 * Deliver a multicast or broadcast datagram to *all* sockets
291 		 * for which the local and remote addresses and ports match
292 		 * those of the incoming datagram.  This allows more than
293 		 * one process to receive multi/broadcasts on the same port.
294 		 * (This really ought to be done for unicast datagrams as
295 		 * well, but that would cause problems with existing
296 		 * applications that open both address-specific sockets and
297 		 * a wildcard socket listening to the same port -- they would
298 		 * end up receiving duplicates of every unicast datagram.
299 		 * Those applications open the multiple sockets to overcome an
300 		 * inadequacy of the UDP socket interface, but for backwards
301 		 * compatibility we avoid the problem here rather than
302 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
303 		 */
304 
305 		/*
306 		 * Locate pcb(s) for datagram.
307 		 * (Algorithm copied from raw_intr().)
308 		 */
309 		last = NULL;
310 		LIST_FOREACH(inp, &udb, inp_list) {
311 			if (inp->inp_lport != uh->uh_dport)
312 				continue;
313 #ifdef INET6
314 			if ((inp->inp_vflag & INP_IPV4) == 0)
315 				continue;
316 #endif
317 			if (inp->inp_laddr.s_addr != INADDR_ANY) {
318 				if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
319 					continue;
320 			}
321 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
322 				if (inp->inp_faddr.s_addr !=
323 				    ip->ip_src.s_addr ||
324 				    inp->inp_fport != uh->uh_sport)
325 					continue;
326 			}
327 			INP_LOCK(inp);
328 
329 			/*
330 			 * Check multicast packets to make sure they are only
331 			 * sent to sockets with multicast memberships for the
332 			 * packet's destination address and arrival interface
333 			 */
334 #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
335 #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
336 			if (strict_mcast_mship && inp->inp_moptions != NULL) {
337 				int mship, foundmship = 0;
338 
339 				for (mship = 0; mship < NMSHIPS(inp); mship++) {
340 					if (MSHIP(inp, mship)->inm_addr.s_addr
341 					    == ip->ip_dst.s_addr &&
342 					    MSHIP(inp, mship)->inm_ifp
343 					    == m->m_pkthdr.rcvif) {
344 						foundmship = 1;
345 						break;
346 					}
347 				}
348 				if (foundmship == 0) {
349 					INP_UNLOCK(inp);
350 					continue;
351 				}
352 			}
353 #undef NMSHIPS
354 #undef MSHIP
355 			if (last != NULL) {
356 				struct mbuf *n;
357 
358 				n = m_copy(m, 0, M_COPYALL);
359 				if (n != NULL)
360 					udp_append(last, ip, n,
361 						   iphlen +
362 						   sizeof(struct udphdr),
363 						   &udp_in);
364 				INP_UNLOCK(last);
365 			}
366 			last = inp;
367 			/*
368 			 * Don't look for additional matches if this one does
369 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
370 			 * socket options set.  This heuristic avoids searching
371 			 * through all pcbs in the common case of a non-shared
372 			 * port.  It * assumes that an application will never
373 			 * clear these options after setting them.
374 			 */
375 			if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
376 				break;
377 		}
378 
379 		if (last == NULL) {
380 			/*
381 			 * No matching pcb found; discard datagram.
382 			 * (No need to send an ICMP Port Unreachable
383 			 * for a broadcast or multicast datgram.)
384 			 */
385 			udpstat.udps_noportbcast++;
386 			goto badheadlocked;
387 		}
388 		udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
389 		    &udp_in);
390 		INP_UNLOCK(last);
391 		INP_INFO_RUNLOCK(&udbinfo);
392 		return;
393 	}
394 	/*
395 	 * Locate pcb for datagram.
396 	 */
397 	inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
398 	    ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
399 	if (inp == NULL) {
400 		if (log_in_vain) {
401 			char buf[4*sizeof "123"];
402 
403 			strcpy(buf, inet_ntoa(ip->ip_dst));
404 			log(LOG_INFO,
405 			    "Connection attempt to UDP %s:%d from %s:%d\n",
406 			    buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
407 			    ntohs(uh->uh_sport));
408 		}
409 		udpstat.udps_noport++;
410 		if (m->m_flags & (M_BCAST | M_MCAST)) {
411 			udpstat.udps_noportbcast++;
412 			goto badheadlocked;
413 		}
414 		if (blackhole)
415 			goto badheadlocked;
416 		if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
417 			goto badheadlocked;
418 		*ip = save_ip;
419 		ip->ip_len += iphlen;
420 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
421 		INP_INFO_RUNLOCK(&udbinfo);
422 		return;
423 	}
424 	INP_LOCK(inp);
425 	/* Check the minimum TTL for socket. */
426 	if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
427 		goto badheadlocked;
428 	udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
429 	INP_UNLOCK(inp);
430 	INP_INFO_RUNLOCK(&udbinfo);
431 	return;
432 
433 badheadlocked:
434 	if (inp)
435 		INP_UNLOCK(inp);
436 	INP_INFO_RUNLOCK(&udbinfo);
437 badunlocked:
438 	m_freem(m);
439 	return;
440 }
441 
442 /*
443  * Subroutine of udp_input(), which appends the provided mbuf chain to the
444  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
445  * contains the source address.  If the socket ends up being an IPv6 socket,
446  * udp_append() will convert to a sockaddr_in6 before passing the address
447  * into the socket code.
448  */
449 static void
450 udp_append(inp, ip, n, off, udp_in)
451 	struct inpcb *inp;
452 	struct ip *ip;
453 	struct mbuf *n;
454 	int off;
455 	struct sockaddr_in *udp_in;
456 {
457 	struct sockaddr *append_sa;
458 	struct socket *so;
459 	struct mbuf *opts = 0;
460 #ifdef INET6
461 	struct sockaddr_in6 udp_in6;
462 #endif
463 
464 	INP_LOCK_ASSERT(inp);
465 
466 #if defined(IPSEC) || defined(FAST_IPSEC)
467 	/* check AH/ESP integrity. */
468 	if (ipsec4_in_reject(n, inp)) {
469 #ifdef IPSEC
470 		ipsecstat.in_polvio++;
471 #endif /*IPSEC*/
472 		m_freem(n);
473 		return;
474 	}
475 #endif /*IPSEC || FAST_IPSEC*/
476 #ifdef MAC
477 	if (mac_check_inpcb_deliver(inp, n) != 0) {
478 		m_freem(n);
479 		return;
480 	}
481 #endif
482 	if (inp->inp_flags & INP_CONTROLOPTS ||
483 	    inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
484 #ifdef INET6
485 		if (inp->inp_vflag & INP_IPV6) {
486 			int savedflags;
487 
488 			savedflags = inp->inp_flags;
489 			inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
490 			ip6_savecontrol(inp, n, &opts);
491 			inp->inp_flags = savedflags;
492 		} else
493 #endif
494 		ip_savecontrol(inp, &opts, ip, n);
495 	}
496 #ifdef INET6
497 	if (inp->inp_vflag & INP_IPV6) {
498 		bzero(&udp_in6, sizeof(udp_in6));
499 		udp_in6.sin6_len = sizeof(udp_in6);
500 		udp_in6.sin6_family = AF_INET6;
501 		in6_sin_2_v4mapsin6(udp_in, &udp_in6);
502 		append_sa = (struct sockaddr *)&udp_in6;
503 	} else
504 #endif
505 	append_sa = (struct sockaddr *)udp_in;
506 	m_adj(n, off);
507 
508 	so = inp->inp_socket;
509 	SOCKBUF_LOCK(&so->so_rcv);
510 	if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
511 		m_freem(n);
512 		if (opts)
513 			m_freem(opts);
514 		udpstat.udps_fullsock++;
515 		SOCKBUF_UNLOCK(&so->so_rcv);
516 	} else
517 		sorwakeup_locked(so);
518 }
519 
520 /*
521  * Notify a udp user of an asynchronous error;
522  * just wake up so that he can collect error status.
523  */
524 struct inpcb *
525 udp_notify(inp, errno)
526 	register struct inpcb *inp;
527 	int errno;
528 {
529 	inp->inp_socket->so_error = errno;
530 	sorwakeup(inp->inp_socket);
531 	sowwakeup(inp->inp_socket);
532 	return inp;
533 }
534 
535 void
536 udp_ctlinput(cmd, sa, vip)
537 	int cmd;
538 	struct sockaddr *sa;
539 	void *vip;
540 {
541 	struct ip *ip = vip;
542 	struct udphdr *uh;
543 	struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
544 	struct in_addr faddr;
545 	struct inpcb *inp;
546 
547 	faddr = ((struct sockaddr_in *)sa)->sin_addr;
548 	if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
549 		return;
550 
551 	/*
552 	 * Redirects don't need to be handled up here.
553 	 */
554 	if (PRC_IS_REDIRECT(cmd))
555 		return;
556 	/*
557 	 * Hostdead is ugly because it goes linearly through all PCBs.
558 	 * XXX: We never get this from ICMP, otherwise it makes an
559 	 * excellent DoS attack on machines with many connections.
560 	 */
561 	if (cmd == PRC_HOSTDEAD)
562 		ip = 0;
563 	else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
564 		return;
565 	if (ip) {
566 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
567 		INP_INFO_RLOCK(&udbinfo);
568 		inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
569 		    ip->ip_src, uh->uh_sport, 0, NULL);
570 		if (inp != NULL) {
571 			INP_LOCK(inp);
572 			if (inp->inp_socket != NULL) {
573 				(*notify)(inp, inetctlerrmap[cmd]);
574 			}
575 			INP_UNLOCK(inp);
576 		}
577 		INP_INFO_RUNLOCK(&udbinfo);
578 	} else
579 		in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
580 }
581 
582 static int
583 udp_pcblist(SYSCTL_HANDLER_ARGS)
584 {
585 	int error, i, n;
586 	struct inpcb *inp, **inp_list;
587 	inp_gen_t gencnt;
588 	struct xinpgen xig;
589 
590 	/*
591 	 * The process of preparing the TCB list is too time-consuming and
592 	 * resource-intensive to repeat twice on every request.
593 	 */
594 	if (req->oldptr == 0) {
595 		n = udbinfo.ipi_count;
596 		req->oldidx = 2 * (sizeof xig)
597 			+ (n + n/8) * sizeof(struct xinpcb);
598 		return 0;
599 	}
600 
601 	if (req->newptr != 0)
602 		return EPERM;
603 
604 	/*
605 	 * OK, now we're committed to doing something.
606 	 */
607 	INP_INFO_RLOCK(&udbinfo);
608 	gencnt = udbinfo.ipi_gencnt;
609 	n = udbinfo.ipi_count;
610 	INP_INFO_RUNLOCK(&udbinfo);
611 
612 	error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
613 		+ n * sizeof(struct xinpcb));
614 	if (error != 0)
615 		return (error);
616 
617 	xig.xig_len = sizeof xig;
618 	xig.xig_count = n;
619 	xig.xig_gen = gencnt;
620 	xig.xig_sogen = so_gencnt;
621 	error = SYSCTL_OUT(req, &xig, sizeof xig);
622 	if (error)
623 		return error;
624 
625 	inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
626 	if (inp_list == 0)
627 		return ENOMEM;
628 
629 	INP_INFO_RLOCK(&udbinfo);
630 	for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
631 	     inp = LIST_NEXT(inp, inp_list)) {
632 		INP_LOCK(inp);
633 		if (inp->inp_gencnt <= gencnt &&
634 		    cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
635 			inp_list[i++] = inp;
636 		INP_UNLOCK(inp);
637 	}
638 	INP_INFO_RUNLOCK(&udbinfo);
639 	n = i;
640 
641 	error = 0;
642 	for (i = 0; i < n; i++) {
643 		inp = inp_list[i];
644 		INP_LOCK(inp);
645 		if (inp->inp_gencnt <= gencnt) {
646 			struct xinpcb xi;
647 			bzero(&xi, sizeof(xi));
648 			xi.xi_len = sizeof xi;
649 			/* XXX should avoid extra copy */
650 			bcopy(inp, &xi.xi_inp, sizeof *inp);
651 			if (inp->inp_socket)
652 				sotoxsocket(inp->inp_socket, &xi.xi_socket);
653 			xi.xi_inp.inp_gencnt = inp->inp_gencnt;
654 			INP_UNLOCK(inp);
655 			error = SYSCTL_OUT(req, &xi, sizeof xi);
656 		} else
657 			INP_UNLOCK(inp);
658 	}
659 	if (!error) {
660 		/*
661 		 * Give the user an updated idea of our state.
662 		 * If the generation differs from what we told
663 		 * her before, she knows that something happened
664 		 * while we were processing this request, and it
665 		 * might be necessary to retry.
666 		 */
667 		INP_INFO_RLOCK(&udbinfo);
668 		xig.xig_gen = udbinfo.ipi_gencnt;
669 		xig.xig_sogen = so_gencnt;
670 		xig.xig_count = udbinfo.ipi_count;
671 		INP_INFO_RUNLOCK(&udbinfo);
672 		error = SYSCTL_OUT(req, &xig, sizeof xig);
673 	}
674 	free(inp_list, M_TEMP);
675 	return error;
676 }
677 
678 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
679 	    udp_pcblist, "S,xinpcb", "List of active UDP sockets");
680 
681 static int
682 udp_getcred(SYSCTL_HANDLER_ARGS)
683 {
684 	struct xucred xuc;
685 	struct sockaddr_in addrs[2];
686 	struct inpcb *inp;
687 	int error;
688 
689 	error = suser_cred(req->td->td_ucred, SUSER_ALLOWJAIL);
690 	if (error)
691 		return (error);
692 	error = SYSCTL_IN(req, addrs, sizeof(addrs));
693 	if (error)
694 		return (error);
695 	INP_INFO_RLOCK(&udbinfo);
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 = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
703 	if (error)
704 		goto out;
705 	cru2x(inp->inp_socket->so_cred, &xuc);
706 out:
707 	INP_INFO_RUNLOCK(&udbinfo);
708 	if (error == 0)
709 		error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
710 	return (error);
711 }
712 
713 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
714     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
715     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
716 
717 static int
718 udp_output(inp, m, addr, control, td)
719 	register struct inpcb *inp;
720 	struct mbuf *m;
721 	struct sockaddr *addr;
722 	struct mbuf *control;
723 	struct thread *td;
724 {
725 	register struct udpiphdr *ui;
726 	register int len = m->m_pkthdr.len;
727 	struct in_addr faddr, laddr;
728 	struct cmsghdr *cm;
729 	struct sockaddr_in *sin, src;
730 	int error = 0;
731 	int ipflags;
732 	u_short fport, lport;
733 	int unlock_udbinfo;
734 
735 	/*
736 	 * udp_output() may need to temporarily bind or connect the current
737 	 * inpcb.  As such, we don't know up front what inpcb locks we will
738 	 * need.  Do any work to decide what is needed up front before
739 	 * acquiring locks.
740 	 */
741 	if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
742 		if (control)
743 			m_freem(control);
744 		m_freem(m);
745 		return EMSGSIZE;
746 	}
747 
748 	src.sin_addr.s_addr = INADDR_ANY;
749 	if (control != NULL) {
750 		/*
751 		 * XXX: Currently, we assume all the optional information
752 		 * is stored in a single mbuf.
753 		 */
754 		if (control->m_next) {
755 			m_freem(control);
756 			m_freem(m);
757 			return EINVAL;
758 		}
759 		for (; control->m_len > 0;
760 		    control->m_data += CMSG_ALIGN(cm->cmsg_len),
761 		    control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
762 			cm = mtod(control, struct cmsghdr *);
763 			if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
764 			    cm->cmsg_len > control->m_len) {
765 				error = EINVAL;
766 				break;
767 			}
768 			if (cm->cmsg_level != IPPROTO_IP)
769 				continue;
770 
771 			switch (cm->cmsg_type) {
772 			case IP_SENDSRCADDR:
773 				if (cm->cmsg_len !=
774 				    CMSG_LEN(sizeof(struct in_addr))) {
775 					error = EINVAL;
776 					break;
777 				}
778 				bzero(&src, sizeof(src));
779 				src.sin_family = AF_INET;
780 				src.sin_len = sizeof(src);
781 				src.sin_port = inp->inp_lport;
782 				src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
783 				break;
784 			default:
785 				error = ENOPROTOOPT;
786 				break;
787 			}
788 			if (error)
789 				break;
790 		}
791 		m_freem(control);
792 	}
793 	if (error) {
794 		m_freem(m);
795 		return error;
796 	}
797 
798 	if (src.sin_addr.s_addr != INADDR_ANY ||
799 	    addr != NULL) {
800 		INP_INFO_WLOCK(&udbinfo);
801 		unlock_udbinfo = 1;
802 	} else
803 		unlock_udbinfo = 0;
804 	INP_LOCK(inp);
805 
806 #ifdef MAC
807 	mac_create_mbuf_from_inpcb(inp, m);
808 #endif
809 
810 	laddr = inp->inp_laddr;
811 	lport = inp->inp_lport;
812 	if (src.sin_addr.s_addr != INADDR_ANY) {
813 		if (lport == 0) {
814 			error = EINVAL;
815 			goto release;
816 		}
817 		error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
818 		    &laddr.s_addr, &lport, td->td_ucred);
819 		if (error)
820 			goto release;
821 	}
822 
823 	if (addr) {
824 		sin = (struct sockaddr_in *)addr;
825 		if (jailed(td->td_ucred))
826 			prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
827 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
828 			error = EISCONN;
829 			goto release;
830 		}
831 		error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
832 		    &faddr.s_addr, &fport, NULL, td->td_ucred);
833 		if (error)
834 			goto release;
835 
836 		/* Commit the local port if newly assigned. */
837 		if (inp->inp_laddr.s_addr == INADDR_ANY &&
838 		    inp->inp_lport == 0) {
839 			/*
840 			 * Remember addr if jailed, to prevent rebinding.
841 			 */
842 			if (jailed(td->td_ucred))
843 				inp->inp_laddr = laddr;
844 			inp->inp_lport = lport;
845 			if (in_pcbinshash(inp) != 0) {
846 				inp->inp_lport = 0;
847 				error = EAGAIN;
848 				goto release;
849 			}
850 			inp->inp_flags |= INP_ANONPORT;
851 		}
852 	} else {
853 		faddr = inp->inp_faddr;
854 		fport = inp->inp_fport;
855 		if (faddr.s_addr == INADDR_ANY) {
856 			error = ENOTCONN;
857 			goto release;
858 		}
859 	}
860 
861 	/*
862 	 * Calculate data length and get a mbuf for UDP, IP, and possible
863 	 * link-layer headers.  Immediate slide the data pointer back forward
864 	 * since we won't use that space at this layer.
865 	 */
866 	M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
867 	if (m == NULL) {
868 		error = ENOBUFS;
869 		goto release;
870 	}
871 	m->m_data += max_linkhdr;
872 	m->m_len -= max_linkhdr;
873 	m->m_pkthdr.len -= max_linkhdr;
874 
875 	/*
876 	 * Fill in mbuf with extended UDP header
877 	 * and addresses and length put into network format.
878 	 */
879 	ui = mtod(m, struct udpiphdr *);
880 	bzero(ui->ui_x1, sizeof(ui->ui_x1));	/* XXX still needed? */
881 	ui->ui_pr = IPPROTO_UDP;
882 	ui->ui_src = laddr;
883 	ui->ui_dst = faddr;
884 	ui->ui_sport = lport;
885 	ui->ui_dport = fport;
886 	ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
887 
888 	/*
889 	 * Set the Don't Fragment bit in the IP header.
890 	 */
891 	if (inp->inp_flags & INP_DONTFRAG) {
892 		struct ip *ip;
893 		ip = (struct ip *)&ui->ui_i;
894 		ip->ip_off |= IP_DF;
895 	}
896 
897 	ipflags = 0;
898 	if (inp->inp_socket->so_options & SO_DONTROUTE)
899 		ipflags |= IP_ROUTETOIF;
900 	if (inp->inp_socket->so_options & SO_BROADCAST)
901 		ipflags |= IP_ALLOWBROADCAST;
902 	if (inp->inp_vflag & INP_ONESBCAST)
903 		ipflags |= IP_SENDONES;
904 
905 	/*
906 	 * Set up checksum and output datagram.
907 	 */
908 	if (udpcksum) {
909 		if (inp->inp_vflag & INP_ONESBCAST)
910 			faddr.s_addr = INADDR_BROADCAST;
911 		ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
912 		    htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
913 		m->m_pkthdr.csum_flags = CSUM_UDP;
914 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
915 	} else {
916 		ui->ui_sum = 0;
917 	}
918 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
919 	((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;	/* XXX */
920 	((struct ip *)ui)->ip_tos = inp->inp_ip_tos;	/* XXX */
921 	udpstat.udps_opackets++;
922 
923 	if (unlock_udbinfo)
924 		INP_INFO_WUNLOCK(&udbinfo);
925 	error = ip_output(m, inp->inp_options, NULL, ipflags,
926 	    inp->inp_moptions, inp);
927 	INP_UNLOCK(inp);
928 	return (error);
929 
930 release:
931 	INP_UNLOCK(inp);
932 	if (unlock_udbinfo)
933 		INP_INFO_WUNLOCK(&udbinfo);
934 	m_freem(m);
935 	return (error);
936 }
937 
938 u_long	udp_sendspace = 9216;		/* really max datagram size */
939 					/* 40 1K datagrams */
940 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
941     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
942 
943 u_long	udp_recvspace = 40 * (1024 +
944 #ifdef INET6
945 				      sizeof(struct sockaddr_in6)
946 #else
947 				      sizeof(struct sockaddr_in)
948 #endif
949 				      );
950 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
951     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
952 
953 static void
954 udp_abort(struct socket *so)
955 {
956 	struct inpcb *inp;
957 
958 	inp = sotoinpcb(so);
959 	KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
960 	INP_INFO_WLOCK(&udbinfo);
961 	INP_LOCK(inp);
962 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
963 		in_pcbdisconnect(inp);
964 		inp->inp_laddr.s_addr = INADDR_ANY;
965 		soisdisconnected(so);
966 	}
967 	INP_UNLOCK(inp);
968 	INP_INFO_WUNLOCK(&udbinfo);
969 }
970 
971 static int
972 udp_attach(struct socket *so, int proto, struct thread *td)
973 {
974 	struct inpcb *inp;
975 	int error;
976 
977 	inp = sotoinpcb(so);
978 	KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
979 	error = soreserve(so, udp_sendspace, udp_recvspace);
980 	if (error)
981 		return error;
982 	INP_INFO_WLOCK(&udbinfo);
983 	error = in_pcballoc(so, &udbinfo);
984 	if (error) {
985 		INP_INFO_WUNLOCK(&udbinfo);
986 		return error;
987 	}
988 
989 	inp = (struct inpcb *)so->so_pcb;
990 	INP_INFO_WUNLOCK(&udbinfo);
991 	inp->inp_vflag |= INP_IPV4;
992 	inp->inp_ip_ttl = ip_defttl;
993 	INP_UNLOCK(inp);
994 	return 0;
995 }
996 
997 static int
998 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
999 {
1000 	struct inpcb *inp;
1001 	int error;
1002 
1003 	inp = sotoinpcb(so);
1004 	KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1005 	INP_INFO_WLOCK(&udbinfo);
1006 	INP_LOCK(inp);
1007 	error = in_pcbbind(inp, nam, td->td_ucred);
1008 	INP_UNLOCK(inp);
1009 	INP_INFO_WUNLOCK(&udbinfo);
1010 	return error;
1011 }
1012 
1013 static void
1014 udp_close(struct socket *so)
1015 {
1016 	struct inpcb *inp;
1017 
1018 	inp = sotoinpcb(so);
1019 	KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1020 	INP_INFO_WLOCK(&udbinfo);
1021 	INP_LOCK(inp);
1022 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1023 		in_pcbdisconnect(inp);
1024 		inp->inp_laddr.s_addr = INADDR_ANY;
1025 		soisdisconnected(so);
1026 	}
1027 	INP_UNLOCK(inp);
1028 	INP_INFO_WUNLOCK(&udbinfo);
1029 }
1030 
1031 static int
1032 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1033 {
1034 	struct inpcb *inp;
1035 	int error;
1036 	struct sockaddr_in *sin;
1037 
1038 	inp = sotoinpcb(so);
1039 	KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1040 	INP_INFO_WLOCK(&udbinfo);
1041 	INP_LOCK(inp);
1042 	if (inp->inp_faddr.s_addr != INADDR_ANY) {
1043 		INP_UNLOCK(inp);
1044 		INP_INFO_WUNLOCK(&udbinfo);
1045 		return EISCONN;
1046 	}
1047 	sin = (struct sockaddr_in *)nam;
1048 	if (jailed(td->td_ucred))
1049 		prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1050 	error = in_pcbconnect(inp, nam, td->td_ucred);
1051 	if (error == 0)
1052 		soisconnected(so);
1053 	INP_UNLOCK(inp);
1054 	INP_INFO_WUNLOCK(&udbinfo);
1055 	return error;
1056 }
1057 
1058 static void
1059 udp_detach(struct socket *so)
1060 {
1061 	struct inpcb *inp;
1062 
1063 	inp = sotoinpcb(so);
1064 	KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1065 	KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1066 	    ("udp_detach: not disconnected"));
1067 	INP_INFO_WLOCK(&udbinfo);
1068 	INP_LOCK(inp);
1069 	in_pcbdetach(inp);
1070 	in_pcbfree(inp);
1071 	INP_INFO_WUNLOCK(&udbinfo);
1072 }
1073 
1074 static int
1075 udp_disconnect(struct socket *so)
1076 {
1077 	struct inpcb *inp;
1078 
1079 	inp = sotoinpcb(so);
1080 	KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1081 	INP_INFO_WLOCK(&udbinfo);
1082 	INP_LOCK(inp);
1083 	if (inp->inp_faddr.s_addr == INADDR_ANY) {
1084 		INP_INFO_WUNLOCK(&udbinfo);
1085 		INP_UNLOCK(inp);
1086 		return ENOTCONN;
1087 	}
1088 
1089 	in_pcbdisconnect(inp);
1090 	inp->inp_laddr.s_addr = INADDR_ANY;
1091 	SOCK_LOCK(so);
1092 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1093 	SOCK_UNLOCK(so);
1094 	INP_UNLOCK(inp);
1095 	INP_INFO_WUNLOCK(&udbinfo);
1096 	return 0;
1097 }
1098 
1099 static int
1100 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1101 	    struct mbuf *control, struct thread *td)
1102 {
1103 	struct inpcb *inp;
1104 
1105 	inp = sotoinpcb(so);
1106 	KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1107 	return udp_output(inp, m, addr, control, td);
1108 }
1109 
1110 int
1111 udp_shutdown(struct socket *so)
1112 {
1113 	struct inpcb *inp;
1114 
1115 	inp = sotoinpcb(so);
1116 	KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1117 	INP_LOCK(inp);
1118 	socantsendmore(so);
1119 	INP_UNLOCK(inp);
1120 	return 0;
1121 }
1122 
1123 /*
1124  * This is the wrapper function for in_setsockaddr.  We just pass down
1125  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking
1126  * here because in_setsockaddr will call malloc and might block.
1127  */
1128 static int
1129 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1130 {
1131 	return (in_setsockaddr(so, nam, &udbinfo));
1132 }
1133 
1134 /*
1135  * This is the wrapper function for in_setpeeraddr.  We just pass down
1136  * the pcbinfo for in_setpeeraddr to lock.
1137  */
1138 static int
1139 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1140 {
1141 	return (in_setpeeraddr(so, nam, &udbinfo));
1142 }
1143 
1144 struct pr_usrreqs udp_usrreqs = {
1145 	.pru_abort =		udp_abort,
1146 	.pru_attach =		udp_attach,
1147 	.pru_bind =		udp_bind,
1148 	.pru_connect =		udp_connect,
1149 	.pru_control =		in_control,
1150 	.pru_detach =		udp_detach,
1151 	.pru_disconnect =	udp_disconnect,
1152 	.pru_peeraddr =		udp_peeraddr,
1153 	.pru_send =		udp_send,
1154 	.pru_sosend =		sosend_dgram,
1155 	.pru_shutdown =		udp_shutdown,
1156 	.pru_sockaddr =		udp_sockaddr,
1157 	.pru_sosetlabel =	in_pcbsosetlabel,
1158 	.pru_close =		udp_close,
1159 };
1160