Added spec file comments.
[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$
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
59 #include <netinet/ether.h>
60
61 #else /* KERNEL_MODULE, kernel part */
62
63 #ifndef _WIN32
64 #include <linux/version.h>
65
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
70
71 #include <linux/stddef.h>       /* linux kernel */
72 #include <linux/types.h>        /* linux kernel */
73
74
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 */
78 #endif
79
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>
83 #endif
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>
90 /*
91  * LIST_HEAD in queue.h conflict with linux/list.h
92  * some previous linux include need list.h definition
93  */
94 #undef LIST_HEAD
95
96 #define         IF_NAMESIZE     16
97 typedef uint32_t      in_addr_t;
98
99 #define printf(fmt, arg...) printk(KERN_ERR fmt, ##arg)
100
101 #endif  /* !_WIN32 */
102 #endif /* KERNEL_MODULE */
103
104 /*
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.
108  */
109
110 struct thread {
111         void *sopt_td;
112         void *td_ucred;
113 };
114
115 struct  sockopt {
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 */
122 };
123
124
125 /* This must be included here after list.h */
126 #include <sys/queue.h>          /* both the kernel side and nat.c needs this */
127
128 #ifndef KERNEL_MODULE
129
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
135
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 */
139
140 /* defined in freebsd netinet/icmp6.h */
141 #define ICMP6_MAXTYPE                   201
142
143 /* on freebsd sys/socket.h pf specific */
144 #define NET_RT_IFLIST   3               /* survey interface list */
145
146 /* on freebsd net/if.h XXX used */
147 struct if_data {
148
149         /* ... */
150         u_long ifi_mtu;                /* maximum transmission unit */   
151 };
152
153 /*
154  * Message format for use in obtaining information about interfaces
155  * from getkerninfo and the routing socket.
156  * This is used in nat.c
157  */
158 struct if_msghdr {
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 */
166 };
167
168 /*
169  * Message format for use in obtaining information about interface addresses
170  * from getkerninfo and the routing socket
171  */
172 struct ifa_msghdr {
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 */
180 };
181
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 */
188 #endif  /* NO_RTM */
189
190 /* SA_SIZE is used in the userland nat.c modified */
191 #define SA_SIZE(sa)                                             \
192     (  (!(sa) ) ?      \
193         sizeof(long)            :                               \
194         1 + ( (sizeof(struct sockaddr) - 1) | (sizeof(long) - 1) ) )
195
196 /* sys/time.h */
197 /*
198  * Getkerninfo clock information structure
199  */
200 struct clockinfo {
201         int     hz;             /* clock frequency */
202         int     tick;           /* micro-seconds per hz tick */
203         int     spare;
204         int     stathz;         /* statistics clock frequency */
205         int     profhz;         /* profiling clock frequency */
206 };
207
208 /*
209  * linux does not have a reentrant version of qsort,
210  * so we the FreeBSD stdlib version.
211  */
212 void
213 qsort_r(void *a, size_t n, size_t es, void *thunk,
214         int cmp_t(void *, const void *, const void *));
215
216 #define setprogname(x)  /* not present in linux */
217
218 extern int optreset;    /* not present in linux */
219
220 size_t strlcpy(char * dst, const char * src, size_t siz);
221 long long int
222 strtonum(const char *nptr, long long minval, long long maxval,
223          const char **errstr);
224  
225 int
226 sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp,
227          size_t newlen);
228  
229 #else /* KERNEL_MODULE */
230
231 /* linux and windows kernel do not have bcopy ? */
232 #define bcopy(_s, _d, _l)       memcpy(_d, _s, _l)
233
234 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17) // or 2.4.x
235 #include <linux/in6.h>
236 #endif
237
238 /* definitions useful for the kernel side */
239
240 struct route_in6 { };
241
242 #endif  /* KERNEL_MODULE */
243
244 /*
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.
249  */
250 #ifndef IP_FW_TABLE_ADD
251 #define _IPFW_SOCKOPT_BASE      100     /* 40 on freebsd */
252 enum ipfw_msg_type {
253         IP_FW_TABLE_ADD         = _IPFW_SOCKOPT_BASE,
254         IP_FW_TABLE_DEL,
255         IP_FW_TABLE_FLUSH,
256         IP_FW_TABLE_GETSIZE,
257         IP_FW_TABLE_LIST,
258
259         IP_FW_ADD               = _IPFW_SOCKOPT_BASE + 10,
260         IP_FW_DEL,
261         IP_FW_FLUSH,
262         IP_FW_ZERO,
263         IP_FW_GET,
264         IP_FW_DYN_GET,
265         IP_FW_RESETLOG,
266
267         IP_FW_NAT_CFG,
268         IP_FW_NAT_DEL,
269         IP_FW_NAT_GET_CONFIG,
270         IP_FW_NAT_GET_LOG,
271
272         IP_DUMMYNET_CONFIGURE,
273         IP_DUMMYNET_DEL ,
274         IP_DUMMYNET_FLUSH,
275         /* 63 is missing */
276         IP_DUMMYNET_GET         = _IPFW_SOCKOPT_BASE + 24,
277         _IPFW_SOCKOPT_END
278 };
279 #endif /* IP_FW_TABLE_ADD */
280
281 #endif /* !_GLUE_H */