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