File: wsp100source.cc

package info (click to toggle)
kismet 2008-05-R1-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 3,232 kB
  • ctags: 3,998
  • sloc: cpp: 33,568; sh: 5,544; ansic: 459; makefile: 457; perl: 62; sql: 41
file content (417 lines) | stat: -rw-r--r-- 12,920 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
407
408
409
410
411
412
413
414
415
416
417
/*
    This file is part of Kismet

    Kismet is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Kismet is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Kismet; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/*
    WSP100 support added by Chris Waters.  chris.waters@networkchemistry.com

    This supports the WSP100 device under Cygwin and Linux

    2/6/2003 - gherlein@herlein.com added TZSP NULL packet generation
    to support late model firmware requirement for this
    heartbeat packet.  Must be sent within every 32 sec and
    must come from the listen port of kismet.

    31-Dec-2004 - nickrusnov@debian.org
    * Fixed all the issues that seemed to have cropped up since somebody last tested this.
      + Several subscript typos in monitor_wsp100. 
      + Misparsing of the device string in OpenSensor (I use the same parsing as in monitor_wsp100 now). 
        (This was probably left over from cut-n-pasting the code from the old version of this interface).
      + Implemented chancontrol_wsp100.
      + Added saving of the parsed/processed device string to the object. This allows easy cut and paste
        of SNMP commands from monitor_wsp100 too.      
    - Should probably use an snmpget to retrieve the current channel rather than saving it on the
      object from the SetChannel call (because Initial channels don't show up, also the user might manually
      enable the WSP's internal channel surfing).
    - Also I note that if Kismet is setuid, this module presents some significant security issuse because
      of its use of ExecCmd using command-line data (to call snmpget/set) -- should probably link with
      libsnmp or something instead.
*/

#include "config.h"
#include "misc.h"

#include "wsp100source.h"

#ifdef HAVE_WSP100

#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdio.h>

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <netdb.h>
#include <fcntl.h>

#include "util.h"

// Straight-C callback
int Wsp100PokeSensor(Timetracker::timer_event *evt, void *call_parm) {
    // Poke it
    ((Wsp100Source *) call_parm)->PokeSensor();

    // And we want to use the event record over again - that is, obey the
    // recurrance field
    return 1;
}

// Build UDP listener code

int Wsp100Source::OpenSource() {
    char listenhost[1024];

    struct hostent *filter_host;

    strncpy(listenhost, wsp100_bits[2].c_str(), 1023);
    listenhost[1023] = '\0';

    port = strtol(wsp100_bits[1].c_str(), NULL, 10);

    if ((filter_host = gethostbyname(listenhost)) == NULL) {
        snprintf(errstr, 1024, "Couldn't resolve host: '%s'", listenhost);
        return -1;
    }

    memcpy((char *) &filter_addr.s_addr, filter_host->h_addr_list[0], 
           filter_host->h_length);

    if ((udp_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
        snprintf(errstr, 1024, "Couldn't create UDP socket: %s", strerror(errno));
        return -1;
    }

    memset(&serv_sockaddr, 0, sizeof(serv_sockaddr));
    serv_sockaddr.sin_family = AF_INET;
    serv_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_sockaddr.sin_port = htons(port);

    if (bind(udp_sock, (struct sockaddr *) &serv_sockaddr, sizeof(serv_sockaddr)) < 0) {
        snprintf(errstr, 1024, "Couldn't bind to UDP socket: %s", strerror(errno));
        return -1;
    }

    paused = 0;

    valid = 1;

    num_packets = 0;

    // Register 'poke' events
    poke_event_id = timetracker->RegisterTimer(TZSP_NULL_PACKET_SLICE, NULL, 1,
                                               &Wsp100PokeSensor, (void *) this);

    return 1;
}


void Wsp100Source::init_bits(string in_dev) {
  wsp100_bits = StrTokenize(in_dev, ":");
}

int Wsp100Source::FetchChannel() {
  return wsp100chan;
}

int Wsp100Source::SetChannel(int chan) {
    char cmdline[2048];
    char *in_err;
    snprintf(cmdline, 2048, "snmpset -C q -v 1 -c public %s .1.3.6.1.4.1.14422.1.3.1 i %d",
             wsp100_bits[2].c_str(), chan);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
	wsp100chan = 0;
        return -1;
    }
    wsp100chan = chan;
    return 0;
}

