2 //#include <sys/param.h>
3 //#include <sys/systm.h>
4 //#include <sys/malloc.h>
5 // #include <sys/mbuf.h>
6 //#include <sys/kernel.h>
7 //#include <sys/lock.h>
8 //#include <sys/jail.h>
9 //#include <sys/module.h>
10 //#include <sys/priv.h>
11 //#include <sys/proc.h>
12 //#include <sys/socket.h>
13 //#include <sys/socketvar.h>
14 //#include <sys/sysctl.h>
15 //#include <sys/syslog.h>
16 //#include <sys/ucred.h>
17 //#include <net/ethernet.h> /* for ETHERTYPE_IP */
19 //#include <net/radix.h>
20 //#include <net/route.h>
21 //#include <net/pf_mtag.h>
24 #include <netinet/ip_fw.h>
26 #include "hashtable.h"
27 #define IPFW_NEWTABLES_MAX 256
30 /* Object stored in the hash table */
36 MALLOC_DEFINE(M_IPFW_HTBL, "ipfw_tbl", "IpFw tables");
38 static struct new_hash_table *global_tables[128];
39 int add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
40 uint8_t mlen, uint32_t value);
41 int new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr);
42 int del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
44 int new_flush_table(uint16_t tbl);
45 int flush_table(struct ip_fw_chain *ch, uint16_t tbl);
46 int lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
48 int new_count_table_entry(uint32_t tbl, uint32_t *cnt);
49 int count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt);
50 int new_dump_table_entry(ipfw_table *tbl);
51 int dump_table(struct ip_fw_chain *ch, ipfw_table *tbl);
52 int init_tables(struct ip_fw_chain *ch);
54 /* hash and compare functions for 32-bit entries */
56 simple_hash32(const void *key, uint32_t size)
58 uint32_t ret = *(const uint32_t *)key % size;
59 printf("%s called\n", __FUNCTION__);
60 printf("Hash returns %d\n", ret);
66 cmp_func32(const void *key1, const void *key2)
68 int k1 = *(const int *)key1;
69 int k2 = *(const int *)key2;
71 printf("(%s) k1=%d, k2=%d\n", __FUNCTION__, k1, k2);
79 printf("compare returns %d\n", ret);
85 add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
86 uint8_t mlen, uint32_t value)
89 * - Search the correct hash table (tbl - IPFW_TABLES_MAX)
90 * - Search if the entry already exists
91 * - Insert the new entry in the table
92 * - Possibly reallocate the table if it is too small
97 int i = tbl - IPFW_TABLES_MAX;
99 int obj_size = sizeof(struct t_o);
101 printf("%s called\n", __FUNCTION__);
102 if (i < 0 || i > size-1) /* wrong table number */
104 if (global_tables[i] == NULL) {
105 printf("Creating table n %d\n", tbl);
106 global_tables[i] = new_table_init(size, obj_size,
107 simple_hash32, cmp_func32, M_IPFW_HTBL);
114 /* Insert the object in the table */
115 ret = new_table_insert_obj(global_tables[i], &obj);
120 new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr)
123 int nr = tbl - IPFW_TABLES_MAX;
125 printf("%s called\n", __FUNCTION__);
127 ret = new_table_delete_obj(global_tables[nr], &addr);
133 del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
136 printf("%s called\n", __FUNCTION__);
137 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
138 new_del_table_entry(ch, tbl, addr);
145 new_flush_table(uint16_t tbl)
147 printf("%s called\n", __FUNCTION__);
148 new_table_destroy(global_tables[tbl - IPFW_TABLES_MAX]);
153 flush_table(struct ip_fw_chain *ch, uint16_t tbl)
155 printf("%s called\n", __FUNCTION__);
156 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX)
157 return new_flush_table(tbl);
163 lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
166 printf("%s called\n", __FUNCTION__);
167 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
168 struct new_hash_table *h;
171 h = global_tables[tbl - IPFW_NEWTABLES_MAX];
172 printf("Search %d in table number %d\n", addr, tbl);
174 obj = (struct t_o *)new_table_extract_obj(h, (void *)&addr);
187 new_count_table_entry(uint32_t tbl, uint32_t *cnt)
189 printf("%s called\n", __FUNCTION__);
190 *cnt = new_table_get_element(global_tables[tbl - IPFW_TABLES_MAX]);
195 count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
197 printf("%s called\n", __FUNCTION__);
198 if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
199 new_count_table_entry(tbl, cnt);
206 new_dump_table_entry(ipfw_table *tbl)
208 /* fill the tbl with all entryes */
209 ipfw_table_entry *ent;
210 const struct t_o *obj;
213 int nr = tbl->tbl - IPFW_TABLES_MAX;
214 struct new_hash_table *t = global_tables[nr];
216 printf("%s called\n", __FUNCTION__);
221 /* XXX determine tbl->size */
222 n_el = new_table_get_element(t);
224 for (; n_el > 0; n_el--) {
225 obj = table_next(t, obj);
229 ent = &tbl->ent[tbl->cnt];
231 ent->addr = obj->addr;
232 ent->value = obj->value;
233 ent->masklen = obj->mask;
241 dump_table(struct ip_fw_chain *ch, ipfw_table *tbl)
243 printf("%s called\n", __FUNCTION__);
244 if (tbl->tbl >= IPFW_TABLES_MAX && tbl->tbl < IPFW_NEWTABLES_MAX) {
245 new_dump_table_entry(tbl);
252 init_tables(struct ip_fw_chain *ch)
256 printf("%s called\n", __FUNCTION__);
257 /* Initialize new tables XXXMPD */
258 for (i = 0; i < IPFW_NEWTABLES_MAX - IPFW_TABLES_MAX; i++) {
259 memset(&global_tables[i], sizeof(struct new_hash_table*), 0);