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