int Wsp100Source::CloseSource() {
    char cmdline[2048];
    char *in_err;

    if (valid)
        close(udp_sock);

    valid = 0;

    if (timetracker != NULL)
        timetracker->RemoveTimer(poke_event_id);

    snprintf(cmdline, 2038, "snmpset -v 1 -c public %s .1.3.6.1.4.1.14422.1.1.4 i 1",
             wsp100_bits[2].c_str());
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    return 1;
}

int Wsp100Source::FetchPacket(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    if (valid == 0)
        return 0;

    struct sockaddr_in cli_sockaddr;
#ifdef HAVE_SOCKLEN_T
    socklen_t cli_len;
#else
    int cli_len;
#endif
    cli_len = sizeof(cli_sockaddr);
    memset(&cli_sockaddr, 0, sizeof(cli_sockaddr));

    if ((read_len = recvfrom(udp_sock, (char *) data, MAX_PACKET_LEN, 0,
                             (struct sockaddr *) &cli_sockaddr, &cli_len)) < 0) {
        if (errno != EINTR) {
            snprintf(errstr, 1024, "recvfrom() error %d (%s)", errno, strerror(errno));
            return -1;
        }
    }

    // Find out if it came from an IP associated with our target sensor system
    if (cli_sockaddr.sin_addr.s_addr != filter_addr.s_addr)
        return 0;
    
    if (paused || Wsp2Common(packet, data, moddata) == 0) {
        return 0;
    }

    num_packets++;

    snprintf(packet->sourcename, 32, "%s", name.c_str());
    packet->parm = parameters;

    return(packet->caplen);
}

int Wsp100Source::Wsp2Common(kis_packet *packet, uint8_t *data, uint8_t *moddata) {
    memset(packet, 0, sizeof(kis_packet));

    uint16_t datalink_type = 0;
	memcpy(&datalink_type, &(data[2]), 2);
	datalink_type = kis_ntoh16(datalink_type);

    if (datalink_type != KWTAP_ENCAP_IEEE_802_11) {
        return 0;
    }

    // Iterate through the dynamic list of tags
    uint8_t pos = 4;
    while (pos < read_len) {
        uint8_t tag, len = 0, bail = 0;
        tag = data[pos++];

        switch (tag) {
        case WSP100_TAG_PAD:
            len = 0;
            break;
        case WSP100_TAG_END:
            len = 0;
            bail = 1;
            break;
        case WSP100_TAG_RADIO_SIGNAL:
            len = data[pos++];
            packet->signal = data[pos];
            break;
        case WSP100_TAG_RADIO_NOISE:
            len = data[pos++];
            packet->noise = data[pos];
            break;
        case WSP100_TAG_RADIO_RATE:
            // We don't handle the rate yet
            len = data[pos++];
            break;
        case WSP100_TAG_RADIO_TIME:
            len = data[pos++];

            /*
             Either the packet timestamp or my decoding of it is broken, so put our
             own timestamp in

             time_sec = kptoh32(&data[pos]);
             in_header->ts.tv_sec = time_sec;
             */

            gettimeofday(&packet->ts, NULL);

            break;
        case WSP100_TAG_RADIO_MSG:
            // We don't really care about this since we get the packet type from
            // the packet contents later
            len = data[pos++];
            break;
        case WSP100_TAG_RADIO_CF:
            // What does this mean?  Ignore it for now.
            len = data[pos++];
            break;
        case WSP100_TAG_RADIO_UNDECR:
            // We don't really care about this, either
            len = data[pos++];
            break;
        case WSP100_TAG_RADIO_FCSERR:
            len = data[pos++];
            packet->error = data[pos];
            break;
        case WSP100_TAG_RADIO_CHANNEL:
            // We get this off the other data inside the packets so we ignore this...
            len = data[pos++];
            break;
        default:
            // Unknown tag, try to keep going by getting the length and skipping
            len = data[pos++];
            break;
        }

        pos += len;

        if (bail)
            break;
    }

    packet->caplen = read_len - pos;
    packet->len = packet->caplen;

    packet->data = data;
    packet->moddata = moddata;
    packet->modified = 0;

    // We use the GPS coordinates of the capturing server
    if (gpsd != NULL) {
        gpsd->FetchLoc(&packet->gps_lat, &packet->gps_lon, &packet->gps_alt,
                       &packet->gps_spd, &packet->gps_heading, &packet->gps_fix);
    }

    memcpy(packet->data, data + pos, packet->caplen);

    packet->carrier = carrier_80211b;

    return 1;

}

