-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdsr-module.c
497 lines (413 loc) · 10.8 KB
/
dsr-module.c
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/* Copyright (C) Uppsala University
*
* This file is distributed under the terms of the GNU general Public
* License (GPL), see the file LICENSE
*
* Author: Erik Nordström, <erikn@it.uu.se>
*/
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
#include <linux/config.h>
#endif
#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <net/protocol.h>
#include <linux/netdevice.h>
#include <linux/if_ether.h>
#include <linux/proc_fs.h>
#include <linux/socket.h>
#include <net/arp.h>
#include <net/ip.h>
#include <net/dst.h>
#include <net/neighbour.h>
#include <asm/uaccess.h>
#include <linux/netfilter_ipv4.h>
#ifdef KERNEL26
#include <linux/moduleparam.h>
#endif
#include <net/icmp.h>
#include <linux/ctype.h>
#include "dsr.h"
#include "dsr-dev.h"
#include "dsr-io.h"
#include "dsr-pkt.h"
#include "debug.h"
#include "neigh.h"
#include "dsr-rreq.h"
#include "dsr-rrep.h"
#include "maint-buf.h"
#include "send-buf.h"
#include "link-cache.h"
static char *ifname = NULL;
static char *mackill = NULL;
MODULE_AUTHOR("erik.nordstrom@it.uu.se");
MODULE_DESCRIPTION("Dynamic Source Routing (DSR) protocol stack");
MODULE_LICENSE("GPL");
#ifdef KERNEL26
module_param(ifname, charp, 0);
module_param(mackill, charp, 0);
#else
MODULE_PARM(ifname, "s");
MODULE_PARM(mackill, "s");
#endif
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25))
#define NF_INET_PRE_ROUTING NF_IP_PRE_ROUTING
#define NF_INET_LOCAL_IN NF_IP_LOCAL_IN
#define NF_INET_FORWARD NF_IP_FORWARD
#define NF_INET_LOCAL_OUT NF_IP_LOCAL_OUT
#define NF_INET_POST_ROUTING NF_IP_POST_ROUTING
#define NF_INET_NUMHOOKS NF_IP_NUMHOOKS
#endif
#define CONFIG_PROC_NAME "dsr_config"
#define MAX_MACKILL 10
static unsigned char mackill_list[MAX_MACKILL][ETH_ALEN];
static int mackill_len = 0;
static char *confval_names[CONFVAL_TYPE_MAX] = {
"Seconds (s)",
"Milliseconds (ms)",
"Microseconds (us)",
"Nanoseconds (ns)",
"Quanta",
"Binary",
"Command"
};
/* Shamelessly stolen from LUNAR <christian.tschudin@unibas.ch> */
static int parse_mackill(void)
{
char *pa[MAX_MACKILL], *cp;
int i, j; // , ia[ETH_ALEN];
cp = mackill;
while (cp && mackill_len < MAX_MACKILL) {
pa[mackill_len] = strsep(&cp, ",");
if (!pa[mackill_len])
break;
mackill_len++;
}
for (i = 0; i < mackill_len; i++) {
// lnx kernel bug in 2.4.X: sscanf format "%x" does not work ....
cp = pa[i];
for (j = 0; j < ETH_ALEN; j++, cp++) {
mackill_list[i][j] = 0;
for (; isxdigit(*cp); cp++) {
mackill_list[i][j] =
(mackill_list[i][j] << 4) |
(*cp <= '9' ? (*cp - '0') :
((*cp & 0x07) + 9));
}
if (*cp && *cp != ':')
break;
}
if (j != ETH_ALEN) {
LOG_DBG("mackill: error in MAC addr %s\n", pa[i]);
mackill_len--;
return -1;
}
LOG_DBG("mackill +%s\n", print_eth(mackill_list[i]));
}
return 0;
}
int do_mackill(char *mac)
{
int i;
for (i = 0; i < mackill_len; i++) {
if (memcmp(mac, mackill_list[i], ETH_ALEN) == 0)
return 1;
}
return 0;
}
int dsr_ip_recv(struct sk_buff *skb)
{
struct dsr_pkt *dp;
#ifdef ENABLE_DEBUG
atomic_inc(&num_pkts);
#endif
LOG_DBG("Received DSR packet\n");
dp = dsr_pkt_alloc(skb);
if (!dp) {
LOG_DBG("Could not allocate DSR packet\n");
dev_kfree_skb_any(skb);
return 0;
}
if (skb->pkt_type == PACKET_OTHERHOST) {
dp->flags |= PKT_PROMISC_RECV;
}
if ((skb->len + (dp->nh.iph->ihl << 2)) < ntohs(dp->nh.iph->tot_len)) {
LOG_DBG("Data to short! IP header len=%d tot_len=%d!\n",
skb->len + (dp->nh.iph->ihl << 2),
ntohs(dp->nh.iph->tot_len));
dsr_pkt_free(dp);
return 0;
}
/* LOG_DBG("iph_len=%d iph_totlen=%d dsr_opts_len=%d data_len=%d\n", */
/* (dp->nh.iph->ihl << 2), ntohs(dp->nh.iph->tot_len), */
/* dsr_pkt_opts_len(dp), dp->payload_len); */
/* Add mac address of previous hop to the arp table */
dsr_recv(dp);
return 0;
};
static void dsr_ip_recv_err(struct sk_buff *skb, u32 info)
{
LOG_DBG("received error, info=%u\n", info);
dev_kfree_skb_any(skb);
}
static int dsr_config_proc_read(char *buffer, char **start,
off_t offset, int length,
int *eof, void *data)
{
int len = 0;
int i;
len += sprintf(buffer + len,
"# %-23s %-6s %s\n", "Name", "Value", "Type");
for (i = 0; i < CONFVAL_MAX; i++)
len += sprintf(buffer + len, "%-25s %-6u %s\n",
confvals_def[i].name,
get_confval(i),
confval_names[confvals_def[i].type]);
*start = buffer + offset;
len -= offset;
if (len > length)
len = length;
else if (len < 0)
len = 0;
return len;
}
static int dsr_config_proc_write(struct file *file, const char *buffer,
unsigned long count, void *data)
{
#define CMD_MAX_LEN 256
char cmd[CMD_MAX_LEN];
int i;
memset(cmd, '\0', CMD_MAX_LEN);
if (count > CMD_MAX_LEN)
return -EINVAL;
/* Don't read the '\n' or '\0' character... */
if (copy_from_user(cmd, buffer, count))
return -EFAULT;
for (i = 0; i < CONFVAL_MAX; i++) {
int n = strlen(confvals_def[i].name);
if (strncmp(cmd, confvals_def[i].name, n) == 0) {
char *from, *to;
unsigned int val, val_prev;
if (confvals_def[i].type == COMMAND) {
if (i == FlushLinkCache)
lc_flush();
break;
}
if (strlen(cmd) - 2 <= n)
continue;
from = strstr(cmd, "=");
from++; /* Exclude '=' */
val_prev = ConfVal(i);
val = simple_strtol(from, &to, 10);
if (confvals_def[i].type == BINARY)
val = (val ? 1 : 0);
set_confval(i, val);
if (i == PromiscOperation && val_prev != val
&& dsr_node) {
if (val) {
LOG_DBG("Setting promiscuous operation\n");
} else {
LOG_DBG("Disabling promiscuous operation\n");
}
dsr_node_lock(dsr_node);
dev_set_promiscuity(dsr_node->slave_dev,
val ? 1 : -1);
dsr_node_unlock(dsr_node);
}
if (i == RequestTableSize)
rreq_tbl_set_max_len(val);
if (i == RexmtBufferSize)
maint_buf_set_max_len(val);
if (i == SendBufferSize)
send_buf_set_max_len(val);
LOG_DBG("Setting %s to %d\n", confvals_def[i].name, val);
}
}
return count;
}
/* This hook is used to do mac filtering or to receive promiscuously snooped
* packets */
static unsigned int dsr_pre_routing_recv(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn) (struct sk_buff *))
{
if (in && in->ifindex == get_slave_dev_ifindex() &&
(skb)->protocol == htons(ETH_P_IP)) {
if (do_mackill(SKB_MAC_HDR_RAW(skb) + ETH_ALEN))
return NF_DROP;
}
return NF_ACCEPT;
}
/* This hook is the architecturally correct place to look at DSR packets that
* are to be forwarded. This enables you to, for example, disable forwarding by
* setting "/proc/sys/net/ipv4/conf/<eth*>/forwarding" to 0. */
static unsigned int dsr_ip_forward_recv(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn) (struct sk_buff *))
{
struct iphdr *iph = SKB_NETWORK_HDR_IPH(skb);
struct in_addr myaddr = my_addr();
if (in && in->ifindex == get_slave_dev_ifindex() &&
skb->protocol == htons(ETH_P_IP)) {
if (iph && iph->protocol == IPPROTO_DSR &&
iph->daddr != myaddr.s_addr &&
iph->daddr != DSR_BROADCAST) {
skb->data = SKB_NETWORK_HDR_RAW(skb) + (iph->ihl << 2);
skb->len = SKB_TAIL(skb) - skb->data;
dsr_ip_recv(skb);
return NF_STOLEN;
}
}
return NF_ACCEPT;
}
static struct nf_hook_ops dsr_pre_routing_hook = {
.hook = dsr_pre_routing_recv,
#ifdef KERNEL26
.owner = THIS_MODULE,
#endif
.pf = PF_INET,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_FIRST,
};
static struct nf_hook_ops dsr_ip_forward_hook = {
.hook = dsr_ip_forward_recv,
#ifdef KERNEL26
.owner = THIS_MODULE,
#endif
.pf = PF_INET,
.hooknum = NF_INET_FORWARD,
.priority = NF_IP_PRI_FIRST,
};
/* This is kind of a mess */
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,7)
static struct inet_protocol dsr_inet_prot = {
#else
static struct net_protocol dsr_inet_prot = {
#endif
.handler = dsr_ip_recv,
.err_handler = dsr_ip_recv_err,
#ifdef KERNEL26
.no_policy = 1,
#else
.protocol = IPPROTO_DSR,
.name = "DSR"
#endif
};
static int __init dsr_module_init(void)
{
int res = -EAGAIN;
struct proc_dir_entry *proc;
#ifdef DEBUG
dbg_init();
#endif
parse_mackill();
res = dsr_dev_init(ifname);
if (res < 0) {
LOG_DBG("dsr-dev init failed\n");
return -EAGAIN;
}
res = send_buf_init();
if (res < 0)
goto cleanup_dsr_dev;
res = rreq_tbl_init();
if (res < 0)
goto cleanup_send_buf;
res = grat_rrep_tbl_init();
if (res < 0)
goto cleanup_grat_rrep_tbl;
res = neigh_tbl_init();
if (res < 0)
goto cleanup_rreq_tbl;
res = nf_register_hook(&dsr_pre_routing_hook);
if (res < 0)
goto cleanup_neigh_tbl;
res = nf_register_hook(&dsr_ip_forward_hook);
if (res < 0)
goto cleanup_nf_hook2;
res = maint_buf_init();
if (res < 0)
goto cleanup_nf_hook1;
#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23))
#define proc_net init_net.proc_net
#endif
proc = create_proc_entry(CONFIG_PROC_NAME, S_IRUGO | S_IWUSR, proc_net);
if (!proc)
goto cleanup_maint_buf;
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30))
proc->owner = THIS_MODULE;
#endif
proc->read_proc = dsr_config_proc_read;
proc->write_proc = dsr_config_proc_write;
#ifndef KERNEL26
inet_add_protocol(&dsr_inet_prot);
LOG_DBG("Setup finished\n");
return 0;
#else
res = inet_add_protocol(&dsr_inet_prot, IPPROTO_DSR);
if (res < 0) {
LOG_DBG("Could not register inet protocol\n");
goto cleanup_proc;
}
LOG_DBG("Setup finished res=%d\n", res);
return 0;
cleanup_proc:
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
proc_net_remove(CONFIG_PROC_NAME);
#else
proc_net_remove(&init_net, CONFIG_PROC_NAME);
#endif
#endif /* KERNEL26 */
cleanup_maint_buf:
maint_buf_cleanup();
cleanup_nf_hook1:
nf_unregister_hook(&dsr_ip_forward_hook);
cleanup_nf_hook2:
nf_unregister_hook(&dsr_pre_routing_hook);
cleanup_neigh_tbl:
neigh_tbl_cleanup();
cleanup_grat_rrep_tbl:
grat_rrep_tbl_cleanup();
cleanup_rreq_tbl:
rreq_tbl_cleanup();
cleanup_send_buf:
send_buf_cleanup();
cleanup_dsr_dev:
dsr_dev_cleanup();
#ifdef DEBUG
dbg_cleanup();
#endif
return res;
}
static void __exit dsr_module_cleanup(void)
{
#ifdef KERNEL26
inet_del_protocol(&dsr_inet_prot, IPPROTO_DSR);
#else
inet_del_protocol(&dsr_inet_prot);
#endif
nf_unregister_hook(&dsr_pre_routing_hook);
nf_unregister_hook(&dsr_ip_forward_hook);
dsr_dev_cleanup();
#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24))
proc_net_remove(CONFIG_PROC_NAME);
#else
proc_net_remove(&init_net, CONFIG_PROC_NAME);
#endif
rreq_tbl_cleanup();
grat_rrep_tbl_cleanup();
neigh_tbl_cleanup();
maint_buf_cleanup();
send_buf_cleanup();
#ifdef DEBUG
dbg_cleanup();
#endif
}
module_init(dsr_module_init);
module_exit(dsr_module_cleanup);