1 /* 2 * Copyright (c) 2014 The DragonFly Project. All rights reserved. 3 * 4 * This code is derived from software contributed to The DragonFly Project 5 * by Bill Yuan <bycn82@gmail.com> 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * 3. Neither the name of The DragonFly Project nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific, prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, 27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #ifndef _IP_FW_NAT_H 36 #define _IP_FW_NAT_H 37 38 #define MODULE_NAT_ID 4 39 #define MODULE_NAT_NAME "nat" 40 41 #ifdef _KERNEL 42 MALLOC_DEFINE(M_IPFW_NAT, "IPFW3/NAT", "IPFW3/NAT 's"); 43 #endif 44 45 enum ipfw_nat_opcodes { 46 O_NAT_NAT, 47 }; 48 49 /* Redirect modes id. */ 50 #define REDIR_ADDR 0x01 51 #define REDIR_PORT 0x02 52 #define REDIR_PROTO 0x04 53 54 /* Server pool support (LSNAT). */ 55 struct cfg_spool { 56 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */ 57 struct in_addr addr; 58 u_short port; 59 }; 60 61 struct cfg_redir { 62 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */ 63 u_int16_t mode; /* type of redirect mode */ 64 struct in_addr laddr; /* local ip address */ 65 struct in_addr paddr; /* public ip address */ 66 struct in_addr raddr; /* remote ip address */ 67 u_short lport; /* local port */ 68 u_short pport; /* public port */ 69 u_short rport; /* remote port */ 70 u_short pport_cnt; /* number of public ports */ 71 u_short rport_cnt; /* number of remote ports */ 72 int proto; /* protocol: tcp/udp */ 73 struct alias_link **alink; 74 /* num of entry in spool chain */ 75 u_int16_t spool_cnt; 76 /* chain of spool instances */ 77 LIST_HEAD(spool_chain, cfg_spool) spool_chain; 78 }; 79 80 /* Nat configuration data struct. */ 81 struct cfg_nat { 82 /* chain of nat instances */ 83 LIST_ENTRY(cfg_nat) _next; 84 int id; /* nat id */ 85 struct in_addr ip; /* nat ip address */ 86 char if_name[IF_NAMESIZE]; /* interface name */ 87 int mode; /* aliasing mode */ 88 struct libalias *lib; /* libalias instance */ 89 /* number of entry in spool chain */ 90 int redir_cnt; 91 /* chain of redir instances */ 92 LIST_HEAD(redir_chain, cfg_redir) redir_chain; 93 }; 94 95 #define SOF_NAT sizeof(struct cfg_nat) 96 #define SOF_REDIR sizeof(struct cfg_redir) 97 #define SOF_SPOOL sizeof(struct cfg_spool) 98 99 /* Nat command. */ 100 typedef struct _ipfw_insn_nat { 101 ipfw_insn o; 102 struct cfg_nat *nat; 103 } ipfw_insn_nat; 104 105 #define LOOKUP_NAT(l, i, p) do { \ 106 LIST_FOREACH((p), &(l.nat), _next){ \ 107 if((p)->id == (i)){ \ 108 break; \ 109 } \ 110 } \ 111 } while (0) 112 113 #define HOOK_NAT(b, p) do { \ 114 LIST_INSERT_HEAD(b, p, _next); \ 115 } while (0) 116 117 #define UNHOOK_NAT(p) do { \ 118 LIST_REMOVE(p, _next); \ 119 } while (0) 120 121 #define HOOK_REDIR(b, p) do { \ 122 LIST_INSERT_HEAD(b, p, _next); \ 123 } while (0) 124 125 #define HOOK_SPOOL(b, p) do { \ 126 LIST_INSERT_HEAD(b, p, _next); \ 127 } while (0) 128 129 #endif 130