1 /* $NetBSD: udp_usrreq.c,v 1.124 2004/09/03 18:14:09 darrenr 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.124 2004/09/03 18:14:09 darrenr 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 so->so_rcv.sb_overflowed++; 515 udpstat.udps_fullsock++; 516 } else 517 sorwakeup(so); 518 } 519 } 520 #endif 521 522 #ifdef INET6 523 static void 524 udp6_sendup(struct mbuf *m, int off /* offset of data portion */, 525 struct sockaddr *src, struct socket *so) 526 { 527 struct mbuf *opts = NULL; 528 struct mbuf *n; 529 struct in6pcb *in6p = NULL; 530 531 if (!so) 532 return; 533 if (so->so_proto->pr_domain->dom_family != AF_INET6) 534 return; 535 in6p = sotoin6pcb(so); 536 537 #if defined(IPSEC) || defined(FAST_IPSEC) 538 /* check AH/ESP integrity. */ 539 if (so != NULL && ipsec6_in_reject_so(m, so)) { 540 ipsec6stat.in_polvio++; 541 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) 542 icmp6_error(n, ICMP6_DST_UNREACH, 543 ICMP6_DST_UNREACH_ADMIN, 0); 544 return; 545 } 546 #endif /*IPSEC*/ 547 548 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 549 if (in6p && (in6p->in6p_flags & IN6P_CONTROLOPTS 550 || in6p->in6p_socket->so_options & SO_TIMESTAMP)) { 551 struct ip6_hdr *ip6 = mtod(n, struct ip6_hdr *); 552 ip6_savecontrol(in6p, &opts, ip6, n); 553 } 554 555 m_adj(n, off); 556 if (sbappendaddr(&so->so_rcv, src, n, opts) == 0) { 557 m_freem(n); 558 if (opts) 559 m_freem(opts); 560 so->so_rcv.sb_overflowed++; 561 udp6stat.udp6s_fullsock++; 562 } else 563 sorwakeup(so); 564 } 565 } 566 #endif 567 568 #ifdef INET 569 static int 570 udp4_realinput(struct sockaddr_in *src, struct sockaddr_in *dst, 571 struct mbuf *m, int off /* offset of udphdr */) 572 { 573 u_int16_t *sport, *dport; 574 int rcvcnt; 575 struct in_addr *src4, *dst4; 576 struct inpcb_hdr *inph; 577 struct inpcb *inp; 578 579 rcvcnt = 0; 580 off += sizeof(struct udphdr); /* now, offset of payload */ 581 582 if (src->sin_family != AF_INET || dst->sin_family != AF_INET) 583 goto bad; 584 585 src4 = &src->sin_addr; 586 sport = &src->sin_port; 587 dst4 = &dst->sin_addr; 588 dport = &dst->sin_port; 589 590 if (IN_MULTICAST(dst4->s_addr) || 591 in_broadcast(*dst4, m->m_pkthdr.rcvif)) { 592 /* 593 * Deliver a multicast or broadcast datagram to *all* sockets 594 * for which the local and remote addresses and ports match 595 * those of the incoming datagram. This allows more than 596 * one process to receive multi/broadcasts on the same port. 597 * (This really ought to be done for unicast datagrams as 598 * well, but that would cause problems with existing 599 * applications that open both address-specific sockets and 600 * a wildcard socket listening to the same port -- they would 601 * end up receiving duplicates of every unicast datagram. 602 * Those applications open the multiple sockets to overcome an 603 * inadequacy of the UDP socket interface, but for backwards 604 * compatibility we avoid the problem here rather than 605 * fixing the interface. Maybe 4.5BSD will remedy this?) 606 */ 607 608 /* 609 * KAME note: traditionally we dropped udpiphdr from mbuf here. 610 * we need udpiphdr for IPsec processing so we do that later. 611 */ 612 /* 613 * Locate pcb(s) for datagram. 614 */ 615 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 616 inp = (struct inpcb *)inph; 617 if (inp->inp_af != AF_INET) 618 continue; 619 620 if (inp->inp_lport != *dport) 621 continue; 622 if (!in_nullhost(inp->inp_laddr)) { 623 if (!in_hosteq(inp->inp_laddr, *dst4)) 624 continue; 625 } 626 if (!in_nullhost(inp->inp_faddr)) { 627 if (!in_hosteq(inp->inp_faddr, *src4) || 628 inp->inp_fport != *sport) 629 continue; 630 } 631 632 udp4_sendup(m, off, (struct sockaddr *)src, 633 inp->inp_socket); 634 rcvcnt++; 635 636 /* 637 * Don't look for additional matches if this one does 638 * not have either the SO_REUSEPORT or SO_REUSEADDR 639 * socket options set. This heuristic avoids searching 640 * through all pcbs in the common case of a non-shared 641 * port. It assumes that an application will never 642 * clear these options after setting them. 643 */ 644 if ((inp->inp_socket->so_options & 645 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 646 break; 647 } 648 } else { 649 /* 650 * Locate pcb for datagram. 651 */ 652 inp = in_pcblookup_connect(&udbtable, *src4, *sport, *dst4, *dport); 653 if (inp == 0) { 654 ++udpstat.udps_pcbhashmiss; 655 inp = in_pcblookup_bind(&udbtable, *dst4, *dport); 656 if (inp == 0) 657 return rcvcnt; 658 } 659 660 udp4_sendup(m, off, (struct sockaddr *)src, inp->inp_socket); 661 rcvcnt++; 662 } 663 664 bad: 665 return rcvcnt; 666 } 667 #endif 668 669 #ifdef INET6 670 static int 671 udp6_realinput(int af, struct sockaddr_in6 *src, struct sockaddr_in6 *dst, 672 struct mbuf *m, int off) 673 { 674 u_int16_t sport, dport; 675 int rcvcnt; 676 struct in6_addr src6, dst6; 677 const struct in_addr *dst4; 678 struct inpcb_hdr *inph; 679 struct in6pcb *in6p; 680 681 rcvcnt = 0; 682 off += sizeof(struct udphdr); /* now, offset of payload */ 683 684 if (af != AF_INET && af != AF_INET6) 685 goto bad; 686 if (src->sin6_family != AF_INET6 || dst->sin6_family != AF_INET6) 687 goto bad; 688 689 in6_embedscope(&src6, src, NULL, NULL); 690 sport = src->sin6_port; 691 in6_embedscope(&dst6, dst, NULL, NULL); 692 dport = dst->sin6_port; 693 dst4 = (struct in_addr *)&dst->sin6_addr.s6_addr[12]; 694 695 if (IN6_IS_ADDR_MULTICAST(&dst6) || 696 (af == AF_INET && IN_MULTICAST(dst4->s_addr))) { 697 /* 698 * Deliver a multicast or broadcast datagram to *all* sockets 699 * for which the local and remote addresses and ports match 700 * those of the incoming datagram. This allows more than 701 * one process to receive multi/broadcasts on the same port. 702 * (This really ought to be done for unicast datagrams as 703 * well, but that would cause problems with existing 704 * applications that open both address-specific sockets and 705 * a wildcard socket listening to the same port -- they would 706 * end up receiving duplicates of every unicast datagram. 707 * Those applications open the multiple sockets to overcome an 708 * inadequacy of the UDP socket interface, but for backwards 709 * compatibility we avoid the problem here rather than 710 * fixing the interface. Maybe 4.5BSD will remedy this?) 711 */ 712 713 /* 714 * KAME note: traditionally we dropped udpiphdr from mbuf here. 715 * we need udpiphdr for IPsec processing so we do that later. 716 */ 717 /* 718 * Locate pcb(s) for datagram. 719 */ 720 CIRCLEQ_FOREACH(inph, &udbtable.inpt_queue, inph_queue) { 721 in6p = (struct in6pcb *)inph; 722 if (in6p->in6p_af != AF_INET6) 723 continue; 724 725 if (in6p->in6p_lport != dport) 726 continue; 727 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr)) { 728 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &dst6)) 729 continue; 730 } else { 731 if (IN6_IS_ADDR_V4MAPPED(&dst6) && 732 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 733 continue; 734 } 735 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) { 736 if (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, 737 &src6) || in6p->in6p_fport != sport) 738 continue; 739 } else { 740 if (IN6_IS_ADDR_V4MAPPED(&src6) && 741 (in6p->in6p_flags & IN6P_IPV6_V6ONLY)) 742 continue; 743 } 744 745 udp6_sendup(m, off, (struct sockaddr *)src, 746 in6p->in6p_socket); 747 rcvcnt++; 748 749 /* 750 * Don't look for additional matches if this one does 751 * not have either the SO_REUSEPORT or SO_REUSEADDR 752 * socket options set. This heuristic avoids searching 753 * through all pcbs in the common case of a non-shared 754 * port. It assumes that an application will never 755 * clear these options after setting them. 756 */ 757 if ((in6p->in6p_socket->so_options & 758 (SO_REUSEPORT|SO_REUSEADDR)) == 0) 759 break; 760 } 761 } else { 762 /* 763 * Locate pcb for datagram. 764 */ 765 in6p = in6_pcblookup_connect(&udbtable, &src6, sport, 766 &dst6, dport, 0); 767 if (in6p == 0) { 768 ++udpstat.udps_pcbhashmiss; 769 in6p = in6_pcblookup_bind(&udbtable, &dst6, dport, 0); 770 if (in6p == 0) 771 return rcvcnt; 772 } 773 774 udp6_sendup(m, off, (struct sockaddr *)src, in6p->in6p_socket); 775 rcvcnt++; 776 } 777 778 bad: 779 return rcvcnt; 780 } 781 #endif 782 783 #ifdef INET 784 /* 785 * Notify a udp user of an asynchronous error; 786 * just wake up so that he can collect error status. 787 */ 788 static void 789 udp_notify(struct inpcb *inp, int errno) 790 { 791 inp->inp_socket->so_error = errno; 792 sorwakeup(inp->inp_socket); 793 sowwakeup(inp->inp_socket); 794 } 795 796 void * 797 udp_ctlinput(int cmd, struct sockaddr *sa, void *v) 798 { 799 struct ip *ip = v; 800 struct udphdr *uh; 801 void (*notify)(struct inpcb *, int) = udp_notify; 802 int errno; 803 804 if (sa->sa_family != AF_INET 805 || sa->sa_len != sizeof(struct sockaddr_in)) 806 return NULL; 807 if ((unsigned)cmd >= PRC_NCMDS) 808 return NULL; 809 errno = inetctlerrmap[cmd]; 810 if (PRC_IS_REDIRECT(cmd)) 811 notify = in_rtchange, ip = 0; 812 else if (cmd == PRC_HOSTDEAD) 813 ip = 0; 814 else if (errno == 0) 815 return NULL; 816 if (ip) { 817 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 818 in_pcbnotify(&udbtable, satosin(sa)->sin_addr, uh->uh_dport, 819 ip->ip_src, uh->uh_sport, errno, notify); 820 821 /* XXX mapped address case */ 822 } else 823 in_pcbnotifyall(&udbtable, satosin(sa)->sin_addr, errno, 824 notify); 825 return NULL; 826 } 827 828 int 829 udp_output(struct mbuf *m, ...) 830 { 831 struct inpcb *inp; 832 struct udpiphdr *ui; 833 int len = m->m_pkthdr.len; 834 int error = 0; 835 va_list ap; 836 837 MCLAIM(m, &udp_tx_mowner); 838 va_start(ap, m); 839 inp = va_arg(ap, struct inpcb *); 840 va_end(ap); 841 842 /* 843 * Calculate data length and get a mbuf 844 * for UDP and IP headers. 845 */ 846 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 847 if (m == 0) { 848 error = ENOBUFS; 849 goto release; 850 } 851 852 /* 853 * Compute the packet length of the IP header, and 854 * punt if the length looks bogus. 855 */ 856 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 857 error = EMSGSIZE; 858 goto release; 859 } 860 861 /* 862 * Fill in mbuf with extended UDP header 863 * and addresses and length put into network format. 864 */ 865 ui = mtod(m, struct udpiphdr *); 866 ui->ui_pr = IPPROTO_UDP; 867 ui->ui_src = inp->inp_laddr; 868 ui->ui_dst = inp->inp_faddr; 869 ui->ui_sport = inp->inp_lport; 870 ui->ui_dport = inp->inp_fport; 871 ui->ui_ulen = htons((u_int16_t)len + sizeof(struct udphdr)); 872 873 /* 874 * Set up checksum and output datagram. 875 */ 876 if (udpcksum) { 877 /* 878 * XXX Cache pseudo-header checksum part for 879 * XXX "connected" UDP sockets. 880 */ 881 ui->ui_sum = in_cksum_phdr(ui->ui_src.s_addr, 882 ui->ui_dst.s_addr, htons((u_int16_t)len + 883 sizeof(struct udphdr) + IPPROTO_UDP)); 884 m->m_pkthdr.csum_flags = M_CSUM_UDPv4; 885 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 886 } else 887 ui->ui_sum = 0; 888 ((struct ip *)ui)->ip_len = htons(sizeof (struct udpiphdr) + len); 889 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 890 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 891 udpstat.udps_opackets++; 892 893 return (ip_output(m, inp->inp_options, &inp->inp_route, 894 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 895 inp->inp_moptions, inp->inp_socket)); 896 897 release: 898 m_freem(m); 899 return (error); 900 } 901 902 int udp_sendspace = 9216; /* really max datagram size */ 903 int udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 904 /* 40 1K datagrams */ 905 906 /*ARGSUSED*/ 907 int 908 udp_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam, 909 struct mbuf *control, struct proc *p) 910 { 911 struct inpcb *inp; 912 int s; 913 int error = 0; 914 915 if (req == PRU_CONTROL) 916 return (in_control(so, (long)m, (caddr_t)nam, 917 (struct ifnet *)control, p)); 918 919 if (req == PRU_PURGEIF) { 920 in_pcbpurgeif0(&udbtable, (struct ifnet *)control); 921 in_purgeif((struct ifnet *)control); 922 in_pcbpurgeif(&udbtable, (struct ifnet *)control); 923 return (0); 924 } 925 926 s = splsoftnet(); 927 inp = sotoinpcb(so); 928 #ifdef DIAGNOSTIC 929 if (req != PRU_SEND && req != PRU_SENDOOB && control) 930 panic("udp_usrreq: unexpected control mbuf"); 931 #endif 932 if (inp == 0 && req != PRU_ATTACH) { 933 error = EINVAL; 934 goto release; 935 } 936 937 /* 938 * Note: need to block udp_input while changing 939 * the udp pcb queue and/or pcb addresses. 940 */ 941 switch (req) { 942 943 case PRU_ATTACH: 944 if (inp != 0) { 945 error = EISCONN; 946 break; 947 } 948 #ifdef MBUFTRACE 949 so->so_mowner = &udp_mowner; 950 so->so_rcv.sb_mowner = &udp_rx_mowner; 951 so->so_snd.sb_mowner = &udp_tx_mowner; 952 #endif 953 if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { 954 error = soreserve(so, udp_sendspace, udp_recvspace); 955 if (error) 956 break; 957 } 958 error = in_pcballoc(so, &udbtable); 959 if (error) 960 break; 961 inp = sotoinpcb(so); 962 inp->inp_ip.ip_ttl = ip_defttl; 963 break; 964 965 case PRU_DETACH: 966 in_pcbdetach(inp); 967 break; 968 969 case PRU_BIND: 970 error = in_pcbbind(inp, nam, p); 971 break; 972 973 case PRU_LISTEN: 974 error = EOPNOTSUPP; 975 break; 976 977 case PRU_CONNECT: 978 error = in_pcbconnect(inp, nam); 979 if (error) 980 break; 981 soisconnected(so); 982 break; 983 984 case PRU_CONNECT2: 985 error = EOPNOTSUPP; 986 break; 987 988 case PRU_DISCONNECT: 989 /*soisdisconnected(so);*/ 990 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 991 in_pcbdisconnect(inp); 992 inp->inp_laddr = zeroin_addr; /* XXX */ 993 in_pcbstate(inp, INP_BOUND); /* XXX */ 994 break; 995 996 case PRU_SHUTDOWN: 997 socantsendmore(so); 998 break; 999 1000 case PRU_RCVD: 1001 error = EOPNOTSUPP; 1002 break; 1003 1004 case PRU_SEND: 1005 if (control && control->m_len) { 1006 m_freem(control); 1007 m_freem(m); 1008 error = EINVAL; 1009 break; 1010 } 1011 { 1012 struct in_addr laddr; /* XXX */ 1013 1014 if (nam) { 1015 laddr = inp->inp_laddr; /* XXX */ 1016 if ((so->so_state & SS_ISCONNECTED) != 0) { 1017 error = EISCONN; 1018 goto die; 1019 } 1020 error = in_pcbconnect(inp, nam); 1021 if (error) 1022 goto die; 1023 } else { 1024 if ((so->so_state & SS_ISCONNECTED) == 0) { 1025 error = ENOTCONN; 1026 goto die; 1027 } 1028 } 1029 error = udp_output(m, inp); 1030 m = NULL; 1031 if (nam) { 1032 in_pcbdisconnect(inp); 1033 inp->inp_laddr = laddr; /* XXX */ 1034 in_pcbstate(inp, INP_BOUND); /* XXX */ 1035 } 1036 die: 1037 if (m) 1038 m_freem(m); 1039 } 1040 break; 1041 1042 case PRU_SENSE: 1043 /* 1044 * stat: don't bother with a blocksize. 1045 */ 1046 splx(s); 1047 return (0); 1048 1049 case PRU_RCVOOB: 1050 error = EOPNOTSUPP; 1051 break; 1052 1053 case PRU_SENDOOB: 1054 m_freem(control); 1055 m_freem(m); 1056 error = EOPNOTSUPP; 1057 break; 1058 1059 case PRU_SOCKADDR: 1060 in_setsockaddr(inp, nam); 1061 break; 1062 1063 case PRU_PEERADDR: 1064 in_setpeeraddr(inp, nam); 1065 break; 1066 1067 default: 1068 panic("udp_usrreq"); 1069 } 1070 1071 release: 1072 splx(s); 1073 return (error); 1074 } 1075 1076 /* 1077 * Sysctl for udp variables. 1078 */ 1079 SYSCTL_SETUP(sysctl_net_inet_udp_setup, "sysctl net.inet.udp subtree setup") 1080 { 1081 1082 sysctl_createv(clog, 0, NULL, NULL, 1083 CTLFLAG_PERMANENT, 1084 CTLTYPE_NODE, "net", NULL, 1085 NULL, 0, NULL, 0, 1086 CTL_NET, CTL_EOL); 1087 sysctl_createv(clog, 0, NULL, NULL, 1088 CTLFLAG_PERMANENT, 1089 CTLTYPE_NODE, "inet", NULL, 1090 NULL, 0, NULL, 0, 1091 CTL_NET, PF_INET, CTL_EOL); 1092 sysctl_createv(clog, 0, NULL, NULL, 1093 CTLFLAG_PERMANENT, 1094 CTLTYPE_NODE, "udp", 1095 SYSCTL_DESCR("UDPv4 related settings"), 1096 NULL, 0, NULL, 0, 1097 CTL_NET, PF_INET, IPPROTO_UDP, CTL_EOL); 1098 1099 sysctl_createv(clog, 0, NULL, NULL, 1100 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1101 CTLTYPE_INT, "checksum", 1102 SYSCTL_DESCR("Compute UDP checksums"), 1103 NULL, 0, &udpcksum, 0, 1104 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_CHECKSUM, 1105 CTL_EOL); 1106 sysctl_createv(clog, 0, NULL, NULL, 1107 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1108 CTLTYPE_INT, "sendspace", 1109 SYSCTL_DESCR("Default UDP send buffer size"), 1110 NULL, 0, &udp_sendspace, 0, 1111 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_SENDSPACE, 1112 CTL_EOL); 1113 sysctl_createv(clog, 0, NULL, NULL, 1114 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, 1115 CTLTYPE_INT, "recvspace", 1116 SYSCTL_DESCR("Default UDP receive buffer size"), 1117 NULL, 0, &udp_recvspace, 0, 1118 CTL_NET, PF_INET, IPPROTO_UDP, UDPCTL_RECVSPACE, 1119 CTL_EOL); 1120 } 1121 #endif 1122