1/*-
2 * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3 *                    with the aid of code written by
4 *                    Junichi SATOH <junichi@astec.co.jp> 1996, 1997.
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#ifdef _KERNEL
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/module.h>
36#else
37#include <errno.h>
38#include <sys/types.h>
39#include <stdio.h>
40#endif
41
42#include <netinet/in_systm.h>
43#include <netinet/in.h>
44#include <netinet/ip.h>
45#include <netinet/udp.h>
46
47#ifdef _KERNEL
48#include <netinet/libalias/alias.h>
49#include <netinet/libalias/alias_local.h>
50#include <netinet/libalias/alias_mod.h>
51#else
52#include "alias_local.h"
53#include "alias_mod.h"
54#endif
55
56#define CUSEEME_PORT_NUMBER 7648
57
58static void
59AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip,
60		      struct alias_link *lnk);
61
62static void
63AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip,
64		     struct in_addr original_addr);
65
66static int
67fingerprint(struct libalias *la, struct alias_data *ah)
68{
69
70	if (ah->dport == NULL || ah->oaddr == NULL)
71		return (-1);
72	if (ntohs(*ah->dport) == CUSEEME_PORT_NUMBER)
73		return (0);
74	return (-1);
75}
76
77static int
78protohandlerin(struct libalias *la, struct ip *pip, struct alias_data *ah)
79{
80
81	AliasHandleCUSeeMeIn(la, pip, *ah->oaddr);
82	return (0);
83}
84
85static int
86protohandlerout(struct libalias *la, struct ip *pip, struct alias_data *ah)
87{
88
89	AliasHandleCUSeeMeOut(la, pip, ah->lnk);
90	return (0);
91}
92
93/* Kernel module definition. */
94struct proto_handler handlers[] = {
95	{
96	  .pri = 120,
97	  .dir = OUT,
98	  .proto = UDP,
99	  .fingerprint = &fingerprint,
100	  .protohandler = &protohandlerout
101	},
102	{
103	  .pri = 120,
104	  .dir = IN,
105	  .proto = UDP,
106	  .fingerprint = &fingerprint,
107	  .protohandler = &protohandlerin
108	},
109	{ EOH }
110};
111
112static int
113mod_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
133static
134#endif
135moduledata_t
136alias_mod = {
137       "alias_cuseeme", mod_handler, NULL
138};
139
140#ifdef	_KERNEL
141DECLARE_MODULE(alias_cuseeme, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
142MODULE_VERSION(alias_cuseeme, 1);
143MODULE_DEPEND(alias_cuseeme, libalias, 1, 1, 1);
144#endif
145
146/* CU-SeeMe Data Header */
147struct cu_header {
148	u_int16_t	dest_family;
149	u_int16_t	dest_port;
150	u_int32_t	dest_addr;
151	int16_t		family;
152	u_int16_t	port;
153	u_int32_t	addr;
154	u_int32_t	seq;
155	u_int16_t	msg;
156	u_int16_t	data_type;
157	u_int16_t	packet_len;
158};
159
160/* Open Continue Header */
161struct oc_header {
162	u_int16_t	client_count;	/* Number of client info structs */
163	u_int32_t	seq_no;
164	char		user_name [20];
165	char		reserved  [4];	/* flags, version stuff, etc */
166};
167
168/* client info structures */
169struct client_info {
170	u_int32_t	address;/* Client address */
171	char		reserved  [8];	/* Flags, pruning bitfield, packet
172					 * counts etc */
173};
174
175static void
176AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *lnk)
177{
178	struct udphdr *ud = ip_next(pip);
179
180	if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
181		struct cu_header *cu;
182		struct alias_link *cu_lnk;
183
184		cu = udp_next(ud);
185		if (cu->addr)
186			cu->addr = (u_int32_t) GetAliasAddress(lnk).s_addr;
187
188		cu_lnk = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(lnk),
189		    ud->uh_dport, 0, IPPROTO_UDP, 1);
190
191#ifndef NO_FW_PUNCH
192		if (cu_lnk)
193			PunchFWHole(cu_lnk);
194#endif
195	}
196}
197
198static void
199AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
200{
201	struct in_addr alias_addr;
202	struct udphdr *ud;
203	struct cu_header *cu;
204	struct oc_header *oc;
205	struct client_info *ci;
206	char *end;
207	int i;
208
209	(void)la;
210	alias_addr.s_addr = pip->ip_dst.s_addr;
211	ud = ip_next(pip);
212	cu = udp_next(ud);
213	oc = (struct oc_header *)(cu + 1);
214	ci = (struct client_info *)(oc + 1);
215	end = (char *)ud + ntohs(ud->uh_ulen);
216
217	if ((char *)oc <= end) {
218		if (cu->dest_addr)
219			cu->dest_addr = (u_int32_t) original_addr.s_addr;
220		if (ntohs(cu->data_type) == 101)
221			/* Find and change our address */
222			for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
223				if (ci->address == (u_int32_t) alias_addr.s_addr) {
224					ci->address = (u_int32_t) original_addr.s_addr;
225					break;
226				}
227	}
228}
229