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