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