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 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.47 2008/11/11 10:46:58 sephe Exp $ 69 */ 70 71 #include "opt_ipsec.h" 72 #include "opt_inet6.h" 73 74 #include <sys/param.h> 75 #include <sys/systm.h> 76 #include <sys/kernel.h> 77 #include <sys/malloc.h> 78 #include <sys/mbuf.h> 79 #include <sys/domain.h> 80 #include <sys/proc.h> 81 #include <sys/priv.h> 82 #include <sys/protosw.h> 83 #include <sys/socket.h> 84 #include <sys/socketvar.h> 85 #include <sys/sysctl.h> 86 #include <sys/syslog.h> 87 #include <sys/thread2.h> 88 #include <sys/in_cksum.h> 89 90 #include <machine/stdarg.h> 91 92 #include <vm/vm_zone.h> 93 94 #include <net/if.h> 95 #include <net/route.h> 96 #include <net/netmsg2.h> 97 98 #include <netinet/in.h> 99 #include <netinet/in_systm.h> 100 #include <netinet/ip.h> 101 #ifdef INET6 102 #include <netinet/ip6.h> 103 #endif 104 #include <netinet/in_pcb.h> 105 #include <netinet/in_var.h> 106 #include <netinet/ip_var.h> 107 #ifdef INET6 108 #include <netinet6/ip6_var.h> 109 #endif 110 #include <netinet/ip_icmp.h> 111 #include <netinet/icmp_var.h> 112 #include <netinet/udp.h> 113 #include <netinet/udp_var.h> 114 115 #ifdef FAST_IPSEC 116 #include <netproto/ipsec/ipsec.h> 117 #endif 118 119 #ifdef IPSEC 120 #include <netinet6/ipsec.h> 121 #endif 122 123 int udp_mpsafe_proto = 0; 124 TUNABLE_INT("net.inet.udp.mpsafe_proto", &udp_mpsafe_proto); 125 126 int udp_mpsafe_thread = NETMSG_SERVICE_ADAPTIVE; 127 TUNABLE_INT("net.inet.udp.mpsafe_thread", &udp_mpsafe_thread); 128 SYSCTL_INT(_net_inet_udp, OID_AUTO, mpsafe_thread, CTLFLAG_RW, 129 &udp_mpsafe_thread, 0, 130 "0:BGL, 1:Adaptive BGL, 2:No BGL(experimental)"); 131 132 /* 133 * UDP protocol implementation. 134 * Per RFC 768, August, 1980. 135 */ 136 #ifndef COMPAT_42 137 static int udpcksum = 1; 138 #else 139 static int udpcksum = 0; /* XXX */ 140 #endif 141 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, 142 &udpcksum, 0, ""); 143 144 int log_in_vain = 0; 145 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 146 &log_in_vain, 0, "Log all incoming UDP packets"); 147 148 static int blackhole = 0; 149 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, 150 &blackhole, 0, "Do not send port unreachables for refused connects"); 151 152 static int strict_mcast_mship = 1; 153 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW, 154 &strict_mcast_mship, 0, "Only send multicast to member sockets"); 155 156 struct inpcbinfo udbinfo; 157 158 #ifndef UDBHASHSIZE 159 #define UDBHASHSIZE 16 160 #endif 161 162 struct udpstat udpstat; /* from udp_var.h */ 163 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, 164 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); 165 166 static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET }; 167 #ifdef INET6 168 struct udp_in6 { 169 struct sockaddr_in6 uin6_sin; 170 u_char uin6_init_done : 1; 171 } udp_in6 = { 172 { sizeof udp_in6.uin6_sin, AF_INET6 }, 173 0 174 }; 175 struct udp_ip6 { 176 struct ip6_hdr uip6_ip6; 177 u_char uip6_init_done : 1; 178 } udp_ip6; 179 #endif /* INET6 */ 180 181 static void udp_append (struct inpcb *last, struct ip *ip, 182 struct mbuf *n, int off); 183 #ifdef INET6 184 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip); 185 #endif 186 187 static int udp_connect_oncpu(struct socket *so, struct thread *td, 188 struct sockaddr_in *sin, struct sockaddr_in *if_sin); 189 static int udp_detach (struct socket *so); 190 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *, 191 struct mbuf *, struct thread *); 192 193 void 194 udp_init(void) 195 { 196 in_pcbinfo_init(&udbinfo); 197 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); 198 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, 199 &udbinfo.porthashmask); 200 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB, 201 &udbinfo.wildcardhashmask); 202 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, 203 ZONE_INTERRUPT, 0); 204 udp_thread_init(); 205 } 206 207 /* 208 * Check multicast packets to make sure they are only sent to sockets with 209 * multicast memberships for the packet's destination address and arrival 210 * interface. Multicast packets to multicast-unaware sockets are also 211 * disallowed. 212 * 213 * Returns 0 if the packet is acceptable, -1 if it is not. 214 */ 215 static __inline int 216 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m) 217 { 218 int mshipno; 219 struct ip_moptions *mopt; 220 221 if (strict_mcast_mship == 0 || 222 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 223 return (0); 224 } 225 mopt = inp->inp_moptions; 226 if (mopt == NULL) 227 return (-1); 228 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) { 229 struct in_multi *maddr = mopt->imo_membership[mshipno]; 230 231 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr && 232 m->m_pkthdr.rcvif == maddr->inm_ifp) { 233 return (0); 234 } 235 } 236 return (-1); 237 } 238 239 void 240 udp_input(struct mbuf *m, ...) 241 { 242 int iphlen; 243 struct ip *ip; 244 struct udphdr *uh; 245 struct inpcb *inp; 246 struct mbuf *opts = NULL; 247 int len, off, proto; 248 struct ip save_ip; 249 struct sockaddr *append_sa; 250 __va_list ap; 251 252 __va_start(ap, m); 253 off = __va_arg(ap, int); 254 proto = __va_arg(ap, int); 255 __va_end(ap); 256 257 iphlen = off; 258 udpstat.udps_ipackets++; 259 260 /* 261 * Strip IP options, if any; should skip this, 262 * make available to user, and use on returned packets, 263 * but we don't yet have a way to check the checksum 264 * with options still present. 265 */ 266 if (iphlen > sizeof(struct ip)) { 267 ip_stripoptions(m); 268 iphlen = sizeof(struct ip); 269 } 270 271 /* 272 * IP and UDP headers are together in first mbuf. 273 * Already checked and pulled up in ip_demux(). 274 */ 275 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr), 276 ("UDP header not in one mbuf")); 277 278 ip = mtod(m, struct ip *); 279 uh = (struct udphdr *)((caddr_t)ip + iphlen); 280 281 /* destination port of 0 is illegal, based on RFC768. */ 282 if (uh->uh_dport == 0) 283 goto bad; 284 285 /* 286 * Make mbuf data length reflect UDP length. 287 * If not enough data to reflect UDP length, drop. 288 */ 289 len = ntohs((u_short)uh->uh_ulen); 290 if (ip->ip_len != len) { 291 if (len > ip->ip_len || len < sizeof(struct udphdr)) { 292 udpstat.udps_badlen++; 293 goto bad; 294 } 295 m_adj(m, len - ip->ip_len); 296 /* ip->ip_len = len; */ 297 } 298 /* 299 * Save a copy of the IP header in case we want restore it 300 * for sending an ICMP error message in response. 301 */ 302 save_ip = *ip; 303 304 /* 305 * Checksum extended UDP header and data. 306 */ 307 if (uh->uh_sum) { 308 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { 309 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) 310 uh->uh_sum = m->m_pkthdr.csum_data; 311 else 312 uh->uh_sum = in_pseudo(ip->ip_src.s_addr, 313 ip->ip_dst.s_addr, htonl((u_short)len + 314 m->m_pkthdr.csum_data + IPPROTO_UDP)); 315 uh->uh_sum ^= 0xffff; 316 } else { 317 char b[9]; 318 319 bcopy(((struct ipovly *)ip)->ih_x1, b, 9); 320 bzero(((struct ipovly *)ip)->ih_x1, 9); 321 ((struct ipovly *)ip)->ih_len = uh->uh_ulen; 322 uh->uh_sum = in_cksum(m, len + sizeof(struct ip)); 323 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9); 324 } 325 if (uh->uh_sum) { 326 udpstat.udps_badsum++; 327 m_freem(m); 328 return; 329 } 330 } else 331 udpstat.udps_nosum++; 332 333 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || 334 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { 335 struct inpcb *last; 336 337 /* 338 * Deliver a multicast or broadcast datagram to *all* sockets 339 * for which the local and remote addresses and ports match 340 * those of the incoming datagram. This allows more than 341 * one process to receive multi/broadcasts on the same port. 342 * (This really ought to be done for unicast datagrams as 343 * well, but that would cause problems with existing 344 * applications that open both address-specific sockets and 345 * a wildcard socket listening to the same port -- they would 346 * end up receiving duplicates of every unicast datagram. 347 * Those applications open the multiple sockets to overcome an 348 * inadequacy of the UDP socket interface, but for backwards 349 * compatibility we avoid the problem here rather than 350 * fixing the interface. Maybe 4.5BSD will remedy this?) 351 */ 352 353 /* 354 * Construct sockaddr format source address. 355 */ 356 udp_in.sin_port = uh->uh_sport; 357 udp_in.sin_addr = ip->ip_src; 358 /* 359 * Locate pcb(s) for datagram. 360 * (Algorithm copied from raw_intr().) 361 */ 362 last = NULL; 363 #ifdef INET6 364 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; 365 #endif 366 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) { 367 if (inp->inp_flags & INP_PLACEMARKER) 368 continue; 369 #ifdef INET6 370 if (!(inp->inp_vflag & INP_IPV4)) 371 continue; 372 #endif 373 if (inp->inp_lport != uh->uh_dport) 374 continue; 375 if (inp->inp_laddr.s_addr != INADDR_ANY) { 376 if (inp->inp_laddr.s_addr != 377 ip->ip_dst.s_addr) 378 continue; 379 } 380 if (inp->inp_faddr.s_addr != INADDR_ANY) { 381 if (inp->inp_faddr.s_addr != 382 ip->ip_src.s_addr || 383 inp->inp_fport != uh->uh_sport) 384 continue; 385 } 386 387 if (check_multicast_membership(ip, inp, m) < 0) 388 continue; 389 390 if (last != NULL) { 391 struct mbuf *n; 392 393 #ifdef IPSEC 394 /* check AH/ESP integrity. */ 395 if (ipsec4_in_reject_so(m, last->inp_socket)) 396 ipsecstat.in_polvio++; 397 /* do not inject data to pcb */ 398 else 399 #endif /*IPSEC*/ 400 #ifdef FAST_IPSEC 401 /* check AH/ESP integrity. */ 402 if (ipsec4_in_reject(m, last)) 403 ; 404 else 405 #endif /*FAST_IPSEC*/ 406 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL) 407 udp_append(last, ip, n, 408 iphlen + 409 sizeof(struct udphdr)); 410 } 411 last = inp; 412 /* 413 * Don't look for additional matches if this one does 414 * not have either the SO_REUSEPORT or SO_REUSEADDR 415 * socket options set. This heuristic avoids searching 416 * through all pcbs in the common case of a non-shared 417 * port. It * assumes that an application will never 418 * clear these options after setting them. 419 */ 420 if (!(last->inp_socket->so_options & 421 (SO_REUSEPORT | SO_REUSEADDR))) 422 break; 423 } 424 425 if (last == NULL) { 426 /* 427 * No matching pcb found; discard datagram. 428 * (No need to send an ICMP Port Unreachable 429 * for a broadcast or multicast datgram.) 430 */ 431 udpstat.udps_noportbcast++; 432 goto bad; 433 } 434 #ifdef IPSEC 435 /* check AH/ESP integrity. */ 436 if (ipsec4_in_reject_so(m, last->inp_socket)) { 437 ipsecstat.in_polvio++; 438 goto bad; 439 } 440 #endif /*IPSEC*/ 441 #ifdef FAST_IPSEC 442 /* check AH/ESP integrity. */ 443 if (ipsec4_in_reject(m, last)) 444 goto bad; 445 #endif /*FAST_IPSEC*/ 446 udp_append(last, ip, m, iphlen + sizeof(struct udphdr)); 447 return; 448 } 449 /* 450 * Locate pcb for datagram. 451 */ 452 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, 453 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); 454 if (inp == NULL) { 455 if (log_in_vain) { 456 char buf[sizeof "aaa.bbb.ccc.ddd"]; 457 458 strcpy(buf, inet_ntoa(ip->ip_dst)); 459 log(LOG_INFO, 460 "Connection attempt to UDP %s:%d from %s:%d\n", 461 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src), 462 ntohs(uh->uh_sport)); 463 } 464 udpstat.udps_noport++; 465 if (m->m_flags & (M_BCAST | M_MCAST)) { 466 udpstat.udps_noportbcast++; 467 goto bad; 468 } 469 if (blackhole) 470 goto bad; 471 #ifdef ICMP_BANDLIM 472 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) 473 goto bad; 474 #endif 475 *ip = save_ip; 476 ip->ip_len += iphlen; 477 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); 478 return; 479 } 480 #ifdef IPSEC 481 if (ipsec4_in_reject_so(m, inp->inp_socket)) { 482 ipsecstat.in_polvio++; 483 goto bad; 484 } 485 #endif /*IPSEC*/ 486 #ifdef FAST_IPSEC 487 if (ipsec4_in_reject(m, inp)) 488 goto bad; 489 #endif /*FAST_IPSEC*/ 490 /* 491 * Check the minimum TTL for socket. 492 */ 493 if (ip->ip_ttl < inp->inp_ip_minttl) 494 goto bad; 495 496 /* 497 * Construct sockaddr format source address. 498 * Stuff source address and datagram in user buffer. 499 */ 500 udp_in.sin_port = uh->uh_sport; 501 udp_in.sin_addr = ip->ip_src; 502 if ((inp->inp_flags & INP_CONTROLOPTS) || 503 (inp->inp_socket->so_options & SO_TIMESTAMP)) { 504 #ifdef INET6 505 if (inp->inp_vflag & INP_IPV6) { 506 int savedflags; 507 508 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 509 savedflags = inp->inp_flags; 510 inp->inp_flags &= ~INP_UNMAPPABLEOPTS; 511 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); 512 inp->inp_flags = savedflags; 513 } else 514 #endif 515 ip_savecontrol(inp, &opts, ip, m); 516 } 517 m_adj(m, iphlen + sizeof(struct udphdr)); 518 #ifdef INET6 519 if (inp->inp_vflag & INP_IPV6) { 520 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 521 append_sa = (struct sockaddr *)&udp_in6; 522 } else 523 #endif 524 append_sa = (struct sockaddr *)&udp_in; 525 if (ssb_appendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) { 526 udpstat.udps_fullsock++; 527 goto bad; 528 } 529 sorwakeup(inp->inp_socket); 530 return; 531 bad: 532 m_freem(m); 533 if (opts) 534 m_freem(opts); 535 return; 536 } 537 538 #ifdef INET6 539 static void 540 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip) 541 { 542 bzero(ip6, sizeof *ip6); 543 544 ip6->ip6_vfc = IPV6_VERSION; 545 ip6->ip6_plen = ip->ip_len; 546 ip6->ip6_nxt = ip->ip_p; 547 ip6->ip6_hlim = ip->ip_ttl; 548 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = 549 IPV6_ADDR_INT32_SMP; 550 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; 551 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; 552 } 553 #endif 554 555 /* 556 * subroutine of udp_input(), mainly for source code readability. 557 * caller must properly init udp_ip6 and udp_in6 beforehand. 558 */ 559 static void 560 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off) 561 { 562 struct sockaddr *append_sa; 563 struct mbuf *opts = NULL; 564 565 if (last->inp_flags & INP_CONTROLOPTS || 566 last->inp_socket->so_options & SO_TIMESTAMP) { 567 #ifdef INET6 568 if (last->inp_vflag & INP_IPV6) { 569 int savedflags; 570 571 if (udp_ip6.uip6_init_done == 0) { 572 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); 573 udp_ip6.uip6_init_done = 1; 574 } 575 savedflags = last->inp_flags; 576 last->inp_flags &= ~INP_UNMAPPABLEOPTS; 577 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n); 578 last->inp_flags = savedflags; 579 } else 580 #endif 581 ip_savecontrol(last, &opts, ip, n); 582 } 583 #ifdef INET6 584 if (last->inp_vflag & INP_IPV6) { 585 if (udp_in6.uin6_init_done == 0) { 586 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); 587 udp_in6.uin6_init_done = 1; 588 } 589 append_sa = (struct sockaddr *)&udp_in6.uin6_sin; 590 } else 591 #endif 592 append_sa = (struct sockaddr *)&udp_in; 593 m_adj(n, off); 594 if (ssb_appendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) { 595 m_freem(n); 596 if (opts) 597 m_freem(opts); 598 udpstat.udps_fullsock++; 599 } else 600 sorwakeup(last->inp_socket); 601 } 602 603 /* 604 * Notify a udp user of an asynchronous error; 605 * just wake up so that he can collect error status. 606 */ 607 void 608 udp_notify(struct inpcb *inp, int error) 609 { 610 inp->inp_socket->so_error = error; 611 sorwakeup(inp->inp_socket); 612 sowwakeup(inp->inp_socket); 613 } 614 615 struct netmsg_udp_notify { 616 struct netmsg nm_nmsg; 617 void (*nm_notify)(struct inpcb *, int); 618 struct in_addr nm_faddr; 619 int nm_arg; 620 }; 621 622 static void 623 udp_notifyall_oncpu(struct netmsg *netmsg) 624 { 625 struct netmsg_udp_notify *nmsg = (struct netmsg_udp_notify *)netmsg; 626 int nextcpu; 627 628 in_pcbnotifyall(&udbinfo.pcblisthead, nmsg->nm_faddr, nmsg->nm_arg, 629 nmsg->nm_notify); 630 631 nextcpu = mycpuid + 1; 632 if (nextcpu < ncpus2) 633 lwkt_forwardmsg(udp_cport(nextcpu), &netmsg->nm_lmsg); 634 else 635 lwkt_replymsg(&netmsg->nm_lmsg, 0); 636 } 637 638 static void 639 udp_rtchange(struct inpcb *inp, int err) 640 { 641 #ifdef SMP 642 /* XXX Nuke this, once UDP inpcbs are CPU localized */ 643 if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->rt_cpuid == mycpuid) { 644 rtfree(inp->inp_route.ro_rt); 645 inp->inp_route.ro_rt = NULL; 646 /* 647 * A new route can be allocated the next time 648 * output is attempted. 649 */ 650 } 651 #else 652 in_rtchange(inp, err); 653 #endif 654 } 655 656 void 657 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip) 658 { 659 struct ip *ip = vip; 660 struct udphdr *uh; 661 void (*notify) (struct inpcb *, int) = udp_notify; 662 struct in_addr faddr; 663 struct inpcb *inp; 664 665 faddr = ((struct sockaddr_in *)sa)->sin_addr; 666 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 667 return; 668 669 if (PRC_IS_REDIRECT(cmd)) { 670 ip = NULL; 671 notify = udp_rtchange; 672 } else if (cmd == PRC_HOSTDEAD) 673 ip = NULL; 674 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) 675 return; 676 if (ip) { 677 crit_enter(); 678 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 679 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, 680 ip->ip_src, uh->uh_sport, 0, NULL); 681 if (inp != NULL && inp->inp_socket != NULL) 682 (*notify)(inp, inetctlerrmap[cmd]); 683 crit_exit(); 684 } else if (PRC_IS_REDIRECT(cmd)) { 685 struct netmsg_udp_notify nmsg; 686 687 KKASSERT(&curthread->td_msgport == cpu_portfn(0)); 688 netmsg_init(&nmsg.nm_nmsg, NULL, &curthread->td_msgport, 689 0, udp_notifyall_oncpu); 690 nmsg.nm_faddr = faddr; 691 nmsg.nm_arg = inetctlerrmap[cmd]; 692 nmsg.nm_notify = notify; 693 694 lwkt_domsg(udp_cport(0), &nmsg.nm_nmsg.nm_lmsg, 0); 695 } else { 696 /* 697 * XXX We should forward msg upon PRC_HOSTHEAD and ip == NULL, 698 * once UDP inpcbs are CPU localized 699 */ 700 KKASSERT(&curthread->td_msgport == udp_cport(0)); 701 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd], 702 notify); 703 } 704 } 705 706 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0, 707 in_pcblist_global, "S,xinpcb", "List of active UDP sockets"); 708 709 static int 710 udp_getcred(SYSCTL_HANDLER_ARGS) 711 { 712 struct sockaddr_in addrs[2]; 713 struct inpcb *inp; 714 int error; 715 716 error = priv_check(req->td, PRIV_ROOT); 717 if (error) 718 return (error); 719 error = SYSCTL_IN(req, addrs, sizeof addrs); 720 if (error) 721 return (error); 722 crit_enter(); 723 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port, 724 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL); 725 if (inp == NULL || inp->inp_socket == NULL) { 726 error = ENOENT; 727 goto out; 728 } 729 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred)); 730 out: 731 crit_exit(); 732 return (error); 733 } 734 735 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW, 736 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection"); 737 738 static int 739 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr, 740 struct mbuf *control, struct thread *td) 741 { 742 struct udpiphdr *ui; 743 int len = m->m_pkthdr.len; 744 struct sockaddr_in *sin; /* really is initialized before use */ 745 int error = 0, lport_any = 0; 746 747 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { 748 error = EMSGSIZE; 749 goto release; 750 } 751 752 if (inp->inp_lport == 0) { /* unbound socket */ 753 error = in_pcbbind(inp, NULL, td); 754 if (error) 755 goto release; 756 in_pcbinswildcardhash(inp); 757 lport_any = 1; 758 } 759 760 if (dstaddr != NULL) { /* destination address specified */ 761 if (inp->inp_faddr.s_addr != INADDR_ANY) { 762 /* already connected */ 763 error = EISCONN; 764 goto release; 765 } 766 sin = (struct sockaddr_in *)dstaddr; 767 if (!prison_remote_ip(td, (struct sockaddr *)&sin)) { 768 error = EAFNOSUPPORT; /* IPv6 only jail */ 769 goto release; 770 } 771 } else { 772 if (inp->inp_faddr.s_addr == INADDR_ANY) { 773 /* no destination specified and not already connected */ 774 error = ENOTCONN; 775 goto release; 776 } 777 sin = NULL; 778 } 779 780 /* 781 * Calculate data length and get a mbuf 782 * for UDP and IP headers. 783 */ 784 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT); 785 if (m == NULL) { 786 error = ENOBUFS; 787 goto release; 788 } 789 790 /* 791 * Fill in mbuf with extended UDP header 792 * and addresses and length put into network format. 793 */ 794 ui = mtod(m, struct udpiphdr *); 795 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */ 796 ui->ui_pr = IPPROTO_UDP; 797 798 /* 799 * Set destination address. 800 */ 801 if (dstaddr != NULL) { /* use specified destination */ 802 ui->ui_dst = sin->sin_addr; 803 ui->ui_dport = sin->sin_port; 804 } else { /* use connected destination */ 805 ui->ui_dst = inp->inp_faddr; 806 ui->ui_dport = inp->inp_fport; 807 } 808 809 /* 810 * Set source address. 811 */ 812 if (inp->inp_laddr.s_addr == INADDR_ANY) { 813 struct sockaddr_in *if_sin; 814 815 if (dstaddr == NULL) { 816 /* 817 * connect() had (or should have) failed because 818 * the interface had no IP address, but the 819 * application proceeded to call send() anyways. 820 */ 821 error = ENOTCONN; 822 goto release; 823 } 824 825 /* Look up outgoing interface. */ 826 if ((error = in_pcbladdr(inp, dstaddr, &if_sin, td))) 827 goto release; 828 ui->ui_src = if_sin->sin_addr; /* use address of interface */ 829 } else { 830 ui->ui_src = inp->inp_laddr; /* use non-null bound address */ 831 } 832 ui->ui_sport = inp->inp_lport; 833 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound")); 834 835 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); 836 837 /* 838 * Set up checksum and output datagram. 839 */ 840 if (udpcksum) { 841 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, 842 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); 843 m->m_pkthdr.csum_flags = CSUM_UDP; 844 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); 845 } else { 846 ui->ui_sum = 0; 847 } 848 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len; 849 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ 850 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ 851 udpstat.udps_opackets++; 852 853 error = ip_output(m, inp->inp_options, &inp->inp_route, 854 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)) | 855 IP_DEBUGROUTE, 856 inp->inp_moptions, inp); 857 858 /* 859 * If this is the first data gram sent on an unbound and unconnected 860 * UDP socket, lport will be changed in this function. If target 861 * CPU after this lport changing is no longer the current CPU, then 862 * free the route entry allocated on the current CPU. 863 */ 864 if (lport_any) { 865 if (udp_addrcpu(inp->inp_faddr.s_addr, inp->inp_fport, 866 inp->inp_laddr.s_addr, inp->inp_lport) != mycpuid) { 867 struct route *ro = &inp->inp_route; 868 869 if (ro->ro_rt != NULL) 870 RTFREE(ro->ro_rt); 871 bzero(ro, sizeof(*ro)); 872 } 873 } 874 return (error); 875 876 release: 877 m_freem(m); 878 return (error); 879 } 880 881 u_long udp_sendspace = 9216; /* really max datagram size */ 882 /* 40 1K datagrams */ 883 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, 884 &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); 885 886 u_long udp_recvspace = 40 * (1024 + 887 #ifdef INET6 888 sizeof(struct sockaddr_in6) 889 #else 890 sizeof(struct sockaddr_in) 891 #endif 892 ); 893 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, 894 &udp_recvspace, 0, "Maximum incoming UDP datagram size"); 895 896 static int 897 udp_abort(struct socket *so) 898 { 899 struct inpcb *inp; 900 901 inp = so->so_pcb; 902 if (inp == NULL) 903 return EINVAL; /* ??? possible? panic instead? */ 904 soisdisconnected(so); 905 crit_enter(); 906 in_pcbdetach(inp); 907 crit_exit(); 908 return 0; 909 } 910 911 static int 912 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai) 913 { 914 struct inpcb *inp; 915 int error; 916 917 inp = so->so_pcb; 918 if (inp != NULL) 919 return EINVAL; 920 921 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit); 922 if (error) 923 return error; 924 crit_enter(); 925 error = in_pcballoc(so, &udbinfo); 926 crit_exit(); 927 if (error) 928 return error; 929 so->so_port = udp_soport_attach(so); 930 931 inp = (struct inpcb *)so->so_pcb; 932 inp->inp_vflag |= INP_IPV4; 933 inp->inp_ip_ttl = ip_defttl; 934 return 0; 935 } 936 937 static int 938 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 939 { 940 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 941 struct inpcb *inp; 942 int error; 943 944 inp = so->so_pcb; 945 if (inp == NULL) 946 return EINVAL; 947 crit_enter(); 948 error = in_pcbbind(inp, nam, td); 949 crit_exit(); 950 if (error == 0) { 951 if (sin->sin_addr.s_addr != INADDR_ANY) 952 inp->inp_flags |= INP_WASBOUND_NOTANY; 953 in_pcbinswildcardhash(inp); 954 } 955 return error; 956 } 957 958 #ifdef SMP 959 960 struct netmsg_udp_connect { 961 struct netmsg nm_netmsg; 962 struct socket *nm_so; 963 struct sockaddr_in *nm_sin; 964 struct sockaddr_in *nm_ifsin; 965 struct thread *nm_td; 966 }; 967 968 static void 969 udp_connect_handler(netmsg_t netmsg) 970 { 971 struct netmsg_udp_connect *msg = (void *)netmsg; 972 int error; 973 974 error = udp_connect_oncpu(msg->nm_so, msg->nm_td, 975 msg->nm_sin, msg->nm_ifsin); 976 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, error); 977 } 978 979 #endif 980 981 static int 982 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td) 983 { 984 struct inpcb *inp; 985 struct sockaddr_in *sin = (struct sockaddr_in *)nam; 986 struct sockaddr_in *if_sin; 987 lwkt_port_t port; 988 int error; 989 990 inp = so->so_pcb; 991 if (inp == NULL) 992 return EINVAL; 993 if (inp->inp_faddr.s_addr != INADDR_ANY) 994 return EISCONN; 995 error = 0; 996 997 /* 998 * Bind if we have to 999 */ 1000 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL && 1001 inp->inp_laddr.s_addr == INADDR_ANY) { 1002 error = in_pcbbind(inp, NULL, td); 1003 if (error) 1004 return (error); 1005 } 1006 1007 /* 1008 * Calculate the correct protocol processing thread. The connect 1009 * operation must run there. 1010 */ 1011 error = in_pcbladdr(inp, nam, &if_sin, td); 1012 if (error) 1013 return(error); 1014 if (!prison_remote_ip(td, nam)) 1015 return(EAFNOSUPPORT); /* IPv6 only jail */ 1016 1017 port = udp_addrport(sin->sin_addr.s_addr, sin->sin_port, 1018 inp->inp_laddr.s_addr, inp->inp_lport); 1019 #ifdef SMP 1020 if (port != &curthread->td_msgport) { 1021 struct netmsg_udp_connect msg; 1022 struct route *ro = &inp->inp_route; 1023 1024 /* 1025 * in_pcbladdr() may have allocated a route entry for us 1026 * on the current CPU, but we need a route entry on the 1027 * inpcb's owner CPU, so free it here. 1028 */ 1029 if (ro->ro_rt != NULL) 1030 RTFREE(ro->ro_rt); 1031 bzero(ro, sizeof(*ro)); 1032 1033 /* 1034 * NOTE: We haven't set so->so_port yet do not pass so 1035 * to netmsg_init() or it will be improperly forwarded. 1036 */ 1037 netmsg_init(&msg.nm_netmsg, NULL, &curthread->td_msgport, 1038 0, udp_connect_handler); 1039 msg.nm_so = so; 1040 msg.nm_sin = sin; 1041 msg.nm_ifsin = if_sin; 1042 msg.nm_td = td; 1043 error = lwkt_domsg(port, &msg.nm_netmsg.nm_lmsg, 0); 1044 } else { 1045 error = udp_connect_oncpu(so, td, sin, if_sin); 1046 } 1047 #else 1048 error = udp_connect_oncpu(so, td, sin, if_sin); 1049 #endif 1050 return (error); 1051 } 1052 1053 static int 1054 udp_connect_oncpu(struct socket *so, struct thread *td, 1055 struct sockaddr_in *sin, struct sockaddr_in *if_sin) 1056 { 1057 struct inpcb *inp; 1058 int error; 1059 1060 inp = so->so_pcb; 1061 if (inp->inp_flags & INP_WILDCARD) 1062 in_pcbremwildcardhash(inp); 1063 error = in_pcbconnect(inp, (struct sockaddr *)sin, td); 1064 1065 if (error == 0) { 1066 /* 1067 * No more errors can occur, finish adjusting the socket 1068 * and change the processing port to reflect the connected 1069 * socket. Once set we can no longer safely mess with the 1070 * socket. 1071 */ 1072 soisconnected(so); 1073 sosetport(so, &curthread->td_msgport); 1074 } else if (error == EAFNOSUPPORT) { /* connection dissolved */ 1075 /* 1076 * Follow traditional BSD behavior and retain 1077 * the local port binding. But, fix the old misbehavior 1078 * of overwriting any previously bound local address. 1079 */ 1080 if (!(inp->inp_flags & INP_WASBOUND_NOTANY)) 1081 inp->inp_laddr.s_addr = INADDR_ANY; 1082 in_pcbinswildcardhash(inp); 1083 } 1084 return error; 1085 } 1086 1087 static int 1088 udp_detach(struct socket *so) 1089 { 1090 struct inpcb *inp; 1091 1092 inp = so->so_pcb; 1093 if (inp == NULL) 1094 return EINVAL; 1095 crit_enter(); 1096 in_pcbdetach(inp); 1097 crit_exit(); 1098 return 0; 1099 } 1100 1101 static int 1102 udp_disconnect(struct socket *so) 1103 { 1104 struct route *ro; 1105 struct inpcb *inp; 1106 1107 inp = so->so_pcb; 1108 if (inp == NULL) 1109 return EINVAL; 1110 if (inp->inp_faddr.s_addr == INADDR_ANY) 1111 return ENOTCONN; 1112 1113 crit_enter(); 1114 in_pcbdisconnect(inp); 1115 crit_exit(); 1116 so->so_state &= ~SS_ISCONNECTED; /* XXX */ 1117 1118 ro = &inp->inp_route; 1119 if (ro->ro_rt != NULL) 1120 RTFREE(ro->ro_rt); 1121 bzero(ro, sizeof(*ro)); 1122 1123 return 0; 1124 } 1125 1126 static int 1127 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, 1128 struct mbuf *control, struct thread *td) 1129 { 1130 struct inpcb *inp; 1131 1132 inp = so->so_pcb; 1133 if (inp == NULL) { 1134 m_freem(m); 1135 return EINVAL; 1136 } 1137 return udp_output(inp, m, addr, control, td); 1138 } 1139 1140 int 1141 udp_shutdown(struct socket *so) 1142 { 1143 struct inpcb *inp; 1144 1145 inp = so->so_pcb; 1146 if (inp == NULL) 1147 return EINVAL; 1148 socantsendmore(so); 1149 return 0; 1150 } 1151 1152 struct pr_usrreqs udp_usrreqs = { 1153 .pru_abort = udp_abort, 1154 .pru_accept = pru_accept_notsupp, 1155 .pru_attach = udp_attach, 1156 .pru_bind = udp_bind, 1157 .pru_connect = udp_connect, 1158 .pru_connect2 = pru_connect2_notsupp, 1159 .pru_control = in_control, 1160 .pru_detach = udp_detach, 1161 .pru_disconnect = udp_disconnect, 1162 .pru_listen = pru_listen_notsupp, 1163 .pru_peeraddr = in_setpeeraddr, 1164 .pru_rcvd = pru_rcvd_notsupp, 1165 .pru_rcvoob = pru_rcvoob_notsupp, 1166 .pru_send = udp_send, 1167 .pru_sense = pru_sense_null, 1168 .pru_shutdown = udp_shutdown, 1169 .pru_sockaddr = in_setsockaddr, 1170 .pru_sosend = sosendudp, 1171 .pru_soreceive = soreceive, 1172 .pru_sopoll = sopoll 1173 }; 1174 1175