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