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 11275 2012-06-10 17:27:40Z marta $
29 * Header for kernel variables and functions that are not available in
36 /* sysctl.h and module.h are included before cdefs.h
37 * because of cdefs.h defines __unused */
39 #include <linux/sysctl.h>
40 #include <linux/module.h>
41 #include <sys/cdefs.h>
42 #include <linux/moduleparam.h>
44 /* portability features, to be set before the rest: */
45 #define HAVE_NET_IPLEN /* iplen/ipoff in net format */
46 #define WITHOUT_BPF /* do not use bpf logging */
50 #ifndef DEFINE_SPINLOCK
51 #define DEFINE_SPINLOCK(x) FAST_MUTEX x
53 /* spinlock --> Guarded Mutex KGUARDED_MUTEX */
54 /* http://www.reactos.org/wiki/index.php/Guarded_Mutex */
55 #define spin_lock_init(_l)
56 #define spin_lock_bh(_l)
57 #define spin_unlock_bh(_l)
59 #include <sys/socket.h> /* bsd-compat.c */
60 #include <netinet/in.h> /* bsd-compat.c */
61 #include <netinet/ip.h> /* local version */
62 #define INADDR_TO_IFP(a, b) b = NULL
66 #define MALLOC_DECLARE(x) /* nothing */
67 #include <linux/time.h> /* do_gettimeofday */
68 #include <netinet/ip.h> /* local version */
72 * Kernel locking support.
73 * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
75 * In linux we use spinlock_bh to implement both.
76 * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
79 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
80 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
84 #define rw_assert(a, b)
85 #define rw_destroy(_l)
86 #define rw_init(_l, msg) spin_lock_init(_l)
87 #define rw_rlock(_l) spin_lock_bh(_l)
88 #define rw_runlock(_l) spin_unlock_bh(_l)
89 #define rw_wlock(_l) spin_lock_bh(_l)
90 #define rw_wunlock(_l) spin_unlock_bh(_l)
91 #define rw_init_flags(_l, s, v)
93 #define mtx_assert(a, b)
94 #define mtx_destroy(m)
95 #define mtx_init(m, a,b,c) spin_lock_init(m)
96 #define mtx_lock(_l) spin_lock_bh(_l)
97 #define mtx_unlock(_l) spin_unlock_bh(_l)
99 #endif /* __linux__ */
100 /* end of locking support */
103 * Reference to an ipfw rule that can be carried outside critical sections.
104 * A rule is identified by rulenum:rule_id which is ordered.
105 * In version chain_id the rule can be found in slot 'slot', so
106 * we don't need a lookup if chain_id == chain->id.
108 * On exit from the firewall this structure refers to the rule after
109 * the matching one (slot points to the new rule; rulenum:rule_id-1
110 * is the matching rule), and additional info (e.g. info often contains
111 * the insn argument or tablearg in the low 16 bits, in host format).
112 * On entry, the structure is valid if slot>0, and refers to the starting
113 * rules. 'info' contains the reason for reinject, e.g. divert port,
114 * divert direction, and so on.
116 struct ipfw_rule_ref {
117 uint32_t slot; /* slot for matching rule */
118 uint32_t rulenum; /* matching rule number */
119 uint32_t rule_id; /* matching rule id */
120 uint32_t chain_id; /* ruleset id */
121 uint32_t info; /* see below */
125 IPFW_INFO_MASK = 0x0000ffff,
126 IPFW_INFO_OUT = 0x00000000, /* outgoing, just for convenience */
127 IPFW_INFO_IN = 0x80000000, /* incoming, overloads dir */
128 IPFW_ONEPASS = 0x40000000, /* One-pass, do not reinject */
129 IPFW_IS_MASK = 0x30000000, /* which source ? */
130 IPFW_IS_DIVERT = 0x20000000,
131 IPFW_IS_DUMMYNET =0x10000000,
132 IPFW_IS_PIPE = 0x08000000, /* pipe=1, queue = 0 */
135 /* in netinet/in.h */
136 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
138 /* bzero not present on linux, but this should go in glue.h */
139 #define bzero(s, n) memset(s, 0, n)
140 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
143 #define ETHERTYPE_IP 0x0800 /* IP protocol */
144 //#define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
145 struct ether_header {
146 u_char ether_dhost[ETHER_ADDR_LEN];
147 u_char ether_shost[ETHER_ADDR_LEN];
151 #define ETHER_TYPE_LEN 2 /* length of the Ethernet type field */
152 #define ETHER_HDR_LEN (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
155 * Historically, BSD keeps ip_len and ip_off in host format
156 * when doing layer 3 processing, and this often requires
157 * to translate the format back and forth.
158 * To make the process explicit, we define a couple of macros
159 * that also take into account the fact that at some point
160 * we may want to keep those fields always in net format.
163 #if (BYTE_ORDER == BIG_ENDIAN) || defined(HAVE_NET_IPLEN)
164 #define SET_NET_IPLEN(p) do {} while (0)
165 #define SET_HOST_IPLEN(p) do {} while (0)
166 #else /* never on linux */
167 #define SET_NET_IPLEN(p) do { \
168 struct ip *h_ip = (p); \
169 h_ip->ip_len = htons(h_ip->ip_len); \
170 h_ip->ip_off = htons(h_ip->ip_off); \
173 #define SET_HOST_IPLEN(p) do { \
174 struct ip *h_ip = (p); \
175 h_ip->ip_len = ntohs(h_ip->ip_len); \
176 h_ip->ip_off = ntohs(h_ip->ip_off); \
178 #endif /* !HAVE_NET_IPLEN */
181 #define __FreeBSD_version 500035
185 int my_mod_register(const char *name,
186 int order, struct moduledata *mod, void *init, void *uninit);
188 /* define some macro for ip_dummynet */
193 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
194 struct malloc_type type[1]; void *md_dummy_ ## type = type
198 /* log... does not use the first argument */
199 #define LOG_ERR 0x100
200 #define LOG_INFO 0x200
201 #define log(_level, fmt, arg...) do { \
202 int __unused _qwerty=_level; printk(KERN_ERR fmt, ##arg); } while (0)
205 * gettimeofday would be in sys/time.h but it is not
206 * visible if _KERNEL is defined
208 int gettimeofday(struct timeval *, struct timezone *);
211 #define MALLOC_DEFINE(a,b,c)
215 extern long tick; /* exists in 2.4 but not in 2.6 */
216 extern int bootverbose;
217 extern struct timeval boottime;
219 /* The time_uptime a FreeBSD variable increased each second */
221 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,37) /* revise boundaries */
222 #define time_uptime get_seconds()
224 #define time_uptime CURRENT_TIME
227 #define time_uptime time_uptime_w32()
230 extern int max_linkhdr;
231 extern int ip_defttl;
232 extern u_long in_ifaddrhmask; /* mask for hash table */
233 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
235 /*-------------------------------------------------*/
237 /* define, includes and functions missing in linux */
238 /* include and define */
239 #include <arpa/inet.h> /* inet_ntoa */
243 /* used by ip_dummynet.c */
244 void reinject_drop(struct mbuf* m);
246 #include <linux/errno.h> /* error define */
247 #include <linux/if.h> /* IFNAMESIZ */
251 * some network structure can be defined in the bsd way
252 * by using the _FAVOR_BSD definition. This is not true
253 * for icmp structure.
254 * XXX struct icmp contains bsd names in
255 * /usr/include/netinet/ip_icmp.h
258 #define icmp_code code
259 #define icmp_type type
261 /* linux in6_addr has no member __u6_addr
262 * replace the whole structure ?
264 #define __u6_addr in6_u
265 #define __u6_addr32 u6_addr32
266 #endif /* __linux__ */
268 /* defined in linux/sctp.h with no bsd definition */
270 uint16_t src_port; /* source port */
271 uint16_t dest_port; /* destination port */
272 uint32_t v_tag; /* verification tag of packet */
273 uint32_t checksum; /* Adler32 C-Sum */
274 /* chunks follow... */
277 /* missing definition */
283 #define RTF_CLONING 0x100 /* generate new routes on use */
285 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
286 #define IPPROTO_CARP 112 /* CARP */
288 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
291 #define CARP_VERSION 2
292 #define CARP_ADVERTISEMENT 0x01
294 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
296 #define IP_FORWARDING 0x1 /* most of ip header exists */
298 #define NETISR_IP 2 /* same as AF_INET */
300 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
302 extern int securelevel;
305 #if BYTE_ORDER == LITTLE_ENDIAN
306 u_int8_t carp_type:4,
309 #if BYTE_ORDER == BIG_ENDIAN
310 u_int8_t carp_version:4,
316 int dummy; /* windows compiler does not like empty definition */
321 struct rtentry *ro_rt;
322 struct sockaddr ro_dst;
331 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
334 #if defined( __linux__)
335 #define if_xname name
337 #elif defined( _WIN32 )
338 /* used in ip_dummynet.c */
340 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
341 // struct ifaltq if_snd; /* output queue (includes altq) */
345 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
350 int in_cksum(struct mbuf *m, int len);
351 #define divert_cookie(mtag) 0
352 #define divert_info(mtag) 0
353 #define pf_find_mtag(a) NULL
354 #define pf_get_mtag(a) NULL
356 #define AF_LINK AF_ASH /* ? our sys/socket.h */
359 /* search local the ip addresses, used for the "me" keyword */
360 #include <linux/inetdevice.h>
361 #define INADDR_TO_IFP(ip, b) \
362 b = ip_dev_find((struct net *)&init_net, ip.s_addr)
364 /* we don't pullup, either success or free and fail */
365 #define m_pullup(m, x) \
366 ((m)->m_len >= x ? (m) : (FREE_PKT(m), NULL))
369 void *hdr; /* saved hdr pos in mbuf, for ECN */
370 sa_family_t af; /* for ECN */
371 u_int32_t qid; /* queue id */
375 /* radix stuff in radix.h and radix.c */
377 caddr_t rn_key; /* object of search */
378 caddr_t rn_mask; /* netmask, if present */
382 /* missing kernel functions */
383 char *inet_ntoa(struct in_addr ina);
387 * Return the risult of a/b
389 * this is used in linux kernel space,
390 * since the 64bit division needs to
391 * be done using a macro
394 div64(int64_t a, int64_t b);
397 inet_ntoa_r(struct in_addr ina, char *buf);
399 /* from bsd sys/queue.h */
400 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
401 for ((var) = TAILQ_FIRST((head)); \
402 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
405 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
406 for ((var) = SLIST_FIRST((head)); \
407 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
410 /* depending of linux version */
411 #ifndef ETHERTYPE_IPV6
412 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
415 /*-------------------------------------------------*/
417 extern u_int rt_numfibs;
419 /* involves kernel locking function */
422 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
425 void getmicrouptime(struct timeval *tv);
427 /* from sys/netinet/ip_output.c */
432 struct mbuf *ip_reass(struct mbuf *);
433 u_short in_cksum_hdr(struct ip *);
434 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
435 struct ip_moptions *imo, struct inpcb *inp);
437 /* from net/netisr.c */
438 void netisr_dispatch(int num, struct mbuf *m);
440 /* definition moved in missing.c */
441 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
443 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
445 /* defined in session.c */
446 int priv_check(struct thread *td, int priv);
448 /* struct ucred is in linux/socket.h and has pid, uid, gid.
449 * We need a 'bsd_ucred' to store also the extra info
460 cred_check(void *insn, int proto, struct ifnet *oif,
461 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip,
462 u_int16_t src_port, struct bsd_ucred *u, int *ugid_lookupp,
463 struct sk_buff *skb);
465 int securelevel_ge(struct ucred *cr, int level);
471 #define module_param_named(_name, _var, _ty, _perm)
474 /* Linux 2.4 is mostly for openwrt */
475 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
476 #include <linux/bitops.h> /* generic_ffs() used in ip_fw2.c */
477 typedef uint32_t __be32;
478 typedef uint16_t __be16;
481 struct inet_hashinfo;
482 struct sock *inet_lookup(
483 struct inet_hashinfo *hashinfo,
484 const __be32 saddr, const __be16 sport,
485 const __be32 daddr, const __be16 dport,
487 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
488 #endif /* Linux < 2.6 */
490 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) && \
491 LINUX_VERSION_CODE > KERNEL_VERSION(2,6,16) /* XXX NOT sure, in 2.6.9 give an error */
492 #define module_param_named(_name, _var, _ty, _perm) \
493 //module_param(_name, _ty, 0644)
496 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
497 typedef unsigned long uintptr_t;
500 static inline unsigned long __fls(unsigned long word)
509 #endif /* LINUX < 2.6.25 */
511 #endif /* !_WIN32 so maybe __linux__ */
513 #if defined (__linux__) && !defined (EMULATE_SYSCTL)
514 #define SYSCTL_DECL(_1)
515 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
516 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
517 #define _SYSCTL_BASE(_name, _var, _ty, _perm) \
518 module_param_named(_name, *(_var), _ty, \
519 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
520 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
522 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
523 _SYSCTL_BASE(_name, _var, int, _mode)
525 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
526 _SYSCTL_BASE(_name, _var, long, _mode)
528 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
529 _SYSCTL_BASE(_name, _var, ulong, _mode)
531 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
532 _SYSCTL_BASE(_name, _var, uint, _mode)
534 #define TUNABLE_INT(_name, _ptr)
536 #define SYSCTL_VNET_PROC SYSCTL_PROC
537 #define SYSCTL_VNET_INT SYSCTL_INT
541 #define SYSCTL_HANDLER_ARGS \
542 struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
543 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
544 int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
547 void ether_demux(struct ifnet *ifp, struct mbuf *m);
549 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
551 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
553 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
555 void rtfree(struct rtentry *rt);
557 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
559 #ifdef INP_LOCK_ASSERT
560 #undef INP_LOCK_ASSERT
561 #define INP_LOCK_ASSERT(a)
564 int jailed(struct ucred *cred);
567 * Return 1 if an internet address is for a ``local'' host
568 * (one to which we have a connection). If subnetsarelocal
569 * is true, this includes other subnets of the local net.
570 * Otherwise, it includes only the directly-connected (sub)nets.
572 int in_localaddr(struct in_addr in);
574 /* the prototype is already in the headers */
575 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
577 int fnmatch(const char *pattern, const char *string, int flags);
580 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
581 const __be32 daddr, const __be16 dport,
582 struct sk_buff *skb, int dir, struct bsd_ucred *u);
584 /* vnet wrappers, in vnet.h and ip_var.h */
585 //int ipfw_init(void);
586 //void ipfw_destroy(void);
588 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
589 #define MTAG_IPFW_RULE 1262273568 /* rule reference */
592 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
595 #define CURVNET_SET(_v)
596 #define CURVNET_RESTORE()
597 #define VNET_ASSERT(condition)
599 #define VNET_NAME(n) n
600 #define VNET_DECLARE(t, n) extern t n
601 #define VNET_DEFINE(t, n) t n
602 #define _VNET_PTR(b, n) &VNET_NAME(n)
604 * Virtualized global variable accessor macros.
606 #define VNET_VNET_PTR(vnet, n) (&(n))
607 #define VNET_VNET(vnet, n) (n)
609 #define VNET_PTR(n) (&(n))
612 VNET_DECLARE(int, ip_defttl);
613 #define V_ip_defttl VNET(ip_defttl);
615 int ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp,
616 int dir, struct inpcb *inp);
618 /* hooks for divert */
619 extern void (*ip_divert_ptr)(struct mbuf *m, int incoming);
621 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
622 typedef int ip_fw_ctl_t(struct sockopt *);
623 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
625 /* netgraph prototypes */
626 typedef int ng_ipfw_input_t(struct mbuf **, int, struct ip_fw_args *, int);
627 extern ng_ipfw_input_t *ng_ipfw_input_p;
629 /* For kernel ipfw_ether and ipfw_bridge. */
631 typedef int ip_fw_chk_t(struct ip_fw_args *args);
632 extern ip_fw_chk_t *ip_fw_chk_ptr;
634 #define V_ip_fw_chk_ptr VNET(ip_fw_chk_ptr)
635 #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr)
636 #define V_tcbinfo VNET(tcbinfo)
637 #define V_udbinfo VNET(udbinfo)
639 #endif /* !_MISSING_H_ */