[go: up one dir, main page]

Menu

[41c17c]: / hash.c  Maximize  Restore  History

Download this file

377 lines (315 with data), 8.3 kB

  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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/* Mis-connector - user-space router and NATP for misconfigured hosts on LAN.
Hash functions.
This file is part of mis-connector.
Copyright (C) 2004,2021 Petr Silhavy <petr.silhavy@yandex.com>
SPDX-License-Identifier: GPL-3.0-or-later
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 3 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 mis-connector. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <net/ethernet.h>
#include "whackparam.h"
/*
* SHM @ 0xA0000000
*
* 0xA0000000 - 0xA0040000 MAC => IP hash - size 0x40000
* 0xA0040000 - 0xA0050000 IP => MAC hash - size 0x10000
* 0xA0050000 - 0xA0051000 bitmap - size 0x1000
* 0xA0051000 - 0xA0151000 entries
*
* 4096 bitmap => 1MB mem
* hash 64k + 64k 131072
* 32 * 16k 524288
*
* = 655360
*/
#define BASE 0xA0000000
#define SHM_SIZE (0x50000 + 0x1000 + 0x100000)
struct sb *
sb_init()
{
key_t key = ftok("/dev/whack", 'W');
int shmid = shmget(key, SHM_SIZE, IPC_CREAT | 0600 );
void *addr ;
struct sb *sb = Malloc( sizeof *sb);
if ( shmid == EOF )
BUG("Can't create shared memmory");
addr = shmat(shmid, (char *)BASE, 0);
if ( addr == (void *)EOF)
BUG("Can't attach shared memmory");
memset( addr, 0, SHM_SIZE);
sb->mac_to_ip = addr ;
sb->ip_to_mac = addr + 0x40000 ;
sb->m = addr + 0x50000 ;
sb->pool = addr + 0x51000 ;
memset(sb->mac_to_ip, 0, SHM_SIZE);
return sb ;
}
/* List funtions were formerly part of glib */
/* GLIB - Library of useful routines for C programming
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*/
#if 0
static struct hash_entry *
g_slist_last (struct hash_entry *list)
{
if (list)
{
while (list->next)
list = list->next;
}
return list;
}
static struct hash_entry *
g_slist_find_mac (struct hash_entry *list, unsigned char *mac)
{
if (list)
{
do
{
if ( ! memcmp(mac, list->mac, ETH_ALEN) )
return list ;
list = list->next ;
}
while (list) ;
}
return list;
}
static void
g_slist_append ( struct hash_entry *list, struct hash_entry *new_list)
{
struct hash_entry *last;
last = g_slist_last (list);
/* g_assert (last != NULL); */
last->next = new_list;
}
static struct hash_entry *
g_slist_remove_link (struct hash_entry *list,
struct hash_entry *link)
{
struct hash_entry *tmp;
struct hash_entry *prev;
prev = NULL;
tmp = list;
while (tmp)
{
if (tmp == link)
{
if (prev)
prev->next = tmp->next;
if (list == tmp)
list = list->next;
tmp->next = NULL;
break;
}
prev = tmp;
tmp = tmp->next;
}
return list;
}
#endif // 0 ///////////////////////////////////////////////
static char ip_pool[IPMAX] = { [0 ... IPMAX-1 ] = ' ' } ;
static int
ip_alloc()
{
char *p = strchr(ip_pool, ' ');
*p = 'X' ;
return p - ip_pool ;
}
static inline unsigned int
ipkey_get(unsigned int netip)
{
#if 0
unsigned char *p = (void *) &wp->fool_mask ;
DEBUGP(stderr,"[%s] %s Mask: %u.%u.%u.%u\n", ids[id], __func__,
p[0],
p[1],
p[2],
p[3]);
#endif /* 0 */
return ntohl(netip & ~wp->fool_mask) ;
}
static inline unsigned int
mackey_get(unsigned char *mac)
{
return mac[4] * 256 + mac[5] ;
}
/* new net order IP */
static unsigned int
ip_make(struct whack_param *wp )
{
char ipbuf[16] ;
unsigned int n = ip_alloc(), ip , hip ;
ip = htonl( wp->fool_min ) + n + 1; /* net order */
hip = ntohl(ip);
inet_ntop(AF_INET, &hip, ipbuf, sizeof ipbuf);
DEBUGP(stderr,"%s New IP: %s\nFor MAC: ", __func__, ipbuf);
return ip ;
}
void
new_hash_entry(struct ether_header *eh , unsigned char *sip /* src IP */)
{
unsigned char *sha = eh->ether_shost ;
unsigned int key = mackey_get(sha) ;
struct hash_entry *he ;
DEBUGP(stderr, "%s %s ->\n", ids[id], __func__ );
if ( sb->mac_to_ip[key] == NULL )
{
he = bm_alloc0(sb, sizeof *he, __func__ );
if ( he == NULL )
{
BUG("No new entry, bm_alloc returns NULL\n");
/* zap old entries, and try againg FIXME !!! or simple *CRASH* */
return ;
}
DEBUGP(stderr, "%s %s allocated -> new_entry\n", ids[id], __func__ );
goto new_entry ;
}
else
{
he = sb->mac_to_ip[key] ;
if ( he->next == NULL && ! memcmp(he->mac, sha, ETH_ALEN) ) /* entry exists */
{
DEBUGP(stderr,"Entry for MAC exists : ");
print_mac(sha);
DEBUGP(stderr,"\n");
return ;
}
/* handle collision */
if ( g_slist_find_mac(he, sha)) /* entry exists */
{
DEBUGP(stderr, "Entry for MAC exists in list : ");
print_mac(sha);
DEBUGP(stderr, "\n");
return ;
}
else
{
struct hash_entry *new = bm_alloc0(sb, sizeof *new, __func__ );
if ( new == NULL )
{
BUG("No new entry, bm_alloc returns NULL\n");
/* zap old entries, and try againg FIXME !!! or simple *CRASH* */
return ;
}
g_slist_append (he, new);
he = new ;
DEBUGP(stderr, "%s %s MAC doesn't exist -> new -> new_entry\n", ids[id], __func__ );
goto new_entry ;
}
}
return ;
new_entry:
memcpy(he->mac, sha, ETH_ALEN);
memcpy(&he->fool_ip, sip, sizeof he->fool_ip);
he->ip = htonl(ip_make(wp));
{
/* unsigned int ipkey = ntohl(ipkey_get(he->ip)); */
unsigned int ipkey = ipkey_get(he->ip);
DEBUGP(stderr, "%s %s ", ids[id], __func__);
print_mac(sha);
DEBUGP(stderr," Fool IP: %u.%u.%u.%u IPkey %d\n",
sip[0],
sip[1],
sip[2],
sip[3],
ipkey);
if ( ipkey > wp->fool_size )
BUG("IP key %u > hash size %d\n", ipkey, wp->fool_size );
else
DEBUGP(stderr,"%s IP key %u\n", __func__, ipkey );
sb->mac_to_ip[key] = he ;
sb->ip_to_mac[ ipkey ] = he ;
}
}
/* return net order IP */
unsigned int
mac_to_ip(struct ether_header *eh )
{ /* */
unsigned int key = mackey_get(eh->ether_shost) ;
struct hash_entry *he ;
again:
if ( sb->mac_to_ip[key] == NULL )
{
unsigned char *sip ;
u_int16_t et = ntohs(eh->ether_type);
struct iphdr *iph = (void *)eh + sizeof *eh ;
print_mac(eh->ether_shost);
WARNING("No entry for MAC\n");
switch ( et )
{
case 0x0 ... 0x800:
iph = (void *)eh + sizeof *eh ;
sip = (void *) &iph->saddr ;
new_hash_entry(eh, sip);
break ;
default:
BUG("Unknown ethertype %u\n", et);
break ;
}
goto again ;
}
else
{
/* Hash entry for MAC found */
union conv c ;
unsigned int *ma = (void *) eh->ether_shost , *mb ; /* 1st four bytes of MAC */
he = sb->mac_to_ip[key] ;
for ( mb = (void *)he->mac ;
he ;
he = he->next , mb = (void *)he->mac )
if ( *ma == *mb )
{
DEBUGP(stderr, "%s %x == %x\n", ids[id], *ma, *mb );
goto found ;
}
else
DEBUGP(stderr, "%s %x != %x => next\n", ids[id], *ma, *mb );
BUG("End of list reached\n");
found:
c.u = he->ip ;
print_mac(eh->ether_shost);
DEBUGP(stderr,"%s Found IP %u.%u.%u.%u\n", ids[id],
c.c[0],
c.c[1],
c.c[2],
c.c[3]);
return he->ip ;
}
}
struct hash_entry *
ip_to_mac_ip(unsigned int netip)
{
unsigned int ipkey = ipkey_get(netip);
struct hash_entry *he ;
he = sb->ip_to_mac[ipkey] ;
if ( he == NULL )
{
union conv c = { .u = netip } ;
WARNING("No IP entry for %u.%u.%u.%u\n",
c.c[0],
c.c[1],
c.c[2],
c.c[3]);
return NULL ;
}
/* No collisions handling needed here , civilised IP is unique key */
return he ;
}