1 2 /* 3 * Copyright (C) 2012 by Darren Reed. 4 * 5 * See the IPFILTER.LICENCE file for details on licencing. 6 * 7 * @(#)ip_nat.h 1.5 2/4/96 8 * Id: ip_nat.h,v 2.90.2.20 2007/09/25 08:27:32 darrenr Exp $ 9 */ 10 11 #ifndef __IP_NAT_H__ 12 #define __IP_NAT_H__ 13 14 #ifndef SOLARIS 15 # if defined(sun) && defined(__SVR4) 16 # define SOLARIS 1 17 # else 18 # define SOLARIS 0 19 # endif 20 #endif 21 22 #define SIOCADNAT _IOW('r', 60, struct ipfobj) 23 #define SIOCRMNAT _IOW('r', 61, struct ipfobj) 24 #define SIOCGNATS _IOWR('r', 62, struct ipfobj) 25 #define SIOCGNATL _IOWR('r', 63, struct ipfobj) 26 #define SIOCPURGENAT _IOWR('r', 100, struct ipfobj) 27 28 #undef LARGE_NAT /* define this if you're setting up a system to NAT 29 * LARGE numbers of networks/hosts - i.e. in the 30 * hundreds or thousands. In such a case, you should 31 * also change the RDR_SIZE and NAT_SIZE below to more 32 * appropriate sizes. The figures below were used for 33 * a setup with 1000-2000 networks to NAT. 34 */ 35 #ifdef NAT_SIZE 36 # define NAT_SIZE_LARGE NAT_SIZE 37 # define NAT_SIZE_NORMAL NAT_SIZE 38 #else 39 # define NAT_SIZE_LARGE 2047 40 # define NAT_SIZE_NORMAL 127 41 #endif 42 #ifdef RDR_SIZE 43 # define RDR_SIZE_LARGE RDR_SIZE 44 # define RDR_SIZE_NORMAL RDR_SIZE 45 #else 46 # define RDR_SIZE_LARGE 2047 47 # define RDR_SIZE_NORMAL 127 48 #endif 49 #ifdef HOSTMAP_SIZE 50 # define HOSTMAP_SIZE_LARGE HOSTMAP_SIZE 51 # define HOSTMAP_SIZE_NORMAL HOSTMAP_SIZE 52 #else 53 # define HOSTMAP_SIZE_LARGE 8191 54 # define HOSTMAP_SIZE_NORMAL 2047 55 #endif 56 /* 57 * This is newly introduced and for the sake of "least surprise", the numbers 58 * present aren't what we'd normally use for creating a proper hash table. 59 */ 60 #ifdef NAT_TABLE_MAX 61 # define NAT_TABLE_MAX_LARGE NAT_TABLE_MAX 62 # define NAT_TABLE_MAX_NORMAL NAT_TABLE_MAX 63 #else 64 # define NAT_TABLE_MAX_LARGE 180000 65 # define NAT_TABLE_MAX_NORMAL 30000 66 #endif 67 #ifdef NAT_TABLE_SZ 68 # define NAT_TABLE_SZ_LARGE NAT_TABLE_SZ 69 # define NAT_TABLE_SZ_NORMAL NAT_TABLE_SZ 70 #else 71 # define NAT_TABLE_SZ_LARGE 16383 72 # define NAT_TABLE_SZ_NORMAL 2047 73 #endif 74 #ifndef APR_LABELLEN 75 #define APR_LABELLEN 16 76 #endif 77 #define NAT_HW_CKSUM 0x80000000 78 #define NAT_HW_CKSUM_PART 0x40000000 79 80 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */ 81 82 struct ipstate; 83 struct ap_session; 84 85 /* 86 * This structure is used in the active NAT table and represents an 87 * active NAT session. 88 */ 89 typedef struct nat { 90 ipfmutex_t nat_lock; 91 struct nat *nat_next; 92 struct nat **nat_pnext; 93 struct nat *nat_hnext[2]; 94 struct nat **nat_phnext[2]; 95 struct hostmap *nat_hm; 96 void *nat_data; 97 struct nat **nat_me; 98 struct ipstate *nat_state; 99 struct ap_session *nat_aps; /* proxy session */ 100 frentry_t *nat_fr; /* filter rule ptr if appropriate */ 101 struct ipnat *nat_ptr; /* pointer back to the rule */ 102 void *nat_ifps[2]; 103 void *nat_sync; 104 ipftqent_t nat_tqe; 105 int nat_mtu[2]; 106 u_32_t nat_flags; 107 u_32_t nat_sumd[2]; /* ip checksum delta for data segment*/ 108 u_32_t nat_ipsumd; /* ip checksum delta for ip header */ 109 u_32_t nat_mssclamp; /* if != zero clamp MSS to this */ 110 i6addr_t nat_odst6; 111 i6addr_t nat_osrc6; 112 i6addr_t nat_ndst6; 113 i6addr_t nat_nsrc6; 114 U_QUAD_T nat_pkts[2]; 115 U_QUAD_T nat_bytes[2]; 116 union { 117 udpinfo_t nat_unu; 118 tcpinfo_t nat_unt; 119 icmpinfo_t nat_uni; 120 greinfo_t nat_ugre; 121 } nat_unold, nat_unnew; 122 int nat_use; 123 int nat_pr[2]; /* protocol for NAT */ 124 int nat_dir; 125 int nat_ref; /* reference count */ 126 u_int nat_hv[2]; 127 char nat_ifnames[2][LIFNAMSIZ]; 128 int nat_rev; /* 0 = forward, 1 = reverse */ 129 int nat_dlocal; 130 int nat_v[2]; /* 0 = old, 1 = new */ 131 u_int nat_redir; /* copy of in_redir */ 132 } nat_t; 133 134 #define nat_osrcip nat_osrc6.in4 135 #define nat_odstip nat_odst6.in4 136 #define nat_nsrcip nat_nsrc6.in4 137 #define nat_ndstip nat_ndst6.in4 138 #define nat_osrcaddr nat_osrc6.in4.s_addr 139 #define nat_odstaddr nat_odst6.in4.s_addr 140 #define nat_nsrcaddr nat_nsrc6.in4.s_addr 141 #define nat_ndstaddr nat_ndst6.in4.s_addr 142 #define nat_age nat_tqe.tqe_die 143 #define nat_osport nat_unold.nat_unt.ts_sport 144 #define nat_odport nat_unold.nat_unt.ts_dport 145 #define nat_nsport nat_unnew.nat_unt.ts_sport 146 #define nat_ndport nat_unnew.nat_unt.ts_dport 147 #define nat_oicmpid nat_unold.nat_uni.ici_id 148 #define nat_nicmpid nat_unnew.nat_uni.ici_id 149 #define nat_type nat_unold.nat_uni.ici_type 150 #define nat_oseq nat_unold.nat_uni.ici_seq 151 #define nat_nseq nat_unnew.nat_uni.ici_seq 152 #define nat_tcpstate nat_tqe.tqe_state 153 #define nat_die nat_tqe.tqe_die 154 #define nat_touched nat_tqe.tqe_touched 155 156 /* 157 * Values for nat_dir 158 */ 159 #define NAT_INBOUND 0 160 #define NAT_OUTBOUND 1 161 #define NAT_ENCAPIN 2 162 #define NAT_ENCAPOUT 3 163 #define NAT_DIVERTIN 4 164 #define NAT_DIVERTOUT 5 165 166 /* 167 * Definitions for nat_flags 168 */ 169 #define NAT_TCP 0x0001 /* IPN_TCP */ 170 #define NAT_UDP 0x0002 /* IPN_UDP */ 171 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */ 172 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */ 173 #define NAT_SEARCH 0x0010 174 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */ 175 #define NAT_NOTRULEPORT 0x0040 /* Don't use the port # in the NAT rule */ 176 177 #define NAT_TCPUDP (NAT_TCP|NAT_UDP) 178 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR) 179 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY) 180 #define NAT_FROMRULE (NAT_TCP|NAT_UDP) 181 182 /* 0x0100 reserved for FI_W_SPORT */ 183 /* 0x0200 reserved for FI_W_DPORT */ 184 /* 0x0400 reserved for FI_W_SADDR */ 185 /* 0x0800 reserved for FI_W_DADDR */ 186 /* 0x1000 reserved for FI_W_NEWFR */ 187 /* 0x2000 reserved for SI_CLONE */ 188 /* 0x4000 reserved for SI_CLONED */ 189 /* 0x8000 reserved for SI_IGNOREPKT */ 190 191 #define NAT_DEBUG 0x800000 192 193 typedef struct nat_addr_s { 194 i6addr_t na_addr[2]; 195 i6addr_t na_nextaddr; 196 int na_atype; 197 int na_function; 198 } nat_addr_t; 199 200 #define na_nextip na_nextaddr.in4.s_addr 201 #define na_nextip6 na_nextaddr.in6 202 #define na_num na_addr[0].iplookupnum 203 #define na_type na_addr[0].iplookuptype 204 #define na_subtype na_addr[0].iplookupsubtype 205 #define na_ptr na_addr[1].iplookupptr 206 #define na_func na_addr[1].iplookupfunc 207 208 209 /* 210 * This structure represents an actual NAT rule, loaded by ipnat. 211 */ 212 typedef struct ipnat { 213 ipfmutex_t in_lock; 214 struct ipnat *in_next; /* NAT rule list next */ 215 struct ipnat **in_pnext; /* prior rdr next ptr */ 216 struct ipnat *in_rnext; /* rdr rule hash next */ 217 struct ipnat **in_prnext; /* prior rdr next ptr */ 218 struct ipnat *in_mnext; /* map rule hash next */ 219 struct ipnat **in_pmnext; /* prior map next ptr */ 220 struct ipftq *in_tqehead[2]; 221 void *in_ifps[2]; 222 void *in_apr; 223 char *in_comment; 224 mb_t *in_divmp; 225 void *in_pconf; 226 U_QUAD_T in_pkts[2]; 227 U_QUAD_T in_bytes[2]; 228 u_long in_space; 229 u_long in_hits; 230 int in_size; 231 int in_use; 232 u_int in_hv[2]; 233 int in_flineno; /* conf. file line number */ 234 int in_stepnext; 235 int in_dlocal; 236 u_short in_dpnext; 237 u_short in_spnext; 238 /* From here to the end is covered by IPN_CMPSIZ */ 239 u_char in_v[2]; /* 0 = old, 1 = new */ 240 u_32_t in_flags; 241 u_32_t in_mssclamp; /* if != 0 clamp MSS to this */ 242 u_int in_age[2]; 243 int in_redir; /* see below for values */ 244 int in_pr[2]; /* protocol. */ 245 nat_addr_t in_ndst; 246 nat_addr_t in_nsrc; 247 nat_addr_t in_osrc; 248 nat_addr_t in_odst; 249 frtuc_t in_tuc; 250 u_short in_ppip; /* ports per IP. */ 251 u_short in_ippip; /* IP #'s per IP# */ 252 u_short in_ndports[2]; 253 u_short in_nsports[2]; 254 int in_ifnames[2]; 255 int in_plabel; /* proxy label. */ 256 int in_pconfig; /* proxy label. */ 257 ipftag_t in_tag; 258 int in_namelen; 259 char in_names[1]; 260 } ipnat_t; 261 262 /* 263 * MAP-IN MAP-OUT RDR-IN RDR-OUT 264 * osrc X == src == src X 265 * odst X == dst == dst X 266 * nsrc == dst X X == dst 267 * ndst == src X X == src 268 */ 269 #define in_dpmin in_ndports[0] /* Also holds static redir port */ 270 #define in_dpmax in_ndports[1] 271 #define in_spmin in_nsports[0] /* Also holds static redir port */ 272 #define in_spmax in_nsports[1] 273 #define in_ndport in_ndports[0] 274 #define in_nsport in_nsports[0] 275 #define in_dipnext in_ndst.na_nextaddr.in4 276 #define in_dipnext6 in_ndst.na_nextaddr 277 #define in_dnip in_ndst.na_nextaddr.in4.s_addr 278 #define in_dnip6 in_ndst.na_nextaddr 279 #define in_sipnext in_nsrc.na_nextaddr.in4 280 #define in_snip in_nsrc.na_nextaddr.in4.s_addr 281 #define in_snip6 in_nsrc.na_nextaddr 282 #define in_odstip in_odst.na_addr[0].in4 283 #define in_odstip6 in_odst.na_addr[0] 284 #define in_odstaddr in_odst.na_addr[0].in4.s_addr 285 #define in_odstmsk in_odst.na_addr[1].in4.s_addr 286 #define in_odstmsk6 in_odst.na_addr[1] 287 #define in_odstatype in_odst.na_atype 288 #define in_osrcip in_osrc.na_addr[0].in4 289 #define in_osrcip6 in_osrc.na_addr[0] 290 #define in_osrcaddr in_osrc.na_addr[0].in4.s_addr 291 #define in_osrcmsk in_osrc.na_addr[1].in4.s_addr 292 #define in_osrcmsk6 in_osrc.na_addr[1] 293 #define in_osrcatype in_osrc.na_atype 294 #define in_ndstip in_ndst.na_addr[0].in4 295 #define in_ndstip6 in_ndst.na_addr[0] 296 #define in_ndstaddr in_ndst.na_addr[0].in4.s_addr 297 #define in_ndstmsk in_ndst.na_addr[1].in4.s_addr 298 #define in_ndstmsk6 in_ndst.na_addr[1] 299 #define in_ndstatype in_ndst.na_atype 300 #define in_ndstafunc in_ndst.na_function 301 #define in_nsrcip in_nsrc.na_addr[0].in4 302 #define in_nsrcip6 in_nsrc.na_addr[0] 303 #define in_nsrcaddr in_nsrc.na_addr[0].in4.s_addr 304 #define in_nsrcmsk in_nsrc.na_addr[1].in4.s_addr 305 #define in_nsrcmsk6 in_nsrc.na_addr[1] 306 #define in_nsrcatype in_nsrc.na_atype 307 #define in_nsrcafunc in_nsrc.na_function 308 #define in_scmp in_tuc.ftu_scmp 309 #define in_dcmp in_tuc.ftu_dcmp 310 #define in_stop in_tuc.ftu_stop 311 #define in_dtop in_tuc.ftu_dtop 312 #define in_osport in_tuc.ftu_sport 313 #define in_odport in_tuc.ftu_dport 314 #define in_ndstnum in_ndst.na_addr[0].iplookupnum 315 #define in_ndsttype in_ndst.na_addr[0].iplookuptype 316 #define in_ndstptr in_ndst.na_addr[1].iplookupptr 317 #define in_ndstfunc in_ndst.na_addr[1].iplookupfunc 318 #define in_nsrcnum in_nsrc.na_addr[0].iplookupnum 319 #define in_nsrctype in_nsrc.na_addr[0].iplookuptype 320 #define in_nsrcptr in_nsrc.na_addr[1].iplookupptr 321 #define in_nsrcfunc in_nsrc.na_addr[1].iplookupfunc 322 #define in_odstnum in_odst.na_addr[0].iplookupnum 323 #define in_odsttype in_odst.na_addr[0].iplookuptype 324 #define in_odstptr in_odst.na_addr[1].iplookupptr 325 #define in_odstfunc in_odst.na_addr[1].iplookupfunc 326 #define in_osrcnum in_osrc.na_addr[0].iplookupnum 327 #define in_osrctype in_osrc.na_addr[0].iplookuptype 328 #define in_osrcptr in_osrc.na_addr[1].iplookupptr 329 #define in_osrcfunc in_osrc.na_addr[1].iplookupfunc 330 #define in_icmpidmin in_nsports[0] 331 #define in_icmpidmax in_nsports[1] 332 333 /* 334 * Bit definitions for in_flags 335 */ 336 #define IPN_ANY 0x00000 337 #define IPN_TCP 0x00001 338 #define IPN_UDP 0x00002 339 #define IPN_TCPUDP (IPN_TCP|IPN_UDP) 340 #define IPN_ICMPERR 0x00004 341 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR) 342 #define IPN_ICMPQUERY 0x00008 343 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY) 344 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR) 345 #define IPN_AUTOPORTMAP 0x00010 346 #define IPN_FILTER 0x00020 347 #define IPN_SPLIT 0x00040 348 #define IPN_ROUNDR 0x00080 349 #define IPN_SIPRANGE 0x00100 350 #define IPN_DIPRANGE 0x00200 351 #define IPN_NOTSRC 0x00400 352 #define IPN_NOTDST 0x00800 353 #define IPN_NO 0x01000 354 #define IPN_DYNSRCIP 0x02000 /* dynamic src IP# */ 355 #define IPN_DYNDSTIP 0x04000 /* dynamic dst IP# */ 356 #define IPN_DELETE 0x08000 357 #define IPN_STICKY 0x10000 358 #define IPN_FRAG 0x20000 359 #define IPN_FIXEDSPORT 0x40000 360 #define IPN_FIXEDDPORT 0x80000 361 #define IPN_FINDFORWARD 0x100000 362 #define IPN_IN 0x200000 363 #define IPN_SEQUENTIAL 0x400000 364 #define IPN_PURGE 0x800000 365 #define IPN_PROXYRULE 0x1000000 366 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_SIPRANGE|IPN_SPLIT|\ 367 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|IPN_NO|\ 368 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\ 369 IPN_DIPRANGE|IPN_SEQUENTIAL|IPN_PURGE) 370 371 /* 372 * Values for in_redir 373 */ 374 #define NAT_MAP 0x01 375 #define NAT_REDIRECT 0x02 376 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT) 377 #define NAT_MAPBLK 0x04 378 #define NAT_REWRITE 0x08 379 #define NAT_ENCAP 0x10 380 #define NAT_DIVERTUDP 0x20 381 382 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */ 383 #define USABLE_PORTS (65536 - MAPBLK_MINPORT) 384 385 #define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_v)) 386 387 typedef struct natlookup { 388 i6addr_t nl_inipaddr; 389 i6addr_t nl_outipaddr; 390 i6addr_t nl_realipaddr; 391 int nl_v; 392 int nl_flags; 393 u_short nl_inport; 394 u_short nl_outport; 395 u_short nl_realport; 396 } natlookup_t; 397 398 #define nl_inip nl_inipaddr.in4 399 #define nl_outip nl_outipaddr.in4 400 #define nl_realip nl_realipaddr.in4 401 #define nl_inip6 nl_inipaddr.in6 402 #define nl_outip6 nl_outipaddr.in6 403 #define nl_realip6 nl_realipaddr.in6 404 405 406 typedef struct nat_save { 407 void *ipn_next; 408 struct nat ipn_nat; 409 struct ipnat ipn_ipnat; 410 struct frentry ipn_fr; 411 int ipn_dsize; 412 char ipn_data[4]; 413 } nat_save_t; 414 415 #define ipn_rule ipn_nat.nat_fr 416 417 typedef struct natget { 418 void *ng_ptr; 419 int ng_sz; 420 } natget_t; 421 422 423 /* 424 * This structure gets used to help NAT sessions keep the same NAT rule (and 425 * thus translation for IP address) when: 426 * (a) round-robin redirects are in use 427 * (b) different IP add 428 */ 429 typedef struct hostmap { 430 struct hostmap *hm_hnext; 431 struct hostmap **hm_phnext; 432 struct hostmap *hm_next; 433 struct hostmap **hm_pnext; 434 struct ipnat *hm_ipnat; 435 i6addr_t hm_osrcip6; 436 i6addr_t hm_odstip6; 437 i6addr_t hm_nsrcip6; 438 i6addr_t hm_ndstip6; 439 u_32_t hm_port; 440 int hm_ref; 441 int hm_hv; 442 int hm_v; 443 } hostmap_t; 444 445 #define hm_osrcip hm_osrcip6.in4 446 #define hm_odstip hm_odstip6.in4 447 #define hm_nsrcip hm_nsrcip6.in4 448 #define hm_ndstip hm_ndstip6.in4 449 #define hm_osrc6 hm_osrcip6.in6 450 #define hm_odst6 hm_odstip6.in6 451 #define hm_nsrc6 hm_nsrcip6.in6 452 #define hm_ndst6 hm_ndstip6.in6 453 454 455 /* 456 * Structure used to pass information in to nat_newmap and nat_newrdr. 457 */ 458 typedef struct natinfo { 459 ipnat_t *nai_np; 460 u_32_t nai_sum1; 461 u_32_t nai_sum2; 462 struct in_addr nai_ip; /* In host byte order */ 463 u_short nai_port; 464 u_short nai_nport; 465 u_short nai_sport; 466 u_short nai_dport; 467 } natinfo_t; 468 469 470 typedef struct nat_stat_side { 471 u_int *ns_bucketlen; 472 nat_t **ns_table; 473 u_long ns_added; 474 u_long ns_appr_fail; 475 u_long ns_badnat; 476 u_long ns_badnatnew; 477 u_long ns_badnextaddr; 478 u_long ns_bucket_max; 479 u_long ns_clone_nomem; 480 u_long ns_decap_bad; 481 u_long ns_decap_fail; 482 u_long ns_decap_pullup; 483 u_long ns_divert_dup; 484 u_long ns_divert_exist; 485 u_long ns_drop; 486 u_long ns_encap_dup; 487 u_long ns_encap_pullup; 488 u_long ns_exhausted; 489 u_long ns_icmp_address; 490 u_long ns_icmp_basic; 491 u_long ns_icmp_mbuf; 492 u_long ns_icmp_notfound; 493 u_long ns_icmp_rebuild; 494 u_long ns_icmp_short; 495 u_long ns_icmp_size; 496 u_long ns_ifpaddrfail; 497 u_long ns_ignored; 498 u_long ns_insert_fail; 499 u_long ns_inuse; 500 u_long ns_log; 501 u_long ns_lookup_miss; 502 u_long ns_lookup_nowild; 503 u_long ns_new_ifpaddr; 504 u_long ns_memfail; 505 u_long ns_table_max; 506 u_long ns_translated; 507 u_long ns_unfinalised; 508 u_long ns_wrap; 509 u_long ns_xlate_null; 510 u_long ns_xlate_exists; 511 u_long ns_ipf_proxy_fail; 512 u_long ns_uncreate[2]; 513 } nat_stat_side_t; 514 515 516 typedef struct natstat { 517 nat_t *ns_instances; 518 ipnat_t *ns_list; 519 hostmap_t *ns_maplist; 520 hostmap_t **ns_maptable; 521 u_int ns_active; 522 u_long ns_addtrpnt; 523 u_long ns_divert_build; 524 u_long ns_expire; 525 u_long ns_flush_all; 526 u_long ns_flush_closing; 527 u_long ns_flush_queue; 528 u_long ns_flush_state; 529 u_long ns_flush_timeout; 530 u_long ns_hm_new; 531 u_long ns_hm_newfail; 532 u_long ns_hm_addref; 533 u_long ns_hm_nullnp; 534 u_long ns_log_ok; 535 u_long ns_log_fail; 536 u_int ns_hostmap_sz; 537 u_int ns_nattab_sz; 538 u_int ns_nattab_max; 539 u_int ns_orphans; 540 u_int ns_rules; 541 u_int ns_rules_map; 542 u_int ns_rules_rdr; 543 u_int ns_rultab_sz; 544 u_int ns_rdrtab_sz; 545 u_32_t ns_ticks; 546 u_int ns_trpntab_sz; 547 u_int ns_wilds; 548 u_long ns_proto[256]; 549 nat_stat_side_t ns_side[2]; 550 #ifdef USE_INET6 551 nat_stat_side_t ns_side6[2]; 552 #endif 553 } natstat_t; 554 555 typedef struct natlog { 556 i6addr_t nl_osrcip; 557 i6addr_t nl_odstip; 558 i6addr_t nl_nsrcip; 559 i6addr_t nl_ndstip; 560 u_short nl_osrcport; 561 u_short nl_odstport; 562 u_short nl_nsrcport; 563 u_short nl_ndstport; 564 int nl_action; 565 int nl_type; 566 int nl_rule; 567 U_QUAD_T nl_pkts[2]; 568 U_QUAD_T nl_bytes[2]; 569 u_char nl_p[2]; 570 u_char nl_v[2]; 571 u_char nl_ifnames[2][LIFNAMSIZ]; 572 } natlog_t; 573 574 575 #define NL_NEW 0 576 #define NL_CLONE 1 577 #define NL_PURGE 0xfffc 578 #define NL_DESTROY 0xfffd 579 #define NL_FLUSH 0xfffe 580 #define NL_EXPIRE 0xffff 581 582 #define NAT_HASH_FN(_k,_l,_m) (((_k) + ((_k) >> 12) + _l) % (_m)) 583 #define NAT_HASH_FN6(_k,_l,_m) ((((u_32_t *)(_k))[3] \ 584 + (((u_32_t *)(_k))[3] >> 12) \ 585 + (((u_32_t *)(_k))[2]) \ 586 + (((u_32_t *)(_k))[2] >> 12) \ 587 + (((u_32_t *)(_k))[1]) \ 588 + (((u_32_t *)(_k))[1] >> 12) \ 589 + (((u_32_t *)(_k))[0]) \ 590 + (((u_32_t *)(_k))[0] >> 12) \ 591 + _l) % (_m)) 592 593 #define LONG_SUM(_i) (((_i) & 0xffff) + ((_i) >> 16)) 594 #define LONG_SUM6(_i) (LONG_SUM(ntohl(((u_32_t *)(_i))[0])) + \ 595 LONG_SUM(ntohl(((u_32_t *)(_i))[1])) + \ 596 LONG_SUM(ntohl(((u_32_t *)(_i))[2])) + \ 597 LONG_SUM(ntohl(((u_32_t *)(_i))[3]))) 598 599 #define CALC_SUMD(s1, s2, sd) { \ 600 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 601 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 602 /* Do it twice */ \ 603 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 604 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 605 /* Because ~1 == -2, We really need ~1 == -1 */ \ 606 if ((s1) > (s2)) (s2)--; \ 607 (sd) = (s2) - (s1); \ 608 (sd) = ((sd) & 0xffff) + ((sd) >> 16); } 609 610 #define NAT_SYSSPACE 0x80000000 611 #define NAT_LOCKHELD 0x40000000 612 613 /* 614 * This is present in ip_nat.h because it needs to be shared between 615 * ip_nat.c and ip_nat6.c 616 */ 617 typedef struct ipf_nat_softc_s { 618 ipfmutex_t ipf_nat_new; 619 ipfmutex_t ipf_nat_io; 620 int ipf_nat_doflush; 621 int ipf_nat_logging; 622 int ipf_nat_lock; 623 int ipf_nat_inited; 624 int ipf_nat_table_wm_high; 625 int ipf_nat_table_wm_low; 626 u_int ipf_nat_table_max; 627 u_int ipf_nat_table_sz; 628 u_int ipf_nat_maprules_sz; 629 u_int ipf_nat_rdrrules_sz; 630 u_int ipf_nat_hostmap_sz; 631 u_int ipf_nat_maxbucket; 632 u_int ipf_nat_last_force_flush; 633 u_int ipf_nat_defage; 634 u_int ipf_nat_defipage; 635 u_int ipf_nat_deficmpage; 636 ipf_v4_masktab_t ipf_nat_map_mask; 637 ipf_v6_masktab_t ipf_nat6_map_mask; 638 ipf_v4_masktab_t ipf_nat_rdr_mask; 639 ipf_v6_masktab_t ipf_nat6_rdr_mask; 640 nat_t **ipf_nat_table[2]; 641 nat_t *ipf_nat_instances; 642 ipnat_t *ipf_nat_list; 643 ipnat_t **ipf_nat_list_tail; 644 ipnat_t **ipf_nat_map_rules; 645 ipnat_t **ipf_nat_rdr_rules; 646 ipftq_t *ipf_nat_utqe; 647 hostmap_t **ipf_hm_maptable ; 648 hostmap_t *ipf_hm_maplist ; 649 ipftuneable_t *ipf_nat_tune; 650 ipftq_t ipf_nat_udptq; 651 ipftq_t ipf_nat_udpacktq; 652 ipftq_t ipf_nat_icmptq; 653 ipftq_t ipf_nat_icmpacktq; 654 ipftq_t ipf_nat_iptq; 655 ipftq_t ipf_nat_pending; 656 ipftq_t ipf_nat_tcptq[IPF_TCP_NSTATES]; 657 natstat_t ipf_nat_stats; 658 } ipf_nat_softc_t ; 659 660 #define ipf_nat_map_max ipf_nat_map_mask.imt4_max 661 #define ipf_nat_rdr_max ipf_nat_rdr_mask.imt4_max 662 #define ipf_nat6_map_max ipf_nat6_map_mask.imt6_max 663 #define ipf_nat6_rdr_max ipf_nat6_rdr_mask.imt6_max 664 #define ipf_nat_map_active_masks ipf_nat_map_mask.imt4_active 665 #define ipf_nat_rdr_active_masks ipf_nat_rdr_mask.imt4_active 666 #define ipf_nat6_map_active_masks ipf_nat6_map_mask.imt6_active 667 #define ipf_nat6_rdr_active_masks ipf_nat6_rdr_mask.imt6_active 668 669 extern frentry_t ipfnatblock; 670 671 extern void ipf_fix_datacksum(u_short *, u_32_t); 672 extern void ipf_fix_incksum(int, u_short *, u_32_t, u_32_t); 673 extern void ipf_fix_outcksum(int, u_short *, u_32_t, u_32_t); 674 675 extern int ipf_nat_checkin(fr_info_t *, u_32_t *); 676 extern int ipf_nat_checkout(fr_info_t *, u_32_t *); 677 extern void ipf_nat_delete(ipf_main_softc_t *, struct nat *, int); 678 extern void ipf_nat_deref(ipf_main_softc_t *, nat_t **); 679 extern void ipf_nat_expire(ipf_main_softc_t *); 680 extern int ipf_nat_hashtab_add(ipf_main_softc_t *, 681 ipf_nat_softc_t *, nat_t *); 682 extern void ipf_nat_hostmapdel(ipf_main_softc_t *, hostmap_t **); 683 extern int ipf_nat_hostmap_rehash(ipf_main_softc_t *, 684 ipftuneable_t *, ipftuneval_t *); 685 extern nat_t *ipf_nat_icmperrorlookup(fr_info_t *, int); 686 extern nat_t *ipf_nat_icmperror(fr_info_t *, u_int *, int); 687 extern int ipf_nat_init(void); 688 extern nat_t *ipf_nat_inlookup(fr_info_t *, u_int, u_int, 689 struct in_addr, struct in_addr); 690 extern int ipf_nat_in(fr_info_t *, nat_t *, int, u_32_t); 691 extern int ipf_nat_insert(ipf_main_softc_t *, ipf_nat_softc_t *, 692 nat_t *); 693 extern int ipf_nat_ioctl(ipf_main_softc_t *, caddr_t, ioctlcmd_t, 694 int, int, void *); 695 extern void ipf_nat_log(ipf_main_softc_t *, ipf_nat_softc_t *, 696 struct nat *, u_int); 697 extern nat_t *ipf_nat_lookupredir(natlookup_t *); 698 extern nat_t *ipf_nat_maplookup(void *, u_int, struct in_addr, 699 struct in_addr); 700 extern nat_t *ipf_nat_add(fr_info_t *, ipnat_t *, nat_t **, 701 u_int, int); 702 extern int ipf_nat_out(fr_info_t *, nat_t *, int, u_32_t); 703 extern nat_t *ipf_nat_outlookup(fr_info_t *, u_int, u_int, 704 struct in_addr, struct in_addr); 705 extern u_short *ipf_nat_proto(fr_info_t *, nat_t *, u_int); 706 extern void ipf_nat_rule_deref(ipf_main_softc_t *, ipnat_t **); 707 extern void ipf_nat_setqueue(ipf_main_softc_t *, ipf_nat_softc_t *, 708 nat_t *); 709 extern void ipf_nat_setpending(ipf_main_softc_t *, nat_t *); 710 extern nat_t *ipf_nat_tnlookup(fr_info_t *, int); 711 extern void ipf_nat_update(fr_info_t *, nat_t *); 712 extern int ipf_nat_rehash(ipf_main_softc_t *, ipftuneable_t *, 713 ipftuneval_t *); 714 extern int ipf_nat_rehash_rules(ipf_main_softc_t *, ipftuneable_t *, 715 ipftuneval_t *); 716 extern int ipf_nat_settimeout(struct ipf_main_softc_s *, 717 ipftuneable_t *, ipftuneval_t *); 718 extern void ipf_nat_sync(ipf_main_softc_t *, void *); 719 720 extern nat_t *ipf_nat_clone(fr_info_t *, nat_t *); 721 extern void ipf_nat_delmap(ipf_nat_softc_t *, ipnat_t *); 722 extern void ipf_nat_delrdr(ipf_nat_softc_t *, ipnat_t *); 723 extern int ipf_nat_wildok(nat_t *, int, int, int, int); 724 extern void ipf_nat_setlock(void *, int); 725 extern void ipf_nat_load(void); 726 extern void *ipf_nat_soft_create(ipf_main_softc_t *); 727 extern int ipf_nat_soft_init(ipf_main_softc_t *, void *); 728 extern void ipf_nat_soft_destroy(ipf_main_softc_t *, void *); 729 extern int ipf_nat_soft_fini(ipf_main_softc_t *, void *); 730 extern int ipf_nat_main_load(void); 731 extern int ipf_nat_main_unload(void); 732 extern ipftq_t *ipf_nat_add_tq(ipf_main_softc_t *, int); 733 extern void ipf_nat_uncreate(fr_info_t *); 734 735 #ifdef USE_INET6 736 extern nat_t *ipf_nat6_add(fr_info_t *, ipnat_t *, nat_t **, 737 u_int, int); 738 extern void ipf_nat6_addrdr(ipf_nat_softc_t *, ipnat_t *); 739 extern void ipf_nat6_addmap(ipf_nat_softc_t *, ipnat_t *); 740 extern void ipf_nat6_addencap(ipf_nat_softc_t *, ipnat_t *); 741 extern int ipf_nat6_checkout(fr_info_t *, u_32_t *); 742 extern int ipf_nat6_checkin(fr_info_t *, u_32_t *); 743 extern void ipf_nat6_delmap(ipf_nat_softc_t *, ipnat_t *); 744 extern void ipf_nat6_delrdr(ipf_nat_softc_t *, ipnat_t *); 745 extern int ipf_nat6_finalise(fr_info_t *, nat_t *); 746 extern nat_t *ipf_nat6_icmperror(fr_info_t *, u_int *, int); 747 extern nat_t *ipf_nat6_icmperrorlookup(fr_info_t *, int); 748 extern nat_t *ipf_nat6_inlookup(fr_info_t *, u_int, u_int, 749 struct in6_addr *, struct in6_addr *); 750 extern u_32_t ipf_nat6_ip6subtract(i6addr_t *, i6addr_t *); 751 extern frentry_t *ipf_nat6_ipfin(fr_info_t *, u_32_t *); 752 extern frentry_t *ipf_nat6_ipfout(fr_info_t *, u_32_t *); 753 extern nat_t *ipf_nat6_lookupredir(natlookup_t *); 754 extern int ipf_nat6_newmap(fr_info_t *, nat_t *, natinfo_t *); 755 extern int ipf_nat6_newrdr(fr_info_t *, nat_t *, natinfo_t *); 756 extern nat_t *ipf_nat6_outlookup(fr_info_t *, u_int, u_int, 757 struct in6_addr *, struct in6_addr *); 758 extern int ipf_nat6_newrewrite(fr_info_t *, nat_t *, natinfo_t *); 759 extern int ipf_nat6_newdivert(fr_info_t *, nat_t *, natinfo_t *); 760 extern int ipf_nat6_ruleaddrinit(ipf_main_softc_t *, ipf_nat_softc_t *, ipnat_t *); 761 762 #endif 763 764 765 #endif /* __IP_NAT_H__ */ 766