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