1 // Adapted for vomp by Chris Tallon by changing "int main" to "int ticonfig_main"
4 * Copyright (C) 2006, Jon Gettler
5 * http://www.mvpmc.org/
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <sys/ioctl.h>
30 #include <net/if_arp.h>
31 #include <netinet/in.h>
35 #define DEVNAME "eth1"
38 unsigned char device[16];
51 unsigned int unknown[3];
54 unsigned int unknown2[27];
62 static int sockfd = -1;
63 static int config_started = 0;
64 static char default_ssid[128];
65 static char default_wep[128];
66 static ti_ssid_list_t slist;
67 static int verbose = 0;
69 static struct option opts[] = {
70 { "help", no_argument, 0, 'h' },
71 { "probe", no_argument, 0, 'p' },
72 { "ssid", required_argument, 0, 's' },
73 { "verbose", no_argument, 0, 'v' },
74 { "wep", required_argument, 0, 'w' },
79 print_help(char *prog)
81 printf("Usage: %s <options>\n", prog);
82 printf("\t-h \tprint this help\n");
83 printf("\t-p \tprobe for wireless networks\n");
84 printf("\t-s ssid \tSSID to use\n");
85 printf("\t-v \tverbose\n");
93 char *vpd, *network, *wep;
96 if ((fd=open("/proc/mtd", O_RDONLY)) > 0) {
97 FILE *f = fdopen(fd, "r");
100 fgets(line, sizeof(line), f);
101 /* read each mtd entry */
102 while (fgets(line, sizeof(line), f) != NULL) {
103 if (strstr(line, " VPD") != NULL) {
113 fprintf(stderr, "not running on a wireless MVP!\n");
117 snprintf(path, sizeof(path), "/dev/mtd%d", i);
119 if ((fd=open(path, O_RDONLY)) < 0)
122 if ((mtd=malloc(65536)) == NULL) {
127 if (read(fd, mtd, 65536) != 65536) {
136 network = vpd+0x2008;
139 if (network[0] != '\0') {
141 printf("Default wireless network: '%s'\n", network);
142 strcpy(default_ssid, network);
145 printf("No default wireless network found in flash\n");
148 if (wep[0] != '\0') {
150 printf("WEP key found!\n");
151 strcpy(default_wep, wep);
154 printf("WEP key not found!\n");
157 if (vpd[0x3000] != '\0') {
159 printf("Default server: %d.%d.%d.%d\n",
160 vpd[0x3000], vpd[0x3001],
161 vpd[0x3002], vpd[0x3003]);
164 printf("No default server found in flash\n");
172 init_device(int fd, char *name)
177 memset(&dev, 0, sizeof(dev));
178 memset(&ssid, 0, sizeof(ssid));
180 strcpy(dev.device, name);
181 dev.arg2 = 0x00223834;
186 if (ioctl(fd, SIOCDEVPRIVATE, &dev) != 0) {
187 perror("ioctl(SIOCDEVPRIVATE)");
192 printf("device initialized!\n");
198 start_config_manager(int fd, char *name)
201 unsigned long buf[256];
203 memset(&dev, 0, sizeof(dev));
204 strcpy(dev.device, name);
205 dev.arg2 = 0x0022381c;
208 dev.ptr = (void*)0x1;
210 if (ioctl(fd, SIOCDEVPRIVATE, &dev) != 0) {
211 if (errno == EALREADY) {
213 printf("config manager already running!\n");
216 perror("ioctl(SIOCDEVPRIVATE)");
221 printf("config manager started!\n");
225 memset(&dev, 0, sizeof(dev));
226 strcpy(dev.device, DEVNAME);
227 dev.arg2 = 0x00223818;
230 dev.ptr = (void*)0x0;
232 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
233 perror("ioctl(SIOCDEVPRIVATE+1)");
237 memset(&dev, 0, sizeof(dev));
238 strcpy(dev.device, DEVNAME);
239 dev.arg2 = 0x00222018;
242 dev.ptr = (void*)buf;
245 memcpy(buf+1, "NON-SSID", 8);
252 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
253 perror("ioctl(SIOCDEVPRIVATE)");
261 up_device(int fd, char *name)
264 unsigned long buf[256];
267 printf("Bringing up eth1...\n");
269 memset(&dev, 0, sizeof(dev));
270 memset(&buf, 0, sizeof(buf));
271 strcpy(dev.device, DEVNAME);
272 dev.arg2 = 0x00224420;
275 dev.ptr = (void*)buf;
277 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
278 perror("ioctl(SIOCDEVPRIVATE+1)");
282 memset(&dev, 0, sizeof(dev));
283 memset(&buf, 0, sizeof(buf));
284 strcpy(dev.device, DEVNAME);
285 dev.arg2 = 0x00222018;
288 dev.ptr = (void*)buf;
290 buf[0] = strlen(default_ssid);
291 strcpy((char*)(&buf[1]), default_ssid);
294 printf("Using SSID: '%s'\n", default_ssid);
296 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
297 perror("ioctl(SIOCDEVPRIVATE)");
301 memset(&dev, 0, sizeof(dev));
302 memset(&buf, 0, sizeof(buf));
303 strcpy(dev.device, DEVNAME);
304 dev.arg2 = 0x00224420;
307 dev.ptr = (void*)buf;
309 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
310 perror("ioctl(SIOCDEVPRIVATE+1)");
314 memset(&dev, 0, sizeof(dev));
315 strcpy(dev.device, DEVNAME);
316 dev.arg2 = 0x00223028;
320 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
321 perror("ioctl(SIOCDEVPRIVATE+1)");
327 printf("Signal strength: %ddb\n", (int)dev.ptr);
328 printf("Signal strength: %p\n", dev.ptr);
336 wep_copy(char *buf, char *key)
340 len = strlen(key) / 2;
342 for (i=0; i<len; i++) {
348 if ((h>='0') && (h<='9')) {
353 if ((l>='0') && (l<='9')) {
359 buf[i] = (h << 4) | l;
364 up_device_wep(int fd, char *name)
367 unsigned long buf[256];
370 printf("Bringing up WEP-enabled eth1...\n");
372 if (init_device(sockfd, DEVNAME) < 0)
375 if (start_config_manager(sockfd, DEVNAME) < 0)
379 memset(&dev, 0, sizeof(dev));
380 memset(&buf, 0, sizeof(buf));
381 strcpy(dev.device, DEVNAME);
382 dev.arg2 = 0x00224420;
385 dev.ptr = (void*)buf;
386 strcpy((char*)buf, default_ssid);
388 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
389 perror("ioctl(SIOCDEVPRIVATE+1)");
393 memset(&dev, 0, sizeof(dev));
394 memset(&buf, 0, sizeof(buf));
395 strcpy(dev.device, DEVNAME);
396 dev.arg2 = 0x00222018;
399 dev.ptr = (void*)buf;
401 buf[0] = strlen(default_ssid);
402 strcpy((char*)(&buf[1]), default_ssid);
405 printf("Using SSID: '%s'\n", default_ssid);
407 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
408 perror("ioctl(SIOCDEVPRIVATE)");
413 memset(&dev, 0, sizeof(dev));
414 strcpy(dev.device, DEVNAME);
415 dev.arg2 = 0x00223410;
419 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
420 perror("ioctl(SIOCDEVPRIVATE)");
424 memset(&dev, 0, sizeof(dev));
425 strcpy(dev.device, DEVNAME);
426 dev.arg2 = 0x00223488;
430 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
432 * XXX: this error seems normal
435 perror("ioctl(SIOCDEVPRIVATE)");
440 memset(&dev, 0, sizeof(dev));
441 memset(&buf, 0, sizeof(buf));
442 strcpy(dev.device, DEVNAME);
443 dev.arg2 = 0x00223404;
446 dev.ptr = (void*)buf;
451 wep_copy((char*)(buf+3), default_wep);
453 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
454 perror("ioctl(SIOCDEVPRIVATE)");
458 memset(&dev, 0, sizeof(dev));
459 memset(&buf, 0, sizeof(buf));
460 strcpy(dev.device, DEVNAME);
461 dev.arg2 = 0x00222018;
464 dev.ptr = (void*)buf;
466 buf[0] = strlen(default_ssid);
467 strcpy((char*)(&buf[1]), default_ssid);
470 printf("Using SSID: '%s'\n", default_ssid);
472 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
473 perror("ioctl(SIOCDEVPRIVATE)");
477 memset(&dev, 0, sizeof(dev));
478 memset(&buf, 0, sizeof(buf));
479 strcpy(dev.device, DEVNAME);
480 dev.arg2 = 0x00224420;
483 dev.ptr = (void*)buf;
484 strcpy((char*)(&buf[0]), default_ssid);
486 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
487 perror("ioctl(SIOCDEVPRIVATE+1)");
491 memset(&dev, 0, sizeof(dev));
492 memset(&buf, 0, sizeof(buf));
493 strcpy(dev.device, DEVNAME);
494 dev.arg2 = 0x00224420;
497 dev.ptr = (void*)buf;
499 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
500 perror("ioctl(SIOCDEVPRIVATE+1)");
505 memset(&dev, 0, sizeof(dev));
506 strcpy(dev.device, DEVNAME);
507 dev.arg2 = 0x00223028;
511 if (ioctl(sockfd, SIOCDEVPRIVATE+1, &dev) != 0) {
512 perror("ioctl(SIOCDEVPRIVATE+1)");
521 get_ssid_list(int fd, char *name)
526 memset(&dev, 0, sizeof(dev));
527 strcpy(dev.device, name);
528 dev.arg2 = 0x00222c20;
530 if (ioctl(fd, SIOCDEVPRIVATE, &dev) != 0) {
531 perror("ioctl(SIOCDEVPRIVATE)");
535 memset(&dev, 0, sizeof(dev));
536 strcpy(dev.device, name);
537 dev.arg2 = 0x00222c1c;
541 if (ioctl(fd, SIOCDEVPRIVATE, &dev) != 0) {
542 perror("ioctl(SIOCDEVPRIVATE)");
546 memset(&dev, 0, sizeof(dev));
547 memset(&slist, 0, sizeof(slist));
548 strcpy(dev.device, name);
549 dev.arg2 = 0x0022200c;
552 dev.ptr = (void*)&slist;
554 if (ioctl(fd, SIOCDEVPRIVATE, &dev) != 0) {
555 perror("ioctl(SIOCDEVPRIVATE)");
561 if ((n > 0) && verbose)
562 printf("Found %d wireless networks!\n", n);
573 strncpy(ifr.ifr_name, DEVNAME, IFNAMSIZ);
575 if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
580 if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
581 perror("ioctl(SIOCGIFFLAGS)");
585 if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
586 perror("ioctl(SIOCSIFFLAGS)");
590 if (init_device(sockfd, DEVNAME) < 0)
593 if (start_config_manager(sockfd, DEVNAME) < 0)
596 memset(&dev, 0, sizeof(dev));
597 strcpy(dev.device, DEVNAME);
598 dev.arg2 = 0x00222020;
600 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
601 perror("ioctl(SIOCDEVPRIVATE)");
605 memset(&dev, 0, sizeof(dev));
606 strcpy(dev.device, DEVNAME);
607 dev.arg2 = 0x00222c20;
609 if (ioctl(sockfd, SIOCDEVPRIVATE, &dev) != 0) {
610 perror("ioctl(SIOCDEVPRIVATE)");
618 ticonfig_main(int argc, char **argv)
623 char *ssid = NULL, *key = NULL;
627 while ((c=getopt_long(argc, argv,
628 "hps:w:v", opts, &opt_index)) != -1) {
638 ssid = strdup(optarg);
641 if (strcasecmp(optarg, "off") == 0) {
643 } else if (strcasecmp(optarg, "on") == 0) {
647 key = strdup(optarg);
660 if (read_vpd() != 0) {
661 fprintf(stderr, "VPD read failed!\n");
665 strcpy(default_wep, key);
668 fprintf(stderr, "initialization failed!\n");
672 n = get_ssid_list(sockfd, DEVNAME);
675 * After starting the config manager, we may need to wait a bit...
677 if ((n == 0) && (config_started == 1)) {
679 n = get_ssid_list(sockfd, DEVNAME);
683 fprintf(stderr, "SSID probe failed!\n");
686 fprintf(stderr, "no wireless networks found!\n");
691 for (i=0; i<n; i++) {
692 printf("Found SSID: '%s'\n", slist.ssid[i].name);
698 strcpy(default_ssid, ssid);
701 for (i=0; i<n; i++) {
702 if (strcmp(default_ssid, slist.ssid[i].name) == 0)
707 printf("Warning: SSID '%s' not found during probe!\n",
711 if ((default_wep[0] == '\0') || (with_wep == 0)) {
712 if (up_device(sockfd, DEVNAME) < 0) {
713 fprintf(stderr, "device bringup failed!\n");
716 printf("wireless network enabled: SSID '%s' without WEP\n",
719 if (up_device_wep(sockfd, DEVNAME) < 0) {
720 fprintf(stderr, "device bringup failed!\n");
723 printf("wireless network enabled: SSID '%s' with WEP\n",