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
26 * $Id: glue.h 4661 2010-01-04 11:56:12Z luigi $
28 * glue code to adapt the FreeBSD version to linux and windows,
29 * userland and kernel.
30 * This is included before any other headers, so we do not have
31 * a chance to override any #define that should appear in other
39 * common definitions to allow portability
46 * emulation of FreeBSD's sockopt and thread
47 * This was in sockopt.h
49 enum sopt_dir { SOPT_GET, SOPT_SET };
51 #ifndef KERNEL_MODULE /* Userland part */
53 #include <stdint.h> /* linux needs this in addition to sys/types.h */
55 #include <sys/types.h> /* for size_t */
56 #include <sys/ioctl.h>
60 #include <netinet/ether.h>
62 #else /* KERNEL_MODULE, kernel part */
65 #include <linux/version.h>
67 #define ifnet net_device /* remap */
68 #define _KERNEL # make kernel structure visible
69 #define KLD_MODULE # add the module glue
70 #define INET # want inet support
72 #include <linux/stddef.h> /* linux kernel */
73 #include <linux/types.h> /* linux kernel */
76 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
77 #include <linux/linkage.h> /* linux/msg.h require this */
78 #include <linux/netdevice.h> /* just MAX_ADDR_LEN 8 on 2.4 32 on 2.6, also brings in byteorder */
81 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) && \
82 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) // under 2.6.22 compilation is required by msg.h
83 #include <linux/spinlock_types.h>
85 #include <linux/msg.h> /* XXX m_type define conflict with include/sys/mbuf.h,
86 * so early include this file (to be solved) */
87 #include <linux/list.h>
88 #include <linux/in.h> /* struct in_addr */
89 #include <linux/in6.h> /* struct in6_addr */
90 #include <linux/icmp.h>
92 * LIST_HEAD in queue.h conflict with linux/list.h
93 * some previous linux include need list.h definition
97 #define IF_NAMESIZE 16
98 typedef uint32_t in_addr_t;
100 #define printf(fmt, arg...) printk(KERN_ERR fmt, ##arg)
103 #endif /* KERNEL_MODULE */
106 * In windows, we need to emulate the sockopt interface
107 * so also the userland needs to have the struct sockopt defined.
108 * No need to declare struct thread on linux, but we need on windows.
117 enum sopt_dir sopt_dir; /* is this a get or a set? */
118 int sopt_level; /* second arg of [gs]etsockopt */
119 int sopt_name; /* third arg of [gs]etsockopt */
120 void *sopt_val; /* fourth arg of [gs]etsockopt */
121 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
122 struct thread *sopt_td; /* calling thread or null if kernel */
126 /* This must be included here after list.h */
127 #include <sys/queue.h> /* both the kernel side and nat.c needs this */
129 #ifndef KERNEL_MODULE
131 /* define internals for struct in6_addr netinet/in6.h on FreeBSD */
132 #define __u6_addr in6_u
133 #define __u6_addr32 u6_addr32
134 /* define missing type for ipv6 (linux 2.6.28) */
135 #define in6_u __in6_u
137 /* missing in linux netinet/ip.h */
138 #define IPTOS_ECN_ECT0 0x02 /* ECN-capable transport (0) */
139 #define IPTOS_ECN_CE 0x03 /* congestion experienced */
141 /* defined in freebsd netinet/icmp6.h */
142 #define ICMP6_MAXTYPE 201
144 /* on freebsd sys/socket.h pf specific */
145 #define NET_RT_IFLIST 3 /* survey interface list */
147 /* on freebsd net/if.h XXX used */
151 u_long ifi_mtu; /* maximum transmission unit */
155 * Message format for use in obtaining information about interfaces
156 * from getkerninfo and the routing socket.
157 * This is used in nat.c
160 u_short ifm_msglen; /* to skip over non-understood messages */
161 u_char ifm_version; /* future binary compatibility */
162 u_char ifm_type; /* message type */
163 int ifm_addrs; /* like rtm_addrs */
164 int ifm_flags; /* value of if_flags */
165 u_short ifm_index; /* index for associated ifp */
166 struct if_data ifm_data;/* statistics and other data about if */
170 * Message format for use in obtaining information about interface addresses
171 * from getkerninfo and the routing socket
174 u_short ifam_msglen; /* to skip over non-understood messages */
175 u_char ifam_version; /* future binary compatibility */
176 u_char ifam_type; /* message type */
177 int ifam_addrs; /* like rtm_addrs */
178 int ifam_flags; /* value of ifa_flags */
179 u_short ifam_index; /* index for associated ifp */
180 int ifam_metric; /* value of ifa_metric */
183 #ifndef NO_RTM /* conflicting with netlink */
184 /* missing in net/route.h */
185 #define RTM_VERSION 5 /* Up the ante and ignore older versions */
186 #define RTM_IFINFO 0xe /* iface going up/down etc. */
187 #define RTM_NEWADDR 0xc /* address being added to iface */
188 #define RTA_IFA 0x20 /* interface addr sockaddr present */
191 /* SA_SIZE is used in the userland nat.c modified */
192 #define SA_SIZE(sa) \
195 1 + ( (sizeof(struct sockaddr) - 1) | (sizeof(long) - 1) ) )
199 * Getkerninfo clock information structure
202 int hz; /* clock frequency */
203 int tick; /* micro-seconds per hz tick */
205 int stathz; /* statistics clock frequency */
206 int profhz; /* profiling clock frequency */
210 * linux does not have a reentrant version of qsort,
211 * so we the FreeBSD stdlib version.
214 qsort_r(void *a, size_t n, size_t es, void *thunk,
215 int cmp_t(void *, const void *, const void *));
217 /* prototypes from libutil */
218 /* humanize_number(3) */
219 #define HN_DECIMAL 0x01
220 #define HN_NOSPACE 0x02
222 #define HN_DIVISOR_1000 0x08
224 #define HN_GETSCALE 0x10
225 #define HN_AUTOSCALE 0x20
227 int humanize_number(char *_buf, size_t _len, int64_t _number,
228 const char *_suffix, int _scale, int _flags);
229 int expand_number(const char *_buf, int64_t *_num);
231 #define setprogname(x) /* not present in linux */
233 extern int optreset; /* not present in linux */
235 size_t strlcpy(char * dst, const char * src, size_t siz);
237 strtonum(const char *nptr, long long minval, long long maxval,
238 const char **errstr);
241 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
245 /* linux does not have sin_len in sockaddr, we only remap in userland */
246 #define sin_len sin_zero[0]
247 #endif /* __linux__ */
249 #else /* KERNEL_MODULE */
251 /* linux and windows kernel do not have bcopy ? */
252 #define bcopy(_s, _d, _l) memcpy(_d, _s, _l)
254 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
255 #include <linux/in6.h>
258 /* skb_dst() was introduced from linux 2.6.31 */
259 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,31) // or 2.4.x
260 #define skb_dst(_dummy) skb->dst
263 /* definitions useful for the kernel side */
265 struct route_in6 { };
267 #endif /* KERNEL_MODULE */
269 /* missing in netinet/in.h */
271 #define INET_ADDRSTRLEN 16
275 * List of values used for set/getsockopt options.
276 * The base value on FreeBSD is defined as a macro,
277 * if not available we will use our own enum.
278 * The TABLE_BASE value is used in the kernel.
280 #ifndef IP_FW_TABLE_ADD
281 #define _IPFW_SOCKOPT_BASE 100 /* 40 on freebsd */
283 IP_FW_TABLE_ADD = _IPFW_SOCKOPT_BASE,
288 IP_FW_DYN_GET, /* new addition */
290 /* IP_FW3 and IP_DUMMYNET3 are the new API */
291 IP_FW3 = _IPFW_SOCKOPT_BASE + 8,
294 IP_FW_ADD = _IPFW_SOCKOPT_BASE + 10,
303 IP_FW_NAT_GET_CONFIG,
306 IP_DUMMYNET_CONFIGURE,
310 IP_DUMMYNET_GET = _IPFW_SOCKOPT_BASE + 24,
313 #endif /* IP_FW_TABLE_ADD */
315 #endif /* !_GLUE_H */