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 * $FreeBSD$ 35 */ 36 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/kernel.h> 40 #include <sys/malloc.h> 41 #include <sys/mbuf.h> 42 #include <sys/proc.h> 43 #include <sys/protosw.h> 44 #include <sys/socket.h> 45 #include <sys/socketvar.h> 46 #include <sys/sysctl.h> 47 #include <sys/syslog.h> 48 49 #include <vm/vm_zone.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/icmp_var.h> 62 #include <netinet/udp.h> 63 #include <netinet/udp_var.h> 64 65 /* 66 * UDP protocol implementation. 67 * Per RFC 768, August, 1980. 68 */ 69 #ifndef COMPAT_42 70 static int udpcksum = 1; 71 #else 72 static int udpcksum = 0; /* XXX */ 73 #endif 74 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 75 &udpcksum, 0, ""); 76 77 int log_in_vain = 0; 78 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 79 &log_in_vain, 0, "Log all incoming UDP packets"); 80 81 static int blackhole = 0; 82 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 83 &blackhole, 0, "Do not send port unreachables for refused connects"); 84 85 struct inpcbhead udb; /* from udp_var.h */ 86 struct inpcbinfo udbinfo; 87 88 #ifndef UDBHASHSIZE 89 #define UDBHASHSIZE 16 90 #endif 91 92 struct udpstat udpstat; /* from udp_var.h */ 93 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, 94 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 95 96 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET }; 97 98 static int udp_output __P((struct inpcb *, struct mbuf *, struct sockaddr *, 99 struct mbuf *, struct proc *)); 100 101 void 102 udp_init() 103 { 104 LIST_INIT(&udb); 105 udbinfo.listhead = &udb; 106 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 107 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 108 &udbinfo.porthashmask); 109 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 110 ZONE_INTERRUPT, 0); 111 } 112 113 void 114 udp_input(m, iphlen) 115 register struct mbuf *m; 116 int iphlen; 117 { 118 register struct ip *ip; 119 register struct udphdr *uh; 120 register struct inpcb *inp; 121 struct mbuf *opts = 0; 122 int len; 123 struct ip save_ip; 124 125 udpstat.udps_ipackets++; 126 127 /* 128 * Strip IP options, if any; should skip this, 129 * make available to user, and use on returned packets, 130 * but we don't yet have a way to check the checksum 131 * with options still present. 132 */ 133 if (iphlen > sizeof (struct ip)) { 134 ip_stripoptions(m, (struct mbuf *)0); 135 iphlen = sizeof(struct ip); 136 } 137 138 /* 139 * Get IP and UDP header together in first mbuf. 140 */ 141 ip = mtod(m, struct ip *); 142 if (m->m_len < iphlen + sizeof(struct udphdr)) { 143 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { 144 udpstat.udps_hdrops++; 145 return; 146 } 147 ip = mtod(m, struct ip *); 148 } 149 uh = (struct udphdr *)((caddr_t)ip + iphlen); 150 151 /* 152 * Make mbuf data length reflect UDP length. 153 * If not enough data to reflect UDP length, drop. 154 */ 155 len = ntohs((u_short)uh->uh_ulen); 156 if (ip->ip_len != len) { 157 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 158 udpstat.udps_badlen++; 159 goto bad; 160 } 161 m_adj(m, len - ip->ip_len); 162 /* ip->ip_len = len; */ 163 } 164 /* 165 * Save a copy of the IP header in case we want restore it 166 * for sending an ICMP error message in response. 167 */ 168 save_ip = *ip; 169 170 /* 171 * Checksum extended UDP header and data. 172 */ 173 if (uh->uh_sum) { 174 bzero(((struct ipovly *)ip)->ih_x1, 9); 175 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 176 uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); 177 if (uh->uh_sum) { 178 udpstat.udps_badsum++; 179 m_freem(m); 180 return; 181 } 182 } 183 184 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 185 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 186 struct inpcb *last; 187 /* 188 * Deliver a multicast or broadcast datagram to *all* sockets 189 * for which the local and remote addresses and ports match 190 * those of the incoming datagram. This allows more than 191 * one process to receive multi/broadcasts on the same port. 192 * (This really ought to be done for unicast datagrams as 193 * well, but that would cause problems with existing 194 * applications that open both address-specific sockets and 195 * a wildcard socket listening to the same port -- they would 196 * end up receiving duplicates of every unicast datagram. 197 * Those applications open the multiple sockets to overcome an 198 * inadequacy of the UDP socket interface, but for backwards 199 * compatibility we avoid the problem here rather than 200 * fixing the interface. Maybe 4.5BSD will remedy this?) 201 */ 202 203 /* 204 * Construct sockaddr format source address. 205 */ 206 udp_in.sin_port = uh->uh_sport; 207 udp_in.sin_addr = ip->ip_src; 208 m->m_len -= sizeof (struct udpiphdr); 209 m->m_data += sizeof (struct udpiphdr); 210 /* 211 * Locate pcb(s) for datagram. 212 * (Algorithm copied from raw_intr().) 213 */ 214 last = NULL; 215 for (inp = udb.lh_first; inp != NULL; inp = inp->inp_list.le_next) { 216 if (inp->inp_lport != uh->uh_dport) 217 continue; 218 if (inp->inp_laddr.s_addr != INADDR_ANY) { 219 if (inp->inp_laddr.s_addr != 220 ip->ip_dst.s_addr) 221 continue; 222 } 223 if (inp->inp_faddr.s_addr != INADDR_ANY) { 224 if (inp->inp_faddr.s_addr != 225 ip->ip_src.s_addr || 226 inp->inp_fport != uh->uh_sport) 227 continue; 228 } 229 230 if (last != NULL) { 231 struct mbuf *n; 232 233 if ((n = m_copy(m, 0, M_COPYALL)) != NULL) { 234 if (last->inp_flags & INP_CONTROLOPTS 235 || last->inp_socket->so_options & SO_TIMESTAMP) 236 ip_savecontrol(last, &opts, ip, n); 237 if (sbappendaddr(&last->inp_socket->so_rcv, 238 (struct sockaddr *)&udp_in, 239 n, opts) == 0) { 240 m_freem(n); 241 if (opts) 242 m_freem(opts); 243 udpstat.udps_fullsock++; 244 } else 245 sorwakeup(last->inp_socket); 246 opts = 0; 247 } 248 } 249 last = inp; 250 /* 251 * Don't look for additional matches if this one does 252 * not have either the SO_REUSEPORT or SO_REUSEADDR 253 * socket options set. This heuristic avoids searching 254 * through all pcbs in the common case of a non-shared 255 * port. It * assumes that an application will never 256 * clear these options after setting them. 257 */ 258 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0) 259 break; 260 } 261 262 if (last == NULL) { 263 /* 264 * No matching pcb found; discard datagram. 265 * (No need to send an ICMP Port Unreachable 266 * for a broadcast or multicast datgram.) 267 */ 268 udpstat.udps_noportbcast++; 269 goto bad; 270 } 271 if (last->inp_flags & INP_CONTROLOPTS 272 || last->inp_socket->so_options & SO_TIMESTAMP) 273 ip_savecontrol(last, &opts, ip, m); 274 if (sbappendaddr(&last->inp_socket->so_rcv, 275 (struct sockaddr *)&udp_in, 276 m, opts) == 0) { 277 udpstat.udps_fullsock++; 278 goto bad; 279 } 280 sorwakeup(last->inp_socket); 281 return; 282 } 283 /* 284 * Locate pcb for datagram. 285 */ 286 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 287 ip->ip_dst, uh->uh_dport, 1); 288 if (inp == NULL) { 289 if (log_in_vain) { 290 char buf[4*sizeof "123"]; 291 292 strcpy(buf, inet_ntoa(ip->ip_dst)); 293 log(LOG_INFO, 294 "Connection attempt to UDP %s:%d from %s:%d\n", 295 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 296 ntohs(uh->uh_sport)); 297 } 298 udpstat.udps_noport++; 299 if (m->m_flags & (M_BCAST | M_MCAST)) { 300 udpstat.udps_noportbcast++; 301 goto bad; 302 } 303 *ip = save_ip; 304 #ifdef ICMP_BANDLIM 305 if (badport_bandlim(0) < 0) 306 goto bad; 307 #endif 308 if (!blackhole) 309 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 310 return; 311 } 312 313 /* 314 * Construct sockaddr format source address. 315 * Stuff source address and datagram in user buffer. 316 */ 317 udp_in.sin_port = uh->uh_sport; 318 udp_in.sin_addr = ip->ip_src; 319 if (inp->inp_flags & INP_CONTROLOPTS 320 || inp->inp_socket->so_options & SO_TIMESTAMP) 321 ip_savecontrol(inp, &opts, ip, m); 322 iphlen += sizeof(struct udphdr); 323 m->m_len -= iphlen; 324 m->m_pkthdr.len -= iphlen; 325 m->m_data += iphlen; 326 if (sbappendaddr(&inp->inp_socket->so_rcv, (struct sockaddr *)&udp_in, 327 m, opts) == 0) { 328 udpstat.udps_fullsock++; 329 goto bad; 330 } 331 sorwakeup(inp->inp_socket); 332 return; 333 bad: 334 m_freem(m); 335 if (opts) 336 m_freem(opts); 337 } 338 339 /* 340 * Notify a udp user of an asynchronous error; 341 * just wake up so that he can collect error status. 342 */ 343 void 344 udp_notify(inp, errno) 345 register struct inpcb *inp; 346 int errno; 347 { 348 inp->inp_socket->so_error = errno; 349 sorwakeup(inp->inp_socket); 350 sowwakeup(inp->inp_socket); 351 } 352 353 void 354 udp_ctlinput(cmd, sa, vip) 355 int cmd; 356 struct sockaddr *sa; 357 void *vip; 358 { 359 register struct ip *ip = vip; 360 register struct udphdr *uh; 361 362 if (!PRC_IS_REDIRECT(cmd) && 363 ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)) 364 return; 365 if (ip) { 366 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 367 in_pcbnotify(&udb, sa, uh->uh_dport, ip->ip_src, uh->uh_sport, 368 cmd, udp_notify); 369 } else 370 in_pcbnotify(&udb, sa, 0, zeroin_addr, 0, cmd, udp_notify); 371 } 372 373 static int 374 udp_pcblist SYSCTL_HANDLER_ARGS 375 { 376 int error, i, n, s; 377 struct inpcb *inp, **inp_list; 378 inp_gen_t gencnt; 379 struct xinpgen xig; 380 381 /* 382 * The process of preparing the TCB list is too time-consuming and 383 * resource-intensive to repeat twice on every request. 384 */ 385 if (req->oldptr == 0) { 386 n = udbinfo.ipi_count; 387 req->oldidx = 2 * (sizeof xig) 388 + (n + n/8) * sizeof(struct xinpcb); 389 return 0; 390 } 391 392 if (req->newptr != 0) 393 return EPERM; 394 395 /* 396 * OK, now we're committed to doing something. 397 */ 398 s = splnet(); 399 gencnt = udbinfo.ipi_gencnt; 400 n = udbinfo.ipi_count; 401 splx(s); 402 403 xig.xig_len = sizeof xig; 404 xig.xig_count = n; 405 xig.xig_gen = gencnt; 406 xig.xig_sogen = so_gencnt; 407 error = SYSCTL_OUT(req, &xig, sizeof xig); 408 if (error) 409 return error; 410 411 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 412 if (inp_list == 0) 413 return ENOMEM; 414 415 s = splnet(); 416 for (inp = udbinfo.listhead->lh_first, i = 0; inp && i < n; 417 inp = inp->inp_list.le_next) { 418 if (inp->inp_gencnt <= gencnt && !prison_xinpcb(req->p, inp)) 419 inp_list[i++] = inp; 420 } 421 splx(s); 422 n = i; 423 424 error = 0; 425 for (i = 0; i < n; i++) { 426 inp = inp_list[i]; 427 if (inp->inp_gencnt <= gencnt) { 428 struct xinpcb xi; 429 xi.xi_len = sizeof xi; 430 /* XXX should avoid extra copy */ 431 bcopy(inp, &xi.xi_inp, sizeof *inp); 432 if (inp->inp_socket) 433 sotoxsocket(inp->inp_socket, &xi.xi_socket); 434 error = SYSCTL_OUT(req, &xi, sizeof xi); 435 } 436 } 437 if (!error) { 438 /* 439 * Give the user an updated idea of our state. 440 * If the generation differs from what we told 441 * her before, she knows that something happened 442 * while we were processing this request, and it 443 * might be necessary to retry. 444 */ 445 s = splnet(); 446 xig.xig_gen = udbinfo.ipi_gencnt; 447 xig.xig_sogen = so_gencnt; 448 xig.xig_count = udbinfo.ipi_count; 449 splx(s); 450 error = SYSCTL_OUT(req, &xig, sizeof xig); 451 } 452 free(inp_list, M_TEMP); 453 return error; 454 } 455 456 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, 457 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 458 459 static int 460 udp_getcred SYSCTL_HANDLER_ARGS 461 { 462 struct sockaddr_in addrs[2]; 463 struct inpcb *inp; 464 int error, s; 465 466 error = suser(req->p); 467 if (error) 468 return (error); 469 error = SYSCTL_IN(req, addrs, sizeof(addrs)); 470 if (error) 471 return (error); 472 s = splnet(); 473 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 474 addrs[0].sin_addr, addrs[0].sin_port, 1); 475 if (inp == NULL || inp->inp_socket == NULL) { 476 error = ENOENT; 477 goto out; 478 } 479 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 480 out: 481 splx(s); 482 return (error); 483 } 484 485 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 486 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 487 488 static int 489 udp_output(inp, m, addr, control, p) 490 register struct inpcb *inp; 491 register struct mbuf *m; 492 struct sockaddr *addr; 493 struct mbuf *control; 494 struct proc *p; 495 { 496 register struct udpiphdr *ui; 497 register int len = m->m_pkthdr.len; 498 struct in_addr laddr; 499 struct sockaddr_in *sin; 500 int s = 0, error = 0; 501 502 if (control) 503 m_freem(control); /* XXX */ 504 505 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 506 error = EMSGSIZE; 507 goto release; 508 } 509 510 if (addr) { 511 sin = (struct sockaddr_in *)addr; 512 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 513 laddr = inp->inp_laddr; 514 if (inp->inp_faddr.s_addr != INADDR_ANY) { 515 error = EISCONN; 516 goto release; 517 } 518 /* 519 * Must block input while temporarily connected. 520 */ 521 s = splnet(); 522 error = in_pcbconnect(inp, addr, p); 523 if (error) { 524 splx(s); 525 goto release; 526 } 527 } else { 528 if (inp->inp_faddr.s_addr == INADDR_ANY) { 529 error = ENOTCONN; 530 goto release; 531 } 532 } 533 /* 534 * Calculate data length and get a mbuf 535 * for UDP and IP headers. 536 */ 537 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); 538 if (m == 0) { 539 error = ENOBUFS; 540 if (addr) 541 splx(s); 542 goto release; 543 } 544 545 /* 546 * Fill in mbuf with extended UDP header 547 * and addresses and length put into network format. 548 */ 549 ui = mtod(m, struct udpiphdr *); 550 bzero(ui->ui_x1, sizeof(ui->ui_x1)); 551 ui->ui_pr = IPPROTO_UDP; 552 ui->ui_len = htons((u_short)len + sizeof (struct udphdr)); 553 ui->ui_src = inp->inp_laddr; 554 ui->ui_dst = inp->inp_faddr; 555 ui->ui_sport = inp->inp_lport; 556 ui->ui_dport = inp->inp_fport; 557 ui->ui_ulen = ui->ui_len; 558 559 /* 560 * Stuff checksum and output datagram. 561 */ 562 ui->ui_sum = 0; 563 if (udpcksum) { 564 if ((ui->ui_sum = in_cksum(m, sizeof (struct udpiphdr) + len)) == 0) 565 ui->ui_sum = 0xffff; 566 } 567 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; 568 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 569 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 570 udpstat.udps_opackets++; 571 error = ip_output(m, inp->inp_options, &inp->inp_route, 572 inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST), 573 inp->inp_moptions); 574 575 if (addr) { 576 in_pcbdisconnect(inp); 577 inp->inp_laddr = laddr; /* XXX rehash? */ 578 splx(s); 579 } 580 return (error); 581 582 release: 583 m_freem(m); 584 return (error); 585 } 586 587 u_long udp_sendspace = 9216; /* really max datagram size */ 588 /* 40 1K datagrams */ 589 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 590 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 591 592 u_long udp_recvspace = 40 * (1024 + sizeof(struct sockaddr_in)); 593 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 594 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 595 596 static int 597 udp_abort(struct socket *so) 598 { 599 struct inpcb *inp; 600 int s; 601 602 inp = sotoinpcb(so); 603 if (inp == 0) 604 return EINVAL; /* ??? possible? panic instead? */ 605 soisdisconnected(so); 606 s = splnet(); 607 in_pcbdetach(inp); 608 splx(s); 609 return 0; 610 } 611 612 static int 613 udp_attach(struct socket *so, int proto, struct proc *p) 614 { 615 struct inpcb *inp; 616 int s, error; 617 618 inp = sotoinpcb(so); 619 if (inp != 0) 620 return EINVAL; 621 622 s = splnet(); 623 error = in_pcballoc(so, &udbinfo, p); 624 splx(s); 625 if (error) 626 return error; 627 error = soreserve(so, udp_sendspace, udp_recvspace); 628 if (error) 629 return error; 630 ((struct inpcb *) so->so_pcb)->inp_ip_ttl = ip_defttl; 631 return 0; 632 } 633 634 static int 635 udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) 636 { 637 struct inpcb *inp; 638 int s, error; 639 640 inp = sotoinpcb(so); 641 if (inp == 0) 642 return EINVAL; 643 s = splnet(); 644 error = in_pcbbind(inp, nam, p); 645 splx(s); 646 return error; 647 } 648 649 static int 650 udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) 651 { 652 struct inpcb *inp; 653 int s, error; 654 struct sockaddr_in *sin; 655 656 inp = sotoinpcb(so); 657 if (inp == 0) 658 return EINVAL; 659 if (inp->inp_faddr.s_addr != INADDR_ANY) 660 return EISCONN; 661 s = splnet(); 662 sin = (struct sockaddr_in *)nam; 663 prison_remote_ip(p, 0, &sin->sin_addr.s_addr); 664 error = in_pcbconnect(inp, nam, p); 665 splx(s); 666 if (error == 0) 667 soisconnected(so); 668 return error; 669 } 670 671 static int 672 udp_detach(struct socket *so) 673 { 674 struct inpcb *inp; 675 int s; 676 677 inp = sotoinpcb(so); 678 if (inp == 0) 679 return EINVAL; 680 s = splnet(); 681 in_pcbdetach(inp); 682 splx(s); 683 return 0; 684 } 685 686 static int 687 udp_disconnect(struct socket *so) 688 { 689 struct inpcb *inp; 690 int s; 691 692 inp = sotoinpcb(so); 693 if (inp == 0) 694 return EINVAL; 695 if (inp->inp_faddr.s_addr == INADDR_ANY) 696 return ENOTCONN; 697 698 s = splnet(); 699 in_pcbdisconnect(inp); 700 inp->inp_laddr.s_addr = INADDR_ANY; 701 splx(s); 702 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 703 return 0; 704 } 705 706 static int 707 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 708 struct mbuf *control, struct proc *p) 709 { 710 struct inpcb *inp; 711 712 inp = sotoinpcb(so); 713 if (inp == 0) { 714 m_freem(m); 715 return EINVAL; 716 } 717 return udp_output(inp, m, addr, control, p); 718 } 719 720 int 721 udp_shutdown(struct socket *so) 722 { 723 struct inpcb *inp; 724 725 inp = sotoinpcb(so); 726 if (inp == 0) 727 return EINVAL; 728 socantsendmore(so); 729 return 0; 730 } 731 732 struct pr_usrreqs udp_usrreqs = { 733 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 734 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 735 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, 736 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, 737 in_setsockaddr, sosend, soreceive, sopoll 738 }; 739 740