xref: /freebsd/sys/netinet/libalias/alias_mod.c (revision dc318a4ffabcbfa23bb56a33403aad36e6de30af)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 <sys/param.h>
35 #include <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 static TAILQ_HEAD(handler_chain, proto_handler) handler_chain =
58     TAILQ_HEAD_INITIALIZER(handler_chain);
59 
60 static int
61 attach_handler(struct proto_handler *p)
62 {
63 	struct proto_handler *b;
64 
65 	TAILQ_FOREACH(b, &handler_chain, link) {
66 		if ((b->pri == p->pri) &&
67 		    (b->dir == p->dir) &&
68 		    (b->proto == p->proto))
69 			return (EEXIST);
70 		if (b->pri > p->pri) {
71 			TAILQ_INSERT_BEFORE(b, p, link);
72 			return (0);
73 		}
74 	}
75 
76 	TAILQ_INSERT_TAIL(&handler_chain, p, link);
77 
78 	return (0);
79 }
80 
81 int
82 LibAliasAttachHandlers(struct proto_handler *p)
83 {
84 	int error;
85 
86 	while (p->dir != NODIR) {
87 		error = attach_handler(p);
88 		if (error)
89 			return (error);
90 		p++;
91 	}
92 
93 	return (0);
94 }
95 
96 /* XXXGL: should be void, but no good reason to break ABI */
97 int
98 LibAliasDetachHandlers(struct proto_handler *p)
99 {
100 	while (p->dir != NODIR) {
101 		TAILQ_REMOVE(&handler_chain, p, link);
102 		p++;
103 	}
104 
105 	return (0);
106 }
107 
108 int
109 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip,
110     struct alias_data *ad)
111 {
112 	struct proto_handler *p;
113 
114 	TAILQ_FOREACH(p, &handler_chain, link)
115 		if ((p->dir & dir) && (p->proto & proto) &&
116 		    p->fingerprint(la, ad) == 0)
117 			return (p->protohandler(la, ip, ad));
118 
119 	return (ENOENT);
120 }
121 
122 struct proto_handler *
123 first_handler(void)
124 {
125 	return (TAILQ_FIRST(&handler_chain));
126 }
127 
128 #ifndef _KERNEL
129 /* Dll manipulation code - this code is not thread safe... */
130 static SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain);
131 int
132 attach_dll(struct dll *p)
133 {
134 	struct dll *b;
135 
136 	SLIST_FOREACH(b, &dll_chain, next) {
137 		if (!strncmp(b->name, p->name, DLL_LEN))
138 			return (EEXIST); /* Dll name conflict. */
139 	}
140 	SLIST_INSERT_HEAD(&dll_chain, p, next);
141 	return (0);
142 }
143 
144 void *
145 detach_dll(char *p)
146 {
147 	struct dll *b, *b_tmp;
148 	void *error;
149 
150 	b = NULL;
151 	error = NULL;
152 	SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
153 		if (!strncmp(b->name, p, DLL_LEN)) {
154 			SLIST_REMOVE(&dll_chain, b, dll, next);
155 			error = b;
156 			break;
157 		}
158 	return (error);
159 }
160 
161 struct dll *
162 walk_dll_chain(void)
163 {
164 	struct dll *t;
165 
166 	t = SLIST_FIRST(&dll_chain);
167 	if (t == NULL)
168 		return (NULL);
169 	SLIST_REMOVE_HEAD(&dll_chain, next);
170 	return (t);
171 }
172 #endif /* !_KERNEL */
173