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