summaryrefslogtreecommitdiffstats
path: root/freebsd/sys/netinet/libalias/alias_mod.c
blob: cb567473252983fb6cca0d4d4df787e7ff4b7e31 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#include <machine/rtems-bsd-config.h>

/*-
 * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#ifdef _KERNEL
#include <sys/libkern.h>
#include <rtems/bsd/sys/param.h>
#include <rtems/bsd/sys/lock.h>
#include <sys/rwlock.h>
#else
#include <stdio.h>
#include <string.h>
#include <rtems/bsd/sys/types.h>
#include <errno.h>
#endif

#include <netinet/in_systm.h>
#include <netinet/in.h>
#include <netinet/ip.h>

#ifdef _KERNEL
#include <netinet/libalias/alias_local.h>
#include <netinet/libalias/alias_mod.h>
#else
#include <rtems/bsd/local/alias_local.h>
#include <rtems/bsd/local/alias_mod.h>
#endif

/* Protocol and userland module handlers chains. */
LIST_HEAD(handler_chain, proto_handler) handler_chain = LIST_HEAD_INITIALIZER(handler_chain);
#ifdef _KERNEL
struct rwlock   handler_rw;
#endif
SLIST_HEAD(dll_chain, dll) dll_chain = SLIST_HEAD_INITIALIZER(dll_chain); 

#ifdef _KERNEL

#define	LIBALIAS_RWLOCK_INIT() \
        rw_init(&handler_rw, "Libalias_modules_rwlock")
#define	LIBALIAS_RWLOCK_DESTROY()	rw_destroy(&handler_rw)
#define	LIBALIAS_WLOCK_ASSERT() \
        rw_assert(&handler_rw, RA_WLOCKED)

static __inline void
LIBALIAS_RLOCK(void)
{
	rw_rlock(&handler_rw);
}

static __inline void
LIBALIAS_RUNLOCK(void)
{
	rw_runlock(&handler_rw);
}

static __inline void
LIBALIAS_WLOCK(void)
{
	rw_wlock(&handler_rw);
}

static __inline void
LIBALIAS_WUNLOCK(void)
{
	rw_wunlock(&handler_rw);
}

static void
_handler_chain_init(void)
{

	if (!rw_initialized(&handler_rw))
		LIBALIAS_RWLOCK_INIT();
}

static void
_handler_chain_destroy(void)
{

	if (rw_initialized(&handler_rw))
		LIBALIAS_RWLOCK_DESTROY();
}

#else
#define	LIBALIAS_RWLOCK_INIT() ;
#define	LIBALIAS_RWLOCK_DESTROY()	;
#define	LIBALIAS_WLOCK_ASSERT()	;
#define	LIBALIAS_RLOCK() ;
#define	LIBALIAS_RUNLOCK() ;
#define	LIBALIAS_WLOCK() ;
#define	LIBALIAS_WUNLOCK() ;
#define _handler_chain_init() ;
#define _handler_chain_destroy() ;
#endif 

void
handler_chain_init(void)
{
	_handler_chain_init();
}

void
handler_chain_destroy(void)
{
	_handler_chain_destroy();
}

static int
_attach_handler(struct proto_handler *p)
{
	struct proto_handler *b;

	LIBALIAS_WLOCK_ASSERT();
	b = NULL;
	LIST_FOREACH(b, &handler_chain, entries) {
		if ((b->pri == p->pri) && 
		    (b->dir == p->dir) &&
		    (b->proto == p->proto))
			return (EEXIST); /* Priority conflict. */
		if (b->pri > p->pri) {
			LIST_INSERT_BEFORE(b, p, entries);
			return (0);
		}
	}
	/* End of list or found right position, inserts here. */
	if (b)
		LIST_INSERT_AFTER(b, p, entries);
	else
		LIST_INSERT_HEAD(&handler_chain, p, entries);
	return (0);
}

static int
_detach_handler(struct proto_handler *p)
{
	struct proto_handler *b, *b_tmp;

	LIBALIAS_WLOCK_ASSERT();	
	LIST_FOREACH_SAFE(b, &handler_chain, entries, b_tmp) {
		if (b == p) {
			LIST_REMOVE(b, entries);
			return (0);
		}
	}
	return (ENOENT); /* Handler not found. */
}

int
LibAliasAttachHandlers(struct proto_handler *_p)
{
	int i, error;

	LIBALIAS_WLOCK();
	error = -1;
	for (i = 0; 1; i++) {
		if (*((int *)&_p[i]) == EOH) 
			break;
		error = _attach_handler(&_p[i]);
		if (error != 0) 
			break;
	}
	LIBALIAS_WUNLOCK();
	return (error);
}

int
LibAliasDetachHandlers(struct proto_handler *_p)
{
	int i, error;

	LIBALIAS_WLOCK();
	error = -1;
	for (i = 0; 1; i++) {
		if (*((int *)&_p[i]) == EOH) 
			break;
		error = _detach_handler(&_p[i]);
		if (error != 0) 
			break;
	}
	LIBALIAS_WUNLOCK();
	return (error);
}

int
detach_handler(struct proto_handler *_p)
{
	int error;

	LIBALIAS_WLOCK();
	error = -1;
	error = _detach_handler(_p);
	LIBALIAS_WUNLOCK();
	return (error);
}

int
find_handler(int8_t dir, int8_t proto, struct libalias *la, __unused struct ip *pip, 
    struct alias_data *ad)
{
	struct proto_handler *p;
	int error;

	LIBALIAS_RLOCK();
	error = ENOENT;
	LIST_FOREACH(p, &handler_chain, entries) {
		if ((p->dir & dir) && (p->proto & proto))
			if (p->fingerprint(la, ad) == 0) {
				error = p->protohandler(la, pip, ad);
				break;
			}
	}
	LIBALIAS_RUNLOCK();
	return (error);	
}

struct proto_handler *
first_handler(void)
{
	
	return (LIST_FIRST(&handler_chain));	
}

/* Dll manipulation code - this code is not thread safe... */

int
attach_dll(struct dll *p)
{
	struct dll *b;

	SLIST_FOREACH(b, &dll_chain, next) {
		if (!strncmp(b->name, p->name, DLL_LEN))
			return (EEXIST); /* Dll name conflict. */
	}
	SLIST_INSERT_HEAD(&dll_chain, p, next);
	return (0);
}

void *
detach_dll(char *p)
{
	struct dll *b, *b_tmp;
	void *error;

	b = NULL;
	error = NULL;
	SLIST_FOREACH_SAFE(b, &dll_chain, next, b_tmp)
		if (!strncmp(b->name, p, DLL_LEN)) {
			SLIST_REMOVE(&dll_chain, b, dll, next); 
			error = b;
			break;
		}
	return (error);
}

struct dll *
walk_dll_chain(void)
{
	struct dll *t;

	t = SLIST_FIRST(&dll_chain);
	if (t == NULL)
		return (NULL);
	SLIST_REMOVE_HEAD(&dll_chain, next);
	return (t);
}