-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtcp.c
229 lines (203 loc) · 3.91 KB
/
tcp.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
/*
* This file is part of the UCB release of Plan 9. It is subject to the license
* terms in the LICENSE file found in the top-level directory of this
* distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
* part of the UCB release of Plan 9, including this file, may be copied,
* modified, propagated, or distributed except according to the terms contained
* in the LICENSE file.
*/
#include "ip.h"
#include "dat.h"
#include "protos.h"
typedef struct Hdr Hdr;
struct Hdr
{
uint8_t sport[2];
uint8_t dport[2];
uint8_t seq[4];
uint8_t ack[4];
uint8_t flag[2];
uint8_t win[2];
uint8_t cksum[2];
uint8_t urg[2];
uint8_t opt[1];
};
typedef struct PseudoHdr{
uint8_t src[4];
uint8_t dst[4];
uint8_t zero;
uint8_t proto;
uint8_t length[2];
uint8_t hdrdata[1580];
} PseudoHdr;
enum
{
TCPLEN= 20,
};
enum
{
Os,
Od,
Osd,
};
static Field p_fields[] =
{
{"s", Fnum, Os, "source port", } ,
{"d", Fnum, Od, "dest port", } ,
{"a", Fnum, Osd, "source/dest port", } ,
{"sd", Fnum, Osd, "source/dest port", } ,
{0}
};
static Mux p_mux[] =
{
{"dns", 53, },
{"ninep", 17007, }, /* exportfs */
{"ninep", 564, }, /* 9fs */
{"ninep", 17005, }, /* ocpu */
{"ninep", 17010, }, /* ncpu */
{"ninep", 17013, }, /* cpu */
{0},
};
enum
{
EOLOPT = 0,
NOOPOPT = 1,
MSSOPT = 2,
MSS_LENGTH = 4, /* Mean segment size */
WSOPT = 3,
WS_LENGTH = 3, /* Bits to scale window size by */
};
static void
p_compile(Filter *f)
{
Mux *m;
if(f->op == '='){
compile_cmp(tcp.name, f, p_fields);
return;
}
for(m = p_mux; m->name != NULL; m++)
if(strcmp(f->s, m->name) == 0){
f->pr = m->pr;
f->ulv = m->val;
f->subop = Osd;
return;
}
sysfatal( "unknown tcp field or protocol: %s", f->s);
}
static int
p_filter(Filter *f, Msg *m)
{
Hdr *h;
if(m->pe - m->ps < TCPLEN)
return 0;
h = (Hdr*)m->ps;
m->ps += (NetS(h->flag)>>10) & 0x3f;
switch(f->subop){
case Os:
return NetS(h->sport) == f->ulv;
case Od:
return NetS(h->dport) == f->ulv;
case Osd:
return NetS(h->sport) == f->ulv || NetS(h->dport) == f->ulv;
}
return 0;
}
enum
{
URG = 0x20, /* Data marked urgent */
ACK = 0x10, /* Aknowledge is valid */
PSH = 0x08, /* Whole data pipe is pushed */
RST = 0x04, /* Reset connection */
SYN = 0x02, /* Pkt. is synchronise */
FIN = 0x01, /* Start close down */
};
static char*
flags(int f)
{
static char fl[20];
char *p;
p = fl;
if(f & URG)
*p++ = 'U';
if(f & ACK)
*p++ = 'A';
if(f & PSH)
*p++ = 'P';
if(f & RST)
*p++ = 'R';
if(f & SYN)
*p++ = 'S';
if(f & FIN)
*p++ = 'F';
*p = 0;
return fl;
}
static int
p_seprint(Msg *m)
{
int dport, sport, len, flag, optlen;
uint8_t *optr;
Hdr *h;
if(m->pe - m->ps < TCPLEN)
return -1;
h = (Hdr*)m->ps;
/* get tcp header length */
flag = NetS(h->flag);
len = (flag>>10) & ~3;
flag &= 0x3ff;
m->ps += len;
/* next protocol */
dport = NetS(h->dport);
sport = NetS(h->sport);
demux(p_mux, sport, dport, m, &dump);
m->p = seprint(m->p, m->e, "s=%d d=%d seq=%lu ack=%lu fl=%s hl=%d win=%d ck=%4.4x",
NetS(h->sport), dport,
(uint32_t)NetL(h->seq), (uint32_t)NetL(h->ack),
flags(flag), len, NetS(h->win),
NetS(h->cksum));
/* tcp options */
len -= TCPLEN;
optr = h->opt;
while(len > 0) {
if(*optr == EOLOPT){
m->p = seprint(m->p, m->e, " opt=EOL");
break;
}
if(*optr == NOOPOPT) {
m->p = seprint(m->p, m->e, " opt=NOOP");
len--;
optr++;
continue;
}
optlen = optr[1];
if(optlen < 2 || optlen > len)
break;
switch(*optr) {
case MSSOPT:
m->p = seprint(m->p, m->e, " opt%d=(mss %u)",
optlen, nhgets(optr+2));
break;
case WSOPT:
m->p = seprint(m->p, m->e, " opt%d=(wscale %u)",
optlen, *(optr+2));
break;
default:
m->p = seprint(m->p, m->e, " opt%d=(%u %.*H)",
optlen, *optr, optlen-2, optr+2);
}
len -= optlen;
optr += optlen;
}
return 0;
}
Proto tcp =
{
"tcp",
p_compile,
p_filter,
p_seprint,
p_mux,
"%lu",
p_fields,
defaultframer,
};