summaryrefslogtreecommitdiff
path: root/drivers/net/bih/map.c
blob: 4f7a9097bd63dbf7891ae9ebc61f9ac86076f4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
 * Bump in the Host (BIH) 
 * http://code.google.com/p/bump-in-the-host/source/
 * ----------------------------------------------------------
 *
 *  Copyrighted (C) 2010,2011 by the China Mobile Communications 
 *  Corporation <bih.cmcc@gmail.com>;
 *  See the COPYRIGHT file for full details. 
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
 *
 */
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/skbuff.h>
#include <linux/slab.h>
#include <linux/timer.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#endif
#include "voslist.h"
#include "map.h"
#define TIMEOUTSEC 60
#define MAP_ARRAY_NUM 13
#define NIPQUAD(addr) \
        ((unsigned char *)&addr)[0], \
        ((unsigned char *)&addr)[1], \
        ((unsigned char *)&addr)[2], \
        ((unsigned char *)&addr)[3]

#define BIH_DEBUG printk
struct map_node *map_array[MAP_ARRAY_NUM];
struct timer_list map_timer;
unsigned long map_timeout=1800;
DEFINE_SPINLOCK(map_lock);

int map_init(void);
int map_uninit(void);
int map_timeout_func(void);
struct map_node *map_find(struct in6_addr in6addr);
struct map_node *map_search(unsigned int id);
struct map_node *map_new(unsigned int id);
struct map_node *map_add(unsigned int in4addr,struct in6_addr in6addr);
int map_del(unsigned int id);
int map_free(struct map_node *node);
void map_shownode(struct map_node *node);
int map_show(unsigned int id);
char *map_showrun(void);
int map_timeout_set(unsigned int secs);

/*
        Initiate the map-node, and set a timer for the node;
        when the node is expired, call map_timeout_func().
*/
int map_init(void)
{
        init_timer(&map_timer);
        map_timer.data = 0;
        map_timer.function = (void (*)(unsigned long))map_timeout_func;
        map_timer.expires = jiffies + TIMEOUTSEC*HZ;
        add_timer(&map_timer);
        return(0);
}

/*
        Delete the timer named map_timer, and destroy the timer's mapping relationship.
*/
int map_uninit(void)
{
        del_timer(&map_timer);
        map_free(NULL);
        return(0);
}

/*
        Check the map_array[] array, and call map_free() to delete the node which is expired(30 minutes).
*/
int map_timeout_func(void)
{
        int i;
        unsigned long now;
        struct map_node *node,*delnode;

        now=jiffies;
        map_timer.expires = jiffies + TIMEOUTSEC*HZ;
        add_timer(&map_timer);
//        spin_lock_bh(&map_lock);
        for(i=0;i<MAP_ARRAY_NUM;i++)
        {
                for(node=map_array[i];node;)
                {
                        delnode=node;
                        node=node->next;
                        if(time_after(now,delnode->last_time+HZ*map_timeout))
                        {
                                map_free(delnode);
                        }
                }
        }
//        spin_unlock_bh(&map_lock);
        return(0);
}

/*
        Check the node in the whole map_array[] array with 'in6_addr',
        if there is an appropriate result, return the node,
        otherwise, return NULL.
*/
struct map_node *map_find(struct in6_addr in6addr)
{
        int i;
        struct map_node *node;

        for(i=0;i<MAP_ARRAY_NUM;i++)
        {
                for(node=map_array[i];node;node=node->next)
                {
                        if(memcmp(&in6addr,&(node->in6addr),sizeof(struct in6_addr))==0)
                        {
                                node->last_time=jiffies;
                                return(node);
                        }
                }
        }
        return(NULL);
}

/*
        Check the linked list with 'id',
        if there is an appropriate result, set the 'last_time' as jiffies, and return the node.
*/
struct map_node *map_search(unsigned int id)
{
        struct map_node *node;
        node=(struct map_node *)listnode_find((struct listnode *)map_array[id%MAP_ARRAY_NUM],id);
        if(node)
                node->last_time=jiffies;
        return(node);
}

/*
        Insert a map_node into the map with a certain id,
        if there is a node with this id already exists in the map, set the node's 'last_time' as jiffies and return the node,
        otherwise, make a new map_node, and insert this new node into the linked list.
*/
struct map_node *map_new(unsigned int id)
{
        struct map_node *node;

        if(id==0)
        {
                BIH_DEBUG("bih: map_new argument error\n");
                lprintf(0,"argument error\n");
                return(NULL);
        }
        node=map_search(id);
        if(node)
        {
                return(node);
        }
        node=(struct map_node *)vmalloc(sizeof(struct map_node));
        if(node==NULL)
        {
                BIH_DEBUG("bih: map_new vmalloc error\n");
                lprintf(0,"vmalloc error\n");
                return(NULL);
        }
        memset(node,0,sizeof(struct map_node));
        node->id=id;
        node->start_time=jiffies;
        node->last_time=node->start_time;
        spin_lock_bh(&map_lock);
        if(listnode_insert((struct listnode **)&(map_array[id%MAP_ARRAY_NUM]),(struct listnode *)node))
        {
                spin_unlock_bh(&map_lock);
                vfree(node);
                BIH_DEBUG("bih: map_new listnode_insert error\n");
                lprintf(0,"listnode_insert error\n");
                return(NULL);
        }
        spin_unlock_bh(&map_lock);
        return(node);
}

