xref: /dragonfly/sys/net/radix.h (revision 4104d691)
1 /*
2  * Copyright (c) 1988, 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	@(#)radix.h	8.2 (Berkeley) 10/31/94
30  * $FreeBSD: src/sys/net/radix.h,v 1.16.2.1 2000/05/03 19:17:11 wollman Exp $
31  */
32 
33 #ifndef _NET_RADIX_H_
34 #define	_NET_RADIX_H_
35 
36 #include <sys/types.h>
37 
38 /*
39  * Radix search tree node layout.
40  */
41 
42 struct radix_node {
43 	struct	radix_mask *rn_mklist;	/* masks contained in subtree */
44 	struct	radix_node *rn_parent;	/* parent */
45 	short	rn_bit;			/* bit offset; -1-index(netmask) */
46 	char	rn_bmask;		/* node: mask for bit test */
47 	u_char	rn_flags;		/* enumerated next */
48 #define RNF_NORMAL	1		/* leaf contains normal route */
49 #define RNF_ROOT	2		/* leaf is root leaf for tree */
50 #define RNF_ACTIVE	4		/* This node is alive (for rtfree) */
51 	union {
52 		struct {			/* leaf only data: */
53 			const char *rn_Key;	/* object of search */
54 			const char *rn_Mask;	/* netmask, if present */
55 			struct	radix_node *rn_Dupedkey;
56 		} rn_leaf;
57 		struct {			/* node only data: */
58 			int	rn_Offset;	/* where to start compare */
59 			struct	radix_node *rn_L; /* progeny */
60 			struct	radix_node *rn_R; /* progeny */
61 		} rn_node;
62 	}		rn_u;
63 #ifdef RN_DEBUG
64 	int rn_info;
65 	struct radix_node *rn_twin;
66 	struct radix_node *rn_ybro;
67 #endif
68 };
69 
70 #define	rn_dupedkey	rn_u.rn_leaf.rn_Dupedkey
71 #define	rn_key		rn_u.rn_leaf.rn_Key
72 #define	rn_mask		rn_u.rn_leaf.rn_Mask
73 #define	rn_offset	rn_u.rn_node.rn_Offset
74 #define	rn_left		rn_u.rn_node.rn_L
75 #define	rn_right	rn_u.rn_node.rn_R
76 
77 /*
78  * We do this statically now because the dynamic initialization
79  * occurs too late and has an ordering problem w/ pf preloads
80  * vs protocol domains.
81  */
82 #define RN_MAXKEYLEN	32
83 #define RN_MAXKEYONES	{ -1, -1, -1, -1, -1, -1, -1, -1,	\
84 			  -1, -1, -1, -1, -1, -1, -1, -1,	\
85 			  -1, -1, -1, -1, -1, -1, -1, -1,	\
86 			  -1, -1, -1, -1, -1, -1, -1, -1 }
87 
88 /*
89  * Annotations to tree concerning potential routes applying to subtrees.
90  */
91 
92 struct radix_mask {
93 	short	rm_bit;			/* bit offset; -1-index(netmask) */
94 	char	rm_unused;		/* cf. rn_bmask */
95 	u_char	rm_flags;		/* cf. rn_flags */
96 	struct	radix_mask *rm_next;	/* list of more masks to try */
97 	union	{
98 		const char *rmu_mask;		/* the mask */
99 		struct	radix_node *rmu_leaf;	/* for normal routes */
100 	}	rm_rmu;
101 	int	rm_refs;		/* # of references to this struct */
102 };
103 
104 #define	rm_mask rm_rmu.rmu_mask
105 #define	rm_leaf rm_rmu.rmu_leaf		/* extra field would make 32 bytes */
106 
107 typedef int walktree_f_t (struct radix_node *, void *);
108 
109 struct radix_node_head {
110 	struct	radix_node *rnh_treetop;
111 
112 	/* add based on sockaddr */
113 	struct	radix_node *(*rnh_addaddr)
114 		    (const char *key, const char *mask,
115 		     struct radix_node_head *head, struct radix_node nodes[]);
116 
117 	/* remove based on sockaddr */
118 	struct	radix_node *(*rnh_deladdr)
119 		    (const char *key, const char *mask,
120 		     struct radix_node_head *head);
121 
122 	/* locate based on sockaddr */
123 	struct	radix_node *(*rnh_matchaddr)
124 		    (const char *key, struct radix_node_head *head);
125 
126 	/* locate based on sockaddr */
127 	struct	radix_node *(*rnh_lookup)
128 		    (const char *key, const char *mask,
129 		     struct radix_node_head *head);
130 
131 	/* traverse tree */
132 	int	(*rnh_walktree)
133 		    (struct radix_node_head *head, walktree_f_t *f, void *w);
134 
135 	/* traverse tree below a */
136 	int	(*rnh_walktree_from)
137 		    (struct radix_node_head *head, const char *a,
138 		     const char *m, walktree_f_t *f, void *w);
139 
140 	/*
141 	 * Do something when the last ref drops.
142 	 * A (*rnh_close)() routine
143 	 *	can clear RTF_UP
144 	 *	can remove a route from the radix tree
145 	 *	cannot change the reference count
146 	 *	cannot deallocate the route
147 	 */
148 	void	(*rnh_close)
149 		    (struct radix_node *rn, struct radix_node_head *head);
150 
151 	struct	radix_node rnh_nodes[3];	/* empty tree for common case */
152 
153 	/* unused entries */
154 	int	rnh_addrsize;		/* permit, but not require fixed keys */
155 	int	rnh_pktsize;		/* permit, but not require fixed keys */
156 	struct	radix_node *(*rnh_addpkt)	/* add based on packet hdr */
157 		    (const void *v, const char *mask,
158 		     struct radix_node_head *head, struct radix_node nodes[]);
159 	struct	radix_node *(*rnh_delpkt)	/* remove based on packet hdr */
160 		    (const void *v, const char *mask,
161 		     struct radix_node_head *head);
162 
163 	/* traverse tree starting from a */
164 	int	(*rnh_walktree_at)
165 		    (struct radix_node_head *head, const char *a, const char *m,
166 		     walktree_f_t *f, void *w);
167 
168 	struct radix_node_head *rnh_maskhead;
169 };
170 
171 #ifdef _KERNEL
172 
173 #ifdef MALLOC_DECLARE
174 MALLOC_DECLARE(M_RTABLE);
175 #endif
176 
177 #define R_Malloc(p, t, n) \
178 	(p = (t) kmalloc((n), M_RTABLE, M_INTWAIT | M_NULLOK))
179 #define R_Free(p)	kfree(p, M_RTABLE)
180 
181 #else /* !_KERNEL */
182 
183 #include <stdbool.h>
184 
185 #define R_Malloc(p, t, n)	(p = (t) malloc((n)))
186 #define R_Free(p)		free(p)
187 
188 #endif /* _KERNEL */
189 
190 void			 rn_init(void);
191 int			 rn_inithead(void **head,
192 				     struct radix_node_head *maskhead, int off);
193 struct radix_node_head	*rn_cpumaskhead(int cpu);
194 bool			 rn_refines(const char *m, const char *n);
195 struct radix_node	*rn_addmask(const char *mask, bool search, int skip,
196 				    struct radix_node_head *maskhead);
197 struct radix_node	*rn_addroute(const char *key, const char *netmask,
198 				     struct radix_node_head *head,
199 				     struct radix_node nodes[2]);
200 struct radix_node	*rn_delete(const char *key, const char *netmask,
201 				   struct radix_node_head *head);
202 struct radix_node	*rn_lookup(const char *key, const char *mask,
203 				   struct radix_node_head *head);
204 struct radix_node	*rn_match(const char *key,
205 				  struct radix_node_head *head);
206 
207 #endif /* _NET_RADIX_H_ */
208