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