File: classifier.cpp

package info (click to toggle)
shaperd 0.2pre44-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 348 kB
  • ctags: 740
  • sloc: cpp: 3,482; ansic: 685; sh: 105; makefile: 84
file content (406 lines) | stat: -rw-r--r-- 8,796 bytes parent folder | download | duplicates (3)
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "tcpip.hpp"
#include "debug.hpp"
#include "classifier.hpp"
#include <new>
#include <stdio.h>
#include <string.h>

extern "C" {
	#include <netinet/in.h>
	#include <netinet/ip.h>
	#include <netinet/tcp.h>
	#include <netinet/udp.h>
	#include <arpa/inet.h>
}

using namespace std;

static int set_if(char *str, char **p_if, int *len);

ipv4_packet_classifier::ipv4_packet_classifier()
{
	saddr = daddr = 0;
	smask = dmask = u_int32_t(-1L);
	user_prio = real_prio = -1;
	proto = __IP_HDR_PROTO_ALL__;
	sport_list = dport_list = 0;
	n_sports = n_dports = 0;
	inp_if = out_if = 0;
}

ipv4_packet_classifier::~ipv4_packet_classifier()
{
	delete[] sport_list;
	delete[] dport_list;
	delete[] inp_if;
	delete[] out_if;
}

// data in host order
// 1 -> hit
// 0 -> miss
inline int ipv4_packet_classifier::check_port(int port, port_range *port_list, 
	int n_ports)
{
	if( n_ports==0 ) return 1;
	for( int i = 0 ; i<n_ports ; i++ ) {
		int lo = port_list[i].low;
		int hi = port_list[i].high;
		if( port>=lo && port<=hi ) return 1;
	}
	return 0;
}

// 1 -> packet hit
// 0 -> miss (didn't match)
int ipv4_packet_classifier::check(generic_packet *pckt)
{
	struct iphdr *ip_hdr = (struct iphdr *)(pckt->payload);
	u_int32_t packet_masked_saddr, packet_masked_daddr;
	int result;

	packet_masked_saddr = ip_hdr->saddr & smask;
	packet_masked_daddr = ip_hdr->daddr & dmask;
	if( packet_masked_saddr != saddr ) return 0;
	if( packet_masked_daddr != daddr ) return 0;

	if( inp_if ) {
		if( pckt->inp_if==0 ) return 0;
		result = strncmp(inp_if, pckt->inp_if, n_inp_if);
		if( result!=0 ) return 0;
	}
	if( out_if ) {
		if( pckt->out_if==0 ) return 0;
		result = strncmp(out_if, pckt->out_if, n_out_if);
		if( result!=0 ) return 0;
	}

	if( proto>=0 ) {
		if( proto!=int(ip_hdr->protocol) ) return 0;

		if( proto==__IP_HDR_PROTO_TCP__ ) {
			unsigned int hl = ip_hdr->ihl << 2;
			void *ip_payload = ((char*)ip_hdr) + hl;
			struct tcphdr *tcp_hdr = (struct tcphdr*)ip_payload;

			result = check_port(ntohs(tcp_hdr->source), sport_list, 
				n_sports);
			if( result==0 ) return 0;

			result = check_port(ntohs(tcp_hdr->dest), dport_list, 
				n_dports);
			if( result==0 ) return 0;

		} else if( proto==__IP_HDR_PROTO_UDP__ ) {
			unsigned int hl = ip_hdr->ihl << 2;
			void *ip_payload = (char*)(ip_hdr) + hl;
			struct udphdr *udp_hdr = (struct udphdr*)ip_payload;

			result = check_port(ntohs(udp_hdr->source), sport_list, 
				n_sports);
			if( result==0 ) return 0;

			result = check_port(ntohs(udp_hdr->dest), dport_list, 
				n_dports);
			if( result==0 ) return 0;			
		}
	}

	return 1;
}

int ipv4_packet_classifier::set_saddr(const char *addr, const char *mask)
{
	int result;
	struct in_addr tmpaddr, tmpmask;

	result = inet_aton(addr, &tmpaddr);
	if( result==0 ) return -1;

	result = inet_aton(mask, &tmpmask);
	if( result==0 ) return -1;

	saddr = tmpaddr.s_addr & tmpmask.s_addr;
	smask = tmpmask.s_addr;

	return 0;
}

