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