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