1162674Spiso/*-
2162674Spiso * Copyright (c) 2005 Paolo Pisati <piso@FreeBSD.org>
3162674Spiso * All rights reserved.
4162674Spiso *
5162674Spiso * Redistribution and use in source and binary forms, with or without
6162674Spiso * modification, are permitted provided that the following conditions
7162674Spiso * are met:
8162674Spiso * 1. Redistributions of source code must retain the above copyright
9162674Spiso *    notice, this list of conditions and the following disclaimer.
10162674Spiso * 2. Redistributions in binary form must reproduce the above copyright
11162674Spiso *    notice, this list of conditions and the following disclaimer in the
12162674Spiso *    documentation and/or other materials provided with the distribution.
13162674Spiso *
14162674Spiso * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15162674Spiso * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16162674Spiso * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17162674Spiso * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18162674Spiso * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19162674Spiso * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20162674Spiso * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21162674Spiso * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22162674Spiso * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23162674Spiso * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24162674Spiso * SUCH DAMAGE.
25162674Spiso *
26162674Spiso * $FreeBSD$
27162674Spiso */
28162674Spiso
29162674Spiso/*
30162674Spiso * Alias_mod.h defines the outside world interfaces for the packet aliasing
31162674Spiso * modular framework
32162674Spiso */
33162674Spiso
34162674Spiso#ifndef _ALIAS_MOD_H_
35162674Spiso#define _ALIAS_MOD_H_
36162674Spiso
37162674Spiso#ifdef _KERNEL
38162674SpisoMALLOC_DECLARE(M_ALIAS);
39162674Spiso
40162674Spiso/* Use kernel allocator. */
41162674Spiso#if defined(_SYS_MALLOC_H_)
42162674Spiso#define	malloc(x)	malloc(x, M_ALIAS, M_NOWAIT|M_ZERO)
43162674Spiso#define	calloc(x, n)	malloc(x*n)
44162674Spiso#define	free(x)		free(x, M_ALIAS)
45162674Spiso#endif
46162674Spiso#endif
47162674Spiso
48162674Spiso/* Protocol handlers struct & function. */
49162674Spiso
50162674Spiso/* Packet flow direction. */
51162674Spiso#define IN                              1
52162674Spiso#define OUT                             2
53162674Spiso
54162674Spiso/* Working protocol. */
55162674Spiso#define IP                              1
56162674Spiso#define TCP                             2
57162674Spiso#define UDP                             4
58162674Spiso
59162674Spiso/*
60162674Spiso * Data passed to protocol handler module, it must be filled
61162674Spiso * right before calling find_handler() to determine which
62162674Spiso * module is elegible to be called.
63162674Spiso */
64162674Spiso
65162674Spisostruct alias_data {
66162674Spiso	struct alias_link       *lnk;
67162674Spiso	struct in_addr          *oaddr;         /* Original address. */
68162674Spiso	struct in_addr          *aaddr;         /* Alias address. */
69162674Spiso	uint16_t                *aport;         /* Alias port. */
70162674Spiso	uint16_t                *sport, *dport;	/* Source & destination port */
71162674Spiso	uint16_t                maxpktsize;     /* Max packet size. */
72162674Spiso};
73162674Spiso
74162674Spiso/*
75162674Spiso * This structure contains all the information necessary to make
76162674Spiso * a protocol handler correctly work.
77162674Spiso */
78162674Spiso
79162674Spisostruct proto_handler {
80162674Spiso	u_int pri;                                              /* Handler priority. */
81162674Spiso        int16_t dir;                                            /* Flow direction. */
82162674Spiso	uint8_t proto;                                          /* Working protocol. */
83190841Spiso	int (*fingerprint)(struct libalias *,                   /* Fingerprint * function. */
84190841Spiso	    struct alias_data *);
85190841Spiso	int (*protohandler)(struct libalias *,                  /* Aliasing * function. */
86190841Spiso	    struct ip *, struct alias_data *);
87162674Spiso	LIST_ENTRY(proto_handler) entries;
88162674Spiso};
89162674Spiso
90162674Spiso
91162674Spiso/*
92162674Spiso * Used only in userland when libalias needs to keep track of all
93162674Spiso * module loaded. In kernel land (kld mode) we don't need to care
94162674Spiso * care about libalias modules cause it's kld to do it for us.
95162674Spiso */
96162674Spiso
97162674Spiso#define DLL_LEN         32
98162674Spisostruct dll {
99162674Spiso	char            name[DLL_LEN];  /* Name of module. */
100162674Spiso	void            *handle;        /*
101162674Spiso					 * Ptr to shared obj obtained through
102162674Spiso					 * dlopen() - use this ptr to get access
103162674Spiso					 * to any symbols from a loaded module
104162674Spiso					 * via dlsym().
105162674Spiso					 */
106162674Spiso	SLIST_ENTRY(dll)        next;
107162674Spiso};
108162674Spiso
109162674Spiso/* Functions used with protocol handlers. */
110162674Spiso
111162674Spisovoid            handler_chain_init(void);
112162674Spisovoid            handler_chain_destroy(void);
113162674Spisoint             LibAliasAttachHandlers(struct proto_handler *);
114162674Spisoint             LibAliasDetachHandlers(struct proto_handler *);
115162674Spisoint             detach_handler(struct proto_handler *);
116162674Spisoint             find_handler(int8_t, int8_t, struct libalias *,
117162674Spiso			   struct ip *, struct alias_data *);
118162674Spisostruct proto_handler *first_handler(void);
119162674Spiso
120162674Spiso/* Functions used with dll module. */
121162674Spiso
122162674Spisovoid            dll_chain_init(void);
123162674Spisovoid            dll_chain_destroy(void);
124162674Spisoint             attach_dll(struct dll *);
125162674Spisovoid            *detach_dll(char *);
126162674Spisostruct dll      *walk_dll_chain(void);
127162674Spiso
128162674Spiso/* End of handlers. */
129162674Spiso#define EOH     -1
130162674Spiso
131162674Spiso/*
132162674Spiso * Some defines borrowed from sys/module.h used to compile a kld
133162674Spiso * in userland as a shared lib.
134162674Spiso */
135162674Spiso
136162674Spiso#ifndef _KERNEL
137162674Spisotypedef enum modeventtype {
138162674Spiso        MOD_LOAD,
139162674Spiso        MOD_UNLOAD,
140162674Spiso        MOD_SHUTDOWN,
141162674Spiso        MOD_QUIESCE
142162674Spiso} modeventtype_t;
143162674Spiso
144162674Spisotypedef struct module *module_t;
145162674Spisotypedef int (*modeventhand_t)(module_t, int /* modeventtype_t */, void *);
146162674Spiso
147162674Spiso/*
148162674Spiso * Struct for registering modules statically via SYSINIT.
149162674Spiso */
150162674Spisotypedef struct moduledata {
151162674Spiso        const char      *name;          /* module name */
152162674Spiso        modeventhand_t  evhand;         /* event handler */
153162674Spiso        void            *priv;          /* extra data */
154162674Spiso} moduledata_t;
155162674Spiso#endif
156162674Spiso
157162674Spiso#endif				/* !_ALIAS_MOD_H_ */
158