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