ip_compat.h revision 60857
11553Srgrimes/*
21553Srgrimes * Copyright (C) 1993-2000 by Darren Reed.
31553Srgrimes *
41553Srgrimes * Redistribution and use in source and binary forms are permitted
51553Srgrimes * provided that this notice is preserved and due credit is given
61553Srgrimes * to the original author and the contributors.
71553Srgrimes *
81553Srgrimes * @(#)ip_compat.h	1.8 1/14/96
91553Srgrimes * $Id: ip_compat.h,v 2.1.2.3 1999/11/18 13:55:26 darrenr Exp $
101553Srgrimes * $FreeBSD: head/sys/contrib/ipfilter/netinet/ip_compat.h 60857 2000-05-24 04:40:17Z darrenr $
111553Srgrimes */
121553Srgrimes
131553Srgrimes#ifndef	__IP_COMPAT_H__
141553Srgrimes#define	__IP_COMPAT_H__
151553Srgrimes
161553Srgrimes#ifndef	__P
171553Srgrimes# ifdef	__STDC__
181553Srgrimes#  define	__P(x)  x
191553Srgrimes# else
201553Srgrimes#  define	__P(x)  ()
211553Srgrimes# endif
221553Srgrimes#endif
231553Srgrimes#ifndef	__STDC__
241553Srgrimes# undef		const
251553Srgrimes# define	const
261553Srgrimes#endif
271553Srgrimes
281553Srgrimes#ifndef	SOLARIS
291553Srgrimes#define	SOLARIS	(defined(sun) && (defined(__svr4__) || defined(__SVR4)))
301553Srgrimes#endif
311553Srgrimes#if SOLARIS2 >= 8
321553Srgrimes# ifndef	USE_INET6
331553Srgrimes#  define	USE_INET6
341553Srgrimes# endif
3530642Scharnier#endif
361553Srgrimes
3730642Scharnier#if defined(_KERNEL) || defined(KERNEL) || defined(__KERNEL__)
3830642Scharnier# undef	KERNEL
3930830Scharnier# undef	_KERNEL
401553Srgrimes# undef 	__KERNEL__
411553Srgrimes# define	KERNEL
421553Srgrimes# define	_KERNEL
431553Srgrimes# define 	__KERNEL__
441553Srgrimes#endif
451553Srgrimes
461553Srgrimes#if defined(__SVR4) || defined(__svr4__) || defined(__sgi)
471553Srgrimes#define index   strchr
481553Srgrimes# if !defined(KERNEL)
491553Srgrimes#  define	bzero(a,b)	memset(a,0,b)
501553Srgrimes#  define	bcmp		memcmp
511553Srgrimes#  define	bcopy(a,b,c)	memmove(b,a,c)
521553Srgrimes# endif
531553Srgrimes#endif
541553Srgrimes
551553Srgrimes#ifndef offsetof
561553Srgrimes#define offsetof(t,m) (int)((&((t *)0L)->m))
571553Srgrimes#endif
581553Srgrimes
5930830Scharnier#if defined(__sgi) || defined(bsdi)
601553Srgrimesstruct  ether_addr {
611553Srgrimes        u_char  ether_addr_octet[6];
621553Srgrimes};
631553Srgrimes#endif
641553Srgrimes
651553Srgrimes#if defined(__sgi) && !defined(IPFILTER_LKM)
661553Srgrimes# ifdef __STDC__
671553Srgrimes#  define IPL_EXTERN(ep) ipfilter##ep
681553Srgrimes# else
691553Srgrimes#  define IPL_EXTERN(ep) ipfilter/**/ep
701553Srgrimes# endif
711553Srgrimes#else
721553Srgrimes# ifdef __STDC__
731553Srgrimes#  define IPL_EXTERN(ep) ipl##ep
741553Srgrimes# else
751553Srgrimes#  define IPL_EXTERN(ep) ipl/**/ep
761553Srgrimes# endif
771553Srgrimes#endif
781553Srgrimes
791553Srgrimes#ifdef	linux
801553Srgrimes# include <sys/sysmacros.h>
811553Srgrimes#endif
821553Srgrimes#if	SOLARIS
831553Srgrimes# define	MTYPE(m)	((m)->b_datap->db_type)
841553Srgrimes# include	<sys/isa_defs.h>
851553Srgrimes# include	<sys/ioccom.h>
861553Srgrimes# include	<sys/sysmacros.h>
871553Srgrimes# include	<sys/kmem.h>
881553Srgrimes/*
891553Srgrimes * because Solaris 2 defines these in two places :-/
901553Srgrimes */
911553Srgrimes# undef	IPOPT_EOL
921553Srgrimes# undef	IPOPT_NOP
931553Srgrimes# undef	IPOPT_LSRR
941553Srgrimes# undef	IPOPT_RR
951553Srgrimes# undef	IPOPT_SSRR
961553Srgrimes# ifndef	KERNEL
971553Srgrimes#  define	_KERNEL
981553Srgrimes#  undef	RES_INIT
991553Srgrimes#  if SOLARIS2 >= 8
1001553Srgrimes#   include <netinet/ip6.h>
1011553Srgrimes#  endif
1021553Srgrimes#  include <inet/common.h>
1031553Srgrimes#  include <inet/ip.h>
1041553Srgrimes#  include <inet/ip_ire.h>
1051553Srgrimes#  undef	_KERNEL
1061553Srgrimes# else /* _KERNEL */
1071553Srgrimes#  if SOLARIS2 >= 8
1081553Srgrimes#   include <netinet/ip6.h>
1091553Srgrimes#  endif
1101553Srgrimes#  include <inet/common.h>
1111553Srgrimes#  include <inet/ip.h>
1121553Srgrimes#  include <inet/ip_ire.h>
1131553Srgrimes# endif /* _KERNEL */
1141553Srgrimes# if SOLARIS2 >= 8
1151553Srgrimes#  include <inet/ip_if.h>
1161553Srgrimes#  include <netinet/ip6.h>
1171553Srgrimes#  define	ipif_local_addr	ipif_lcl_addr
1181553Srgrimes/* Only defined in private include file */
1191553Srgrimes#  ifndef	V4_PART_OF_V6
1201553Srgrimes#   define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
1211553Srgrimes#  endif
1221553Srgrimes# endif
1231553Srgrimes#else
1241553Srgrimes# if !defined(__sgi)
1251553Srgrimestypedef	 int	minor_t;
1261553Srgrimes#endif
1271553Srgrimes#endif /* SOLARIS */
1281553Srgrimes#define	IPMINLEN(i, h)	((i)->ip_len >= ((i)->ip_hl * 4 + sizeof(struct h)))
1291553Srgrimes
1301553Srgrimes#ifndef	IP_OFFMASK
1311553Srgrimes#define	IP_OFFMASK	0x1fff
1321553Srgrimes#endif
133
134#if	BSD > 199306
135# define	USE_QUAD_T
136# define	U_QUAD_T	u_quad_t
137# define	QUAD_T		quad_t
138#else /* BSD > 199306 */
139# define	U_QUAD_T	u_long
140# define	QUAD_T		long
141#endif /* BSD > 199306 */
142
143
144/*
145 * These operating systems already take care of the problem for us.
146 */
147#if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__) || \
148    defined(__sgi)
149typedef u_int32_t       u_32_t;
150# if defined(_KERNEL) && !defined(IPFILTER_LKM)
151#  if defined(__NetBSD_Version__) && (__NetBSD_Version__ >= 104110000)
152#   include "opt_inet.h"
153#  endif
154#  if defined(__FreeBSD_version) && (__FreeBSD_version >= 400000) && \
155      !defined(KLD_MODULE)
156#   include "opt_inet6.h"
157#  endif
158#  ifdef INET6
159#   define USE_INET6
160#  endif
161# endif
162#else
163/*
164 * Really, any arch where sizeof(long) != sizeof(int).
165 */
166# if defined(__alpha__) || defined(__alpha) || defined(_LP64)
167typedef unsigned int    u_32_t;
168# else
169#  if SOLARIS2 >= 6
170typedef	uint32_t	u_32_t;
171#  else
172typedef unsigned int	u_32_t;
173#  endif
174# endif
175#endif /* __NetBSD__ || __OpenBSD__ || __FreeBSD__ || __sgi */
176
177#ifdef	USE_INET6
178# if defined(__NetBSD__) || defined(__OpenBSD__) || defined(__FreeBSD__)
179#  include <netinet/ip6.h>
180#  ifdef	_KERNEL
181#   include <netinet6/ip6_var.h>
182#  endif
183typedef	struct ip6_hdr	ip6_t;
184# endif
185union	i6addr	{
186	u_32_t	i6[4];
187	struct	in_addr	in4;
188	struct	in6_addr in6;
189};
190#else
191union	i6addr	{
192	u_32_t	i6[4];
193	struct	in_addr	in4;
194};
195#endif
196
197#define	IP6CMP(a,b)	bcmp((char *)&(a), (char *)&(b), sizeof(a))
198#define	IP6EQ(a,b)	(bcmp((char *)&(a), (char *)&(b), sizeof(a)) == 0)
199#define	IP6NEQ(a,b)	(bcmp((char *)&(a), (char *)&(b), sizeof(a)) != 0)
200
201#ifndef	MAX
202#define	MAX(a,b)	(((a) > (b)) ? (a) : (b))
203#endif
204
205/*
206 * Security Options for Intenet Protocol (IPSO) as defined in RFC 1108.
207 *
208 * Basic Option
209 *
210 * 00000001   -   (Reserved 4)
211 * 00111101   -   Top Secret
212 * 01011010   -   Secret
213 * 10010110   -   Confidential
214 * 01100110   -   (Reserved 3)
215 * 11001100   -   (Reserved 2)
216 * 10101011   -   Unclassified
217 * 11110001   -   (Reserved 1)
218 */
219#define	IPSO_CLASS_RES4		0x01
220#define	IPSO_CLASS_TOPS		0x3d
221#define	IPSO_CLASS_SECR		0x5a
222#define	IPSO_CLASS_CONF		0x96
223#define	IPSO_CLASS_RES3		0x66
224#define	IPSO_CLASS_RES2		0xcc
225#define	IPSO_CLASS_UNCL		0xab
226#define	IPSO_CLASS_RES1		0xf1
227
228#define	IPSO_AUTH_GENSER	0x80
229#define	IPSO_AUTH_ESI		0x40
230#define	IPSO_AUTH_SCI		0x20
231#define	IPSO_AUTH_NSA		0x10
232#define	IPSO_AUTH_DOE		0x08
233#define	IPSO_AUTH_UN		0x06
234#define	IPSO_AUTH_FTE		0x01
235
236/*
237 * IP option #defines
238 */
239/*#define	IPOPT_RR	7 */
240#define	IPOPT_ZSU	10	/* ZSU */
241#define	IPOPT_MTUP	11	/* MTUP */
242#define	IPOPT_MTUR	12	/* MTUR */
243#define	IPOPT_ENCODE	15	/* ENCODE */
244/*#define	IPOPT_TS	68 */
245#define	IPOPT_TR	82	/* TR */
246/*#define	IPOPT_SECURITY	130 */
247/*#define	IPOPT_LSRR	131 */
248#define	IPOPT_E_SEC	133	/* E-SEC */
249#define	IPOPT_CIPSO	134	/* CIPSO */
250/*#define	IPOPT_SATID	136 */
251#ifndef	IPOPT_SID
252# define	IPOPT_SID	IPOPT_SATID
253#endif
254/*#define	IPOPT_SSRR	137 */
255#define	IPOPT_ADDEXT	147	/* ADDEXT */
256#define	IPOPT_VISA	142	/* VISA */
257#define	IPOPT_IMITD	144	/* IMITD */
258#define	IPOPT_EIP	145	/* EIP */
259#define	IPOPT_FINN	205	/* FINN */
260
261
262#if defined(__FreeBSD__) && (defined(KERNEL) || defined(_KERNEL))
263# if __FreeBSD__ < 3
264#  include <machine/spl.h>
265# else
266#  if __FreeBSD__ == 3
267#   if defined(IPFILTER_LKM) && !defined(ACTUALLY_LKM_NOT_KERNEL)
268#    define	ACTUALLY_LKM_NOT_KERNEL
269#   endif
270#  endif
271# endif
272#endif /* __FreeBSD__ && KERNEL */
273
274/*
275 * Build some macros and #defines to enable the same code to compile anywhere
276 * Well, that's the idea, anyway :-)
277 */
278#if !SOLARIS || (SOLARIS2 < 6) || !defined(KERNEL)
279# define	ATOMIC_INCL		ATOMIC_INC
280# define	ATOMIC_INC64		ATOMIC_INC
281# define	ATOMIC_INC32		ATOMIC_INC
282# define	ATOMIC_INC16		ATOMIC_INC
283# define	ATOMIC_DECL		ATOMIC_DEC
284# define	ATOMIC_DEC64		ATOMIC_DEC
285# define	ATOMIC_DEC32		ATOMIC_DEC
286# define	ATOMIC_DEC16		ATOMIC_DEC
287#endif
288#ifdef KERNEL
289# if SOLARIS
290#  if SOLARIS2 >= 6
291#   include <sys/atomic.h>
292#   if SOLARIS2 == 6
293#    define	ATOMIC_INCL(x)		atomic_add_long((uint32_t*)&(x), 1)
294#    define	ATOMIC_DECL(x)		atomic_add_long((uint32_t*)&(x), -1)
295#   else
296#    define	ATOMIC_INCL(x)		atomic_add_long(&(x), 1)
297#    define	ATOMIC_DECL(x)		atomic_add_long(&(x), -1)
298#   endif
299#   define	ATOMIC_INC64(x)		atomic_add_64((uint64_t*)&(x), 1)
300#   define	ATOMIC_INC32(x)		atomic_add_32((uint32_t*)&(x), 1)
301#   define	ATOMIC_INC16(x)		atomic_add_16((uint16_t*)&(x), 1)
302#   define	ATOMIC_DEC64(x)		atomic_add_64((uint64_t*)&(x), -1)
303#   define	ATOMIC_DEC32(x)		atomic_add_32((uint32_t*)&(x), -1)
304#   define	ATOMIC_DEC16(x)		atomic_add_16((uint16_t*)&(x), -1)
305#  else
306#   define	ATOMIC_INC(x)		{ mutex_enter(&ipf_rw); (x)++; \
307					  mutex_exit(&ipf_rw); }
308#   define	ATOMIC_DEC(x)		{ mutex_enter(&ipf_rw); (x)--; \
309					  mutex_exit(&ipf_rw); }
310#  endif
311#  define	MUTEX_ENTER(x)		mutex_enter(x)
312#  if 1
313#   define	KRWLOCK_T		krwlock_t
314#   define	READ_ENTER(x)		rw_enter(x, RW_READER)
315#   define	WRITE_ENTER(x)		rw_enter(x, RW_WRITER)
316#   define	RW_UPGRADE(x)		{ if (rw_tryupgrade(x) == 0) { \
317					      rw_exit(x); \
318					      rw_enter(x, RW_WRITER); } \
319					}
320#   define	MUTEX_DOWNGRADE(x)	rw_downgrade(x)
321#   define	RWLOCK_INIT(x, y, z)	rw_init((x), (y), RW_DRIVER, (z))
322#   define	RWLOCK_EXIT(x)		rw_exit(x)
323#   define	RW_DESTROY(x)		rw_destroy(x)
324#  else
325#   define	KRWLOCK_T		kmutex_t
326#   define	READ_ENTER(x)		mutex_enter(x)
327#   define	WRITE_ENTER(x)		mutex_enter(x)
328#   define	MUTEX_DOWNGRADE(x)	;
329#   define	RWLOCK_INIT(x, y, z)	mutex_init((x), (y), MUTEX_DRIVER, (z))
330#   define	RWLOCK_EXIT(x)		mutex_exit(x)
331#   define	RW_DESTROY(x)		mutex_destroy(x)
332#  endif
333#  define	MUTEX_INIT(x, y, z)	mutex_init((x), (y), MUTEX_DRIVER, (z))
334#  define	MUTEX_DESTROY(x)	mutex_destroy(x)
335#  define	MUTEX_EXIT(x)	mutex_exit(x)
336#  define	MTOD(m,t)	(t)((m)->b_rptr)
337#  define	IRCOPY(a,b,c)	copyin((a), (b), (c))
338#  define	IWCOPY(a,b,c)	copyout((a), (b), (c))
339#  define	IRCOPYPTR	ircopyptr
340#  define	IWCOPYPTR	iwcopyptr
341#  define	FREE_MB_T(m)	freemsg(m)
342#  define	SPL_NET(x)	;
343#  define	SPL_IMP(x)	;
344#  undef	SPL_X
345#  define	SPL_X(x)	;
346#  ifdef sparc
347#   define	ntohs(x)	(x)
348#   define	ntohl(x)	(x)
349#   define	htons(x)	(x)
350#   define	htonl(x)	(x)
351#  endif /* sparc */
352#  define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
353#  define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
354#  define	GET_MINOR(x)	getminor(x)
355typedef	struct	qif	{
356	struct	qif	*qf_next;
357	ill_t	*qf_ill;
358	kmutex_t	qf_lock;
359	void	*qf_iptr;
360	void	*qf_optr;
361	queue_t	*qf_in;
362	queue_t	*qf_out;
363	struct	qinit	*qf_wqinfo;
364	struct	qinit	*qf_rqinfo;
365	struct	qinit	qf_wqinit;
366	struct	qinit	qf_rqinit;
367	mblk_t	*qf_m;	/* These three fields are for passing data up from */
368	queue_t	*qf_q;	/* fr_qin and fr_qout to the packet processing. */
369	size_t	qf_off;
370	size_t	qf_len;	/* this field is used for in ipfr_fastroute */
371	char	qf_name[8];
372	/*
373	 * in case the ILL has disappeared...
374	 */
375	size_t	qf_hl;	/* header length */
376	int	qf_sap;
377} qif_t;
378extern	ill_t	*get_unit __P((char *, int));
379#  define	GETUNIT(n, v)	get_unit(n, v)
380#  define	IFNAME(x)	((ill_t *)x)->ill_name
381# else /* SOLARIS */
382#  if defined(__sgi)
383#   define  hz HZ
384#   include <sys/ksynch.h>
385#   define	IPF_LOCK_PL	plhi
386#   include <sys/sema.h>
387#undef kmutex_t
388typedef struct {
389	lock_t *l;
390	int pl;
391} kmutex_t;
392#   define	ATOMIC_INC(x)		{ MUTEX_ENTER(&ipf_rw); \
393					  (x)++; MUTEX_EXIT(&ipf_rw); }
394#   define	ATOMIC_DEC(x)		{ MUTEX_ENTER(&ipf_rw); \
395					  (x)--; MUTEX_EXIT(&ipf_rw); }
396#   define	MUTEX_ENTER(x)		(x)->pl = LOCK((x)->l, IPF_LOCK_PL);
397#   define	KRWLOCK_T		kmutex_t
398#   define	READ_ENTER(x)		MUTEX_ENTER(x)
399#   define	WRITE_ENTER(x)		MUTEX_ENTER(x)
400#   define	RW_UPGRADE(x)		;
401#   define	MUTEX_DOWNGRADE(x)	;
402#   define	RWLOCK_EXIT(x)		MUTEX_EXIT(x)
403#   define	MUTEX_EXIT(x)		UNLOCK((x)->l, (x)->pl);
404#   define	MUTEX_INIT(x,y,z)	(x).l = LOCK_ALLOC((uchar_t)-1, IPF_LOCK_PL, (lkinfo_t *)-1, KM_NOSLEEP)
405#   define	MUTEX_DESTROY(x)	LOCK_DEALLOC((x).l)
406#  else /* __sgi */
407#   define	ATOMIC_INC(x)		(x)++
408#   define	ATOMIC_DEC(x)		(x)--
409#   define	MUTEX_ENTER(x)		;
410#   define	READ_ENTER(x)		;
411#   define	WRITE_ENTER(x)		;
412#   define	RW_UPGRADE(x)		;
413#   define	MUTEX_DOWNGRADE(x)	;
414#   define	RWLOCK_EXIT(x)		;
415#   define	MUTEX_EXIT(x)		;
416#   define	MUTEX_INIT(x,y,z)	;
417#   define	MUTEX_DESTROY(x)	;
418#  endif /* __sgi */
419#  ifndef linux
420#   define	FREE_MB_T(m)	m_freem(m)
421#   define	MTOD(m,t)	mtod(m,t)
422#   define	IRCOPY(a,b,c)	(bcopy((a), (b), (c)), 0)
423#   define	IWCOPY(a,b,c)	(bcopy((a), (b), (c)), 0)
424#   define	IRCOPYPTR	ircopyptr
425#   define	IWCOPYPTR	iwcopyptr
426#  endif /* !linux */
427# endif /* SOLARIS */
428
429# ifdef sun
430#  if !SOLARIS
431#   include	<sys/kmem_alloc.h>
432#   define	GETUNIT(n, v)	ifunit(n, IFNAMSIZ)
433#   define	IFNAME(x)	((struct ifnet *)x)->if_name
434#  endif
435# else
436#  ifndef	linux
437#   define	GETUNIT(n, v)	ifunit(n)
438#   if (defined(NetBSD) && (NetBSD <= 1991011) && (NetBSD >= 199606)) || \
439        (defined(OpenBSD) && (OpenBSD >= 199603))
440#    define	IFNAME(x)	((struct ifnet *)x)->if_xname
441#   else
442#    define	IFNAME(x)	((struct ifnet *)x)->if_name
443#   endif
444#  endif
445# endif /* sun */
446
447# if defined(sun) && !defined(linux) || defined(__sgi)
448#  define	UIOMOVE(a,b,c,d)	uiomove((caddr_t)a,b,c,d)
449#  define	SLEEP(id, n)	sleep((id), PZERO+1)
450#  define	WAKEUP(id)	wakeup(id)
451#  define	KFREE(x)	kmem_free((char *)(x), sizeof(*(x)))
452#  define	KFREES(x,s)	kmem_free((char *)(x), (s))
453#  if !SOLARIS
454extern	void	m_copydata __P((struct mbuf *, int, int, caddr_t));
455extern	void	m_copyback __P((struct mbuf *, int, int, caddr_t));
456#  endif
457#  ifdef __sgi
458#   include <sys/kmem.h>
459#   include <sys/ddi.h>
460#   define	KMALLOC(a,b)	(a) = (b)kmem_alloc(sizeof(*(a)), KM_NOSLEEP)
461#   define	KMALLOCS(a,b,c)	(a) = (b)kmem_alloc((c), KM_NOSLEEP)
462#   define	GET_MINOR(x)	getminor(x)
463#  else
464#   if !SOLARIS
465#    define	KMALLOC(a,b)	(a) = (b)new_kmem_alloc(sizeof(*(a)), \
466							KMEM_NOSLEEP)
467#    define	KMALLOCS(a,b,c)	(a) = (b)new_kmem_alloc((c), KMEM_NOSLEEP)
468#   endif /* SOLARIS */
469#  endif /* __sgi */
470# endif /* sun && !linux */
471# ifndef	GET_MINOR
472#  define	GET_MINOR(x)	minor(x)
473# endif
474# if (BSD >= 199306) || defined(__FreeBSD__)
475#  include <vm/vm.h>
476#  if !defined(__FreeBSD__) || (defined (__FreeBSD__) && __FreeBSD__>=3)
477#   include <vm/vm_extern.h>
478#   include <sys/proc.h>
479extern	vm_map_t	kmem_map;
480#  else /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD__>=3) */
481#   include <vm/vm_kern.h>
482#  endif /* !__FreeBSD__ || (__FreeBSD__ && __FreeBSD__>=3) */
483#  ifdef	M_PFIL
484#   define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_PFIL, M_NOWAIT)
485#   define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_PFIL, M_NOWAIT)
486#   define	KFREE(x)	FREE((x), M_PFIL)
487#   define	KFREES(x,s)	FREE((x), M_PFIL)
488#  else
489#   define	KMALLOC(a, b)	MALLOC((a), b, sizeof(*(a)), M_TEMP, M_NOWAIT)
490#   define	KMALLOCS(a, b, c)	MALLOC((a), b, (c), M_TEMP, M_NOWAIT)
491#   define	KFREE(x)	FREE((x), M_TEMP)
492#   define	KFREES(x,s)	FREE((x), M_TEMP)
493#  endif /* M_PFIL */
494#  define	UIOMOVE(a,b,c,d)	uiomove(a,b,d)
495#  define	SLEEP(id, n)	tsleep((id), PPAUSE|PCATCH, n, 0)
496#  define	WAKEUP(id)	wakeup(id)
497# endif /* BSD */
498# if defined(NetBSD) && NetBSD <= 1991011 && NetBSD >= 199407
499#  define	SPL_NET(x)	x = splsoftnet()
500#  define	SPL_X(x)	(void) splx(x)
501# else
502#  if !SOLARIS && !defined(linux)
503#   define	SPL_IMP(x)	x = splimp()
504#   define	SPL_NET(x)	x = splnet()
505#   define	SPL_X(x)	(void) splx(x)
506#  endif
507# endif /* NetBSD && NetBSD <= 1991011 && NetBSD >= 199407 */
508# define	PANIC(x,y)	if (x) panic y
509#else /* KERNEL */
510# define	SLEEP(x,y)	;
511# define	WAKEUP(x)	;
512# define	PANIC(x,y)	;
513# define	ATOMIC_INC(x)	(x)++
514# define	ATOMIC_DEC(x)	(x)--
515# define	MUTEX_ENTER(x)	;
516# define	READ_ENTER(x)	;
517# define	MUTEX_INIT(x,y,z)	;
518# define	MUTEX_DESTROY(x)	;
519# define	WRITE_ENTER(x)	;
520# define	RW_UPGRADE(x)	;
521# define	MUTEX_DOWNGRADE(x)	;
522# define	RWLOCK_EXIT(x)	;
523# define	MUTEX_EXIT(x)	;
524# define	SPL_NET(x)	;
525# define	SPL_IMP(x)	;
526# undef		SPL_X
527# define	SPL_X(x)	;
528# define	KMALLOC(a,b)	(a) = (b)malloc(sizeof(*a))
529# define	KMALLOCS(a,b,c)	(a) = (b)malloc(c)
530# define	KFREE(x)	free(x)
531# define	KFREES(x,s)	free(x)
532# define	GETUNIT(x, v)	get_unit(x,v)
533# define	IRCOPY(a,b,c)	(bcopy((a), (b), (c)), 0)
534# define	IWCOPY(a,b,c)	(bcopy((a), (b), (c)), 0)
535# define	IRCOPYPTR	ircopyptr
536# define	IWCOPYPTR	iwcopyptr
537#endif /* KERNEL */
538
539#if SOLARIS
540typedef mblk_t mb_t;
541# if SOLARIS2 >= 7
542#  ifdef lint
543#   define ALIGN32(ptr)    (ptr ? 0L : 0L)
544#   define ALIGN16(ptr)    (ptr ? 0L : 0L)
545#  else
546#   define ALIGN32(ptr)    (ptr)
547#   define ALIGN16(ptr)    (ptr)
548#  endif
549# endif
550#else
551# ifdef	linux
552#  ifndef kernel
553typedef struct mb {
554	struct mb *next;
555	u_int len;
556	u_char *data;
557} mb_t;
558#  else
559typedef struct sk_buff mb_t;
560#  endif
561# else
562typedef struct mbuf mb_t;
563# endif
564#endif /* SOLARIS */
565
566#if defined(linux) || defined(__sgi)
567/*
568 * These #ifdef's are here mainly for linux, but who knows, they may
569 * not be in other places or maybe one day linux will grow up and some
570 * of these will turn up there too.
571 */
572#ifndef	ICMP_MINLEN
573# define	ICMP_MINLEN	8
574#endif
575#ifndef	ICMP_UNREACH
576# define	ICMP_UNREACH	ICMP_DEST_UNREACH
577#endif
578#ifndef	ICMP_SOURCEQUENCH
579# define	ICMP_SOURCEQUENCH	ICMP_SOURCE_QUENCH
580#endif
581#ifndef	ICMP_TIMXCEED
582# define	ICMP_TIMXCEED	ICMP_TIME_EXCEEDED
583#endif
584#ifndef	ICMP_PARAMPROB
585# define	ICMP_PARAMPROB	ICMP_PARAMETERPROB
586#endif
587#ifndef ICMP_TSTAMP
588# define	ICMP_TSTAMP	ICMP_TIMESTAMP
589#endif
590#ifndef ICMP_TSTAMPREPLY
591# define	ICMP_TSTAMPREPLY	ICMP_TIMESTAMPREPLY
592#endif
593#ifndef ICMP_IREQ
594# define	ICMP_IREQ	ICMP_INFO_REQUEST
595#endif
596#ifndef ICMP_IREQREPLY
597# define	ICMP_IREQREPLY	ICMP_INFO_REPLY
598#endif
599#ifndef	ICMP_MASKREQ
600# define	ICMP_MASKREQ	ICMP_ADDRESS
601#endif
602#ifndef ICMP_MASKREPLY
603# define	ICMP_MASKREPLY	ICMP_ADDRESSREPLY
604#endif
605#ifndef	IPVERSION
606# define	IPVERSION	4
607#endif
608#ifndef	IPOPT_MINOFF
609# define	IPOPT_MINOFF	4
610#endif
611#ifndef	IPOPT_COPIED
612# define	IPOPT_COPIED(x)	((x)&0x80)
613#endif
614#ifndef	IPOPT_EOL
615# define	IPOPT_EOL	0
616#endif
617#ifndef	IPOPT_NOP
618# define	IPOPT_NOP	1
619#endif
620#ifndef	IP_MF
621# define	IP_MF	((u_short)0x2000)
622#endif
623#ifndef	ETHERTYPE_IP
624# define	ETHERTYPE_IP	((u_short)0x0800)
625#endif
626#ifndef	TH_FIN
627# define	TH_FIN	0x01
628#endif
629#ifndef	TH_SYN
630# define	TH_SYN	0x02
631#endif
632#ifndef	TH_RST
633# define	TH_RST	0x04
634#endif
635#ifndef	TH_PUSH
636# define	TH_PUSH	0x08
637#endif
638#ifndef	TH_ACK
639# define	TH_ACK	0x10
640#endif
641#ifndef	TH_URG
642# define	TH_URG	0x20
643#endif
644#ifndef	IPOPT_EOL
645# define	IPOPT_EOL	0
646#endif
647#ifndef	IPOPT_NOP
648# define	IPOPT_NOP	1
649#endif
650#ifndef	IPOPT_RR
651# define	IPOPT_RR	7
652#endif
653#ifndef	IPOPT_TS
654# define	IPOPT_TS	68
655#endif
656#ifndef	IPOPT_SECURITY
657# define	IPOPT_SECURITY	130
658#endif
659#ifndef	IPOPT_LSRR
660# define	IPOPT_LSRR	131
661#endif
662#ifndef	IPOPT_SATID
663# define	IPOPT_SATID	136
664#endif
665#ifndef	IPOPT_SSRR
666# define	IPOPT_SSRR	137
667#endif
668#ifndef	IPOPT_SECUR_UNCLASS
669# define	IPOPT_SECUR_UNCLASS	((u_short)0x0000)
670#endif
671#ifndef	IPOPT_SECUR_CONFID
672# define	IPOPT_SECUR_CONFID	((u_short)0xf135)
673#endif
674#ifndef	IPOPT_SECUR_EFTO
675# define	IPOPT_SECUR_EFTO	((u_short)0x789a)
676#endif
677#ifndef	IPOPT_SECUR_MMMM
678# define	IPOPT_SECUR_MMMM	((u_short)0xbc4d)
679#endif
680#ifndef	IPOPT_SECUR_RESTR
681# define	IPOPT_SECUR_RESTR	((u_short)0xaf13)
682#endif
683#ifndef	IPOPT_SECUR_SECRET
684# define	IPOPT_SECUR_SECRET	((u_short)0xd788)
685#endif
686#ifndef IPOPT_SECUR_TOPSECRET
687# define	IPOPT_SECUR_TOPSECRET	((u_short)0x6bc5)
688#endif
689#ifndef IPOPT_OLEN
690# define	IPOPT_OLEN	1
691#endif
692#endif /* linux || __sgi */
693
694#ifdef	linux
695#include <linux/in_systm.h>
696/*
697 * TCP States
698 */
699#define	TCPS_CLOSED		0	/* closed */
700#define	TCPS_LISTEN		1	/* listening for connection */
701#define	TCPS_SYN_SENT		2	/* active, have sent syn */
702#define	TCPS_SYN_RECEIVED	3	/* have send and received syn */
703/* states < TCPS_ESTABLISHED are those where connections not established */
704#define	TCPS_ESTABLISHED	4	/* established */
705#define	TCPS_CLOSE_WAIT		5	/* rcvd fin, waiting for close */
706/* states > TCPS_CLOSE_WAIT are those where user has closed */
707#define	TCPS_FIN_WAIT_1		6	/* have closed, sent fin */
708#define	TCPS_CLOSING		7	/* closed xchd FIN; await FIN ACK */
709#define	TCPS_LAST_ACK		8	/* had fin and close; await FIN ACK */
710/* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */
711#define	TCPS_FIN_WAIT_2		9	/* have closed, fin is acked */
712#define	TCPS_TIME_WAIT		10	/* in 2*msl quiet wait after close */
713
714/*
715 * file flags.
716 */
717#ifdef WRITE
718#define	FWRITE	WRITE
719#define	FREAD	READ
720#else
721#define	FWRITE	_IOC_WRITE
722#define	FREAD	_IOC_READ
723#endif
724/*
725 * mbuf related problems.
726 */
727#define	mtod(m,t)	(t)((m)->data)
728#define	m_len		len
729#define	m_next		next
730
731#ifdef	IP_DF
732#undef	IP_DF
733#endif
734#define	IP_DF		0x4000
735
736typedef	struct	{
737	__u16	th_sport;
738	__u16	th_dport;
739	__u32	th_seq;
740	__u32	th_ack;
741# if defined(__i386__) || defined(__MIPSEL__) || defined(__alpha__) ||\
742    defined(vax)
743	__u8	th_res:4;
744	__u8	th_off:4;
745#else
746	__u8	th_off:4;
747	__u8	th_res:4;
748#endif
749	__u8	th_flags;
750	__u16	th_win;
751	__u16	th_sum;
752	__u16	th_urp;
753} tcphdr_t;
754
755typedef	struct	{
756	__u16	uh_sport;
757	__u16	uh_dport;
758	__u16	uh_ulen;
759	__u16	uh_sum;
760} udphdr_t;
761
762typedef	struct	{
763# if defined(__i386__) || defined(__MIPSEL__) || defined(__alpha__) ||\
764    defined(vax)
765	__u8	ip_hl:4;
766	__u8	ip_v:4;
767# else
768	__u8	ip_v:4;
769	__u8	ip_hl:4;
770# endif
771	__u8	ip_tos;
772	__u16	ip_len;
773	__u16	ip_id;
774	__u16	ip_off;
775	__u8	ip_ttl;
776	__u8	ip_p;
777	__u16	ip_sum;
778	struct	in_addr	ip_src;
779	struct	in_addr	ip_dst;
780} ip_t;
781
782/*
783 * Structure of an icmp header.
784 */
785typedef struct icmp {
786	__u8	icmp_type;		/* type of message, see below */
787	__u8	icmp_code;		/* type sub code */
788	__u16	icmp_cksum;		/* ones complement cksum of struct */
789	union {
790		__u8	ih_pptr;		/* ICMP_PARAMPROB */
791		struct	in_addr	ih_gwaddr;	/* ICMP_REDIRECT */
792		struct	ih_idseq {
793			__u16	icd_id;
794			__u16	icd_seq;
795		} ih_idseq;
796		int ih_void;
797	} icmp_hun;
798# define	icmp_pptr	icmp_hun.ih_pptr
799# define	icmp_gwaddr	icmp_hun.ih_gwaddr
800# define	icmp_id		icmp_hun.ih_idseq.icd_id
801# define	icmp_seq	icmp_hun.ih_idseq.icd_seq
802# define	icmp_void	icmp_hun.ih_void
803	union {
804		struct id_ts {
805			n_time its_otime;
806			n_time its_rtime;
807			n_time its_ttime;
808		} id_ts;
809		struct id_ip  {
810			ip_t idi_ip;
811			/* options and then 64 bits of data */
812		} id_ip;
813		u_long	id_mask;
814		char	id_data[1];
815	} icmp_dun;
816# define	icmp_otime	icmp_dun.id_ts.its_otime
817# define	icmp_rtime	icmp_dun.id_ts.its_rtime
818# define	icmp_ttime	icmp_dun.id_ts.its_ttime
819# define	icmp_ip		icmp_dun.id_ip.idi_ip
820# define	icmp_mask	icmp_dun.id_mask
821# define	icmp_data	icmp_dun.id_data
822} icmphdr_t;
823
824# ifndef LINUX_IPOVLY
825#  define LINUX_IPOVLY
826struct ipovly {
827	caddr_t	ih_next, ih_prev;	/* for protocol sequence q's */
828	u_char	ih_x1;			/* (unused) */
829	u_char	ih_pr;			/* protocol */
830	short	ih_len;			/* protocol length */
831	struct	in_addr ih_src;		/* source internet address */
832	struct	in_addr ih_dst;		/* destination internet address */
833};
834# endif
835
836typedef struct  {
837	__u8	ether_dhost[6];
838	__u8	ether_shost[6];
839	__u16	ether_type;
840} ether_header_t;
841
842typedef	struct	uio	{
843	int	uio_resid;
844	int	uio_rw;
845	caddr_t	uio_buf;
846} uio_t;
847
848# define	UIO_READ	0
849# define	UIO_WRITE	1
850# define	UIOMOVE(a, b, c, d)	uiomove(a,b,c,d)
851
852/*
853 * For masking struct ifnet onto struct device
854 */
855# define	if_name	name
856
857# ifdef	KERNEL
858#  define	GETUNIT(x, v)	dev_get(x)
859#  define	FREE_MB_T(m)	kfree_skb(m, FREE_WRITE)
860#  define	uniqtime	do_gettimeofday
861#  undef INT_MAX
862#  undef UINT_MAX
863#  undef LONG_MAX
864#  undef ULONG_MAX
865#  include <linux/netdevice.h>
866#  define	SPL_X(x)
867#  define	SPL_NET(x)
868#  define	SPL_IMP(x)
869
870#  define	bcmp(a,b,c)	memcmp(a,b,c)
871#  define	bcopy(a,b,c)	memcpy(b,a,c)
872#  define	bzero(a,c)	memset(a,0,c)
873
874#  define	UNITNAME(n)	dev_get((n))
875
876#  define	KMALLOC(a,b)	(a) = (b)kmalloc(sizeof(*(a)), GFP_ATOMIC)
877#  define	KMALLOCS(a,b,c)	(a) = (b)kmalloc((c), GFP_ATOMIC)
878#  define	KFREE(x)	kfree_s((x), sizeof(*(x)))
879#  define	KFREES(x,s)	kfree_s((x), (s))
880#define IRCOPY(const void *a, void *b, size_t c)	{ \
881	int error; \
882
883	error = verify_area(VERIFY_READ, a ,c); \
884	if (!error) \
885		memcpy_fromfs(b, a, c); \
886	return error; \
887}
888static inline int IWCOPY(const void *a, void *b, size_t c)
889{
890	int error;
891
892	error = verify_area(VERIFY_WRITE, b, c);
893	if (!error)
894		memcpy_tofs(b, a, c);
895	return error;
896}
897static inline int IRCOPYPTR(const void *a, void *b, size_t c) {
898	caddr_t ca;
899	int	error;
900
901	error = verify_area(VERIFY_READ, a ,sizeof(ca));
902	if (!error) {
903		memcpy_fromfs(ca, a, sizeof(ca));
904		error = verify_area(VERIFY_READ, ca , c);
905		if (!error)
906			memcpy_fromfs(b, ca, c);
907	}
908	return error;
909}
910static inline int IWCOPYPTR(const void *a, void *b, size_t c) {
911	caddr_t ca;
912	int	error;
913
914
915	error = verify_area(VERIFY_READ, b ,sizeof(ca));
916	if (!error) {
917		memcpy_fromfs(ca, b, sizeof(ca));
918		error = verify_area(VERIFY_WRITE, ca, c);
919		if (!error)
920			memcpy_tofs(ca, a, c);
921	}
922	return error;
923}
924# else
925#  define	__KERNEL__
926#  undef INT_MAX
927#  undef UINT_MAX
928#  undef LONG_MAX
929#  undef ULONG_MAX
930#  define	s8 __s8
931#  define	u8 __u8
932#  define	s16 __s16
933#  define	u16 __u16
934#  define	s32 __s32
935#  define	u32 __u32
936#  include <linux/netdevice.h>
937#  undef	__KERNEL__
938# endif
939# define	ifnet	device
940#else
941typedef	struct	tcphdr	tcphdr_t;
942typedef	struct	udphdr	udphdr_t;
943typedef	struct	icmp	icmphdr_t;
944typedef	struct	ip	ip_t;
945typedef	struct	ether_header	ether_header_t;
946#endif /* linux */
947typedef	struct	tcpiphdr	tcpiphdr_t;
948
949#if defined(hpux) || defined(linux)
950struct	ether_addr	{
951	char	ether_addr_octet[6];
952};
953#endif
954
955/*
956 * XXX - This is one of those *awful* hacks which nobody likes
957 */
958#ifdef	ultrix
959#define	A_A
960#else
961#define	A_A	&
962#endif
963
964#define	TCPF_ALL	(TH_FIN|TH_SYN|TH_RST|TH_PUSH|TH_ACK|TH_URG)
965
966#ifndef	ICMP_ROUTERADVERT
967# define	ICMP_ROUTERADVERT	9
968#endif
969#ifndef	ICMP_ROUTERSOLICIT
970# define	ICMP_ROUTERSOLICIT	10
971#endif
972#undef	ICMP_MAX_UNREACH
973#define	ICMP_MAX_UNREACH	14
974#undef	ICMP_MAXTYPE
975#define	ICMP_MAXTYPE		18
976/*
977 * ICMP error replies have an IP header (20 bytes), 8 bytes of ICMP data,
978 * another IP header and then 64 bits of data, totalling 56.  Of course,
979 * the last 64 bits is dependant on that being available.
980 */
981#define	ICMPERR_ICMPHLEN	8
982#define	ICMPERR_IPICMPHLEN	(20 + 8)
983#define	ICMPERR_MINPKTLEN	(20 + 8 + 20)
984#define	ICMPERR_MAXPKTLEN	(20 + 8 + 20 + 8)
985#define	ICMP6ERR_MINPKTLEN	(20 + 8)
986
987#endif	/* __IP_COMPAT_H__ */
988