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