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
27162674Spiso#include <sys/cdefs.h>
28162674Spiso__FBSDID("$FreeBSD: releng/11.0/sys/netinet/libalias/alias_dummy.c 259858 2013-12-25 02:06:57Z glebius $");
29162674Spiso
30259858Sglebius/*
31162674Spiso * Alias_dummy is just an empty skeleton used to demostrate how to write
32162674Spiso * a module for libalias, that will run unalterated in userland or in
33162674Spiso * kernel land.
34162674Spiso */
35162674Spiso
36162674Spiso#ifdef _KERNEL
37162674Spiso#include <sys/param.h>
38162674Spiso#include <sys/kernel.h>
39162674Spiso#include <sys/module.h>
40162674Spiso#else
41162674Spiso#include <errno.h>
42162674Spiso#include <sys/types.h>
43162674Spiso#include <stdio.h>
44162674Spiso#endif
45162674Spiso
46162674Spiso#include <netinet/in_systm.h>
47162674Spiso#include <netinet/in.h>
48162674Spiso#include <netinet/ip.h>
49162674Spiso#include <netinet/udp.h>
50162674Spiso
51162674Spiso#ifdef _KERNEL
52162674Spiso#include <netinet/libalias/alias_local.h>
53162674Spiso#include <netinet/libalias/alias_mod.h>
54162674Spiso#else
55162674Spiso#include "alias_local.h"
56162674Spiso#include "alias_mod.h"
57162674Spiso#endif
58162674Spiso
59162674Spisostatic void
60162674SpisoAliasHandleDummy(struct libalias *la, struct ip *ip, struct alias_data *ah);
61162674Spiso
62259858Sglebiusstatic int
63190841Spisofingerprint(struct libalias *la, struct alias_data *ah)
64162674Spiso{
65162674Spiso
66259858Sglebius	/*
67259858Sglebius	 * Check here all the data that will be used later, if any field
68162674Spiso	 * is empy/NULL, return a -1 value.
69162674Spiso	 */
70259858Sglebius	if (ah->dport == NULL || ah->sport == NULL || ah->lnk == NULL ||
71162674Spiso		ah->maxpktsize == 0)
72162674Spiso		return (-1);
73259858Sglebius	/*
74259858Sglebius	 * Fingerprint the incoming packet, if it matches any conditions
75162674Spiso	 * return an OK value.
76162674Spiso	 */
77162674Spiso	if (ntohs(*ah->dport) == 123
78162674Spiso	    || ntohs(*ah->sport) == 456)
79162674Spiso		return (0); /* I know how to handle it. */
80162674Spiso	return (-1); /* I don't recognize this packet. */
81162674Spiso}
82162674Spiso
83259858Sglebius/*
84259858Sglebius * Wrap in this general purpose function, the real function used to alias the
85162674Spiso * packets.
86162674Spiso */
87162674Spiso
88259858Sglebiusstatic int
89162674Spisoprotohandler(struct libalias *la, struct ip *pip, struct alias_data *ah)
90162674Spiso{
91162674Spiso
92162674Spiso	AliasHandleDummy(la, pip, ah);
93162674Spiso	return (0);
94162674Spiso}
95162674Spiso
96259858Sglebius/*
97259858Sglebius * NOTA BENE: the next variable MUST NOT be renamed in any case if you want
98259858Sglebius * your module to work in userland, cause it's used to find and use all
99162674Spiso * the protocol handlers present in every module.
100259858Sglebius * So WATCH OUT, your module needs this variables and it needs it with
101162674Spiso * ITS EXACT NAME: handlers.
102162674Spiso */
103162674Spiso
104162674Spisostruct proto_handler handlers [] = {
105259858Sglebius	{
106259858Sglebius	  .pri = 666,
107259858Sglebius	  .dir = IN|OUT,
108259858Sglebius	  .proto = UDP|TCP,
109259858Sglebius	  .fingerprint = &fingerprint,
110162674Spiso	  .protohandler = &protohandler
111259858Sglebius	},
112162674Spiso	{ EOH }
113162674Spiso};
114162674Spiso
115162674Spisostatic int
116162674Spisomod_handler(module_t mod, int type, void *data)
117162674Spiso{
118162674Spiso	int error;
119162674Spiso
120259858Sglebius	switch (type) {
121162674Spiso	case MOD_LOAD:
122162674Spiso		error = 0;
123162674Spiso		LibAliasAttachHandlers(handlers);
124162674Spiso		break;
125162674Spiso	case MOD_UNLOAD:
126162674Spiso		error = 0;
127162674Spiso		LibAliasDetachHandlers(handlers);
128162674Spiso		break;
129162674Spiso	default:
130162674Spiso		error = EINVAL;
131162674Spiso	}
132162674Spiso	return (error);
133162674Spiso}
134162674Spiso
135162674Spiso#ifdef _KERNEL
136162674Spisostatic
137162674Spiso#endif
138162674Spisomoduledata_t alias_mod = {
139162674Spiso       "alias_dummy", mod_handler, NULL
140162674Spiso};
141162674Spiso
142162674Spiso#ifdef	_KERNEL
143162674SpisoDECLARE_MODULE(alias_dummy, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
144162674SpisoMODULE_VERSION(alias_dummy, 1);
145162674SpisoMODULE_DEPEND(alias_dummy, libalias, 1, 1, 1);
146162674Spiso#endif
147162674Spiso
148162674Spisostatic void
149162674SpisoAliasHandleDummy(struct libalias *la, struct ip *ip, struct alias_data *ah)
150162674Spiso{
151162674Spiso	; /* Dummy. */
152162674Spiso}
153162674Spiso
154