1 /* $NetBSD: udp_usrreq.c,v 1.182 2011/07/17 20:54:53 joerg Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 * 60 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 61 */ 62 63 #include <sys/cdefs.h> 64 __KERNEL_RCSID(0, "$NetBSD: udp_usrreq.c,v 1.182 2011/07/17 20:54:53 joerg Exp $"); 65 66 #include "opt_inet.h" 67 #include "opt_compat_netbsd.h" 68 #include "opt_ipsec.h" 69 #include "opt_inet_csum.h" 70 #include "opt_ipkdb.h" 71 #include "opt_mbuftrace.h" 72 73 #include <sys/param.h> 74 #include <sys/malloc.h> 75 #include <sys/mbuf.h> 76 #include <sys/protosw.h> 77 #include <sys/socket.h> 78 #include <sys/socketvar.h> 79 #include <sys/errno.h> 80 #include <sys/stat.h> 81 #include <sys/systm.h> 82 #include <sys/proc.h> 83 #include <sys/domain.h> 84 #include <sys/sysctl.h> 85 86 #include <net/if.h> 87 #include <net/route.h> 88 89 #include <netinet/in.h> 90 #include <netinet/in_systm.h> 91 #include <netinet/in_var.h> 92 #include <netinet/ip.h> 93 #include <netinet/in_pcb.h> 94 #include <netinet/ip_var.h> 95 #include <netinet/ip_icmp.h> 96 #include <netinet/udp.h> 97 #include <netinet/udp_var.h> 98 #include <netinet/udp_private.h> 99 100 #ifdef INET6 101 #include <netinet/ip6.h> 102 #include <netinet/icmp6.h> 103 #include <netinet6/ip6_var.h> 104 #include <netinet6/ip6_private.h> 105 #include <netinet6/in6_pcb.h> 106 #include <netinet6/udp6_var.h> 107 #include <netinet6/udp6_private.h> 108 #include <netinet6/scope6_var.h> 109 #endif 110 111 #ifndef INET6 112 /* always need ip6.h for IP6_EXTHDR_GET */ 113 #include <netinet/ip6.h> 114 #endif 115 116 #include "faith.h" 117 #if defined(NFAITH) && NFAITH > 0 118 #include <net/if_faith.h> 119 #endif 120 121 #ifdef FAST_IPSEC 122 #include <netipsec/ipsec.h> 123 #include <netipsec/ipsec_var.h> 124 #include <netipsec/ipsec_private.h> 125 #include <netipsec/esp.h> 126 #ifdef INET6 127 #include <netipsec/ipsec6.h> 128 #endif 129 #endif /* FAST_IPSEC */ 130 131 #ifdef IPSEC 132 #include <netinet6/ipsec.h> 133 #include <netinet6/ipsec_private.h> 134 #include <netinet6/esp.h> 135 #include <netkey/key.h> 136 #endif /* IPSEC */ 137 138 #ifdef COMPAT_50 139 #include <compat/sys/socket.h> 140 #endif 141 142 #ifdef IPKDB 143 #include <ipkdb/ipkdb.h> 144 #endif 145 146 /* 147 * UDP protocol implementation. 148 * Per RFC 768, August, 1980. 149 */ 150 int udpcksum = 1; 151 int udp_do_loopback_cksum = 0; 152 153 struct inpcbtable udbtable; 154 155 percpu_t *udpstat_percpu; 156 157 #ifdef INET 158 #ifdef IPSEC_NAT_T 159 static int udp4_espinudp (struct mbuf **, int, struct sockaddr *, 160 struct socket *); 161 #endif 162 static void udp4_sendup (struct mbuf *, int, struct sockaddr *, 163 struct socket *); 164 static int udp4_realinput (struct sockaddr_in *, struct sockaddr_in *, 165 struct mbuf **, int); 166 static int udp4_input_checksum(struct mbuf *, const struct udphdr *, int, int); 167 #endif 168 #ifdef INET6 169 static void udp6_sendup (struct mbuf *, int, struct sockaddr *, 170 struct socket *); 171 static int udp6_realinput (int, struct sockaddr_in6 *, 172 struct sockaddr_in6 *, struct mbuf *, int); 173 static int udp6_input_checksum(struct mbuf *, const struct udphdr *, int, int); 174 #endif 175 #ifdef INET 176 static void udp_notify (struct inpcb *, int); 177 #endif 178 179 #ifndef UDBHASHSIZE 180 #define UDBHASHSIZE 128 181 #endif 182 int udbhashsize = UDBHASHSIZE; 183 184 #ifdef MBUFTRACE 185 struct mowner udp_mowner = MOWNER_INIT("udp", ""); 186 struct mowner udp_rx_mowner = MOWNER_INIT("udp", "rx"); 187 struct mowner udp_tx_mowner = MOWNER_INIT("udp", "tx"); 188 #endif 189 190 #ifdef UDP_CSUM_COUNTERS 191 #include <sys/device.h> 192 193 #if defined(INET) 194 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 195 NULL, "udp", "hwcsum bad"); 196 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 197 NULL, "udp", "hwcsum ok"); 198 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 199 NULL, "udp", "hwcsum data"); 200 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 201 NULL, "udp", "swcsum"); 202 203 EVCNT_ATTACH_STATIC(udp_hwcsum_bad); 204 EVCNT_ATTACH_STATIC(udp_hwcsum_ok); 205 EVCNT_ATTACH_STATIC(udp_hwcsum_data); 206 EVCNT_ATTACH_STATIC(udp_swcsum); 207 #endif /* defined(INET) */ 208 209 #if defined(INET6) 210 struct evcnt udp6_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 211 NULL, "udp6", "hwcsum bad"); 212 struct evcnt udp6_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 213 NULL, "udp6", "hwcsum ok"); 214 struct evcnt udp6_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 215 NULL, "udp6", "hwcsum data"); 216 struct evcnt udp6_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 217 NULL, "udp6", "swcsum"); 218 219 EVCNT_ATTACH_STATIC(udp6_hwcsum_bad); 220 EVCNT_ATTACH_STATIC(udp6_hwcsum_ok); 221 EVCNT_ATTACH_STATIC(udp6_hwcsum_data); 222 EVCNT_ATTACH_STATIC(udp6_swcsum); 223 #endif /* defined(INET6) */ 224 225 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 226 227 #else 228 229 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */ 230 231 #endif /* UDP_CSUM_COUNTERS */ 232 233 static void sysctl_net_inet_udp_setup(struct sysctllog **); 234 235 void 236 udp_init(void) 237 { 238 239 sysctl_net_inet_udp_setup(NULL); 240 241 in_pcbinit(&udbtable, udbhashsize, udbhashsize); 242 243 MOWNER_ATTACH(&udp_tx_mowner); 244 MOWNER_ATTACH(&udp_rx_mowner); 245 MOWNER_ATTACH(&udp_mowner); 246 247 #ifdef INET 248 udpstat_percpu = percpu_alloc(sizeof(uint64_t) * UDP_NSTATS); 249 #endif 250 #ifdef INET6 251 udp6stat_percpu = percpu_alloc(sizeof(uint64_t) * UDP6_NSTATS); 252 #endif 253 } 254 255 /* 256 * Checksum extended UDP header and data. 257 */ 258 259 int 260 udp_input_checksum(int af, struct mbuf *m, const struct udphdr *uh, 261 int iphlen, int len) 262 { 263 264 switch (af) { 265 #ifdef INET 266 case AF_INET: 267 return udp4_input_checksum(m, uh, iphlen, len); 268 #endif 269 #ifdef INET6 270 case AF_INET6: 271 return udp6_input_checksum(m, uh, iphlen, len); 272 #endif 273 } 274 #ifdef DIAGNOSTIC 275 panic("udp_input_checksum: unknown af %d", af); 276 #endif 277 /* NOTREACHED */ 278 return -1; 279 } 280 281 #ifdef INET 282 283 /* 284 * Checksum extended UDP header and data. 285 */ 286 287 static int 288 udp4_input_checksum(struct mbuf *m, const struct udphdr *uh, 289 int iphlen, int len) 290 { 291 292 /* 293 * XXX it's better to record and check if this mbuf is 294 * already checked. 295 */ 296 297 if (uh->uh_sum == 0) 298 return 0; 299 300 switch (m->m_pkthdr.csum_flags & 301 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) | 302 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 303 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD: 304 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad); 305 goto badcsum; 306 307 case M_CSUM_UDPv4|M_CSUM_DATA: { 308 u_int32_t hw_csum = m->m_pkthdr.csum_data; 309 310 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data); 311 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) { 312 const struct ip *ip = 313 mtod(m, const struct ip *); 314 315 hw_csum = in_cksum_phdr(ip->ip_src.s_addr, 316 ip->ip_dst.s_addr, 317 htons(hw_csum + len + IPPROTO_UDP)); 318 } 319 if ((hw_csum ^ 0xffff) != 0) 320 goto badcsum; 321 break; 322 } 323 324 case M_CSUM_UDPv4: 325 /* Checksum was okay. */ 326 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok); 327 break; 328 329 default: 330 /* 331 * Need to compute it ourselves. Maybe skip checksum 332 * on loopback interfaces. 333 */ 334 if (__predict_true(!(m->m_pkthdr.rcvif->if_flags & 335 IFF_LOOPBACK) || 336 udp_do_loopback_cksum)) { 337 UDP_CSUM_COUNTER_INCR(&udp_swcsum); 338 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) 339 goto badcsum; 340 } 341 break; 342 } 343 344 return 0; 345 346 badcsum: 347 UDP_STATINC(UDP_STAT_BADSUM); 348 return -1; 349 } 350 351 void 352 udp_input(struct mbuf *m, ...) 353 { 354 va_list ap; 355 struct sockaddr_in src, dst; 356 struct ip *ip; 357 struct udphdr *uh; 358 int iphlen; 359 int len; 360 int n; 361 u_int16_t ip_len; 362 363 va_start(ap, m); 364 iphlen = va_arg(ap, int); 365 (void)va_arg(ap, int); /* ignore value, advance ap */ 366 va_end(ap); 367 368 MCLAIM(m, &udp_rx_mowner); 369 UDP_STATINC(UDP_STAT_IPACKETS); 370 371 /* 372 * Get IP and UDP header together in first mbuf. 373 */ 374 ip = mtod(m, struct ip *); 375 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr)); 376 if (uh == NULL) { 377 UDP_STATINC(UDP_STAT_HDROPS); 378 return; 379 } 380 KASSERT(UDP_HDR_ALIGNED_P(uh)); 381 382 /* destination port of 0 is illegal, based on RFC768. */ 383 if (uh->uh_dport == 0) 384 goto bad; 385 386 /* 387 * Make mbuf data length reflect UDP length. 388 * If not enough data to reflect UDP length, drop. 389 */ 390 ip_len = ntohs(ip->ip_len); 391 len = ntohs((u_int16_t)uh->uh_ulen); 392 if (ip_len != iphlen + len) { 393 if (ip_len < iphlen + len || len < sizeof(struct udphdr)) { 394 UDP_STATINC(UDP_STAT_BADLEN); 395 goto bad; 396 } 397 m_adj(m, iphlen + len - ip_len); 398 } 399 400 /* 401 * Checksum extended UDP header and data. 402 */ 403 if (udp4_input_checksum(m, uh, iphlen, len)) 404 goto badcsum; 405 406 /* construct source and dst sockaddrs. */ 407 sockaddr_in_init(&src, &ip->ip_src, uh->uh_sport); 408 sockaddr_in_init(&dst, &ip->ip_dst, uh->uh_dport); 409 410 if ((n = udp4_realinput(&src, &dst, &m, iphlen)) == -1) { 411 UDP_STATINC(UDP_STAT_HDROPS); 412 return; 413 } 414 #ifdef INET6 415 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) { 416 struct sockaddr_in6 src6, dst6; 417 418 memset(&src6, 0, sizeof(src6)); 419 src6.sin6_family = AF_INET6; 420 src6.sin6_len = sizeof(struct sockaddr_in6); 421 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff; 422 memcpy(&src6.sin6_addr.s6_addr[12], &ip->ip_src, 423 sizeof(ip->ip_src)); 424 src6.sin6_port = uh->uh_sport; 425 memset(&dst6, 0, sizeof(dst6)); 426 dst6.sin6_family = AF_INET6; 427 dst6.sin6_len = sizeof(struct sockaddr_in6); 428 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff; 429 memcpy(&dst6.sin6_addr.s6_addr[12], &ip->ip_dst, 430 sizeof(ip->ip_dst)); 431 dst6.sin6_port = uh->uh_dport; 432 433 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen); 434 } 435 #endif 436 437 if (n == 0) { 438 if (m->m_flags & (M_BCAST | M_MCAST)) { 439 UDP_STATINC(UDP_STAT_NOPORTBCAST); 440 goto bad; 441 } 442 UDP_STATINC(UDP_STAT_NOPORT); 443 #ifdef IPKDB 444 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport, 445 m, iphlen + sizeof(struct udphdr), 446 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) { 447 /* 448 * It was a debugger connect packet, 449 * just drop it now 450 */ 451 goto bad; 452 } 453 #endif 454 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 455 m = NULL; 456 } 457 458 bad: 459 if (m) 460 m_freem(m); 461 return; 462 463 badcsum: 464 m_freem(m); 465 } 466 #endif 467 468 #ifdef INET6 469 static int 470 udp6_input_checksum(struct mbuf *m, const struct udphdr *uh, int off, int len) 471 { 472 473 /* 474 * XXX it's better to record and check if this mbuf is 475 * already checked. 476 */ 477 478 if (__predict_false((m->m_flags & M_LOOP) && !udp_do_loopback_cksum)) { 479 goto good; 480 } 481 if (uh->uh_sum == 0) { 482 UDP6_STATINC(UDP6_STAT_NOSUM); 483 goto bad; 484 } 485 486 switch (m->m_pkthdr.csum_flags & 487 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv6) | 488 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 489 case M_CSUM_UDPv6|M_CSUM_TCP_UDP_BAD: 490 UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_bad); 491 UDP6_STATINC(UDP6_STAT_BADSUM); 492 goto bad; 493 494 #if 0 /* notyet */ 495 case M_CSUM_UDPv6|M_CSUM_DATA: 496 #endif 497 498 case M_CSUM_UDPv6: 499 /* Checksum was okay. */ 500 UDP_CSUM_COUNTER_INCR(&udp6_hwcsum_ok); 501 break; 502 503 default: 504 /* 505 * Need to compute it ourselves. Maybe skip checksum 506 * on loopback interfaces. 507 */ 508 UDP_CSUM_COUNTER_INCR(&udp6_swcsum); 509 if (in6_cksum(m, IPPROTO_UDP, off, len) != 0) { 510 UDP6_STATINC(UDP6_STAT_BADSUM); 511 goto bad; 512 } 513 } 514 515 good: 516 return 0; 517 bad: 518 return -1; 519 } 520 521 int 522 udp6_input(struct mbuf **mp, int *offp, int proto) 523 { 524 struct mbuf *m = *mp; 525 int off = *offp; 526 struct sockaddr_in6 src, dst; 527 struct ip6_hdr *ip6; 528 struct udphdr *uh; 529 u_int32_t plen, ulen; 530 531 ip6 = mtod(m, struct ip6_hdr *); 532 533 #if defined(NFAITH) && 0 < NFAITH 534 if (faithprefix(&ip6->ip6_dst)) { 535 /* send icmp6 host unreach? */ 536 m_freem(m); 537 return IPPROTO_DONE; 538 } 539 #endif 540 541 UDP6_STATINC(UDP6_STAT_IPACKETS); 542 543 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */ 544 plen = m->m_pkthdr.len - off; 545 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr)); 546 if (uh == NULL) { 547 IP6_STATINC(IP6_STAT_TOOSHORT); 548 return IPPROTO_DONE; 549 } 550 KASSERT(UDP_HDR_ALIGNED_P(uh)); 551 ulen = ntohs((u_short)uh->uh_ulen); 552 /* 553 * RFC2675 section 4: jumbograms will have 0 in the UDP header field, 554 * iff payload length > 0xffff. 555 */ 556 if (ulen == 0 && plen > 0xffff) 557 ulen = plen; 558 559 if (plen != ulen) { 560 UDP6_STATINC(UDP6_STAT_BADLEN); 561 goto bad; 562 } 563 564 /* destination port of 0 is illegal, based on RFC768. */ 565 if (uh->uh_dport == 0) 566 goto bad; 567 568 /* Be proactive about malicious use of IPv4 mapped address */ 569 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 570 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 571 /* XXX stat */ 572 goto bad; 573 } 574 575 /* 576 * Checksum extended UDP header and data. Maybe skip checksum 577 * on loopback interfaces. 578 */ 579 if (udp6_input_checksum(m, uh, off, ulen)) 580 goto bad; 581 582 /* 583 * Construct source and dst sockaddrs. 584 */ 585 memset(&src, 0, sizeof(src)); 586 src.sin6_family = AF_INET6; 587 src.sin6_len = sizeof(struct sockaddr_in6); 588 src.sin6_addr = ip6->ip6_src; 589 src.sin6_port = uh->uh_sport; 590 memset(&dst, 0, sizeof(dst)); 591 dst.sin6_family = AF_INET6; 592 dst.sin6_len = sizeof(struct sockaddr_in6); 593 dst.sin6_addr = ip6->ip6_dst; 594 dst.sin6_port = uh->uh_dport; 595 596 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) { 597 if (m->m_flags & M_MCAST) { 598 UDP6_STATINC(UDP6_STAT_NOPORTMCAST); 599 goto bad; 600 } 601 UDP6_STATINC(UDP6_STAT_NOPORT); 602 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 603 m = NULL; 604 } 605 606 bad: 607 if (m) 608 m_freem(m); 609 return IPPROTO_DONE; 610 } 611 #endif 612 613 #ifdef INET 614 static void 615 udp4_sendup(struct mbuf *m, int off /* offset of data portion */, 616 struct sockaddr *src, struct socket *so) 617 { 618 struct mbuf *opts = NULL; 619 struct mbuf *n; 620 struct inpcb *inp = NULL; 621 622 if (!so) 623 return; 624 switch (so->so_proto->pr_domain->dom_family) { 625 case AF_INET: 626 inp = sotoinpcb(so); 627 break; 628 #ifdef INET6 629 case AF_INET6: 630 break; 631 #endif 632 default: 633 return; 634 } 635 636 #if defined(IPSEC) || defined(FAST_IPSEC) 637 /* check AH/ESP integrity. */ 638 if (so != NULL && ipsec4_in_reject_so(m, so)) { 639 IPSEC_STATINC(IPSEC_STAT_IN_POLVIO); 640 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) 641 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 642 0, 0); 643 return; 644 } 645 #endif /*IPSEC*/ 646 647 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { 648 if (inp && (inp->inp_flags & INP_CONTROLOPTS 649 #ifdef SO_OTIMESTAMP 650 || so->so_options & SO_OTIMESTAMP 651 #endif 652 || so->so_options & SO_TIMESTAMP)) { 653 struct ip *ip = mtod(n, struct ip *); 654 ip_savecontrol(inp, &opts, ip, n); 655 } 656 657 m_adj(n, off); 658 if (sbappendaddr(&so->so_rcv, src, n, 659 opts) == 0) { 660 m_freem(n); 661 if (opts) 662 m_freem(opts); 663 so->so_rcv.sb_overflowed++; 664 UDP_STATINC(UDP_STAT_FULLSOCK); 665 } else 666 sorwakeup(so); 667 } 668 } 669 #endif 670 671 #ifdef INET6 672 static void 673 udp6_sendup(struct mbuf *m, int off /* offset of data portion */, 674 struct sockaddr *src, struct socket *so) 675 { 676 struct mbuf *opts = NULL; 677 struct mbuf *n; 678 struct in6pcb *in6p = NULL; 679 680 if (!so) 681 return; 682 if (so->so_proto->pr_domain->dom_family != AF_INET6) 683 return; 684 in6p = sotoin6pcb(so); 685 686 #if defined(IPSEC) || defined(FAST_IPSEC) 687 /* check AH/ESP integrity. */ 688 if (so != NULL && ipsec6_in_reject_so(m, so)) { 689 IPSEC6_STATINC(IPSEC_STAT_IN_POLVIO); 690 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) 691 icmp6_error(n, ICMP6_DST_UNREACH, 692 ICMP6_DST_UNREACH_ADMIN, 0); 693 return; 694 } 695 #endif /*IPSEC*/ 696 697 if ((n = m_copypacket(m, M_DONTWAIT)) != NULL) { 698 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS 699 #ifdef SO_OTIMESTAMP 700 || in6p->in6p_socket->so_options & SO_OTIMESTAMP 701 #endif 702 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) { 703 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *); 704 ip6_savecontrol(in6p, &opts, ip6, n); 705 } 706 707 m_adj(n, off); 708 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) { 709 m_freem(n); 710 if (opts) 711 m_freem(opts); 712 so->so_rcv.sb_overflowed++; 713 UDP6_STATINC(UDP6_STAT_FULLSOCK); 714 } else 715 sorwakeup(so); 716 } 717 } 718 #endif 719 720 #ifdef INET 721 static int 722 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst, 723 struct mbuf **mp, int off /* offset of udphdr */) 724 { 725 u_int16_t *sport, *dport; 726 int rcvcnt; 727 struct in_addr *src4, *dst4; 728 struct inpcb_hdr *inph; 729 struct inpcb *inp; 730 struct mbuf *m = *mp; 731 732 rcvcnt = 0; 733 off += sizeof(struct udphdr); /* now, offset of payload */ 734 735 if (src->sin_family != AF_INET || dst->sin_family != AF_INET) 736 goto bad; 737 738 src4 = &src->sin_addr; 739 sport = &src->sin_port; 740 dst4 = &dst->sin_addr; 741 dport = &dst->sin_port; 742 743 if (IN_MULTICAST(dst4->s_addr) || 744 in_broadcast(*dst4, m->m_pkthdr.rcvif)) { 745 /* 746 * Deliver a multicast or broadcast datagram to *all* sockets 747 * for which the local and remote addresses and ports match 748 * those of the incoming datagram. This allows more than 749 * one process to receive multi/broadcasts on the same port. 750 * (This really ought to be done for unicast datagrams as 751 * well, but that would cause problems with existing 752 * applications that open both address-specific sockets and 753 * a wildcard socket listening to the same port -- they would 754 * end up receiving duplicates of every unicast datagram. 755 * Those applications open the multiple sockets to overcome an 756 * inadequacy of the UDP socket interface, but for backwards 757 * compatibility we avoid the problem here rather than 758 * fixing the interface. Maybe 4.5BSD will remedy this?) 759 */ 760 761 /* 762 * KAME note: traditionally we dropped udpiphdr from mbuf here. 763 * we need udpiphdr for IPsec processing so we do that later. 764 */ 765 /* 766 * Locate pcb(s) for datagram. 767 */ 768 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 769 inp = (struct inpcb *)inph; 770 if (inp->inp_af != AF_INET) 771 continue; 772 773 if (inp->inp_lport != *dport) 774 continue; 775 if (!in_nullhost(inp->inp_laddr)) { 776 if (!in_hosteq(inp->inp_laddr, *dst4)) 777 continue; 778 } 779 if (!in_nullhost(inp->inp_faddr)) { 780 if (!in_hosteq(inp->inp_faddr, *src4) || 781 inp->inp_fport != *sport) 782 continue; 783 } 784 785 udp4_sendup(m, off, (struct sockaddr *)src, 786 inp->inp_socket); 787 rcvcnt++; 788 789 /* 790 * Don't look for additional matches if this one does 791 * not have either the SO_REUSEPORT or SO_REUSEADDR 792 * socket options set. This heuristic avoids searching 793 * through all pcbs in the common case of a non-shared 794 * port. It assumes that an application will never 795 * clear these options after setting them. 796 */ 797 if ((inp->inp_socket->so_options & 798 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 799 break; 800 } 801 } else { 802 /* 803 * Locate pcb for datagram. 804 */ 805 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, 806 *dport, 0); 807 if (inp == 0) { 808 UDP_STATINC(UDP_STAT_PCBHASHMISS); 809 inp = in_pcblookup_bind(&udbtable, *dst4, *dport); 810 if (inp == 0) 811 return rcvcnt; 812 } 813 814 #ifdef IPSEC_NAT_T 815 /* Handle ESP over UDP */ 816 if (inp->inp_flags & INP_ESPINUDP_ALL) { 817 struct sockaddr *sa = (struct sockaddr *)src; 818 819 switch(udp4_espinudp(mp, off, sa, inp->inp_socket)) { 820 case -1: /* Error, m was freeed */ 821 rcvcnt = -1; 822 goto bad; 823 break; 824 825 case 1: /* ESP over UDP */ 826 rcvcnt++; 827 goto bad; 828 break; 829 830 case 0: /* plain UDP */ 831 default: /* Unexpected */ 832 /* 833 * Normal UDP processing will take place 834 * m may have changed. 835 */ 836 m = *mp; 837 break; 838 } 839 } 840 #endif 841 842 /* 843 * Check the minimum TTL for socket. 844 */ 845 if (mtod(m, struct ip *)->ip_ttl < inp->inp_ip_minttl) 846 goto bad; 847 848 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); 849 rcvcnt++; 850 } 851 852 bad: 853 return rcvcnt; 854 } 855 #endif 856 857 #ifdef INET6 858 static int 859 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst, 860 struct mbuf *m, int off) 861 { 862 u_int16_t sport, dport; 863 int rcvcnt; 864 struct in6_addr src6, *dst6; 865 const struct in_addr *dst4; 866 struct inpcb_hdr *inph; 867 struct in6pcb *in6p; 868 869 rcvcnt = 0; 870 off += sizeof(struct udphdr); /* now, offset of payload */ 871 872 if (af != AF_INET && af != AF_INET6) 873 goto bad; 874 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6) 875 goto bad; 876 877 src6 = src->sin6_addr; 878 if (sa6_recoverscope(src) != 0) { 879 /* XXX: should be impossible. */ 880 goto bad; 881 } 882 sport = src->sin6_port; 883 884 dport = dst->sin6_port; 885 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12]; 886 dst6 = &dst->sin6_addr; 887 888 if (IN6_IS_ADDR_MULTICAST(dst6) || 889 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) { 890 /* 891 * Deliver a multicast or broadcast datagram to *all* sockets 892 * for which the local and remote addresses and ports match 893 * those of the incoming datagram. This allows more than 894 * one process to receive multi/broadcasts on the same port. 895 * (This really ought to be done for unicast datagrams as 896 * well, but that would cause problems with existing 897 * applications that open both address-specific sockets and 898 * a wildcard socket listening to the same port -- they would 899 * end up receiving duplicates of every unicast datagram. 900 * Those applications open the multiple sockets to overcome an 901 * inadequacy of the UDP socket interface, but for backwards 902 * compatibility we avoid the problem here rather than 903 * fixing the interface. Maybe 4.5BSD will remedy this?) 904 */ 905 906 /* 907 * KAME note: traditionally we dropped udpiphdr from mbuf here. 908 * we need udpiphdr for IPsec processing so we do that later. 909 */ 910 /* 911 * Locate pcb(s) for datagram. 912 */ 913 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 914 in6p = (struct in6pcb *)inph; 915 if (in6p->in6p_af != AF_INET6) 916 continue; 917 918 if (in6p->in6p_lport != dport) 919 continue; 920 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 921 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, 922 dst6)) 923 continue; 924 } else { 925 if (IN6_IS_ADDR_V4MAPPED(dst6) && 926 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 927 continue; 928 } 929 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 930 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 931 &src6) || in6p->in6p_fport != sport) 932 continue; 933 } else { 934 if (IN6_IS_ADDR_V4MAPPED(&src6) && 935 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 936 continue; 937 } 938 939 udp6_sendup(m, off, (struct sockaddr *)src, 940 in6p->in6p_socket); 941 rcvcnt++; 942 943 /* 944 * Don't look for additional matches if this one does 945 * not have either the SO_REUSEPORT or SO_REUSEADDR 946 * socket options set. This heuristic avoids searching 947 * through all pcbs in the common case of a non-shared 948 * port. It assumes that an application will never 949 * clear these options after setting them. 950 */ 951 if ((in6p->in6p_socket->so_options & 952 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 953 break; 954 } 955 } else { 956 /* 957 * Locate pcb for datagram. 958 */ 959 in6p = in6_pcblookup_connect(&udbtable, &src6, sport, dst6, 960 dport, 0, 0); 961 if (in6p == 0) { 962 UDP_STATINC(UDP_STAT_PCBHASHMISS); 963 in6p = in6_pcblookup_bind(&udbtable, dst6, dport, 0); 964 if (in6p == 0) 965 return rcvcnt; 966 } 967 968 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket); 969 rcvcnt++; 970 } 971 972 bad: 973 return rcvcnt; 974 } 975 #endif 976 977 #ifdef INET 978 /* 979 * Notify a udp user of an asynchronous error; 980 * just wake up so that he can collect error status. 981 */ 982 static void 983 udp_notify(struct inpcb *inp, int errno) 984 { 985 inp->inp_socket->so_error = errno; 986 sorwakeup(inp->inp_socket); 987 sowwakeup(inp->inp_socket); 988 } 989 990 void * 991 udp_ctlinput(int cmd, const struct sockaddr *sa, void *v) 992 { 993 struct ip *ip = v; 994 struct udphdr *uh; 995 void (*notify)(struct inpcb *, int) = udp_notify; 996 int errno; 997 998 if (sa->sa_family != AF_INET 999 || sa->sa_len != sizeof(struct sockaddr_in)) 1000 return NULL; 1001 if ((unsigned)cmd >= PRC_NCMDS) 1002 return NULL; 1003 errno = inetctlerrmap[cmd]; 1004 if (PRC_IS_REDIRECT(cmd)) 1005 notify = in_rtchange, ip = 0; 1006 else if (cmd == PRC_HOSTDEAD) 1007 ip = 0; 1008 else if (errno == 0) 1009 return NULL; 1010 if (ip) { 1011 uh = (struct udphdr *)((char *)ip + (ip->ip_hl << 2)); 1012 in_pcbnotify(&udbtable, satocsin(sa)->sin_addr, uh->uh_dport, 1013 ip->ip_src, uh->uh_sport, errno, notify); 1014 1015 /* XXX mapped address case */ 1016 } else 1017 in_pcbnotifyall(&udbtable, satocsin(sa)->sin_addr, errno, 1018 notify); 1019 return NULL; 1020 } 1021 1022 int 1023 udp_ctloutput(int op, struct socket *so, struct sockopt *sopt) 1024 { 1025 int s; 1026 int error = 0; 1027 struct inpcb *inp; 1028 int family; 1029 int optval; 1030 1031 family = so->so_proto->pr_domain->dom_family; 1032 1033 s = splsoftnet(); 1034 switch (family) { 1035 #ifdef INET 1036 case PF_INET: 1037 if (sopt->sopt_level != IPPROTO_UDP) { 1038 error = ip_ctloutput(op, so, sopt); 1039 goto end; 1040 } 1041 break; 1042 #endif 1043 #ifdef INET6 1044 case PF_INET6: 1045 if (sopt->sopt_level != IPPROTO_UDP) { 1046 error = ip6_ctloutput(op, so, sopt); 1047 goto end; 1048 } 1049 break; 1050 #endif 1051 default: 1052 error = EAFNOSUPPORT; 1053 goto end; 1054 } 1055 1056 1057 switch (op) { 1058 case PRCO_SETOPT: 1059 inp = sotoinpcb(so); 1060 1061 switch (sopt->sopt_name) { 1062 case UDP_ENCAP: 1063 error = sockopt_getint(sopt, &optval); 1064 if (error) 1065 break; 1066 1067 switch(optval) { 1068 #ifdef IPSEC_NAT_T 1069 case 0: 1070 inp->inp_flags &= ~INP_ESPINUDP_ALL; 1071 break; 1072 1073 case UDP_ENCAP_ESPINUDP: 1074 inp->inp_flags &= ~INP_ESPINUDP_ALL; 1075 inp->inp_flags |= INP_ESPINUDP; 1076 break; 1077 1078 case UDP_ENCAP_ESPINUDP_NON_IKE: 1079 inp->inp_flags &= ~INP_ESPINUDP_ALL; 1080 inp->inp_flags |= INP_ESPINUDP_NON_IKE; 1081 break; 1082 #endif 1083 default: 1084 error = EINVAL; 1085 break; 1086 } 1087 break; 1088 1089 default: 1090 error = ENOPROTOOPT; 1091 break; 1092 } 1093 break; 1094 1095 default: 1096 error = EINVAL; 1097 break; 1098 } 1099 1100 end: 1101 splx(s); 1102 return error; 1103 } 1104 1105 1106 int 1107 udp_output(struct mbuf *m, ...) 1108 { 1109 struct inpcb *inp; 1110 struct udpiphdr *ui; 1111 struct route *ro; 1112 int len = m->m_pkthdr.len; 1113 int error = 0; 1114 va_list ap; 1115 1116 MCLAIM(m, &udp_tx_mowner); 1117 va_start(ap, m); 1118 inp = va_arg(ap, struct inpcb *); 1119 va_end(ap); 1120 1121 /* 1122 * Calculate data length and get a mbuf 1123 * for UDP and IP headers. 1124 */ 1125 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 1126 if (m == 0) { 1127 error = ENOBUFS; 1128 goto release; 1129 } 1130 1131 /* 1132 * Compute the packet length of the IP header, and 1133 * punt if the length looks bogus. 1134 */ 1135 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 1136 error = EMSGSIZE; 1137 goto release; 1138 } 1139 1140 /* 1141 * Fill in mbuf with extended UDP header 1142 * and addresses and length put into network format. 1143 */ 1144 ui = mtod(m, struct udpiphdr *); 1145 ui->ui_pr = IPPROTO_UDP; 1146 ui->ui_src = inp->inp_laddr; 1147 ui->ui_dst = inp->inp_faddr; 1148 ui->ui_sport = inp->inp_lport; 1149 ui->ui_dport = inp->inp_fport; 1150 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr)); 1151 1152 ro = &inp->inp_route; 1153 1154 /* 1155 * Set up checksum and output datagram. 1156 */ 1157 if (udpcksum) { 1158 /* 1159 * XXX Cache pseudo-header checksum part for 1160 * XXX "connected" UDP sockets. 1161 */ 1162 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr, 1163 ui->ui_dst.s_addr, htons((u_int16_t)len + 1164 sizeof(struct udphdr) + IPPROTO_UDP)); 1165 m->m_pkthdr.csum_flags = M_CSUM_UDPv4; 1166 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 1167 } else 1168 ui->ui_sum = 0; 1169 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len); 1170 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 1171 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 1172 UDP_STATINC(UDP_STAT_OPACKETS); 1173 1174 return (ip_output(m, inp->inp_options, ro, 1175 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 1176 inp->inp_moptions, inp->inp_socket)); 1177 1178 release: 1179 m_freem(m); 1180 return (error); 1181 } 1182 1183 int udp_sendspace = 9216; /* really max datagram size */ 1184 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 1185 /* 40 1K datagrams */ 1186 1187 /*ARGSUSED*/ 1188 int 1189 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 1190 struct mbuf *control, struct lwp *l) 1191 { 1192 struct inpcb *inp; 1193 int s; 1194 int error = 0; 1195 1196 if (req == PRU_CONTROL) 1197 return (in_control(so, (long)m, (void *)nam, 1198 (struct ifnet *)control, l)); 1199 1200 s = splsoftnet(); 1201 1202 if (req == PRU_PURGEIF) { 1203 mutex_enter(softnet_lock); 1204 in_pcbpurgeif0(&udbtable, (struct ifnet *)control); 1205 in_purgeif((struct ifnet *)control); 1206 in_pcbpurgeif(&udbtable, (struct ifnet *)control); 1207 mutex_exit(softnet_lock); 1208 splx(s); 1209 return (0); 1210 } 1211 1212 inp = sotoinpcb(so); 1213 #ifdef DIAGNOSTIC 1214 if (req != PRU_SEND && req != PRU_SENDOOB && control) 1215 panic("udp_usrreq: unexpected control mbuf"); 1216 #endif 1217 if (req == PRU_ATTACH) { 1218 sosetlock(so); 1219 } else if (inp == 0) { 1220 error = EINVAL; 1221 goto release; 1222 } 1223 1224 /* 1225 * Note: need to block udp_input while changing 1226 * the udp pcb queue and/or pcb addresses. 1227 */ 1228 switch (req) { 1229 1230 case PRU_ATTACH: 1231 if (inp != 0) { 1232 error = EISCONN; 1233 break; 1234 } 1235 #ifdef MBUFTRACE 1236 so->so_mowner = &udp_mowner; 1237 so->so_rcv.sb_mowner = &udp_rx_mowner; 1238 so->so_snd.sb_mowner = &udp_tx_mowner; 1239 #endif 1240 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 1241 error = soreserve(so, udp_sendspace, udp_recvspace); 1242 if (error) 1243 break; 1244 } 1245 error = in_pcballoc(so, &udbtable); 1246 if (error) 1247 break; 1248 inp = sotoinpcb(so); 1249 inp->inp_ip.ip_ttl = ip_defttl; 1250 break; 1251 1252 case PRU_DETACH: 1253 in_pcbdetach(inp); 1254 break; 1255 1256 case PRU_BIND: 1257 error = in_pcbbind(inp, nam, l); 1258 break; 1259 1260 case PRU_LISTEN: 1261 error = EOPNOTSUPP; 1262 break; 1263 1264 case PRU_CONNECT: 1265 error = in_pcbconnect(inp, nam, l); 1266 if (error) 1267 break; 1268 soisconnected(so); 1269 break; 1270 1271 case PRU_CONNECT2: 1272 error = EOPNOTSUPP; 1273 break; 1274 1275 case PRU_DISCONNECT: 1276 /*soisdisconnected(so);*/ 1277 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1278 in_pcbdisconnect(inp); 1279 inp->inp_laddr = zeroin_addr; /* XXX */ 1280 in_pcbstate(inp, INP_BOUND); /* XXX */ 1281 break; 1282 1283 case PRU_SHUTDOWN: 1284 socantsendmore(so); 1285 break; 1286 1287 case PRU_RCVD: 1288 error = EOPNOTSUPP; 1289 break; 1290 1291 case PRU_SEND: 1292 if (control && control->m_len) { 1293 m_freem(control); 1294 m_freem(m); 1295 error = EINVAL; 1296 break; 1297 } 1298 { 1299 struct in_addr laddr; /* XXX */ 1300 1301 memset(&laddr, 0, sizeof laddr); 1302 if (nam) { 1303 laddr = inp->inp_laddr; /* XXX */ 1304 if ((so->so_state & SS_ISCONNECTED) != 0) { 1305 error = EISCONN; 1306 goto die; 1307 } 1308 error = in_pcbconnect(inp, nam, l); 1309 if (error) 1310 goto die; 1311 } else { 1312 if ((so->so_state & SS_ISCONNECTED) == 0) { 1313 error = ENOTCONN; 1314 goto die; 1315 } 1316 } 1317 error = udp_output(m, inp); 1318 m = NULL; 1319 if (nam) { 1320 in_pcbdisconnect(inp); 1321 inp->inp_laddr = laddr; /* XXX */ 1322 in_pcbstate(inp, INP_BOUND); /* XXX */ 1323 } 1324 die: 1325 if (m) 1326 m_freem(m); 1327 } 1328 break; 1329 1330 case PRU_SENSE: 1331 /* 1332 * stat: don't bother with a blocksize. 1333 */ 1334 splx(s); 1335 return (0); 1336 1337 case PRU_RCVOOB: 1338 error = EOPNOTSUPP; 1339 break; 1340 1341 case PRU_SENDOOB: 1342 m_freem(control); 1343 m_freem(m); 1344 error = EOPNOTSUPP; 1345 break; 1346 1347 case PRU_SOCKADDR: 1348 in_setsockaddr(inp, nam); 1349 break; 1350 1351 case PRU_PEERADDR: 1352 in_setpeeraddr(inp, nam); 1353 break; 1354 1355 default: 1356 panic("udp_usrreq"); 1357 } 1358 1359 release: 1360 splx(s); 1361 return (error); 1362 } 1363 1364 static int 1365 sysctl_net_inet_udp_stats(SYSCTLFN_ARGS) 1366 { 1367 1368 return (NETSTAT_SYSCTL(udpstat_percpu, UDP_NSTATS)); 1369 } 1370 1371 /* 1372 * Sysctl for udp variables. 1373 */ 1374 static void 1375 sysctl_net_inet_udp_setup(struct sysctllog **clog) 1376 { 1377 1378 sysctl_createv(clog, 0, NULL, NULL, 1379 CTLFLAG_PERMANENT, 1380 CTLTYPE_NODE, "net", NULL, 1381 NULL, 0, NULL, 0, 1382 CTL_NET, CTL_EOL); 1383 sysctl_createv(clog, 0, NULL, NULL, 1384 CTLFLAG_PERMANENT, 1385 CTLTYPE_NODE, "inet", NULL, 1386 NULL, 0, NULL, 0, 1387 CTL_NET, PF_INET, CTL_EOL); 1388 sysctl_createv(clog, 0, NULL, NULL, 1389 CTLFLAG_PERMANENT, 1390 CTLTYPE_NODE, "udp", 1391 SYSCTL_DESCR("UDPv4 related settings"), 1392 NULL, 0, NULL, 0, 1393 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL); 1394 1395 sysctl_createv(clog, 0, NULL, NULL, 1396 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1397 CTLTYPE_INT, "checksum", 1398 SYSCTL_DESCR("Compute UDP checksums"), 1399 NULL, 0, &udpcksum, 0, 1400 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM, 1401 CTL_EOL); 1402 sysctl_createv(clog, 0, NULL, NULL, 1403 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1404 CTLTYPE_INT, "sendspace", 1405 SYSCTL_DESCR("Default UDP send buffer size"), 1406 NULL, 0, &udp_sendspace, 0, 1407 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE, 1408 CTL_EOL); 1409 sysctl_createv(clog, 0, NULL, NULL, 1410 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1411 CTLTYPE_INT, "recvspace", 1412 SYSCTL_DESCR("Default UDP receive buffer size"), 1413 NULL, 0, &udp_recvspace, 0, 1414 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE, 1415 CTL_EOL); 1416 sysctl_createv(clog, 0, NULL, NULL, 1417 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1418 CTLTYPE_INT, "do_loopback_cksum", 1419 SYSCTL_DESCR("Perform UDP checksum on loopback"), 1420 NULL, 0, &udp_do_loopback_cksum, 0, 1421 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_LOOPBACKCKSUM, 1422 CTL_EOL); 1423 sysctl_createv(clog, 0, NULL, NULL, 1424 CTLFLAG_PERMANENT, 1425 CTLTYPE_STRUCT, "pcblist", 1426 SYSCTL_DESCR("UDP protocol control block list"), 1427 sysctl_inpcblist, 0, &udbtable, 0, 1428 CTL_NET, PF_INET, IPPROTO_UDP, CTL_CREATE, 1429 CTL_EOL); 1430 sysctl_createv(clog, 0, NULL, NULL, 1431 CTLFLAG_PERMANENT, 1432 CTLTYPE_STRUCT, "stats", 1433 SYSCTL_DESCR("UDP statistics"), 1434 sysctl_net_inet_udp_stats, 0, NULL, 0, 1435 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_STATS, 1436 CTL_EOL); 1437 } 1438 #endif 1439 1440 void 1441 udp_statinc(u_int stat) 1442 { 1443 1444 KASSERT(stat < UDP_NSTATS); 1445 UDP_STATINC(stat); 1446 } 1447 1448 #if (defined INET && defined IPSEC_NAT_T) 1449 /* 1450 * Returns: 1451 * 1 if the packet was processed 1452 * 0 if normal UDP processing should take place 1453 * -1 if an error occurent and m was freed 1454 */ 1455 static int 1456 udp4_espinudp(struct mbuf **mp, int off, struct sockaddr *src, 1457 struct socket *so) 1458 { 1459 size_t len; 1460 void *data; 1461 struct inpcb *inp; 1462 size_t skip = 0; 1463 size_t minlen; 1464 size_t iphdrlen; 1465 struct ip *ip; 1466 struct mbuf *n; 1467 struct m_tag *tag; 1468 struct udphdr *udphdr; 1469 u_int16_t sport, dport; 1470 struct mbuf *m = *mp; 1471 1472 /* 1473 * Collapse the mbuf chain if the first mbuf is too short 1474 * The longest case is: UDP + non ESP marker + ESP 1475 */ 1476 minlen = off + sizeof(u_int64_t) + sizeof(struct esp); 1477 if (minlen > m->m_pkthdr.len) 1478 minlen = m->m_pkthdr.len; 1479 1480 if (m->m_len < minlen) { 1481 if ((*mp = m_pullup(m, minlen)) == NULL) { 1482 printf("udp4_espinudp: m_pullup failed\n"); 1483 return -1; 1484 } 1485 m = *mp; 1486 } 1487 1488 len = m->m_len - off; 1489 data = mtod(m, char *) + off; 1490 inp = sotoinpcb(so); 1491 1492 /* Ignore keepalive packets */ 1493 if ((len == 1) && (*(unsigned char *)data == 0xff)) { 1494 return 1; 1495 } 1496 1497 /* 1498 * Check that the payload is long enough to hold 1499 * an ESP header and compute the length of encapsulation 1500 * header to remove 1501 */ 1502 if (inp->inp_flags & INP_ESPINUDP) { 1503 u_int32_t *st = (u_int32_t *)data; 1504 1505 if ((len <= sizeof(struct esp)) || (*st == 0)) 1506 return 0; /* Normal UDP processing */ 1507 1508 skip = sizeof(struct udphdr); 1509 } 1510 1511 if (inp->inp_flags & INP_ESPINUDP_NON_IKE) { 1512 u_int32_t *st = (u_int32_t *)data; 1513 1514 if ((len <= sizeof(u_int64_t) + sizeof(struct esp)) 1515 || ((st[0] | st[1]) != 0)) 1516 return 0; /* Normal UDP processing */ 1517 1518 skip = sizeof(struct udphdr) + sizeof(u_int64_t); 1519 } 1520 1521 /* 1522 * Get the UDP ports. They are handled in network 1523 * order everywhere in IPSEC_NAT_T code. 1524 */ 1525 udphdr = (struct udphdr *)((char *)data - skip); 1526 sport = udphdr->uh_sport; 1527 dport = udphdr->uh_dport; 1528 1529 /* 1530 * Remove the UDP header (and possibly the non ESP marker) 1531 * IP header lendth is iphdrlen 1532 * Before: 1533 * <--- off ---> 1534 * +----+------+-----+ 1535 * | IP | UDP | ESP | 1536 * +----+------+-----+ 1537 * <-skip-> 1538 * After: 1539 * +----+-----+ 1540 * | IP | ESP | 1541 * +----+-----+ 1542 * <-skip-> 1543 */ 1544 iphdrlen = off - sizeof(struct udphdr); 1545 memmove(mtod(m, char *) + skip, mtod(m, void *), iphdrlen); 1546 m_adj(m, skip); 1547 1548 ip = mtod(m, struct ip *); 1549 ip->ip_len = htons(ntohs(ip->ip_len) - skip); 1550 ip->ip_p = IPPROTO_ESP; 1551 1552 /* 1553 * Copy the mbuf to avoid multiple free, as both 1554 * esp4_input (which we call) and udp_input (which 1555 * called us) free the mbuf. 1556 */ 1557 if ((n = m_dup(m, 0, M_COPYALL, M_DONTWAIT)) == NULL) { 1558 printf("udp4_espinudp: m_dup failed\n"); 1559 return 0; 1560 } 1561 1562 /* 1563 * Add a PACKET_TAG_IPSEC_NAT_T_PORT tag to remember 1564 * the source UDP port. This is required if we want 1565 * to select the right SPD for multiple hosts behind 1566 * same NAT 1567 */ 1568 if ((tag = m_tag_get(PACKET_TAG_IPSEC_NAT_T_PORTS, 1569 sizeof(sport) + sizeof(dport), M_DONTWAIT)) == NULL) { 1570 printf("udp4_espinudp: m_tag_get failed\n"); 1571 m_freem(n); 1572 return 0; 1573 } 1574 ((u_int16_t *)(tag + 1))[0] = sport; 1575 ((u_int16_t *)(tag + 1))[1] = dport; 1576 m_tag_prepend(n, tag); 1577 1578 #ifdef FAST_IPSEC 1579 ipsec4_common_input(n, iphdrlen, IPPROTO_ESP); 1580 #else 1581 esp4_input(n, iphdrlen); 1582 #endif 1583 1584 /* We handled it, it shoudln't be handled by UDP */ 1585 return 1; 1586 } 1587 #endif 1588