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
|
/*
* IP_MASQ_MOD masq modules support
*
*
* Version: @(#)ip_masq_mod.c 0.02 97/10/30
*
* Author: Juan Jose Ciarlante, <jjciarla@raiz.uncu.edu.ar>
*
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version
* 2 of the License, or (at your option) any later version.
*
* Changes:
* Cyrus Durgin: fixed kerneld stuff for kmod.
*/
#include <linux/config.h>
#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/errno.h>
#include <net/ip_masq.h>
#include <net/ip_masq_mod.h>
#ifdef CONFIG_KMOD
#include <linux/kmod.h>
#endif
EXPORT_SYMBOL(register_ip_masq_mod);
EXPORT_SYMBOL(unregister_ip_masq_mod);
EXPORT_SYMBOL(ip_masq_mod_lkp_link);
EXPORT_SYMBOL(ip_masq_mod_lkp_unlink);
/*
* Base pointer for registered modules
*/
struct ip_masq_mod * ip_masq_mod_reg_base = NULL;
/*
* Base pointer for lookup (subset of above, a module could be
* registered, but it could have no active rule); will avoid
* unnecessary lookups.
*/
struct ip_masq_mod * ip_masq_mod_lkp_base = NULL;
int ip_masq_mod_register_proc(struct ip_masq_mod *mmod)
{
#ifdef CONFIG_PROC_FS
int ret;
struct proc_dir_entry *ent = mmod->mmod_proc_ent;
if (!ent)
return 0;
if (!ent->name) {
ent->name = mmod->mmod_name;
ent->namelen = strlen (mmod->mmod_name);
}
ret = proc_net_register(ent);
if (ret) mmod->mmod_proc_ent = NULL;
return ret;
#else
return 0;
#endif
}
void ip_masq_mod_unregister_proc(struct ip_masq_mod *mmod)
{
#ifdef CONFIG_PROC_FS
struct proc_dir_entry *ent = mmod->mmod_proc_ent;
if (!ent)
return;
proc_unregister(proc_net, ent->low_ino);
#endif
}
/*
* Link/unlink object for lookups
*/
int ip_masq_mod_lkp_unlink(struct ip_masq_mod *mmod)
{
struct ip_masq_mod **mmod_p;
start_bh_atomic();
for (mmod_p = &ip_masq_mod_lkp_base; *mmod_p ; mmod_p = &(*mmod_p)->next)
if (mmod == (*mmod_p)) {
*mmod_p = mmod->next;
mmod->next = NULL;
end_bh_atomic();
return 0;
}
end_bh_atomic();
return -EINVAL;
}
int ip_masq_mod_lkp_link(struct ip_masq_mod *mmod)
{
start_bh_atomic();
mmod->next = ip_masq_mod_lkp_base;
ip_masq_mod_lkp_base=mmod;
end_bh_atomic();
return 0;
}
int register_ip_masq_mod(struct ip_masq_mod *mmod)
{
if (!mmod) {
IP_MASQ_ERR("register_ip_masq_mod(): NULL arg\n");
return -EINVAL;
}
if (!mmod->mmod_name) {
IP_MASQ_ERR("register_ip_masq_mod(): NULL mmod_name\n");
return -EINVAL;
}
ip_masq_mod_register_proc(mmod);
mmod->next_reg = ip_masq_mod_reg_base;
ip_masq_mod_reg_base=mmod;
return 0;
}
int unregister_ip_masq_mod(struct ip_masq_mod *mmod)
{
struct ip_masq_mod **mmod_p;
if (!mmod) {
IP_MASQ_ERR( "unregister_ip_masq_mod(): NULL arg\n");
return -EINVAL;
}
/*
* Only allow unregistration if it is not referenced
*/
if (atomic_read(&mmod->refcnt)) {
IP_MASQ_ERR( "unregister_ip_masq_mod(): is in use by %d guys. failed\n",
atomic_read(&mmod->refcnt));
return -EINVAL;
}
/*
* Must be already unlinked from lookup list
*/
if (mmod->next) {
IP_MASQ_WARNING("MASQ: unregistering \"%s\" while in lookup list.fixed.",
mmod->mmod_name);
ip_masq_mod_lkp_unlink(mmod);
}
for (mmod_p = &ip_masq_mod_reg_base; *mmod_p ; mmod_p = &(*mmod_p)->next_reg)
if (mmod == (*mmod_p)) {
ip_masq_mod_unregister_proc(mmod);
*mmod_p = mmod->next_reg;
return 0;
}
IP_MASQ_ERR("unregister_ip_masq_mod(%s): not linked \n", mmod->mmod_name);
return -EINVAL;
}
int ip_masq_mod_in_rule(struct iphdr *iph, __u16 *portp)
{
struct ip_masq_mod *mmod;
int ret;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_in_rule) continue;
switch (ret=mmod->mmod_in_rule(iph, portp)) {
case IP_MASQ_MOD_NOP:
continue;
case IP_MASQ_MOD_ACCEPT:
return 1;
case IP_MASQ_MOD_REJECT:
return -1;
}
}
return 0;
}
int ip_masq_mod_out_rule(struct iphdr *iph, __u16 *portp)
{
struct ip_masq_mod *mmod;
int ret;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_out_rule) continue;
switch (ret=mmod->mmod_out_rule(iph, portp)) {
case IP_MASQ_MOD_NOP:
continue;
case IP_MASQ_MOD_ACCEPT:
return 1;
case IP_MASQ_MOD_REJECT:
return -1;
}
}
return 0;
}
struct ip_masq * ip_masq_mod_in_create(struct iphdr *iph, __u16 *portp, __u32 maddr)
{
struct ip_masq_mod *mmod;
struct ip_masq *ms;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_in_create) continue;
if ((ms=mmod->mmod_in_create(iph, portp, maddr))) {
return ms;
}
}
return NULL;
}
struct ip_masq * ip_masq_mod_out_create(struct iphdr *iph, __u16 *portp, __u32 maddr)
{
struct ip_masq_mod *mmod;
struct ip_masq *ms;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_out_create) continue;
if ((ms=mmod->mmod_out_create(iph, portp, maddr))) {
return ms;
}
}
return NULL;
}
int ip_masq_mod_in_update(struct iphdr *iph, __u16 *portp, struct ip_masq *ms)
{
struct ip_masq_mod *mmod;
int ret;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_in_update) continue;
switch (ret=mmod->mmod_in_update(iph, ms)) {
case IP_MASQ_MOD_NOP:
continue;
case IP_MASQ_MOD_ACCEPT:
return 1;
case IP_MASQ_MOD_REJECT:
return -1;
}
}
return 0;
}
int ip_masq_mod_out_update(struct iphdr *iph, __u16 *portp, struct ip_masq *ms)
{
struct ip_masq_mod *mmod;
int ret;
for (mmod=ip_masq_mod_lkp_base;mmod;mmod=mmod->next) {
if (!mmod->mmod_out_update) continue;
switch (ret=mmod->mmod_out_update(iph, portp, ms)) {
case IP_MASQ_MOD_NOP:
continue;
case IP_MASQ_MOD_ACCEPT:
return 1;
case IP_MASQ_MOD_REJECT:
return -1;
}
}
return 0;
}
struct ip_masq_mod * ip_masq_mod_getbyname(const char *mmod_name)
{
struct ip_masq_mod * mmod;
IP_MASQ_DEBUG(1, "searching mmod_name \"%s\"\n", mmod_name);
for (mmod=ip_masq_mod_reg_base; mmod ; mmod=mmod->next_reg) {
if (mmod->mmod_ctl && *(mmod_name)
&& (strcmp(mmod_name, mmod->mmod_name)==0)) {
/* HIT */
return mmod;
}
}
return NULL;
}
/*
* Module control entry
* no need to lock (already locked in ip_masq.c)
*/
int ip_masq_mod_ctl(int optname, struct ip_fw_masqctl *mctl, int optlen)
{
struct ip_masq_mod * mmod;
#ifdef CONFIG_KMOD
char kmod_name[IP_MASQ_MOD_NMAX+8];
#endif
/* tappo */
mctl->u.mod.name[IP_MASQ_MOD_NMAX-1] = 0;
mmod = ip_masq_mod_getbyname(mctl->u.mod.name);
if (mmod)
return mmod->mmod_ctl(optname, mctl, optlen);
#ifdef CONFIG_KMOD
sprintf(kmod_name,"ip_masq_%s", mctl->u.mod.name);
IP_MASQ_DEBUG(1, "About to request \"%s\" module\n", kmod_name);
/*
* Let sleep for a while ...
*/
request_module(kmod_name);
mmod = ip_masq_mod_getbyname(mctl->u.mod.name);
if (mmod)
return mmod->mmod_ctl(optname, mctl, optlen);
#endif
return ESRCH;
}
|