2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include <sys/libkern.h>
34 #include <sys/param.h>
36 #include <sys/rwlock.h>
40 #include <sys/types.h>
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
49 #include <netinet/libalias/alias_local.h>
50 #include <netinet/libalias/alias_mod.h>
52 #include "alias_local.h"
53 #include "alias_mod.h"
56 /* Protocol and userland module handlers chains. */
57 static TAILQ_HEAD(handler_chain, proto_handler) handler_chain =
58 TAILQ_HEAD_INITIALIZER(handler_chain);
61 attach_handler(struct proto_handler *p)
63 struct proto_handler *b;
65 TAILQ_FOREACH(b, &handler_chain, link) {
66 if ((b->pri == p->pri) &&
68 (b->proto == p->proto))
70 if (b->pri > p->pri) {
71 TAILQ_INSERT_BEFORE(b, p, link);
76 TAILQ_INSERT_TAIL(&handler_chain, p, link);
82 LibAliasAttachHandlers(struct proto_handler *p)
86 while (p->dir != NODIR) {
87 error = attach_handler(p);
96 /* XXXGL: should be void, but no good reason to break ABI */
98 LibAliasDetachHandlers(struct proto_handler *p)
101 while (p->dir != NODIR) {
102 TAILQ_REMOVE(&handler_chain, p, link);
110 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip,
111 struct alias_data *ad)
113 struct proto_handler *p;
115 TAILQ_FOREACH(p, &handler_chain, link)
116 if ((p->dir & dir) && (p->proto & proto) &&
117 p->fingerprint(la, ad) == 0)
118 return (p->protohandler(la, ip, ad));
123 struct proto_handler *
127 return (TAILQ_FIRST(&handler_chain));
131 /* Dll manipulation code - this code is not thread safe... */
132 SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain);
134 attach_dll(struct dll *p)
138 SLIST_FOREACH(b, &dll_chain, next) {
139 if (!strncmp(b->name, p->name, DLL_LEN))
140 return (EEXIST); /* Dll name conflict. */
142 SLIST_INSERT_HEAD(&dll_chain, p, next);
149 struct dll *b, *b_tmp;
154 SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
155 if (!strncmp(b->name, p, DLL_LEN)) {
156 SLIST_REMOVE(&dll_chain, b, dll, next);
168 t = SLIST_FIRST(&dll_chain);
171 SLIST_REMOVE_HEAD(&dll_chain, next);
174 #endif /* !_KERNEL */