1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
|
/*
* Wireless Tools
*
* Jean II - HPLB 97->99 - HPL 99->00
*
* Main code for "iwconfig". This is the generic tool for most
* manipulations...
* You need to link this code against "iwlib.c" and "-lm".
*
* This file is released under the GPL license.
* Copyright (c) 1997-2002 Jean Tourrilhes <jt@hpl.hp.com>
*/
#include "iwlib.h" /* Header */
/************************* MISC SUBROUTINES **************************/
/*------------------------------------------------------------------*/
/*
* Print usage string
*/
static void
iw_usage(void)
{
fprintf(stderr, "Usage: iwpriv interface [private-command [private-arguments]]\n");
fprintf(stderr, " interface [roam {on|off}]\n");
fprintf(stderr, " interface [port {ad-hoc|managed|N}]\n");
}
/************************ GENERIC FUNCTIONS *************************/
/*------------------------------------------------------------------*/
/*
* Print on the screen in a neat fashion all the info we have collected
* on a device.
*/
static void
print_priv_info(int skfd,
char * ifname)
{
int k;
iwprivargs priv[32];
int n;
char * argtype[] = { " ", "byte", "char", "", "int ", "float" };
/* Read the private ioctls */
n = iw_get_priv_info(skfd, ifname, priv);
/* Is there any ? */
if(n <= 0)
{
/* Could skip this message ? */
fprintf(stderr, "%-8.8s no private ioctls.\n\n",
ifname);
}
else
{
printf("%-8.8s Available private ioctl :\n", ifname);
/* Print the all */
for(k = 0; k < n; k++)
printf(" %s (%X) : set %3d %s & get %3d %s\n",
priv[k].name, priv[k].cmd,
priv[k].set_args & IW_PRIV_SIZE_MASK,
argtype[(priv[k].set_args & IW_PRIV_TYPE_MASK) >> 12],
priv[k].get_args & IW_PRIV_SIZE_MASK,
argtype[(priv[k].get_args & IW_PRIV_TYPE_MASK) >> 12]);
printf("\n");
}
}
/*------------------------------------------------------------------*/
/*
* Get info on all devices and print it on the screen
*/
static void
print_priv_devices(int skfd)
{
char buff[1024];
struct ifconf ifc;
struct ifreq *ifr;
int i;
/* Get list of active devices */
ifc.ifc_len = sizeof(buff);
ifc.ifc_buf = buff;
if(ioctl(skfd, SIOCGIFCONF, &ifc) < 0)
{
fprintf(stderr, "SIOCGIFCONF: %s\n", strerror(errno));
return;
}
ifr = ifc.ifc_req;
/* Print them */
for(i = ifc.ifc_len / sizeof(struct ifreq); --i >= 0; ifr++)
print_priv_info(skfd, ifr->ifr_name);
}
/************************* SETTING ROUTINES **************************/
/*------------------------------------------------------------------*/
/*
* Execute a private command on the interface
*/
static int
set_private(int skfd, /* Socket */
char * args[], /* Command line args */
int count, /* Args count */
char * ifname) /* Dev name */
{
u_char buffer[1024];
struct iwreq wrq;
int i = 0; /* Start with first arg */
int k;
iwprivargs priv[16];
int number;
int temp;
/* Read the private ioctls */
number = iw_get_priv_info(skfd, ifname, priv);
/* Is there any ? */
if(number <= 0)
{
/* Could skip this message ? */
fprintf(stderr, "%-8.8s no private ioctls.\n\n",
ifname);
return(-1);
}
/* Search the correct ioctl */
k = -1;
while((++k < number) && strcmp(priv[k].name, args[i]));
/* If not found... */
if(k == number)
{
fprintf(stderr, "Invalid command : %s\n", args[i]);
return(-1);
}
/* Next arg */
i++;
/* If we have to set some data */
if((priv[k].set_args & IW_PRIV_TYPE_MASK) &&
(priv[k].set_args & IW_PRIV_SIZE_MASK))
{
switch(priv[k].set_args & IW_PRIV_TYPE_MASK)
{
case IW_PRIV_TYPE_BYTE:
/* Number of args to fetch */
wrq.u.data.length = count - 1;
if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
/* Fetch args */
for(; i < wrq.u.data.length + 1; i++) {
sscanf(args[i], "%d", &temp);
buffer[i - 1] = (char) temp;
}
break;
case IW_PRIV_TYPE_INT:
/* Number of args to fetch */
wrq.u.data.length = count - 1;
if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
/* Fetch args */
for(; i < wrq.u.data.length + 1; i++)
sscanf(args[i], "%d", ((u_int *) buffer) + i - 1);
break;
case IW_PRIV_TYPE_CHAR:
if(i < count)
{
/* Size of the string to fetch */
wrq.u.data.length = strlen(args[i]) + 1;
if(wrq.u.data.length > (priv[k].set_args & IW_PRIV_SIZE_MASK))
wrq.u.data.length = priv[k].set_args & IW_PRIV_SIZE_MASK;
/* Fetch string */
memcpy(buffer, args[i], wrq.u.data.length);
buffer[sizeof(buffer) - 1] = '\0';
i++;
}
else
{
wrq.u.data.length = 1;
buffer[0] = '\0';
}
break;
default:
fprintf(stderr, "Not yet implemented...\n");
return(-1);
}
if((priv[k].set_args & IW_PRIV_SIZE_FIXED) &&
(wrq.u.data.length != (priv[k].set_args & IW_PRIV_SIZE_MASK)))
{
printf("The command %s need exactly %d argument...\n",
priv[k].name, priv[k].set_args & IW_PRIV_SIZE_MASK);
return(-1);
}
} /* if args to set */
else
{
wrq.u.data.length = 0L;
}
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
if((priv[k].set_args & IW_PRIV_SIZE_FIXED) &&
(iw_byte_size(priv[k].set_args) < IFNAMSIZ))
memcpy(wrq.u.name, buffer, IFNAMSIZ);
else
{
wrq.u.data.pointer = (caddr_t) buffer;
wrq.u.data.flags = 0;
}
/* Perform the private ioctl */
if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
{
fprintf(stderr, "Interface doesn't accept private ioctl...\n");
fprintf(stderr, "%X: %s\n", priv[k].cmd, strerror(errno));
return(-1);
}
/* If we have to get some data */
if((priv[k].get_args & IW_PRIV_TYPE_MASK) &&
(priv[k].get_args & IW_PRIV_SIZE_MASK))
{
int j;
int n = 0; /* number of args */
printf("%-8.8s %s:", ifname, priv[k].name);
if((priv[k].get_args & IW_PRIV_SIZE_FIXED) &&
(iw_byte_size(priv[k].get_args) < IFNAMSIZ))
{
memcpy(buffer, wrq.u.name, IFNAMSIZ);
n = priv[k].get_args & IW_PRIV_SIZE_MASK;
}
else
n = wrq.u.data.length;
switch(priv[k].get_args & IW_PRIV_TYPE_MASK)
{
case IW_PRIV_TYPE_BYTE:
/* Display args */
for(j = 0; j < n; j++)
printf("%d ", buffer[j]);
printf("\n");
break;
case IW_PRIV_TYPE_INT:
/* Display args */
for(j = 0; j < n; j++)
printf("%d ", ((u_int *) buffer)[j]);
printf("\n");
break;
case IW_PRIV_TYPE_CHAR:
/* Display args */
buffer[wrq.u.data.length - 1] = '\0';
printf("%s\n", buffer);
break;
default:
fprintf(stderr, "Not yet implemented...\n");
return(-1);
}
} /* if args to set */
return(0);
}
/********************** PRIVATE IOCTLS MANIPS ***********************/
/*
* Convenient access to some private ioctls of some devices
*/
/*------------------------------------------------------------------*/
/*
* Set roaming mode on and off
* Found in wavelan_cs driver
*/
static int
set_roaming(int skfd, /* Socket */
char * args[], /* Command line args */
int count, /* Args count */
char * ifname) /* Dev name */
{
u_char buffer[1024];
struct iwreq wrq;
int i = 0; /* Start with first arg */
int k;
iwprivargs priv[16];
int number;
char RoamState; /* buffer to hold new roam state */
char ChangeRoamState=0; /* whether or not we are going to
change roam states */
/* Read the private ioctls */
number = iw_get_priv_info(skfd, ifname, priv);
/* Is there any ? */
if(number <= 0)
{
/* Could skip this message ? */
fprintf(stderr, "%-8.8s no private ioctls.\n\n",
ifname);
return(-1);
}
if(count != 1)
{
iw_usage();
return(-1);
}
if(!strcasecmp(args[i], "on"))
{
printf("%-8.8s enable roaming\n", ifname);
if(!number)
{
fprintf(stderr, "This device doesn't support roaming\n");
return(-1);
}
ChangeRoamState=1;
RoamState=1;
}
else
if(!strcasecmp(args[i], "off"))
{
i++;
printf("%-8.8s disable roaming\n", ifname);
if(!number)
{
fprintf(stderr, "This device doesn't support roaming\n");
return(-1);
}
ChangeRoamState=1;
RoamState=0;
}
else
{
iw_usage();
return(-1);
}
if(ChangeRoamState)
{
k = -1;
while((++k < number) && strcmp(priv[k].name, "setroam"));
if(k == number)
{
fprintf(stderr, "This device doesn't support roaming\n");
return(-1);
}
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
buffer[0]=RoamState;
memcpy(wrq.u.name, &buffer, IFNAMSIZ);
if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
{
fprintf(stderr, "Roaming support is broken.\n");
return(-1);
}
}
return(0);
}
/*------------------------------------------------------------------*/
/*
* Get and set the port type
* Found in wavelan2_cs and wvlan_cs drivers
*/
static int
port_type(int skfd, /* Socket */
char * args[], /* Command line args */
int count, /* Args count */
char * ifname) /* Dev name */
{
struct iwreq wrq;
int i = 0; /* Start with first arg */
int k;
iwprivargs priv[16];
int number;
char ptype = 0;
char * modes[] = { "invalid", "managed (BSS)", "reserved", "ad-hoc" };
/* Read the private ioctls */
number = iw_get_priv_info(skfd, ifname, priv);
/* Is there any ? */
if(number <= 0)
{
/* Could skip this message ? */
fprintf(stderr, "%-8.8s no private ioctls.\n\n", ifname);
return(-1);
}
/* Arguments ? */
if(count == 0)
{
/* So, we just want to see the current value... */
k = -1;
while((++k < number) && strcmp(priv[k].name, "gport_type") &&
strcmp(priv[k].name, "get_port"));
if(k == number)
{
fprintf(stderr, "This device doesn't support getting port type\n");
return(-1);
}
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
/* Get it */
if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
{
fprintf(stderr, "Port type support is broken.\n");
exit(0);
}
ptype = *wrq.u.name;
/* Display it */
printf("%-8.8s Current port mode is %s <port type is %d>.\n\n",
ifname, modes[(int) ptype], ptype);
return(0);
}
if(count != 1)
{
iw_usage();
return(-1);
}
/* Read it */
/* As a string... */
k = 0;
while((k < 4) && strncasecmp(args[i], modes[k], 2))
k++;
if(k < 4)
ptype = k;
else
/* ...or as an integer */
if(sscanf(args[i], "%d", (int *) &ptype) != 1)
{
iw_usage();
return(-1);
}
k = -1;
while((++k < number) && strcmp(priv[k].name, "sport_type") &&
strcmp(priv[k].name, "set_port"));
if(k == number)
{
fprintf(stderr, "This device doesn't support setting port type\n");
return(-1);
}
strncpy(wrq.ifr_name, ifname, IFNAMSIZ);
*(wrq.u.name) = ptype;
if(ioctl(skfd, priv[k].cmd, &wrq) < 0)
{
fprintf(stderr, "Invalid port type (or setting not allowed)\n");
return(-1);
}
return(0);
}
/******************************* MAIN ********************************/
/*------------------------------------------------------------------*/
/*
* The main !
*/
int
main(int argc,
char ** argv)
{
int skfd = -1; /* generic raw socket desc. */
int goterr = 0;
/* Create a channel to the NET kernel. */
if((skfd = iw_sockets_open()) < 0)
{
perror("socket");
return(-1);
}
/* No argument : show the list of all device + info */
if(argc == 1)
{
print_priv_devices(skfd);
close(skfd);
return(0);
}
/* Special cases take one... */
/* Help */
if((!strncmp(argv[1], "-h", 9)) ||
(!strcmp(argv[1], "--help")))
{
iw_usage();
close(skfd);
return(0);
}
/* The device name must be the first argument */
/* Name only : show for that device only */
if(argc == 2)
{
print_priv_info(skfd, argv[1]);
close(skfd);
return(0);
}
/* Special cases take two... */
/* Roaming */
if(!strncmp(argv[2], "roam", 4))
{
goterr = set_roaming(skfd, argv + 3, argc - 3, argv[1]);
close(skfd);
return(goterr);
}
/* Port type */
if(!strncmp(argv[2], "port", 4))
{
goterr = port_type(skfd, argv + 3, argc - 3, argv[1]);
close(skfd);
return(goterr);
}
/* Otherwise, it's a private ioctl */
goterr = set_private(skfd, argv + 2, argc - 2, argv[1]);
/* Close the socket. */
close(skfd);
return(goterr);
}
|