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
|
---|
116 | #define LIBALIAS_RWLOCK_INIT() ;
|
---|
117 | #define LIBALIAS_RWLOCK_DESTROY() ;
|
---|
118 | #define LIBALIAS_WLOCK_ASSERT() ;
|
---|
119 | #define LIBALIAS_RLOCK() ;
|
---|
120 | #define LIBALIAS_RUNLOCK() ;
|
---|
121 | #define LIBALIAS_WLOCK() ;
|
---|
122 | #define LIBALIAS_WUNLOCK() ;
|
---|
123 | #define _handler_chain_init() ;
|
---|
124 | #define _handler_chain_destroy() ;
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | void
|
---|
128 | handler_chain_init(void)
|
---|
129 | {
|
---|
130 | _handler_chain_init();
|
---|
131 | }
|
---|
132 |
|
---|
133 | void
|
---|
134 | handler_chain_destroy(void)
|
---|
135 | {
|
---|
136 | _handler_chain_destroy();
|
---|
137 | }
|
---|
138 |
|
---|
139 | static int
|
---|
140 | #ifdef VBOX
|
---|
141 | _attach_handler(PNATState pData, struct proto_handler *p)
|
---|
142 | #else
|
---|
143 | _attach_handler(struct proto_handler *p)
|
---|
144 | #endif
|
---|
145 | {
|
---|
146 | struct proto_handler *b = NULL;
|
---|
147 |
|
---|
148 | LIBALIAS_WLOCK_ASSERT();
|
---|
149 | LIST_FOREACH(b, &handler_chain, entries) {
|
---|
150 | if ((b->pri == p->pri) &&
|
---|
151 | (b->dir == p->dir) &&
|
---|
152 | (b->proto == p->proto))
|
---|
153 | return (EEXIST); /* Priority conflict. */
|
---|
154 | if (b->pri > p->pri) {
|
---|
155 | LIST_INSERT_BEFORE(b, p, entries);
|
---|
156 | return (0);
|
---|
157 | }
|
---|
158 | }
|
---|
159 | /* End of list or found right position, inserts here. */
|
---|
160 | if (b)
|
---|
161 | LIST_INSERT_AFTER(b, p, entries);
|
---|
162 | else
|
---|
163 | LIST_INSERT_HEAD(&handler_chain, p, entries);
|
---|
164 | return (0);
|
---|
165 | }
|
---|
166 |
|
---|
167 | static int
|
---|
168 | #ifdef VBOX
|
---|
169 | _detach_handler(PNATState pData, struct proto_handler *p)
|
---|
170 | #else
|
---|
171 | _detach_handler(struct proto_handler *p)
|
---|
172 | #endif
|
---|
173 | {
|
---|
174 | struct proto_handler *b, *b_tmp;;
|
---|
175 |
|
---|
176 | LIBALIAS_WLOCK_ASSERT();
|
---|
177 | LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) {
|
---|
178 | if (b == p) {
|
---|
179 | LIST_REMOVE(b, entries);
|
---|
180 | return (0);
|
---|
181 | }
|
---|
182 | }
|
---|
183 | return (ENOENT); /* Handler not found. */
|
---|
184 | }
|
---|
185 |
|
---|
186 | int
|
---|
187 | #ifdef VBOX
|
---|
188 | LibAliasAttachHandlers(PNATState pData, struct proto_handler *_p)
|
---|
189 | #else
|
---|
190 | LibAliasAttachHandlers(struct proto_handler *_p)
|
---|
191 | #endif
|
---|
192 | {
|
---|
193 | int i, error = -1;
|
---|
194 |
|
---|
195 | LIBALIAS_WLOCK();
|
---|
196 | for (i=0; 1; i++) {
|
---|
197 | if (*((int *)&_p[i]) == EOH)
|
---|
198 | break;
|
---|
199 | #ifdef VBOX
|
---|
200 | error = _attach_handler(pData, &_p[i]);
|
---|
201 | #else
|
---|
202 | error = _attach_handler(&_p[i]);
|
---|
203 | #endif
|
---|
204 | if (error != 0)
|
---|
205 | break;
|
---|
206 | }
|
---|
207 | LIBALIAS_WUNLOCK();
|
---|
208 | return (error);
|
---|
209 | }
|
---|
210 |
|
---|
211 | int
|
---|
212 | #ifdef VBOX
|
---|
213 | LibAliasDetachHandlers(PNATState pData, struct proto_handler *_p)
|
---|
214 | #else
|
---|
215 | LibAliasDetachHandlers(struct proto_handler *_p)
|
---|
216 | #endif
|
---|
217 | {
|
---|
218 | int i, error = -1;
|
---|
219 |
|
---|
220 | LIBALIAS_WLOCK();
|
---|
221 | for (i=0; 1; i++) {
|
---|
222 | if (*((int *)&_p[i]) == EOH)
|
---|
223 | break;
|
---|
224 | #ifdef VBOX
|
---|
225 | error = _detach_handler(pData, &_p[i]);
|
---|
226 | #else
|
---|
227 | error = _detach_handler(&_p[i]);
|
---|
228 | #endif
|
---|
229 | if (error != 0)
|
---|
230 | break;
|
---|
231 | }
|
---|
232 | LIBALIAS_WUNLOCK();
|
---|
233 | return (error);
|
---|
234 | }
|
---|
235 |
|
---|
236 | int
|
---|
237 | #ifdef VBOX
|
---|
238 | detach_handler(PNATState pData, struct proto_handler *_p)
|
---|
239 | #else
|
---|
240 | detach_handler(struct proto_handler *_p)
|
---|
241 | #endif
|
---|
242 | {
|
---|
243 | int error = -1;
|
---|
244 |
|
---|
245 | LIBALIAS_WLOCK();
|
---|
246 | #ifdef VBOX
|
---|
247 | error = _detach_handler(pData, _p);
|
---|
248 | #else
|
---|
249 | error = _detach_handler(_p);
|
---|
250 | #endif
|
---|
251 | LIBALIAS_WUNLOCK();
|
---|
252 | return (error);
|
---|
253 | }
|
---|
254 |
|
---|
255 | int
|
---|
256 | find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *pip,
|
---|
257 | struct alias_data *ad)
|
---|
258 | {
|
---|
259 | #ifdef VBOX
|
---|
260 | PNATState pData = la->pData;
|
---|
261 | #endif
|
---|
262 | struct proto_handler *p;
|
---|
263 | int error = ENOENT;
|
---|
264 |
|
---|
265 | LIBALIAS_RLOCK();
|
---|
266 |
|
---|
267 | LIST_FOREACH(p, &handler_chain, entries) {
|
---|
268 | if ((p->dir & dir) && (p->proto & proto))
|
---|
269 | if (p->fingerprint(la, pip, ad) == 0) {
|
---|
270 | error = p->protohandler(la, pip, ad);
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | }
|
---|
274 | LIBALIAS_RUNLOCK();
|
---|
275 | return (error);
|
---|
276 | }
|
---|
277 |
|
---|
278 | struct proto_handler *
|
---|
279 | #ifdef VBOX
|
---|
280 | first_handler(PNATState pData)
|
---|
281 | #else
|
---|
282 | first_handler(void)
|
---|
283 | #endif
|
---|
284 | {
|
---|
285 |
|
---|
286 | return (LIST_FIRST(&handler_chain));
|
---|
287 | }
|
---|
288 |
|
---|
289 | /* Dll manipulation code - this code is not thread safe... */
|
---|
290 |
|
---|
291 | int
|
---|
292 | attach_dll(struct dll *p)
|
---|
293 | {
|
---|
294 | struct dll *b;
|
---|
295 |
|
---|
296 | SLIST_FOREACH(b, &dll_chain, next) {
|
---|
297 | if (!strncmp(b->name, p->name, DLL_LEN))
|
---|
298 | return (EEXIST); /* Dll name conflict. */
|
---|
299 | }
|
---|
300 | SLIST_INSERT_HEAD(&dll_chain, p, next);
|
---|
301 | return (0);
|
---|
302 | }
|
---|
303 |
|
---|
304 | void *
|
---|
305 | detach_dll(char *p)
|
---|
306 | {
|
---|
307 | struct dll *b = NULL, *b_tmp;
|
---|
308 | void *error = NULL;
|
---|
309 |
|
---|
310 | SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
|
---|
311 | if (!strncmp(b->name, p, DLL_LEN)) {
|
---|
312 | SLIST_REMOVE(&dll_chain, b, dll, next);
|
---|
313 | error = b;
|
---|
314 | break;
|
---|
315 | }
|
---|
316 | return (error);
|
---|
317 | }
|
---|
318 |
|
---|
319 | struct dll *
|
---|
320 | walk_dll_chain(void)
|
---|
321 | {
|
---|
322 | struct dll *t;
|
---|
323 |
|
---|
324 | t = SLIST_FIRST(&dll_chain);
|
---|
325 | if (t == NULL)
|
---|
326 | return (NULL);
|
---|
327 | SLIST_REMOVE_HEAD(&dll_chain, next);
|
---|
328 | return (t);
|
---|
329 | }
|
---|