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