xref: /freebsd/sys/netinet/libalias/alias_mod.h (revision cc16dea626cf2fc80cde667ac4798065108e596c)
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
38 MALLOC_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 /* Protocol handlers struct & function. */
49 
50 /* Packet flow direction. */
51 #define IN                              1
52 #define OUT                             2
53 
54 /* Working protocol. */
55 #define IP                              1
56 #define TCP                             2
57 #define UDP                             4
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 
65 struct alias_data {
66 	struct alias_link       *lnk;
67 	struct in_addr          *oaddr;         /* Original address. */
68 	struct in_addr          *aaddr;         /* Alias address. */
69 	uint16_t                *aport;         /* Alias port. */
70 	uint16_t                *sport, *dport;	/* Source & destination port */
71 	uint16_t                maxpktsize;     /* Max packet size. */
72 };
73 
74 /*
75  * This structure contains all the information necessary to make
76  * a protocol handler correctly work.
77  */
78 
79 struct proto_handler {
80 	u_int pri;                                              /* Handler priority. */
81         int16_t dir;                                            /* Flow direction. */
82 	uint8_t proto;                                          /* Working protocol. */
83 	int (*fingerprint)(struct libalias *,                   /* Fingerprint * function. */
84 	    struct alias_data *);
85 	int (*protohandler)(struct libalias *,                  /* Aliasing * function. */
86 	    struct ip *, struct alias_data *);
87 	LIST_ENTRY(proto_handler) entries;
88 };
89 
90 
91 /*
92  * Used only in userland when libalias needs to keep track of all
93  * module loaded. In kernel land (kld mode) we don't need to care
94  * care about libalias modules cause it's kld to do it for us.
95  */
96 
97 #define DLL_LEN         32
98 struct dll {
99 	char            name[DLL_LEN];  /* Name of module. */
100 	void            *handle;        /*
101 					 * Ptr to shared obj obtained through
102 					 * dlopen() - use this ptr to get access
103 					 * to any symbols from a loaded module
104 					 * via dlsym().
105 					 */
106 	SLIST_ENTRY(dll)        next;
107 };
108 
109 /* Functions used with protocol handlers. */
110 
111 void            handler_chain_init(void);
112 void            handler_chain_destroy(void);
113 int             LibAliasAttachHandlers(struct proto_handler *);
114 int             LibAliasDetachHandlers(struct proto_handler *);
115 int             detach_handler(struct proto_handler *);
116 int             find_handler(int8_t, int8_t, struct libalias *,
117 			   struct ip *, struct alias_data *);
118 struct proto_handler *first_handler(void);
119 
120 /* Functions used with dll module. */
121 
122 void            dll_chain_init(void);
123 void            dll_chain_destroy(void);
124 int             attach_dll(struct dll *);
125 void            *detach_dll(char *);
126 struct dll      *walk_dll_chain(void);
127 
128 /* End of handlers. */
129 #define EOH     -1
130 
131 /*
132  * Some defines borrowed from sys/module.h used to compile a kld
133  * in userland as a shared lib.
134  */
135 
136 #ifndef _KERNEL
137 typedef enum modeventtype {
138         MOD_LOAD,
139         MOD_UNLOAD,
140         MOD_SHUTDOWN,
141         MOD_QUIESCE
142 } modeventtype_t;
143 
144 typedef struct module *module_t;
145 typedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
146 
147 /*
148  * Struct for registering modules statically via SYSINIT.
149  */
150 typedef struct moduledata {
151         const char      *name;          /* module name */
152         modeventhand_t  evhand;         /* event handler */
153         void            *priv;          /* extra data */
154 } moduledata_t;
155 #endif
156 
157 #endif				/* !_ALIAS_MOD_H_ */
158