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) 1982, 1986, 1988, 1990, 1993, 1995 36 * The Regents of the University of California. All rights reserved. 37 * 38 * Redistribution and use in source and binary forms, with or without 39 * modification, are permitted provided that the following conditions 40 * are met: 41 * 1. Redistributions of source code must retain the above copyright 42 * notice, this list of conditions and the following disclaimer. 43 * 2. Redistributions in binary form must reproduce the above copyright 44 * notice, this list of conditions and the following disclaimer in the 45 * documentation and/or other materials provided with the distribution. 46 * 3. Neither the name of the University nor the names of its contributors 47 * may be used to endorse or promote products derived from this software 48 * without specific prior written permission. 49 * 50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 60 * SUCH DAMAGE. 61 * 62 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 63 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $ 64 */ 65 66 #include "opt_ipsec.h" 67 #include "opt_inet6.h" 68 69 #include <sys/param.h> 70 #include <sys/systm.h> 71 #include <sys/kernel.h> 72 #include <sys/malloc.h> 73 #include <sys/mbuf.h> 74 #include <sys/domain.h> 75 #include <sys/proc.h> 76 #include <sys/priv.h> 77 #include <sys/protosw.h> 78 #include <sys/socket.h> 79 #include <sys/socketvar.h> 80 #include <sys/sysctl.h> 81 #include <sys/syslog.h> 82 #include <sys/in_cksum.h> 83 84 #include <sys/thread2.h> 85 #include <sys/socketvar2.h> 86 #include <sys/serialize.h> 87 88 #include <machine/stdarg.h> 89 90 #include <net/if.h> 91 #include <net/route.h> 92 #include <net/netmsg2.h> 93 #include <net/netisr2.h> 94 95 #include <netinet/in.h> 96 #include <netinet/in_systm.h> 97 #include <netinet/ip.h> 98 #ifdef INET6 99 #include <netinet/ip6.h> 100 #endif 101 #include <netinet/in_pcb.h> 102 #include <netinet/in_var.h> 103 #include <netinet/ip_var.h> 104 #ifdef INET6 105 #include <netinet6/ip6_var.h> 106 #endif 107 #include <netinet/ip_icmp.h> 108 #include <netinet/icmp_var.h> 109 #include <netinet/udp.h> 110 #include <netinet/udp_var.h> 111 112 #ifdef FAST_IPSEC 113 #include <netproto/ipsec/ipsec.h> 114 #endif 115 116 #ifdef IPSEC 117 #include <netinet6/ipsec.h> 118 #endif 119 120 /* 121 * UDP protocol implementation. 122 * Per RFC 768, August, 1980. 123 */ 124 #ifndef COMPAT_42 125 static int udpcksum = 1; 126 #else 127 static int udpcksum = 0; /* XXX */ 128 #endif 129 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 130 &udpcksum, 0, "Enable checksumming of UDP packets"); 131 132 int log_in_vain = 0; 133 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 134 &log_in_vain, 0, "Log all incoming UDP packets"); 135 136 static int blackhole = 0; 137 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 138 &blackhole, 0, "Do not send port unreachables for refused connects"); 139 140 static int strict_mcast_mship = 1; 141 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, 142 &strict_mcast_mship, 0, "Only send multicast to member sockets"); 143 144 int udp_sosend_async = 1; 145 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_async, CTLFLAG_RW, 146 &udp_sosend_async, 0, "UDP asynchronized pru_send"); 147 148 int udp_sosend_prepend = 1; 149 SYSCTL_INT(_net_inet_udp, OID_AUTO, sosend_prepend, CTLFLAG_RW, 150 &udp_sosend_prepend, 0, 151 "Prepend enough space for proto and link header in pru_send"); 152 153 static int udp_reuseport_ext = 1; 154 SYSCTL_INT(_net_inet_udp, OID_AUTO, reuseport_ext, CTLFLAG_RW, 155 &udp_reuseport_ext, 0, "SO_REUSEPORT extension"); 156 157 struct inpcbinfo udbinfo; 158 159 static struct netisr_barrier *udbinfo_br; 160 static struct lwkt_serialize udbinfo_slize = LWKT_SERIALIZE_INITIALIZER; 161 162 #ifndef UDBHASHSIZE 163 #define UDBHASHSIZE 16 164 #endif 165 166 struct udpstat udpstat_percpu[MAXCPU] __cachealign; 167 168 #ifdef INET6 169 struct udp_in6 { 170 struct sockaddr_in6 uin6_sin; 171 u_char uin6_init_done : 1; 172 }; 173 struct udp_ip6 { 174 struct ip6_hdr uip6_ip6; 175 u_char uip6_init_done : 1; 176 }; 177 #else 178 struct udp_in6; 179 struct udp_ip6; 180 #endif /* INET6 */ 181 182 static void udp_append (struct inpcb *last, struct ip *ip, 183 struct mbuf *n, int off, struct sockaddr_in *udp_in, 184 struct udp_in6 *, struct udp_ip6 *); 185 #ifdef INET6 186 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip); 187 #endif 188 189 static int udp_connect_oncpu(struct socket *so, struct thread *td, 190 struct sockaddr_in *sin, struct sockaddr_in *if_sin); 191 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *, 192 struct thread *, int); 193 194 void 195 udp_init(void) 196 { 197 int cpu; 198 199 in_pcbinfo_init(&udbinfo); 200 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 201 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 202 &udbinfo.porthashmask); 203 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB, 204 &udbinfo.wildcardhashmask); 205 udbinfo.localgrphashbase = hashinit(UDBHASHSIZE, M_PCB, 206 &udbinfo.localgrphashmask); 207 udbinfo.ipi_size = sizeof(struct inpcb); 208 209 udbinfo_br = netisr_barrier_create(); 210 211 /* 212 * Initialize UDP statistics counters for each CPU. 213 */ 214 for (cpu = 0; cpu < ncpus; ++cpu) 215 bzero(&udpstat_percpu[cpu], sizeof(struct udpstat)); 216 } 217 218 static int 219 sysctl_udpstat(SYSCTL_HANDLER_ARGS) 220 { 221 int cpu, error = 0; 222 223 for (cpu = 0; cpu < ncpus; ++cpu) { 224 if ((error = SYSCTL_OUT(req, &udpstat_percpu[cpu], 225 sizeof(struct udpstat)))) 226 break; 227 if ((error = SYSCTL_IN(req, &udpstat_percpu[cpu], 228 sizeof(struct udpstat)))) 229 break; 230 } 231 232 return (error); 233 } 234 SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, (CTLTYPE_OPAQUE | CTLFLAG_RW), 235 0, 0, sysctl_udpstat, "S,udpstat", "UDP statistics"); 236 237 /* 238 * Check multicast packets to make sure they are only sent to sockets with 239 * multicast memberships for the packet's destination address and arrival 240 * interface. Multicast packets to multicast-unaware sockets are also 241 * disallowed. 242 * 243 * Returns 0 if the packet is acceptable, -1 if it is not. 244 */ 245 static __inline int 246 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m) 247 { 248 int mshipno; 249 struct ip_moptions *mopt; 250 251 if (strict_mcast_mship == 0 || 252 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 253 return (0); 254 } 255 mopt = inp->inp_moptions; 256 if (mopt == NULL) 257 return (-1); 258 for (mshipno = 0; mshipno < mopt->imo_num_memberships; ++mshipno) { 259 struct in_multi *maddr = mopt->imo_membership[mshipno]; 260 261 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr && 262 m->m_pkthdr.rcvif == maddr->inm_ifp) { 263 return (0); 264 } 265 } 266 return (-1); 267 } 268 269 int 270 udp_input(struct mbuf **mp, int *offp, int proto) 271 { 272 struct sockaddr_in udp_in = { sizeof udp_in, AF_INET }; 273 #ifdef INET6 274 struct udp_in6 udp_in6 = { 275 { sizeof udp_in6.uin6_sin, AF_INET6 }, 0 276 }; 277 struct udp_ip6 udp_ip6; 278 #endif 279 280 int iphlen; 281 struct ip *ip; 282 struct udphdr *uh; 283 struct inpcb *inp; 284 struct mbuf *m; 285 struct mbuf *opts = NULL; 286 int len, off; 287 struct ip save_ip; 288 struct sockaddr *append_sa; 289 290 off = *offp; 291 m = *mp; 292 *mp = NULL; 293 294 iphlen = off; 295 udp_stat.udps_ipackets++; 296 297 /* 298 * Strip IP options, if any; should skip this, 299 * make available to user, and use on returned packets, 300 * but we don't yet have a way to check the checksum 301 * with options still present. 302 */ 303 if (iphlen > sizeof(struct ip)) { 304 ip_stripoptions(m); 305 iphlen = sizeof(struct ip); 306 } 307 308 /* 309 * IP and UDP headers are together in first mbuf. 310 * Already checked and pulled up in ip_demux(). 311 */ 312 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr), 313 ("UDP header not in one mbuf")); 314 315 ip = mtod(m, struct ip *); 316 uh = (struct udphdr *)((caddr_t)ip + iphlen); 317 318 /* destination port of 0 is illegal, based on RFC768. */ 319 if (uh->uh_dport == 0) 320 goto bad; 321 322 /* 323 * Make mbuf data length reflect UDP length. 324 * If not enough data to reflect UDP length, drop. 325 */ 326 len = ntohs((u_short)uh->uh_ulen); 327 if (ip->ip_len != len) { 328 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 329 udp_stat.udps_badlen++; 330 goto bad; 331 } 332 m_adj(m, len - ip->ip_len); 333 /* ip->ip_len = len; */ 334 } 335 /* 336 * Save a copy of the IP header in case we want restore it 337 * for sending an ICMP error message in response. 338 */ 339 save_ip = *ip; 340 341 /* 342 * Checksum extended UDP header and data. 343 */ 344 if (uh->uh_sum) { 345 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 346 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 347 uh->uh_sum = m->m_pkthdr.csum_data; 348 else 349 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 350 ip->ip_dst.s_addr, htonl((u_short)len + 351 m->m_pkthdr.csum_data + IPPROTO_UDP)); 352 uh->uh_sum ^= 0xffff; 353 } else { 354 char b[9]; 355 356 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 357 bzero(((struct ipovly *)ip)->ih_x1, 9); 358 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 359 uh->uh_sum = in_cksum(m, len + sizeof(struct ip)); 360 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 361 } 362 if (uh->uh_sum) { 363 udp_stat.udps_badsum++; 364 m_freem(m); 365 return(IPPROTO_DONE); 366 } 367 } else 368 udp_stat.udps_nosum++; 369 370 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 371 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 372 struct inpcb *last; 373 374 /* 375 * Deliver a multicast or broadcast datagram to *all* sockets 376 * for which the local and remote addresses and ports match 377 * those of the incoming datagram. This allows more than 378 * one process to receive multi/broadcasts on the same port. 379 * (This really ought to be done for unicast datagrams as 380 * well, but that would cause problems with existing 381 * applications that open both address-specific sockets and 382 * a wildcard socket listening to the same port -- they would 383 * end up receiving duplicates of every unicast datagram. 384 * Those applications open the multiple sockets to overcome an 385 * inadequacy of the UDP socket interface, but for backwards 386 * compatibility we avoid the problem here rather than 387 * fixing the interface. Maybe 4.5BSD will remedy this?) 388 */ 389 390 /* 391 * Construct sockaddr format source address. 392 */ 393 udp_in.sin_port = uh->uh_sport; 394 udp_in.sin_addr = ip->ip_src; 395 /* 396 * Locate pcb(s) for datagram. 397 * (Algorithm copied from raw_intr().) 398 */ 399 last = NULL; 400 #ifdef INET6 401 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 402 #endif 403 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) { 404 KKASSERT((inp->inp_flags & INP_PLACEMARKER) == 0); 405 #ifdef INET6 406 if (!(inp->inp_vflag & INP_IPV4)) 407 continue; 408 #endif 409 if (inp->inp_lport != uh->uh_dport) 410 continue; 411 if (inp->inp_laddr.s_addr != INADDR_ANY) { 412 if (inp->inp_laddr.s_addr != 413 ip->ip_dst.s_addr) 414 continue; 415 } 416 if (inp->inp_faddr.s_addr != INADDR_ANY) { 417 if (inp->inp_faddr.s_addr != 418 ip->ip_src.s_addr || 419 inp->inp_fport != uh->uh_sport) 420 continue; 421 } 422 423 if (check_multicast_membership(ip, inp, m) < 0) 424 continue; 425 426 if (last != NULL) { 427 struct mbuf *n; 428 429 #ifdef IPSEC 430 /* check AH/ESP integrity. */ 431 if (ipsec4_in_reject_so(m, last->inp_socket)) 432 ipsecstat.in_polvio++; 433 /* do not inject data to pcb */ 434 else 435 #endif /*IPSEC*/ 436 #ifdef FAST_IPSEC 437 /* check AH/ESP integrity. */ 438 if (ipsec4_in_reject(m, last)) 439 ; 440 else 441 #endif /*FAST_IPSEC*/ 442 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) 443 udp_append(last, ip, n, 444 iphlen + sizeof(struct udphdr), 445 &udp_in, 446 #ifdef INET6 447 &udp_in6, &udp_ip6 448 #else 449 NULL, NULL 450 #endif 451 ); 452 } 453 last = inp; 454 /* 455 * Don't look for additional matches if this one does 456 * not have either the SO_REUSEPORT or SO_REUSEADDR 457 * socket options set. This heuristic avoids searching 458 * through all pcbs in the common case of a non-shared 459 * port. It * assumes that an application will never 460 * clear these options after setting them. 461 */ 462 if (!(last->inp_socket->so_options & 463 (SO_REUSEPORT | SO_REUSEADDR))) 464 break; 465 } 466 467 if (last == NULL) { 468 /* 469 * No matching pcb found; discard datagram. 470 * (No need to send an ICMP Port Unreachable 471 * for a broadcast or multicast datgram.) 472 */ 473 udp_stat.udps_noportbcast++; 474 goto bad; 475 } 476 #ifdef IPSEC 477 /* check AH/ESP integrity. */ 478 if (ipsec4_in_reject_so(m, last->inp_socket)) { 479 ipsecstat.in_polvio++; 480 goto bad; 481 } 482 #endif /*IPSEC*/ 483 #ifdef FAST_IPSEC 484 /* check AH/ESP integrity. */ 485 if (ipsec4_in_reject(m, last)) 486 goto bad; 487 #endif /*FAST_IPSEC*/ 488 udp_append(last, ip, m, iphlen + sizeof(struct udphdr), 489 &udp_in, 490 #ifdef INET6 491 &udp_in6, &udp_ip6 492 #else 493 NULL, NULL 494 #endif 495 ); 496 return(IPPROTO_DONE); 497 } 498 /* 499 * Locate pcb for datagram. 500 */ 501 inp = in_pcblookup_pkthash(&udbinfo, ip->ip_src, uh->uh_sport, 502 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif, 503 udp_reuseport_ext ? m : NULL); 504 if (inp == NULL) { 505 if (log_in_vain) { 506 char buf[sizeof "aaa.bbb.ccc.ddd"]; 507 508 strcpy(buf, inet_ntoa(ip->ip_dst)); 509 log(LOG_INFO, 510 "Connection attempt to UDP %s:%d from %s:%d\n", 511 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 512 ntohs(uh->uh_sport)); 513 } 514 udp_stat.udps_noport++; 515 if (m->m_flags & (M_BCAST | M_MCAST)) { 516 udp_stat.udps_noportbcast++; 517 goto bad; 518 } 519 if (blackhole) 520 goto bad; 521 #ifdef ICMP_BANDLIM 522 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 523 goto bad; 524 #endif 525 *ip = save_ip; 526 ip->ip_len += iphlen; 527 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 528 return(IPPROTO_DONE); 529 } 530 #ifdef IPSEC 531 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 532 ipsecstat.in_polvio++; 533 goto bad; 534 } 535 #endif /*IPSEC*/ 536 #ifdef FAST_IPSEC 537 if (ipsec4_in_reject(m, inp)) 538 goto bad; 539 #endif /*FAST_IPSEC*/ 540 /* 541 * Check the minimum TTL for socket. 542 */ 543 if (ip->ip_ttl < inp->inp_ip_minttl) 544 goto bad; 545 546 /* 547 * Construct sockaddr format source address. 548 * Stuff source address and datagram in user buffer. 549 */ 550 udp_in.sin_port = uh->uh_sport; 551 udp_in.sin_addr = ip->ip_src; 552 if ((inp->inp_flags & INP_CONTROLOPTS) || 553 (inp->inp_socket->so_options & SO_TIMESTAMP)) { 554 #ifdef INET6 555 if (inp->inp_vflag & INP_IPV6) { 556 int savedflags; 557 558 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 559 savedflags = inp->inp_flags; 560 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 561 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 562 inp->inp_flags = savedflags; 563 } else 564 #endif 565 ip_savecontrol(inp, &opts, ip, m); 566 } 567 m_adj(m, iphlen + sizeof(struct udphdr)); 568 #ifdef INET6 569 if (inp->inp_vflag & INP_IPV6) { 570 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 571 append_sa = (struct sockaddr *)&udp_in6; 572 } else 573 #endif 574 append_sa = (struct sockaddr *)&udp_in; 575 576 lwkt_gettoken(&inp->inp_socket->so_rcv.ssb_token); 577 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 578 udp_stat.udps_fullsock++; 579 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token); 580 goto bad; 581 } 582 lwkt_reltoken(&inp->inp_socket->so_rcv.ssb_token); 583 sorwakeup(inp->inp_socket); 584 return(IPPROTO_DONE); 585 bad: 586 m_freem(m); 587 if (opts) 588 m_freem(opts); 589 return(IPPROTO_DONE); 590 } 591 592 #ifdef INET6 593 static void 594 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip) 595 { 596 bzero(ip6, sizeof *ip6); 597 598 ip6->ip6_vfc = IPV6_VERSION; 599 ip6->ip6_plen = ip->ip_len; 600 ip6->ip6_nxt = ip->ip_p; 601 ip6->ip6_hlim = ip->ip_ttl; 602 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 603 IPV6_ADDR_INT32_SMP; 604 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 605 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 606 } 607 #endif 608 609 /* 610 * subroutine of udp_input(), mainly for source code readability. 611 * caller must properly init udp_ip6 and udp_in6 beforehand. 612 */ 613 static void 614 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, 615 struct sockaddr_in *udp_in, 616 struct udp_in6 *udp_in6, struct udp_ip6 *udp_ip6) 617 { 618 struct sockaddr *append_sa; 619 struct mbuf *opts = NULL; 620 621 if (last->inp_flags & INP_CONTROLOPTS || 622 last->inp_socket->so_options & SO_TIMESTAMP) { 623 #ifdef INET6 624 if (last->inp_vflag & INP_IPV6) { 625 int savedflags; 626 627 if (udp_ip6->uip6_init_done == 0) { 628 ip_2_ip6_hdr(&udp_ip6->uip6_ip6, ip); 629 udp_ip6->uip6_init_done = 1; 630 } 631 savedflags = last->inp_flags; 632 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 633 ip6_savecontrol(last, &opts, &udp_ip6->uip6_ip6, n); 634 last->inp_flags = savedflags; 635 } else 636 #endif 637 ip_savecontrol(last, &opts, ip, n); 638 } 639 #ifdef INET6 640 if (last->inp_vflag & INP_IPV6) { 641 if (udp_in6->uin6_init_done == 0) { 642 in6_sin_2_v4mapsin6(udp_in, &udp_in6->uin6_sin); 643 udp_in6->uin6_init_done = 1; 644 } 645 append_sa = (struct sockaddr *)&udp_in6->uin6_sin; 646 } else 647 #endif 648 append_sa = (struct sockaddr *)udp_in; 649 m_adj(n, off); 650 lwkt_gettoken(&last->inp_socket->so_rcv.ssb_token); 651 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 652 m_freem(n); 653 if (opts) 654 m_freem(opts); 655 udp_stat.udps_fullsock++; 656 } else { 657 sorwakeup(last->inp_socket); 658 } 659 lwkt_reltoken(&last->inp_socket->so_rcv.ssb_token); 660 } 661 662 /* 663 * Notify a udp user of an asynchronous error; 664 * just wake up so that he can collect error status. 665 */ 666 void 667 udp_notify(struct inpcb *inp, int error) 668 { 669 inp->inp_socket->so_error = error; 670 sorwakeup(inp->inp_socket); 671 sowwakeup(inp->inp_socket); 672 } 673 674 struct netmsg_udp_notify { 675 struct netmsg_base base; 676 void (*nm_notify)(struct inpcb *, int); 677 struct in_addr nm_faddr; 678 int nm_arg; 679 }; 680 681 static void 682 udp_notifyall_oncpu(netmsg_t msg) 683 { 684 struct netmsg_udp_notify *nm = (struct netmsg_udp_notify *)msg; 685 #if 0 686 int nextcpu; 687 #endif 688 689 in_pcbnotifyall(&udbinfo.pcblisthead, nm->nm_faddr, 690 nm->nm_arg, nm->nm_notify); 691 lwkt_replymsg(&nm->base.lmsg, 0); 692 693 #if 0 694 /* XXX currently udp only runs on cpu 0 */ 695 nextcpu = mycpuid + 1; 696 if (nextcpu < ncpus2) 697 lwkt_forwardmsg(netisr_cpuport(nextcpu), &nm->base.lmsg); 698 else 699 lwkt_replymsg(&nmsg->base.lmsg, 0); 700 #endif 701 } 702 703 static void 704 udp_rtchange(struct inpcb *inp, int err) 705 { 706 /* XXX Nuke this, once UDP inpcbs are CPU localized */ 707 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) { 708 rtfree(inp->inp_route.ro_rt); 709 inp->inp_route.ro_rt = NULL; 710 /* 711 * A new route can be allocated the next time 712 * output is attempted. 713 */ 714 } 715 } 716 717 void 718 udp_ctlinput(netmsg_t msg) 719 { 720 struct sockaddr *sa = msg->ctlinput.nm_arg; 721 struct ip *ip = msg->ctlinput.nm_extra; 722 int cmd = msg->ctlinput.nm_cmd; 723 struct udphdr *uh; 724 void (*notify) (struct inpcb *, int) = udp_notify; 725 struct in_addr faddr; 726 struct inpcb *inp; 727 728 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 729 730 faddr = ((struct sockaddr_in *)sa)->sin_addr; 731 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 732 goto done; 733 734 if (PRC_IS_REDIRECT(cmd)) { 735 ip = NULL; 736 notify = udp_rtchange; 737 } else if (cmd == PRC_HOSTDEAD) { 738 ip = NULL; 739 } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { 740 goto done; 741 } 742 743 if (ip) { 744 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 745 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 746 ip->ip_src, uh->uh_sport, 0, NULL); 747 if (inp != NULL && inp->inp_socket != NULL) 748 (*notify)(inp, inetctlerrmap[cmd]); 749 } else if (PRC_IS_REDIRECT(cmd)) { 750 struct netmsg_udp_notify *nm; 751 752 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 753 nm = kmalloc(sizeof(*nm), M_LWKTMSG, M_INTWAIT); 754 netmsg_init(&nm->base, NULL, &netisr_afree_rport, 755 0, udp_notifyall_oncpu); 756 nm->nm_faddr = faddr; 757 nm->nm_arg = inetctlerrmap[cmd]; 758 nm->nm_notify = notify; 759 lwkt_sendmsg(netisr_cpuport(0), &nm->base.lmsg); 760 } else { 761 /* 762 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL, 763 * once UDP inpcbs are CPU localized 764 */ 765 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 766 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd], 767 notify); 768 } 769 done: 770 lwkt_replymsg(&msg->lmsg, 0); 771 } 772 773 static int 774 udp_pcblist(SYSCTL_HANDLER_ARGS) 775 { 776 struct xinpcb *xi; 777 int error, nxi, i; 778 779 udbinfo_lock(); 780 error = in_pcblist_global_nomarker(oidp, arg1, arg2, req, &xi, &nxi); 781 udbinfo_unlock(); 782 783 if (error) { 784 KKASSERT(xi == NULL); 785 return error; 786 } 787 if (nxi == 0) { 788 KKASSERT(xi == NULL); 789 return 0; 790 } 791 792 for (i = 0; i < nxi; ++i) { 793 error = SYSCTL_OUT(req, &xi[i], sizeof(xi[i])); 794 if (error) 795 break; 796 } 797 kfree(xi, M_TEMP); 798 799 return error; 800 } 801 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0, 802 udp_pcblist, "S,xinpcb", "List of active UDP sockets"); 803 804 static int 805 udp_getcred(SYSCTL_HANDLER_ARGS) 806 { 807 struct sockaddr_in addrs[2]; 808 struct ucred cred0, *cred = NULL; 809 struct inpcb *inp; 810 int error; 811 812 error = priv_check(req->td, PRIV_ROOT); 813 if (error) 814 return (error); 815 error = SYSCTL_IN(req, addrs, sizeof addrs); 816 if (error) 817 return (error); 818 819 udbinfo_lock(); 820 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 821 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 822 if (inp == NULL || inp->inp_socket == NULL) { 823 error = ENOENT; 824 } else { 825 if (inp->inp_socket->so_cred != NULL) { 826 cred0 = *(inp->inp_socket->so_cred); 827 cred = &cred0; 828 } 829 } 830 udbinfo_unlock(); 831 832 if (error) 833 return error; 834 835 return SYSCTL_OUT(req, cred, sizeof(struct ucred)); 836 } 837 838 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 839 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 840 841 static int 842 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr, 843 struct thread *td, int flags) 844 { 845 struct udpiphdr *ui; 846 int len = m->m_pkthdr.len; 847 struct sockaddr_in *sin; /* really is initialized before use */ 848 int error = 0, lport_any = 0; 849 850 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 851 error = EMSGSIZE; 852 goto release; 853 } 854 855 if (inp->inp_lport == 0) { /* unbound socket */ 856 error = in_pcbbind(inp, NULL, td); 857 if (error) 858 goto release; 859 860 udbinfo_barrier_set(); 861 in_pcbinswildcardhash(inp); 862 udbinfo_barrier_rem(); 863 lport_any = 1; 864 } 865 866 if (dstaddr != NULL) { /* destination address specified */ 867 if (inp->inp_faddr.s_addr != INADDR_ANY) { 868 /* already connected */ 869 error = EISCONN; 870 goto release; 871 } 872 sin = (struct sockaddr_in *)dstaddr; 873 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) { 874 error = EAFNOSUPPORT; /* IPv6 only jail */ 875 goto release; 876 } 877 } else { 878 if (inp->inp_faddr.s_addr == INADDR_ANY) { 879 /* no destination specified and not already connected */ 880 error = ENOTCONN; 881 goto release; 882 } 883 sin = NULL; 884 } 885 886 /* 887 * Calculate data length and get a mbuf 888 * for UDP and IP headers. 889 */ 890 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT); 891 if (m == NULL) { 892 error = ENOBUFS; 893 goto release; 894 } 895 896 /* 897 * Fill in mbuf with extended UDP header 898 * and addresses and length put into network format. 899 */ 900 ui = mtod(m, struct udpiphdr *); 901 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */ 902 ui->ui_pr = IPPROTO_UDP; 903 904 /* 905 * Set destination address. 906 */ 907 if (dstaddr != NULL) { /* use specified destination */ 908 ui->ui_dst = sin->sin_addr; 909 ui->ui_dport = sin->sin_port; 910 } else { /* use connected destination */ 911 ui->ui_dst = inp->inp_faddr; 912 ui->ui_dport = inp->inp_fport; 913 } 914 915 /* 916 * Set source address. 917 */ 918 if (inp->inp_laddr.s_addr == INADDR_ANY || 919 IN_MULTICAST(ntohl(inp->inp_laddr.s_addr))) { 920 struct sockaddr_in *if_sin; 921 922 if (dstaddr == NULL) { 923 /* 924 * connect() had (or should have) failed because 925 * the interface had no IP address, but the 926 * application proceeded to call send() anyways. 927 */ 928 error = ENOTCONN; 929 goto release; 930 } 931 932 /* Look up outgoing interface. */ 933 error = in_pcbladdr_find(inp, dstaddr, &if_sin, td, 1); 934 if (error) 935 goto release; 936 ui->ui_src = if_sin->sin_addr; /* use address of interface */ 937 } else { 938 ui->ui_src = inp->inp_laddr; /* use non-null bound address */ 939 } 940 ui->ui_sport = inp->inp_lport; 941 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound")); 942 943 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 944 945 /* 946 * Set up checksum and output datagram. 947 */ 948 if (udpcksum) { 949 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 950 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 951 m->m_pkthdr.csum_flags = CSUM_UDP; 952 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 953 m->m_pkthdr.csum_thlen = sizeof(struct udphdr); 954 } else { 955 ui->ui_sum = 0; 956 } 957 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len; 958 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 959 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 960 udp_stat.udps_opackets++; 961 962 error = ip_output(m, inp->inp_options, &inp->inp_route, 963 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) | 964 flags | IP_DEBUGROUTE, 965 inp->inp_moptions, inp); 966 967 /* 968 * If this is the first data gram sent on an unbound and unconnected 969 * UDP socket, lport will be changed in this function. If target 970 * CPU after this lport changing is no longer the current CPU, then 971 * free the route entry allocated on the current CPU. 972 */ 973 if (lport_any) { 974 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport, 975 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) { 976 #ifdef notyet 977 struct route *ro = &inp->inp_route; 978 979 if (ro->ro_rt != NULL) 980 RTFREE(ro->ro_rt); 981 bzero(ro, sizeof(*ro)); 982 #else 983 panic("UDP activity should only be in netisr0"); 984 #endif 985 } 986 } 987 return (error); 988 989 release: 990 m_freem(m); 991 return (error); 992 } 993 994 u_long udp_sendspace = 9216; /* really max datagram size */ 995 /* 40 1K datagrams */ 996 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 997 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 998 999 u_long udp_recvspace = 40 * (1024 + 1000 #ifdef INET6 1001 sizeof(struct sockaddr_in6) 1002 #else 1003 sizeof(struct sockaddr_in) 1004 #endif 1005 ); 1006 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 1007 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 1008 1009 /* 1010 * NOTE: (so) is referenced from soabort*() and netmsg_pru_abort() 1011 * will sofree() it when we return. 1012 */ 1013 static void 1014 udp_abort(netmsg_t msg) 1015 { 1016 struct socket *so = msg->abort.base.nm_so; 1017 struct inpcb *inp; 1018 int error; 1019 1020 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1021 1022 inp = so->so_pcb; 1023 if (inp) { 1024 soisdisconnected(so); 1025 1026 udbinfo_barrier_set(); 1027 in_pcbdetach(inp); 1028 udbinfo_barrier_rem(); 1029 error = 0; 1030 } else { 1031 error = EINVAL; 1032 } 1033 lwkt_replymsg(&msg->abort.base.lmsg, error); 1034 } 1035 1036 static void 1037 udp_attach(netmsg_t msg) 1038 { 1039 struct socket *so = msg->attach.base.nm_so; 1040 struct pru_attach_info *ai = msg->attach.nm_ai; 1041 struct inpcb *inp; 1042 int error; 1043 1044 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1045 1046 inp = so->so_pcb; 1047 if (inp != NULL) { 1048 error = EINVAL; 1049 goto out; 1050 } 1051 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit); 1052 if (error) 1053 goto out; 1054 1055 udbinfo_barrier_set(); 1056 error = in_pcballoc(so, &udbinfo); 1057 udbinfo_barrier_rem(); 1058 1059 if (error) 1060 goto out; 1061 1062 /* 1063 * Set default port for protocol processing prior to bind/connect. 1064 */ 1065 sosetport(so, netisr_cpuport(0)); 1066 1067 inp = (struct inpcb *)so->so_pcb; 1068 inp->inp_vflag |= INP_IPV4; 1069 inp->inp_ip_ttl = ip_defttl; 1070 error = 0; 1071 out: 1072 lwkt_replymsg(&msg->attach.base.lmsg, error); 1073 } 1074 1075 static void 1076 udp_bind(netmsg_t msg) 1077 { 1078 struct socket *so = msg->bind.base.nm_so; 1079 struct sockaddr *nam = msg->bind.nm_nam; 1080 struct thread *td = msg->bind.nm_td; 1081 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 1082 struct inpcb *inp; 1083 int error; 1084 1085 inp = so->so_pcb; 1086 if (inp) { 1087 error = in_pcbbind(inp, nam, td); 1088 if (error == 0) { 1089 if (sin->sin_addr.s_addr != INADDR_ANY) 1090 inp->inp_flags |= INP_WASBOUND_NOTANY; 1091 1092 udbinfo_barrier_set(); 1093 in_pcbinswildcardhash(inp); 1094 udbinfo_barrier_rem(); 1095 } 1096 } else { 1097 error = EINVAL; 1098 } 1099 lwkt_replymsg(&msg->bind.base.lmsg, error); 1100 } 1101 1102 static void 1103 udp_connect(netmsg_t msg) 1104 { 1105 struct socket *so = msg->connect.base.nm_so; 1106 struct sockaddr *nam = msg->connect.nm_nam; 1107 struct thread *td = msg->connect.nm_td; 1108 struct inpcb *inp; 1109 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 1110 struct sockaddr_in *if_sin; 1111 lwkt_port_t port; 1112 int error; 1113 1114 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1115 1116 inp = so->so_pcb; 1117 if (inp == NULL) { 1118 error = EINVAL; 1119 goto out; 1120 } 1121 1122 if (msg->connect.nm_reconnect & NMSG_RECONNECT_RECONNECT) { 1123 panic("UDP does not support RECONNECT"); 1124 #ifdef notyet 1125 msg->connect.nm_reconnect &= ~NMSG_RECONNECT_RECONNECT; 1126 in_pcblink(inp, &udbinfo); 1127 #endif 1128 } 1129 1130 if (inp->inp_faddr.s_addr != INADDR_ANY) { 1131 error = EISCONN; 1132 goto out; 1133 } 1134 error = 0; 1135 1136 /* 1137 * Bind if we have to 1138 */ 1139 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL && 1140 inp->inp_laddr.s_addr == INADDR_ANY) { 1141 error = in_pcbbind(inp, NULL, td); 1142 if (error) 1143 goto out; 1144 } 1145 1146 /* 1147 * Calculate the correct protocol processing thread. The connect 1148 * operation must run there. 1149 */ 1150 error = in_pcbladdr(inp, nam, &if_sin, td); 1151 if (error) 1152 goto out; 1153 if (!prison_remote_ip(td, nam)) { 1154 error = EAFNOSUPPORT; /* IPv6 only jail */ 1155 goto out; 1156 } 1157 1158 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port, 1159 inp->inp_laddr.s_addr, inp->inp_lport); 1160 if (port != &curthread->td_msgport) { 1161 #ifdef notyet 1162 struct route *ro = &inp->inp_route; 1163 1164 /* 1165 * in_pcbladdr() may have allocated a route entry for us 1166 * on the current CPU, but we need a route entry on the 1167 * inpcb's owner CPU, so free it here. 1168 */ 1169 if (ro->ro_rt != NULL) 1170 RTFREE(ro->ro_rt); 1171 bzero(ro, sizeof(*ro)); 1172 1173 /* 1174 * We are moving the protocol processing port the socket 1175 * is on, we have to unlink here and re-link on the 1176 * target cpu. 1177 */ 1178 in_pcbunlink(so->so_pcb, &udbinfo); 1179 /* in_pcbunlink(so->so_pcb, &udbinfo[mycpu->gd_cpuid]); */ 1180 sosetport(so, port); 1181 msg->connect.nm_reconnect |= NMSG_RECONNECT_RECONNECT; 1182 msg->connect.base.nm_dispatch = udp_connect; 1183 1184 lwkt_forwardmsg(port, &msg->connect.base.lmsg); 1185 /* msg invalid now */ 1186 return; 1187 #else 1188 panic("UDP activity should only be in netisr0"); 1189 #endif 1190 } 1191 KKASSERT(port == &curthread->td_msgport); 1192 error = udp_connect_oncpu(so, td, sin, if_sin); 1193 out: 1194 KKASSERT(msg->connect.nm_m == NULL); 1195 lwkt_replymsg(&msg->connect.base.lmsg, error); 1196 } 1197 1198 static int 1199 udp_connect_oncpu(struct socket *so, struct thread *td, 1200 struct sockaddr_in *sin, struct sockaddr_in *if_sin) 1201 { 1202 struct inpcb *inp; 1203 int error; 1204 1205 udbinfo_barrier_set(); 1206 1207 inp = so->so_pcb; 1208 if (inp->inp_flags & INP_WILDCARD) 1209 in_pcbremwildcardhash(inp); 1210 error = in_pcbconnect(inp, (struct sockaddr *)sin, td); 1211 1212 if (error == 0) { 1213 /* 1214 * No more errors can occur, finish adjusting the socket 1215 * and change the processing port to reflect the connected 1216 * socket. Once set we can no longer safely mess with the 1217 * socket. 1218 */ 1219 soisconnected(so); 1220 } else if (error == EAFNOSUPPORT) { /* connection dissolved */ 1221 /* 1222 * Follow traditional BSD behavior and retain 1223 * the local port binding. But, fix the old misbehavior 1224 * of overwriting any previously bound local address. 1225 */ 1226 if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) 1227 inp->inp_laddr.s_addr = INADDR_ANY; 1228 in_pcbinswildcardhash(inp); 1229 } 1230 1231 udbinfo_barrier_rem(); 1232 return error; 1233 } 1234 1235 static void 1236 udp_detach(netmsg_t msg) 1237 { 1238 struct socket *so = msg->detach.base.nm_so; 1239 struct inpcb *inp; 1240 int error; 1241 1242 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1243 1244 inp = so->so_pcb; 1245 if (inp) { 1246 udbinfo_barrier_set(); 1247 in_pcbdetach(inp); 1248 udbinfo_barrier_rem(); 1249 error = 0; 1250 } else { 1251 error = EINVAL; 1252 } 1253 lwkt_replymsg(&msg->detach.base.lmsg, error); 1254 } 1255 1256 static void 1257 udp_disconnect(netmsg_t msg) 1258 { 1259 struct socket *so = msg->disconnect.base.nm_so; 1260 struct route *ro; 1261 struct inpcb *inp; 1262 int error; 1263 1264 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1265 1266 inp = so->so_pcb; 1267 if (inp == NULL) { 1268 error = EINVAL; 1269 goto out; 1270 } 1271 if (inp->inp_faddr.s_addr == INADDR_ANY) { 1272 error = ENOTCONN; 1273 goto out; 1274 } 1275 1276 soreference(so); 1277 1278 udbinfo_barrier_set(); 1279 in_pcbdisconnect(inp); 1280 udbinfo_barrier_rem(); 1281 1282 soclrstate(so, SS_ISCONNECTED); /* XXX */ 1283 sofree(so); 1284 1285 ro = &inp->inp_route; 1286 if (ro->ro_rt != NULL) 1287 RTFREE(ro->ro_rt); 1288 bzero(ro, sizeof(*ro)); 1289 error = 0; 1290 out: 1291 lwkt_replymsg(&msg->disconnect.base.lmsg, error); 1292 } 1293 1294 static void 1295 udp_send(netmsg_t msg) 1296 { 1297 struct socket *so = msg->send.base.nm_so; 1298 struct mbuf *m = msg->send.nm_m; 1299 struct sockaddr *addr = msg->send.nm_addr; 1300 int pru_flags = msg->send.nm_flags; 1301 struct inpcb *inp; 1302 int error; 1303 1304 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1305 KKASSERT(msg->send.nm_control == NULL); 1306 1307 inp = so->so_pcb; 1308 if (inp) { 1309 struct thread *td = msg->send.nm_td; 1310 int flags = 0; 1311 1312 if (pru_flags & PRUS_DONTROUTE) 1313 flags |= SO_DONTROUTE; 1314 error = udp_output(inp, m, addr, td, flags); 1315 } else { 1316 m_freem(m); 1317 error = EINVAL; 1318 } 1319 1320 if (pru_flags & PRUS_FREEADDR) 1321 kfree(addr, M_SONAME); 1322 1323 if ((pru_flags & PRUS_NOREPLY) == 0) 1324 lwkt_replymsg(&msg->send.base.lmsg, error); 1325 } 1326 1327 void 1328 udp_shutdown(netmsg_t msg) 1329 { 1330 struct socket *so = msg->shutdown.base.nm_so; 1331 struct inpcb *inp; 1332 int error; 1333 1334 KKASSERT(&curthread->td_msgport == netisr_cpuport(0)); 1335 1336 inp = so->so_pcb; 1337 if (inp) { 1338 socantsendmore(so); 1339 error = 0; 1340 } else { 1341 error = EINVAL; 1342 } 1343 lwkt_replymsg(&msg->shutdown.base.lmsg, error); 1344 } 1345 1346 void 1347 udbinfo_lock(void) 1348 { 1349 lwkt_serialize_enter(&udbinfo_slize); 1350 } 1351 1352 void 1353 udbinfo_unlock(void) 1354 { 1355 lwkt_serialize_exit(&udbinfo_slize); 1356 } 1357 1358 void 1359 udbinfo_barrier_set(void) 1360 { 1361 netisr_barrier_set(udbinfo_br); 1362 udbinfo_lock(); 1363 } 1364 1365 void 1366 udbinfo_barrier_rem(void) 1367 { 1368 udbinfo_unlock(); 1369 netisr_barrier_rem(udbinfo_br); 1370 } 1371 1372 struct pr_usrreqs udp_usrreqs = { 1373 .pru_abort = udp_abort, 1374 .pru_accept = pr_generic_notsupp, 1375 .pru_attach = udp_attach, 1376 .pru_bind = udp_bind, 1377 .pru_connect = udp_connect, 1378 .pru_connect2 = pr_generic_notsupp, 1379 .pru_control = in_control_dispatch, 1380 .pru_detach = udp_detach, 1381 .pru_disconnect = udp_disconnect, 1382 .pru_listen = pr_generic_notsupp, 1383 .pru_peeraddr = in_setpeeraddr_dispatch, 1384 .pru_rcvd = pr_generic_notsupp, 1385 .pru_rcvoob = pr_generic_notsupp, 1386 .pru_send = udp_send, 1387 .pru_sense = pru_sense_null, 1388 .pru_shutdown = udp_shutdown, 1389 .pru_sockaddr = in_setsockaddr_dispatch, 1390 .pru_sosend = sosendudp, 1391 .pru_soreceive = soreceive 1392 }; 1393 1394