Update the work on ipfw tables, reduce diffs.
[ipfw.git] / dummynet / new_glue.c
1 #include "missing.h"
2
3 #define IPFW_INTERNAL
4 #include <netinet/ip_fw.h>
5
6 #include "hashtable.h"
7 #define IPFW_NEWTABLES_MAX      256
8
9 struct t_o {
10         /* Object stored in the hash table */
11         uint32_t addr;
12         uint32_t value;
13         uint8_t mask;
14 };
15
16 MALLOC_DEFINE(M_IPFW_HTBL, "ipfw_tbl", "IpFw tables");
17
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,
22                     uint8_t mlen);
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, 
26                 uint32_t *val);
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);
32
33 /* hash and compare functions for 32-bit entries */
34 static uint32_t
35 simple_hash32(const void *key, uint32_t size)
36 {
37         uint32_t ret = *(const uint32_t *)key % size;
38         printf("%s called\n", __FUNCTION__);
39         printf("Hash returns %d\n", ret);
40
41         return ret;
42 }
43
44 static int
45 cmp_func32(const void *key1, const void *key2)
46 {
47         int k1 = *(const int *)key1;
48         int k2 = *(const int *)key2;
49         int ret;
50         printf("(%s) k1=%d, k2=%d\n", __FUNCTION__, k1, k2);
51         if (k1 < k2)
52                 ret = -1;
53         else if (k1 > k2)
54                 ret = 1;
55         else
56                 ret = 0;
57
58         printf("compare returns %d\n", ret);
59
60         return ret;
61 }
62
63 int
64 add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
65     uint8_t mlen, uint32_t value)
66 {
67         /* TODO:
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
72          */
73
74         struct t_o obj;
75         int ret;
76         int i = tbl - IPFW_TABLES_MAX;
77         int size = 128;
78         int obj_size = sizeof(struct t_o);
79
80         printf("%s called\n", __FUNCTION__);
81         if (i < 0 || i > size-1) /* wrong table number */
82                 return 1;
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);
87         }
88
89         obj.addr = addr;
90         obj.value = value;
91         obj.mask = mlen;
92
93         /* Insert the object in the table */
94         ret = new_table_insert_obj(ch->global_tables[i], &obj);
95         return ret;
96 }
97
98 int
99 new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr)
100 {
101         int ret;
102         int nr = tbl - IPFW_TABLES_MAX;
103
104         printf("%s called\n", __FUNCTION__);
105
106         ret = new_table_delete_obj(ch->global_tables[nr], &addr);
107
108         return ret;
109 }
110
111 int
112 del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
113     uint8_t mlen)
114 {
115         printf("%s called\n", __FUNCTION__);
116         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
117                 new_del_table_entry(ch, tbl, addr);
118                 return 0;
119         }
120         return (EINVAL);
121 }
122
123 int
124 new_flush_table(struct ip_fw_chain *ch, uint16_t tbl)
125 {
126         printf("%s called\n", __FUNCTION__);
127         new_table_destroy(ch->global_tables[tbl - IPFW_TABLES_MAX]);
128         return 0;
129 }
130
131 int
132 flush_table(struct ip_fw_chain *ch, uint16_t tbl)
133 {
134         printf("%s called\n", __FUNCTION__);
135         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX)
136                 return new_flush_table(ch, tbl);
137         
138         return (EINVAL);
139 }
140
141 int
142 lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
143     uint32_t *val)
144 {
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;
149
150                 h = ch->global_tables[tbl - IPFW_TABLES_MAX];
151                 printf("Search %d in table number %d\n", addr, tbl);
152
153                 obj = new_table_extract_obj(h, (void *)&addr);
154                 if (obj == NULL)
155                         return 0; /* no match */
156
157                 *val = obj->value;
158                 printf("obj->addr=%d,obj->value=%d\n",obj->addr, obj->value);
159                 return 1; /* match */
160         }
161         return 0;
162 }
163
164 int
165 new_count_table_entry(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
166 {
167         printf("%s called\n", __FUNCTION__);
168         *cnt = new_table_get_element(ch->global_tables[tbl - IPFW_TABLES_MAX]);
169         return 0;
170 }
171
172 int
173 count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
174 {
175         printf("%s called\n", __FUNCTION__);
176         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
177                 new_count_table_entry(ch, tbl, cnt);
178                 return (0);
179         }
180         return (EINVAL);
181 }
182
183 int
184 new_dump_table_entry(struct ip_fw_chain *ch, ipfw_table *tbl)
185 {
186         /* fill the tbl with all entryes */
187         ipfw_table_entry *ent;
188         const struct t_o *obj;
189         int i;
190         int n_el;
191         int nr = tbl->tbl - IPFW_TABLES_MAX;
192         struct new_hash_table *t = ch->global_tables[nr];
193
194         printf("%s called\n", __FUNCTION__);
195
196         i = 0;
197         tbl->cnt = 0;
198
199         /* XXX determine tbl->size */
200         n_el = new_table_get_element(t);
201         obj = NULL;
202         for (; n_el > 0; n_el--) {
203                 obj = table_next(t, obj);
204                 if (obj == NULL)
205                         break;
206                 printf("Found \n");
207                 ent = &tbl->ent[tbl->cnt];
208
209                 ent->addr = obj->addr;
210                 ent->value = obj->value;
211                 ent->masklen = obj->mask;
212                 tbl->cnt++;
213         }
214         printf("\n");
215         return 0;
216 }
217
218 int
219 dump_table(struct ip_fw_chain *ch, ipfw_table *tbl)
220 {
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);
224                 return (0);
225         }
226         return (EINVAL);
227 }
228
229 int
230 init_tables(struct ip_fw_chain *ch)
231 {
232
233         int i;
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);
238         }
239
240         return (0);
241 }