Added the new version for dummynet.
[ipfw.git] / glue.h
1 /*
2  * Copyright (c) 2009 Luigi Rizzo, Marta Carbone, Universita` di Pisa
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  */
25 /*
26  * $Id: glue.h 4661 2010-01-04 11:56:12Z luigi $
27  *
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
32  * headers.
33  */
34  
35 #ifndef _GLUE_H
36 #define _GLUE_H
37
38 /*
39  * common definitions to allow portability
40  */
41 #ifndef __FBSDID
42 #define __FBSDID(x)
43 #endif  /* FBSDID */
44
45 /*
46  * emulation of FreeBSD's sockopt and thread
47  * This was in sockopt.h
48  */
49 enum sopt_dir { SOPT_GET, SOPT_SET };
50
51 #ifndef KERNEL_MODULE   /* Userland part */
52
53 #include <stdint.h>             /* linux needs this in addition to sys/types.h */
54
55 #include <sys/types.h>          /* for size_t */
56 #include <sys/ioctl.h>
57 #include <time.h>
58 #include <errno.h>
59
60 #include <netinet/ether.h>
61
62 #else /* KERNEL_MODULE, kernel part */
63
64 #ifndef _WIN32
65 #include <linux/version.h>
66
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
71
72 #include <linux/stddef.h>       /* linux kernel */
73 #include <linux/types.h>        /* linux kernel */
74
75
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 */
79 #endif
80
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>
84 #endif
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>
91 /*
92  * LIST_HEAD in queue.h conflict with linux/list.h
93  * some previous linux include need list.h definition
94  */
95 #undef LIST_HEAD
96
97 #define         IF_NAMESIZE     16
98 typedef uint32_t      in_addr_t;
99
100 #define printf(fmt, arg...) printk(KERN_ERR fmt, ##arg)
101
102 #endif  /* !_WIN32 */
103 #endif /* KERNEL_MODULE */
104
105 /*
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.
109  */
110
111 struct thread {
112         void *sopt_td;
113         void *td_ucred;
114 };
115
116 struct  sockopt {
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 */
123 };
124
125
126 /* This must be included here after list.h */
127 #include <sys/queue.h>          /* both the kernel side and nat.c needs this */
128
129 #ifndef KERNEL_MODULE
130
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
136
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 */
140
141 /* defined in freebsd netinet/icmp6.h */
142 #define ICMP6_MAXTYPE                   201
143
144 /* on freebsd sys/socket.h pf specific */
145 #define NET_RT_IFLIST   3               /* survey interface list */
146
147 /* on freebsd net/if.h XXX used */
148 struct if_data {
149
150         /* ... */
151         u_long ifi_mtu;                /* maximum transmission unit */   
152 };
153
154 /*
155  * Message format for use in obtaining information about interfaces
156  * from getkerninfo and the routing socket.
157  * This is used in nat.c
158  */
159 struct if_msghdr {
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 */
167 };
168
169 /*
170  * Message format for use in obtaining information about interface addresses
171  * from getkerninfo and the routing socket
172  */
173 struct ifa_msghdr {
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 */
181 };
182
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 */
189 #endif  /* NO_RTM */
190
191 /* SA_SIZE is used in the userland nat.c modified */
192 #define SA_SIZE(sa)                                             \
193     (  (!(sa) ) ?      \
194         sizeof(long)            :                               \
195         1 + ( (sizeof(struct sockaddr) - 1) | (sizeof(long) - 1) ) )
196
197 /* sys/time.h */
198 /*
199  * Getkerninfo clock information structure
200  */
201 struct clockinfo {
202         int     hz;             /* clock frequency */
203         int     tick;           /* micro-seconds per hz tick */
204         int     spare;
205         int     stathz;         /* statistics clock frequency */
206         int     profhz;         /* profiling clock frequency */
207 };
208
209 /*
210  * linux does not have a reentrant version of qsort,
211  * so we the FreeBSD stdlib version.
212  */
213 void
214 qsort_r(void *a, size_t n, size_t es, void *thunk,
215         int cmp_t(void *, const void *, const void *));
216
217 /* prototypes from libutil */
218 /* humanize_number(3) */
219 #define HN_DECIMAL              0x01
220 #define HN_NOSPACE              0x02
221 #define HN_B                    0x04
222 #define HN_DIVISOR_1000         0x08
223
224 #define HN_GETSCALE             0x10
225 #define HN_AUTOSCALE            0x20
226
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);
230
231 #define setprogname(x)  /* not present in linux */
232
233 extern int optreset;    /* not present in linux */
234
235 size_t strlcpy(char * dst, const char * src, size_t siz);
236 long long int
237 strtonum(const char *nptr, long long minval, long long maxval,
238          const char **errstr);
239  
240 int
241 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
242          size_t newlen);
243  
244 #ifdef __linux__
245 /* linux does not have sin_len in sockaddr, we only remap in userland */
246 #define sin_len sin_zero[0]
247 #endif /* __linux__ */
248
249 #else /* KERNEL_MODULE */
250
251 /* linux and windows kernel do not have bcopy ? */
252 #define bcopy(_s, _d, _l)       memcpy(_d, _s, _l)
253
254 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
255 #include <linux/in6.h>
256 #endif
257
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
261 #endif
262
263 /* definitions useful for the kernel side */
264
265 struct route_in6 { };
266
267 #endif  /* KERNEL_MODULE */
268
269 /* missing in netinet/in.h */
270
271 #define INET_ADDRSTRLEN         16
272
273
274 /*
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.
279  */
280 #ifndef IP_FW_TABLE_ADD
281 #define _IPFW_SOCKOPT_BASE      100     /* 40 on freebsd */
282 enum ipfw_msg_type {
283         IP_FW_TABLE_ADD         = _IPFW_SOCKOPT_BASE,
284         IP_FW_TABLE_DEL,
285         IP_FW_TABLE_FLUSH,
286         IP_FW_TABLE_GETSIZE,
287         IP_FW_TABLE_LIST,
288         IP_FW_DYN_GET,          /* new addition */
289
290         /* IP_FW3 and IP_DUMMYNET3 are the new API */
291         IP_FW3                  = _IPFW_SOCKOPT_BASE + 8,
292         IP_DUMMYNET3,
293
294         IP_FW_ADD               = _IPFW_SOCKOPT_BASE + 10,
295         IP_FW_DEL,
296         IP_FW_FLUSH,
297         IP_FW_ZERO,
298         IP_FW_GET,
299         IP_FW_RESETLOG,
300
301         IP_FW_NAT_CFG,
302         IP_FW_NAT_DEL,
303         IP_FW_NAT_GET_CONFIG,
304         IP_FW_NAT_GET_LOG,
305
306         IP_DUMMYNET_CONFIGURE,
307         IP_DUMMYNET_DEL ,
308         IP_DUMMYNET_FLUSH,
309         /* 63 is missing */
310         IP_DUMMYNET_GET         = _IPFW_SOCKOPT_BASE + 24,
311         _IPFW_SOCKOPT_END
312 };
313 #endif /* IP_FW_TABLE_ADD */
314
315 #endif /* !_GLUE_H */