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