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 #if SOLARIS2 > 10
33*c793af95Ssangeeta #include <net/radix.h>
34*c793af95Ssangeeta #else
35ab25eeb5Syz #if !defined(_NET_RADIX_H_) && !defined(_RADIX_H_)
367c478bd9Sstevel@tonic-gate #define	_NET_RADIX_H_
37ab25eeb5Syz #ifndef _RADIX_H_
38ab25eeb5Syz #define	_RADIX_H_
39ab25eeb5Syz #endif /* _RADIX_H_ */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #ifndef __P
427c478bd9Sstevel@tonic-gate # ifdef __STDC__
437c478bd9Sstevel@tonic-gate #  define	__P(x)  x
447c478bd9Sstevel@tonic-gate # else
457c478bd9Sstevel@tonic-gate #  define	__P(x)  ()
467c478bd9Sstevel@tonic-gate # endif
477c478bd9Sstevel@tonic-gate #endif
487c478bd9Sstevel@tonic-gate 
49ab25eeb5Syz #if defined(__sgi)
50ab25eeb5Syz # define	radix_mask	ipf_radix_mask
51ab25eeb5Syz # define	radix_node	ipf_radix_node
52ab25eeb5Syz # define	radix_node_head	ipf_radix_node_head
53ab25eeb5Syz #endif
54ab25eeb5Syz 
557c478bd9Sstevel@tonic-gate /*
567c478bd9Sstevel@tonic-gate  * Radix search tree node layout.
577c478bd9Sstevel@tonic-gate  */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate struct radix_node {
607c478bd9Sstevel@tonic-gate 	struct	radix_mask *rn_mklist;	/* list of masks contained in subtree */
617c478bd9Sstevel@tonic-gate 	struct	radix_node *rn_p;	/* parent */
627c478bd9Sstevel@tonic-gate 	short	rn_b;			/* bit offset; -1-index(netmask) */
637c478bd9Sstevel@tonic-gate 	char	rn_bmask;		/* node: mask for bit test*/
647c478bd9Sstevel@tonic-gate 	u_char	rn_flags;		/* enumerated next */
657c478bd9Sstevel@tonic-gate #define RNF_NORMAL	1		/* leaf contains normal route */
667c478bd9Sstevel@tonic-gate #define RNF_ROOT	2		/* leaf is root leaf for tree */
677c478bd9Sstevel@tonic-gate #define RNF_ACTIVE	4		/* This node is alive (for rtfree) */
687c478bd9Sstevel@tonic-gate 	union {
697c478bd9Sstevel@tonic-gate 		struct {			/* leaf only data: */
707c478bd9Sstevel@tonic-gate 			caddr_t rn_Key;		/* object of search */
717c478bd9Sstevel@tonic-gate 			caddr_t rn_Mask;	/* netmask, if present */
727c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_Dupedkey;
737c478bd9Sstevel@tonic-gate 		} rn_leaf;
747c478bd9Sstevel@tonic-gate 		struct {			/* node only data: */
757c478bd9Sstevel@tonic-gate 			int	rn_Off;		/* where to start compare */
767c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_L;/* progeny */
777c478bd9Sstevel@tonic-gate 			struct	radix_node *rn_R;/* progeny */
787c478bd9Sstevel@tonic-gate 		} rn_node;
797c478bd9Sstevel@tonic-gate 	} rn_u;
807c478bd9Sstevel@tonic-gate #ifdef RN_DEBUG
817c478bd9Sstevel@tonic-gate 	int rn_info;
827c478bd9Sstevel@tonic-gate 	struct radix_node *rn_twin;
837c478bd9Sstevel@tonic-gate 	struct radix_node *rn_ybro;
847c478bd9Sstevel@tonic-gate #endif
857c478bd9Sstevel@tonic-gate };
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate #define rn_dupedkey rn_u.rn_leaf.rn_Dupedkey
887c478bd9Sstevel@tonic-gate #define rn_key rn_u.rn_leaf.rn_Key
897c478bd9Sstevel@tonic-gate #define rn_mask rn_u.rn_leaf.rn_Mask
907c478bd9Sstevel@tonic-gate #define rn_off rn_u.rn_node.rn_Off
917c478bd9Sstevel@tonic-gate #define rn_l rn_u.rn_node.rn_L
927c478bd9Sstevel@tonic-gate #define rn_r rn_u.rn_node.rn_R
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Annotations to tree concerning potential routes applying to subtrees.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate 
98ab25eeb5Syz struct radix_mask {
997c478bd9Sstevel@tonic-gate 	short	rm_b;			/* bit offset; -1-index(netmask) */
1007c478bd9Sstevel@tonic-gate 	char	rm_unused;		/* cf. rn_bmask */
1017c478bd9Sstevel@tonic-gate 	u_char	rm_flags;		/* cf. rn_flags */
1027c478bd9Sstevel@tonic-gate 	struct	radix_mask *rm_mklist;	/* more masks to try */
1037c478bd9Sstevel@tonic-gate 	union	{
1047c478bd9Sstevel@tonic-gate 		caddr_t	rmu_mask;		/* the mask */
1057c478bd9Sstevel@tonic-gate 		struct	radix_node *rmu_leaf;	/* for normal routes */
1067c478bd9Sstevel@tonic-gate 	}	rm_rmu;
1077c478bd9Sstevel@tonic-gate 	int	rm_refs;		/* # of references to this struct */
108ab25eeb5Syz };
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate #define rm_mask rm_rmu.rmu_mask
1117c478bd9Sstevel@tonic-gate #define rm_leaf rm_rmu.rmu_leaf		/* extra field would make 32 bytes */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate #define MKGet(m) {\
1147c478bd9Sstevel@tonic-gate 	if (rn_mkfreelist) {\
1157c478bd9Sstevel@tonic-gate 		m = rn_mkfreelist; \
1167c478bd9Sstevel@tonic-gate 		rn_mkfreelist = (m)->rm_mklist; \
1177c478bd9Sstevel@tonic-gate 	} else \
1187c478bd9Sstevel@tonic-gate 		R_Malloc(m, struct radix_mask *, sizeof (*(m))); }\
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #define MKFree(m) { (m)->rm_mklist = rn_mkfreelist; rn_mkfreelist = (m);}
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate struct radix_node_head {
1237c478bd9Sstevel@tonic-gate 	struct	radix_node *rnh_treetop;
1247c478bd9Sstevel@tonic-gate 	struct	radix_node *rnh_leaflist;
1257c478bd9Sstevel@tonic-gate 	u_long	rnh_hits;
1267c478bd9Sstevel@tonic-gate 	u_int	rnh_number;
1277c478bd9Sstevel@tonic-gate 	u_int	rnh_ref;
1287c478bd9Sstevel@tonic-gate 	int	rnh_addrsize;		/* permit, but not require fixed keys */
1297c478bd9Sstevel@tonic-gate 	int	rnh_pktsize;		/* permit, but not require fixed keys */
1307c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_addaddr)	/* add based on sockaddr */
1317c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask,
1327c478bd9Sstevel@tonic-gate 		     struct radix_node_head *head, struct radix_node nodes[]));
1337c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_addpkt)	/* add based on packet hdr */
1347c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask,
1357c478bd9Sstevel@tonic-gate 		     struct radix_node_head *head, struct radix_node nodes[]));
1367c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_deladdr)	/* remove based on sockaddr */
1377c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1387c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_delpkt)	/* remove based on packet hdr */
1397c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1407c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_matchaddr)	/* locate based on sockaddr */
1417c478bd9Sstevel@tonic-gate 		__P((void *v, struct radix_node_head *head));
1427c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_lookup)	/* locate based on sockaddr */
1437c478bd9Sstevel@tonic-gate 		__P((void *v, void *mask, struct radix_node_head *head));
1447c478bd9Sstevel@tonic-gate 	struct	radix_node *(*rnh_matchpkt)	/* locate based on packet hdr */
1457c478bd9Sstevel@tonic-gate 		__P((void *v, struct radix_node_head *head));
1467c478bd9Sstevel@tonic-gate 	int	(*rnh_walktree)			/* traverse tree */
1477c478bd9Sstevel@tonic-gate 		__P((struct radix_node_head *,
1487c478bd9Sstevel@tonic-gate 		     int (*)(struct radix_node *, void *), void *));
1497c478bd9Sstevel@tonic-gate 	struct	radix_node rnh_nodes[3];	/* empty tree for common case */
1507c478bd9Sstevel@tonic-gate };
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 
153ab25eeb5Syz #if defined(AIX)
154ab25eeb5Syz # undef Bcmp
155ab25eeb5Syz # undef Bzero
156ab25eeb5Syz # undef R_Malloc
157ab25eeb5Syz # undef Free
158ab25eeb5Syz #endif
1597c478bd9Sstevel@tonic-gate #define Bcmp(a, b, n)	bcmp(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n))
160ab25eeb5Syz #if defined(linux) && defined(_KERNEL)
161ab25eeb5Syz # define Bcopy(a, b, n)	memmove(((caddr_t)(b)), ((caddr_t)(a)), (unsigned)(n))
162ab25eeb5Syz #else
163ab25eeb5Syz # define Bcopy(a, b, n)	bcopy(((caddr_t)(a)), ((caddr_t)(b)), (unsigned)(n))
164ab25eeb5Syz #endif
1657c478bd9Sstevel@tonic-gate #define Bzero(p, n)		bzero((caddr_t)(p), (unsigned)(n));
1667c478bd9Sstevel@tonic-gate #define R_Malloc(p, t, n)	KMALLOCS(p, t, n)
1677c478bd9Sstevel@tonic-gate #define FreeS(p, z)		KFREES(p, z)
1687c478bd9Sstevel@tonic-gate #define Free(p)			KFREE(p)
1697c478bd9Sstevel@tonic-gate 
170ab25eeb5Syz #if (defined(__osf__) || defined(AIX) || (IRIX >= 60516)) && defined(_KERNEL)
171ab25eeb5Syz # define	rn_init		ipf_rn_init
172ab25eeb5Syz # define	rn_fini		ipf_rn_fini
173ab25eeb5Syz # define	rn_inithead	ipf_rn_inithead
174ab25eeb5Syz # define	rn_freehead	ipf_rn_freehead
175ab25eeb5Syz # define	rn_inithead0	ipf_rn_inithead0
176ab25eeb5Syz # define	rn_refines	ipf_rn_refines
177ab25eeb5Syz # define	rn_walktree	ipf_rn_walktree
178ab25eeb5Syz # define	rn_addmask	ipf_rn_addmask
179ab25eeb5Syz # define	rn_addroute	ipf_rn_addroute
180ab25eeb5Syz # define	rn_delete	ipf_rn_delete
181ab25eeb5Syz # define	rn_insert	ipf_rn_insert
182ab25eeb5Syz # define	rn_lookup	ipf_rn_lookup
183ab25eeb5Syz # define	rn_match	ipf_rn_match
184ab25eeb5Syz # define	rn_newpair	ipf_rn_newpair
185ab25eeb5Syz # define	rn_search	ipf_rn_search
186ab25eeb5Syz # define	rn_search_m	ipf_rn_search_m
187ab25eeb5Syz # define	max_keylen	ipf_maxkeylen
188ab25eeb5Syz # define	rn_mkfreelist	ipf_rn_mkfreelist
189ab25eeb5Syz # define	rn_zeros	ipf_rn_zeros
190ab25eeb5Syz # define	rn_ones		ipf_rn_ones
191ab25eeb5Syz # define	rn_satisfies_leaf	ipf_rn_satisfies_leaf
192ab25eeb5Syz # define	rn_lexobetter	ipf_rn_lexobetter
193ab25eeb5Syz # define	rn_new_radix_mask	ipf_rn_new_radix_mask
194ab25eeb5Syz # define	rn_freenode	ipf_rn_freenode
195ab25eeb5Syz #endif
196ab25eeb5Syz 
1977c478bd9Sstevel@tonic-gate void	 rn_init __P((void));
1987c478bd9Sstevel@tonic-gate void	 rn_fini __P((void));
1997c478bd9Sstevel@tonic-gate int	 rn_inithead __P((void **, int));
2007c478bd9Sstevel@tonic-gate void	 rn_freehead __P((struct radix_node_head *));
2017c478bd9Sstevel@tonic-gate int	 rn_inithead0 __P((struct radix_node_head *, int));
2027c478bd9Sstevel@tonic-gate int	 rn_refines __P((void *, void *));
2037c478bd9Sstevel@tonic-gate int	 rn_walktree __P((struct radix_node_head *,
2047c478bd9Sstevel@tonic-gate 			  int (*)(struct radix_node *, void *), void *));
2057c478bd9Sstevel@tonic-gate struct radix_node
2067c478bd9Sstevel@tonic-gate 	 *rn_addmask __P((void *, int, int)),
2077c478bd9Sstevel@tonic-gate 	 *rn_addroute __P((void *, void *, struct radix_node_head *,
2087c478bd9Sstevel@tonic-gate 			struct radix_node [2])),
2097c478bd9Sstevel@tonic-gate 	 *rn_delete __P((void *, void *, struct radix_node_head *)),
2107c478bd9Sstevel@tonic-gate 	 *rn_insert __P((void *, struct radix_node_head *, int *,
2117c478bd9Sstevel@tonic-gate 			struct radix_node [2])),
2127c478bd9Sstevel@tonic-gate 	 *rn_lookup __P((void *, void *, struct radix_node_head *)),
2137c478bd9Sstevel@tonic-gate 	 *rn_match __P((void *, struct radix_node_head *)),
2147c478bd9Sstevel@tonic-gate 	 *rn_newpair __P((void *, int, struct radix_node[2])),
2157c478bd9Sstevel@tonic-gate 	 *rn_search __P((void *, struct radix_node *)),
2167c478bd9Sstevel@tonic-gate 	 *rn_search_m __P((void *, struct radix_node *, void *));
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate #endif /* _NET_RADIX_H_ */
219*c793af95Ssangeeta #endif /* SOLARIS2 */
220*c793af95Ssangeeta #endif /* RADIX_IPF_H */
221