/*
        Insert or upgrade the IPv6 address,
        find the node with 'id' or 'key', and then make a new mapping relationship.
*/
struct map_node *map_add(unsigned int in4addr,struct in6_addr in6addr)
{
        struct map_node *node;
        if(in4addr==0)
        {
                BIH_DEBUG("bih: map_add argument error\n");
                lprintf(0,"argument error\n");
                return(NULL);
        }
        node=map_search(in4addr);
        if(node==NULL)
                node=map_new(in4addr);
        memcpy(&(node->in6addr), &in6addr, sizeof(struct in6_addr));
        node->last_time=jiffies;
        return(node);
}

/*
        Call the map_free() to delete the certain node with 'id',
        or, clear the whole map_array[].
*/
int map_del(unsigned int id)
{
        struct map_node *node;
        node=map_search(id);
        if(node)
        {
                map_free(node);
        }
        return(0);
}

/*
        Called by the map_del().
*/
int map_free(struct map_node *node)
{
        unsigned long now;
        char buff[64];
        now=jiffies;
        if(node==NULL)
        {
                int i;
                struct map_node *delnode;
                for(i=0;i<MAP_ARRAY_NUM;i++)
                {
                        for(node=map_array[i];node;)
                        {
                                delnode=node;
                                node=node->next;
                                map_free(delnode);
                        }
                }
        }
        else
        {
                inet_ntop6((unsigned char *)&(node->in6addr), buff, sizeof(buff)-1);
                lprintf(1,"MAP: (%u.%u.%u.%u-%s) timeout: used(%lu) unused(%lu)\n",
                        NIPQUAD(node->id),buff,(now-node->start_time)/HZ,(now-node->last_time)/HZ);
                spin_lock_bh(&map_lock);
                listnode_uninsert((struct listnode **)&(map_array[node->id%MAP_ARRAY_NUM]),(struct listnode *)node);
                vfree(node);
                spin_unlock_bh(&map_lock);
        }
        return(0);
}

/*
        Called by the map_show() and print the information of a certain map_node.
*/
void map_shownode(struct map_node *node)
{
        unsigned long now;
        char buff[64];
        if(node==NULL)
                return;
        now=jiffies;
        inet_ntop6((unsigned char *)&(node->in6addr), buff, sizeof(buff)-1);
        BIH_DEBUG("bih: %u.%u.%u.%u-%s : used(%lu) unused(%lu)\n",
                NIPQUAD(node->id),buff,(now-node->start_time)/HZ,(now-node->last_time)/HZ);
        lprintf(5,"%u.%u.%u.%u-%s : used(%lu) unused(%lu)\n",
                NIPQUAD(node->id),buff,(now-node->start_time)/HZ,(now-node->last_time)/HZ);
        return;
}

/*
        Print the information of a certain node or all the nodes.
*/
int map_show(unsigned int id)
{
        struct map_node *node;
        if(id==0)
        {
                int i;
                lprintf(5,"==================== MAP TABLE ====================\n");
                for(i=0;i<MAP_ARRAY_NUM;i++)
                {
                        for(node=map_array[i];node;node=node->next)
                        {
                                map_shownode(node);
                        }
                }
        }
        else
        {
                node=map_search(id);
                if(node==NULL)
                {
                        lprintf(0,"id(%u.%u.%u.%u) error\n",NIPQUAD(id));
                        return(-1);
                }
                map_shownode(node);
        }
        return(0);
}

/*
        Print the infomation of mapping-address,
        such as the time used and left.
*/
char *map_showrun(void)
{
        int i;
        struct map_node *node;
        unsigned long now=jiffies;
        char strbuff[64];
        SHOW2BUF_DEF;
        SHOW2BUF_INIT();
        spin_lock_bh(&map_lock);
        for(i=0;i<MAP_ARRAY_NUM;i++)
        {
                for(node=map_array[i];node;node=node->next)
                {
                        inet_ntop6((unsigned char *)&(node->in6addr), strbuff, sizeof(strbuff)-1);
                        SHOW2BUF("[%d]: %u.%u.%u.%u-%s : used(%lu) unused(%lu)\n",
                                i,NIPQUAD(node->id),strbuff,(now-node->start_time)/HZ,(now-node->last_time)/HZ);
                }
        }
        spin_unlock_bh(&map_lock);
        SHOW2BUF_RET;
}

int map_timeout_set(unsigned int secs)
{
        map_timeout=secs;
        return(0);
}