1 /* $NetBSD: ip_nat.c,v 1.6 2012/07/30 19:27:46 pgoyette Exp $ */ 2 3 /* 4 * Copyright (C) 2012 by Darren Reed. 5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 */ 8 #if defined(KERNEL) || defined(_KERNEL) 9 # undef KERNEL 10 # undef KERNEL 11 # define KERNEL 1 12 # define KERNEL 1 13 #endif 14 #include <sys/errno.h> 15 #include <sys/types.h> 16 #include <sys/param.h> 17 #include <sys/time.h> 18 #include <sys/file.h> 19 #if defined(_KERNEL) && \ 20 (defined(__NetBSD_Version) && (__NetBSD_Version >= 399002000)) 21 # include <sys/kauth.h> 22 #endif 23 #if !defined(_KERNEL) 24 # include <stdio.h> 25 # include <string.h> 26 # include <stdlib.h> 27 # define KERNEL 28 # ifdef _OpenBSD__ 29 struct file; 30 # endif 31 # include <sys/uio.h> 32 # undef KERNEL 33 #endif 34 #if defined(_KERNEL) && \ 35 defined(__FreeBSD_version) && (__FreeBSD_version >= 220000) 36 # include <sys/filio.h> 37 # include <sys/fcntl.h> 38 #else 39 # include <sys/ioctl.h> 40 #endif 41 #if !defined(AIX) 42 # include <sys/fcntl.h> 43 #endif 44 #if !defined(linux) 45 # include <sys/protosw.h> 46 #endif 47 #include <sys/socket.h> 48 #if defined(_KERNEL) 49 # include <sys/systm.h> 50 # if !defined(__SVR4) && !defined(__svr4__) 51 # include <sys/mbuf.h> 52 # endif 53 #endif 54 #if defined(__SVR4) || defined(__svr4__) 55 # include <sys/filio.h> 56 # include <sys/byteorder.h> 57 # ifdef KERNEL 58 # include <sys/dditypes.h> 59 # endif 60 # include <sys/stream.h> 61 # include <sys/kmem.h> 62 #endif 63 #if _FreeBSD_version >= 300000 64 # include <sys/queue.h> 65 #endif 66 #include <net/if.h> 67 #if _FreeBSD_version >= 300000 68 # include <net/if_var.h> 69 #endif 70 #ifdef sun 71 # include <net/af.h> 72 #endif 73 #include <netinet/in.h> 74 #include <netinet/in_systm.h> 75 #include <netinet/ip.h> 76 77 #ifdef RFC1825 78 # include <vpn/md5.h> 79 # include <vpn/ipsec.h> 80 extern struct ifnet vpnif; 81 #endif 82 83 #if !defined(linux) 84 # include <netinet/ip_var.h> 85 #endif 86 #include <netinet/tcp.h> 87 #include <netinet/udp.h> 88 #include <netinet/ip_icmp.h> 89 #include "netinet/ip_compat.h" 90 #include <netinet/tcpip.h> 91 #include "netinet/ipl.h" 92 #include "netinet/ip_fil.h" 93 #include "netinet/ip_nat.h" 94 #include "netinet/ip_frag.h" 95 #include "netinet/ip_state.h" 96 #include "netinet/ip_proxy.h" 97 #include "netinet/ip_lookup.h" 98 #include "netinet/ip_dstlist.h" 99 #include "netinet/ip_sync.h" 100 #if FREEBSD_GE_REV(300000) 101 # include <sys/malloc.h> 102 #endif 103 #ifdef HAS_SYS_MD5_H 104 # include <sys/md5.h> 105 #else 106 # include "md5.h" 107 #endif 108 /* END OF INCLUDES */ 109 110 #undef SOCKADDR_IN 111 #define SOCKADDR_IN struct sockaddr_in 112 113 #if !defined(lint) 114 #if defined(__NetBSD__) 115 #include <sys/cdefs.h> 116 __KERNEL_RCSID(0, "$NetBSD: ip_nat.c,v 1.6 2012/07/30 19:27:46 pgoyette Exp $"); 117 #else 118 static const char sccsid[] = "@(#)ip_nat.c 1.11 6/5/96 (C) 1995 Darren Reed"; 119 static const char rcsid[] = "@(#)Id: ip_nat.c,v 1.1.1.2 2012/07/22 13:45:27 darrenr Exp"; 120 #endif 121 #endif 122 123 124 #define NATFSUM(n,v,f) ((v) == 4 ? (n)->f.in4.s_addr : (n)->f.i6[0] + \ 125 (n)->f.i6[1] + (n)->f.i6[2] + (n)->f.i6[3]) 126 #define NBUMP(x) softn->(x)++ 127 #define NBUMPD(x, y) do { \ 128 softn->x.y++; \ 129 DT(y); \ 130 } while (0) 131 #define NBUMPSIDE(y,x) softn->ipf_nat_stats.ns_side[y].x++ 132 #define NBUMPSIDED(y,x) do { softn->ipf_nat_stats.ns_side[y].x++; \ 133 DT(x); } while (0) 134 #define NBUMPSIDEX(y,x,z) \ 135 do { softn->ipf_nat_stats.ns_side[y].x++; \ 136 DT(z); } while (0) 137 #define NBUMPSIDEDF(y,x)do { softn->ipf_nat_stats.ns_side[y].x++; \ 138 DT1(x, fr_info_t *, fin); } while (0) 139 140 frentry_t ipfnatblock; 141 142 static ipftuneable_t ipf_nat_tuneables[] = { 143 /* nat */ 144 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_lock) }, 145 "nat_lock", 0, 1, 146 stsizeof(ipf_nat_softc_t, ipf_nat_lock), 147 IPFT_RDONLY, NULL, NULL }, 148 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_sz) }, 149 "nat_table_size", 1, 0x7fffffff, 150 stsizeof(ipf_nat_softc_t, ipf_nat_table_sz), 151 0, NULL, ipf_nat_rehash }, 152 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_max) }, 153 "nat_table_max", 1, 0x7fffffff, 154 stsizeof(ipf_nat_softc_t, ipf_nat_table_max), 155 0, NULL, NULL }, 156 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maprules_sz) }, 157 "nat_rules_size", 1, 0x7fffffff, 158 stsizeof(ipf_nat_softc_t, ipf_nat_maprules_sz), 159 0, NULL, ipf_nat_rehash_rules }, 160 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_rdrrules_sz) }, 161 "rdr_rules_size", 1, 0x7fffffff, 162 stsizeof(ipf_nat_softc_t, ipf_nat_rdrrules_sz), 163 0, NULL, ipf_nat_rehash_rules }, 164 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_hostmap_sz) }, 165 "hostmap_size", 1, 0x7fffffff, 166 stsizeof(ipf_nat_softc_t, ipf_nat_hostmap_sz), 167 0, NULL, ipf_nat_hostmap_rehash }, 168 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_maxbucket) }, 169 "nat_maxbucket",1, 0x7fffffff, 170 stsizeof(ipf_nat_softc_t, ipf_nat_maxbucket), 171 0, NULL, NULL }, 172 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_logging) }, 173 "nat_logging", 0, 1, 174 stsizeof(ipf_nat_softc_t, ipf_nat_logging), 175 0, NULL, NULL }, 176 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_doflush) }, 177 "nat_doflush", 0, 1, 178 stsizeof(ipf_nat_softc_t, ipf_nat_doflush), 179 0, NULL, NULL }, 180 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_low) }, 181 "nat_table_wm_low", 1, 99, 182 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_low), 183 0, NULL, NULL }, 184 { { (void *)offsetof(ipf_nat_softc_t, ipf_nat_table_wm_high) }, 185 "nat_table_wm_high", 2, 100, 186 stsizeof(ipf_nat_softc_t, ipf_nat_table_wm_high), 187 0, NULL, NULL }, 188 { { 0 }, 189 NULL, 0, 0, 190 0, 191 0, NULL, NULL } 192 }; 193 194 /* ======================================================================== */ 195 /* How the NAT is organised and works. */ 196 /* */ 197 /* Inside (interface y) NAT Outside (interface x) */ 198 /* -------------------- -+- ------------------------------------- */ 199 /* Packet going | out, processsed by ipf_nat_checkout() for x */ 200 /* ------------> | ------------> */ 201 /* src=10.1.1.1 | src=192.1.1.1 */ 202 /* | */ 203 /* | in, processed by ipf_nat_checkin() for x */ 204 /* <------------ | <------------ */ 205 /* dst=10.1.1.1 | dst=192.1.1.1 */ 206 /* -------------------- -+- ------------------------------------- */ 207 /* ipf_nat_checkout() - changes ip_src and if required, sport */ 208 /* - creates a new mapping, if required. */ 209 /* ipf_nat_checkin() - changes ip_dst and if required, dport */ 210 /* */ 211 /* In the NAT table, internal source is recorded as "in" and externally */ 212 /* seen as "out". */ 213 /* ======================================================================== */ 214 215 216 #if SOLARIS && !defined(INSTANCES) 217 extern int pfil_delayed_copy; 218 #endif 219 220 static int ipf_nat_flush_entry(ipf_main_softc_t *, void *); 221 static int ipf_nat_getent(ipf_main_softc_t *, void *, int); 222 static int ipf_nat_getsz(ipf_main_softc_t *, void *, int); 223 static int ipf_nat_putent(ipf_main_softc_t *, void *, int); 224 static void ipf_nat_addmap(ipf_nat_softc_t *, ipnat_t *); 225 static void ipf_nat_addrdr(ipf_nat_softc_t *, ipnat_t *); 226 static int ipf_nat_builddivertmp(ipf_nat_softc_t *, ipnat_t *); 227 static int ipf_nat_clearlist(ipf_main_softc_t *, ipf_nat_softc_t *); 228 static int ipf_nat_cmp_rules(ipnat_t *, ipnat_t *); 229 static int ipf_nat_decap(fr_info_t *, nat_t *); 230 static void ipf_nat_delrule(ipf_main_softc_t *, ipf_nat_softc_t *, 231 ipnat_t *, int); 232 static int ipf_nat_extraflush(ipf_main_softc_t *, ipf_nat_softc_t *, int); 233 static int ipf_nat_finalise(fr_info_t *, nat_t *); 234 static int ipf_nat_flushtable(ipf_main_softc_t *, ipf_nat_softc_t *); 235 static int ipf_nat_getnext(ipf_main_softc_t *, ipftoken_t *, 236 ipfgeniter_t *, ipfobj_t *); 237 static int ipf_nat_gettable(ipf_main_softc_t *, ipf_nat_softc_t *, char *); 238 static hostmap_t *ipf_nat_hostmap(ipf_nat_softc_t *, ipnat_t *, 239 struct in_addr, struct in_addr, 240 struct in_addr, u_32_t); 241 static int ipf_nat_icmpquerytype(int); 242 static int ipf_nat_iterator(ipf_main_softc_t *, ipftoken_t *, 243 ipfgeniter_t *, ipfobj_t *); 244 static int ipf_nat_match(fr_info_t *, ipnat_t *); 245 static int ipf_nat_matcharray(nat_t *, int *, u_long); 246 static int ipf_nat_matchflush(ipf_main_softc_t *, ipf_nat_softc_t *, 247 void *); 248 static void ipf_nat_mssclamp(tcphdr_t *, u_32_t, fr_info_t *, u_short *); 249 static int ipf_nat_newmap(fr_info_t *, nat_t *, natinfo_t *); 250 static int ipf_nat_newdivert(fr_info_t *, nat_t *, natinfo_t *); 251 static int ipf_nat_newrdr(fr_info_t *, nat_t *, natinfo_t *); 252 static int ipf_nat_newrewrite(fr_info_t *, nat_t *, natinfo_t *); 253 static int ipf_nat_nextaddr(fr_info_t *, nat_addr_t *, u_32_t *, u_32_t *); 254 static int ipf_nat_nextaddrinit(ipf_main_softc_t *, char *, 255 nat_addr_t *, int, void *); 256 static int ipf_nat_resolverule(ipf_main_softc_t *, ipnat_t *); 257 static int ipf_nat_ruleaddrinit(ipf_main_softc_t *, 258 ipf_nat_softc_t *, ipnat_t *); 259 static void ipf_nat_rule_fini(ipf_main_softc_t *, ipnat_t *); 260 static int ipf_nat_rule_init(ipf_main_softc_t *, ipf_nat_softc_t *, 261 ipnat_t *); 262 static int ipf_nat_siocaddnat(ipf_main_softc_t *, ipf_nat_softc_t *, 263 ipnat_t *, int); 264 static void ipf_nat_siocdelnat(ipf_main_softc_t *, ipf_nat_softc_t *, 265 ipnat_t *, int); 266 static void ipf_nat_tabmove(ipf_nat_softc_t *, nat_t *); 267 268 /* ------------------------------------------------------------------------ */ 269 /* Function: ipf_nat_main_load */ 270 /* Returns: int - 0 == success, -1 == failure */ 271 /* Parameters: Nil */ 272 /* */ 273 /* The only global NAT structure that needs to be initialised is the filter */ 274 /* rule that is used with blocking packets. */ 275 /* ------------------------------------------------------------------------ */ 276 int 277 ipf_nat_main_load(void) 278 { 279 bzero((char *)&ipfnatblock, sizeof(ipfnatblock)); 280 ipfnatblock.fr_flags = FR_BLOCK|FR_QUICK; 281 ipfnatblock.fr_ref = 1; 282 283 return 0; 284 } 285 286 287 /* ------------------------------------------------------------------------ */ 288 /* Function: ipf_nat_main_unload */ 289 /* Returns: int - 0 == success, -1 == failure */ 290 /* Parameters: Nil */ 291 /* */ 292 /* A null-op function that exists as a placeholder so that the flow in */ 293 /* other functions is obvious. */ 294 /* ------------------------------------------------------------------------ */ 295 int 296 ipf_nat_main_unload(void) 297 { 298 return 0; 299 } 300 301 302 /* ------------------------------------------------------------------------ */ 303 /* Function: ipf_nat_soft_create */ 304 /* Returns: void * - NULL = failure, else pointer to NAT context */ 305 /* Parameters: softc(I) - pointer to soft context main structure */ 306 /* */ 307 /* Allocate the initial soft context structure for NAT and populate it with */ 308 /* some default values. Creating the tables is left until we call _init so */ 309 /* that sizes can be changed before we get under way. */ 310 /* ------------------------------------------------------------------------ */ 311 void * 312 ipf_nat_soft_create(ipf_main_softc_t *softc) 313 { 314 ipf_nat_softc_t *softn; 315 316 KMALLOC(softn, ipf_nat_softc_t *); 317 if (softn == NULL) 318 return NULL; 319 320 bzero((char *)softn, sizeof(*softn)); 321 322 softn->ipf_nat_tune = ipf_tune_array_copy(softn, 323 sizeof(ipf_nat_tuneables), 324 ipf_nat_tuneables); 325 if (softn->ipf_nat_tune == NULL) { 326 ipf_nat_soft_destroy(softc, softn); 327 return NULL; 328 } 329 if (ipf_tune_array_link(softc, softn->ipf_nat_tune) == -1) { 330 ipf_nat_soft_destroy(softc, softn); 331 return NULL; 332 } 333 334 softn->ipf_nat_list_tail = &softn->ipf_nat_list; 335 336 softn->ipf_nat_table_max = NAT_TABLE_MAX; 337 softn->ipf_nat_table_sz = NAT_TABLE_SZ; 338 softn->ipf_nat_maprules_sz = NAT_SIZE; 339 softn->ipf_nat_rdrrules_sz = RDR_SIZE; 340 softn->ipf_nat_hostmap_sz = HOSTMAP_SIZE; 341 softn->ipf_nat_doflush = 0; 342 #ifdef IPFILTER_LOG 343 softn->ipf_nat_logging = 1; 344 #else 345 softn->ipf_nat_logging = 0; 346 #endif 347 348 softn->ipf_nat_defage = DEF_NAT_AGE; 349 softn->ipf_nat_defipage = IPF_TTLVAL(60); 350 softn->ipf_nat_deficmpage = IPF_TTLVAL(3); 351 softn->ipf_nat_table_wm_high = 99; 352 softn->ipf_nat_table_wm_low = 90; 353 354 return softn; 355 } 356 357 /* ------------------------------------------------------------------------ */ 358 /* Function: ipf_nat_soft_destroy */ 359 /* Returns: Nil */ 360 /* Parameters: softc(I) - pointer to soft context main structure */ 361 /* */ 362 /* ------------------------------------------------------------------------ */ 363 void 364 ipf_nat_soft_destroy(ipf_main_softc_t *softc, void *arg) 365 { 366 ipf_nat_softc_t *softn = arg; 367 368 if (softn->ipf_nat_tune != NULL) { 369 ipf_tune_array_unlink(softc, softn->ipf_nat_tune); 370 KFREES(softn->ipf_nat_tune, sizeof(ipf_nat_tuneables)); 371 softn->ipf_nat_tune = NULL; 372 } 373 374 KFREE(softn); 375 } 376 377 378 /* ------------------------------------------------------------------------ */ 379 /* Function: ipf_nat_init */ 380 /* Returns: int - 0 == success, -1 == failure */ 381 /* Parameters: softc(I) - pointer to soft context main structure */ 382 /* */ 383 /* Initialise all of the NAT locks, tables and other structures. */ 384 /* ------------------------------------------------------------------------ */ 385 int 386 ipf_nat_soft_init(ipf_main_softc_t *softc, void *arg) 387 { 388 ipf_nat_softc_t *softn = arg; 389 ipftq_t *tq; 390 int i; 391 392 KMALLOCS(softn->ipf_nat_table[0], nat_t **, \ 393 sizeof(nat_t *) * softn->ipf_nat_table_sz); 394 395 if (softn->ipf_nat_table[0] != NULL) { 396 bzero((char *)softn->ipf_nat_table[0], 397 softn->ipf_nat_table_sz * sizeof(nat_t *)); 398 } else { 399 return -1; 400 } 401 402 KMALLOCS(softn->ipf_nat_table[1], nat_t **, \ 403 sizeof(nat_t *) * softn->ipf_nat_table_sz); 404 405 if (softn->ipf_nat_table[1] != NULL) { 406 bzero((char *)softn->ipf_nat_table[1], 407 softn->ipf_nat_table_sz * sizeof(nat_t *)); 408 } else { 409 return -2; 410 } 411 412 KMALLOCS(softn->ipf_nat_map_rules, ipnat_t **, \ 413 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz); 414 415 if (softn->ipf_nat_map_rules != NULL) { 416 bzero((char *)softn->ipf_nat_map_rules, 417 softn->ipf_nat_maprules_sz * sizeof(ipnat_t *)); 418 } else { 419 return -3; 420 } 421 422 KMALLOCS(softn->ipf_nat_rdr_rules, ipnat_t **, \ 423 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz); 424 425 if (softn->ipf_nat_rdr_rules != NULL) { 426 bzero((char *)softn->ipf_nat_rdr_rules, 427 softn->ipf_nat_rdrrules_sz * sizeof(ipnat_t *)); 428 } else { 429 return -4; 430 } 431 432 KMALLOCS(softn->ipf_hm_maptable, hostmap_t **, \ 433 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz); 434 435 if (softn->ipf_hm_maptable != NULL) { 436 bzero((char *)softn->ipf_hm_maptable, 437 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz); 438 } else { 439 return -5; 440 } 441 softn->ipf_hm_maplist = NULL; 442 443 KMALLOCS(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, u_int *, 444 softn->ipf_nat_table_sz * sizeof(u_int)); 445 446 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen == NULL) { 447 return -6; 448 } 449 bzero((char *)softn->ipf_nat_stats.ns_side[0].ns_bucketlen, 450 softn->ipf_nat_table_sz * sizeof(u_int)); 451 452 KMALLOCS(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, u_int *, 453 softn->ipf_nat_table_sz * sizeof(u_int)); 454 455 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen == NULL) { 456 return -7; 457 } 458 459 bzero((char *)softn->ipf_nat_stats.ns_side[1].ns_bucketlen, 460 softn->ipf_nat_table_sz * sizeof(u_int)); 461 462 if (softn->ipf_nat_maxbucket == 0) { 463 for (i = softn->ipf_nat_table_sz; i > 0; i >>= 1) 464 softn->ipf_nat_maxbucket++; 465 softn->ipf_nat_maxbucket *= 2; 466 } 467 468 ipf_sttab_init(softc, softn->ipf_nat_tcptq); 469 /* 470 * Increase this because we may have "keep state" following this too 471 * and packet storms can occur if this is removed too quickly. 472 */ 473 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack; 474 softn->ipf_nat_tcptq[IPF_TCP_NSTATES - 1].ifq_next = 475 &softn->ipf_nat_udptq; 476 477 IPFTQ_INIT(&softn->ipf_nat_udptq, softn->ipf_nat_defage, 478 "nat ipftq udp tab"); 479 softn->ipf_nat_udptq.ifq_next = &softn->ipf_nat_udpacktq; 480 481 IPFTQ_INIT(&softn->ipf_nat_udpacktq, softn->ipf_nat_defage, 482 "nat ipftq udpack tab"); 483 softn->ipf_nat_udpacktq.ifq_next = &softn->ipf_nat_icmptq; 484 485 IPFTQ_INIT(&softn->ipf_nat_icmptq, softn->ipf_nat_deficmpage, 486 "nat icmp ipftq tab"); 487 softn->ipf_nat_icmptq.ifq_next = &softn->ipf_nat_icmpacktq; 488 489 IPFTQ_INIT(&softn->ipf_nat_icmpacktq, softn->ipf_nat_defage, 490 "nat icmpack ipftq tab"); 491 softn->ipf_nat_icmpacktq.ifq_next = &softn->ipf_nat_iptq; 492 493 IPFTQ_INIT(&softn->ipf_nat_iptq, softn->ipf_nat_defipage, 494 "nat ip ipftq tab"); 495 softn->ipf_nat_iptq.ifq_next = &softn->ipf_nat_pending; 496 497 IPFTQ_INIT(&softn->ipf_nat_pending, 1, "nat pending ipftq tab"); 498 softn->ipf_nat_pending.ifq_next = NULL; 499 500 for (i = 0, tq = softn->ipf_nat_tcptq; i < IPF_TCP_NSTATES; i++, tq++) { 501 if (tq->ifq_ttl < softn->ipf_nat_deficmpage) 502 tq->ifq_ttl = softn->ipf_nat_deficmpage; 503 #ifdef LARGE_NAT 504 else if (tq->ifq_ttl > softn->ipf_nat_defage) 505 tq->ifq_ttl = softn->ipf_nat_defage; 506 #endif 507 } 508 509 /* 510 * Increase this because we may have "keep state" following 511 * this too and packet storms can occur if this is removed 512 * too quickly. 513 */ 514 softn->ipf_nat_tcptq[IPF_TCPS_CLOSED].ifq_ttl = softc->ipf_tcplastack; 515 516 MUTEX_INIT(&softn->ipf_nat_new, "ipf nat new mutex"); 517 MUTEX_INIT(&softn->ipf_nat_io, "ipf nat io mutex"); 518 519 softn->ipf_nat_inited = 1; 520 521 return 0; 522 } 523 524 525 /* ------------------------------------------------------------------------ */ 526 /* Function: ipf_nat_soft_fini */ 527 /* Returns: Nil */ 528 /* Parameters: softc(I) - pointer to soft context main structure */ 529 /* */ 530 /* Free all memory used by NAT structures allocated at runtime. */ 531 /* ------------------------------------------------------------------------ */ 532 int 533 ipf_nat_soft_fini(ipf_main_softc_t *softc, void *arg) 534 { 535 ipf_nat_softc_t *softn = arg; 536 ipftq_t *ifq, *ifqnext; 537 538 (void) ipf_nat_clearlist(softc, softn); 539 (void) ipf_nat_flushtable(softc, softn); 540 541 /* 542 * Proxy timeout queues are not cleaned here because although they 543 * exist on the NAT list, ipf_proxy_unload is called after unload 544 * and the proxies actually are responsible for them being created. 545 * Should the proxy timeouts have their own list? There's no real 546 * justification as this is the only complication. 547 */ 548 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) { 549 ifqnext = ifq->ifq_next; 550 if (ipf_deletetimeoutqueue(ifq) == 0) 551 ipf_freetimeoutqueue(softc, ifq); 552 } 553 554 if (softn->ipf_nat_table[0] != NULL) { 555 KFREES(softn->ipf_nat_table[0], 556 sizeof(nat_t *) * softn->ipf_nat_table_sz); 557 softn->ipf_nat_table[0] = NULL; 558 } 559 if (softn->ipf_nat_table[1] != NULL) { 560 KFREES(softn->ipf_nat_table[1], 561 sizeof(nat_t *) * softn->ipf_nat_table_sz); 562 softn->ipf_nat_table[1] = NULL; 563 } 564 if (softn->ipf_nat_map_rules != NULL) { 565 KFREES(softn->ipf_nat_map_rules, 566 sizeof(ipnat_t *) * softn->ipf_nat_maprules_sz); 567 softn->ipf_nat_map_rules = NULL; 568 } 569 if (softn->ipf_nat_rdr_rules != NULL) { 570 KFREES(softn->ipf_nat_rdr_rules, 571 sizeof(ipnat_t *) * softn->ipf_nat_rdrrules_sz); 572 softn->ipf_nat_rdr_rules = NULL; 573 } 574 if (softn->ipf_hm_maptable != NULL) { 575 KFREES(softn->ipf_hm_maptable, 576 sizeof(hostmap_t *) * softn->ipf_nat_hostmap_sz); 577 softn->ipf_hm_maptable = NULL; 578 } 579 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) { 580 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, 581 sizeof(u_int) * softn->ipf_nat_table_sz); 582 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = NULL; 583 } 584 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) { 585 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, 586 sizeof(u_int) * softn->ipf_nat_table_sz); 587 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = NULL; 588 } 589 590 if (softn->ipf_nat_inited == 1) { 591 softn->ipf_nat_inited = 0; 592 ipf_sttab_destroy(softn->ipf_nat_tcptq); 593 594 MUTEX_DESTROY(&softn->ipf_nat_new); 595 MUTEX_DESTROY(&softn->ipf_nat_io); 596 597 MUTEX_DESTROY(&softn->ipf_nat_udptq.ifq_lock); 598 MUTEX_DESTROY(&softn->ipf_nat_udpacktq.ifq_lock); 599 MUTEX_DESTROY(&softn->ipf_nat_icmptq.ifq_lock); 600 MUTEX_DESTROY(&softn->ipf_nat_icmpacktq.ifq_lock); 601 MUTEX_DESTROY(&softn->ipf_nat_iptq.ifq_lock); 602 MUTEX_DESTROY(&softn->ipf_nat_pending.ifq_lock); 603 } 604 605 return 0; 606 } 607 608 609 /* ------------------------------------------------------------------------ */ 610 /* Function: ipf_nat_setlock */ 611 /* Returns: Nil */ 612 /* Parameters: arg(I) - pointer to soft state information */ 613 /* tmp(I) - new lock value */ 614 /* */ 615 /* Set the "lock status" of NAT to the value in tmp. */ 616 /* ------------------------------------------------------------------------ */ 617 void 618 ipf_nat_setlock(void *arg, int tmp) 619 { 620 ipf_nat_softc_t *softn = arg; 621 622 softn->ipf_nat_lock = tmp; 623 } 624 625 626 /* ------------------------------------------------------------------------ */ 627 /* Function: ipf_nat_addrdr */ 628 /* Returns: Nil */ 629 /* Parameters: n(I) - pointer to NAT rule to add */ 630 /* */ 631 /* Adds a redirect rule to the hash table of redirect rules and the list of */ 632 /* loaded NAT rules. Updates the bitmask indicating which netmasks are in */ 633 /* use by redirect rules. */ 634 /* ------------------------------------------------------------------------ */ 635 static void 636 ipf_nat_addrdr(ipf_nat_softc_t *softn, ipnat_t *n) 637 { 638 ipnat_t **np; 639 u_32_t j; 640 u_int hv; 641 u_int rhv; 642 int k; 643 644 if (n->in_odstatype == FRI_NORMAL) { 645 k = count4bits(n->in_odstmsk); 646 ipf_inet_mask_add(k, &softn->ipf_nat_rdr_mask); 647 j = (n->in_odstaddr & n->in_odstmsk); 648 rhv = NAT_HASH_FN(j, 0, 0xffffffff); 649 } else { 650 ipf_inet_mask_add(0, &softn->ipf_nat_rdr_mask); 651 j = 0; 652 rhv = 0; 653 } 654 hv = rhv % softn->ipf_nat_rdrrules_sz; 655 np = softn->ipf_nat_rdr_rules + hv; 656 while (*np != NULL) 657 np = &(*np)->in_rnext; 658 n->in_rnext = NULL; 659 n->in_prnext = np; 660 n->in_hv[0] = hv; 661 n->in_use++; 662 *np = n; 663 } 664 665 666 /* ------------------------------------------------------------------------ */ 667 /* Function: ipf_nat_addmap */ 668 /* Returns: Nil */ 669 /* Parameters: n(I) - pointer to NAT rule to add */ 670 /* */ 671 /* Adds a NAT map rule to the hash table of rules and the list of loaded */ 672 /* NAT rules. Updates the bitmask indicating which netmasks are in use by */ 673 /* redirect rules. */ 674 /* ------------------------------------------------------------------------ */ 675 static void 676 ipf_nat_addmap(ipf_nat_softc_t *softn, ipnat_t *n) 677 { 678 ipnat_t **np; 679 u_32_t j; 680 u_int hv; 681 u_int rhv; 682 int k; 683 684 if (n->in_osrcatype == FRI_NORMAL) { 685 k = count4bits(n->in_osrcmsk); 686 ipf_inet_mask_add(k, &softn->ipf_nat_map_mask); 687 j = (n->in_osrcaddr & n->in_osrcmsk); 688 rhv = NAT_HASH_FN(j, 0, 0xffffffff); 689 } else { 690 ipf_inet_mask_add(0, &softn->ipf_nat_map_mask); 691 j = 0; 692 rhv = 0; 693 } 694 hv = rhv % softn->ipf_nat_maprules_sz; 695 np = softn->ipf_nat_map_rules + hv; 696 while (*np != NULL) 697 np = &(*np)->in_mnext; 698 n->in_mnext = NULL; 699 n->in_pmnext = np; 700 n->in_hv[1] = rhv; 701 n->in_use++; 702 *np = n; 703 } 704 705 706 /* ------------------------------------------------------------------------ */ 707 /* Function: ipf_nat_delrdr */ 708 /* Returns: Nil */ 709 /* Parameters: n(I) - pointer to NAT rule to delete */ 710 /* */ 711 /* Removes a redirect rule from the hash table of redirect rules. */ 712 /* ------------------------------------------------------------------------ */ 713 void 714 ipf_nat_delrdr(ipf_nat_softc_t *softn, ipnat_t *n) 715 { 716 if (n->in_odstatype == FRI_NORMAL) { 717 int k = count4bits(n->in_odstmsk); 718 ipf_inet_mask_del(k, &softn->ipf_nat_rdr_mask); 719 } else { 720 ipf_inet_mask_del(0, &softn->ipf_nat_rdr_mask); 721 } 722 if (n->in_rnext) 723 n->in_rnext->in_prnext = n->in_prnext; 724 *n->in_prnext = n->in_rnext; 725 n->in_use--; 726 } 727 728 729 /* ------------------------------------------------------------------------ */ 730 /* Function: ipf_nat_delmap */ 731 /* Returns: Nil */ 732 /* Parameters: n(I) - pointer to NAT rule to delete */ 733 /* */ 734 /* Removes a NAT map rule from the hash table of NAT map rules. */ 735 /* ------------------------------------------------------------------------ */ 736 void 737 ipf_nat_delmap(ipf_nat_softc_t *softn, ipnat_t *n) 738 { 739 if (n->in_osrcatype == FRI_NORMAL) { 740 int k = count4bits(n->in_osrcmsk); 741 ipf_inet_mask_del(k, &softn->ipf_nat_map_mask); 742 } else { 743 ipf_inet_mask_del(0, &softn->ipf_nat_map_mask); 744 } 745 if (n->in_mnext != NULL) 746 n->in_mnext->in_pmnext = n->in_pmnext; 747 *n->in_pmnext = n->in_mnext; 748 n->in_use--; 749 } 750 751 752 /* ------------------------------------------------------------------------ */ 753 /* Function: ipf_nat_hostmap */ 754 /* Returns: struct hostmap* - NULL if no hostmap could be created, */ 755 /* else a pointer to the hostmapping to use */ 756 /* Parameters: np(I) - pointer to NAT rule */ 757 /* real(I) - real IP address */ 758 /* map(I) - mapped IP address */ 759 /* port(I) - destination port number */ 760 /* Write Locks: ipf_nat */ 761 /* */ 762 /* Check if an ip address has already been allocated for a given mapping */ 763 /* that is not doing port based translation. If is not yet allocated, then */ 764 /* create a new entry if a non-NULL NAT rule pointer has been supplied. */ 765 /* ------------------------------------------------------------------------ */ 766 static struct hostmap * 767 ipf_nat_hostmap(ipf_nat_softc_t *softn, ipnat_t *np, struct in_addr src, 768 struct in_addr dst, struct in_addr map, u_32_t port) 769 { 770 hostmap_t *hm; 771 u_int hv, rhv; 772 773 hv = (src.s_addr ^ dst.s_addr); 774 hv += src.s_addr; 775 hv += dst.s_addr; 776 rhv = hv; 777 hv %= softn->ipf_nat_hostmap_sz; 778 for (hm = softn->ipf_hm_maptable[hv]; hm; hm = hm->hm_hnext) 779 if ((hm->hm_osrcip.s_addr == src.s_addr) && 780 (hm->hm_odstip.s_addr == dst.s_addr) && 781 ((np == NULL) || (np == hm->hm_ipnat)) && 782 ((port == 0) || (port == hm->hm_port))) { 783 softn->ipf_nat_stats.ns_hm_addref++; 784 hm->hm_ref++; 785 return hm; 786 } 787 788 if (np == NULL) { 789 softn->ipf_nat_stats.ns_hm_nullnp++; 790 return NULL; 791 } 792 793 KMALLOC(hm, hostmap_t *); 794 if (hm) { 795 hm->hm_next = softn->ipf_hm_maplist; 796 hm->hm_pnext = &softn->ipf_hm_maplist; 797 if (softn->ipf_hm_maplist != NULL) 798 softn->ipf_hm_maplist->hm_pnext = &hm->hm_next; 799 softn->ipf_hm_maplist = hm; 800 hm->hm_hnext = softn->ipf_hm_maptable[hv]; 801 hm->hm_phnext = softn->ipf_hm_maptable + hv; 802 if (softn->ipf_hm_maptable[hv] != NULL) 803 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext; 804 softn->ipf_hm_maptable[hv] = hm; 805 hm->hm_ipnat = np; 806 np->in_use++; 807 hm->hm_osrcip = src; 808 hm->hm_odstip = dst; 809 hm->hm_nsrcip = map; 810 hm->hm_ndstip.s_addr = 0; 811 hm->hm_ref = 1; 812 hm->hm_port = port; 813 hm->hm_hv = rhv; 814 hm->hm_v = 4; 815 softn->ipf_nat_stats.ns_hm_new++; 816 } else { 817 softn->ipf_nat_stats.ns_hm_newfail++; 818 } 819 return hm; 820 } 821 822 823 /* ------------------------------------------------------------------------ */ 824 /* Function: ipf_nat_hostmapdel */ 825 /* Returns: Nil */ 826 /* Parameters: hmp(I) - pointer to hostmap structure pointer */ 827 /* Write Locks: ipf_nat */ 828 /* */ 829 /* Decrement the references to this hostmap structure by one. If this */ 830 /* reaches zero then remove it and free it. */ 831 /* ------------------------------------------------------------------------ */ 832 void 833 ipf_nat_hostmapdel(ipf_main_softc_t *softc, struct hostmap **hmp) 834 { 835 struct hostmap *hm; 836 837 hm = *hmp; 838 *hmp = NULL; 839 840 hm->hm_ref--; 841 if (hm->hm_ref == 0) { 842 ipf_nat_rule_deref(softc, &hm->hm_ipnat); 843 if (hm->hm_hnext) 844 hm->hm_hnext->hm_phnext = hm->hm_phnext; 845 *hm->hm_phnext = hm->hm_hnext; 846 if (hm->hm_next) 847 hm->hm_next->hm_pnext = hm->hm_pnext; 848 *hm->hm_pnext = hm->hm_next; 849 KFREE(hm); 850 } 851 } 852 853 854 /* ------------------------------------------------------------------------ */ 855 /* Function: ipf_fix_outcksum */ 856 /* Returns: Nil */ 857 /* Parameters: fin(I) - pointer to packet information */ 858 /* sp(I) - location of 16bit checksum to update */ 859 /* n((I) - amount to adjust checksum by */ 860 /* */ 861 /* Adjusts the 16bit checksum by "n" for packets going out. */ 862 /* ------------------------------------------------------------------------ */ 863 void 864 ipf_fix_outcksum(int cksum, u_short *sp, u_32_t n, u_32_t partial) 865 { 866 u_short sumshort; 867 u_32_t sum1; 868 869 if (n == 0) 870 return; 871 872 if (cksum == 4) { 873 *sp = 0; 874 return; 875 } 876 if (cksum == 2) { 877 sum1 = partial; 878 sum1 = (sum1 & 0xffff) + (sum1 >> 16); 879 *sp = htons(sum1); 880 return; 881 } 882 sum1 = (~ntohs(*sp)) & 0xffff; 883 sum1 += (n); 884 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 885 /* Again */ 886 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 887 sumshort = ~(u_short)sum1; 888 *(sp) = htons(sumshort); 889 } 890 891 892 /* ------------------------------------------------------------------------ */ 893 /* Function: ipf_fix_incksum */ 894 /* Returns: Nil */ 895 /* Parameters: fin(I) - pointer to packet information */ 896 /* sp(I) - location of 16bit checksum to update */ 897 /* n((I) - amount to adjust checksum by */ 898 /* */ 899 /* Adjusts the 16bit checksum by "n" for packets going in. */ 900 /* ------------------------------------------------------------------------ */ 901 void 902 ipf_fix_incksum(int cksum, u_short *sp, u_32_t n, u_32_t partial) 903 { 904 u_short sumshort; 905 u_32_t sum1; 906 907 if (n == 0) 908 return; 909 910 if (cksum == 4) { 911 *sp = 0; 912 return; 913 } 914 if (cksum == 2) { 915 sum1 = partial; 916 sum1 = (sum1 & 0xffff) + (sum1 >> 16); 917 *sp = htons(sum1); 918 return; 919 } 920 921 sum1 = (~ntohs(*sp)) & 0xffff; 922 sum1 += ~(n) & 0xffff; 923 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 924 /* Again */ 925 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 926 sumshort = ~(u_short)sum1; 927 *(sp) = htons(sumshort); 928 } 929 930 931 /* ------------------------------------------------------------------------ */ 932 /* Function: ipf_fix_datacksum */ 933 /* Returns: Nil */ 934 /* Parameters: sp(I) - location of 16bit checksum to update */ 935 /* n((I) - amount to adjust checksum by */ 936 /* */ 937 /* Fix_datacksum is used *only* for the adjustments of checksums in the */ 938 /* data section of an IP packet. */ 939 /* */ 940 /* The only situation in which you need to do this is when NAT'ing an */ 941 /* ICMP error message. Such a message, contains in its body the IP header */ 942 /* of the original IP packet, that causes the error. */ 943 /* */ 944 /* You can't use fix_incksum or fix_outcksum in that case, because for the */ 945 /* kernel the data section of the ICMP error is just data, and no special */ 946 /* processing like hardware cksum or ntohs processing have been done by the */ 947 /* kernel on the data section. */ 948 /* ------------------------------------------------------------------------ */ 949 void 950 ipf_fix_datacksum(u_short *sp, u_32_t n) 951 { 952 u_short sumshort; 953 u_32_t sum1; 954 955 if (n == 0) 956 return; 957 958 sum1 = (~ntohs(*sp)) & 0xffff; 959 sum1 += (n); 960 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 961 /* Again */ 962 sum1 = (sum1 >> 16) + (sum1 & 0xffff); 963 sumshort = ~(u_short)sum1; 964 *(sp) = htons(sumshort); 965 } 966 967 968 /* ------------------------------------------------------------------------ */ 969 /* Function: ipf_nat_ioctl */ 970 /* Returns: int - 0 == success, != 0 == failure */ 971 /* Parameters: softc(I) - pointer to soft context main structure */ 972 /* data(I) - pointer to ioctl data */ 973 /* cmd(I) - ioctl command integer */ 974 /* mode(I) - file mode bits used with open */ 975 /* uid(I) - uid of calling process */ 976 /* ctx(I) - pointer used as key for finding context */ 977 /* */ 978 /* Processes an ioctl call made to operate on the IP Filter NAT device. */ 979 /* ------------------------------------------------------------------------ */ 980 int 981 ipf_nat_ioctl(ipf_main_softc_t *softc, void *data, ioctlcmd_t cmd, int mode, 982 int uid, void *ctx) 983 { 984 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 985 int error = 0, ret, arg, getlock; 986 ipnat_t *nat, *nt, *n; 987 ipnat_t natd; 988 SPL_INT(s); 989 990 #if BSD_GE_YEAR(199306) && defined(_KERNEL) 991 # if NETBSD_GE_REV(399002000) 992 if ((mode & FWRITE) && 993 kauth_authorize_network(curlwp->l_cred, KAUTH_NETWORK_FIREWALL, 994 KAUTH_REQ_NETWORK_FIREWALL_FW, 995 NULL, NULL, NULL)) 996 # else 997 # if defined(__FreeBSD_version) && (__FreeBSD_version >= 500034) 998 if (securelevel_ge(curthread->td_ucred, 3) && (mode & FWRITE)) 999 # else 1000 if ((securelevel >= 3) && (mode & FWRITE)) 1001 # endif 1002 # endif 1003 { 1004 IPFERROR(60001); 1005 return EPERM; 1006 } 1007 #endif 1008 1009 #if defined(__osf__) && defined(_KERNEL) 1010 getlock = 0; 1011 #else 1012 getlock = (mode & NAT_LOCKHELD) ? 0 : 1; 1013 #endif 1014 1015 n = NULL; 1016 nt = NULL; 1017 nat = NULL; 1018 1019 if ((cmd == (ioctlcmd_t)SIOCADNAT) || (cmd == (ioctlcmd_t)SIOCRMNAT) || 1020 (cmd == (ioctlcmd_t)SIOCPURGENAT)) { 1021 if (mode & NAT_SYSSPACE) { 1022 bcopy(data, (char *)&natd, sizeof(natd)); 1023 nat = &natd; 1024 error = 0; 1025 } else { 1026 bzero(&natd, sizeof(natd)); 1027 error = ipf_inobj(softc, data, NULL, &natd, 1028 IPFOBJ_IPNAT); 1029 if (error != 0) 1030 goto done; 1031 1032 if (natd.in_size < sizeof(ipnat_t)) { 1033 error = EINVAL; 1034 goto done; 1035 } 1036 KMALLOCS(nt, ipnat_t *, natd.in_size); 1037 if (nt == NULL) { 1038 IPFERROR(60070); 1039 error = ENOMEM; 1040 goto done; 1041 } 1042 bzero(nt, natd.in_size); 1043 error = ipf_inobjsz(softc, data, nt, IPFOBJ_IPNAT, 1044 natd.in_size); 1045 if (error) 1046 goto done; 1047 nat = nt; 1048 } 1049 1050 /* 1051 * For add/delete, look to see if the NAT entry is 1052 * already present 1053 */ 1054 nat->in_flags &= IPN_USERFLAGS; 1055 if ((nat->in_redir & NAT_MAPBLK) == 0) { 1056 if (nat->in_osrcatype == FRI_NORMAL || 1057 nat->in_osrcatype == FRI_NONE) 1058 nat->in_osrcaddr &= nat->in_osrcmsk; 1059 if (nat->in_odstatype == FRI_NORMAL || 1060 nat->in_odstatype == FRI_NONE) 1061 nat->in_odstaddr &= nat->in_odstmsk; 1062 if ((nat->in_flags & (IPN_SPLIT|IPN_SIPRANGE)) == 0) { 1063 if (nat->in_nsrcatype == FRI_NORMAL) 1064 nat->in_nsrcaddr &= nat->in_nsrcmsk; 1065 if (nat->in_ndstatype == FRI_NORMAL) 1066 nat->in_ndstaddr &= nat->in_ndstmsk; 1067 } 1068 } 1069 1070 error = ipf_nat_rule_init(softc, softn, nat); 1071 if (error != 0) 1072 goto done; 1073 1074 MUTEX_ENTER(&softn->ipf_nat_io); 1075 for (n = softn->ipf_nat_list; n != NULL; n = n->in_next) 1076 if (ipf_nat_cmp_rules(nat, n) == 0) 1077 break; 1078 } 1079 1080 switch (cmd) 1081 { 1082 #ifdef IPFILTER_LOG 1083 case SIOCIPFFB : 1084 { 1085 int tmp; 1086 1087 if (!(mode & FWRITE)) { 1088 IPFERROR(60002); 1089 error = EPERM; 1090 } else { 1091 tmp = ipf_log_clear(softc, IPL_LOGNAT); 1092 error = BCOPYOUT(&tmp, data, sizeof(tmp)); 1093 if (error != 0) { 1094 IPFERROR(60057); 1095 error = EFAULT; 1096 } 1097 } 1098 break; 1099 } 1100 1101 case SIOCSETLG : 1102 if (!(mode & FWRITE)) { 1103 IPFERROR(60003); 1104 error = EPERM; 1105 } else { 1106 error = BCOPYIN(data, &softn->ipf_nat_logging, 1107 sizeof(softn->ipf_nat_logging)); 1108 if (error != 0) 1109 error = EFAULT; 1110 } 1111 break; 1112 1113 case SIOCGETLG : 1114 error = BCOPYOUT(&softn->ipf_nat_logging, data, 1115 sizeof(softn->ipf_nat_logging)); 1116 if (error != 0) { 1117 IPFERROR(60004); 1118 error = EFAULT; 1119 } 1120 break; 1121 1122 case FIONREAD : 1123 arg = ipf_log_bytesused(softc, IPL_LOGNAT); 1124 error = BCOPYOUT(&arg, data, sizeof(arg)); 1125 if (error != 0) { 1126 IPFERROR(60005); 1127 error = EFAULT; 1128 } 1129 break; 1130 #endif 1131 case SIOCADNAT : 1132 if (!(mode & FWRITE)) { 1133 IPFERROR(60006); 1134 error = EPERM; 1135 } else if (n != NULL) { 1136 natd.in_flineno = n->in_flineno; 1137 (void) ipf_outobj(softc, data, &natd, IPFOBJ_IPNAT); 1138 IPFERROR(60007); 1139 error = EEXIST; 1140 } else if (nt == NULL) { 1141 IPFERROR(60008); 1142 error = ENOMEM; 1143 } 1144 if (error != 0) { 1145 MUTEX_EXIT(&softn->ipf_nat_io); 1146 break; 1147 } 1148 if (nat != nt) 1149 bcopy((char *)nat, (char *)nt, sizeof(*n)); 1150 error = ipf_nat_siocaddnat(softc, softn, nt, getlock); 1151 MUTEX_EXIT(&softn->ipf_nat_io); 1152 if (error == 0) { 1153 nat = NULL; 1154 nt = NULL; 1155 } 1156 break; 1157 1158 case SIOCRMNAT : 1159 case SIOCPURGENAT : 1160 if (!(mode & FWRITE)) { 1161 IPFERROR(60009); 1162 error = EPERM; 1163 n = NULL; 1164 } else if (n == NULL) { 1165 IPFERROR(60010); 1166 error = ESRCH; 1167 } 1168 1169 if (error != 0) { 1170 MUTEX_EXIT(&softn->ipf_nat_io); 1171 break; 1172 } 1173 if (cmd == (ioctlcmd_t)SIOCPURGENAT) { 1174 error = ipf_outobjsz(softc, data, n, IPFOBJ_IPNAT, 1175 n->in_size); 1176 if (error) { 1177 MUTEX_EXIT(&softn->ipf_nat_io); 1178 goto done; 1179 } 1180 n->in_flags |= IPN_PURGE; 1181 } 1182 ipf_nat_siocdelnat(softc, softn, n, getlock); 1183 1184 MUTEX_EXIT(&softn->ipf_nat_io); 1185 n = NULL; 1186 break; 1187 1188 case SIOCGNATS : 1189 { 1190 natstat_t *nsp = &softn->ipf_nat_stats; 1191 1192 nsp->ns_side[0].ns_table = softn->ipf_nat_table[0]; 1193 nsp->ns_side[1].ns_table = softn->ipf_nat_table[1]; 1194 nsp->ns_list = softn->ipf_nat_list; 1195 nsp->ns_maptable = softn->ipf_hm_maptable; 1196 nsp->ns_maplist = softn->ipf_hm_maplist; 1197 nsp->ns_nattab_sz = softn->ipf_nat_table_sz; 1198 nsp->ns_nattab_max = softn->ipf_nat_table_max; 1199 nsp->ns_rultab_sz = softn->ipf_nat_maprules_sz; 1200 nsp->ns_rdrtab_sz = softn->ipf_nat_rdrrules_sz; 1201 nsp->ns_hostmap_sz = softn->ipf_nat_hostmap_sz; 1202 nsp->ns_instances = softn->ipf_nat_instances; 1203 nsp->ns_ticks = softc->ipf_ticks; 1204 #ifdef IPFILTER_LOGGING 1205 nsp->ns_log_ok = ipf_log_logok(softc, IPF_LOGNAT); 1206 nsp->ns_log_fail = ipf_log_failures(softc, IPF_LOGNAT); 1207 #else 1208 nsp->ns_log_ok = 0; 1209 nsp->ns_log_fail = 0; 1210 #endif 1211 error = ipf_outobj(softc, data, nsp, IPFOBJ_NATSTAT); 1212 break; 1213 } 1214 1215 case SIOCGNATL : 1216 { 1217 natlookup_t nl; 1218 1219 error = ipf_inobj(softc, data, NULL, &nl, IPFOBJ_NATLOOKUP); 1220 if (error == 0) { 1221 void *ptr; 1222 1223 if (getlock) { 1224 READ_ENTER(&softc->ipf_nat); 1225 } 1226 1227 switch (nl.nl_v) 1228 { 1229 case 4 : 1230 ptr = ipf_nat_lookupredir(&nl); 1231 break; 1232 #ifdef USE_INET6 1233 case 6 : 1234 ptr = ipf_nat6_lookupredir(&nl); 1235 break; 1236 #endif 1237 default: 1238 ptr = NULL; 1239 break; 1240 } 1241 1242 if (getlock) { 1243 RWLOCK_EXIT(&softc->ipf_nat); 1244 } 1245 if (ptr != NULL) { 1246 error = ipf_outobj(softc, data, &nl, 1247 IPFOBJ_NATLOOKUP); 1248 } else { 1249 IPFERROR(60011); 1250 error = ESRCH; 1251 } 1252 } 1253 break; 1254 } 1255 1256 case SIOCIPFFL : /* old SIOCFLNAT & SIOCCNATL */ 1257 if (!(mode & FWRITE)) { 1258 IPFERROR(60012); 1259 error = EPERM; 1260 break; 1261 } 1262 if (getlock) { 1263 WRITE_ENTER(&softc->ipf_nat); 1264 } 1265 1266 error = BCOPYIN(data, &arg, sizeof(arg)); 1267 if (error != 0) { 1268 IPFERROR(60013); 1269 error = EFAULT; 1270 } else { 1271 if (arg == 0) 1272 ret = ipf_nat_flushtable(softc, softn); 1273 else if (arg == 1) 1274 ret = ipf_nat_clearlist(softc, softn); 1275 else 1276 ret = ipf_nat_extraflush(softc, softn, arg); 1277 ipf_proxy_flush(softc->ipf_proxy_soft, arg); 1278 } 1279 1280 if (getlock) { 1281 RWLOCK_EXIT(&softc->ipf_nat); 1282 } 1283 if (error == 0) { 1284 error = BCOPYOUT(&ret, data, sizeof(ret)); 1285 } 1286 break; 1287 1288 case SIOCMATCHFLUSH : 1289 if (!(mode & FWRITE)) { 1290 IPFERROR(60014); 1291 error = EPERM; 1292 break; 1293 } 1294 if (getlock) { 1295 WRITE_ENTER(&softc->ipf_nat); 1296 } 1297 1298 error = ipf_nat_matchflush(softc, softn, data); 1299 1300 if (getlock) { 1301 RWLOCK_EXIT(&softc->ipf_nat); 1302 } 1303 break; 1304 1305 case SIOCPROXY : 1306 error = ipf_proxy_ioctl(softc, data, cmd, mode, ctx); 1307 break; 1308 1309 case SIOCSTLCK : 1310 if (!(mode & FWRITE)) { 1311 IPFERROR(60015); 1312 error = EPERM; 1313 } else { 1314 error = ipf_lock(data, &softn->ipf_nat_lock); 1315 } 1316 break; 1317 1318 case SIOCSTPUT : 1319 if ((mode & FWRITE) != 0) { 1320 error = ipf_nat_putent(softc, data, getlock); 1321 } else { 1322 IPFERROR(60016); 1323 error = EACCES; 1324 } 1325 break; 1326 1327 case SIOCSTGSZ : 1328 if (softn->ipf_nat_lock) { 1329 error = ipf_nat_getsz(softc, data, getlock); 1330 } else { 1331 IPFERROR(60017); 1332 error = EACCES; 1333 } 1334 break; 1335 1336 case SIOCSTGET : 1337 if (softn->ipf_nat_lock) { 1338 error = ipf_nat_getent(softc, data, getlock); 1339 } else { 1340 IPFERROR(60018); 1341 error = EACCES; 1342 } 1343 break; 1344 1345 case SIOCGENITER : 1346 { 1347 ipfgeniter_t iter; 1348 ipftoken_t *token; 1349 ipfobj_t obj; 1350 1351 error = ipf_inobj(softc, data, &obj, &iter, IPFOBJ_GENITER); 1352 if (error != 0) 1353 break; 1354 1355 SPL_SCHED(s); 1356 token = ipf_token_find(softc, iter.igi_type, uid, ctx); 1357 if (token != NULL) { 1358 error = ipf_nat_iterator(softc, token, &iter, &obj); 1359 WRITE_ENTER(&softc->ipf_tokens); 1360 ipf_token_deref(softc, token); 1361 RWLOCK_EXIT(&softc->ipf_tokens); 1362 } 1363 SPL_X(s); 1364 break; 1365 } 1366 1367 case SIOCIPFDELTOK : 1368 error = BCOPYIN(data, &arg, sizeof(arg)); 1369 if (error == 0) { 1370 SPL_SCHED(s); 1371 error = ipf_token_del(softc, arg, uid, ctx); 1372 SPL_X(s); 1373 } else { 1374 IPFERROR(60019); 1375 error = EFAULT; 1376 } 1377 break; 1378 1379 case SIOCGTQTAB : 1380 error = ipf_outobj(softc, data, softn->ipf_nat_tcptq, 1381 IPFOBJ_STATETQTAB); 1382 break; 1383 1384 case SIOCGTABL : 1385 error = ipf_nat_gettable(softc, softn, data); 1386 break; 1387 1388 default : 1389 IPFERROR(60020); 1390 error = EINVAL; 1391 break; 1392 } 1393 done: 1394 if (nat != NULL) 1395 ipf_nat_rule_fini(softc, nat); 1396 if (nt != NULL) 1397 KFREES(nt, nt->in_size); 1398 return error; 1399 } 1400 1401 1402 /* ------------------------------------------------------------------------ */ 1403 /* Function: ipf_nat_siocaddnat */ 1404 /* Returns: int - 0 == success, != 0 == failure */ 1405 /* Parameters: softc(I) - pointer to soft context main structure */ 1406 /* softn(I) - pointer to NAT context structure */ 1407 /* n(I) - pointer to new NAT rule */ 1408 /* np(I) - pointer to where to insert new NAT rule */ 1409 /* getlock(I) - flag indicating if lock on is held */ 1410 /* Mutex Locks: ipf_nat_io */ 1411 /* */ 1412 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */ 1413 /* from information passed to the kernel, then add it to the appropriate */ 1414 /* NAT rule table(s). */ 1415 /* ------------------------------------------------------------------------ */ 1416 static int 1417 ipf_nat_siocaddnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n, 1418 int getlock) 1419 { 1420 int error = 0; 1421 1422 if (ipf_nat_resolverule(softc, n) != 0) { 1423 IPFERROR(60022); 1424 return ENOENT; 1425 } 1426 1427 if ((n->in_age[0] == 0) && (n->in_age[1] != 0)) { 1428 IPFERROR(60023); 1429 return EINVAL; 1430 } 1431 1432 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) { 1433 /* 1434 * Prerecord whether or not the destination of the divert 1435 * is local or not to the interface the packet is going 1436 * to be sent out. 1437 */ 1438 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1], 1439 n->in_ifps[1], &n->in_ndstip6); 1440 } 1441 1442 if (getlock) { 1443 WRITE_ENTER(&softc->ipf_nat); 1444 } 1445 n->in_next = NULL; 1446 n->in_pnext = softn->ipf_nat_list_tail; 1447 *n->in_pnext = n; 1448 softn->ipf_nat_list_tail = &n->in_next; 1449 n->in_use++; 1450 1451 if (n->in_redir & NAT_REDIRECT) { 1452 n->in_flags &= ~IPN_NOTDST; 1453 switch (n->in_v[0]) 1454 { 1455 case 4 : 1456 ipf_nat_addrdr(softn, n); 1457 break; 1458 #ifdef USE_INET6 1459 case 6 : 1460 ipf_nat6_addrdr(softn, n); 1461 break; 1462 #endif 1463 default : 1464 break; 1465 } 1466 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_rdr); 1467 } 1468 1469 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) { 1470 n->in_flags &= ~IPN_NOTSRC; 1471 switch (n->in_v[0]) 1472 { 1473 case 4 : 1474 ipf_nat_addmap(softn, n); 1475 break; 1476 #ifdef USE_INET6 1477 case 6 : 1478 ipf_nat6_addmap(softn, n); 1479 break; 1480 #endif 1481 default : 1482 break; 1483 } 1484 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules_map); 1485 } 1486 1487 if (n->in_age[0] != 0) 1488 n->in_tqehead[0] = ipf_addtimeoutqueue(softc, 1489 &softn->ipf_nat_utqe, 1490 n->in_age[0]); 1491 1492 if (n->in_age[1] != 0) 1493 n->in_tqehead[1] = ipf_addtimeoutqueue(softc, 1494 &softn->ipf_nat_utqe, 1495 n->in_age[1]); 1496 1497 MUTEX_INIT(&n->in_lock, "ipnat rule lock"); 1498 1499 n = NULL; 1500 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules); 1501 #if SOLARIS && !defined(INSTANCES) 1502 pfil_delayed_copy = 0; 1503 #endif 1504 if (getlock) { 1505 RWLOCK_EXIT(&softc->ipf_nat); /* WRITE */ 1506 } 1507 1508 return error; 1509 } 1510 1511 1512 /* ------------------------------------------------------------------------ */ 1513 /* Function: ipf_nat_ruleaddrinit */ 1514 /* Parameters: softc(I) - pointer to soft context main structure */ 1515 /* softn(I) - pointer to NAT context structure */ 1516 /* n(I) - pointer to NAT rule */ 1517 /* */ 1518 /* Initialise all of the NAT address structures in a NAT rule. */ 1519 /* ------------------------------------------------------------------------ */ 1520 static int 1521 ipf_nat_ruleaddrinit(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, 1522 ipnat_t *n) 1523 { 1524 int idx, error; 1525 1526 if ((n->in_ndst.na_atype == FRI_LOOKUP) && 1527 (n->in_ndst.na_type != IPLT_DSTLIST)) { 1528 IPFERROR(60071); 1529 return EINVAL; 1530 } 1531 if ((n->in_nsrc.na_atype == FRI_LOOKUP) && 1532 (n->in_nsrc.na_type != IPLT_DSTLIST)) { 1533 IPFERROR(60069); 1534 return EINVAL; 1535 } 1536 1537 if (n->in_redir == NAT_BIMAP) { 1538 n->in_ndstaddr = n->in_osrcaddr; 1539 n->in_ndstmsk = n->in_osrcmsk; 1540 n->in_odstaddr = n->in_nsrcaddr; 1541 n->in_odstmsk = n->in_nsrcmsk; 1542 1543 } 1544 1545 if (n->in_redir & NAT_REDIRECT) 1546 idx = 1; 1547 else 1548 idx = 0; 1549 /* 1550 * Initialise all of the address fields. 1551 */ 1552 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 1, 1553 n->in_ifps[idx]); 1554 if (error != 0) 1555 return error; 1556 1557 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 1, 1558 n->in_ifps[idx]); 1559 if (error != 0) 1560 return error; 1561 1562 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 1, 1563 n->in_ifps[idx]); 1564 if (error != 0) 1565 return error; 1566 1567 error = ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 1, 1568 n->in_ifps[idx]); 1569 if (error != 0) 1570 return error; 1571 1572 if (n->in_redir & NAT_DIVERTUDP) 1573 ipf_nat_builddivertmp(softn, n); 1574 1575 return 0; 1576 } 1577 1578 1579 /* ------------------------------------------------------------------------ */ 1580 /* Function: ipf_nat_resolvrule */ 1581 /* Returns: Nil */ 1582 /* Parameters: softc(I) - pointer to soft context main structure */ 1583 /* n(I) - pointer to NAT rule */ 1584 /* */ 1585 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */ 1586 /* from information passed to the kernel, then add it to the appropriate */ 1587 /* NAT rule table(s). */ 1588 /* ------------------------------------------------------------------------ */ 1589 static int 1590 ipf_nat_resolverule(ipf_main_softc_t *softc, ipnat_t *n) 1591 { 1592 char *base; 1593 1594 base = n->in_names; 1595 1596 n->in_ifps[0] = ipf_resolvenic(softc, base + n->in_ifnames[0], 1597 n->in_v[0]); 1598 1599 if (n->in_ifnames[1] == -1) { 1600 n->in_ifnames[1] = n->in_ifnames[0]; 1601 n->in_ifps[1] = n->in_ifps[0]; 1602 } else { 1603 n->in_ifps[1] = ipf_resolvenic(softc, base + n->in_ifnames[1], 1604 n->in_v[1]); 1605 } 1606 1607 if (n->in_plabel != -1) { 1608 if (n->in_redir & NAT_REDIRECT) 1609 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft, 1610 n->in_pr[0], 1611 base + n->in_plabel); 1612 else 1613 n->in_apr = ipf_proxy_lookup(softc->ipf_proxy_soft, 1614 n->in_pr[1], 1615 base + n->in_plabel); 1616 if (n->in_apr == NULL) 1617 return -1; 1618 } 1619 return 0; 1620 } 1621 1622 1623 /* ------------------------------------------------------------------------ */ 1624 /* Function: ipf_nat_siocdelnat */ 1625 /* Returns: int - 0 == success, != 0 == failure */ 1626 /* Parameters: softc(I) - pointer to soft context main structure */ 1627 /* softn(I) - pointer to NAT context structure */ 1628 /* n(I) - pointer to new NAT rule */ 1629 /* getlock(I) - flag indicating if lock on is held */ 1630 /* Mutex Locks: ipf_nat_io */ 1631 /* */ 1632 /* Handle SIOCADNAT. Resolve and calculate details inside the NAT rule */ 1633 /* from information passed to the kernel, then add it to the appropriate */ 1634 /* NAT rule table(s). */ 1635 /* ------------------------------------------------------------------------ */ 1636 static void 1637 ipf_nat_siocdelnat(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n, 1638 int getlock) 1639 { 1640 #ifdef IPF_NAT6 1641 int i; 1642 #endif 1643 1644 if (getlock) { 1645 WRITE_ENTER(&softc->ipf_nat); 1646 } 1647 1648 ipf_nat_delrule(softc, softn, n, 1); 1649 1650 if (getlock) { 1651 RWLOCK_EXIT(&softc->ipf_nat); /* READ/WRITE */ 1652 } 1653 } 1654 1655 1656 /* ------------------------------------------------------------------------ */ 1657 /* Function: ipf_nat_getsz */ 1658 /* Returns: int - 0 == success, != 0 is the error value. */ 1659 /* Parameters: softc(I) - pointer to soft context main structure */ 1660 /* data(I) - pointer to natget structure with kernel */ 1661 /* pointer get the size of. */ 1662 /* getlock(I) - flag indicating whether or not the caller */ 1663 /* holds a lock on ipf_nat */ 1664 /* */ 1665 /* Handle SIOCSTGSZ. */ 1666 /* Return the size of the nat list entry to be copied back to user space. */ 1667 /* The size of the entry is stored in the ng_sz field and the enture natget */ 1668 /* structure is copied back to the user. */ 1669 /* ------------------------------------------------------------------------ */ 1670 static int 1671 ipf_nat_getsz(ipf_main_softc_t *softc, void *data, int getlock) 1672 { 1673 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 1674 ap_session_t *aps; 1675 nat_t *nat, *n; 1676 natget_t ng; 1677 int error; 1678 1679 error = BCOPYIN(data, &ng, sizeof(ng)); 1680 if (error != 0) { 1681 IPFERROR(60024); 1682 return EFAULT; 1683 } 1684 1685 if (getlock) { 1686 READ_ENTER(&softc->ipf_nat); 1687 } 1688 1689 nat = ng.ng_ptr; 1690 if (!nat) { 1691 nat = softn->ipf_nat_instances; 1692 ng.ng_sz = 0; 1693 /* 1694 * Empty list so the size returned is 0. Simple. 1695 */ 1696 if (nat == NULL) { 1697 if (getlock) { 1698 RWLOCK_EXIT(&softc->ipf_nat); 1699 } 1700 error = BCOPYOUT(&ng, data, sizeof(ng)); 1701 if (error != 0) { 1702 IPFERROR(60025); 1703 return EFAULT; 1704 } 1705 return 0; 1706 } 1707 } else { 1708 /* 1709 * Make sure the pointer we're copying from exists in the 1710 * current list of entries. Security precaution to prevent 1711 * copying of random kernel data. 1712 */ 1713 for (n = softn->ipf_nat_instances; n; n = n->nat_next) 1714 if (n == nat) 1715 break; 1716 if (n == NULL) { 1717 if (getlock) { 1718 RWLOCK_EXIT(&softc->ipf_nat); 1719 } 1720 IPFERROR(60026); 1721 return ESRCH; 1722 } 1723 } 1724 1725 /* 1726 * Incluse any space required for proxy data structures. 1727 */ 1728 ng.ng_sz = sizeof(nat_save_t); 1729 aps = nat->nat_aps; 1730 if (aps != NULL) { 1731 ng.ng_sz += sizeof(ap_session_t) - 4; 1732 if (aps->aps_data != 0) 1733 ng.ng_sz += aps->aps_psiz; 1734 } 1735 if (getlock) { 1736 RWLOCK_EXIT(&softc->ipf_nat); 1737 } 1738 1739 error = BCOPYOUT(&ng, data, sizeof(ng)); 1740 if (error != 0) { 1741 IPFERROR(60027); 1742 return EFAULT; 1743 } 1744 return 0; 1745 } 1746 1747 1748 /* ------------------------------------------------------------------------ */ 1749 /* Function: ipf_nat_getent */ 1750 /* Returns: int - 0 == success, != 0 is the error value. */ 1751 /* Parameters: softc(I) - pointer to soft context main structure */ 1752 /* data(I) - pointer to natget structure with kernel pointer*/ 1753 /* to NAT structure to copy out. */ 1754 /* getlock(I) - flag indicating whether or not the caller */ 1755 /* holds a lock on ipf_nat */ 1756 /* */ 1757 /* Handle SIOCSTGET. */ 1758 /* Copies out NAT entry to user space. Any additional data held for a */ 1759 /* proxy is also copied, as to is the NAT rule which was responsible for it */ 1760 /* ------------------------------------------------------------------------ */ 1761 static int 1762 ipf_nat_getent(ipf_main_softc_t *softc, void *data, int getlock) 1763 { 1764 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 1765 int error, outsize; 1766 ap_session_t *aps; 1767 nat_save_t *ipn, ipns; 1768 nat_t *n, *nat; 1769 1770 error = ipf_inobj(softc, data, NULL, &ipns, IPFOBJ_NATSAVE); 1771 if (error != 0) 1772 return error; 1773 1774 if ((ipns.ipn_dsize < sizeof(ipns)) || (ipns.ipn_dsize > 81920)) { 1775 IPFERROR(60028); 1776 return EINVAL; 1777 } 1778 1779 KMALLOCS(ipn, nat_save_t *, ipns.ipn_dsize); 1780 if (ipn == NULL) { 1781 IPFERROR(60029); 1782 return ENOMEM; 1783 } 1784 1785 if (getlock) { 1786 READ_ENTER(&softc->ipf_nat); 1787 } 1788 1789 ipn->ipn_dsize = ipns.ipn_dsize; 1790 nat = ipns.ipn_next; 1791 if (nat == NULL) { 1792 nat = softn->ipf_nat_instances; 1793 if (nat == NULL) { 1794 if (softn->ipf_nat_instances == NULL) { 1795 IPFERROR(60030); 1796 error = ENOENT; 1797 } 1798 goto finished; 1799 } 1800 } else { 1801 /* 1802 * Make sure the pointer we're copying from exists in the 1803 * current list of entries. Security precaution to prevent 1804 * copying of random kernel data. 1805 */ 1806 for (n = softn->ipf_nat_instances; n; n = n->nat_next) 1807 if (n == nat) 1808 break; 1809 if (n == NULL) { 1810 IPFERROR(60031); 1811 error = ESRCH; 1812 goto finished; 1813 } 1814 } 1815 ipn->ipn_next = nat->nat_next; 1816 1817 /* 1818 * Copy the NAT structure. 1819 */ 1820 bcopy((char *)nat, &ipn->ipn_nat, sizeof(*nat)); 1821 1822 /* 1823 * If we have a pointer to the NAT rule it belongs to, save that too. 1824 */ 1825 if (nat->nat_ptr != NULL) 1826 bcopy((char *)nat->nat_ptr, (char *)&ipn->ipn_ipnat, 1827 ipn->ipn_ipnat.in_size); 1828 1829 /* 1830 * If we also know the NAT entry has an associated filter rule, 1831 * save that too. 1832 */ 1833 if (nat->nat_fr != NULL) 1834 bcopy((char *)nat->nat_fr, (char *)&ipn->ipn_fr, 1835 sizeof(ipn->ipn_fr)); 1836 1837 /* 1838 * Last but not least, if there is an application proxy session set 1839 * up for this NAT entry, then copy that out too, including any 1840 * private data saved along side it by the proxy. 1841 */ 1842 aps = nat->nat_aps; 1843 outsize = ipn->ipn_dsize - sizeof(*ipn) + sizeof(ipn->ipn_data); 1844 if (aps != NULL) { 1845 char *s; 1846 1847 if (outsize < sizeof(*aps)) { 1848 IPFERROR(60032); 1849 error = ENOBUFS; 1850 goto finished; 1851 } 1852 1853 s = ipn->ipn_data; 1854 bcopy((char *)aps, s, sizeof(*aps)); 1855 s += sizeof(*aps); 1856 outsize -= sizeof(*aps); 1857 if ((aps->aps_data != NULL) && (outsize >= aps->aps_psiz)) 1858 bcopy(aps->aps_data, s, aps->aps_psiz); 1859 else { 1860 IPFERROR(60033); 1861 error = ENOBUFS; 1862 } 1863 } 1864 if (error == 0) { 1865 if (getlock) { 1866 READ_ENTER(&softc->ipf_nat); 1867 getlock = 0; 1868 } 1869 error = ipf_outobjsz(softc, data, ipn, IPFOBJ_NATSAVE, 1870 ipns.ipn_dsize); 1871 } 1872 1873 finished: 1874 if (getlock) { 1875 READ_ENTER(&softc->ipf_nat); 1876 } 1877 if (ipn != NULL) { 1878 KFREES(ipn, ipns.ipn_dsize); 1879 } 1880 return error; 1881 } 1882 1883 1884 /* ------------------------------------------------------------------------ */ 1885 /* Function: ipf_nat_putent */ 1886 /* Returns: int - 0 == success, != 0 is the error value. */ 1887 /* Parameters: softc(I) - pointer to soft context main structure */ 1888 /* data(I) - pointer to natget structure with NAT */ 1889 /* structure information to load into the kernel */ 1890 /* getlock(I) - flag indicating whether or not a write lock */ 1891 /* on is already held. */ 1892 /* */ 1893 /* Handle SIOCSTPUT. */ 1894 /* Loads a NAT table entry from user space, including a NAT rule, proxy and */ 1895 /* firewall rule data structures, if pointers to them indicate so. */ 1896 /* ------------------------------------------------------------------------ */ 1897 static int 1898 ipf_nat_putent(ipf_main_softc_t *softc, void *data, int getlock) 1899 { 1900 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 1901 nat_save_t *ipn, *ipnn; 1902 ap_session_t *aps; 1903 nat_t *n, *nat; 1904 frentry_t *fr; 1905 fr_info_t *fin; 1906 ipnat_t *in; 1907 int error; 1908 1909 KMALLOC(ipn, nat_save_t *); 1910 if (ipn == NULL) 1911 return ENOMEM; 1912 error = ipf_inobj(softc, data, NULL, ipn, IPFOBJ_NATSAVE); 1913 if (error != 0) 1914 return error; 1915 1916 /* 1917 * Initialise early because of code at junkput label. 1918 */ 1919 n = NULL; 1920 in = NULL; 1921 aps = NULL; 1922 nat = NULL; 1923 ipnn = NULL; 1924 fin = NULL; 1925 fr = NULL; 1926 1927 /* 1928 * New entry, copy in the rest of the NAT entry if it's size is more 1929 * than just the nat_t structure. 1930 */ 1931 if (ipn->ipn_dsize > sizeof(*ipn)) { 1932 if (ipn->ipn_dsize > 81920) { 1933 IPFERROR(60034); 1934 error = ENOMEM; 1935 goto junkput; 1936 } 1937 1938 KMALLOCS(ipnn, nat_save_t *, ipn->ipn_dsize); 1939 if (ipnn == NULL) { 1940 IPFERROR(60035); 1941 return ENOMEM; 1942 } 1943 1944 bzero(ipnn, ipn->ipn_dsize); 1945 error = ipf_inobjsz(softc, data, ipnn, IPFOBJ_NATSAVE, 1946 ipn->ipn_dsize); 1947 if (error != 0) { 1948 goto junkput; 1949 } 1950 } else 1951 ipnn = ipn; 1952 1953 KMALLOC(nat, nat_t *); 1954 if (nat == NULL) { 1955 IPFERROR(60037); 1956 error = ENOMEM; 1957 goto junkput; 1958 } 1959 1960 bcopy((char *)&ipnn->ipn_nat, (char *)nat, sizeof(*nat)); 1961 1962 switch (nat->nat_v[0]) 1963 { 1964 case 4: 1965 #ifdef USE_IENT6 1966 case 6 : 1967 #endif 1968 break; 1969 default : 1970 IPFERROR(60061); 1971 error = EPROTONOSUPPORT; 1972 goto junkput; 1973 /*NOTREACHED*/ 1974 } 1975 1976 /* 1977 * Initialize all these so that ipf_nat_delete() doesn't cause a crash. 1978 */ 1979 bzero((char *)nat, offsetof(struct nat, nat_tqe)); 1980 nat->nat_tqe.tqe_pnext = NULL; 1981 nat->nat_tqe.tqe_next = NULL; 1982 nat->nat_tqe.tqe_ifq = NULL; 1983 nat->nat_tqe.tqe_parent = nat; 1984 1985 /* 1986 * Restore the rule associated with this nat session 1987 */ 1988 in = ipnn->ipn_nat.nat_ptr; 1989 if (in != NULL) { 1990 KMALLOCS(in, ipnat_t *, ipnn->ipn_ipnat.in_size); 1991 nat->nat_ptr = in; 1992 if (in == NULL) { 1993 IPFERROR(60038); 1994 error = ENOMEM; 1995 goto junkput; 1996 } 1997 bcopy((char *)&ipnn->ipn_ipnat, (char *)in, 1998 ipnn->ipn_ipnat.in_size); 1999 in->in_use = 1; 2000 in->in_flags |= IPN_DELETE; 2001 2002 ATOMIC_INC32(softn->ipf_nat_stats.ns_rules); 2003 2004 if (ipf_nat_resolverule(softc, in) != 0) { 2005 IPFERROR(60039); 2006 error = ESRCH; 2007 goto junkput; 2008 } 2009 } 2010 2011 /* 2012 * Check that the NAT entry doesn't already exist in the kernel. 2013 * 2014 * For NAT_OUTBOUND, we're lookup for a duplicate MAP entry. To do 2015 * this, we check to see if the inbound combination of addresses and 2016 * ports is already known. Similar logic is applied for NAT_INBOUND. 2017 * 2018 */ 2019 KMALLOC(fin, fr_info_t *); 2020 if (fin == NULL) { 2021 error = ENOMEM; 2022 goto junkput; 2023 } 2024 bzero(fin, sizeof(*fin)); 2025 fin->fin_v = nat->nat_v[0]; 2026 fin->fin_p = nat->nat_pr[0]; 2027 fin->fin_rev = nat->nat_rev; 2028 fin->fin_ifp = nat->nat_ifps[0]; 2029 fin->fin_data[0] = ntohs(nat->nat_ndport); 2030 fin->fin_data[1] = ntohs(nat->nat_nsport); 2031 2032 switch (nat->nat_dir) 2033 { 2034 case NAT_OUTBOUND : 2035 case NAT_DIVERTOUT : 2036 if (getlock) { 2037 READ_ENTER(&softc->ipf_nat); 2038 } 2039 2040 fin->fin_v = nat->nat_v[1]; 2041 if (nat->nat_v[1] == 4) { 2042 n = ipf_nat_inlookup(fin, nat->nat_flags, fin->fin_p, 2043 nat->nat_ndstip, nat->nat_nsrcip); 2044 #ifdef USE_INET6 2045 } else if (nat->nat_v[1] == 6) { 2046 n = ipf_nat6_inlookup(fin, nat->nat_flags, fin->fin_p, 2047 &nat->nat_ndst6.in6, 2048 &nat->nat_nsrc6.in6); 2049 #endif 2050 } 2051 2052 if (getlock) { 2053 RWLOCK_EXIT(&softc->ipf_nat); 2054 } 2055 if (n != NULL) { 2056 IPFERROR(60040); 2057 error = EEXIST; 2058 goto junkput; 2059 } 2060 break; 2061 2062 case NAT_INBOUND : 2063 case NAT_DIVERTIN : 2064 if (getlock) { 2065 READ_ENTER(&softc->ipf_nat); 2066 } 2067 2068 if (fin->fin_v == 4) { 2069 n = ipf_nat_outlookup(fin, nat->nat_flags, fin->fin_p, 2070 nat->nat_ndstip, 2071 nat->nat_nsrcip); 2072 #ifdef USE_INET6 2073 } else if (fin->fin_v == 6) { 2074 n = ipf_nat6_outlookup(fin, nat->nat_flags, fin->fin_p, 2075 &nat->nat_ndst6.in6, 2076 &nat->nat_nsrc6.in6); 2077 #endif 2078 } 2079 2080 if (getlock) { 2081 RWLOCK_EXIT(&softc->ipf_nat); 2082 } 2083 if (n != NULL) { 2084 IPFERROR(60041); 2085 error = EEXIST; 2086 goto junkput; 2087 } 2088 break; 2089 2090 default : 2091 IPFERROR(60042); 2092 error = EINVAL; 2093 goto junkput; 2094 } 2095 2096 /* 2097 * Restore ap_session_t structure. Include the private data allocated 2098 * if it was there. 2099 */ 2100 aps = nat->nat_aps; 2101 if (aps != NULL) { 2102 KMALLOC(aps, ap_session_t *); 2103 nat->nat_aps = aps; 2104 if (aps == NULL) { 2105 IPFERROR(60043); 2106 error = ENOMEM; 2107 goto junkput; 2108 } 2109 bcopy(ipnn->ipn_data, (char *)aps, sizeof(*aps)); 2110 if (in != NULL) 2111 aps->aps_apr = in->in_apr; 2112 else 2113 aps->aps_apr = NULL; 2114 if (aps->aps_psiz != 0) { 2115 if (aps->aps_psiz > 81920) { 2116 IPFERROR(60044); 2117 error = ENOMEM; 2118 goto junkput; 2119 } 2120 KMALLOCS(aps->aps_data, void *, aps->aps_psiz); 2121 if (aps->aps_data == NULL) { 2122 IPFERROR(60045); 2123 error = ENOMEM; 2124 goto junkput; 2125 } 2126 bcopy(ipnn->ipn_data + sizeof(*aps), aps->aps_data, 2127 aps->aps_psiz); 2128 } else { 2129 aps->aps_psiz = 0; 2130 aps->aps_data = NULL; 2131 } 2132 } 2133 2134 /* 2135 * If there was a filtering rule associated with this entry then 2136 * build up a new one. 2137 */ 2138 fr = nat->nat_fr; 2139 if (fr != NULL) { 2140 if ((nat->nat_flags & SI_NEWFR) != 0) { 2141 KMALLOC(fr, frentry_t *); 2142 nat->nat_fr = fr; 2143 if (fr == NULL) { 2144 IPFERROR(60046); 2145 error = ENOMEM; 2146 goto junkput; 2147 } 2148 ipnn->ipn_nat.nat_fr = fr; 2149 fr->fr_ref = 1; 2150 (void) ipf_outobj(softc, data, ipnn, IPFOBJ_NATSAVE); 2151 bcopy((char *)&ipnn->ipn_fr, (char *)fr, sizeof(*fr)); 2152 2153 fr->fr_ref = 1; 2154 fr->fr_dsize = 0; 2155 fr->fr_data = NULL; 2156 fr->fr_type = FR_T_NONE; 2157 2158 MUTEX_NUKE(&fr->fr_lock); 2159 MUTEX_INIT(&fr->fr_lock, "nat-filter rule lock"); 2160 } else { 2161 if (getlock) { 2162 READ_ENTER(&softc->ipf_nat); 2163 } 2164 for (n = softn->ipf_nat_instances; n; n = n->nat_next) 2165 if (n->nat_fr == fr) 2166 break; 2167 2168 if (n != NULL) { 2169 MUTEX_ENTER(&fr->fr_lock); 2170 fr->fr_ref++; 2171 MUTEX_EXIT(&fr->fr_lock); 2172 } 2173 if (getlock) { 2174 RWLOCK_EXIT(&softc->ipf_nat); 2175 } 2176 2177 if (n == NULL) { 2178 IPFERROR(60047); 2179 error = ESRCH; 2180 goto junkput; 2181 } 2182 } 2183 } 2184 2185 if (ipnn != ipn) { 2186 KFREES(ipnn, ipn->ipn_dsize); 2187 ipnn = NULL; 2188 } 2189 2190 if (getlock) { 2191 WRITE_ENTER(&softc->ipf_nat); 2192 } 2193 2194 if (fin->fin_v == 4) 2195 error = ipf_nat_finalise(fin, nat); 2196 #ifdef USE_INET6 2197 else 2198 error = ipf_nat6_finalise(fin, nat); 2199 #endif 2200 2201 if (getlock) { 2202 RWLOCK_EXIT(&softc->ipf_nat); 2203 } 2204 2205 if (error == 0) 2206 return 0; 2207 2208 IPFERROR(60048); 2209 error = ENOMEM; 2210 2211 junkput: 2212 if (fin != NULL) 2213 KFREE(fin); 2214 if (fr != NULL) { 2215 (void) ipf_derefrule(softc, &fr); 2216 } 2217 2218 if ((ipnn != NULL) && (ipnn != ipn)) { 2219 KFREES(ipnn, ipn->ipn_dsize); 2220 } 2221 if (ipn != NULL) 2222 KFREE(ipn); 2223 if (nat != NULL) { 2224 if (aps != NULL) { 2225 if (aps->aps_data != NULL) { 2226 KFREES(aps->aps_data, aps->aps_psiz); 2227 } 2228 KFREE(aps); 2229 } 2230 if (in != NULL) { 2231 if (in->in_apr) 2232 ipf_proxy_deref(in->in_apr); 2233 KFREES(in, in->in_size); 2234 } 2235 KFREE(nat); 2236 } 2237 return error; 2238 } 2239 2240 2241 /* ------------------------------------------------------------------------ */ 2242 /* Function: ipf_nat_delete */ 2243 /* Returns: Nil */ 2244 /* Parameters: softc(I) - pointer to soft context main structure */ 2245 /* nat(I) - pointer to NAT structure to delete */ 2246 /* logtype(I) - type of LOG record to create before deleting */ 2247 /* Write Lock: ipf_nat */ 2248 /* */ 2249 /* Delete a nat entry from the various lists and table. If NAT logging is */ 2250 /* enabled then generate a NAT log record for this event. */ 2251 /* ------------------------------------------------------------------------ */ 2252 void 2253 ipf_nat_delete(ipf_main_softc_t *softc, struct nat *nat, int logtype) 2254 { 2255 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 2256 int madeorphan = 0, bkt, removed = 0; 2257 nat_stat_side_t *nss; 2258 struct ipnat *ipn; 2259 2260 if (logtype != 0 && softn->ipf_nat_logging != 0) 2261 ipf_nat_log(softc, softn, nat, logtype); 2262 2263 /* 2264 * Take it as a general indication that all the pointers are set if 2265 * nat_pnext is set. 2266 */ 2267 if (nat->nat_pnext != NULL) { 2268 removed = 1; 2269 2270 bkt = nat->nat_hv[0] % softn->ipf_nat_table_sz; 2271 nss = &softn->ipf_nat_stats.ns_side[0]; 2272 nss->ns_bucketlen[bkt]--; 2273 if (nss->ns_bucketlen[bkt] == 0) { 2274 nss->ns_inuse--; 2275 } 2276 2277 bkt = nat->nat_hv[1] % softn->ipf_nat_table_sz; 2278 nss = &softn->ipf_nat_stats.ns_side[1]; 2279 nss->ns_bucketlen[bkt]--; 2280 if (nss->ns_bucketlen[bkt] == 0) { 2281 nss->ns_inuse--; 2282 } 2283 2284 *nat->nat_pnext = nat->nat_next; 2285 if (nat->nat_next != NULL) { 2286 nat->nat_next->nat_pnext = nat->nat_pnext; 2287 nat->nat_next = NULL; 2288 } 2289 nat->nat_pnext = NULL; 2290 2291 *nat->nat_phnext[0] = nat->nat_hnext[0]; 2292 if (nat->nat_hnext[0] != NULL) { 2293 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0]; 2294 nat->nat_hnext[0] = NULL; 2295 } 2296 nat->nat_phnext[0] = NULL; 2297 2298 *nat->nat_phnext[1] = nat->nat_hnext[1]; 2299 if (nat->nat_hnext[1] != NULL) { 2300 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1]; 2301 nat->nat_hnext[1] = NULL; 2302 } 2303 nat->nat_phnext[1] = NULL; 2304 2305 if ((nat->nat_flags & SI_WILDP) != 0) { 2306 ATOMIC_DEC32(softn->ipf_nat_stats.ns_wilds); 2307 } 2308 madeorphan = 1; 2309 } 2310 2311 if (nat->nat_me != NULL) { 2312 *nat->nat_me = NULL; 2313 nat->nat_me = NULL; 2314 nat->nat_ref--; 2315 ASSERT(nat->nat_ref >= 0); 2316 } 2317 2318 if (nat->nat_tqe.tqe_ifq != NULL) { 2319 /* 2320 * No call to ipf_freetimeoutqueue() is made here, they are 2321 * garbage collected in ipf_nat_expire(). 2322 */ 2323 (void) ipf_deletequeueentry(&nat->nat_tqe); 2324 } 2325 2326 if (nat->nat_sync) { 2327 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync); 2328 nat->nat_sync = NULL; 2329 } 2330 2331 if (logtype == NL_EXPIRE) 2332 softn->ipf_nat_stats.ns_expire++; 2333 2334 MUTEX_ENTER(&nat->nat_lock); 2335 /* 2336 * NL_DESTROY should only be passed in when we've got nat_ref >= 2. 2337 * This happens when a nat'd packet is blocked and we want to throw 2338 * away the NAT session. 2339 */ 2340 if (logtype == NL_DESTROY) { 2341 if (nat->nat_ref > 2) { 2342 nat->nat_ref -= 2; 2343 MUTEX_EXIT(&nat->nat_lock); 2344 if (removed) 2345 softn->ipf_nat_stats.ns_orphans++; 2346 return; 2347 } 2348 } else if (nat->nat_ref > 1) { 2349 nat->nat_ref--; 2350 MUTEX_EXIT(&nat->nat_lock); 2351 if (madeorphan == 1) 2352 softn->ipf_nat_stats.ns_orphans++; 2353 return; 2354 } 2355 ASSERT(nat->nat_ref >= 0); 2356 MUTEX_EXIT(&nat->nat_lock); 2357 2358 nat->nat_ref = 0; 2359 2360 if (madeorphan == 0) 2361 softn->ipf_nat_stats.ns_orphans--; 2362 2363 /* 2364 * At this point, nat_ref can be either 0 or -1 2365 */ 2366 softn->ipf_nat_stats.ns_proto[nat->nat_pr[0]]--; 2367 2368 if (nat->nat_fr != NULL) { 2369 (void) ipf_derefrule(softc, &nat->nat_fr); 2370 } 2371 2372 if (nat->nat_hm != NULL) { 2373 ipf_nat_hostmapdel(softc, &nat->nat_hm); 2374 } 2375 2376 /* 2377 * If there is an active reference from the nat entry to its parent 2378 * rule, decrement the rule's reference count and free it too if no 2379 * longer being used. 2380 */ 2381 ipn = nat->nat_ptr; 2382 nat->nat_ptr = NULL; 2383 2384 if (ipn != NULL) { 2385 ipn->in_space++; 2386 ipf_nat_rule_deref(softc, &ipn); 2387 } 2388 2389 if (nat->nat_aps != NULL) { 2390 ipf_proxy_free(softc, nat->nat_aps); 2391 nat->nat_aps = NULL; 2392 } 2393 2394 MUTEX_DESTROY(&nat->nat_lock); 2395 2396 softn->ipf_nat_stats.ns_active--; 2397 2398 /* 2399 * If there's a fragment table entry too for this nat entry, then 2400 * dereference that as well. This is after nat_lock is released 2401 * because of Tru64. 2402 */ 2403 ipf_frag_natforget(softc, (void *)nat); 2404 2405 KFREE(nat); 2406 } 2407 2408 2409 /* ------------------------------------------------------------------------ */ 2410 /* Function: ipf_nat_flushtable */ 2411 /* Returns: int - number of NAT rules deleted */ 2412 /* Parameters: softc(I) - pointer to soft context main structure */ 2413 /* softn(I) - pointer to NAT context structure */ 2414 /* Write Lock: ipf_nat */ 2415 /* */ 2416 /* Deletes all currently active NAT sessions. In deleting each NAT entry a */ 2417 /* log record should be emitted in ipf_nat_delete() if NAT logging is */ 2418 /* enabled. */ 2419 /* ------------------------------------------------------------------------ */ 2420 /* 2421 * nat_flushtable - clear the NAT table of all mapping entries. 2422 */ 2423 static int 2424 ipf_nat_flushtable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn) 2425 { 2426 nat_t *nat; 2427 int j = 0; 2428 2429 /* 2430 * ALL NAT mappings deleted, so lets just make the deletions 2431 * quicker. 2432 */ 2433 if (softn->ipf_nat_table[0] != NULL) 2434 bzero((char *)softn->ipf_nat_table[0], 2435 sizeof(softn->ipf_nat_table[0]) * 2436 softn->ipf_nat_table_sz); 2437 if (softn->ipf_nat_table[1] != NULL) 2438 bzero((char *)softn->ipf_nat_table[1], 2439 sizeof(softn->ipf_nat_table[1]) * 2440 softn->ipf_nat_table_sz); 2441 2442 while ((nat = softn->ipf_nat_instances) != NULL) { 2443 ipf_nat_delete(softc, nat, NL_FLUSH); 2444 j++; 2445 } 2446 2447 return j; 2448 } 2449 2450 2451 /* ------------------------------------------------------------------------ */ 2452 /* Function: ipf_nat_clearlist */ 2453 /* Returns: int - number of NAT/RDR rules deleted */ 2454 /* Parameters: softc(I) - pointer to soft context main structure */ 2455 /* softn(I) - pointer to NAT context structure */ 2456 /* */ 2457 /* Delete all rules in the current list of rules. There is nothing elegant */ 2458 /* about this cleanup: simply free all entries on the list of rules and */ 2459 /* clear out the tables used for hashed NAT rule lookups. */ 2460 /* ------------------------------------------------------------------------ */ 2461 static int 2462 ipf_nat_clearlist(ipf_main_softc_t *softc, ipf_nat_softc_t *softn) 2463 { 2464 ipnat_t *n; 2465 int i = 0; 2466 2467 if (softn->ipf_nat_map_rules != NULL) { 2468 bzero((char *)softn->ipf_nat_map_rules, 2469 sizeof(*softn->ipf_nat_map_rules) * 2470 softn->ipf_nat_maprules_sz); 2471 } 2472 if (softn->ipf_nat_rdr_rules != NULL) { 2473 bzero((char *)softn->ipf_nat_rdr_rules, 2474 sizeof(*softn->ipf_nat_rdr_rules) * 2475 softn->ipf_nat_rdrrules_sz); 2476 } 2477 2478 while ((n = softn->ipf_nat_list) != NULL) { 2479 ipf_nat_delrule(softc, softn, n, 0); 2480 i++; 2481 } 2482 #if SOLARIS && !defined(INSTANCES) 2483 pfil_delayed_copy = 1; 2484 #endif 2485 return i; 2486 } 2487 2488 2489 /* ------------------------------------------------------------------------ */ 2490 /* Function: ipf_nat_delrule */ 2491 /* Returns: Nil */ 2492 /* Parameters: softc(I) - pointer to soft context main structure */ 2493 /* softn(I) - pointer to NAT context structure */ 2494 /* np(I) - pointer to NAT rule to delete */ 2495 /* purge(I) - 1 == allow purge, 0 == prevent purge */ 2496 /* Locks: WRITE(ipf_nat) */ 2497 /* */ 2498 /* Preventing "purge" from occuring is allowed because when all of the NAT */ 2499 /* rules are being removed, allowing the "purge" to walk through the list */ 2500 /* of NAT sessions, possibly multiple times, would be a large performance */ 2501 /* hit, on the order of O(N^2). */ 2502 /* ------------------------------------------------------------------------ */ 2503 static void 2504 ipf_nat_delrule(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *np, 2505 int purge) 2506 { 2507 2508 if (np->in_pnext != NULL) { 2509 *np->in_pnext = np->in_next; 2510 if (np->in_next != NULL) 2511 np->in_next->in_pnext = np->in_pnext; 2512 if (softn->ipf_nat_list_tail == &np->in_next) 2513 softn->ipf_nat_list_tail = np->in_pnext; 2514 } 2515 2516 if ((purge == 1) && ((np->in_flags & IPN_PURGE) != 0)) { 2517 nat_t *next; 2518 nat_t *nat; 2519 2520 for (next = softn->ipf_nat_instances; (nat = next) != NULL;) { 2521 next = nat->nat_next; 2522 if (nat->nat_ptr == np) 2523 ipf_nat_delete(softc, nat, NL_PURGE); 2524 } 2525 } 2526 2527 if ((np->in_flags & IPN_DELETE) == 0) { 2528 if (np->in_redir & NAT_REDIRECT) { 2529 switch (np->in_v[0]) 2530 { 2531 case 4 : 2532 ipf_nat_delrdr(softn, np); 2533 break; 2534 #ifdef USE_INET6 2535 case 6 : 2536 ipf_nat6_delrdr(softn, np); 2537 break; 2538 #endif 2539 } 2540 } 2541 if (np->in_redir & (NAT_MAPBLK|NAT_MAP)) { 2542 switch (np->in_v[0]) 2543 { 2544 case 4 : 2545 ipf_nat_delmap(softn, np); 2546 break; 2547 #ifdef USE_INET6 2548 case 6 : 2549 ipf_nat6_delmap(softn, np); 2550 break; 2551 #endif 2552 } 2553 } 2554 } 2555 2556 np->in_flags |= IPN_DELETE; 2557 ipf_nat_rule_deref(softc, &np); 2558 } 2559 2560 2561 /* ------------------------------------------------------------------------ */ 2562 /* Function: ipf_nat_newmap */ 2563 /* Returns: int - -1 == error, 0 == success */ 2564 /* Parameters: fin(I) - pointer to packet information */ 2565 /* nat(I) - pointer to NAT entry */ 2566 /* ni(I) - pointer to structure with misc. information needed */ 2567 /* to create new NAT entry. */ 2568 /* */ 2569 /* Given an empty NAT structure, populate it with new information about a */ 2570 /* new NAT session, as defined by the matching NAT rule. */ 2571 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/ 2572 /* to the new IP address for the translation. */ 2573 /* ------------------------------------------------------------------------ */ 2574 static int 2575 ipf_nat_newmap(fr_info_t *fin, nat_t *nat, natinfo_t *ni) 2576 { 2577 ipf_main_softc_t *softc = fin->fin_main_soft; 2578 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 2579 u_short st_port, dport, sport, port, sp, dp; 2580 struct in_addr in, inb; 2581 hostmap_t *hm; 2582 u_32_t flags; 2583 u_32_t st_ip; 2584 ipnat_t *np; 2585 nat_t *natl; 2586 int l; 2587 2588 /* 2589 * If it's an outbound packet which doesn't match any existing 2590 * record, then create a new port 2591 */ 2592 l = 0; 2593 hm = NULL; 2594 np = ni->nai_np; 2595 st_ip = np->in_snip; 2596 st_port = np->in_spnext; 2597 flags = nat->nat_flags; 2598 2599 if (flags & IPN_ICMPQUERY) { 2600 sport = fin->fin_data[1]; 2601 dport = 0; 2602 } else { 2603 sport = htons(fin->fin_data[0]); 2604 dport = htons(fin->fin_data[1]); 2605 } 2606 2607 /* 2608 * Do a loop until we either run out of entries to try or we find 2609 * a NAT mapping that isn't currently being used. This is done 2610 * because the change to the source is not (usually) being fixed. 2611 */ 2612 do { 2613 port = 0; 2614 in.s_addr = htonl(np->in_snip); 2615 if (l == 0) { 2616 /* 2617 * Check to see if there is an existing NAT 2618 * setup for this IP address pair. 2619 */ 2620 hm = ipf_nat_hostmap(softn, np, fin->fin_src, 2621 fin->fin_dst, in, 0); 2622 if (hm != NULL) 2623 in.s_addr = hm->hm_nsrcip.s_addr; 2624 } else if ((l == 1) && (hm != NULL)) { 2625 ipf_nat_hostmapdel(softc, &hm); 2626 } 2627 in.s_addr = ntohl(in.s_addr); 2628 2629 nat->nat_hm = hm; 2630 2631 if ((np->in_nsrcmsk == 0xffffffff) && (np->in_spnext == 0)) { 2632 if (l > 0) { 2633 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_1); 2634 return -1; 2635 } 2636 } 2637 2638 if (np->in_redir == NAT_BIMAP && 2639 np->in_osrcmsk == np->in_nsrcmsk) { 2640 /* 2641 * map the address block in a 1:1 fashion 2642 */ 2643 in.s_addr = np->in_nsrcaddr; 2644 in.s_addr |= fin->fin_saddr & ~np->in_osrcmsk; 2645 in.s_addr = ntohl(in.s_addr); 2646 2647 } else if (np->in_redir & NAT_MAPBLK) { 2648 if ((l >= np->in_ppip) || ((l > 0) && 2649 !(flags & IPN_TCPUDP))) { 2650 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_2); 2651 return -1; 2652 } 2653 /* 2654 * map-block - Calculate destination address. 2655 */ 2656 in.s_addr = ntohl(fin->fin_saddr); 2657 in.s_addr &= ntohl(~np->in_osrcmsk); 2658 inb.s_addr = in.s_addr; 2659 in.s_addr /= np->in_ippip; 2660 in.s_addr &= ntohl(~np->in_nsrcmsk); 2661 in.s_addr += ntohl(np->in_nsrcaddr); 2662 /* 2663 * Calculate destination port. 2664 */ 2665 if ((flags & IPN_TCPUDP) && 2666 (np->in_ppip != 0)) { 2667 port = ntohs(sport) + l; 2668 port %= np->in_ppip; 2669 port += np->in_ppip * 2670 (inb.s_addr % np->in_ippip); 2671 port += MAPBLK_MINPORT; 2672 port = htons(port); 2673 } 2674 2675 } else if ((np->in_nsrcaddr == 0) && 2676 (np->in_nsrcmsk == 0xffffffff)) { 2677 i6addr_t in6; 2678 2679 /* 2680 * 0/32 - use the interface's IP address. 2681 */ 2682 if ((l > 0) || 2683 ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp, 2684 &in6, NULL) == -1) { 2685 NBUMPSIDEX(1, ns_new_ifpaddr, ns_new_ifpaddr_1); 2686 return -1; 2687 } 2688 in.s_addr = ntohl(in6.in4.s_addr); 2689 2690 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) { 2691 /* 2692 * 0/0 - use the original source address/port. 2693 */ 2694 if (l > 0) { 2695 NBUMPSIDEX(1, ns_exhausted, ns_exhausted_3); 2696 return -1; 2697 } 2698 in.s_addr = ntohl(fin->fin_saddr); 2699 2700 } else if ((np->in_nsrcmsk != 0xffffffff) && 2701 (np->in_spnext == 0) && ((l > 0) || (hm == NULL))) 2702 np->in_snip++; 2703 2704 natl = NULL; 2705 2706 if ((flags & IPN_TCPUDP) && 2707 ((np->in_redir & NAT_MAPBLK) == 0) && 2708 (np->in_flags & IPN_AUTOPORTMAP)) { 2709 /* 2710 * "ports auto" (without map-block) 2711 */ 2712 if ((l > 0) && np->in_ppip && (l % np->in_ppip == 0)) { 2713 if (l > np->in_space) { 2714 return -1; 2715 } else if ((l > np->in_ppip) && 2716 np->in_nsrcmsk != 0xffffffff) 2717 np->in_snip++; 2718 } 2719 if (np->in_ppip != 0) { 2720 port = ntohs(sport); 2721 port += (l % np->in_ppip); 2722 port %= np->in_ppip; 2723 port += np->in_ppip * 2724 (ntohl(fin->fin_saddr) % 2725 np->in_ippip); 2726 port += MAPBLK_MINPORT; 2727 port = htons(port); 2728 } 2729 2730 } else if (((np->in_redir & NAT_MAPBLK) == 0) && 2731 (flags & IPN_TCPUDPICMP) && (np->in_spnext != 0)) { 2732 /* 2733 * Standard port translation. Select next port. 2734 */ 2735 if (np->in_flags & IPN_SEQUENTIAL) { 2736 port = np->in_spnext; 2737 } else { 2738 port = ipf_random() % (np->in_spmax - 2739 np->in_spmin + 1); 2740 port += np->in_spmin; 2741 } 2742 port = htons(port); 2743 np->in_spnext++; 2744 2745 if (np->in_spnext > np->in_spmax) { 2746 np->in_spnext = np->in_spmin; 2747 if (np->in_nsrcmsk != 0xffffffff) 2748 np->in_snip++; 2749 } 2750 } 2751 2752 if (np->in_flags & IPN_SIPRANGE) { 2753 if (np->in_snip > ntohl(np->in_nsrcmsk)) 2754 np->in_snip = ntohl(np->in_nsrcaddr); 2755 } else { 2756 if ((np->in_nsrcmsk != 0xffffffff) && 2757 ((np->in_snip + 1) & ntohl(np->in_nsrcmsk)) > 2758 ntohl(np->in_nsrcaddr)) 2759 np->in_snip = ntohl(np->in_nsrcaddr) + 1; 2760 } 2761 2762 if ((port == 0) && (flags & (IPN_TCPUDPICMP|IPN_ICMPQUERY))) 2763 port = sport; 2764 2765 /* 2766 * Here we do a lookup of the connection as seen from 2767 * the outside. If an IP# pair already exists, try 2768 * again. So if you have A->B becomes C->B, you can 2769 * also have D->E become C->E but not D->B causing 2770 * another C->B. Also take protocol and ports into 2771 * account when determining whether a pre-existing 2772 * NAT setup will cause an external conflict where 2773 * this is appropriate. 2774 */ 2775 inb.s_addr = htonl(in.s_addr); 2776 sp = fin->fin_data[0]; 2777 dp = fin->fin_data[1]; 2778 fin->fin_data[0] = fin->fin_data[1]; 2779 fin->fin_data[1] = ntohs(port); 2780 natl = ipf_nat_inlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH), 2781 (u_int)fin->fin_p, fin->fin_dst, inb); 2782 fin->fin_data[0] = sp; 2783 fin->fin_data[1] = dp; 2784 2785 /* 2786 * Has the search wrapped around and come back to the 2787 * start ? 2788 */ 2789 if ((natl != NULL) && 2790 (np->in_spnext != 0) && (st_port == np->in_spnext) && 2791 (np->in_snip != 0) && (st_ip == np->in_snip)) { 2792 NBUMPSIDED(1, ns_wrap); 2793 return -1; 2794 } 2795 l++; 2796 } while (natl != NULL); 2797 2798 /* Setup the NAT table */ 2799 nat->nat_osrcip = fin->fin_src; 2800 nat->nat_nsrcaddr = htonl(in.s_addr); 2801 nat->nat_odstip = fin->fin_dst; 2802 nat->nat_ndstip = fin->fin_dst; 2803 if (nat->nat_hm == NULL) 2804 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src, 2805 fin->fin_dst, nat->nat_nsrcip, 2806 0); 2807 2808 if (flags & IPN_TCPUDP) { 2809 nat->nat_osport = sport; 2810 nat->nat_nsport = port; /* sport */ 2811 nat->nat_odport = dport; 2812 nat->nat_ndport = dport; 2813 ((tcphdr_t *)fin->fin_dp)->th_sport = port; 2814 } else if (flags & IPN_ICMPQUERY) { 2815 nat->nat_oicmpid = fin->fin_data[1]; 2816 ((icmphdr_t *)fin->fin_dp)->icmp_id = port; 2817 nat->nat_nicmpid = port; 2818 } 2819 return 0; 2820 } 2821 2822 2823 /* ------------------------------------------------------------------------ */ 2824 /* Function: ipf_nat_newrdr */ 2825 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */ 2826 /* allow rule to be moved if IPN_ROUNDR is set. */ 2827 /* Parameters: fin(I) - pointer to packet information */ 2828 /* nat(I) - pointer to NAT entry */ 2829 /* ni(I) - pointer to structure with misc. information needed */ 2830 /* to create new NAT entry. */ 2831 /* */ 2832 /* ni.nai_ip is passed in uninitialised and must be set, in host byte order,*/ 2833 /* to the new IP address for the translation. */ 2834 /* ------------------------------------------------------------------------ */ 2835 static int 2836 ipf_nat_newrdr(fr_info_t *fin, nat_t *nat, natinfo_t *ni) 2837 { 2838 ipf_main_softc_t *softc = fin->fin_main_soft; 2839 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 2840 u_short nport, dport, sport; 2841 struct in_addr in, inb; 2842 u_short sp, dp; 2843 hostmap_t *hm; 2844 u_32_t flags; 2845 ipnat_t *np; 2846 nat_t *natl; 2847 int move; 2848 2849 move = 1; 2850 hm = NULL; 2851 in.s_addr = 0; 2852 np = ni->nai_np; 2853 flags = nat->nat_flags; 2854 2855 if (flags & IPN_ICMPQUERY) { 2856 dport = fin->fin_data[1]; 2857 sport = 0; 2858 } else { 2859 sport = htons(fin->fin_data[0]); 2860 dport = htons(fin->fin_data[1]); 2861 } 2862 2863 /* TRACE sport, dport */ 2864 2865 2866 /* 2867 * If the matching rule has IPN_STICKY set, then we want to have the 2868 * same rule kick in as before. Why would this happen? If you have 2869 * a collection of rdr rules with "round-robin sticky", the current 2870 * packet might match a different one to the previous connection but 2871 * we want the same destination to be used. 2872 */ 2873 if (((np->in_flags & (IPN_ROUNDR|IPN_SPLIT)) != 0) && 2874 ((np->in_flags & IPN_STICKY) != 0)) { 2875 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, fin->fin_dst, 2876 in, (u_32_t)dport); 2877 if (hm != NULL) { 2878 in.s_addr = ntohl(hm->hm_ndstip.s_addr); 2879 np = hm->hm_ipnat; 2880 ni->nai_np = np; 2881 move = 0; 2882 ipf_nat_hostmapdel(softc, &hm); 2883 } 2884 } 2885 2886 /* 2887 * Otherwise, it's an inbound packet. Most likely, we don't 2888 * want to rewrite source ports and source addresses. Instead, 2889 * we want to rewrite to a fixed internal address and fixed 2890 * internal port. 2891 */ 2892 if (np->in_flags & IPN_SPLIT) { 2893 in.s_addr = np->in_dnip; 2894 2895 if ((np->in_flags & (IPN_ROUNDR|IPN_STICKY)) == IPN_STICKY) { 2896 hm = ipf_nat_hostmap(softn, NULL, fin->fin_src, 2897 fin->fin_dst, in, (u_32_t)dport); 2898 if (hm != NULL) { 2899 in.s_addr = hm->hm_ndstip.s_addr; 2900 move = 0; 2901 } 2902 } 2903 2904 if (hm == NULL || hm->hm_ref == 1) { 2905 if (np->in_ndstaddr == htonl(in.s_addr)) { 2906 np->in_dnip = ntohl(np->in_ndstmsk); 2907 move = 0; 2908 } else { 2909 np->in_dnip = ntohl(np->in_ndstaddr); 2910 } 2911 } 2912 if (hm != NULL) 2913 ipf_nat_hostmapdel(softc, &hm); 2914 2915 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) { 2916 i6addr_t in6; 2917 2918 /* 2919 * 0/32 - use the interface's IP address. 2920 */ 2921 if (ipf_ifpaddr(softc, 4, FRI_NORMAL, fin->fin_ifp, 2922 &in6, NULL) == -1) { 2923 NBUMPSIDEX(0, ns_new_ifpaddr, ns_new_ifpaddr_2); 2924 return -1; 2925 } 2926 in.s_addr = ntohl(in6.in4.s_addr); 2927 2928 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk== 0)) { 2929 /* 2930 * 0/0 - use the original destination address/port. 2931 */ 2932 in.s_addr = ntohl(fin->fin_daddr); 2933 2934 } else if (np->in_redir == NAT_BIMAP && 2935 np->in_ndstmsk == np->in_odstmsk) { 2936 /* 2937 * map the address block in a 1:1 fashion 2938 */ 2939 in.s_addr = np->in_ndstaddr; 2940 in.s_addr |= fin->fin_daddr & ~np->in_ndstmsk; 2941 in.s_addr = ntohl(in.s_addr); 2942 } else { 2943 in.s_addr = ntohl(np->in_ndstaddr); 2944 } 2945 2946 if ((np->in_dpnext == 0) || ((flags & NAT_NOTRULEPORT) != 0)) 2947 nport = dport; 2948 else { 2949 /* 2950 * Whilst not optimized for the case where 2951 * pmin == pmax, the gain is not significant. 2952 */ 2953 if (((np->in_flags & IPN_FIXEDDPORT) == 0) && 2954 (np->in_odport != np->in_dtop)) { 2955 nport = ntohs(dport) - np->in_odport + np->in_dpmax; 2956 nport = htons(nport); 2957 } else { 2958 nport = htons(np->in_dpnext); 2959 np->in_dpnext++; 2960 if (np->in_dpnext > np->in_dpmax) 2961 np->in_dpnext = np->in_dpmin; 2962 } 2963 } 2964 2965 /* 2966 * When the redirect-to address is set to 0.0.0.0, just 2967 * assume a blank `forwarding' of the packet. We don't 2968 * setup any translation for this either. 2969 */ 2970 if (in.s_addr == 0) { 2971 if (nport == dport) { 2972 NBUMPSIDED(0, ns_xlate_null); 2973 return -1; 2974 } 2975 in.s_addr = ntohl(fin->fin_daddr); 2976 } 2977 2978 /* 2979 * Check to see if this redirect mapping already exists and if 2980 * it does, return "failure" (allowing it to be created will just 2981 * cause one or both of these "connections" to stop working.) 2982 */ 2983 inb.s_addr = htonl(in.s_addr); 2984 sp = fin->fin_data[0]; 2985 dp = fin->fin_data[1]; 2986 fin->fin_data[1] = fin->fin_data[0]; 2987 fin->fin_data[0] = ntohs(nport); 2988 natl = ipf_nat_outlookup(fin, flags & ~(SI_WILDP|NAT_SEARCH), 2989 (u_int)fin->fin_p, inb, fin->fin_src); 2990 fin->fin_data[0] = sp; 2991 fin->fin_data[1] = dp; 2992 if (natl != NULL) { 2993 DT2(ns_new_xlate_exists, fr_info_t *, fin, nat_t *, natl); 2994 NBUMPSIDE(0, ns_xlate_exists); 2995 return -1; 2996 } 2997 2998 nat->nat_ndstaddr = htonl(in.s_addr); 2999 nat->nat_odstip = fin->fin_dst; 3000 nat->nat_nsrcip = fin->fin_src; 3001 nat->nat_osrcip = fin->fin_src; 3002 if ((nat->nat_hm == NULL) && ((np->in_flags & IPN_STICKY) != 0)) 3003 nat->nat_hm = ipf_nat_hostmap(softn, np, fin->fin_src, 3004 fin->fin_dst, in, (u_32_t)dport); 3005 3006 if (flags & IPN_TCPUDP) { 3007 nat->nat_odport = dport; 3008 nat->nat_ndport = nport; 3009 nat->nat_osport = sport; 3010 nat->nat_nsport = sport; 3011 ((tcphdr_t *)fin->fin_dp)->th_dport = nport; 3012 } else if (flags & IPN_ICMPQUERY) { 3013 nat->nat_oicmpid = fin->fin_data[1]; 3014 ((icmphdr_t *)fin->fin_dp)->icmp_id = nport; 3015 nat->nat_nicmpid = nport; 3016 } 3017 3018 return move; 3019 } 3020 3021 /* ------------------------------------------------------------------------ */ 3022 /* Function: ipf_nat_add */ 3023 /* Returns: nat_t* - NULL == failure to create new NAT structure, */ 3024 /* else pointer to new NAT structure */ 3025 /* Parameters: fin(I) - pointer to packet information */ 3026 /* np(I) - pointer to NAT rule */ 3027 /* natsave(I) - pointer to where to store NAT struct pointer */ 3028 /* flags(I) - flags describing the current packet */ 3029 /* direction(I) - direction of packet (in/out) */ 3030 /* Write Lock: ipf_nat */ 3031 /* */ 3032 /* Attempts to create a new NAT entry. Does not actually change the packet */ 3033 /* in any way. */ 3034 /* */ 3035 /* This fucntion is in three main parts: (1) deal with creating a new NAT */ 3036 /* structure for a "MAP" rule (outgoing NAT translation); (2) deal with */ 3037 /* creating a new NAT structure for a "RDR" rule (incoming NAT translation) */ 3038 /* and (3) building that structure and putting it into the NAT table(s). */ 3039 /* */ 3040 /* NOTE: natsave should NOT be used top point back to an ipstate_t struct */ 3041 /* as it can result in memory being corrupted. */ 3042 /* ------------------------------------------------------------------------ */ 3043 nat_t * 3044 ipf_nat_add(fr_info_t *fin, ipnat_t *np, nat_t **natsave, u_int flags, 3045 int direction) 3046 { 3047 ipf_main_softc_t *softc = fin->fin_main_soft; 3048 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 3049 hostmap_t *hm = NULL; 3050 nat_t *nat, *natl; 3051 natstat_t *nsp; 3052 u_int nflags; 3053 natinfo_t ni; 3054 int move; 3055 3056 memset(&ni, 0, sizeof ni); /* XXX gcc */ 3057 nsp = &softn->ipf_nat_stats; 3058 3059 if ((nsp->ns_active * 100 / softn->ipf_nat_table_max) > 3060 softn->ipf_nat_table_wm_high) { 3061 softn->ipf_nat_doflush = 1; 3062 } 3063 3064 if (nsp->ns_active >= softn->ipf_nat_table_max) { 3065 NBUMPSIDED(fin->fin_out, ns_table_max); 3066 return NULL; 3067 } 3068 3069 move = 1; 3070 nflags = np->in_flags & flags; 3071 nflags &= NAT_FROMRULE; 3072 3073 ni.nai_np = np; 3074 ni.nai_dport = 0; 3075 ni.nai_sport = 0; 3076 3077 /* Give me a new nat */ 3078 KMALLOC(nat, nat_t *); 3079 if (nat == NULL) { 3080 NBUMPSIDED(fin->fin_out, ns_memfail); 3081 /* 3082 * Try to automatically tune the max # of entries in the 3083 * table allowed to be less than what will cause kmem_alloc() 3084 * to fail and try to eliminate panics due to out of memory 3085 * conditions arising. 3086 */ 3087 if ((softn->ipf_nat_table_max > softn->ipf_nat_table_sz) && 3088 (nsp->ns_active > 100)) { 3089 softn->ipf_nat_table_max = nsp->ns_active - 100; 3090 printf("table_max reduced to %d\n", 3091 softn->ipf_nat_table_max); 3092 } 3093 return NULL; 3094 } 3095 3096 if (flags & IPN_ICMPQUERY) { 3097 /* 3098 * In the ICMP query NAT code, we translate the ICMP id fields 3099 * to make them unique. This is indepedent of the ICMP type 3100 * (e.g. in the unlikely event that a host sends an echo and 3101 * an tstamp request with the same id, both packets will have 3102 * their ip address/id field changed in the same way). 3103 */ 3104 /* The icmp_id field is used by the sender to identify the 3105 * process making the icmp request. (the receiver justs 3106 * copies it back in its response). So, it closely matches 3107 * the concept of source port. We overlay sport, so we can 3108 * maximally reuse the existing code. 3109 */ 3110 ni.nai_sport = fin->fin_data[1]; 3111 ni.nai_dport = 0; 3112 } 3113 3114 bzero((char *)nat, sizeof(*nat)); 3115 nat->nat_flags = flags; 3116 nat->nat_redir = np->in_redir; 3117 nat->nat_dir = direction; 3118 nat->nat_pr[0] = fin->fin_p; 3119 nat->nat_pr[1] = fin->fin_p; 3120 3121 /* 3122 * Search the current table for a match and create a new mapping 3123 * if there is none found. 3124 */ 3125 if (np->in_redir & NAT_DIVERTUDP) { 3126 move = ipf_nat_newdivert(fin, nat, &ni); 3127 3128 } else if (np->in_redir & NAT_REWRITE) { 3129 move = ipf_nat_newrewrite(fin, nat, &ni); 3130 3131 } else if (direction == NAT_OUTBOUND) { 3132 /* 3133 * We can now arrange to call this for the same connection 3134 * because ipf_nat_new doesn't protect the code path into 3135 * this function. 3136 */ 3137 natl = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p, 3138 fin->fin_src, fin->fin_dst); 3139 if (natl != NULL) { 3140 KFREE(nat); 3141 nat = natl; 3142 goto done; 3143 } 3144 3145 move = ipf_nat_newmap(fin, nat, &ni); 3146 } else { 3147 /* 3148 * NAT_INBOUND is used for redirects rules 3149 */ 3150 natl = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p, 3151 fin->fin_src, fin->fin_dst); 3152 if (natl != NULL) { 3153 KFREE(nat); 3154 nat = natl; 3155 goto done; 3156 } 3157 3158 move = ipf_nat_newrdr(fin, nat, &ni); 3159 } 3160 if (move == -1) 3161 goto badnat; 3162 3163 np = ni.nai_np; 3164 3165 nat->nat_mssclamp = np->in_mssclamp; 3166 nat->nat_me = natsave; 3167 nat->nat_fr = fin->fin_fr; 3168 nat->nat_rev = fin->fin_rev; 3169 nat->nat_ptr = np; 3170 nat->nat_dlocal = np->in_dlocal; 3171 3172 if ((np->in_apr != NULL) && ((nat->nat_flags & NAT_SLAVE) == 0)) { 3173 if (ipf_proxy_new(fin, nat) == -1) { 3174 NBUMPSIDED(fin->fin_out, ns_appr_fail); 3175 goto badnat; 3176 } 3177 } 3178 3179 nat->nat_ifps[0] = np->in_ifps[0]; 3180 if (np->in_ifps[0] != NULL) { 3181 COPYIFNAME(np->in_v[0], np->in_ifps[0], nat->nat_ifnames[0]); 3182 } 3183 3184 nat->nat_ifps[1] = np->in_ifps[1]; 3185 if (np->in_ifps[1] != NULL) { 3186 COPYIFNAME(np->in_v[1], np->in_ifps[1], nat->nat_ifnames[1]); 3187 } 3188 3189 if (ipf_nat_finalise(fin, nat) == -1) { 3190 goto badnat; 3191 } 3192 3193 np->in_use++; 3194 3195 if ((move == 1) && (np->in_flags & IPN_ROUNDR)) { 3196 if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_REDIRECT) { 3197 ipf_nat_delrdr(softn, np); 3198 ipf_nat_addrdr(softn, np); 3199 } else if ((np->in_redir & (NAT_REDIRECT|NAT_MAP)) == NAT_MAP) { 3200 ipf_nat_delmap(softn, np); 3201 ipf_nat_addmap(softn, np); 3202 } 3203 } 3204 3205 if (flags & SI_WILDP) 3206 nsp->ns_wilds++; 3207 nsp->ns_proto[nat->nat_pr[0]]++; 3208 3209 goto done; 3210 badnat: 3211 DT2(ns_badnatnew, fr_info_t *, fin, nat_t *, nat); 3212 NBUMPSIDE(fin->fin_out, ns_badnatnew); 3213 if ((hm = nat->nat_hm) != NULL) 3214 ipf_nat_hostmapdel(softc, &hm); 3215 KFREE(nat); 3216 nat = NULL; 3217 done: 3218 if (nat != NULL && np != NULL) 3219 np->in_hits++; 3220 if (natsave != NULL) 3221 *natsave = nat; 3222 return nat; 3223 } 3224 3225 3226 /* ------------------------------------------------------------------------ */ 3227 /* Function: ipf_nat_finalise */ 3228 /* Returns: int - 0 == sucess, -1 == failure */ 3229 /* Parameters: fin(I) - pointer to packet information */ 3230 /* nat(I) - pointer to NAT entry */ 3231 /* Write Lock: ipf_nat */ 3232 /* */ 3233 /* This is the tail end of constructing a new NAT entry and is the same */ 3234 /* for both IPv4 and IPv6. */ 3235 /* ------------------------------------------------------------------------ */ 3236 /*ARGSUSED*/ 3237 static int 3238 ipf_nat_finalise(fr_info_t *fin, nat_t *nat) 3239 { 3240 ipf_main_softc_t *softc = fin->fin_main_soft; 3241 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 3242 u_32_t sum1, sum2, sumd; 3243 frentry_t *fr; 3244 u_32_t flags; 3245 #if SOLARIS && defined(_KERNEL) && (SOLARIS2 >= 6) && defined(ICK_M_CTL_MAGIC) 3246 qpktinfo_t *qpi = fin->fin_qpi; 3247 #endif 3248 3249 flags = nat->nat_flags; 3250 3251 switch (nat->nat_pr[0]) 3252 { 3253 case IPPROTO_ICMP : 3254 sum1 = LONG_SUM(ntohs(nat->nat_oicmpid)); 3255 sum2 = LONG_SUM(ntohs(nat->nat_nicmpid)); 3256 CALC_SUMD(sum1, sum2, sumd); 3257 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16); 3258 3259 break; 3260 3261 default : 3262 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr) + \ 3263 ntohs(nat->nat_osport)); 3264 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr) + \ 3265 ntohs(nat->nat_nsport)); 3266 CALC_SUMD(sum1, sum2, sumd); 3267 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16); 3268 3269 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr) + \ 3270 ntohs(nat->nat_odport)); 3271 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr) + \ 3272 ntohs(nat->nat_ndport)); 3273 CALC_SUMD(sum1, sum2, sumd); 3274 nat->nat_sumd[0] += (sumd & 0xffff) + (sumd >> 16); 3275 break; 3276 } 3277 3278 /* 3279 * Compute the partial checksum, just in case. 3280 * This is only ever placed into outbound packets so care needs 3281 * to be taken over which pair of addresses are used. 3282 */ 3283 if (nat->nat_dir == NAT_OUTBOUND) { 3284 sum1 = LONG_SUM(ntohl(nat->nat_nsrcaddr)); 3285 sum1 += LONG_SUM(ntohl(nat->nat_ndstaddr)); 3286 } else { 3287 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr)); 3288 sum1 += LONG_SUM(ntohl(nat->nat_odstaddr)); 3289 } 3290 sum1 += nat->nat_pr[1]; 3291 nat->nat_sumd[1] = (sum1 & 0xffff) + (sum1 >> 16); 3292 3293 sum1 = LONG_SUM(ntohl(nat->nat_osrcaddr)); 3294 sum2 = LONG_SUM(ntohl(nat->nat_nsrcaddr)); 3295 CALC_SUMD(sum1, sum2, sumd); 3296 nat->nat_ipsumd = (sumd & 0xffff) + (sumd >> 16); 3297 3298 sum1 = LONG_SUM(ntohl(nat->nat_odstaddr)); 3299 sum2 = LONG_SUM(ntohl(nat->nat_ndstaddr)); 3300 CALC_SUMD(sum1, sum2, sumd); 3301 nat->nat_ipsumd += (sumd & 0xffff) + (sumd >> 16); 3302 3303 nat->nat_v[0] = 4; 3304 nat->nat_v[1] = 4; 3305 3306 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) { 3307 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]); 3308 } 3309 3310 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) { 3311 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]); 3312 } 3313 3314 if ((nat->nat_flags & SI_CLONE) == 0) 3315 nat->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, nat); 3316 3317 if (ipf_nat_insert(softc, softn, nat) == 0) { 3318 if (softn->ipf_nat_logging) 3319 ipf_nat_log(softc, softn, nat, NL_NEW); 3320 fr = nat->nat_fr; 3321 if (fr != NULL) { 3322 MUTEX_ENTER(&fr->fr_lock); 3323 fr->fr_ref++; 3324 MUTEX_EXIT(&fr->fr_lock); 3325 } 3326 return 0; 3327 } 3328 3329 NBUMPSIDED(fin->fin_out, ns_unfinalised); 3330 /* 3331 * nat_insert failed, so cleanup time... 3332 */ 3333 if (nat->nat_sync != NULL) 3334 ipf_sync_del_nat(softc->ipf_sync_soft, nat->nat_sync); 3335 return -1; 3336 } 3337 3338 3339 /* ------------------------------------------------------------------------ */ 3340 /* Function: ipf_nat_insert */ 3341 /* Returns: int - 0 == sucess, -1 == failure */ 3342 /* Parameters: softc(I) - pointer to soft context main structure */ 3343 /* softn(I) - pointer to NAT context structure */ 3344 /* nat(I) - pointer to NAT structure */ 3345 /* Write Lock: ipf_nat */ 3346 /* */ 3347 /* Insert a NAT entry into the hash tables for searching and add it to the */ 3348 /* list of active NAT entries. Adjust global counters when complete. */ 3349 /* ------------------------------------------------------------------------ */ 3350 int 3351 ipf_nat_insert(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat) 3352 { 3353 u_int hv0, hv1; 3354 u_int sp, dp; 3355 ipnat_t *in; 3356 3357 /* 3358 * Try and return an error as early as possible, so calculate the hash 3359 * entry numbers first and then proceed. 3360 */ 3361 if ((nat->nat_flags & (SI_W_SPORT|SI_W_DPORT)) == 0) { 3362 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 3363 sp = nat->nat_osport; 3364 dp = nat->nat_odport; 3365 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) { 3366 sp = 0; 3367 dp = nat->nat_oicmpid; 3368 } else { 3369 sp = 0; 3370 dp = 0; 3371 } 3372 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, sp, 0xffffffff); 3373 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0 + dp, 0xffffffff); 3374 /* 3375 * TRACE nat_osrcaddr, nat_osport, nat_odstaddr, 3376 * nat_odport, hv0 3377 */ 3378 3379 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 3380 sp = nat->nat_nsport; 3381 dp = nat->nat_ndport; 3382 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) { 3383 sp = 0; 3384 dp = nat->nat_nicmpid; 3385 } else { 3386 sp = 0; 3387 dp = 0; 3388 } 3389 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, sp, 0xffffffff); 3390 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1 + dp, 0xffffffff); 3391 /* 3392 * TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, 3393 * nat_ndport, hv1 3394 */ 3395 } else { 3396 hv0 = NAT_HASH_FN(nat->nat_osrcaddr, 0, 0xffffffff); 3397 hv0 = NAT_HASH_FN(nat->nat_odstaddr, hv0, 0xffffffff); 3398 /* TRACE nat_osrcaddr, nat_odstaddr, hv0 */ 3399 3400 hv1 = NAT_HASH_FN(nat->nat_nsrcaddr, 0, 0xffffffff); 3401 hv1 = NAT_HASH_FN(nat->nat_ndstaddr, hv1, 0xffffffff); 3402 /* TRACE nat_nsrcaddr, nat_ndstaddr, hv1 */ 3403 } 3404 3405 nat->nat_hv[0] = hv0; 3406 nat->nat_hv[1] = hv1; 3407 3408 MUTEX_INIT(&nat->nat_lock, "nat entry lock"); 3409 3410 in = nat->nat_ptr; 3411 nat->nat_ref = nat->nat_me ? 2 : 1; 3412 3413 nat->nat_ifnames[0][LIFNAMSIZ - 1] = '\0'; 3414 nat->nat_ifps[0] = ipf_resolvenic(softc, nat->nat_ifnames[0], 4); 3415 3416 if (nat->nat_ifnames[1][0] != '\0') { 3417 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0'; 3418 nat->nat_ifps[1] = ipf_resolvenic(softc, 3419 nat->nat_ifnames[1], 4); 3420 } else if (in->in_ifnames[1] != -1) { 3421 char *name; 3422 3423 name = in->in_names + in->in_ifnames[1]; 3424 if (name[1] != '\0' && name[0] != '-' && name[0] != '*') { 3425 (void) strncpy(nat->nat_ifnames[1], 3426 nat->nat_ifnames[0], LIFNAMSIZ); 3427 nat->nat_ifnames[1][LIFNAMSIZ - 1] = '\0'; 3428 nat->nat_ifps[1] = nat->nat_ifps[0]; 3429 } 3430 } 3431 if ((nat->nat_ifps[0] != NULL) && (nat->nat_ifps[0] != (void *)-1)) { 3432 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]); 3433 } 3434 if ((nat->nat_ifps[1] != NULL) && (nat->nat_ifps[1] != (void *)-1)) { 3435 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]); 3436 } 3437 3438 return ipf_nat_hashtab_add(softc, softn, nat); 3439 } 3440 3441 3442 /* ------------------------------------------------------------------------ */ 3443 /* Function: ipf_nat_hashtab_add */ 3444 /* Parameters: softc(I) - pointer to soft context main structure */ 3445 /* softn(I) - pointer to NAT context structure */ 3446 /* nat(I) - pointer to NAT structure */ 3447 /* */ 3448 /* Handle the insertion of a NAT entry into the table/list. */ 3449 /* ------------------------------------------------------------------------ */ 3450 int 3451 ipf_nat_hashtab_add(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat) 3452 { 3453 nat_t **natp; 3454 u_int hv0; 3455 u_int hv1; 3456 3457 hv0 = nat->nat_hv[0] % softn->ipf_nat_table_sz; 3458 hv1 = nat->nat_hv[1] % softn->ipf_nat_table_sz; 3459 3460 if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) { 3461 u_int swap; 3462 3463 swap = hv0; 3464 hv0 = hv1; 3465 hv1 = swap; 3466 } 3467 3468 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0] >= 3469 softn->ipf_nat_maxbucket) { 3470 DT1(ns_bucket_max_0, int, 3471 softn->ipf_nat_stats.ns_side[0].ns_bucketlen[hv0]); 3472 NBUMPSIDE(0, ns_bucket_max); 3473 return -1; 3474 } 3475 3476 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1] >= 3477 softn->ipf_nat_maxbucket) { 3478 DT1(ns_bucket_max_1, int, 3479 softn->ipf_nat_stats.ns_side[1].ns_bucketlen[hv1]); 3480 NBUMPSIDE(1, ns_bucket_max); 3481 return -1; 3482 } 3483 3484 /* 3485 * The ordering of operations in the list and hash table insertion 3486 * is very important. The last operation for each task should be 3487 * to update the top of the list, after all the "nexts" have been 3488 * done so that walking the list while it is being done does not 3489 * find strange pointers. 3490 * 3491 * Global list of NAT instances 3492 */ 3493 nat->nat_next = softn->ipf_nat_instances; 3494 nat->nat_pnext = &softn->ipf_nat_instances; 3495 if (softn->ipf_nat_instances) 3496 softn->ipf_nat_instances->nat_pnext = &nat->nat_next; 3497 softn->ipf_nat_instances = nat; 3498 3499 /* 3500 * Inbound hash table. 3501 */ 3502 natp = &softn->ipf_nat_table[0][hv0]; 3503 nat->nat_phnext[0] = natp; 3504 nat->nat_hnext[0] = *natp; 3505 if (*natp) { 3506 (*natp)->nat_phnext[0] = &nat->nat_hnext[0]; 3507 } else { 3508 NBUMPSIDE(0, ns_inuse); 3509 } 3510 *natp = nat; 3511 NBUMPSIDE(0, ns_bucketlen[hv0]); 3512 3513 /* 3514 * Outbound hash table. 3515 */ 3516 natp = &softn->ipf_nat_table[1][hv1]; 3517 nat->nat_phnext[1] = natp; 3518 nat->nat_hnext[1] = *natp; 3519 if (*natp) 3520 (*natp)->nat_phnext[1] = &nat->nat_hnext[1]; 3521 else { 3522 NBUMPSIDE(1, ns_inuse); 3523 } 3524 *natp = nat; 3525 NBUMPSIDE(1, ns_bucketlen[hv1]); 3526 3527 ipf_nat_setqueue(softc, softn, nat); 3528 3529 if (nat->nat_dir & NAT_OUTBOUND) { 3530 NBUMPSIDE(1, ns_added); 3531 } else { 3532 NBUMPSIDE(0, ns_added); 3533 } 3534 softn->ipf_nat_stats.ns_active++; 3535 return 0; 3536 } 3537 3538 3539 /* ------------------------------------------------------------------------ */ 3540 /* Function: ipf_nat_icmperrorlookup */ 3541 /* Returns: nat_t* - point to matching NAT structure */ 3542 /* Parameters: fin(I) - pointer to packet information */ 3543 /* dir(I) - direction of packet (in/out) */ 3544 /* */ 3545 /* Check if the ICMP error message is related to an existing TCP, UDP or */ 3546 /* ICMP query nat entry. It is assumed that the packet is already of the */ 3547 /* the required length. */ 3548 /* ------------------------------------------------------------------------ */ 3549 nat_t * 3550 ipf_nat_icmperrorlookup(fr_info_t *fin, int dir) 3551 { 3552 ipf_main_softc_t *softc = fin->fin_main_soft; 3553 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 3554 int flags = 0, type, minlen; 3555 icmphdr_t *icmp, *orgicmp; 3556 nat_stat_side_t *nside; 3557 tcphdr_t *tcp = NULL; 3558 u_short data[2]; 3559 nat_t *nat; 3560 ip_t *oip; 3561 u_int p; 3562 3563 icmp = fin->fin_dp; 3564 type = icmp->icmp_type; 3565 nside = &softn->ipf_nat_stats.ns_side[fin->fin_out]; 3566 /* 3567 * Does it at least have the return (basic) IP header ? 3568 * Only a basic IP header (no options) should be with an ICMP error 3569 * header. Also, if it's not an error type, then return. 3570 */ 3571 if ((fin->fin_hlen != sizeof(ip_t)) || !(fin->fin_flx & FI_ICMPERR)) { 3572 ATOMIC_INCL(nside->ns_icmp_basic); 3573 return NULL; 3574 } 3575 3576 /* 3577 * Check packet size 3578 */ 3579 oip = (ip_t *)((char *)fin->fin_dp + 8); 3580 minlen = IP_HL(oip) << 2; 3581 if ((minlen < sizeof(ip_t)) || 3582 (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen)) { 3583 ATOMIC_INCL(nside->ns_icmp_size); 3584 return NULL; 3585 } 3586 3587 /* 3588 * Is the buffer big enough for all of it ? It's the size of the IP 3589 * header claimed in the encapsulated part which is of concern. It 3590 * may be too big to be in this buffer but not so big that it's 3591 * outside the ICMP packet, leading to TCP deref's causing problems. 3592 * This is possible because we don't know how big oip_hl is when we 3593 * do the pullup early in ipf_check() and thus can't gaurantee it is 3594 * all here now. 3595 */ 3596 #ifdef ipf_nat_KERNEL 3597 { 3598 mb_t *m; 3599 3600 m = fin->fin_m; 3601 # if defined(MENTAT) 3602 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > 3603 (char *)m->b_wptr) { 3604 ATOMIC_INCL(nside->ns_icmp_mbuf); 3605 return NULL; 3606 } 3607 # else 3608 if ((char *)oip + fin->fin_dlen - ICMPERR_ICMPHLEN > 3609 (char *)fin->fin_ip + M_LEN(m)) { 3610 ATOMIC_INCL(nside->ns_icmp_mbuf); 3611 return NULL; 3612 } 3613 # endif 3614 } 3615 #endif 3616 3617 if (fin->fin_daddr != oip->ip_src.s_addr) { 3618 ATOMIC_INCL(nside->ns_icmp_address); 3619 return NULL; 3620 } 3621 3622 p = oip->ip_p; 3623 if (p == IPPROTO_TCP) 3624 flags = IPN_TCP; 3625 else if (p == IPPROTO_UDP) 3626 flags = IPN_UDP; 3627 else if (p == IPPROTO_ICMP) { 3628 orgicmp = (icmphdr_t *)((char *)oip + (IP_HL(oip) << 2)); 3629 3630 /* see if this is related to an ICMP query */ 3631 if (ipf_nat_icmpquerytype(orgicmp->icmp_type)) { 3632 data[0] = fin->fin_data[0]; 3633 data[1] = fin->fin_data[1]; 3634 fin->fin_data[0] = 0; 3635 fin->fin_data[1] = orgicmp->icmp_id; 3636 3637 flags = IPN_ICMPERR|IPN_ICMPQUERY; 3638 /* 3639 * NOTE : dir refers to the direction of the original 3640 * ip packet. By definition the icmp error 3641 * message flows in the opposite direction. 3642 */ 3643 if (dir == NAT_INBOUND) 3644 nat = ipf_nat_inlookup(fin, flags, p, 3645 oip->ip_dst, 3646 oip->ip_src); 3647 else 3648 nat = ipf_nat_outlookup(fin, flags, p, 3649 oip->ip_dst, 3650 oip->ip_src); 3651 fin->fin_data[0] = data[0]; 3652 fin->fin_data[1] = data[1]; 3653 return nat; 3654 } 3655 } 3656 3657 if (flags & IPN_TCPUDP) { 3658 minlen += 8; /* + 64bits of data to get ports */ 3659 /* TRACE (fin,minlen) */ 3660 if (fin->fin_plen < ICMPERR_IPICMPHLEN + minlen) { 3661 ATOMIC_INCL(nside->ns_icmp_short); 3662 return NULL; 3663 } 3664 3665 data[0] = fin->fin_data[0]; 3666 data[1] = fin->fin_data[1]; 3667 tcp = (tcphdr_t *)((char *)oip + (IP_HL(oip) << 2)); 3668 fin->fin_data[0] = ntohs(tcp->th_dport); 3669 fin->fin_data[1] = ntohs(tcp->th_sport); 3670 3671 if (dir == NAT_INBOUND) { 3672 nat = ipf_nat_inlookup(fin, flags, p, oip->ip_dst, 3673 oip->ip_src); 3674 } else { 3675 nat = ipf_nat_outlookup(fin, flags, p, oip->ip_dst, 3676 oip->ip_src); 3677 } 3678 fin->fin_data[0] = data[0]; 3679 fin->fin_data[1] = data[1]; 3680 return nat; 3681 } 3682 if (dir == NAT_INBOUND) 3683 nat = ipf_nat_inlookup(fin, 0, p, oip->ip_dst, oip->ip_src); 3684 else 3685 nat = ipf_nat_outlookup(fin, 0, p, oip->ip_dst, oip->ip_src); 3686 3687 return nat; 3688 } 3689 3690 3691 /* ------------------------------------------------------------------------ */ 3692 /* Function: ipf_nat_icmperror */ 3693 /* Returns: nat_t* - point to matching NAT structure */ 3694 /* Parameters: fin(I) - pointer to packet information */ 3695 /* nflags(I) - NAT flags for this packet */ 3696 /* dir(I) - direction of packet (in/out) */ 3697 /* */ 3698 /* Fix up an ICMP packet which is an error message for an existing NAT */ 3699 /* session. This will correct both packet header data and checksums. */ 3700 /* */ 3701 /* This should *ONLY* be used for incoming ICMP error packets to make sure */ 3702 /* a NAT'd ICMP packet gets correctly recognised. */ 3703 /* ------------------------------------------------------------------------ */ 3704 nat_t * 3705 ipf_nat_icmperror(fr_info_t *fin, u_int *nflags, int dir) 3706 { 3707 ipf_main_softc_t *softc = fin->fin_main_soft; 3708 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 3709 u_32_t sum1, sum2, sumd, sumd2; 3710 struct in_addr a1, a2, a3, a4; 3711 int flags, dlen, odst; 3712 icmphdr_t *icmp; 3713 u_short *csump; 3714 tcphdr_t *tcp; 3715 nat_t *nat; 3716 ip_t *oip; 3717 void *dp; 3718 3719 if ((fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) { 3720 NBUMPSIDED(fin->fin_out, ns_icmp_short); 3721 return NULL; 3722 } 3723 3724 /* 3725 * ipf_nat_icmperrorlookup() will return NULL for `defective' packets. 3726 */ 3727 if ((fin->fin_v != 4) || !(nat = ipf_nat_icmperrorlookup(fin, dir))) { 3728 NBUMPSIDED(fin->fin_out, ns_icmp_notfound); 3729 return NULL; 3730 } 3731 3732 tcp = NULL; 3733 csump = NULL; 3734 flags = 0; 3735 sumd2 = 0; 3736 *nflags = IPN_ICMPERR; 3737 icmp = fin->fin_dp; 3738 oip = (ip_t *)&icmp->icmp_ip; 3739 dp = (((char *)oip) + (IP_HL(oip) << 2)); 3740 if (oip->ip_p == IPPROTO_TCP) { 3741 tcp = (tcphdr_t *)dp; 3742 csump = (u_short *)&tcp->th_sum; 3743 flags = IPN_TCP; 3744 } else if (oip->ip_p == IPPROTO_UDP) { 3745 udphdr_t *udp; 3746 3747 udp = (udphdr_t *)dp; 3748 tcp = (tcphdr_t *)dp; 3749 csump = (u_short *)&udp->uh_sum; 3750 flags = IPN_UDP; 3751 } else if (oip->ip_p == IPPROTO_ICMP) 3752 flags = IPN_ICMPQUERY; 3753 dlen = fin->fin_plen - ((char *)dp - (char *)fin->fin_ip); 3754 3755 /* 3756 * Need to adjust ICMP header to include the real IP#'s and 3757 * port #'s. Only apply a checksum change relative to the 3758 * IP address change as it will be modified again in ipf_nat_checkout 3759 * for both address and port. Two checksum changes are 3760 * necessary for the two header address changes. Be careful 3761 * to only modify the checksum once for the port # and twice 3762 * for the IP#. 3763 */ 3764 3765 /* 3766 * Step 1 3767 * Fix the IP addresses in the offending IP packet. You also need 3768 * to adjust the IP header checksum of that offending IP packet. 3769 * 3770 * Normally, you would expect that the ICMP checksum of the 3771 * ICMP error message needs to be adjusted as well for the 3772 * IP address change in oip. 3773 * However, this is a NOP, because the ICMP checksum is 3774 * calculated over the complete ICMP packet, which includes the 3775 * changed oip IP addresses and oip->ip_sum. However, these 3776 * two changes cancel each other out (if the delta for 3777 * the IP address is x, then the delta for ip_sum is minus x), 3778 * so no change in the icmp_cksum is necessary. 3779 * 3780 * Inbound ICMP 3781 * ------------ 3782 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b 3783 * - response to outgoing packet (a,b)=>(c,b) (OIP_SRC=c,OIP_DST=b) 3784 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(b)=nat_newdstip 3785 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(b)=nat_olddstip 3786 * 3787 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c 3788 * - response to outgoing packet (c,a)=>(b,a) (OIP_SRC=b,OIP_DST=a) 3789 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip 3790 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip 3791 * 3792 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d 3793 * - response to outgoing packet (a,b)=>(c,d) (OIP_SRC=c,OIP_DST=d) 3794 * - OIP_SRC(c)=nat_newsrcip, OIP_DST(d)=nat_newdstip 3795 *=> OIP_SRC(c)=nat_oldsrcip, OIP_DST(d)=nat_olddstip 3796 * 3797 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d 3798 * - response to outgoing packet (d,c)=>(b,a) (OIP_SRC=b,OIP_DST=a) 3799 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip 3800 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip 3801 * 3802 * Outbound ICMP 3803 * ------------- 3804 * MAP rule, SRC=a,DST=b -> SRC=c,DST=b 3805 * - response to incoming packet (b,c)=>(b,a) (OIP_SRC=b,OIP_DST=a) 3806 * - OIP_SRC(b)=nat_olddstip, OIP_DST(a)=nat_oldsrcip 3807 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip 3808 * 3809 * RDR rule, SRC=a,DST=b -> SRC=a,DST=c 3810 * - response to incoming packet (a,b)=>(a,c) (OIP_SRC=a,OIP_DST=c) 3811 * - OIP_SRC(a)=nat_newsrcip, OIP_DST(c)=nat_newdstip 3812 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip 3813 * 3814 * REWRITE out rule, SRC=a,DST=b -> SRC=c,DST=d 3815 * - response to incoming packet (d,c)=>(b,a) (OIP_SRC=c,OIP_DST=d) 3816 * - OIP_SRC(c)=nat_olddstip, OIP_DST(d)=nat_oldsrcip 3817 *=> OIP_SRC(b)=nat_newdstip, OIP_DST(a)=nat_newsrcip 3818 * 3819 * REWRITE in rule, SRC=a,DST=b -> SRC=c,DST=d 3820 * - response to incoming packet (a,b)=>(c,d) (OIP_SRC=b,OIP_DST=a) 3821 * - OIP_SRC(b)=nat_newsrcip, OIP_DST(a)=nat_newdstip 3822 *=> OIP_SRC(a)=nat_oldsrcip, OIP_DST(c)=nat_olddstip 3823 */ 3824 3825 if (((fin->fin_out == 0) && ((nat->nat_redir & NAT_MAP) != 0)) || 3826 ((fin->fin_out == 1) && ((nat->nat_redir & NAT_REDIRECT) != 0))) { 3827 a1.s_addr = ntohl(nat->nat_osrcaddr); 3828 a4.s_addr = ntohl(oip->ip_src.s_addr); 3829 a3.s_addr = ntohl(nat->nat_odstaddr); 3830 a2.s_addr = ntohl(oip->ip_dst.s_addr); 3831 oip->ip_src.s_addr = htonl(a1.s_addr); 3832 oip->ip_dst.s_addr = htonl(a3.s_addr); 3833 odst = 1; 3834 } else { 3835 a1.s_addr = ntohl(nat->nat_ndstaddr); 3836 a2.s_addr = ntohl(oip->ip_dst.s_addr); 3837 a3.s_addr = ntohl(nat->nat_nsrcaddr); 3838 a4.s_addr = ntohl(oip->ip_src.s_addr); 3839 oip->ip_dst.s_addr = htonl(a3.s_addr); 3840 oip->ip_src.s_addr = htonl(a1.s_addr); 3841 odst = 0; 3842 } 3843 sum1 = 0; 3844 sum2 = 0; 3845 sumd = 0; 3846 CALC_SUMD(a2.s_addr, a3.s_addr, sum1); 3847 CALC_SUMD(a4.s_addr, a1.s_addr, sum2); 3848 sumd = sum2 + sum1; 3849 if (sumd != 0) 3850 ipf_fix_datacksum(&oip->ip_sum, sumd); 3851 3852 sumd2 = sumd; 3853 sum1 = 0; 3854 sum2 = 0; 3855 3856 /* 3857 * Fix UDP pseudo header checksum to compensate for the 3858 * IP address change. 3859 */ 3860 if (((flags & IPN_TCPUDP) != 0) && (dlen >= 4)) { 3861 u_32_t sum3, sum4, sumt; 3862 3863 /* 3864 * Step 2 : 3865 * For offending TCP/UDP IP packets, translate the ports as 3866 * well, based on the NAT specification. Of course such 3867 * a change may be reflected in the ICMP checksum as well. 3868 * 3869 * Since the port fields are part of the TCP/UDP checksum 3870 * of the offending IP packet, you need to adjust that checksum 3871 * as well... except that the change in the port numbers should 3872 * be offset by the checksum change. However, the TCP/UDP 3873 * checksum will also need to change if there has been an 3874 * IP address change. 3875 */ 3876 if (odst == 1) { 3877 sum1 = ntohs(nat->nat_osport); 3878 sum4 = ntohs(tcp->th_sport); 3879 sum3 = ntohs(nat->nat_odport); 3880 sum2 = ntohs(tcp->th_dport); 3881 3882 tcp->th_sport = htons(sum1); 3883 tcp->th_dport = htons(sum3); 3884 } else { 3885 sum1 = ntohs(nat->nat_ndport); 3886 sum2 = ntohs(tcp->th_dport); 3887 sum3 = ntohs(nat->nat_nsport); 3888 sum4 = ntohs(tcp->th_sport); 3889 3890 tcp->th_dport = htons(sum3); 3891 tcp->th_sport = htons(sum1); 3892 } 3893 CALC_SUMD(sum4, sum1, sumt); 3894 sumd += sumt; 3895 CALC_SUMD(sum2, sum3, sumt); 3896 sumd += sumt; 3897 3898 if (sumd != 0 || sumd2 != 0) { 3899 /* 3900 * At this point, sumd is the delta to apply to the 3901 * TCP/UDP header, given the changes in both the IP 3902 * address and the ports and sumd2 is the delta to 3903 * apply to the ICMP header, given the IP address 3904 * change delta that may need to be applied to the 3905 * TCP/UDP checksum instead. 3906 * 3907 * If we will both the IP and TCP/UDP checksums 3908 * then the ICMP checksum changes by the address 3909 * delta applied to the TCP/UDP checksum. If we 3910 * do not change the TCP/UDP checksum them we 3911 * apply the delta in ports to the ICMP checksum. 3912 */ 3913 if (oip->ip_p == IPPROTO_UDP) { 3914 if ((dlen >= 8) && (*csump != 0)) { 3915 ipf_fix_datacksum(csump, sumd); 3916 } else { 3917 CALC_SUMD(sum1, sum4, sumd2); 3918 CALC_SUMD(sum3, sum2, sumt); 3919 sumd2 += sumt; 3920 } 3921 } else if (oip->ip_p == IPPROTO_TCP) { 3922 if (dlen >= 18) { 3923 ipf_fix_datacksum(csump, sumd); 3924 } else { 3925 CALC_SUMD(sum1, sum4, sumd2); 3926 CALC_SUMD(sum3, sum2, sumt); 3927 sumd2 += sumt; 3928 } 3929 } 3930 if (sumd2 != 0) { 3931 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16); 3932 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16); 3933 sumd2 = (sumd2 & 0xffff) + (sumd2 >> 16); 3934 ipf_fix_incksum(0, &icmp->icmp_cksum, sumd2, 0); 3935 } 3936 } 3937 } else if (((flags & IPN_ICMPQUERY) != 0) && (dlen >= 8)) { 3938 icmphdr_t *orgicmp; 3939 3940 /* 3941 * XXX - what if this is bogus hl and we go off the end ? 3942 * In this case, ipf_nat_icmperrorlookup() will have 3943 * returned NULL. 3944 */ 3945 orgicmp = (icmphdr_t *)dp; 3946 3947 if (odst == 1) { 3948 if (orgicmp->icmp_id != nat->nat_osport) { 3949 3950 /* 3951 * Fix ICMP checksum (of the offening ICMP 3952 * query packet) to compensate the change 3953 * in the ICMP id of the offending ICMP 3954 * packet. 3955 * 3956 * Since you modify orgicmp->icmp_id with 3957 * a delta (say x) and you compensate that 3958 * in origicmp->icmp_cksum with a delta 3959 * minus x, you don't have to adjust the 3960 * overall icmp->icmp_cksum 3961 */ 3962 sum1 = ntohs(orgicmp->icmp_id); 3963 sum2 = ntohs(nat->nat_oicmpid); 3964 CALC_SUMD(sum1, sum2, sumd); 3965 orgicmp->icmp_id = nat->nat_oicmpid; 3966 ipf_fix_datacksum(&orgicmp->icmp_cksum, sumd); 3967 } 3968 } /* nat_dir == NAT_INBOUND is impossible for icmp queries */ 3969 } 3970 return nat; 3971 } 3972 3973 3974 /* 3975 * MAP-IN MAP-OUT RDR-IN RDR-OUT 3976 * osrc X == src == src X 3977 * odst X == dst == dst X 3978 * nsrc == dst X X == dst 3979 * ndst == src X X == src 3980 * MAP = NAT_OUTBOUND, RDR = NAT_INBOUND 3981 */ 3982 /* 3983 * NB: these lookups don't lock access to the list, it assumed that it has 3984 * already been done! 3985 */ 3986 /* ------------------------------------------------------------------------ */ 3987 /* Function: ipf_nat_inlookup */ 3988 /* Returns: nat_t* - NULL == no match, */ 3989 /* else pointer to matching NAT entry */ 3990 /* Parameters: fin(I) - pointer to packet information */ 3991 /* flags(I) - NAT flags for this packet */ 3992 /* p(I) - protocol for this packet */ 3993 /* src(I) - source IP address */ 3994 /* mapdst(I) - destination IP address */ 3995 /* */ 3996 /* Lookup a nat entry based on the mapped destination ip address/port and */ 3997 /* real source address/port. We use this lookup when receiving a packet, */ 3998 /* we're looking for a table entry, based on the destination address. */ 3999 /* */ 4000 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */ 4001 /* */ 4002 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */ 4003 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */ 4004 /* */ 4005 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */ 4006 /* the packet is of said protocol */ 4007 /* ------------------------------------------------------------------------ */ 4008 nat_t * 4009 ipf_nat_inlookup(fr_info_t *fin, u_int flags, u_int p, struct in_addr src, 4010 struct in_addr mapdst) 4011 { 4012 ipf_main_softc_t *softc = fin->fin_main_soft; 4013 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 4014 u_short sport, dport; 4015 grehdr_t *gre; 4016 ipnat_t *ipn; 4017 u_int sflags; 4018 nat_t *nat; 4019 int nflags; 4020 u_32_t dst; 4021 void *ifp; 4022 u_int hv, rhv; 4023 4024 ifp = fin->fin_ifp; 4025 gre = NULL; 4026 dst = mapdst.s_addr; 4027 sflags = flags & NAT_TCPUDPICMP; 4028 4029 switch (p) 4030 { 4031 case IPPROTO_TCP : 4032 case IPPROTO_UDP : 4033 sport = htons(fin->fin_data[0]); 4034 dport = htons(fin->fin_data[1]); 4035 break; 4036 case IPPROTO_ICMP : 4037 if (flags & IPN_ICMPERR) { 4038 sport = fin->fin_data[1]; 4039 dport = 0; 4040 } else { 4041 dport = fin->fin_data[1]; 4042 sport = 0; 4043 } 4044 break; 4045 default : 4046 sport = 0; 4047 dport = 0; 4048 break; 4049 } 4050 4051 4052 if ((flags & SI_WILDP) != 0) 4053 goto find_in_wild_ports; 4054 4055 rhv = NAT_HASH_FN(dst, dport, 0xffffffff); 4056 rhv = NAT_HASH_FN(src.s_addr, rhv + sport, 0xffffffff); 4057 hv = rhv % softn->ipf_nat_table_sz; 4058 nat = softn->ipf_nat_table[1][hv]; 4059 /* TRACE dst, dport, src, sport, hv, nat */ 4060 4061 for (; nat; nat = nat->nat_hnext[1]) { 4062 if (nat->nat_ifps[0] != NULL) { 4063 if ((ifp != NULL) && (ifp != nat->nat_ifps[0])) 4064 continue; 4065 } 4066 4067 if (nat->nat_pr[0] != p) 4068 continue; 4069 4070 switch (nat->nat_dir) 4071 { 4072 case NAT_INBOUND : 4073 case NAT_DIVERTIN : 4074 if (nat->nat_v[0] != 4) 4075 continue; 4076 if (nat->nat_osrcaddr != src.s_addr || 4077 nat->nat_odstaddr != dst) 4078 continue; 4079 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 4080 if (nat->nat_osport != sport) 4081 continue; 4082 if (nat->nat_odport != dport) 4083 continue; 4084 4085 } else if (p == IPPROTO_ICMP) { 4086 if (nat->nat_osport != dport) { 4087 continue; 4088 } 4089 } 4090 break; 4091 case NAT_DIVERTOUT : 4092 if (nat->nat_dlocal) 4093 continue; 4094 case NAT_OUTBOUND : 4095 if (nat->nat_v[1] != 4) 4096 continue; 4097 if (nat->nat_dlocal) 4098 continue; 4099 if (nat->nat_dlocal) 4100 continue; 4101 if (nat->nat_ndstaddr != src.s_addr || 4102 nat->nat_nsrcaddr != dst) 4103 continue; 4104 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 4105 if (nat->nat_ndport != sport) 4106 continue; 4107 if (nat->nat_nsport != dport) 4108 continue; 4109 4110 } else if (p == IPPROTO_ICMP) { 4111 if (nat->nat_osport != dport) { 4112 continue; 4113 } 4114 } 4115 break; 4116 } 4117 4118 4119 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 4120 ipn = nat->nat_ptr; 4121 if ((ipn != NULL) && (nat->nat_aps != NULL)) 4122 if (ipf_proxy_match(fin, nat) != 0) 4123 continue; 4124 } 4125 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) { 4126 nat->nat_ifps[0] = ifp; 4127 nat->nat_mtu[0] = GETIFMTU_4(ifp); 4128 } 4129 return nat; 4130 } 4131 4132 /* 4133 * So if we didn't find it but there are wildcard members in the hash 4134 * table, go back and look for them. We do this search and update here 4135 * because it is modifying the NAT table and we want to do this only 4136 * for the first packet that matches. The exception, of course, is 4137 * for "dummy" (FI_IGNORE) lookups. 4138 */ 4139 find_in_wild_ports: 4140 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) { 4141 NBUMPSIDEX(0, ns_lookup_miss, ns_lookup_miss_0); 4142 return NULL; 4143 } 4144 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) { 4145 NBUMPSIDEX(0, ns_lookup_nowild, ns_lookup_nowild_0); 4146 return NULL; 4147 } 4148 4149 RWLOCK_EXIT(&softc->ipf_nat); 4150 4151 hv = NAT_HASH_FN(dst, 0, 0xffffffff); 4152 hv = NAT_HASH_FN(src.s_addr, hv, softn->ipf_nat_table_sz); 4153 WRITE_ENTER(&softc->ipf_nat); 4154 4155 nat = softn->ipf_nat_table[1][hv]; 4156 /* TRACE dst, src, hv, nat */ 4157 for (; nat; nat = nat->nat_hnext[1]) { 4158 if (nat->nat_ifps[0] != NULL) { 4159 if ((ifp != NULL) && (ifp != nat->nat_ifps[0])) 4160 continue; 4161 } 4162 4163 if (nat->nat_pr[0] != fin->fin_p) 4164 continue; 4165 4166 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)) 4167 { 4168 case NAT_INBOUND : 4169 if (nat->nat_v[0] != 4) 4170 continue; 4171 if (nat->nat_osrcaddr != src.s_addr || 4172 nat->nat_odstaddr != dst) 4173 continue; 4174 break; 4175 case NAT_OUTBOUND : 4176 if (nat->nat_v[1] != 4) 4177 continue; 4178 if (nat->nat_ndstaddr != src.s_addr || 4179 nat->nat_nsrcaddr != dst) 4180 continue; 4181 break; 4182 } 4183 4184 nflags = nat->nat_flags; 4185 if (!(nflags & (NAT_TCPUDP|SI_WILDP))) 4186 continue; 4187 4188 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nflags, 4189 NAT_INBOUND) == 1) { 4190 if ((fin->fin_flx & FI_IGNORE) != 0) 4191 break; 4192 if ((nflags & SI_CLONE) != 0) { 4193 nat = ipf_nat_clone(fin, nat); 4194 if (nat == NULL) 4195 break; 4196 } else { 4197 MUTEX_ENTER(&softn->ipf_nat_new); 4198 softn->ipf_nat_stats.ns_wilds--; 4199 MUTEX_EXIT(&softn->ipf_nat_new); 4200 } 4201 4202 if (nat->nat_dir == NAT_INBOUND) { 4203 if (nat->nat_osport == 0) { 4204 nat->nat_osport = sport; 4205 nat->nat_nsport = sport; 4206 } 4207 if (nat->nat_odport == 0) { 4208 nat->nat_odport = dport; 4209 nat->nat_ndport = dport; 4210 } 4211 } else if (nat->nat_dir == NAT_OUTBOUND) { 4212 if (nat->nat_osport == 0) { 4213 nat->nat_osport = dport; 4214 nat->nat_nsport = dport; 4215 } 4216 if (nat->nat_odport == 0) { 4217 nat->nat_odport = sport; 4218 nat->nat_ndport = sport; 4219 } 4220 } 4221 if ((nat->nat_ifps[0] == NULL) && (ifp != NULL)) { 4222 nat->nat_ifps[0] = ifp; 4223 nat->nat_mtu[0] = GETIFMTU_4(ifp); 4224 } 4225 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT); 4226 ipf_nat_tabmove(softn, nat); 4227 break; 4228 } 4229 } 4230 4231 MUTEX_DOWNGRADE(&softc->ipf_nat); 4232 4233 if (nat == NULL) { 4234 NBUMPSIDE(0, ns_lookup_miss); 4235 } 4236 return nat; 4237 } 4238 4239 4240 /* ------------------------------------------------------------------------ */ 4241 /* Function: ipf_nat_tabmove */ 4242 /* Returns: Nil */ 4243 /* Parameters: softn(I) - pointer to NAT context structure */ 4244 /* nat(I) - pointer to NAT structure */ 4245 /* Write Lock: ipf_nat */ 4246 /* */ 4247 /* This function is only called for TCP/UDP NAT table entries where the */ 4248 /* original was placed in the table without hashing on the ports and we now */ 4249 /* want to include hashing on port numbers. */ 4250 /* ------------------------------------------------------------------------ */ 4251 static void 4252 ipf_nat_tabmove(ipf_nat_softc_t *softn, nat_t *nat) 4253 { 4254 u_int hv0, hv1, rhv0, rhv1; 4255 natstat_t *nsp; 4256 nat_t **natp; 4257 4258 if (nat->nat_flags & SI_CLONE) 4259 return; 4260 4261 nsp = &softn->ipf_nat_stats; 4262 /* 4263 * Remove the NAT entry from the old location 4264 */ 4265 if (nat->nat_hnext[0]) 4266 nat->nat_hnext[0]->nat_phnext[0] = nat->nat_phnext[0]; 4267 *nat->nat_phnext[0] = nat->nat_hnext[0]; 4268 nsp->ns_side[0].ns_bucketlen[nat->nat_hv[0] % 4269 softn->ipf_nat_table_sz]--; 4270 4271 if (nat->nat_hnext[1]) 4272 nat->nat_hnext[1]->nat_phnext[1] = nat->nat_phnext[1]; 4273 *nat->nat_phnext[1] = nat->nat_hnext[1]; 4274 nsp->ns_side[1].ns_bucketlen[nat->nat_hv[1] % 4275 softn->ipf_nat_table_sz]--; 4276 4277 /* 4278 * Add into the NAT table in the new position 4279 */ 4280 rhv0 = NAT_HASH_FN(nat->nat_osrcaddr, nat->nat_osport, 0xffffffff); 4281 rhv0 = NAT_HASH_FN(nat->nat_odstaddr, rhv0 + nat->nat_odport, 4282 0xffffffff); 4283 rhv1 = NAT_HASH_FN(nat->nat_nsrcaddr, nat->nat_nsport, 0xffffffff); 4284 rhv1 = NAT_HASH_FN(nat->nat_ndstaddr, rhv1 + nat->nat_ndport, 4285 0xffffffff); 4286 4287 hv0 = rhv0 % softn->ipf_nat_table_sz; 4288 hv1 = rhv1 % softn->ipf_nat_table_sz; 4289 4290 if (nat->nat_dir == NAT_INBOUND || nat->nat_dir == NAT_DIVERTIN) { 4291 u_int swap; 4292 4293 swap = hv0; 4294 hv0 = hv1; 4295 hv1 = swap; 4296 } 4297 4298 /* TRACE nat_osrcaddr, nat_osport, nat_odstaddr, nat_odport, hv0 */ 4299 /* TRACE nat_nsrcaddr, nat_nsport, nat_ndstaddr, nat_ndport, hv1 */ 4300 4301 nat->nat_hv[0] = rhv0; 4302 natp = &softn->ipf_nat_table[0][hv0]; 4303 if (*natp) 4304 (*natp)->nat_phnext[0] = &nat->nat_hnext[0]; 4305 nat->nat_phnext[0] = natp; 4306 nat->nat_hnext[0] = *natp; 4307 *natp = nat; 4308 nsp->ns_side[0].ns_bucketlen[hv0]++; 4309 4310 nat->nat_hv[1] = rhv1; 4311 natp = &softn->ipf_nat_table[1][hv1]; 4312 if (*natp) 4313 (*natp)->nat_phnext[1] = &nat->nat_hnext[1]; 4314 nat->nat_phnext[1] = natp; 4315 nat->nat_hnext[1] = *natp; 4316 *natp = nat; 4317 nsp->ns_side[1].ns_bucketlen[hv1]++; 4318 } 4319 4320 4321 /* ------------------------------------------------------------------------ */ 4322 /* Function: ipf_nat_outlookup */ 4323 /* Returns: nat_t* - NULL == no match, */ 4324 /* else pointer to matching NAT entry */ 4325 /* Parameters: fin(I) - pointer to packet information */ 4326 /* flags(I) - NAT flags for this packet */ 4327 /* p(I) - protocol for this packet */ 4328 /* src(I) - source IP address */ 4329 /* dst(I) - destination IP address */ 4330 /* rw(I) - 1 == write lock on held, 0 == read lock. */ 4331 /* */ 4332 /* Lookup a nat entry based on the source 'real' ip address/port and */ 4333 /* destination address/port. We use this lookup when sending a packet out, */ 4334 /* we're looking for a table entry, based on the source address. */ 4335 /* */ 4336 /* NOTE: THE PACKET BEING CHECKED (IF FOUND) HAS A MAPPING ALREADY. */ 4337 /* */ 4338 /* NOTE: IT IS ASSUMED THAT IS ONLY HELD WITH A READ LOCK WHEN */ 4339 /* THIS FUNCTION IS CALLED WITH NAT_SEARCH SET IN nflags. */ 4340 /* */ 4341 /* flags -> relevant are IPN_UDP/IPN_TCP/IPN_ICMPQUERY that indicate if */ 4342 /* the packet is of said protocol */ 4343 /* ------------------------------------------------------------------------ */ 4344 nat_t * 4345 ipf_nat_outlookup(fr_info_t *fin, u_int flags, u_int p, struct in_addr src, 4346 struct in_addr dst) 4347 { 4348 ipf_main_softc_t *softc = fin->fin_main_soft; 4349 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 4350 u_short sport, dport; 4351 u_int sflags; 4352 ipnat_t *ipn; 4353 nat_t *nat; 4354 void *ifp; 4355 u_int hv; 4356 4357 ifp = fin->fin_ifp; 4358 sflags = flags & IPN_TCPUDPICMP; 4359 sport = 0; 4360 dport = 0; 4361 4362 switch (p) 4363 { 4364 case IPPROTO_TCP : 4365 case IPPROTO_UDP : 4366 sport = htons(fin->fin_data[0]); 4367 dport = htons(fin->fin_data[1]); 4368 break; 4369 case IPPROTO_ICMP : 4370 if (flags & IPN_ICMPERR) 4371 sport = fin->fin_data[1]; 4372 else 4373 dport = fin->fin_data[1]; 4374 break; 4375 default : 4376 break; 4377 } 4378 4379 if ((flags & SI_WILDP) != 0) 4380 goto find_out_wild_ports; 4381 4382 hv = NAT_HASH_FN(src.s_addr, sport, 0xffffffff); 4383 hv = NAT_HASH_FN(dst.s_addr, hv + dport, softn->ipf_nat_table_sz); 4384 nat = softn->ipf_nat_table[0][hv]; 4385 4386 /* TRACE src, sport, dst, dport, hv, nat */ 4387 4388 for (; nat; nat = nat->nat_hnext[0]) { 4389 if (nat->nat_ifps[1] != NULL) { 4390 if ((ifp != NULL) && (ifp != nat->nat_ifps[1])) 4391 continue; 4392 } 4393 4394 if (nat->nat_pr[1] != p) 4395 continue; 4396 4397 switch (nat->nat_dir) 4398 { 4399 case NAT_INBOUND : 4400 case NAT_DIVERTIN : 4401 if (nat->nat_v[1] != 4) 4402 continue; 4403 if (nat->nat_ndstaddr != src.s_addr || 4404 nat->nat_nsrcaddr != dst.s_addr) 4405 continue; 4406 4407 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 4408 if (nat->nat_ndport != sport) 4409 continue; 4410 if (nat->nat_nsport != dport) 4411 continue; 4412 4413 } else if (p == IPPROTO_ICMP) { 4414 if (nat->nat_osport != dport) { 4415 continue; 4416 } 4417 } 4418 break; 4419 case NAT_OUTBOUND : 4420 case NAT_DIVERTOUT : 4421 if (nat->nat_v[0] != 4) 4422 continue; 4423 if (nat->nat_osrcaddr != src.s_addr || 4424 nat->nat_odstaddr != dst.s_addr) 4425 continue; 4426 4427 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 4428 if (nat->nat_odport != dport) 4429 continue; 4430 if (nat->nat_osport != sport) 4431 continue; 4432 4433 } else if (p == IPPROTO_ICMP) { 4434 if (nat->nat_osport != dport) { 4435 continue; 4436 } 4437 } 4438 break; 4439 } 4440 4441 ipn = nat->nat_ptr; 4442 if ((ipn != NULL) && (nat->nat_aps != NULL)) 4443 if (ipf_proxy_match(fin, nat) != 0) 4444 continue; 4445 4446 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) { 4447 nat->nat_ifps[1] = ifp; 4448 nat->nat_mtu[1] = GETIFMTU_4(ifp); 4449 } 4450 return nat; 4451 } 4452 4453 /* 4454 * So if we didn't find it but there are wildcard members in the hash 4455 * table, go back and look for them. We do this search and update here 4456 * because it is modifying the NAT table and we want to do this only 4457 * for the first packet that matches. The exception, of course, is 4458 * for "dummy" (FI_IGNORE) lookups. 4459 */ 4460 find_out_wild_ports: 4461 if (!(flags & NAT_TCPUDP) || !(flags & NAT_SEARCH)) { 4462 NBUMPSIDEX(1, ns_lookup_miss, ns_lookup_miss_1); 4463 return NULL; 4464 } 4465 if (softn->ipf_nat_stats.ns_wilds == 0 || (fin->fin_flx & FI_NOWILD)) { 4466 NBUMPSIDEX(1, ns_lookup_nowild, ns_lookup_nowild_1); 4467 return NULL; 4468 } 4469 4470 RWLOCK_EXIT(&softc->ipf_nat); 4471 4472 hv = NAT_HASH_FN(src.s_addr, 0, 0xffffffff); 4473 hv = NAT_HASH_FN(dst.s_addr, hv, softn->ipf_nat_table_sz); 4474 4475 WRITE_ENTER(&softc->ipf_nat); 4476 4477 nat = softn->ipf_nat_table[0][hv]; 4478 for (; nat; nat = nat->nat_hnext[0]) { 4479 if (nat->nat_ifps[1] != NULL) { 4480 if ((ifp != NULL) && (ifp != nat->nat_ifps[1])) 4481 continue; 4482 } 4483 4484 if (nat->nat_pr[1] != fin->fin_p) 4485 continue; 4486 4487 switch (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND)) 4488 { 4489 case NAT_INBOUND : 4490 if (nat->nat_v[1] != 4) 4491 continue; 4492 if (nat->nat_ndstaddr != src.s_addr || 4493 nat->nat_nsrcaddr != dst.s_addr) 4494 continue; 4495 break; 4496 case NAT_OUTBOUND : 4497 if (nat->nat_v[0] != 4) 4498 continue; 4499 if (nat->nat_osrcaddr != src.s_addr || 4500 nat->nat_odstaddr != dst.s_addr) 4501 continue; 4502 break; 4503 } 4504 4505 if (!(nat->nat_flags & (NAT_TCPUDP|SI_WILDP))) 4506 continue; 4507 4508 if (ipf_nat_wildok(nat, (int)sport, (int)dport, nat->nat_flags, 4509 NAT_OUTBOUND) == 1) { 4510 if ((fin->fin_flx & FI_IGNORE) != 0) 4511 break; 4512 if ((nat->nat_flags & SI_CLONE) != 0) { 4513 nat = ipf_nat_clone(fin, nat); 4514 if (nat == NULL) 4515 break; 4516 } else { 4517 MUTEX_ENTER(&softn->ipf_nat_new); 4518 softn->ipf_nat_stats.ns_wilds--; 4519 MUTEX_EXIT(&softn->ipf_nat_new); 4520 } 4521 4522 if (nat->nat_dir == NAT_OUTBOUND) { 4523 if (nat->nat_osport == 0) { 4524 nat->nat_osport = sport; 4525 nat->nat_nsport = sport; 4526 } 4527 if (nat->nat_odport == 0) { 4528 nat->nat_odport = dport; 4529 nat->nat_ndport = dport; 4530 } 4531 } else if (nat->nat_dir == NAT_INBOUND) { 4532 if (nat->nat_osport == 0) { 4533 nat->nat_osport = dport; 4534 nat->nat_nsport = dport; 4535 } 4536 if (nat->nat_odport == 0) { 4537 nat->nat_odport = sport; 4538 nat->nat_ndport = sport; 4539 } 4540 } 4541 if ((nat->nat_ifps[1] == NULL) && (ifp != NULL)) { 4542 nat->nat_ifps[1] = ifp; 4543 nat->nat_mtu[1] = GETIFMTU_4(ifp); 4544 } 4545 nat->nat_flags &= ~(SI_W_DPORT|SI_W_SPORT); 4546 ipf_nat_tabmove(softn, nat); 4547 break; 4548 } 4549 } 4550 4551 MUTEX_DOWNGRADE(&softc->ipf_nat); 4552 4553 if (nat == NULL) { 4554 NBUMPSIDE(1, ns_lookup_miss); 4555 } 4556 return nat; 4557 } 4558 4559 4560 /* ------------------------------------------------------------------------ */ 4561 /* Function: ipf_nat_lookupredir */ 4562 /* Returns: nat_t* - NULL == no match, */ 4563 /* else pointer to matching NAT entry */ 4564 /* Parameters: np(I) - pointer to description of packet to find NAT table */ 4565 /* entry for. */ 4566 /* */ 4567 /* Lookup the NAT tables to search for a matching redirect */ 4568 /* The contents of natlookup_t should imitate those found in a packet that */ 4569 /* would be translated - ie a packet coming in for RDR or going out for MAP.*/ 4570 /* We can do the lookup in one of two ways, imitating an inbound or */ 4571 /* outbound packet. By default we assume outbound, unless IPN_IN is set. */ 4572 /* For IN, the fields are set as follows: */ 4573 /* nl_real* = source information */ 4574 /* nl_out* = destination information (translated) */ 4575 /* For an out packet, the fields are set like this: */ 4576 /* nl_in* = source information (untranslated) */ 4577 /* nl_out* = destination information (translated) */ 4578 /* ------------------------------------------------------------------------ */ 4579 nat_t * 4580 ipf_nat_lookupredir(natlookup_t *np) 4581 { 4582 fr_info_t fi; 4583 nat_t *nat; 4584 4585 bzero((char *)&fi, sizeof(fi)); 4586 if (np->nl_flags & IPN_IN) { 4587 fi.fin_data[0] = ntohs(np->nl_realport); 4588 fi.fin_data[1] = ntohs(np->nl_outport); 4589 } else { 4590 fi.fin_data[0] = ntohs(np->nl_inport); 4591 fi.fin_data[1] = ntohs(np->nl_outport); 4592 } 4593 if (np->nl_flags & IPN_TCP) 4594 fi.fin_p = IPPROTO_TCP; 4595 else if (np->nl_flags & IPN_UDP) 4596 fi.fin_p = IPPROTO_UDP; 4597 else if (np->nl_flags & (IPN_ICMPERR|IPN_ICMPQUERY)) 4598 fi.fin_p = IPPROTO_ICMP; 4599 4600 /* 4601 * We can do two sorts of lookups: 4602 * - IPN_IN: we have the `real' and `out' address, look for `in'. 4603 * - default: we have the `in' and `out' address, look for `real'. 4604 */ 4605 if (np->nl_flags & IPN_IN) { 4606 if ((nat = ipf_nat_inlookup(&fi, np->nl_flags, fi.fin_p, 4607 np->nl_realip, np->nl_outip))) { 4608 np->nl_inip = nat->nat_odstip; 4609 np->nl_inport = nat->nat_odport; 4610 } 4611 } else { 4612 /* 4613 * If nl_inip is non null, this is a lookup based on the real 4614 * ip address. Else, we use the fake. 4615 */ 4616 if ((nat = ipf_nat_outlookup(&fi, np->nl_flags, fi.fin_p, 4617 np->nl_inip, np->nl_outip))) { 4618 4619 if ((np->nl_flags & IPN_FINDFORWARD) != 0) { 4620 fr_info_t fin; 4621 bzero((char *)&fin, sizeof(fin)); 4622 fin.fin_p = nat->nat_pr[0]; 4623 fin.fin_data[0] = ntohs(nat->nat_ndport); 4624 fin.fin_data[1] = ntohs(nat->nat_nsport); 4625 if (ipf_nat_inlookup(&fin, np->nl_flags, 4626 fin.fin_p, nat->nat_ndstip, 4627 nat->nat_nsrcip) != NULL) { 4628 np->nl_flags &= ~IPN_FINDFORWARD; 4629 } 4630 } 4631 4632 np->nl_realip = nat->nat_ndstip; 4633 np->nl_realport = nat->nat_ndport; 4634 } 4635 } 4636 4637 return nat; 4638 } 4639 4640 4641 /* ------------------------------------------------------------------------ */ 4642 /* Function: ipf_nat_match */ 4643 /* Returns: int - 0 == no match, 1 == match */ 4644 /* Parameters: fin(I) - pointer to packet information */ 4645 /* np(I) - pointer to NAT rule */ 4646 /* */ 4647 /* Pull the matching of a packet against a NAT rule out of that complex */ 4648 /* loop inside ipf_nat_checkin() and lay it out properly in its own function. */ 4649 /* ------------------------------------------------------------------------ */ 4650 static int 4651 ipf_nat_match(fr_info_t *fin, ipnat_t *np) 4652 { 4653 ipf_main_softc_t *softc = fin->fin_main_soft; 4654 frtuc_t *ft; 4655 int match; 4656 4657 match = 0; 4658 switch (np->in_osrcatype) 4659 { 4660 case FRI_NORMAL : 4661 match = ((fin->fin_saddr & np->in_osrcmsk) != np->in_osrcaddr); 4662 break; 4663 case FRI_LOOKUP : 4664 match = (*np->in_osrcfunc)(softc, np->in_osrcptr, 4665 4, &fin->fin_saddr, fin->fin_plen); 4666 break; 4667 } 4668 match ^= ((np->in_flags & IPN_NOTSRC) != 0); 4669 if (match) 4670 return 0; 4671 4672 match = 0; 4673 switch (np->in_odstatype) 4674 { 4675 case FRI_NORMAL : 4676 match = ((fin->fin_daddr & np->in_odstmsk) != np->in_odstaddr); 4677 break; 4678 case FRI_LOOKUP : 4679 match = (*np->in_odstfunc)(softc, np->in_odstptr, 4680 4, &fin->fin_daddr, fin->fin_plen); 4681 break; 4682 } 4683 4684 match ^= ((np->in_flags & IPN_NOTDST) != 0); 4685 if (match) 4686 return 0; 4687 4688 ft = &np->in_tuc; 4689 if (!(fin->fin_flx & FI_TCPUDP) || 4690 (fin->fin_flx & (FI_SHORT|FI_FRAGBODY))) { 4691 if (ft->ftu_scmp || ft->ftu_dcmp) 4692 return 0; 4693 return 1; 4694 } 4695 4696 return ipf_tcpudpchk(&fin->fin_fi, ft); 4697 } 4698 4699 4700 /* ------------------------------------------------------------------------ */ 4701 /* Function: ipf_nat_update */ 4702 /* Returns: Nil */ 4703 /* Parameters: fin(I) - pointer to packet information */ 4704 /* nat(I) - pointer to NAT structure */ 4705 /* */ 4706 /* Updates the lifetime of a NAT table entry for non-TCP packets. Must be */ 4707 /* called with fin_rev updated - i.e. after calling ipf_nat_proto(). */ 4708 /* */ 4709 /* This *MUST* be called after ipf_nat_proto() as it expects fin_rev to */ 4710 /* already be set. */ 4711 /* ------------------------------------------------------------------------ */ 4712 void 4713 ipf_nat_update(fr_info_t *fin, nat_t *nat) 4714 { 4715 ipf_main_softc_t *softc = fin->fin_main_soft; 4716 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 4717 ipftq_t *ifq, *ifq2; 4718 ipftqent_t *tqe; 4719 ipnat_t *np = nat->nat_ptr; 4720 4721 tqe = &nat->nat_tqe; 4722 ifq = tqe->tqe_ifq; 4723 4724 /* 4725 * We allow over-riding of NAT timeouts from NAT rules, even for 4726 * TCP, however, if it is TCP and there is no rule timeout set, 4727 * then do not update the timeout here. 4728 */ 4729 if (np != NULL) { 4730 np->in_bytes[fin->fin_rev] += fin->fin_plen; 4731 ifq2 = np->in_tqehead[fin->fin_rev]; 4732 } else { 4733 ifq2 = NULL; 4734 } 4735 4736 if (nat->nat_pr[0] == IPPROTO_TCP && ifq2 == NULL) { 4737 (void) ipf_tcp_age(&nat->nat_tqe, fin, softn->ipf_nat_tcptq, 4738 0, 2); 4739 } else { 4740 if (ifq2 == NULL) { 4741 if (nat->nat_pr[0] == IPPROTO_UDP) 4742 ifq2 = fin->fin_rev ? &softn->ipf_nat_udpacktq : 4743 &softn->ipf_nat_udptq; 4744 else if (nat->nat_pr[0] == IPPROTO_ICMP || 4745 nat->nat_pr[0] == IPPROTO_ICMPV6) 4746 ifq2 = fin->fin_rev ? &softn->ipf_nat_icmpacktq: 4747 &softn->ipf_nat_icmptq; 4748 else 4749 ifq2 = &softn->ipf_nat_iptq; 4750 } 4751 4752 ipf_movequeue(softc->ipf_ticks, tqe, ifq, ifq2); 4753 } 4754 } 4755 4756 4757 /* ------------------------------------------------------------------------ */ 4758 /* Function: ipf_nat_checkout */ 4759 /* Returns: int - -1 == packet failed NAT checks so block it, */ 4760 /* 0 == no packet translation occurred, */ 4761 /* 1 == packet was successfully translated. */ 4762 /* Parameters: fin(I) - pointer to packet information */ 4763 /* passp(I) - pointer to filtering result flags */ 4764 /* */ 4765 /* Check to see if an outcoming packet should be changed. ICMP packets are */ 4766 /* first checked to see if they match an existing entry (if an error), */ 4767 /* otherwise a search of the current NAT table is made. If neither results */ 4768 /* in a match then a search for a matching NAT rule is made. Create a new */ 4769 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */ 4770 /* packet header(s) as required. */ 4771 /* ------------------------------------------------------------------------ */ 4772 int 4773 ipf_nat_checkout(fr_info_t *fin, u_32_t *passp) 4774 { 4775 ipnat_t *np = NULL, *npnext; 4776 struct ifnet *ifp, *sifp; 4777 ipf_main_softc_t *softc; 4778 ipf_nat_softc_t *softn; 4779 icmphdr_t *icmp = NULL; 4780 tcphdr_t *tcp = NULL; 4781 int rval, natfailed; 4782 u_int nflags = 0; 4783 u_32_t ipa, iph; 4784 int natadd = 1; 4785 frentry_t *fr; 4786 nat_t *nat; 4787 4788 if (fin->fin_v == 6) { 4789 #ifdef USE_INET6 4790 return ipf_nat6_checkout(fin, passp); 4791 #else 4792 return 0; 4793 #endif 4794 } 4795 4796 softc = fin->fin_main_soft; 4797 softn = softc->ipf_nat_soft; 4798 4799 if (softn->ipf_nat_lock != 0) 4800 return 0; 4801 if (softn->ipf_nat_stats.ns_rules == 0 && 4802 softn->ipf_nat_instances == NULL) 4803 return 0; 4804 4805 natfailed = 0; 4806 fr = fin->fin_fr; 4807 sifp = fin->fin_ifp; 4808 if (fr != NULL) { 4809 ifp = fr->fr_tifs[fin->fin_rev].fd_ptr; 4810 if ((ifp != NULL) && (ifp != (void *)-1)) 4811 fin->fin_ifp = ifp; 4812 } 4813 ifp = fin->fin_ifp; 4814 4815 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) { 4816 switch (fin->fin_p) 4817 { 4818 case IPPROTO_TCP : 4819 nflags = IPN_TCP; 4820 break; 4821 case IPPROTO_UDP : 4822 nflags = IPN_UDP; 4823 break; 4824 case IPPROTO_ICMP : 4825 icmp = fin->fin_dp; 4826 4827 /* 4828 * This is an incoming packet, so the destination is 4829 * the icmp_id and the source port equals 0 4830 */ 4831 if ((fin->fin_flx & FI_ICMPQUERY) != 0) 4832 nflags = IPN_ICMPQUERY; 4833 break; 4834 default : 4835 break; 4836 } 4837 4838 if ((nflags & IPN_TCPUDP)) 4839 tcp = fin->fin_dp; 4840 } 4841 4842 ipa = fin->fin_saddr; 4843 4844 READ_ENTER(&softc->ipf_nat); 4845 4846 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) && 4847 (nat = ipf_nat_icmperror(fin, &nflags, NAT_OUTBOUND))) 4848 /*EMPTY*/; 4849 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin))) 4850 natadd = 0; 4851 else if ((nat = ipf_nat_outlookup(fin, nflags|NAT_SEARCH, 4852 (u_int)fin->fin_p, fin->fin_src, 4853 fin->fin_dst))) { 4854 nflags = nat->nat_flags; 4855 } else if (fin->fin_off == 0) { 4856 u_32_t hv, msk, nmsk = 0; 4857 4858 /* 4859 * If there is no current entry in the nat table for this IP#, 4860 * create one for it (if there is a matching rule). 4861 */ 4862 maskloop: 4863 msk = softn->ipf_nat_map_active_masks[nmsk]; 4864 iph = ipa & msk; 4865 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_maprules_sz); 4866 retry_roundrobin: 4867 for (np = softn->ipf_nat_map_rules[hv]; np; np = npnext) { 4868 npnext = np->in_mnext; 4869 if ((np->in_ifps[1] && (np->in_ifps[1] != ifp))) 4870 continue; 4871 if (np->in_v[0] != 4) 4872 continue; 4873 if (np->in_pr[1] && (np->in_pr[1] != fin->fin_p)) 4874 continue; 4875 if ((np->in_flags & IPN_RF) && 4876 !(np->in_flags & nflags)) 4877 continue; 4878 if (np->in_flags & IPN_FILTER) { 4879 switch (ipf_nat_match(fin, np)) 4880 { 4881 case 0 : 4882 continue; 4883 case -1 : 4884 rval = -1; 4885 goto outmatchfail; 4886 case 1 : 4887 default : 4888 break; 4889 } 4890 } else if ((ipa & np->in_osrcmsk) != np->in_osrcaddr) 4891 continue; 4892 4893 if ((fr != NULL) && 4894 !ipf_matchtag(&np->in_tag, &fr->fr_nattag)) 4895 continue; 4896 4897 if (np->in_plabel != -1) { 4898 if (((np->in_flags & IPN_FILTER) == 0) && 4899 (np->in_odport != fin->fin_data[1])) 4900 continue; 4901 if (ipf_proxy_ok(fin, tcp, np) == 0) 4902 continue; 4903 } 4904 4905 if (np->in_flags & IPN_NO) { 4906 np->in_hits++; 4907 break; 4908 } 4909 MUTEX_ENTER(&softn->ipf_nat_new); 4910 /* 4911 * If we've matched a round-robin rule but it has 4912 * moved in the list since we got it, start over as 4913 * this is now no longer correct. 4914 */ 4915 if (npnext != np->in_mnext) { 4916 if ((np->in_flags & IPN_ROUNDR) != 0) { 4917 MUTEX_EXIT(&softn->ipf_nat_new); 4918 goto retry_roundrobin; 4919 } 4920 npnext = np->in_mnext; 4921 } 4922 4923 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_OUTBOUND); 4924 MUTEX_EXIT(&softn->ipf_nat_new); 4925 if (nat != NULL) { 4926 natfailed = 0; 4927 break; 4928 } 4929 natfailed = -1; 4930 } 4931 if ((np == NULL) && (nmsk < softn->ipf_nat_map_max)) { 4932 nmsk++; 4933 goto maskloop; 4934 } 4935 } 4936 4937 if (nat != NULL) { 4938 rval = ipf_nat_out(fin, nat, natadd, nflags); 4939 if (rval == 1) { 4940 MUTEX_ENTER(&nat->nat_lock); 4941 ipf_nat_update(fin, nat); 4942 nat->nat_bytes[1] += fin->fin_plen; 4943 nat->nat_pkts[1]++; 4944 fin->fin_pktnum = nat->nat_pkts[1]; 4945 MUTEX_EXIT(&nat->nat_lock); 4946 } 4947 } else 4948 rval = natfailed; 4949 outmatchfail: 4950 RWLOCK_EXIT(&softc->ipf_nat); 4951 4952 switch (rval) 4953 { 4954 case -1 : 4955 if (passp != NULL) { 4956 DT1(frb_natv4out, fr_info_t *, fin); 4957 NBUMPSIDED(1, ns_drop); 4958 *passp = FR_BLOCK; 4959 fin->fin_reason = FRB_NATV4; 4960 } 4961 fin->fin_flx |= FI_BADNAT; 4962 NBUMPSIDED(1, ns_badnat); 4963 break; 4964 case 0 : 4965 NBUMPSIDE(1, ns_ignored); 4966 break; 4967 case 1 : 4968 NBUMPSIDE(1, ns_translated); 4969 break; 4970 } 4971 fin->fin_ifp = sifp; 4972 return rval; 4973 } 4974 4975 /* ------------------------------------------------------------------------ */ 4976 /* Function: ipf_nat_out */ 4977 /* Returns: int - -1 == packet failed NAT checks so block it, */ 4978 /* 1 == packet was successfully translated. */ 4979 /* Parameters: fin(I) - pointer to packet information */ 4980 /* nat(I) - pointer to NAT structure */ 4981 /* natadd(I) - flag indicating if it is safe to add frag cache */ 4982 /* nflags(I) - NAT flags set for this packet */ 4983 /* */ 4984 /* Translate a packet coming "out" on an interface. */ 4985 /* ------------------------------------------------------------------------ */ 4986 int 4987 ipf_nat_out(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags) 4988 { 4989 ipf_main_softc_t *softc = fin->fin_main_soft; 4990 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 4991 icmphdr_t *icmp; 4992 tcphdr_t *tcp; 4993 ipnat_t *np; 4994 int skip; 4995 int i; 4996 4997 tcp = NULL; 4998 icmp = NULL; 4999 np = nat->nat_ptr; 5000 5001 if ((natadd != 0) && (fin->fin_flx & FI_FRAG) && (np != NULL)) 5002 (void) ipf_frag_natnew(softc, fin, 0, nat); 5003 5004 /* 5005 * Fix up checksums, not by recalculating them, but 5006 * simply computing adjustments. 5007 * This is only done for STREAMS based IP implementations where the 5008 * checksum has already been calculated by IP. In all other cases, 5009 * IPFilter is called before the checksum needs calculating so there 5010 * is no call to modify whatever is in the header now. 5011 */ 5012 if (nflags == IPN_ICMPERR) { 5013 u_32_t s1, s2, sumd, msumd; 5014 5015 s1 = LONG_SUM(ntohl(fin->fin_saddr)); 5016 if (nat->nat_dir == NAT_OUTBOUND) { 5017 s2 = LONG_SUM(ntohl(nat->nat_nsrcaddr)); 5018 } else { 5019 s2 = LONG_SUM(ntohl(nat->nat_odstaddr)); 5020 } 5021 CALC_SUMD(s1, s2, sumd); 5022 msumd = sumd; 5023 5024 s1 = LONG_SUM(ntohl(fin->fin_daddr)); 5025 if (nat->nat_dir == NAT_OUTBOUND) { 5026 s2 = LONG_SUM(ntohl(nat->nat_ndstaddr)); 5027 } else { 5028 s2 = LONG_SUM(ntohl(nat->nat_osrcaddr)); 5029 } 5030 CALC_SUMD(s1, s2, sumd); 5031 msumd += sumd; 5032 5033 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, msumd, 0); 5034 } 5035 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 5036 defined(linux) || defined(BRIDGE_IPF) 5037 else { 5038 /* 5039 * Strictly speaking, this isn't necessary on BSD 5040 * kernels because they do checksum calculation after 5041 * this code has run BUT if ipfilter is being used 5042 * to do NAT as a bridge, that code doesn't exist. 5043 */ 5044 switch (nat->nat_dir) 5045 { 5046 case NAT_OUTBOUND : 5047 ipf_fix_outcksum(fin->fin_cksum & FI_CK_L4PART, 5048 &fin->fin_ip->ip_sum, 5049 nat->nat_ipsumd, 0); 5050 break; 5051 5052 case NAT_INBOUND : 5053 ipf_fix_incksum(fin->fin_cksum & FI_CK_L4PART, 5054 &fin->fin_ip->ip_sum, 5055 nat->nat_ipsumd, 0); 5056 break; 5057 5058 default : 5059 break; 5060 } 5061 } 5062 #endif 5063 5064 /* 5065 * Address assignment is after the checksum modification because 5066 * we are using the address in the packet for determining the 5067 * correct checksum offset (the ICMP error could be coming from 5068 * anyone...) 5069 */ 5070 switch (nat->nat_dir) 5071 { 5072 case NAT_OUTBOUND : 5073 fin->fin_ip->ip_src = nat->nat_nsrcip; 5074 fin->fin_saddr = nat->nat_nsrcaddr; 5075 fin->fin_ip->ip_dst = nat->nat_ndstip; 5076 fin->fin_daddr = nat->nat_ndstaddr; 5077 break; 5078 5079 case NAT_INBOUND : 5080 fin->fin_ip->ip_src = nat->nat_odstip; 5081 fin->fin_saddr = nat->nat_ndstaddr; 5082 fin->fin_ip->ip_dst = nat->nat_osrcip; 5083 fin->fin_daddr = nat->nat_nsrcaddr; 5084 break; 5085 5086 case NAT_DIVERTIN : 5087 { 5088 mb_t *m; 5089 5090 skip = ipf_nat_decap(fin, nat); 5091 if (skip <= 0) { 5092 NBUMPSIDED(1, ns_decap_fail); 5093 return -1; 5094 } 5095 5096 m = fin->fin_m; 5097 5098 #if defined(MENTAT) && defined(_KERNEL) 5099 m->b_rptr += skip; 5100 #else 5101 m->m_data += skip; 5102 m->m_len -= skip; 5103 5104 # ifdef M_PKTHDR 5105 if (m->m_flags & M_PKTHDR) 5106 m->m_pkthdr.len -= skip; 5107 # endif 5108 #endif 5109 5110 MUTEX_ENTER(&nat->nat_lock); 5111 ipf_nat_update(fin, nat); 5112 MUTEX_EXIT(&nat->nat_lock); 5113 fin->fin_flx |= FI_NATED; 5114 if (np != NULL && np->in_tag.ipt_num[0] != 0) 5115 fin->fin_nattag = &np->in_tag; 5116 return 1; 5117 /* NOTREACHED */ 5118 } 5119 5120 case NAT_DIVERTOUT : 5121 { 5122 u_32_t s1, s2, sumd; 5123 udphdr_t *uh; 5124 ip_t *ip; 5125 mb_t *m; 5126 5127 m = M_DUP(np->in_divmp); 5128 if (m == NULL) { 5129 NBUMPSIDED(1, ns_divert_dup); 5130 return -1; 5131 } 5132 5133 ip = MTOD(m, ip_t *); 5134 ip->ip_id = htons(ipf_nextipid(fin)); 5135 s2 = ntohs(ip->ip_id); 5136 5137 s1 = ip->ip_len; 5138 ip->ip_len = ntohs(ip->ip_len); 5139 ip->ip_len += fin->fin_plen; 5140 ip->ip_len = htons(ip->ip_len); 5141 s2 += ntohs(ip->ip_len); 5142 CALC_SUMD(s1, s2, sumd); 5143 5144 uh = (udphdr_t *)(ip + 1); 5145 uh->uh_ulen += fin->fin_plen; 5146 uh->uh_ulen = htons(uh->uh_ulen); 5147 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 5148 defined(linux) || defined(BRIDGE_IPF) 5149 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0); 5150 #endif 5151 5152 PREP_MB_T(fin, m); 5153 5154 fin->fin_src = ip->ip_src; 5155 fin->fin_dst = ip->ip_dst; 5156 fin->fin_ip = ip; 5157 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */ 5158 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + IPv4 hdr */ 5159 5160 nflags &= ~IPN_TCPUDPICMP; 5161 5162 break; 5163 } 5164 5165 default : 5166 break; 5167 } 5168 5169 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) { 5170 u_short *csump; 5171 5172 if ((nat->nat_nsport != 0) && (nflags & IPN_TCPUDP)) { 5173 tcp = fin->fin_dp; 5174 5175 switch (nat->nat_dir) 5176 { 5177 case NAT_OUTBOUND : 5178 tcp->th_sport = nat->nat_nsport; 5179 fin->fin_data[0] = ntohs(nat->nat_nsport); 5180 tcp->th_dport = nat->nat_ndport; 5181 fin->fin_data[1] = ntohs(nat->nat_ndport); 5182 break; 5183 5184 case NAT_INBOUND : 5185 tcp->th_sport = nat->nat_odport; 5186 fin->fin_data[0] = ntohs(nat->nat_odport); 5187 tcp->th_dport = nat->nat_osport; 5188 fin->fin_data[1] = ntohs(nat->nat_osport); 5189 break; 5190 } 5191 } 5192 5193 if ((nat->nat_nsport != 0) && (nflags & IPN_ICMPQUERY)) { 5194 icmp = fin->fin_dp; 5195 icmp->icmp_id = nat->nat_nicmpid; 5196 } 5197 5198 csump = ipf_nat_proto(fin, nat, nflags); 5199 5200 /* 5201 * The above comments do not hold for layer 4 (or higher) 5202 * checksums... 5203 */ 5204 if (csump != NULL) { 5205 if (nat->nat_dir == NAT_OUTBOUND) 5206 ipf_fix_outcksum(fin->fin_cksum, csump, 5207 nat->nat_sumd[0], 5208 nat->nat_sumd[1] + 5209 fin->fin_dlen); 5210 else 5211 ipf_fix_incksum(fin->fin_cksum, csump, 5212 nat->nat_sumd[0], 5213 nat->nat_sumd[1] + 5214 fin->fin_dlen); 5215 } 5216 } 5217 5218 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync); 5219 /* ------------------------------------------------------------- */ 5220 /* A few quick notes: */ 5221 /* Following are test conditions prior to calling the */ 5222 /* ipf_proxy_check routine. */ 5223 /* */ 5224 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */ 5225 /* with a redirect rule, we attempt to match the packet's */ 5226 /* source port against in_dport, otherwise we'd compare the */ 5227 /* packet's destination. */ 5228 /* ------------------------------------------------------------- */ 5229 if ((np != NULL) && (np->in_apr != NULL)) { 5230 i = ipf_proxy_check(fin, nat); 5231 if (i == 0) { 5232 i = 1; 5233 } else if (i == -1) { 5234 NBUMPSIDED(1, ns_ipf_proxy_fail); 5235 } 5236 } else { 5237 i = 1; 5238 } 5239 fin->fin_flx |= FI_NATED; 5240 return i; 5241 } 5242 5243 5244 /* ------------------------------------------------------------------------ */ 5245 /* Function: ipf_nat_checkin */ 5246 /* Returns: int - -1 == packet failed NAT checks so block it, */ 5247 /* 0 == no packet translation occurred, */ 5248 /* 1 == packet was successfully translated. */ 5249 /* Parameters: fin(I) - pointer to packet information */ 5250 /* passp(I) - pointer to filtering result flags */ 5251 /* */ 5252 /* Check to see if an incoming packet should be changed. ICMP packets are */ 5253 /* first checked to see if they match an existing entry (if an error), */ 5254 /* otherwise a search of the current NAT table is made. If neither results */ 5255 /* in a match then a search for a matching NAT rule is made. Create a new */ 5256 /* NAT entry if a we matched a NAT rule. Lastly, actually change the */ 5257 /* packet header(s) as required. */ 5258 /* ------------------------------------------------------------------------ */ 5259 int 5260 ipf_nat_checkin(fr_info_t *fin, u_32_t *passp) 5261 { 5262 ipf_main_softc_t *softc; 5263 ipf_nat_softc_t *softn; 5264 u_int nflags, natadd; 5265 ipnat_t *np, *npnext; 5266 int rval, natfailed; 5267 struct ifnet *ifp; 5268 struct in_addr in; 5269 icmphdr_t *icmp; 5270 tcphdr_t *tcp; 5271 u_short dport; 5272 nat_t *nat; 5273 u_32_t iph; 5274 5275 softc = fin->fin_main_soft; 5276 softn = softc->ipf_nat_soft; 5277 5278 if (softn->ipf_nat_lock != 0) 5279 return 0; 5280 if (softn->ipf_nat_stats.ns_rules == 0 && 5281 softn->ipf_nat_instances == NULL) 5282 return 0; 5283 5284 tcp = NULL; 5285 icmp = NULL; 5286 dport = 0; 5287 natadd = 1; 5288 nflags = 0; 5289 natfailed = 0; 5290 ifp = fin->fin_ifp; 5291 5292 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) { 5293 switch (fin->fin_p) 5294 { 5295 case IPPROTO_TCP : 5296 nflags = IPN_TCP; 5297 break; 5298 case IPPROTO_UDP : 5299 nflags = IPN_UDP; 5300 break; 5301 case IPPROTO_ICMP : 5302 icmp = fin->fin_dp; 5303 5304 /* 5305 * This is an incoming packet, so the destination is 5306 * the icmp_id and the source port equals 0 5307 */ 5308 if ((fin->fin_flx & FI_ICMPQUERY) != 0) { 5309 nflags = IPN_ICMPQUERY; 5310 dport = icmp->icmp_id; 5311 } break; 5312 default : 5313 break; 5314 } 5315 5316 if ((nflags & IPN_TCPUDP)) { 5317 tcp = fin->fin_dp; 5318 dport = fin->fin_data[1]; 5319 } 5320 } 5321 5322 in = fin->fin_dst; 5323 5324 READ_ENTER(&softc->ipf_nat); 5325 5326 if ((fin->fin_p == IPPROTO_ICMP) && !(nflags & IPN_ICMPQUERY) && 5327 (nat = ipf_nat_icmperror(fin, &nflags, NAT_INBOUND))) 5328 /*EMPTY*/; 5329 else if ((fin->fin_flx & FI_FRAG) && (nat = ipf_frag_natknown(fin))) 5330 natadd = 0; 5331 else if ((nat = ipf_nat_inlookup(fin, nflags|NAT_SEARCH, 5332 (u_int)fin->fin_p, 5333 fin->fin_src, in))) { 5334 nflags = nat->nat_flags; 5335 } else if (fin->fin_off == 0) { 5336 u_32_t hv, msk, rmsk = 0; 5337 5338 /* 5339 * If there is no current entry in the nat table for this IP#, 5340 * create one for it (if there is a matching rule). 5341 */ 5342 maskloop: 5343 msk = softn->ipf_nat_rdr_active_masks[rmsk]; 5344 iph = in.s_addr & msk; 5345 hv = NAT_HASH_FN(iph, 0, softn->ipf_nat_rdrrules_sz); 5346 retry_roundrobin: 5347 /* TRACE (iph,msk,rmsk,hv,softn->ipf_nat_rdrrules_sz) */ 5348 for (np = softn->ipf_nat_rdr_rules[hv]; np; np = npnext) { 5349 npnext = np->in_rnext; 5350 if (np->in_ifps[0] && (np->in_ifps[0] != ifp)) 5351 continue; 5352 if (np->in_v[0] != 4) 5353 continue; 5354 if (np->in_pr[0] && (np->in_pr[0] != fin->fin_p)) 5355 continue; 5356 if ((np->in_flags & IPN_RF) && !(np->in_flags & nflags)) 5357 continue; 5358 if (np->in_flags & IPN_FILTER) { 5359 switch (ipf_nat_match(fin, np)) 5360 { 5361 case 0 : 5362 continue; 5363 case -1 : 5364 rval = -1; 5365 goto inmatchfail; 5366 case 1 : 5367 default : 5368 break; 5369 } 5370 } else { 5371 if ((in.s_addr & np->in_odstmsk) != 5372 np->in_odstaddr) 5373 continue; 5374 if (np->in_odport && 5375 ((np->in_dtop < dport) || 5376 (dport < np->in_odport))) 5377 continue; 5378 } 5379 5380 if (np->in_plabel != -1) { 5381 if (!ipf_proxy_ok(fin, tcp, np)) { 5382 continue; 5383 } 5384 } 5385 5386 if (np->in_flags & IPN_NO) { 5387 np->in_hits++; 5388 break; 5389 } 5390 5391 MUTEX_ENTER(&softn->ipf_nat_new); 5392 /* 5393 * If we've matched a round-robin rule but it has 5394 * moved in the list since we got it, start over as 5395 * this is now no longer correct. 5396 */ 5397 if (npnext != np->in_rnext) { 5398 if ((np->in_flags & IPN_ROUNDR) != 0) { 5399 MUTEX_EXIT(&softn->ipf_nat_new); 5400 goto retry_roundrobin; 5401 } 5402 npnext = np->in_rnext; 5403 } 5404 5405 nat = ipf_nat_add(fin, np, NULL, nflags, NAT_INBOUND); 5406 MUTEX_EXIT(&softn->ipf_nat_new); 5407 if (nat != NULL) { 5408 natfailed = 0; 5409 break; 5410 } 5411 natfailed = -1; 5412 } 5413 if ((np == NULL) && (rmsk < softn->ipf_nat_rdr_max)) { 5414 rmsk++; 5415 goto maskloop; 5416 } 5417 } 5418 5419 if (nat != NULL) { 5420 rval = ipf_nat_in(fin, nat, natadd, nflags); 5421 if (rval == 1) { 5422 MUTEX_ENTER(&nat->nat_lock); 5423 ipf_nat_update(fin, nat); 5424 nat->nat_bytes[0] += fin->fin_plen; 5425 nat->nat_pkts[0]++; 5426 fin->fin_pktnum = nat->nat_pkts[0]; 5427 MUTEX_EXIT(&nat->nat_lock); 5428 } 5429 } else 5430 rval = natfailed; 5431 inmatchfail: 5432 RWLOCK_EXIT(&softc->ipf_nat); 5433 5434 switch (rval) 5435 { 5436 case -1 : 5437 if (passp != NULL) { 5438 DT1(frb_natv4in, fr_info_t *, fin); 5439 NBUMPSIDED(0, ns_drop); 5440 *passp = FR_BLOCK; 5441 fin->fin_reason = FRB_NATV4; 5442 } 5443 fin->fin_flx |= FI_BADNAT; 5444 NBUMPSIDED(0, ns_badnat); 5445 break; 5446 case 0 : 5447 NBUMPSIDE(0, ns_ignored); 5448 break; 5449 case 1 : 5450 NBUMPSIDE(0, ns_translated); 5451 break; 5452 } 5453 return rval; 5454 } 5455 5456 5457 /* ------------------------------------------------------------------------ */ 5458 /* Function: ipf_nat_in */ 5459 /* Returns: int - -1 == packet failed NAT checks so block it, */ 5460 /* 1 == packet was successfully translated. */ 5461 /* Parameters: fin(I) - pointer to packet information */ 5462 /* nat(I) - pointer to NAT structure */ 5463 /* natadd(I) - flag indicating if it is safe to add frag cache */ 5464 /* nflags(I) - NAT flags set for this packet */ 5465 /* Locks Held: ipf_nat(READ) */ 5466 /* */ 5467 /* Translate a packet coming "in" on an interface. */ 5468 /* ------------------------------------------------------------------------ */ 5469 int 5470 ipf_nat_in(fr_info_t *fin, nat_t *nat, int natadd, u_32_t nflags) 5471 { 5472 ipf_main_softc_t *softc = fin->fin_main_soft; 5473 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 5474 u_32_t sumd, ipsumd, sum1, sum2; 5475 icmphdr_t *icmp; 5476 tcphdr_t *tcp; 5477 ipnat_t *np; 5478 int skip; 5479 int i; 5480 5481 tcp = NULL; 5482 np = nat->nat_ptr; 5483 fin->fin_fr = nat->nat_fr; 5484 5485 if (np != NULL) { 5486 if ((natadd != 0) && (fin->fin_flx & FI_FRAG)) 5487 (void) ipf_frag_natnew(softc, fin, 0, nat); 5488 5489 /* ------------------------------------------------------------- */ 5490 /* A few quick notes: */ 5491 /* Following are test conditions prior to calling the */ 5492 /* ipf_proxy_check routine. */ 5493 /* */ 5494 /* A NULL tcp indicates a non TCP/UDP packet. When dealing */ 5495 /* with a map rule, we attempt to match the packet's */ 5496 /* source port against in_dport, otherwise we'd compare the */ 5497 /* packet's destination. */ 5498 /* ------------------------------------------------------------- */ 5499 if (np->in_apr != NULL) { 5500 i = ipf_proxy_check(fin, nat); 5501 if (i == -1) { 5502 NBUMPSIDED(0, ns_ipf_proxy_fail); 5503 return -1; 5504 } 5505 } 5506 } 5507 5508 ipf_sync_update(softc, SMC_NAT, fin, nat->nat_sync); 5509 5510 ipsumd = nat->nat_ipsumd; 5511 /* 5512 * Fix up checksums, not by recalculating them, but 5513 * simply computing adjustments. 5514 * Why only do this for some platforms on inbound packets ? 5515 * Because for those that it is done, IP processing is yet to happen 5516 * and so the IPv4 header checksum has not yet been evaluated. 5517 * Perhaps it should always be done for the benefit of things like 5518 * fast forwarding (so that it doesn't need to be recomputed) but with 5519 * header checksum offloading, perhaps it is a moot point. 5520 */ 5521 5522 switch (nat->nat_dir) 5523 { 5524 case NAT_INBOUND : 5525 if ((fin->fin_flx & FI_ICMPERR) == 0) { 5526 fin->fin_ip->ip_src = nat->nat_nsrcip; 5527 fin->fin_saddr = nat->nat_nsrcaddr; 5528 } else { 5529 sum1 = nat->nat_osrcaddr; 5530 sum2 = nat->nat_nsrcaddr; 5531 CALC_SUMD(sum1, sum2, sumd); 5532 ipsumd -= sumd; 5533 } 5534 fin->fin_ip->ip_dst = nat->nat_ndstip; 5535 fin->fin_daddr = nat->nat_ndstaddr; 5536 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 5537 defined(__osf__) || defined(linux) 5538 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, ipsumd, 0); 5539 #endif 5540 break; 5541 5542 case NAT_OUTBOUND : 5543 if ((fin->fin_flx & FI_ICMPERR) == 0) { 5544 fin->fin_ip->ip_src = nat->nat_odstip; 5545 fin->fin_saddr = nat->nat_odstaddr; 5546 } else { 5547 sum1 = nat->nat_odstaddr; 5548 sum2 = nat->nat_ndstaddr; 5549 CALC_SUMD(sum1, sum2, sumd); 5550 ipsumd -= sumd; 5551 } 5552 fin->fin_ip->ip_dst = nat->nat_osrcip; 5553 fin->fin_daddr = nat->nat_osrcaddr; 5554 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 5555 defined(__osf__) || defined(linux) 5556 ipf_fix_incksum(0, &fin->fin_ip->ip_sum, ipsumd, 0); 5557 #endif 5558 break; 5559 5560 case NAT_DIVERTIN : 5561 { 5562 udphdr_t *uh; 5563 ip_t *ip; 5564 mb_t *m; 5565 5566 m = M_DUP(np->in_divmp); 5567 if (m == NULL) { 5568 NBUMPSIDED(0, ns_divert_dup); 5569 return -1; 5570 } 5571 5572 ip = MTOD(m, ip_t *); 5573 ip->ip_id = htons(ipf_nextipid(fin)); 5574 sum1 = ntohs(ip->ip_len); 5575 ip->ip_len = ntohs(ip->ip_len); 5576 ip->ip_len += fin->fin_plen; 5577 ip->ip_len = htons(ip->ip_len); 5578 5579 uh = (udphdr_t *)(ip + 1); 5580 uh->uh_ulen += fin->fin_plen; 5581 uh->uh_ulen = htons(uh->uh_ulen); 5582 5583 sum2 = ntohs(ip->ip_id) + ntohs(ip->ip_len); 5584 sum2 += ntohs(ip->ip_off) & IP_DF; 5585 CALC_SUMD(sum1, sum2, sumd); 5586 5587 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 5588 defined(__osf__) || defined(linux) 5589 ipf_fix_outcksum(0, &ip->ip_sum, sumd, 0); 5590 #endif 5591 PREP_MB_T(fin, m); 5592 5593 fin->fin_ip = ip; 5594 fin->fin_plen += sizeof(ip_t) + 8; /* UDP + new IPv4 hdr */ 5595 fin->fin_dlen += sizeof(ip_t) + 8; /* UDP + old IPv4 hdr */ 5596 5597 nflags &= ~IPN_TCPUDPICMP; 5598 5599 break; 5600 } 5601 5602 case NAT_DIVERTOUT : 5603 { 5604 mb_t *m; 5605 5606 skip = ipf_nat_decap(fin, nat); 5607 if (skip <= 0) { 5608 NBUMPSIDED(0, ns_decap_fail); 5609 return -1; 5610 } 5611 5612 m = fin->fin_m; 5613 5614 #if defined(MENTAT) && defined(_KERNEL) 5615 m->b_rptr += skip; 5616 #else 5617 m->m_data += skip; 5618 m->m_len -= skip; 5619 5620 # ifdef M_PKTHDR 5621 if (m->m_flags & M_PKTHDR) 5622 m->m_pkthdr.len -= skip; 5623 # endif 5624 #endif 5625 5626 ipf_nat_update(fin, nat); 5627 nflags &= ~IPN_TCPUDPICMP; 5628 fin->fin_flx |= FI_NATED; 5629 if (np != NULL && np->in_tag.ipt_num[0] != 0) 5630 fin->fin_nattag = &np->in_tag; 5631 return 1; 5632 /* NOTREACHED */ 5633 } 5634 } 5635 if (nflags & IPN_TCPUDP) 5636 tcp = fin->fin_dp; 5637 5638 if (!(fin->fin_flx & FI_SHORT) && (fin->fin_off == 0)) { 5639 u_short *csump; 5640 5641 if ((nat->nat_odport != 0) && (nflags & IPN_TCPUDP)) { 5642 switch (nat->nat_dir) 5643 { 5644 case NAT_INBOUND : 5645 tcp->th_sport = nat->nat_nsport; 5646 fin->fin_data[0] = ntohs(nat->nat_nsport); 5647 tcp->th_dport = nat->nat_ndport; 5648 fin->fin_data[1] = ntohs(nat->nat_ndport); 5649 break; 5650 5651 case NAT_OUTBOUND : 5652 tcp->th_sport = nat->nat_odport; 5653 fin->fin_data[0] = ntohs(nat->nat_odport); 5654 tcp->th_dport = nat->nat_osport; 5655 fin->fin_data[1] = ntohs(nat->nat_osport); 5656 break; 5657 } 5658 } 5659 5660 5661 if ((nat->nat_odport != 0) && (nflags & IPN_ICMPQUERY)) { 5662 icmp = fin->fin_dp; 5663 5664 icmp->icmp_id = nat->nat_nicmpid; 5665 } 5666 5667 csump = ipf_nat_proto(fin, nat, nflags); 5668 5669 /* 5670 * The above comments do not hold for layer 4 (or higher) 5671 * checksums... 5672 */ 5673 if (csump != NULL) { 5674 if (nat->nat_dir == NAT_OUTBOUND) 5675 ipf_fix_incksum(0, csump, nat->nat_sumd[0], 0); 5676 else 5677 ipf_fix_outcksum(0, csump, nat->nat_sumd[0], 0); 5678 } 5679 } 5680 5681 fin->fin_flx |= FI_NATED; 5682 if (np != NULL && np->in_tag.ipt_num[0] != 0) 5683 fin->fin_nattag = &np->in_tag; 5684 return 1; 5685 } 5686 5687 5688 /* ------------------------------------------------------------------------ */ 5689 /* Function: ipf_nat_proto */ 5690 /* Returns: u_short* - pointer to transport header checksum to update, */ 5691 /* NULL if the transport protocol is not recognised */ 5692 /* as needing a checksum update. */ 5693 /* Parameters: fin(I) - pointer to packet information */ 5694 /* nat(I) - pointer to NAT structure */ 5695 /* nflags(I) - NAT flags set for this packet */ 5696 /* */ 5697 /* Return the pointer to the checksum field for each protocol so understood.*/ 5698 /* If support for making other changes to a protocol header is required, */ 5699 /* that is not strictly 'address' translation, such as clamping the MSS in */ 5700 /* TCP down to a specific value, then do it from here. */ 5701 /* ------------------------------------------------------------------------ */ 5702 u_short * 5703 ipf_nat_proto(fr_info_t *fin, nat_t *nat, u_int nflags) 5704 { 5705 icmphdr_t *icmp; 5706 u_short *csump; 5707 tcphdr_t *tcp; 5708 udphdr_t *udp; 5709 5710 csump = NULL; 5711 if (fin->fin_out == 0) { 5712 fin->fin_rev = (nat->nat_dir & NAT_OUTBOUND); 5713 } else { 5714 fin->fin_rev = ((nat->nat_dir & NAT_OUTBOUND) == 0); 5715 } 5716 5717 switch (fin->fin_p) 5718 { 5719 case IPPROTO_TCP : 5720 tcp = fin->fin_dp; 5721 5722 if ((nflags & IPN_TCP) != 0) 5723 csump = &tcp->th_sum; 5724 5725 /* 5726 * Do a MSS CLAMPING on a SYN packet, 5727 * only deal IPv4 for now. 5728 */ 5729 if ((nat->nat_mssclamp != 0) && (tcp->th_flags & TH_SYN) != 0) 5730 ipf_nat_mssclamp(tcp, nat->nat_mssclamp, fin, csump); 5731 5732 break; 5733 5734 case IPPROTO_UDP : 5735 udp = fin->fin_dp; 5736 5737 if ((nflags & IPN_UDP) != 0) { 5738 if (udp->uh_sum != 0) 5739 csump = &udp->uh_sum; 5740 } 5741 break; 5742 5743 case IPPROTO_ICMP : 5744 icmp = fin->fin_dp; 5745 5746 if ((nflags & IPN_ICMPQUERY) != 0) { 5747 if (icmp->icmp_cksum != 0) 5748 csump = &icmp->icmp_cksum; 5749 } 5750 break; 5751 5752 #ifdef USE_INET6 5753 case IPPROTO_ICMPV6 : 5754 { 5755 struct icmp6_hdr *icmp6 = (struct icmp6_hdr *)fin->fin_dp; 5756 5757 icmp6 = fin->fin_dp; 5758 5759 if ((nflags & IPN_ICMPQUERY) != 0) { 5760 if (icmp6->icmp6_cksum != 0) 5761 csump = &icmp6->icmp6_cksum; 5762 } 5763 break; 5764 } 5765 #endif 5766 } 5767 return csump; 5768 } 5769 5770 5771 /* ------------------------------------------------------------------------ */ 5772 /* Function: ipf_nat_expire */ 5773 /* Returns: Nil */ 5774 /* Parameters: softc(I) - pointer to soft context main structure */ 5775 /* */ 5776 /* Check all of the timeout queues for entries at the top which need to be */ 5777 /* expired. */ 5778 /* ------------------------------------------------------------------------ */ 5779 void 5780 ipf_nat_expire(ipf_main_softc_t *softc) 5781 { 5782 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 5783 ipftq_t *ifq, *ifqnext; 5784 ipftqent_t *tqe, *tqn; 5785 int i; 5786 SPL_INT(s); 5787 5788 SPL_NET(s); 5789 WRITE_ENTER(&softc->ipf_nat); 5790 for (ifq = softn->ipf_nat_tcptq, i = 0; ifq != NULL; 5791 ifq = ifq->ifq_next) { 5792 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) { 5793 if (tqe->tqe_die > softc->ipf_ticks) 5794 break; 5795 tqn = tqe->tqe_next; 5796 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE); 5797 } 5798 } 5799 5800 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifq->ifq_next) { 5801 for (tqn = ifq->ifq_head; ((tqe = tqn) != NULL); i++) { 5802 if (tqe->tqe_die > softc->ipf_ticks) 5803 break; 5804 tqn = tqe->tqe_next; 5805 ipf_nat_delete(softc, tqe->tqe_parent, NL_EXPIRE); 5806 } 5807 } 5808 5809 for (ifq = softn->ipf_nat_utqe; ifq != NULL; ifq = ifqnext) { 5810 ifqnext = ifq->ifq_next; 5811 5812 if (((ifq->ifq_flags & IFQF_DELETE) != 0) && 5813 (ifq->ifq_ref == 0)) { 5814 ipf_freetimeoutqueue(softc, ifq); 5815 } 5816 } 5817 5818 if (softn->ipf_nat_doflush != 0) { 5819 ipf_nat_extraflush(softc, softn, 2); 5820 softn->ipf_nat_doflush = 0; 5821 } 5822 5823 RWLOCK_EXIT(&softc->ipf_nat); 5824 SPL_X(s); 5825 } 5826 5827 5828 /* ------------------------------------------------------------------------ */ 5829 /* Function: ipf_nat_sync */ 5830 /* Returns: Nil */ 5831 /* Parameters: softc(I) - pointer to soft context main structure */ 5832 /* ifp(I) - pointer to network interface */ 5833 /* */ 5834 /* Walk through all of the currently active NAT sessions, looking for those */ 5835 /* which need to have their translated address updated. */ 5836 /* ------------------------------------------------------------------------ */ 5837 void 5838 ipf_nat_sync(ipf_main_softc_t *softc, void *ifp) 5839 { 5840 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 5841 u_32_t sum1, sum2, sumd; 5842 i6addr_t in; 5843 ipnat_t *n; 5844 nat_t *nat; 5845 void *ifp2; 5846 int idx; 5847 SPL_INT(s); 5848 5849 if (softc->ipf_running <= 0) 5850 return; 5851 5852 /* 5853 * Change IP addresses for NAT sessions for any protocol except TCP 5854 * since it will break the TCP connection anyway. The only rules 5855 * which will get changed are those which are "map ... -> 0/32", 5856 * where the rule specifies the address is taken from the interface. 5857 */ 5858 SPL_NET(s); 5859 WRITE_ENTER(&softc->ipf_nat); 5860 5861 if (softc->ipf_running <= 0) { 5862 RWLOCK_EXIT(&softc->ipf_nat); 5863 return; 5864 } 5865 5866 for (nat = softn->ipf_nat_instances; nat; nat = nat->nat_next) { 5867 if ((nat->nat_flags & IPN_TCP) != 0) 5868 continue; 5869 5870 n = nat->nat_ptr; 5871 if (n != NULL) { 5872 if (n->in_v[1] == 4) { 5873 if (n->in_redir & NAT_MAP) { 5874 if ((n->in_nsrcaddr != 0) || 5875 (n->in_nsrcmsk != 0xffffffff)) 5876 continue; 5877 } else if (n->in_redir & NAT_REDIRECT) { 5878 if ((n->in_ndstaddr != 0) || 5879 (n->in_ndstmsk != 0xffffffff)) 5880 continue; 5881 } 5882 } 5883 #ifdef USE_INET6 5884 if (n->in_v[1] == 4) { 5885 if (n->in_redir & NAT_MAP) { 5886 if (!IP6_ISZERO(&n->in_nsrcaddr) || 5887 !IP6_ISONES(&n->in_nsrcmsk)) 5888 continue; 5889 } else if (n->in_redir & NAT_REDIRECT) { 5890 if (!IP6_ISZERO(&n->in_ndstaddr) || 5891 !IP6_ISONES(&n->in_ndstmsk)) 5892 continue; 5893 } 5894 } 5895 #endif 5896 } 5897 5898 if (((ifp == NULL) || (ifp == nat->nat_ifps[0]) || 5899 (ifp == nat->nat_ifps[1]))) { 5900 nat->nat_ifps[0] = GETIFP(nat->nat_ifnames[0], 5901 nat->nat_v[0]); 5902 if ((nat->nat_ifps[0] != NULL) && 5903 (nat->nat_ifps[0] != (void *)-1)) { 5904 nat->nat_mtu[0] = GETIFMTU_4(nat->nat_ifps[0]); 5905 } 5906 if (nat->nat_ifnames[1][0] != '\0') { 5907 nat->nat_ifps[1] = GETIFP(nat->nat_ifnames[1], 5908 nat->nat_v[1]); 5909 } else { 5910 nat->nat_ifps[1] = nat->nat_ifps[0]; 5911 } 5912 if ((nat->nat_ifps[1] != NULL) && 5913 (nat->nat_ifps[1] != (void *)-1)) { 5914 nat->nat_mtu[1] = GETIFMTU_4(nat->nat_ifps[1]); 5915 } 5916 ifp2 = nat->nat_ifps[0]; 5917 if (ifp2 == NULL) 5918 continue; 5919 5920 /* 5921 * Change the map-to address to be the same as the 5922 * new one. 5923 */ 5924 sum1 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6); 5925 if (ipf_ifpaddr(softc, nat->nat_v[0], FRI_NORMAL, ifp2, 5926 &in, NULL) != -1) { 5927 if (nat->nat_v[0] == 4) 5928 nat->nat_nsrcip = in.in4; 5929 } 5930 sum2 = NATFSUM(nat, nat->nat_v[1], nat_nsrc6); 5931 5932 if (sum1 == sum2) 5933 continue; 5934 /* 5935 * Readjust the checksum adjustment to take into 5936 * account the new IP#. 5937 */ 5938 CALC_SUMD(sum1, sum2, sumd); 5939 /* XXX - dont change for TCP when solaris does 5940 * hardware checksumming. 5941 */ 5942 sumd += nat->nat_sumd[0]; 5943 nat->nat_sumd[0] = (sumd & 0xffff) + (sumd >> 16); 5944 nat->nat_sumd[1] = nat->nat_sumd[0]; 5945 } 5946 } 5947 5948 for (n = softn->ipf_nat_list; (n != NULL); n = n->in_next) { 5949 char *base = n->in_names; 5950 5951 if ((ifp == NULL) || (n->in_ifps[0] == ifp)) 5952 n->in_ifps[0] = ipf_resolvenic(softc, 5953 base + n->in_ifnames[0], 5954 n->in_v[0]); 5955 if ((ifp == NULL) || (n->in_ifps[1] == ifp)) 5956 n->in_ifps[1] = ipf_resolvenic(softc, 5957 base + n->in_ifnames[1], 5958 n->in_v[1]); 5959 5960 if (n->in_redir & NAT_REDIRECT) 5961 idx = 1; 5962 else 5963 idx = 0; 5964 5965 if (((ifp == NULL) || (n->in_ifps[idx] == ifp)) && 5966 (n->in_ifps[idx] != NULL && 5967 n->in_ifps[idx] != (void *)-1)) { 5968 5969 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_osrc, 5970 0, n->in_ifps[idx]); 5971 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_odst, 5972 0, n->in_ifps[idx]); 5973 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_nsrc, 5974 0, n->in_ifps[idx]); 5975 ipf_nat_nextaddrinit(softc, n->in_names, &n->in_ndst, 5976 0, n->in_ifps[idx]); 5977 } 5978 } 5979 RWLOCK_EXIT(&softc->ipf_nat); 5980 SPL_X(s); 5981 } 5982 5983 5984 /* ------------------------------------------------------------------------ */ 5985 /* Function: ipf_nat_icmpquerytype */ 5986 /* Returns: int - 1 == success, 0 == failure */ 5987 /* Parameters: icmptype(I) - ICMP type number */ 5988 /* */ 5989 /* Tests to see if the ICMP type number passed is a query/response type or */ 5990 /* not. */ 5991 /* ------------------------------------------------------------------------ */ 5992 static int 5993 ipf_nat_icmpquerytype(int icmptype) 5994 { 5995 5996 /* 5997 * For the ICMP query NAT code, it is essential that both the query 5998 * and the reply match on the NAT rule. Because the NAT structure 5999 * does not keep track of the icmptype, and a single NAT structure 6000 * is used for all icmp types with the same src, dest and id, we 6001 * simply define the replies as queries as well. The funny thing is, 6002 * although it seems silly to call a reply a query, this is exactly 6003 * as it is defined in the IPv4 specification 6004 */ 6005 switch (icmptype) 6006 { 6007 case ICMP_ECHOREPLY: 6008 case ICMP_ECHO: 6009 /* route aedvertisement/solliciation is currently unsupported: */ 6010 /* it would require rewriting the ICMP data section */ 6011 case ICMP_TSTAMP: 6012 case ICMP_TSTAMPREPLY: 6013 case ICMP_IREQ: 6014 case ICMP_IREQREPLY: 6015 case ICMP_MASKREQ: 6016 case ICMP_MASKREPLY: 6017 return 1; 6018 default: 6019 return 0; 6020 } 6021 } 6022 6023 6024 /* ------------------------------------------------------------------------ */ 6025 /* Function: nat_log */ 6026 /* Returns: Nil */ 6027 /* Parameters: softc(I) - pointer to soft context main structure */ 6028 /* softn(I) - pointer to NAT context structure */ 6029 /* nat(I) - pointer to NAT structure */ 6030 /* action(I) - action related to NAT structure being performed */ 6031 /* */ 6032 /* Creates a NAT log entry. */ 6033 /* ------------------------------------------------------------------------ */ 6034 void 6035 ipf_nat_log(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, struct nat *nat, 6036 u_int action) 6037 { 6038 #ifdef IPFILTER_LOG 6039 # ifndef LARGE_NAT 6040 struct ipnat *np; 6041 int rulen; 6042 # endif 6043 struct natlog natl; 6044 void *items[1]; 6045 size_t sizes[1]; 6046 int types[1]; 6047 6048 bcopy((char *)&nat->nat_osrc6, (char *)&natl.nl_osrcip, 6049 sizeof(natl.nl_osrcip)); 6050 bcopy((char *)&nat->nat_nsrc6, (char *)&natl.nl_nsrcip, 6051 sizeof(natl.nl_nsrcip)); 6052 bcopy((char *)&nat->nat_odst6, (char *)&natl.nl_odstip, 6053 sizeof(natl.nl_odstip)); 6054 bcopy((char *)&nat->nat_ndst6, (char *)&natl.nl_ndstip, 6055 sizeof(natl.nl_ndstip)); 6056 6057 natl.nl_bytes[0] = nat->nat_bytes[0]; 6058 natl.nl_bytes[1] = nat->nat_bytes[1]; 6059 natl.nl_pkts[0] = nat->nat_pkts[0]; 6060 natl.nl_pkts[1] = nat->nat_pkts[1]; 6061 natl.nl_odstport = nat->nat_odport; 6062 natl.nl_osrcport = nat->nat_osport; 6063 natl.nl_nsrcport = nat->nat_nsport; 6064 natl.nl_ndstport = nat->nat_ndport; 6065 natl.nl_p[0] = nat->nat_pr[0]; 6066 natl.nl_p[1] = nat->nat_pr[1]; 6067 natl.nl_v[0] = nat->nat_v[0]; 6068 natl.nl_v[1] = nat->nat_v[1]; 6069 natl.nl_type = nat->nat_redir; 6070 natl.nl_action = action; 6071 natl.nl_rule = -1; 6072 6073 bcopy(nat->nat_ifnames[0], natl.nl_ifnames[0], 6074 sizeof(nat->nat_ifnames[0])); 6075 bcopy(nat->nat_ifnames[1], natl.nl_ifnames[1], 6076 sizeof(nat->nat_ifnames[1])); 6077 6078 # ifndef LARGE_NAT 6079 if (nat->nat_ptr != NULL) { 6080 for (rulen = 0, np = softn->ipf_nat_list; np != NULL; 6081 np = np->in_next, rulen++) 6082 if (np == nat->nat_ptr) { 6083 natl.nl_rule = rulen; 6084 break; 6085 } 6086 } 6087 # endif 6088 items[0] = &natl; 6089 sizes[0] = sizeof(natl); 6090 types[0] = 0; 6091 6092 (void) ipf_log_items(softc, IPL_LOGNAT, NULL, items, sizes, types, 1); 6093 #endif 6094 } 6095 6096 6097 #if defined(__OpenBSD__) 6098 /* ------------------------------------------------------------------------ */ 6099 /* Function: ipf_nat_ifdetach */ 6100 /* Returns: Nil */ 6101 /* Parameters: ifp(I) - pointer to network interface */ 6102 /* */ 6103 /* Compatibility interface for OpenBSD to trigger the correct updating of */ 6104 /* interface references within IPFilter. */ 6105 /* ------------------------------------------------------------------------ */ 6106 void 6107 ipf_nat_ifdetach(ifp) 6108 void *ifp; 6109 { 6110 ipf_main_softc_t *softc; 6111 6112 softc = ipf_get_softc(0); 6113 6114 ipf_sync(ifp); 6115 return; 6116 } 6117 #endif 6118 6119 6120 /* ------------------------------------------------------------------------ */ 6121 /* Function: ipf_nat_rule_deref */ 6122 /* Returns: Nil */ 6123 /* Parameters: softc(I) - pointer to soft context main structure */ 6124 /* inp(I) - pointer to pointer to NAT rule */ 6125 /* Write Locks: ipf_nat */ 6126 /* */ 6127 /* Dropping the refernce count for a rule means that whatever held the */ 6128 /* pointer to this rule (*inp) is no longer interested in it and when the */ 6129 /* reference count drops to zero, any resources allocated for the rule can */ 6130 /* be released and the rule itself free'd. */ 6131 /* ------------------------------------------------------------------------ */ 6132 void 6133 ipf_nat_rule_deref(ipf_main_softc_t *softc, ipnat_t **inp) 6134 { 6135 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 6136 ipnat_t *n; 6137 6138 n = *inp; 6139 *inp = NULL; 6140 n->in_use--; 6141 if (n->in_use > 0) 6142 return; 6143 6144 if (n->in_apr != NULL) 6145 ipf_proxy_deref(n->in_apr); 6146 6147 ipf_nat_rule_fini(softc, n); 6148 6149 if (n->in_redir & NAT_REDIRECT) { 6150 if ((n->in_flags & IPN_PROXYRULE) == 0) { 6151 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_rdr); 6152 } 6153 } 6154 if (n->in_redir & (NAT_MAP|NAT_MAPBLK)) { 6155 if ((n->in_flags & IPN_PROXYRULE) == 0) { 6156 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules_map); 6157 } 6158 } 6159 6160 if (n->in_tqehead[0] != NULL) { 6161 if (ipf_deletetimeoutqueue(n->in_tqehead[0]) == 0) { 6162 ipf_freetimeoutqueue(softc, n->in_tqehead[1]); 6163 } 6164 } 6165 6166 if (n->in_tqehead[1] != NULL) { 6167 if (ipf_deletetimeoutqueue(n->in_tqehead[1]) == 0) { 6168 ipf_freetimeoutqueue(softc, n->in_tqehead[1]); 6169 } 6170 } 6171 6172 if ((n->in_flags & IPN_PROXYRULE) == 0) { 6173 ATOMIC_DEC32(softn->ipf_nat_stats.ns_rules); 6174 } 6175 6176 MUTEX_DESTROY(&n->in_lock); 6177 6178 KFREES(n, n->in_size); 6179 6180 #if SOLARIS && !defined(INSTANCES) 6181 if (softn->ipf_nat_stats.ns_rules == 0) 6182 pfil_delayed_copy = 1; 6183 #endif 6184 } 6185 6186 6187 /* ------------------------------------------------------------------------ */ 6188 /* Function: ipf_nat_deref */ 6189 /* Returns: Nil */ 6190 /* Parameters: softc(I) - pointer to soft context main structure */ 6191 /* natp(I) - pointer to pointer to NAT table entry */ 6192 /* */ 6193 /* Decrement the reference counter for this NAT table entry and free it if */ 6194 /* there are no more things using it. */ 6195 /* */ 6196 /* IF nat_ref == 1 when this function is called, then we have an orphan nat */ 6197 /* structure *because* it only gets called on paths _after_ nat_ref has been*/ 6198 /* incremented. If nat_ref == 1 then we shouldn't decrement it here */ 6199 /* because nat_delete() will do that and send nat_ref to -1. */ 6200 /* */ 6201 /* Holding the lock on nat_lock is required to serialise nat_delete() being */ 6202 /* called from a NAT flush ioctl with a deref happening because of a packet.*/ 6203 /* ------------------------------------------------------------------------ */ 6204 void 6205 ipf_nat_deref(ipf_main_softc_t *softc, nat_t **natp) 6206 { 6207 nat_t *nat; 6208 6209 nat = *natp; 6210 *natp = NULL; 6211 6212 MUTEX_ENTER(&nat->nat_lock); 6213 if (nat->nat_ref > 1) { 6214 nat->nat_ref--; 6215 ASSERT(nat->nat_ref >= 0); 6216 MUTEX_EXIT(&nat->nat_lock); 6217 return; 6218 } 6219 MUTEX_EXIT(&nat->nat_lock); 6220 6221 WRITE_ENTER(&softc->ipf_nat); 6222 ipf_nat_delete(softc, nat, NL_EXPIRE); 6223 RWLOCK_EXIT(&softc->ipf_nat); 6224 } 6225 6226 6227 /* ------------------------------------------------------------------------ */ 6228 /* Function: ipf_nat_clone */ 6229 /* Returns: ipstate_t* - NULL == cloning failed, */ 6230 /* else pointer to new state structure */ 6231 /* Parameters: fin(I) - pointer to packet information */ 6232 /* is(I) - pointer to master state structure */ 6233 /* Write Lock: ipf_nat */ 6234 /* */ 6235 /* Create a "duplcate" state table entry from the master. */ 6236 /* ------------------------------------------------------------------------ */ 6237 nat_t * 6238 ipf_nat_clone(fr_info_t *fin, nat_t *nat) 6239 { 6240 ipf_main_softc_t *softc = fin->fin_main_soft; 6241 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 6242 frentry_t *fr; 6243 nat_t *clone; 6244 ipnat_t *np; 6245 6246 KMALLOC(clone, nat_t *); 6247 if (clone == NULL) { 6248 NBUMPSIDED(fin->fin_out, ns_clone_nomem); 6249 return NULL; 6250 } 6251 bcopy((char *)nat, (char *)clone, sizeof(*clone)); 6252 6253 MUTEX_NUKE(&clone->nat_lock); 6254 6255 clone->nat_rev = fin->fin_rev; 6256 clone->nat_aps = NULL; 6257 /* 6258 * Initialize all these so that ipf_nat_delete() doesn't cause a crash. 6259 */ 6260 clone->nat_tqe.tqe_pnext = NULL; 6261 clone->nat_tqe.tqe_next = NULL; 6262 clone->nat_tqe.tqe_ifq = NULL; 6263 clone->nat_tqe.tqe_parent = clone; 6264 6265 clone->nat_flags &= ~SI_CLONE; 6266 clone->nat_flags |= SI_CLONED; 6267 6268 if (clone->nat_hm) 6269 clone->nat_hm->hm_ref++; 6270 6271 if (ipf_nat_insert(softc, softn, clone) == -1) { 6272 KFREE(clone); 6273 NBUMPSIDED(fin->fin_out, ns_insert_fail); 6274 return NULL; 6275 } 6276 6277 np = clone->nat_ptr; 6278 if (np != NULL) { 6279 if (softn->ipf_nat_logging) 6280 ipf_nat_log(softc, softn, clone, NL_CLONE); 6281 np->in_use++; 6282 } 6283 fr = clone->nat_fr; 6284 if (fr != NULL) { 6285 MUTEX_ENTER(&fr->fr_lock); 6286 fr->fr_ref++; 6287 MUTEX_EXIT(&fr->fr_lock); 6288 } 6289 6290 6291 /* 6292 * Because the clone is created outside the normal loop of things and 6293 * TCP has special needs in terms of state, initialise the timeout 6294 * state of the new NAT from here. 6295 */ 6296 if (clone->nat_pr[0] == IPPROTO_TCP) { 6297 (void) ipf_tcp_age(&clone->nat_tqe, fin, softn->ipf_nat_tcptq, 6298 clone->nat_flags, 2); 6299 } 6300 clone->nat_sync = ipf_sync_new(softc, SMC_NAT, fin, clone); 6301 if (softn->ipf_nat_logging) 6302 ipf_nat_log(softc, softn, clone, NL_CLONE); 6303 return clone; 6304 } 6305 6306 6307 /* ------------------------------------------------------------------------ */ 6308 /* Function: ipf_nat_wildok */ 6309 /* Returns: int - 1 == packet's ports match wildcards */ 6310 /* 0 == packet's ports don't match wildcards */ 6311 /* Parameters: nat(I) - NAT entry */ 6312 /* sport(I) - source port */ 6313 /* dport(I) - destination port */ 6314 /* flags(I) - wildcard flags */ 6315 /* dir(I) - packet direction */ 6316 /* */ 6317 /* Use NAT entry and packet direction to determine which combination of */ 6318 /* wildcard flags should be used. */ 6319 /* ------------------------------------------------------------------------ */ 6320 int 6321 ipf_nat_wildok(nat_t *nat, int sport, int dport, int flags, int dir) 6322 { 6323 /* 6324 * When called by dir is set to 6325 * nat_inlookup NAT_INBOUND (0) 6326 * nat_outlookup NAT_OUTBOUND (1) 6327 * 6328 * We simply combine the packet's direction in dir with the original 6329 * "intended" direction of that NAT entry in nat->nat_dir to decide 6330 * which combination of wildcard flags to allow. 6331 */ 6332 switch ((dir << 1) | (nat->nat_dir & (NAT_INBOUND|NAT_OUTBOUND))) 6333 { 6334 case 3: /* outbound packet / outbound entry */ 6335 if (((nat->nat_osport == sport) || 6336 (flags & SI_W_SPORT)) && 6337 ((nat->nat_odport == dport) || 6338 (flags & SI_W_DPORT))) 6339 return 1; 6340 break; 6341 case 2: /* outbound packet / inbound entry */ 6342 if (((nat->nat_osport == dport) || 6343 (flags & SI_W_SPORT)) && 6344 ((nat->nat_odport == sport) || 6345 (flags & SI_W_DPORT))) 6346 return 1; 6347 break; 6348 case 1: /* inbound packet / outbound entry */ 6349 if (((nat->nat_osport == dport) || 6350 (flags & SI_W_SPORT)) && 6351 ((nat->nat_odport == sport) || 6352 (flags & SI_W_DPORT))) 6353 return 1; 6354 break; 6355 case 0: /* inbound packet / inbound entry */ 6356 if (((nat->nat_osport == sport) || 6357 (flags & SI_W_SPORT)) && 6358 ((nat->nat_odport == dport) || 6359 (flags & SI_W_DPORT))) 6360 return 1; 6361 break; 6362 default: 6363 break; 6364 } 6365 6366 return(0); 6367 } 6368 6369 6370 /* ------------------------------------------------------------------------ */ 6371 /* Function: nat_mssclamp */ 6372 /* Returns: Nil */ 6373 /* Parameters: tcp(I) - pointer to TCP header */ 6374 /* maxmss(I) - value to clamp the TCP MSS to */ 6375 /* fin(I) - pointer to packet information */ 6376 /* csump(I) - pointer to TCP checksum */ 6377 /* */ 6378 /* Check for MSS option and clamp it if necessary. If found and changed, */ 6379 /* then the TCP header checksum will be updated to reflect the change in */ 6380 /* the MSS. */ 6381 /* ------------------------------------------------------------------------ */ 6382 static void 6383 ipf_nat_mssclamp(tcphdr_t *tcp, u_32_t maxmss, fr_info_t *fin, u_short *csump) 6384 { 6385 u_char *cp, *ep, opt; 6386 int hlen, advance; 6387 u_32_t mss, sumd; 6388 6389 hlen = TCP_OFF(tcp) << 2; 6390 if (hlen > sizeof(*tcp)) { 6391 cp = (u_char *)tcp + sizeof(*tcp); 6392 ep = (u_char *)tcp + hlen; 6393 6394 while (cp < ep) { 6395 opt = cp[0]; 6396 if (opt == TCPOPT_EOL) 6397 break; 6398 else if (opt == TCPOPT_NOP) { 6399 cp++; 6400 continue; 6401 } 6402 6403 if (cp + 1 >= ep) 6404 break; 6405 advance = cp[1]; 6406 if ((cp + advance > ep) || (advance <= 0)) 6407 break; 6408 switch (opt) 6409 { 6410 case TCPOPT_MAXSEG: 6411 if (advance != 4) 6412 break; 6413 mss = cp[2] * 256 + cp[3]; 6414 if (mss > maxmss) { 6415 cp[2] = maxmss / 256; 6416 cp[3] = maxmss & 0xff; 6417 CALC_SUMD(mss, maxmss, sumd); 6418 ipf_fix_outcksum(0, csump, sumd, 0); 6419 } 6420 break; 6421 default: 6422 /* ignore unknown options */ 6423 break; 6424 } 6425 6426 cp += advance; 6427 } 6428 } 6429 } 6430 6431 6432 /* ------------------------------------------------------------------------ */ 6433 /* Function: ipf_nat_setqueue */ 6434 /* Returns: Nil */ 6435 /* Parameters: softc(I) - pointer to soft context main structure */ 6436 /* softn(I) - pointer to NAT context structure */ 6437 /* nat(I)- pointer to NAT structure */ 6438 /* Locks: ipf_nat (read or write) */ 6439 /* */ 6440 /* Put the NAT entry on its default queue entry, using rev as a helped in */ 6441 /* determining which queue it should be placed on. */ 6442 /* ------------------------------------------------------------------------ */ 6443 void 6444 ipf_nat_setqueue(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, nat_t *nat) 6445 { 6446 ipftq_t *oifq, *nifq; 6447 int rev = nat->nat_rev; 6448 6449 if (nat->nat_ptr != NULL) 6450 nifq = nat->nat_ptr->in_tqehead[rev]; 6451 else 6452 nifq = NULL; 6453 6454 if (nifq == NULL) { 6455 switch (nat->nat_pr[0]) 6456 { 6457 case IPPROTO_UDP : 6458 nifq = &softn->ipf_nat_udptq; 6459 break; 6460 case IPPROTO_ICMP : 6461 nifq = &softn->ipf_nat_icmptq; 6462 break; 6463 case IPPROTO_TCP : 6464 nifq = softn->ipf_nat_tcptq + 6465 nat->nat_tqe.tqe_state[rev]; 6466 break; 6467 default : 6468 nifq = &softn->ipf_nat_iptq; 6469 break; 6470 } 6471 } 6472 6473 oifq = nat->nat_tqe.tqe_ifq; 6474 /* 6475 * If it's currently on a timeout queue, move it from one queue to 6476 * another, else put it on the end of the newly determined queue. 6477 */ 6478 if (oifq != NULL) 6479 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, nifq); 6480 else 6481 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, nifq, nat); 6482 return; 6483 } 6484 6485 6486 /* ------------------------------------------------------------------------ */ 6487 /* Function: nat_getnext */ 6488 /* Returns: int - 0 == ok, else error */ 6489 /* Parameters: softc(I) - pointer to soft context main structure */ 6490 /* t(I) - pointer to ipftoken structure */ 6491 /* itp(I) - pointer to ipfgeniter_t structure */ 6492 /* */ 6493 /* Fetch the next nat/ipnat structure pointer from the linked list and */ 6494 /* copy it out to the storage space pointed to by itp_data. The next item */ 6495 /* in the list to look at is put back in the ipftoken struture. */ 6496 /* ------------------------------------------------------------------------ */ 6497 static int 6498 ipf_nat_getnext(ipf_main_softc_t *softc, ipftoken_t *t, ipfgeniter_t *itp, 6499 ipfobj_t *objp) 6500 { 6501 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 6502 hostmap_t *hm, *nexthm = NULL, zerohm; 6503 ipnat_t *ipn, *nextipnat = NULL, zeroipn; 6504 nat_t *nat, *nextnat = NULL, zeronat; 6505 int error = 0; 6506 void *nnext; 6507 6508 if (itp->igi_nitems != 1) { 6509 IPFERROR(60075); 6510 return ENOSPC; 6511 } 6512 6513 READ_ENTER(&softc->ipf_nat); 6514 6515 switch (itp->igi_type) 6516 { 6517 case IPFGENITER_HOSTMAP : 6518 hm = t->ipt_data; 6519 if (hm == NULL) { 6520 nexthm = softn->ipf_hm_maplist; 6521 } else { 6522 nexthm = hm->hm_next; 6523 } 6524 if (nexthm != NULL) { 6525 ATOMIC_INC32(nexthm->hm_ref); 6526 t->ipt_data = nexthm; 6527 } else { 6528 bzero(&zerohm, sizeof(zerohm)); 6529 nexthm = &zerohm; 6530 t->ipt_data = NULL; 6531 } 6532 nnext = nexthm->hm_next; 6533 break; 6534 6535 case IPFGENITER_IPNAT : 6536 ipn = t->ipt_data; 6537 if (ipn == NULL) { 6538 nextipnat = softn->ipf_nat_list; 6539 } else { 6540 nextipnat = ipn->in_next; 6541 } 6542 if (nextipnat != NULL) { 6543 ATOMIC_INC32(nextipnat->in_use); 6544 t->ipt_data = nextipnat; 6545 } else { 6546 bzero(&zeroipn, sizeof(zeroipn)); 6547 nextipnat = &zeroipn; 6548 t->ipt_data = NULL; 6549 } 6550 nnext = nextipnat->in_next; 6551 break; 6552 6553 case IPFGENITER_NAT : 6554 nat = t->ipt_data; 6555 if (nat == NULL) { 6556 nextnat = softn->ipf_nat_instances; 6557 } else { 6558 nextnat = nat->nat_next; 6559 } 6560 if (nextnat != NULL) { 6561 MUTEX_ENTER(&nextnat->nat_lock); 6562 nextnat->nat_ref++; 6563 MUTEX_EXIT(&nextnat->nat_lock); 6564 t->ipt_data = nextnat; 6565 } else { 6566 bzero(&zeronat, sizeof(zeronat)); 6567 nextnat = &zeronat; 6568 t->ipt_data = NULL; 6569 } 6570 nnext = nextnat->nat_next; 6571 break; 6572 6573 default : 6574 RWLOCK_EXIT(&softc->ipf_nat); 6575 IPFERROR(60055); 6576 return EINVAL; 6577 } 6578 6579 RWLOCK_EXIT(&softc->ipf_nat); 6580 6581 objp->ipfo_ptr = itp->igi_data; 6582 6583 switch (itp->igi_type) 6584 { 6585 case IPFGENITER_HOSTMAP : 6586 error = COPYOUT(nexthm, objp->ipfo_ptr, sizeof(*nexthm)); 6587 if (error != 0) { 6588 IPFERROR(60049); 6589 error = EFAULT; 6590 } 6591 if (hm != NULL) { 6592 WRITE_ENTER(&softc->ipf_nat); 6593 ipf_nat_hostmapdel(softc, &hm); 6594 RWLOCK_EXIT(&softc->ipf_nat); 6595 } 6596 break; 6597 6598 case IPFGENITER_IPNAT : 6599 objp->ipfo_size = nextipnat->in_size; 6600 objp->ipfo_type = IPFOBJ_IPNAT; 6601 error = ipf_outobjk(softc, objp, nextipnat); 6602 if (ipn != NULL) { 6603 WRITE_ENTER(&softc->ipf_nat); 6604 ipf_nat_rule_deref(softc, &ipn); 6605 RWLOCK_EXIT(&softc->ipf_nat); 6606 } 6607 break; 6608 6609 case IPFGENITER_NAT : 6610 objp->ipfo_size = sizeof(nat_t); 6611 objp->ipfo_type = IPFOBJ_NAT; 6612 error = ipf_outobjk(softc, objp, nextnat); 6613 if (nat != NULL) 6614 ipf_nat_deref(softc, &nat); 6615 6616 break; 6617 } 6618 6619 if (nnext == NULL) 6620 ipf_token_mark_complete(t); 6621 6622 return error; 6623 } 6624 6625 6626 /* ------------------------------------------------------------------------ */ 6627 /* Function: nat_extraflush */ 6628 /* Returns: int - 0 == success, -1 == failure */ 6629 /* Parameters: softc(I) - pointer to soft context main structure */ 6630 /* softn(I) - pointer to NAT context structure */ 6631 /* which(I) - how to flush the active NAT table */ 6632 /* Write Locks: ipf_nat */ 6633 /* */ 6634 /* Flush nat tables. Three actions currently defined: */ 6635 /* which == 0 : flush all nat table entries */ 6636 /* which == 1 : flush TCP connections which have started to close but are */ 6637 /* stuck for some reason. */ 6638 /* which == 2 : flush TCP connections which have been idle for a long time, */ 6639 /* starting at > 4 days idle and working back in successive half-*/ 6640 /* days to at most 12 hours old. If this fails to free enough */ 6641 /* slots then work backwards in half hour slots to 30 minutes. */ 6642 /* If that too fails, then work backwards in 30 second intervals */ 6643 /* for the last 30 minutes to at worst 30 seconds idle. */ 6644 /* ------------------------------------------------------------------------ */ 6645 static int 6646 ipf_nat_extraflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, int which) 6647 { 6648 nat_t *nat, **natp; 6649 ipftqent_t *tqn; 6650 ipftq_t *ifq; 6651 int removed; 6652 SPL_INT(s); 6653 6654 removed = 0; 6655 6656 SPL_NET(s); 6657 switch (which) 6658 { 6659 case 0 : 6660 softn->ipf_nat_stats.ns_flush_all++; 6661 /* 6662 * Style 0 flush removes everything... 6663 */ 6664 for (natp = &softn->ipf_nat_instances; 6665 ((nat = *natp) != NULL); ) { 6666 ipf_nat_delete(softc, nat, NL_FLUSH); 6667 removed++; 6668 } 6669 break; 6670 6671 case 1 : 6672 softn->ipf_nat_stats.ns_flush_closing++; 6673 /* 6674 * Since we're only interested in things that are closing, 6675 * we can start with the appropriate timeout queue. 6676 */ 6677 for (ifq = softn->ipf_nat_tcptq + IPF_TCPS_CLOSE_WAIT; 6678 ifq != NULL; ifq = ifq->ifq_next) { 6679 6680 for (tqn = ifq->ifq_head; tqn != NULL; ) { 6681 nat = tqn->tqe_parent; 6682 tqn = tqn->tqe_next; 6683 if (nat->nat_pr[0] != IPPROTO_TCP || 6684 nat->nat_pr[1] != IPPROTO_TCP) 6685 break; 6686 ipf_nat_delete(softc, nat, NL_EXPIRE); 6687 removed++; 6688 } 6689 } 6690 6691 /* 6692 * Also need to look through the user defined queues. 6693 */ 6694 for (ifq = softn->ipf_nat_utqe; ifq != NULL; 6695 ifq = ifq->ifq_next) { 6696 for (tqn = ifq->ifq_head; tqn != NULL; ) { 6697 nat = tqn->tqe_parent; 6698 tqn = tqn->tqe_next; 6699 if (nat->nat_pr[0] != IPPROTO_TCP || 6700 nat->nat_pr[1] != IPPROTO_TCP) 6701 continue; 6702 6703 if ((nat->nat_tcpstate[0] > 6704 IPF_TCPS_ESTABLISHED) && 6705 (nat->nat_tcpstate[1] > 6706 IPF_TCPS_ESTABLISHED)) { 6707 ipf_nat_delete(softc, nat, NL_EXPIRE); 6708 removed++; 6709 } 6710 } 6711 } 6712 break; 6713 6714 /* 6715 * Args 5-11 correspond to flushing those particular states 6716 * for TCP connections. 6717 */ 6718 case IPF_TCPS_CLOSE_WAIT : 6719 case IPF_TCPS_FIN_WAIT_1 : 6720 case IPF_TCPS_CLOSING : 6721 case IPF_TCPS_LAST_ACK : 6722 case IPF_TCPS_FIN_WAIT_2 : 6723 case IPF_TCPS_TIME_WAIT : 6724 case IPF_TCPS_CLOSED : 6725 softn->ipf_nat_stats.ns_flush_state++; 6726 tqn = softn->ipf_nat_tcptq[which].ifq_head; 6727 while (tqn != NULL) { 6728 nat = tqn->tqe_parent; 6729 tqn = tqn->tqe_next; 6730 ipf_nat_delete(softc, nat, NL_FLUSH); 6731 removed++; 6732 } 6733 break; 6734 6735 default : 6736 if (which < 30) 6737 break; 6738 6739 softn->ipf_nat_stats.ns_flush_timeout++; 6740 /* 6741 * Take a large arbitrary number to mean the number of seconds 6742 * for which which consider to be the maximum value we'll allow 6743 * the expiration to be. 6744 */ 6745 which = IPF_TTLVAL(which); 6746 for (natp = &softn->ipf_nat_instances; 6747 ((nat = *natp) != NULL); ) { 6748 if (softc->ipf_ticks - nat->nat_touched > which) { 6749 ipf_nat_delete(softc, nat, NL_FLUSH); 6750 removed++; 6751 } else 6752 natp = &nat->nat_next; 6753 } 6754 break; 6755 } 6756 6757 if (which != 2) { 6758 SPL_X(s); 6759 return removed; 6760 } 6761 6762 softn->ipf_nat_stats.ns_flush_queue++; 6763 6764 /* 6765 * Asked to remove inactive entries because the table is full, try 6766 * again, 3 times, if first attempt failed with a different criteria 6767 * each time. The order tried in must be in decreasing age. 6768 * Another alternative is to implement random drop and drop N entries 6769 * at random until N have been freed up. 6770 */ 6771 if (softc->ipf_ticks - softn->ipf_nat_last_force_flush > 6772 IPF_TTLVAL(5)) { 6773 softn->ipf_nat_last_force_flush = softc->ipf_ticks; 6774 6775 removed = ipf_queueflush(softc, ipf_nat_flush_entry, 6776 softn->ipf_nat_tcptq, 6777 softn->ipf_nat_utqe, 6778 &softn->ipf_nat_stats.ns_active, 6779 softn->ipf_nat_table_sz, 6780 softn->ipf_nat_table_wm_low); 6781 } 6782 6783 SPL_X(s); 6784 return removed; 6785 } 6786 6787 6788 /* ------------------------------------------------------------------------ */ 6789 /* Function: ipf_nat_flush_entry */ 6790 /* Returns: 0 - always succeeds */ 6791 /* Parameters: softc(I) - pointer to soft context main structure */ 6792 /* entry(I) - pointer to NAT entry */ 6793 /* Write Locks: ipf_nat */ 6794 /* */ 6795 /* This function is a stepping stone between ipf_queueflush() and */ 6796 /* nat_dlete(). It is used so we can provide a uniform interface via the */ 6797 /* ipf_queueflush() function. Since the nat_delete() function returns void */ 6798 /* we translate that to mean it always succeeds in deleting something. */ 6799 /* ------------------------------------------------------------------------ */ 6800 static int 6801 ipf_nat_flush_entry(ipf_main_softc_t *softc, void *entry) 6802 { 6803 ipf_nat_delete(softc, entry, NL_FLUSH); 6804 return 0; 6805 } 6806 6807 6808 /* ------------------------------------------------------------------------ */ 6809 /* Function: ipf_nat_iterator */ 6810 /* Returns: int - 0 == ok, else error */ 6811 /* Parameters: softc(I) - pointer to soft context main structure */ 6812 /* token(I) - pointer to ipftoken structure */ 6813 /* itp(I) - pointer to ipfgeniter_t structure */ 6814 /* obj(I) - pointer to data description structure */ 6815 /* */ 6816 /* This function acts as a handler for the SIOCGENITER ioctls that use a */ 6817 /* generic structure to iterate through a list. There are three different */ 6818 /* linked lists of NAT related information to go through: NAT rules, active */ 6819 /* NAT mappings and the NAT fragment cache. */ 6820 /* ------------------------------------------------------------------------ */ 6821 static int 6822 ipf_nat_iterator(ipf_main_softc_t *softc, ipftoken_t *token, ipfgeniter_t *itp, 6823 ipfobj_t *obj) 6824 { 6825 int error; 6826 6827 if (itp->igi_data == NULL) { 6828 IPFERROR(60052); 6829 return EFAULT; 6830 } 6831 6832 switch (itp->igi_type) 6833 { 6834 case IPFGENITER_HOSTMAP : 6835 case IPFGENITER_IPNAT : 6836 case IPFGENITER_NAT : 6837 error = ipf_nat_getnext(softc, token, itp, obj); 6838 break; 6839 6840 case IPFGENITER_NATFRAG : 6841 error = ipf_frag_nat_next(softc, token, itp); 6842 break; 6843 default : 6844 IPFERROR(60053); 6845 error = EINVAL; 6846 break; 6847 } 6848 6849 return error; 6850 } 6851 6852 6853 /* ------------------------------------------------------------------------ */ 6854 /* Function: ipf_nat_setpending */ 6855 /* Returns: Nil */ 6856 /* Parameters: softc(I) - pointer to soft context main structure */ 6857 /* nat(I) - pointer to NAT structure */ 6858 /* Locks: ipf_nat (read or write) */ 6859 /* */ 6860 /* Put the NAT entry on to the pending queue - this queue has a very short */ 6861 /* lifetime where items are put that can't be deleted straight away because */ 6862 /* of locking issues but we want to delete them ASAP, anyway. In calling */ 6863 /* this function, it is assumed that the owner (if there is one, as shown */ 6864 /* by nat_me) is no longer interested in it. */ 6865 /* ------------------------------------------------------------------------ */ 6866 void 6867 ipf_nat_setpending(ipf_main_softc_t *softc, nat_t *nat) 6868 { 6869 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 6870 ipftq_t *oifq; 6871 6872 oifq = nat->nat_tqe.tqe_ifq; 6873 if (oifq != NULL) 6874 ipf_movequeue(softc->ipf_ticks, &nat->nat_tqe, oifq, 6875 &softn->ipf_nat_pending); 6876 else 6877 ipf_queueappend(softc->ipf_ticks, &nat->nat_tqe, 6878 &softn->ipf_nat_pending, nat); 6879 6880 if (nat->nat_me != NULL) { 6881 *nat->nat_me = NULL; 6882 nat->nat_me = NULL; 6883 nat->nat_ref--; 6884 ASSERT(nat->nat_ref >= 0); 6885 } 6886 } 6887 6888 6889 /* ------------------------------------------------------------------------ */ 6890 /* Function: nat_newrewrite */ 6891 /* Returns: int - -1 == error, 0 == success (no move), 1 == success and */ 6892 /* allow rule to be moved if IPN_ROUNDR is set. */ 6893 /* Parameters: fin(I) - pointer to packet information */ 6894 /* nat(I) - pointer to NAT entry */ 6895 /* ni(I) - pointer to structure with misc. information needed */ 6896 /* to create new NAT entry. */ 6897 /* Write Lock: ipf_nat */ 6898 /* */ 6899 /* This function is responsible for setting up an active NAT session where */ 6900 /* we are changing both the source and destination parameters at the same */ 6901 /* time. The loop in here works differently to elsewhere - each iteration */ 6902 /* is responsible for changing a single parameter that can be incremented. */ 6903 /* So one pass may increase the source IP#, next source port, next dest. IP#*/ 6904 /* and the last destination port for a total of 4 iterations to try each. */ 6905 /* This is done to try and exhaustively use the translation space available.*/ 6906 /* ------------------------------------------------------------------------ */ 6907 static int 6908 ipf_nat_newrewrite(fr_info_t *fin, nat_t *nat, natinfo_t *nai) 6909 { 6910 int src_search = 1; 6911 int dst_search = 1; 6912 fr_info_t frnat; 6913 u_32_t flags; 6914 u_short swap; 6915 ipnat_t *np; 6916 nat_t *natl; 6917 int l = 0; 6918 int changed; 6919 6920 natl = NULL; 6921 changed = -1; 6922 np = nai->nai_np; 6923 flags = nat->nat_flags; 6924 bcopy((char *)fin, (char *)&frnat, sizeof(*fin)); 6925 6926 nat->nat_hm = NULL; 6927 6928 do { 6929 changed = -1; 6930 /* TRACE (l, src_search, dst_search, np) */ 6931 6932 if ((src_search == 0) && (np->in_spnext == 0) && 6933 (dst_search == 0) && (np->in_dpnext == 0)) { 6934 if (l > 0) 6935 return -1; 6936 } 6937 6938 /* 6939 * Find a new source address 6940 */ 6941 if (ipf_nat_nextaddr(fin, &np->in_nsrc, &frnat.fin_saddr, 6942 &frnat.fin_saddr) == -1) { 6943 return -1; 6944 } 6945 6946 if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0xffffffff)) { 6947 src_search = 0; 6948 if (np->in_stepnext == 0) 6949 np->in_stepnext = 1; 6950 6951 } else if ((np->in_nsrcaddr == 0) && (np->in_nsrcmsk == 0)) { 6952 src_search = 0; 6953 if (np->in_stepnext == 0) 6954 np->in_stepnext = 1; 6955 6956 } else if (np->in_nsrcmsk == 0xffffffff) { 6957 src_search = 0; 6958 if (np->in_stepnext == 0) 6959 np->in_stepnext = 1; 6960 6961 } else if (np->in_nsrcmsk != 0xffffffff) { 6962 if (np->in_stepnext == 0 && changed == -1) { 6963 np->in_snip++; 6964 np->in_stepnext++; 6965 changed = 0; 6966 } 6967 } 6968 6969 if ((flags & IPN_TCPUDPICMP) != 0) { 6970 if (np->in_spnext != 0) 6971 frnat.fin_data[0] = np->in_spnext; 6972 6973 /* 6974 * Standard port translation. Select next port. 6975 */ 6976 if ((flags & IPN_FIXEDSPORT) != 0) { 6977 np->in_stepnext = 2; 6978 } else if ((np->in_stepnext == 1) && 6979 (changed == -1) && (natl != NULL)) { 6980 np->in_spnext++; 6981 np->in_stepnext++; 6982 changed = 1; 6983 if (np->in_spnext > np->in_spmax) 6984 np->in_spnext = np->in_spmin; 6985 } 6986 } else { 6987 np->in_stepnext = 2; 6988 } 6989 np->in_stepnext &= 0x3; 6990 6991 /* 6992 * Find a new destination address 6993 */ 6994 /* TRACE (fin, np, l, frnat) */ 6995 6996 if (ipf_nat_nextaddr(fin, &np->in_ndst, &frnat.fin_daddr, 6997 &frnat.fin_daddr) == -1) 6998 return -1; 6999 if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0xffffffff)) { 7000 dst_search = 0; 7001 if (np->in_stepnext == 2) 7002 np->in_stepnext = 3; 7003 7004 } else if ((np->in_ndstaddr == 0) && (np->in_ndstmsk == 0)) { 7005 dst_search = 0; 7006 if (np->in_stepnext == 2) 7007 np->in_stepnext = 3; 7008 7009 } else if (np->in_ndstmsk == 0xffffffff) { 7010 dst_search = 0; 7011 if (np->in_stepnext == 2) 7012 np->in_stepnext = 3; 7013 7014 } else if (np->in_ndstmsk != 0xffffffff) { 7015 if ((np->in_stepnext == 2) && (changed == -1) && 7016 (natl != NULL)) { 7017 changed = 2; 7018 np->in_stepnext++; 7019 np->in_dnip++; 7020 } 7021 } 7022 7023 if ((flags & IPN_TCPUDPICMP) != 0) { 7024 if (np->in_dpnext != 0) 7025 frnat.fin_data[1] = np->in_dpnext; 7026 7027 /* 7028 * Standard port translation. Select next port. 7029 */ 7030 if ((flags & IPN_FIXEDDPORT) != 0) { 7031 np->in_stepnext = 0; 7032 } else if (np->in_stepnext == 3 && changed == -1) { 7033 np->in_dpnext++; 7034 np->in_stepnext++; 7035 changed = 3; 7036 if (np->in_dpnext > np->in_dpmax) 7037 np->in_dpnext = np->in_dpmin; 7038 } 7039 } else { 7040 if (np->in_stepnext == 3) 7041 np->in_stepnext = 0; 7042 } 7043 7044 /* TRACE (frnat) */ 7045 7046 /* 7047 * Here we do a lookup of the connection as seen from 7048 * the outside. If an IP# pair already exists, try 7049 * again. So if you have A->B becomes C->B, you can 7050 * also have D->E become C->E but not D->B causing 7051 * another C->B. Also take protocol and ports into 7052 * account when determining whether a pre-existing 7053 * NAT setup will cause an external conflict where 7054 * this is appropriate. 7055 * 7056 * fin_data[] is swapped around because we are doing a 7057 * lookup of the packet is if it were moving in the opposite 7058 * direction of the one we are working with now. 7059 */ 7060 if (flags & IPN_TCPUDP) { 7061 swap = frnat.fin_data[0]; 7062 frnat.fin_data[0] = frnat.fin_data[1]; 7063 frnat.fin_data[1] = swap; 7064 } 7065 if (fin->fin_out == 1) { 7066 natl = ipf_nat_inlookup(&frnat, 7067 flags & ~(SI_WILDP|NAT_SEARCH), 7068 (u_int)frnat.fin_p, 7069 frnat.fin_dst, frnat.fin_src); 7070 7071 } else { 7072 natl = ipf_nat_outlookup(&frnat, 7073 flags & ~(SI_WILDP|NAT_SEARCH), 7074 (u_int)frnat.fin_p, 7075 frnat.fin_dst, frnat.fin_src); 7076 } 7077 if (flags & IPN_TCPUDP) { 7078 swap = frnat.fin_data[0]; 7079 frnat.fin_data[0] = frnat.fin_data[1]; 7080 frnat.fin_data[1] = swap; 7081 } 7082 7083 /* TRACE natl, in_stepnext, l */ 7084 7085 if ((natl != NULL) && (l > 8)) /* XXX 8 is arbitrary */ 7086 return -1; 7087 7088 np->in_stepnext &= 0x3; 7089 7090 l++; 7091 changed = -1; 7092 } while (natl != NULL); 7093 7094 nat->nat_osrcip = fin->fin_src; 7095 nat->nat_odstip = fin->fin_dst; 7096 nat->nat_nsrcip = frnat.fin_src; 7097 nat->nat_ndstip = frnat.fin_dst; 7098 7099 if ((flags & IPN_TCPUDP) != 0) { 7100 nat->nat_osport = htons(fin->fin_data[0]); 7101 nat->nat_odport = htons(fin->fin_data[1]); 7102 nat->nat_nsport = htons(frnat.fin_data[0]); 7103 nat->nat_ndport = htons(frnat.fin_data[1]); 7104 } else if ((flags & IPN_ICMPQUERY) != 0) { 7105 nat->nat_oicmpid = fin->fin_data[1]; 7106 nat->nat_nicmpid = frnat.fin_data[1]; 7107 } 7108 7109 return 0; 7110 } 7111 7112 7113 /* ------------------------------------------------------------------------ */ 7114 /* Function: nat_newdivert */ 7115 /* Returns: int - -1 == error, 0 == success */ 7116 /* Parameters: fin(I) - pointer to packet information */ 7117 /* nat(I) - pointer to NAT entry */ 7118 /* ni(I) - pointer to structure with misc. information needed */ 7119 /* to create new NAT entry. */ 7120 /* Write Lock: ipf_nat */ 7121 /* */ 7122 /* Create a new NAT divert session as defined by the NAT rule. This is */ 7123 /* somewhat different to other NAT session creation routines because we */ 7124 /* do not iterate through either port numbers or IP addresses, searching */ 7125 /* for a unique mapping, however, a complimentary duplicate check is made. */ 7126 /* ------------------------------------------------------------------------ */ 7127 static int 7128 ipf_nat_newdivert(fr_info_t *fin, nat_t *nat, natinfo_t *nai) 7129 { 7130 ipf_main_softc_t *softc = fin->fin_main_soft; 7131 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 7132 fr_info_t frnat; 7133 ipnat_t *np; 7134 nat_t *natl; 7135 int p; 7136 7137 np = nai->nai_np; 7138 bcopy((char *)fin, (char *)&frnat, sizeof(*fin)); 7139 7140 nat->nat_pr[0] = 0; 7141 nat->nat_osrcaddr = fin->fin_saddr; 7142 nat->nat_odstaddr = fin->fin_daddr; 7143 frnat.fin_saddr = htonl(np->in_snip); 7144 frnat.fin_daddr = htonl(np->in_dnip); 7145 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 7146 nat->nat_osport = htons(fin->fin_data[0]); 7147 nat->nat_odport = htons(fin->fin_data[1]); 7148 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) { 7149 nat->nat_oicmpid = fin->fin_data[1]; 7150 } 7151 7152 if (np->in_redir & NAT_DIVERTUDP) { 7153 frnat.fin_data[0] = np->in_spnext; 7154 frnat.fin_data[1] = np->in_dpnext; 7155 frnat.fin_flx |= FI_TCPUDP; 7156 p = IPPROTO_UDP; 7157 } else { 7158 frnat.fin_flx &= ~FI_TCPUDP; 7159 p = IPPROTO_IPIP; 7160 } 7161 7162 if (fin->fin_out == 1) { 7163 natl = ipf_nat_inlookup(&frnat, 0, p, 7164 frnat.fin_dst, frnat.fin_src); 7165 7166 } else { 7167 natl = ipf_nat_outlookup(&frnat, 0, p, 7168 frnat.fin_dst, frnat.fin_src); 7169 } 7170 7171 if (natl != NULL) { 7172 NBUMPSIDED(fin->fin_out, ns_divert_exist); 7173 return -1; 7174 } 7175 7176 nat->nat_nsrcaddr = frnat.fin_saddr; 7177 nat->nat_ndstaddr = frnat.fin_daddr; 7178 if ((nat->nat_flags & IPN_TCPUDP) != 0) { 7179 nat->nat_nsport = htons(frnat.fin_data[0]); 7180 nat->nat_ndport = htons(frnat.fin_data[1]); 7181 } else if ((nat->nat_flags & IPN_ICMPQUERY) != 0) { 7182 nat->nat_nicmpid = frnat.fin_data[1]; 7183 } 7184 7185 nat->nat_pr[fin->fin_out] = fin->fin_p; 7186 nat->nat_pr[1 - fin->fin_out] = p; 7187 7188 if (np->in_redir & NAT_REDIRECT) 7189 nat->nat_dir = NAT_DIVERTIN; 7190 else 7191 nat->nat_dir = NAT_DIVERTOUT; 7192 7193 return 0; 7194 } 7195 7196 7197 /* ------------------------------------------------------------------------ */ 7198 /* Function: nat_builddivertmp */ 7199 /* Returns: int - -1 == error, 0 == success */ 7200 /* Parameters: softn(I) - pointer to NAT context structure */ 7201 /* np(I) - pointer to a NAT rule */ 7202 /* */ 7203 /* For divert rules, a skeleton packet representing what will be prepended */ 7204 /* to the real packet is created. Even though we don't have the full */ 7205 /* packet here, a checksum is calculated that we update later when we */ 7206 /* fill in the final details. At present a 0 checksum for UDP is being set */ 7207 /* here because it is expected that divert will be used for localhost. */ 7208 /* ------------------------------------------------------------------------ */ 7209 static int 7210 ipf_nat_builddivertmp(ipf_nat_softc_t *softn, ipnat_t *np) 7211 { 7212 udphdr_t *uh; 7213 size_t len; 7214 ip_t *ip; 7215 7216 if ((np->in_redir & NAT_DIVERTUDP) != 0) 7217 len = sizeof(ip_t) + sizeof(udphdr_t); 7218 else 7219 len = sizeof(ip_t); 7220 7221 ALLOC_MB_T(np->in_divmp, len); 7222 if (np->in_divmp == NULL) { 7223 NBUMPD(ipf_nat_stats, ns_divert_build); 7224 return -1; 7225 } 7226 7227 /* 7228 * First, the header to get the packet diverted to the new destination 7229 */ 7230 ip = MTOD(np->in_divmp, ip_t *); 7231 IP_V_A(ip, 4); 7232 IP_HL_A(ip, 5); 7233 ip->ip_tos = 0; 7234 if ((np->in_redir & NAT_DIVERTUDP) != 0) 7235 ip->ip_p = IPPROTO_UDP; 7236 else 7237 ip->ip_p = IPPROTO_IPIP; 7238 ip->ip_ttl = 255; 7239 ip->ip_off = 0; 7240 ip->ip_sum = 0; 7241 ip->ip_len = htons(len); 7242 ip->ip_id = 0; 7243 ip->ip_src.s_addr = htonl(np->in_snip); 7244 ip->ip_dst.s_addr = htonl(np->in_dnip); 7245 ip->ip_sum = ipf_cksum((u_short *)ip, sizeof(*ip)); 7246 7247 if (np->in_redir & NAT_DIVERTUDP) { 7248 uh = (udphdr_t *)(ip + 1); 7249 uh->uh_sum = 0; 7250 uh->uh_ulen = 8; 7251 uh->uh_sport = htons(np->in_spnext); 7252 uh->uh_dport = htons(np->in_dpnext); 7253 } 7254 7255 return 0; 7256 } 7257 7258 7259 #define MINDECAP (sizeof(ip_t) + sizeof(udphdr_t) + sizeof(ip_t)) 7260 7261 /* ------------------------------------------------------------------------ */ 7262 /* Function: nat_decap */ 7263 /* Returns: int - -1 == error, 0 == success */ 7264 /* Parameters: fin(I) - pointer to packet information */ 7265 /* nat(I) - pointer to current NAT session */ 7266 /* */ 7267 /* This function is responsible for undoing a packet's encapsulation in the */ 7268 /* reverse of an encap/divert rule. After removing the outer encapsulation */ 7269 /* it is necessary to call ipf_makefrip() again so that the contents of 'fin'*/ 7270 /* match the "new" packet as it may still be used by IPFilter elsewhere. */ 7271 /* We use "dir" here as the basis for some of the expectations about the */ 7272 /* outer header. If we return an error, the goal is to leave the original */ 7273 /* packet information undisturbed - this falls short at the end where we'd */ 7274 /* need to back a backup copy of "fin" - expensive. */ 7275 /* ------------------------------------------------------------------------ */ 7276 static int 7277 ipf_nat_decap(fr_info_t *fin, nat_t *nat) 7278 { 7279 ipf_main_softc_t *softc = fin->fin_main_soft; 7280 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 7281 char *hdr; 7282 int hlen; 7283 int skip; 7284 mb_t *m; 7285 7286 if ((fin->fin_flx & FI_ICMPERR) != 0) { 7287 /* 7288 * ICMP packets don't get decapsulated, instead what we need 7289 * to do is change the ICMP reply from including (in the data 7290 * portion for errors) the encapsulated packet that we sent 7291 * out to something that resembles the original packet prior 7292 * to encapsulation. This isn't done here - all we're doing 7293 * here is changing the outer address to ensure that it gets 7294 * targetted back to the correct system. 7295 */ 7296 7297 if (nat->nat_dir & NAT_OUTBOUND) { 7298 u_32_t sum1, sum2, sumd; 7299 7300 sum1 = ntohl(fin->fin_daddr); 7301 sum2 = ntohl(nat->nat_osrcaddr); 7302 CALC_SUMD(sum1, sum2, sumd); 7303 fin->fin_ip->ip_dst = nat->nat_osrcip; 7304 fin->fin_daddr = nat->nat_osrcaddr; 7305 #if !defined(_KERNEL) || defined(MENTAT) || defined(__sgi) || \ 7306 defined(__osf__) || defined(linux) 7307 ipf_fix_outcksum(0, &fin->fin_ip->ip_sum, sumd, 0); 7308 #endif 7309 } 7310 return 0; 7311 } 7312 7313 m = fin->fin_m; 7314 skip = fin->fin_hlen; 7315 7316 switch (nat->nat_dir) 7317 { 7318 case NAT_DIVERTIN : 7319 case NAT_DIVERTOUT : 7320 if (fin->fin_plen < MINDECAP) 7321 return -1; 7322 skip += sizeof(udphdr_t); 7323 break; 7324 7325 case NAT_ENCAPIN : 7326 case NAT_ENCAPOUT : 7327 if (fin->fin_plen < (skip + sizeof(ip_t))) 7328 return -1; 7329 break; 7330 default : 7331 return -1; 7332 /* NOTREACHED */ 7333 } 7334 7335 /* 7336 * The aim here is to keep the original packet details in "fin" for 7337 * as long as possible so that returning with an error is for the 7338 * original packet and there is little undoing work to do. 7339 */ 7340 if (M_LEN(m) < skip + sizeof(ip_t)) { 7341 if (ipf_pr_pullup(fin, skip + sizeof(ip_t)) == -1) 7342 return -1; 7343 } 7344 7345 hdr = MTOD(fin->fin_m, char *); 7346 fin->fin_ip = (ip_t *)(hdr + skip); 7347 hlen = IP_HL(fin->fin_ip) << 2; 7348 7349 if (ipf_pr_pullup(fin, skip + hlen) == -1) { 7350 NBUMPSIDED(fin->fin_out, ns_decap_pullup); 7351 return -1; 7352 } 7353 7354 fin->fin_hlen = hlen; 7355 fin->fin_dlen -= skip; 7356 fin->fin_plen -= skip; 7357 fin->fin_ipoff += skip; 7358 7359 if (ipf_makefrip(hlen, (ip_t *)hdr, fin) == -1) { 7360 NBUMPSIDED(fin->fin_out, ns_decap_bad); 7361 return -1; 7362 } 7363 7364 return skip; 7365 } 7366 7367 7368 /* ------------------------------------------------------------------------ */ 7369 /* Function: nat_nextaddr */ 7370 /* Returns: int - -1 == bad input (no new address), */ 7371 /* 0 == success and dst has new address */ 7372 /* Parameters: fin(I) - pointer to packet information */ 7373 /* na(I) - how to generate new address */ 7374 /* old(I) - original address being replaced */ 7375 /* dst(O) - where to put the new address */ 7376 /* Write Lock: ipf_nat */ 7377 /* */ 7378 /* This function uses the contents of the "na" structure, in combination */ 7379 /* with "old" to produce a new address to store in "dst". Not all of the */ 7380 /* possible uses of "na" will result in a new address. */ 7381 /* ------------------------------------------------------------------------ */ 7382 static int 7383 ipf_nat_nextaddr(fr_info_t *fin, nat_addr_t *na, u_32_t *old, u_32_t *dst) 7384 { 7385 ipf_main_softc_t *softc = fin->fin_main_soft; 7386 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 7387 u_32_t amin, amax, new; 7388 i6addr_t newip; 7389 int error; 7390 7391 new = 0; 7392 amin = na->na_addr[0].in4.s_addr; 7393 7394 switch (na->na_atype) 7395 { 7396 case FRI_RANGE : 7397 amax = na->na_addr[1].in4.s_addr; 7398 break; 7399 7400 case FRI_NETMASKED : 7401 case FRI_DYNAMIC : 7402 case FRI_NORMAL : 7403 /* 7404 * Compute the maximum address by adding the inverse of the 7405 * netmask to the minimum address. 7406 */ 7407 amax = ~na->na_addr[1].in4.s_addr; 7408 amax |= amin; 7409 break; 7410 7411 case FRI_LOOKUP : 7412 break; 7413 7414 case FRI_BROADCAST : 7415 case FRI_PEERADDR : 7416 case FRI_NETWORK : 7417 default : 7418 return -1; 7419 } 7420 7421 error = -1; 7422 7423 if (na->na_atype == FRI_LOOKUP) { 7424 if (na->na_type == IPLT_DSTLIST) { 7425 error = ipf_dstlist_select_node(fin, na->na_ptr, dst, 7426 NULL); 7427 } else { 7428 NBUMPSIDE(fin->fin_out, ns_badnextaddr); 7429 } 7430 7431 } else if (na->na_atype == IPLT_NONE) { 7432 /* 7433 * 0/0 as the new address means leave it alone. 7434 */ 7435 if (na->na_addr[0].in4.s_addr == 0 && 7436 na->na_addr[1].in4.s_addr == 0) { 7437 new = *old; 7438 7439 /* 7440 * 0/32 means get the interface's address 7441 */ 7442 } else if (na->na_addr[0].in4.s_addr == 0 && 7443 na->na_addr[1].in4.s_addr == 0xffffffff) { 7444 if (ipf_ifpaddr(softc, 4, na->na_atype, 7445 fin->fin_ifp, &newip, NULL) == -1) { 7446 NBUMPSIDED(fin->fin_out, ns_ifpaddrfail); 7447 return -1; 7448 } 7449 new = newip.in4.s_addr; 7450 } else { 7451 new = htonl(na->na_nextip); 7452 } 7453 *dst = new; 7454 error = 0; 7455 7456 } else { 7457 NBUMPSIDE(fin->fin_out, ns_badnextaddr); 7458 } 7459 7460 return error; 7461 } 7462 7463 7464 /* ------------------------------------------------------------------------ */ 7465 /* Function: nat_nextaddrinit */ 7466 /* Returns: int - 0 == success, else error number */ 7467 /* Parameters: softc(I) - pointer to soft context main structure */ 7468 /* na(I) - NAT address information for generating new addr*/ 7469 /* initial(I) - flag indicating if it is the first call for */ 7470 /* this "na" structure. */ 7471 /* ifp(I) - network interface to derive address */ 7472 /* information from. */ 7473 /* */ 7474 /* This function is expected to be called in two scenarious: when a new NAT */ 7475 /* rule is loaded into the kernel and when the list of NAT rules is sync'd */ 7476 /* up with the valid network interfaces (possibly due to them changing.) */ 7477 /* To distinguish between these, the "initial" parameter is used. If it is */ 7478 /* 1 then this indicates the rule has just been reloaded and 0 for when we */ 7479 /* are updating information. This difference is important because in */ 7480 /* instances where we are not updating address information associated with */ 7481 /* a network interface, we don't want to disturb what the "next" address to */ 7482 /* come out of ipf_nat_nextaddr() will be. */ 7483 /* ------------------------------------------------------------------------ */ 7484 static int 7485 ipf_nat_nextaddrinit(ipf_main_softc_t *softc, char *base, nat_addr_t *na, 7486 int initial, void *ifp) 7487 { 7488 7489 switch (na->na_atype) 7490 { 7491 case FRI_LOOKUP : 7492 if (na->na_subtype == 0) { 7493 na->na_ptr = ipf_lookup_res_num(softc, IPL_LOGNAT, 7494 na->na_type, 7495 na->na_num, 7496 &na->na_func); 7497 } else if (na->na_subtype == 1) { 7498 na->na_ptr = ipf_lookup_res_name(softc, IPL_LOGNAT, 7499 na->na_type, 7500 base + na->na_num, 7501 &na->na_func); 7502 } 7503 if (na->na_func == NULL) { 7504 IPFERROR(60060); 7505 return ESRCH; 7506 } 7507 if (na->na_ptr == NULL) { 7508 IPFERROR(60056); 7509 return ESRCH; 7510 } 7511 break; 7512 7513 case FRI_DYNAMIC : 7514 case FRI_BROADCAST : 7515 case FRI_NETWORK : 7516 case FRI_NETMASKED : 7517 case FRI_PEERADDR : 7518 if (ifp != NULL) 7519 (void )ipf_ifpaddr(softc, 4, na->na_atype, ifp, 7520 &na->na_addr[0], &na->na_addr[1]); 7521 break; 7522 7523 case FRI_SPLIT : 7524 case FRI_RANGE : 7525 if (initial) 7526 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr); 7527 break; 7528 7529 case FRI_NONE : 7530 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr; 7531 return 0; 7532 7533 case FRI_NORMAL : 7534 na->na_addr[0].in4.s_addr &= na->na_addr[1].in4.s_addr; 7535 break; 7536 7537 default : 7538 IPFERROR(60054); 7539 return EINVAL; 7540 } 7541 7542 if (initial && (na->na_atype == FRI_NORMAL)) { 7543 if (na->na_addr[0].in4.s_addr == 0) { 7544 if ((na->na_addr[1].in4.s_addr == 0xffffffff) || 7545 (na->na_addr[1].in4.s_addr == 0)) { 7546 return 0; 7547 } 7548 } 7549 7550 if (na->na_addr[1].in4.s_addr == 0xffffffff) { 7551 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr); 7552 } else { 7553 na->na_nextip = ntohl(na->na_addr[0].in4.s_addr) + 1; 7554 } 7555 } 7556 7557 return 0; 7558 } 7559 7560 7561 /* ------------------------------------------------------------------------ */ 7562 /* Function: ipf_nat_matchflush */ 7563 /* Returns: int - -1 == error, 0 == success */ 7564 /* Parameters: softc(I) - pointer to soft context main structure */ 7565 /* softn(I) - pointer to NAT context structure */ 7566 /* nat(I) - pointer to current NAT session */ 7567 /* */ 7568 /* ------------------------------------------------------------------------ */ 7569 static int 7570 ipf_nat_matchflush(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, void *data) 7571 { 7572 int *array, flushed, error; 7573 nat_t *nat, *natnext; 7574 ipfobj_t obj; 7575 7576 error = ipf_matcharray_load(softc, data, &obj, &array); 7577 if (error != 0) 7578 return error; 7579 7580 flushed = 0; 7581 7582 for (nat = softn->ipf_nat_instances; nat != NULL; nat = natnext) { 7583 natnext = nat->nat_next; 7584 if (ipf_nat_matcharray(nat, array, softc->ipf_ticks) == 0) { 7585 ipf_nat_delete(softc, nat, NL_FLUSH); 7586 flushed++; 7587 } 7588 } 7589 7590 obj.ipfo_retval = flushed; 7591 error = BCOPYOUT(&obj, data, sizeof(obj)); 7592 7593 KFREES(array, array[0] * sizeof(*array)); 7594 7595 return error; 7596 } 7597 7598 7599 /* ------------------------------------------------------------------------ */ 7600 /* Function: ipf_nat_matcharray */ 7601 /* Returns: int - -1 == error, 0 == success */ 7602 /* Parameters: fin(I) - pointer to packet information */ 7603 /* nat(I) - pointer to current NAT session */ 7604 /* */ 7605 /* ------------------------------------------------------------------------ */ 7606 static int 7607 ipf_nat_matcharray(nat_t *nat, int *array, u_long ticks) 7608 { 7609 int i, n, *x, e, p; 7610 7611 e = 0; 7612 n = array[0]; 7613 x = array + 1; 7614 7615 for (; n > 0; x += 3 + x[2]) { 7616 if (x[0] == IPF_EXP_END) 7617 break; 7618 e = 0; 7619 7620 n -= x[2] + 3; 7621 if (n < 0) 7622 break; 7623 7624 p = x[0] >> 16; 7625 if (p != 0 && p != nat->nat_pr[1]) 7626 break; 7627 7628 switch (x[0]) 7629 { 7630 case IPF_EXP_IP_PR : 7631 for (i = 0; !e && i < x[2]; i++) { 7632 e |= (nat->nat_pr[1] == x[i + 3]); 7633 } 7634 break; 7635 7636 case IPF_EXP_IP_SRCADDR : 7637 if (nat->nat_v[0] == 4) { 7638 for (i = 0; !e && i < x[2]; i++) { 7639 e |= ((nat->nat_osrcaddr & x[i + 4]) == 7640 x[i + 3]); 7641 } 7642 } 7643 if (nat->nat_v[1] == 4) { 7644 for (i = 0; !e && i < x[2]; i++) { 7645 e |= ((nat->nat_nsrcaddr & x[i + 4]) == 7646 x[i + 3]); 7647 } 7648 } 7649 break; 7650 7651 case IPF_EXP_IP_DSTADDR : 7652 if (nat->nat_v[0] == 4) { 7653 for (i = 0; !e && i < x[2]; i++) { 7654 e |= ((nat->nat_odstaddr & x[i + 4]) == 7655 x[i + 3]); 7656 } 7657 } 7658 if (nat->nat_v[1] == 4) { 7659 for (i = 0; !e && i < x[2]; i++) { 7660 e |= ((nat->nat_ndstaddr & x[i + 4]) == 7661 x[i + 3]); 7662 } 7663 } 7664 break; 7665 7666 case IPF_EXP_IP_ADDR : 7667 for (i = 0; !e && i < x[2]; i++) { 7668 if (nat->nat_v[0] == 4) { 7669 e |= ((nat->nat_osrcaddr & x[i + 4]) == 7670 x[i + 3]); 7671 } 7672 if (nat->nat_v[1] == 4) { 7673 e |= ((nat->nat_nsrcaddr & x[i + 4]) == 7674 x[i + 3]); 7675 } 7676 if (nat->nat_v[0] == 4) { 7677 e |= ((nat->nat_odstaddr & x[i + 4]) == 7678 x[i + 3]); 7679 } 7680 if (nat->nat_v[1] == 4) { 7681 e |= ((nat->nat_ndstaddr & x[i + 4]) == 7682 x[i + 3]); 7683 } 7684 } 7685 break; 7686 7687 #ifdef USE_INET6 7688 case IPF_EXP_IP6_SRCADDR : 7689 if (nat->nat_v[0] == 6) { 7690 for (i = 0; !e && i < x[3]; i++) { 7691 e |= IP6_MASKEQ(&nat->nat_osrc6, 7692 x + i + 7, x + i + 3); 7693 } 7694 } 7695 if (nat->nat_v[1] == 6) { 7696 for (i = 0; !e && i < x[3]; i++) { 7697 e |= IP6_MASKEQ(&nat->nat_nsrc6, 7698 x + i + 7, x + i + 3); 7699 } 7700 } 7701 break; 7702 7703 case IPF_EXP_IP6_DSTADDR : 7704 if (nat->nat_v[0] == 6) { 7705 for (i = 0; !e && i < x[3]; i++) { 7706 e |= IP6_MASKEQ(&nat->nat_odst6, 7707 x + i + 7, 7708 x + i + 3); 7709 } 7710 } 7711 if (nat->nat_v[1] == 6) { 7712 for (i = 0; !e && i < x[3]; i++) { 7713 e |= IP6_MASKEQ(&nat->nat_ndst6, 7714 x + i + 7, 7715 x + i + 3); 7716 } 7717 } 7718 break; 7719 7720 case IPF_EXP_IP6_ADDR : 7721 for (i = 0; !e && i < x[3]; i++) { 7722 if (nat->nat_v[0] == 6) { 7723 e |= IP6_MASKEQ(&nat->nat_osrc6, 7724 x + i + 7, 7725 x + i + 3); 7726 } 7727 if (nat->nat_v[0] == 6) { 7728 e |= IP6_MASKEQ(&nat->nat_odst6, 7729 x + i + 7, 7730 x + i + 3); 7731 } 7732 if (nat->nat_v[1] == 6) { 7733 e |= IP6_MASKEQ(&nat->nat_nsrc6, 7734 x + i + 7, 7735 x + i + 3); 7736 } 7737 if (nat->nat_v[1] == 6) { 7738 e |= IP6_MASKEQ(&nat->nat_ndst6, 7739 x + i + 7, 7740 x + i + 3); 7741 } 7742 } 7743 break; 7744 #endif 7745 7746 case IPF_EXP_UDP_PORT : 7747 case IPF_EXP_TCP_PORT : 7748 for (i = 0; !e && i < x[2]; i++) { 7749 e |= (nat->nat_nsport == x[i + 3]) || 7750 (nat->nat_ndport == x[i + 3]); 7751 } 7752 break; 7753 7754 case IPF_EXP_UDP_SPORT : 7755 case IPF_EXP_TCP_SPORT : 7756 for (i = 0; !e && i < x[2]; i++) { 7757 e |= (nat->nat_nsport == x[i + 3]); 7758 } 7759 break; 7760 7761 case IPF_EXP_UDP_DPORT : 7762 case IPF_EXP_TCP_DPORT : 7763 for (i = 0; !e && i < x[2]; i++) { 7764 e |= (nat->nat_ndport == x[i + 3]); 7765 } 7766 break; 7767 7768 case IPF_EXP_TCP_STATE : 7769 for (i = 0; !e && i < x[2]; i++) { 7770 e |= (nat->nat_tcpstate[0] == x[i + 3]) || 7771 (nat->nat_tcpstate[1] == x[i + 3]); 7772 } 7773 break; 7774 7775 case IPF_EXP_IDLE_GT : 7776 e |= (ticks - nat->nat_touched > x[3]); 7777 break; 7778 } 7779 e ^= x[1]; 7780 7781 if (!e) 7782 break; 7783 } 7784 7785 return e; 7786 } 7787 7788 7789 /* ------------------------------------------------------------------------ */ 7790 /* Function: ipf_nat_gettable */ 7791 /* Returns: int - 0 = success, else error */ 7792 /* Parameters: softc(I) - pointer to soft context main structure */ 7793 /* softn(I) - pointer to NAT context structure */ 7794 /* data(I) - pointer to ioctl data */ 7795 /* */ 7796 /* This function handles ioctl requests for tables of nat information. */ 7797 /* At present the only table it deals with is the hash bucket statistics. */ 7798 /* ------------------------------------------------------------------------ */ 7799 static int 7800 ipf_nat_gettable(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, char *data) 7801 { 7802 ipftable_t table; 7803 int error; 7804 7805 error = ipf_inobj(softc, data, NULL, &table, IPFOBJ_GTABLE); 7806 if (error != 0) 7807 return error; 7808 7809 switch (table.ita_type) 7810 { 7811 case IPFTABLE_BUCKETS_NATIN : 7812 error = COPYOUT(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, 7813 table.ita_table, 7814 softn->ipf_nat_table_sz * sizeof(u_int)); 7815 break; 7816 7817 case IPFTABLE_BUCKETS_NATOUT : 7818 error = COPYOUT(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, 7819 table.ita_table, 7820 softn->ipf_nat_table_sz * sizeof(u_int)); 7821 break; 7822 7823 default : 7824 IPFERROR(60058); 7825 return EINVAL; 7826 } 7827 7828 if (error != 0) { 7829 IPFERROR(60059); 7830 error = EFAULT; 7831 } 7832 return error; 7833 } 7834 7835 7836 /* ------------------------------------------------------------------------ */ 7837 /* Function: ipf_nat_settimeout */ 7838 /* Returns: int - 0 = success, else failure */ 7839 /* Parameters: softc(I) - pointer to soft context main structure */ 7840 /* t(I) - pointer to tunable */ 7841 /* p(I) - pointer to new tuning data */ 7842 /* */ 7843 /* Apply the timeout change to the NAT timeout queues. */ 7844 /* ------------------------------------------------------------------------ */ 7845 int 7846 ipf_nat_settimeout(struct ipf_main_softc_s *softc, ipftuneable_t *t, 7847 ipftuneval_t *p) 7848 { 7849 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 7850 7851 if (!strncmp(t->ipft_name, "tcp_", 4)) 7852 return ipf_settimeout_tcp(t, p, softn->ipf_nat_tcptq); 7853 7854 if (!strcmp(t->ipft_name, "udp_timeout")) { 7855 ipf_apply_timeout(&softn->ipf_nat_udptq, p->ipftu_int); 7856 } else if (!strcmp(t->ipft_name, "udp_ack_timeout")) { 7857 ipf_apply_timeout(&softn->ipf_nat_udpacktq, p->ipftu_int); 7858 } else if (!strcmp(t->ipft_name, "icmp_timeout")) { 7859 ipf_apply_timeout(&softn->ipf_nat_icmptq, p->ipftu_int); 7860 } else if (!strcmp(t->ipft_name, "icmp_ack_timeout")) { 7861 ipf_apply_timeout(&softn->ipf_nat_icmpacktq, p->ipftu_int); 7862 } else if (!strcmp(t->ipft_name, "ip_timeout")) { 7863 ipf_apply_timeout(&softn->ipf_nat_iptq, p->ipftu_int); 7864 } else { 7865 IPFERROR(60062); 7866 return ESRCH; 7867 } 7868 return 0; 7869 } 7870 7871 7872 /* ------------------------------------------------------------------------ */ 7873 /* Function: ipf_nat_rehash */ 7874 /* Returns: int - 0 = success, else failure */ 7875 /* Parameters: softc(I) - pointer to soft context main structure */ 7876 /* t(I) - pointer to tunable */ 7877 /* p(I) - pointer to new tuning data */ 7878 /* */ 7879 /* To change the size of the basic NAT table, we need to first allocate the */ 7880 /* new tables (lest it fails and we've got nowhere to store all of the NAT */ 7881 /* sessions currently active) and then walk through the entire list and */ 7882 /* insert them into the table. There are two tables here: an inbound one */ 7883 /* and an outbound one. Each NAT entry goes into each table once. */ 7884 /* ------------------------------------------------------------------------ */ 7885 int 7886 ipf_nat_rehash(ipf_main_softc_t *softc, ipftuneable_t *t, ipftuneval_t *p) 7887 { 7888 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 7889 nat_t **newtab[2], *nat, **natp; 7890 u_int *bucketlens[2]; 7891 u_int maxbucket; 7892 u_int newsize; 7893 int error; 7894 u_int hv; 7895 int i; 7896 7897 newsize = p->ipftu_int; 7898 /* 7899 * In case there is nothing to do... 7900 */ 7901 if (newsize == softn->ipf_nat_table_sz) 7902 return 0; 7903 7904 newtab[0] = NULL; 7905 newtab[1] = NULL; 7906 bucketlens[0] = NULL; 7907 bucketlens[1] = NULL; 7908 /* 7909 * 4 tables depend on the NAT table size: the inbound looking table, 7910 * the outbound lookup table and the hash chain length for each. 7911 */ 7912 KMALLOCS(newtab[0], nat_t **, newsize * sizeof(nat_t *)); 7913 if (newtab == NULL) { 7914 error = 60063; 7915 goto badrehash; 7916 } 7917 7918 KMALLOCS(newtab[1], nat_t **, newsize * sizeof(nat_t *)); 7919 if (newtab == NULL) { 7920 error = 60064; 7921 goto badrehash; 7922 } 7923 7924 KMALLOCS(bucketlens[0], u_int *, newsize * sizeof(u_int)); 7925 if (bucketlens[0] == NULL) { 7926 error = 60065; 7927 goto badrehash; 7928 } 7929 7930 KMALLOCS(bucketlens[1], u_int *, newsize * sizeof(u_int)); 7931 if (bucketlens[1] == NULL) { 7932 error = 60066; 7933 goto badrehash; 7934 } 7935 7936 /* 7937 * Recalculate the maximum length based on the new size. 7938 */ 7939 for (maxbucket = 0, i = newsize; i > 0; i >>= 1) 7940 maxbucket++; 7941 maxbucket *= 2; 7942 7943 bzero((char *)newtab[0], newsize * sizeof(nat_t *)); 7944 bzero((char *)newtab[1], newsize * sizeof(nat_t *)); 7945 bzero((char *)bucketlens[0], newsize * sizeof(u_int)); 7946 bzero((char *)bucketlens[1], newsize * sizeof(u_int)); 7947 7948 WRITE_ENTER(&softc->ipf_nat); 7949 7950 if (softn->ipf_nat_table[0] != NULL) { 7951 KFREES(softn->ipf_nat_table[0], 7952 softn->ipf_nat_table_sz * 7953 sizeof(*softn->ipf_nat_table[0])); 7954 } 7955 softn->ipf_nat_table[0] = newtab[0]; 7956 7957 if (softn->ipf_nat_table[1] != NULL) { 7958 KFREES(softn->ipf_nat_table[1], 7959 softn->ipf_nat_table_sz * 7960 sizeof(*softn->ipf_nat_table[1])); 7961 } 7962 softn->ipf_nat_table[1] = newtab[1]; 7963 7964 if (softn->ipf_nat_stats.ns_side[0].ns_bucketlen != NULL) { 7965 KFREES(softn->ipf_nat_stats.ns_side[0].ns_bucketlen, 7966 softn->ipf_nat_table_sz * sizeof(u_int)); 7967 } 7968 softn->ipf_nat_stats.ns_side[0].ns_bucketlen = bucketlens[0]; 7969 7970 if (softn->ipf_nat_stats.ns_side[1].ns_bucketlen != NULL) { 7971 KFREES(softn->ipf_nat_stats.ns_side[1].ns_bucketlen, 7972 softn->ipf_nat_table_sz * sizeof(u_int)); 7973 } 7974 softn->ipf_nat_stats.ns_side[1].ns_bucketlen = bucketlens[1]; 7975 7976 softn->ipf_nat_maxbucket = maxbucket; 7977 softn->ipf_nat_table_sz = newsize; 7978 /* 7979 * Walk through the entire list of NAT table entries and put them 7980 * in the new NAT table, somewhere. Because we have a new table, 7981 * we need to restart the counter of how many chains are in use. 7982 */ 7983 softn->ipf_nat_stats.ns_side[0].ns_inuse = 0; 7984 softn->ipf_nat_stats.ns_side[1].ns_inuse = 0; 7985 7986 for (nat = softn->ipf_nat_instances; nat != NULL; nat = nat->nat_next) { 7987 nat->nat_hnext[0] = NULL; 7988 nat->nat_phnext[0] = NULL; 7989 hv = nat->nat_hv[0] % softn->ipf_nat_table_sz; 7990 7991 natp = &softn->ipf_nat_table[0][hv]; 7992 if (*natp) { 7993 (*natp)->nat_phnext[0] = &nat->nat_hnext[0]; 7994 } else { 7995 NBUMPSIDE(0, ns_inuse); 7996 } 7997 nat->nat_phnext[0] = natp; 7998 nat->nat_hnext[0] = *natp; 7999 *natp = nat; 8000 NBUMPSIDE(0, ns_bucketlen[hv]); 8001 8002 nat->nat_hnext[1] = NULL; 8003 nat->nat_phnext[1] = NULL; 8004 hv = nat->nat_hv[1] % softn->ipf_nat_table_sz; 8005 8006 natp = &softn->ipf_nat_table[1][hv]; 8007 if (*natp) { 8008 (*natp)->nat_phnext[1] = &nat->nat_hnext[1]; 8009 } else { 8010 NBUMPSIDE(1, ns_inuse); 8011 } 8012 nat->nat_phnext[1] = natp; 8013 nat->nat_hnext[1] = *natp; 8014 *natp = nat; 8015 NBUMPSIDE(1, ns_bucketlen[hv]); 8016 } 8017 RWLOCK_EXIT(&softc->ipf_nat); 8018 8019 return 0; 8020 8021 badrehash: 8022 if (bucketlens[1] != NULL) { 8023 KFREES(bucketlens[0], newsize * sizeof(u_int)); 8024 } 8025 if (bucketlens[0] != NULL) { 8026 KFREES(bucketlens[0], newsize * sizeof(u_int)); 8027 } 8028 if (newtab[0] != NULL) { 8029 KFREES(newtab[0], newsize * sizeof(nat_t *)); 8030 } 8031 if (newtab[1] != NULL) { 8032 KFREES(newtab[1], newsize * sizeof(nat_t *)); 8033 } 8034 IPFERROR(error); 8035 return ENOMEM; 8036 } 8037 8038 8039 /* ------------------------------------------------------------------------ */ 8040 /* Function: ipf_nat_rehash_rules */ 8041 /* Returns: int - 0 = success, else failure */ 8042 /* Parameters: softc(I) - pointer to soft context main structure */ 8043 /* t(I) - pointer to tunable */ 8044 /* p(I) - pointer to new tuning data */ 8045 /* */ 8046 /* All of the NAT rules hang off of a hash table that is searched with a */ 8047 /* hash on address after the netmask is applied. There is a different table*/ 8048 /* for both inbound rules (rdr) and outbound (map.) The resizing will only */ 8049 /* affect one of these two tables. */ 8050 /* ------------------------------------------------------------------------ */ 8051 int 8052 ipf_nat_rehash_rules(ipf_main_softc_t *softc, ipftuneable_t *t, ipftuneval_t *p) 8053 { 8054 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 8055 ipnat_t **newtab, *np, ***old, **npp; 8056 u_int newsize; 8057 u_int mask; 8058 u_int hv; 8059 8060 newsize = p->ipftu_int; 8061 /* 8062 * In case there is nothing to do... 8063 */ 8064 if (newsize == *t->ipft_pint) 8065 return 0; 8066 8067 /* 8068 * All inbound rules have the NAT_REDIRECT bit set in in_redir and 8069 * all outbound rules have either NAT_MAP or MAT_MAPBLK set. 8070 * This if statement allows for some more generic code to be below, 8071 * rather than two huge gobs of code that almost do the same thing. 8072 */ 8073 if (t->ipft_pint == &softn->ipf_nat_rdrrules_sz) { 8074 old = &softn->ipf_nat_rdr_rules; 8075 mask = NAT_REDIRECT; 8076 } else { 8077 old = &softn->ipf_nat_map_rules; 8078 mask = NAT_MAP|NAT_MAPBLK; 8079 } 8080 8081 KMALLOCS(newtab, ipnat_t **, newsize * sizeof(ipnat_t *)); 8082 if (newtab == NULL) { 8083 IPFERROR(60067); 8084 return ENOMEM; 8085 } 8086 8087 bzero((char *)newtab, newsize * sizeof(ipnat_t *)); 8088 8089 WRITE_ENTER(&softc->ipf_nat); 8090 8091 if (*old != NULL) { 8092 KFREES(*old, *t->ipft_pint * sizeof(ipnat_t **)); 8093 } 8094 *old = newtab; 8095 *t->ipft_pint = newsize; 8096 8097 for (np = softn->ipf_nat_list; np != NULL; np = np->in_next) { 8098 if ((np->in_redir & mask) == 0) 8099 continue; 8100 8101 if (np->in_redir & NAT_REDIRECT) { 8102 np->in_rnext = NULL; 8103 hv = np->in_hv[0] % newsize; 8104 for (npp = newtab + hv; *npp != NULL; ) 8105 npp = &(*npp)->in_rnext; 8106 np->in_prnext = npp; 8107 *npp = np; 8108 } 8109 if (np->in_redir & NAT_MAP) { 8110 np->in_mnext = NULL; 8111 hv = np->in_hv[1] % newsize; 8112 for (npp = newtab + hv; *npp != NULL; ) 8113 npp = &(*npp)->in_mnext; 8114 np->in_pmnext = npp; 8115 *npp = np; 8116 } 8117 8118 } 8119 RWLOCK_EXIT(&softc->ipf_nat); 8120 8121 return 0; 8122 } 8123 8124 8125 /* ------------------------------------------------------------------------ */ 8126 /* Function: ipf_nat_hostmap_rehash */ 8127 /* Returns: int - 0 = success, else failure */ 8128 /* Parameters: softc(I) - pointer to soft context main structure */ 8129 /* t(I) - pointer to tunable */ 8130 /* p(I) - pointer to new tuning data */ 8131 /* */ 8132 /* Allocate and populate a new hash table that will contain a reference to */ 8133 /* all of the active IP# translations currently in place. */ 8134 /* ------------------------------------------------------------------------ */ 8135 int 8136 ipf_nat_hostmap_rehash(ipf_main_softc_t *softc, ipftuneable_t *t, 8137 ipftuneval_t *p) 8138 { 8139 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 8140 hostmap_t *hm, **newtab; 8141 u_int newsize; 8142 u_int hv; 8143 8144 newsize = p->ipftu_int; 8145 /* 8146 * In case there is nothing to do... 8147 */ 8148 if (newsize == *t->ipft_pint) 8149 return 0; 8150 8151 KMALLOCS(newtab, hostmap_t **, newsize * sizeof(hostmap_t *)); 8152 if (newtab == NULL) { 8153 IPFERROR(60068); 8154 return ENOMEM; 8155 } 8156 8157 bzero((char *)newtab, newsize * sizeof(hostmap_t *)); 8158 8159 WRITE_ENTER(&softc->ipf_nat); 8160 if (softn->ipf_hm_maptable != NULL) { 8161 KFREES(softn->ipf_hm_maptable, 8162 softn->ipf_nat_hostmap_sz * sizeof(hostmap_t *)); 8163 } 8164 softn->ipf_hm_maptable = newtab; 8165 softn->ipf_nat_hostmap_sz = newsize; 8166 8167 for (hm = softn->ipf_hm_maplist; hm != NULL; hm = hm->hm_next) { 8168 hv = hm->hm_hv % softn->ipf_nat_hostmap_sz; 8169 hm->hm_hnext = softn->ipf_hm_maptable[hv]; 8170 hm->hm_phnext = softn->ipf_hm_maptable + hv; 8171 if (softn->ipf_hm_maptable[hv] != NULL) 8172 softn->ipf_hm_maptable[hv]->hm_phnext = &hm->hm_hnext; 8173 softn->ipf_hm_maptable[hv] = hm; 8174 } 8175 RWLOCK_EXIT(&softc->ipf_nat); 8176 8177 return 0; 8178 } 8179 8180 8181 /* ------------------------------------------------------------------------ */ 8182 /* Function: ipf_nat_add_tq */ 8183 /* Parameters: softc(I) - pointer to soft context main structure */ 8184 /* */ 8185 /* ------------------------------------------------------------------------ */ 8186 ipftq_t * 8187 ipf_nat_add_tq(ipf_main_softc_t *softc, int ttl) 8188 { 8189 ipf_nat_softc_t *softs = softc->ipf_nat_soft; 8190 8191 return ipf_addtimeoutqueue(softc, &softs->ipf_nat_utqe, ttl); 8192 } 8193 8194 /* ------------------------------------------------------------------------ */ 8195 /* Function: ipf_nat_uncreate */ 8196 /* Returns: Nil */ 8197 /* Parameters: fin(I) - pointer to packet information */ 8198 /* */ 8199 /* This function is used to remove a NAT entry from the NAT table when we */ 8200 /* decide that the create was actually in error. It is thus assumed that */ 8201 /* fin_flx will have both FI_NATED and FI_NATNEW set. Because we're dealing */ 8202 /* with the translated packet (not the original), we have to reverse the */ 8203 /* lookup. Although doing the lookup is expensive (relatively speaking), it */ 8204 /* is not anticipated that this will be a frequent occurance for normal */ 8205 /* traffic patterns. */ 8206 /* ------------------------------------------------------------------------ */ 8207 void 8208 ipf_nat_uncreate(fr_info_t *fin) 8209 { 8210 ipf_main_softc_t *softc = fin->fin_main_soft; 8211 ipf_nat_softc_t *softn = softc->ipf_nat_soft; 8212 int nflags; 8213 nat_t *nat; 8214 8215 switch (fin->fin_p) 8216 { 8217 case IPPROTO_TCP : 8218 nflags = IPN_TCP; 8219 break; 8220 case IPPROTO_UDP : 8221 nflags = IPN_UDP; 8222 break; 8223 default : 8224 nflags = 0; 8225 break; 8226 } 8227 8228 WRITE_ENTER(&softc->ipf_nat); 8229 8230 if (fin->fin_out == 0) { 8231 nat = ipf_nat_outlookup(fin, nflags, (u_int)fin->fin_p, 8232 fin->fin_dst, fin->fin_src); 8233 } else { 8234 nat = ipf_nat_inlookup(fin, nflags, (u_int)fin->fin_p, 8235 fin->fin_src, fin->fin_dst); 8236 } 8237 8238 if (nat != NULL) { 8239 NBUMPSIDE(fin->fin_out, ns_uncreate[0]); 8240 ipf_nat_delete(softc, nat, NL_DESTROY); 8241 } else { 8242 NBUMPSIDE(fin->fin_out, ns_uncreate[1]); 8243 } 8244 8245 RWLOCK_EXIT(&softc->ipf_nat); 8246 } 8247 8248 8249 /* ------------------------------------------------------------------------ */ 8250 /* Function: ipf_nat_cmp_rules */ 8251 /* Returns: int - 0 == success, else rules do not match. */ 8252 /* Parameters: n1(I) - first rule to compare */ 8253 /* n2(I) - first rule to compare */ 8254 /* */ 8255 /* Compare two rules using pointers to each rule. A straight bcmp will not */ 8256 /* work as some fields (such as in_dst, in_pkts) actually do change once */ 8257 /* the rule has been loaded into the kernel. Whilst this function returns */ 8258 /* various non-zero returns, they're strictly to aid in debugging. Use of */ 8259 /* this function should simply care if the result is zero or not. */ 8260 /* ------------------------------------------------------------------------ */ 8261 static int 8262 ipf_nat_cmp_rules(ipnat_t *n1, ipnat_t *n2) 8263 { 8264 if (n1->in_size != n2->in_size) 8265 return 1; 8266 8267 if (bcmp((char *)&n1->in_v, (char *)&n2->in_v, 8268 offsetof(ipnat_t, in_ndst) - offsetof(ipnat_t, in_v)) != 0) 8269 return 2; 8270 8271 if (bcmp((char *)&n1->in_tuc, (char *)&n2->in_tuc, 8272 n1->in_size - offsetof(ipnat_t, in_tuc)) != 0) 8273 return 3; 8274 if (n1->in_ndst.na_atype != n2->in_ndst.na_atype) 8275 return 5; 8276 if (n1->in_ndst.na_function != n2->in_ndst.na_function) 8277 return 6; 8278 if (bcmp((char *)&n1->in_ndst.na_addr, (char *)&n2->in_ndst.na_addr, 8279 sizeof(n1->in_ndst.na_addr))) 8280 return 7; 8281 if (n1->in_nsrc.na_atype != n2->in_nsrc.na_atype) 8282 return 8; 8283 if (n1->in_nsrc.na_function != n2->in_nsrc.na_function) 8284 return 9; 8285 if (bcmp((char *)&n1->in_nsrc.na_addr, (char *)&n2->in_nsrc.na_addr, 8286 sizeof(n1->in_nsrc.na_addr))) 8287 return 10; 8288 if (n1->in_odst.na_atype != n2->in_odst.na_atype) 8289 return 11; 8290 if (n1->in_odst.na_function != n2->in_odst.na_function) 8291 return 12; 8292 if (bcmp((char *)&n1->in_odst.na_addr, (char *)&n2->in_odst.na_addr, 8293 sizeof(n1->in_odst.na_addr))) 8294 return 13; 8295 if (n1->in_osrc.na_atype != n2->in_osrc.na_atype) 8296 return 14; 8297 if (n1->in_osrc.na_function != n2->in_osrc.na_function) 8298 return 15; 8299 if (bcmp((char *)&n1->in_osrc.na_addr, (char *)&n2->in_osrc.na_addr, 8300 sizeof(n1->in_osrc.na_addr))) 8301 return 16; 8302 return 0; 8303 } 8304 8305 8306 /* ------------------------------------------------------------------------ */ 8307 /* Function: ipf_nat_rule_init */ 8308 /* Returns: int - 0 == success, else rules do not match. */ 8309 /* Parameters: softc(I) - pointer to soft context main structure */ 8310 /* softn(I) - pointer to NAT context structure */ 8311 /* n(I) - first rule to compare */ 8312 /* */ 8313 /* ------------------------------------------------------------------------ */ 8314 static int 8315 ipf_nat_rule_init(ipf_main_softc_t *softc, ipf_nat_softc_t *softn, ipnat_t *n) 8316 { 8317 int error = 0; 8318 8319 if ((n->in_flags & IPN_SIPRANGE) != 0) 8320 n->in_nsrcatype = FRI_RANGE; 8321 8322 if ((n->in_flags & IPN_DIPRANGE) != 0) 8323 n->in_ndstatype = FRI_RANGE; 8324 8325 if ((n->in_flags & IPN_SPLIT) != 0) 8326 n->in_ndstatype = FRI_SPLIT; 8327 8328 if ((n->in_redir & (NAT_MAP|NAT_REWRITE|NAT_DIVERTUDP)) != 0) 8329 n->in_spnext = n->in_spmin; 8330 8331 if ((n->in_redir & (NAT_REWRITE|NAT_DIVERTUDP)) != 0) { 8332 n->in_dpnext = n->in_dpmin; 8333 } else if (n->in_redir == NAT_REDIRECT) { 8334 n->in_dpnext = n->in_dpmin; 8335 } 8336 8337 n->in_stepnext = 0; 8338 8339 switch (n->in_v[0]) 8340 { 8341 case 4 : 8342 error = ipf_nat_ruleaddrinit(softc, softn, n); 8343 if (error != 0) 8344 return error; 8345 break; 8346 #ifdef USE_INET6 8347 case 6 : 8348 error = ipf_nat6_ruleaddrinit(softc, softn, n); 8349 if (error != 0) 8350 return error; 8351 break; 8352 #endif 8353 default : 8354 break; 8355 } 8356 8357 if (n->in_redir == (NAT_DIVERTUDP|NAT_MAP)) { 8358 /* 8359 * Prerecord whether or not the destination of the divert 8360 * is local or not to the interface the packet is going 8361 * to be sent out. 8362 */ 8363 n->in_dlocal = ipf_deliverlocal(softc, n->in_v[1], 8364 n->in_ifps[1], &n->in_ndstip6); 8365 } 8366 8367 return error; 8368 } 8369 8370 8371 /* ------------------------------------------------------------------------ */ 8372 /* Function: ipf_nat_rule_fini */ 8373 /* Returns: int - 0 == success, else rules do not match. */ 8374 /* Parameters: softc(I) - pointer to soft context main structure */ 8375 /* n(I) - rule to work on */ 8376 /* */ 8377 /* This function is used to release any objects that were referenced during */ 8378 /* the rule initialisation. This is useful both when free'ing the rule and */ 8379 /* when handling ioctls that need to initialise these fields but not */ 8380 /* actually use them after the ioctl processing has finished. */ 8381 /* ------------------------------------------------------------------------ */ 8382 static void 8383 ipf_nat_rule_fini(ipf_main_softc_t *softc, ipnat_t *n) 8384 { 8385 if (n->in_odst.na_atype == FRI_LOOKUP && n->in_odst.na_ptr != NULL) 8386 ipf_lookup_deref(softc, n->in_odst.na_type, n->in_odst.na_ptr); 8387 8388 if (n->in_osrc.na_atype == FRI_LOOKUP && n->in_osrc.na_ptr != NULL) 8389 ipf_lookup_deref(softc, n->in_osrc.na_type, n->in_osrc.na_ptr); 8390 8391 if (n->in_ndst.na_atype == FRI_LOOKUP && n->in_ndst.na_ptr != NULL) 8392 ipf_lookup_deref(softc, n->in_ndst.na_type, n->in_ndst.na_ptr); 8393 8394 if (n->in_nsrc.na_atype == FRI_LOOKUP && n->in_nsrc.na_ptr != NULL) 8395 ipf_lookup_deref(softc, n->in_nsrc.na_type, n->in_nsrc.na_ptr); 8396 8397 if (n->in_divmp != NULL) 8398 FREE_MB_T(n->in_divmp); 8399 } 8400