summaryrefslogtreecommitdiff
path: root/pfinet/linux-src/net/ipv4/ip_masq_quake.c
blob: 995c3a0a36195f4bdd1cf9e8ceb5a8fe799ca950 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
 *		IP_MASQ_QUAKE quake masquerading module
 *
 *
 * Version:	@(#)ip_masq_quake.c 0.02   22/02/97
 *
 * Author:	Harald Hoyer mailto:HarryH@Royal.Net
 *		
 *
 * Fixes: 
 *      Harald Hoyer            :       Unofficial Quake Specs found at 
 *                                 http://www.gamers.org/dEngine/quake/spec/ 
 *      Harald Hoyer            :       Check for QUAKE-STRING
 *	Juan Jose Ciarlante	:  litl bits for 2.1
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 *  
 *  
 */

#include <linux/module.h>
#include <asm/system.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/skbuff.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/init.h>
#include <net/protocol.h>
#include <net/udp.h>
#include <net/ip_masq.h>

#define DEBUG_CONFIG_IP_MASQ_QUAKE 0

typedef struct
{ 
        __u16 type;     // (Little Endian) Type of message.
	__u16 length;   // (Little Endian) Length of message, header included. 
	char  message[0];  // The contents of the message.
} QUAKEHEADER;

struct quake_priv_data {
	/* Have we seen a client connect message */
	signed char	cl_connect;
};

static int
masq_quake_init_1 (struct ip_masq_app *mapp, struct ip_masq *ms)
{
        MOD_INC_USE_COUNT;
	if ((ms->app_data = kmalloc(sizeof(struct quake_priv_data),
				    GFP_ATOMIC)) == NULL) 
		printk(KERN_INFO "Quake: No memory for application data\n");
	else 
	{
		struct quake_priv_data *priv = 
			(struct quake_priv_data *)ms->app_data;
		priv->cl_connect = 0;
	}
        return 0;
}

static int
masq_quake_done_1 (struct ip_masq_app *mapp, struct ip_masq *ms)
{
	MOD_DEC_USE_COUNT;
	if (ms->app_data)
		kfree_s(ms->app_data, sizeof(struct quake_priv_data));
	return 0;
}

int
masq_quake_in (struct ip_masq_app *mapp, struct ip_masq *ms, struct sk_buff **skb_p, __u32 maddr)
{
	struct sk_buff *skb;
	struct iphdr *iph;
	struct udphdr *uh;
	QUAKEHEADER *qh;
	__u16 udp_port;
	char *data;
	unsigned char code;
	struct quake_priv_data *priv = (struct quake_priv_data *)ms->app_data;
        
	if(priv->cl_connect == -1)
	  return 0;

	skb = *skb_p;

	iph = skb->nh.iph;
	uh = (struct udphdr *)&(((char *)iph)[iph->ihl*4]);

	/* Check for lenght */
	if(ntohs(uh->len) < 5)
	  return 0;
	
	qh = (QUAKEHEADER *)&uh[1];

	if(qh->type != 0x0080)
	  return 0;

	
	code = qh->message[0];

#if DEBUG_CONFIG_IP_MASQ_QUAKE
	  printk("Quake_in: code = %d \n", (int)code);
#endif

	switch(code) {
	case 0x01:
	  /* Connection Request */

	  if(ntohs(qh->length) < 0x0c) {
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_in: length < 0xc \n");
#endif
	    return 0;
	  }

	  data = &qh->message[1];

	  /* Check for stomping string */
	  if(memcmp(data,"QUAKE\0\3",7)) {
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_out: memcmp failed \n");
#endif
	    return 0;
	  }
	  else {
	    priv->cl_connect = 1;
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_out: memcmp ok \n");
#endif
	  }
	  break;

	case 0x81:
	  /* Accept Connection */
	  if((ntohs(qh->length) < 0x09) || (priv->cl_connect == 0))
	    return 0;
	  data = &qh->message[1];

	  memcpy(&udp_port, data, 2);

	  ms->dport = htons(udp_port);

#if DEBUG_CONFIG_IP_MASQ_QUAKE
	  printk("Quake_in: in_rewrote UDP port %d \n", udp_port);
#endif
	  priv->cl_connect = -1;

	  break;
	}
	 
	return 0;
}

int
masq_quake_out (struct ip_masq_app *mapp, struct ip_masq *ms, struct sk_buff **skb_p, __u32 maddr)
{
	struct sk_buff *skb;
	struct iphdr *iph;
	struct udphdr *uh;
	QUAKEHEADER *qh;
	__u16 udp_port;
	char *data;
	unsigned char code;
	struct ip_masq *n_ms;
	struct quake_priv_data *priv = (struct quake_priv_data *)ms->app_data;

	if(priv->cl_connect == -1)
	  return 0;
        
	skb = *skb_p;

	iph = skb->nh.iph;
	uh = (struct udphdr *)&(((char *)iph)[iph->ihl*4]);

	/* Check for lenght */
	if(ntohs(uh->len) < 5)
	  return 0;
	
	qh = (QUAKEHEADER *)&uh[1];

#if DEBUG_CONFIG_IP_MASQ_QUAKE
	  printk("Quake_out: qh->type = %d \n", (int)qh->type);
#endif

	if(qh->type != 0x0080)
	  return 0;
	
	code = qh->message[0];

#if DEBUG_CONFIG_IP_MASQ_QUAKE
	  printk("Quake_out: code = %d \n", (int)code);
#endif

	switch(code) {
	case 0x01:
	  /* Connection Request */

	  if(ntohs(qh->length) < 0x0c) {
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_out: length < 0xc \n");
#endif
	    return 0;
	  }

	  data = &qh->message[1];

	  /* Check for stomping string */
	  if(memcmp(data,"QUAKE\0\3",7)) {
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_out: memcmp failed \n");
#endif
	    return 0;
	  }
	  else {
	    priv->cl_connect = 1;
#if DEBUG_CONFIG_IP_MASQ_QUAKE
	    printk("Quake_out: memcmp ok \n");
#endif
	  }
	  break;

	case 0x81:
	  /* Accept Connection */
	  if((ntohs(qh->length) < 0x09) || (priv->cl_connect == 0))
	    return 0;

	  data = &qh->message[1];

	  memcpy(&udp_port, data, 2);
	  
	  n_ms = ip_masq_new(IPPROTO_UDP,
			     maddr, 0,
			     ms->saddr, htons(udp_port),
			     ms->daddr, ms->dport,
			     0);

	  if (n_ms==NULL)
	    return 0;

#if DEBUG_CONFIG_IP_MASQ_QUAKE
	  printk("Quake_out: out_rewrote UDP port %d -> %d\n",
		 udp_port, ntohs(n_ms->mport));
#endif
	  udp_port = ntohs(n_ms->mport);
	  memcpy(data, &udp_port, 2);

	  ip_masq_listen(n_ms);
	  ip_masq_control_add(n_ms, ms);
	  ip_masq_put(n_ms);

	  break;
	}
	 
	return 0;
}

struct ip_masq_app ip_masq_quake = {
        NULL,			/* next */
	"Quake_26",	       	/* name */
        0,                      /* type */
        0,                      /* n_attach */
        masq_quake_init_1,      /* ip_masq_init_1 */
        masq_quake_done_1,      /* ip_masq_done_1 */
        masq_quake_out,         /* pkt_out */
        masq_quake_in           /* pkt_in */
};
struct ip_masq_app ip_masq_quakenew = {
        NULL,			/* next */
	"Quake_27",	       	/* name */
        0,                      /* type */
        0,                      /* n_attach */
        masq_quake_init_1,      /* ip_masq_init_1 */
        masq_quake_done_1,      /* ip_masq_done_1 */
        masq_quake_out,         /* pkt_out */
        masq_quake_in           /* pkt_in */
};

/*
 * 	ip_masq_quake initialization
 */

__initfunc(int ip_masq_quake_init(void))
{
        return (register_ip_masq_app(&ip_masq_quake, IPPROTO_UDP, 26000) +
		register_ip_masq_app(&ip_masq_quakenew, IPPROTO_UDP, 27000));
}

/*
 * 	ip_masq_quake fin.
 */

int ip_masq_quake_done(void)
{
        return (unregister_ip_masq_app(&ip_masq_quake) +
                unregister_ip_masq_app(&ip_masq_quakenew));
}

#ifdef MODULE
EXPORT_NO_SYMBOLS;

int init_module(void)
{
        if (ip_masq_quake_init() != 0)
                return -EIO;
        return 0;
}

void cleanup_module(void)
{
        if (ip_masq_quake_done() != 0)
                printk("ip_masq_quake: can't remove module");
}

#endif /* MODULE */