xref: /freebsd/sys/netinet/libalias/alias_mod.c (revision 4436b51dff5736e74da464946049ea6899a88938)
1 /*-
2  * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
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 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #ifdef _KERNEL
31 #include <sys/libkern.h>
32 #include <sys/param.h>
33 #include <sys/lock.h>
34 #include <sys/rwlock.h>
35 #else
36 #include <stdio.h>
37 #include <string.h>
38 #include <sys/types.h>
39 #include <errno.h>
40 #endif
41 
42 #include <netinet/in_systm.h>
43 #include <netinet/in.h>
44 #include <netinet/ip.h>
45 
46 #ifdef _KERNEL
47 #include <netinet/libalias/alias_local.h>
48 #include <netinet/libalias/alias_mod.h>
49 #else
50 #include "alias_local.h"
51 #include "alias_mod.h"
52 #endif
53 
54 /* Protocol and userland module handlers chains. */
55 static TAILQ_HEAD(handler_chain, proto_handler) handler_chain =
56     TAILQ_HEAD_INITIALIZER(handler_chain);
57 
58 static int
59 attach_handler(struct proto_handler *p)
60 {
61 	struct proto_handler *b;
62 
63 	TAILQ_FOREACH(b, &handler_chain, link) {
64 		if ((b->pri == p->pri) &&
65 		    (b->dir == p->dir) &&
66 		    (b->proto == p->proto))
67 			return (EEXIST);
68 		if (b->pri > p->pri) {
69 			TAILQ_INSERT_BEFORE(b, p, link);
70 			return (0);
71 		}
72 	}
73 
74 	TAILQ_INSERT_TAIL(&handler_chain, p, link);
75 
76 	return (0);
77 }
78 
79 int
80 LibAliasAttachHandlers(struct proto_handler *p)
81 {
82 	int error;
83 
84 	while (p->dir != NODIR) {
85 		error = attach_handler(p);
86 		if (error)
87 			return (error);
88 		p++;
89 	}
90 
91 	return (0);
92 }
93 
94 /* XXXGL: should be void, but no good reason to break ABI */
95 int
96 LibAliasDetachHandlers(struct proto_handler *p)
97 {
98 
99 	while (p->dir != NODIR) {
100 		TAILQ_REMOVE(&handler_chain, p, link);
101 		p++;
102 	}
103 
104 	return (0);
105 }
106 
107 int
108 find_handler(int8_t dir, int8_t proto, struct libalias *la, struct ip *ip,
109     struct alias_data *ad)
110 {
111 	struct proto_handler *p;
112 
113 	TAILQ_FOREACH(p, &handler_chain, link)
114 		if ((p->dir & dir) && (p->proto & proto) &&
115 		    p->fingerprint(la, ad) == 0)
116 			return (p->protohandler(la, ip, ad));
117 
118 	return (ENOENT);
119 }
120 
121 struct proto_handler *
122 first_handler(void)
123 {
124 
125 	return (TAILQ_FIRST(&handler_chain));
126 }
127 
128 #ifndef _KERNEL
129 /* Dll manipulation code - this code is not thread safe... */
130 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