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