-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmerwall_module.c
443 lines (367 loc) · 9.57 KB
/
merwall_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
/* For separation in shared header include with userspace. */
#define MER_KERNELSPACE
#include "merwall_module.h"
/* Rules linked list */
static LIST_HEAD(rules_list);
/* Netfilter hook structures: Incoming and Outgoing traffic. */
static struct nf_hook_ops nfhi;
static struct nf_hook_ops nfho;
/* Incoming traffic hook. */
unsigned int merwall_hook_in(unsigned int hooknum, struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
return packet_handle(skb, DIRECTION_IN);
}
/* Outgoing traffic hook. */
unsigned int merwall_hook_out(unsigned int hooknum, struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
return packet_handle(skb, DIRECTION_OUT);
}
/* Packet handler.
*
* @skb sk_buff packet structure.
* @direction DIRECTION_* value for traffic direction (Incoming/Outgoing.)
*
* @return NF_* value. Based on rules list matching. */
unsigned int packet_handle(const struct sk_buff *skb, __u8 direction)
{
struct mer_rule *rule, packet_info;
/* Get packet info in a mer_rule style structure */
memset(&packet_info, '\0', sizeof(packet_info));
packet_info.direction = direction;
if (parse_packet(skb, &packet_info))
return NF_ACCEPT;
/* Match packet with every rule */
list_for_each_entry(rule, &rules_list, list)
if (rule_match(rule, &packet_info)) {
switch(rule->action) {
case ACT_DROP:
printk("Merwall: #%d -> Drop packet.\n",
rule->index);
return NF_DROP;
case ACT_PASS:
printk("Merwall: #%d -> Pass packet.\n",
rule->index);
return NF_STOP;
case ACT_LOG:
printk("Merwall: #%d -> Log packet (XXX).\n",
rule->index);
return NF_ACCEPT;
}
}
/* Default is to accept packet. */
return NF_ACCEPT;
}
/*
* Extracts relevant information from an sk_buff structure into a mer_rule
* structure to be easily matched with rules list.
*
* @return 1 if error, 0 otherwise.
*/
static int parse_packet(const struct sk_buff *skb, struct mer_rule *packet_info)
{
struct iphdr *ip_header;
if (!packet_info || !skb)
return 1;
ip_header = (struct iphdr *) skb_network_header(skb);
packet_info->srcip = ip_header->saddr;
packet_info->dstip = ip_header->daddr;
/* Network Protocol */
if (ip_header->protocol == IPPROTO_TCP) {
struct tcphdr *tcp_header;
packet_info->proto = PROTO_TCP;
tcp_header = (struct tcphdr *) ((char *) ip_header
+ (ip_header->ihl * 4));
packet_info->srcport = ntohs(tcp_header->source);
packet_info->dstport = ntohs(tcp_header->dest);
}
else if (ip_header->protocol == IPPROTO_UDP) {
struct udphdr *udp_header;
packet_info->proto = PROTO_UDP;
udp_header = (struct udphdr *) ((char *) ip_header
+ (ip_header->ihl * 4));
packet_info->srcport = ntohs(udp_header->source);
packet_info->dstport = ntohs(udp_header->dest);
}
else
packet_info->proto = PROTO_ALL;
return 0;
}
/*
* @brief Checks whether a rule matches with a packet's info.
*
* @return 1: if rule matches packet info, 0 otherwise.
*/
static int rule_match(const struct mer_rule *rule,
const struct mer_rule *pinfo)
{
if (rule->direction != DIRECTION_ALL
&& rule->direction != pinfo->direction)
return 0;
if (rule->proto != PROTO_ALL
&& rule->proto != pinfo->proto)
return 0;
if (rule->srcip && rule->srcip != pinfo->srcip)
return 0;
if (rule->dstip && rule->dstip != pinfo->dstip)
return 0;
if (rule->srcport && rule->srcport != pinfo->srcport)
return 0;
if (rule->dstport && rule->dstport != pinfo->dstport)
return 0;
return 1;
}
static struct mer_rule* rule_create(const char *buffer, size_t size)
{
int cmd, action, index, direction, proto, srcport, dstport;
struct mer_rule *new_rule;
__u32 srcip, dstip;
if (sscanf(buffer, "%d %d %d %d %d %u %u %d %d", &cmd, &index, &action,
&direction, &proto, &srcip, &dstip, &srcport, &dstport)
!= 9) {
printk("Merwall: sscanf() failed.\n");
return NULL;
}
if (cmd != CMD_ADD)
return NULL;
if (action < 0 || action >= ACT_MAX)
return NULL;
if (index < 0)
return NULL;
/* Default index: Higher than all current rules in list. */
if (index == 0)
index = rules_index_new();
if (direction < 0 || direction >= DIRECTION_MAX)
return NULL;
if (proto < 0 || proto > PROTO_MAX)
return NULL;
if (srcport < 0 || srcport > PORT_MAX)
return NULL;
if (dstport < 0 || dstport > PORT_MAX)
return NULL;
new_rule = kmalloc(sizeof(*new_rule), GFP_KERNEL);
if (!new_rule)
return NULL;
new_rule->index = index;
new_rule->action = action;
new_rule->direction = direction;
new_rule->proto = proto;
new_rule->srcip = srcip;
new_rule->dstip = dstip;
new_rule->srcport = srcport;
new_rule->dstport = dstport;
return new_rule;
}
static void rule_add(struct mer_rule *newrule)
{
struct mer_rule *rule = NULL;
/* Find where to insert new rule. */
list_for_each_entry(rule, &rules_list, list) {
if (rule->index > newrule->index)
break;
}
/* Insert rule */
if (rule)
list_add_tail(&newrule->list, &rule->list);
/* Either list is empty, or new index is higher than all others. */
else
list_add_tail(&newrule->list, &rules_list);
printk("Merwall: #%d added.\n", newrule->index);
}
static int rule_delete(const char *buffer, size_t size, int *index)
{
int cmd;
struct mer_rule *rule, *tmp;
if (!index)
return -1;
sscanf(buffer, "%d %d", &cmd, index);
if (cmd != CMD_DEL)
return -1;
list_for_each_entry_safe(rule, tmp, &rules_list, list)
if (rule->index == *index) {
printk("Merwall: #%d deleted.\n", rule->index);
list_del(&rule->list);
rule_free(rule);
return 0;
}
return 1;
}
static int rule_show(struct mer_rule *rule, char *buf, int size)
{
return snprintf(buf, size, "#%d: %s %s %pI4->%pI4 %s:%d->%d\n",
rule->index,
action_to_str(rule->action),
direction_to_str(rule->direction),
&rule->srcip,
&rule->dstip,
proto_to_str(rule->proto),
rule->srcport,
rule->dstport);
}
/*
* Check if a rule in list has provided index already.
*/
static int rules_index_exists(unsigned int index)
{
struct mer_rule *rule;
list_for_each_entry(rule, &rules_list, list)
if (rule->index == index)
return 1;
return 0;
}
/*
* Get new rule index.
* Returned index is higher than all existing ones in list.
*/
static unsigned int rules_index_new()
{
struct mer_rule *rule;
/* Rules are ordered by index. Last index is highest */
if (rules_list.prev != &rules_list) {
rule = list_entry(rules_list.prev, struct mer_rule, list);
return rule->index + 1;
}
return 1;
}
/*
* Delete all rules in list.
*/
static void rules_delete(void)
{
struct mer_rule *rule, *tmp;
list_for_each_entry_safe(rule, tmp, &rules_list, list) {
list_del(&rule->list);
rule_free(rule);
}
}
/*
* Free rule memory.
*/
static void rule_free(struct mer_rule *rule)
{
kfree(rule);
}
static ssize_t mer_show(struct class *cls, struct class_attribute *attr,
char *buf)
{
struct mer_rule *rule;
int index = 0;
list_for_each_entry(rule, &rules_list, list) {
index += rule_show(rule, buf + index, PAGE_SIZE - index);
if (index >= PAGE_SIZE) {
// XXX
printk("Merwall: Rules print higher than page size.\n");
break;
}
}
return index;
}
static ssize_t mer_store(struct class *cls,
struct class_attribute *attr,
const char *buffer, size_t count)
{
struct mer_rule *rule;
int command;
unsigned int index;
sscanf(buffer, "%d", &command);
printk("Merwall: %s CMD.\n", cmd_to_str(command));
switch(command) {
case CMD_ADD:
/* Scan buffer and create rule structure. */
rule = rule_create(buffer, count);
if (!rule) {
printk("Merwall: Rule creation failed.\n");
return count;
}
/* Check that the index is unique. */
if (rules_index_exists(rule->index)) {
printk("Merwall: #%d exists already.\n",
rule->index);
rule_free(rule);
return count;
}
/* Add rule to rules list. */
rule_add(rule);
return count;
case CMD_DEL:
if (rule_delete(buffer, count, &index))
printk("Merwall: #%d not found.\n", index);
return count;
case CMD_FLUSH:
rules_delete();
return count;
default:
printk("Merwall: Unknown command value %d.\n", command);
return count;
}
}
/*
* Sysfs class
*/
static struct class *mer_class;
static const struct class_attribute mer_attr = {
.attr = {
.name = "merwall_file",
.mode = S_IWUSR | S_IRUGO,
},
.show = mer_show,
.store = mer_store,
};
/*
* Create sysfs class/file
*/
int sysfs_init(void)
{
mer_class = class_create(THIS_MODULE, "merwall");
if (IS_ERR(mer_class)) {
pr_err("Couldn't create Sysfs class.\n");
return PTR_ERR(mer_class);
}
return class_create_file(mer_class, &mer_attr);
}
/*
* Initialize module.
*/
int merwall_init(void)
{
printk("Merwall: module inserted.\n");
/* Register Netfilter hooks. */
/* Incoming */
nfhi.hook = merwall_hook_in;
nfhi.hooknum = NF_INET_PRE_ROUTING;
nfhi.pf = PF_INET;
nfhi.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfhi);
/* Outgoing */
nfho.hook = merwall_hook_out;
nfho.hooknum = NF_INET_POST_ROUTING;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfho);
/* Create sysfs class */
return sysfs_init();
}
/*
* Remove module.
*/
void merwall_exit(void)
{
/* Unregister Netfilter hooks. */
nf_unregister_hook(&nfhi);
nf_unregister_hook(&nfho);
/* Destroy sysfs class */
class_destroy(mer_class);
/* Free rules */
rules_delete();
printk("Merwall: module removed.\n");
}
module_init(merwall_init);
module_exit(merwall_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Hani Benhabiles <hani.benhabiles@owasp.org>");
MODULE_DESCRIPTION("Merwall is a lightweight firewall.");