2 * Copyright (c) 2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Bill Yuan <bycn82@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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
38 #define MODULE_NAT_ID 4
39 #define MODULE_NAT_NAME "nat"
43 MALLOC_DEFINE(M_IPFW_NAT, "IPFW3/NAT", "IPFW3/NAT 's");
45 /* place to hold the nat conf */
46 struct ipfw_nat_context {
47 LIST_HEAD(, cfg_nat) nat; /* list of nat entries*/
50 struct netmsg_nat_del {
51 struct netmsg_base base;
55 struct netmsg_nat_add {
56 struct netmsg_base base;
60 struct netmsg_alias_link_add {
61 struct netmsg_base base;
62 struct alias_link *lnk;
70 enum ipfw_nat_opcodes {
74 struct ipfw_ioc_nat_state {
75 struct in_addr src_addr;
76 struct in_addr dst_addr;
77 struct in_addr alias_addr;
89 /* Redirect modes id. */
90 #define REDIR_ADDR 0x01
91 #define REDIR_PORT 0x02
92 #define REDIR_PROTO 0x04
94 /* Server pool support (LSNAT). */
96 LIST_ENTRY(cfg_spool) _next; /* chain of spool instances */
102 LIST_ENTRY(cfg_redir) _next; /* chain of redir instances */
103 u_int16_t mode; /* type of redirect mode */
104 struct in_addr laddr; /* local ip address */
105 struct in_addr paddr; /* public ip address */
106 struct in_addr raddr; /* remote ip address */
107 u_short lport; /* local port */
108 u_short pport; /* public port */
109 u_short rport; /* remote port */
110 u_short pport_cnt; /* number of public ports */
111 u_short rport_cnt; /* number of remote ports */
112 int proto; /* protocol: tcp/udp */
113 struct alias_link **alink;
114 /* num of entry in spool chain */
116 /* chain of spool instances */
117 LIST_HEAD(spool_chain, cfg_spool) spool_chain;
120 /* Nat configuration data struct. */
122 /* chain of nat instances */
123 LIST_ENTRY(cfg_nat) _next;
125 struct in_addr ip; /* nat ip address */
126 char if_name[IF_NAMESIZE]; /* interface name */
127 int mode; /* aliasing mode */
128 struct libalias *lib; /* libalias instance */
129 /* number of entry in spool chain */
131 /* chain of redir instances */
132 LIST_HEAD(redir_chain, cfg_redir) redir_chain;
135 #define SOF_NAT sizeof(struct cfg_nat)
136 #define SOF_REDIR sizeof(struct cfg_redir)
137 #define SOF_SPOOL sizeof(struct cfg_spool)
140 typedef struct _ipfw_insn_nat {
145 #define LOOKUP_NAT(l, i, p) do { \
146 LIST_FOREACH((p), &(l.nat), _next){ \
147 if((p)->id == (i)){ \
153 #define HOOK_NAT(b, p) do { \
154 LIST_INSERT_HEAD(b, p, _next); \
157 #define UNHOOK_NAT(p) do { \
158 LIST_REMOVE(p, _next); \
161 #define HOOK_REDIR(b, p) do { \
162 LIST_INSERT_HEAD(b, p, _next); \
165 #define HOOK_SPOOL(b, p) do { \
166 LIST_INSERT_HEAD(b, p, _next); \