-
Notifications
You must be signed in to change notification settings - Fork 83
/
tcp_raw.c
236 lines (195 loc) · 4.77 KB
/
tcp_raw.c
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
/*
* tcp_raw.c
*
* Copyright (c) 2000 Dug Song <[email protected]>
*
* $Id: tcp_raw.c,v 1.10 2001/03/15 08:33:04 dugsong Exp $
*/
#include "config.h"
#include <sys/types.h>
#include <sys/uio.h>
#include <netinet/in.h>
#include <time.h>
#include <err.h>
#include <libnet.h>
#include "options.h"
#include "tcp_raw.h"
struct tha {
in_addr_t src;
in_addr_t dst;
u_short port;
};
struct tcp_seg {
u_int32_t seq;
u_char *data;
int len;
};
struct tcp_conn {
struct tha tha;
time_t mtime;
struct tcp_seg *seg;
int segcnt;
int segmax;
struct tcp_conn *next;
};
#define TCP_TIMEOUT 60
#define TCP_HASHSIZE 919
static struct tcp_conn conntab[TCP_HASHSIZE];
static int
tcp_seg_compare(const void *a, const void *b)
{
struct tcp_seg *sa, *sb;
sa = (struct tcp_seg *) a;
sb = (struct tcp_seg *) b;
if (sa->seq < sb->seq)
return (-1);
else if (sa->seq > sb->seq)
return (1);
else return (0);
}
static void
tcp_raw_delete(struct tcp_conn *conn)
{
struct tcp_conn *hold;
int i;
if (conn->next != NULL) {
for (i = 0; i < conn->segcnt; i++) {
if (conn->seg[i].data)
free(conn->seg[i].data);
}
free(conn->seg);
conn->seg = NULL;
conn->segcnt = conn->segmax = 0;
if (conn->next->next != NULL) {
hold = conn->next;
*conn = *conn->next;
free(hold);
}
else {
free(conn->next);
conn->next = NULL;
}
}
}
static struct iovec *
tcp_raw_reassemble(struct tcp_conn *conn, int minlen)
{
struct iovec *iov;
int i, len;
len = 0;
for (i = 0; i < conn->segcnt; i++)
len += conn->seg[i].len;
if (len < minlen)
return (NULL);
if ((iov = (struct iovec *) malloc(sizeof(*iov))) == NULL)
err(1, "tcp_raw_reassemble: malloc");
if ((iov->iov_base = (u_char *) malloc(len)) == NULL)
err(1, "tcp_raw_reassemble: malloc");
iov->iov_len = 0;
qsort(conn->seg, conn->segcnt, sizeof(*conn->seg), tcp_seg_compare);
for (i = 0; i < conn->segcnt; i++) {
len = conn->seg[i].len;
memcpy(iov->iov_base + iov->iov_len, conn->seg[i].data, len);
iov->iov_len += len;
}
return (iov);
}
struct iovec *
tcp_raw_input(struct libnet_ipv4_hdr *ip, struct libnet_tcp_hdr *tcp, int len)
{
struct tha tha;
struct tcp_conn *conn;
struct tcp_seg seg;
struct iovec *iov;
u_short cksum;
u_char *buf;
int tcp_hl = tcp->th_off * 4;
/* Verify TCP checksum. */
cksum = tcp->th_sum;
libnet_do_checksum(NULL, (u_char *) ip, IPPROTO_TCP, len);
if (cksum != tcp->th_sum)
return (NULL);
tha.src = ip->ip_src.s_addr;
tha.dst = ip->ip_dst.s_addr;
tha.port = ntohs(tcp->th_sport) << 16 | ntohs(tcp->th_dport);
buf = (u_char *)tcp + tcp_hl;
len -= tcp_hl;
iov = NULL;
/* Find half-duplex stream associated with this segment. */
for (conn = &conntab[tha.port % TCP_HASHSIZE];
conn->next != NULL; conn = conn->next) {
if (memcmp((char *)&tha, (char *)&conn->tha, sizeof(tha)) == 0)
break;
}
/* Process by TCP flags. */
if (conn->next == NULL) {
if (tcp->th_flags & TH_SYN) {
if (conn->next == NULL &&
(conn->next = (struct tcp_conn *)
calloc(1, sizeof(*conn))) == NULL) {
err(1, "tcp_raw_input: calloc");
}
conn->tha = tha;
if (conn->seg == NULL &&
(conn->seg = (struct tcp_seg *)
malloc(sizeof(seg) * 128)) == NULL) {
err(1, "tcp_raw_input: malloc");
}
conn->segmax = 128;
}
}
else if (tcp->th_flags & TH_FIN || tcp->th_flags & TH_RST) {
iov = tcp_raw_reassemble(conn, 1);
}
else if (tcp->th_flags & TH_ACK && len > 0) {
seg.seq = ntohl(tcp->th_seq);
if (bsearch(&seg, conn->seg, conn->segcnt,
sizeof(seg), tcp_seg_compare) == NULL) {
if ((seg.data = (u_char *) malloc(len)) == NULL)
err(1, "tcp_raw_input: malloc");
memcpy(seg.data, buf, len);
seg.len = len;
if (conn->segcnt == conn->segmax) {
if ((conn->seg = (struct tcp_seg *)
realloc(conn->seg, (conn->segmax * 2) *
sizeof(seg))) == NULL)
err(1, "tcp_raw_input: realloc");
conn->segmax *= 2;
}
conn->seg[conn->segcnt++] = seg;
iov = tcp_raw_reassemble(conn, Opt_snaplen);
}
}
conn->mtime = time(NULL);
/* If we successfully reassembled the stream, delete its entry. */
if (iov != NULL) {
tcp_raw_delete(conn);
}
return (iov);
}
void
tcp_raw_timeout(int timeout, tcp_raw_callback_t callback)
{
struct tcp_conn *conn;
struct iovec *iov;
time_t now;
int i;
now = time(NULL);
for (i = 0; i < TCP_HASHSIZE; i++) {
for (conn = &conntab[i]; conn != NULL && conn->next != NULL;
conn = conn->next) {
if (now - conn->mtime > timeout) {
iov = tcp_raw_reassemble(conn, 1);
if (iov != NULL) {
callback(conn->tha.src, conn->tha.dst,
conn->tha.port >> 16,
conn->tha.port & 0xffff,
iov->iov_base, iov->iov_len);
free(iov->iov_base);
free(iov);
}
tcp_raw_delete(conn);
}
}
}
}