int ipv4_packet_classifier::set_daddr(const char *addr, const char *mask)
{
	int result;
	struct in_addr tmpaddr, tmpmask;

	result = inet_aton(addr, &tmpaddr);
	if( result==0 ) return -1;

	result = inet_aton(mask, &tmpmask);
	if( result==0 ) return -1;

	daddr = tmpaddr.s_addr & tmpmask.s_addr;
	dmask = tmpmask.s_addr;

	return 0;
}

int ipv4_packet_classifier::set_proto(int n)
{
	if( n<-1 || n>255 ) return -1;
	proto = n;
	return 0;
}

int ipv4_packet_classifier::add_src_port(int lo, int hi)
{
	if( lo<0 || lo>65535 ) return -1;
	if( hi<0 || hi>65535 ) return -1;
	if( lo>hi ) return -1;

	struct port_range *new_list = new(nothrow) port_range[n_sports+1];
	if( new_list==0 ) return -1;

	if( n_sports>0 ) {
		memcpy(new_list, sport_list, n_sports*sizeof(port_range));
		delete[] sport_list;
	}
	sport_list = new_list;

	sport_list[n_sports].low = lo;
	sport_list[n_sports].high = hi;
	n_sports++;
	return 0;
}

int ipv4_packet_classifier::add_dst_port(int lo, int hi)
{
	if( lo<0 || lo>65535 ) return -1;
	if( hi<0 || hi>65535 ) return -1;
	if( lo>hi ) return -1;

	struct port_range *new_list = new(nothrow) port_range[n_dports+1];
	if( new_list==0 ) return -1;

	if( n_dports>0 ) {
		memcpy(new_list, dport_list, n_dports*sizeof(port_range));
		delete[] dport_list;
	}
	dport_list = new_list;

	dport_list[n_dports].low = lo;
	dport_list[n_dports].high = hi;
	n_dports++;
	return 0;
}

char *ipv4_packet_classifier::log_attr(char *line, int bytes)
{
	int i, n;
	char *proto_str, *inp_if_str, *out_if_str;
	struct in_addr in_saddr, in_smask, in_daddr, in_dmask;
	char saddr_str[64], daddr_str[64], smask_str[64], dmask_str[64];

	switch(proto) {
		case __IP_HDR_PROTO_TCP__ : proto_str = "tcp";  break;
		case __IP_HDR_PROTO_UDP__ : proto_str = "udp";  break;
		case __IP_HDR_PROTO_ICMP__: proto_str = "icmp"; break;
		case __IP_HDR_PROTO_ALL__ : proto_str = "all";  break;
		default: proto_str = "?"; break;
	}

	inp_if_str = inp_if ? inp_if : ((char*)"all");
	out_if_str = out_if ? out_if : ((char*)"all");

	in_saddr.s_addr = saddr;
	in_smask.s_addr = smask;
	in_daddr.s_addr = daddr;
	in_dmask.s_addr = dmask;
	snprintf(saddr_str, 63, "%s", inet_ntoa(in_saddr));
	snprintf(smask_str, 63, "%s", inet_ntoa(in_smask));
	snprintf(daddr_str, 63, "%s", inet_ntoa(in_daddr));
	snprintf(dmask_str, 63, "%s", inet_ntoa(in_dmask));

	n = snprintf(line, bytes-1, "prio=%d(%d) proto=%s "
		"inp_if=\"%s\" out_if=\"%s\" saddr=%s/%s sport=", 
		user_prio, real_prio, proto_str, inp_if_str, out_if_str, 
		saddr_str, smask_str);
	if( n>=bytes-2 )
		return "buffer overflow!";

	if( n_sports==0 ) {
		n += snprintf(line+n, bytes-n-1, "all ");
		if( n>=bytes-2) 
			return "buffer overflow!";
	} else {
		for( i = 0 ; i<n_sports ; i++ ) {
			int lo = sport_list[i].low;
			int hi = sport_list[i].high;
			if( lo==hi ) {
				n += snprintf(line+n, bytes-n-1, 
					i==n_sports-1 ? "%d " : "%d,", 
					lo);
			} else {
				n += snprintf(line+n, bytes-n-1, 
					i==n_sports-1 ? "%d-%d " : "%d-%d,", 
					lo, hi);
			}
			if( n>=bytes-2 ) 
				return "buffer overflow!";
		}
	}

	n += snprintf(line+n, bytes-n-1, "daddr=%s/%s dport=",
		daddr_str, dmask_str);
	if( n>=bytes-2 ) 
		return "buffer overflow!";

	if( n_dports==0 ) {
		n += snprintf(line+n, bytes-n-1, "all");
		if( n>=bytes-2 ) 
			return "buffer overflow!";
	} else {
		for( i = 0 ; i<n_dports ; i++ ) {
			int lo = dport_list[i].low;
			int hi = dport_list[i].high;
			if( lo==hi ) {
				n += snprintf(line+n, bytes-n-1, 
					i==n_dports-1 ? "%d " : "%d,", 
					lo);
			} else {
				n += snprintf(line+n, bytes-n-1, 
					i==n_dports-1 ? "%d-%d " : "%d-%d,",
					lo, hi);
			}
			if( n>=bytes-2 ) 
				return "buffer overflow!";
		}
	}

	return line;
}

