xref: /freebsd/sys/netinet/libalias/alias_dummy.c (revision 3078531de10dcae44b253a35125c949ff4235284)
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 	 * Check here all the data that will be used later, if any field
69 	 * is empy/NULL, return a -1 value.
70 	 */
71 	if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
72 	    ah->maxpktsize == 0)
73 		return (-1);
74 	/*
75 	 * Fingerprint the incoming packet, if it matches any conditions
76 	 * return an OK value.
77 	 */
78 	if (ntohs(*ah->dport) == 123 || 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 	AliasHandleDummy(la, pip, ah);
92 	return (0);
93 }
94 
95 /*
96  * NOTA BENE: the next variable MUST NOT be renamed in any case if you want
97  * your module to work in userland, cause it's used to find and use all
98  * the protocol handlers present in every module.
99  * So WATCH OUT, your module needs this variables and it needs it with
100  * ITS EXACT NAME: handlers.
101  */
102 
103 struct proto_handler handlers [] = {
104 	{
105 	  .pri = 666,
106 	  .dir = IN|OUT,
107 	  .proto = UDP|TCP,
108 	  .fingerprint = &fingerprint,
109 	  .protohandler = &protohandler
110 	},
111 	{ EOH }
112 };
113 
114 static int
115 mod_handler(module_t mod, int type, void *data)
116 {
117 	int error;
118 
119 	switch (type) {
120 	case MOD_LOAD:
121 		error = 0;
122 		LibAliasAttachHandlers(handlers);
123 		break;
124 	case MOD_UNLOAD:
125 		error = 0;
126 		LibAliasDetachHandlers(handlers);
127 		break;
128 	default:
129 		error = EINVAL;
130 	}
131 	return (error);
132 }
133 
134 #ifdef _KERNEL
135 static
136 #endif
137 moduledata_t alias_mod = {
138        "alias_dummy", mod_handler, NULL
139 };
140 
141 #ifdef _KERNEL
142 DECLARE_MODULE(alias_dummy, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
143 MODULE_VERSION(alias_dummy, 1);
144 MODULE_DEPEND(alias_dummy, libalias, 1, 1, 1);
145 #endif
146 
147 static void
148 AliasHandleDummy(struct libalias *la, struct ip *ip, struct alias_data *ah)
149 {
150 	; /* Dummy. */
151 }
152