Deleted Added
sdiff udiff text old ( 200838 ) new ( 201120 )
full compact
1/*-
2 * Copyright (c) 2004 Ruslan Ermilov and Vsevolod Lobko.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.

--- 10 unchanged lines hidden (view full) ---

19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23 * SUCH DAMAGE.
24 */
25
26#include <sys/cdefs.h>
27__FBSDID("$FreeBSD: head/sys/netinet/ipfw/ip_fw_table.c 200838 2009-12-22 13:53:34Z luigi $");
28
29/*
30 * Lookup table support for ipfw
31 *
32 * Lookup tables are implemented (at the moment) using the radix
33 * tree used for routing tables. Tables store key-value entries, where
34 * keys are network prefixes (addr/masklen), and values are integers.
35 * As a degenerate case we can interpret keys as 32-bit integers

--- 38 unchanged lines hidden (view full) ---

74MALLOC_DEFINE(M_IPFW_TBL, "ipfw_tbl", "IpFw tables");
75
76struct table_entry {
77 struct radix_node rn[2];
78 struct sockaddr_in addr, mask;
79 u_int32_t value;
80};
81
82int
83ipfw_add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
84 uint8_t mlen, uint32_t value)
85{
86 struct radix_node_head *rnh;
87 struct table_entry *ent;
88 struct radix_node *rn;
89
90 if (tbl >= IPFW_TABLES_MAX)
91 return (EINVAL);
92 rnh = ch->tables[tbl];
93 ent = malloc(sizeof(*ent), M_IPFW_TBL, M_NOWAIT | M_ZERO);
94 if (ent == NULL)
95 return (ENOMEM);
96 ent->value = value;
97 ent->addr.sin_len = ent->mask.sin_len = 8;
98 ent->mask.sin_addr.s_addr = htonl(mlen ? ~((1 << (32 - mlen)) - 1) : 0);
99 ent->addr.sin_addr.s_addr = addr & ent->mask.sin_addr.s_addr;
100 IPFW_WLOCK(ch);
101 rn = rnh->rnh_addaddr(&ent->addr, &ent->mask, rnh, (void *)ent);
102 if (rn == NULL) {
103 IPFW_WUNLOCK(ch);
104 free(ent, M_IPFW_TBL);
105 return (EEXIST);

--- 8 unchanged lines hidden (view full) ---

114{
115 struct radix_node_head *rnh;
116 struct table_entry *ent;
117 struct sockaddr_in sa, mask;
118
119 if (tbl >= IPFW_TABLES_MAX)
120 return (EINVAL);
121 rnh = ch->tables[tbl];
122 sa.sin_len = mask.sin_len = 8;
123 mask.sin_addr.s_addr = htonl(mlen ? ~((1 << (32 - mlen)) - 1) : 0);
124 sa.sin_addr.s_addr = addr & mask.sin_addr.s_addr;
125 IPFW_WLOCK(ch);
126 ent = (struct table_entry *)rnh->rnh_deladdr(&sa, &mask, rnh);
127 if (ent == NULL) {
128 IPFW_WUNLOCK(ch);
129 return (ESRCH);
130 }

--- 43 unchanged lines hidden (view full) ---

174
175int
176ipfw_init_tables(struct ip_fw_chain *ch)
177{
178 int i;
179 uint16_t j;
180
181 for (i = 0; i < IPFW_TABLES_MAX; i++) {
182 if (!rn_inithead((void **)&ch->tables[i], 32)) {
183 for (j = 0; j < i; j++) {
184 (void) ipfw_flush_table(ch, j);
185 }
186 return (ENOMEM);
187 }
188 }
189 return (0);
190}

--- 4 unchanged lines hidden (view full) ---

195{
196 struct radix_node_head *rnh;
197 struct table_entry *ent;
198 struct sockaddr_in sa;
199
200 if (tbl >= IPFW_TABLES_MAX)
201 return (0);
202 rnh = ch->tables[tbl];
203 sa.sin_len = 8;
204 sa.sin_addr.s_addr = addr;
205 ent = (struct table_entry *)(rnh->rnh_lookup(&sa, NULL, rnh));
206 if (ent != NULL) {
207 *val = ent->value;
208 return (1);
209 }
210 return (0);
211}

--- 57 unchanged lines hidden ---