1 /* 2 * Copyright (c) 1982, 1986, 1988, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)udp_usrreq.c 8.4 (Berkeley) 1/21/94 34 * $Id: udp_usrreq.c,v 1.3 1994/08/02 07:49:24 davidg Exp $ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/socket.h> 43 #include <sys/socketvar.h> 44 #include <sys/errno.h> 45 #include <sys/stat.h> 46 47 #include <net/if.h> 48 #include <net/route.h> 49 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #include <netinet/ip.h> 53 #include <netinet/in_pcb.h> 54 #include <netinet/ip_var.h> 55 #include <netinet/ip_icmp.h> 56 #include <netinet/udp.h> 57 #include <netinet/udp_var.h> 58 59 /* 60 * UDP protocol implementation. 61 * Per RFC 768, August, 1980. 62 */ 63 #ifndef COMPAT_42 64 int udpcksum = 1; 65 #else 66 int udpcksum = 0; /* XXX */ 67 #endif 68 69 struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 70 struct inpcb *udp_last_inpcb = &udb; 71 72 static void udp_detach __P((struct inpcb *)); 73 static void udp_notify __P((struct inpcb *, int)); 74 static struct mbuf *udp_saveopt __P((caddr_t, int, int)); 75 76 void 77 udp_init() 78 { 79 udb.inp_next = udb.inp_prev = &udb; 80 } 81 82 void 83 udp_input(m, iphlen) 84 register struct mbuf *m; 85 int iphlen; 86 { 87 register struct ip *ip; 88 register struct udphdr *uh; 89 register struct inpcb *inp; 90 struct mbuf *opts = 0; 91 int len; 92 struct ip save_ip; 93 94 udpstat.udps_ipackets++; 95 96 /* 97 * Strip IP options, if any; should skip this, 98 * make available to user, and use on returned packets, 99 * but we don't yet have a way to check the checksum 100 * with options still present. 101 */ 102 if (iphlen > sizeof (struct ip)) { 103 ip_stripoptions(m, (struct mbuf *)0); 104 iphlen = sizeof(struct ip); 105 } 106 107 /* 108 * Get IP and UDP header together in first mbuf. 109 */ 110 ip = mtod(m, struct ip *); 111 if (m->m_len < iphlen + sizeof(struct udphdr)) { 112 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 113 udpstat.udps_hdrops++; 114 return; 115 } 116 ip = mtod(m, struct ip *); 117 } 118 uh = (struct udphdr *)((caddr_t)ip + iphlen); 119 120 /* 121 * Make mbuf data length reflect UDP length. 122 * If not enough data to reflect UDP length, drop. 123 */ 124 len = ntohs((u_short)uh->uh_ulen); 125 if (ip->ip_len != len) { 126 if (len > ip->ip_len) { 127 udpstat.udps_badlen++; 128 goto bad; 129 } 130 m_adj(m, len - ip->ip_len); 131 /* ip->ip_len = len; */ 132 } 133 /* 134 * Save a copy of the IP header in case we want restore it 135 * for sending an ICMP error message in response. 136 */ 137 save_ip = *ip; 138 139 /* 140 * Checksum extended UDP header and data. 141 */ 142 if (udpcksum && uh->uh_sum) { 143 ((struct ipovly *)ip)->ih_next = 0; 144 ((struct ipovly *)ip)->ih_prev = 0; 145 ((struct ipovly *)ip)->ih_x1 = 0; 146 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 147 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 148 if (uh->uh_sum) { 149 udpstat.udps_badsum++; 150 m_freem(m); 151 return; 152 } 153 } 154 155 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 156 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 157 struct socket *last; 158 /* 159 * Deliver a multicast or broadcast datagram to *all* sockets 160 * for which the local and remote addresses and ports match 161 * those of the incoming datagram. This allows more than 162 * one process to receive multi/broadcasts on the same port. 163 * (This really ought to be done for unicast datagrams as 164 * well, but that would cause problems with existing 165 * applications that open both address-specific sockets and 166 * a wildcard socket listening to the same port -- they would 167 * end up receiving duplicates of every unicast datagram. 168 * Those applications open the multiple sockets to overcome an 169 * inadequacy of the UDP socket interface, but for backwards 170 * compatibility we avoid the problem here rather than 171 * fixing the interface. Maybe 4.5BSD will remedy this?) 172 */ 173 174 /* 175 * Construct sockaddr format source address. 176 */ 177 udp_in.sin_port = uh->uh_sport; 178 udp_in.sin_addr = ip->ip_src; 179 m->m_len -= sizeof (struct udpiphdr); 180 m->m_data += sizeof (struct udpiphdr); 181 /* 182 * Locate pcb(s) for datagram. 183 * (Algorithm copied from raw_intr().) 184 */ 185 last = NULL; 186 for (inp = udb.inp_next; inp != &udb; inp = inp->inp_next) { 187 if (inp->inp_lport != uh->uh_dport) 188 continue; 189 if (inp->inp_laddr.s_addr != INADDR_ANY) { 190 if (inp->inp_laddr.s_addr != 191 ip->ip_dst.s_addr) 192 continue; 193 } 194 if (inp->inp_faddr.s_addr != INADDR_ANY) { 195 if (inp->inp_faddr.s_addr != 196 ip->ip_src.s_addr || 197 inp->inp_fport != uh->uh_sport) 198 continue; 199 } 200 201 if (last != NULL) { 202 struct mbuf *n; 203 204 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 205 if (sbappendaddr(&last->so_rcv, 206 (struct sockaddr *)&udp_in, 207 n, (struct mbuf *)0) == 0) { 208 m_freem(n); 209 udpstat.udps_fullsock++; 210 } else 211 sorwakeup(last); 212 } 213 } 214 last = inp->inp_socket; 215 /* 216 * Don't look for additional matches if this one does 217 * not have either the SO_REUSEPORT or SO_REUSEADDR 218 * socket options set. This heuristic avoids searching 219 * through all pcbs in the common case of a non-shared 220 * port. It * assumes that an application will never 221 * clear these options after setting them. 222 */ 223 if ((last->so_options&(SO_REUSEPORT|SO_REUSEADDR) == 0)) 224 break; 225 } 226 227 if (last == NULL) { 228 /* 229 * No matching pcb found; discard datagram. 230 * (No need to send an ICMP Port Unreachable 231 * for a broadcast or multicast datgram.) 232 */ 233 udpstat.udps_noportbcast++; 234 goto bad; 235 } 236 if (sbappendaddr(&last->so_rcv, (struct sockaddr *)&udp_in, 237 m, (struct mbuf *)0) == 0) { 238 udpstat.udps_fullsock++; 239 goto bad; 240 } 241 sorwakeup(last); 242 return; 243 } 244 /* 245 * Locate pcb for datagram. 246 */ 247 inp = udp_last_inpcb; 248 if (inp->inp_lport != uh->uh_dport || 249 inp->inp_fport != uh->uh_sport || 250 inp->inp_faddr.s_addr != ip->ip_src.s_addr || 251 inp->inp_laddr.s_addr != ip->ip_dst.s_addr) { 252 inp = in_pcblookup(&udb, ip->ip_src, uh->uh_sport, 253 ip->ip_dst, uh->uh_dport, INPLOOKUP_WILDCARD); 254 if (inp) 255 udp_last_inpcb = inp; 256 udpstat.udpps_pcbcachemiss++; 257 } 258 if (inp == 0) { 259 udpstat.udps_noport++; 260 if (m->m_flags & (M_BCAST | M_MCAST)) { 261 udpstat.udps_noportbcast++; 262 goto bad; 263 } 264 *ip = save_ip; 265 ip->ip_len += iphlen; 266 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 267 return; 268 } 269 270 /* 271 * Construct sockaddr format source address. 272 * Stuff source address and datagram in user buffer. 273 */ 274 udp_in.sin_port = uh->uh_sport; 275 udp_in.sin_addr = ip->ip_src; 276 if (inp->inp_flags & INP_CONTROLOPTS) { 277 struct mbuf **mp = &opts; 278 279 if (inp->inp_flags & INP_RECVDSTADDR) { 280 *mp = udp_saveopt((caddr_t) &ip->ip_dst, 281 sizeof(struct in_addr), IP_RECVDSTADDR); 282 if (*mp) 283 mp = &(*mp)->m_next; 284 } 285 #ifdef notyet 286 /* options were tossed above */ 287 if (inp->inp_flags & INP_RECVOPTS) { 288 *mp = udp_saveopt((caddr_t) opts_deleted_above, 289 sizeof(struct in_addr), IP_RECVOPTS); 290 if (*mp) 291 mp = &(*mp)->m_next; 292 } 293 /* ip_srcroute doesn't do what we want here, need to fix */ 294 if (inp->inp_flags & INP_RECVRETOPTS) { 295 *mp = udp_saveopt((caddr_t) ip_srcroute(), 296 sizeof(struct in_addr), IP_RECVRETOPTS); 297 if (*mp) 298 mp = &(*mp)->m_next; 299 } 300 #endif 301 } 302 iphlen += sizeof(struct udphdr); 303 m->m_len -= iphlen; 304 m->m_pkthdr.len -= iphlen; 305 m->m_data += iphlen; 306 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 307 m, opts) == 0) { 308 udpstat.udps_fullsock++; 309 goto bad; 310 } 311 sorwakeup(inp->inp_socket); 312 return; 313 bad: 314 m_freem(m); 315 if (opts) 316 m_freem(opts); 317 } 318 319 /* 320 * Create a "control" mbuf containing the specified data 321 * with the specified type for presentation with a datagram. 322 */ 323 struct mbuf * 324 udp_saveopt(p, size, type) 325 caddr_t p; 326 register int size; 327 int type; 328 { 329 register struct cmsghdr *cp; 330 struct mbuf *m; 331 332 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL) 333 return ((struct mbuf *) NULL); 334 cp = (struct cmsghdr *) mtod(m, struct cmsghdr *); 335 bcopy(p, CMSG_DATA(cp), size); 336 size += sizeof(*cp); 337 m->m_len = size; 338 cp->cmsg_len = size; 339 cp->cmsg_level = IPPROTO_IP; 340 cp->cmsg_type = type; 341 return (m); 342 } 343 344 /* 345 * Notify a udp user of an asynchronous error; 346 * just wake up so that he can collect error status. 347 */ 348 static void 349 udp_notify(inp, errno) 350 register struct inpcb *inp; 351 int errno; 352 { 353 inp->inp_socket->so_error = errno; 354 sorwakeup(inp->inp_socket); 355 sowwakeup(inp->inp_socket); 356 } 357 358 void 359 udp_ctlinput(cmd, sa, ip) 360 int cmd; 361 struct sockaddr *sa; 362 register struct ip *ip; 363 { 364 register struct udphdr *uh; 365 extern struct in_addr zeroin_addr; 366 extern u_char inetctlerrmap[]; 367 368 if (!PRC_IS_REDIRECT(cmd) && 369 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 370 return; 371 if (ip) { 372 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 373 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 374 cmd, udp_notify); 375 } else 376 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 377 } 378 379 int 380 udp_output(inp, m, addr, control) 381 register struct inpcb *inp; 382 register struct mbuf *m; 383 struct mbuf *addr, *control; 384 { 385 register struct udpiphdr *ui; 386 register int len = m->m_pkthdr.len; 387 struct in_addr laddr; 388 int s = 0, error = 0; 389 390 if (control) 391 m_freem(control); /* XXX */ 392 393 if (addr) { 394 laddr = inp->inp_laddr; 395 if (inp->inp_faddr.s_addr != INADDR_ANY) { 396 error = EISCONN; 397 goto release; 398 } 399 /* 400 * Must block input while temporarily connected. 401 */ 402 s = splnet(); 403 error = in_pcbconnect(inp, addr); 404 if (error) { 405 splx(s); 406 goto release; 407 } 408 } else { 409 if (inp->inp_faddr.s_addr == INADDR_ANY) { 410 error = ENOTCONN; 411 goto release; 412 } 413 } 414 /* 415 * Calculate data length and get a mbuf 416 * for UDP and IP headers. 417 */ 418 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 419 if (m == 0) { 420 error = ENOBUFS; 421 goto release; 422 } 423 424 /* 425 * Fill in mbuf with extended UDP header 426 * and addresses and length put into network format. 427 */ 428 ui = mtod(m, struct udpiphdr *); 429 ui->ui_next = ui->ui_prev = 0; 430 ui->ui_x1 = 0; 431 ui->ui_pr = IPPROTO_UDP; 432 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 433 ui->ui_src = inp->inp_laddr; 434 ui->ui_dst = inp->inp_faddr; 435 ui->ui_sport = inp->inp_lport; 436 ui->ui_dport = inp->inp_fport; 437 ui->ui_ulen = ui->ui_len; 438 439 /* 440 * Stuff checksum and output datagram. 441 */ 442 ui->ui_sum = 0; 443 if (udpcksum) { 444 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 445 ui->ui_sum = 0xffff; 446 } 447 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 448 ((struct ip *)ui)->ip_ttl = inp->inp_ip.ip_ttl; /* XXX */ 449 ((struct ip *)ui)->ip_tos = inp->inp_ip.ip_tos; /* XXX */ 450 udpstat.udps_opackets++; 451 error = ip_output(m, inp->inp_options, &inp->inp_route, 452 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 453 inp->inp_moptions); 454 455 if (addr) { 456 in_pcbdisconnect(inp); 457 inp->inp_laddr = laddr; 458 splx(s); 459 } 460 return (error); 461 462 release: 463 m_freem(m); 464 return (error); 465 } 466 467 u_long udp_sendspace = 9216; /* really max datagram size */ 468 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 469 /* 40 1K datagrams */ 470 471 /*ARGSUSED*/ 472 int 473 udp_usrreq(so, req, m, addr, control) 474 struct socket *so; 475 int req; 476 struct mbuf *m, *addr, *control; 477 { 478 struct inpcb *inp = sotoinpcb(so); 479 int error = 0; 480 int s; 481 482 if (req == PRU_CONTROL) 483 return (in_control(so, (int)m, (caddr_t)addr, 484 (struct ifnet *)control)); 485 if (inp == NULL && req != PRU_ATTACH) { 486 error = EINVAL; 487 goto release; 488 } 489 /* 490 * Note: need to block udp_input while changing 491 * the udp pcb queue and/or pcb addresses. 492 */ 493 switch (req) { 494 495 case PRU_ATTACH: 496 if (inp != NULL) { 497 error = EINVAL; 498 break; 499 } 500 s = splnet(); 501 error = in_pcballoc(so, &udb); 502 splx(s); 503 if (error) 504 break; 505 error = soreserve(so, udp_sendspace, udp_recvspace); 506 if (error) 507 break; 508 ((struct inpcb *) so->so_pcb)->inp_ip.ip_ttl = ip_defttl; 509 break; 510 511 case PRU_DETACH: 512 udp_detach(inp); 513 break; 514 515 case PRU_BIND: 516 s = splnet(); 517 error = in_pcbbind(inp, addr); 518 splx(s); 519 break; 520 521 case PRU_LISTEN: 522 error = EOPNOTSUPP; 523 break; 524 525 case PRU_CONNECT: 526 if (inp->inp_faddr.s_addr != INADDR_ANY) { 527 error = EISCONN; 528 break; 529 } 530 s = splnet(); 531 error = in_pcbconnect(inp, addr); 532 splx(s); 533 if (error == 0) 534 soisconnected(so); 535 break; 536 537 case PRU_CONNECT2: 538 error = EOPNOTSUPP; 539 break; 540 541 case PRU_ACCEPT: 542 error = EOPNOTSUPP; 543 break; 544 545 case PRU_DISCONNECT: 546 if (inp->inp_faddr.s_addr == INADDR_ANY) { 547 error = ENOTCONN; 548 break; 549 } 550 s = splnet(); 551 in_pcbdisconnect(inp); 552 inp->inp_laddr.s_addr = INADDR_ANY; 553 splx(s); 554 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 555 break; 556 557 case PRU_SHUTDOWN: 558 socantsendmore(so); 559 break; 560 561 case PRU_SEND: 562 return (udp_output(inp, m, addr, control)); 563 564 case PRU_ABORT: 565 soisdisconnected(so); 566 udp_detach(inp); 567 break; 568 569 case PRU_SOCKADDR: 570 in_setsockaddr(inp, addr); 571 break; 572 573 case PRU_PEERADDR: 574 in_setpeeraddr(inp, addr); 575 break; 576 577 case PRU_SENSE: 578 /* 579 * stat: don't bother with a blocksize. 580 */ 581 return (0); 582 583 case PRU_SENDOOB: 584 case PRU_FASTTIMO: 585 case PRU_SLOWTIMO: 586 case PRU_PROTORCV: 587 case PRU_PROTOSEND: 588 error = EOPNOTSUPP; 589 break; 590 591 case PRU_RCVD: 592 case PRU_RCVOOB: 593 return (EOPNOTSUPP); /* do not free mbuf's */ 594 595 default: 596 panic("udp_usrreq"); 597 } 598 599 release: 600 if (control) { 601 printf("udp control data unexpectedly retained\n"); 602 m_freem(control); 603 } 604 if (m) 605 m_freem(m); 606 return (error); 607 } 608 609 static void 610 udp_detach(inp) 611 struct inpcb *inp; 612 { 613 int s = splnet(); 614 615 if (inp == udp_last_inpcb) 616 udp_last_inpcb = &udb; 617 in_pcbdetach(inp); 618 splx(s); 619 } 620 621 /* 622 * Sysctl for udp variables. 623 */ 624 int 625 udp_sysctl(name, namelen, oldp, oldlenp, newp, newlen) 626 int *name; 627 u_int namelen; 628 void *oldp; 629 size_t *oldlenp; 630 void *newp; 631 size_t newlen; 632 { 633 /* All sysctl names at this level are terminal. */ 634 if (namelen != 1) 635 return (ENOTDIR); 636 637 switch (name[0]) { 638 case UDPCTL_CHECKSUM: 639 return (sysctl_int(oldp, oldlenp, newp, newlen, &udpcksum)); 640 default: 641 return (ENOPROTOOPT); 642 } 643 /* NOTREACHED */ 644 } 645