| 1 | /* |
|---|
| 2 | To add: |
|---|
| 3 | - Track what SSIDs clients are asking for (up to 20?) |
|---|
| 4 | - Find IP addresses for clients |
|---|
| 5 | - |
|---|
| 6 | */ |
|---|
| 7 | |
|---|
| 8 | #include <stdio.h> |
|---|
| 9 | #include <signal.h> |
|---|
| 10 | #include <sys/types.h> |
|---|
| 11 | #include <sys/socket.h> |
|---|
| 12 | #include <sys/ioctl.h> |
|---|
| 13 | #include <net/if.h> |
|---|
| 14 | #include <linux/if_packet.h> |
|---|
| 15 | #include <linux/if_ether.h> |
|---|
| 16 | #include <sys/mman.h> |
|---|
| 17 | #include <stdio.h> |
|---|
| 18 | #include <unistd.h> |
|---|
| 19 | #include <signal.h> |
|---|
| 20 | #include <fcntl.h> |
|---|
| 21 | |
|---|
| 22 | #include <sys/types.h> |
|---|
| 23 | #include <sys/file.h> |
|---|
| 24 | #include <sys/ioctl.h> |
|---|
| 25 | #include <sys/socket.h> |
|---|
| 26 | |
|---|
| 27 | #include <stdio.h> |
|---|
| 28 | #include <stdlib.h> |
|---|
| 29 | #include <string.h> |
|---|
| 30 | #include <stdint.h> |
|---|
| 31 | #include <ctype.h> |
|---|
| 32 | #include <getopt.h> |
|---|
| 33 | #include <err.h> |
|---|
| 34 | |
|---|
| 35 | #include <ctype.h> |
|---|
| 36 | #include <string.h> |
|---|
| 37 | #include <stdlib.h> |
|---|
| 38 | #include <stdio.h> |
|---|
| 39 | #include <bcmnvram.h> |
|---|
| 40 | #include <bcmutils.h> |
|---|
| 41 | #include <shutils.h> |
|---|
| 42 | #include <utils.h> |
|---|
| 43 | #include <unistd.h> |
|---|
| 44 | |
|---|
| 45 | #define HOST_TIMEOUT 300 |
|---|
| 46 | |
|---|
| 47 | #include "wl_access.h" |
|---|
| 48 | #include "structs.h" |
|---|
| 49 | #include "channelhopper.h" |
|---|
| 50 | |
|---|
| 51 | #ifdef WIVIZ_GPS |
|---|
| 52 | #include "wiviz_gps.h" |
|---|
| 53 | #endif |
|---|
| 54 | |
|---|
| 55 | #ifndef __cplusplus |
|---|
| 56 | #define __cdecl |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #define nonzeromac(x) memcmp(x, "\0\0\0\0\0\0", 6) |
|---|
| 60 | |
|---|
| 61 | int openMonitorSocket(char * dev); |
|---|
| 62 | void dealWithPacket(wiviz_cfg * cfg, int len, const u_char * packet); |
|---|
| 63 | wiviz_host * gotHost(wiviz_cfg * cfg, u_char * mac, host_type type); |
|---|
| 64 | void print_host(FILE * outf, wiviz_host * host); |
|---|
| 65 | void __cdecl signal_handler(int); |
|---|
| 66 | void readWL(wiviz_cfg * cfg); |
|---|
| 67 | void reloadConfig(); |
|---|
| 68 | |
|---|
| 69 | wiviz_cfg * global_cfg; |
|---|
| 70 | char *wl_dev; |
|---|
| 71 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 72 | int main(int argc, char * * argv) { |
|---|
| 73 | char *dev; |
|---|
| 74 | int stop = 0; |
|---|
| 75 | int oldMonitor, newMonitor; |
|---|
| 76 | u_char packet[4096]; |
|---|
| 77 | int pktlen; |
|---|
| 78 | wiviz_cfg cfg; |
|---|
| 79 | int i; |
|---|
| 80 | int defaultHopSeq[] = { 1, 3, 6, 8, 11 }; |
|---|
| 81 | int s, one; |
|---|
| 82 | wl_dev=get_wdev(); |
|---|
| 83 | global_cfg = &cfg; |
|---|
| 84 | signal(SIGUSR1, &signal_handler); |
|---|
| 85 | signal(SIGUSR2, &signal_handler); |
|---|
| 86 | |
|---|
| 87 | printf( "Wi-Viz 2 infogathering daemon by Nathan True\n"); |
|---|
| 88 | printf( "http://wiviz.natetrue.com\n"); |
|---|
| 89 | |
|---|
| 90 | memset(&cfg, 0, sizeof(wiviz_cfg)); |
|---|
| 91 | cfg.numHosts = 0; |
|---|
| 92 | cfg.lastKeepAlive = time(NULL); |
|---|
| 93 | cfg.channelHopping = 0; |
|---|
| 94 | cfg.channelDwellTime = 1000; |
|---|
| 95 | cfg.channelHopSeqLen = 5; |
|---|
| 96 | memcpy(cfg.channelHopSeq, defaultHopSeq, sizeof(defaultHopSeq)); |
|---|
| 97 | |
|---|
| 98 | #ifndef HAVE_MADWIFI |
|---|
| 99 | wl_ioctl(wl_dev, WLC_GET_MAGIC, &i, 4); |
|---|
| 100 | if (i != WLC_IOCTL_MAGIC) { |
|---|
| 101 | printf( "Wireless magic not correct, not querying wl for info %X!=%X\n",i,WLC_IOCTL_MAGIC); |
|---|
| 102 | cfg.readFromWl = 0; |
|---|
| 103 | } |
|---|
| 104 | else { |
|---|
| 105 | cfg.readFromWl = 1; |
|---|
| 106 | wl_ioctl(wl_dev, WLC_GET_MONITOR, &oldMonitor, 4); |
|---|
| 107 | newMonitor = 1; |
|---|
| 108 | wl_ioctl(wl_dev, WLC_SET_MONITOR, &newMonitor, 4); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | #else |
|---|
| 112 | sysprintf("wlanconfig %s create wlandev %s wlanmode monitor",get_monitor(),getWifi(get_wdev())); |
|---|
| 113 | sysprintf("ifconfig %s up",get_monitor()); |
|---|
| 114 | cfg.readFromWl = 1; |
|---|
| 115 | #endif |
|---|
| 116 | reloadConfig(); |
|---|
| 117 | |
|---|
| 118 | #ifdef HAVE_MADWIFI |
|---|
| 119 | s = openMonitorSocket(get_monitor()); // for testing we use ath0 |
|---|
| 120 | #else |
|---|
| 121 | s = openMonitorSocket("prism0"); |
|---|
| 122 | #endif |
|---|
| 123 | if (s == -1) return; |
|---|
| 124 | one = 1; |
|---|
| 125 | ioctl(s, FIONBIO, (char *)&one); |
|---|
| 126 | |
|---|
| 127 | if (cfg.readFromWl) { |
|---|
| 128 | readWL(&cfg); |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | #ifdef WIVIZ_GPS |
|---|
| 132 | gps_init(&cfg); |
|---|
| 133 | #endif |
|---|
| 134 | |
|---|
| 135 | while (!stop) { |
|---|
| 136 | #ifdef WIVIZ_GPS |
|---|
| 137 | gps_tick(); |
|---|
| 138 | #else |
|---|
| 139 | if (time(NULL) - cfg.lastKeepAlive > 30) stop = 1; |
|---|
| 140 | #endif |
|---|
| 141 | pktlen = recv(s, packet, 4096, 0); |
|---|
| 142 | if (pktlen <= 0) continue; |
|---|
| 143 | dealWithPacket(&cfg, pktlen, packet); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | signal_handler(SIGUSR1); |
|---|
| 147 | |
|---|
| 148 | if (cfg.channelHopperPID) kill(cfg.channelHopperPID, SIGKILL); |
|---|
| 149 | |
|---|
| 150 | #ifndef WIVIZ_GPS |
|---|
| 151 | for (i = 0; i < MAX_HOSTS; i++) { |
|---|
| 152 | print_host(stderr, cfg.hosts + i); |
|---|
| 153 | if (cfg.hosts[i].occupied) printf("\n"); |
|---|
| 154 | if (cfg.hosts[i].apInfo) free(cfg.hosts[i].apInfo); |
|---|
| 155 | if (cfg.hosts[i].staInfo) free(cfg.hosts[i].staInfo); |
|---|
| 156 | } |
|---|
| 157 | #endif |
|---|
| 158 | #ifndef HAVE_MADWIFI |
|---|
| 159 | wl_ioctl(wl_dev, WLC_SET_MONITOR, &oldMonitor, 4); |
|---|
| 160 | #else |
|---|
| 161 | // return to original channel |
|---|
| 162 | sysprintf("iwconfig %s channel %sM",get_monitor(),nvram_nget("%s_channel",get_wdev())); |
|---|
| 163 | sleep(1); |
|---|
| 164 | sysprintf("ifconfig %s down",get_monitor()); |
|---|
| 165 | sysprintf("wlanconfig %s destroy",get_monitor()); |
|---|
| 166 | #endif |
|---|
| 167 | close(s); |
|---|
| 168 | return 0; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 172 | int openMonitorSocket(char * dev) { |
|---|
| 173 | //Open the socket |
|---|
| 174 | struct ifreq ifr; |
|---|
| 175 | struct sockaddr_ll addr; |
|---|
| 176 | int s; |
|---|
| 177 | |
|---|
| 178 | s=socket(PF_PACKET, SOCK_RAW,0); |
|---|
| 179 | memset(&ifr,0,sizeof(ifr)); |
|---|
| 180 | strcpy(ifr.ifr_name, dev); |
|---|
| 181 | if(ioctl(s, SIOCGIFINDEX, &ifr) !=0) { |
|---|
| 182 | printf( "ioctl IFINDEX failed!!!\n"); |
|---|
| 183 | return -1; |
|---|
| 184 | } |
|---|
| 185 | close(s); |
|---|
| 186 | |
|---|
| 187 | s= socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); |
|---|
| 188 | memset(&addr, 0, sizeof(addr)); |
|---|
| 189 | addr.sll_family=AF_PACKET; |
|---|
| 190 | addr.sll_ifindex=ifr.ifr_ifindex; |
|---|
| 191 | addr.sll_protocol=0; |
|---|
| 192 | if (bind(s, (struct sockaddr *)&addr, sizeof(addr))<0) { |
|---|
| 193 | printf( "bind failed!!! (%s)\n", dev); |
|---|
| 194 | return -1; |
|---|
| 195 | } |
|---|
| 196 | |
|---|
| 197 | return s; |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 201 | void writeJavascript() { |
|---|
| 202 | int i; |
|---|
| 203 | FILE * outf; |
|---|
| 204 | wiviz_host * h; |
|---|
| 205 | |
|---|
| 206 | outf = fopen("/tmp/wiviz2-dump", "w"); |
|---|
| 207 | if (!outf) { |
|---|
| 208 | printf( "Failure to open output file\n"); |
|---|
| 209 | return; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | global_cfg->lastKeepAlive = time(NULL); |
|---|
| 213 | |
|---|
| 214 | if(global_cfg->readFromWl) readWL(global_cfg); |
|---|
| 215 | |
|---|
| 216 | fprintf(outf, "top.hosts = new Array();\nvar hnum = 0;\nvar h;\n"); |
|---|
| 217 | for (i = 0; i < MAX_HOSTS; i++) { |
|---|
| 218 | h = global_cfg->hosts + i; |
|---|
| 219 | if (h->occupied == 0) continue; |
|---|
| 220 | if (time(NULL) - h->lastSeen > HOST_TIMEOUT) { |
|---|
| 221 | h->occupied = 0; |
|---|
| 222 | } |
|---|
| 223 | fprintf(outf, "h = new Object();\n"); |
|---|
| 224 | print_host(outf, h); |
|---|
| 225 | fprintf(outf, "top.hosts[hnum] = h; hnum++;\n"); |
|---|
| 226 | } |
|---|
| 227 | fprintf(outf, "\nvar wiviz_cfg = new Object();\n wiviz_cfg.channel = "); |
|---|
| 228 | if (global_cfg->channelHopping) { |
|---|
| 229 | fprintf(outf, "'hopping'"); |
|---|
| 230 | } |
|---|
| 231 | else { |
|---|
| 232 | fprintf(outf, "%i", global_cfg->curChannel); |
|---|
| 233 | } |
|---|
| 234 | fprintf(outf, ";\ntop.wiviz_callback(top.hosts, wiviz_cfg);\n"); |
|---|
| 235 | fprintf(outf, "function wiviz_callback(one, two) {\n"); |
|---|
| 236 | fprintf(outf, "alert('This asp is intended to run inside Wi-Viz. You will now be redirected there.');\n"); |
|---|
| 237 | fprintf(outf, "location.replace('Wiviz_Survey.asp');\n"); |
|---|
| 238 | fprintf(outf, "}"); |
|---|
| 239 | fclose(outf); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 243 | void reloadConfig() { |
|---|
| 244 | FILE * cnf; |
|---|
| 245 | wiviz_cfg * cfg = global_cfg; |
|---|
| 246 | char filebuffer[512]; |
|---|
| 247 | char * fbptr, * p, * v, * vv; |
|---|
| 248 | int fblen, val; |
|---|
| 249 | int hopCfgChanged = 0; |
|---|
| 250 | int newHopSeq[12]; |
|---|
| 251 | int newHopSeqLen = 0; |
|---|
| 252 | |
|---|
| 253 | printf( "Loading config file\n"); |
|---|
| 254 | |
|---|
| 255 | cnf = fopen("/tmp/wiviz2-cfg", "r"); |
|---|
| 256 | if (!cnf) { |
|---|
| 257 | printf( "Wiviz: No config file (/tmp/wiviz2-cfg) present, using defaults\n"); |
|---|
| 258 | return; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | fblen = fread(filebuffer, 1, 512, cnf); |
|---|
| 262 | fclose(cnf); |
|---|
| 263 | if (fblen >= 512) { |
|---|
| 264 | printf( "Error reading config file\n"); |
|---|
| 265 | return; |
|---|
| 266 | } |
|---|
| 267 | filebuffer[fblen] = 0; |
|---|
| 268 | printf( "Read %i bytes from config file\n", fblen); |
|---|
| 269 | |
|---|
| 270 | fbptr = filebuffer; |
|---|
| 271 | |
|---|
| 272 | while (fbptr < filebuffer + fblen && *fbptr != 0) { |
|---|
| 273 | p = fbptr; |
|---|
| 274 | //Find end of parameter |
|---|
| 275 | for (; *fbptr != '=' && *fbptr != 0; fbptr++); |
|---|
| 276 | *fbptr = 0; |
|---|
| 277 | v = ++fbptr; |
|---|
| 278 | //Find end of value |
|---|
| 279 | for (; *fbptr != '&' && *fbptr != 0; fbptr++); |
|---|
| 280 | *(fbptr++) = 0; |
|---|
| 281 | printf( "Config: %s=%s\n", p, v); |
|---|
| 282 | //Apply configuration |
|---|
| 283 | if (!strcmp(p, "channelsel")) { |
|---|
| 284 | //Channel selector |
|---|
| 285 | cfg->channelHopping = 0; |
|---|
| 286 | if (!strcmp(v, "hop")) { |
|---|
| 287 | //Set channel hopping |
|---|
| 288 | cfg->channelHopping = 1; |
|---|
| 289 | hopCfgChanged = 1; |
|---|
| 290 | } |
|---|
| 291 | else if (!strcmp(v, "nochange")) { |
|---|
| 292 | //Don't change anything, read channel from wireless card |
|---|
| 293 | readWL(cfg); |
|---|
| 294 | } |
|---|
| 295 | else { |
|---|
| 296 | val = atoi(v); |
|---|
| 297 | if (val < 0 || val > 254) { |
|---|
| 298 | printf( "Channel setting in config file invalid (%i)\n", cfg->curChannel); |
|---|
| 299 | } |
|---|
| 300 | else { |
|---|
| 301 | cfg->curChannel = val; |
|---|
| 302 | if (cfg->readFromWl) { |
|---|
| 303 | #ifdef HAVE_MADWIFI |
|---|
| 304 | set_channel(wl_dev,cfg->curChannel); |
|---|
| 305 | // sysprintf("iwconfig %s channel %d\n",wl_dev,cfg->curChannel); |
|---|
| 306 | #else |
|---|
| 307 | if (wl_ioctl(wl_dev, WLC_SET_CHANNEL, &cfg->curChannel, 4) < 0) { |
|---|
| 308 | printf( "Channel set to %i failed\n", cfg->curChannel); |
|---|
| 309 | } |
|---|
| 310 | #endif |
|---|
| 311 | } |
|---|
| 312 | else { |
|---|
| 313 | printf( "Can't set channel, no Broadcom wireless device present\n"); |
|---|
| 314 | } |
|---|
| 315 | } |
|---|
| 316 | } |
|---|
| 317 | } |
|---|
| 318 | if (!strcmp(p, "hopdwell")) { |
|---|
| 319 | val = atoi(v); |
|---|
| 320 | if (val < 100) val = 100; |
|---|
| 321 | if (val > 30000) val = 30000; |
|---|
| 322 | if (cfg->channelDwellTime != val) hopCfgChanged = 1; |
|---|
| 323 | cfg->channelDwellTime = val; |
|---|
| 324 | } |
|---|
| 325 | if (!strcmp(p, "hopseq")) { |
|---|
| 326 | cfg->channelHopSeqLen = 0; |
|---|
| 327 | while (v < fbptr) { |
|---|
| 328 | for (vv = v; *vv != ',' && *vv != 0; vv++); |
|---|
| 329 | if (*vv == 0) { |
|---|
| 330 | cfg->channelHopSeq[cfg->channelHopSeqLen++] = atoi(v); |
|---|
| 331 | break; |
|---|
| 332 | } |
|---|
| 333 | *vv = 0; |
|---|
| 334 | cfg->channelHopSeq[cfg->channelHopSeqLen++] = atoi(v); |
|---|
| 335 | v = vv + 1; |
|---|
| 336 | } |
|---|
| 337 | } |
|---|
| 338 | /* |
|---|
| 339 | if (!strcmp(p, "")) { |
|---|
| 340 | } |
|---|
| 341 | */ |
|---|
| 342 | } |
|---|
| 343 | //Apply channel hopper settings |
|---|
| 344 | if (cfg->channelHopping == 0 && cfg->channelHopperPID) { |
|---|
| 345 | kill(cfg->channelHopperPID, SIGKILL); |
|---|
| 346 | cfg->channelHopperPID = 0; |
|---|
| 347 | } |
|---|
| 348 | if (cfg->channelHopping == 1 && hopCfgChanged) { |
|---|
| 349 | if (cfg->channelHopperPID) kill(cfg->channelHopperPID, SIGKILL); |
|---|
| 350 | if ((cfg->channelHopperPID = fork()) == 0) { |
|---|
| 351 | channelHopper(cfg); |
|---|
| 352 | } |
|---|
| 353 | } |
|---|
| 354 | } |
|---|
| 355 | |
|---|
| 356 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 357 | void __cdecl signal_handler(int signum) { |
|---|
| 358 | if (signum == SIGUSR1) writeJavascript(); |
|---|
| 359 | if (signum == SIGUSR2) reloadConfig(); |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 363 | void dealWithPacket(wiviz_cfg * cfg, int pktlen, const u_char * packet) { |
|---|
| 364 | ieee802_11_hdr * hWifi; |
|---|
| 365 | wiviz_host * host; |
|---|
| 366 | wiviz_host * emergebss; |
|---|
| 367 | host_type type = typeUnknown; |
|---|
| 368 | int wfType; |
|---|
| 369 | int rssi = 0; |
|---|
| 370 | int to_ds, from_ds; |
|---|
| 371 | ieee_802_11_tag * e; |
|---|
| 372 | ieee_802_11_mgt_frame * m; |
|---|
| 373 | char * src = "\0\0\0\0\0\0"; |
|---|
| 374 | char * dst = "\0\0\0\0\0\0"; |
|---|
| 375 | char * bss = "\0\0\0\0\0\0"; |
|---|
| 376 | char * ssid = ""; |
|---|
| 377 | int channel = 0; |
|---|
| 378 | int adhocbeacon = 0; |
|---|
| 379 | u_char ssidlen = 0; |
|---|
| 380 | ap_enc_type encType = aetUnknown; |
|---|
| 381 | if (!packet) return; |
|---|
| 382 | |
|---|
| 383 | #ifdef HAVE_MADWIFI |
|---|
| 384 | int noise; |
|---|
| 385 | if (packet[0]>0) |
|---|
| 386 | { |
|---|
| 387 | printf( "Wrong radiotap header version.\n" ); |
|---|
| 388 | return; |
|---|
| 389 | } |
|---|
| 390 | int number = packet[2] | (unsigned int)((unsigned int)packet[3]<<8); |
|---|
| 391 | if (number<=0 || number>=pktlen) |
|---|
| 392 | { |
|---|
| 393 | printf("something wrong %d\n",number); |
|---|
| 394 | return; |
|---|
| 395 | } |
|---|
| 396 | noise = packet[number-3]; |
|---|
| 397 | rssi = -(100-(packet[number-4]-noise)); |
|---|
| 398 | hWifi = (ieee802_11_hdr *) (packet + (number)); |
|---|
| 399 | #else |
|---|
| 400 | prism_hdr * hPrism; |
|---|
| 401 | prism_did * i; |
|---|
| 402 | if (pktlen < sizeof(prism_hdr) + sizeof(ieee802_11_hdr)) return; |
|---|
| 403 | |
|---|
| 404 | hPrism = (prism_hdr *) packet; |
|---|
| 405 | hWifi = (ieee802_11_hdr *) (packet + (hPrism->msg_length)); |
|---|
| 406 | |
|---|
| 407 | //Parse the prism DIDs |
|---|
| 408 | i = (prism_did *)((char *)hPrism + sizeof(prism_hdr)); |
|---|
| 409 | while ((int)i < (int)hWifi) { |
|---|
| 410 | if (i->did == pdn_rssi) rssi = *(int *)(i+1); |
|---|
| 411 | i = (prism_did *) ((int)(i+1) + i->length); |
|---|
| 412 | } |
|---|
| 413 | #endif |
|---|
| 414 | |
|---|
| 415 | //Establish the frame type |
|---|
| 416 | wfType = ((hWifi->frame_control & 0xF0) >> 4) + ((hWifi->frame_control & 0xC) << 2); |
|---|
| 417 | |
|---|
| 418 | switch (wfType) { |
|---|
| 419 | case mgt_assocRequest: |
|---|
| 420 | case mgt_reassocRequest: |
|---|
| 421 | case mgt_probeRequest: |
|---|
| 422 | type = typeSta; |
|---|
| 423 | src=hWifi->addr2; |
|---|
| 424 | dst=hWifi->addr1; |
|---|
| 425 | break; |
|---|
| 426 | case mgt_assocResponse: |
|---|
| 427 | case mgt_reassocResponse: |
|---|
| 428 | case mgt_probeResponse: |
|---|
| 429 | case mgt_beacon: |
|---|
| 430 | src=hWifi->addr2; |
|---|
| 431 | dst=hWifi->addr1; |
|---|
| 432 | bss=hWifi->addr3; |
|---|
| 433 | type = typeAP; |
|---|
| 434 | break; |
|---|
| 435 | } |
|---|
| 436 | to_ds = hWifi->flags & IEEE80211_TO_DS; |
|---|
| 437 | from_ds = hWifi->flags & IEEE80211_FROM_DS; |
|---|
| 438 | if ((wfType & 0xF0) == 0x20 && (wfType & 0xF) < 4) { |
|---|
| 439 | //Data frame |
|---|
| 440 | src=hWifi->addr2; |
|---|
| 441 | dst=hWifi->addr1; |
|---|
| 442 | if (!from_ds) type = typeSta; |
|---|
| 443 | else type = typeAP; |
|---|
| 444 | if (!to_ds && !from_ds) bss = hWifi->addr3; |
|---|
| 445 | if (to_ds && !from_ds) bss = hWifi->addr1; |
|---|
| 446 | if (!to_ds && from_ds) bss = hWifi->addr2; |
|---|
| 447 | } |
|---|
| 448 | if (type == typeUnknown) return; |
|---|
| 449 | |
|---|
| 450 | //Parse the 802.11 tags |
|---|
| 451 | if (wfType == mgt_probeResponse || wfType == mgt_beacon || wfType == mgt_probeRequest) { |
|---|
| 452 | m = (ieee_802_11_mgt_frame *) (hWifi + 1); |
|---|
| 453 | if (m->caps & MGT_CAPS_IBSS) { |
|---|
| 454 | type = typeSta; |
|---|
| 455 | adhocbeacon = 1; |
|---|
| 456 | } |
|---|
| 457 | if (m->caps & MGT_CAPS_WEP) encType = aetEncWEP; |
|---|
| 458 | else encType = aetUnencrypted; |
|---|
| 459 | e = (ieee_802_11_tag *) ((int) m + sizeof(ieee_802_11_mgt_frame)); |
|---|
| 460 | while ((u_int)e < (u_int)packet + pktlen) { |
|---|
| 461 | if (e->tag == tagSSID) { |
|---|
| 462 | ssidlen = e->length; |
|---|
| 463 | ssid = (char *)(e + 1); |
|---|
| 464 | } |
|---|
| 465 | if (e->tag == tagChannel) { |
|---|
| 466 | channel = *(char *)(e + 1); |
|---|
| 467 | } |
|---|
| 468 | if (e->tag == tagVendorSpecific) { |
|---|
| 469 | if (e->length >= 4 && memcmp(e + 1, "\x00\x50\xf2\x01", 4) == 0) { |
|---|
| 470 | //WPA encryption |
|---|
| 471 | if (encType != aetEncWPAmix) |
|---|
| 472 | { |
|---|
| 473 | if (encType==aetEncWPA2) |
|---|
| 474 | encType = aetEncWPAmix; |
|---|
| 475 | else |
|---|
| 476 | encType = aetEncWPA; |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | if (e->length >= 4 && memcmp(e + 1, "\x00\x0f\xac\x01", 4) == 0) { |
|---|
| 480 | //WPA2 encryption |
|---|
| 481 | if (encType != aetEncWPAmix) |
|---|
| 482 | { |
|---|
| 483 | if (encType==aetEncWPA) |
|---|
| 484 | encType = aetEncWPAmix; |
|---|
| 485 | else |
|---|
| 486 | encType = aetEncWPA2; |
|---|
| 487 | } |
|---|
| 488 | } |
|---|
| 489 | } |
|---|
| 490 | e = (ieee_802_11_tag *) ((int)(e + 1) + e->length); |
|---|
| 491 | } |
|---|
| 492 | } |
|---|
| 493 | |
|---|
| 494 | //Look up the host in the hash table |
|---|
| 495 | host = gotHost(cfg, src, type); |
|---|
| 496 | |
|---|
| 497 | //Add any info we received |
|---|
| 498 | if (host->RSSI) { |
|---|
| 499 | host->RSSI = host->RSSI * 9 / 10 + (-rssi * 10); |
|---|
| 500 | } |
|---|
| 501 | else { |
|---|
| 502 | host->RSSI = -rssi * 100; |
|---|
| 503 | } |
|---|
| 504 | if (type == typeSta) { |
|---|
| 505 | if (nonzeromac(bss)) { |
|---|
| 506 | memcpy(host->staInfo->connectedBSSID, bss, 6); |
|---|
| 507 | host->staInfo->state = ssAssociated; |
|---|
| 508 | emergebss = gotHost(cfg, bss, typeAP); |
|---|
| 509 | if (emergebss->RSSI == 0) emergebss->RSSI = 10000; |
|---|
| 510 | memcpy(emergebss->apInfo->bssid, bss, 6); |
|---|
| 511 | if (adhocbeacon) { |
|---|
| 512 | emergebss->type = typeAdhocHub; |
|---|
| 513 | if (ssidlen > 0 && ssidlen <= 32) { |
|---|
| 514 | memcpy(emergebss->apInfo->ssid, ssid, ssidlen); |
|---|
| 515 | emergebss->apInfo->ssidlen = ssidlen; |
|---|
| 516 | } |
|---|
| 517 | if (channel) emergebss->apInfo->channel = channel; |
|---|
| 518 | emergebss->apInfo->flags = hWifi->flags; |
|---|
| 519 | emergebss->RSSI = host->RSSI; |
|---|
| 520 | if (encType != aetUnknown) emergebss->apInfo->encryption = encType; |
|---|
| 521 | } |
|---|
| 522 | } |
|---|
| 523 | if (wfType == mgt_probeRequest && host->staInfo->state == ssUnknown) |
|---|
| 524 | host->staInfo->state = ssUnassociated; |
|---|
| 525 | if (wfType == mgt_probeRequest && ssidlen > 0 && ssidlen <= 32) { |
|---|
| 526 | memcpy(host->staInfo->lastssid, ssid, ssidlen); |
|---|
| 527 | host->staInfo->lastssid[ssidlen] = 0; |
|---|
| 528 | host->staInfo->lastssidlen = ssidlen; |
|---|
| 529 | } |
|---|
| 530 | } |
|---|
| 531 | if (type == typeAP) { |
|---|
| 532 | if (nonzeromac(bss)) { |
|---|
| 533 | memcpy(host->apInfo->bssid, bss, 6); |
|---|
| 534 | } |
|---|
| 535 | if (ssidlen > 0 && ssidlen <= 32) { |
|---|
| 536 | memcpy(host->apInfo->ssid, ssid, ssidlen); |
|---|
| 537 | host->apInfo->ssid[ssidlen] = 0; |
|---|
| 538 | host->apInfo->ssidlen = ssidlen; |
|---|
| 539 | } |
|---|
| 540 | if (channel) host->apInfo->channel = channel; |
|---|
| 541 | host->apInfo->flags = hWifi->flags; |
|---|
| 542 | if (encType != aetUnknown) host->apInfo->encryption = encType; |
|---|
| 543 | } |
|---|
| 544 | } |
|---|
| 545 | |
|---|
| 546 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 547 | void print_mac(u_char * mac, char * extra) { |
|---|
| 548 | fprint_mac(stdout, mac, extra); |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 552 | void fprint_mac(FILE * outf, u_char * mac, char * extra) { |
|---|
| 553 | fprintf(outf, "%02X:%02X:%02X:%02X:%02X:%02X%s", |
|---|
| 554 | mac[0] & 0xFF, |
|---|
| 555 | mac[1] & 0xFF, |
|---|
| 556 | mac[2] & 0xFF, |
|---|
| 557 | mac[3] & 0xFF, |
|---|
| 558 | mac[4] & 0xFF, |
|---|
| 559 | mac[5] & 0xFF, |
|---|
| 560 | extra); |
|---|
| 561 | } |
|---|
| 562 | |
|---|
| 563 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 564 | #define MAX_PROBES MAX_HOSTS/2 |
|---|
| 565 | wiviz_host * gotHost(wiviz_cfg * cfg, u_char * mac, host_type type) { |
|---|
| 566 | int i = (mac[5] + (mac[4] << 8)) % MAX_HOSTS; |
|---|
| 567 | int c = 0; |
|---|
| 568 | wiviz_host * h = cfg->hosts + i; |
|---|
| 569 | while (h->occupied && memcmp(h->mac, mac, 6)) { |
|---|
| 570 | i++; h++; c++; |
|---|
| 571 | if (i >= MAX_HOSTS) { |
|---|
| 572 | i = 0; |
|---|
| 573 | h = cfg->hosts; |
|---|
| 574 | } |
|---|
| 575 | if (c > MAX_PROBES) break; |
|---|
| 576 | } |
|---|
| 577 | if (!h->occupied) { |
|---|
| 578 | printf( "New host, "); |
|---|
| 579 | #ifdef NEED_PRINTF |
|---|
| 580 | fprint_mac(stdout, mac, ", type="); |
|---|
| 581 | #endif |
|---|
| 582 | printf( "%s\n", (type==typeAP) ? "AP" : ((type==typeSta) ? "Sta" : "Unk")); |
|---|
| 583 | } |
|---|
| 584 | h->occupied = 1; |
|---|
| 585 | h->lastSeen = time(NULL); |
|---|
| 586 | h->type = type; |
|---|
| 587 | memcpy(h->mac, mac, 6); |
|---|
| 588 | if (h->type == typeAP && !h->apInfo) { |
|---|
| 589 | h->apInfo = (ap_info *) malloc(sizeof(ap_info)); |
|---|
| 590 | memset(h->apInfo, 0, sizeof(ap_info)); |
|---|
| 591 | } |
|---|
| 592 | if (h->type == typeSta && !h->staInfo) { |
|---|
| 593 | h->staInfo = (sta_info *) malloc(sizeof(sta_info)); |
|---|
| 594 | memset(h->staInfo, 0, sizeof(sta_info)); |
|---|
| 595 | } |
|---|
| 596 | return h; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 600 | void print_host(FILE * outf, wiviz_host * host) { |
|---|
| 601 | int i; |
|---|
| 602 | |
|---|
| 603 | if (!host->occupied) return; |
|---|
| 604 | fprintf(outf, "h.mac = '"); |
|---|
| 605 | fprint_mac(outf, host->mac, "';\n"); |
|---|
| 606 | fprintf(outf, "h.rssi = -%i;\nh.type = '", host->RSSI / 100); |
|---|
| 607 | switch (host->type) { |
|---|
| 608 | case typeAP: fprintf(outf, "ap"); break; |
|---|
| 609 | case typeSta: fprintf(outf, "sta"); break; |
|---|
| 610 | case typeAdhocHub: fprintf(outf, "adhoc"); break; |
|---|
| 611 | } |
|---|
| 612 | fprintf(outf, "';\nh.self = "); |
|---|
| 613 | fprintf(outf, host->isSelf ? "true;\n" : "false;\n"); |
|---|
| 614 | if (host->type == typeSta) { |
|---|
| 615 | switch(host->staInfo->state) { |
|---|
| 616 | case ssAssociated: |
|---|
| 617 | fprintf(outf, "h.sta_state='assoc';\nh.sta_bssid='"); |
|---|
| 618 | fprint_mac(outf, host->staInfo->connectedBSSID, "';\n"); |
|---|
| 619 | break; |
|---|
| 620 | case ssUnassociated: |
|---|
| 621 | fprintf(outf, "h.sta_state='unassoc';\n"); |
|---|
| 622 | } |
|---|
| 623 | fprintf(outf, "h.sta_lastssid = '"); |
|---|
| 624 | for (i = 0; i < host->staInfo->lastssidlen; i++) { |
|---|
| 625 | fprintf(outf, "&#%04i;", *((char *)host->staInfo->lastssid + i) & 0xFF); |
|---|
| 626 | } |
|---|
| 627 | fprintf(outf, "';\n"); |
|---|
| 628 | } |
|---|
| 629 | if (host->type == typeAP || host->type == typeAdhocHub) { |
|---|
| 630 | fprintf(outf, "h.channel = %i;\nh.ssid = '", host->apInfo->channel & 0xFF); |
|---|
| 631 | for (i = 0; i < host->apInfo->ssidlen; i++) { |
|---|
| 632 | fprintf(outf, "&#%04i;", *((char *)host->apInfo->ssid + i) & 0xFF); |
|---|
| 633 | } |
|---|
| 634 | fprintf(outf, "';\nh.encrypted = "); |
|---|
| 635 | switch (host->apInfo->encryption) { |
|---|
| 636 | case aetUnknown: fprintf(outf, "'unknown';\n"); break; |
|---|
| 637 | case aetUnencrypted: fprintf(outf, "'no';\n"); break; |
|---|
| 638 | case aetEncUnknown: fprintf(outf, "'yes'; h.enctype = 'unknown';\n"); break; |
|---|
| 639 | case aetEncWEP: fprintf(outf, "'yes'; h.enctype = 'wep';\n"); break; |
|---|
| 640 | case aetEncWPA: fprintf(outf, "'yes'; h.enctype = 'wpa';\n"); break; |
|---|
| 641 | case aetEncWPA2: fprintf(outf, "'yes'; h.enctype = 'wpa2';\n"); break; |
|---|
| 642 | case aetEncWPAmix: fprintf(outf, "'yes'; h.enctype = 'wpa wpa2';\n"); break; |
|---|
| 643 | } |
|---|
| 644 | } |
|---|
| 645 | fprintf(outf, "h.age = %i;\n", time(0) - host->lastSeen); |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 649 | #define MAX_STA_COUNT 64 |
|---|
| 650 | void readWL(wiviz_cfg * cfg) { |
|---|
| 651 | int ap, i; |
|---|
| 652 | wiviz_host * host, * sta; |
|---|
| 653 | uchar mac[6]; |
|---|
| 654 | wlc_ssid_t ssid; |
|---|
| 655 | channel_info_t channel; |
|---|
| 656 | maclist_t * macs; |
|---|
| 657 | sta_rssi_t starssi; |
|---|
| 658 | char buf[32]; |
|---|
| 659 | |
|---|
| 660 | get_mac(wl_dev, mac); |
|---|
| 661 | printf( "AP mac: "); |
|---|
| 662 | #ifdef NEED_PRINTF |
|---|
| 663 | print_mac(mac, "\n"); |
|---|
| 664 | #endif |
|---|
| 665 | if (!nonzeromac(mac)) return; |
|---|
| 666 | if (nvram_nmatch("ap","%s_mode",wl_dev)) |
|---|
| 667 | ap=1; |
|---|
| 668 | if (nvram_nmatch("wdsap","%s_mode",wl_dev)) |
|---|
| 669 | ap=1; |
|---|
| 670 | // wl_ioctl(wl_dev, WLC_GET_AP, &ap, 4); |
|---|
| 671 | if (ap) { |
|---|
| 672 | host = gotHost(cfg, mac, typeAP); |
|---|
| 673 | host->isSelf = 1; |
|---|
| 674 | #ifdef HAVE_MADWIFI |
|---|
| 675 | strcpy(host->apInfo->ssid,nvram_nget("%s_ssid",wl_dev)); |
|---|
| 676 | host->apInfo->ssidlen = strlen(host->apInfo->ssid); |
|---|
| 677 | ether_atoe (nvram_nget("%s_hwaddr",wl_dev),buf); |
|---|
| 678 | memcpy(host->apInfo->bssid,buf,6); |
|---|
| 679 | #else |
|---|
| 680 | wl_ioctl(wl_dev, WLC_GET_BSSID, host->apInfo->bssid, 6); |
|---|
| 681 | wl_ioctl(wl_dev, WLC_GET_SSID, &ssid, sizeof(wlc_ssid_t)); |
|---|
| 682 | memcpy(host->apInfo->ssid, ssid.SSID, 32); |
|---|
| 683 | host->apInfo->ssidlen = ssid.SSID_len; |
|---|
| 684 | #endif |
|---|
| 685 | host->RSSI = 0; |
|---|
| 686 | #ifdef HAVE_MADWIFI |
|---|
| 687 | host->apInfo->channel = wifi_getchannel( wl_dev ); |
|---|
| 688 | #else |
|---|
| 689 | wl_ioctl(wl_dev, WLC_GET_CHANNEL, &channel, sizeof(channel_info_t)); |
|---|
| 690 | host->apInfo->channel = channel.hw_channel; |
|---|
| 691 | #endif |
|---|
| 692 | |
|---|
| 693 | macs = (maclist_t *) malloc(4 + MAX_STA_COUNT * sizeof(ether_addr_t)); |
|---|
| 694 | macs->count = MAX_STA_COUNT; |
|---|
| 695 | int code = getassoclist(wl_dev,macs); |
|---|
| 696 | printf("code :%d\n",code); |
|---|
| 697 | if (code>0) |
|---|
| 698 | { |
|---|
| 699 | for (i = 0; i < macs->count; i++) { |
|---|
| 700 | sta = gotHost(cfg, (char *)&macs->ea[i], typeSta); |
|---|
| 701 | #ifdef HAVE_MADWIFI |
|---|
| 702 | sta->RSSI = -getRssi(wl_dev,macs->ea)*100; |
|---|
| 703 | #else |
|---|
| 704 | memcpy(starssi.mac, &macs->ea[i], 6); |
|---|
| 705 | starssi.RSSI = 3000; |
|---|
| 706 | starssi.zero_ex_forty_one = 0x41; |
|---|
| 707 | if (wl_ioctl(wl_dev, WLC_GET_RSSI, &starssi, 12) < 0) printf("rssifail\n"); |
|---|
| 708 | sta->RSSI = -starssi.RSSI * 100; |
|---|
| 709 | #endif |
|---|
| 710 | sta->staInfo->state = ssAssociated; |
|---|
| 711 | memcpy(sta->staInfo->connectedBSSID, host->apInfo->bssid, 6); |
|---|
| 712 | } |
|---|
| 713 | } |
|---|
| 714 | } |
|---|
| 715 | else { |
|---|
| 716 | host = gotHost(cfg, mac, typeSta); |
|---|
| 717 | host->isSelf = 1; |
|---|
| 718 | host->RSSI = 0; |
|---|
| 719 | #ifdef HAVE_MADWIFI |
|---|
| 720 | if (getassoclist(wl_dev,macs)>-1) |
|---|
| 721 | { |
|---|
| 722 | if (macs->count>0){ |
|---|
| 723 | host->staInfo->state = ssUnassociated; |
|---|
| 724 | } |
|---|
| 725 | else { |
|---|
| 726 | host->staInfo->state = ssAssociated; |
|---|
| 727 | } |
|---|
| 728 | }else |
|---|
| 729 | { |
|---|
| 730 | host->staInfo->state = ssUnassociated; |
|---|
| 731 | |
|---|
| 732 | } |
|---|
| 733 | |
|---|
| 734 | |
|---|
| 735 | #else |
|---|
| 736 | if (wl_ioctl(wl_dev, WLC_GET_BSSID, &host->staInfo->connectedBSSID, 6) < 0) { |
|---|
| 737 | host->staInfo->state = ssUnassociated; |
|---|
| 738 | } |
|---|
| 739 | else { |
|---|
| 740 | host->staInfo->state = ssAssociated; |
|---|
| 741 | } |
|---|
| 742 | #endif |
|---|
| 743 | } |
|---|
| 744 | #ifdef HAVE_MADWIFI |
|---|
| 745 | cfg->curChannel = wifi_getchannel(wl_dev); |
|---|
| 746 | |
|---|
| 747 | #else |
|---|
| 748 | if (wl_ioctl(wl_dev, WLC_GET_CHANNEL, &channel, sizeof(channel_info_t)) >= 0) { |
|---|
| 749 | cfg->curChannel = channel.hw_channel; |
|---|
| 750 | printf( "Current channel is %i\n", cfg->curChannel); |
|---|
| 751 | } |
|---|
| 752 | |
|---|
| 753 | #endif |
|---|
| 754 | } |
|---|
| 755 | |
|---|
| 756 | |
|---|
| 757 | |
|---|
| 758 | |
|---|
| 759 | |
|---|