1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2008 Paolo Pisati 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/eventhandler.h> 35 #include <sys/malloc.h> 36 #include <sys/mbuf.h> 37 #include <sys/kernel.h> 38 #include <sys/lock.h> 39 #include <sys/module.h> 40 #include <sys/rwlock.h> 41 #include <sys/rmlock.h> 42 43 #include <netinet/libalias/alias.h> 44 #include <netinet/libalias/alias_local.h> 45 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <netinet/in.h> 49 #include <netinet/ip.h> 50 #include <netinet/ip_var.h> 51 #include <netinet/ip_fw.h> 52 #include <netinet/tcp.h> 53 #include <netinet/udp.h> 54 55 #include <netpfil/ipfw/ip_fw_private.h> 56 57 #include <machine/in_cksum.h> /* XXX for in_cksum */ 58 59 struct cfg_spool { 60 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */ 61 struct in_addr addr; 62 uint16_t port; 63 }; 64 65 /* Nat redirect configuration. */ 66 struct cfg_redir { 67 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */ 68 uint16_t mode; /* type of redirect mode */ 69 uint16_t proto; /* protocol: tcp/udp */ 70 struct in_addr laddr; /* local ip address */ 71 struct in_addr paddr; /* public ip address */ 72 struct in_addr raddr; /* remote ip address */ 73 uint16_t lport; /* local port */ 74 uint16_t pport; /* public port */ 75 uint16_t rport; /* remote port */ 76 uint16_t pport_cnt; /* number of public ports */ 77 uint16_t rport_cnt; /* number of remote ports */ 78 struct alias_link **alink; 79 u_int16_t spool_cnt; /* num of entry in spool chain */ 80 /* chain of spool instances */ 81 LIST_HEAD(spool_chain, cfg_spool) spool_chain; 82 }; 83 84 /* Nat configuration data struct. */ 85 struct cfg_nat { 86 /* chain of nat instances */ 87 LIST_ENTRY(cfg_nat) _next; 88 int id; /* nat id */ 89 struct in_addr ip; /* nat ip address */ 90 struct libalias *lib; /* libalias instance */ 91 int mode; /* aliasing mode */ 92 int redir_cnt; /* number of entry in spool chain */ 93 /* chain of redir instances */ 94 LIST_HEAD(redir_chain, cfg_redir) redir_chain; 95 char if_name[IF_NAMESIZE]; /* interface name */ 96 }; 97 98 static eventhandler_tag ifaddr_event_tag; 99 100 static void 101 ifaddr_change(void *arg __unused, struct ifnet *ifp) 102 { 103 struct cfg_nat *ptr; 104 struct ifaddr *ifa; 105 struct ip_fw_chain *chain; 106 107 KASSERT(curvnet == ifp->if_vnet, 108 ("curvnet(%p) differs from iface vnet(%p)", curvnet, ifp->if_vnet)); 109 110 if (V_ipfw_vnet_ready == 0 || V_ipfw_nat_ready == 0) 111 return; 112 113 chain = &V_layer3_chain; 114 IPFW_UH_WLOCK(chain); 115 /* Check every nat entry... */ 116 LIST_FOREACH(ptr, &chain->nat, _next) { 117 struct epoch_tracker et; 118 119 /* ...using nic 'ifp->if_xname' as dynamic alias address. */ 120 if (strncmp(ptr->if_name, ifp->if_xname, IF_NAMESIZE) != 0) 121 continue; 122 NET_EPOCH_ENTER(et); 123 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { 124 if (ifa->ifa_addr == NULL) 125 continue; 126 if (ifa->ifa_addr->sa_family != AF_INET) 127 continue; 128 IPFW_WLOCK(chain); 129 ptr->ip = ((struct sockaddr_in *) 130 (ifa->ifa_addr))->sin_addr; 131 LibAliasSetAddress(ptr->lib, ptr->ip); 132 IPFW_WUNLOCK(chain); 133 } 134 NET_EPOCH_EXIT(et); 135 } 136 IPFW_UH_WUNLOCK(chain); 137 } 138 139 /* 140 * delete the pointers for nat entry ix, or all of them if ix < 0 141 */ 142 static void 143 flush_nat_ptrs(struct ip_fw_chain *chain, const int ix) 144 { 145 ipfw_insn_nat *cmd; 146 int i; 147 148 IPFW_WLOCK_ASSERT(chain); 149 for (i = 0; i < chain->n_rules; i++) { 150 cmd = (ipfw_insn_nat *)ipfw_get_action(chain->map[i]); 151 if (cmd->o.opcode == O_NAT && cmd->nat != NULL && 152 (ix < 0 || cmd->nat->id == ix)) 153 cmd->nat = NULL; 154 } 155 } 156 157 static void 158 del_redir_spool_cfg(struct cfg_nat *n, struct redir_chain *head) 159 { 160 struct cfg_redir *r, *tmp_r; 161 struct cfg_spool *s, *tmp_s; 162 int i, num; 163 164 LIST_FOREACH_SAFE(r, head, _next, tmp_r) { 165 num = 1; /* Number of alias_link to delete. */ 166 switch (r->mode) { 167 case NAT44_REDIR_PORT: 168 num = r->pport_cnt; 169 /* FALLTHROUGH */ 170 case NAT44_REDIR_ADDR: 171 case NAT44_REDIR_PROTO: 172 /* Delete all libalias redirect entry. */ 173 for (i = 0; i < num; i++) 174 LibAliasRedirectDelete(n->lib, r->alink[i]); 175 /* Del spool cfg if any. */ 176 LIST_FOREACH_SAFE(s, &r->spool_chain, _next, tmp_s) { 177 LIST_REMOVE(s, _next); 178 free(s, M_IPFW); 179 } 180 free(r->alink, M_IPFW); 181 LIST_REMOVE(r, _next); 182 free(r, M_IPFW); 183 break; 184 default: 185 printf("unknown redirect mode: %u\n", r->mode); 186 /* XXX - panic?!?!? */ 187 break; 188 } 189 } 190 } 191 192 static int 193 add_redir_spool_cfg(char *buf, struct cfg_nat *ptr) 194 { 195 struct cfg_redir *r; 196 struct cfg_spool *s; 197 struct nat44_cfg_redir *ser_r; 198 struct nat44_cfg_spool *ser_s; 199 200 int cnt, off, i; 201 202 for (cnt = 0, off = 0; cnt < ptr->redir_cnt; cnt++) { 203 ser_r = (struct nat44_cfg_redir *)&buf[off]; 204 r = malloc(sizeof(*r), M_IPFW, M_WAITOK | M_ZERO); 205 r->mode = ser_r->mode; 206 r->laddr = ser_r->laddr; 207 r->paddr = ser_r->paddr; 208 r->raddr = ser_r->raddr; 209 r->lport = ser_r->lport; 210 r->pport = ser_r->pport; 211 r->rport = ser_r->rport; 212 r->pport_cnt = ser_r->pport_cnt; 213 r->rport_cnt = ser_r->rport_cnt; 214 r->proto = ser_r->proto; 215 r->spool_cnt = ser_r->spool_cnt; 216 //memcpy(r, ser_r, SOF_REDIR); 217 LIST_INIT(&r->spool_chain); 218 off += sizeof(struct nat44_cfg_redir); 219 r->alink = malloc(sizeof(struct alias_link *) * r->pport_cnt, 220 M_IPFW, M_WAITOK | M_ZERO); 221 switch (r->mode) { 222 case NAT44_REDIR_ADDR: 223 r->alink[0] = LibAliasRedirectAddr(ptr->lib, r->laddr, 224 r->paddr); 225 break; 226 case NAT44_REDIR_PORT: 227 for (i = 0 ; i < r->pport_cnt; i++) { 228 /* If remotePort is all ports, set it to 0. */ 229 u_short remotePortCopy = r->rport + i; 230 if (r->rport_cnt == 1 && r->rport == 0) 231 remotePortCopy = 0; 232 r->alink[i] = LibAliasRedirectPort(ptr->lib, 233 r->laddr, htons(r->lport + i), r->raddr, 234 htons(remotePortCopy), r->paddr, 235 htons(r->pport + i), r->proto); 236 if (r->alink[i] == NULL) { 237 r->alink[0] = NULL; 238 break; 239 } 240 } 241 break; 242 case NAT44_REDIR_PROTO: 243 r->alink[0] = LibAliasRedirectProto(ptr->lib ,r->laddr, 244 r->raddr, r->paddr, r->proto); 245 break; 246 default: 247 printf("unknown redirect mode: %u\n", r->mode); 248 break; 249 } 250 if (r->alink[0] == NULL) { 251 printf("LibAliasRedirect* returned NULL\n"); 252 free(r->alink, M_IPFW); 253 free(r, M_IPFW); 254 return (EINVAL); 255 } 256 /* LSNAT handling. */ 257 for (i = 0; i < r->spool_cnt; i++) { 258 ser_s = (struct nat44_cfg_spool *)&buf[off]; 259 s = malloc(sizeof(*s), M_IPFW, M_WAITOK | M_ZERO); 260 s->addr = ser_s->addr; 261 s->port = ser_s->port; 262 LibAliasAddServer(ptr->lib, r->alink[0], 263 s->addr, htons(s->port)); 264 off += sizeof(struct nat44_cfg_spool); 265 /* Hook spool entry. */ 266 LIST_INSERT_HEAD(&r->spool_chain, s, _next); 267 } 268 /* And finally hook this redir entry. */ 269 LIST_INSERT_HEAD(&ptr->redir_chain, r, _next); 270 } 271 272 return (0); 273 } 274 275 static void 276 free_nat_instance(struct cfg_nat *ptr) 277 { 278 279 del_redir_spool_cfg(ptr, &ptr->redir_chain); 280 LibAliasUninit(ptr->lib); 281 free(ptr, M_IPFW); 282 } 283 284 /* 285 * ipfw_nat - perform mbuf header translation. 286 * 287 * Note V_layer3_chain has to be locked while calling ipfw_nat() in 288 * 'global' operation mode (t == NULL). 289 * 290 */ 291 static int 292 ipfw_nat(struct ip_fw_args *args, struct cfg_nat *t, struct mbuf *m) 293 { 294 struct mbuf *mcl; 295 struct ip *ip; 296 /* XXX - libalias duct tape */ 297 int ldt, retval, found; 298 struct ip_fw_chain *chain; 299 char *c; 300 301 ldt = 0; 302 retval = 0; 303 mcl = m_megapullup(m, m->m_pkthdr.len); 304 if (mcl == NULL) { 305 args->m = NULL; 306 return (IP_FW_DENY); 307 } 308 ip = mtod(mcl, struct ip *); 309 310 /* 311 * XXX - Libalias checksum offload 'duct tape': 312 * 313 * locally generated packets have only pseudo-header checksum 314 * calculated and libalias will break it[1], so mark them for 315 * later fix. Moreover there are cases when libalias modifies 316 * tcp packet data[2], mark them for later fix too. 317 * 318 * [1] libalias was never meant to run in kernel, so it does 319 * not have any knowledge about checksum offloading, and 320 * expects a packet with a full internet checksum. 321 * Unfortunately, packets generated locally will have just the 322 * pseudo header calculated, and when libalias tries to adjust 323 * the checksum it will actually compute a wrong value. 324 * 325 * [2] when libalias modifies tcp's data content, full TCP 326 * checksum has to be recomputed: the problem is that 327 * libalias does not have any idea about checksum offloading. 328 * To work around this, we do not do checksumming in LibAlias, 329 * but only mark the packets in th_x2 field. If we receive a 330 * marked packet, we calculate correct checksum for it 331 * aware of offloading. Why such a terrible hack instead of 332 * recalculating checksum for each packet? 333 * Because the previous checksum was not checked! 334 * Recalculating checksums for EVERY packet will hide ALL 335 * transmission errors. Yes, marked packets still suffer from 336 * this problem. But, sigh, natd(8) has this problem, too. 337 * 338 * TODO: -make libalias mbuf aware (so 339 * it can handle delayed checksum and tso) 340 */ 341 342 if (mcl->m_pkthdr.rcvif == NULL && 343 mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA) 344 ldt = 1; 345 346 c = mtod(mcl, char *); 347 348 /* Check if this is 'global' instance */ 349 if (t == NULL) { 350 if (args->flags & IPFW_ARGS_IN) { 351 /* Wrong direction, skip processing */ 352 args->m = mcl; 353 return (IP_FW_NAT); 354 } 355 356 found = 0; 357 chain = &V_layer3_chain; 358 IPFW_RLOCK_ASSERT(chain); 359 /* Check every nat entry... */ 360 LIST_FOREACH(t, &chain->nat, _next) { 361 if ((t->mode & PKT_ALIAS_SKIP_GLOBAL) != 0) 362 continue; 363 retval = LibAliasOutTry(t->lib, c, 364 mcl->m_len + M_TRAILINGSPACE(mcl), 0); 365 if (retval == PKT_ALIAS_OK) { 366 /* Nat instance recognises state */ 367 found = 1; 368 break; 369 } 370 } 371 if (found != 1) { 372 /* No instance found, return ignore */ 373 args->m = mcl; 374 return (IP_FW_NAT); 375 } 376 } else { 377 if (args->flags & IPFW_ARGS_IN) 378 retval = LibAliasIn(t->lib, c, 379 mcl->m_len + M_TRAILINGSPACE(mcl)); 380 else 381 retval = LibAliasOut(t->lib, c, 382 mcl->m_len + M_TRAILINGSPACE(mcl)); 383 } 384 385 /* 386 * We drop packet when: 387 * 1. libalias returns PKT_ALIAS_ERROR; 388 * 2. For incoming packets: 389 * a) for unresolved fragments; 390 * b) libalias returns PKT_ALIAS_IGNORED and 391 * PKT_ALIAS_DENY_INCOMING flag is set. 392 */ 393 if (retval == PKT_ALIAS_ERROR || 394 ((args->flags & IPFW_ARGS_IN) && 395 (retval == PKT_ALIAS_UNRESOLVED_FRAGMENT || 396 (retval == PKT_ALIAS_IGNORED && 397 (t->mode & PKT_ALIAS_DENY_INCOMING) != 0)))) { 398 /* XXX - should i add some logging? */ 399 m_free(mcl); 400 args->m = NULL; 401 return (IP_FW_DENY); 402 } 403 404 if (retval == PKT_ALIAS_RESPOND) 405 mcl->m_flags |= M_SKIP_FIREWALL; 406 mcl->m_pkthdr.len = mcl->m_len = ntohs(ip->ip_len); 407 408 /* 409 * XXX - libalias checksum offload 410 * 'duct tape' (see above) 411 */ 412 413 if ((ip->ip_off & htons(IP_OFFMASK)) == 0 && 414 ip->ip_p == IPPROTO_TCP) { 415 struct tcphdr *th; 416 417 th = (struct tcphdr *)(ip + 1); 418 if (th->th_x2) 419 ldt = 1; 420 } 421 422 if (ldt) { 423 struct tcphdr *th; 424 struct udphdr *uh; 425 uint16_t ip_len, cksum; 426 427 ip_len = ntohs(ip->ip_len); 428 cksum = in_pseudo(ip->ip_src.s_addr, ip->ip_dst.s_addr, 429 htons(ip->ip_p + ip_len - (ip->ip_hl << 2))); 430 431 switch (ip->ip_p) { 432 case IPPROTO_TCP: 433 th = (struct tcphdr *)(ip + 1); 434 /* 435 * Maybe it was set in 436 * libalias... 437 */ 438 th->th_x2 = 0; 439 th->th_sum = cksum; 440 mcl->m_pkthdr.csum_data = 441 offsetof(struct tcphdr, th_sum); 442 break; 443 case IPPROTO_UDP: 444 uh = (struct udphdr *)(ip + 1); 445 uh->uh_sum = cksum; 446 mcl->m_pkthdr.csum_data = 447 offsetof(struct udphdr, uh_sum); 448 break; 449 } 450 /* No hw checksum offloading: do it ourselves */ 451 if ((mcl->m_pkthdr.csum_flags & CSUM_DELAY_DATA) == 0) { 452 in_delayed_cksum(mcl); 453 mcl->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 454 } 455 } 456 args->m = mcl; 457 return (IP_FW_NAT); 458 } 459 460 static struct cfg_nat * 461 lookup_nat(struct nat_list *l, int nat_id) 462 { 463 struct cfg_nat *res; 464 465 LIST_FOREACH(res, l, _next) { 466 if (res->id == nat_id) 467 break; 468 } 469 return res; 470 } 471 472 static struct cfg_nat * 473 lookup_nat_name(struct nat_list *l, char *name) 474 { 475 struct cfg_nat *res; 476 int id; 477 char *errptr; 478 479 id = strtol(name, &errptr, 10); 480 if (id == 0 || *errptr != '\0') 481 return (NULL); 482 483 LIST_FOREACH(res, l, _next) { 484 if (res->id == id) 485 break; 486 } 487 return (res); 488 } 489 490 /* IP_FW3 configuration routines */ 491 492 static void 493 nat44_config(struct ip_fw_chain *chain, struct nat44_cfg_nat *ucfg) 494 { 495 struct cfg_nat *ptr, *tcfg; 496 int gencnt; 497 498 /* 499 * Find/create nat rule. 500 */ 501 IPFW_UH_WLOCK(chain); 502 gencnt = chain->gencnt; 503 ptr = lookup_nat_name(&chain->nat, ucfg->name); 504 if (ptr == NULL) { 505 IPFW_UH_WUNLOCK(chain); 506 /* New rule: allocate and init new instance. */ 507 ptr = malloc(sizeof(struct cfg_nat), M_IPFW, M_WAITOK | M_ZERO); 508 ptr->lib = LibAliasInit(NULL); 509 LIST_INIT(&ptr->redir_chain); 510 } else { 511 /* Entry already present: temporarily unhook it. */ 512 IPFW_WLOCK(chain); 513 LIST_REMOVE(ptr, _next); 514 flush_nat_ptrs(chain, ptr->id); 515 IPFW_WUNLOCK(chain); 516 IPFW_UH_WUNLOCK(chain); 517 } 518 519 /* 520 * Basic nat (re)configuration. 521 */ 522 ptr->id = strtol(ucfg->name, NULL, 10); 523 /* 524 * XXX - what if this rule doesn't nat any ip and just 525 * redirect? 526 * do we set aliasaddress to 0.0.0.0? 527 */ 528 ptr->ip = ucfg->ip; 529 ptr->redir_cnt = ucfg->redir_cnt; 530 ptr->mode = ucfg->mode; 531 strlcpy(ptr->if_name, ucfg->if_name, sizeof(ptr->if_name)); 532 LibAliasSetMode(ptr->lib, ptr->mode, ~0); 533 LibAliasSetAddress(ptr->lib, ptr->ip); 534 535 /* 536 * Redir and LSNAT configuration. 537 */ 538 /* Delete old cfgs. */ 539 del_redir_spool_cfg(ptr, &ptr->redir_chain); 540 /* Add new entries. */ 541 add_redir_spool_cfg((char *)(ucfg + 1), ptr); 542 IPFW_UH_WLOCK(chain); 543 544 /* Extra check to avoid race with another ipfw_nat_cfg() */ 545 tcfg = NULL; 546 if (gencnt != chain->gencnt) 547 tcfg = lookup_nat_name(&chain->nat, ucfg->name); 548 IPFW_WLOCK(chain); 549 if (tcfg != NULL) 550 LIST_REMOVE(tcfg, _next); 551 LIST_INSERT_HEAD(&chain->nat, ptr, _next); 552 IPFW_WUNLOCK(chain); 553 chain->gencnt++; 554 555 IPFW_UH_WUNLOCK(chain); 556 557 if (tcfg != NULL) 558 free_nat_instance(ptr); 559 } 560 561 /* 562 * Creates/configure nat44 instance 563 * Data layout (v0)(current): 564 * Request: [ ipfw_obj_header nat44_cfg_nat .. ] 565 * 566 * Returns 0 on success 567 */ 568 static int 569 nat44_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3, 570 struct sockopt_data *sd) 571 { 572 ipfw_obj_header *oh; 573 struct nat44_cfg_nat *ucfg; 574 int id; 575 size_t read; 576 char *errptr; 577 578 /* Check minimum header size */ 579 if (sd->valsize < (sizeof(*oh) + sizeof(*ucfg))) 580 return (EINVAL); 581 582 oh = (ipfw_obj_header *)sd->kbuf; 583 584 /* Basic length checks for TLVs */ 585 if (oh->ntlv.head.length != sizeof(oh->ntlv)) 586 return (EINVAL); 587 588 ucfg = (struct nat44_cfg_nat *)(oh + 1); 589 590 /* Check if name is properly terminated and looks like number */ 591 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name)) 592 return (EINVAL); 593 id = strtol(ucfg->name, &errptr, 10); 594 if (id == 0 || *errptr != '\0') 595 return (EINVAL); 596 597 read = sizeof(*oh) + sizeof(*ucfg); 598 /* Check number of redirs */ 599 if (sd->valsize < read + ucfg->redir_cnt*sizeof(struct nat44_cfg_redir)) 600 return (EINVAL); 601 602 nat44_config(chain, ucfg); 603 return (0); 604 } 605 606 /* 607 * Destroys given nat instances. 608 * Data layout (v0)(current): 609 * Request: [ ipfw_obj_header ] 610 * 611 * Returns 0 on success 612 */ 613 static int 614 nat44_destroy(struct ip_fw_chain *chain, ip_fw3_opheader *op3, 615 struct sockopt_data *sd) 616 { 617 ipfw_obj_header *oh; 618 struct cfg_nat *ptr; 619 ipfw_obj_ntlv *ntlv; 620 621 /* Check minimum header size */ 622 if (sd->valsize < sizeof(*oh)) 623 return (EINVAL); 624 625 oh = (ipfw_obj_header *)sd->kbuf; 626 627 /* Basic length checks for TLVs */ 628 if (oh->ntlv.head.length != sizeof(oh->ntlv)) 629 return (EINVAL); 630 631 ntlv = &oh->ntlv; 632 /* Check if name is properly terminated */ 633 if (strnlen(ntlv->name, sizeof(ntlv->name)) == sizeof(ntlv->name)) 634 return (EINVAL); 635 636 IPFW_UH_WLOCK(chain); 637 ptr = lookup_nat_name(&chain->nat, ntlv->name); 638 if (ptr == NULL) { 639 IPFW_UH_WUNLOCK(chain); 640 return (ESRCH); 641 } 642 IPFW_WLOCK(chain); 643 LIST_REMOVE(ptr, _next); 644 flush_nat_ptrs(chain, ptr->id); 645 IPFW_WUNLOCK(chain); 646 IPFW_UH_WUNLOCK(chain); 647 648 free_nat_instance(ptr); 649 650 return (0); 651 } 652 653 static void 654 export_nat_cfg(struct cfg_nat *ptr, struct nat44_cfg_nat *ucfg) 655 { 656 657 snprintf(ucfg->name, sizeof(ucfg->name), "%d", ptr->id); 658 ucfg->ip = ptr->ip; 659 ucfg->redir_cnt = ptr->redir_cnt; 660 ucfg->mode = ptr->mode; 661 strlcpy(ucfg->if_name, ptr->if_name, sizeof(ucfg->if_name)); 662 } 663 664 /* 665 * Gets config for given nat instance 666 * Data layout (v0)(current): 667 * Request: [ ipfw_obj_header nat44_cfg_nat .. ] 668 * 669 * Returns 0 on success 670 */ 671 static int 672 nat44_get_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3, 673 struct sockopt_data *sd) 674 { 675 ipfw_obj_header *oh; 676 struct nat44_cfg_nat *ucfg; 677 struct cfg_nat *ptr; 678 struct cfg_redir *r; 679 struct cfg_spool *s; 680 struct nat44_cfg_redir *ser_r; 681 struct nat44_cfg_spool *ser_s; 682 size_t sz; 683 684 sz = sizeof(*oh) + sizeof(*ucfg); 685 /* Check minimum header size */ 686 if (sd->valsize < sz) 687 return (EINVAL); 688 689 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz); 690 691 /* Basic length checks for TLVs */ 692 if (oh->ntlv.head.length != sizeof(oh->ntlv)) 693 return (EINVAL); 694 695 ucfg = (struct nat44_cfg_nat *)(oh + 1); 696 697 /* Check if name is properly terminated */ 698 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name)) 699 return (EINVAL); 700 701 IPFW_UH_RLOCK(chain); 702 ptr = lookup_nat_name(&chain->nat, ucfg->name); 703 if (ptr == NULL) { 704 IPFW_UH_RUNLOCK(chain); 705 return (ESRCH); 706 } 707 708 export_nat_cfg(ptr, ucfg); 709 710 /* Estimate memory amount */ 711 sz = sizeof(ipfw_obj_header) + sizeof(struct nat44_cfg_nat); 712 LIST_FOREACH(r, &ptr->redir_chain, _next) { 713 sz += sizeof(struct nat44_cfg_redir); 714 LIST_FOREACH(s, &r->spool_chain, _next) 715 sz += sizeof(struct nat44_cfg_spool); 716 } 717 718 ucfg->size = sz; 719 if (sd->valsize < sz) { 720 /* 721 * Submitted buffer size is not enough. 722 * WE've already filled in @ucfg structure with 723 * relevant info including size, so we 724 * can return. Buffer will be flushed automatically. 725 */ 726 IPFW_UH_RUNLOCK(chain); 727 return (ENOMEM); 728 } 729 730 /* Size OK, let's copy data */ 731 LIST_FOREACH(r, &ptr->redir_chain, _next) { 732 ser_r = (struct nat44_cfg_redir *)ipfw_get_sopt_space(sd, 733 sizeof(*ser_r)); 734 ser_r->mode = r->mode; 735 ser_r->laddr = r->laddr; 736 ser_r->paddr = r->paddr; 737 ser_r->raddr = r->raddr; 738 ser_r->lport = r->lport; 739 ser_r->pport = r->pport; 740 ser_r->rport = r->rport; 741 ser_r->pport_cnt = r->pport_cnt; 742 ser_r->rport_cnt = r->rport_cnt; 743 ser_r->proto = r->proto; 744 ser_r->spool_cnt = r->spool_cnt; 745 746 LIST_FOREACH(s, &r->spool_chain, _next) { 747 ser_s = (struct nat44_cfg_spool *)ipfw_get_sopt_space( 748 sd, sizeof(*ser_s)); 749 750 ser_s->addr = s->addr; 751 ser_s->port = s->port; 752 } 753 } 754 755 IPFW_UH_RUNLOCK(chain); 756 757 return (0); 758 } 759 760 /* 761 * Lists all nat44 instances currently available in kernel. 762 * Data layout (v0)(current): 763 * Request: [ ipfw_obj_lheader ] 764 * Reply: [ ipfw_obj_lheader nat44_cfg_nat x N ] 765 * 766 * Returns 0 on success 767 */ 768 static int 769 nat44_list_nat(struct ip_fw_chain *chain, ip_fw3_opheader *op3, 770 struct sockopt_data *sd) 771 { 772 ipfw_obj_lheader *olh; 773 struct nat44_cfg_nat *ucfg; 774 struct cfg_nat *ptr; 775 int nat_count; 776 777 /* Check minimum header size */ 778 if (sd->valsize < sizeof(ipfw_obj_lheader)) 779 return (EINVAL); 780 781 olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh)); 782 IPFW_UH_RLOCK(chain); 783 nat_count = 0; 784 LIST_FOREACH(ptr, &chain->nat, _next) 785 nat_count++; 786 787 olh->count = nat_count; 788 olh->objsize = sizeof(struct nat44_cfg_nat); 789 olh->size = sizeof(*olh) + olh->count * olh->objsize; 790 791 if (sd->valsize < olh->size) { 792 IPFW_UH_RUNLOCK(chain); 793 return (ENOMEM); 794 } 795 796 LIST_FOREACH(ptr, &chain->nat, _next) { 797 ucfg = (struct nat44_cfg_nat *)ipfw_get_sopt_space(sd, 798 sizeof(*ucfg)); 799 export_nat_cfg(ptr, ucfg); 800 } 801 802 IPFW_UH_RUNLOCK(chain); 803 804 return (0); 805 } 806 807 /* 808 * Gets log for given nat instance 809 * Data layout (v0)(current): 810 * Request: [ ipfw_obj_header nat44_cfg_nat ] 811 * Reply: [ ipfw_obj_header nat44_cfg_nat LOGBUFFER ] 812 * 813 * Returns 0 on success 814 */ 815 static int 816 nat44_get_log(struct ip_fw_chain *chain, ip_fw3_opheader *op3, 817 struct sockopt_data *sd) 818 { 819 ipfw_obj_header *oh; 820 struct nat44_cfg_nat *ucfg; 821 struct cfg_nat *ptr; 822 void *pbuf; 823 size_t sz; 824 825 sz = sizeof(*oh) + sizeof(*ucfg); 826 /* Check minimum header size */ 827 if (sd->valsize < sz) 828 return (EINVAL); 829 830 oh = (struct _ipfw_obj_header *)ipfw_get_sopt_header(sd, sz); 831 832 /* Basic length checks for TLVs */ 833 if (oh->ntlv.head.length != sizeof(oh->ntlv)) 834 return (EINVAL); 835 836 ucfg = (struct nat44_cfg_nat *)(oh + 1); 837 838 /* Check if name is properly terminated */ 839 if (strnlen(ucfg->name, sizeof(ucfg->name)) == sizeof(ucfg->name)) 840 return (EINVAL); 841 842 IPFW_UH_RLOCK(chain); 843 ptr = lookup_nat_name(&chain->nat, ucfg->name); 844 if (ptr == NULL) { 845 IPFW_UH_RUNLOCK(chain); 846 return (ESRCH); 847 } 848 849 if (ptr->lib->logDesc == NULL) { 850 IPFW_UH_RUNLOCK(chain); 851 return (ENOENT); 852 } 853 854 export_nat_cfg(ptr, ucfg); 855 856 /* Estimate memory amount */ 857 ucfg->size = sizeof(struct nat44_cfg_nat) + LIBALIAS_BUF_SIZE; 858 if (sd->valsize < sz + sizeof(*oh)) { 859 /* 860 * Submitted buffer size is not enough. 861 * WE've already filled in @ucfg structure with 862 * relevant info including size, so we 863 * can return. Buffer will be flushed automatically. 864 */ 865 IPFW_UH_RUNLOCK(chain); 866 return (ENOMEM); 867 } 868 869 pbuf = (void *)ipfw_get_sopt_space(sd, LIBALIAS_BUF_SIZE); 870 memcpy(pbuf, ptr->lib->logDesc, LIBALIAS_BUF_SIZE); 871 872 IPFW_UH_RUNLOCK(chain); 873 874 return (0); 875 } 876 877 static struct ipfw_sopt_handler scodes[] = { 878 { IP_FW_NAT44_XCONFIG, 0, HDIR_SET, nat44_cfg }, 879 { IP_FW_NAT44_DESTROY, 0, HDIR_SET, nat44_destroy }, 880 { IP_FW_NAT44_XGETCONFIG, 0, HDIR_GET, nat44_get_cfg }, 881 { IP_FW_NAT44_LIST_NAT, 0, HDIR_GET, nat44_list_nat }, 882 { IP_FW_NAT44_XGETLOG, 0, HDIR_GET, nat44_get_log }, 883 }; 884 885 /* 886 * Legacy configuration routines 887 */ 888 889 struct cfg_spool_legacy { 890 LIST_ENTRY(cfg_spool_legacy) _next; 891 struct in_addr addr; 892 u_short port; 893 }; 894 895 struct cfg_redir_legacy { 896 LIST_ENTRY(cfg_redir) _next; 897 u_int16_t mode; 898 struct in_addr laddr; 899 struct in_addr paddr; 900 struct in_addr raddr; 901 u_short lport; 902 u_short pport; 903 u_short rport; 904 u_short pport_cnt; 905 u_short rport_cnt; 906 int proto; 907 struct alias_link **alink; 908 u_int16_t spool_cnt; 909 LIST_HEAD(, cfg_spool_legacy) spool_chain; 910 }; 911 912 struct cfg_nat_legacy { 913 LIST_ENTRY(cfg_nat_legacy) _next; 914 int id; 915 struct in_addr ip; 916 char if_name[IF_NAMESIZE]; 917 int mode; 918 struct libalias *lib; 919 int redir_cnt; 920 LIST_HEAD(, cfg_redir_legacy) redir_chain; 921 }; 922 923 static int 924 ipfw_nat_cfg(struct sockopt *sopt) 925 { 926 struct cfg_nat_legacy *cfg; 927 struct nat44_cfg_nat *ucfg; 928 struct cfg_redir_legacy *rdir; 929 struct nat44_cfg_redir *urdir; 930 char *buf; 931 size_t len, len2; 932 int error, i; 933 934 len = sopt->sopt_valsize; 935 len2 = len + 128; 936 937 /* 938 * Allocate 2x buffer to store converted structures. 939 * new redir_cfg has shrunk, so we're sure that 940 * new buffer size is enough. 941 */ 942 buf = malloc(roundup2(len, 8) + len2, M_TEMP, M_WAITOK | M_ZERO); 943 error = sooptcopyin(sopt, buf, len, sizeof(struct cfg_nat_legacy)); 944 if (error != 0) 945 goto out; 946 947 cfg = (struct cfg_nat_legacy *)buf; 948 if (cfg->id < 0) { 949 error = EINVAL; 950 goto out; 951 } 952 953 ucfg = (struct nat44_cfg_nat *)&buf[roundup2(len, 8)]; 954 snprintf(ucfg->name, sizeof(ucfg->name), "%d", cfg->id); 955 strlcpy(ucfg->if_name, cfg->if_name, sizeof(ucfg->if_name)); 956 ucfg->ip = cfg->ip; 957 ucfg->mode = cfg->mode; 958 ucfg->redir_cnt = cfg->redir_cnt; 959 960 if (len < sizeof(*cfg) + cfg->redir_cnt * sizeof(*rdir)) { 961 error = EINVAL; 962 goto out; 963 } 964 965 urdir = (struct nat44_cfg_redir *)(ucfg + 1); 966 rdir = (struct cfg_redir_legacy *)(cfg + 1); 967 for (i = 0; i < cfg->redir_cnt; i++) { 968 urdir->mode = rdir->mode; 969 urdir->laddr = rdir->laddr; 970 urdir->paddr = rdir->paddr; 971 urdir->raddr = rdir->raddr; 972 urdir->lport = rdir->lport; 973 urdir->pport = rdir->pport; 974 urdir->rport = rdir->rport; 975 urdir->pport_cnt = rdir->pport_cnt; 976 urdir->rport_cnt = rdir->rport_cnt; 977 urdir->proto = rdir->proto; 978 urdir->spool_cnt = rdir->spool_cnt; 979 980 urdir++; 981 rdir++; 982 } 983 984 nat44_config(&V_layer3_chain, ucfg); 985 986 out: 987 free(buf, M_TEMP); 988 return (error); 989 } 990 991 static int 992 ipfw_nat_del(struct sockopt *sopt) 993 { 994 struct cfg_nat *ptr; 995 struct ip_fw_chain *chain = &V_layer3_chain; 996 int i; 997 998 sooptcopyin(sopt, &i, sizeof i, sizeof i); 999 /* XXX validate i */ 1000 IPFW_UH_WLOCK(chain); 1001 ptr = lookup_nat(&chain->nat, i); 1002 if (ptr == NULL) { 1003 IPFW_UH_WUNLOCK(chain); 1004 return (EINVAL); 1005 } 1006 IPFW_WLOCK(chain); 1007 LIST_REMOVE(ptr, _next); 1008 flush_nat_ptrs(chain, i); 1009 IPFW_WUNLOCK(chain); 1010 IPFW_UH_WUNLOCK(chain); 1011 free_nat_instance(ptr); 1012 return (0); 1013 } 1014 1015 static int 1016 ipfw_nat_get_cfg(struct sockopt *sopt) 1017 { 1018 struct ip_fw_chain *chain = &V_layer3_chain; 1019 struct cfg_nat *n; 1020 struct cfg_nat_legacy *ucfg; 1021 struct cfg_redir *r; 1022 struct cfg_spool *s; 1023 struct cfg_redir_legacy *ser_r; 1024 struct cfg_spool_legacy *ser_s; 1025 char *data; 1026 int gencnt, nat_cnt, len, error; 1027 1028 nat_cnt = 0; 1029 len = sizeof(nat_cnt); 1030 1031 IPFW_UH_RLOCK(chain); 1032 retry: 1033 gencnt = chain->gencnt; 1034 /* Estimate memory amount */ 1035 LIST_FOREACH(n, &chain->nat, _next) { 1036 nat_cnt++; 1037 len += sizeof(struct cfg_nat_legacy); 1038 LIST_FOREACH(r, &n->redir_chain, _next) { 1039 len += sizeof(struct cfg_redir_legacy); 1040 LIST_FOREACH(s, &r->spool_chain, _next) 1041 len += sizeof(struct cfg_spool_legacy); 1042 } 1043 } 1044 IPFW_UH_RUNLOCK(chain); 1045 1046 data = malloc(len, M_TEMP, M_WAITOK | M_ZERO); 1047 bcopy(&nat_cnt, data, sizeof(nat_cnt)); 1048 1049 nat_cnt = 0; 1050 len = sizeof(nat_cnt); 1051 1052 IPFW_UH_RLOCK(chain); 1053 if (gencnt != chain->gencnt) { 1054 free(data, M_TEMP); 1055 goto retry; 1056 } 1057 /* Serialize all the data. */ 1058 LIST_FOREACH(n, &chain->nat, _next) { 1059 ucfg = (struct cfg_nat_legacy *)&data[len]; 1060 ucfg->id = n->id; 1061 ucfg->ip = n->ip; 1062 ucfg->redir_cnt = n->redir_cnt; 1063 ucfg->mode = n->mode; 1064 strlcpy(ucfg->if_name, n->if_name, sizeof(ucfg->if_name)); 1065 len += sizeof(struct cfg_nat_legacy); 1066 LIST_FOREACH(r, &n->redir_chain, _next) { 1067 ser_r = (struct cfg_redir_legacy *)&data[len]; 1068 ser_r->mode = r->mode; 1069 ser_r->laddr = r->laddr; 1070 ser_r->paddr = r->paddr; 1071 ser_r->raddr = r->raddr; 1072 ser_r->lport = r->lport; 1073 ser_r->pport = r->pport; 1074 ser_r->rport = r->rport; 1075 ser_r->pport_cnt = r->pport_cnt; 1076 ser_r->rport_cnt = r->rport_cnt; 1077 ser_r->proto = r->proto; 1078 ser_r->spool_cnt = r->spool_cnt; 1079 len += sizeof(struct cfg_redir_legacy); 1080 LIST_FOREACH(s, &r->spool_chain, _next) { 1081 ser_s = (struct cfg_spool_legacy *)&data[len]; 1082 ser_s->addr = s->addr; 1083 ser_s->port = s->port; 1084 len += sizeof(struct cfg_spool_legacy); 1085 } 1086 } 1087 } 1088 IPFW_UH_RUNLOCK(chain); 1089 1090 error = sooptcopyout(sopt, data, len); 1091 free(data, M_TEMP); 1092 1093 return (error); 1094 } 1095 1096 static int 1097 ipfw_nat_get_log(struct sockopt *sopt) 1098 { 1099 uint8_t *data; 1100 struct cfg_nat *ptr; 1101 int i, size; 1102 struct ip_fw_chain *chain; 1103 IPFW_RLOCK_TRACKER; 1104 1105 chain = &V_layer3_chain; 1106 1107 IPFW_RLOCK(chain); 1108 /* one pass to count, one to copy the data */ 1109 i = 0; 1110 LIST_FOREACH(ptr, &chain->nat, _next) { 1111 if (ptr->lib->logDesc == NULL) 1112 continue; 1113 i++; 1114 } 1115 size = i * (LIBALIAS_BUF_SIZE + sizeof(int)); 1116 data = malloc(size, M_IPFW, M_NOWAIT | M_ZERO); 1117 if (data == NULL) { 1118 IPFW_RUNLOCK(chain); 1119 return (ENOSPC); 1120 } 1121 i = 0; 1122 LIST_FOREACH(ptr, &chain->nat, _next) { 1123 if (ptr->lib->logDesc == NULL) 1124 continue; 1125 bcopy(&ptr->id, &data[i], sizeof(int)); 1126 i += sizeof(int); 1127 bcopy(ptr->lib->logDesc, &data[i], LIBALIAS_BUF_SIZE); 1128 i += LIBALIAS_BUF_SIZE; 1129 } 1130 IPFW_RUNLOCK(chain); 1131 sooptcopyout(sopt, data, size); 1132 free(data, M_IPFW); 1133 return(0); 1134 } 1135 1136 static int 1137 vnet_ipfw_nat_init(const void *arg __unused) 1138 { 1139 1140 V_ipfw_nat_ready = 1; 1141 return (0); 1142 } 1143 1144 static int 1145 vnet_ipfw_nat_uninit(const void *arg __unused) 1146 { 1147 struct cfg_nat *ptr, *ptr_temp; 1148 struct ip_fw_chain *chain; 1149 1150 chain = &V_layer3_chain; 1151 IPFW_WLOCK(chain); 1152 V_ipfw_nat_ready = 0; 1153 LIST_FOREACH_SAFE(ptr, &chain->nat, _next, ptr_temp) { 1154 LIST_REMOVE(ptr, _next); 1155 free_nat_instance(ptr); 1156 } 1157 flush_nat_ptrs(chain, -1 /* flush all */); 1158 IPFW_WUNLOCK(chain); 1159 return (0); 1160 } 1161 1162 static void 1163 ipfw_nat_init(void) 1164 { 1165 1166 /* init ipfw hooks */ 1167 ipfw_nat_ptr = ipfw_nat; 1168 lookup_nat_ptr = lookup_nat; 1169 ipfw_nat_cfg_ptr = ipfw_nat_cfg; 1170 ipfw_nat_del_ptr = ipfw_nat_del; 1171 ipfw_nat_get_cfg_ptr = ipfw_nat_get_cfg; 1172 ipfw_nat_get_log_ptr = ipfw_nat_get_log; 1173 IPFW_ADD_SOPT_HANDLER(1, scodes); 1174 1175 ifaddr_event_tag = EVENTHANDLER_REGISTER(ifaddr_event, ifaddr_change, 1176 NULL, EVENTHANDLER_PRI_ANY); 1177 } 1178 1179 static void 1180 ipfw_nat_destroy(void) 1181 { 1182 1183 EVENTHANDLER_DEREGISTER(ifaddr_event, ifaddr_event_tag); 1184 /* deregister ipfw_nat */ 1185 IPFW_DEL_SOPT_HANDLER(1, scodes); 1186 ipfw_nat_ptr = NULL; 1187 lookup_nat_ptr = NULL; 1188 ipfw_nat_cfg_ptr = NULL; 1189 ipfw_nat_del_ptr = NULL; 1190 ipfw_nat_get_cfg_ptr = NULL; 1191 ipfw_nat_get_log_ptr = NULL; 1192 } 1193 1194 static int 1195 ipfw_nat_modevent(module_t mod, int type, void *unused) 1196 { 1197 int err = 0; 1198 1199 switch (type) { 1200 case MOD_LOAD: 1201 break; 1202 1203 case MOD_UNLOAD: 1204 break; 1205 1206 default: 1207 return EOPNOTSUPP; 1208 break; 1209 } 1210 return err; 1211 } 1212 1213 static moduledata_t ipfw_nat_mod = { 1214 "ipfw_nat", 1215 ipfw_nat_modevent, 1216 0 1217 }; 1218 1219 /* Define startup order. */ 1220 #define IPFW_NAT_SI_SUB_FIREWALL SI_SUB_PROTO_FIREWALL 1221 #define IPFW_NAT_MODEVENT_ORDER (SI_ORDER_ANY - 128) /* after ipfw */ 1222 #define IPFW_NAT_MODULE_ORDER (IPFW_NAT_MODEVENT_ORDER + 1) 1223 #define IPFW_NAT_VNET_ORDER (IPFW_NAT_MODEVENT_ORDER + 2) 1224 1225 DECLARE_MODULE(ipfw_nat, ipfw_nat_mod, IPFW_NAT_SI_SUB_FIREWALL, SI_ORDER_ANY); 1226 MODULE_DEPEND(ipfw_nat, libalias, 1, 1, 1); 1227 MODULE_DEPEND(ipfw_nat, ipfw, 3, 3, 3); 1228 MODULE_VERSION(ipfw_nat, 1); 1229 1230 SYSINIT(ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER, 1231 ipfw_nat_init, NULL); 1232 VNET_SYSINIT(vnet_ipfw_nat_init, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_VNET_ORDER, 1233 vnet_ipfw_nat_init, NULL); 1234 1235 SYSUNINIT(ipfw_nat_destroy, IPFW_NAT_SI_SUB_FIREWALL, IPFW_NAT_MODULE_ORDER, 1236 ipfw_nat_destroy, NULL); 1237 VNET_SYSUNINIT(vnet_ipfw_nat_uninit, IPFW_NAT_SI_SUB_FIREWALL, 1238 IPFW_NAT_VNET_ORDER, vnet_ipfw_nat_uninit, NULL); 1239 1240 /* end of file */ 1241