1/*	$NetBSD: net_osdep.h,v 1.17 2007/03/04 06:03:17 christos Exp $	*/
2/*	$KAME: net_osdep.h,v 1.51 2001/07/06 06:21:43 itojun Exp $	*/
3
4/*
5 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the project nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32/*
33 * glue for kernel code programming differences.
34 */
35
36/*
37 * OS dependencies:
38 * - RTFREE()
39 *   bsdi does not escape this macro using do-clause, so it is recommended
40 *   to escape the macro explicitly.
41 *   e.g.
42 *	if (rt) {
43 *		RTFREE(rt);
44 *	}
45 *
46 * - whether the IPv4 input routine convert the byte order of some fileds
47 *   of the IP header (x: convert to the host byte order, s: strip the header
48 *   length for possible reassembly)
49 *             ip_len ip_id ip_off
50 * bsdi3:          xs     x      x
51 * bsdi4:          xs            x
52 * freebsd[23]:    xs     x      x
53 * freebsd4:       xs            x
54 * NetBSD:
55 * OpenBSD:        xs            x
56 *
57 * - ifa_ifwithaf()
58 *   bsdi[34], netbsd, and openbsd define it in sys/net/if.c
59 *   freebsd (all versions) does not have it.
60 *
61 * - struct rt_addrinfo
62 *   bsdi4, netbsd 1.5R and beyond: rti_addrs, rti_info[], rti_flags, rti_ifa,
63 *	rti_ifp, and rti_rtm.
64 *   others: rti_addrs and rti_info[] only.
65 *
66 * - ifa->ifa_rtrequest
67 *   bsdi4, netbsd 1.5R and beyond: rt_addrinfo *
68 *   others: sockaddr * (note that sys/net/route.c:rtrequest() has an unsafe
69 *	typecast code, from 4.3BSD-reno)
70 *
71 * - side effects of rtrequest{,1}(RTM_DELETE)
72 *	BSDI[34]: delete all cloned routes underneath the route.
73 *	FreeBSD[234]: delete all protocol-cloned routes underneath the route.
74 *		      note that cloned routes from an interface direct route
75 *		      still remain.
76 *	NetBSD: 1.5 have no side effects.  KAME/netbsd15, and post-1.5R, have
77 *		the same effects as of BSDI.
78 *	OpenBSD: have no side effects.  KAME/openbsd has the same effects as
79 *		of BSDI (the change is not merged - yet).
80 *
81 * - privileged process
82 *	NetBSD 2, 3
83 *		struct lwp *l;
84 *		if (l->l_proc &&
85 *		    !suser(l->l_proc->p_ucred, &l->l_proc->p_acflag))
86 *			privileged
87 *	NetBSD >= 4
88 *		below is the generic authorization call, please see kauth(9)
89 *		for more specific alternatives (for proper integration with
90 *		secmodels)
91 *
92 *		struct lwp *l;
93 *		if (l != NULL && kauth_authorize_generic(l->l_cred,
94 *		    KAUTH_GENERIC_ISSUSER, NULL) == 0)
95 *			privileged;
96 *	NetBSD < 2, FreeBSD 3
97 *		struct proc *p;
98 *		if (p && !suser(p->p_ucred, &p->p_acflag))
99 *			privileged;
100 *	FreeBSD 4
101 *		struct proc *p;
102 *		if (p && !suser(p))
103 *			privileged;
104 *	OpenBSD, BSDI [34], FreeBSD 2
105 *		struct socket *so;
106 *		if (so->so_state & SS_PRIV)
107 *			privileged;
108 * - foo_control
109 *	NetBSD, FreeBSD 3
110 *		needs to give struct proc * as argument
111 *	OpenBSD, BSDI [34], FreeBSD 2
112 *		do not need struct proc *
113 *
114 * - bpf:
115 *	OpenBSD, NetBSD 1.5, BSDI [34]
116 *		need void ** (= if_bpf **) and struct ifnet *
117 *	FreeBSD 2, FreeBSD 3, NetBSD post-1.5N
118 *		need only struct ifnet * as argument
119 *
120 * - struct ifnet
121 *			use queue.h?	member names	if name
122 *			---		---		---
123 *	FreeBSD 2	no		old standard	if_name+unit
124 *	FreeBSD 3	yes		strange		if_name+unit
125 *	OpenBSD		yes		standard	if_xname
126 *	NetBSD		yes		standard	if_xname
127 *	BSDI [34]	no		old standard	if_name+unit
128 *
129 * - usrreq
130 *	NetBSD, OpenBSD, BSDI [34], FreeBSD 2
131 *		single function with PRU_xx, arguments are mbuf
132 *	FreeBSD 3
133 *		separates functions, non-mbuf arguments
134 *
135 * - {set,get}sockopt
136 *	NetBSD, OpenBSD, BSDI [34], FreeBSD 2
137 *		manipulation based on mbuf
138 *	FreeBSD 3
139 *		non-mbuf manipulation using sooptcopy{in,out}()
140 *
141 * - timeout() and untimeout()
142 *	NetBSD 1.4.x, OpenBSD, BSDI [34], FreeBSD 2
143 *		timeout() is a void function
144 *	FreeBSD 3
145 *		timeout() is non-void, must keep returned value for untimeout()
146 *		callout_xx is also available (sys/callout.h)
147 *	NetBSD 1.5
148 *		timeout() is obsoleted, use callout_xx (sys/callout.h)
149 *	OpenBSD 2.8
150 *		timeout_{add,set,del} is encouraged (sys/timeout.h)
151 *
152 * - kernel internal time structure
153 *	FreeBSD 2, NetBSD, OpenBSD, BSD/OS
154 *		mono_time.tv_u?sec, time.tv_u?sec
155 *	FreeBSD [34]
156 *		time_second
157 *	if you need portability, #ifdef out FreeBSD[34], or use microtime(&tv)
158 *	then touch tv.tv_sec.
159 *
160 * - sysctl
161 *	NetBSD, OpenBSD
162 *		foo_sysctl()
163 *	BSDI [34]
164 *		foo_sysctl() but with different style.  sysctl_int_arr() takes
165 *		care of most of the cases.
166 *	FreeBSD
167 *		linker hack.  however, there are freebsd version differences
168 *		(how wonderful!).
169 *		on FreeBSD[23] function arg #define includes paren.
170 *			int foo SYSCTL_HANDLER_ARGS;
171 *		on FreeBSD4, function arg #define does not include paren.
172 *			int foo(SYSCTL_HANDLER_ARGS);
173 *		on some versions, forward reference to the tree is okay.
174 *		on some versions, you need SYSCTL_DECL().  you need things
175 *		like this.
176 *			#ifdef SYSCTL_DECL
177 *			SYSCTL_DECL(net_inet_ip6);
178 *			#endif
179 *		it is hard to share functions between freebsd and non-freebsd.
180 *
181 * - if_ioctl
182 *	NetBSD, FreeBSD 3, BSDI [34]
183 *		2nd argument is u_long cmd
184 *	FreeBSD 2
185 *		2nd argument is int cmd
186 *
187 * - if attach routines
188 *	NetBSD
189 *		void xxattach(int);
190 *	FreeBSD 2, FreeBSD 3
191 *		void xxattach(void *);
192 *		PSEUDO_SET(xxattach, if_xx);
193 *
194 * - ovbcopy()
195 *	in NetBSD 1.4 or later, ovbcopy() is not supplied in the kernel.
196 *	we have updated sys/systm.h to include declaration.
197 *
198 * - splnet()
199 *	NetBSD 1.4 or later requires splsoftnet().
200 *	other operating systems use splnet().
201 *
202 * - splimp()
203 *	NetBSD-current (2001/4/13): use splnet() in network, splvm() in vm.
204 *	other operating systems: use splimp().
205 *
206 * - dtom()
207 *	NEVER USE IT!
208 *
209 * - struct ifnet for loopback interface
210 *	BSDI3: struct ifnet loif;
211 *	BSDI4: struct ifnet *loifp;
212 *	NetBSD 2.0, OpenBSD 2.8, FreeBSD2: struct ifnet loif[NLOOP];
213 *	NetBSD 3.0, OpenBSD 2.9: struct ifnet *lo0ifp;
214 *
215 *	odd thing is that many of them refers loif as ifnet *loif,
216 *	not loif[NLOOP], from outside of if_loop.c.
217 *
218 * - number of bpf pseudo devices
219 *	others: bpfilter.h, NBPFILTER
220 *	FreeBSD4: bpf.h, NBPF
221 *	solution:
222 *		#if defined(__FreeBSD__) && __FreeBSD__ >= 4
223 *		#include "bpf.h"
224 *		#define NBPFILTER	NBPF
225 *		#else
226 *		#include "bpfilter.h"
227 *		#endif
228 *
229 * - protosw for IPv4 (sys/netinet)
230 *	FreeBSD4: struct ipprotosw in netinet/ipprotosw.h
231 *	others: struct protosw in sys/protosw.h
232 *
233 * - protosw in general.
234 *	NetBSD 1.5 has extra member for ipfilter (netbsd-current dropped
235 *	it so it will go away in 1.6).
236 *	NetBSD 1.5 requires PR_LISTEN flag bit with protocols that permit
237 *	listen/accept (like tcp).
238 *
239 * - header files with defopt (opt_xx.h)
240 *	FreeBSD3: opt_{inet,ipsec,ip6fw,altq}.h
241 *	FreeBSD4: opt_{inet,inet6,ipsec,ip6fw,altq}.h
242 *	NetBSD: opt_{inet,ipsec,altq}.h
243 *	others: does not use defopt
244 *
245 * - IN_MULTICAST/IN_CLASS[A-D] macro.
246 *	OpenBSD and NetBSD: net endian (kernel) or host endian (userland)
247 *	others: always host endian
248 *
249 * - (m->m_flags & M_EXT) != 0 does *not* mean that the max data length of
250 *   the mbuf == MCLBYTES.
251 *
252 * - sys/kern/uipc_mbuf.c:m_dup()
253 *	freebsd[34]: copies the whole mbuf chain.
254 *	netbsd: similar arg with m_copym().
255 *	others: no m_dup().
256 *
257 * - ifa_refcnt (struct ifaddr) management (IFAREF/IFAFREE).
258 *	NetBSD 1.5: always use IFAREF whenever reference gets added.
259 *		always use IFAFREE whenever reference gets freed.
260 *		IFAFREE frees ifaddr when ifa_refcnt reaches 0.
261 *	others: do not increase refcnt for ifp->if_addrlist and in_ifaddr.
262 *		use IFAFREE once when ifaddr is disconnected from
263 *		ifp->if_addrlist and in_ifaddr.  IFAFREE frees ifaddr when
264 *		ifa_refcnt goes negative.  in KAME environment, IFAREF is
265 *		provided as a compatibility wrapper (use it instead of
266 *		ifa_refcnt++ to reduce #ifdef).
267 *
268 * - ifnet.if_lastchange
269 *	freebsd, bsdi, netbsd-current (jun 14 2001-),
270 *	openbsd-current (jun 15 2001-): updated only when IFF_UP changes.
271 *		(RFC1573 ifLastChange interpretation)
272 *	netbsd151, openbsd29: updated whenever packets go through the interface.
273 *		(4.4BSD interpretation)
274 *
275 * - kernel compilation options ("options HOGE" in kernel config file)
276 *	freebsd4: sys/conf/options has to have mapping between option
277 *		and a header file (opt_hoge.h).
278 *	netbsd: by default, -DHOGE will go into
279 *		sys/arch/foo/compile/BAR/Makefile.
280 *		if you define mapping in sys/conf/files, you can create
281 *		a header file like opt_hoge.h to help make dependencies.
282 *	bsdi/openbsd: always use -DHOGE in Makefile.  there's no need/way
283 *		to have opt_hoge.h.
284 *
285 *	therefore, opt_hoge.h is mandatory on freebsd4 only.
286 *
287 * - MALLOC() macro
288 *	Use it only if the size of the allocation is constant.
289 *	When we do NOT collect statistics about kernel memory usage, the result
290 *	of macro expansion contains a large set of condition branches.  If the
291 *	size is not constant, compilation optimization cannot be applied, and
292 *	a bunch of the large branch will be embedded in the kernel code.
293 *
294 * - M_COPY_PKTHDR
295 *	openbsd30: M_COPY_PKTHDR is deprecated.  use M_MOVE_PKTHDR or
296 *		M_DUP_PKTHDR, depending on how you want to handle m_tag.
297 *	others: M_COPY_PKTHDR is available as usual.
298 */
299
300#ifndef _NET_NET_OSDEP_H_
301#define _NET_NET_OSDEP_H_
302#ifdef _KERNEL
303
304#define if_name(ifp)	((ifp)->if_xname)
305
306#if defined(__NetBSD__) && __NetBSD_Version__ >= 104000000
307#define ovbcopy(src, dst, len)	memmove((dst), (src), (len))
308#endif
309
310#endif /*_KERNEL*/
311#endif /* !_NET_NET_OSDEP_H_ */
312