2 * Copyright (C) 2009 Luigi Rizzo, Marta Carbone, Universita` di Pisa
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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
29 * Header for kernel variables and functions that are not available in
38 #ifndef DEFINE_SPINLOCK
39 #define DEFINE_SPINLOCK(x) FAST_MUTEX x
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)
47 #include <sys/socket.h> /* bsd-compat.c */
48 #include <netinet/in.h> /* bsd-compat.c */
49 #include <netinet/ip.h> /* local version */
53 #include <linux/time.h> /* do_gettimeofday */
54 #include <netinet/ip.h> /* local version */
58 * Kernel locking support.
59 * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
61 * In linux we use spinlock_bh to implement both.
62 * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
65 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
66 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
69 #endif /* __linux__ */
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 #define rw_init_flags(_l, s, v)
80 #define mtx_assert(a, b)
81 #define mtx_destroy(m)
82 #define mtx_init(m, a,b,c) spin_lock_init(m)
83 #define mtx_lock(_l) spin_lock_bh(_l)
84 #define mtx_unlock(_l) spin_unlock_bh(_l)
86 /* end of locking support */
89 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
91 /* bzero not present on linux, but this should go in glue.h */
92 #define bzero(s, n) memset(s, 0, n)
93 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
96 #define ETHERTYPE_IP 0x0800 /* IP protocol */
97 #define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
99 u_char ether_dhost[ETHER_ADDR_LEN];
100 u_char ether_shost[ETHER_ADDR_LEN];
104 #define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
105 #define ETHER_TYPE_LEN 2 /* length of the Ethernet type field */
106 #define ETHER_HDR_LEN (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
109 #define __FreeBSD_version 500035
113 int my_mod_register(struct moduledata *mod, const char *name, int order);
115 /* define some macro for ip_dummynet */
120 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
121 struct malloc_type type[1]; void *md_dummy_ ## type = type
125 #define log(_level, fmt, arg...) printk(KERN_ERR fmt, ##arg)
128 * gettimeofday would be in sys/time.h but it is not
129 * visible if _KERNEL is defined
131 int gettimeofday(struct timeval *, struct timezone *);
134 #define MALLOC_DEFINE(a,b,c)
138 extern long tick; /* exists in 2.4 but not in 2.6 */
139 extern int bootverbose;
140 extern time_t time_uptime;
141 extern struct timeval boottime;
143 extern int max_linkhdr;
144 extern int ip_defttl;
145 extern u_long in_ifaddrhmask; /* mask for hash table */
146 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
148 /*-------------------------------------------------*/
150 /* define, includes and functions missing in linux */
151 /* include and define */
152 #include <arpa/inet.h> /* inet_ntoa */
156 /* used by ip_dummynet.c */
157 void reinject_drop(struct mbuf* m);
159 #include <linux/errno.h> /* error define */
160 #include <linux/if.h> /* IFNAMESIZ */
163 * some network structure can be defined in the bsd way
164 * by using the _FAVOR_BSD definition. This is not true
165 * for icmp structure.
166 * XXX struct icmp contains bsd names in
167 * /usr/include/netinet/ip_icmp.h
170 #define icmp_code code
171 #define icmp_type type
173 /* linux in6_addr has no member __u6_addr
174 * replace the whole structure ?
176 #define __u6_addr in6_u
177 #define __u6_addr32 u6_addr32
178 #endif /* __linux__ */
180 /* defined in linux/sctp.h with no bsd definition */
182 uint16_t src_port; /* source port */
183 uint16_t dest_port; /* destination port */
184 uint32_t v_tag; /* verification tag of packet */
185 uint32_t checksum; /* Adler32 C-Sum */
186 /* chunks follow... */
189 /* missing definition */
195 #define RTF_CLONING 0x100 /* generate new routes on use */
197 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
198 #define IPPROTO_CARP 112 /* CARP */
200 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
203 #define CARP_VERSION 2
204 #define CARP_ADVERTISEMENT 0x01
206 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
208 #define IP_FORWARDING 0x1 /* most of ip header exists */
210 #define NETISR_IP 2 /* same as AF_INET */
212 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
214 extern int securelevel;
217 #if BYTE_ORDER == LITTLE_ENDIAN
218 u_int8_t carp_type:4,
221 #if BYTE_ORDER == BIG_ENDIAN
222 u_int8_t carp_version:4,
228 int dummy; /* windows compiler does not like empty definition */
232 struct rtentry *ro_rt;
233 struct sockaddr ro_dst;
241 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
244 #if defined( __linux__)
245 #define if_xname name
247 #elif defined( _WIN32 )
248 /* used in ip_dummynet.c */
250 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
251 // struct ifaltq if_snd; /* output queue (includes altq) */
255 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
260 int in_cksum(struct mbuf *m, int len);
261 #define divert_cookie(mtag) 0
262 #define divert_info(mtag) 0
263 #define INADDR_TO_IFP(a, b) b = NULL
264 #define pf_find_mtag(a) NULL
265 #define pf_get_mtag(a) NULL
267 #define AF_LINK AF_ASH /* ? our sys/socket.h */
271 void *hdr; /* saved hdr pos in mbuf, for ECN */
272 sa_family_t af; /* for ECN */
273 u_int32_t qid; /* queue id */
277 /* radix stuff in radix.h and radix.c */
279 caddr_t rn_key; /* object of search */
280 caddr_t rn_mask; /* netmask, if present */
284 /* missing kernel functions */
285 char *inet_ntoa(struct in_addr ina);
289 * Return the risult of a/b
291 * this is used in linux kernel space,
292 * since the 64bit division needs to
293 * be done using a macro
296 div64(int64_t a, int64_t b);
299 inet_ntoa_r(struct in_addr ina, char *buf);
301 /* from bsd sys/queue.h */
302 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
303 for ((var) = TAILQ_FIRST((head)); \
304 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
307 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
308 for ((var) = SLIST_FIRST((head)); \
309 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
312 /* depending of linux version */
313 #ifndef ETHERTYPE_IPV6
314 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
317 /*-------------------------------------------------*/
319 extern u_int rt_numfibs;
321 /* involves kernel locking function */
324 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
327 void getmicrouptime(struct timeval *tv);
329 /* from sys/netinet/ip_output.c */
334 struct mbuf *ip_reass(struct mbuf *);
335 u_short in_cksum_hdr(struct ip *);
336 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
337 struct ip_moptions *imo, struct inpcb *inp);
339 /* from net/netisr.c */
340 void netisr_dispatch(int num, struct mbuf *m);
342 /* definition moved in missing.c */
343 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
345 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
347 /* defined in session.c */
348 int priv_check(struct thread *td, int priv);
350 /* struct ucred is in linux/socket.h and has pid, uid, gid.
351 * We need a 'bsd_ucred' to store also the extra info
361 int securelevel_ge(struct ucred *cr, int level);
367 * sysctl are mapped into /sys/module/ipfw_mod parameters
370 #define CTLFLAG_RDTUN 1
372 #define CTLFLAG_SECURE3 0 // unsupported
373 #define CTLFLAG_VNET 0 /* unsupported */
376 #define module_param_named(_name, _var, _ty, _perm)
379 /* Linux 2.4 is mostly for openwrt */
380 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
381 #include <linux/bitops.h> /* generic_ffs() used in ip_fw2.c */
382 typedef uint32_t __be32;
383 typedef uint16_t __be16;
386 struct inet_hashinfo;
387 struct sock *inet_lookup(
388 struct inet_hashinfo *hashinfo,
389 const __be32 saddr, const __be16 sport,
390 const __be32 daddr, const __be16 dport,
392 static int inet_iif(const struct sk_buff *skb);
393 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
394 #endif /* Linux < 2.6 */
396 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
397 #define module_param_named(_name, _var, _ty, _perm) \
398 //module_param(_name, _ty, 0644)
400 #endif /* __linux__ */
402 #define SYSCTL_DECL(_1)
403 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
404 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
405 #define _SYSCTL_BASE(_name, _var, _ty, _perm) \
406 module_param_named(_name, *(_var), _ty, \
407 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
408 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
410 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
411 _SYSCTL_BASE(_name, _var, int, _mode)
413 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
414 _SYSCTL_BASE(_name, _var, long, _mode)
416 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
417 _SYSCTL_BASE(_name, _var, ulong, _mode)
419 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
420 _SYSCTL_BASE(_name, _var, uint, _mode)
422 #define SYSCTL_HANDLER_ARGS \
423 struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
424 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
425 int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
427 #define TUNABLE_INT(_name, _ptr)
429 void ether_demux(struct ifnet *ifp, struct mbuf *m);
431 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
433 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
435 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
437 void rtfree(struct rtentry *rt);
439 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
441 #ifdef INP_LOCK_ASSERT
442 #undef INP_LOCK_ASSERT
443 #define INP_LOCK_ASSERT(a)
446 int jailed(struct ucred *cred);
449 * Return 1 if an internet address is for a ``local'' host
450 * (one to which we have a connection). If subnetsarelocal
451 * is true, this includes other subnets of the local net.
452 * Otherwise, it includes only the directly-connected (sub)nets.
454 int in_localaddr(struct in_addr in);
456 /* the prototype is already in the headers */
457 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
459 int fnmatch(const char *pattern, const char *string, int flags);
462 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
463 const __be32 daddr, const __be16 dport,
464 struct sk_buff *skb, int dir, struct bsd_ucred *u);
466 /* vnet wrappers, in vnet.h and ip_var.h */
468 void ipfw_destroy(void);
470 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
473 #define CURVNET_SET(_v)
474 #define CURVNET_RESTORE()
475 #define VNET_ASSERT(condition)
477 #define VNET_NAME(n) n
478 #define VNET_DECLARE(t, n) extern t n
479 #define VNET_DEFINE(t, n) t n
480 #define _VNET_PTR(b, n) &VNET_NAME(n)
482 * Virtualized global variable accessor macros.
484 #define VNET_VNET_PTR(vnet, n) (&(n))
485 #define VNET_VNET(vnet, n) (n)
487 #define VNET_PTR(n) (&(n))
490 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
491 typedef int ip_fw_ctl_t(struct sockopt *);
492 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
494 /* For kernel ipfw_ether and ipfw_bridge. */
496 typedef int ip_fw_chk_t(struct ip_fw_args *args);
497 extern ip_fw_chk_t *ip_fw_chk_ptr;
499 #define V_ip_fw_chk_ptr VNET(ip_fw_chk_ptr)
500 #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr)
501 #define V_tcbinfo VNET(tcbinfo)
502 #define V_udbinfo VNET(udbinfo)
504 #define SYSCTL_VNET_PROC SYSCTL_PROC
505 #define SYSCTL_VNET_INT SYSCTL_INT
507 #endif /* !_MISSING_H_ */