2 * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 #include <sys/libkern.h>
32 #include <sys/param.h>
34 #include <sys/rwlock.h>
38 #include <sys/types.h>
42 #include <netinet/in_systm.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
47 #include <netinet/libalias/alias_local.h>
48 #include <netinet/libalias/alias_mod.h>
50 #include "alias_local.h"
51 #include "alias_mod.h"
54 /* Protocol and userland module handlers chains. */
55 LIST_HEAD(handler_chain, proto_handler) handler_chain = LIST_HEAD_INITIALIZER(handler_chain);
57 struct rwlock handler_rw;
59 SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain);
63 #define LIBALIAS_RWLOCK_INIT() \
64 rw_init(&handler_rw, "Libalias_modules_rwlock")
65 #define LIBALIAS_RWLOCK_DESTROY() rw_destroy(&handler_rw)
66 #define LIBALIAS_WLOCK_ASSERT() \
67 rw_assert(&handler_rw, RA_WLOCKED)
72 rw_rlock(&handler_rw);
76 LIBALIAS_RUNLOCK(void)
78 rw_runlock(&handler_rw);
84 rw_wlock(&handler_rw);
88 LIBALIAS_WUNLOCK(void)
90 rw_wunlock(&handler_rw);
94 _handler_chain_init(void)
97 if (!rw_initialized(&handler_rw))
98 LIBALIAS_RWLOCK_INIT();
102 _handler_chain_destroy(void)
105 if (rw_initialized(&handler_rw))
106 LIBALIAS_RWLOCK_DESTROY();
110 #define LIBALIAS_RWLOCK_INIT() ;
111 #define LIBALIAS_RWLOCK_DESTROY() ;
112 #define LIBALIAS_WLOCK_ASSERT() ;
113 #define LIBALIAS_RLOCK() ;
114 #define LIBALIAS_RUNLOCK() ;
115 #define LIBALIAS_WLOCK() ;
116 #define LIBALIAS_WUNLOCK() ;
117 #define _handler_chain_init() ;
118 #define _handler_chain_destroy() ;
122 handler_chain_init(void)
124 _handler_chain_init();
128 handler_chain_destroy(void)
130 _handler_chain_destroy();
134 _attach_handler(struct proto_handler *p)
136 struct proto_handler *b;
138 LIBALIAS_WLOCK_ASSERT();
140 LIST_FOREACH(b, &handler_chain, entries) {
141 if ((b->pri == p->pri) &&
142 (b->dir == p->dir) &&
143 (b->proto == p->proto))
144 return (EEXIST); /* Priority conflict. */
145 if (b->pri > p->pri) {
146 LIST_INSERT_BEFORE(b, p, entries);
150 /* End of list or found right position, inserts here. */
152 LIST_INSERT_AFTER(b, p, entries);
154 LIST_INSERT_HEAD(&handler_chain, p, entries);
159 _detach_handler(struct proto_handler *p)
161 struct proto_handler *b, *b_tmp;
163 LIBALIAS_WLOCK_ASSERT();
164 LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) {
166 LIST_REMOVE(b, entries);
170 return (ENOENT); /* Handler not found. */
174 LibAliasAttachHandlers(struct proto_handler *_p)
180 for (i = 0; 1; i++) {
181 if (*((int *)&_p[i]) == EOH)
183 error = _attach_handler(&_p[i]);
192 LibAliasDetachHandlers(struct proto_handler *_p)
198 for (i = 0; 1; i++) {
199 if (*((int *)&_p[i]) == EOH)
201 error = _detach_handler(&_p[i]);
210 detach_handler(struct proto_handler *_p)
216 error = _detach_handler(_p);
222 find_handler(int8_t dir, int8_t proto, struct libalias *la, __unused struct ip *pip,
223 struct alias_data *ad)
225 struct proto_handler *p;
230 LIST_FOREACH(p, &handler_chain, entries) {
231 if ((p->dir & dir) && (p->proto & proto))
232 if (p->fingerprint(la, ad) == 0) {
233 error = p->protohandler(la, pip, ad);
241 struct proto_handler *
245 return (LIST_FIRST(&handler_chain));
248 /* Dll manipulation code - this code is not thread safe... */
251 attach_dll(struct dll *p)
255 SLIST_FOREACH(b, &dll_chain, next) {
256 if (!strncmp(b->name, p->name, DLL_LEN))
257 return (EEXIST); /* Dll name conflict. */
259 SLIST_INSERT_HEAD(&dll_chain, p, next);
266 struct dll *b, *b_tmp;
271 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
272 if (!strncmp(b->name, p, DLL_LEN)) {
273 SLIST_REMOVE(&dll_chain, b, dll, next);
285 t = SLIST_FIRST(&dll_chain);
288 SLIST_REMOVE_HEAD(&dll_chain, next);