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