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