17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 1988, 1989, 1993
37c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
67c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
77c478bd9Sstevel@tonic-gate  * are met:
87c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
97c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
107c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
127c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
157c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
167c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
177c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
187c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
197c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
207c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
217c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
227c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
237c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
247c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  *	@(#)radix.h	8.2 (Berkeley) 10/31/94
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
29*c793af95Ssangeeta #ifndef RADIX_IPF_H
30*c793af95Ssangeeta #define RADIX_IPF_H
31*c793af95Ssangeeta 
32*c793af95Ssangeeta #pragma ident	"%Z%%M%	%I%	%E% SMI"
33*c793af95Ssangeeta 
34*c793af95Ssangeeta #if SOLARIS2 > 10
35*c793af95Ssangeeta #include <net/radix.h>
36*c793af95Ssangeeta #else
37ab25eeb5Syz #if !defined(_NET_RADIX_H_) && !defined(_RADIX_H_)
387c478bd9Sstevel@tonic-gate #define	_NET_RADIX_H_
39ab25eeb5Syz #ifndef _RADIX_H_
40ab25eeb5Syz #define	_RADIX_H_
41ab25eeb5Syz #endif /* _RADIX_H_ */
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifndef __P
447c478bd9Sstevel@tonic-gate # ifdef __STDC__
457c478bd9Sstevel@tonic-gate #  define	__P(x)  x
467c478bd9Sstevel@tonic-gate # else
477c478bd9Sstevel@tonic-gate #  define	__P(x)  ()
487c478bd9Sstevel@tonic-gate # endif
497c478bd9Sstevel@tonic-gate #endif
507c478bd9Sstevel@tonic-gate 
51ab25eeb5Syz #if defined(__sgi)
52ab25eeb5Syz # define	radix_mask	ipf_radix_mask
53ab25eeb5Syz # define	radix_node	ipf_radix_node
54ab25eeb5Syz # define	radix_node_head	ipf_radix_node_head
55ab25eeb5Syz #endif
56ab25eeb5Syz 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Radix search tree node layout.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate struct radix_node {
627c478bd9Sstevel@tonic-gate 	struct	radix_mask *rn_mklist;	/* list of masks contained in subtree */
637c478bd9Sstevel@tonic-gate 	struct	radix_node *rn_p;	/* parent */
647c478bd9Sstevel@tonic-gate 	short	rn_b;			/* bit offset; -1-index(netmask) */
657c478bd9Sstevel@tonic-gate 	char	rn_bmask;		/* node: mask for bit test*/
667c478bd9Sstevel@tonic-gate 	u_char	rn_flags;		/* enumerated next */
677c478bd9Sstevel@tonic-gate #define RNF_NORMAL	1		/* leaf contains normal route */
687c478bd9Sstevel@tonic-gate #define RNF_ROOT	2		/* leaf is root leaf for tree */
697c478bd9Sstevel@tonic-gate #define RNF_ACTIVE	4		/* This node is alive (for rtfree) */
707c478bd9Sstevel@tonic-gate 	union {
717c478bd9Sstevel@tonic-gate 		struct {			/* leaf only data: */
727c478bd9Sstevel@tonic-gate 			caddr_t rn_Key;		/* object of search */
737c478bd9Sstevel@tonic-gate 			caddr_t rn_Mask;	/* netmask, if present */
747c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_Dupedkey;
757c478bd9Sstevel@tonic-gate 		} rn_leaf;
767c478bd9Sstevel@tonic-gate 		struct {			/* node only data: */
777c478bd9Sstevel@tonic-gate 			int	rn_Off;		/* where to start compare */
787c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_L;/* progeny */
797c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_R;/* progeny */
807c478bd9Sstevel@tonic-gate 		} rn_node;
817c478bd9Sstevel@tonic-gate 	} rn_u;
827c478bd9Sstevel@tonic-gate #ifdef RN_DEBUG
837c478bd9Sstevel@tonic-gate 	int rn_info;
847c478bd9Sstevel@tonic-gate 	struct radix_node *rn_twin;
857c478bd9Sstevel@tonic-gate 	struct radix_node *rn_ybro;
867c478bd9Sstevel@tonic-gate #endif
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey
907c478bd9Sstevel@tonic-gate #define rn_key rn_u.rn_leaf.rn_Key
917c478bd9Sstevel@tonic-gate #define rn_mask rn_u.rn_leaf.rn_Mask
927c478bd9Sstevel@tonic-gate #define rn_off rn_u.rn_node.rn_Off
937c478bd9Sstevel@tonic-gate #define rn_l rn_u.rn_node.rn_L
947c478bd9Sstevel@tonic-gate #define rn_r rn_u.rn_node.rn_R
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Annotations to tree concerning potential routes applying to subtrees.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate 
100ab25eeb5Syz struct radix_mask {
1017c478bd9Sstevel@tonic-gate 	short	rm_b;			/* bit offset; -1-index(netmask) */
1027c478bd9Sstevel@tonic-gate 	char	rm_unused;		/* cf. rn_bmask */
1037c478bd9Sstevel@tonic-gate 	u_char	rm_flags;		/* cf. rn_flags */
1047c478bd9Sstevel@tonic-gate 	struct	radix_mask *rm_mklist;	/* more masks to try */
1057c478bd9Sstevel@tonic-gate 	union	{
1067c478bd9Sstevel@tonic-gate 		caddr_t	rmu_mask;		/* the mask */
1077c478bd9Sstevel@tonic-gate 		struct	radix_node *rmu_leaf;	/* for normal routes */
1087c478bd9Sstevel@tonic-gate 	}	rm_rmu;
1097c478bd9Sstevel@tonic-gate 	int	rm_refs;		/* # of references to this struct */
110ab25eeb5Syz };
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate #define rm_mask rm_rmu.rmu_mask
1137c478bd9Sstevel@tonic-gate #define rm_leaf rm_rmu.rmu_leaf		/* extra field would make 32 bytes */
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate #define MKGet(m) {\
1167c478bd9Sstevel@tonic-gate 	if (rn_mkfreelist) {\
1177c478bd9Sstevel@tonic-gate 		m = rn_mkfreelist; \
1187c478bd9Sstevel@tonic-gate 		rn_mkfreelist = (m)->rm_mklist; \
1197c478bd9Sstevel@tonic-gate 	} else \
1207c478bd9Sstevel@tonic-gate 		R_Malloc(m, struct radix_mask *, sizeof (*(m))); }\
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #define MKFree(m) { (m)->rm_mklist = rn_mkfreelist; rn_mkfreelist = (m);}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate struct radix_node_head {
1257c478bd9Sstevel@tonic-gate 	struct	radix_node *rnh_treetop;
1267c478bd9Sstevel@tonic-gate 	struct	radix_node *rnh_leaflist;
1277c478bd9Sstevel@tonic-gate 	u_long	rnh_hits;
1287c478bd9Sstevel@tonic-gate 	u_int	rnh_number;
1297c478bd9Sstevel@tonic-gate 	u_int	rnh_ref;
1307c478bd9Sstevel@tonic-gate 	int	rnh_addrsize;		/* permit, but not require fixed keys */
1317c478bd9Sstevel@tonic-gate 	int	rnh_pktsize;		/* permit, but not require fixed keys */
1327c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_addaddr)	/* add based on sockaddr */
1337c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask,
1347c478bd9Sstevel@tonic-gate 		     struct radix_node_head *head, struct radix_node nodes[]));
1357c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_addpkt)	/* add based on packet hdr */
1367c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask,
1377c478bd9Sstevel@tonic-gate 		     struct radix_node_head *head, struct radix_node nodes[]));
1387c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_deladdr)	/* remove based on sockaddr */
1397c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1407c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_delpkt)	/* remove based on packet hdr */
1417c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1427c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_matchaddr)	/* locate based on sockaddr */
1437c478bd9Sstevel@tonic-gate 		__P((void *v, struct radix_node_head *head));
1447c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_lookup)	/* locate based on sockaddr */
1457c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1467c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_matchpkt)	/* locate based on packet hdr */
1477c478bd9Sstevel@tonic-gate 		__P((void *v, struct radix_node_head *head));
1487c478bd9Sstevel@tonic-gate 	int	(*rnh_walktree)			/* traverse tree */
1497c478bd9Sstevel@tonic-gate 		__P((struct radix_node_head *,
1507c478bd9Sstevel@tonic-gate 		     int (*)(struct radix_node *, void *), void *));
1517c478bd9Sstevel@tonic-gate 	struct	radix_node rnh_nodes[3];	/* empty tree for common case */
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 
155ab25eeb5Syz #if defined(AIX)
156ab25eeb5Syz # undef Bcmp
157ab25eeb5Syz # undef Bzero
158ab25eeb5Syz # undef R_Malloc
159ab25eeb5Syz # undef Free
160ab25eeb5Syz #endif
1617c478bd9Sstevel@tonic-gate #define Bcmp(a, b, n)	bcmp(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n))
162ab25eeb5Syz #if defined(linux) && defined(_KERNEL)
163ab25eeb5Syz # define Bcopy(a, b, n)	memmove(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
164ab25eeb5Syz #else
165ab25eeb5Syz # define Bcopy(a, b, n)	bcopy(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n))
166ab25eeb5Syz #endif
1677c478bd9Sstevel@tonic-gate #define Bzero(p, n)		bzero((caddr_t)(p), (unsigned)(n));
1687c478bd9Sstevel@tonic-gate #define R_Malloc(p, t, n)	KMALLOCS(p, t, n)
1697c478bd9Sstevel@tonic-gate #define FreeS(p, z)		KFREES(p, z)
1707c478bd9Sstevel@tonic-gate #define Free(p)			KFREE(p)
1717c478bd9Sstevel@tonic-gate 
172ab25eeb5Syz #if (defined(__osf__) || defined(AIX) || (IRIX >= 60516)) && defined(_KERNEL)
173ab25eeb5Syz # define	rn_init		ipf_rn_init
174ab25eeb5Syz # define	rn_fini		ipf_rn_fini
175ab25eeb5Syz # define	rn_inithead	ipf_rn_inithead
176ab25eeb5Syz # define	rn_freehead	ipf_rn_freehead
177ab25eeb5Syz # define	rn_inithead0	ipf_rn_inithead0
178ab25eeb5Syz # define	rn_refines	ipf_rn_refines
179ab25eeb5Syz # define	rn_walktree	ipf_rn_walktree
180ab25eeb5Syz # define	rn_addmask	ipf_rn_addmask
181ab25eeb5Syz # define	rn_addroute	ipf_rn_addroute
182ab25eeb5Syz # define	rn_delete	ipf_rn_delete
183ab25eeb5Syz # define	rn_insert	ipf_rn_insert
184ab25eeb5Syz # define	rn_lookup	ipf_rn_lookup
185ab25eeb5Syz # define	rn_match	ipf_rn_match
186ab25eeb5Syz # define	rn_newpair	ipf_rn_newpair
187ab25eeb5Syz # define	rn_search	ipf_rn_search
188ab25eeb5Syz # define	rn_search_m	ipf_rn_search_m
189ab25eeb5Syz # define	max_keylen	ipf_maxkeylen
190ab25eeb5Syz # define	rn_mkfreelist	ipf_rn_mkfreelist
191ab25eeb5Syz # define	rn_zeros	ipf_rn_zeros
192ab25eeb5Syz # define	rn_ones		ipf_rn_ones
193ab25eeb5Syz # define	rn_satisfies_leaf	ipf_rn_satisfies_leaf
194ab25eeb5Syz # define	rn_lexobetter	ipf_rn_lexobetter
195ab25eeb5Syz # define	rn_new_radix_mask	ipf_rn_new_radix_mask
196ab25eeb5Syz # define	rn_freenode	ipf_rn_freenode
197ab25eeb5Syz #endif
198ab25eeb5Syz 
1997c478bd9Sstevel@tonic-gate void	 rn_init __P((void));
2007c478bd9Sstevel@tonic-gate void	 rn_fini __P((void));
2017c478bd9Sstevel@tonic-gate int	 rn_inithead __P((void **, int));
2027c478bd9Sstevel@tonic-gate void	 rn_freehead __P((struct radix_node_head *));
2037c478bd9Sstevel@tonic-gate int	 rn_inithead0 __P((struct radix_node_head *, int));
2047c478bd9Sstevel@tonic-gate int	 rn_refines __P((void *, void *));
2057c478bd9Sstevel@tonic-gate int	 rn_walktree __P((struct radix_node_head *,
2067c478bd9Sstevel@tonic-gate 			  int (*)(struct radix_node *, void *), void *));
2077c478bd9Sstevel@tonic-gate struct radix_node
2087c478bd9Sstevel@tonic-gate 	 *rn_addmask __P((void *, int, int)),
2097c478bd9Sstevel@tonic-gate 	 *rn_addroute __P((void *, void *, struct radix_node_head *,
2107c478bd9Sstevel@tonic-gate 			struct radix_node [2])),
2117c478bd9Sstevel@tonic-gate 	 *rn_delete __P((void *, void *, struct radix_node_head *)),
2127c478bd9Sstevel@tonic-gate 	 *rn_insert __P((void *, struct radix_node_head *, int *,
2137c478bd9Sstevel@tonic-gate 			struct radix_node [2])),
2147c478bd9Sstevel@tonic-gate 	 *rn_lookup __P((void *, void *, struct radix_node_head *)),
2157c478bd9Sstevel@tonic-gate 	 *rn_match __P((void *, struct radix_node_head *)),
2167c478bd9Sstevel@tonic-gate 	 *rn_newpair __P((void *, int, struct radix_node[2])),
2177c478bd9Sstevel@tonic-gate 	 *rn_search __P((void *, struct radix_node *)),
2187c478bd9Sstevel@tonic-gate 	 *rn_search_m __P((void *, struct radix_node *, void *));
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate #endif /* _NET_RADIX_H_ */
221*c793af95Ssangeeta #endif /* SOLARIS2 */
222*c793af95Ssangeeta #endif /* RADIX_IPF_H */
223