forked from jvns/kernel-module-fun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello-packet.c
More file actions
58 lines (43 loc) · 1.28 KB
/
Copy pathhello-packet.c
File metadata and controls
58 lines (43 loc) · 1.28 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
#include <linux/module.h> // included for all kernel modules
#include <linux/kernel.h> // included for KERN_INFO
#include <linux/init.h> // included for __init and __exit macros
#include <linux/netfilter.h>
#include <linux/vmalloc.h>
//#undef __KERNEL__
#include <linux/netfilter_ipv4.h>
//#define __KERNEL__
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Lakshmanan");
MODULE_DESCRIPTION("A Simple Hello World module");
static struct nf_hook_ops nfho; //net filter hook option struct
unsigned int my_hook(unsigned int hooknum,
struct sk_buff *skb,
const struct net_device *in,
const struct net_device *out,
int (*okfn)(struct sk_buff *)) {
struct sock *sk = skb->sk;
printk("Hello packet!");
return NF_ACCEPT;
}
static int init_filter_if(void)
{
nfho.hook = my_hook;
nfho.hooknum = 0 ; //NF_IP_PRE_ROUTING;
nfho.pf = PF_INET;
nfho.priority = NF_IP_PRI_FIRST;
nf_register_hook(&nfho);
return 0;
}
static int __init hello_init(void)
{
printk(KERN_INFO "Hello world!\n");
init_filter_if();
return 0; // Non-zero return means that the module couldn't be loaded.
}
static void __exit hello_cleanup(void)
{
nf_unregister_hook(&nfho);
printk(KERN_INFO "Cleaning up module.\n");
}
module_init(hello_init);
module_exit(hello_cleanup);