char *ipv4_packet_classifier::log()
{
	int n;
	static char line[1024];

	n = snprintf(line, 1024, "%s",
		"ipv4 classifier ");
	log_attr(line+n, 1024-n);
	return line;
}

int ipv4_packet_classifier::set_user_prio(int pr)
{
	if( pr<0 ) return -1;
	user_prio = pr;	
	return 0;
}

int ipv4_packet_classifier::set_real_prio(int pr)
{
	if( pr<0 ) return -1;
	real_prio = pr;	
	return 0;
}

int ipv4_packet_classifier::get_user_prio()
{
	return user_prio;
}

int ipv4_packet_classifier::get_real_prio()
{
	return real_prio;
}

// doc, i think i'm starting to hate c++
static int set_if(char *str, char **p_if, int *len)
{
	char *p;
	int n = strlen(str);

	assert(n>1);

	p = new(nothrow) char[n+1];
	if( p==0 ) return -1;

	memcpy(p, str, n+1);
	*p_if  = p;
	*len = p[n-1]=='+' ? n-1 : n;

	return 0;
}

int ipv4_packet_classifier::set_inp_if(char *str)
{
	assert(inp_if==0);
	return set_if(str, &inp_if, &n_inp_if);
}

int ipv4_packet_classifier::set_out_if(char *str)
{
	assert(out_if==0);
	return set_if(str, &out_if, &n_out_if);
}

#ifdef WITH_IPQ
nf_packet_classifier::nf_packet_classifier()
{
	fwmark = fwmask = 0;
}

nf_packet_classifier::~nf_packet_classifier()
{
}

// 1 -> packet hit
// 0 -> miss (didn't match)
int nf_packet_classifier::check(generic_packet *pckt)
{
	int result;

	result = ipv4_packet_classifier::check(pckt);
	if (result != 1)
		return 0; 

	// this could be easily done with c++'s rtti support
	// but, it isn't necessary since there's just one and
	// only source for the packets
	ipq_packet *ipq_pckt = (ipq_packet*)(pckt);

	if ((ipq_pckt->meta->mark&fwmask) != fwmark)
		return 0;

	return 1;
}

char *nf_packet_classifier::log_attr(char *line, int bytes)
{
	char *pc;
	int n, len;

	pc = ipv4_packet_classifier::log_attr(line, bytes);
	if (pc != line)
		return pc;

	len = strlen(line);
	pc = line + len;
	n = snprintf(pc, bytes-len, " fwmark=0x%lx/0x%lx",
		fwmark, fwmask);
	if (n >= bytes-len)
		return "buffer overflow!";

	return line;
}

char *nf_packet_classifier::log()
{
	int n;
	static char line[1024];

	n = snprintf(line, 1024, "%s",
		"nf classifier ");
	log_attr(line+n, 1024);
	return line;
}
#endif