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
27 * $Id: missing.h 5817 2010-03-23 09:50:56Z svn_panicucci $
29 * Header for kernel variables and functions that are not available in
36 #include <sys/cdefs.h>
38 /* portability features, to be set before the rest: */
39 #define HAVE_NET_IPLEN /* iplen/ipoff in net format */
40 #define WITHOUT_BPF /* do not use bpf logging */
44 #ifndef DEFINE_SPINLOCK
45 #define DEFINE_SPINLOCK(x) FAST_MUTEX x
47 /* spinlock --> Guarded Mutex KGUARDED_MUTEX */
48 /* http://www.reactos.org/wiki/index.php/Guarded_Mutex */
49 #define spin_lock_init(_l)
50 #define spin_lock_bh(_l)
51 #define spin_unlock_bh(_l)
53 #include <sys/socket.h> /* bsd-compat.c */
54 #include <netinet/in.h> /* bsd-compat.c */
55 #include <netinet/ip.h> /* local version */
59 #define MALLOC_DECLARE(x) /* nothing */
60 #include <linux/time.h> /* do_gettimeofday */
61 #include <netinet/ip.h> /* local version */
65 * Kernel locking support.
66 * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
68 * In linux we use spinlock_bh to implement both.
69 * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
72 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
73 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
77 #define rw_assert(a, b)
78 #define rw_destroy(_l)
79 #define rw_init(_l, msg) spin_lock_init(_l)
80 #define rw_rlock(_l) spin_lock_bh(_l)
81 #define rw_runlock(_l) spin_unlock_bh(_l)
82 #define rw_wlock(_l) spin_lock_bh(_l)
83 #define rw_wunlock(_l) spin_unlock_bh(_l)
84 #define rw_init_flags(_l, s, v)
86 #define mtx_assert(a, b)
87 #define mtx_destroy(m)
88 #define mtx_init(m, a,b,c) spin_lock_init(m)
89 #define mtx_lock(_l) spin_lock_bh(_l)
90 #define mtx_unlock(_l) spin_unlock_bh(_l)
92 #endif /* __linux__ */
93 /* end of locking support */
96 * Reference to an ipfw rule that can be carried outside critical sections.
97 * A rule is identified by rulenum:rule_id which is ordered.
98 * In version chain_id the rule can be found in slot 'slot', so
99 * we don't need a lookup if chain_id == chain->id.
101 * On exit from the firewall this structure refers to the rule after
102 * the matching one (slot points to the new rule; rulenum:rule_id-1
103 * is the matching rule), and additional info (e.g. info often contains
104 * the insn argument or tablearg in the low 16 bits, in host format).
105 * On entry, the structure is valid if slot>0, and refers to the starting
106 * rules. 'info' contains the reason for reinject, e.g. divert port,
107 * divert direction, and so on.
109 struct ipfw_rule_ref {
110 uint32_t slot; /* slot for matching rule */
111 uint32_t rulenum; /* matching rule number */
112 uint32_t rule_id; /* matching rule id */
113 uint32_t chain_id; /* ruleset id */
114 uint32_t info; /* see below */
118 IPFW_INFO_MASK = 0x0000ffff,
119 IPFW_INFO_OUT = 0x00000000, /* outgoing, just for convenience */
120 IPFW_INFO_IN = 0x80000000, /* incoming, overloads dir */
121 IPFW_ONEPASS = 0x40000000, /* One-pass, do not reinject */
122 IPFW_IS_MASK = 0x30000000, /* which source ? */
123 IPFW_IS_DIVERT = 0x20000000,
124 IPFW_IS_DUMMYNET =0x10000000,
125 IPFW_IS_PIPE = 0x08000000, /* pipe=1, queue = 0 */
128 /* in netinet/in.h */
129 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
131 /* bzero not present on linux, but this should go in glue.h */
132 #define bzero(s, n) memset(s, 0, n)
133 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
136 #define ETHERTYPE_IP 0x0800 /* IP protocol */
137 //#define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
138 struct ether_header {
139 u_char ether_dhost[ETHER_ADDR_LEN];
140 u_char ether_shost[ETHER_ADDR_LEN];
144 #define ETHER_TYPE_LEN 2 /* length of the Ethernet type field */
145 #define ETHER_HDR_LEN (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
148 * Historically, BSD keeps ip_len and ip_off in host format
149 * when doing layer 3 processing, and this often requires
150 * to translate the format back and forth.
151 * To make the process explicit, we define a couple of macros
152 * that also take into account the fact that at some point
153 * we may want to keep those fields always in net format.
156 #if (BYTE_ORDER == BIG_ENDIAN) || defined(HAVE_NET_IPLEN)
157 #define SET_NET_IPLEN(p) do {} while (0)
158 #define SET_HOST_IPLEN(p) do {} while (0)
159 #else /* never on linux */
160 #define SET_NET_IPLEN(p) do { \
161 struct ip *h_ip = (p); \
162 h_ip->ip_len = htons(h_ip->ip_len); \
163 h_ip->ip_off = htons(h_ip->ip_off); \
166 #define SET_HOST_IPLEN(p) do { \
167 struct ip *h_ip = (p); \
168 h_ip->ip_len = ntohs(h_ip->ip_len); \
169 h_ip->ip_off = ntohs(h_ip->ip_off); \
171 #endif /* !HAVE_NET_IPLEN */
174 #define __FreeBSD_version 500035
178 int my_mod_register(const char *name,
179 int order, struct moduledata *mod, void *init, void *uninit);
181 /* define some macro for ip_dummynet */
186 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
187 struct malloc_type type[1]; void *md_dummy_ ## type = type
191 /* log... does not use the first argument */
192 #define LOG_ERR 0x100
193 #define LOG_INFO 0x200
194 #define log(_level, fmt, arg...) do { \
195 int __unused _qwerty=_level; printk(KERN_ERR fmt, ##arg); } while (0)
198 * gettimeofday would be in sys/time.h but it is not
199 * visible if _KERNEL is defined
201 int gettimeofday(struct timeval *, struct timezone *);
204 #define MALLOC_DEFINE(a,b,c)
208 extern long tick; /* exists in 2.4 but not in 2.6 */
209 extern int bootverbose;
210 extern struct timeval boottime;
212 /* The time_uptime a FreeBSD variable increased each second */
214 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,4,37) /* revise boundaries */
215 #define time_uptime get_seconds()
217 #define time_uptime CURRENT_TIME
220 #define time_uptime time_uptime_w32()
223 extern int max_linkhdr;
224 extern int ip_defttl;
225 extern u_long in_ifaddrhmask; /* mask for hash table */
226 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
228 /*-------------------------------------------------*/
230 /* define, includes and functions missing in linux */
231 /* include and define */
232 #include <arpa/inet.h> /* inet_ntoa */
236 /* used by ip_dummynet.c */
237 void reinject_drop(struct mbuf* m);
239 #include <linux/errno.h> /* error define */
240 #include <linux/if.h> /* IFNAMESIZ */
244 * some network structure can be defined in the bsd way
245 * by using the _FAVOR_BSD definition. This is not true
246 * for icmp structure.
247 * XXX struct icmp contains bsd names in
248 * /usr/include/netinet/ip_icmp.h
251 #define icmp_code code
252 #define icmp_type type
254 /* linux in6_addr has no member __u6_addr
255 * replace the whole structure ?
257 #define __u6_addr in6_u
258 #define __u6_addr32 u6_addr32
259 #endif /* __linux__ */
261 /* defined in linux/sctp.h with no bsd definition */
263 uint16_t src_port; /* source port */
264 uint16_t dest_port; /* destination port */
265 uint32_t v_tag; /* verification tag of packet */
266 uint32_t checksum; /* Adler32 C-Sum */
267 /* chunks follow... */
270 /* missing definition */
276 #define RTF_CLONING 0x100 /* generate new routes on use */
278 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
279 #define IPPROTO_CARP 112 /* CARP */
281 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
284 #define CARP_VERSION 2
285 #define CARP_ADVERTISEMENT 0x01
287 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
289 #define IP_FORWARDING 0x1 /* most of ip header exists */
291 #define NETISR_IP 2 /* same as AF_INET */
293 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
295 extern int securelevel;
298 #if BYTE_ORDER == LITTLE_ENDIAN
299 u_int8_t carp_type:4,
302 #if BYTE_ORDER == BIG_ENDIAN
303 u_int8_t carp_version:4,
309 int dummy; /* windows compiler does not like empty definition */
314 struct rtentry *ro_rt;
315 struct sockaddr ro_dst;
324 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
327 #if defined( __linux__)
328 #define if_xname name
330 #elif defined( _WIN32 )
331 /* used in ip_dummynet.c */
333 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
334 // struct ifaltq if_snd; /* output queue (includes altq) */
338 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
343 int in_cksum(struct mbuf *m, int len);
344 #define divert_cookie(mtag) 0
345 #define divert_info(mtag) 0
346 #define INADDR_TO_IFP(a, b) b = NULL
347 #define pf_find_mtag(a) NULL
348 #define pf_get_mtag(a) NULL
350 #define AF_LINK AF_ASH /* ? our sys/socket.h */
353 /* we don't pullup, either success or free and fail */
354 #define m_pullup(m, x) \
355 ((m)->m_len >= x ? (m) : (FREE_PKT(m), NULL))
358 void *hdr; /* saved hdr pos in mbuf, for ECN */
359 sa_family_t af; /* for ECN */
360 u_int32_t qid; /* queue id */
364 /* radix stuff in radix.h and radix.c */
366 caddr_t rn_key; /* object of search */
367 caddr_t rn_mask; /* netmask, if present */
371 /* missing kernel functions */
372 char *inet_ntoa(struct in_addr ina);
376 * Return the risult of a/b
378 * this is used in linux kernel space,
379 * since the 64bit division needs to
380 * be done using a macro
383 div64(int64_t a, int64_t b);
386 inet_ntoa_r(struct in_addr ina, char *buf);
388 /* from bsd sys/queue.h */
389 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
390 for ((var) = TAILQ_FIRST((head)); \
391 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
394 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
395 for ((var) = SLIST_FIRST((head)); \
396 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
399 /* depending of linux version */
400 #ifndef ETHERTYPE_IPV6
401 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
404 /*-------------------------------------------------*/
406 extern u_int rt_numfibs;
408 /* involves kernel locking function */
411 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
414 void getmicrouptime(struct timeval *tv);
416 /* from sys/netinet/ip_output.c */
421 struct mbuf *ip_reass(struct mbuf *);
422 u_short in_cksum_hdr(struct ip *);
423 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
424 struct ip_moptions *imo, struct inpcb *inp);
426 /* from net/netisr.c */
427 void netisr_dispatch(int num, struct mbuf *m);
429 /* definition moved in missing.c */
430 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
432 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
434 /* defined in session.c */
435 int priv_check(struct thread *td, int priv);
437 /* struct ucred is in linux/socket.h and has pid, uid, gid.
438 * We need a 'bsd_ucred' to store also the extra info
449 cred_check(void *insn, int proto, struct ifnet *oif,
450 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip,
451 u_int16_t src_port, struct bsd_ucred *u, int *ugid_lookupp,
452 struct sk_buff *skb);
454 int securelevel_ge(struct ucred *cr, int level);
460 #define module_param_named(_name, _var, _ty, _perm)
463 /* Linux 2.4 is mostly for openwrt */
464 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
465 #include <linux/bitops.h> /* generic_ffs() used in ip_fw2.c */
466 typedef uint32_t __be32;
467 typedef uint16_t __be16;
470 struct inet_hashinfo;
471 struct sock *inet_lookup(
472 struct inet_hashinfo *hashinfo,
473 const __be32 saddr, const __be16 sport,
474 const __be32 daddr, const __be16 dport,
476 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
477 #endif /* Linux < 2.6 */
479 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
480 #define module_param_named(_name, _var, _ty, _perm) \
481 //module_param(_name, _ty, 0644)
484 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
485 typedef unsigned long uintptr_t;
488 static inline unsigned long __fls (unsigned long word)
493 static inline unsigned long __fls(unsigned long word)
502 #endif /* LINUX < 2.6.25 */
504 #endif /* !_WIN32 so maybe __linux__ */
506 #if defined (__linux__) && !defined (EMULATE_SYSCTL)
507 #define SYSCTL_DECL(_1)
508 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
509 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
510 #define _SYSCTL_BASE(_name, _var, _ty, _perm) \
511 module_param_named(_name, *(_var), _ty, \
512 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
513 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
515 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
516 _SYSCTL_BASE(_name, _var, int, _mode)
518 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
519 _SYSCTL_BASE(_name, _var, long, _mode)
521 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
522 _SYSCTL_BASE(_name, _var, ulong, _mode)
524 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
525 _SYSCTL_BASE(_name, _var, uint, _mode)
527 #define TUNABLE_INT(_name, _ptr)
529 #define SYSCTL_VNET_PROC SYSCTL_PROC
530 #define SYSCTL_VNET_INT SYSCTL_INT
534 #define SYSCTL_HANDLER_ARGS \
535 struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
536 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
537 int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
540 void ether_demux(struct ifnet *ifp, struct mbuf *m);
542 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
544 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
546 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
548 void rtfree(struct rtentry *rt);
550 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
552 #ifdef INP_LOCK_ASSERT
553 #undef INP_LOCK_ASSERT
554 #define INP_LOCK_ASSERT(a)
557 int jailed(struct ucred *cred);
560 * Return 1 if an internet address is for a ``local'' host
561 * (one to which we have a connection). If subnetsarelocal
562 * is true, this includes other subnets of the local net.
563 * Otherwise, it includes only the directly-connected (sub)nets.
565 int in_localaddr(struct in_addr in);
567 /* the prototype is already in the headers */
568 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
570 int fnmatch(const char *pattern, const char *string, int flags);
573 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
574 const __be32 daddr, const __be16 dport,
575 struct sk_buff *skb, int dir, struct bsd_ucred *u);
577 /* vnet wrappers, in vnet.h and ip_var.h */
578 //int ipfw_init(void);
579 //void ipfw_destroy(void);
581 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
582 #define MTAG_IPFW_RULE 1262273568 /* rule reference */
585 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
588 #define CURVNET_SET(_v)
589 #define CURVNET_RESTORE()
590 #define VNET_ASSERT(condition)
592 #define VNET_NAME(n) n
593 #define VNET_DECLARE(t, n) extern t n
594 #define VNET_DEFINE(t, n) t n
595 #define _VNET_PTR(b, n) &VNET_NAME(n)
597 * Virtualized global variable accessor macros.
599 #define VNET_VNET_PTR(vnet, n) (&(n))
600 #define VNET_VNET(vnet, n) (n)
602 #define VNET_PTR(n) (&(n))
605 int ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp,
606 int dir, struct inpcb *inp);
608 /* hooks for divert */
609 extern void (*ip_divert_ptr)(struct mbuf *m, int incoming);
611 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
612 typedef int ip_fw_ctl_t(struct sockopt *);
613 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
615 /* netgraph prototypes */
616 typedef int ng_ipfw_input_t(struct mbuf **, int, struct ip_fw_args *, int);
617 extern ng_ipfw_input_t *ng_ipfw_input_p;
619 /* For kernel ipfw_ether and ipfw_bridge. */
621 typedef int ip_fw_chk_t(struct ip_fw_args *args);
622 extern ip_fw_chk_t *ip_fw_chk_ptr;
624 #define V_ip_fw_chk_ptr VNET(ip_fw_chk_ptr)
625 #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr)
626 #define V_tcbinfo VNET(tcbinfo)
627 #define V_udbinfo VNET(udbinfo)
629 #endif /* !_MISSING_H_ */