void Wsp100Source::PokeSensor() {
    struct sockaddr_in poke_sockaddr;
    uint8_t null_frame[4] = TZSP_NULL_PACKET;

    memset(&poke_sockaddr, 0, sizeof(poke_sockaddr));
    poke_sockaddr.sin_family = AF_INET;
    poke_sockaddr.sin_addr.s_addr = filter_addr.s_addr;
    poke_sockaddr.sin_port = htons(TZSP_PORT);
    sendto(udp_sock, &null_frame, sizeof(null_frame), 0, (struct sockaddr *) &poke_sockaddr,
           sizeof(poke_sockaddr));
}

KisPacketSource *wsp100source_registrant(string in_name, string in_device,
                                         char *in_err) {
    return new Wsp100Source(in_name, in_device);
}

int monitor_wsp100(const char *in_dev, int initch, char *in_err, void **in_if, void *in_ext) {
    // Split the device
    vector<string> wsp100_bits;
    char cmdline[2048];

    wsp100_bits = StrTokenize(in_dev, ":");

    if (wsp100_bits.size() < 3) {
        snprintf(in_err, STATUS_MAX, "Malformed wsp100 device string '%s', should be "
                 "localip:localport:remoteip", in_dev);
        return -1;
    }

    // sanitize it
    for (unsigned int x = 0; x < wsp100_bits[0].size(); x++) {
        if (!isdigit(wsp100_bits[0][x]) && wsp100_bits[0][x] != '.') {
            snprintf(in_err, STATUS_MAX, "Malformed wsp100 localip '%s', should be "
                     "x.x.x.x", wsp100_bits[0].c_str());
            return -1;
        }
    }
    for (unsigned int x = 0; x < wsp100_bits[1].size(); x++) {
        if (!isdigit(wsp100_bits[1][x])) {
            snprintf(in_err, STATUS_MAX, "Malformed wsp100 localport '%s'",
                     wsp100_bits[1].c_str());
            return -1;
        }
    }
    for (unsigned int x = 0; x < wsp100_bits[2].size(); x++) {
        char bit = wsp100_bits[2][x];
        if (!isalnum(bit) && bit != '.' && bit != '-' && bit != '_') {
            snprintf(in_err, STATUS_MAX, "Malformed wsp100 remoteip '%s', should be "
                     "x.x.x.x or domain name", wsp100_bits[2].c_str());
            return -1;
        }
    }

    // Do system calls to talk snmp.
    // sendor.longhostaddress
    snprintf(cmdline, 2048, "snmpset -v1 -c public %s .1.3.6.1.4.1.14422.1.1.5 a %s",
             wsp100_bits[2].c_str(), wsp100_bits[0].c_str());
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

     // Turn off internal channel-surf.
     snprintf(cmdline, 2048, "snmpset -v 1 -c public %s .1.3.6.1.4.1.14422.1.3.13 i 0",
              wsp100_bits[2].c_str());
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // sensor.channel
    snprintf(cmdline, 2048, "snmpset -v 1 -c public %s .1.3.6.1.4.1.14422.1.3.1 i %d",
             wsp100_bits[2].c_str(), initch);
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // sensor.serverport
    snprintf(cmdline, 2048, "snmpset -v 1 -c public %s .1.3.6.1.4.1.14422.1.4.1 i %s",
             wsp100_bits[2].c_str(), wsp100_bits[1].c_str());
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    // sensor.enable
    snprintf(cmdline, 2038, "snmpset -v 1 -c public %s .1.3.6.1.4.1.14422.1.1.4 i 1",
             wsp100_bits[2].c_str());
    if (RunSysCmd(cmdline) < 0) {
        snprintf(in_err, 1024, "Unable to execute '%s'", cmdline);
        return -1;
    }

    return 0;
}

int chancontrol_wsp100(const char *in_dev, int in_ch, char *in_err, void *in_ext) {
    ((Wsp100Source *) in_ext)->SetChannel(in_ch);
    return 0;
}

// wsp100
#endif