Work on the radix code, added support to compile on OpenWRT,
[ipfw.git] / dummynet / missing.h
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  * Header for kernel variables and functions that are not available in
30  * userland.
31  */
32
33 #ifndef _MISSING_H_
34 #define _MISSING_H_
35
36 #ifdef _WIN32
37
38 #ifndef DEFINE_SPINLOCK
39 #define DEFINE_SPINLOCK(x)      FAST_MUTEX x
40 #endif
41 /* spinlock --> Guarded Mutex KGUARDED_MUTEX */
42 /* http://www.reactos.org/wiki/index.php/Guarded_Mutex */
43 #define spin_lock_init(_l)
44 #define spin_lock_bh(_l)
45 #define spin_unlock_bh(_l)
46
47 #include <sys/socket.h>         /* bsd-compat.c */
48 #include <netinet/in.h>         /* bsd-compat.c */
49 #include <netinet/ip.h>         /* local version */
50
51 #else   /* __linux__ */
52
53 #include <linux/time.h>         /* do_gettimeofday */
54 #include <netinet/ip.h>         /* local version */
55 struct inpcb;
56
57 /*
58  * Kernel locking support.
59  * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
60  *
61  * In linux we use spinlock_bh to implement both.
62  * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
63  */
64
65 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
66 #define DEFINE_SPINLOCK(x)   spinlock_t x = SPIN_LOCK_UNLOCKED
67 #endif
68
69 #endif  /* __linux__ */
70
71 #define rw_assert(a, b)
72 #define rw_destroy(_l)
73 #define rw_init(_l, msg)        spin_lock_init(_l)
74 #define rw_rlock(_l)            spin_lock_bh(_l)
75 #define rw_runlock(_l)          spin_unlock_bh(_l)
76 #define rw_wlock(_l)            spin_lock_bh(_l)
77 #define rw_wunlock(_l)          spin_unlock_bh(_l)
78 #define rw_init_flags(_l, s, v)
79
80 #define mtx_assert(a, b)
81 #define mtx_destroy(m)
82 #define mtx_init(m, a,b,c)      spin_lock_init(m)
83 #define mtx_lock(_l)            spin_lock_bh(_l)
84 #define mtx_unlock(_l)          spin_unlock_bh(_l)
85
86 /* end of locking support */
87
88 /* in netinet/in.h */
89 #define        in_nullhost(x)  ((x).s_addr == INADDR_ANY)
90
91 /* bzero not present on linux, but this should go in glue.h */
92 #define bzero(s, n) memset(s, 0, n)
93 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
94
95 /* ethernet stuff */
96 #define ETHERTYPE_IP            0x0800  /* IP protocol */
97 #define ETHER_ADDR_LEN          6       /* length of an Ethernet address */
98 struct ether_header {
99         u_char  ether_dhost[ETHER_ADDR_LEN];
100         u_char  ether_shost[ETHER_ADDR_LEN];
101         u_short ether_type;
102 };
103
104 #define ETHER_ADDR_LEN          6       /* length of an Ethernet address */
105 #define ETHER_TYPE_LEN          2       /* length of the Ethernet type field */
106 #define ETHER_HDR_LEN           (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
107
108 /* ip_dummynet.c */
109 #define __FreeBSD_version 500035
110
111 #ifdef __linux__
112 struct moduledata;
113 int my_mod_register(struct moduledata *mod, const char *name, int order);
114
115 /* define some macro for ip_dummynet */
116
117 struct malloc_type {
118 };
119
120 #define MALLOC_DEFINE(type, shortdesc, longdesc)        \
121         struct malloc_type type[1]; void *md_dummy_ ## type = type
122
123 #define CTASSERT(x)
124
125 #define log(_level, fmt, arg...)  printk(KERN_ERR fmt, ##arg)
126
127 /*
128  * gettimeofday would be in sys/time.h but it is not
129  * visible if _KERNEL is defined
130  */
131 int gettimeofday(struct timeval *, struct timezone *);
132
133 #else  /* _WIN32 */
134 #define MALLOC_DEFINE(a,b,c)
135 #endif /* _WIN32 */
136
137 extern int      hz;
138 extern long     tick;           /* exists in 2.4 but not in 2.6 */
139 extern int      bootverbose;
140 extern time_t   time_uptime;
141 extern struct timeval boottime;
142
143 extern int      max_linkhdr;
144 extern int      ip_defttl;
145 extern u_long   in_ifaddrhmask;                         /* mask for hash table */
146 extern struct in_ifaddrhashhead *in_ifaddrhashtbl;    /* inet addr hash table  */
147
148 /*-------------------------------------------------*/
149
150 /* define, includes and functions missing in linux */
151 /* include and define */
152 #include <arpa/inet.h>          /* inet_ntoa */
153
154 struct mbuf;
155
156 /* used by ip_dummynet.c */
157 void reinject_drop(struct mbuf* m);
158
159 #include <linux/errno.h>        /* error define */
160 #include <linux/if.h>           /* IFNAMESIZ */
161
162 /*
163  * some network structure can be defined in the bsd way
164  * by using the _FAVOR_BSD definition. This is not true
165  * for icmp structure.
166  * XXX struct icmp contains bsd names in 
167  * /usr/include/netinet/ip_icmp.h
168  */
169 #ifdef __linux__
170 #define icmp_code code
171 #define icmp_type type
172
173 /* linux in6_addr has no member __u6_addr
174  * replace the whole structure ?
175  */
176 #define __u6_addr       in6_u
177 #define __u6_addr32     u6_addr32
178 #endif /* __linux__ */
179
180 /* defined in linux/sctp.h with no bsd definition */
181 struct sctphdr {
182         uint16_t src_port;      /* source port */
183         uint16_t dest_port;     /* destination port */
184         uint32_t v_tag;         /* verification tag of packet */
185         uint32_t checksum;      /* Adler32 C-Sum */
186         /* chunks follow... */
187 };
188
189 /* missing definition */
190 #define TH_FIN  0x01
191 #define TH_SYN  0x02
192 #define TH_RST  0x04
193 #define TH_ACK  0x10
194
195 #define RTF_CLONING     0x100           /* generate new routes on use */
196
197 #define IPPROTO_OSPFIGP         89              /* OSPFIGP */
198 #define IPPROTO_CARP            112             /* CARP */
199 #ifndef _WIN32
200 #define IPPROTO_IPV4            IPPROTO_IPIP    /* for compatibility */
201 #endif
202
203 #define CARP_VERSION            2
204 #define CARP_ADVERTISEMENT      0x01
205
206 #define PRIV_NETINET_IPFW       491     /* Administer IPFW firewall. */
207
208 #define IP_FORWARDING           0x1             /* most of ip header exists */
209
210 #define NETISR_IP       2               /* same as AF_INET */
211
212 #define PRIV_NETINET_DUMMYNET   494     /* Administer DUMMYNET. */
213
214 extern int securelevel;
215
216 struct carp_header {
217 #if BYTE_ORDER == LITTLE_ENDIAN
218         u_int8_t        carp_type:4,
219                         carp_version:4;
220 #endif
221 #if BYTE_ORDER == BIG_ENDIAN
222         u_int8_t        carp_version:4,
223                         carp_type:4;
224 #endif
225 };
226
227 struct pim {
228         int dummy;      /* windows compiler does not like empty definition */
229 };
230
231 struct route {
232         struct  rtentry *ro_rt;
233         struct  sockaddr ro_dst;
234 };
235
236 struct ifaltq {
237         void *ifq_head;
238 };
239
240 /*
241  * ifnet->if_snd is used in ip_dummynet.c to take the transmission
242  * clock.
243  */
244 #if defined( __linux__)
245 #define if_xname        name
246 #define if_snd          XXX
247 #elif defined( _WIN32 )
248 /* used in ip_dummynet.c */
249 struct ifnet {
250         char    if_xname[IFNAMSIZ];     /* external name (name + unit) */
251 //        struct ifaltq if_snd;          /* output queue (includes altq) */
252 };
253
254 struct net_device {
255         char    if_xname[IFNAMSIZ];     /* external name (name + unit) */
256 };
257 #endif
258
259 /* involves mbufs */
260 int in_cksum(struct mbuf *m, int len);
261 #define divert_cookie(mtag) 0
262 #define divert_info(mtag) 0
263 #define INADDR_TO_IFP(a, b) b = NULL
264 #define pf_find_mtag(a) NULL
265 #define pf_get_mtag(a) NULL
266 #ifndef _WIN32
267 #define AF_LINK AF_ASH  /* ? our sys/socket.h */
268 #endif
269
270 struct pf_mtag {
271         void            *hdr;           /* saved hdr pos in mbuf, for ECN */
272         sa_family_t      af;            /* for ECN */
273         u_int32_t        qid;           /* queue id */
274 };
275
276 #if 0 // ndef radix
277 /* radix stuff in radix.h and radix.c */
278 struct radix_node {
279         caddr_t rn_key;         /* object of search */
280         caddr_t rn_mask;        /* netmask, if present */
281 };
282 #endif /* !radix */
283
284 /* missing kernel functions */
285 char *inet_ntoa(struct in_addr ina);
286 int random(void);
287
288 /*
289  * Return the risult of a/b
290  *
291  * this is used in linux kernel space,
292  * since the 64bit division needs to
293  * be done using a macro
294  */
295 int64_t
296 div64(int64_t a, int64_t b);
297
298 char *
299 inet_ntoa_r(struct in_addr ina, char *buf);
300
301 /* from bsd sys/queue.h */
302 #define TAILQ_FOREACH_SAFE(var, head, field, tvar)                      \
303         for ((var) = TAILQ_FIRST((head));                               \
304             (var) && ((tvar) = TAILQ_NEXT((var), field), 1);            \
305             (var) = (tvar))
306
307 #define SLIST_FOREACH_SAFE(var, head, field, tvar)                      \
308         for ((var) = SLIST_FIRST((head));                               \
309             (var) && ((tvar) = SLIST_NEXT((var), field), 1);            \
310             (var) = (tvar))
311
312 /* depending of linux version */
313 #ifndef ETHERTYPE_IPV6
314 #define ETHERTYPE_IPV6          0x86dd          /* IP protocol version 6 */
315 #endif
316
317 /*-------------------------------------------------*/
318 #define RT_NUMFIBS 1
319 extern u_int rt_numfibs;
320
321 /* involves kernel locking function */
322 #ifdef RTFREE
323 #undef RTFREE
324 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
325 #endif
326
327 void getmicrouptime(struct timeval *tv);
328
329 /* from sys/netinet/ip_output.c */
330 struct ip_moptions;
331 struct route;
332 struct ip;
333
334 struct mbuf *ip_reass(struct mbuf *);
335 u_short in_cksum_hdr(struct ip *);
336 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
337     struct ip_moptions *imo, struct inpcb *inp);
338
339 /* from net/netisr.c */
340 void netisr_dispatch(int num, struct mbuf *m);
341
342 /* definition moved in missing.c */
343 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
344
345 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
346
347 /* defined in session.c */
348 int priv_check(struct thread *td, int priv);
349
350 /* struct ucred is in linux/socket.h and has pid, uid, gid.
351  * We need a 'bsd_ucred' to store also the extra info
352  */
353
354 struct bsd_ucred {
355         uid_t           uid;
356         gid_t           gid;
357         uint32_t        xid;
358         uint32_t        nid;
359 };
360
361 int securelevel_ge(struct ucred *cr, int level);
362
363 struct sysctl_oid;
364 struct sysctl_req;
365
366 /*
367  * sysctl are mapped into /sys/module/ipfw_mod parameters
368  */
369 #define CTLFLAG_RD              1
370 #define CTLFLAG_RDTUN           1
371 #define CTLFLAG_RW              2
372 #define CTLFLAG_SECURE3         0 // unsupported
373 #define CTLFLAG_VNET    0       /* unsupported */
374
375 #ifdef _WIN32
376 #define module_param_named(_name, _var, _ty, _perm)
377 #else
378
379 /* Linux 2.4 is mostly for openwrt */
380 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
381 #include <linux/bitops.h>        /* generic_ffs() used in ip_fw2.c */
382 typedef uint32_t __be32;
383 typedef uint16_t __be16;
384 struct sock;
385 struct net;
386 struct inet_hashinfo;
387 struct sock *inet_lookup(
388         struct inet_hashinfo *hashinfo,
389         const __be32 saddr, const __be16 sport,
390         const __be32 daddr, const __be16 dport,
391         const int dif);
392 static int inet_iif(const struct sk_buff *skb);
393 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
394 #endif /* Linux < 2.6 */
395
396 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
397 #define module_param_named(_name, _var, _ty, _perm)     \
398         //module_param(_name, _ty, 0644)
399 #endif
400 #endif /* __linux__ */
401
402 #define SYSCTL_DECL(_1)
403 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
404 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
405 #define _SYSCTL_BASE(_name, _var, _ty, _perm)           \
406         module_param_named(_name, *(_var), _ty,         \
407                 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
408 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
409
410 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc)        \
411         _SYSCTL_BASE(_name, _var, int, _mode)
412
413 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc)       \
414         _SYSCTL_BASE(_name, _var, long, _mode)
415
416 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc)      \
417         _SYSCTL_BASE(_name, _var, ulong, _mode)
418
419 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc)       \
420          _SYSCTL_BASE(_name, _var, uint, _mode)
421
422 #define SYSCTL_HANDLER_ARGS             \
423         struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
424 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
425 int sysctl_handle_long(SYSCTL_HANDLER_ARGS); 
426
427 #define TUNABLE_INT(_name, _ptr)
428
429 void ether_demux(struct ifnet *ifp, struct mbuf *m);
430
431 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
432
433 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
434
435 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
436
437 void rtfree(struct rtentry *rt);
438
439 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
440
441 #ifdef INP_LOCK_ASSERT
442 #undef INP_LOCK_ASSERT
443 #define INP_LOCK_ASSERT(a)
444 #endif
445
446 int jailed(struct ucred *cred);
447
448 /*
449 * Return 1 if an internet address is for a ``local'' host
450 * (one to which we have a connection).  If subnetsarelocal
451 * is true, this includes other subnets of the local net.
452 * Otherwise, it includes only the directly-connected (sub)nets.
453 */
454 int in_localaddr(struct in_addr in);
455
456 /* the prototype is already in the headers */
457 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS); 
458
459 int fnmatch(const char *pattern, const char *string, int flags);
460
461 int
462 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
463         const __be32 daddr, const __be16 dport,
464         struct sk_buff *skb, int dir, struct bsd_ucred *u);
465
466 /* vnet wrappers, in vnet.h and ip_var.h */
467 int ipfw_init(void);
468 void ipfw_destroy(void);
469 struct ip_fw_args;
470 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
471
472 #define curvnet                 NULL
473 #define CURVNET_SET(_v)
474 #define CURVNET_RESTORE()
475 #define VNET_ASSERT(condition)
476
477 #define VNET_NAME(n)            n
478 #define VNET_DECLARE(t, n)      extern t n
479 #define VNET_DEFINE(t, n)       t n
480 #define _VNET_PTR(b, n)         &VNET_NAME(n)
481 /*
482  * Virtualized global variable accessor macros.
483  */
484 #define VNET_VNET_PTR(vnet, n)          (&(n))
485 #define VNET_VNET(vnet, n)              (n)
486
487 #define VNET_PTR(n)             (&(n))
488 #define VNET(n)                 (n)
489
490 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
491 typedef int ip_fw_ctl_t(struct sockopt *);
492 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
493
494 /* For kernel ipfw_ether and ipfw_bridge. */
495 struct ip_fw_args;
496 typedef int ip_fw_chk_t(struct ip_fw_args *args);
497 extern  ip_fw_chk_t     *ip_fw_chk_ptr;
498
499 #define V_ip_fw_chk_ptr         VNET(ip_fw_chk_ptr)
500 #define V_ip_fw_ctl_ptr         VNET(ip_fw_ctl_ptr)
501 #define V_tcbinfo               VNET(tcbinfo)
502 #define V_udbinfo               VNET(udbinfo)
503
504 #define SYSCTL_VNET_PROC        SYSCTL_PROC
505 #define SYSCTL_VNET_INT         SYSCTL_INT
506
507 #endif /* !_MISSING_H_ */