1a9643ea8Slogwang /*-
2*22ce4affSfengbojiang  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3*22ce4affSfengbojiang  *
4a9643ea8Slogwang  * Copyright (c) 2005 Paolo Pisati <[email protected]>
5a9643ea8Slogwang  * All rights reserved.
6a9643ea8Slogwang  *
7a9643ea8Slogwang  * Redistribution and use in source and binary forms, with or without
8a9643ea8Slogwang  * modification, are permitted provided that the following conditions
9a9643ea8Slogwang  * are met:
10a9643ea8Slogwang  * 1. Redistributions of source code must retain the above copyright
11a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer.
12a9643ea8Slogwang  * 2. Redistributions in binary form must reproduce the above copyright
13a9643ea8Slogwang  *    notice, this list of conditions and the following disclaimer in the
14a9643ea8Slogwang  *    documentation and/or other materials provided with the distribution.
15a9643ea8Slogwang  *
16a9643ea8Slogwang  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17a9643ea8Slogwang  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18a9643ea8Slogwang  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19a9643ea8Slogwang  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20a9643ea8Slogwang  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21a9643ea8Slogwang  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22a9643ea8Slogwang  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23a9643ea8Slogwang  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24a9643ea8Slogwang  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25a9643ea8Slogwang  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26a9643ea8Slogwang  * SUCH DAMAGE.
27a9643ea8Slogwang  *
28a9643ea8Slogwang  * $FreeBSD$
29a9643ea8Slogwang  */
30a9643ea8Slogwang 
31a9643ea8Slogwang /*
32a9643ea8Slogwang  * Alias_mod.h defines the outside world interfaces for the packet aliasing
33a9643ea8Slogwang  * modular framework
34a9643ea8Slogwang  */
35a9643ea8Slogwang 
36a9643ea8Slogwang #ifndef _ALIAS_MOD_H_
37a9643ea8Slogwang #define _ALIAS_MOD_H_
38a9643ea8Slogwang 
39a9643ea8Slogwang #ifdef _KERNEL
40a9643ea8Slogwang MALLOC_DECLARE(M_ALIAS);
41a9643ea8Slogwang 
42a9643ea8Slogwang /* Use kernel allocator. */
43a9643ea8Slogwang #if defined(_SYS_MALLOC_H_)
44*22ce4affSfengbojiang #undef malloc
45a9643ea8Slogwang #define	malloc(x)	malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
46*22ce4affSfengbojiang #define	calloc(n, x)	mallocarray((n), (x), M_ALIAS, M_NOWAIT|M_ZERO)
47a9643ea8Slogwang #define	free(x)		free(x, M_ALIAS)
48a9643ea8Slogwang #endif
49a9643ea8Slogwang #endif
50a9643ea8Slogwang 
51a9643ea8Slogwang /* Packet flow direction flags. */
52a9643ea8Slogwang #define IN	0x0001
53a9643ea8Slogwang #define OUT	0x0002
54a9643ea8Slogwang #define	NODIR	0x4000
55a9643ea8Slogwang 
56a9643ea8Slogwang /* Working protocol flags. */
57a9643ea8Slogwang #define IP	0x01
58a9643ea8Slogwang #define TCP	0x02
59a9643ea8Slogwang #define UDP	0x04
60a9643ea8Slogwang 
61a9643ea8Slogwang /*
62a9643ea8Slogwang  * Data passed to protocol handler module, it must be filled
63a9643ea8Slogwang  * right before calling find_handler() to determine which
64a9643ea8Slogwang  * module is elegible to be called.
65a9643ea8Slogwang  */
66a9643ea8Slogwang struct alias_data {
67a9643ea8Slogwang 	struct alias_link	*lnk;
68a9643ea8Slogwang 	struct in_addr		*oaddr;		/* Original address. */
69a9643ea8Slogwang 	struct in_addr		*aaddr;		/* Alias address. */
70a9643ea8Slogwang 	uint16_t		*aport;		/* Alias port. */
71a9643ea8Slogwang 	uint16_t		*sport, *dport;	/* Source & destination port */
72a9643ea8Slogwang 	uint16_t		maxpktsize;	/* Max packet size. */
73a9643ea8Slogwang };
74a9643ea8Slogwang 
75a9643ea8Slogwang /*
76a9643ea8Slogwang  * This structure contains all the information necessary to make
77a9643ea8Slogwang  * a protocol handler correctly work.
78a9643ea8Slogwang  */
79a9643ea8Slogwang struct proto_handler {
80a9643ea8Slogwang 	u_int pri;		/* Handler priority. */
81a9643ea8Slogwang 	int16_t dir;		/* Flow direction. */
82a9643ea8Slogwang 	uint8_t proto;		/* Working protocol. */
83a9643ea8Slogwang 	/* Fingerprint * function. */
84a9643ea8Slogwang 	int (*fingerprint)(struct libalias *, struct alias_data *);
85a9643ea8Slogwang 	/* Aliasing * function. */
86a9643ea8Slogwang 	int (*protohandler)(struct libalias *, struct ip *,
87a9643ea8Slogwang 	    struct alias_data *);
88a9643ea8Slogwang 	TAILQ_ENTRY(proto_handler) link;
89a9643ea8Slogwang };
90a9643ea8Slogwang 
91a9643ea8Slogwang /* End of handlers. */
92a9643ea8Slogwang #define EOH	.dir = NODIR
93a9643ea8Slogwang 
94a9643ea8Slogwang /* Functions used with protocol handlers. */
95a9643ea8Slogwang int LibAliasAttachHandlers(struct proto_handler *);
96a9643ea8Slogwang int LibAliasDetachHandlers(struct proto_handler *);
97a9643ea8Slogwang int find_handler(int8_t, int8_t, struct libalias *, struct ip *,
98a9643ea8Slogwang     struct alias_data *);
99a9643ea8Slogwang struct proto_handler *first_handler(void);
100a9643ea8Slogwang 
101a9643ea8Slogwang #ifndef _KERNEL
102a9643ea8Slogwang /*
103a9643ea8Slogwang  * Used only in userland when libalias needs to keep track of all
104a9643ea8Slogwang  * module loaded. In kernel land (kld mode) we don't need to care
105a9643ea8Slogwang  * care about libalias modules cause it's kld to do it for us.
106a9643ea8Slogwang  */
107a9643ea8Slogwang #define DLL_LEN	 32
108a9643ea8Slogwang struct dll {
109a9643ea8Slogwang 	char	name[DLL_LEN];	/* Name of module. */
110a9643ea8Slogwang 	void	*handle;	/*
111a9643ea8Slogwang 				 * Ptr to shared obj obtained through
112a9643ea8Slogwang 				 * dlopen() - use this ptr to get access
113a9643ea8Slogwang 				 * to any symbols from a loaded module
114a9643ea8Slogwang 				 * via dlsym().
115a9643ea8Slogwang 				 */
116a9643ea8Slogwang 	SLIST_ENTRY(dll)	next;
117a9643ea8Slogwang };
118a9643ea8Slogwang 
119a9643ea8Slogwang /* Functions used with dll module. */
120a9643ea8Slogwang void dll_chain_init(void);
121a9643ea8Slogwang void dll_chain_destroy(void);
122a9643ea8Slogwang int attach_dll(struct dll *);
123a9643ea8Slogwang void *detach_dll(char *);
124a9643ea8Slogwang struct dll *walk_dll_chain(void);
125a9643ea8Slogwang 
126a9643ea8Slogwang /*
127a9643ea8Slogwang  * Some defines borrowed from sys/module.h used to compile a kld
128a9643ea8Slogwang  * in userland as a shared lib.
129a9643ea8Slogwang  */
130a9643ea8Slogwang typedef enum modeventtype {
131a9643ea8Slogwang 	MOD_LOAD,
132a9643ea8Slogwang 	MOD_UNLOAD,
133a9643ea8Slogwang 	MOD_SHUTDOWN,
134a9643ea8Slogwang 	MOD_QUIESCE
135a9643ea8Slogwang } modeventtype_t;
136a9643ea8Slogwang 
137a9643ea8Slogwang typedef struct module *module_t;
138a9643ea8Slogwang typedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
139a9643ea8Slogwang 
140a9643ea8Slogwang /*
141a9643ea8Slogwang  * Struct for registering modules statically via SYSINIT.
142a9643ea8Slogwang  */
143a9643ea8Slogwang typedef struct moduledata {
144a9643ea8Slogwang 	const char	*name;	/* module name */
145a9643ea8Slogwang 	modeventhand_t	evhand;	/* event handler */
146a9643ea8Slogwang 	void		*priv;	/* extra data */
147a9643ea8Slogwang } moduledata_t;
148a9643ea8Slogwang #endif /* !_KERNEL */
149a9643ea8Slogwang 
150a9643ea8Slogwang #endif /* !_ALIAS_MOD_H_ */
151