1 /* $NetBSD: udp_usrreq.c,v 1.114 2003/12/04 19:38:24 atatat 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.114 2003/12/04 19:38:24 atatat 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 INET6 99 #include <netinet/ip6.h> 100 #include <netinet/icmp6.h> 101 #include <netinet6/ip6_var.h> 102 #include <netinet6/in6_pcb.h> 103 #include <netinet6/udp6_var.h> 104 #endif 105 106 #ifndef INET6 107 /* always need ip6.h for IP6_EXTHDR_GET */ 108 #include <netinet/ip6.h> 109 #endif 110 111 #include "faith.h" 112 #if defined(NFAITH) && NFAITH > 0 113 #include <net/if_faith.h> 114 #endif 115 116 #include <machine/stdarg.h> 117 118 #ifdef FAST_IPSEC 119 #include <netipsec/ipsec.h> 120 #ifdef INET6 121 #include <netipsec/ipsec6.h> 122 #endif 123 #endif /* FAST_IPSEC*/ 124 125 #ifdef IPSEC 126 #include <netinet6/ipsec.h> 127 #include <netkey/key.h> 128 #endif /*IPSEC*/ 129 130 #ifdef IPKDB 131 #include <ipkdb/ipkdb.h> 132 #endif 133 134 /* 135 * UDP protocol implementation. 136 * Per RFC 768, August, 1980. 137 */ 138 #ifndef COMPAT_42 139 int udpcksum = 1; 140 #else 141 int udpcksum = 0; /* XXX */ 142 #endif 143 144 struct inpcbtable udbtable; 145 struct udpstat udpstat; 146 147 #ifdef INET 148 static void udp4_sendup __P((struct mbuf *, int, struct sockaddr *, 149 struct socket *)); 150 static int udp4_realinput __P((struct sockaddr_in *, struct sockaddr_in *, 151 struct mbuf *, int)); 152 #endif 153 #ifdef INET6 154 static void udp6_sendup __P((struct mbuf *, int, struct sockaddr *, 155 struct socket *)); 156 static int udp6_realinput __P((int, struct sockaddr_in6 *, 157 struct sockaddr_in6 *, struct mbuf *, int)); 158 #endif 159 #ifdef INET 160 static void udp_notify __P((struct inpcb *, int)); 161 #endif 162 163 #ifndef UDBHASHSIZE 164 #define UDBHASHSIZE 128 165 #endif 166 int udbhashsize = UDBHASHSIZE; 167 168 #ifdef MBUFTRACE 169 struct mowner udp_mowner = { "udp" }; 170 struct mowner udp_rx_mowner = { "udp", "rx" }; 171 struct mowner udp_tx_mowner = { "udp", "tx" }; 172 #endif 173 174 #ifdef UDP_CSUM_COUNTERS 175 #include <sys/device.h> 176 177 struct evcnt udp_hwcsum_bad = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 178 NULL, "udp", "hwcsum bad"); 179 struct evcnt udp_hwcsum_ok = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 180 NULL, "udp", "hwcsum ok"); 181 struct evcnt udp_hwcsum_data = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 182 NULL, "udp", "hwcsum data"); 183 struct evcnt udp_swcsum = EVCNT_INITIALIZER(EVCNT_TYPE_MISC, 184 NULL, "udp", "swcsum"); 185 186 #define UDP_CSUM_COUNTER_INCR(ev) (ev)->ev_count++ 187 188 #else 189 190 #define UDP_CSUM_COUNTER_INCR(ev) /* nothing */ 191 192 #endif /* UDP_CSUM_COUNTERS */ 193 194 void 195 udp_init() 196 { 197 198 in_pcbinit(&udbtable, udbhashsize, udbhashsize); 199 200 #ifdef UDP_CSUM_COUNTERS 201 evcnt_attach_static(&udp_hwcsum_bad); 202 evcnt_attach_static(&udp_hwcsum_ok); 203 evcnt_attach_static(&udp_hwcsum_data); 204 evcnt_attach_static(&udp_swcsum); 205 #endif /* UDP_CSUM_COUNTERS */ 206 207 MOWNER_ATTACH(&udp_tx_mowner); 208 MOWNER_ATTACH(&udp_rx_mowner); 209 MOWNER_ATTACH(&udp_mowner); 210 } 211 212 #ifdef INET 213 void 214 #if __STDC__ 215 udp_input(struct mbuf *m, ...) 216 #else 217 udp_input(m, va_alist) 218 struct mbuf *m; 219 va_dcl 220 #endif 221 { 222 va_list ap; 223 struct sockaddr_in src, dst; 224 struct ip *ip; 225 struct udphdr *uh; 226 int iphlen; 227 int len; 228 int n; 229 u_int16_t ip_len; 230 231 va_start(ap, m); 232 iphlen = va_arg(ap, int); 233 (void)va_arg(ap, int); /* ignore value, advance ap */ 234 va_end(ap); 235 236 MCLAIM(m, &udp_rx_mowner); 237 udpstat.udps_ipackets++; 238 239 /* 240 * Get IP and UDP header together in first mbuf. 241 */ 242 ip = mtod(m, struct ip *); 243 IP6_EXTHDR_GET(uh, struct udphdr *, m, iphlen, sizeof(struct udphdr)); 244 if (uh == NULL) { 245 udpstat.udps_hdrops++; 246 return; 247 } 248 KASSERT(UDP_HDR_ALIGNED_P(uh)); 249 250 /* destination port of 0 is illegal, based on RFC768. */ 251 if (uh->uh_dport == 0) 252 goto bad; 253 254 /* 255 * Make mbuf data length reflect UDP length. 256 * If not enough data to reflect UDP length, drop. 257 */ 258 ip_len = ntohs(ip->ip_len); 259 len = ntohs((u_int16_t)uh->uh_ulen); 260 if (ip_len != iphlen + len) { 261 if (ip_len < iphlen + len || len < sizeof(struct udphdr)) { 262 udpstat.udps_badlen++; 263 goto bad; 264 } 265 m_adj(m, iphlen + len - ip_len); 266 } 267 268 /* 269 * Checksum extended UDP header and data. 270 */ 271 if (uh->uh_sum) { 272 switch (m->m_pkthdr.csum_flags & 273 ((m->m_pkthdr.rcvif->if_csum_flags_rx & M_CSUM_UDPv4) | 274 M_CSUM_TCP_UDP_BAD | M_CSUM_DATA)) { 275 case M_CSUM_UDPv4|M_CSUM_TCP_UDP_BAD: 276 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_bad); 277 goto badcsum; 278 279 case M_CSUM_UDPv4|M_CSUM_DATA: { 280 u_int32_t hw_csum = m->m_pkthdr.csum_data; 281 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_data); 282 if (m->m_pkthdr.csum_flags & M_CSUM_NO_PSEUDOHDR) 283 hw_csum = in_cksum_phdr(ip->ip_src.s_addr, 284 ip->ip_dst.s_addr, 285 htonl(hw_csum + ntohs(ip->ip_len) + 286 IPPROTO_UDP)); 287 if ((hw_csum ^ 0xffff) != 0) 288 goto badcsum; 289 break; 290 } 291 292 case M_CSUM_UDPv4: 293 /* Checksum was okay. */ 294 UDP_CSUM_COUNTER_INCR(&udp_hwcsum_ok); 295 break; 296 297 default: 298 /* Need to compute it ourselves. */ 299 UDP_CSUM_COUNTER_INCR(&udp_swcsum); 300 if (in4_cksum(m, IPPROTO_UDP, iphlen, len) != 0) 301 goto badcsum; 302 break; 303 } 304 } 305 306 /* construct source and dst sockaddrs. */ 307 bzero(&src, sizeof(src)); 308 src.sin_family = AF_INET; 309 src.sin_len = sizeof(struct sockaddr_in); 310 bcopy(&ip->ip_src, &src.sin_addr, sizeof(src.sin_addr)); 311 src.sin_port = uh->uh_sport; 312 bzero(&dst, sizeof(dst)); 313 dst.sin_family = AF_INET; 314 dst.sin_len = sizeof(struct sockaddr_in); 315 bcopy(&ip->ip_dst, &dst.sin_addr, sizeof(dst.sin_addr)); 316 dst.sin_port = uh->uh_dport; 317 318 n = udp4_realinput(&src, &dst, m, iphlen); 319 #ifdef INET6 320 if (IN_MULTICAST(ip->ip_dst.s_addr) || n == 0) { 321 struct sockaddr_in6 src6, dst6; 322 323 bzero(&src6, sizeof(src6)); 324 src6.sin6_family = AF_INET6; 325 src6.sin6_len = sizeof(struct sockaddr_in6); 326 src6.sin6_addr.s6_addr[10] = src6.sin6_addr.s6_addr[11] = 0xff; 327 bcopy(&ip->ip_src, &src6.sin6_addr.s6_addr[12], 328 sizeof(ip->ip_src)); 329 src6.sin6_port = uh->uh_sport; 330 bzero(&dst6, sizeof(dst6)); 331 dst6.sin6_family = AF_INET6; 332 dst6.sin6_len = sizeof(struct sockaddr_in6); 333 dst6.sin6_addr.s6_addr[10] = dst6.sin6_addr.s6_addr[11] = 0xff; 334 bcopy(&ip->ip_dst, &dst6.sin6_addr.s6_addr[12], 335 sizeof(ip->ip_dst)); 336 dst6.sin6_port = uh->uh_dport; 337 338 n += udp6_realinput(AF_INET, &src6, &dst6, m, iphlen); 339 } 340 #endif 341 342 if (n == 0) { 343 if (m->m_flags & (M_BCAST | M_MCAST)) { 344 udpstat.udps_noportbcast++; 345 goto bad; 346 } 347 udpstat.udps_noport++; 348 #ifdef IPKDB 349 if (checkipkdb(&ip->ip_src, uh->uh_sport, uh->uh_dport, 350 m, iphlen + sizeof(struct udphdr), 351 m->m_pkthdr.len - iphlen - sizeof(struct udphdr))) { 352 /* 353 * It was a debugger connect packet, 354 * just drop it now 355 */ 356 goto bad; 357 } 358 #endif 359 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 360 m = NULL; 361 } 362 363 bad: 364 if (m) 365 m_freem(m); 366 return; 367 368 badcsum: 369 m_freem(m); 370 udpstat.udps_badsum++; 371 } 372 #endif 373 374 #ifdef INET6 375 int 376 udp6_input(mp, offp, proto) 377 struct mbuf **mp; 378 int *offp, proto; 379 { 380 struct mbuf *m = *mp; 381 int off = *offp; 382 struct sockaddr_in6 src, dst; 383 struct ip6_hdr *ip6; 384 struct udphdr *uh; 385 u_int32_t plen, ulen; 386 387 ip6 = mtod(m, struct ip6_hdr *); 388 389 #if defined(NFAITH) && 0 < NFAITH 390 if (faithprefix(&ip6->ip6_dst)) { 391 /* send icmp6 host unreach? */ 392 m_freem(m); 393 return IPPROTO_DONE; 394 } 395 #endif 396 397 udp6stat.udp6s_ipackets++; 398 399 /* check for jumbogram is done in ip6_input. we can trust pkthdr.len */ 400 plen = m->m_pkthdr.len - off; 401 IP6_EXTHDR_GET(uh, struct udphdr *, m, off, sizeof(struct udphdr)); 402 if (uh == NULL) { 403 ip6stat.ip6s_tooshort++; 404 return IPPROTO_DONE; 405 } 406 KASSERT(UDP_HDR_ALIGNED_P(uh)); 407 ulen = ntohs((u_short)uh->uh_ulen); 408 /* 409 * RFC2675 section 4: jumbograms will have 0 in the UDP header field, 410 * iff payload length > 0xffff. 411 */ 412 if (ulen == 0 && plen > 0xffff) 413 ulen = plen; 414 415 if (plen != ulen) { 416 udp6stat.udp6s_badlen++; 417 goto bad; 418 } 419 420 /* destination port of 0 is illegal, based on RFC768. */ 421 if (uh->uh_dport == 0) 422 goto bad; 423 424 /* Be proactive about malicious use of IPv4 mapped address */ 425 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) || 426 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) { 427 /* XXX stat */ 428 goto bad; 429 } 430 431 /* 432 * Checksum extended UDP header and data. 433 */ 434 if (uh->uh_sum == 0) 435 udp6stat.udp6s_nosum++; 436 else if (in6_cksum(m, IPPROTO_UDP, off, ulen) != 0) { 437 udp6stat.udp6s_badsum++; 438 goto bad; 439 } 440 441 /* 442 * Construct source and dst sockaddrs. 443 * Note that ifindex (s6_addr16[1]) is already filled. 444 */ 445 bzero(&src, sizeof(src)); 446 src.sin6_family = AF_INET6; 447 src.sin6_len = sizeof(struct sockaddr_in6); 448 /* KAME hack: recover scopeid */ 449 (void)in6_recoverscope(&src, &ip6->ip6_src, m->m_pkthdr.rcvif); 450 src.sin6_port = uh->uh_sport; 451 bzero(&dst, sizeof(dst)); 452 dst.sin6_family = AF_INET6; 453 dst.sin6_len = sizeof(struct sockaddr_in6); 454 /* KAME hack: recover scopeid */ 455 (void)in6_recoverscope(&dst, &ip6->ip6_dst, m->m_pkthdr.rcvif); 456 dst.sin6_port = uh->uh_dport; 457 458 if (udp6_realinput(AF_INET6, &src, &dst, m, off) == 0) { 459 if (m->m_flags & M_MCAST) { 460 udp6stat.udp6s_noportmcast++; 461 goto bad; 462 } 463 udp6stat.udp6s_noport++; 464 icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); 465 m = NULL; 466 } 467 468 bad: 469 if (m) 470 m_freem(m); 471 return IPPROTO_DONE; 472 } 473 #endif 474 475 #ifdef INET 476 static void 477 udp4_sendup(m, off, src, so) 478 struct mbuf *m; 479 int off; /* offset of data portion */ 480 struct sockaddr *src; 481 struct socket *so; 482 { 483 struct mbuf *opts = NULL; 484 struct mbuf *n; 485 struct inpcb *inp = NULL; 486 487 if (!so) 488 return; 489 switch (so->so_proto->pr_domain->dom_family) { 490 case AF_INET: 491 inp = sotoinpcb(so); 492 break; 493 #ifdef INET6 494 case AF_INET6: 495 break; 496 #endif 497 default: 498 return; 499 } 500 501 #if defined(IPSEC) || defined(FAST_IPSEC) 502 /* check AH/ESP integrity. */ 503 if (so != NULL && ipsec4_in_reject_so(m, so)) { 504 ipsecstat.in_polvio++; 505 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 506 icmp_error(n, ICMP_UNREACH, ICMP_UNREACH_ADMIN_PROHIBIT, 507 0, 0); 508 return; 509 } 510 #endif /*IPSEC*/ 511 512 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 513 if (inp && (inp->inp_flags & INP_CONTROLOPTS 514 || so->so_options & SO_TIMESTAMP)) { 515 struct ip *ip = mtod(n, struct ip *); 516 ip_savecontrol(inp, &opts, ip, n); 517 } 518 519 m_adj(n, off); 520 if (sbappendaddr(&so->so_rcv, src, n, 521 opts) == 0) { 522 m_freem(n); 523 if (opts) 524 m_freem(opts); 525 udpstat.udps_fullsock++; 526 } else 527 sorwakeup(so); 528 } 529 } 530 #endif 531 532 #ifdef INET6 533 static void 534 udp6_sendup(m, off, src, so) 535 struct mbuf *m; 536 int off; /* offset of data portion */ 537 struct sockaddr *src; 538 struct socket *so; 539 { 540 struct mbuf *opts = NULL; 541 struct mbuf *n; 542 struct in6pcb *in6p = NULL; 543 544 if (!so) 545 return; 546 if (so->so_proto->pr_domain->dom_family != AF_INET6) 547 return; 548 in6p = sotoin6pcb(so); 549 550 #if defined(IPSEC) || defined(FAST_IPSEC) 551 /* check AH/ESP integrity. */ 552 if (so != NULL && ipsec6_in_reject_so(m, so)) { 553 ipsec6stat.in_polvio++; 554 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 555 icmp6_error(n, ICMP6_DST_UNREACH, 556 ICMP6_DST_UNREACH_ADMIN, 0); 557 return; 558 } 559 #endif /*IPSEC*/ 560 561 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 562 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS 563 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) { 564 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *); 565 ip6_savecontrol(in6p, &opts, ip6, n); 566 } 567 568 m_adj(n, off); 569 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) { 570 m_freem(n); 571 if (opts) 572 m_freem(opts); 573 udp6stat.udp6s_fullsock++; 574 } else 575 sorwakeup(so); 576 } 577 } 578 #endif 579 580 #ifdef INET 581 static int 582 udp4_realinput(src, dst, m, off) 583 struct sockaddr_in *src; 584 struct sockaddr_in *dst; 585 struct mbuf *m; 586 int off; /* offset of udphdr */ 587 { 588 u_int16_t *sport, *dport; 589 int rcvcnt; 590 struct in_addr *src4, *dst4; 591 struct inpcb_hdr *inph; 592 struct inpcb *inp; 593 594 rcvcnt = 0; 595 off += sizeof(struct udphdr); /* now, offset of payload */ 596 597 if (src->sin_family != AF_INET || dst->sin_family != AF_INET) 598 goto bad; 599 600 src4 = &src->sin_addr; 601 sport = &src->sin_port; 602 dst4 = &dst->sin_addr; 603 dport = &dst->sin_port; 604 605 if (IN_MULTICAST(dst4->s_addr) || 606 in_broadcast(*dst4, m->m_pkthdr.rcvif)) { 607 /* 608 * Deliver a multicast or broadcast datagram to *all* sockets 609 * for which the local and remote addresses and ports match 610 * those of the incoming datagram. This allows more than 611 * one process to receive multi/broadcasts on the same port. 612 * (This really ought to be done for unicast datagrams as 613 * well, but that would cause problems with existing 614 * applications that open both address-specific sockets and 615 * a wildcard socket listening to the same port -- they would 616 * end up receiving duplicates of every unicast datagram. 617 * Those applications open the multiple sockets to overcome an 618 * inadequacy of the UDP socket interface, but for backwards 619 * compatibility we avoid the problem here rather than 620 * fixing the interface. Maybe 4.5BSD will remedy this?) 621 */ 622 623 /* 624 * KAME note: traditionally we dropped udpiphdr from mbuf here. 625 * we need udpiphdr for IPsec processing so we do that later. 626 */ 627 /* 628 * Locate pcb(s) for datagram. 629 */ 630 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 631 inp = (struct inpcb *)inph; 632 if (inp->inp_af != AF_INET) 633 continue; 634 635 if (inp->inp_lport != *dport) 636 continue; 637 if (!in_nullhost(inp->inp_laddr)) { 638 if (!in_hosteq(inp->inp_laddr, *dst4)) 639 continue; 640 } 641 if (!in_nullhost(inp->inp_faddr)) { 642 if (!in_hosteq(inp->inp_faddr, *src4) || 643 inp->inp_fport != *sport) 644 continue; 645 } 646 647 udp4_sendup(m, off, (struct sockaddr *)src, 648 inp->inp_socket); 649 rcvcnt++; 650 651 /* 652 * Don't look for additional matches if this one does 653 * not have either the SO_REUSEPORT or SO_REUSEADDR 654 * socket options set. This heuristic avoids searching 655 * through all pcbs in the common case of a non-shared 656 * port. It assumes that an application will never 657 * clear these options after setting them. 658 */ 659 if ((inp->inp_socket->so_options & 660 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 661 break; 662 } 663 } else { 664 /* 665 * Locate pcb for datagram. 666 */ 667 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport); 668 if (inp == 0) { 669 ++udpstat.udps_pcbhashmiss; 670 inp = in_pcblookup_bind(&udbtable, *dst4, *dport); 671 if (inp == 0) 672 return rcvcnt; 673 } 674 675 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); 676 rcvcnt++; 677 } 678 679 bad: 680 return rcvcnt; 681 } 682 #endif 683 684 #ifdef INET6 685 static int 686 udp6_realinput(af, src, dst, m, off) 687 int af; /* af on packet */ 688 struct sockaddr_in6 *src; 689 struct sockaddr_in6 *dst; 690 struct mbuf *m; 691 int off; /* offset of udphdr */ 692 { 693 u_int16_t sport, dport; 694 int rcvcnt; 695 struct in6_addr src6, dst6; 696 const struct in_addr *dst4; 697 struct inpcb_hdr *inph; 698 struct in6pcb *in6p; 699 700 rcvcnt = 0; 701 off += sizeof(struct udphdr); /* now, offset of payload */ 702 703 if (af != AF_INET && af != AF_INET6) 704 goto bad; 705 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6) 706 goto bad; 707 708 in6_embedscope(&src6, src, NULL, NULL); 709 sport = src->sin6_port; 710 in6_embedscope(&dst6, dst, NULL, NULL); 711 dport = dst->sin6_port; 712 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12]; 713 714 if (IN6_IS_ADDR_MULTICAST(&dst6) || 715 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) { 716 /* 717 * Deliver a multicast or broadcast datagram to *all* sockets 718 * for which the local and remote addresses and ports match 719 * those of the incoming datagram. This allows more than 720 * one process to receive multi/broadcasts on the same port. 721 * (This really ought to be done for unicast datagrams as 722 * well, but that would cause problems with existing 723 * applications that open both address-specific sockets and 724 * a wildcard socket listening to the same port -- they would 725 * end up receiving duplicates of every unicast datagram. 726 * Those applications open the multiple sockets to overcome an 727 * inadequacy of the UDP socket interface, but for backwards 728 * compatibility we avoid the problem here rather than 729 * fixing the interface. Maybe 4.5BSD will remedy this?) 730 */ 731 732 /* 733 * KAME note: traditionally we dropped udpiphdr from mbuf here. 734 * we need udpiphdr for IPsec processing so we do that later. 735 */ 736 /* 737 * Locate pcb(s) for datagram. 738 */ 739 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 740 in6p = (struct in6pcb *)inph; 741 if (in6p->in6p_af != AF_INET6) 742 continue; 743 744 if (in6p->in6p_lport != dport) 745 continue; 746 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 747 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6)) 748 continue; 749 } else { 750 if (IN6_IS_ADDR_V4MAPPED(&dst6) && 751 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 752 continue; 753 } 754 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 755 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 756 &src6) || in6p->in6p_fport != sport) 757 continue; 758 } else { 759 if (IN6_IS_ADDR_V4MAPPED(&src6) && 760 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 761 continue; 762 } 763 764 udp6_sendup(m, off, (struct sockaddr *)src, 765 in6p->in6p_socket); 766 rcvcnt++; 767 768 /* 769 * Don't look for additional matches if this one does 770 * not have either the SO_REUSEPORT or SO_REUSEADDR 771 * socket options set. This heuristic avoids searching 772 * through all pcbs in the common case of a non-shared 773 * port. It assumes that an application will never 774 * clear these options after setting them. 775 */ 776 if ((in6p->in6p_socket->so_options & 777 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 778 break; 779 } 780 } else { 781 /* 782 * Locate pcb for datagram. 783 */ 784 in6p = in6_pcblookup_connect(&udbtable, &src6, sport, 785 &dst6, dport, 0); 786 if (in6p == 0) { 787 ++udpstat.udps_pcbhashmiss; 788 in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0); 789 if (in6p == 0) 790 return rcvcnt; 791 } 792 793 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket); 794 rcvcnt++; 795 } 796 797 bad: 798 return rcvcnt; 799 } 800 #endif 801 802 #ifdef INET 803 /* 804 * Notify a udp user of an asynchronous error; 805 * just wake up so that he can collect error status. 806 */ 807 static void 808 udp_notify(inp, errno) 809 struct inpcb *inp; 810 int errno; 811 { 812 813 inp->inp_socket->so_error = errno; 814 sorwakeup(inp->inp_socket); 815 sowwakeup(inp->inp_socket); 816 } 817 818 void * 819 udp_ctlinput(cmd, sa, v) 820 int cmd; 821 struct sockaddr *sa; 822 void *v; 823 { 824 struct ip *ip = v; 825 struct udphdr *uh; 826 void (*notify) __P((struct inpcb *, int)) = udp_notify; 827 int errno; 828 829 if (sa->sa_family != AF_INET 830 || sa->sa_len != sizeof(struct sockaddr_in)) 831 return NULL; 832 if ((unsigned)cmd >= PRC_NCMDS) 833 return NULL; 834 errno = inetctlerrmap[cmd]; 835 if (PRC_IS_REDIRECT(cmd)) 836 notify = in_rtchange, ip = 0; 837 else if (cmd == PRC_HOSTDEAD) 838 ip = 0; 839 else if (errno == 0) 840 return NULL; 841 if (ip) { 842 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 843 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport, 844 ip->ip_src, uh->uh_sport, errno, notify); 845 846 /* XXX mapped address case */ 847 } else 848 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno, 849 notify); 850 return NULL; 851 } 852 853 int 854 #if __STDC__ 855 udp_output(struct mbuf *m, ...) 856 #else 857 udp_output(m, va_alist) 858 struct mbuf *m; 859 va_dcl 860 #endif 861 { 862 struct inpcb *inp; 863 struct udpiphdr *ui; 864 int len = m->m_pkthdr.len; 865 int error = 0; 866 va_list ap; 867 868 MCLAIM(m, &udp_tx_mowner); 869 va_start(ap, m); 870 inp = va_arg(ap, struct inpcb *); 871 va_end(ap); 872 873 /* 874 * Calculate data length and get a mbuf 875 * for UDP and IP headers. 876 */ 877 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 878 if (m == 0) { 879 error = ENOBUFS; 880 goto release; 881 } 882 883 /* 884 * Compute the packet length of the IP header, and 885 * punt if the length looks bogus. 886 */ 887 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 888 error = EMSGSIZE; 889 goto release; 890 } 891 892 /* 893 * Fill in mbuf with extended UDP header 894 * and addresses and length put into network format. 895 */ 896 ui = mtod(m, struct udpiphdr *); 897 ui->ui_pr = IPPROTO_UDP; 898 ui->ui_src = inp->inp_laddr; 899 ui->ui_dst = inp->inp_faddr; 900 ui->ui_sport = inp->inp_lport; 901 ui->ui_dport = inp->inp_fport; 902 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr)); 903 904 /* 905 * Set up checksum and output datagram. 906 */ 907 if (udpcksum) { 908 /* 909 * XXX Cache pseudo-header checksum part for 910 * XXX "connected" UDP sockets. 911 */ 912 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr, 913 ui->ui_dst.s_addr, htons((u_int16_t)len + 914 sizeof(struct udphdr) + IPPROTO_UDP)); 915 m->m_pkthdr.csum_flags = M_CSUM_UDPv4; 916 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 917 } else 918 ui->ui_sum = 0; 919 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len); 920 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 921 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 922 udpstat.udps_opackets++; 923 924 return (ip_output(m, inp->inp_options, &inp->inp_route, 925 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 926 inp->inp_moptions, inp->inp_socket)); 927 928 release: 929 m_freem(m); 930 return (error); 931 } 932 933 int udp_sendspace = 9216; /* really max datagram size */ 934 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 935 /* 40 1K datagrams */ 936 937 /*ARGSUSED*/ 938 int 939 udp_usrreq(so, req, m, nam, control, p) 940 struct socket *so; 941 int req; 942 struct mbuf *m, *nam, *control; 943 struct proc *p; 944 { 945 struct inpcb *inp; 946 int s; 947 int error = 0; 948 949 if (req == PRU_CONTROL) 950 return (in_control(so, (long)m, (caddr_t)nam, 951 (struct ifnet *)control, p)); 952 953 if (req == PRU_PURGEIF) { 954 in_pcbpurgeif0(&udbtable, (struct ifnet *)control); 955 in_purgeif((struct ifnet *)control); 956 in_pcbpurgeif(&udbtable, (struct ifnet *)control); 957 return (0); 958 } 959 960 s = splsoftnet(); 961 inp = sotoinpcb(so); 962 #ifdef DIAGNOSTIC 963 if (req != PRU_SEND && req != PRU_SENDOOB && control) 964 panic("udp_usrreq: unexpected control mbuf"); 965 #endif 966 if (inp == 0 && req != PRU_ATTACH) { 967 error = EINVAL; 968 goto release; 969 } 970 971 /* 972 * Note: need to block udp_input while changing 973 * the udp pcb queue and/or pcb addresses. 974 */ 975 switch (req) { 976 977 case PRU_ATTACH: 978 if (inp != 0) { 979 error = EISCONN; 980 break; 981 } 982 #ifdef MBUFTRACE 983 so->so_mowner = &udp_mowner; 984 so->so_rcv.sb_mowner = &udp_rx_mowner; 985 so->so_snd.sb_mowner = &udp_tx_mowner; 986 #endif 987 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 988 error = soreserve(so, udp_sendspace, udp_recvspace); 989 if (error) 990 break; 991 } 992 error = in_pcballoc(so, &udbtable); 993 if (error) 994 break; 995 inp = sotoinpcb(so); 996 inp->inp_ip.ip_ttl = ip_defttl; 997 break; 998 999 case PRU_DETACH: 1000 in_pcbdetach(inp); 1001 break; 1002 1003 case PRU_BIND: 1004 error = in_pcbbind(inp, nam, p); 1005 break; 1006 1007 case PRU_LISTEN: 1008 error = EOPNOTSUPP; 1009 break; 1010 1011 case PRU_CONNECT: 1012 error = in_pcbconnect(inp, nam); 1013 if (error) 1014 break; 1015 soisconnected(so); 1016 break; 1017 1018 case PRU_CONNECT2: 1019 error = EOPNOTSUPP; 1020 break; 1021 1022 case PRU_DISCONNECT: 1023 /*soisdisconnected(so);*/ 1024 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1025 in_pcbdisconnect(inp); 1026 inp->inp_laddr = zeroin_addr; /* XXX */ 1027 in_pcbstate(inp, INP_BOUND); /* XXX */ 1028 break; 1029 1030 case PRU_SHUTDOWN: 1031 socantsendmore(so); 1032 break; 1033 1034 case PRU_RCVD: 1035 error = EOPNOTSUPP; 1036 break; 1037 1038 case PRU_SEND: 1039 if (control && control->m_len) { 1040 m_freem(control); 1041 m_freem(m); 1042 error = EINVAL; 1043 break; 1044 } 1045 { 1046 struct in_addr laddr; /* XXX */ 1047 1048 if (nam) { 1049 laddr = inp->inp_laddr; /* XXX */ 1050 if ((so->so_state & SS_ISCONNECTED) != 0) { 1051 error = EISCONN; 1052 goto die; 1053 } 1054 error = in_pcbconnect(inp, nam); 1055 if (error) 1056 goto die; 1057 } else { 1058 if ((so->so_state & SS_ISCONNECTED) == 0) { 1059 error = ENOTCONN; 1060 goto die; 1061 } 1062 } 1063 error = udp_output(m, inp); 1064 m = NULL; 1065 if (nam) { 1066 in_pcbdisconnect(inp); 1067 inp->inp_laddr = laddr; /* XXX */ 1068 in_pcbstate(inp, INP_BOUND); /* XXX */ 1069 } 1070 die: 1071 if (m) 1072 m_freem(m); 1073 } 1074 break; 1075 1076 case PRU_SENSE: 1077 /* 1078 * stat: don't bother with a blocksize. 1079 */ 1080 splx(s); 1081 return (0); 1082 1083 case PRU_RCVOOB: 1084 error = EOPNOTSUPP; 1085 break; 1086 1087 case PRU_SENDOOB: 1088 m_freem(control); 1089 m_freem(m); 1090 error = EOPNOTSUPP; 1091 break; 1092 1093 case PRU_SOCKADDR: 1094 in_setsockaddr(inp, nam); 1095 break; 1096 1097 case PRU_PEERADDR: 1098 in_setpeeraddr(inp, nam); 1099 break; 1100 1101 default: 1102 panic("udp_usrreq"); 1103 } 1104 1105 release: 1106 splx(s); 1107 return (error); 1108 } 1109 1110 /* 1111 * Sysctl for udp variables. 1112 */ 1113 SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup") 1114 { 1115 1116 sysctl_createv(SYSCTL_PERMANENT, 1117 CTLTYPE_NODE, "net", NULL, 1118 NULL, 0, NULL, 0, 1119 CTL_NET, CTL_EOL); 1120 sysctl_createv(SYSCTL_PERMANENT, 1121 CTLTYPE_NODE, "inet", NULL, 1122 NULL, 0, NULL, 0, 1123 CTL_NET, PF_INET, CTL_EOL); 1124 sysctl_createv(SYSCTL_PERMANENT, 1125 CTLTYPE_NODE, "udp", NULL, 1126 NULL, 0, NULL, 0, 1127 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL); 1128 1129 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 1130 CTLTYPE_INT, "checksum", NULL, 1131 NULL, 0, &udpcksum, 0, 1132 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM, 1133 CTL_EOL); 1134 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 1135 CTLTYPE_INT, "sendspace", NULL, 1136 NULL, 0, &udp_sendspace, 0, 1137 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE, 1138 CTL_EOL); 1139 sysctl_createv(SYSCTL_PERMANENT|SYSCTL_READWRITE, 1140 CTLTYPE_INT, "recvspace", NULL, 1141 NULL, 0, &udp_recvspace, 0, 1142 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE, 1143 CTL_EOL); 1144 } 1145 #endif 1146