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
36 #include <sys/cdefs.h>
40 #ifndef DEFINE_SPINLOCK
41 #define DEFINE_SPINLOCK(x) FAST_MUTEX x
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)
49 #include <sys/socket.h> /* bsd-compat.c */
50 #include <netinet/in.h> /* bsd-compat.c */
51 #include <netinet/ip.h> /* local version */
55 #define MALLOC_DECLARE(x) /* nothing */
56 #include <linux/time.h> /* do_gettimeofday */
57 #include <netinet/ip.h> /* local version */
61 * Kernel locking support.
62 * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
64 * In linux we use spinlock_bh to implement both.
65 * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
68 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
69 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
72 #endif /* __linux__ */
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)
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)
89 /* end of locking support */
92 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
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)
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];
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)
112 #define __FreeBSD_version 500035
116 int my_mod_register(struct moduledata *mod, const char *name, int order);
118 /* define some macro for ip_dummynet */
123 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
124 struct malloc_type type[1]; void *md_dummy_ ## type = type
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)
135 * gettimeofday would be in sys/time.h but it is not
136 * visible if _KERNEL is defined
138 int gettimeofday(struct timeval *, struct timezone *);
141 #define MALLOC_DEFINE(a,b,c)
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;
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 */
155 /*-------------------------------------------------*/
157 /* define, includes and functions missing in linux */
158 /* include and define */
159 #include <arpa/inet.h> /* inet_ntoa */
163 /* used by ip_dummynet.c */
164 void reinject_drop(struct mbuf* m);
166 #include <linux/errno.h> /* error define */
167 #include <linux/if.h> /* IFNAMESIZ */
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
177 #define icmp_code code
178 #define icmp_type type
180 /* linux in6_addr has no member __u6_addr
181 * replace the whole structure ?
183 #define __u6_addr in6_u
184 #define __u6_addr32 u6_addr32
185 #endif /* __linux__ */
187 /* defined in linux/sctp.h with no bsd definition */
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... */
196 /* missing definition */
202 #define RTF_CLONING 0x100 /* generate new routes on use */
204 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
205 #define IPPROTO_CARP 112 /* CARP */
207 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
210 #define CARP_VERSION 2
211 #define CARP_ADVERTISEMENT 0x01
213 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
215 #define IP_FORWARDING 0x1 /* most of ip header exists */
217 #define NETISR_IP 2 /* same as AF_INET */
219 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
221 extern int securelevel;
224 #if BYTE_ORDER == LITTLE_ENDIAN
225 u_int8_t carp_type:4,
228 #if BYTE_ORDER == BIG_ENDIAN
229 u_int8_t carp_version:4,
235 int dummy; /* windows compiler does not like empty definition */
239 struct rtentry *ro_rt;
240 struct sockaddr ro_dst;
248 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
251 #if defined( __linux__)
252 #define if_xname name
254 #elif defined( _WIN32 )
255 /* used in ip_dummynet.c */
257 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
258 // struct ifaltq if_snd; /* output queue (includes altq) */
262 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
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))
278 #define AF_LINK AF_ASH /* ? our sys/socket.h */
282 void *hdr; /* saved hdr pos in mbuf, for ECN */
283 sa_family_t af; /* for ECN */
284 u_int32_t qid; /* queue id */
288 /* radix stuff in radix.h and radix.c */
290 caddr_t rn_key; /* object of search */
291 caddr_t rn_mask; /* netmask, if present */
295 /* missing kernel functions */
296 char *inet_ntoa(struct in_addr ina);
300 * Return the risult of a/b
302 * this is used in linux kernel space,
303 * since the 64bit division needs to
304 * be done using a macro
307 div64(int64_t a, int64_t b);
310 inet_ntoa_r(struct in_addr ina, char *buf);
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); \
318 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
319 for ((var) = SLIST_FIRST((head)); \
320 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
323 /* depending of linux version */
324 #ifndef ETHERTYPE_IPV6
325 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
328 /*-------------------------------------------------*/
330 extern u_int rt_numfibs;
332 /* involves kernel locking function */
335 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
338 void getmicrouptime(struct timeval *tv);
340 /* from sys/netinet/ip_output.c */
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);
350 /* from net/netisr.c */
351 void netisr_dispatch(int num, struct mbuf *m);
353 /* definition moved in missing.c */
354 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
356 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
358 /* defined in session.c */
359 int priv_check(struct thread *td, int priv);
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
372 int securelevel_ge(struct ucred *cr, int level);
378 * sysctl are mapped into /sys/module/ipfw_mod parameters
381 #define CTLFLAG_RDTUN 1
383 #define CTLFLAG_SECURE3 0 // unsupported
384 #define CTLFLAG_VNET 0 /* unsupported */
387 #define module_param_named(_name, _var, _ty, _perm)
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;
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,
403 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
404 #endif /* Linux < 2.6 */
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)
410 #endif /* __linux__ */
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)
420 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
421 _SYSCTL_BASE(_name, _var, int, _mode)
423 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
424 _SYSCTL_BASE(_name, _var, long, _mode)
426 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
427 _SYSCTL_BASE(_name, _var, ulong, _mode)
429 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
430 _SYSCTL_BASE(_name, _var, uint, _mode)
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);
437 #define TUNABLE_INT(_name, _ptr)
439 void ether_demux(struct ifnet *ifp, struct mbuf *m);
441 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
443 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
445 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
447 void rtfree(struct rtentry *rt);
449 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
451 #ifdef INP_LOCK_ASSERT
452 #undef INP_LOCK_ASSERT
453 #define INP_LOCK_ASSERT(a)
456 int jailed(struct ucred *cred);
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.
464 int in_localaddr(struct in_addr in);
466 /* the prototype is already in the headers */
467 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
469 int fnmatch(const char *pattern, const char *string, int flags);
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);
476 /* vnet wrappers, in vnet.h and ip_var.h */
478 void ipfw_destroy(void);
480 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
483 #define CURVNET_SET(_v)
484 #define CURVNET_RESTORE()
485 #define VNET_ASSERT(condition)
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)
492 * Virtualized global variable accessor macros.
494 #define VNET_VNET_PTR(vnet, n) (&(n))
495 #define VNET_VNET(vnet, n) (n)
497 #define VNET_PTR(n) (&(n))
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;
504 /* For kernel ipfw_ether and ipfw_bridge. */
506 typedef int ip_fw_chk_t(struct ip_fw_args *args);
507 extern ip_fw_chk_t *ip_fw_chk_ptr;
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)
514 #define SYSCTL_VNET_PROC SYSCTL_PROC
515 #define SYSCTL_VNET_INT SYSCTL_INT
517 int my_mod_register(struct moduledata *mod, const char *name, int order);
519 #endif /* !_MISSING_H_ */