xref: /freebsd/sys/netinet/libalias/alias_mod.c (revision 389e4940069316fe667ffa263fa7d6390d0a960f)
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 
101 	while (p->dir != NODIR) {
102 		TAILQ_REMOVE(&handler_chain, p, link);
103 		p++;
104 	}
105 
106 	return (0);
107 }
108 
109 int
110 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip,
111     struct alias_data *ad)
112 {
113 	struct proto_handler *p;
114 
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));
119 
120 	return (ENOENT);
121 }
122 
123 struct proto_handler *
124 first_handler(void)
125 {
126 
127 	return (TAILQ_FIRST(&handler_chain));
128 }
129 
130 #ifndef _KERNEL
131 /* Dll manipulation code - this code is not thread safe... */
132 SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain);
133 int
134 attach_dll(struct dll *p)
135 {
136 	struct dll *b;
137 
138 	SLIST_FOREACH(b, &dll_chain, next) {
139 		if (!strncmp(b->name, p->name, DLL_LEN))
140 			return (EEXIST); /* Dll name conflict. */
141 	}
142 	SLIST_INSERT_HEAD(&dll_chain, p, next);
143 	return (0);
144 }
145 
146 void *
147 detach_dll(char *p)
148 {
149 	struct dll *b, *b_tmp;
150 	void *error;
151 
152 	b = NULL;
153 	error = NULL;
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);
157 			error = b;
158 			break;
159 		}
160 	return (error);
161 }
162 
163 struct dll *
164 walk_dll_chain(void)
165 {
166 	struct dll *t;
167 
168 	t = SLIST_FIRST(&dll_chain);
169 	if (t == NULL)
170 		return (NULL);
171 	SLIST_REMOVE_HEAD(&dll_chain, next);
172 	return (t);
173 }
174 #endif /* !_KERNEL */
175