144} 145 146/* 147 * IPPROTO_DIVERT is not in the real IP protocol number space; this 148 * function should never be called. Just in case, drop any packets. 149 */ 150void 151div_input(struct mbuf *m, int off) 152{ 153 ipstat.ips_noproto++; 154 m_freem(m); 155} 156 157/* 158 * Divert a packet by passing it up to the divert socket at port 'port'. 159 * 160 * Setup generic address and protocol structures for div_input routine, 161 * then pass them along with mbuf chain. 162 */ 163static void 164divert_packet(struct mbuf *m, int incoming) 165{ 166 struct ip *ip; 167 struct inpcb *inp; 168 struct socket *sa; 169 u_int16_t nport; 170 struct sockaddr_in divsrc; 171 struct m_tag *mtag; 172 173 mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL); 174 if (mtag == NULL) { 175 printf("%s: no divert tag\n", __func__); 176 m_freem(m); 177 return; 178 } 179 /* Assure header */ 180 if (m->m_len < sizeof(struct ip) && 181 (m = m_pullup(m, sizeof(struct ip))) == 0) 182 return; 183 ip = mtod(m, struct ip *); 184 185 /* Delayed checksums are currently not compatible with divert. */ 186 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 187 ip->ip_len = ntohs(ip->ip_len); 188 in_delayed_cksum(m); 189 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 190 ip->ip_len = htons(ip->ip_len); 191 } 192 193 /* 194 * Record receive interface address, if any. 195 * But only for incoming packets. 196 */ 197 bzero(&divsrc, sizeof(divsrc)); 198 divsrc.sin_len = sizeof(divsrc); 199 divsrc.sin_family = AF_INET; 200 divsrc.sin_port = divert_cookie(mtag); /* record matching rule */ 201 if (incoming) { 202 struct ifaddr *ifa; 203 204 /* Sanity check */ 205 M_ASSERTPKTHDR(m); 206 207 /* Find IP address for receive interface */ 208 TAILQ_FOREACH(ifa, &m->m_pkthdr.rcvif->if_addrhead, ifa_link) { 209 if (ifa->ifa_addr == NULL) 210 continue; 211 if (ifa->ifa_addr->sa_family != AF_INET) 212 continue; 213 divsrc.sin_addr = 214 ((struct sockaddr_in *) ifa->ifa_addr)->sin_addr; 215 break; 216 } 217 } 218 /* 219 * Record the incoming interface name whenever we have one. 220 */ 221 if (m->m_pkthdr.rcvif) { 222 /* 223 * Hide the actual interface name in there in the 224 * sin_zero array. XXX This needs to be moved to a 225 * different sockaddr type for divert, e.g. 226 * sockaddr_div with multiple fields like 227 * sockaddr_dl. Presently we have only 7 bytes 228 * but that will do for now as most interfaces 229 * are 4 or less + 2 or less bytes for unit. 230 * There is probably a faster way of doing this, 231 * possibly taking it from the sockaddr_dl on the iface. 232 * This solves the problem of a P2P link and a LAN interface 233 * having the same address, which can result in the wrong 234 * interface being assigned to the packet when fed back 235 * into the divert socket. Theoretically if the daemon saves 236 * and re-uses the sockaddr_in as suggested in the man pages, 237 * this iface name will come along for the ride. 238 * (see div_output for the other half of this.) 239 */ 240 strlcpy(divsrc.sin_zero, m->m_pkthdr.rcvif->if_xname, 241 sizeof(divsrc.sin_zero)); 242 } 243 244 /* Put packet on socket queue, if any */ 245 sa = NULL; 246 nport = htons((u_int16_t)divert_info(mtag)); 247 INP_INFO_RLOCK(&divcbinfo); 248 LIST_FOREACH(inp, &divcb, inp_list) { 249 INP_LOCK(inp); 250 /* XXX why does only one socket match? */ 251 if (inp->inp_lport == nport) { 252 sa = inp->inp_socket; 253 SOCKBUF_LOCK(&sa->so_rcv); 254 if (sbappendaddr_locked(&sa->so_rcv, 255 (struct sockaddr *)&divsrc, m, 256 (struct mbuf *)0) == 0) { 257 SOCKBUF_UNLOCK(&sa->so_rcv); 258 sa = NULL; /* force mbuf reclaim below */ 259 } else 260 sorwakeup_locked(sa); 261 INP_UNLOCK(inp); 262 break; 263 } 264 INP_UNLOCK(inp); 265 } 266 INP_INFO_RUNLOCK(&divcbinfo); 267 if (sa == NULL) { 268 m_freem(m); 269 ipstat.ips_noproto++; 270 ipstat.ips_delivered--; 271 } 272} 273 274/* 275 * Deliver packet back into the IP processing machinery. 276 * 277 * If no address specified, or address is 0.0.0.0, send to ip_output(); 278 * otherwise, send to ip_input() and mark as having been received on 279 * the interface with that address. 280 */ 281static int 282div_output(struct socket *so, struct mbuf *m, 283 struct sockaddr_in *sin, struct mbuf *control) 284{ 285 struct m_tag *mtag; 286 struct divert_tag *dt; 287 int error = 0; 288 289 /* 290 * An mbuf may hasn't come from userland, but we pretend 291 * that it has. 292 */ 293 m->m_pkthdr.rcvif = NULL; 294 m->m_nextpkt = NULL; 295 296 if (control) 297 m_freem(control); /* XXX */ 298 299 if ((mtag = m_tag_find(m, PACKET_TAG_DIVERT, NULL)) == NULL) { 300 mtag = m_tag_get(PACKET_TAG_DIVERT, sizeof(struct divert_tag), 301 M_NOWAIT | M_ZERO); 302 if (mtag == NULL) { 303 error = ENOBUFS; 304 goto cantsend; 305 } 306 dt = (struct divert_tag *)(mtag+1); 307 m_tag_prepend(m, mtag); 308 } else 309 dt = (struct divert_tag *)(mtag+1); 310 311 /* Loopback avoidance and state recovery */ 312 if (sin) { 313 int i; 314 315 dt->cookie = sin->sin_port; 316 /* 317 * Find receive interface with the given name, stuffed 318 * (if it exists) in the sin_zero[] field. 319 * The name is user supplied data so don't trust its size 320 * or that it is zero terminated. 321 */ 322 for (i = 0; i < sizeof(sin->sin_zero) && sin->sin_zero[i]; i++) 323 ; 324 if ( i > 0 && i < sizeof(sin->sin_zero)) 325 m->m_pkthdr.rcvif = ifunit(sin->sin_zero); 326 } 327 328 /* Reinject packet into the system as incoming or outgoing */ 329 if (!sin || sin->sin_addr.s_addr == 0) { 330 struct ip *const ip = mtod(m, struct ip *); 331 struct inpcb *inp; 332 333 dt->info |= IP_FW_DIVERT_OUTPUT_FLAG; 334 INP_INFO_WLOCK(&divcbinfo); 335 inp = sotoinpcb(so); 336 INP_LOCK(inp); 337 /* 338 * Don't allow both user specified and setsockopt options, 339 * and don't allow packet length sizes that will crash 340 */ 341 if (((ip->ip_hl != (sizeof (*ip) >> 2)) && inp->inp_options) || 342 ((u_short)ntohs(ip->ip_len) > m->m_pkthdr.len)) { 343 error = EINVAL; 344 m_freem(m); 345 } else { 346 /* Convert fields to host order for ip_output() */ 347 ip->ip_len = ntohs(ip->ip_len); 348 ip->ip_off = ntohs(ip->ip_off); 349 350 /* Send packet to output processing */ 351 ipstat.ips_rawout++; /* XXX */ 352 353#ifdef MAC 354 mac_create_mbuf_from_inpcb(inp, m); 355#endif 356 error = ip_output(m, 357 inp->inp_options, NULL, 358 ((so->so_options & SO_DONTROUTE) ? 359 IP_ROUTETOIF : 0) | 360 IP_ALLOWBROADCAST | IP_RAWOUTPUT, 361 inp->inp_moptions, NULL); 362 } 363 INP_UNLOCK(inp); 364 INP_INFO_WUNLOCK(&divcbinfo); 365 } else { 366 dt->info |= IP_FW_DIVERT_LOOPBACK_FLAG; 367 if (m->m_pkthdr.rcvif == NULL) { 368 /* 369 * No luck with the name, check by IP address. 370 * Clear the port and the ifname to make sure 371 * there are no distractions for ifa_ifwithaddr. 372 */ 373 struct ifaddr *ifa; 374 375 bzero(sin->sin_zero, sizeof(sin->sin_zero)); 376 sin->sin_port = 0; 377 ifa = ifa_ifwithaddr((struct sockaddr *) sin); 378 if (ifa == NULL) { 379 error = EADDRNOTAVAIL; 380 goto cantsend; 381 } 382 m->m_pkthdr.rcvif = ifa->ifa_ifp; 383 } 384#ifdef MAC 385 SOCK_LOCK(so); 386 mac_create_mbuf_from_socket(so, m); 387 SOCK_UNLOCK(so); 388#endif 389 /* Send packet to input processing */ 390 ip_input(m); 391 } 392 393 return error; 394 395cantsend: 396 m_freem(m); 397 return error; 398} 399 400static int 401div_attach(struct socket *so, int proto, struct thread *td) 402{ 403 struct inpcb *inp; 404 int error; 405 406 inp = sotoinpcb(so); 407 KASSERT(inp == NULL, ("div_attach: inp != NULL")); 408 if (td && (error = suser(td)) != 0) 409 return error; 410 error = soreserve(so, div_sendspace, div_recvspace); 411 if (error) 412 return error; 413 INP_INFO_WLOCK(&divcbinfo); 414 error = in_pcballoc(so, &divcbinfo, "divinp"); 415 if (error) { 416 INP_INFO_WUNLOCK(&divcbinfo); 417 return error; 418 } 419 inp = (struct inpcb *)so->so_pcb; 420 INP_LOCK(inp); 421 INP_INFO_WUNLOCK(&divcbinfo); 422 inp->inp_ip_p = proto; 423 inp->inp_vflag |= INP_IPV4; 424 inp->inp_flags |= INP_HDRINCL; 425 INP_UNLOCK(inp); 426 return 0; 427} 428 429static void 430div_detach(struct socket *so) 431{ 432 struct inpcb *inp; 433 434 inp = sotoinpcb(so); 435 KASSERT(inp != NULL, ("div_detach: inp == NULL")); 436 INP_INFO_WLOCK(&divcbinfo); 437 INP_LOCK(inp); 438 in_pcbdetach(inp); 439 in_pcbfree(inp); 440 INP_INFO_WUNLOCK(&divcbinfo); 441} 442 443static int 444div_bind(struct socket *so, struct sockaddr *nam, struct thread *td) 445{ 446 struct inpcb *inp; 447 int error; 448 449 inp = sotoinpcb(so); 450 KASSERT(inp != NULL, ("div_bind: inp == NULL")); 451 /* in_pcbbind assumes that nam is a sockaddr_in 452 * and in_pcbbind requires a valid address. Since divert 453 * sockets don't we need to make sure the address is 454 * filled in properly. 455 * XXX -- divert should not be abusing in_pcbind 456 * and should probably have its own family. 457 */ 458 if (nam->sa_family != AF_INET) 459 return EAFNOSUPPORT; 460 ((struct sockaddr_in *)nam)->sin_addr.s_addr = INADDR_ANY; 461 INP_INFO_WLOCK(&divcbinfo); 462 INP_LOCK(inp); 463 error = in_pcbbind(inp, nam, td->td_ucred); 464 INP_UNLOCK(inp); 465 INP_INFO_WUNLOCK(&divcbinfo); 466 return error; 467} 468 469static int 470div_shutdown(struct socket *so) 471{ 472 struct inpcb *inp; 473 474 inp = sotoinpcb(so); 475 KASSERT(inp != NULL, ("div_shutdown: inp == NULL")); 476 INP_LOCK(inp); 477 socantsendmore(so); 478 INP_UNLOCK(inp); 479 return 0; 480} 481 482static int 483div_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam, 484 struct mbuf *control, struct thread *td) 485{ 486 /* Packet must have a header (but that's about it) */ 487 if (m->m_len < sizeof (struct ip) && 488 (m = m_pullup(m, sizeof (struct ip))) == 0) { 489 ipstat.ips_toosmall++; 490 m_freem(m); 491 return EINVAL; 492 } 493 494 /* Send packet */ 495 return div_output(so, m, (struct sockaddr_in *)nam, control); 496} 497 498void 499div_ctlinput(int cmd, struct sockaddr *sa, void *vip) 500{ 501 struct in_addr faddr; 502 503 faddr = ((struct sockaddr_in *)sa)->sin_addr; 504 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) 505 return; 506 if (PRC_IS_REDIRECT(cmd)) 507 return; 508} 509 510static int 511div_pcblist(SYSCTL_HANDLER_ARGS) 512{ 513 int error, i, n; 514 struct inpcb *inp, **inp_list; 515 inp_gen_t gencnt; 516 struct xinpgen xig; 517 518 /* 519 * The process of preparing the TCB list is too time-consuming and 520 * resource-intensive to repeat twice on every request. 521 */ 522 if (req->oldptr == 0) { 523 n = divcbinfo.ipi_count; 524 req->oldidx = 2 * (sizeof xig) 525 + (n + n/8) * sizeof(struct xinpcb); 526 return 0; 527 } 528 529 if (req->newptr != 0) 530 return EPERM; 531 532 /* 533 * OK, now we're committed to doing something. 534 */ 535 INP_INFO_RLOCK(&divcbinfo); 536 gencnt = divcbinfo.ipi_gencnt; 537 n = divcbinfo.ipi_count; 538 INP_INFO_RUNLOCK(&divcbinfo); 539 540 error = sysctl_wire_old_buffer(req, 541 2 * sizeof(xig) + n*sizeof(struct xinpcb)); 542 if (error != 0) 543 return (error); 544 545 xig.xig_len = sizeof xig; 546 xig.xig_count = n; 547 xig.xig_gen = gencnt; 548 xig.xig_sogen = so_gencnt; 549 error = SYSCTL_OUT(req, &xig, sizeof xig); 550 if (error) 551 return error; 552 553 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK); 554 if (inp_list == 0) 555 return ENOMEM; 556 557 INP_INFO_RLOCK(&divcbinfo); 558 for (inp = LIST_FIRST(divcbinfo.listhead), i = 0; inp && i < n; 559 inp = LIST_NEXT(inp, inp_list)) { 560 INP_LOCK(inp); 561 if (inp->inp_gencnt <= gencnt && 562 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0) 563 inp_list[i++] = inp; 564 INP_UNLOCK(inp); 565 } 566 INP_INFO_RUNLOCK(&divcbinfo); 567 n = i; 568 569 error = 0; 570 for (i = 0; i < n; i++) { 571 inp = inp_list[i]; 572 if (inp->inp_gencnt <= gencnt) { 573 struct xinpcb xi; 574 bzero(&xi, sizeof(xi)); 575 xi.xi_len = sizeof xi; 576 /* XXX should avoid extra copy */ 577 bcopy(inp, &xi.xi_inp, sizeof *inp); 578 if (inp->inp_socket) 579 sotoxsocket(inp->inp_socket, &xi.xi_socket); 580 error = SYSCTL_OUT(req, &xi, sizeof xi); 581 } 582 } 583 if (!error) { 584 /* 585 * Give the user an updated idea of our state. 586 * If the generation differs from what we told 587 * her before, she knows that something happened 588 * while we were processing this request, and it 589 * might be necessary to retry. 590 */ 591 INP_INFO_RLOCK(&divcbinfo); 592 xig.xig_gen = divcbinfo.ipi_gencnt; 593 xig.xig_sogen = so_gencnt; 594 xig.xig_count = divcbinfo.ipi_count; 595 INP_INFO_RUNLOCK(&divcbinfo); 596 error = SYSCTL_OUT(req, &xig, sizeof xig); 597 } 598 free(inp_list, M_TEMP); 599 return error; 600} 601 602/* 603 * This is the wrapper function for in_setsockaddr. We just pass down 604 * the pcbinfo for in_setpeeraddr to lock. 605 */ 606static int 607div_sockaddr(struct socket *so, struct sockaddr **nam) 608{ 609 return (in_setsockaddr(so, nam, &divcbinfo)); 610} 611 612/* 613 * This is the wrapper function for in_setpeeraddr. We just pass down 614 * the pcbinfo for in_setpeeraddr to lock. 615 */ 616static int 617div_peeraddr(struct socket *so, struct sockaddr **nam) 618{ 619 return (in_setpeeraddr(so, nam, &divcbinfo)); 620} 621 622#ifdef SYSCTL_NODE 623SYSCTL_NODE(_net_inet, IPPROTO_DIVERT, divert, CTLFLAG_RW, 0, "IPDIVERT"); 624SYSCTL_PROC(_net_inet_divert, OID_AUTO, pcblist, CTLFLAG_RD, 0, 0, 625 div_pcblist, "S,xinpcb", "List of active divert sockets"); 626#endif 627 628struct pr_usrreqs div_usrreqs = { 629 .pru_attach = div_attach, 630 .pru_bind = div_bind, 631 .pru_control = in_control, 632 .pru_detach = div_detach, 633 .pru_peeraddr = div_peeraddr, 634 .pru_send = div_send, 635 .pru_shutdown = div_shutdown, 636 .pru_sockaddr = div_sockaddr, 637 .pru_sosetlabel = in_pcbsosetlabel 638}; 639 640struct protosw div_protosw = { 641 .pr_type = SOCK_RAW, 642 .pr_protocol = IPPROTO_DIVERT, 643 .pr_flags = PR_ATOMIC|PR_ADDR, 644 .pr_input = div_input, 645 .pr_ctlinput = div_ctlinput, 646 .pr_ctloutput = ip_ctloutput, 647 .pr_init = div_init, 648 .pr_usrreqs = &div_usrreqs 649}; 650 651static int 652div_modevent(module_t mod, int type, void *unused) 653{ 654 int err = 0; 655 int n; 656 657 switch (type) { 658 case MOD_LOAD: 659 /* 660 * Protocol will be initialized by pf_proto_register(). 661 * We don't have to register ip_protox because we are not 662 * a true IP protocol that goes over the wire. 663 */ 664 err = pf_proto_register(PF_INET, &div_protosw); 665 ip_divert_ptr = divert_packet; 666 break; 667 case MOD_QUIESCE: 668 /* 669 * IPDIVERT may normally not be unloaded because of the 670 * potential race conditions. Tell kldunload we can't be 671 * unloaded unless the unload is forced. 672 */ 673 err = EPERM; 674 break; 675 case MOD_UNLOAD: 676 /* 677 * Forced unload. 678 * 679 * Module ipdivert can only be unloaded if no sockets are 680 * connected. Maybe this can be changed later to forcefully 681 * disconnect any open sockets. 682 * 683 * XXXRW: Note that there is a slight race here, as a new 684 * socket open request could be spinning on the lock and then 685 * we destroy the lock. 686 */ 687 INP_INFO_WLOCK(&divcbinfo); 688 n = divcbinfo.ipi_count; 689 if (n != 0) { 690 err = EBUSY; 691 INP_INFO_WUNLOCK(&divcbinfo); 692 break; 693 } 694 ip_divert_ptr = NULL; 695 err = pf_proto_unregister(PF_INET, IPPROTO_DIVERT, SOCK_RAW); 696 INP_INFO_WUNLOCK(&divcbinfo); 697 INP_INFO_LOCK_DESTROY(&divcbinfo); 698 uma_zdestroy(divcbinfo.ipi_zone); 699 break; 700 default: 701 err = EOPNOTSUPP; 702 break; 703 } 704 return err; 705} 706 707static moduledata_t ipdivertmod = { 708 "ipdivert", 709 div_modevent, 710 0 711}; 712 713DECLARE_MODULE(ipdivert, ipdivertmod, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_ANY); 714MODULE_DEPEND(dummynet, ipfw, 2, 2, 2); 715MODULE_VERSION(ipdivert, 1);
|