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