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
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>
59 #include <netinet/ether.h>
61 #else /* KERNEL_MODULE, kernel part */
64 #include <linux/version.h>
66 #define ifnet net_device /* remap */
67 #define _KERNEL # make kernel structure visible
68 #define KLD_MODULE # add the module glue
69 #define INET # want inet support
71 #include <linux/stddef.h> /* linux kernel */
72 #include <linux/types.h> /* linux kernel */
75 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
76 #include <linux/linkage.h> /* linux/msg.h require this */
77 #include <linux/netdevice.h> /* just MAX_ADDR_LEN 8 on 2.4 32 on 2.6, also brings in byteorder */
80 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,0) && \
81 LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) // under 2.6.22 compilation is required by msg.h
82 #include <linux/spinlock_types.h>
84 #include <linux/msg.h> /* XXX m_type define conflict with include/sys/mbuf.h,
85 * so early include this file (to be solved) */
86 #include <linux/list.h>
87 #include <linux/in.h> /* struct in_addr */
88 #include <linux/in6.h> /* struct in6_addr */
89 #include <linux/icmp.h>
91 * LIST_HEAD in queue.h conflict with linux/list.h
92 * some previous linux include need list.h definition
96 #define IF_NAMESIZE 16
97 typedef uint32_t in_addr_t;
99 #define printf(fmt, arg...) printk(KERN_ERR fmt, ##arg)
102 #endif /* KERNEL_MODULE */
105 * In windows, we need to emulate the sockopt interface
106 * so also the userland needs to have the struct sockopt defined.
107 * No need to declare struct thread on linux, but we need on windows.
116 enum sopt_dir sopt_dir; /* is this a get or a set? */
117 int sopt_level; /* second arg of [gs]etsockopt */
118 int sopt_name; /* third arg of [gs]etsockopt */
119 void *sopt_val; /* fourth arg of [gs]etsockopt */
120 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */
121 struct thread *sopt_td; /* calling thread or null if kernel */
125 /* This must be included here after list.h */
126 #include <sys/queue.h> /* both the kernel side and nat.c needs this */
128 #ifndef KERNEL_MODULE
130 /* define internals for struct in6_addr netinet/in6.h on FreeBSD */
131 #define __u6_addr in6_u
132 #define __u6_addr32 u6_addr32
133 /* define missing type for ipv6 (linux 2.6.28) */
134 #define in6_u __in6_u
136 /* missing in linux netinet/ip.h */
137 #define IPTOS_ECN_ECT0 0x02 /* ECN-capable transport (0) */
138 #define IPTOS_ECN_CE 0x03 /* congestion experienced */
140 /* defined in freebsd netinet/icmp6.h */
141 #define ICMP6_MAXTYPE 201
143 /* on freebsd sys/socket.h pf specific */
144 #define NET_RT_IFLIST 3 /* survey interface list */
146 /* on freebsd net/if.h XXX used */
150 u_long ifi_mtu; /* maximum transmission unit */
154 * Message format for use in obtaining information about interfaces
155 * from getkerninfo and the routing socket.
156 * This is used in nat.c
159 u_short ifm_msglen; /* to skip over non-understood messages */
160 u_char ifm_version; /* future binary compatibility */
161 u_char ifm_type; /* message type */
162 int ifm_addrs; /* like rtm_addrs */
163 int ifm_flags; /* value of if_flags */
164 u_short ifm_index; /* index for associated ifp */
165 struct if_data ifm_data;/* statistics and other data about if */
169 * Message format for use in obtaining information about interface addresses
170 * from getkerninfo and the routing socket
173 u_short ifam_msglen; /* to skip over non-understood messages */
174 u_char ifam_version; /* future binary compatibility */
175 u_char ifam_type; /* message type */
176 int ifam_addrs; /* like rtm_addrs */
177 int ifam_flags; /* value of ifa_flags */
178 u_short ifam_index; /* index for associated ifp */
179 int ifam_metric; /* value of ifa_metric */
182 #ifndef NO_RTM /* conflicting with netlink */
183 /* missing in net/route.h */
184 #define RTM_VERSION 5 /* Up the ante and ignore older versions */
185 #define RTM_IFINFO 0xe /* iface going up/down etc. */
186 #define RTM_NEWADDR 0xc /* address being added to iface */
187 #define RTA_IFA 0x20 /* interface addr sockaddr present */
190 /* SA_SIZE is used in the userland nat.c modified */
191 #define SA_SIZE(sa) \
194 1 + ( (sizeof(struct sockaddr) - 1) | (sizeof(long) - 1) ) )
198 * Getkerninfo clock information structure
201 int hz; /* clock frequency */
202 int tick; /* micro-seconds per hz tick */
204 int stathz; /* statistics clock frequency */
205 int profhz; /* profiling clock frequency */
209 * linux does not have a reentrant version of qsort,
210 * so we the FreeBSD stdlib version.
213 qsort_r(void *a, size_t n, size_t es, void *thunk,
214 int cmp_t(void *, const void *, const void *));
216 #define setprogname(x) /* not present in linux */
218 extern int optreset; /* not present in linux */
220 size_t strlcpy(char * dst, const char * src, size_t siz);
222 strtonum(const char *nptr, long long minval, long long maxval,
223 const char **errstr);
226 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
229 #else /* KERNEL_MODULE */
231 /* linux and windows kernel do not have bcopy ? */
232 #define bcopy(_s, _d, _l) memcpy(_d, _s, _l)
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
235 #include <linux/in6.h>
238 /* definitions useful for the kernel side */
240 struct route_in6 { };
242 #endif /* KERNEL_MODULE */
245 * List of values used for set/getsockopt options.
246 * The base value on FreeBSD is defined as a macro,
247 * if not available we will use our own enum.
248 * The TABLE_BASE value is used in the kernel.
250 #ifndef IP_FW_TABLE_ADD
251 #define _IPFW_SOCKOPT_BASE 100 /* 40 on freebsd */
253 IP_FW_TABLE_ADD = _IPFW_SOCKOPT_BASE,
259 IP_FW_ADD = _IPFW_SOCKOPT_BASE + 10,
269 IP_FW_NAT_GET_CONFIG,
272 IP_DUMMYNET_CONFIGURE,
276 IP_DUMMYNET_GET = _IPFW_SOCKOPT_BASE + 24,
279 #endif /* IP_FW_TABLE_ADD */
281 #endif /* !_GLUE_H */