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