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 * $FreeBSD$
27 */
28
29/*
30 * Alias_mod.h defines the outside world interfaces for the packet aliasing
31 * modular framework
32 */
33
34#ifndef _ALIAS_MOD_H_
35#define _ALIAS_MOD_H_
36
37#ifdef _KERNEL
38MALLOC_DECLARE(M_ALIAS);
39
40/* Use kernel allocator. */
41#if defined(_SYS_MALLOC_H_)
42#define	malloc(x)	malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
43#define	calloc(x, n)	malloc(x*n)
44#define	free(x)		free(x, M_ALIAS)
45#endif
46#endif
47
48/* Packet flow direction flags. */
49#define IN	0x0001
50#define OUT	0x0002
51#define	NODIR	0x4000
52
53/* Working protocol flags. */
54#define IP	0x01
55#define TCP	0x02
56#define UDP	0x04
57
58/*
59 * Data passed to protocol handler module, it must be filled
60 * right before calling find_handler() to determine which
61 * module is elegible to be called.
62 */
63struct alias_data {
64	struct alias_link	*lnk;
65	struct in_addr		*oaddr;		/* Original address. */
66	struct in_addr		*aaddr;		/* Alias address. */
67	uint16_t		*aport;		/* Alias port. */
68	uint16_t		*sport, *dport;	/* Source & destination port */
69	uint16_t		maxpktsize;	/* Max packet size. */
70};
71
72/*
73 * This structure contains all the information necessary to make
74 * a protocol handler correctly work.
75 */
76struct proto_handler {
77	u_int pri;		/* Handler priority. */
78	int16_t dir;		/* Flow direction. */
79	uint8_t proto;		/* Working protocol. */
80	/* Fingerprint * function. */
81	int (*fingerprint)(struct libalias *, struct alias_data *);
82	/* Aliasing * function. */
83	int (*protohandler)(struct libalias *, struct ip *,
84	    struct alias_data *);
85	TAILQ_ENTRY(proto_handler) link;
86};
87
88/* End of handlers. */
89#define EOH	.dir = NODIR
90
91/* Functions used with protocol handlers. */
92int LibAliasAttachHandlers(struct proto_handler *);
93int LibAliasDetachHandlers(struct proto_handler *);
94int find_handler(int8_t, int8_t, struct libalias *, struct ip *,
95    struct alias_data *);
96struct proto_handler *first_handler(void);
97
98#ifndef _KERNEL
99/*
100 * Used only in userland when libalias needs to keep track of all
101 * module loaded. In kernel land (kld mode) we don't need to care
102 * care about libalias modules cause it's kld to do it for us.
103 */
104#define DLL_LEN	 32
105struct dll {
106	char	name[DLL_LEN];	/* Name of module. */
107	void	*handle;	/*
108				 * Ptr to shared obj obtained through
109				 * dlopen() - use this ptr to get access
110				 * to any symbols from a loaded module
111				 * via dlsym().
112				 */
113	SLIST_ENTRY(dll)	next;
114};
115
116/* Functions used with dll module. */
117void dll_chain_init(void);
118void dll_chain_destroy(void);
119int attach_dll(struct dll *);
120void *detach_dll(char *);
121struct dll *walk_dll_chain(void);
122
123/*
124 * Some defines borrowed from sys/module.h used to compile a kld
125 * in userland as a shared lib.
126 */
127typedef enum modeventtype {
128	MOD_LOAD,
129	MOD_UNLOAD,
130	MOD_SHUTDOWN,
131	MOD_QUIESCE
132} modeventtype_t;
133
134typedef struct module *module_t;
135typedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
136
137/*
138 * Struct for registering modules statically via SYSINIT.
139 */
140typedef struct moduledata {
141	const char	*name;	/* module name */
142	modeventhand_t	evhand;	/* event handler */
143	void		*priv;	/* extra data */
144} moduledata_t;
145#endif /* !_KERNEL */
146
147#endif /* !_ALIAS_MOD_H_ */
148