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 12256 2013-04-26 21:12:44Z luigi $
29 * Header for kernel variables and functions that are not available in
36 #include <sys/cdefs.h>
38 #include <linux/sysctl.h>
39 #include <linux/module.h>
40 #include <linux/moduleparam.h>
43 /* portability features, to be set before the rest: */
44 #define HAVE_NET_IPLEN /* iplen/ipoff in net format */
45 #define WITHOUT_BPF /* do not use bpf logging */
49 #ifndef DEFINE_SPINLOCK
50 #define DEFINE_SPINLOCK(x) FAST_MUTEX x
52 /* spinlock --> Guarded Mutex KGUARDED_MUTEX */
53 /* http://www.reactos.org/wiki/index.php/Guarded_Mutex */
54 #define spin_lock_init(_l)
55 #define spin_lock_bh(_l)
56 #define spin_unlock_bh(_l)
58 #include <sys/socket.h> /* bsd-compat.c */
59 #include <netinet/in.h> /* bsd-compat.c */
60 #include <netinet/ip.h> /* local version */
61 #define INADDR_TO_IFP(a, b) b = NULL
65 #define MALLOC_DECLARE(x) /* nothing */
66 #include <linux/time.h> /* do_gettimeofday */
67 #include <netinet/ip.h> /* local version */
71 * Kernel locking support.
72 * FreeBSD uses mtx in dummynet.c and struct rwlock ip_fw2.c
74 * In linux we use spinlock_bh to implement both.
75 * For 'struct rwlock' we need an #ifdef to change it to spinlock_t
78 #ifndef DEFINE_SPINLOCK /* this is for linux 2.4 */
79 #define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
83 #define rw_assert(a, b)
84 #define rw_destroy(_l)
85 #define rw_init(_l, msg) spin_lock_init(_l)
86 #define rw_rlock(_l) spin_lock_bh(_l)
87 #define rw_runlock(_l) spin_unlock_bh(_l)
88 #define rw_wlock(_l) spin_lock_bh(_l)
89 #define rw_wunlock(_l) spin_unlock_bh(_l)
90 #define rw_init_flags(_l, s, v)
92 #define mtx_assert(a, b)
93 #define mtx_destroy(m)
94 #define mtx_init(m, a,b,c) spin_lock_init(m)
95 #define mtx_lock(_l) spin_lock_bh(_l)
96 #define mtx_unlock(_l) spin_unlock_bh(_l)
98 #endif /* __linux__ */
99 /* end of locking support */
102 * Reference to an ipfw rule that can be carried outside critical sections.
103 * A rule is identified by rulenum:rule_id which is ordered.
104 * In version chain_id the rule can be found in slot 'slot', so
105 * we don't need a lookup if chain_id == chain->id.
107 * On exit from the firewall this structure refers to the rule after
108 * the matching one (slot points to the new rule; rulenum:rule_id-1
109 * is the matching rule), and additional info (e.g. info often contains
110 * the insn argument or tablearg in the low 16 bits, in host format).
111 * On entry, the structure is valid if slot>0, and refers to the starting
112 * rules. 'info' contains the reason for reinject, e.g. divert port,
113 * divert direction, and so on.
115 struct ipfw_rule_ref {
116 uint32_t slot; /* slot for matching rule */
117 uint32_t rulenum; /* matching rule number */
118 uint32_t rule_id; /* matching rule id */
119 uint32_t chain_id; /* ruleset id */
120 uint32_t info; /* see below */
124 IPFW_INFO_MASK = 0x0000ffff,
125 IPFW_INFO_OUT = 0x00000000, /* outgoing, just for convenience */
126 IPFW_INFO_IN = 0x80000000, /* incoming, overloads dir */
127 IPFW_ONEPASS = 0x40000000, /* One-pass, do not reinject */
128 IPFW_IS_MASK = 0x30000000, /* which source ? */
129 IPFW_IS_DIVERT = 0x20000000,
130 IPFW_IS_DUMMYNET =0x10000000,
131 IPFW_IS_PIPE = 0x08000000, /* pipe=1, queue = 0 */
134 /* in netinet/in.h */
135 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
137 /* bzero not present on linux, but this should go in glue.h */
138 #define bzero(s, n) memset(s, 0, n)
139 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
142 #define ETHERTYPE_IP 0x0800 /* IP protocol */
143 //#define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
144 struct ether_header {
145 u_char ether_dhost[ETHER_ADDR_LEN];
146 u_char ether_shost[ETHER_ADDR_LEN];
150 #define ETHER_TYPE_LEN 2 /* length of the Ethernet type field */
151 #define ETHER_HDR_LEN (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
154 * Historically, BSD keeps ip_len and ip_off in host format
155 * when doing layer 3 processing, and this often requires
156 * to translate the format back and forth.
157 * To make the process explicit, we define a couple of macros
158 * that also take into account the fact that at some point
159 * we may want to keep those fields always in net format.
162 #if (BYTE_ORDER == BIG_ENDIAN) || defined(HAVE_NET_IPLEN)
163 #define SET_NET_IPLEN(p) do {} while (0)
164 #define SET_HOST_IPLEN(p) do {} while (0)
165 #else /* never on linux */
166 #define SET_NET_IPLEN(p) do { \
167 struct ip *h_ip = (p); \
168 h_ip->ip_len = htons(h_ip->ip_len); \
169 h_ip->ip_off = htons(h_ip->ip_off); \
172 #define SET_HOST_IPLEN(p) do { \
173 struct ip *h_ip = (p); \
174 h_ip->ip_len = ntohs(h_ip->ip_len); \
175 h_ip->ip_off = ntohs(h_ip->ip_off); \
177 #endif /* !HAVE_NET_IPLEN */
180 #define __FreeBSD_version 500035
184 int my_mod_register(const char *name,
185 int order, struct moduledata *mod, void *init, void *uninit);
187 /* define some macro for ip_dummynet */
192 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
193 struct malloc_type type[1]; void *md_dummy_ ## type = type
197 /* log... does not use the first argument */
198 #define LOG_ERR 0x100
199 #define LOG_INFO 0x200
200 #define log(_level, fmt, arg...) do { \
201 int _qwerty=_level;(void)_qwerty; printk(KERN_ERR fmt, ##arg); } while (0)
204 * gettimeofday would be in sys/time.h but it is not
205 * visible if _KERNEL is defined
207 int gettimeofday(struct timeval *, struct timezone *);
210 #define MALLOC_DEFINE(a,b,c)
214 extern long tick; /* exists in 2.4 but not in 2.6 */
215 extern int bootverbose;
216 extern struct timeval boottime;
218 /* The time_uptime a FreeBSD variable increased each second */
220 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,4,37) /* revise boundaries */
221 #define time_uptime get_seconds()
223 #define time_uptime CURRENT_TIME
226 #define time_uptime time_uptime_w32()
229 extern int max_linkhdr;
230 extern int ip_defttl;
231 extern u_long in_ifaddrhmask; /* mask for hash table */
232 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
234 /*-------------------------------------------------*/
236 /* define, includes and functions missing in linux */
237 /* include and define */
238 #include <arpa/inet.h> /* inet_ntoa */
242 /* used by ip_dummynet.c */
243 void reinject_drop(struct mbuf* m);
245 #include <linux/errno.h> /* error define */
246 #include <linux/if.h> /* IFNAMESIZ */
250 * some network structure can be defined in the bsd way
251 * by using the _FAVOR_BSD definition. This is not true
252 * for icmp structure.
253 * XXX struct icmp contains bsd names in
254 * /usr/include/netinet/ip_icmp.h
257 #define icmp_code code
258 #define icmp_type type
260 /* linux in6_addr has no member __u6_addr
261 * replace the whole structure ?
263 #define __u6_addr in6_u
264 #define __u6_addr32 u6_addr32
265 #endif /* __linux__ */
267 /* defined in linux/sctp.h with no bsd definition */
269 uint16_t src_port; /* source port */
270 uint16_t dest_port; /* destination port */
271 uint32_t v_tag; /* verification tag of packet */
272 uint32_t checksum; /* Adler32 C-Sum */
273 /* chunks follow... */
276 /* missing definition */
282 #define RTF_CLONING 0x100 /* generate new routes on use */
284 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
285 #define IPPROTO_CARP 112 /* CARP */
287 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
290 #define CARP_VERSION 2
291 #define CARP_ADVERTISEMENT 0x01
293 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
295 #define IP_FORWARDING 0x1 /* most of ip header exists */
297 #define NETISR_IP 2 /* same as AF_INET */
299 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
301 extern int securelevel;
304 #if BYTE_ORDER == LITTLE_ENDIAN
305 u_int8_t carp_type:4,
308 #if BYTE_ORDER == BIG_ENDIAN
309 u_int8_t carp_version:4,
315 int dummy; /* windows compiler does not like empty definition */
320 struct rtentry *ro_rt;
321 struct sockaddr ro_dst;
330 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
333 #if defined( __linux__)
334 #define if_xname name
336 /* search local the ip addresses, used for the "me" keyword */
337 #include <linux/inetdevice.h>
339 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
340 #define INADDR_TO_IFP(ip, b) \
341 b = ip_dev_find(ip.s_addr)
343 #define INADDR_TO_IFP(ip, b) \
344 b = ip_dev_find((struct net *)&init_net, ip.s_addr)
347 #elif defined( _WIN32 )
348 /* used in ip_dummynet.c */
350 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
351 // struct ifaltq if_snd; /* output queue (includes altq) */
355 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
360 int in_cksum(struct mbuf *m, int len);
361 #define divert_cookie(mtag) 0
362 #define divert_info(mtag) 0
363 #define pf_find_mtag(a) NULL
364 #define pf_get_mtag(a) NULL
366 #define AF_LINK AF_ASH /* ? our sys/socket.h */
369 /* we don't pullup, either success or free and fail */
370 #define m_pullup(m, x) \
371 ((m)->m_len >= x ? (m) : (FREE_PKT(m), NULL))
374 void *hdr; /* saved hdr pos in mbuf, for ECN */
375 sa_family_t af; /* for ECN */
376 u_int32_t qid; /* queue id */
380 /* radix stuff in radix.h and radix.c */
382 caddr_t rn_key; /* object of search */
383 caddr_t rn_mask; /* netmask, if present */
387 /* missing kernel functions */
388 char *inet_ntoa(struct in_addr ina);
392 * Return the risult of a/b
394 * this is used in linux kernel space,
395 * since the 64bit division needs to
396 * be done using a macro
399 div64(int64_t a, int64_t b);
402 inet_ntoa_r(struct in_addr ina, char *buf);
404 /* from bsd sys/queue.h */
405 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
406 for ((var) = TAILQ_FIRST((head)); \
407 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
410 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
411 for ((var) = SLIST_FIRST((head)); \
412 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
415 /* depending of linux version */
416 #ifndef ETHERTYPE_IPV6
417 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
420 /*-------------------------------------------------*/
422 extern u_int rt_numfibs;
424 /* involves kernel locking function */
427 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
430 void getmicrouptime(struct timeval *tv);
432 /* from sys/netinet/ip_output.c */
437 struct mbuf *ip_reass(struct mbuf *);
438 u_short in_cksum_hdr(struct ip *);
439 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
440 struct ip_moptions *imo, struct inpcb *inp);
442 /* from net/netisr.c */
443 void netisr_dispatch(int num, struct mbuf *m);
445 /* definition moved in missing.c */
446 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
448 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
450 /* defined in session.c */
451 int priv_check(struct thread *td, int priv);
453 /* struct ucred is in linux/socket.h and has pid, uid, gid.
454 * We need a 'bsd_ucred' to store also the extra info
465 cred_check(void *insn, int proto, struct ifnet *oif,
466 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip,
467 u_int16_t src_port, struct bsd_ucred *u, int *ugid_lookupp,
468 struct sk_buff *skb);
470 int securelevel_ge(struct ucred *cr, int level);
476 #define module_param_named(_name, _var, _ty, _perm)
479 /* Linux 2.4 is mostly for openwrt */
480 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
481 #include <linux/bitops.h> /* generic_ffs() used in ip_fw2.c */
482 typedef uint32_t __be32;
483 typedef uint16_t __be16;
486 struct inet_hashinfo;
487 struct sock *inet_lookup(
488 struct inet_hashinfo *hashinfo,
489 const __be32 saddr, const __be16 sport,
490 const __be32 daddr, const __be16 dport,
492 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
493 #endif /* Linux < 2.6 */
495 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) && \
496 LINUX_VERSION_CODE > KERNEL_VERSION(2,6,16) /* XXX NOT sure, in 2.6.9 give an error */
497 #define module_param_named(_name, _var, _ty, _perm) \
498 //module_param(_name, _ty, 0644)
501 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24)
502 typedef unsigned long uintptr_t;
505 static inline unsigned long __fls(unsigned long word)
514 #endif /* LINUX < 2.6.25 */
516 #endif /* !_WIN32 so maybe __linux__ */
518 #if defined (__linux__) && !defined (EMULATE_SYSCTL)
519 #define SYSCTL_DECL(_1)
520 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
521 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
522 #define _SYSCTL_BASE(_name, _var, _ty, _perm) \
523 module_param_named(_name, *(_var), _ty, \
524 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
525 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
527 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
528 _SYSCTL_BASE(_name, _var, int, _mode)
530 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
531 _SYSCTL_BASE(_name, _var, long, _mode)
533 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
534 _SYSCTL_BASE(_name, _var, ulong, _mode)
536 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
537 _SYSCTL_BASE(_name, _var, uint, _mode)
539 #define TUNABLE_INT(_name, _ptr)
541 #define SYSCTL_VNET_PROC SYSCTL_PROC
542 #define SYSCTL_VNET_INT SYSCTL_INT
543 #define SYSCTL_VNET_UINT SYSCTL_UINT
547 #define SYSCTL_HANDLER_ARGS \
548 struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
549 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
550 int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
553 void ether_demux(struct ifnet *ifp, struct mbuf *m);
555 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
557 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
559 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
561 void rtfree(struct rtentry *rt);
563 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
565 #ifdef INP_LOCK_ASSERT
566 #undef INP_LOCK_ASSERT
567 #define INP_LOCK_ASSERT(a)
570 int jailed(struct ucred *cred);
573 * Return 1 if an internet address is for a ``local'' host
574 * (one to which we have a connection). If subnetsarelocal
575 * is true, this includes other subnets of the local net.
576 * Otherwise, it includes only the directly-connected (sub)nets.
578 int in_localaddr(struct in_addr in);
580 /* the prototype is already in the headers */
581 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
583 int fnmatch(const char *pattern, const char *string, int flags);
586 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
587 const __be32 daddr, const __be16 dport,
588 struct sk_buff *skb, int dir, struct bsd_ucred *u);
590 /* vnet wrappers, in vnet.h and ip_var.h */
591 //int ipfw_init(void);
592 //void ipfw_destroy(void);
594 #define MTAG_IPFW 1148380143 /* IPFW-tagged cookie */
595 #define MTAG_IPFW_RULE 1262273568 /* rule reference */
598 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
601 #define CURVNET_SET(_v)
602 #define CURVNET_RESTORE()
603 #define VNET_ASSERT(condition)
605 #define VNET_NAME(n) n
606 #define VNET_DECLARE(t, n) extern t n
607 #define VNET_DEFINE(t, n) t n
608 #define _VNET_PTR(b, n) &VNET_NAME(n)
610 * Virtualized global variable accessor macros.
612 #define VNET_VNET_PTR(vnet, n) (&(n))
613 #define VNET_VNET(vnet, n) (n)
615 #define VNET_PTR(n) (&(n))
618 VNET_DECLARE(int, ip_defttl);
619 #define V_ip_defttl VNET(ip_defttl);
621 int ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp,
622 int dir, struct inpcb *inp);
624 /* hooks for divert */
625 extern void (*ip_divert_ptr)(struct mbuf *m, int incoming);
627 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
628 typedef int ip_fw_ctl_t(struct sockopt *);
629 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
631 /* netgraph prototypes */
632 typedef int ng_ipfw_input_t(struct mbuf **, int, struct ip_fw_args *, int);
633 extern ng_ipfw_input_t *ng_ipfw_input_p;
635 /* For kernel ipfw_ether and ipfw_bridge. */
637 typedef int ip_fw_chk_t(struct ip_fw_args *args);
638 extern ip_fw_chk_t *ip_fw_chk_ptr;
640 #define V_ip_fw_chk_ptr VNET(ip_fw_chk_ptr)
641 #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr)
642 #define V_tcbinfo VNET(tcbinfo)
643 #define V_udbinfo VNET(udbinfo)
645 #endif /* !_MISSING_H_ */