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