Added the new version for dummynet.
[ipfw.git] / dummynet / ipfw2_mod.c
1 /*
2  * Copyright (C) 2009 Luigi Rizzo, Marta Carbone, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 /*
27  * $Id$
28  *
29  * The main interface to build ipfw+dummynet as a linux module.
30  * (and possibly as a windows module as well, though that part
31  * is not complete yet).
32  *
33  * The control interface uses the sockopt mechanism
34  * on a socket(AF_INET, SOCK_RAW, IPPROTO_RAW).
35  *
36  * The data interface uses the netfilter interface, at the moment
37  * hooked to the PRE_ROUTING and POST_ROUTING hooks.
38  * Unfortunately the netfilter interface is a moving target,
39  * so we need a set of macros to adapt to the various cases.
40  *
41  * In the netfilter hook we just mark packet as 'QUEUE' and then
42  * let the queue handler to do the whole work (filtering and
43  * possibly emulation).
44  * As we receive packets, we wrap them with an mbuf descriptor
45  * so the existing ipfw+dummynet code runs unmodified.
46  */
47
48 #include <sys/cdefs.h>
49 #include <sys/mbuf.h>                   /* sizeof struct mbuf */
50 #include <sys/param.h>                  /* NGROUPS */
51
52 #ifdef __linux__
53 #include <linux/module.h>
54 #include <linux/kernel.h>
55 #include <linux/netfilter.h>
56 #include <linux/netfilter_ipv4.h>       /* NF_IP_PRI_FILTER */
57
58 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,25)
59 #include <net/netfilter/nf_queue.h>     /* nf_queue */
60 #endif
61
62 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
63 #define __read_mostly
64 #endif
65
66 #endif /* !__linux__ */
67
68 #include <netinet/in.h>                 /* in_addr */
69 #include <netinet/ip_fw.h>              /* ip_fw_ctl_t, ip_fw_chk_t */
70 #include <netinet/ip_dummynet.h>        /* ip_dn_ctl_t, ip_dn_io_t */
71 #include <net/pfil.h>                   /* PFIL_IN, PFIL_OUT */
72 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
73 #warning --- inet_hashtables not present on 2.4
74 #include <linux/tcp.h>
75 #include <net/route.h>
76 #include <net/sock.h>
77 static inline int inet_iif(const struct sk_buff *skb)
78 {
79         return ((struct rtable *)skb->dst)->rt_iif;
80 }
81
82 #else
83 #include <net/inet_hashtables.h>        /* inet_lookup */
84 #endif
85 #include <net/route.h>                  /* inet_iif */
86
87 /*
88  * Here we allocate some global variables used in the firewall.
89  */
90 //ip_dn_ctl_t    *ip_dn_ctl_ptr;
91 int (*ip_dn_ctl_ptr)(struct sockopt *);
92
93 ip_fw_ctl_t    *ip_fw_ctl_ptr;
94
95 int     (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
96 ip_fw_chk_t    *ip_fw_chk_ptr;
97
98 void            (*bridge_dn_p)(struct mbuf *, struct ifnet *);
99
100 /*---
101  * Glue code to implement the registration of children with the parent.
102  * Each child should call my_mod_register() when linking, so that
103  * module_init() and module_exit() can call init_children() and
104  * fini_children() to provide the necessary initialization.
105  */
106 #include <sys/module.h>
107 struct mod_args {
108         struct moduledata *mod;
109         const char *name;
110         int order;
111 };
112
113 static unsigned int mod_idx;
114 static struct mod_args mods[10];        /* hard limit to 10 modules */
115
116 /*
117  * my_mod_register should be called automatically as the init
118  * functions in the submodules. Unfortunately this compiler/linker
119  * trick is not supported yet so we call it manually.
120  */
121 int
122 my_mod_register(struct moduledata *mod, const char *name, int order)
123 {
124         struct mod_args m = { mod, name, order };
125
126         printf("%s %s called\n", __FUNCTION__, name);
127         if (mod_idx < sizeof(mods) / sizeof(mods[0]))
128                 mods[mod_idx++] = m;
129         return 0;
130 }
131
132 static void
133 init_children(void)
134 {
135         unsigned int i;
136
137         /* Call the functions registered at init time. */
138         printf("%s mod_idx value %d\n", __FUNCTION__, mod_idx);
139         for (i = 0; i < mod_idx; i++) {
140                 printf("+++ start module %d %s %s at %p order 0x%x\n",
141                         i, mods[i].name, mods[i].mod->name,
142                         mods[i].mod, mods[i].order);
143                 mods[i].mod->evhand(NULL, MOD_LOAD, mods[i].mod->priv);
144         }
145 }
146
147 static void
148 fini_children(void)
149 {
150         int i;
151
152         /* Call the functions registered at init time. */
153         for (i = mod_idx - 1; i >= 0; i--) {
154                 printf("+++ end module %d %s %s at %p order 0x%x\n",
155                         i, mods[i].name, mods[i].mod->name,
156                         mods[i].mod, mods[i].order);
157                 mods[i].mod->evhand(NULL, MOD_UNLOAD, mods[i].mod->priv);
158         }
159 }
160 /*--- end of module binding helper functions ---*/
161
162 /*---
163  * Control hooks:
164  * ipfw_ctl_h() is a wrapper for linux to FreeBSD sockopt call convention.
165  * then call the ipfw handler in order to manage requests.
166  * In turn this is called by the linux set/get handlers.
167  */
168 static int
169 ipfw_ctl_h(struct sockopt *s, int cmd, int dir, int len, void __user *user)
170 {
171         struct thread t;
172         int ret = EINVAL;
173
174         memset(s, 0, sizeof(s));
175         s->sopt_name = cmd;
176         s->sopt_dir = dir;
177         s->sopt_valsize = len;
178         s->sopt_val = user;
179
180         /* sopt_td is not used but it is referenced */
181         memset(&t, 0, sizeof(t));
182         s->sopt_td = &t;
183         
184         // printf("%s called with cmd %d len %d\n", __FUNCTION__, cmd, len);
185
186         if (cmd < IP_DUMMYNET_CONFIGURE && ip_fw_ctl_ptr)
187                 ret = ip_fw_ctl_ptr(s);
188         else if (cmd >= IP_DUMMYNET_CONFIGURE && ip_dn_ctl_ptr)
189                 ret = ip_dn_ctl_ptr(s);
190
191         return -ret;    /* errors are < 0 on linux */
192 }
193
194 #ifdef _WIN32
195
196 void
197 netisr_dispatch(int __unused num, struct mbuf *m)
198 {
199 }
200
201 int
202 ip_output(struct mbuf *m, struct mbuf __unused *opt,
203         struct route __unused *ro, int __unused flags,
204     struct ip_moptions __unused *imo, struct inpcb __unused *inp)
205 {
206         netisr_dispatch(0, m);
207         return 0;
208 }
209
210 #else /* this is the linux glue */
211 /*
212  * setsockopt hook has no return value other than the error code.
213  */
214 static int
215 do_ipfw_set_ctl(struct sock __unused *sk, int cmd,
216         void __user *user, unsigned int len)
217 {
218         struct sockopt s;       /* pass arguments */
219
220         return ipfw_ctl_h(&s, cmd, SOPT_SET, len, user);
221 }
222
223 /*
224  * getsockopt can can return a block of data in response.
225  */
226 static int
227 do_ipfw_get_ctl(struct sock __unused *sk,
228         int cmd, void __user *user, int *len)
229 {
230         struct sockopt s;       /* pass arguments */
231         int ret = ipfw_ctl_h(&s, cmd, SOPT_GET, *len, user);
232
233         *len = s.sopt_valsize;  /* return lenght back to the caller */
234         return ret;
235 }
236
237 /*
238  * declare our [get|set]sockopt hooks
239  */
240 static struct nf_sockopt_ops ipfw_sockopts = {
241         .pf             = PF_INET,
242         .set_optmin     = _IPFW_SOCKOPT_BASE,
243         .set_optmax     = _IPFW_SOCKOPT_END,
244         .set            = do_ipfw_set_ctl,
245         .get_optmin     = _IPFW_SOCKOPT_BASE,
246         .get_optmax     = _IPFW_SOCKOPT_END,
247         .get            = do_ipfw_get_ctl,
248 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,24)
249         .owner          = THIS_MODULE,
250 #endif
251 };
252
253 /*----
254  * We need a number of macros to adapt to the various APIs in
255  * different linux versions. Among them:
256  *
257  * - the hook names change between macros (NF_IP*) and enum NF_INET_*
258  *
259  * - the second argument to the netfilter hook is
260  *      struct sk_buff **       in kernels <= 2.6.22
261  *      struct sk_buff *        in kernels > 2.6.22
262  *
263  * - NF_STOP is not defined before 2.6 so we remap it to NF_ACCEPT
264  *
265  * - the packet descriptor passed to the queue handler is
266  *      struct nf_info          in kernels <= 2.6.24
267  *      struct nf_queue_entry   in kernels <= 2.6.24
268  *
269  * - the arguments to the queue handler also change;
270  */
271
272 /*
273  * declare hook to grab packets from the netfilter interface.
274  * The NF_* names change in different versions of linux, in some
275  * cases they are #defines, in others they are enum, so we
276  * need to adapt.
277  */
278 #ifndef NF_IP_PRE_ROUTING
279 #define NF_IP_PRE_ROUTING       NF_INET_PRE_ROUTING
280 #endif
281 #ifndef NF_IP_POST_ROUTING
282 #define NF_IP_POST_ROUTING      NF_INET_POST_ROUTING
283 #endif
284
285 /*
286  * ipfw hooks into the POST_ROUTING and the PRE_ROUTING chains.
287  * PlanetLab sets skb_tag to the slice id in the LOCAL_INPUT and
288  * POST_ROUTING chains, so if we want to use that information we
289  * need to hook the LOCAL_INPUT chain instead of the PRE_ROUTING.
290  * However at the moment the skb_tag info is not reliable so
291  * we stay with the standard hooks.
292  */
293 #if 0 // defined(IPFW_PLANETLAB)
294 #define IPFW_HOOK_IN NF_IP_LOCAL_IN
295 #else
296 #define IPFW_HOOK_IN NF_IP_PRE_ROUTING
297 #endif
298
299 /*
300  * The main netfilter hook.
301  * To make life simple, we queue everything and then do all the
302  * decision in the queue handler.
303  *
304  * XXX note that in 2.4 and up to 2.6.22 the skbuf is passed as sk_buff**
305  * so we have an #ifdef to set the proper argument type.
306  */
307 static unsigned int
308 call_ipfw(unsigned int __unused hooknum,
309 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) // in 2.6.22 we have **
310         struct sk_buff  __unused **skb,
311 #else
312         struct sk_buff  __unused *skb,
313 #endif
314         const struct net_device  __unused *in,
315         const struct net_device  __unused *out,
316         int __unused (*okfn)(struct sk_buff *))
317 {
318         return NF_QUEUE;
319 }
320
321 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
322 #define NF_STOP         NF_ACCEPT
323 #endif
324
325 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
326
327 /*
328  * nf_queue_entry is a recent addition, in previous versions
329  * of the code the struct is called nf_info.
330  */
331 #define nf_queue_entry  nf_info /* for simplicity */
332
333 /* also, 2.4 and perhaps something else have different arguments */
334 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)  /* unsure on the exact boundary */
335 /* on 2.4 we use nf_info */
336 #define QH_ARGS         struct sk_buff *skb, struct nf_info *info, void *data
337 #else   /* 2.6.1.. 2.6.24 */
338 #define QH_ARGS         struct sk_buff *skb, struct nf_info *info, unsigned int qnum, void *data
339 #endif
340
341 #define DEFINE_SKB      /* nothing, already an argument */
342 #define REINJECT(_inf, _verd)   nf_reinject(skb, _inf, _verd)
343
344 #else   /* 2.6.25 and above */
345
346 #define QH_ARGS         struct nf_queue_entry *info, unsigned int queuenum
347 #define DEFINE_SKB      struct sk_buff *skb = info->skb;
348 #define REINJECT(_inf, _verd)   nf_reinject(_inf, _verd)
349 #endif
350
351 /*
352  * used by dummynet when dropping packets
353  * XXX use dummynet_send()
354  */
355 void
356 reinject_drop(struct mbuf* m)
357 {
358 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) /* unsure on the exact boundary */
359         struct sk_buff *skb = (struct sk_buff *)m;
360 #endif
361         REINJECT(m->queue_entry, NF_DROP);
362 }
363
364 /*
365  * The real call to the firewall. nf_queue_entry points to the skbuf,
366  * and eventually we need to return both through nf_reinject().
367  */
368 static int
369 ipfw2_queue_handler(QH_ARGS)
370 {
371         DEFINE_SKB      /* no semicolon here, goes in the macro */
372         int ret = 0;    /* return value */
373         struct mbuf *m;
374
375 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
376         if (skb->nh.iph == NULL) {
377                 printf("null dp, len %d reinject now\n", skb->len);
378                 REINJECT(info, NF_ACCEPT);
379                 return 0;
380         }
381 #endif
382         m = malloc(sizeof(*m), 0, 0);
383         if (m == NULL) {
384                 printf("malloc fail, len %d reinject now\n", skb->len);
385                 REINJECT(info, NF_ACCEPT);
386                 return 0;
387         }
388
389         m->m_skb = skb;
390         m->m_len = skb->len;            /* len in this skbuf */
391         m->m_pkthdr.len = skb->len;     /* total packet len */
392         m->m_pkthdr.rcvif = info->indev;
393         m->queue_entry = info;
394 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
395         m->m_data = skb->nh.iph;
396 #else
397         m->m_data = skb_network_header(skb);
398 #endif
399
400         /* XXX add the interface */
401         if (info->hook == IPFW_HOOK_IN) {
402                 ret = ipfw_check_in(NULL, &m, info->indev, PFIL_IN, NULL);
403         } else {
404                 ret = ipfw_check_out(NULL, &m, info->outdev, PFIL_OUT, NULL);
405         }
406
407         if (m != NULL) {        /* Accept. reinject and free the mbuf */
408                 REINJECT(info, NF_ACCEPT);
409                 m_freem(m);
410         } else if (ret == 0) {
411                 /* dummynet has kept the packet, will reinject later. */
412         } else {
413                 /*
414                  * Packet dropped by ipfw or dummynet, reinject as NF_DROP
415                  * mbuf already released by ipfw itself
416                  */
417                 REINJECT(info, NF_DROP);
418         }
419         return 0;
420 }
421
422 struct route;
423 struct ip_moptions;
424 struct inpcb;
425
426
427 /* XXX should include prototypes for netisr_dispatch and ip_output */
428 /*
429  * The reinjection routine after a packet comes out from dummynet.
430  * We must update the skb timestamp so ping reports the right time.
431  */
432 void
433 netisr_dispatch(int num, struct mbuf *m)
434 {
435         struct nf_queue_entry *info = m->queue_entry;
436         struct sk_buff *skb = m->m_skb; /* always used */
437
438         m_freem(m);
439
440         KASSERT((info != NULL), ("%s info null!\n", __FUNCTION__));
441 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)        // XXX above 2.6.x ?
442         __net_timestamp(skb);   /* update timestamp */
443 #endif
444
445         /* XXX to obey one-pass, possibly call the queue handler here */
446         REINJECT(info, ((num == -1)?NF_DROP:NF_STOP));  /* accept but no more firewall */
447 }
448
449 int
450 ip_output(struct mbuf *m, struct mbuf __unused *opt,
451         struct route __unused *ro, int __unused flags,
452     struct ip_moptions __unused *imo, struct inpcb __unused *inp)
453 {
454         netisr_dispatch(0, m);
455         return 0;
456 }
457
458 /*
459  * socket lookup function for linux.
460  * This code is used to associate uid, gid, jail/xid to packets,
461  * and store the info in a cache *ugp where they can be accessed quickly.
462  * The function returns 1 if the info is found, -1 otherwise.
463  *
464  * We do this only on selected protocols: TCP, ...
465  *
466  * The chain is the following
467  *   sk_buff*  sock*  socket*    file*
468  *      skb  ->  sk ->sk_socket->file ->f_owner    ->pid
469  *      skb  ->  sk ->sk_socket->file ->f_uid (direct)
470  *      skb  ->  sk ->sk_socket->file ->f_cred->fsuid (2.6.29+)
471  *
472  * Related headers:
473  * linux/skbuff.h       struct skbuff
474  * net/sock.h           struct sock
475  * linux/net.h          struct socket
476  * linux/fs.h           struct file
477  *
478  * With vserver we may have sk->sk_xid and sk->sk_nid that
479  * which we store in fw_groups[1] (matches O_JAIL) and fw_groups[2]
480  * (no matches yet)
481  *
482  * Note- for locally generated, outgoing packets we should not need
483  * need a lookup because the sk_buff already points to the socket where
484  * the info is.
485  */
486 extern struct inet_hashinfo tcp_hashinfo;
487 int
488 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
489                 const __be32 daddr, const __be16 dport,
490                 struct sk_buff *skb, int dir, struct bsd_ucred *u)
491 {
492 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,0)
493         return -1;
494 #else
495         struct sock *sk;
496         int ret = -1;   /* default return value */
497         int st = -1;    /* state */
498
499
500         if (proto != IPPROTO_TCP)       /* XXX extend for UDP */
501                 return -1;
502
503         if ((dir ? (void *)skb_dst(skb) : (void *)skb->dev) == NULL) {
504                 panic(" -- this should not happen\n");
505                 return -1;
506         }
507
508         if (skb->sk) {
509                 sk = skb->sk;
510         } else {
511                 /*
512                  * Try a lookup. On a match, sk has a refcount that we must
513                  * release on exit (we know it because skb->sk = NULL).
514                  *
515                  * inet_lookup above 2.6.24 has an additional 'net' parameter
516                  * so we use a macro to conditionally supply it.
517                  * swap dst and src depending on the direction.
518                  */
519 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,24)
520 #define _OPT_NET_ARG
521 #else
522 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,26)
523 /* there is no dev_net() on 2.6.25 */
524 #define _OPT_NET_ARG (skb->dev->nd_net),
525 #else   /* 2.6.26 and above */
526 #define _OPT_NET_ARG dev_net(skb->dev),
527 #endif
528 #endif
529                 sk =  (dir) ? /* dir != 0 on output */
530                     inet_lookup(_OPT_NET_ARG &tcp_hashinfo,
531                         daddr, dport, saddr, sport,     // match outgoing
532                         inet_iif(skb)) :
533                     inet_lookup(_OPT_NET_ARG &tcp_hashinfo,
534                         saddr, sport, daddr, dport,     // match incoming
535                         skb->dev->ifindex);
536 #undef _OPT_NET_ARG
537
538                 if (sk == NULL) /* no match, nothing to be done */
539                         return -1;
540         }
541         ret = 1;        /* retrying won't make things better */
542         st = sk->sk_state;
543 #ifdef CONFIG_VSERVER
544         u->xid = sk->sk_xid;
545         u->nid = sk->sk_nid;
546 #else
547         u->xid = u->nid = 0;
548 #endif
549         /*
550          * Exclude tcp states where sk points to a inet_timewait_sock which
551          * has no sk_socket field (surely TCP_TIME_WAIT, perhaps more).
552          * To be safe, use a whitelist and not a blacklist.
553          * Before dereferencing sk_socket grab a lock on sk_callback_lock.
554          *
555          * Once again we need conditional code because the UID and GID
556          * location changes between kernels.
557          */
558 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,28)
559 /* use the current's real uid/gid */
560 #define _CURR_UID f_uid
561 #define _CURR_GID f_gid
562 #else /* 2.6.29 and above */
563 /* use the current's file access real uid/gid */
564 #define _CURR_UID f_cred->fsuid
565 #define _CURR_GID f_cred->fsgid
566 #endif
567
568 #define GOOD_STATES (   \
569         (1<<TCP_LISTEN) | (1<<TCP_SYN_RECV)   | (1<<TCP_SYN_SENT)   | \
570         (1<<TCP_ESTABLISHED)  | (1<<TCP_FIN_WAIT1) | (1<<TCP_FIN_WAIT2) )
571         // surely exclude TCP_CLOSE, TCP_TIME_WAIT, TCP_LAST_ACK
572         // uncertain TCP_CLOSE_WAIT and TCP_CLOSING
573
574         if ((1<<st) & GOOD_STATES) {
575                 read_lock_bh(&sk->sk_callback_lock);
576                 if (sk->sk_socket && sk->sk_socket->file) {
577                         u->uid = sk->sk_socket->file->_CURR_UID;
578                         u->gid = sk->sk_socket->file->_CURR_GID;
579                 }
580                 read_unlock_bh(&sk->sk_callback_lock);
581         } else {
582                 u->uid = u->gid = 0;
583         }
584         if (!skb->sk) /* return the reference that came from the lookup */
585                 sock_put(sk);
586 #undef GOOD_STATES
587 #undef _CURR_UID
588 #undef _CURR_GID
589         return ret;
590
591 #endif /* LINUX > 2.4 */
592 }
593
594 /*
595  * Now prepare to hook the various functions.
596  * Linux 2.4 has a different API so we need some adaptation
597  * for register and unregister hooks
598  *
599  * the unregister function changed arguments between 2.6.22 and 2.6.24
600  */
601 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
602 static int
603 nf_register_hooks(struct nf_hook_ops *ops, int n)
604 {
605         int i, ret = 0;
606         for (i = 0; i < n; i++) {
607                 ret = nf_register_hook(ops + i);
608                 if (ret < 0)
609                         break;
610         }
611         return ret;
612 }
613
614 static void
615 nf_unregister_hooks(struct nf_hook_ops *ops, int n)
616 {
617         int i;
618         for (i = 0; i < n; i++) {
619                 nf_unregister_hook(ops + i);
620         }
621 }
622 #define REG_QH_ARG(fn)  fn, NULL        /* argument for nf_[un]register_queue_handler */
623 #define UNREG_QH_ARG(fn) //fn   /* argument for nf_[un]register_queue_handler */
624 #define SET_MOD_OWNER
625
626 #else /* linux >= 2.6.0 */
627
628 struct nf_queue_handler ipfw2_queue_handler_desc = {
629         .outfn = ipfw2_queue_handler,
630         .name = "ipfw2 dummynet queue",
631 };
632 #define REG_QH_ARG(fn)  &(fn ## _desc)
633
634 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
635 #define UNREG_QH_ARG(fn) //fn   /* argument for nf_[un]register_queue_handler */
636 #else
637 #define UNREG_QH_ARG(fn)        , &(fn ## _desc)
638 #endif /* 2.6.0 < LINUX > 2.6.24 */
639
640 #define SET_MOD_OWNER   .owner = THIS_MODULE,
641
642 #endif  /* !LINUX < 2.6.0 */
643
644 static struct nf_hook_ops ipfw_ops[] __read_mostly = {
645         {
646                 .hook           = call_ipfw,
647                 .pf             = PF_INET,
648                 .hooknum        = IPFW_HOOK_IN,
649                 .priority       = NF_IP_PRI_FILTER,
650                 SET_MOD_OWNER
651         },
652         {
653                 .hook           = call_ipfw,
654                 .pf             = PF_INET,
655                 .hooknum        = NF_IP_POST_ROUTING,
656                 .priority       = NF_IP_PRI_FILTER,
657                 SET_MOD_OWNER
658         },
659 };
660 #endif /* !__linux__ */
661
662 /* descriptors for the children */
663 extern moduledata_t *moddesc_ipfw;
664 extern moduledata_t *moddesc_dummynet;
665
666 extern void rn_init(void);
667 /*
668  * Module glue - init and exit function.
669  */
670 static int __init
671 ipfw_module_init(void)
672 {
673         int ret = 0;
674
675         printf("%s in-hook %d svn id %s\n", __FUNCTION__, IPFW_HOOK_IN, "$Id$");
676
677         rn_init();
678
679         my_mod_register(moddesc_ipfw, "ipfw",  1);
680         my_mod_register(moddesc_dummynet, "dummynet",  2);
681         init_children();
682
683 #ifdef _WIN32
684         return ret;
685
686 #else  /* linux hook */
687         /* sockopt register, in order to talk with user space */
688         ret = nf_register_sockopt(&ipfw_sockopts);
689         if (ret < 0) {
690                 printf("error %d in nf_register_sockopt\n", ret);
691                 goto clean_modules;
692         }
693
694         /* queue handler registration, in order to get network
695          * packet under a private queue */
696         ret = nf_register_queue_handler(PF_INET, REG_QH_ARG(ipfw2_queue_handler) );
697         if (ret < 0)    /* queue busy */
698                 goto unregister_sockopt;
699
700         ret = nf_register_hooks(ipfw_ops, ARRAY_SIZE(ipfw_ops));
701         if (ret < 0)
702                 goto unregister_sockopt;
703
704         printf("%s loaded\n", __FUNCTION__);
705         return 0;
706
707
708 /* handle errors on load */
709 unregister_sockopt:
710         nf_unregister_queue_handler(PF_INET  UNREG_QH_ARG(ipfw2_queue_handler) );
711         nf_unregister_sockopt(&ipfw_sockopts);
712
713 clean_modules:
714         fini_children();
715         printf("%s error\n", __FUNCTION__);
716
717         return ret;
718 #endif  /* linux */
719 }
720
721 /* module shutdown */
722 static void __exit
723 ipfw_module_exit(void)
724 {
725 #ifdef _WIN32
726 #else  /* linux hook */
727         nf_unregister_hooks(ipfw_ops, ARRAY_SIZE(ipfw_ops));
728         /* maybe drain the queue before unregistering ? */
729         nf_unregister_queue_handler(PF_INET  UNREG_QH_ARG(ipfw2_queue_handler) );
730         nf_unregister_sockopt(&ipfw_sockopts);
731 #endif  /* linux */
732
733         fini_children();
734
735         printf("%s unloaded\n", __FUNCTION__);
736 }
737
738 #ifdef __linux__
739 module_init(ipfw_module_init)
740 module_exit(ipfw_module_exit)
741 MODULE_LICENSE("Dual BSD/GPL"); /* the code here is all BSD. */
742 #endif