1 | #include <machine/rtems-bsd-kernel-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org> |
---|
5 | * All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions |
---|
9 | * are met: |
---|
10 | * 1. Redistributions of source code must retain the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer. |
---|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer in the |
---|
14 | * documentation and/or other materials provided with the distribution. |
---|
15 | * |
---|
16 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
17 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
20 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
21 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
22 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
24 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
25 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
26 | * SUCH DAMAGE. |
---|
27 | * |
---|
28 | */ |
---|
29 | #include <sys/cdefs.h> |
---|
30 | __FBSDID("$FreeBSD$"); |
---|
31 | |
---|
32 | #ifdef _KERNEL |
---|
33 | #include <sys/libkern.h> |
---|
34 | #include <rtems/bsd/sys/param.h> |
---|
35 | #include <rtems/bsd/sys/lock.h> |
---|
36 | #include <sys/rwlock.h> |
---|
37 | #else |
---|
38 | #include <stdio.h> |
---|
39 | #include <string.h> |
---|
40 | #include <sys/types.h> |
---|
41 | #include <errno.h> |
---|
42 | #endif |
---|
43 | |
---|
44 | #include <netinet/in_systm.h> |
---|
45 | #include <netinet/in.h> |
---|
46 | #include <netinet/ip.h> |
---|
47 | |
---|
48 | #ifdef _KERNEL |
---|
49 | #include <netinet/libalias/alias_local.h> |
---|
50 | #include <netinet/libalias/alias_mod.h> |
---|
51 | #else |
---|
52 | #include "alias_local.h" |
---|
53 | #include "alias_mod.h" |
---|
54 | #endif |
---|
55 | |
---|
56 | /* Protocol and userland module handlers chains. */ |
---|
57 | LIST_HEAD(handler_chain, proto_handler) handler_chain = LIST_HEAD_INITIALIZER(handler_chain); |
---|
58 | #ifdef _KERNEL |
---|
59 | struct rwlock handler_rw; |
---|
60 | #endif |
---|
61 | SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain); |
---|
62 | |
---|
63 | #ifdef _KERNEL |
---|
64 | |
---|
65 | #define LIBALIAS_RWLOCK_INIT() \ |
---|
66 | rw_init(&handler_rw, "Libalias_modules_rwlock") |
---|
67 | #define LIBALIAS_RWLOCK_DESTROY() rw_destroy(&handler_rw) |
---|
68 | #define LIBALIAS_WLOCK_ASSERT() \ |
---|
69 | rw_assert(&handler_rw, RA_WLOCKED) |
---|
70 | |
---|
71 | static __inline void |
---|
72 | LIBALIAS_RLOCK(void) |
---|
73 | { |
---|
74 | rw_rlock(&handler_rw); |
---|
75 | } |
---|
76 | |
---|
77 | static __inline void |
---|
78 | LIBALIAS_RUNLOCK(void) |
---|
79 | { |
---|
80 | rw_runlock(&handler_rw); |
---|
81 | } |
---|
82 | |
---|
83 | static __inline void |
---|
84 | LIBALIAS_WLOCK(void) |
---|
85 | { |
---|
86 | rw_wlock(&handler_rw); |
---|
87 | } |
---|
88 | |
---|
89 | static __inline void |
---|
90 | LIBALIAS_WUNLOCK(void) |
---|
91 | { |
---|
92 | rw_wunlock(&handler_rw); |
---|
93 | } |
---|
94 | |
---|
95 | static void |
---|
96 | _handler_chain_init(void) |
---|
97 | { |
---|
98 | |
---|
99 | if (!rw_initialized(&handler_rw)) |
---|
100 | LIBALIAS_RWLOCK_INIT(); |
---|
101 | } |
---|
102 | |
---|
103 | static void |
---|
104 | _handler_chain_destroy(void) |
---|
105 | { |
---|
106 | |
---|
107 | if (rw_initialized(&handler_rw)) |
---|
108 | LIBALIAS_RWLOCK_DESTROY(); |
---|
109 | } |
---|
110 | |
---|
111 | #else |
---|
112 | #define LIBALIAS_RWLOCK_INIT() ; |
---|
113 | #define LIBALIAS_RWLOCK_DESTROY() ; |
---|
114 | #define LIBALIAS_WLOCK_ASSERT() ; |
---|
115 | #define LIBALIAS_RLOCK() ; |
---|
116 | #define LIBALIAS_RUNLOCK() ; |
---|
117 | #define LIBALIAS_WLOCK() ; |
---|
118 | #define LIBALIAS_WUNLOCK() ; |
---|
119 | #define _handler_chain_init() ; |
---|
120 | #define _handler_chain_destroy() ; |
---|
121 | #endif |
---|
122 | |
---|
123 | void |
---|
124 | handler_chain_init(void) |
---|
125 | { |
---|
126 | _handler_chain_init(); |
---|
127 | } |
---|
128 | |
---|
129 | void |
---|
130 | handler_chain_destroy(void) |
---|
131 | { |
---|
132 | _handler_chain_destroy(); |
---|
133 | } |
---|
134 | |
---|
135 | static int |
---|
136 | _attach_handler(struct proto_handler *p) |
---|
137 | { |
---|
138 | struct proto_handler *b; |
---|
139 | |
---|
140 | LIBALIAS_WLOCK_ASSERT(); |
---|
141 | b = NULL; |
---|
142 | LIST_FOREACH(b, &handler_chain, entries) { |
---|
143 | if ((b->pri == p->pri) && |
---|
144 | (b->dir == p->dir) && |
---|
145 | (b->proto == p->proto)) |
---|
146 | return (EEXIST); /* Priority conflict. */ |
---|
147 | if (b->pri > p->pri) { |
---|
148 | LIST_INSERT_BEFORE(b, p, entries); |
---|
149 | return (0); |
---|
150 | } |
---|
151 | } |
---|
152 | /* End of list or found right position, inserts here. */ |
---|
153 | if (b) |
---|
154 | LIST_INSERT_AFTER(b, p, entries); |
---|
155 | else |
---|
156 | LIST_INSERT_HEAD(&handler_chain, p, entries); |
---|
157 | return (0); |
---|
158 | } |
---|
159 | |
---|
160 | static int |
---|
161 | _detach_handler(struct proto_handler *p) |
---|
162 | { |
---|
163 | struct proto_handler *b, *b_tmp; |
---|
164 | |
---|
165 | LIBALIAS_WLOCK_ASSERT(); |
---|
166 | LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) { |
---|
167 | if (b == p) { |
---|
168 | LIST_REMOVE(b, entries); |
---|
169 | return (0); |
---|
170 | } |
---|
171 | } |
---|
172 | return (ENOENT); /* Handler not found. */ |
---|
173 | } |
---|
174 | |
---|
175 | int |
---|
176 | LibAliasAttachHandlers(struct proto_handler *_p) |
---|
177 | { |
---|
178 | int i, error; |
---|
179 | |
---|
180 | LIBALIAS_WLOCK(); |
---|
181 | error = -1; |
---|
182 | for (i = 0; 1; i++) { |
---|
183 | if (*((int *)&_p[i]) == EOH) |
---|
184 | break; |
---|
185 | error = _attach_handler(&_p[i]); |
---|
186 | if (error != 0) |
---|
187 | break; |
---|
188 | } |
---|
189 | LIBALIAS_WUNLOCK(); |
---|
190 | return (error); |
---|
191 | } |
---|
192 | |
---|
193 | int |
---|
194 | LibAliasDetachHandlers(struct proto_handler *_p) |
---|
195 | { |
---|
196 | int i, error; |
---|
197 | |
---|
198 | LIBALIAS_WLOCK(); |
---|
199 | error = -1; |
---|
200 | for (i = 0; 1; i++) { |
---|
201 | if (*((int *)&_p[i]) == EOH) |
---|
202 | break; |
---|
203 | error = _detach_handler(&_p[i]); |
---|
204 | if (error != 0) |
---|
205 | break; |
---|
206 | } |
---|
207 | LIBALIAS_WUNLOCK(); |
---|
208 | return (error); |
---|
209 | } |
---|
210 | |
---|
211 | int |
---|
212 | detach_handler(struct proto_handler *_p) |
---|
213 | { |
---|
214 | int error; |
---|
215 | |
---|
216 | LIBALIAS_WLOCK(); |
---|
217 | error = -1; |
---|
218 | error = _detach_handler(_p); |
---|
219 | LIBALIAS_WUNLOCK(); |
---|
220 | return (error); |
---|
221 | } |
---|
222 | |
---|
223 | int |
---|
224 | find_handler(int8_t dir, int8_t proto, struct libalias *la, __unused struct ip *pip, |
---|
225 | struct alias_data *ad) |
---|
226 | { |
---|
227 | struct proto_handler *p; |
---|
228 | int error; |
---|
229 | |
---|
230 | LIBALIAS_RLOCK(); |
---|
231 | error = ENOENT; |
---|
232 | LIST_FOREACH(p, &handler_chain, entries) { |
---|
233 | if ((p->dir & dir) && (p->proto & proto)) |
---|
234 | if (p->fingerprint(la, ad) == 0) { |
---|
235 | error = p->protohandler(la, pip, ad); |
---|
236 | break; |
---|
237 | } |
---|
238 | } |
---|
239 | LIBALIAS_RUNLOCK(); |
---|
240 | return (error); |
---|
241 | } |
---|
242 | |
---|
243 | struct proto_handler * |
---|
244 | first_handler(void) |
---|
245 | { |
---|
246 | |
---|
247 | return (LIST_FIRST(&handler_chain)); |
---|
248 | } |
---|
249 | |
---|
250 | /* Dll manipulation code - this code is not thread safe... */ |
---|
251 | |
---|
252 | int |
---|
253 | attach_dll(struct dll *p) |
---|
254 | { |
---|
255 | struct dll *b; |
---|
256 | |
---|
257 | SLIST_FOREACH(b, &dll_chain, next) { |
---|
258 | if (!strncmp(b->name, p->name, DLL_LEN)) |
---|
259 | return (EEXIST); /* Dll name conflict. */ |
---|
260 | } |
---|
261 | SLIST_INSERT_HEAD(&dll_chain, p, next); |
---|
262 | return (0); |
---|
263 | } |
---|
264 | |
---|
265 | void * |
---|
266 | detach_dll(char *p) |
---|
267 | { |
---|
268 | struct dll *b, *b_tmp; |
---|
269 | void *error; |
---|
270 | |
---|
271 | b = NULL; |
---|
272 | error = NULL; |
---|
273 | SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp) |
---|
274 | if (!strncmp(b->name, p, DLL_LEN)) { |
---|
275 | SLIST_REMOVE(&dll_chain, b, dll, next); |
---|
276 | error = b; |
---|
277 | break; |
---|
278 | } |
---|
279 | return (error); |
---|
280 | } |
---|
281 | |
---|
282 | struct dll * |
---|
283 | walk_dll_chain(void) |
---|
284 | { |
---|
285 | struct dll *t; |
---|
286 | |
---|
287 | t = SLIST_FIRST(&dll_chain); |
---|
288 | if (t == NULL) |
---|
289 | return (NULL); |
---|
290 | SLIST_REMOVE_HEAD(&dll_chain, next); |
---|
291 | return (t); |
---|
292 | } |
---|