File: mod_tcp_seg.c

package info (click to toggle)
fragroute 1.2-7
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 692 kB
  • ctags: 520
  • sloc: ansic: 4,331; sh: 2,842; makefile: 74
file content (150 lines) | stat: -rw-r--r-- 3,630 bytes parent folder | download | duplicates (2)
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
/*
 * mod_tcp_seg.c
 *
 * Copyright (c) 2001 Dug Song <dugsong@monkey.org>
 *
 * $Id: mod_tcp_seg.c,v 1.12 2002/04/07 22:55:20 dugsong Exp $
 */

#include "config.h"

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "mod.h"
#include "pkt.h"
#include "randutil.h"

#ifndef MIN
#define MIN(a,b)	(((a)<(b))?(a):(b))
#endif

#define FAVOR_OLD	1
#define FAVOR_NEW	2

static struct tcp_seg_data {
	rand_t	*rnd;
	int	 size;
	int	 overlap;
} tcp_seg_data;

void *
tcp_seg_close(void *d)
{
	if (tcp_seg_data.rnd != NULL)
		rand_close(tcp_seg_data.rnd);
	tcp_seg_data.size = 0;
	return (NULL);
}

void *
tcp_seg_open(int argc, char *argv[])
{
	if (argc < 2) {
		warnx("need segment <size> in bytes");
		return (NULL);
	}
	tcp_seg_data.rnd = rand_open();
	
	if ((tcp_seg_data.size = atoi(argv[1])) == 0) {
		warnx("invalid segment size '%s'", argv[1]);
		return (tcp_seg_close(&tcp_seg_data));
	}
	if (argc == 3) {
		if (strcmp(argv[2], "old") == 0 ||
		    strcmp(argv[2], "win32") == 0)
			tcp_seg_data.overlap = FAVOR_OLD;
		else if (strcmp(argv[2], "new") == 0 ||
		    strcmp(argv[2], "unix") == 0)
			tcp_seg_data.overlap = FAVOR_NEW;
		else
			return (tcp_seg_close(&tcp_seg_data));
	}
	return (&tcp_seg_data);
}

int
tcp_seg_apply(void *d, struct pktq *pktq)
{
	struct pkt *pkt, *new, *next, tmp;
	uint32_t seq;
	int hl, tl, len;	
	u_char *p, *p1, *p2;

	for (pkt = TAILQ_FIRST(pktq); pkt != TAILQ_END(pktq); pkt = next) {
		next = TAILQ_NEXT(pkt, pkt_next);
		
		if (pkt->pkt_ip == NULL || pkt->pkt_ip->ip_p != IP_PROTO_TCP ||
		    pkt->pkt_tcp == NULL || pkt->pkt_tcp_data == NULL ||
		    (pkt->pkt_tcp->th_flags & TH_ACK) == 0 ||
		    pkt->pkt_end - pkt->pkt_tcp_data <= tcp_seg_data.size)
			continue;
		
		hl = pkt->pkt_ip->ip_hl << 2;
		tl = pkt->pkt_tcp->th_off << 2;
		seq = ntohl(pkt->pkt_tcp->th_seq);
	
		for (p = pkt->pkt_tcp_data; p < pkt->pkt_end; p += len) {
			new = pkt_new();
			p1 = p, p2 = NULL;
			len = MIN(pkt->pkt_end - p, tcp_seg_data.size);
		
			if (tcp_seg_data.overlap != 0 &&
			    p + (len << 1) < pkt->pkt_end) {
				rand_strset(tcp_seg_data.rnd, tmp.pkt_buf,len);
				
				if (tcp_seg_data.overlap == FAVOR_OLD) {
					p1 = p + len;
					p2 = tmp.pkt_buf;
				} else if (tcp_seg_data.overlap == FAVOR_NEW) {
					p1 = tmp.pkt_buf;
					p2 = p + len;
				}
				len = tcp_seg_data.size;
				seq += tcp_seg_data.size;
			}
			memcpy(new->pkt_ip, pkt->pkt_ip, hl + tl);
			new->pkt_ip_data = new->pkt_eth_data + hl;
			new->pkt_tcp_data = new->pkt_ip_data + tl;
			memcpy(new->pkt_tcp_data, p1, len);
			new->pkt_end = new->pkt_tcp_data + len;
			
			new->pkt_ip->ip_id = rand_uint16(tcp_seg_data.rnd);
			new->pkt_ip->ip_len = htons(hl + tl + len);
			new->pkt_tcp->th_seq = htonl(seq);
			ip_checksum(new->pkt_ip, hl + tl + len);
			TAILQ_INSERT_BEFORE(pkt, new, pkt_next);
			
			if (p2 != NULL) {
				new = pkt_dup(new);
				new->pkt_ts.tv_usec = 1;
				new->pkt_ip->ip_id =
				    rand_uint16(tcp_seg_data.rnd);
				new->pkt_ip->ip_len = htons(hl + tl +
				    (len << 1));
				new->pkt_tcp->th_seq = htonl(seq - len);
				
				memcpy(new->pkt_tcp_data, p, len);
				memcpy(new->pkt_tcp_data + len, p2, len);
				new->pkt_end = new->pkt_tcp_data + (len << 1);
				ip_checksum(new->pkt_ip, hl + tl + (len << 1));
				TAILQ_INSERT_BEFORE(pkt, new, pkt_next);
				p += len;
			}
			seq += len;
		}
		TAILQ_REMOVE(pktq, pkt, pkt_next);
		pkt_free(pkt);
	}
	return (0);
}

struct mod mod_tcp_seg = {
	"tcp_seg",				/* name */
	"tcp_seg <size> [old|new]",		/* usage */
	tcp_seg_open,				/* open */
	tcp_seg_apply,				/* apply */
	tcp_seg_close				/* close */
};