1 /* $FreeBSD: releng/5.3/sys/netipsec/ipsec_osdep.h 120584 2003-09-29 22:47:45Z sam $ */
2 /* $NetBSD: ipsec_osdep.h,v 1.1 2003/08/13 20:06:51 jonathan Exp $ */
3
4 /*
5 * Copyright (c) 2003 Jonathan Stone (jonathan@cs.stanford.edu)
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
20 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #ifndef NETIPSEC_OSDEP_H
30 #define NETIPSEC_OSDEP_H
31
32 /*
33 * Hide porting differences across different 4.4BSD-derived platforms.
34 *
35 * 1. KASSERT() differences:
36 * 2. Kernel Random-number API differences.
37 * 3. Is packet data in an mbuf object writeable?
38 * 4. Packet-header semantics.
39 * 5. Fast mbuf-cluster allocation.
40 * 6. Network packet-output macros.
41 * 7. Elased time, in seconds.
42 * 8. Test if a socket object opened by a privileged (super) user.
43 * 9. Global SLIST of all open raw sockets.
44 * 10. Global SLIST of known interface addresses.
45 */
46
47 /*
48 * 1. KASSERT and spl differences
49 *
50 * FreeBSD takes an expression and parenthesized printf() argument-list.
51 * NetBSD takes one arg: the expression being asserted.
52 * FreeBSD's SPLASSERT() takes an SPL level as 1st arg and a
53 * parenthesized printf-format argument list as the second argument.
54 *
55 * This difference is hidden by two 2-argument macros and one 1-arg macro:
56 * IPSEC_ASSERT(expr, msg)
57 * IPSEC_SPLASSERT(spl, msg)
58 * One further difference is the spl names:
59 * NetBSD splsoftnet equates to FreeBSD splnet;
60 * NetBSD splnet equates to FreeBSD splimp.
61 * which is hidden by the macro IPSEC_SPLASSERT_SOFTNET(msg).
62 */
63 #ifdef __FreeBSD__
64 #if __FreeBSD_version < 500000
65 #define IPSEC_SPLASSERT(_x,_y) SPLASSERT(_x, _y)
66 #else
67 #define IPSEC_SPLASSERT(_x,_y)
68 #endif
69 #define IPSEC_SPLASSERT_SOFTNET(_m) IPSEC_SPLASSERT(net,_m)
70 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
71 #endif /* __FreeBSD__ */
72
73 #ifdef __NetBSD__
74 #define IPSEC_SPLASSERT(x,y) (void)0
75 #define IPSEC_ASSERT(c,m) KASSERT(c)
76 #define IPSEC_SPLASSERT_SOFTNET(m) IPSEC_SPLASSERT(softnet, m)
77 #endif /* __NetBSD__ */
78
79 /*
80 * 2. Kernel Randomness API.
81 * FreeBSD uses:
82 * u_int read_random(void *outbuf, int nbytes).
83 */
84 #ifdef __FreeBSD__
85 #include <sys/random.h>
86 /* do nothing, use native random code. */
87 #endif /* __FreeBSD__ */
88
89 #ifdef __NetBSD__
90 #include <sys/rnd.h>
91 static __inline u_int read_random(void *p, u_int len);
92
93 static __inline u_int
94 read_random(void *bufp, u_int len)
95 {
96 return rnd_extract_data(bufp, len, RND_EXTRACT_ANY /*XXX FIXME */);
97 }
98 #endif /* __NetBSD__ */
99
100 /*
101 * 3. Test for mbuf mutability
102 * FreeBSD 4.x uses: M_EXT_WRITABLE
103 * NetBSD has M_READONLY(). Use !M_READONLY().
104 * Not an exact match to FreeBSD semantics, but adequate for IPsec purposes.
105 *
106 */
107 #ifdef __NetBSD__
108 /* XXX wrong, but close enough for restricted ipsec usage. */
109 #define M_EXT_WRITABLE(m) (!M_READONLY(m))
110 #endif /* __NetBSD__ */
111
112 /*
113 * 4. mbuf packet-header/packet-tag semantics.
114 * Sam Leffler explains, in private email, some problems with
115 * M_COPY_PKTHDR(), and why FreeBSD deprecated it and replaced it
116 * with new, explicit macros M_MOVE_PKTHDR()/M_DUP_PKTHDR().
117 * he original fast-ipsec source uses M_MOVE_PKTHDR.
118 * NetBSD currently still uses M_COPY_PKTHDR(), so we define
119 * M_MOVE_PKTHDR in terms of M_COPY_PKTHDR(). Fast-IPsec
120 * will delete the source mbuf shortly after copying packet tags,
121 * so we are safe for fast-ipsec but not in general..
122 */
123 #ifdef __NetBSD__
124 #define M_MOVE_PKTHDR(_f, _t) M_COPY_PKTHDR(_f, _t)
125 #endif /* __NetBSD__ */
126
127
128 /*
129 * 5. Fast mbuf-cluster allocation.
130 * FreeBSD 4.6 introduce m_getcl(), which performs `fast' allocation
131 * mbuf clusters from a cache of recently-freed clusters. (If the cache
132 * is empty, new clusters are allocated en-masse).
133 * On NetBSD, for now, implement the `cache' as an inline function
134 *using normal NetBSD mbuf/cluster allocation macros. Replace this
135 * with fast-cache code, if and when netBSD implements one.
136 */
137 #ifdef __NetBSD__
138 static __inline struct mbuf *
139 m_getcl(int how, short type, int flags)
140 {
141 struct mbuf *mp;
142 if (flags & M_PKTHDR)
143 MGETHDR(mp, how, type);
144 else
145 MGET(mp, how, type);
146 if (mp == NULL)
147 return NULL;
148
149 MCLGET(mp, how);
150 return mp;
151 }
152 #endif /* __NetBSD__ */
153
154 /*
155 * 6. Network output macros
156 * FreeBSD uses the IF_HANDOFF(), which raises SPL, enqueues
157 * a packet, and updates interface counters. NetBSD has IFQ_ENQUE(),
158 * which leaves SPL changes up to the caller.
159 * For now, we provide an emulation of IF_HANOOFF() which works
160 * for protocol input queues.
161 */
162 #ifdef __FreeBSD__
163 /* nothing to do */
164 #endif /* __FreeBSD__ */
165 #ifdef __NetBSD__
166 #define IF_HANDOFF(ifq, m, f) if_handoff(ifq, m, f, 0)
167
168 #include <net/if.h>
169
170 static __inline int
171 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
172 {
173 int need_if_start = 0;
174 int s = splnet();
175
176 if (IF_QFULL(ifq)) {
177 IF_DROP(ifq);
178 splx(s);
179 m_freem(m);
180 return (0);
181 }
182 if (ifp != NULL) {
183 ifp->if_obytes += m->m_pkthdr.len + adjust;
184 if (m->m_flags & M_MCAST)
185 ifp->if_omcasts++;
186 need_if_start = !(ifp->if_flags & IFF_OACTIVE);
187 }
188 IF_ENQUEUE(ifq, m);
189 if (need_if_start)
190 (*ifp->if_start)(ifp);
191 splx(s);
192 return (1);
193 }
194 #endif /* __NetBSD__ */
195
196 /*
197 * 7. Elapsed Time: time_second as time in seconds.
198 * Original FreeBSD fast-ipsec code references a FreeBSD kernel global,
199 * time_second(). NetBSD: kludge #define to use time_mono_time.tv_sec.
200 */
201 #ifdef __NetBSD__
202 #include <sys/kernel.h>
203 #define time_second mono_time.tv_sec
204 #endif /* __NetBSD__ */
205
206 /* protosw glue */
207 #ifdef __NetBSD__
208 #include <sys/protosw.h>
209 #define ipprotosw protosw
210 #endif /* __NetBSD__ */
211
212 /*
213 * 8. Test for "privileged" socket opened by superuser.
214 * FreeBSD tests ((so)->so_cred && (so)->so_cred.cr_uid == 0),
215 * NetBSD (1.6N) tests (so)->so_uid == 0).
216 * This difference is wrapped inside the IPSEC_PRIVILEGED_SO() macro.
217 *
218 */
219 #ifdef __FreeBSD__
220 #define IPSEC_IS_PRIVILEGED_SO(_so) \
221 ((_so)->so_cred && (_so)->so_cred->cr_uid == 0)
222 #endif /* __FreeBSD__ */
223
224 #ifdef __NetBSD__
225 /* superuser opened socket? */
226 #define IPSEC_IS_PRIVILEGED_SO(so) ((so)->so_uid == 0)
227 #endif /* __NetBSD__ */
228
229 /*
230 * 9. Raw socket list
231 * FreeBSD uses: listhead = rawcb_list, SLIST()-next field "list".
232 * NetBSD uses: listhead = rawcb, SLIST()-next field "list"
233 *
234 * This version of fast-ipsec source code uses rawcb_list as the head,
235 * and (to avoid namespace collisions) uses rcb_list as the "next" field.
236 */
237 #ifdef __FreeBSD__
238 #define rcb_list list
239 #endif /* __FreeBSD__ */
240 #ifdef __NetBSD__
241 #define rawcb_list rawcb
242 #endif /* __NetBSD__ */
243
244
245 /*
246 * 10. List of all known network interfaces.
247 * FreeBSD has listhead in_ifaddread, with ia_link as link.
248 * NetBSD has listhead in_ifaddr, with ia_list as link.
249 * No name-clahses, so just #define the appropriate names on NetBSD.
250 * NB: Is it worth introducing iterator (find-first-list/find-next-list)
251 * functions or macros to encapsulate these?
252 */
253 #ifdef __FreeBSD__
254 /* nothing to do for raw interface list */
255 #endif /* FreeBSD */
256 #ifdef __NetBSD__
257 /* For now, use FreeBSD-compatible names for raw interface list. */
258 #define in_ifaddrhead in_ifaddr
259 #define ia_link ia_list
260 #endif /* __NetBSD__ */
261
262
263
264
265 /*
266 * Differences that we don't attempt to hide:
267 *
268 * A. Initialization code. This is the largest difference of all.
269 *
270 * FreeBSD uses compile/link-time perl hackery to generate special
271 * .o files with linker sections that give the moral equivalent of
272 * C++ file-level-object constructors. NetBSD has no such facility.
273 *
274 * Either we implement it (ideally, in a way that can emulate
275 * FreeBSD's SYSINIT() macros), or we must take other means
276 * to have the per-file init functions called at some appropriate time.
277 *
278 * In the absence of SYSINIT(), all the file-level init functions
279 * now have "extern" linkage. There is a new fast-ipsec init()
280 * function which calls each of the per-file in an appropriate order.
281 * init_main will arrange to call the fast-ipsec init function
282 * after the crypto framework has registered its transforms (including
283 * any autoconfigured hardware crypto accelerators) but before
284 * initializing the network stack to send or receive packet.
285 *
286 * B. Protosw() differences.
287 * CSRG-style BSD TCP/IP uses a generic protocol-dispatch-function
288 * where the specific request is identified by an enum argument.
289 * FreeBSD replaced that with an array of request-specific
290 * function pointers.
291 *
292 * These differences affect the handlers for key-protocol user requests
293 * so pervasively that I gave up on the fast-ipsec code, and re-worked the
294 * NetBSD KAME code to match the (relative few) API differences
295 * between NetBSD and FreeBSD's KAME netkey, and Fast-IPsec netkey.
296 *
297 * C. Timeout() versus callout(9):
298 * The FreeBSD 4.x netipsec/ code still uses timeout().
299 * FreeBSD 4.7 has callout(9), so I just replaced
300 * timeout_*() with the nearest callout_*() equivalents,
301 * and added a callout handle to the ipsec context.
302 *
303 * D. SPL name differences.
304 * FreeBSD splnet() equates directly to NetBSD's splsoftnet();
305 * FreeBSD uses splimp() where (for networking) NetBSD would use splnet().
306 */
307 #endif /* NETIPSEC_OSDEP_H */
Cache object: b41a5d134fb2ec49e1934ac1fea9f56b
|