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