1 /* 2 * Copyright (C) 1995-2001, 2003 by Darren Reed. 3 * 4 * See the IPFILTER.LICENCE file for details on licencing. 5 * 6 * @(#)ip_nat.h 1.5 2/4/96 7 * $Id: ip_nat.h,v 2.90.2.11 2005/06/18 02:41:32 darrenr Exp $ 8 * 9 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 10 * Use is subject to license terms. 11 */ 12 13 #ifndef __IP_NAT_H__ 14 #define __IP_NAT_H__ 15 16 #ifndef SOLARIS 17 #define SOLARIS (defined(sun) && (defined(__svr4__) || defined(__SVR4))) 18 #endif 19 20 #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51) 21 #define SIOCADNAT _IOW('r', 60, struct ipfobj) 22 #define SIOCRMNAT _IOW('r', 61, struct ipfobj) 23 #define SIOCGNATS _IOWR('r', 62, struct ipfobj) 24 #define SIOCGNATL _IOWR('r', 63, struct ipfobj) 25 #else 26 #define SIOCADNAT _IOW(r, 60, struct ipfobj) 27 #define SIOCRMNAT _IOW(r, 61, struct ipfobj) 28 #define SIOCGNATS _IOWR(r, 62, struct ipfobj) 29 #define SIOCGNATL _IOWR(r, 63, struct ipfobj) 30 #endif 31 32 #undef LARGE_NAT /* define this if you're setting up a system to NAT 33 * LARGE numbers of networks/hosts - i.e. in the 34 * hundreds or thousands. In such a case, you should 35 * also change the RDR_SIZE and NAT_SIZE below to more 36 * appropriate sizes. The figures below were used for 37 * a setup with 1000-2000 networks to NAT. 38 */ 39 #ifndef NAT_SIZE 40 # ifdef LARGE_NAT 41 # define NAT_SIZE 2047 42 # else 43 # define NAT_SIZE 127 44 # endif 45 #endif 46 #ifndef RDR_SIZE 47 # ifdef LARGE_NAT 48 # define RDR_SIZE 2047 49 # else 50 # define RDR_SIZE 127 51 # endif 52 #endif 53 #ifndef HOSTMAP_SIZE 54 # ifdef LARGE_NAT 55 # define HOSTMAP_SIZE 8191 56 # else 57 # define HOSTMAP_SIZE 2047 58 # endif 59 #endif 60 #ifndef NAT_TABLE_MAX 61 /* 62 * This is newly introduced and for the sake of "least surprise", the numbers 63 * present aren't what we'd normally use for creating a proper hash table. 64 */ 65 # ifdef LARGE_NAT 66 # define NAT_TABLE_MAX 180000 67 # else 68 # define NAT_TABLE_MAX 30000 69 # endif 70 #endif 71 #ifndef NAT_TABLE_SZ 72 # ifdef LARGE_NAT 73 # define NAT_TABLE_SZ 16383 74 # else 75 # define NAT_TABLE_SZ 2047 76 # endif 77 #endif 78 #ifndef APR_LABELLEN 79 #define APR_LABELLEN 16 80 #endif 81 82 #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */ 83 84 /* 85 * Default hi and lo watermarks used with forced flush of nat table. 86 */ 87 88 #define NAT_FLUSH_HI 95 89 #define NAT_FLUSH_LO 75 90 91 /* How full is the nat table? */ 92 93 #define NAT_TAB_WATER_LEVEL(x) ((x)->ifs_nat_stats.ns_inuse * 100 \ 94 / (x)->ifs_ipf_nattable_max) 95 96 struct ipstate; 97 struct ap_session; 98 99 typedef struct nat { 100 ipfmutex_t nat_lock; 101 struct nat *nat_next; 102 struct nat **nat_pnext; 103 struct nat *nat_hnext[2]; 104 struct nat **nat_phnext[2]; 105 struct hostmap *nat_hm; 106 void *nat_data; 107 struct nat **nat_me; 108 struct ipstate *nat_state; 109 struct ap_session *nat_aps; /* proxy session */ 110 frentry_t *nat_fr; /* filter rule ptr if appropriate */ 111 struct ipnat *nat_ptr; /* pointer back to the rule */ 112 void *nat_ifps[2]; 113 void *nat_sync; 114 ipftqent_t nat_tqe; 115 u_32_t nat_flags; 116 u_32_t nat_sumd[2]; /* ip checksum delta for data segment */ 117 u_32_t nat_ipsumd; /* ip checksum delta for ip header */ 118 u_32_t nat_mssclamp; /* if != zero clamp MSS to this */ 119 i6addr_t nat_inip6; 120 i6addr_t nat_outip6; 121 i6addr_t nat_oip6; /* other ip */ 122 U_QUAD_T nat_pkts[2]; 123 U_QUAD_T nat_bytes[2]; 124 union { 125 udpinfo_t nat_unu; 126 tcpinfo_t nat_unt; 127 icmpinfo_t nat_uni; 128 greinfo_t nat_ugre; 129 } nat_un; 130 u_short nat_oport; /* other port */ 131 u_short nat_use; 132 u_char nat_p; /* protocol for NAT */ 133 int nat_dir; 134 int nat_ref; /* reference count */ 135 int nat_hv[2]; 136 char nat_ifnames[2][LIFNAMSIZ]; 137 int nat_rev; /* 0 = forward, 1 = reverse */ 138 int nat_v; 139 int nat_redir; /* copy of in_redir */ 140 } nat_t; 141 142 #define nat_inip nat_inip6.in4 143 #define nat_outip nat_outip6.in4 144 #define nat_oip nat_oip6.in4 145 #define nat_age nat_tqe.tqe_die 146 #define nat_inport nat_un.nat_unt.ts_sport 147 #define nat_outport nat_un.nat_unt.ts_dport 148 #define nat_type nat_un.nat_uni.ici_type 149 #define nat_seq nat_un.nat_uni.ici_seq 150 #define nat_id nat_un.nat_uni.ici_id 151 #define nat_tcpstate nat_tqe.tqe_state 152 153 /* 154 * Values for nat_dir 155 */ 156 #define NAT_INBOUND 0 157 #define NAT_OUTBOUND 1 158 159 /* 160 * Definitions for nat_flags 161 */ 162 #define NAT_TCP 0x0001 /* IPN_TCP */ 163 #define NAT_UDP 0x0002 /* IPN_UDP */ 164 #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */ 165 #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */ 166 #define NAT_SEARCH 0x0010 167 #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */ 168 #define NAT_NOTRULEPORT 0x0040 169 170 #define NAT_TCPUDP (NAT_TCP|NAT_UDP) 171 #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR) 172 #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY) 173 #define NAT_FROMRULE (NAT_TCP|NAT_UDP) 174 175 /* 0x0100 reserved for FI_W_SPORT */ 176 /* 0x0200 reserved for FI_W_DPORT */ 177 /* 0x0400 reserved for FI_W_SADDR */ 178 /* 0x0800 reserved for FI_W_DADDR */ 179 /* 0x1000 reserved for FI_W_NEWFR */ 180 /* 0x2000 reserved for SI_CLONE */ 181 /* 0x4000 reserved for SI_CLONED */ 182 /* 0x8000 reserved for SI_IGNOREPKT */ 183 184 #define NAT_DEBUG 0x800000 185 186 typedef struct ipnat { 187 struct ipnat *in_next; /* NAT rule list next */ 188 struct ipnat *in_rnext; /* rdr rule hash next */ 189 struct ipnat **in_prnext; /* prior rdr next ptr */ 190 struct ipnat *in_mnext; /* map rule hash next */ 191 struct ipnat **in_pmnext; /* prior map next ptr */ 192 struct ipftq *in_tqehead[2]; 193 void *in_ifps[2]; 194 void *in_apr; 195 char *in_comment; 196 i6addr_t in_next6; 197 u_long in_space; 198 u_long in_hits; 199 u_int in_use; 200 u_int in_hv; 201 int in_flineno; /* conf. file line number */ 202 u_short in_pnext; 203 u_char in_v; 204 u_char in_xxx; 205 /* From here to the end is covered by IPN_CMPSIZ */ 206 u_32_t in_flags; 207 u_32_t in_mssclamp; /* if != 0 clamp MSS to this */ 208 u_int in_age[2]; 209 int in_redir; /* see below for values */ 210 int in_p; /* protocol. */ 211 i6addr_t in_in[2]; 212 i6addr_t in_out[2]; 213 i6addr_t in_src[2]; 214 frtuc_t in_tuc; 215 u_short in_port[2]; 216 u_short in_ppip; /* ports per IP. */ 217 u_short in_ippip; /* IP #'s per IP# */ 218 char in_ifnames[2][LIFNAMSIZ]; 219 char in_plabel[APR_LABELLEN]; /* proxy label. */ 220 ipftag_t in_tag; 221 } ipnat_t; 222 223 #define in_pmin in_port[0] /* Also holds static redir port */ 224 #define in_pmax in_port[1] 225 #define in_nextip in_next6.in4 226 #define in_nip in_next6.in4.s_addr 227 #define in_inip in_in[0].in4.s_addr 228 #define in_inmsk in_in[1].in4.s_addr 229 #define in_outip in_out[0].in4.s_addr 230 #define in_outmsk in_out[1].in4.s_addr 231 #define in_srcip in_src[0].in4.s_addr 232 #define in_srcmsk in_src[1].in4.s_addr 233 #define in_scmp in_tuc.ftu_scmp 234 #define in_dcmp in_tuc.ftu_dcmp 235 #define in_stop in_tuc.ftu_stop 236 #define in_dtop in_tuc.ftu_dtop 237 #define in_sport in_tuc.ftu_sport 238 #define in_dport in_tuc.ftu_dport 239 240 /* 241 * Bit definitions for in_flags 242 */ 243 #define IPN_ANY 0x00000 244 #define IPN_TCP 0x00001 245 #define IPN_UDP 0x00002 246 #define IPN_TCPUDP (IPN_TCP|IPN_UDP) 247 #define IPN_ICMPERR 0x00004 248 #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR) 249 #define IPN_ICMPQUERY 0x00008 250 #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY) 251 #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR) 252 #define IPN_AUTOPORTMAP 0x00010 253 #define IPN_IPRANGE 0x00020 254 #define IPN_FILTER 0x00040 255 #define IPN_SPLIT 0x00080 256 #define IPN_ROUNDR 0x00100 257 #define IPN_NOTSRC 0x04000 258 #define IPN_NOTDST 0x08000 259 #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */ 260 #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */ 261 #define IPN_DELETE 0x40000 262 #define IPN_STICKY 0x80000 263 #define IPN_FRAG 0x100000 264 #define IPN_FIXEDDPORT 0x200000 265 #define IPN_FINDFORWARD 0x400000 266 #define IPN_IN 0x800000 267 #define IPN_SEQUENTIAL 0x1000000 268 #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\ 269 IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\ 270 IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\ 271 IPN_SEQUENTIAL) 272 273 /* 274 * Values for in_redir 275 */ 276 #define NAT_MAP 0x01 277 #define NAT_REDIRECT 0x02 278 #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT) 279 #define NAT_MAPBLK 0x04 280 281 #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */ 282 #define USABLE_PORTS (65536 - MAPBLK_MINPORT) 283 284 #define IPN_CMPSIZ (sizeof (ipnat_t) - offsetof(ipnat_t, in_flags)) 285 286 typedef struct natlookup { 287 i6addr_t nl_inipaddr; 288 i6addr_t nl_outipaddr; 289 i6addr_t nl_realipaddr; 290 int nl_v; 291 int nl_flags; 292 u_short nl_inport; 293 u_short nl_outport; 294 u_short nl_realport; 295 } natlookup_t; 296 297 #define nl_inip nl_inipaddr.in4 298 #define nl_outip nl_outipaddr.in4 299 #define nl_realip nl_realipaddr.in4 300 #define nl_inip6 nl_inipaddr.in6 301 #define nl_outip6 nl_outipaddr.in6 302 #define nl_realip6 nl_realipaddr.in6 303 304 305 typedef struct nat_save { 306 void *ipn_next; 307 struct nat ipn_nat; 308 struct ipnat ipn_ipnat; 309 struct frentry ipn_fr; 310 int ipn_dsize; 311 char ipn_data[4]; 312 } nat_save_t; 313 314 #define ipn_rule ipn_nat.nat_fr 315 316 typedef struct natget { 317 void *ng_ptr; 318 int ng_sz; 319 } natget_t; 320 321 322 #undef tr_flags 323 typedef struct nattrpnt { 324 struct in_addr tr_dstip; /* real destination IP# */ 325 struct in_addr tr_srcip; /* real source IP# */ 326 struct in_addr tr_locip; /* local source IP# */ 327 u_int tr_flags; 328 int tr_expire; 329 u_short tr_dstport; /* real destination port# */ 330 u_short tr_srcport; /* real source port# */ 331 u_short tr_locport; /* local source port# */ 332 struct nattrpnt *tr_hnext; 333 struct nattrpnt **tr_phnext; 334 struct nattrpnt *tr_next; 335 struct nattrpnt **tr_pnext; /* previous next */ 336 } nattrpnt_t; 337 338 #define TN_CMPSIZ offsetof(nattrpnt_t, tr_hnext) 339 340 341 /* 342 * This structure gets used to help NAT sessions keep the same NAT rule (and 343 * thus translation for IP address) when: 344 * (a) round-robin redirects are in use 345 * (b) different IP add 346 */ 347 typedef struct hostmap { 348 struct hostmap *hm_next; 349 struct hostmap **hm_pnext; 350 struct hostmap *hm_hnext; 351 struct hostmap **hm_phnext; 352 struct ipnat *hm_ipnat; 353 i6addr_t hm_src; 354 i6addr_t hm_dst; 355 i6addr_t hm_map; 356 u_32_t hm_port; 357 int hm_ref; 358 int hm_v; 359 } hostmap_t; 360 361 #define hm_srcip hm_src.in4 362 #define hm_dstip hm_dst.in4 363 #define hm_mapip hm_map.in4 364 #define hm_srcip6 hm_src.in6 365 #define hm_dstip6 hm_dst.in6 366 #define hm_mapip6 hm_map.in6 367 368 369 /* 370 * Structure used to pass information in to nat_newmap and nat_newrdr. 371 */ 372 typedef struct natinfo { 373 ipnat_t *nai_np; 374 u_32_t nai_nflags; 375 u_32_t nai_flags; 376 struct in_addr nai_ip; 377 u_short nai_port; 378 u_short nai_nport; 379 u_short nai_sport; 380 u_short nai_dport; 381 } natinfo_t; 382 383 384 typedef struct natstat { 385 u_long ns_mapped[2]; 386 u_long ns_rules; 387 u_long ns_added; 388 u_long ns_expire; 389 u_long ns_inuse; 390 u_long ns_logged; 391 u_long ns_logfail; 392 u_long ns_memfail; 393 u_long ns_badnat; 394 u_long ns_addtrpnt; 395 nat_t **ns_table[2]; 396 hostmap_t **ns_maptable; 397 ipnat_t *ns_list; 398 void *ns_apslist; 399 u_int ns_wilds; 400 u_int ns_nattab_sz; 401 u_int ns_nattab_max; 402 u_int ns_rultab_sz; 403 u_int ns_rdrtab_sz; 404 u_int ns_trpntab_sz; 405 u_int ns_hostmap_sz; 406 nat_t *ns_instances; 407 nattrpnt_t *ns_trpntlist; 408 hostmap_t *ns_maplist; 409 u_long *ns_bucketlen[2]; 410 u_int ns_orphans; 411 } natstat_t; 412 413 typedef struct natlog { 414 i6addr_t nlg_origip; 415 i6addr_t nlg_outip; 416 i6addr_t nlg_inip; 417 u_short nlg_origport; 418 u_short nlg_outport; 419 u_short nlg_inport; 420 u_short nlg_type; 421 int nlg_rule; 422 U_QUAD_T nlg_pkts[2]; 423 U_QUAD_T nlg_bytes[2]; 424 u_char nlg_p; 425 int nlg_v; 426 } natlog_t; 427 428 429 #define NL_NEWMAP NAT_MAP 430 #define NL_NEWRDR NAT_REDIRECT 431 #define NL_NEWBIMAP NAT_BIMAP 432 #define NL_NEWBLOCK NAT_MAPBLK 433 #define NL_DESTROY 0xfffc 434 #define NL_CLONE 0xfffd 435 #define NL_FLUSH 0xfffe 436 #define NL_EXPIRE 0xffff 437 438 #define NAT_HASH_FN(k, l, m) (((k) + ((k) >> 12) + l) % (m)) 439 #define NAT_HASH_FN6(k, l, m) ((((u_32_t *)(k))[3] \ 440 + (((u_32_t *)(k))[3] >> 12) \ 441 + (((u_32_t *)(k))[2]) \ 442 + (((u_32_t *)(k))[2] >> 12) \ 443 + (((u_32_t *)(k))[1]) \ 444 + (((u_32_t *)(k))[1] >> 12) \ 445 + (((u_32_t *)(k))[0]) \ 446 + (((u_32_t *)(k))[0] >> 12) \ 447 + l) % (m)) 448 449 #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16)) 450 #define LONG_SUM6(in) (LONG_SUM(ntohl(((u_32_t *)(in))[0])) + \ 451 LONG_SUM(ntohl(((u_32_t *)(in))[1])) + \ 452 LONG_SUM(ntohl(((u_32_t *)(in))[2])) + \ 453 LONG_SUM(ntohl(((u_32_t *)(in))[3]))) 454 455 #define CALC_SUMD(s1, s2, sd) { \ 456 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 457 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 458 /* Do it twice */ \ 459 (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 460 (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 461 /* Because ~1 == -2, We really need ~1 == -1 */ \ 462 if ((s1) > (s2)) (s2)--; \ 463 (sd) = (s2) - (s1); \ 464 (sd) = ((sd) & 0xffff) + ((sd) >> 16); } 465 466 #define NAT_SYSSPACE 0x80000000 467 #define NAT_LOCKHELD 0x40000000 468 469 extern void fr_natsync __P((void *, ipf_stack_t *)); 470 extern void fr_nataddrsync __P((int, void *, void *, ipf_stack_t *)); 471 extern void fr_natifpsync __P((int, int, void *, char *, ipf_stack_t *)); 472 473 #if defined(__OpenBSD__) 474 extern void nat_ifdetach __P((void *, ipf_stack_t *)); 475 #endif 476 extern int fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *)); 477 extern int fr_natinit __P((ipf_stack_t *)); 478 extern nat_t *nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int)); 479 extern nat_t *nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr, 480 struct in_addr)); 481 extern void fix_datacksum __P((u_short *, u_32_t)); 482 extern nat_t *nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr, 483 struct in_addr)); 484 extern nat_t *nat_tnlookup __P((fr_info_t *, int)); 485 extern nat_t *nat_maplookup __P((void *, u_int, struct in_addr, 486 struct in_addr)); 487 extern nat_t *nat_lookupredir __P((natlookup_t *, ipf_stack_t *)); 488 extern nat_t *nat_icmperrorlookup __P((fr_info_t *, int)); 489 extern nat_t *nat_icmperror __P((fr_info_t *, u_int *, int)); 490 extern void nat_delete __P((struct nat *, int, ipf_stack_t *)); 491 extern int nat_insert __P((nat_t *, int, ipf_stack_t *)); 492 493 extern int fr_checknatout __P((fr_info_t *, u_32_t *)); 494 extern int fr_natout __P((fr_info_t *, nat_t *, int, u_32_t)); 495 extern int fr_checknatin __P((fr_info_t *, u_32_t *)); 496 extern int fr_natin __P((fr_info_t *, nat_t *, int, u_32_t)); 497 extern void fr_natunload __P((ipf_stack_t *)); 498 extern void fr_natexpire __P((ipf_stack_t *)); 499 extern void nat_log __P((struct nat *, u_int, ipf_stack_t *)); 500 extern void fix_incksum __P((u_short *, u_32_t)); 501 extern void fix_outcksum __P((u_short *, u_32_t)); 502 extern void fr_ipnatderef __P((ipnat_t **, ipf_stack_t *)); 503 extern void fr_natderef __P((nat_t **, ipf_stack_t *)); 504 extern u_short *nat_proto __P((fr_info_t *, nat_t *, u_int)); 505 extern void nat_update __P((fr_info_t *, nat_t *, ipnat_t *)); 506 extern void fr_setnatqueue __P((nat_t *, int, ipf_stack_t *)); 507 extern void fr_hostmapdel __P((hostmap_t **)); 508 509 extern nat_t *fr_natclone __P((fr_info_t *, nat_t *)); 510 extern void nat_delrdr __P((struct ipnat *)); 511 extern void nat_delnat __P((struct ipnat *)); 512 extern int nat_wildok __P((nat_t *, int, int, int, int)); 513 extern void nat_calc_chksum_diffs __P((nat_t *)); 514 515 #ifdef USE_INET6 516 extern void nat6_addnat __P((ipnat_t *, ipf_stack_t *)); 517 extern void nat6_addrdr __P((ipnat_t *, ipf_stack_t *)); 518 extern nat_t *nat6_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int)); 519 extern nat_t *nat6_outlookup __P((fr_info_t *, u_int, u_int, 520 struct in6_addr *, struct in6_addr *)); 521 extern nat_t *nat6_inlookup __P((fr_info_t *, u_int, u_int, 522 struct in6_addr *, struct in6_addr *)); 523 extern nat_t *nat6_lookupredir __P((natlookup_t *, ipf_stack_t *)); 524 extern nat_t *nat6_icmperrorlookup __P((fr_info_t *, int)); 525 extern nat_t *nat6_icmperror __P((fr_info_t *, u_int *, int)); 526 extern int nat6_insert __P((nat_t *, int, ipf_stack_t *)); 527 extern int fr_checknat6out __P((fr_info_t *, u_32_t *)); 528 extern int fr_nat6out __P((fr_info_t *, nat_t *, int, u_32_t)); 529 extern int fr_checknat6in __P((fr_info_t *, u_32_t *)); 530 extern int fr_nat6in __P((fr_info_t *, nat_t *, int, u_32_t)); 531 #endif 532 533 #endif /* __IP_NAT_H__ */ 534