4 #include <netinet/ip_fw.h>
7 #define IPFW_NEWTABLES_MAX 256
10 /* Object stored in the hash table */
16 MALLOC_DEFINE(M_IPFW_HTBL, "ipfw_tbl", "IpFw tables");
18 int add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
19 uint8_t mlen, uint32_t value);
20 int new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr);
21 int del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
23 int new_flush_table(struct ip_fw_chain *ch, uint16_t tbl);
24 int flush_table(struct ip_fw_chain *ch, uint16_t tbl);
25 int lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
27 int new_count_table_entry(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
28 int count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
29 int new_dump_table_entry(struct ip_fw_chain *ch, ipfw_table *tbl);
30 int dump_table(struct ip_fw_chain *ch, ipfw_table *tbl);
31 int init_tables(struct ip_fw_chain *ch);
33 /* hash and compare functions for 32-bit entries */
35 simple_hash32(const void *key, uint32_t size)
37 uint32_t ret = *(const uint32_t *)key % size;
38 printf("%s called\n", __FUNCTION__);
39 printf("Hash returns %d\n", ret);
45 cmp_func32(const void *key1, const void *key2)
47 int k1 = *(const int *)key1;
48 int k2 = *(const int *)key2;
50 printf("(%s) k1=%d, k2=%d\n", __FUNCTION__, k1, k2);
58 printf("compare returns %d\n", ret);
64 add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
65 uint8_t mlen, uint32_t value)
68 * - Search the correct hash table (tbl - IPFW_TABLES_MAX)
69 * - Search if the entry already exists
70 * - Insert the new entry in the table
71 * - Possibly reallocate the table if it is too small
76 int i = tbl - IPFW_TABLES_MAX;
78 int obj_size = sizeof(struct t_o);
80 printf("%s called\n", __FUNCTION__);
81 if (i < 0 || i > size-1) /* wrong table number */
83 if (ch->global_tables[i] == NULL) {
84 printf("Creating table n %d\n", tbl);
85 ch->global_tables[i] = new_table_init(size, obj_size,
86 simple_hash32, cmp_func32, M_IPFW_HTBL);
93 /* Insert the object in the table */
94 ret = new_table_insert_obj(ch->global_tables[i], &obj);
99 new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr)
102 int nr = tbl - IPFW_TABLES_MAX;
104 printf("%s called\n", __FUNCTION__);
106 ret = new_table_delete_obj(ch->global_tables[nr], &addr);
112 del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
115 printf("%s called\n", __FUNCTION__);
116 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
117 new_del_table_entry(ch, tbl, addr);
124 new_flush_table(struct ip_fw_chain *ch, uint16_t tbl)
126 printf("%s called\n", __FUNCTION__);
127 new_table_destroy(ch->global_tables[tbl - IPFW_TABLES_MAX]);
132 flush_table(struct ip_fw_chain *ch, uint16_t tbl)
134 printf("%s called\n", __FUNCTION__);
135 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX)
136 return new_flush_table(ch, tbl);
142 lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
145 printf("%s called\n", __FUNCTION__);
146 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
147 struct new_hash_table *h;
148 const struct t_o *obj;
150 h = ch->global_tables[tbl - IPFW_TABLES_MAX];
151 printf("Search %d in table number %d\n", addr, tbl);
153 obj = new_table_extract_obj(h, (void *)&addr);
155 return 0; /* no match */
158 printf("obj->addr=%d,obj->value=%d\n",obj->addr, obj->value);
159 return 1; /* match */
165 new_count_table_entry(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
167 printf("%s called\n", __FUNCTION__);
168 *cnt = new_table_get_element(ch->global_tables[tbl - IPFW_TABLES_MAX]);
173 count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
175 printf("%s called\n", __FUNCTION__);
176 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
177 new_count_table_entry(ch, tbl, cnt);
184 new_dump_table_entry(struct ip_fw_chain *ch, ipfw_table *tbl)
186 /* fill the tbl with all entryes */
187 ipfw_table_entry *ent;
188 const struct t_o *obj;
191 int nr = tbl->tbl - IPFW_TABLES_MAX;
192 struct new_hash_table *t = ch->global_tables[nr];
194 printf("%s called\n", __FUNCTION__);
199 /* XXX determine tbl->size */
200 n_el = new_table_get_element(t);
202 for (; n_el > 0; n_el--) {
203 obj = table_next(t, obj);
207 ent = &tbl->ent[tbl->cnt];
209 ent->addr = obj->addr;
210 ent->value = obj->value;
211 ent->masklen = obj->mask;
219 dump_table(struct ip_fw_chain *ch, ipfw_table *tbl)
221 printf("%s called\n", __FUNCTION__);
222 if (tbl->tbl >= IPFW_TABLES_MAX && tbl->tbl < IPFW_NEWTABLES_MAX) {
223 new_dump_table_entry(ch, tbl);
230 init_tables(struct ip_fw_chain *ch)
234 printf("%s called\n", __FUNCTION__);
235 /* Initialize new tables XXXMPD */
236 for (i = 0; i < IPFW_NEWTABLES_MAX - IPFW_TABLES_MAX; i++) {
237 memset(&ch->global_tables[i], sizeof(struct new_hash_table*), 0);