-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook5_2.6_32_v6.c
225 lines (199 loc) · 5.92 KB
/
hook5_2.6_32_v6.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
//#define _IPV4_
#include <linux/init.h>
#include <linux/module.h>
#include <linux/ip.h>
#include <linux/ipv6.h>
#include <linux/tcp.h>
#include <linux/netfilter.h>
#include <linux/netfilter_ipv4.h>
#include <linux/netfilter_ipv6.h>
MODULE_LICENSE("GPL");
#ifdef _IPV4_
inline void dumpIpHdr(const char *fn, const struct sk_buff *skb)
{
const struct iphdr *ip = ip_hdr(skb);
const struct tcphdr *tcp = tcp_hdr(skb);
// unsigned char *target_port = "\x00\x50"; // port:80
// if( (tcp->source == *(unsigned short *)target_port)||(tcp->dest == *(unsigned short *)target_port) )
// {
printk(KERN_ALERT "%s, saddr:%pI4:%hu, daddr:%pI4:%hu\n", fn, &ip->saddr, ntohs(tcp->source), &ip->daddr, ntohs(tcp->dest));
if(!strcmp(fn, "postrouting"))
printk(KERN_ALERT "---------------------------------\n");
// }
// ref: sort by ICMP
//if(iph->protocol == IPPROTO_ICMP)
//{
// printk(KERN_INFO"hook_icmp::icmp_srv: receive ICMP packet\n");
// printk(KERN_INFO"src: ");
//}
}
static unsigned int
prerouting(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIpHdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
localin(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIpHdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
localout(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIpHdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
postrouting(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIpHdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
fwding(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIpHdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
#endif
inline void dumpIp6Hdr(const char *fn, const struct sk_buff *skb)
{
const struct ipv6hdr *ipv6 = ipv6_hdr(skb);
const struct tcphdr *tcp = tcp_hdr(skb);
printk(KERN_ALERT "%s, saddr:%pI6:%hu, daddr:%pI6:%hu\n", fn, &ipv6->saddr, ntohs(tcp->source), &ipv6->daddr, ntohs(tcp->dest));
//printk(KERN_ALERT "%s, saddr:%pI6:%d, daddr:%pI6:%d\n", fn, &ipv6->saddr, ntohs(tcp->source), &ipv6->daddr, ntohs(tcp->dest));
//printk(KERN_ALERT "%s, saddr:%pI6:, daddr:%pI6:\n", fn, &ipv6->saddr, &ipv6->daddr );
if(!strcmp(fn, "postrouting6"))
printk(KERN_ALERT "---------------------------------\n");
}
static unsigned int
prerouting6(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIp6Hdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
localin6(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIp6Hdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
localout6(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIp6Hdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
postrouting6(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIp6Hdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static unsigned int
fwding6(unsigned int hook, struct sk_buff *skb,
const struct net_device *in, const struct net_device *out,
int (*okfn)(struct sk_buff*))
{
dumpIp6Hdr(__FUNCTION__, skb);
return NF_ACCEPT;
}
static struct nf_hook_ops brook_ops[] __read_mostly = {
#ifdef _IPV4_
{
.hook = prerouting,
.pf = PF_INET,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP_PRI_RAW,
.owner = THIS_MODULE,
}, {
.hook = localin,
.pf = PF_INET,
.hooknum = NF_INET_LOCAL_IN,
.priority = NF_IP_PRI_RAW,
.owner = THIS_MODULE,
}, {
.hook = fwding,
.pf = PF_INET,
.hooknum = NF_INET_FORWARD,
.priority = NF_IP_PRI_RAW,
.owner = THIS_MODULE,
}, {
.hook = localout,
.pf = PF_INET,
.hooknum = NF_INET_LOCAL_OUT,
.priority = NF_IP_PRI_RAW,
.owner = THIS_MODULE,
}, {
.hook = postrouting,
.pf = PF_INET,
.hooknum = NF_INET_POST_ROUTING,
.priority = NF_IP_PRI_RAW,
.owner = THIS_MODULE,
},
#endif
{
.hook = prerouting6,
.pf = PF_INET6,
.hooknum = NF_INET_PRE_ROUTING,
.priority = NF_IP6_PRI_FIRST,
.owner = THIS_MODULE,
}, {
.hook = localin6,
.pf = PF_INET6,
.hooknum = NF_INET_LOCAL_IN,
.priority = NF_IP6_PRI_FIRST,
.owner = THIS_MODULE,
}, {
.hook = fwding6,
.pf = PF_INET6,
.hooknum = NF_INET_FORWARD,
.priority = NF_IP6_PRI_FIRST,
.owner = THIS_MODULE,
}, {
.hook = localout6,
.pf = PF_INET6,
.hooknum = NF_INET_LOCAL_OUT,
.priority = NF_IP6_PRI_FIRST,
.owner = THIS_MODULE,
}, {
.hook = postrouting6,
.pf = PF_INET6,
.hooknum = NF_INET_POST_ROUTING,
.priority = NF_IP6_PRI_FIRST,
.owner = THIS_MODULE,
},
};
static int __init init_modules(void)
{
if (nf_register_hooks(brook_ops, ARRAY_SIZE(brook_ops)) < 0) {
printk("nf_register_hook failed\n");
}
return 0;
}
static void __exit exit_modules(void)
{
nf_unregister_hooks(brook_ops, ARRAY_SIZE(brook_ops));
}
module_init(init_modules);
module_exit(exit_modules);