xref: /netbsd/sys/netinet/udp_usrreq.c (revision bf9ec67e)
1 /*	$NetBSD: udp_usrreq.c,v 1.93 2002/05/12 20:33:51 matt Exp $	*/
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
34  *	The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *	This product includes software developed by the University of
47  *	California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *	@(#)udp_usrreq.c	8.6 (Berkeley) 5/23/95
65  */
66 
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.93 2002/05/12 20:33:51 matt Exp $");
69 
70 #include "opt_inet.h"
71 #include "opt_ipsec.h"
72 #include "opt_inet_csum.h"
73 #include "opt_ipkdb.h"
74 
75 #include <sys/param.h>
76 #include <sys/malloc.h>
77 #include <sys/mbuf.h>
78 #include <sys/protosw.h>
79 #include <sys/socket.h>
80 #include <sys/socketvar.h>
81 #include <sys/errno.h>
82 #include <sys/stat.h>
83 #include <sys/systm.h>
84 #include <sys/proc.h>
85 #include <sys/domain.h>
86 #include <sys/sysctl.h>
87 
88 #include <net/if.h>
89 #include <net/route.h>
90 
91 #include <netinet/in.h>
92 #include <netinet/in_systm.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip.h>
95 #include <netinet/in_pcb.h>
96 #include <netinet/ip_var.h>
97 #include <netinet/ip_icmp.h>
98 #include <netinet/udp.h>
99 #include <netinet/udp_var.h>
100 
101 #ifdef INET6
102 #include <netinet/ip6.h>
103 #include <netinet/icmp6.h>
104 #include <netinet6/ip6_var.h>
105 #include <netinet6/in6_pcb.h>
106 #include <netinet6/udp6_var.h>
107 #endif
108 
109 #ifdef PULLDOWN_TEST
110 #ifndef INET6
111 /* always need ip6.h for IP6_EXTHDR_GET */
112 #include <netinet/ip6.h>
113 #endif
114 #endif
115 
116 #include "faith.h"
117 #if defined(NFAITH) && NFAITH > 0
118 #include <net/if_faith.h>
119 #endif
120 
121 #include <machine/stdarg.h>
122 
123 #ifdef IPSEC
124 #include <netinet6/ipsec.h>
125 #include <netkey/key.h>
126 #endif /*IPSEC*/
127 
128 #ifdef IPKDB
129 #include <ipkdb/ipkdb.h>
130 #endif
131 
132 /*
133  * UDP protocol implementation.
134  * Per RFC 768, August, 1980.
135  */
136 #ifndef	COMPAT_42
137 int	udpcksum = 1;
138 #else
139 int	udpcksum = 0;		/* XXX */
140 #endif
141 
142 struct	inpcbtable udbtable;
143 struct	udpstat udpstat;
144 
145 #ifdef INET
146 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *,
147 	struct socket *));
148 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *,
149 	struct mbuf *, int));
150 #endif
151 #ifdef INET6
152 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *,
153 	struct socket *));
154 static int udp6_realinput __P((int, struct sockaddr_in6 *,
155 	struct sockaddr_in6 *, struct mbuf *, int));
156 #endif
157 #ifdef INET
158 static	void udp_notify __P((struct inpcb *, int));
159 #endif
160 
161 #ifndef UDBHASHSIZE
162 #define	UDBHASHSIZE	128
163 #endif
164 int	udbhashsize = UDBHASHSIZE;
165 
166 #ifdef UDP_CSUM_COUNTERS
167 #include <sys/device.h>
168 
169 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
170     NULL, "udp", "hwcsum bad");
171 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
172     NULL, "udp", "hwcsum ok");
173 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
174     NULL, "udp", "hwcsum data");
175 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
176     NULL, "udp", "swcsum");
177 
178 #define	UDP_CSUM_COUNTER_INCR(ev)	(ev)->ev_count++
179 
180 #else
181 
182 #define	UDP_CSUM_COUNTER_INCR(ev)	/* nothing */
183 
184 #endif /* UDP_CSUM_COUNTERS */
185 
186 void
187 udp_init()
188 {
189 
190 #ifdef INET
191 	in_pcbinit(&udbtable, udbhashsize, udbhashsize);
192 #endif
193 
194 #ifdef UDP_CSUM_COUNTERS
195 	evcnt_attach_static(&udp_hwcsum_bad);
196 	evcnt_attach_static(&udp_hwcsum_ok);
197 	evcnt_attach_static(&udp_hwcsum_data);
198 	evcnt_attach_static(&udp_swcsum);
199 #endif /* UDP_CSUM_COUNTERS */
200 }
201 
202 #ifdef INET
203 void
204 #if __STDC__
205 udp_input(struct mbuf *m, ...)
206 #else
207 udp_input(m, va_alist)
208 	struct mbuf *m;
209 	va_dcl
210 #endif
211 {
212 	va_list ap;
213 	struct sockaddr_in src, dst;
214 	struct ip *ip;
215 	struct udphdr *uh;
216 	int iphlen, proto;
217 	int len;
218 	int n;
219 
220 	va_start(ap, m);
221 	iphlen = va_arg(ap, int);
222 	proto = va_arg(ap, int);
223 	va_end(ap);
224 
225 	udpstat.udps_ipackets++;
226 
227 #ifndef PULLDOWN_TEST
228 	/*
229 	 * Strip IP options, if any; should skip this,
230 	 * make available to user, and use on returned packets,
231 	 * but we don't yet have a way to check the checksum
232 	 * with options still present.
233 	 */
234 	if (iphlen > sizeof (struct ip)) {
235 		ip_stripoptions(m, (struct mbuf *)0);
236 		iphlen = sizeof(struct ip);
237 	}
238 #else
239 	/*
240 	 * we may enable the above code if we save and pass IPv4 options
241 	 * to the userland.
242 	 */
243 #endif
244 
245 	/*
246 	 * Get IP and UDP header together in first mbuf.
247 	 */
248 	ip = mtod(m, struct ip *);
249 #ifndef PULLDOWN_TEST
250 	if (m->m_len < iphlen + sizeof(struct udphdr)) {
251 		if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
252 			udpstat.udps_hdrops++;
253 			return;
254 		}
255 		ip = mtod(m, struct ip *);
256 	}
257 	uh = (struct udphdr *)((caddr_t)ip + iphlen);
258 #else
259 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
260 	if (uh == NULL) {
261 		udpstat.udps_hdrops++;
262 		return;
263 	}
264 #endif
265 
266 	/* destination port of 0 is illegal, based on RFC768. */
267 	if (uh->uh_dport == 0)
268 		goto bad;
269 
270 	/*
271 	 * Make mbuf data length reflect UDP length.
272 	 * If not enough data to reflect UDP length, drop.
273 	 */
274 	len = ntohs((u_int16_t)uh->uh_ulen);
275 	if (ip->ip_len != iphlen + len) {
276 		if (ip->ip_len < iphlen + len || len < sizeof(struct udphdr)) {
277 			udpstat.udps_badlen++;
278 			goto bad;
279 		}
280 		m_adj(m, iphlen + len - ip->ip_len);
281 	}
282 
283 	/*
284 	 * Checksum extended UDP header and data.
285 	 */
286 	if (uh->uh_sum) {
287 		switch (m->m_pkthdr.csum_flags &
288 			((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) |
289 			 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) {
290 		case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD:
291 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad);
292 			goto badcsum;
293 
294 		case M_CSUM_UDPv4|M_CSUM_DATA:
295 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data);
296 			if ((m->m_pkthdr.csum_data ^ 0xffff) != 0)
297 				goto badcsum;
298 			break;
299 
300 		case M_CSUM_UDPv4:
301 			/* Checksum was okay. */
302 			UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok);
303 			break;
304 
305 		default:
306 			/* Need to compute it ourselves. */
307 			UDP_CSUM_COUNTER_INCR(&udp_swcsum);
308 			if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0)
309 				goto badcsum;
310 			break;
311 		}
312 	}
313 
314 	/* construct source and dst sockaddrs. */
315 	bzero(&src, sizeof(src));
316 	src.sin_family = AF_INET;
317 	src.sin_len = sizeof(struct sockaddr_in);
318 	bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr));
319 	src.sin_port = uh->uh_sport;
320 	bzero(&dst, sizeof(dst));
321 	dst.sin_family = AF_INET;
322 	dst.sin_len = sizeof(struct sockaddr_in);
323 	bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr));
324 	dst.sin_port = uh->uh_dport;
325 
326 	n = udp4_realinput(&src, &dst, m, iphlen);
327 #ifdef INET6
328 	if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) {
329 		struct sockaddr_in6 src6, dst6;
330 
331 		bzero(&src6, sizeof(src6));
332 		src6.sin6_family = AF_INET6;
333 		src6.sin6_len = sizeof(struct sockaddr_in6);
334 		src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff;
335 		bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12],
336 			sizeof(ip->ip_src));
337 		src6.sin6_port = uh->uh_sport;
338 		bzero(&dst6, sizeof(dst6));
339 		dst6.sin6_family = AF_INET6;
340 		dst6.sin6_len = sizeof(struct sockaddr_in6);
341 		dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff;
342 		bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12],
343 			sizeof(ip->ip_dst));
344 		dst6.sin6_port = uh->uh_dport;
345 
346 		n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen);
347 	}
348 #endif
349 
350 	if (n == 0) {
351 		if (m->m_flags & (M_BCAST | M_MCAST)) {
352 			udpstat.udps_noportbcast++;
353 			goto bad;
354 		}
355 		udpstat.udps_noport++;
356 #ifdef IPKDB
357 		if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport,
358 				m, iphlen + sizeof(struct udphdr),
359 				m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) {
360 			/*
361 			 * It was a debugger connect packet,
362 			 * just drop it now
363 			 */
364 			goto bad;
365 		}
366 #endif
367 		icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
368 		m = NULL;
369 	}
370 
371 bad:
372 	if (m)
373 		m_freem(m);
374 	return;
375 
376 badcsum:
377 	m_freem(m);
378 	udpstat.udps_badsum++;
379 }
380 #endif
381 
382 #ifdef INET6
383 int
384 udp6_input(mp, offp, proto)
385 	struct mbuf **mp;
386 	int *offp, proto;
387 {
388 	struct mbuf *m = *mp;
389 	int off = *offp;
390 	struct sockaddr_in6 src, dst;
391 	struct ip6_hdr *ip6;
392 	struct udphdr *uh;
393 	u_int32_t plen, ulen;
394 
395 #ifndef PULLDOWN_TEST
396 	IP6_EXTHDR_CHECK(m, off, sizeof(struct udphdr), IPPROTO_DONE);
397 #endif
398 	ip6 = mtod(m, struct ip6_hdr *);
399 
400 #if defined(NFAITH) && 0 < NFAITH
401 	if (faithprefix(&ip6->ip6_dst)) {
402 		/* send icmp6 host unreach? */
403 		m_freem(m);
404 		return IPPROTO_DONE;
405 	}
406 #endif
407 
408 	udp6stat.udp6s_ipackets++;
409 
410 	/* check for jumbogram is done in ip6_input.  we can trust pkthdr.len */
411 	plen = m->m_pkthdr.len - off;
412 #ifndef PULLDOWN_TEST
413 	uh = (struct udphdr *)((caddr_t)ip6 + off);
414 #else
415 	IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr));
416 	if (uh == NULL) {
417 		ip6stat.ip6s_tooshort++;
418 		return IPPROTO_DONE;
419 	}
420 #endif
421 	ulen = ntohs((u_short)uh->uh_ulen);
422 	/*
423 	 * RFC2675 section 4: jumbograms will have 0 in the UDP header field,
424 	 * iff payload length > 0xffff.
425 	 */
426 	if (ulen == 0 && plen > 0xffff)
427 		ulen = plen;
428 
429 	if (plen != ulen) {
430 		udp6stat.udp6s_badlen++;
431 		goto bad;
432 	}
433 
434 	/* destination port of 0 is illegal, based on RFC768. */
435 	if (uh->uh_dport == 0)
436 		goto bad;
437 
438 	/* Be proactive about malicious use of IPv4 mapped address */
439 	if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
440 	    IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
441 		/* XXX stat */
442 		goto bad;
443 	}
444 
445 	/*
446 	 * Checksum extended UDP header and data.
447 	 */
448 	if (uh->uh_sum == 0)
449 		udp6stat.udp6s_nosum++;
450 	else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) {
451 		udp6stat.udp6s_badsum++;
452 		goto bad;
453 	}
454 
455 	/*
456 	 * Construct source and dst sockaddrs.
457 	 * Note that ifindex (s6_addr16[1]) is already filled.
458 	 */
459 	bzero(&src, sizeof(src));
460 	src.sin6_family = AF_INET6;
461 	src.sin6_len = sizeof(struct sockaddr_in6);
462 	/* KAME hack: recover scopeid */
463 	(void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif);
464 	src.sin6_port = uh->uh_sport;
465 	bzero(&dst, sizeof(dst));
466 	dst.sin6_family = AF_INET6;
467 	dst.sin6_len = sizeof(struct sockaddr_in6);
468 	/* KAME hack: recover scopeid */
469 	(void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif);
470 	dst.sin6_port = uh->uh_dport;
471 
472 	if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) {
473 		if (m->m_flags & M_MCAST) {
474 			udp6stat.udp6s_noportmcast++;
475 			goto bad;
476 		}
477 		udp6stat.udp6s_noport++;
478 		icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0);
479 		m = NULL;
480 	}
481 
482 bad:
483 	if (m)
484 		m_freem(m);
485 	return IPPROTO_DONE;
486 }
487 #endif
488 
489 #ifdef INET
490 static void
491 udp4_sendup(m, off, src, so)
492 	struct mbuf *m;
493 	int off;	/* offset of data portion */
494 	struct sockaddr *src;
495 	struct socket *so;
496 {
497 	struct mbuf *opts = NULL;
498 	struct mbuf *n;
499 	struct inpcb *inp = NULL;
500 #ifdef INET6
501 	struct in6pcb *in6p = NULL;
502 #endif
503 
504 	if (!so)
505 		return;
506 	switch (so->so_proto->pr_domain->dom_family) {
507 	case AF_INET:
508 		inp = sotoinpcb(so);
509 		break;
510 #ifdef INET6
511 	case AF_INET6:
512 		in6p = sotoin6pcb(so);
513 		break;
514 #endif
515 	default:
516 		return;
517 	}
518 
519 #ifdef IPSEC
520 	/* check AH/ESP integrity. */
521 	if (so != NULL && ipsec4_in_reject_so(m, so)) {
522 		ipsecstat.in_polvio++;
523 		return;
524 	}
525 #endif /*IPSEC*/
526 
527 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
528 		if (inp && (inp->inp_flags & INP_CONTROLOPTS
529 			 || so->so_options & SO_TIMESTAMP)) {
530 			struct ip *ip = mtod(n, struct ip *);
531 			ip_savecontrol(inp, &opts, ip, n);
532 		}
533 
534 		m_adj(n, off);
535 		if (sbappendaddr(&so->so_rcv, src, n,
536 				opts) == 0) {
537 			m_freem(n);
538 			if (opts)
539 				m_freem(opts);
540 			udpstat.udps_fullsock++;
541 		} else
542 			sorwakeup(so);
543 	}
544 }
545 #endif
546 
547 #ifdef INET6
548 static void
549 udp6_sendup(m, off, src, so)
550 	struct mbuf *m;
551 	int off;	/* offset of data portion */
552 	struct sockaddr *src;
553 	struct socket *so;
554 {
555 	struct mbuf *opts = NULL;
556 	struct mbuf *n;
557 	struct in6pcb *in6p = NULL;
558 
559 	if (!so)
560 		return;
561 	if (so->so_proto->pr_domain->dom_family != AF_INET6)
562 		return;
563 	in6p = sotoin6pcb(so);
564 
565 #ifdef IPSEC
566 	/* check AH/ESP integrity. */
567 	if (so != NULL && ipsec6_in_reject_so(m, so)) {
568 		ipsec6stat.in_polvio++;
569 		return;
570 	}
571 #endif /*IPSEC*/
572 
573 	if ((n = m_copy(m, 0, M_COPYALL)) != NULL) {
574 		if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS
575 			  || in6p->in6p_socket->so_options & SO_TIMESTAMP)) {
576 			struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *);
577 			ip6_savecontrol(in6p, &opts, ip6, n);
578 		}
579 
580 		m_adj(n, off);
581 		if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) {
582 			m_freem(n);
583 			if (opts)
584 				m_freem(opts);
585 			udp6stat.udp6s_fullsock++;
586 		} else
587 			sorwakeup(so);
588 	}
589 }
590 #endif
591 
592 #ifdef INET
593 static int
594 udp4_realinput(src, dst, m, off)
595 	struct sockaddr_in *src;
596 	struct sockaddr_in *dst;
597 	struct mbuf *m;
598 	int off;	/* offset of udphdr */
599 {
600 	u_int16_t *sport, *dport;
601 	int rcvcnt;
602 	struct in_addr *src4, *dst4;
603 	struct inpcb *inp;
604 
605 	rcvcnt = 0;
606 	off += sizeof(struct udphdr);	/* now, offset of payload */
607 
608 	if (src->sin_family != AF_INET || dst->sin_family != AF_INET)
609 		goto bad;
610 
611 	src4 = &src->sin_addr;
612 	sport = &src->sin_port;
613 	dst4 = &dst->sin_addr;
614 	dport = &dst->sin_port;
615 
616 	if (IN_MULTICAST(dst4->s_addr) ||
617 	    in_broadcast(*dst4, m->m_pkthdr.rcvif)) {
618 		struct inpcb *last;
619 		/*
620 		 * Deliver a multicast or broadcast datagram to *all* sockets
621 		 * for which the local and remote addresses and ports match
622 		 * those of the incoming datagram.  This allows more than
623 		 * one process to receive multi/broadcasts on the same port.
624 		 * (This really ought to be done for unicast datagrams as
625 		 * well, but that would cause problems with existing
626 		 * applications that open both address-specific sockets and
627 		 * a wildcard socket listening to the same port -- they would
628 		 * end up receiving duplicates of every unicast datagram.
629 		 * Those applications open the multiple sockets to overcome an
630 		 * inadequacy of the UDP socket interface, but for backwards
631 		 * compatibility we avoid the problem here rather than
632 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
633 		 */
634 
635 		/*
636 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
637 		 * we need udpiphdr for IPsec processing so we do that later.
638 		 */
639 		/*
640 		 * Locate pcb(s) for datagram.
641 		 */
642 		CIRCLEQ_FOREACH(inp, &udbtable.inpt_queue, inp_queue) {
643 			if (inp->inp_lport != *dport)
644 				continue;
645 			if (!in_nullhost(inp->inp_laddr)) {
646 				if (!in_hosteq(inp->inp_laddr, *dst4))
647 					continue;
648 			}
649 			if (!in_nullhost(inp->inp_faddr)) {
650 				if (!in_hosteq(inp->inp_faddr, *src4) ||
651 				    inp->inp_fport != *sport)
652 					continue;
653 			}
654 
655 			last = inp;
656 			udp4_sendup(m, off, (struct sockaddr *)src,
657 				inp->inp_socket);
658 			rcvcnt++;
659 
660 			/*
661 			 * Don't look for additional matches if this one does
662 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
663 			 * socket options set.  This heuristic avoids searching
664 			 * through all pcbs in the common case of a non-shared
665 			 * port.  It assumes that an application will never
666 			 * clear these options after setting them.
667 			 */
668 			if ((inp->inp_socket->so_options &
669 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
670 				break;
671 		}
672 	} else {
673 		/*
674 		 * Locate pcb for datagram.
675 		 */
676 		inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport);
677 		if (inp == 0) {
678 			++udpstat.udps_pcbhashmiss;
679 			inp = in_pcblookup_bind(&udbtable, *dst4, *dport);
680 			if (inp == 0)
681 				return rcvcnt;
682 		}
683 
684 		udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket);
685 		rcvcnt++;
686 	}
687 
688 bad:
689 	return rcvcnt;
690 }
691 #endif
692 
693 #ifdef INET6
694 static int
695 udp6_realinput(af, src, dst, m, off)
696 	int af;		/* af on packet */
697 	struct sockaddr_in6 *src;
698 	struct sockaddr_in6 *dst;
699 	struct mbuf *m;
700 	int off;	/* offset of udphdr */
701 {
702 	u_int16_t sport, dport;
703 	int rcvcnt;
704 	struct in6_addr src6, dst6;
705 	const struct in_addr *dst4;
706 	struct in6pcb *in6p;
707 
708 	rcvcnt = 0;
709 	off += sizeof(struct udphdr);	/* now, offset of payload */
710 
711 	if (af != AF_INET && af != AF_INET6)
712 		goto bad;
713 	if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6)
714 		goto bad;
715 
716 	in6_embedscope(&src6, src, NULL, NULL);
717 	sport = src->sin6_port;
718 	in6_embedscope(&dst6, dst, NULL, NULL);
719 	dport = dst->sin6_port;
720 	dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12];
721 
722 	if (IN6_IS_ADDR_MULTICAST(&dst6) ||
723 	    (af == AF_INET && IN_MULTICAST(dst4->s_addr))) {
724 		struct in6pcb *last;
725 		/*
726 		 * Deliver a multicast or broadcast datagram to *all* sockets
727 		 * for which the local and remote addresses and ports match
728 		 * those of the incoming datagram.  This allows more than
729 		 * one process to receive multi/broadcasts on the same port.
730 		 * (This really ought to be done for unicast datagrams as
731 		 * well, but that would cause problems with existing
732 		 * applications that open both address-specific sockets and
733 		 * a wildcard socket listening to the same port -- they would
734 		 * end up receiving duplicates of every unicast datagram.
735 		 * Those applications open the multiple sockets to overcome an
736 		 * inadequacy of the UDP socket interface, but for backwards
737 		 * compatibility we avoid the problem here rather than
738 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
739 		 */
740 
741 		/*
742 		 * KAME note: traditionally we dropped udpiphdr from mbuf here.
743 		 * we need udpiphdr for IPsec processing so we do that later.
744 		 */
745 		/*
746 		 * Locate pcb(s) for datagram.
747 		 */
748 		for (in6p = udb6.in6p_next; in6p != &udb6;
749 		     in6p = in6p->in6p_next) {
750 			if (in6p->in6p_lport != dport)
751 				continue;
752 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) {
753 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6))
754 					continue;
755 			} else {
756 				if (IN6_IS_ADDR_V4MAPPED(&dst6) &&
757 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
758 					continue;
759 			}
760 			if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
761 				if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr,
762 				    &src6) || in6p->in6p_fport != sport)
763 					continue;
764 			} else {
765 				if (IN6_IS_ADDR_V4MAPPED(&src6) &&
766 				    (in6p->in6p_flags & IN6P_IPV6_V6ONLY))
767 					continue;
768 			}
769 
770 			last = in6p;
771 			udp6_sendup(m, off, (struct sockaddr *)src,
772 				in6p->in6p_socket);
773 			rcvcnt++;
774 
775 			/*
776 			 * Don't look for additional matches if this one does
777 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
778 			 * socket options set.  This heuristic avoids searching
779 			 * through all pcbs in the common case of a non-shared
780 			 * port.  It assumes that an application will never
781 			 * clear these options after setting them.
782 			 */
783 			if ((in6p->in6p_socket->so_options &
784 			    (SO_REUSEPORT|SO_REUSEADDR)) == 0)
785 				break;
786 		}
787 	} else {
788 		/*
789 		 * Locate pcb for datagram.
790 		 */
791 		in6p = in6_pcblookup_connect(&udb6, &src6, sport,
792 		    &dst6, dport, 0);
793 		if (in6p == 0) {
794 			++udpstat.udps_pcbhashmiss;
795 			in6p = in6_pcblookup_bind(&udb6, &dst6, dport, 0);
796 			if (in6p == 0)
797 				return rcvcnt;
798 		}
799 
800 		udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket);
801 		rcvcnt++;
802 	}
803 
804 bad:
805 	return rcvcnt;
806 }
807 #endif
808 
809 #ifdef INET
810 /*
811  * Notify a udp user of an asynchronous error;
812  * just wake up so that he can collect error status.
813  */
814 static void
815 udp_notify(inp, errno)
816 	struct inpcb *inp;
817 	int errno;
818 {
819 
820 	inp->inp_socket->so_error = errno;
821 	sorwakeup(inp->inp_socket);
822 	sowwakeup(inp->inp_socket);
823 }
824 
825 void *
826 udp_ctlinput(cmd, sa, v)
827 	int cmd;
828 	struct sockaddr *sa;
829 	void *v;
830 {
831 	struct ip *ip = v;
832 	struct udphdr *uh;
833 	void (*notify) __P((struct inpcb *, int)) = udp_notify;
834 	int errno;
835 
836 	if (sa->sa_family != AF_INET
837 	 || sa->sa_len != sizeof(struct sockaddr_in))
838 		return NULL;
839 	if ((unsigned)cmd >= PRC_NCMDS)
840 		return NULL;
841 	errno = inetctlerrmap[cmd];
842 	if (PRC_IS_REDIRECT(cmd))
843 		notify = in_rtchange, ip = 0;
844 	else if (cmd == PRC_HOSTDEAD)
845 		ip = 0;
846 	else if (errno == 0)
847 		return NULL;
848 	if (ip) {
849 		uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
850 		in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport,
851 		    ip->ip_src, uh->uh_sport, errno, notify);
852 
853 		/* XXX mapped address case */
854 	} else
855 		in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno,
856 		    notify);
857 	return NULL;
858 }
859 
860 int
861 #if __STDC__
862 udp_output(struct mbuf *m, ...)
863 #else
864 udp_output(m, va_alist)
865 	struct mbuf *m;
866 	va_dcl
867 #endif
868 {
869 	struct inpcb *inp;
870 	struct udpiphdr *ui;
871 	int len = m->m_pkthdr.len;
872 	int error = 0;
873 	va_list ap;
874 
875 	va_start(ap, m);
876 	inp = va_arg(ap, struct inpcb *);
877 	va_end(ap);
878 
879 	/*
880 	 * Calculate data length and get a mbuf
881 	 * for UDP and IP headers.
882 	 */
883 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
884 	if (m == 0) {
885 		error = ENOBUFS;
886 		goto release;
887 	}
888 
889 	/*
890 	 * Compute the packet length of the IP header, and
891 	 * punt if the length looks bogus.
892 	 */
893 	if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
894 		error = EMSGSIZE;
895 		goto release;
896 	}
897 
898 	/*
899 	 * Fill in mbuf with extended UDP header
900 	 * and addresses and length put into network format.
901 	 */
902 	ui = mtod(m, struct udpiphdr *);
903 	ui->ui_pr = IPPROTO_UDP;
904 	ui->ui_src = inp->inp_laddr;
905 	ui->ui_dst = inp->inp_faddr;
906 	ui->ui_sport = inp->inp_lport;
907 	ui->ui_dport = inp->inp_fport;
908 	ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr));
909 
910 	/*
911 	 * Set up checksum and output datagram.
912 	 */
913 	if (udpcksum) {
914 		/*
915 		 * XXX Cache pseudo-header checksum part for
916 		 * XXX "connected" UDP sockets.
917 		 */
918 		ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr,
919 		    ui->ui_dst.s_addr, htons((u_int16_t)len +
920 		    sizeof(struct udphdr) + IPPROTO_UDP));
921 		m->m_pkthdr.csum_flags = M_CSUM_UDPv4;
922 		m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
923 	} else
924 		ui->ui_sum = 0;
925 	((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
926 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;	/* XXX */
927 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;	/* XXX */
928 	udpstat.udps_opackets++;
929 
930 #ifdef IPSEC
931 	if (ipsec_setsocket(m, inp->inp_socket) != 0) {
932 		error = ENOBUFS;
933 		goto release;
934 	}
935 #endif /*IPSEC*/
936 
937 	return (ip_output(m, inp->inp_options, &inp->inp_route,
938 	    inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST),
939 	    inp->inp_moptions));
940 
941 release:
942 	m_freem(m);
943 	return (error);
944 }
945 
946 int	udp_sendspace = 9216;		/* really max datagram size */
947 int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
948 					/* 40 1K datagrams */
949 
950 /*ARGSUSED*/
951 int
952 udp_usrreq(so, req, m, nam, control, p)
953 	struct socket *so;
954 	int req;
955 	struct mbuf *m, *nam, *control;
956 	struct proc *p;
957 {
958 	struct inpcb *inp;
959 	int s;
960 	int error = 0;
961 
962 	if (req == PRU_CONTROL)
963 		return (in_control(so, (long)m, (caddr_t)nam,
964 		    (struct ifnet *)control, p));
965 
966 	if (req == PRU_PURGEIF) {
967 		in_pcbpurgeif0(&udbtable, (struct ifnet *)control);
968 		in_purgeif((struct ifnet *)control);
969 		in_pcbpurgeif(&udbtable, (struct ifnet *)control);
970 		return (0);
971 	}
972 
973 	s = splsoftnet();
974 	inp = sotoinpcb(so);
975 #ifdef DIAGNOSTIC
976 	if (req != PRU_SEND && req != PRU_SENDOOB && control)
977 		panic("udp_usrreq: unexpected control mbuf");
978 #endif
979 	if (inp == 0 && req != PRU_ATTACH) {
980 		error = EINVAL;
981 		goto release;
982 	}
983 
984 	/*
985 	 * Note: need to block udp_input while changing
986 	 * the udp pcb queue and/or pcb addresses.
987 	 */
988 	switch (req) {
989 
990 	case PRU_ATTACH:
991 		if (inp != 0) {
992 			error = EISCONN;
993 			break;
994 		}
995 		if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
996 			error = soreserve(so, udp_sendspace, udp_recvspace);
997 			if (error)
998 				break;
999 		}
1000 		error = in_pcballoc(so, &udbtable);
1001 		if (error)
1002 			break;
1003 		inp = sotoinpcb(so);
1004 		inp->inp_ip.ip_ttl = ip_defttl;
1005 		break;
1006 
1007 	case PRU_DETACH:
1008 		in_pcbdetach(inp);
1009 		break;
1010 
1011 	case PRU_BIND:
1012 		error = in_pcbbind(inp, nam, p);
1013 		break;
1014 
1015 	case PRU_LISTEN:
1016 		error = EOPNOTSUPP;
1017 		break;
1018 
1019 	case PRU_CONNECT:
1020 		error = in_pcbconnect(inp, nam);
1021 		if (error)
1022 			break;
1023 		soisconnected(so);
1024 		break;
1025 
1026 	case PRU_CONNECT2:
1027 		error = EOPNOTSUPP;
1028 		break;
1029 
1030 	case PRU_DISCONNECT:
1031 		/*soisdisconnected(so);*/
1032 		so->so_state &= ~SS_ISCONNECTED;	/* XXX */
1033 		in_pcbdisconnect(inp);
1034 		inp->inp_laddr = zeroin_addr;		/* XXX */
1035 		in_pcbstate(inp, INP_BOUND);		/* XXX */
1036 		break;
1037 
1038 	case PRU_SHUTDOWN:
1039 		socantsendmore(so);
1040 		break;
1041 
1042 	case PRU_RCVD:
1043 		error = EOPNOTSUPP;
1044 		break;
1045 
1046 	case PRU_SEND:
1047 		if (control && control->m_len) {
1048 			m_freem(control);
1049 			m_freem(m);
1050 			error = EINVAL;
1051 			break;
1052 		}
1053 	{
1054 		struct in_addr laddr;			/* XXX */
1055 
1056 		if (nam) {
1057 			laddr = inp->inp_laddr;		/* XXX */
1058 			if ((so->so_state & SS_ISCONNECTED) != 0) {
1059 				error = EISCONN;
1060 				goto die;
1061 			}
1062 			error = in_pcbconnect(inp, nam);
1063 			if (error) {
1064 			die:
1065 				m_freem(m);
1066 				break;
1067 			}
1068 		} else {
1069 			if ((so->so_state & SS_ISCONNECTED) == 0) {
1070 				error = ENOTCONN;
1071 				goto die;
1072 			}
1073 		}
1074 		error = udp_output(m, inp);
1075 		if (nam) {
1076 			in_pcbdisconnect(inp);
1077 			inp->inp_laddr = laddr;		/* XXX */
1078 			in_pcbstate(inp, INP_BOUND);	/* XXX */
1079 		}
1080 	}
1081 		break;
1082 
1083 	case PRU_SENSE:
1084 		/*
1085 		 * stat: don't bother with a blocksize.
1086 		 */
1087 		splx(s);
1088 		return (0);
1089 
1090 	case PRU_RCVOOB:
1091 		error =  EOPNOTSUPP;
1092 		break;
1093 
1094 	case PRU_SENDOOB:
1095 		m_freem(control);
1096 		m_freem(m);
1097 		error =  EOPNOTSUPP;
1098 		break;
1099 
1100 	case PRU_SOCKADDR:
1101 		in_setsockaddr(inp, nam);
1102 		break;
1103 
1104 	case PRU_PEERADDR:
1105 		in_setpeeraddr(inp, nam);
1106 		break;
1107 
1108 	default:
1109 		panic("udp_usrreq");
1110 	}
1111 
1112 release:
1113 	splx(s);
1114 	return (error);
1115 }
1116 
1117 /*
1118  * Sysctl for udp variables.
1119  */
1120 int
1121 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen)
1122 	int *name;
1123 	u_int namelen;
1124 	void *oldp;
1125 	size_t *oldlenp;
1126 	void *newp;
1127 	size_t newlen;
1128 {
1129 	/* All sysctl names at this level are terminal. */
1130 	if (namelen != 1)
1131 		return (ENOTDIR);
1132 
1133 	switch (name[0]) {
1134 	case UDPCTL_CHECKSUM:
1135 		return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum));
1136 	case UDPCTL_SENDSPACE:
1137 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1138 		    &udp_sendspace));
1139 	case UDPCTL_RECVSPACE:
1140 		return (sysctl_int(oldp, oldlenp, newp, newlen,
1141 		    &udp_recvspace));
1142 	default:
1143 		return (ENOPROTOOPT);
1144 	}
1145 	/* NOTREACHED */
1146 }
1147 #endif
1148