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 4696 2010-01-07 12:37:09Z marta $
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
76 #endif /* __linux__ */
78 #define rw_assert(a, b)
79 #define rw_destroy(_l)
80 #define rw_init(_l, msg) spin_lock_init(_l)
81 #define rw_rlock(_l) spin_lock_bh(_l)
82 #define rw_runlock(_l) spin_unlock_bh(_l)
83 #define rw_wlock(_l) spin_lock_bh(_l)
84 #define rw_wunlock(_l) spin_unlock_bh(_l)
85 #define rw_init_flags(_l, s, v)
87 #define mtx_assert(a, b)
88 #define mtx_destroy(m)
89 #define mtx_init(m, a,b,c) spin_lock_init(m)
90 #define mtx_lock(_l) spin_lock_bh(_l)
91 #define mtx_unlock(_l) spin_unlock_bh(_l)
93 /* end of locking support */
96 #define in_nullhost(x) ((x).s_addr == INADDR_ANY)
98 /* bzero not present on linux, but this should go in glue.h */
99 #define bzero(s, n) memset(s, 0, n)
100 #define bcmp(p1, p2, n) memcmp(p1, p2, n)
103 #define ETHERTYPE_IP 0x0800 /* IP protocol */
104 #define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
105 struct ether_header {
106 u_char ether_dhost[ETHER_ADDR_LEN];
107 u_char ether_shost[ETHER_ADDR_LEN];
111 #define ETHER_ADDR_LEN 6 /* length of an Ethernet address */
112 #define ETHER_TYPE_LEN 2 /* length of the Ethernet type field */
113 #define ETHER_HDR_LEN (ETHER_ADDR_LEN*2+ETHER_TYPE_LEN)
116 * Historically, BSD keeps ip_len and ip_off in host format
117 * when doing layer 3 processing, and this often requires
118 * to translate the format back and forth.
119 * To make the process explicit, we define a couple of macros
120 * that also take into account the fact that at some point
121 * we may want to keep those fields always in net format.
124 #if (BYTE_ORDER == BIG_ENDIAN) || defined(HAVE_NET_IPLEN)
125 #define SET_NET_IPLEN(p) do {} while (0)
126 #define SET_HOST_IPLEN(p) do {} while (0)
127 #else /* never on linux */
128 #define SET_NET_IPLEN(p) do { \
129 struct ip *h_ip = (p); \
130 h_ip->ip_len = htons(h_ip->ip_len); \
131 h_ip->ip_off = htons(h_ip->ip_off); \
134 #define SET_HOST_IPLEN(p) do { \
135 struct ip *h_ip = (p); \
136 h_ip->ip_len = ntohs(h_ip->ip_len); \
137 h_ip->ip_off = ntohs(h_ip->ip_off); \
139 #endif /* !HAVE_NET_IPLEN */
142 #define __FreeBSD_version 500035
146 int my_mod_register(const char *name,
147 int order, struct moduledata *mod, void *init, void *uninit);
149 /* define some macro for ip_dummynet */
154 #define MALLOC_DEFINE(type, shortdesc, longdesc) \
155 struct malloc_type type[1]; void *md_dummy_ ## type = type
159 /* log... does not use the first argument */
160 #define LOG_ERR 0x100
161 #define LOG_INFO 0x200
162 #define log(_level, fmt, arg...) do { \
163 int __unused _qwerty=_level; printk(KERN_ERR fmt, ##arg); } while (0)
166 * gettimeofday would be in sys/time.h but it is not
167 * visible if _KERNEL is defined
169 int gettimeofday(struct timeval *, struct timezone *);
172 #define MALLOC_DEFINE(a,b,c)
176 extern long tick; /* exists in 2.4 but not in 2.6 */
177 extern int bootverbose;
178 extern time_t time_uptime;
179 extern struct timeval boottime;
181 extern int max_linkhdr;
182 extern int ip_defttl;
183 extern u_long in_ifaddrhmask; /* mask for hash table */
184 extern struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */
186 /*-------------------------------------------------*/
188 /* define, includes and functions missing in linux */
189 /* include and define */
190 #include <arpa/inet.h> /* inet_ntoa */
194 /* used by ip_dummynet.c */
195 void reinject_drop(struct mbuf* m);
197 #include <linux/errno.h> /* error define */
198 #include <linux/if.h> /* IFNAMESIZ */
202 * some network structure can be defined in the bsd way
203 * by using the _FAVOR_BSD definition. This is not true
204 * for icmp structure.
205 * XXX struct icmp contains bsd names in
206 * /usr/include/netinet/ip_icmp.h
209 #define icmp_code code
210 #define icmp_type type
212 /* linux in6_addr has no member __u6_addr
213 * replace the whole structure ?
215 #define __u6_addr in6_u
216 #define __u6_addr32 u6_addr32
217 #endif /* __linux__ */
219 /* defined in linux/sctp.h with no bsd definition */
221 uint16_t src_port; /* source port */
222 uint16_t dest_port; /* destination port */
223 uint32_t v_tag; /* verification tag of packet */
224 uint32_t checksum; /* Adler32 C-Sum */
225 /* chunks follow... */
228 /* missing definition */
234 #define RTF_CLONING 0x100 /* generate new routes on use */
236 #define IPPROTO_OSPFIGP 89 /* OSPFIGP */
237 #define IPPROTO_CARP 112 /* CARP */
239 #define IPPROTO_IPV4 IPPROTO_IPIP /* for compatibility */
242 #define CARP_VERSION 2
243 #define CARP_ADVERTISEMENT 0x01
245 #define PRIV_NETINET_IPFW 491 /* Administer IPFW firewall. */
247 #define IP_FORWARDING 0x1 /* most of ip header exists */
249 #define NETISR_IP 2 /* same as AF_INET */
251 #define PRIV_NETINET_DUMMYNET 494 /* Administer DUMMYNET. */
253 extern int securelevel;
256 #if BYTE_ORDER == LITTLE_ENDIAN
257 u_int8_t carp_type:4,
260 #if BYTE_ORDER == BIG_ENDIAN
261 u_int8_t carp_version:4,
267 int dummy; /* windows compiler does not like empty definition */
271 struct rtentry *ro_rt;
272 struct sockaddr ro_dst;
280 * ifnet->if_snd is used in ip_dummynet.c to take the transmission
283 #if defined( __linux__)
284 #define if_xname name
286 #elif defined( _WIN32 )
287 /* used in ip_dummynet.c */
289 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
290 // struct ifaltq if_snd; /* output queue (includes altq) */
294 char if_xname[IFNAMSIZ]; /* external name (name + unit) */
299 int in_cksum(struct mbuf *m, int len);
300 #define divert_cookie(mtag) 0
301 #define divert_info(mtag) 0
302 #define INADDR_TO_IFP(a, b) b = NULL
303 #define pf_find_mtag(a) NULL
304 #define pf_get_mtag(a) NULL
306 #define AF_LINK AF_ASH /* ? our sys/socket.h */
309 /* we don't pullup, either success or free and fail */
310 #define m_pullup(m, x) \
311 ((m)->m_len >= x ? (m) : (FREE_PKT(m), NULL))
314 void *hdr; /* saved hdr pos in mbuf, for ECN */
315 sa_family_t af; /* for ECN */
316 u_int32_t qid; /* queue id */
320 /* radix stuff in radix.h and radix.c */
322 caddr_t rn_key; /* object of search */
323 caddr_t rn_mask; /* netmask, if present */
327 /* missing kernel functions */
328 char *inet_ntoa(struct in_addr ina);
332 * Return the risult of a/b
334 * this is used in linux kernel space,
335 * since the 64bit division needs to
336 * be done using a macro
339 div64(int64_t a, int64_t b);
342 inet_ntoa_r(struct in_addr ina, char *buf);
344 /* from bsd sys/queue.h */
345 #define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
346 for ((var) = TAILQ_FIRST((head)); \
347 (var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
350 #define SLIST_FOREACH_SAFE(var, head, field, tvar) \
351 for ((var) = SLIST_FIRST((head)); \
352 (var) && ((tvar) = SLIST_NEXT((var), field), 1); \
355 /* depending of linux version */
356 #ifndef ETHERTYPE_IPV6
357 #define ETHERTYPE_IPV6 0x86dd /* IP protocol version 6 */
360 /*-------------------------------------------------*/
362 extern u_int rt_numfibs;
364 /* involves kernel locking function */
367 #define RTFREE(a) fprintf(stderr, "RTFREE: commented out locks\n");
370 void getmicrouptime(struct timeval *tv);
372 /* from sys/netinet/ip_output.c */
377 struct mbuf *ip_reass(struct mbuf *);
378 u_short in_cksum_hdr(struct ip *);
379 int ip_output(struct mbuf *m, struct mbuf *opt, struct route *ro, int flags,
380 struct ip_moptions *imo, struct inpcb *inp);
382 /* from net/netisr.c */
383 void netisr_dispatch(int num, struct mbuf *m);
385 /* definition moved in missing.c */
386 int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
388 int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
390 /* defined in session.c */
391 int priv_check(struct thread *td, int priv);
393 /* struct ucred is in linux/socket.h and has pid, uid, gid.
394 * We need a 'bsd_ucred' to store also the extra info
405 cred_check(void *insn, int proto, struct ifnet *oif,
406 struct in_addr dst_ip, u_int16_t dst_port, struct in_addr src_ip,
407 u_int16_t src_port, struct bsd_ucred *u, int *ugid_lookupp,
408 struct sk_buff *skb);
410 int securelevel_ge(struct ucred *cr, int level);
416 * sysctl are mapped into /sys/module/ipfw_mod parameters
419 #define CTLFLAG_RDTUN 1
421 #define CTLFLAG_SECURE3 0 // unsupported
422 #define CTLFLAG_VNET 0 /* unsupported */
425 #define module_param_named(_name, _var, _ty, _perm)
428 /* Linux 2.4 is mostly for openwrt */
429 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
430 #include <linux/bitops.h> /* generic_ffs() used in ip_fw2.c */
431 typedef uint32_t __be32;
432 typedef uint16_t __be16;
435 struct inet_hashinfo;
436 struct sock *inet_lookup(
437 struct inet_hashinfo *hashinfo,
438 const __be32 saddr, const __be16 sport,
439 const __be32 daddr, const __be16 dport,
441 struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
442 #endif /* Linux < 2.6 */
444 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
445 #define module_param_named(_name, _var, _ty, _perm) \
446 //module_param(_name, _ty, 0644)
448 #endif /* __linux__ */
449 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25)
450 typedef unsigned long uintptr_t;
453 #define SYSCTL_DECL(_1)
454 #define SYSCTL_OID(_1, _2, _3, _4, _5, _6, _7, _8)
455 #define SYSCTL_NODE(_1, _2, _3, _4, _5, _6)
456 #define _SYSCTL_BASE(_name, _var, _ty, _perm) \
457 module_param_named(_name, *(_var), _ty, \
458 ( (_perm) == CTLFLAG_RD) ? 0444: 0644 )
459 #define SYSCTL_PROC(_base, _oid, _name, _mode, _var, _val, _desc, _a, _b)
461 #define SYSCTL_INT(_base, _oid, _name, _mode, _var, _val, _desc) \
462 _SYSCTL_BASE(_name, _var, int, _mode)
464 #define SYSCTL_LONG(_base, _oid, _name, _mode, _var, _val, _desc) \
465 _SYSCTL_BASE(_name, _var, long, _mode)
467 #define SYSCTL_ULONG(_base, _oid, _name, _mode, _var, _val, _desc) \
468 _SYSCTL_BASE(_name, _var, ulong, _mode)
470 #define SYSCTL_UINT(_base, _oid, _name, _mode, _var, _val, _desc) \
471 _SYSCTL_BASE(_name, _var, uint, _mode)
473 #define SYSCTL_HANDLER_ARGS \
474 struct sysctl_oid *oidp, void *arg1, int arg2, struct sysctl_req *req
475 int sysctl_handle_int(SYSCTL_HANDLER_ARGS);
476 int sysctl_handle_long(SYSCTL_HANDLER_ARGS);
478 #define TUNABLE_INT(_name, _ptr)
480 void ether_demux(struct ifnet *ifp, struct mbuf *m);
482 int ether_output_frame(struct ifnet *ifp, struct mbuf *m);
484 void in_rtalloc_ign(struct route *ro, u_long ignflags, u_int fibnum);
486 void icmp_error(struct mbuf *n, int type, int code, uint32_t dest, int mtu);
488 void rtfree(struct rtentry *rt);
490 u_short in_cksum_skip(struct mbuf *m, int len, int skip);
492 #ifdef INP_LOCK_ASSERT
493 #undef INP_LOCK_ASSERT
494 #define INP_LOCK_ASSERT(a)
497 int jailed(struct ucred *cred);
500 * Return 1 if an internet address is for a ``local'' host
501 * (one to which we have a connection). If subnetsarelocal
502 * is true, this includes other subnets of the local net.
503 * Otherwise, it includes only the directly-connected (sub)nets.
505 int in_localaddr(struct in_addr in);
507 /* the prototype is already in the headers */
508 //int ipfw_chg_hook(SYSCTL_HANDLER_ARGS);
510 int fnmatch(const char *pattern, const char *string, int flags);
513 linux_lookup(const int proto, const __be32 saddr, const __be16 sport,
514 const __be32 daddr, const __be16 dport,
515 struct sk_buff *skb, int dir, struct bsd_ucred *u);
517 /* vnet wrappers, in vnet.h and ip_var.h */
518 //int ipfw_init(void);
519 //void ipfw_destroy(void);
521 extern int (*ip_dn_io_ptr)(struct mbuf **m, int dir, struct ip_fw_args *fwa);
524 #define CURVNET_SET(_v)
525 #define CURVNET_RESTORE()
526 #define VNET_ASSERT(condition)
528 #define VNET_NAME(n) n
529 #define VNET_DECLARE(t, n) extern t n
530 #define VNET_DEFINE(t, n) t n
531 #define _VNET_PTR(b, n) &VNET_NAME(n)
533 * Virtualized global variable accessor macros.
535 #define VNET_VNET_PTR(vnet, n) (&(n))
536 #define VNET_VNET(vnet, n) (n)
538 #define VNET_PTR(n) (&(n))
542 ipfw_check_hook(void *arg, struct mbuf **m0, struct ifnet *ifp, int dir,
545 extern int (*ip_dn_ctl_ptr)(struct sockopt *);
546 typedef int ip_fw_ctl_t(struct sockopt *);
547 extern ip_fw_ctl_t *ip_fw_ctl_ptr;
549 /* For kernel ipfw_ether and ipfw_bridge. */
551 typedef int ip_fw_chk_t(struct ip_fw_args *args);
552 extern ip_fw_chk_t *ip_fw_chk_ptr;
554 #define V_ip_fw_chk_ptr VNET(ip_fw_chk_ptr)
555 #define V_ip_fw_ctl_ptr VNET(ip_fw_ctl_ptr)
556 #define V_tcbinfo VNET(tcbinfo)
557 #define V_udbinfo VNET(udbinfo)
559 #define SYSCTL_VNET_PROC SYSCTL_PROC
560 #define SYSCTL_VNET_INT SYSCTL_INT
562 #endif /* !_MISSING_H_ */