1/* $FreeBSD: head/sys/contrib/ipfilter/netinet/ip_nat.h 145522 2005-04-25 18:43:14Z darrenr $ */ 2 |
3/* |
4 * Copyright (C) 1995-2001, 2003 by Darren Reed. |
5 * 6 * See the IPFILTER.LICENCE file for details on licencing. 7 * 8 * @(#)ip_nat.h 1.5 2/4/96 |
9 * $FreeBSD: head/sys/contrib/ipfilter/netinet/ip_nat.h 145522 2005-04-25 18:43:14Z darrenr $ 10 * Id: ip_nat.h,v 2.90.2.9 2005/03/28 11:09:55 darrenr Exp |
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__) |
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#define SIOCPROXY _IOWR('r', 64, struct ap_control) |
26#else |
27#define SIOCADNAT _IOW(r, 60, struct ipfobj) 28#define SIOCRMNAT _IOW(r, 61, struct ipfobj) 29#define SIOCGNATS _IOWR(r, 62, struct ipfobj) 30#define SIOCGNATL _IOWR(r, 63, struct ipfobj) 31#define SIOCPROXY _IOWR(r, 64, struct ap_control) |
32#endif 33 |
34#undef LARGE_NAT /* define this if you're setting up a system to NAT |
35 * LARGE numbers of networks/hosts - i.e. in the 36 * hundreds or thousands. In such a case, you should 37 * also change the RDR_SIZE and NAT_SIZE below to more 38 * appropriate sizes. The figures below were used for 39 * a setup with 1000-2000 networks to NAT. 40 */ |
41#ifndef NAT_SIZE 42# ifdef LARGE_NAT |
43# define NAT_SIZE 2047 44# else 45# define NAT_SIZE 127 46# endif 47#endif |
48#ifndef RDR_SIZE 49# ifdef LARGE_NAT |
50# define RDR_SIZE 2047 51# else 52# define RDR_SIZE 127 53# endif 54#endif |
55#ifndef HOSTMAP_SIZE 56# ifdef LARGE_NAT |
57# define HOSTMAP_SIZE 8191 58# else 59# define HOSTMAP_SIZE 2047 60# endif 61#endif 62#ifndef NAT_TABLE_MAX |
63/* 64 * This is newly introduced and for the sake of "least surprise", the numbers 65 * present aren't what we'd normally use for creating a proper hash table. 66 */ |
67# ifdef LARGE_NAT 68# define NAT_TABLE_MAX 180000 69# else 70# define NAT_TABLE_MAX 30000 71# endif 72#endif |
73#ifndef NAT_TABLE_SZ 74# ifdef LARGE_NAT |
75# define NAT_TABLE_SZ 16383 76# else 77# define NAT_TABLE_SZ 2047 78# endif 79#endif 80#ifndef APR_LABELLEN 81#define APR_LABELLEN 16 82#endif 83#define NAT_HW_CKSUM 0x80000000 84 85#define DEF_NAT_AGE 1200 /* 10 minutes (600 seconds) */ 86 |
87struct ipstate; |
88struct ap_session; 89 90typedef struct nat { |
91 ipfmutex_t nat_lock; |
92 struct nat *nat_next; |
93 struct nat **nat_pnext; |
94 struct nat *nat_hnext[2]; 95 struct nat **nat_phnext[2]; |
96 struct hostmap *nat_hm; 97 void *nat_data; |
98 struct nat **nat_me; |
99 struct ipstate *nat_state; 100 struct ap_session *nat_aps; /* proxy session */ 101 frentry_t *nat_fr; /* filter rule ptr if appropriate */ 102 struct ipnat *nat_ptr; /* pointer back to the rule */ 103 void *nat_ifps[2]; 104 void *nat_sync; 105 ipftqent_t nat_tqe; 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_inip6; 111 i6addr_t nat_outip6; 112 i6addr_t nat_oip6; /* other ip */ 113 U_QUAD_T nat_pkts[2]; 114 U_QUAD_T nat_bytes[2]; 115 union { 116 udpinfo_t nat_unu; 117 tcpinfo_t nat_unt; 118 icmpinfo_t nat_uni; 119 greinfo_t nat_ugre; 120 } nat_un; 121 u_short nat_oport; /* other port */ 122 u_short nat_use; 123 u_char nat_p; /* protocol for NAT */ 124 int nat_dir; 125 int nat_ref; /* reference count */ 126 int nat_hv[2]; 127 char nat_ifnames[2][LIFNAMSIZ]; 128 int nat_rev; /* 0 = forward, 1 = reverse */ |
129} nat_t; 130 |
131#define nat_inip nat_inip6.in4 132#define nat_outip nat_outip6.in4 133#define nat_oip nat_oip6.in4 134#define nat_age nat_tqe.tqe_die 135#define nat_inport nat_un.nat_unt.ts_sport 136#define nat_outport nat_un.nat_unt.ts_dport 137#define nat_type nat_un.nat_uni.ici_type 138#define nat_seq nat_un.nat_uni.ici_seq 139#define nat_id nat_un.nat_uni.ici_id 140#define nat_tcpstate nat_tqe.tqe_state 141 142/* 143 * Values for nat_dir 144 */ 145#define NAT_INBOUND 0 146#define NAT_OUTBOUND 1 147 148/* 149 * Definitions for nat_flags 150 */ 151#define NAT_TCP 0x0001 /* IPN_TCP */ 152#define NAT_UDP 0x0002 /* IPN_UDP */ 153#define NAT_ICMPERR 0x0004 /* IPN_ICMPERR */ 154#define NAT_ICMPQUERY 0x0008 /* IPN_ICMPQUERY */ 155#define NAT_SEARCH 0x0010 156#define NAT_SLAVE 0x0020 /* Slave connection for a proxy */ 157#define NAT_NOTRULEPORT 0x0040 158 159#define NAT_TCPUDP (NAT_TCP|NAT_UDP) 160#define NAT_TCPUDPICMP (NAT_TCP|NAT_UDP|NAT_ICMPERR) 161#define NAT_TCPUDPICMPQ (NAT_TCP|NAT_UDP|NAT_ICMPQUERY) 162#define NAT_FROMRULE (NAT_TCP|NAT_UDP) 163 164/* 0x0100 reserved for FI_W_SPORT */ 165/* 0x0200 reserved for FI_W_DPORT */ 166/* 0x0400 reserved for FI_W_SADDR */ 167/* 0x0800 reserved for FI_W_DADDR */ 168/* 0x1000 reserved for FI_W_NEWFR */ 169/* 0x2000 reserved for SI_CLONE */ 170/* 0x4000 reserved for SI_CLONED */ 171/* 0x8000 reserved for SI_IGNOREPKT */ 172 173#define NAT_DEBUG 0x800000 174 |
175typedef struct ipnat { |
176 struct ipnat *in_next; /* NAT rule list next */ 177 struct ipnat *in_rnext; /* rdr rule hash next */ 178 struct ipnat **in_prnext; /* prior rdr next ptr */ 179 struct ipnat *in_mnext; /* map rule hash next */ 180 struct ipnat **in_pmnext; /* prior map next ptr */ 181 struct ipftq *in_tqehead[2]; 182 void *in_ifps[2]; 183 void *in_apr; 184 char *in_comment; 185 i6addr_t in_next6; 186 u_long in_space; 187 u_long in_hits; 188 u_int in_use; 189 u_int in_hv; 190 int in_flineno; /* conf. file line number */ 191 u_short in_pnext; 192 u_char in_v; 193 u_char in_xxx; 194 /* From here to the end is covered by IPN_CMPSIZ */ 195 u_32_t in_flags; 196 u_32_t in_mssclamp; /* if != 0 clamp MSS to this */ 197 u_int in_age[2]; 198 int in_redir; /* see below for values */ 199 int in_p; /* protocol. */ 200 i6addr_t in_in[2]; 201 i6addr_t in_out[2]; 202 i6addr_t in_src[2]; 203 frtuc_t in_tuc; 204 u_short in_port[2]; 205 u_short in_ppip; /* ports per IP. */ 206 u_short in_ippip; /* IP #'s per IP# */ 207 char in_ifnames[2][LIFNAMSIZ]; 208 char in_plabel[APR_LABELLEN]; /* proxy label. */ 209 ipftag_t in_tag; |
210} ipnat_t; 211 212#define in_pmin in_port[0] /* Also holds static redir port */ 213#define in_pmax in_port[1] |
214#define in_nextip in_next6.in4 215#define in_nip in_next6.in4.s_addr 216#define in_inip in_in[0].in4.s_addr 217#define in_inmsk in_in[1].in4.s_addr 218#define in_outip in_out[0].in4.s_addr 219#define in_outmsk in_out[1].in4.s_addr 220#define in_srcip in_src[0].in4.s_addr 221#define in_srcmsk in_src[1].in4.s_addr |
222#define in_scmp in_tuc.ftu_scmp 223#define in_dcmp in_tuc.ftu_dcmp 224#define in_stop in_tuc.ftu_stop 225#define in_dtop in_tuc.ftu_dtop 226#define in_sport in_tuc.ftu_sport 227#define in_dport in_tuc.ftu_dport 228 |
229/* 230 * Bit definitions for in_flags 231 */ 232#define IPN_ANY 0x00000 233#define IPN_TCP 0x00001 234#define IPN_UDP 0x00002 235#define IPN_TCPUDP (IPN_TCP|IPN_UDP) 236#define IPN_ICMPERR 0x00004 237#define IPN_TCPUDPICMP (IPN_TCP|IPN_UDP|IPN_ICMPERR) 238#define IPN_ICMPQUERY 0x00008 239#define IPN_TCPUDPICMPQ (IPN_TCP|IPN_UDP|IPN_ICMPQUERY) 240#define IPN_RF (IPN_TCPUDP|IPN_DELETE|IPN_ICMPERR) 241#define IPN_AUTOPORTMAP 0x00010 242#define IPN_IPRANGE 0x00020 243#define IPN_FILTER 0x00040 244#define IPN_SPLIT 0x00080 245#define IPN_ROUNDR 0x00100 246#define IPN_NOTSRC 0x04000 247#define IPN_NOTDST 0x08000 248#define IPN_DYNSRCIP 0x10000 /* dynamic src IP# */ 249#define IPN_DYNDSTIP 0x20000 /* dynamic dst IP# */ 250#define IPN_DELETE 0x40000 251#define IPN_STICKY 0x80000 252#define IPN_FRAG 0x100000 253#define IPN_FIXEDDPORT 0x200000 254#define IPN_FINDFORWARD 0x400000 255#define IPN_IN 0x800000 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 |
260/* 261 * Values for in_redir 262 */ |
263#define NAT_MAP 0x01 264#define NAT_REDIRECT 0x02 265#define NAT_BIMAP (NAT_MAP|NAT_REDIRECT) 266#define NAT_MAPBLK 0x04 |
267 268#define MAPBLK_MINPORT 1024 /* don't use reserved ports for src port */ 269#define USABLE_PORTS (65536 - MAPBLK_MINPORT) 270 271#define IPN_CMPSIZ (sizeof(ipnat_t) - offsetof(ipnat_t, in_flags)) 272 273typedef struct natlookup { 274 struct in_addr nl_inip; --- 18 unchanged lines hidden (view full) --- 293#define ipn_rule ipn_nat.nat_fr 294 295typedef struct natget { 296 void *ng_ptr; 297 int ng_sz; 298} natget_t; 299 300 |
301typedef struct nattrpnt { 302 struct in_addr tr_dstip; /* real destination IP# */ 303 struct in_addr tr_srcip; /* real source IP# */ 304 struct in_addr tr_locip; /* local source IP# */ 305 u_int tr_flags; 306 int tr_expire; 307 u_short tr_dstport; /* real destination port# */ 308 u_short tr_srcport; /* real source port# */ 309 u_short tr_locport; /* local source port# */ 310 struct nattrpnt *tr_hnext; 311 struct nattrpnt **tr_phnext; 312 struct nattrpnt *tr_next; 313 struct nattrpnt **tr_pnext; /* previous next */ 314} nattrpnt_t; 315 316#define TN_CMPSIZ offsetof(nattrpnt_t, tr_hnext) 317 318 319/* 320 * This structure gets used to help NAT sessions keep the same NAT rule (and 321 * thus translation for IP address) when: 322 * (a) round-robin redirects are in use 323 * (b) different IP add 324 */ |
325typedef struct hostmap { 326 struct hostmap *hm_next; 327 struct hostmap **hm_pnext; 328 struct ipnat *hm_ipnat; |
329 struct in_addr hm_srcip; 330 struct in_addr hm_dstip; |
331 struct in_addr hm_mapip; |
332 u_32_t hm_port; 333 int hm_ref; |
334} hostmap_t; 335 336 |
337/* 338 * Structure used to pass information in to nat_newmap and nat_newrdr. 339 */ 340typedef struct natinfo { 341 ipnat_t *nai_np; 342 u_32_t nai_sum1; 343 u_32_t nai_sum2; 344 u_32_t nai_nflags; 345 u_32_t nai_flags; 346 struct in_addr nai_ip; 347 u_short nai_port; 348 u_short nai_nport; 349 u_short nai_sport; 350 u_short nai_dport; 351} natinfo_t; 352 353 |
354typedef struct natstat { 355 u_long ns_mapped[2]; 356 u_long ns_rules; 357 u_long ns_added; 358 u_long ns_expire; 359 u_long ns_inuse; 360 u_long ns_logged; 361 u_long ns_logfail; 362 u_long ns_memfail; 363 u_long ns_badnat; |
364 u_long ns_addtrpnt; |
365 nat_t **ns_table[2]; 366 hostmap_t **ns_maptable; 367 ipnat_t *ns_list; 368 void *ns_apslist; |
369 u_int ns_wilds; |
370 u_int ns_nattab_sz; |
371 u_int ns_nattab_max; |
372 u_int ns_rultab_sz; 373 u_int ns_rdrtab_sz; |
374 u_int ns_trpntab_sz; |
375 u_int ns_hostmap_sz; 376 nat_t *ns_instances; |
377 nattrpnt_t *ns_trpntlist; 378 u_long *ns_bucketlen[2]; |
379} natstat_t; 380 |
381typedef struct natlog { 382 struct in_addr nl_origip; 383 struct in_addr nl_outip; 384 struct in_addr nl_inip; 385 u_short nl_origport; 386 u_short nl_outport; 387 u_short nl_inport; 388 u_short nl_type; 389 int nl_rule; |
390 U_QUAD_T nl_pkts[2]; 391 U_QUAD_T nl_bytes[2]; |
392 u_char nl_p; 393} natlog_t; 394 395 396#define NL_NEWMAP NAT_MAP 397#define NL_NEWRDR NAT_REDIRECT 398#define NL_NEWBIMAP NAT_BIMAP 399#define NL_NEWBLOCK NAT_MAPBLK |
400#define NL_CLONE 0xfffd |
401#define NL_FLUSH 0xfffe 402#define NL_EXPIRE 0xffff 403 404#define NAT_HASH_FN(k,l,m) (((k) + ((k) >> 12) + l) % (m)) 405 406#define LONG_SUM(in) (((in) & 0xffff) + ((in) >> 16)) 407 408#define CALC_SUMD(s1, s2, sd) { \ --- 5 unchanged lines hidden (view full) --- 414 /* Because ~1 == -2, We really need ~1 == -1 */ \ 415 if ((s1) > (s2)) (s2)--; \ 416 (sd) = (s2) - (s1); \ 417 (sd) = ((sd) & 0xffff) + ((sd) >> 16); } 418 419#define NAT_SYSSPACE 0x80000000 420#define NAT_LOCKHELD 0x40000000 421 |
422 |
423extern u_int ipf_nattable_sz; |
424extern u_int ipf_nattable_max; |
425extern u_int ipf_natrules_sz; 426extern u_int ipf_rdrrules_sz; |
427extern u_int ipf_hostmap_sz; 428extern u_int fr_nat_maxbucket; 429extern u_int fr_nat_maxbucket_reset; |
430extern int fr_nat_lock; |
431extern void fr_natsync __P((void *)); |
432extern u_long fr_defnatage; 433extern u_long fr_defnaticmpage; |
434extern u_long fr_defnatipage; 435 /* nat_table[0] -> hashed list sorted by inside (ip, port) */ 436 /* nat_table[1] -> hashed list sorted by outside (ip, port) */ |
437extern nat_t **nat_table[2]; 438extern nat_t *nat_instances; |
439extern ipnat_t *nat_list; |
440extern ipnat_t **nat_rules; 441extern ipnat_t **rdr_rules; |
442extern ipftq_t *nat_utqe; |
443extern natstat_t nat_stats; |
444 |
445#if defined(__OpenBSD__) 446extern void nat_ifdetach __P((void *)); 447#endif |
448extern int fr_nat_ioctl __P((caddr_t, ioctlcmd_t, int)); 449extern int fr_natinit __P((void)); 450extern nat_t *nat_new __P((fr_info_t *, ipnat_t *, nat_t **, u_int, int)); |
451extern nat_t *nat_outlookup __P((fr_info_t *, u_int, u_int, struct in_addr, |
452 struct in_addr)); 453extern void fix_datacksum __P((u_short *, u_32_t)); |
454extern nat_t *nat_inlookup __P((fr_info_t *, u_int, u_int, struct in_addr, |
455 struct in_addr)); 456extern nat_t *nat_tnlookup __P((fr_info_t *, int)); 457extern nat_t *nat_maplookup __P((void *, u_int, struct in_addr, 458 struct in_addr)); |
459extern nat_t *nat_lookupredir __P((natlookup_t *)); |
460extern nat_t *nat_icmperrorlookup __P((fr_info_t *, int)); 461extern nat_t *nat_icmperror __P((fr_info_t *, u_int *, int)); 462extern int nat_insert __P((nat_t *, int)); |
463 |
464extern int fr_checknatout __P((fr_info_t *, u_32_t *)); 465extern int fr_natout __P((fr_info_t *, nat_t *, int, u_32_t)); 466extern int fr_checknatin __P((fr_info_t *, u_32_t *)); 467extern int fr_natin __P((fr_info_t *, nat_t *, int, u_32_t)); 468extern void fr_natunload __P((void)); 469extern void fr_natexpire __P((void)); |
470extern void nat_log __P((struct nat *, u_int)); 471extern void fix_incksum __P((fr_info_t *, u_short *, u_32_t)); 472extern void fix_outcksum __P((fr_info_t *, u_short *, u_32_t)); |
473extern void fr_natderef __P((nat_t **)); 474extern u_short *nat_proto __P((fr_info_t *, nat_t *, u_int)); 475extern void nat_update __P((fr_info_t *, nat_t *, ipnat_t *)); 476extern void fr_setnatqueue __P((nat_t *, int)); |
477 478#endif /* __IP_NAT_H__ */ |