xref: /openbsd-src/sys/netinet/udp_usrreq.c (revision dcc91c2622318df8f66a9bca2d2864253df1bfc3)
1 /*	$OpenBSD: udp_usrreq.c,v 1.324 2024/08/06 20:15:53 mvs Exp $	*/
2 /*	$NetBSD: udp_usrreq.c,v 1.28 1996/03/16 23:54:03 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)COPYRIGHT	1.1 (NRL) 17 January 1995
33  *
34  * NRL grants permission for redistribution and use in source and binary
35  * forms, with or without modification, of the software and documentation
36  * created at NRL provided that the following conditions are met:
37  *
38  * 1. Redistributions of source code must retain the above copyright
39  *    notice, this list of conditions and the following disclaimer.
40  * 2. Redistributions in binary form must reproduce the above copyright
41  *    notice, this list of conditions and the following disclaimer in the
42  *    documentation and/or other materials provided with the distribution.
43  * 3. All advertising materials mentioning features or use of this software
44  *    must display the following acknowledgements:
45  *	This product includes software developed by the University of
46  *	California, Berkeley and its contributors.
47  *	This product includes software developed at the Information
48  *	Technology Division, US Naval Research Laboratory.
49  * 4. Neither the name of the NRL nor the names of its contributors
50  *    may be used to endorse or promote products derived from this software
51  *    without specific prior written permission.
52  *
53  * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54  * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL NRL OR
57  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64  *
65  * The views and conclusions contained in the software and documentation
66  * are those of the authors and should not be interpreted as representing
67  * official policies, either expressed or implied, of the US Naval
68  * Research Laboratory (NRL).
69  */
70 
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/mbuf.h>
74 #include <sys/protosw.h>
75 #include <sys/socket.h>
76 #include <sys/socketvar.h>
77 #include <sys/sysctl.h>
78 #include <sys/domain.h>
79 
80 #include <net/if.h>
81 #include <net/if_var.h>
82 #include <net/if_media.h>
83 #include <net/route.h>
84 
85 #include <netinet/in.h>
86 #include <netinet/in_var.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/ip_var.h>
90 #include <netinet/ip_icmp.h>
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 
94 #ifdef IPSEC
95 #include <netinet/ip_ipsp.h>
96 #include <netinet/ip_esp.h>
97 #endif
98 
99 #ifdef INET6
100 #include <netinet6/in6_var.h>
101 #include <netinet6/ip6_var.h>
102 #include <netinet6/ip6protosw.h>
103 #endif /* INET6 */
104 
105 #include "pf.h"
106 #if NPF > 0
107 #include <net/pfvar.h>
108 #endif
109 
110 #ifdef PIPEX
111 #include <netinet/if_ether.h>
112 #include <net/pipex.h>
113 #endif
114 
115 /*
116  * Locks used to protect data:
117  *	a	atomic
118  */
119 
120 /*
121  * UDP protocol implementation.
122  * Per RFC 768, August, 1980.
123  */
124 int	udpcksum = 1;			/* [a] */
125 
126 u_int	udp_sendspace = 9216;		/* [a] really max datagram size */
127 u_int	udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in));
128 					/* [a] 40 1K datagrams */
129 
130 const struct pr_usrreqs udp_usrreqs = {
131 	.pru_attach	= udp_attach,
132 	.pru_detach	= udp_detach,
133 	.pru_bind	= udp_bind,
134 	.pru_connect	= udp_connect,
135 	.pru_disconnect	= udp_disconnect,
136 	.pru_shutdown	= udp_shutdown,
137 	.pru_send	= udp_send,
138 	.pru_control	= in_control,
139 	.pru_sockaddr	= in_sockaddr,
140 	.pru_peeraddr	= in_peeraddr,
141 };
142 
143 #ifdef INET6
144 const struct pr_usrreqs udp6_usrreqs = {
145 	.pru_attach	= udp_attach,
146 	.pru_detach	= udp_detach,
147 	.pru_bind	= udp_bind,
148 	.pru_connect	= udp_connect,
149 	.pru_disconnect	= udp_disconnect,
150 	.pru_shutdown	= udp_shutdown,
151 	.pru_send	= udp_send,
152 	.pru_control	= in6_control,
153 	.pru_sockaddr	= in6_sockaddr,
154 	.pru_peeraddr	= in6_peeraddr,
155 };
156 #endif
157 
158 const struct sysctl_bounded_args udpctl_vars[] = {
159 	{ UDPCTL_CHECKSUM, &udpcksum, 0, 1 },
160 	{ UDPCTL_RECVSPACE, &udp_recvspace, 0, INT_MAX },
161 	{ UDPCTL_SENDSPACE, &udp_sendspace, 0, INT_MAX },
162 };
163 
164 struct	inpcbtable udbtable;
165 #ifdef INET6
166 struct	inpcbtable udb6table;
167 #endif
168 struct	cpumem *udpcounters;
169 
170 void	udp_sbappend(struct inpcb *, struct mbuf *, struct ip *,
171 	    struct ip6_hdr *, int, struct udphdr *, struct sockaddr *,
172 	    u_int32_t);
173 int	udp_output(struct inpcb *, struct mbuf *, struct mbuf *, struct mbuf *);
174 void	udp_notify(struct inpcb *, int);
175 int	udp_sysctl_udpstat(void *, size_t *, void *);
176 
177 #ifndef	UDB_INITIAL_HASH_SIZE
178 #define	UDB_INITIAL_HASH_SIZE	128
179 #endif
180 
181 void
182 udp_init(void)
183 {
184 	udpcounters = counters_alloc(udps_ncounters);
185 	in_pcbinit(&udbtable, UDB_INITIAL_HASH_SIZE);
186 #ifdef INET6
187 	in_pcbinit(&udb6table, UDB_INITIAL_HASH_SIZE);
188 #endif
189 }
190 
191 int
192 udp_input(struct mbuf **mp, int *offp, int proto, int af)
193 {
194 	struct mbuf *m = *mp;
195 	int iphlen = *offp;
196 	struct ip *ip = NULL;
197 	struct udphdr *uh;
198 	struct inpcb *inp = NULL;
199 	struct ip save_ip;
200 	int len;
201 	u_int16_t savesum;
202 	union {
203 		struct sockaddr sa;
204 		struct sockaddr_in sin;
205 #ifdef INET6
206 		struct sockaddr_in6 sin6;
207 #endif /* INET6 */
208 	} srcsa, dstsa;
209 	struct ip6_hdr *ip6 = NULL;
210 	u_int32_t ipsecflowinfo = 0;
211 
212 	udpstat_inc(udps_ipackets);
213 
214 	IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr));
215 	if (!uh) {
216 		udpstat_inc(udps_hdrops);
217 		return IPPROTO_DONE;
218 	}
219 
220 	/* Check for illegal destination port 0 */
221 	if (uh->uh_dport == 0) {
222 		udpstat_inc(udps_noport);
223 		goto bad;
224 	}
225 
226 	/*
227 	 * Make mbuf data length reflect UDP length.
228 	 * If not enough data to reflect UDP length, drop.
229 	 */
230 	len = ntohs((u_int16_t)uh->uh_ulen);
231 	switch (af) {
232 	case AF_INET:
233 		if (m->m_pkthdr.len - iphlen != len) {
234 			if (len > (m->m_pkthdr.len - iphlen) ||
235 			    len < sizeof(struct udphdr)) {
236 				udpstat_inc(udps_badlen);
237 				goto bad;
238 			}
239 			m_adj(m, len - (m->m_pkthdr.len - iphlen));
240 		}
241 		ip = mtod(m, struct ip *);
242 		/*
243 		 * Save a copy of the IP header in case we want restore it
244 		 * for sending an ICMP error message in response.
245 		 */
246 		save_ip = *ip;
247 		break;
248 #ifdef INET6
249 	case AF_INET6:
250 		/* jumbograms */
251 		if (len == 0 && m->m_pkthdr.len - iphlen > 0xffff)
252 			len = m->m_pkthdr.len - iphlen;
253 		if (len != m->m_pkthdr.len - iphlen) {
254 			udpstat_inc(udps_badlen);
255 			goto bad;
256 		}
257 		ip6 = mtod(m, struct ip6_hdr *);
258 		break;
259 #endif /* INET6 */
260 	default:
261 		unhandled_af(af);
262 	}
263 
264 	/*
265 	 * Checksum extended UDP header and data.
266 	 * from W.R.Stevens: check incoming udp cksums even if
267 	 *	udpcksum is not set.
268 	 */
269 	savesum = uh->uh_sum;
270 	if (uh->uh_sum == 0) {
271 		udpstat_inc(udps_nosum);
272 #ifdef INET6
273 		/*
274 		 * In IPv6, the UDP checksum is ALWAYS used.
275 		 */
276 		if (ip6)
277 			goto bad;
278 #endif /* INET6 */
279 	} else {
280 		if ((m->m_pkthdr.csum_flags & M_UDP_CSUM_IN_OK) == 0) {
281 			if (m->m_pkthdr.csum_flags & M_UDP_CSUM_IN_BAD) {
282 				udpstat_inc(udps_badsum);
283 				goto bad;
284 			}
285 			udpstat_inc(udps_inswcsum);
286 
287 			if (ip)
288 				uh->uh_sum = in4_cksum(m, IPPROTO_UDP,
289 				    iphlen, len);
290 #ifdef INET6
291 			else if (ip6)
292 				uh->uh_sum = in6_cksum(m, IPPROTO_UDP,
293 				    iphlen, len);
294 #endif /* INET6 */
295 			if (uh->uh_sum != 0) {
296 				udpstat_inc(udps_badsum);
297 				goto bad;
298 			}
299 		}
300 	}
301 
302 #ifdef IPSEC
303 	if (udpencap_enable && udpencap_port && esp_enable &&
304 #if NPF > 0
305 	    !(m->m_pkthdr.pf.flags & PF_TAG_DIVERTED) &&
306 #endif
307 	    uh->uh_dport == htons(udpencap_port)) {
308 		u_int32_t spi;
309 		int skip = iphlen + sizeof(struct udphdr);
310 
311 		if (m->m_pkthdr.len - skip < sizeof(u_int32_t)) {
312 			/* packet too short */
313 			m_freem(m);
314 			return IPPROTO_DONE;
315 		}
316 		m_copydata(m, skip, sizeof(u_int32_t), (caddr_t) &spi);
317 		/*
318 		 * decapsulate if the SPI is not zero, otherwise pass
319 		 * to userland
320 		 */
321 		if (spi != 0) {
322 			int protoff;
323 
324 			if ((m = *mp = m_pullup(m, skip)) == NULL) {
325 				udpstat_inc(udps_hdrops);
326 				return IPPROTO_DONE;
327 			}
328 
329 			/* remove the UDP header */
330 			bcopy(mtod(m, u_char *),
331 			    mtod(m, u_char *) + sizeof(struct udphdr), iphlen);
332 			m_adj(m, sizeof(struct udphdr));
333 			skip -= sizeof(struct udphdr);
334 
335 			espstat_inc(esps_udpencin);
336 			protoff = af == AF_INET ? offsetof(struct ip, ip_p) :
337 			    offsetof(struct ip6_hdr, ip6_nxt);
338 			return ipsec_common_input(mp, skip, protoff,
339 			    af, IPPROTO_ESP, 1);
340 		}
341 	}
342 #endif /* IPSEC */
343 
344 	switch (af) {
345 	case AF_INET:
346 		bzero(&srcsa, sizeof(struct sockaddr_in));
347 		srcsa.sin.sin_len = sizeof(struct sockaddr_in);
348 		srcsa.sin.sin_family = AF_INET;
349 		srcsa.sin.sin_port = uh->uh_sport;
350 		srcsa.sin.sin_addr = ip->ip_src;
351 
352 		bzero(&dstsa, sizeof(struct sockaddr_in));
353 		dstsa.sin.sin_len = sizeof(struct sockaddr_in);
354 		dstsa.sin.sin_family = AF_INET;
355 		dstsa.sin.sin_port = uh->uh_dport;
356 		dstsa.sin.sin_addr = ip->ip_dst;
357 		break;
358 #ifdef INET6
359 	case AF_INET6:
360 		bzero(&srcsa, sizeof(struct sockaddr_in6));
361 		srcsa.sin6.sin6_len = sizeof(struct sockaddr_in6);
362 		srcsa.sin6.sin6_family = AF_INET6;
363 		srcsa.sin6.sin6_port = uh->uh_sport;
364 #if 0 /*XXX inbound flowinfo */
365 		srcsa.sin6.sin6_flowinfo = htonl(0x0fffffff) & ip6->ip6_flow;
366 #endif
367 		/* KAME hack: recover scopeid */
368 		in6_recoverscope(&srcsa.sin6, &ip6->ip6_src);
369 
370 		bzero(&dstsa, sizeof(struct sockaddr_in6));
371 		dstsa.sin6.sin6_len = sizeof(struct sockaddr_in6);
372 		dstsa.sin6.sin6_family = AF_INET6;
373 		dstsa.sin6.sin6_port = uh->uh_dport;
374 #if 0 /*XXX inbound flowinfo */
375 		dstsa.sin6.sin6_flowinfo = htonl(0x0fffffff) & ip6->ip6_flow;
376 #endif
377 		/* KAME hack: recover scopeid */
378 		in6_recoverscope(&dstsa.sin6, &ip6->ip6_dst);
379 		break;
380 #endif /* INET6 */
381 	}
382 
383 	if (m->m_flags & (M_BCAST|M_MCAST)) {
384 		SIMPLEQ_HEAD(, inpcb) inpcblist;
385 		struct inpcbtable *table;
386 
387 		/*
388 		 * Deliver a multicast or broadcast datagram to *all* sockets
389 		 * for which the local and remote addresses and ports match
390 		 * those of the incoming datagram.  This allows more than
391 		 * one process to receive multi/broadcasts on the same port.
392 		 * (This really ought to be done for unicast datagrams as
393 		 * well, but that would cause problems with existing
394 		 * applications that open both address-specific sockets and
395 		 * a wildcard socket listening to the same port -- they would
396 		 * end up receiving duplicates of every unicast datagram.
397 		 * Those applications open the multiple sockets to overcome an
398 		 * inadequacy of the UDP socket interface, but for backwards
399 		 * compatibility we avoid the problem here rather than
400 		 * fixing the interface.  Maybe 4.5BSD will remedy this?)
401 		 */
402 
403 		/*
404 		 * Locate pcb(s) for datagram.
405 		 * (Algorithm copied from raw_intr().)
406 		 */
407 		SIMPLEQ_INIT(&inpcblist);
408 #ifdef INET6
409 		if (ip6)
410 			table = &udb6table;
411 		else
412 #endif
413 			table = &udbtable;
414 
415 		rw_enter_write(&table->inpt_notify);
416 		mtx_enter(&table->inpt_mtx);
417 		TAILQ_FOREACH(inp, &table->inpt_queue, inp_queue) {
418 			if (ip6)
419 				KASSERT(ISSET(inp->inp_flags, INP_IPV6));
420 			else
421 				KASSERT(!ISSET(inp->inp_flags, INP_IPV6));
422 
423 			if (inp->inp_socket->so_rcv.sb_state & SS_CANTRCVMORE)
424 				continue;
425 			if (rtable_l2(inp->inp_rtableid) !=
426 			    rtable_l2(m->m_pkthdr.ph_rtableid))
427 				continue;
428 			if (inp->inp_lport != uh->uh_dport)
429 				continue;
430 #ifdef INET6
431 			if (ip6) {
432 				if (inp->inp_ip6_minhlim &&
433 				    inp->inp_ip6_minhlim > ip6->ip6_hlim)
434 					continue;
435 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_laddr6))
436 					if (!IN6_ARE_ADDR_EQUAL(
437 					    &inp->inp_laddr6, &ip6->ip6_dst))
438 						continue;
439 			} else
440 #endif /* INET6 */
441 			{
442 				if (inp->inp_ip_minttl &&
443 				    inp->inp_ip_minttl > ip->ip_ttl)
444 					continue;
445 
446 				if (inp->inp_laddr.s_addr != INADDR_ANY) {
447 					if (inp->inp_laddr.s_addr !=
448 					    ip->ip_dst.s_addr)
449 						continue;
450 				}
451 			}
452 #ifdef INET6
453 			if (ip6) {
454 				if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
455 					if (!IN6_ARE_ADDR_EQUAL(
456 					    &inp->inp_faddr6, &ip6->ip6_src) ||
457 					    inp->inp_fport != uh->uh_sport)
458 						continue;
459 			} else
460 #endif /* INET6 */
461 			if (inp->inp_faddr.s_addr != INADDR_ANY) {
462 				if (inp->inp_faddr.s_addr !=
463 				    ip->ip_src.s_addr ||
464 				    inp->inp_fport != uh->uh_sport)
465 					continue;
466 			}
467 
468 			in_pcbref(inp);
469 			SIMPLEQ_INSERT_TAIL(&inpcblist, inp, inp_notify);
470 
471 			/*
472 			 * Don't look for additional matches if this one does
473 			 * not have either the SO_REUSEPORT or SO_REUSEADDR
474 			 * socket options set.  This heuristic avoids searching
475 			 * through all pcbs in the common case of a non-shared
476 			 * port.  It assumes that an application will never
477 			 * clear these options after setting them.
478 			 */
479 			if ((inp->inp_socket->so_options & (SO_REUSEPORT |
480 			    SO_REUSEADDR)) == 0)
481 				break;
482 		}
483 		mtx_leave(&table->inpt_mtx);
484 
485 		if (SIMPLEQ_EMPTY(&inpcblist)) {
486 			rw_exit_write(&table->inpt_notify);
487 
488 			/*
489 			 * No matching pcb found; discard datagram.
490 			 * (No need to send an ICMP Port Unreachable
491 			 * for a broadcast or multicast datgram.)
492 			 */
493 			udpstat_inc(udps_noportbcast);
494 			goto bad;
495 		}
496 
497 		while ((inp = SIMPLEQ_FIRST(&inpcblist)) != NULL) {
498 			struct mbuf *n;
499 
500 			SIMPLEQ_REMOVE_HEAD(&inpcblist, inp_notify);
501 			if (SIMPLEQ_EMPTY(&inpcblist))
502 				n = m;
503 			else
504 				n = m_copym(m, 0, M_COPYALL, M_NOWAIT);
505 			if (n != NULL) {
506 				udp_sbappend(inp, n, ip, ip6, iphlen, uh,
507 				    &srcsa.sa, 0);
508 			}
509 			in_pcbunref(inp);
510 		}
511 		rw_exit_write(&table->inpt_notify);
512 
513 		return IPPROTO_DONE;
514 	}
515 	/*
516 	 * Locate pcb for datagram.
517 	 */
518 #if NPF > 0
519 	inp = pf_inp_lookup(m);
520 #endif
521 	if (inp == NULL) {
522 #ifdef INET6
523 		if (ip6) {
524 			inp = in6_pcblookup(&udb6table, &ip6->ip6_src,
525 			    uh->uh_sport, &ip6->ip6_dst, uh->uh_dport,
526 			    m->m_pkthdr.ph_rtableid);
527 		} else
528 #endif /* INET6 */
529 		{
530 			inp = in_pcblookup(&udbtable, ip->ip_src,
531 			    uh->uh_sport, ip->ip_dst, uh->uh_dport,
532 			    m->m_pkthdr.ph_rtableid);
533 		}
534 	}
535 	if (inp == NULL) {
536 		udpstat_inc(udps_pcbhashmiss);
537 #ifdef INET6
538 		if (ip6) {
539 			inp = in6_pcblookup_listen(&udb6table, &ip6->ip6_dst,
540 			    uh->uh_dport, m, m->m_pkthdr.ph_rtableid);
541 		} else
542 #endif /* INET6 */
543 		{
544 			inp = in_pcblookup_listen(&udbtable, ip->ip_dst,
545 			    uh->uh_dport, m, m->m_pkthdr.ph_rtableid);
546 		}
547 	}
548 
549 #ifdef IPSEC
550 	if (ipsec_in_use) {
551 		struct m_tag *mtag;
552 		struct tdb_ident *tdbi;
553 		struct tdb *tdb;
554 		int error;
555 
556 		mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
557 		if (mtag != NULL) {
558 			tdbi = (struct tdb_ident *)(mtag + 1);
559 			tdb = gettdb(tdbi->rdomain, tdbi->spi,
560 			    &tdbi->dst, tdbi->proto);
561 		} else
562 			tdb = NULL;
563 		error = ipsp_spd_lookup(m, af, iphlen, IPSP_DIRECTION_IN,
564 		    tdb, inp ? &inp->inp_seclevel : NULL, NULL, NULL);
565 		if (error) {
566 			udpstat_inc(udps_nosec);
567 			tdb_unref(tdb);
568 			goto bad;
569 		}
570 		/* create ipsec options, id is not modified after creation */
571 		if (tdb && tdb->tdb_ids)
572 			ipsecflowinfo = tdb->tdb_ids->id_flow;
573 		tdb_unref(tdb);
574 	}
575 #endif /*IPSEC */
576 
577 	if (inp == NULL) {
578 		udpstat_inc(udps_noport);
579 		if (m->m_flags & (M_BCAST | M_MCAST)) {
580 			udpstat_inc(udps_noportbcast);
581 			goto bad;
582 		}
583 #ifdef INET6
584 		if (ip6) {
585 			uh->uh_sum = savesum;
586 			icmp6_error(m, ICMP6_DST_UNREACH,
587 			    ICMP6_DST_UNREACH_NOPORT,0);
588 		} else
589 #endif /* INET6 */
590 		{
591 			*ip = save_ip;
592 			uh->uh_sum = savesum;
593 			icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT,
594 			    0, 0);
595 		}
596 		return IPPROTO_DONE;
597 	}
598 
599 	KASSERT(sotoinpcb(inp->inp_socket) == inp);
600 	soassertlocked_readonly(inp->inp_socket);
601 
602 #ifdef INET6
603 	if (ip6 && inp->inp_ip6_minhlim &&
604 	    inp->inp_ip6_minhlim > ip6->ip6_hlim) {
605 		goto bad;
606 	} else
607 #endif
608 	if (ip && inp->inp_ip_minttl &&
609 	    inp->inp_ip_minttl > ip->ip_ttl) {
610 		goto bad;
611 	}
612 
613 #if NPF > 0
614 	if (inp->inp_socket->so_state & SS_ISCONNECTED)
615 		pf_inp_link(m, inp);
616 #endif
617 
618 #ifdef PIPEX
619 	if (pipex_enable && inp->inp_pipex) {
620 		struct pipex_session *session;
621 		int off = iphlen + sizeof(struct udphdr);
622 
623 		if ((session = pipex_l2tp_lookup_session(m, off)) != NULL) {
624 			m = *mp = pipex_l2tp_input(m, off, session,
625 			    ipsecflowinfo);
626 			pipex_rele_session(session);
627 			if (m == NULL) {
628 				in_pcbunref(inp);
629 				return IPPROTO_DONE;
630 			}
631 		}
632 	}
633 #endif
634 
635 	udp_sbappend(inp, m, ip, ip6, iphlen, uh, &srcsa.sa, ipsecflowinfo);
636 	in_pcbunref(inp);
637 	return IPPROTO_DONE;
638 bad:
639 	m_freem(m);
640 	in_pcbunref(inp);
641 	return IPPROTO_DONE;
642 }
643 
644 void
645 udp_sbappend(struct inpcb *inp, struct mbuf *m, struct ip *ip,
646     struct ip6_hdr *ip6, int hlen, struct udphdr *uh,
647     struct sockaddr *srcaddr, u_int32_t ipsecflowinfo)
648 {
649 	struct socket *so = inp->inp_socket;
650 	struct mbuf *opts = NULL;
651 
652 	hlen += sizeof(*uh);
653 
654 	if (inp->inp_upcall != NULL) {
655 		m = (*inp->inp_upcall)(inp->inp_upcall_arg, m,
656 		    ip, ip6, uh, hlen);
657 		if (m == NULL)
658 			return;
659 	}
660 
661 #ifdef INET6
662 	if (ip6 && (inp->inp_flags & IN6P_CONTROLOPTS ||
663 	    so->so_options & SO_TIMESTAMP))
664 		ip6_savecontrol(inp, m, &opts);
665 #endif /* INET6 */
666 	if (ip && (inp->inp_flags & INP_CONTROLOPTS ||
667 	    so->so_options & SO_TIMESTAMP))
668 		ip_savecontrol(inp, &opts, ip, m);
669 #ifdef INET6
670 	if (ip6 && (inp->inp_flags & IN6P_RECVDSTPORT)) {
671 		struct mbuf **mp = &opts;
672 
673 		while (*mp)
674 			mp = &(*mp)->m_next;
675 		*mp = sbcreatecontrol((caddr_t)&uh->uh_dport, sizeof(u_int16_t),
676 		    IPV6_RECVDSTPORT, IPPROTO_IPV6);
677 	}
678 #endif /* INET6 */
679 	if (ip && (inp->inp_flags & INP_RECVDSTPORT)) {
680 		struct mbuf **mp = &opts;
681 
682 		while (*mp)
683 			mp = &(*mp)->m_next;
684 		*mp = sbcreatecontrol((caddr_t)&uh->uh_dport, sizeof(u_int16_t),
685 		    IP_RECVDSTPORT, IPPROTO_IP);
686 	}
687 #ifdef IPSEC
688 	if (ipsecflowinfo && (inp->inp_flags & INP_IPSECFLOWINFO)) {
689 		struct mbuf **mp = &opts;
690 
691 		while (*mp)
692 			mp = &(*mp)->m_next;
693 		*mp = sbcreatecontrol((caddr_t)&ipsecflowinfo,
694 		    sizeof(u_int32_t), IP_IPSECFLOWINFO, IPPROTO_IP);
695 	}
696 #endif
697 	m_adj(m, hlen);
698 
699 	mtx_enter(&so->so_rcv.sb_mtx);
700 	if (sbappendaddr(so, &so->so_rcv, srcaddr, m, opts) == 0) {
701 		mtx_leave(&so->so_rcv.sb_mtx);
702 		udpstat_inc(udps_fullsock);
703 		m_freem(m);
704 		m_freem(opts);
705 		return;
706 	}
707 	mtx_leave(&so->so_rcv.sb_mtx);
708 
709 	sorwakeup(so);
710 }
711 
712 /*
713  * Notify a udp user of an asynchronous error;
714  * just wake up so that he can collect error status.
715  */
716 void
717 udp_notify(struct inpcb *inp, int errno)
718 {
719 	inp->inp_socket->so_error = errno;
720 	sorwakeup(inp->inp_socket);
721 	sowwakeup(inp->inp_socket);
722 }
723 
724 #ifdef INET6
725 void
726 udp6_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *d)
727 {
728 	struct udphdr uh;
729 	struct sockaddr_in6 sa6;
730 	struct ip6_hdr *ip6;
731 	struct mbuf *m;
732 	int off;
733 	void *cmdarg;
734 	struct ip6ctlparam *ip6cp = NULL;
735 	struct udp_portonly {
736 		u_int16_t uh_sport;
737 		u_int16_t uh_dport;
738 	} *uhp;
739 	struct inpcb *inp;
740 	void (*notify)(struct inpcb *, int) = udp_notify;
741 
742 	if (sa == NULL)
743 		return;
744 	if (sa->sa_family != AF_INET6 ||
745 	    sa->sa_len != sizeof(struct sockaddr_in6))
746 		return;
747 
748 	if ((unsigned)cmd >= PRC_NCMDS)
749 		return;
750 	if (PRC_IS_REDIRECT(cmd))
751 		notify = in_rtchange, d = NULL;
752 	else if (cmd == PRC_HOSTDEAD)
753 		d = NULL;
754 	else if (cmd == PRC_MSGSIZE)
755 		; /* special code is present, see below */
756 	else if (inet6ctlerrmap[cmd] == 0)
757 		return;
758 
759 	/* if the parameter is from icmp6, decode it. */
760 	if (d != NULL) {
761 		ip6cp = (struct ip6ctlparam *)d;
762 		m = ip6cp->ip6c_m;
763 		ip6 = ip6cp->ip6c_ip6;
764 		off = ip6cp->ip6c_off;
765 		cmdarg = ip6cp->ip6c_cmdarg;
766 	} else {
767 		m = NULL;
768 		ip6 = NULL;
769 		cmdarg = NULL;
770 		/* XXX: translate addresses into internal form */
771 		sa6 = *satosin6(sa);
772 		if (in6_embedscope(&sa6.sin6_addr, &sa6, NULL, NULL)) {
773 			/* should be impossible */
774 			return;
775 		}
776 	}
777 
778 	if (ip6cp && ip6cp->ip6c_finaldst) {
779 		bzero(&sa6, sizeof(sa6));
780 		sa6.sin6_family = AF_INET6;
781 		sa6.sin6_len = sizeof(sa6);
782 		sa6.sin6_addr = *ip6cp->ip6c_finaldst;
783 		/* XXX: assuming M is valid in this case */
784 		sa6.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.ph_ifidx,
785 		    ip6cp->ip6c_finaldst);
786 		if (in6_embedscope(ip6cp->ip6c_finaldst, &sa6, NULL, NULL)) {
787 			/* should be impossible */
788 			return;
789 		}
790 	} else {
791 		/* XXX: translate addresses into internal form */
792 		sa6 = *satosin6(sa);
793 		if (in6_embedscope(&sa6.sin6_addr, &sa6, NULL, NULL)) {
794 			/* should be impossible */
795 			return;
796 		}
797 	}
798 
799 	if (ip6) {
800 		/*
801 		 * XXX: We assume that when IPV6 is non NULL,
802 		 * M and OFF are valid.
803 		 */
804 		struct sockaddr_in6 sa6_src;
805 
806 		/* check if we can safely examine src and dst ports */
807 		if (m->m_pkthdr.len < off + sizeof(*uhp))
808 			return;
809 
810 		bzero(&uh, sizeof(uh));
811 		m_copydata(m, off, sizeof(*uhp), (caddr_t)&uh);
812 
813 		bzero(&sa6_src, sizeof(sa6_src));
814 		sa6_src.sin6_family = AF_INET6;
815 		sa6_src.sin6_len = sizeof(sa6_src);
816 		sa6_src.sin6_addr = ip6->ip6_src;
817 		sa6_src.sin6_scope_id = in6_addr2scopeid(m->m_pkthdr.ph_ifidx,
818 		    &ip6->ip6_src);
819 		if (in6_embedscope(&sa6_src.sin6_addr, &sa6_src, NULL, NULL)) {
820 			/* should be impossible */
821 			return;
822 		}
823 
824 		if (cmd == PRC_MSGSIZE) {
825 			/*
826 			 * Check to see if we have a valid UDP socket
827 			 * corresponding to the address in the ICMPv6 message
828 			 * payload.
829 			 */
830 			inp = in6_pcblookup(&udb6table, &sa6.sin6_addr,
831 			    uh.uh_dport, &sa6_src.sin6_addr, uh.uh_sport,
832 			    rdomain);
833 #if 0
834 			/*
835 			 * As the use of sendto(2) is fairly popular,
836 			 * we may want to allow non-connected pcb too.
837 			 * But it could be too weak against attacks...
838 			 * We should at least check if the local address (= s)
839 			 * is really ours.
840 			 */
841 			if (inp == NULL) {
842 				inp = in6_pcblookup_listen(&udb6table,
843 				    &sa6_src.sin6_addr, uh.uh_sport, NULL,
844 				    rdomain))
845 			}
846 #endif
847 
848 			/*
849 			 * Depending on the value of "valid" and routing table
850 			 * size (mtudisc_{hi,lo}wat), we will:
851 			 * - recalculate the new MTU and create the
852 			 *   corresponding routing entry, or
853 			 * - ignore the MTU change notification.
854 			 */
855 			icmp6_mtudisc_update((struct ip6ctlparam *)d,
856 			    inp != NULL);
857 			in_pcbunref(inp);
858 
859 			/*
860 			 * regardless of if we called icmp6_mtudisc_update(),
861 			 * we need to call in6_pcbnotify(), to notify path
862 			 * MTU change to the userland (2292bis-02), because
863 			 * some unconnected sockets may share the same
864 			 * destination and want to know the path MTU.
865 			 */
866 		}
867 
868 		in6_pcbnotify(&udb6table, &sa6, uh.uh_dport,
869 		    &sa6_src, uh.uh_sport, rdomain, cmd, cmdarg, notify);
870 	} else {
871 		in6_pcbnotify(&udb6table, &sa6, 0,
872 		    &sa6_any, 0, rdomain, cmd, cmdarg, notify);
873 	}
874 }
875 #endif
876 
877 void
878 udp_ctlinput(int cmd, struct sockaddr *sa, u_int rdomain, void *v)
879 {
880 	struct ip *ip = v;
881 	struct udphdr *uhp;
882 	struct in_addr faddr;
883 	struct inpcb *inp;
884 	void (*notify)(struct inpcb *, int) = udp_notify;
885 	int errno;
886 
887 	if (sa == NULL)
888 		return;
889 	if (sa->sa_family != AF_INET ||
890 	    sa->sa_len != sizeof(struct sockaddr_in))
891 		return;
892 	faddr = satosin(sa)->sin_addr;
893 	if (faddr.s_addr == INADDR_ANY)
894 		return;
895 
896 	if ((unsigned)cmd >= PRC_NCMDS)
897 		return;
898 	errno = inetctlerrmap[cmd];
899 	if (PRC_IS_REDIRECT(cmd))
900 		notify = in_rtchange, ip = 0;
901 	else if (cmd == PRC_HOSTDEAD)
902 		ip = 0;
903 	else if (errno == 0)
904 		return;
905 	if (ip) {
906 		uhp = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
907 
908 #ifdef IPSEC
909 		/* PMTU discovery for udpencap */
910 		if (cmd == PRC_MSGSIZE && ip_mtudisc && udpencap_enable &&
911 		    udpencap_port && uhp->uh_sport == htons(udpencap_port)) {
912 			udpencap_ctlinput(cmd, sa, rdomain, v);
913 			return;
914 		}
915 #endif
916 		inp = in_pcblookup(&udbtable,
917 		    ip->ip_dst, uhp->uh_dport, ip->ip_src, uhp->uh_sport,
918 		    rdomain);
919 		if (inp != NULL)
920 			notify(inp, errno);
921 		in_pcbunref(inp);
922 	} else
923 		in_pcbnotifyall(&udbtable, satosin(sa), rdomain, errno, notify);
924 }
925 
926 int
927 udp_output(struct inpcb *inp, struct mbuf *m, struct mbuf *addr,
928     struct mbuf *control)
929 {
930 	struct sockaddr_in *sin = NULL;
931 	struct udpiphdr *ui;
932 	u_int32_t ipsecflowinfo = 0;
933 	struct sockaddr_in src_sin;
934 	int len = m->m_pkthdr.len;
935 	struct in_addr laddr;
936 	int error = 0;
937 
938 #ifdef INET6
939 	if (ISSET(inp->inp_flags, INP_IPV6))
940 		return (udp6_output(inp, m, addr, control));
941 #endif
942 
943 	/*
944 	 * Compute the packet length of the IP header, and
945 	 * punt if the length looks bogus.
946 	 */
947 	if ((len + sizeof(struct udpiphdr)) > IP_MAXPACKET) {
948 		error = EMSGSIZE;
949 		goto release;
950 	}
951 
952 	memset(&src_sin, 0, sizeof(src_sin));
953 
954 	if (control) {
955 		u_int clen;
956 		struct cmsghdr *cm;
957 		caddr_t cmsgs;
958 
959 		/*
960 		 * XXX: Currently, we assume all the optional information is
961 		 * stored in a single mbuf.
962 		 */
963 		if (control->m_next) {
964 			error = EINVAL;
965 			goto release;
966 		}
967 
968 		clen = control->m_len;
969 		cmsgs = mtod(control, caddr_t);
970 		do {
971 			if (clen < CMSG_LEN(0)) {
972 				error = EINVAL;
973 				goto release;
974 			}
975 			cm = (struct cmsghdr *)cmsgs;
976 			if (cm->cmsg_len < CMSG_LEN(0) ||
977 			    CMSG_ALIGN(cm->cmsg_len) > clen) {
978 				error = EINVAL;
979 				goto release;
980 			}
981 #ifdef IPSEC
982 			if ((inp->inp_flags & INP_IPSECFLOWINFO) != 0 &&
983 			    cm->cmsg_len == CMSG_LEN(sizeof(ipsecflowinfo)) &&
984 			    cm->cmsg_level == IPPROTO_IP &&
985 			    cm->cmsg_type == IP_IPSECFLOWINFO) {
986 				ipsecflowinfo = *(u_int32_t *)CMSG_DATA(cm);
987 			} else
988 #endif
989 			if (cm->cmsg_len == CMSG_LEN(sizeof(struct in_addr)) &&
990 			    cm->cmsg_level == IPPROTO_IP &&
991 			    cm->cmsg_type == IP_SENDSRCADDR) {
992 				memcpy(&src_sin.sin_addr, CMSG_DATA(cm),
993 				    sizeof(struct in_addr));
994 				src_sin.sin_family = AF_INET;
995 				src_sin.sin_len = sizeof(src_sin);
996 				/* no check on reuse when sin->sin_port == 0 */
997 				if ((error = in_pcbaddrisavail(inp, &src_sin,
998 				    0, curproc)))
999 					goto release;
1000 			}
1001 			clen -= CMSG_ALIGN(cm->cmsg_len);
1002 			cmsgs += CMSG_ALIGN(cm->cmsg_len);
1003 		} while (clen);
1004 	}
1005 
1006 	if (addr) {
1007 		if ((error = in_nam2sin(addr, &sin)))
1008 			goto release;
1009 		if (sin->sin_port == 0) {
1010 			error = EADDRNOTAVAIL;
1011 			goto release;
1012 		}
1013 		if (inp->inp_faddr.s_addr != INADDR_ANY) {
1014 			error = EISCONN;
1015 			goto release;
1016 		}
1017 		error = in_pcbselsrc(&laddr, sin, inp);
1018 		if (error)
1019 			goto release;
1020 
1021 		if (inp->inp_lport == 0) {
1022 			error = in_pcbbind(inp, NULL, curproc);
1023 			if (error)
1024 				goto release;
1025 		}
1026 
1027 		if (src_sin.sin_len > 0 &&
1028 		    src_sin.sin_addr.s_addr != INADDR_ANY &&
1029 		    src_sin.sin_addr.s_addr != inp->inp_laddr.s_addr) {
1030 			src_sin.sin_port = inp->inp_lport;
1031 			if (inp->inp_laddr.s_addr != INADDR_ANY &&
1032 			    (error =
1033 			    in_pcbaddrisavail(inp, &src_sin, 0, curproc)))
1034 				goto release;
1035 			laddr = src_sin.sin_addr;
1036 		}
1037 	} else {
1038 		if (inp->inp_faddr.s_addr == INADDR_ANY) {
1039 			error = ENOTCONN;
1040 			goto release;
1041 		}
1042 		laddr = inp->inp_laddr;
1043 	}
1044 
1045 	/*
1046 	 * Calculate data length and get a mbuf
1047 	 * for UDP and IP headers.
1048 	 */
1049 	M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
1050 	if (m == NULL) {
1051 		error = ENOBUFS;
1052 		goto bail;
1053 	}
1054 
1055 	/*
1056 	 * Fill in mbuf with extended UDP header
1057 	 * and addresses and length put into network format.
1058 	 */
1059 	ui = mtod(m, struct udpiphdr *);
1060 	bzero(ui->ui_x1, sizeof ui->ui_x1);
1061 	ui->ui_pr = IPPROTO_UDP;
1062 	ui->ui_len = htons((u_int16_t)len + sizeof (struct udphdr));
1063 	ui->ui_src = laddr;
1064 	ui->ui_dst = sin ? sin->sin_addr : inp->inp_faddr;
1065 	ui->ui_sport = inp->inp_lport;
1066 	ui->ui_dport = sin ? sin->sin_port : inp->inp_fport;
1067 	ui->ui_ulen = ui->ui_len;
1068 	((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len);
1069 	((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl;
1070 	((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos;
1071 	if (atomic_load_int(&udpcksum))
1072 		m->m_pkthdr.csum_flags |= M_UDP_CSUM_OUT;
1073 
1074 	udpstat_inc(udps_opackets);
1075 
1076 	/* force routing table */
1077 	m->m_pkthdr.ph_rtableid = inp->inp_rtableid;
1078 
1079 #if NPF > 0
1080 	if (inp->inp_socket->so_state & SS_ISCONNECTED)
1081 		pf_mbuf_link_inpcb(m, inp);
1082 #endif
1083 
1084 	error = ip_output(m, inp->inp_options, &inp->inp_route,
1085 	    (inp->inp_socket->so_options & SO_BROADCAST), inp->inp_moptions,
1086 	    &inp->inp_seclevel, ipsecflowinfo);
1087 
1088 bail:
1089 	m_freem(control);
1090 	return (error);
1091 
1092 release:
1093 	m_freem(m);
1094 	goto bail;
1095 }
1096 
1097 int
1098 udp_attach(struct socket *so, int proto, int wait)
1099 {
1100 	struct inpcbtable *table;
1101 	int error;
1102 
1103 	if (so->so_pcb != NULL)
1104 		return EINVAL;
1105 
1106 	if ((error = soreserve(so, atomic_load_int(&udp_sendspace),
1107 	    atomic_load_int(&udp_recvspace))))
1108 		return error;
1109 
1110 	NET_ASSERT_LOCKED();
1111 #ifdef INET6
1112 	if (so->so_proto->pr_domain->dom_family == PF_INET6)
1113 		table = &udb6table;
1114 	else
1115 #endif
1116 		table = &udbtable;
1117 	if ((error = in_pcballoc(so, table, wait)))
1118 		return error;
1119 #ifdef INET6
1120 	if (ISSET(sotoinpcb(so)->inp_flags, INP_IPV6))
1121 		sotoinpcb(so)->inp_ipv6.ip6_hlim = ip6_defhlim;
1122 	else
1123 #endif
1124 		sotoinpcb(so)->inp_ip.ip_ttl = ip_defttl;
1125 	return 0;
1126 }
1127 
1128 int
1129 udp_detach(struct socket *so)
1130 {
1131 	struct inpcb *inp;
1132 
1133 	soassertlocked(so);
1134 
1135 	inp = sotoinpcb(so);
1136 	if (inp == NULL)
1137 		return (EINVAL);
1138 
1139 	in_pcbdetach(inp);
1140 	return (0);
1141 }
1142 
1143 int
1144 udp_bind(struct socket *so, struct mbuf *addr, struct proc *p)
1145 {
1146 	struct inpcb *inp = sotoinpcb(so);
1147 
1148 	soassertlocked(so);
1149 	return in_pcbbind(inp, addr, p);
1150 }
1151 
1152 int
1153 udp_connect(struct socket *so, struct mbuf *addr)
1154 {
1155 	struct inpcb *inp = sotoinpcb(so);
1156 	int error;
1157 
1158 	soassertlocked(so);
1159 
1160 #ifdef INET6
1161 	if (ISSET(inp->inp_flags, INP_IPV6)) {
1162 		if (!IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
1163 			return (EISCONN);
1164 	} else
1165 #endif
1166 	{
1167 		if (inp->inp_faddr.s_addr != INADDR_ANY)
1168 			return (EISCONN);
1169 	}
1170 	error = in_pcbconnect(inp, addr);
1171 	if (error)
1172 		return (error);
1173 
1174 	soisconnected(so);
1175 	return (0);
1176 }
1177 
1178 int
1179 udp_disconnect(struct socket *so)
1180 {
1181 	struct inpcb *inp = sotoinpcb(so);
1182 
1183 	soassertlocked(so);
1184 
1185 #ifdef INET6
1186 	if (ISSET(inp->inp_flags, INP_IPV6)) {
1187 		if (IN6_IS_ADDR_UNSPECIFIED(&inp->inp_faddr6))
1188 			return (ENOTCONN);
1189 	} else
1190 #endif
1191 	{
1192 		if (inp->inp_faddr.s_addr == INADDR_ANY)
1193 			return (ENOTCONN);
1194 	}
1195 	in_pcbunset_laddr(inp);
1196 	in_pcbdisconnect(inp);
1197 	so->so_state &= ~SS_ISCONNECTED;		/* XXX */
1198 
1199 	return (0);
1200 }
1201 
1202 int
1203 udp_shutdown(struct socket *so)
1204 {
1205 	soassertlocked(so);
1206 	socantsendmore(so);
1207 	return (0);
1208 }
1209 
1210 int
1211 udp_send(struct socket *so, struct mbuf *m, struct mbuf *addr,
1212     struct mbuf *control)
1213 {
1214 	struct inpcb *inp = sotoinpcb(so);
1215 
1216 	soassertlocked_readonly(so);
1217 
1218 	if (inp == NULL) {
1219 		/* PCB could be destroyed, but socket still spliced. */
1220 		return (EINVAL);
1221 	}
1222 
1223 #ifdef PIPEX
1224 	if (inp->inp_pipex) {
1225 		struct pipex_session *session;
1226 
1227 		if (addr != NULL)
1228 			session =
1229 			    pipex_l2tp_userland_lookup_session(m,
1230 				mtod(addr, struct sockaddr *));
1231 		else
1232 #ifdef INET6
1233 		if (ISSET(inp->inp_flags, INP_IPV6))
1234 			session =
1235 			    pipex_l2tp_userland_lookup_session_ipv6(
1236 				m, inp->inp_faddr6);
1237 		else
1238 #endif
1239 			session =
1240 			    pipex_l2tp_userland_lookup_session_ipv4(
1241 				m, inp->inp_faddr);
1242 		if (session != NULL) {
1243 			m = pipex_l2tp_userland_output(m, session);
1244 			pipex_rele_session(session);
1245 
1246 			if (m == NULL) {
1247 				m_freem(control);
1248 				return (ENOMEM);
1249 			}
1250 		}
1251 	}
1252 #endif
1253 
1254 	return (udp_output(inp, m, addr, control));
1255 }
1256 
1257 /*
1258  * Sysctl for udp variables.
1259  */
1260 int
1261 udp_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp, void *newp,
1262     size_t newlen)
1263 {
1264 	int error;
1265 
1266 	/* All sysctl names at this level are terminal. */
1267 	if (namelen != 1)
1268 		return (ENOTDIR);
1269 
1270 	switch (name[0]) {
1271 	case UDPCTL_BADDYNAMIC:
1272 		NET_LOCK();
1273 		error = sysctl_struct(oldp, oldlenp, newp, newlen,
1274 		    baddynamicports.udp, sizeof(baddynamicports.udp));
1275 		NET_UNLOCK();
1276 		return (error);
1277 
1278 	case UDPCTL_ROOTONLY:
1279 		if (newp && securelevel > 0)
1280 			return (EPERM);
1281 		NET_LOCK();
1282 		error = sysctl_struct(oldp, oldlenp, newp, newlen,
1283 		    rootonlyports.udp, sizeof(rootonlyports.udp));
1284 		NET_UNLOCK();
1285 		return (error);
1286 
1287 	case UDPCTL_STATS:
1288 		if (newp != NULL)
1289 			return (EPERM);
1290 
1291 		return (udp_sysctl_udpstat(oldp, oldlenp, newp));
1292 
1293 	default:
1294 		error = sysctl_bounded_arr(udpctl_vars, nitems(udpctl_vars),
1295 		    name, namelen, oldp, oldlenp, newp, newlen);
1296 		return (error);
1297 	}
1298 	/* NOTREACHED */
1299 }
1300 
1301 int
1302 udp_sysctl_udpstat(void *oldp, size_t *oldlenp, void *newp)
1303 {
1304 	uint64_t counters[udps_ncounters];
1305 	struct udpstat udpstat;
1306 	u_long *words = (u_long *)&udpstat;
1307 	int i;
1308 
1309 	CTASSERT(sizeof(udpstat) == (nitems(counters) * sizeof(u_long)));
1310 	memset(&udpstat, 0, sizeof udpstat);
1311 	counters_read(udpcounters, counters, nitems(counters), NULL);
1312 
1313 	for (i = 0; i < nitems(counters); i++)
1314 		words[i] = (u_long)counters[i];
1315 
1316 	return (sysctl_rdstruct(oldp, oldlenp, newp,
1317 	    &udpstat, sizeof(udpstat)));
1318 }
1319