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
|
/*
* IEEE 802.11.
*/
#include <u.h>
#include <libc.h>
#include <ip.h>
#include "dat.h"
#include "protos.h"
enum
{
Tmgmt = 0,
Tctl,
Tdata,
CtlPoll = 0xA,
CtlRts,
CtlCts,
CtlAck,
CtlCfEnd,
CtlCfEndAck,
Data = 0,
DataCfAck,
DataCfPoll,
DataCfAckPoll,
Nodata,
NodataCfAck,
NodataCfPoll,
NodataCfAckPoll,
FlagTods = 0x1,
FlagFromds = 0x2,
FlagMoreflag = 0x4,
FlagRetry = 0x8,
FlagPowerMgmt = 0x10,
FlagMoreData = 0x20,
FlagWep = 0x40,
FlagOrder = 0x80,
ProtoNone = 0,
ProtoLlc,
};
static Mux p_mux[] =
{
{ "llc", ProtoLlc },
{ 0 }
};
typedef struct Hdr Hdr;
struct Hdr
{
uchar vers;
uchar type;
uchar subtype;
uchar flags;
ushort dur;
uchar aid;
uchar ra[6];
uchar ta[6];
uchar bssid[6];
uchar sa[6];
uchar da[6];
ushort seq;
int proto;
int hdrlen;
};
static int
unpackhdr(uchar *p, uchar *ep, Hdr *h)
{
if(p+2 > ep)
return -1;
h->vers = p[0]&3;
if(h->vers != 0){
h->hdrlen = 2;
return -1;
}
h->type = (p[0]>>2)&3;
h->subtype = (p[0]>>4)&15;
h->flags = p[1];
h->hdrlen = 2;
if(h->vers != 0)
return 0;
switch(h->type){
case Tmgmt:
// fc dur da sa bssid seq
if(p+2+2+6+6+6+2 > ep)
return -1;
h->hdrlen = 24;
h->dur = LittleS(p+2);
memmove(h->da, p+4, 6);
memmove(h->sa, p+10, 6);
memmove(h->bssid, p+16, 6);
h->seq = LittleS(p+22);
break;
case Tctl:
switch(h->subtype){
case CtlPoll:
// fc aid bssid ta
if(p+2+2+6+6 > ep)
return -1;
h->hdrlen = 16;
h->aid = LittleS(p+2);
memmove(h->bssid, p+4, 6);
memmove(h->ta, p+10, 6);
break;
case CtlRts:
// fc dur ra ta
if(p+2+2+6+6 > ep)
return -1;
h->hdrlen = 16;
h->dur = LittleS(p+2);
memmove(h->ra, p+4, 6);
memmove(h->ta, p+10, 6);
break;
case CtlCts:
case CtlAck:
// fc dur ra
if(p+2+2+6 > ep)
return -1;
h->hdrlen = 10;
h->dur = LittleS(p+2);
memmove(h->ra, p+4, 6);
break;
case CtlCfEnd:
case CtlCfEndAck:
// fc dur ra bssid
if(p+2+2+6+6 > ep)
return -1;
h->hdrlen = 16;
h->dur = LittleS(p+2);
memmove(h->ra, p+4, 6);
memmove(h->bssid, p+10, 6);
break;
}
break;
case Tdata:
if(p+24 > ep)
return -1;
h->hdrlen = 24;
h->dur = LittleS(p+2); // ??? maybe
// Also, what is at p+22?
switch(h->flags&(FlagFromds|FlagTods)){
case 0:
memmove(h->da, p+4, 6);
memmove(h->sa, p+10, 6);
memmove(h->bssid, p+16, 6);
break;
case FlagFromds:
memmove(h->da, p+4, 6);
memmove(h->bssid, p+10, 6);
memmove(h->sa, p+16, 6);
break;
case FlagTods:
memmove(h->bssid, p+4, 6);
memmove(h->sa, p+10, 6);
memmove(h->da, p+16, 6);
break;
case FlagFromds|FlagTods:
if(p+30 > ep)
return -1;
h->hdrlen = 30;
memmove(h->ra, p+4, 6);
memmove(h->ta, p+10, 6);
memmove(h->da, p+16, 6);
memmove(h->sa, p+24, 6); // 24 sic
break;
}
p += h->hdrlen;
h->proto = ProtoNone;
if(!(h->flags&FlagWep))
h->proto = ProtoLlc;
break;
}
return 0;
}
enum
{
Os,
Od,
Ot,
Or,
Obssid,
Oa,
Opr,
};
static Field p_fields[] =
{
{ "s", Fether, Os, "source address" },
{ "d", Fether, Od, "destination address" },
{ "t", Fether, Ot, "transmit address" },
{ "r", Fether, Or, "receive address" },
{ "bssid", Fether, Obssid, "bssid address" },
{ "a", Fether, Oa, "any address" },
{ "sd", Fether, Oa, "source|destination address" },
{ 0 }
};
static void
p_compile(Filter *f)
{
Mux *m;
if(f->op == '='){
compile_cmp(p80211.name, f, p_fields);
return;
}
if(strcmp(f->s, "mgmt") == 0){
f->pr = &p80211;
f->ulv = Tmgmt;
f->subop = Ot;
return;
}
if(strcmp(f->s, "ctl") == 0){
f->pr = &p80211;
f->ulv = Tctl;
f->subop = Ot;
return;
}
if(strcmp(f->s, "data") == 0){
f->pr = &p80211;
f->ulv = Tdata;
f->subop = Ot;
return;
}
for(m = p_mux; m->name != nil; m++){
if(strcmp(f->s, m->name) == 0){
f->pr = m->pr;
f->ulv = m->val;
f->subop = Opr;
return;
}
}
sysfatal("unknown 802.11 field or protocol: %s", f->s);
}
static int
p_filter(Filter *f, Msg *m)
{
Hdr h;
memset(&h, 0, sizeof h);
if(unpackhdr(m->ps, m->pe, &h) < 0)
return 0;
m->ps += h.hdrlen;
switch(f->subop){
case Os:
return memcmp(h.sa, f->a, 6) == 0;
case Od:
return memcmp(h.da, f->a, 6) == 0;
case Ot:
return memcmp(h.ta, f->a, 6) == 0;
case Or:
return memcmp(h.ra, f->a, 6) == 0;
case Obssid:
return memcmp(h.bssid, f->a, 6) == 0;
case Oa:
return memcmp(h.sa, f->a, 6) == 0
|| memcmp(h.da, f->a, 6) == 0
|| memcmp(h.ta, f->a, 6) == 0
|| memcmp(h.ra, f->a, 6) == 0
|| memcmp(h.bssid, f->a, 6) == 0;
case Opr:
return h.proto == f->ulv;
}
return 0;
}
static int
p_seprint(Msg *m)
{
Hdr h;
memset(&h, 0, sizeof h);
if(unpackhdr(m->ps, m->pe, &h) < 0)
return -1;
m->pr = &dump;
m->p = seprint(m->p, m->e, "fc=%02x flags=%02x ", m->ps[0], m->ps[1]);
switch(h.type){
case Tmgmt:
m->p = seprint(m->p, m->e, "mgmt dur=%d d=%E s=%E bssid=%E seq=%d",
h.dur, h.da, h.sa, h.bssid, h.seq);
break;
case Tctl:
switch(h.subtype){
case CtlPoll:
m->p = seprint(m->p, m->e, "ctl poll aid=%d bssid=%E t=%E",
h.aid, h.bssid, h.ta);
break;
case CtlRts:
m->p = seprint(m->p, m->e, "ctl rts dur=%d r=%E t=%E",
h.dur, h.ra, h.ta);
break;
case CtlCts:
m->p = seprint(m->p, m->e, "ctl cts dur=%d r=%E",
h.dur, h.ra);
break;
case CtlAck:
m->p = seprint(m->p, m->e, "ctl ack dur=%d r=%E",
h.dur, h.ra);
break;
case CtlCfEnd:
m->p = seprint(m->p, m->e, "ctl cf end dur=%d r=%E bssid=%E",
h.dur, h.ra, h.bssid);
break;
case CtlCfEndAck:
m->p = seprint(m->p, m->e, "ctl cf end ack dur=%d r=%E bssid=%E",
h.dur, h.ra, h.bssid);
break;
default:
m->p = seprint(m->p, m->e, "ctl %.*H", m->ps, h.hdrlen);
break;
}
break;
case Tdata:
switch(h.flags&(FlagFromds|FlagTods)){
case 0:
m->p = seprint(m->p, m->e, "data d=%E s=%E bssid=%E",
h.da, h.sa, h.bssid);
break;
case FlagFromds:
m->p = seprint(m->p, m->e, "data fds d=%E bssid=%E s=%E",
h.da, h.bssid, h.sa);
break;
case FlagTods:
m->p = seprint(m->p, m->e, "data tds bssid=%E s=%E d=%E",
h.bssid, h.sa, h.da);
break;
case FlagFromds|FlagTods:
m->p = seprint(m->p, m->e, "data fds tds r=%E t=%E d=%E s=%E",
h.ra, h.ta, h.da, h.sa);
break;
}
if(!(h.flags&FlagWep))
m->pr = &llc;
break;
}
m->ps += h.hdrlen;
return 0;
}
Proto p80211 =
{
"802.11",
p_compile,
p_filter,
p_seprint,
p_mux,
nil,
nil,
defaultframer
};
|