1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 1998 Brian Somers <[email protected]>
5 * with the aid of code written by
6 * Junichi SATOH <[email protected]> 1996, 1997.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #ifdef _KERNEL
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #else
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <stdio.h>
42 #endif
43
44 #include <netinet/in_systm.h>
45 #include <netinet/in.h>
46 #include <netinet/ip.h>
47 #include <netinet/udp.h>
48
49 #ifdef _KERNEL
50 #include <netinet/libalias/alias.h>
51 #include <netinet/libalias/alias_local.h>
52 #include <netinet/libalias/alias_mod.h>
53 #else
54 #include "alias_local.h"
55 #include "alias_mod.h"
56 #endif
57
58 #define CUSEEME_PORT_NUMBER 7648
59
60 static void
61 AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip,
62 struct alias_link *lnk);
63
64 static void
65 AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip,
66 struct in_addr original_addr);
67
68 static int
fingerprint(struct libalias * la,struct alias_data * ah)69 fingerprint(struct libalias *la, struct alias_data *ah)
70 {
71
72 if (ah->dport == NULL || ah->oaddr == NULL)
73 return (-1);
74 if (ntohs(*ah->dport) == CUSEEME_PORT_NUMBER)
75 return (0);
76 return (-1);
77 }
78
79 static int
protohandlerin(struct libalias * la,struct ip * pip,struct alias_data * ah)80 protohandlerin(struct libalias *la, struct ip *pip, struct alias_data *ah)
81 {
82
83 AliasHandleCUSeeMeIn(la, pip, *ah->oaddr);
84 return (0);
85 }
86
87 static int
protohandlerout(struct libalias * la,struct ip * pip,struct alias_data * ah)88 protohandlerout(struct libalias *la, struct ip *pip, struct alias_data *ah)
89 {
90
91 AliasHandleCUSeeMeOut(la, pip, ah->lnk);
92 return (0);
93 }
94
95 /* Kernel module definition. */
96 struct proto_handler handlers[] = {
97 {
98 .pri = 120,
99 .dir = OUT,
100 .proto = UDP,
101 .fingerprint = &fingerprint,
102 .protohandler = &protohandlerout
103 },
104 {
105 .pri = 120,
106 .dir = IN,
107 .proto = UDP,
108 .fingerprint = &fingerprint,
109 .protohandler = &protohandlerin
110 },
111 { EOH }
112 };
113
114 static int
mod_handler(module_t mod,int type,void * data)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
138 alias_mod = {
139 "alias_cuseeme", mod_handler, NULL
140 };
141
142 #ifdef _KERNEL
143 DECLARE_MODULE(alias_cuseeme, alias_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
144 MODULE_VERSION(alias_cuseeme, 1);
145 MODULE_DEPEND(alias_cuseeme, libalias, 1, 1, 1);
146 #endif
147
148 /* CU-SeeMe Data Header */
149 struct cu_header {
150 u_int16_t dest_family;
151 u_int16_t dest_port;
152 u_int32_t dest_addr;
153 int16_t family;
154 u_int16_t port;
155 u_int32_t addr;
156 u_int32_t seq;
157 u_int16_t msg;
158 u_int16_t data_type;
159 u_int16_t packet_len;
160 };
161
162 /* Open Continue Header */
163 struct oc_header {
164 u_int16_t client_count; /* Number of client info structs */
165 u_int32_t seq_no;
166 char user_name [20];
167 char reserved [4]; /* flags, version stuff, etc */
168 };
169
170 /* client info structures */
171 struct client_info {
172 u_int32_t address;/* Client address */
173 char reserved [8]; /* Flags, pruning bitfield, packet
174 * counts etc */
175 };
176
177 static void
AliasHandleCUSeeMeOut(struct libalias * la,struct ip * pip,struct alias_link * lnk)178 AliasHandleCUSeeMeOut(struct libalias *la, struct ip *pip, struct alias_link *lnk)
179 {
180 struct udphdr *ud = ip_next(pip);
181
182 if (ntohs(ud->uh_ulen) - sizeof(struct udphdr) >= sizeof(struct cu_header)) {
183 struct cu_header *cu;
184 struct alias_link *cu_lnk;
185
186 cu = udp_next(ud);
187 if (cu->addr)
188 cu->addr = (u_int32_t) GetAliasAddress(lnk).s_addr;
189
190 cu_lnk = FindUdpTcpOut(la, pip->ip_src, GetDestAddress(lnk),
191 ud->uh_dport, 0, IPPROTO_UDP, 1);
192
193 #ifndef NO_FW_PUNCH
194 if (cu_lnk)
195 PunchFWHole(cu_lnk);
196 #endif
197 }
198 }
199
200 static void
AliasHandleCUSeeMeIn(struct libalias * la,struct ip * pip,struct in_addr original_addr)201 AliasHandleCUSeeMeIn(struct libalias *la, struct ip *pip, struct in_addr original_addr)
202 {
203 struct in_addr alias_addr;
204 struct udphdr *ud;
205 struct cu_header *cu;
206 struct oc_header *oc;
207 struct client_info *ci;
208 char *end;
209 int i;
210
211 (void)la;
212 alias_addr.s_addr = pip->ip_dst.s_addr;
213 ud = ip_next(pip);
214 cu = udp_next(ud);
215 oc = (struct oc_header *)(cu + 1);
216 ci = (struct client_info *)(oc + 1);
217 end = (char *)ud + ntohs(ud->uh_ulen);
218
219 if ((char *)oc <= end) {
220 if (cu->dest_addr)
221 cu->dest_addr = (u_int32_t) original_addr.s_addr;
222 if (ntohs(cu->data_type) == 101)
223 /* Find and change our address */
224 for (i = 0; (char *)(ci + 1) <= end && i < oc->client_count; i++, ci++)
225 if (ci->address == (u_int32_t) alias_addr.s_addr) {
226 ci->address = (u_int32_t) original_addr.s_addr;
227 break;
228 }
229 }
230 }
231