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