summaryrefslogtreecommitdiff
path: root/2003/netfilter-programming-ols2003/nf_workshop.c
blob: 959226425d808b9af5b970ef349ced69aa354ff9 (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
#include <linux/module.h>
#include <linux/config.h>
#include <linux/skbuff.h>
#include <linux/ip.h>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
MODULE_DESCRIPTION("OLS2003 workshop module");

static unsigned int
workshop_fn(unsigned int hooknum,
	    struct sk_buff **pskb,
	    const struct net_device *in,
	    const struct net_device *out,
	    int (*okfn)(struct sk_buff *))
{
	struct iphdr *iph = (*pskb)->nh.iph;
	/* do whatever we want to do */

	printk(KERN_NOTICE "packet from %u.%u.%u.%u received\n",
		NIPQUAD(iph->saddr));

	return NF_ACCEPT;
}

static struct nf_hook_ops workshop_ops = {
	.list = { .prev = NULL, .next = NULL },
	.hook = &workshop_fn,
	.pf = PF_INET,
	.hooknum = NF_IP_PRE_ROUTING,
	.priority = NF_IP_PRI_LAST-1
};

static int __init init(void)
{
	int ret = 0;

	ret = nf_register_hook(&workshop_ops);
	if (ret < 0) {
		printk(KERN_ERR "something went wrong while registering\n");
		return ret;
	}

	printk(KERN_DEBUG "workshop netfilter module successfully loaded\n");
	return ret;
}

static void __exit fini(void)
{
	nf_unregister_hook(&workshop_ops);
}

module_init(init);
module_exit(fini);
personal git repositories of Harald Welte. Your mileage may vary