2 /* $NetBSD: ipsec_osdep.h,v 1.1 2003/08/13 20:06:51 jonathan Exp $ */
5 * Copyright (c) 2003 Jonathan Stone (jonathan@cs.stanford.edu)
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #ifndef NETIPSEC_OSDEP_H
30 #define NETIPSEC_OSDEP_H
33 * Hide porting differences across different 4.4BSD-derived platforms.
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.
48 * 1. KASSERT and spl differences
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.
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).
64 #if __FreeBSD_version < 500000
65 #define IPSEC_SPLASSERT(_x,_y) SPLASSERT(_x, _y)
67 #define IPSEC_SPLASSERT(_x,_y)
69 #define IPSEC_SPLASSERT_SOFTNET(_m) IPSEC_SPLASSERT(net,_m)
70 #define IPSEC_ASSERT(_c,_m) KASSERT(_c, _m)
71 #endif /* __FreeBSD__ */
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__ */
80 * 2. Kernel Randomness API.
82 * u_int read_random(void *outbuf, int nbytes).
85 #include <sys/random.h>
86 /* do nothing, use native random code. */
87 #endif /* __FreeBSD__ */
91 static __inline u_int read_random(void *p, u_int len);
94 read_random(void *bufp, u_int len)
96 return rnd_extract_data(bufp, len, RND_EXTRACT_ANY /*XXX FIXME */);
98 #endif /* __NetBSD__ */
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.
108 /* XXX wrong, but close enough for restricted ipsec usage. */
109 #define M_EXT_WRITABLE(m) (!M_READONLY(m))
110 #endif /* __NetBSD__ */
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..
124 #define M_MOVE_PKTHDR(_f, _t) M_COPY_PKTHDR(_f, _t)
125 #endif /* __NetBSD__ */
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.
138 static __inline struct mbuf *
139 m_getcl(int how, short type, int flags)
142 if (flags & M_PKTHDR)
143 MGETHDR(mp, how, type);
152 #endif /* __NetBSD__ */
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.
164 #endif /* __FreeBSD__ */
166 #define IF_HANDOFF(ifq, m, f) if_handoff(ifq, m, f, 0)
171 if_handoff(struct ifqueue *ifq, struct mbuf *m, struct ifnet *ifp, int adjust)
173 int need_if_start = 0;
183 ifp->if_obytes += m->m_pkthdr.len + adjust;
184 if (m->m_flags & M_MCAST)
186 need_if_start = !(ifp->if_flags & IFF_OACTIVE);
190 (*ifp->if_start)(ifp);
194 #endif /* __NetBSD__ */
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.
202 #include <sys/kernel.h>
203 #define time_second mono_time.tv_sec
204 #endif /* __NetBSD__ */
208 #include <sys/protosw.h>
209 #define ipprotosw protosw
210 #endif /* __NetBSD__ */
213 * 8. Test for "privileged" socket opened by superuser.
214 * FreeBSD tests ((so)->so_cred != NULL && priv_check_cred((so)->so_cred,
215 * PRIV_NETINET_IPSEC, 0) == 0).
216 * NetBSD (1.6N) tests (so)->so_uid == 0).
217 * This difference is wrapped inside the IPSEC_PRIVILEGED_SO() macro.
220 #define IPSEC_IS_PRIVILEGED_SO(_so) \
221 ((_so)->so_cred != NULL && \
222 priv_check_cred((_so)->so_cred, PRIV_NETINET_IPSEC, 0) \
224 #endif /* __FreeBSD__ */
227 /* superuser opened socket? */
228 #define IPSEC_IS_PRIVILEGED_SO(so) ((so)->so_uid == 0)
229 #endif /* __NetBSD__ */
233 * FreeBSD uses: listhead = rawcb_list, SLIST()-next field "list".
234 * NetBSD uses: listhead = rawcb, SLIST()-next field "list"
236 * This version of fast-ipsec source code uses rawcb_list as the head,
237 * and (to avoid namespace collisions) uses rcb_list as the "next" field.
240 #define rcb_list list
241 #endif /* __FreeBSD__ */
243 #define rawcb_list rawcb
244 #endif /* __NetBSD__ */
248 * 10. List of all known network interfaces.
249 * FreeBSD has listhead in_ifaddread, with ia_link as link.
250 * NetBSD has listhead in_ifaddr, with ia_list as link.
251 * No name-clahses, so just #define the appropriate names on NetBSD.
252 * NB: Is it worth introducing iterator (find-first-list/find-next-list)
253 * functions or macros to encapsulate these?
256 /* nothing to do for raw interface list */
259 /* For now, use FreeBSD-compatible names for raw interface list. */
260 #define in_ifaddrhead in_ifaddr
261 #define ia_link ia_list
262 #endif /* __NetBSD__ */
268 * Differences that we don't attempt to hide:
270 * A. Initialization code. This is the largest difference of all.
272 * FreeBSD uses compile/link-time perl hackery to generate special
273 * .o files with linker sections that give the moral equivalent of
274 * C++ file-level-object constructors. NetBSD has no such facility.
276 * Either we implement it (ideally, in a way that can emulate
277 * FreeBSD's SYSINIT() macros), or we must take other means
278 * to have the per-file init functions called at some appropriate time.
280 * In the absence of SYSINIT(), all the file-level init functions
281 * now have "extern" linkage. There is a new fast-ipsec init()
282 * function which calls each of the per-file in an appropriate order.
283 * init_main will arrange to call the fast-ipsec init function
284 * after the crypto framework has registered its transforms (including
285 * any autoconfigured hardware crypto accelerators) but before
286 * initializing the network stack to send or receive packet.
288 * B. Protosw() differences.
289 * CSRG-style BSD TCP/IP uses a generic protocol-dispatch-function
290 * where the specific request is identified by an enum argument.
291 * FreeBSD replaced that with an array of request-specific
294 * These differences affect the handlers for key-protocol user requests
295 * so pervasively that I gave up on the fast-ipsec code, and re-worked the
296 * NetBSD KAME code to match the (relative few) API differences
297 * between NetBSD and FreeBSD's KAME netkey, and Fast-IPsec netkey.
299 * C. Timeout() versus callout(9):
300 * The FreeBSD 4.x netipsec/ code still uses timeout().
301 * FreeBSD 4.7 has callout(9), so I just replaced
302 * timeout_*() with the nearest callout_*() equivalents,
303 * and added a callout handle to the ipsec context.
305 * D. SPL name differences.
306 * FreeBSD splnet() equates directly to NetBSD's splsoftnet();
307 * FreeBSD uses splimp() where (for networking) NetBSD would use splnet().
309 #endif /* NETIPSEC_OSDEP_H */