xref: /freebsd/sys/netinet/libalias/alias_dummy.c (revision b740c88bfb6453416926271c089262e7164dace3)
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 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 /*
31  * Alias_dummy is just an empty skeleton used to demostrate how to write
32  * a module for libalias, that will run unalterated in userland or in
33  * kernel land.
34  */
35 
36 #ifdef _KERNEL
37 #include <sys/param.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #else
41 #include <errno.h>
42 #include <sys/types.h>
43 #include <stdio.h>
44 #endif
45 
46 #include <netinet/in_systm.h>
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <netinet/udp.h>
50 
51 #ifdef _KERNEL
52 #include <netinet/libalias/alias_local.h>
53 #include <netinet/libalias/alias_mod.h>
54 #else
55 #include "alias_local.h"
56 #include "alias_mod.h"
57 #endif
58 
59 static void
60 AliasHandleDummy(struct libalias *la, struct ip *ip, struct alias_data *ah);
61 
62 static int
63 fingerprint(struct libalias *la, struct alias_data *ah)
64 {
65 
66 	/*
67 	 * Check here all the data that will be used later, if any field
68 	 * is empy/NULL, return a -1 value.
69 	 */
70 	if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
71 		ah->maxpktsize == 0)
72 		return (-1);
73 	/*
74 	 * Fingerprint the incoming packet, if it matches any conditions
75 	 * return an OK value.
76 	 */
77 	if (ntohs(*ah->dport) == 123
78 	    || ntohs(*ah->sport) == 456)
79 		return (0); /* I know how to handle it. */
80 	return (-1); /* I don't recognize this packet. */
81 }
82 
83 /*
84  * Wrap in this general purpose function, the real function used to alias the
85  * packets.
86  */
87 
88 static int
89 protohandler(struct libalias *la, struct ip *pip, struct alias_data *ah)
90 {
91 
92 	AliasHandleDummy(la, pip, ah);
93 	return (0);
94 }
95 
96 /*
97  * NOTA BENE: the next variable MUST NOT be renamed in any case if you want
98  * your module to work in userland, cause it's used to find and use all
99  * the protocol handlers present in every module.
100  * So WATCH OUT, your module needs this variables and it needs it with
101  * ITS EXACT NAME: handlers.
102  */
103 
104 struct proto_handler handlers [] = {
105 	{
106 	  .pri = 666,
107 	  .dir = IN|OUT,
108 	  .proto = UDP|TCP,
109 	  .fingerprint = &fingerprint,
110 	  .protohandler = &protohandler
111 	},
112 	{ EOH }
113 };
114 
115 static int
116 mod_handler(module_t mod, int type, void *data)
117 {
118 	int error;
119 
120 	switch (type) {
121 	case MOD_LOAD:
122 		error = 0;
123 		LibAliasAttachHandlers(handlers);
124 		break;
125 	case MOD_UNLOAD:
126 		error = 0;
127 		LibAliasDetachHandlers(handlers);
128 		break;
129 	default:
130 		error = EINVAL;
131 	}
132 	return (error);
133 }
134 
135 #ifdef _KERNEL
136 static
137 #endif
138 moduledata_t alias_mod = {
139        "alias_dummy", mod_handler, NULL
140 };
141 
142 #ifdef	_KERNEL
143 DECLARE_MODULE(alias_dummy, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
144 MODULE_VERSION(alias_dummy, 1);
145 MODULE_DEPEND(alias_dummy, libalias, 1, 1, 1);
146 #endif
147 
148 static void
149 AliasHandleDummy(struct libalias *la, struct ip *ip, struct alias_data *ah)
150 {
151 	; /* Dummy. */
152 }
153 
154