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