17c478bd9Sstevel@tonic-gate /* 2ab25eeb5Syz * Copyright (C) 1995-2001, 2003 by Darren Reed. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 57c478bd9Sstevel@tonic-gate * 67c478bd9Sstevel@tonic-gate * @(#)ip_nat.h 1.5 2/4/96 7ab25eeb5Syz * $Id: ip_nat.h,v 2.90.2.11 2005/06/18 02:41:32 darrenr Exp $ 83805c50fSan * 933f2fefdSDarren Reed * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 105b6dcef9Sdr * Use is subject to license terms. 117c478bd9Sstevel@tonic-gate */ 127c478bd9Sstevel@tonic-gate 137c478bd9Sstevel@tonic-gate #ifndef __IP_NAT_H__ 147c478bd9Sstevel@tonic-gate #define __IP_NAT_H__ 157c478bd9Sstevel@tonic-gate 16*ae7a42b1SToomas Soome #ifdef SOLARIS 17*ae7a42b1SToomas Soome #undef SOLARIS 18*ae7a42b1SToomas Soome #endif 19*ae7a42b1SToomas Soome #if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) 20*ae7a42b1SToomas Soome #define SOLARIS (1) 21*ae7a42b1SToomas Soome #else 22*ae7a42b1SToomas Soome #define SOLARIS (0) 237c478bd9Sstevel@tonic-gate #endif 247c478bd9Sstevel@tonic-gate 25ab25eeb5Syz #if defined(__STDC__) || defined(__GNUC__) || defined(_AIX51) 267c478bd9Sstevel@tonic-gate #define SIOCADNAT _IOW('r', 60, struct ipfobj) 277c478bd9Sstevel@tonic-gate #define SIOCRMNAT _IOW('r', 61, struct ipfobj) 287c478bd9Sstevel@tonic-gate #define SIOCGNATS _IOWR('r', 62, struct ipfobj) 297c478bd9Sstevel@tonic-gate #define SIOCGNATL _IOWR('r', 63, struct ipfobj) 307c478bd9Sstevel@tonic-gate #else 317c478bd9Sstevel@tonic-gate #define SIOCADNAT _IOW(r, 60, struct ipfobj) 327c478bd9Sstevel@tonic-gate #define SIOCRMNAT _IOW(r, 61, struct ipfobj) 337c478bd9Sstevel@tonic-gate #define SIOCGNATS _IOWR(r, 62, struct ipfobj) 347c478bd9Sstevel@tonic-gate #define SIOCGNATL _IOWR(r, 63, struct ipfobj) 357c478bd9Sstevel@tonic-gate #endif 367c478bd9Sstevel@tonic-gate 37d6c23f6fSyx #undef LARGE_NAT /* define this if you're setting up a system to NAT 387c478bd9Sstevel@tonic-gate * LARGE numbers of networks/hosts - i.e. in the 397c478bd9Sstevel@tonic-gate * hundreds or thousands. In such a case, you should 407c478bd9Sstevel@tonic-gate * also change the RDR_SIZE and NAT_SIZE below to more 417c478bd9Sstevel@tonic-gate * appropriate sizes. The figures below were used for 427c478bd9Sstevel@tonic-gate * a setup with 1000-2000 networks to NAT. 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate #ifndef NAT_SIZE 457c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT 467c478bd9Sstevel@tonic-gate # define NAT_SIZE 2047 477c478bd9Sstevel@tonic-gate # else 487c478bd9Sstevel@tonic-gate # define NAT_SIZE 127 497c478bd9Sstevel@tonic-gate # endif 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate #ifndef RDR_SIZE 527c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT 537c478bd9Sstevel@tonic-gate # define RDR_SIZE 2047 547c478bd9Sstevel@tonic-gate # else 557c478bd9Sstevel@tonic-gate # define RDR_SIZE 127 567c478bd9Sstevel@tonic-gate # endif 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate #ifndef HOSTMAP_SIZE 597c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT 60ab25eeb5Syz # define HOSTMAP_SIZE 8191 61ab25eeb5Syz # else 627c478bd9Sstevel@tonic-gate # define HOSTMAP_SIZE 2047 63ab25eeb5Syz # endif 64ab25eeb5Syz #endif 65ab25eeb5Syz #ifndef NAT_TABLE_MAX 66ab25eeb5Syz /* 67ab25eeb5Syz * This is newly introduced and for the sake of "least surprise", the numbers 68ab25eeb5Syz * present aren't what we'd normally use for creating a proper hash table. 69ab25eeb5Syz */ 70ab25eeb5Syz # ifdef LARGE_NAT 71ab25eeb5Syz # define NAT_TABLE_MAX 180000 727c478bd9Sstevel@tonic-gate # else 73ab25eeb5Syz # define NAT_TABLE_MAX 30000 747c478bd9Sstevel@tonic-gate # endif 757c478bd9Sstevel@tonic-gate #endif 767c478bd9Sstevel@tonic-gate #ifndef NAT_TABLE_SZ 777c478bd9Sstevel@tonic-gate # ifdef LARGE_NAT 787c478bd9Sstevel@tonic-gate # define NAT_TABLE_SZ 16383 79ab25eeb5Syz # else 80ab25eeb5Syz # define NAT_TABLE_SZ 2047 817c478bd9Sstevel@tonic-gate # endif 827c478bd9Sstevel@tonic-gate #endif 837c478bd9Sstevel@tonic-gate #ifndef APR_LABELLEN 847c478bd9Sstevel@tonic-gate #define APR_LABELLEN 16 857c478bd9Sstevel@tonic-gate #endif 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate #define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */ 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate struct ipstate; 907c478bd9Sstevel@tonic-gate struct ap_session; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate typedef struct nat { 937c478bd9Sstevel@tonic-gate ipfmutex_t nat_lock; 947c478bd9Sstevel@tonic-gate struct nat *nat_next; 957c478bd9Sstevel@tonic-gate struct nat **nat_pnext; 967c478bd9Sstevel@tonic-gate struct nat *nat_hnext[2]; 977c478bd9Sstevel@tonic-gate struct nat **nat_phnext[2]; 987c478bd9Sstevel@tonic-gate struct hostmap *nat_hm; 997c478bd9Sstevel@tonic-gate void *nat_data; 1007c478bd9Sstevel@tonic-gate struct nat **nat_me; 1017c478bd9Sstevel@tonic-gate struct ipstate *nat_state; 1027c478bd9Sstevel@tonic-gate struct ap_session *nat_aps; /* proxy session */ 1037c478bd9Sstevel@tonic-gate frentry_t *nat_fr; /* filter rule ptr if appropriate */ 1047c478bd9Sstevel@tonic-gate struct ipnat *nat_ptr; /* pointer back to the rule */ 1057c478bd9Sstevel@tonic-gate void *nat_ifps[2]; 1067c478bd9Sstevel@tonic-gate void *nat_sync; 1077c478bd9Sstevel@tonic-gate ipftqent_t nat_tqe; 1087c478bd9Sstevel@tonic-gate u_32_t nat_flags; 109d6c23f6fSyx u_32_t nat_sumd[2]; /* ip checksum delta for data segment */ 1107c478bd9Sstevel@tonic-gate u_32_t nat_ipsumd; /* ip checksum delta for ip header */ 1117c478bd9Sstevel@tonic-gate u_32_t nat_mssclamp; /* if != zero clamp MSS to this */ 1127c478bd9Sstevel@tonic-gate i6addr_t nat_inip6; 1137c478bd9Sstevel@tonic-gate i6addr_t nat_outip6; 1147c478bd9Sstevel@tonic-gate i6addr_t nat_oip6; /* other ip */ 1157c478bd9Sstevel@tonic-gate U_QUAD_T nat_pkts[2]; 1167c478bd9Sstevel@tonic-gate U_QUAD_T nat_bytes[2]; 1177c478bd9Sstevel@tonic-gate union { 1187c478bd9Sstevel@tonic-gate udpinfo_t nat_unu; 1197c478bd9Sstevel@tonic-gate tcpinfo_t nat_unt; 1207c478bd9Sstevel@tonic-gate icmpinfo_t nat_uni; 1217c478bd9Sstevel@tonic-gate greinfo_t nat_ugre; 1227c478bd9Sstevel@tonic-gate } nat_un; 1237c478bd9Sstevel@tonic-gate u_short nat_oport; /* other port */ 1247c478bd9Sstevel@tonic-gate u_short nat_use; 1257c478bd9Sstevel@tonic-gate u_char nat_p; /* protocol for NAT */ 1267c478bd9Sstevel@tonic-gate int nat_dir; 1277c478bd9Sstevel@tonic-gate int nat_ref; /* reference count */ 1287c478bd9Sstevel@tonic-gate int nat_hv[2]; 1297c478bd9Sstevel@tonic-gate char nat_ifnames[2][LIFNAMSIZ]; 130ab25eeb5Syz int nat_rev; /* 0 = forward, 1 = reverse */ 131d6c23f6fSyx int nat_v; 132f4b3ec61Sdh int nat_redir; /* copy of in_redir */ 1337c478bd9Sstevel@tonic-gate } nat_t; 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate #define nat_inip nat_inip6.in4 1367c478bd9Sstevel@tonic-gate #define nat_outip nat_outip6.in4 1377c478bd9Sstevel@tonic-gate #define nat_oip nat_oip6.in4 1387c478bd9Sstevel@tonic-gate #define nat_age nat_tqe.tqe_die 1397c478bd9Sstevel@tonic-gate #define nat_inport nat_un.nat_unt.ts_sport 1407c478bd9Sstevel@tonic-gate #define nat_outport nat_un.nat_unt.ts_dport 1417c478bd9Sstevel@tonic-gate #define nat_type nat_un.nat_uni.ici_type 1427c478bd9Sstevel@tonic-gate #define nat_seq nat_un.nat_uni.ici_seq 1437c478bd9Sstevel@tonic-gate #define nat_id nat_un.nat_uni.ici_id 1447c478bd9Sstevel@tonic-gate #define nat_tcpstate nat_tqe.tqe_state 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * Values for nat_dir 1487c478bd9Sstevel@tonic-gate */ 1497c478bd9Sstevel@tonic-gate #define NAT_INBOUND 0 1507c478bd9Sstevel@tonic-gate #define NAT_OUTBOUND 1 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate /* 1537c478bd9Sstevel@tonic-gate * Definitions for nat_flags 1547c478bd9Sstevel@tonic-gate */ 1557c478bd9Sstevel@tonic-gate #define NAT_TCP 0x0001 /* IPN_TCP */ 1567c478bd9Sstevel@tonic-gate #define NAT_UDP 0x0002 /* IPN_UDP */ 1577c478bd9Sstevel@tonic-gate #define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */ 1587c478bd9Sstevel@tonic-gate #define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */ 1597c478bd9Sstevel@tonic-gate #define NAT_SEARCH 0x0010 1607c478bd9Sstevel@tonic-gate #define NAT_SLAVE 0x0020 /* Slave connection for a proxy */ 1617c478bd9Sstevel@tonic-gate #define NAT_NOTRULEPORT 0x0040 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate #define NAT_TCPUDP (NAT_TCP|NAT_UDP) 1647c478bd9Sstevel@tonic-gate #define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR) 165ab25eeb5Syz #define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY) 1667c478bd9Sstevel@tonic-gate #define NAT_FROMRULE (NAT_TCP|NAT_UDP) 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 0x0100 reserved for FI_W_SPORT */ 1697c478bd9Sstevel@tonic-gate /* 0x0200 reserved for FI_W_DPORT */ 1707c478bd9Sstevel@tonic-gate /* 0x0400 reserved for FI_W_SADDR */ 1717c478bd9Sstevel@tonic-gate /* 0x0800 reserved for FI_W_DADDR */ 1727c478bd9Sstevel@tonic-gate /* 0x1000 reserved for FI_W_NEWFR */ 1737c478bd9Sstevel@tonic-gate /* 0x2000 reserved for SI_CLONE */ 1747c478bd9Sstevel@tonic-gate /* 0x4000 reserved for SI_CLONED */ 1757c478bd9Sstevel@tonic-gate /* 0x8000 reserved for SI_IGNOREPKT */ 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate #define NAT_DEBUG 0x800000 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate typedef struct ipnat { 1807c478bd9Sstevel@tonic-gate struct ipnat *in_next; /* NAT rule list next */ 1817c478bd9Sstevel@tonic-gate struct ipnat *in_rnext; /* rdr rule hash next */ 1827c478bd9Sstevel@tonic-gate struct ipnat **in_prnext; /* prior rdr next ptr */ 1837c478bd9Sstevel@tonic-gate struct ipnat *in_mnext; /* map rule hash next */ 1847c478bd9Sstevel@tonic-gate struct ipnat **in_pmnext; /* prior map next ptr */ 1857c478bd9Sstevel@tonic-gate struct ipftq *in_tqehead[2]; 1867c478bd9Sstevel@tonic-gate void *in_ifps[2]; 1877c478bd9Sstevel@tonic-gate void *in_apr; 1887c478bd9Sstevel@tonic-gate char *in_comment; 1897c478bd9Sstevel@tonic-gate i6addr_t in_next6; 1907c478bd9Sstevel@tonic-gate u_long in_space; 1917c478bd9Sstevel@tonic-gate u_long in_hits; 1927c478bd9Sstevel@tonic-gate u_int in_use; 1937c478bd9Sstevel@tonic-gate u_int in_hv; 1947c478bd9Sstevel@tonic-gate int in_flineno; /* conf. file line number */ 1957c478bd9Sstevel@tonic-gate u_short in_pnext; 196ab25eeb5Syz u_char in_v; 197ab25eeb5Syz u_char in_xxx; 1987c478bd9Sstevel@tonic-gate /* From here to the end is covered by IPN_CMPSIZ */ 1997c478bd9Sstevel@tonic-gate u_32_t in_flags; 2007c478bd9Sstevel@tonic-gate u_32_t in_mssclamp; /* if != 0 clamp MSS to this */ 2017c478bd9Sstevel@tonic-gate u_int in_age[2]; 2027c478bd9Sstevel@tonic-gate int in_redir; /* see below for values */ 2037c478bd9Sstevel@tonic-gate int in_p; /* protocol. */ 2047c478bd9Sstevel@tonic-gate i6addr_t in_in[2]; 2057c478bd9Sstevel@tonic-gate i6addr_t in_out[2]; 2067c478bd9Sstevel@tonic-gate i6addr_t in_src[2]; 2077c478bd9Sstevel@tonic-gate frtuc_t in_tuc; 2087c478bd9Sstevel@tonic-gate u_short in_port[2]; 2097c478bd9Sstevel@tonic-gate u_short in_ppip; /* ports per IP. */ 2107c478bd9Sstevel@tonic-gate u_short in_ippip; /* IP #'s per IP# */ 2117c478bd9Sstevel@tonic-gate char in_ifnames[2][LIFNAMSIZ]; 2127c478bd9Sstevel@tonic-gate char in_plabel[APR_LABELLEN]; /* proxy label. */ 2137c478bd9Sstevel@tonic-gate ipftag_t in_tag; 2147c478bd9Sstevel@tonic-gate } ipnat_t; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate #define in_pmin in_port[0] /* Also holds static redir port */ 2177c478bd9Sstevel@tonic-gate #define in_pmax in_port[1] 2187c478bd9Sstevel@tonic-gate #define in_nextip in_next6.in4 2197c478bd9Sstevel@tonic-gate #define in_nip in_next6.in4.s_addr 2207c478bd9Sstevel@tonic-gate #define in_inip in_in[0].in4.s_addr 2217c478bd9Sstevel@tonic-gate #define in_inmsk in_in[1].in4.s_addr 2227c478bd9Sstevel@tonic-gate #define in_outip in_out[0].in4.s_addr 2237c478bd9Sstevel@tonic-gate #define in_outmsk in_out[1].in4.s_addr 2247c478bd9Sstevel@tonic-gate #define in_srcip in_src[0].in4.s_addr 2257c478bd9Sstevel@tonic-gate #define in_srcmsk in_src[1].in4.s_addr 2267c478bd9Sstevel@tonic-gate #define in_scmp in_tuc.ftu_scmp 2277c478bd9Sstevel@tonic-gate #define in_dcmp in_tuc.ftu_dcmp 2287c478bd9Sstevel@tonic-gate #define in_stop in_tuc.ftu_stop 2297c478bd9Sstevel@tonic-gate #define in_dtop in_tuc.ftu_dtop 2307c478bd9Sstevel@tonic-gate #define in_sport in_tuc.ftu_sport 2317c478bd9Sstevel@tonic-gate #define in_dport in_tuc.ftu_dport 2327c478bd9Sstevel@tonic-gate 2337c478bd9Sstevel@tonic-gate /* 2347c478bd9Sstevel@tonic-gate * Bit definitions for in_flags 2357c478bd9Sstevel@tonic-gate */ 2367c478bd9Sstevel@tonic-gate #define IPN_ANY 0x00000 2377c478bd9Sstevel@tonic-gate #define IPN_TCP 0x00001 2387c478bd9Sstevel@tonic-gate #define IPN_UDP 0x00002 2397c478bd9Sstevel@tonic-gate #define IPN_TCPUDP (IPN_TCP|IPN_UDP) 2407c478bd9Sstevel@tonic-gate #define IPN_ICMPERR 0x00004 2417c478bd9Sstevel@tonic-gate #define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR) 2427c478bd9Sstevel@tonic-gate #define IPN_ICMPQUERY 0x00008 243ab25eeb5Syz #define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY) 2447c478bd9Sstevel@tonic-gate #define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR) 2457c478bd9Sstevel@tonic-gate #define IPN_AUTOPORTMAP 0x00010 2467c478bd9Sstevel@tonic-gate #define IPN_IPRANGE 0x00020 2477c478bd9Sstevel@tonic-gate #define IPN_FILTER 0x00040 2487c478bd9Sstevel@tonic-gate #define IPN_SPLIT 0x00080 2497c478bd9Sstevel@tonic-gate #define IPN_ROUNDR 0x00100 2507c478bd9Sstevel@tonic-gate #define IPN_NOTSRC 0x04000 2517c478bd9Sstevel@tonic-gate #define IPN_NOTDST 0x08000 2527c478bd9Sstevel@tonic-gate #define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */ 2537c478bd9Sstevel@tonic-gate #define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */ 2547c478bd9Sstevel@tonic-gate #define IPN_DELETE 0x40000 2557c478bd9Sstevel@tonic-gate #define IPN_STICKY 0x80000 2567c478bd9Sstevel@tonic-gate #define IPN_FRAG 0x100000 257ab25eeb5Syz #define IPN_FIXEDDPORT 0x200000 2585b6dcef9Sdr #define IPN_FINDFORWARD 0x400000 259ab25eeb5Syz #define IPN_IN 0x800000 260ab073b32Sdr #define IPN_SEQUENTIAL 0x1000000 2617c478bd9Sstevel@tonic-gate #define IPN_USERFLAGS (IPN_TCPUDP|IPN_AUTOPORTMAP|IPN_IPRANGE|IPN_SPLIT|\ 2627c478bd9Sstevel@tonic-gate IPN_ROUNDR|IPN_FILTER|IPN_NOTSRC|IPN_NOTDST|\ 263ab073b32Sdr IPN_FRAG|IPN_STICKY|IPN_FIXEDDPORT|IPN_ICMPQUERY|\ 264ab073b32Sdr IPN_SEQUENTIAL) 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * Values for in_redir 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate #define NAT_MAP 0x01 2707c478bd9Sstevel@tonic-gate #define NAT_REDIRECT 0x02 2717c478bd9Sstevel@tonic-gate #define NAT_BIMAP (NAT_MAP|NAT_REDIRECT) 2727c478bd9Sstevel@tonic-gate #define NAT_MAPBLK 0x04 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate #define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */ 2757c478bd9Sstevel@tonic-gate #define USABLE_PORTS (65536 - MAPBLK_MINPORT) 2767c478bd9Sstevel@tonic-gate 277d6c23f6fSyx #define IPN_CMPSIZ (sizeof (ipnat_t) - offsetof(ipnat_t, in_flags)) 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate typedef struct natlookup { 280d6c23f6fSyx i6addr_t nl_inipaddr; 281d6c23f6fSyx i6addr_t nl_outipaddr; 282d6c23f6fSyx i6addr_t nl_realipaddr; 283d6c23f6fSyx int nl_v; 284d6c23f6fSyx int nl_flags; 285d6c23f6fSyx u_short nl_inport; 286d6c23f6fSyx u_short nl_outport; 287d6c23f6fSyx u_short nl_realport; 2887c478bd9Sstevel@tonic-gate } natlookup_t; 2897c478bd9Sstevel@tonic-gate 290d6c23f6fSyx #define nl_inip nl_inipaddr.in4 291d6c23f6fSyx #define nl_outip nl_outipaddr.in4 292d6c23f6fSyx #define nl_realip nl_realipaddr.in4 293d6c23f6fSyx #define nl_inip6 nl_inipaddr.in6 294d6c23f6fSyx #define nl_outip6 nl_outipaddr.in6 295d6c23f6fSyx #define nl_realip6 nl_realipaddr.in6 296d6c23f6fSyx 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate typedef struct nat_save { 2997c478bd9Sstevel@tonic-gate void *ipn_next; 3007c478bd9Sstevel@tonic-gate struct nat ipn_nat; 3017c478bd9Sstevel@tonic-gate struct ipnat ipn_ipnat; 3027c478bd9Sstevel@tonic-gate struct frentry ipn_fr; 3037c478bd9Sstevel@tonic-gate int ipn_dsize; 3047c478bd9Sstevel@tonic-gate char ipn_data[4]; 3057c478bd9Sstevel@tonic-gate } nat_save_t; 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate #define ipn_rule ipn_nat.nat_fr 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate typedef struct natget { 3107c478bd9Sstevel@tonic-gate void *ng_ptr; 3117c478bd9Sstevel@tonic-gate int ng_sz; 3127c478bd9Sstevel@tonic-gate } natget_t; 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate 315ab25eeb5Syz #undef tr_flags 3167c478bd9Sstevel@tonic-gate typedef struct nattrpnt { 3177c478bd9Sstevel@tonic-gate struct in_addr tr_dstip; /* real destination IP# */ 3187c478bd9Sstevel@tonic-gate struct in_addr tr_srcip; /* real source IP# */ 3197c478bd9Sstevel@tonic-gate struct in_addr tr_locip; /* local source IP# */ 3207c478bd9Sstevel@tonic-gate u_int tr_flags; 3217c478bd9Sstevel@tonic-gate int tr_expire; 3227c478bd9Sstevel@tonic-gate u_short tr_dstport; /* real destination port# */ 3237c478bd9Sstevel@tonic-gate u_short tr_srcport; /* real source port# */ 3247c478bd9Sstevel@tonic-gate u_short tr_locport; /* local source port# */ 3257c478bd9Sstevel@tonic-gate struct nattrpnt *tr_hnext; 3267c478bd9Sstevel@tonic-gate struct nattrpnt **tr_phnext; 3277c478bd9Sstevel@tonic-gate struct nattrpnt *tr_next; 3287c478bd9Sstevel@tonic-gate struct nattrpnt **tr_pnext; /* previous next */ 3297c478bd9Sstevel@tonic-gate } nattrpnt_t; 3307c478bd9Sstevel@tonic-gate 3317c478bd9Sstevel@tonic-gate #define TN_CMPSIZ offsetof(nattrpnt_t, tr_hnext) 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate 3347c478bd9Sstevel@tonic-gate /* 3357c478bd9Sstevel@tonic-gate * This structure gets used to help NAT sessions keep the same NAT rule (and 3367c478bd9Sstevel@tonic-gate * thus translation for IP address) when: 3377c478bd9Sstevel@tonic-gate * (a) round-robin redirects are in use 3387c478bd9Sstevel@tonic-gate * (b) different IP add 3397c478bd9Sstevel@tonic-gate */ 3407c478bd9Sstevel@tonic-gate typedef struct hostmap { 3417c478bd9Sstevel@tonic-gate struct hostmap *hm_next; 3427c478bd9Sstevel@tonic-gate struct hostmap **hm_pnext; 343f4b3ec61Sdh struct hostmap *hm_hnext; 344f4b3ec61Sdh struct hostmap **hm_phnext; 3457c478bd9Sstevel@tonic-gate struct ipnat *hm_ipnat; 346d6c23f6fSyx i6addr_t hm_src; 347d6c23f6fSyx i6addr_t hm_dst; 348d6c23f6fSyx i6addr_t hm_map; 3497c478bd9Sstevel@tonic-gate u_32_t hm_port; 3507c478bd9Sstevel@tonic-gate int hm_ref; 351d6c23f6fSyx int hm_v; 3527c478bd9Sstevel@tonic-gate } hostmap_t; 3537c478bd9Sstevel@tonic-gate 354d6c23f6fSyx #define hm_srcip hm_src.in4 355d6c23f6fSyx #define hm_dstip hm_dst.in4 356d6c23f6fSyx #define hm_mapip hm_map.in4 357d6c23f6fSyx #define hm_srcip6 hm_src.in6 358d6c23f6fSyx #define hm_dstip6 hm_dst.in6 359d6c23f6fSyx #define hm_mapip6 hm_map.in6 360d6c23f6fSyx 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate /* 3637c478bd9Sstevel@tonic-gate * Structure used to pass information in to nat_newmap and nat_newrdr. 3647c478bd9Sstevel@tonic-gate */ 3657c478bd9Sstevel@tonic-gate typedef struct natinfo { 3667c478bd9Sstevel@tonic-gate ipnat_t *nai_np; 3677c478bd9Sstevel@tonic-gate u_32_t nai_nflags; 3687c478bd9Sstevel@tonic-gate u_32_t nai_flags; 3697c478bd9Sstevel@tonic-gate struct in_addr nai_ip; 3707c478bd9Sstevel@tonic-gate u_short nai_port; 3717c478bd9Sstevel@tonic-gate u_short nai_nport; 3727c478bd9Sstevel@tonic-gate u_short nai_sport; 3737c478bd9Sstevel@tonic-gate u_short nai_dport; 3747c478bd9Sstevel@tonic-gate } natinfo_t; 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate typedef struct natstat { 3787c478bd9Sstevel@tonic-gate u_long ns_mapped[2]; 3797c478bd9Sstevel@tonic-gate u_long ns_rules; 3807c478bd9Sstevel@tonic-gate u_long ns_added; 3817c478bd9Sstevel@tonic-gate u_long ns_expire; 3827c478bd9Sstevel@tonic-gate u_long ns_inuse; 3837c478bd9Sstevel@tonic-gate u_long ns_logged; 3847c478bd9Sstevel@tonic-gate u_long ns_logfail; 3857c478bd9Sstevel@tonic-gate u_long ns_memfail; 3867c478bd9Sstevel@tonic-gate u_long ns_badnat; 3877c478bd9Sstevel@tonic-gate u_long ns_addtrpnt; 3887c478bd9Sstevel@tonic-gate nat_t **ns_table[2]; 3897c478bd9Sstevel@tonic-gate hostmap_t **ns_maptable; 3907c478bd9Sstevel@tonic-gate ipnat_t *ns_list; 3917c478bd9Sstevel@tonic-gate void *ns_apslist; 3927c478bd9Sstevel@tonic-gate u_int ns_wilds; 3937c478bd9Sstevel@tonic-gate u_int ns_nattab_sz; 394ab25eeb5Syz u_int ns_nattab_max; 3957c478bd9Sstevel@tonic-gate u_int ns_rultab_sz; 3967c478bd9Sstevel@tonic-gate u_int ns_rdrtab_sz; 3977c478bd9Sstevel@tonic-gate u_int ns_trpntab_sz; 3987c478bd9Sstevel@tonic-gate u_int ns_hostmap_sz; 3997c478bd9Sstevel@tonic-gate nat_t *ns_instances; 4007c478bd9Sstevel@tonic-gate nattrpnt_t *ns_trpntlist; 401f4b3ec61Sdh hostmap_t *ns_maplist; 4027c478bd9Sstevel@tonic-gate u_long *ns_bucketlen[2]; 4035b48165cSJohn Ojemann u_int ns_orphans; 40433f2fefdSDarren Reed u_long ns_uncreate[2][2]; 4057c478bd9Sstevel@tonic-gate } natstat_t; 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate typedef struct natlog { 408d6c23f6fSyx i6addr_t nlg_origip; 409d6c23f6fSyx i6addr_t nlg_outip; 410d6c23f6fSyx i6addr_t nlg_inip; 411d6c23f6fSyx u_short nlg_origport; 412d6c23f6fSyx u_short nlg_outport; 413d6c23f6fSyx u_short nlg_inport; 414d6c23f6fSyx u_short nlg_type; 415d6c23f6fSyx int nlg_rule; 416d6c23f6fSyx U_QUAD_T nlg_pkts[2]; 417d6c23f6fSyx U_QUAD_T nlg_bytes[2]; 418d6c23f6fSyx u_char nlg_p; 419d6c23f6fSyx int nlg_v; 4207c478bd9Sstevel@tonic-gate } natlog_t; 4217c478bd9Sstevel@tonic-gate 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate #define NL_NEWMAP NAT_MAP 4247c478bd9Sstevel@tonic-gate #define NL_NEWRDR NAT_REDIRECT 4257c478bd9Sstevel@tonic-gate #define NL_NEWBIMAP NAT_BIMAP 4267c478bd9Sstevel@tonic-gate #define NL_NEWBLOCK NAT_MAPBLK 4275b48165cSJohn Ojemann #define NL_DESTROY 0xfffc 4287c478bd9Sstevel@tonic-gate #define NL_CLONE 0xfffd 4297c478bd9Sstevel@tonic-gate #define NL_FLUSH 0xfffe 4307c478bd9Sstevel@tonic-gate #define NL_EXPIRE 0xffff 4317c478bd9Sstevel@tonic-gate 432d6c23f6fSyx #define NAT_HASH_FN(k, l, m) (((k) + ((k) >> 12) + l) % (m)) 433d6c23f6fSyx #define NAT_HASH_FN6(k, l, m) ((((u_32_t *)(k))[3] \ 434d6c23f6fSyx + (((u_32_t *)(k))[3] >> 12) \ 435d6c23f6fSyx + (((u_32_t *)(k))[2]) \ 436d6c23f6fSyx + (((u_32_t *)(k))[2] >> 12) \ 437d6c23f6fSyx + (((u_32_t *)(k))[1]) \ 438d6c23f6fSyx + (((u_32_t *)(k))[1] >> 12) \ 439d6c23f6fSyx + (((u_32_t *)(k))[0]) \ 440d6c23f6fSyx + (((u_32_t *)(k))[0] >> 12) \ 441d6c23f6fSyx + l) % (m)) 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate #define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16)) 444d6c23f6fSyx #define LONG_SUM6(in) (LONG_SUM(ntohl(((u_32_t *)(in))[0])) + \ 445d6c23f6fSyx LONG_SUM(ntohl(((u_32_t *)(in))[1])) + \ 446d6c23f6fSyx LONG_SUM(ntohl(((u_32_t *)(in))[2])) + \ 447d6c23f6fSyx LONG_SUM(ntohl(((u_32_t *)(in))[3]))) 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate #define CALC_SUMD(s1, s2, sd) { \ 4507c478bd9Sstevel@tonic-gate (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 4517c478bd9Sstevel@tonic-gate (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 4527c478bd9Sstevel@tonic-gate /* Do it twice */ \ 4537c478bd9Sstevel@tonic-gate (s1) = ((s1) & 0xffff) + ((s1) >> 16); \ 4547c478bd9Sstevel@tonic-gate (s2) = ((s2) & 0xffff) + ((s2) >> 16); \ 4557c478bd9Sstevel@tonic-gate /* Because ~1 == -2, We really need ~1 == -1 */ \ 4567c478bd9Sstevel@tonic-gate if ((s1) > (s2)) (s2)--; \ 4577c478bd9Sstevel@tonic-gate (sd) = (s2) - (s1); \ 4587c478bd9Sstevel@tonic-gate (sd) = ((sd) & 0xffff) + ((sd) >> 16); } 4597c478bd9Sstevel@tonic-gate 4607c478bd9Sstevel@tonic-gate #define NAT_SYSSPACE 0x80000000 4617c478bd9Sstevel@tonic-gate #define NAT_LOCKHELD 0x40000000 4627c478bd9Sstevel@tonic-gate 463f4b3ec61Sdh extern void fr_natsync __P((void *, ipf_stack_t *)); 464d6c23f6fSyx extern void fr_nataddrsync __P((int, void *, void *, ipf_stack_t *)); 465d6c23f6fSyx extern void fr_natifpsync __P((int, int, void *, char *, ipf_stack_t *)); 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate #if defined(__OpenBSD__) 468f4b3ec61Sdh extern void nat_ifdetach __P((void *, ipf_stack_t *)); 4697c478bd9Sstevel@tonic-gate #endif 470f4b3ec61Sdh extern int fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int, int, void *, ipf_stack_t *)); 471f4b3ec61Sdh extern int fr_natinit __P((ipf_stack_t *)); 4727c478bd9Sstevel@tonic-gate extern nat_t *nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int)); 4737c478bd9Sstevel@tonic-gate extern nat_t *nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr, 4747c478bd9Sstevel@tonic-gate struct in_addr)); 4757c478bd9Sstevel@tonic-gate extern void fix_datacksum __P((u_short *, u_32_t)); 4767c478bd9Sstevel@tonic-gate extern nat_t *nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr, 4777c478bd9Sstevel@tonic-gate struct in_addr)); 4787c478bd9Sstevel@tonic-gate extern nat_t *nat_tnlookup __P((fr_info_t *, int)); 4797c478bd9Sstevel@tonic-gate extern nat_t *nat_maplookup __P((void *, u_int, struct in_addr, 4807c478bd9Sstevel@tonic-gate struct in_addr)); 481f4b3ec61Sdh extern nat_t *nat_lookupredir __P((natlookup_t *, ipf_stack_t *)); 4827c478bd9Sstevel@tonic-gate extern nat_t *nat_icmperrorlookup __P((fr_info_t *, int)); 4837c478bd9Sstevel@tonic-gate extern nat_t *nat_icmperror __P((fr_info_t *, u_int *, int)); 484ea8244dcSJohn Ojemann extern int nat_delete __P((struct nat *, int, ipf_stack_t *)); 485f4b3ec61Sdh extern int nat_insert __P((nat_t *, int, ipf_stack_t *)); 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate extern int fr_checknatout __P((fr_info_t *, u_32_t *)); 4887c478bd9Sstevel@tonic-gate extern int fr_natout __P((fr_info_t *, nat_t *, int, u_32_t)); 4897c478bd9Sstevel@tonic-gate extern int fr_checknatin __P((fr_info_t *, u_32_t *)); 4907c478bd9Sstevel@tonic-gate extern int fr_natin __P((fr_info_t *, nat_t *, int, u_32_t)); 491f4b3ec61Sdh extern void fr_natunload __P((ipf_stack_t *)); 49233f2fefdSDarren Reed extern void nat_uncreate __P((fr_info_t *)); 493f4b3ec61Sdh extern void fr_natexpire __P((ipf_stack_t *)); 494f4b3ec61Sdh extern void nat_log __P((struct nat *, u_int, ipf_stack_t *)); 495381a2a9aSdr extern void fix_incksum __P((u_short *, u_32_t)); 496381a2a9aSdr extern void fix_outcksum __P((u_short *, u_32_t)); 497f4b3ec61Sdh extern void fr_ipnatderef __P((ipnat_t **, ipf_stack_t *)); 498f4b3ec61Sdh extern void fr_natderef __P((nat_t **, ipf_stack_t *)); 4997c478bd9Sstevel@tonic-gate extern u_short *nat_proto __P((fr_info_t *, nat_t *, u_int)); 5007c478bd9Sstevel@tonic-gate extern void nat_update __P((fr_info_t *, nat_t *, ipnat_t *)); 501f4b3ec61Sdh extern void fr_setnatqueue __P((nat_t *, int, ipf_stack_t *)); 50290b0a856Sjojemann extern void fr_hostmapdel __P((hostmap_t **)); 503e8d569f4SAlexandr Nedvedicky #if SOLARIS2 >= 10 504e8d569f4SAlexandr Nedvedicky extern void fr_natifindexsync __P((void *, void *, ipf_stack_t *)); 505e8d569f4SAlexandr Nedvedicky #endif 5067c478bd9Sstevel@tonic-gate 507d6c23f6fSyx extern nat_t *fr_natclone __P((fr_info_t *, nat_t *)); 508d6c23f6fSyx extern void nat_delrdr __P((struct ipnat *)); 509d6c23f6fSyx extern void nat_delnat __P((struct ipnat *)); 510d6c23f6fSyx extern int nat_wildok __P((nat_t *, int, int, int, int)); 511d6c23f6fSyx extern void nat_calc_chksum_diffs __P((nat_t *)); 512d6c23f6fSyx 513d6c23f6fSyx #ifdef USE_INET6 514d6c23f6fSyx extern void nat6_addnat __P((ipnat_t *, ipf_stack_t *)); 515d6c23f6fSyx extern void nat6_addrdr __P((ipnat_t *, ipf_stack_t *)); 516d6c23f6fSyx extern nat_t *nat6_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int)); 517d6c23f6fSyx extern nat_t *nat6_outlookup __P((fr_info_t *, u_int, u_int, 518d6c23f6fSyx struct in6_addr *, struct in6_addr *)); 519d6c23f6fSyx extern nat_t *nat6_inlookup __P((fr_info_t *, u_int, u_int, 520d6c23f6fSyx struct in6_addr *, struct in6_addr *)); 521d6c23f6fSyx extern nat_t *nat6_lookupredir __P((natlookup_t *, ipf_stack_t *)); 522d6c23f6fSyx extern nat_t *nat6_icmperrorlookup __P((fr_info_t *, int)); 523d6c23f6fSyx extern nat_t *nat6_icmperror __P((fr_info_t *, u_int *, int)); 524d6c23f6fSyx extern int nat6_insert __P((nat_t *, int, ipf_stack_t *)); 525d6c23f6fSyx extern int fr_checknat6out __P((fr_info_t *, u_32_t *)); 526d6c23f6fSyx extern int fr_nat6out __P((fr_info_t *, nat_t *, int, u_32_t)); 527d6c23f6fSyx extern int fr_checknat6in __P((fr_info_t *, u_32_t *)); 528d6c23f6fSyx extern int fr_nat6in __P((fr_info_t *, nat_t *, int, u_32_t)); 529d6c23f6fSyx #endif 530d6c23f6fSyx 5317c478bd9Sstevel@tonic-gate #endif /* __IP_NAT_H__ */ 532