52a1a7fb271258dc9f95e5c22812bcb17927a3df
[ipfw.git] / dummynet / new_glue.c
1 #include "missing.h"
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 */
18 //#include <net/if.h>
19 //#include <net/radix.h>
20 //#include <net/route.h>
21 //#include <net/pf_mtag.h>
22
23 #define IPFW_INTERNAL
24 #include <netinet/ip_fw.h>
25
26 #include "hashtable.h"
27 #define IPFW_NEWTABLES_MAX      256
28
29 struct t_o {
30         /* Object stored in the hash table */
31         uint32_t addr;
32         uint32_t value;
33         uint8_t mask;
34 };
35
36 MALLOC_DEFINE(M_IPFW_HTBL, "ipfw_tbl", "IpFw tables");
37
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,
43                     uint8_t mlen);
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, 
47                 uint32_t *val);
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);
53
54 /* hash and compare functions for 32-bit entries */
55 static uint32_t
56 simple_hash32(const void *key, uint32_t size)
57 {
58         uint32_t ret = *(const uint32_t *)key % size;
59         printf("%s called\n", __FUNCTION__);
60         printf("Hash returns %d\n", ret);
61
62         return ret;
63 }
64
65 static int
66 cmp_func32(const void *key1, const void *key2)
67 {
68         int k1 = *(const int *)key1;
69         int k2 = *(const int *)key2;
70         int ret;
71         printf("(%s) k1=%d, k2=%d\n", __FUNCTION__, k1, k2);
72         if (k1 < k2)
73                 ret = -1;
74         else if (k1 > k2)
75                 ret = 1;
76         else
77                 ret = 0;
78
79         printf("compare returns %d\n", ret);
80
81         return ret;
82 }
83
84 int
85 add_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
86     uint8_t mlen, uint32_t value)
87 {
88         /* TODO:
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
93          */
94
95         struct t_o obj;
96         int ret;
97         int i = tbl - IPFW_TABLES_MAX;
98         int size = 128;
99         int obj_size = sizeof(struct t_o);
100
101         printf("%s called\n", __FUNCTION__);
102         if (i < 0 || i > size-1) /* wrong table number */
103                 return 1;
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);
108         }
109
110         obj.addr = addr;
111         obj.value = value;
112         obj.mask = mlen;
113
114         /* Insert the object in the table */
115         ret = new_table_insert_obj(global_tables[i], &obj);
116         return ret;
117 }
118
119 int
120 new_del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr)
121 {
122         int ret;
123         int nr = tbl - IPFW_TABLES_MAX;
124
125         printf("%s called\n", __FUNCTION__);
126
127         ret = new_table_delete_obj(global_tables[nr], &addr);
128
129         return ret;
130 }
131
132 int
133 del_table_entry(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
134     uint8_t mlen)
135 {
136         printf("%s called\n", __FUNCTION__);
137         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
138                 new_del_table_entry(ch, tbl, addr);
139                 return 0;
140         }
141         return (EINVAL);
142 }
143
144 int
145 new_flush_table(uint16_t tbl)
146 {
147         printf("%s called\n", __FUNCTION__);
148         new_table_destroy(global_tables[tbl - IPFW_TABLES_MAX]);
149         return 0;
150 }
151
152 int
153 flush_table(struct ip_fw_chain *ch, uint16_t tbl)
154 {
155         printf("%s called\n", __FUNCTION__);
156         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX)
157                 return new_flush_table(tbl);
158         
159         return (EINVAL);
160 }
161
162 int
163 lookup_table(struct ip_fw_chain *ch, uint16_t tbl, in_addr_t addr,
164     uint32_t *val)
165 {
166         printf("%s called\n", __FUNCTION__);
167         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
168                 struct new_hash_table *h;
169                 struct t_o *obj;
170
171                 h = global_tables[tbl - IPFW_NEWTABLES_MAX];
172                 printf("Search %d in table number %d\n", addr, tbl);
173
174                 obj = (struct t_o *)new_table_extract_obj(h, (void *)&addr);
175                 if (obj == NULL)
176                         return 0;
177
178                 *val = obj->value;
179
180                 return 1;
181         }
182
183         return 1;
184 }
185
186 int
187 new_count_table_entry(uint32_t tbl, uint32_t *cnt)
188 {
189         printf("%s called\n", __FUNCTION__);
190         *cnt = new_table_get_element(global_tables[tbl - IPFW_TABLES_MAX]);
191         return 0;
192 }
193
194 int
195 count_table(struct ip_fw_chain *ch, uint32_t tbl, uint32_t *cnt)
196 {
197         printf("%s called\n", __FUNCTION__);
198         if (tbl >= IPFW_TABLES_MAX && tbl < IPFW_NEWTABLES_MAX) {
199                 new_count_table_entry(tbl, cnt);
200                 return (0);
201         }
202         return (EINVAL);
203 }
204
205 int
206 new_dump_table_entry(ipfw_table *tbl)
207 {
208         /* fill the tbl with all entryes */
209         ipfw_table_entry *ent;
210         const struct t_o *obj;
211         int i;
212         int n_el;
213         int nr = tbl->tbl - IPFW_TABLES_MAX;
214         struct new_hash_table *t = global_tables[nr];
215
216         printf("%s called\n", __FUNCTION__);
217
218         i = 0;
219         tbl->cnt = 0;
220
221         /* XXX determine tbl->size */
222         n_el = new_table_get_element(t);
223         obj = NULL;
224         for (; n_el > 0; n_el--) {
225                 obj = table_next(t, obj);
226                 if (obj == NULL)
227                         break;
228                 printf("Found \n");
229                 ent = &tbl->ent[tbl->cnt];
230
231                 ent->addr = obj->addr;
232                 ent->value = obj->value;
233                 ent->masklen = obj->mask;
234                 tbl->cnt++;
235         }
236         printf("\n");
237         return 0;
238 }
239
240 int
241 dump_table(struct ip_fw_chain *ch, ipfw_table *tbl)
242 {
243         printf("%s called\n", __FUNCTION__);
244         if (tbl->tbl >= IPFW_TABLES_MAX && tbl->tbl < IPFW_NEWTABLES_MAX) {
245                 new_dump_table_entry(tbl);
246                 return (0);
247         }
248         return (EINVAL);
249 }
250
251 int
252 init_tables(struct ip_fw_chain *ch)
253 {
254
255         int i;
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);
260         }
261
262         return (0);
263 }