1 /* 2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 2004 The DragonFly Project. All rights reserved. 4 * 5 * This code is derived from software contributed to The DragonFly Project 6 * by Jeffrey M. Hsu. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of The DragonFly Project nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific, prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 /* 35 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved. 36 * 37 * License terms: all terms for the DragonFly license above plus the following: 38 * 39 * 4. All advertising materials mentioning features or use of this software 40 * must display the following acknowledgement: 41 * 42 * This product includes software developed by Jeffrey M. Hsu 43 * for the DragonFly Project. 44 * 45 * This requirement may be waived with permission from Jeffrey Hsu. 46 * This requirement will sunset and may be removed on July 8 2005, 47 * after which the standard DragonFly license (as shown above) will 48 * apply. 49 */ 50 51 /* 52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 53 * The Regents of the University of California. All rights reserved. 54 * 55 * Redistribution and use in source and binary forms, with or without 56 * modification, are permitted provided that the following conditions 57 * are met: 58 * 1. Redistributions of source code must retain the above copyright 59 * notice, this list of conditions and the following disclaimer. 60 * 2. Redistributions in binary form must reproduce the above copyright 61 * notice, this list of conditions and the following disclaimer in the 62 * documentation and/or other materials provided with the distribution. 63 * 3. All advertising materials mentioning features or use of this software 64 * must display the following acknowledgement: 65 * This product includes software developed by the University of 66 * California, Berkeley and its contributors. 67 * 4. Neither the name of the University nor the names of its contributors 68 * may be used to endorse or promote products derived from this software 69 * without specific prior written permission. 70 * 71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 81 * SUCH DAMAGE. 82 * 83 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 84 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $ 85 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.35 2005/06/02 23:52:42 dillon Exp $ 86 */ 87 88 #include "opt_ipsec.h" 89 #include "opt_inet6.h" 90 91 #include <sys/param.h> 92 #include <sys/systm.h> 93 #include <sys/kernel.h> 94 #include <sys/malloc.h> 95 #include <sys/mbuf.h> 96 #include <sys/domain.h> 97 #include <sys/proc.h> 98 #include <sys/protosw.h> 99 #include <sys/socket.h> 100 #include <sys/socketvar.h> 101 #include <sys/sysctl.h> 102 #include <sys/syslog.h> 103 #include <sys/thread2.h> 104 #include <sys/in_cksum.h> 105 106 #include <machine/stdarg.h> 107 108 #include <vm/vm_zone.h> 109 110 #include <net/if.h> 111 #include <net/route.h> 112 113 #include <netinet/in.h> 114 #include <netinet/in_systm.h> 115 #include <netinet/ip.h> 116 #ifdef INET6 117 #include <netinet/ip6.h> 118 #endif 119 #include <netinet/in_pcb.h> 120 #include <netinet/in_var.h> 121 #include <netinet/ip_var.h> 122 #ifdef INET6 123 #include <netinet6/ip6_var.h> 124 #endif 125 #include <netinet/ip_icmp.h> 126 #include <netinet/icmp_var.h> 127 #include <netinet/udp.h> 128 #include <netinet/udp_var.h> 129 130 #ifdef FAST_IPSEC 131 #include <netproto/ipsec/ipsec.h> 132 #endif 133 134 #ifdef IPSEC 135 #include <netinet6/ipsec.h> 136 #endif 137 138 /* 139 * UDP protocol implementation. 140 * Per RFC 768, August, 1980. 141 */ 142 #ifndef COMPAT_42 143 static int udpcksum = 1; 144 #else 145 static int udpcksum = 0; /* XXX */ 146 #endif 147 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 148 &udpcksum, 0, ""); 149 150 int log_in_vain = 0; 151 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 152 &log_in_vain, 0, "Log all incoming UDP packets"); 153 154 static int blackhole = 0; 155 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 156 &blackhole, 0, "Do not send port unreachables for refused connects"); 157 158 static int strict_mcast_mship = 1; 159 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, 160 &strict_mcast_mship, 0, "Only send multicast to member sockets"); 161 162 struct inpcbinfo udbinfo; 163 164 #ifndef UDBHASHSIZE 165 #define UDBHASHSIZE 16 166 #endif 167 168 struct udpstat udpstat; /* from udp_var.h */ 169 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 170 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 171 172 static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET }; 173 #ifdef INET6 174 struct udp_in6 { 175 struct sockaddr_in6 uin6_sin; 176 u_char uin6_init_done : 1; 177 } udp_in6 = { 178 { sizeof udp_in6.uin6_sin, AF_INET6 }, 179 0 180 }; 181 struct udp_ip6 { 182 struct ip6_hdr uip6_ip6; 183 u_char uip6_init_done : 1; 184 } udp_ip6; 185 #endif /* INET6 */ 186 187 static void udp_append (struct inpcb *last, struct ip *ip, 188 struct mbuf *n, int off); 189 #ifdef INET6 190 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip); 191 #endif 192 193 static int udp_detach (struct socket *so); 194 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *, 195 struct mbuf *, struct thread *); 196 197 void 198 udp_init() 199 { 200 in_pcbinfo_init(&udbinfo); 201 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 202 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 203 &udbinfo.porthashmask); 204 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB, 205 &udbinfo.wildcardhashmask); 206 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 207 ZONE_INTERRUPT, 0); 208 udp_thread_init(); 209 } 210 211 /* 212 * Check multicast packets to make sure they are only sent to sockets with 213 * multicast memberships for the packet's destination address and arrival 214 * interface. Multicast packets to multicast-unaware sockets are also 215 * disallowed. 216 * 217 * Returns 0 if the packet is acceptable, -1 if it is not. 218 */ 219 static __inline int 220 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m) 221 { 222 int mshipno; 223 struct ip_moptions *mopt; 224 225 if (strict_mcast_mship == 0 || 226 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 227 return (0); 228 } 229 mopt = inp->inp_moptions; 230 if (mopt == NULL) 231 return (-1); 232 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) { 233 struct in_multi *maddr = mopt->imo_membership[mshipno]; 234 235 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr && 236 m->m_pkthdr.rcvif == maddr->inm_ifp) { 237 return (0); 238 } 239 } 240 return (-1); 241 } 242 243 void 244 udp_input(struct mbuf *m, ...) 245 { 246 int iphlen; 247 struct ip *ip; 248 struct udphdr *uh; 249 struct inpcb *inp; 250 struct mbuf *opts = NULL; 251 int len, off, proto; 252 struct ip save_ip; 253 struct sockaddr *append_sa; 254 __va_list ap; 255 256 __va_start(ap, m); 257 off = __va_arg(ap, int); 258 proto = __va_arg(ap, int); 259 __va_end(ap); 260 261 iphlen = off; 262 udpstat.udps_ipackets++; 263 264 /* 265 * Strip IP options, if any; should skip this, 266 * make available to user, and use on returned packets, 267 * but we don't yet have a way to check the checksum 268 * with options still present. 269 */ 270 if (iphlen > sizeof(struct ip)) { 271 ip_stripoptions(m); 272 iphlen = sizeof(struct ip); 273 } 274 275 /* 276 * IP and UDP headers are together in first mbuf. 277 * Already checked and pulled up in ip_demux(). 278 */ 279 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr), 280 ("UDP header not in one mbuf")); 281 282 ip = mtod(m, struct ip *); 283 uh = (struct udphdr *)((caddr_t)ip + iphlen); 284 285 /* destination port of 0 is illegal, based on RFC768. */ 286 if (uh->uh_dport == 0) 287 goto bad; 288 289 /* 290 * Make mbuf data length reflect UDP length. 291 * If not enough data to reflect UDP length, drop. 292 */ 293 len = ntohs((u_short)uh->uh_ulen); 294 if (ip->ip_len != len) { 295 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 296 udpstat.udps_badlen++; 297 goto bad; 298 } 299 m_adj(m, len - ip->ip_len); 300 /* ip->ip_len = len; */ 301 } 302 /* 303 * Save a copy of the IP header in case we want restore it 304 * for sending an ICMP error message in response. 305 */ 306 save_ip = *ip; 307 308 /* 309 * Checksum extended UDP header and data. 310 */ 311 if (uh->uh_sum) { 312 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 313 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 314 uh->uh_sum = m->m_pkthdr.csum_data; 315 else 316 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 317 ip->ip_dst.s_addr, htonl((u_short)len + 318 m->m_pkthdr.csum_data + IPPROTO_UDP)); 319 uh->uh_sum ^= 0xffff; 320 } else { 321 char b[9]; 322 323 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 324 bzero(((struct ipovly *)ip)->ih_x1, 9); 325 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 326 uh->uh_sum = in_cksum(m, len + sizeof(struct ip)); 327 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 328 } 329 if (uh->uh_sum) { 330 udpstat.udps_badsum++; 331 m_freem(m); 332 return; 333 } 334 } else 335 udpstat.udps_nosum++; 336 337 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 338 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 339 struct inpcb *last; 340 341 /* 342 * Deliver a multicast or broadcast datagram to *all* sockets 343 * for which the local and remote addresses and ports match 344 * those of the incoming datagram. This allows more than 345 * one process to receive multi/broadcasts on the same port. 346 * (This really ought to be done for unicast datagrams as 347 * well, but that would cause problems with existing 348 * applications that open both address-specific sockets and 349 * a wildcard socket listening to the same port -- they would 350 * end up receiving duplicates of every unicast datagram. 351 * Those applications open the multiple sockets to overcome an 352 * inadequacy of the UDP socket interface, but for backwards 353 * compatibility we avoid the problem here rather than 354 * fixing the interface. Maybe 4.5BSD will remedy this?) 355 */ 356 357 /* 358 * Construct sockaddr format source address. 359 */ 360 udp_in.sin_port = uh->uh_sport; 361 udp_in.sin_addr = ip->ip_src; 362 /* 363 * Locate pcb(s) for datagram. 364 * (Algorithm copied from raw_intr().) 365 */ 366 last = NULL; 367 #ifdef INET6 368 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 369 #endif 370 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) { 371 if (inp->inp_flags & INP_PLACEMARKER) 372 continue; 373 #ifdef INET6 374 if (!(inp->inp_vflag & INP_IPV4)) 375 continue; 376 #endif 377 if (inp->inp_lport != uh->uh_dport) 378 continue; 379 if (inp->inp_laddr.s_addr != INADDR_ANY) { 380 if (inp->inp_laddr.s_addr != 381 ip->ip_dst.s_addr) 382 continue; 383 } 384 if (inp->inp_faddr.s_addr != INADDR_ANY) { 385 if (inp->inp_faddr.s_addr != 386 ip->ip_src.s_addr || 387 inp->inp_fport != uh->uh_sport) 388 continue; 389 } 390 391 if (check_multicast_membership(ip, inp, m) < 0) 392 continue; 393 394 if (last != NULL) { 395 struct mbuf *n; 396 397 #ifdef IPSEC 398 /* check AH/ESP integrity. */ 399 if (ipsec4_in_reject_so(m, last->inp_socket)) 400 ipsecstat.in_polvio++; 401 /* do not inject data to pcb */ 402 else 403 #endif /*IPSEC*/ 404 #ifdef FAST_IPSEC 405 /* check AH/ESP integrity. */ 406 if (ipsec4_in_reject(m, last)) 407 ; 408 else 409 #endif /*FAST_IPSEC*/ 410 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) 411 udp_append(last, ip, n, 412 iphlen + 413 sizeof(struct udphdr)); 414 } 415 last = inp; 416 /* 417 * Don't look for additional matches if this one does 418 * not have either the SO_REUSEPORT or SO_REUSEADDR 419 * socket options set. This heuristic avoids searching 420 * through all pcbs in the common case of a non-shared 421 * port. It * assumes that an application will never 422 * clear these options after setting them. 423 */ 424 if (!(last->inp_socket->so_options & 425 (SO_REUSEPORT | SO_REUSEADDR))) 426 break; 427 } 428 429 if (last == NULL) { 430 /* 431 * No matching pcb found; discard datagram. 432 * (No need to send an ICMP Port Unreachable 433 * for a broadcast or multicast datgram.) 434 */ 435 udpstat.udps_noportbcast++; 436 goto bad; 437 } 438 #ifdef IPSEC 439 /* check AH/ESP integrity. */ 440 if (ipsec4_in_reject_so(m, last->inp_socket)) { 441 ipsecstat.in_polvio++; 442 goto bad; 443 } 444 #endif /*IPSEC*/ 445 #ifdef FAST_IPSEC 446 /* check AH/ESP integrity. */ 447 if (ipsec4_in_reject(m, last)) 448 goto bad; 449 #endif /*FAST_IPSEC*/ 450 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 451 return; 452 } 453 /* 454 * Locate pcb for datagram. 455 */ 456 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 457 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 458 if (inp == NULL) { 459 if (log_in_vain) { 460 char buf[sizeof "aaa.bbb.ccc.ddd"]; 461 462 strcpy(buf, inet_ntoa(ip->ip_dst)); 463 log(LOG_INFO, 464 "Connection attempt to UDP %s:%d from %s:%d\n", 465 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 466 ntohs(uh->uh_sport)); 467 } 468 udpstat.udps_noport++; 469 if (m->m_flags & (M_BCAST | M_MCAST)) { 470 udpstat.udps_noportbcast++; 471 goto bad; 472 } 473 if (blackhole) 474 goto bad; 475 #ifdef ICMP_BANDLIM 476 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 477 goto bad; 478 #endif 479 *ip = save_ip; 480 ip->ip_len += iphlen; 481 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 482 return; 483 } 484 #ifdef IPSEC 485 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 486 ipsecstat.in_polvio++; 487 goto bad; 488 } 489 #endif /*IPSEC*/ 490 #ifdef FAST_IPSEC 491 if (ipsec4_in_reject(m, inp)) 492 goto bad; 493 #endif /*FAST_IPSEC*/ 494 495 /* 496 * Construct sockaddr format source address. 497 * Stuff source address and datagram in user buffer. 498 */ 499 udp_in.sin_port = uh->uh_sport; 500 udp_in.sin_addr = ip->ip_src; 501 if ((inp->inp_flags & INP_CONTROLOPTS) || 502 (inp->inp_socket->so_options & SO_TIMESTAMP)) { 503 #ifdef INET6 504 if (inp->inp_vflag & INP_IPV6) { 505 int savedflags; 506 507 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 508 savedflags = inp->inp_flags; 509 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 510 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 511 inp->inp_flags = savedflags; 512 } else 513 #endif 514 ip_savecontrol(inp, &opts, ip, m); 515 } 516 m_adj(m, iphlen + sizeof(struct udphdr)); 517 #ifdef INET6 518 if (inp->inp_vflag & INP_IPV6) { 519 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 520 append_sa = (struct sockaddr *)&udp_in6; 521 } else 522 #endif 523 append_sa = (struct sockaddr *)&udp_in; 524 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 525 udpstat.udps_fullsock++; 526 goto bad; 527 } 528 sorwakeup(inp->inp_socket); 529 return; 530 bad: 531 m_freem(m); 532 if (opts) 533 m_freem(opts); 534 return; 535 } 536 537 #ifdef INET6 538 static void 539 ip_2_ip6_hdr(ip6, ip) 540 struct ip6_hdr *ip6; 541 struct ip *ip; 542 { 543 bzero(ip6, sizeof *ip6); 544 545 ip6->ip6_vfc = IPV6_VERSION; 546 ip6->ip6_plen = ip->ip_len; 547 ip6->ip6_nxt = ip->ip_p; 548 ip6->ip6_hlim = ip->ip_ttl; 549 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 550 IPV6_ADDR_INT32_SMP; 551 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 552 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 553 } 554 #endif 555 556 /* 557 * subroutine of udp_input(), mainly for source code readability. 558 * caller must properly init udp_ip6 and udp_in6 beforehand. 559 */ 560 static void 561 udp_append(last, ip, n, off) 562 struct inpcb *last; 563 struct ip *ip; 564 struct mbuf *n; 565 int off; 566 { 567 struct sockaddr *append_sa; 568 struct mbuf *opts = NULL; 569 570 if (last->inp_flags & INP_CONTROLOPTS || 571 last->inp_socket->so_options & SO_TIMESTAMP) { 572 #ifdef INET6 573 if (last->inp_vflag & INP_IPV6) { 574 int savedflags; 575 576 if (udp_ip6.uip6_init_done == 0) { 577 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 578 udp_ip6.uip6_init_done = 1; 579 } 580 savedflags = last->inp_flags; 581 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 582 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 583 last->inp_flags = savedflags; 584 } else 585 #endif 586 ip_savecontrol(last, &opts, ip, n); 587 } 588 #ifdef INET6 589 if (last->inp_vflag & INP_IPV6) { 590 if (udp_in6.uin6_init_done == 0) { 591 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 592 udp_in6.uin6_init_done = 1; 593 } 594 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 595 } else 596 #endif 597 append_sa = (struct sockaddr *)&udp_in; 598 m_adj(n, off); 599 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 600 m_freem(n); 601 if (opts) 602 m_freem(opts); 603 udpstat.udps_fullsock++; 604 } else 605 sorwakeup(last->inp_socket); 606 } 607 608 /* 609 * Notify a udp user of an asynchronous error; 610 * just wake up so that he can collect error status. 611 */ 612 void 613 udp_notify(inp, errno) 614 struct inpcb *inp; 615 int errno; 616 { 617 inp->inp_socket->so_error = errno; 618 sorwakeup(inp->inp_socket); 619 sowwakeup(inp->inp_socket); 620 } 621 622 void 623 udp_ctlinput(cmd, sa, vip) 624 int cmd; 625 struct sockaddr *sa; 626 void *vip; 627 { 628 struct ip *ip = vip; 629 struct udphdr *uh; 630 void (*notify) (struct inpcb *, int) = udp_notify; 631 struct in_addr faddr; 632 struct inpcb *inp; 633 634 faddr = ((struct sockaddr_in *)sa)->sin_addr; 635 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 636 return; 637 638 if (PRC_IS_REDIRECT(cmd)) { 639 ip = NULL; 640 notify = in_rtchange; 641 } else if (cmd == PRC_HOSTDEAD) 642 ip = NULL; 643 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 644 return; 645 if (ip) { 646 crit_enter(); 647 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 648 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 649 ip->ip_src, uh->uh_sport, 0, NULL); 650 if (inp != NULL && inp->inp_socket != NULL) 651 (*notify)(inp, inetctlerrmap[cmd]); 652 crit_exit(); 653 } else 654 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd], 655 notify); 656 } 657 658 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0, 659 in_pcblist_global, "S,xinpcb", "List of active UDP sockets"); 660 661 static int 662 udp_getcred(SYSCTL_HANDLER_ARGS) 663 { 664 struct sockaddr_in addrs[2]; 665 struct inpcb *inp; 666 int error; 667 668 error = suser(req->td); 669 if (error) 670 return (error); 671 error = SYSCTL_IN(req, addrs, sizeof addrs); 672 if (error) 673 return (error); 674 crit_enter(); 675 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 676 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 677 if (inp == NULL || inp->inp_socket == NULL) { 678 error = ENOENT; 679 goto out; 680 } 681 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 682 out: 683 crit_exit(); 684 return (error); 685 } 686 687 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 688 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 689 690 static int 691 udp_output(inp, m, dstaddr, control, td) 692 struct inpcb *inp; 693 struct mbuf *m; 694 struct sockaddr *dstaddr; 695 struct mbuf *control; 696 struct thread *td; 697 { 698 struct udpiphdr *ui; 699 int len = m->m_pkthdr.len; 700 struct sockaddr_in *sin; /* really is initialized before use */ 701 int error = 0; 702 703 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 704 error = EMSGSIZE; 705 goto release; 706 } 707 708 if (inp->inp_lport == 0) { /* unbound socket */ 709 error = in_pcbbind(inp, (struct sockaddr *)NULL, td); 710 if (error) 711 goto release; 712 in_pcbinswildcardhash(inp); 713 } 714 715 if (dstaddr != NULL) { /* destination address specified */ 716 if (inp->inp_faddr.s_addr != INADDR_ANY) { 717 /* already connected */ 718 error = EISCONN; 719 goto release; 720 } 721 sin = (struct sockaddr_in *)dstaddr; 722 prison_remote_ip(td, 0, &sin->sin_addr.s_addr); 723 } else { 724 if (inp->inp_faddr.s_addr == INADDR_ANY) { 725 /* no destination specified and not already connected */ 726 error = ENOTCONN; 727 goto release; 728 } 729 sin = NULL; 730 } 731 732 /* 733 * Calculate data length and get a mbuf 734 * for UDP and IP headers. 735 */ 736 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT); 737 if (m == NULL) { 738 error = ENOBUFS; 739 goto release; 740 } 741 742 /* 743 * Fill in mbuf with extended UDP header 744 * and addresses and length put into network format. 745 */ 746 ui = mtod(m, struct udpiphdr *); 747 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */ 748 ui->ui_pr = IPPROTO_UDP; 749 750 /* 751 * Set destination address. 752 */ 753 if (dstaddr != NULL) { /* use specified destination */ 754 ui->ui_dst = sin->sin_addr; 755 ui->ui_dport = sin->sin_port; 756 } else { /* use connected destination */ 757 ui->ui_dst = inp->inp_faddr; 758 ui->ui_dport = inp->inp_fport; 759 } 760 761 /* 762 * Set source address. 763 */ 764 if (inp->inp_laddr.s_addr == INADDR_ANY) { 765 struct sockaddr_in *if_sin; 766 767 if (dstaddr == NULL) { 768 /* 769 * connect() had (or should have) failed because 770 * the interface had no IP address, but the 771 * application proceeded to call send() anyways. 772 */ 773 error = ENOTCONN; 774 goto release; 775 } 776 777 /* Look up outgoing interface. */ 778 if ((error = in_pcbladdr(inp, dstaddr, &if_sin))) 779 goto release; 780 ui->ui_src = if_sin->sin_addr; /* use address of interface */ 781 } else { 782 ui->ui_src = inp->inp_laddr; /* use non-null bound address */ 783 } 784 ui->ui_sport = inp->inp_lport; 785 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound")); 786 787 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 788 789 /* 790 * Set up checksum and output datagram. 791 */ 792 if (udpcksum) { 793 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 794 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 795 m->m_pkthdr.csum_flags = CSUM_UDP; 796 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 797 } else { 798 ui->ui_sum = 0; 799 } 800 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len; 801 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 802 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 803 udpstat.udps_opackets++; 804 805 error = ip_output(m, inp->inp_options, &inp->inp_route, 806 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)), 807 inp->inp_moptions, inp); 808 809 return (error); 810 811 release: 812 m_freem(m); 813 return (error); 814 } 815 816 u_long udp_sendspace = 9216; /* really max datagram size */ 817 /* 40 1K datagrams */ 818 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 819 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 820 821 u_long udp_recvspace = 40 * (1024 + 822 #ifdef INET6 823 sizeof(struct sockaddr_in6) 824 #else 825 sizeof(struct sockaddr_in) 826 #endif 827 ); 828 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 829 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 830 831 static int 832 udp_abort(struct socket *so) 833 { 834 struct inpcb *inp; 835 836 inp = so->so_pcb; 837 if (inp == NULL) 838 return EINVAL; /* ??? possible? panic instead? */ 839 soisdisconnected(so); 840 crit_enter(); 841 in_pcbdetach(inp); 842 crit_exit(); 843 return 0; 844 } 845 846 static int 847 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai) 848 { 849 struct inpcb *inp; 850 int error; 851 852 inp = so->so_pcb; 853 if (inp != NULL) 854 return EINVAL; 855 856 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit); 857 if (error) 858 return error; 859 crit_enter(); 860 error = in_pcballoc(so, &udbinfo); 861 crit_exit(); 862 if (error) 863 return error; 864 865 inp = (struct inpcb *)so->so_pcb; 866 inp->inp_vflag |= INP_IPV4; 867 inp->inp_ip_ttl = ip_defttl; 868 return 0; 869 } 870 871 static int 872 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 873 { 874 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 875 struct inpcb *inp; 876 int error; 877 878 inp = so->so_pcb; 879 if (inp == NULL) 880 return EINVAL; 881 crit_enter(); 882 error = in_pcbbind(inp, nam, td); 883 crit_exit(); 884 if (error == 0) { 885 if (sin->sin_addr.s_addr != INADDR_ANY) 886 inp->inp_flags |= INP_WASBOUND_NOTANY; 887 in_pcbinswildcardhash(inp); 888 } 889 return error; 890 } 891 892 static int 893 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 894 { 895 struct inpcb *inp; 896 int error; 897 struct sockaddr_in *sin; 898 899 inp = so->so_pcb; 900 if (inp == NULL) 901 return EINVAL; 902 if (inp->inp_faddr.s_addr != INADDR_ANY) 903 return EISCONN; 904 error = 0; 905 crit_enter(); 906 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL && 907 inp->inp_laddr.s_addr == INADDR_ANY) { 908 error = in_pcbbind(inp, NULL, td); 909 } 910 if (error == 0) { 911 sin = (struct sockaddr_in *)nam; 912 prison_remote_ip(td, 0, &sin->sin_addr.s_addr); 913 if (inp->inp_flags & INP_WILDCARD) 914 in_pcbremwildcardhash(inp); 915 error = in_pcbconnect(inp, nam, td); 916 } 917 crit_exit(); 918 if (error == 0) 919 soisconnected(so); 920 else if (error == EAFNOSUPPORT) { /* connection dissolved */ 921 /* 922 * Follow traditional BSD behavior and retain 923 * the local port binding. But, fix the old misbehavior 924 * of overwriting any previously bound local address. 925 */ 926 if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) 927 inp->inp_laddr.s_addr = INADDR_ANY; 928 in_pcbinswildcardhash(inp); 929 } 930 return error; 931 } 932 933 static int 934 udp_detach(struct socket *so) 935 { 936 struct inpcb *inp; 937 938 inp = so->so_pcb; 939 if (inp == NULL) 940 return EINVAL; 941 crit_enter(); 942 in_pcbdetach(inp); 943 crit_exit(); 944 return 0; 945 } 946 947 static int 948 udp_disconnect(struct socket *so) 949 { 950 struct inpcb *inp; 951 952 inp = so->so_pcb; 953 if (inp == NULL) 954 return EINVAL; 955 if (inp->inp_faddr.s_addr == INADDR_ANY) 956 return ENOTCONN; 957 958 crit_enter(); 959 in_pcbdisconnect(inp); 960 crit_exit(); 961 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 962 return 0; 963 } 964 965 static int 966 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 967 struct mbuf *control, struct thread *td) 968 { 969 struct inpcb *inp; 970 971 inp = so->so_pcb; 972 if (inp == NULL) { 973 m_freem(m); 974 return EINVAL; 975 } 976 return udp_output(inp, m, addr, control, td); 977 } 978 979 int 980 udp_shutdown(struct socket *so) 981 { 982 struct inpcb *inp; 983 984 inp = so->so_pcb; 985 if (inp == NULL) 986 return EINVAL; 987 socantsendmore(so); 988 return 0; 989 } 990 991 struct pr_usrreqs udp_usrreqs = { 992 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 993 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 994 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 995 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 996 in_setsockaddr, sosendudp, soreceive, sopoll 997 }; 998 999