int connect_wifi() { struct iwreq wrq; char essid[IW_ESSID_MAX_SIZE + 1]; unsigned char key[IW_ENCODING_TOKEN_MAX]; int32_t keylen = 0; int wlsock = iw_sockets_open(); const char *itfName = "wlan0"; const char *networkName = "linksys"; const char *com_key = "9F1C3EE11CBA230B27BF1C1B6F"; if(wlsock < 0) return -1; memset(&wrq,0,sizeof(struct iwreq)); keylen = iw_in_key_full(wlsock, itfName, com_key, key, &wrq.u.data.flags); if(keylen <= 0) return -1; wrq.u.data.length = keylen; wrq.u.data.pointer = (caddr_t) key; wrq.u.data.flags |= IW_ENCODE_RESTRICTED; if(iw_set_ext(wlsock, itfName, SIOCSIWENCODE, &wrq) < 0) return -1; memset(&wrq,0,sizeof(struct iwreq)); wrq.u.mode = IW_MODE_INFRA; if(iw_set_ext( wlsock, itfName, SIOCSIWMODE, &wrq) < 0) return -1; if(strlen(networkName) > IW_ESSID_MAX_SIZE) return -1; memset(essid,0,IW_ESSID_MAX_SIZE + 1); memset(&wrq,0,sizeof(struct iwreq)); strncpy(essid,networkName,strlen(networkName)); wrq.u.essid.flags = 1; wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid) + 1; if(iw_set_ext( wlsock, itfName, SIOCSIWESSID, &wrq) < 0) return -1; iw_sockets_close(wlsock); return 0; }
/* * Set Nickname */ static int set_nick_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int we_kernel_version; /* Avoid "Unused parameter" warning */ count = count; if(strlen(args[0]) > IW_ESSID_MAX_SIZE) { errmax = IW_ESSID_MAX_SIZE; return(IWERR_ARG_SIZE); } we_kernel_version = iw_get_kernel_we_version(); wrq.u.essid.pointer = (caddr_t) args[0]; wrq.u.essid.length = strlen(args[0]); if(we_kernel_version < 21) wrq.u.essid.length++; if(iw_set_ext(skfd, ifname, SIOCSIWNICKN, &wrq) < 0) return(IWERR_SET_EXT); /* 1 args */ return(1); }
/* * Set Sensitivity */ static int set_sens_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int temp; /* Avoid "Unused parameter" warning */ count = count; if(sscanf(args[0], "%i", &(temp)) != 1) { errarg = 0; return(IWERR_ARG_TYPE); } wrq.u.sens.value = temp; if(iw_set_ext(skfd, ifname, SIOCSIWSENS, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1); }
/* * Set Mode */ static int set_mode_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; unsigned int k; /* Must be unsigned */ /* Avoid "Unused parameter" warning */ count = count; /* Check if it is a uint, otherwise get is as a string */ if(sscanf(args[0], "%i", &k) != 1) { k = 0; while((k < IW_NUM_OPER_MODE) && strncasecmp(args[0], iw_operation_mode[k], 3)) k++; } if(k >= IW_NUM_OPER_MODE) { errarg = 0; return(IWERR_ARG_TYPE); } wrq.u.mode = k; if(iw_set_ext(skfd, ifname, SIOCSIWMODE, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1); }
C_RESULT vp_com_wf_disconnect(vp_com_wifi_config_t* config, vp_com_wifi_connection_t* connection) { C_RESULT res = VP_COM_OK; #ifdef USE_IWLIB struct iwreq wrq; char essid[IW_ESSID_MAX_SIZE + 1]; int wlsock = iw_sockets_open(); vp_os_memset(&wrq, 0, sizeof(struct iwreq)); strncpy(essid, connection->networkName, strlen(connection->networkName)); wrq.u.essid.flags = 0; wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid); if(iw_get_kernel_we_version() < 21) wrq.u.essid.length++; // Get version from kernel, device may not have range... res = ( iw_set_ext( wlsock, config->itfName, SIOCSIWESSID, &wrq) < 0 ) ? VP_COM_ERROR : VP_COM_OK; iw_sockets_close(wlsock); #endif return res; }
int change_channel(int channel) { int skfd = 0, ret_val = 0; struct iwreq wrq; memset((void *) &wrq, 0, sizeof(struct iwreq)); /* Open NET socket */ if((skfd = iw_sockets_open()) < 0) { perror("iw_sockets_open"); } else if(get_iface()) { /* Convert channel to a frequency */ iw_float2freq((double) channel, &(wrq.u.freq)); /* Fixed frequency */ wrq.u.freq.flags = IW_FREQ_FIXED; cprintf(VERBOSE, "[+] Switching %s to channel %d\n", get_iface(), channel); /* Set frequency */ if(iw_set_ext(skfd, get_iface(), SIOCSIWFREQ, &wrq) >= 0) { set_channel(channel); ret_val = 1; } iw_sockets_close(skfd); } return ret_val; }
static int wpa_ndiswrapper_associate(void *priv, struct wpa_driver_associate_params *params) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct wpa_assoc_info wpa_assoc_info; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); memset(&wpa_assoc_info, 0, sizeof(wpa_assoc_info)); wpa_assoc_info.bssid = params->bssid; wpa_assoc_info.ssid = params->ssid; wpa_assoc_info.ssid_len = params->ssid_len; wpa_assoc_info.freq = params->freq; wpa_assoc_info.wpa_ie = params->wpa_ie; wpa_assoc_info.wpa_ie_len = params->wpa_ie_len; wpa_assoc_info.pairwise_suite = params->pairwise_suite; wpa_assoc_info.group_suite = params->group_suite; wpa_assoc_info.key_mgmt_suite = params->key_mgmt_suite; wpa_assoc_info.auth_alg = params->auth_alg; wpa_assoc_info.mode = params->mode; priv_req.u.data.pointer = (void *)&wpa_assoc_info; priv_req.u.data.length = sizeof(wpa_assoc_info); if (iw_set_ext(drv, WPA_ASSOCIATE, &priv_req) < 0) ret = -1; return ret; }
int netcfg_wireless_set_wep (struct debconfclient * client, struct netcfg_interface *interface) { wireless_config wconf; char* rv = NULL; int ret, keylen, err = 0; unsigned char buf [IW_ENCODING_TOKEN_MAX + 1]; struct iwreq wrq; iw_get_basic_config (wfd, interface->name, &wconf); debconf_subst(client, "netcfg/wireless_wep", "iface", interface->name); debconf_input (client, "high", "netcfg/wireless_wep"); ret = debconf_go(client); if (ret == CMD_GOBACK) return GO_BACK; debconf_get(client, "netcfg/wireless_wep"); rv = client->value; if (empty_str(rv)) { unset_wep_key (interface->name); if (interface->wepkey != NULL) { free(interface->wepkey); interface->wepkey = NULL; } return 0; } while ((keylen = iw_in_key (rv, buf)) == -1) { debconf_subst(client, "netcfg/invalid_wep", "wepkey", rv); debconf_input(client, "critical", "netcfg/invalid_wep"); debconf_go(client); debconf_input (client, "high", "netcfg/wireless_wep"); ret = debconf_go(client); if (ret == CMD_GOBACK) return GO_BACK; debconf_get(client, "netcfg/wireless_wep"); rv = client->value; } /* Now rv is safe to store since it parsed fine */ interface->wepkey = strdup(rv); wrq.u.data.pointer = buf; wrq.u.data.flags = 0; wrq.u.data.length = keylen; if ((err = iw_set_ext(skfd, interface->name, SIOCSIWENCODE, &wrq)) < 0) { di_warning("setting WEP key on %s failed with code %d", interface->name, err); return -1; } return 0; }
/* * Set spy thresholds in the driver from command line */ static int set_spy_threshold(int skfd, /* The socket */ char * ifname, /* Dev name */ char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; struct iw_thrspy threshold; int low_thr; int high_thr; /* Init */ memset(&threshold, '\0', sizeof(threshold)); /* "off" : disable functionality (set 0 addresses) */ if(!strcmp(args[0], "off")) { /* Just send null threshold, will disable it */ } else { /* Try to get our threshold */ if(count < 2) { fprintf(stderr, "%-8.16s Need two threshold values\n", ifname); return(-1); } if((sscanf(args[0], "%i", &low_thr) != 1) || (sscanf(args[1], "%i", &high_thr) != 1)) { fprintf(stderr, "%-8.16s Invalid threshold values\n", ifname); return(-1); } /* Basic sanity check */ if(high_thr < low_thr) { fprintf(stderr, "%-8.16s Inverted threshold range\n", ifname); return(-1); } /* Copy thresholds */ threshold.low.level = low_thr; threshold.low.updated = 0x2; threshold.high.level = high_thr; threshold.high.updated = 0x2; } /* Time to send thresholds to the driver */ wrq.u.data.pointer = (caddr_t) &threshold; wrq.u.data.length = 1; wrq.u.data.flags = 0; if(iw_set_ext(skfd, ifname, SIOCSIWTHRSPY, &wrq) < 0) { fprintf(stderr, "Interface doesn't accept thresholds...\n"); fprintf(stderr, "SIOCSIWTHRSPY: %s\n", strerror(errno)); return(-1); } return(0); }
/* * Get spy thresholds from the driver and display */ static int get_spy_threshold(int skfd, /* The socket */ char * ifname, /* Dev name */ char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; struct iw_thrspy threshold; iwrange range; int has_range = 0; /* Avoid "Unused parameter" warning */ args = args; count = count; /* Time to send thresholds to the driver */ wrq.u.data.pointer = (caddr_t) &threshold; wrq.u.data.length = 1; wrq.u.data.flags = 0; if(iw_set_ext(skfd, ifname, SIOCGIWTHRSPY, &wrq) < 0) { fprintf(stderr, "Interface doesn't support thresholds...\n"); fprintf(stderr, "SIOCGIWTHRSPY: %s\n", strerror(errno)); return(-1); } /* Get range info if we can */ if(iw_get_range_info(skfd, ifname, &(range)) >= 0) has_range = 1; /* Display thresholds */ if((has_range) && (threshold.low.level)) { /* If the statistics are in dBm */ if(threshold.low.level > range.max_qual.level) { /* Statistics are in dBm (absolute power measurement) */ printf("%-8.16s Low threshold:%d dBm High threshold:%d dBm\n\n", ifname, threshold.low.level - 0x100, threshold.high.level - 0x100); } else { /* Statistics are relative values (0 -> max) */ printf("%-8.16s Low threshold:%d/%d High threshold:%d/%d\n\n", ifname, threshold.low.level, range.max_qual.level, threshold.high.level, range.max_qual.level); } } else { /* We can't read the range, so we don't know... */ printf("%-8.16s Low threshold:%d High threshold:%d\n\n", ifname, threshold.low.level, threshold.high.level); } return(0); }
/* * Set RTS Threshold */ static int set_rts_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; /* Avoid "Unused parameter" warning */ count = count; wrq.u.rts.value = -1; wrq.u.rts.fixed = 1; wrq.u.rts.disabled = 0; if(!strcasecmp(args[0], "off")) wrq.u.rts.disabled = 1; /* i.e. max size */ else if(!strcasecmp(args[0], "auto")) wrq.u.rts.fixed = 0; else { if(!strcasecmp(args[0], "fixed")) { /* Get old RTS threshold */ if(iw_get_ext(skfd, ifname, SIOCGIWRTS, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.rts.fixed = 1; } else { /* Should be a numeric value */ long temp; if(sscanf(args[0], "%li", (unsigned long *) &(temp)) != 1) { errarg = 0; return(IWERR_ARG_TYPE); } wrq.u.rts.value = temp; /*Begin: Modified by zhanghu to support minimum rts to 100 in iwconfig ath0 rts 100 2013-01-29*/ if(wrq.u.rts.value < 100) { //deleted by zhanghu if(wrq.u.rts.value < 256) { /*End: Modified by zhanghu to support minimum rts to 100 in iwconfig ath0 rts 100 2013-01-29*/ fprintf(stderr, "Minimum RTS Threshold should be 100 bytes\n"); errarg = 0; return(IWERR_ARG_TYPE); } } } if(iw_set_ext(skfd, ifname, SIOCSIWRTS, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1); }
/* * Set frequency/channel */ static int set_channel(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int i = 1; if(!strcasecmp(args[0], "auto")) { wrq.u.freq.m = -1; wrq.u.freq.e = 0; wrq.u.freq.flags = 0; } else { if(!strcasecmp(args[0], "fixed")) { /* Get old frequency */ if(iw_get_ext(skfd, ifname, SIOCGIWFREQ, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.freq.flags = IW_FREQ_FIXED; } else { /* Should be a numeric value */ double freq; char * unit; freq = strtod(args[0], &unit); if(unit == args[0]) { errarg = 0; return(IWERR_ARG_TYPE); } if(unit != NULL) { if(unit[0] == 'G') freq *= GIGA; if(unit[0] == 'M') freq *= MEGA; if(unit[0] == 'k') freq *= KILO; } iw_float2freq(freq, &(wrq.u.freq)); wrq.u.freq.flags = IW_FREQ_FIXED; /* Check for an additional argument */ if((i < count) && (!strcasecmp(args[i], "auto"))) { wrq.u.freq.flags = 0; ++i; } if((i < count) && (!strcasecmp(args[i], "fixed"))) { wrq.u.freq.flags = IW_FREQ_FIXED; ++i; } } } if(iw_set_ext(skfd, ifname, SIOCSIWFREQ, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i); }
static int wpa_ndiswrapper_set_wpa(void *priv, int enabled) { struct wpa_driver_ndiswrapper_data *drv = priv; struct iwreq priv_req; int ret = 0; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.data.flags = enabled; if (iw_set_ext(drv, WPA_SET_WPA, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_set_auth_alg(void *priv, int auth_alg) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.param.value = auth_alg; if (iw_set_ext(drv, WPA_SET_AUTH_ALG, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_set_countermeasures(void *priv, int enabled) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.param.value = enabled; if (iw_set_ext(drv, WPA_SET_COUNTERMEASURES, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_set_drop_unencrypted(void *priv, int enabled) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.param.value = enabled; if (iw_set_ext(drv, WPA_DROP_UNENCRYPTED, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_disassociate(void *priv, const u8 *addr, int reason_code) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); memcpy(&priv_req.u.ap_addr.sa_data, addr, ETH_ALEN); if (iw_set_ext(drv, WPA_DISASSOCIATE, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_get_capa(void *priv, struct wpa_driver_capa *capa) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.data.pointer = (void *) capa; priv_req.u.data.length = sizeof(*capa); if (iw_set_ext(drv, WPA_GET_CAPA, &priv_req) < 0) ret = -1; return ret; }
static int wpa_ndiswrapper_deauthenticate(void *priv, const u8 *addr, int reason_code) { struct wpa_driver_ndiswrapper_data *drv = priv; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); priv_req.u.param.value = reason_code; memcpy(&priv_req.u.ap_addr.sa_data, addr, ETH_ALEN); if (iw_set_ext(drv, WPA_DEAUTHENTICATE, &priv_req) < 0) ret = -1; return ret; }
/* * Set Fragmentation Threshold */ static int set_frag_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; /* Avoid "Unused parameter" warning */ count = count; wrq.u.frag.value = -1; wrq.u.frag.fixed = 1; wrq.u.frag.disabled = 0; if(!strcasecmp(args[0], "off")) wrq.u.frag.disabled = 1; /* i.e. max size */ else if(!strcasecmp(args[0], "auto")) wrq.u.frag.fixed = 0; else { if(!strcasecmp(args[0], "fixed")) { /* Get old fragmentation threshold */ if(iw_get_ext(skfd, ifname, SIOCGIWFRAG, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.frag.fixed = 1; } else { /* Should be a numeric value */ long temp; if(sscanf(args[0], "%li", &(temp)) != 1) { errarg = 0; return(IWERR_ARG_TYPE); } wrq.u.frag.value = temp; } } if(iw_set_ext(skfd, ifname, SIOCSIWFRAG, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1); }
int ws_connect(wireless_scan *ws) { char *netfile = NULL; if (hook_preup) system(hook_preup); if (mode & MODE_SECURE) { netfile = (char *) calloc(strlen(netpath)+strlen(ws->b.essid)+2, sizeof(char)); strcpy(netfile,netpath); strcat(netfile,ws->b.essid); } if ( !is_known(ws) && (mode & MODE_SECURE)) { /* secure unknown network */ char psk[64]; fprintf(stdout,"Enter passkey for \"%s\"\n> ",ws->b.essid); fflush(stdout); scanf("%s",psk); sprintf(cmd,"wpa_passphrase \"%s\" \"%s\" > \"%s\"",ws->b.essid,psk,netfile); system(cmd); } if (mode & MODE_SECURE) { /* secure known/new */ spawn("wpa_supplicant",netfile); } else { /* unsecure network */ struct iwreq req; req.u.essid.flags = 1; req.u.essid.pointer = (caddr_t) ws->b.essid; req.u.essid.length = strlen(ws->b.essid); if (we_ver < 21) req.u.essid.length++; iw_set_ext(skfd,ifname,SIOCSIWESSID,&req); } if (!is_known(ws) && (mode & MODE_ADD)) { FILE *cfg; if ( (cfg=fopen(config,"ax")) ) /* no config file, create new */ fprintf(cfg,"\n[NETWORKS]\n%s\n",ws->b.essid); else if ( (cfg=fopen(config,"a")) ) /* normal append */ fprintf(cfg,"%s\n",ws->b.essid); if (cfg) fclose(cfg); } else if (!is_known(ws) && (mode & MODE_SECURE)) { sprintf(cmd,"rm \"%s\"",netfile); system(cmd); } if (netfile) { free(netfile); netfile = NULL; } spawn(dhcp,netfile); if (hook_postup) system(hook_postup); }
/* * Set commit */ static int set_commit_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; /* Avoid "Unused parameter" warning */ args = args; count = count; if(iw_set_ext(skfd, ifname, SIOCSIWCOMMIT, &wrq) < 0) return(IWERR_SET_EXT); /* No args */ return(0); }
/* * Set commit */ static int set_nwid_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; unsigned long temp; /* Avoid "Unused parameter" warning */ count = count; if((!strcasecmp(args[0], "off")) || (!strcasecmp(args[0], "any"))) wrq.u.nwid.disabled = 1; else if(!strcasecmp(args[0], "on")) { /* Get old nwid */ if(iw_get_ext(skfd, ifname, SIOCGIWNWID, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.nwid.disabled = 0; } else if(sscanf(args[0], "%lX", &(temp)) != 1) { errarg = 0; return(IWERR_ARG_TYPE); } else { wrq.u.nwid.value = temp; wrq.u.nwid.disabled = 0; } wrq.u.nwid.fixed = 1; /* Set new nwid */ if(iw_set_ext(skfd, ifname, SIOCSIWNWID, &wrq) < 0) return(IWERR_SET_EXT); /* 1 arg */ return(1); }
static int set_clearstat_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; //printf("[%s][%d]:\n", __FUNCTION__, __LINE__); /* Avoid "Unused parameter" warning */ args = args; count = count; if(iw_set_ext(skfd, ifname, SIOCSIWSTATS, &wrq) < 0) return(IWERR_SET_EXT); /* No args */ return(0); }
static int wpa_ndiswrapper_set_key(void *priv, wpa_alg alg, const u8 *addr, int key_idx, int set_tx, const u8 *seq, size_t seq_len, const u8 *key, size_t key_len) { struct wpa_driver_ndiswrapper_data *drv = priv; struct wpa_key wpa_key; int ret = 0; struct iwreq priv_req; memset(&priv_req, 0, sizeof(priv_req)); wpa_key.alg = alg; wpa_key.addr = addr; wpa_key.key_index = key_idx; wpa_key.set_tx = set_tx; wpa_key.seq = seq; wpa_key.seq_len = seq_len; wpa_key.key = key; wpa_key.key_len = key_len; priv_req.u.data.pointer = (void *)&wpa_key; priv_req.u.data.length = sizeof(wpa_key); if (iw_set_ext(drv, WPA_SET_KEY, &priv_req) < 0) ret = -1; if (alg == WPA_ALG_NONE) { /* * ndiswrapper did not seem to be clearing keys properly in * some cases with WPA_SET_KEY. For example, roaming from WPA * enabled AP to plaintext one seemed to fail since the driver * did not associate. Try to make sure the keys are cleared so * that plaintext APs can be used in all cases. */ wpa_driver_wext_set_key(drv->wext, alg, addr, key_idx, set_tx, seq, seq_len, key, key_len); } return ret; }
/* * Set AP Address */ static int set_apaddr_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; /* Avoid "Unused parameter" warning */ count = count; if((!strcasecmp(args[0], "auto")) || (!strcasecmp(args[0], "any"))) { /* Send a broadcast address */ iw_broad_ether(&(wrq.u.ap_addr)); } else { if(!strcasecmp(args[0], "off")) { /* Send a NULL address */ iw_null_ether(&(wrq.u.ap_addr)); } else { /* Get the address and check if the interface supports it */ if(iw_in_addr(skfd, ifname, args[0], &(wrq.u.ap_addr)) < 0) { errarg = 0; return(IWERR_ARG_TYPE); } } } if(iw_set_ext(skfd, ifname, SIOCSIWAP, &wrq) < 0) return(IWERR_SET_EXT); /* 1 args */ return(1); }
/* * Set encryption */ static int set_enc_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int i = 1; unsigned char key[IW_ENCODING_TOKEN_MAX]; if(!strcasecmp(args[0], "on")) { /* Get old encryption information */ wrq.u.data.pointer = (caddr_t) key; wrq.u.data.length = IW_ENCODING_TOKEN_MAX; wrq.u.data.flags = 0; if(iw_get_ext(skfd, ifname, SIOCGIWENCODE, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.data.flags &= ~IW_ENCODE_DISABLED; /* Enable */ } else { int gotone = 0; int oldone; int keylen; int temp; wrq.u.data.pointer = (caddr_t) NULL; wrq.u.data.flags = 0; wrq.u.data.length = 0; i = 0; /* Allow arguments in any order (it's safe) */ do { oldone = gotone; /* -- Check for the key -- */ if(i < count) { keylen = iw_in_key_full(skfd, ifname, args[i], key, &wrq.u.data.flags); if(keylen > 0) { wrq.u.data.length = keylen; wrq.u.data.pointer = (caddr_t) key; ++i; gotone++; } } /* -- Check for token index -- */ if((i < count) && (sscanf(args[i], "[%i]", &temp) == 1) && (temp > 0) && (temp < IW_ENCODE_INDEX)) { wrq.u.encoding.flags |= temp; ++i; gotone++; } /* -- Check the various flags -- */ if((i < count) && (!strcasecmp(args[i], "off"))) { wrq.u.data.flags |= IW_ENCODE_DISABLED; ++i; gotone++; } if((i < count) && (!strcasecmp(args[i], "open"))) { wrq.u.data.flags |= IW_ENCODE_OPEN; ++i; gotone++; } if((i < count) && (!strncasecmp(args[i], "restricted", 5))) { wrq.u.data.flags |= IW_ENCODE_RESTRICTED; ++i; gotone++; } if((i < count) && (!strncasecmp(args[i], "temporary", 4))) { wrq.u.data.flags |= IW_ENCODE_TEMP; ++i; gotone++; } } while(gotone != oldone); /* Pointer is absent in new API */ if(wrq.u.data.pointer == NULL) wrq.u.data.flags |= IW_ENCODE_NOKEY; /* Check if we have any invalid argument */ if(!gotone) { errarg = 0; return(IWERR_ARG_TYPE); } } if(iw_set_ext(skfd, ifname, SIOCSIWENCODE, &wrq) < 0) return(IWERR_SET_EXT); /* Var arg */ return(i); }
/* * Set Bit Rate */ static int set_bitrate_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int i = 1; wrq.u.bitrate.flags = 0; if(!strcasecmp(args[0], "auto")) { wrq.u.bitrate.value = -1; wrq.u.bitrate.fixed = 0; } else { if(!strcasecmp(args[0], "fixed")) { /* Get old bitrate */ if(iw_get_ext(skfd, ifname, SIOCGIWRATE, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.bitrate.fixed = 1; } else /* Should be a numeric value */ { double brate; char * unit; brate = strtod(args[0], &unit); if(unit == args[0]) { errarg = 0; return(IWERR_ARG_TYPE); } if(unit != NULL) { if(unit[0] == 'G') brate *= GIGA; if(unit[0] == 'M') brate *= MEGA; if(unit[0] == 'k') brate *= KILO; } wrq.u.bitrate.value = (long) brate; wrq.u.bitrate.fixed = 1; /* Check for an additional argument */ if((i < count) && (!strcasecmp(args[i], "auto"))) { wrq.u.bitrate.fixed = 0; ++i; } if((i < count) && (!strcasecmp(args[i], "fixed"))) { wrq.u.bitrate.fixed = 1; ++i; } if((i < count) && (!strcasecmp(args[i], "unicast"))) { wrq.u.bitrate.flags |= IW_BITRATE_UNICAST; ++i; } if((i < count) && (!strcasecmp(args[i], "broadcast"))) { wrq.u.bitrate.flags |= IW_BITRATE_BROADCAST; ++i; } } } if(iw_set_ext(skfd, ifname, SIOCSIWRATE, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i); }
/* * Set ESSID */ static int set_essid_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int i = 1; char essid[IW_ESSID_MAX_SIZE + 1]; int we_kernel_version; if((!strcasecmp(args[0], "off")) || (!strcasecmp(args[0], "any"))) { wrq.u.essid.flags = 0; essid[0] = '\0'; } else if(!strcasecmp(args[0], "on")) { /* Get old essid */ memset(essid, '\0', sizeof(essid)); wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = IW_ESSID_MAX_SIZE + 1; wrq.u.essid.flags = 0; if(iw_get_ext(skfd, ifname, SIOCGIWESSID, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.essid.flags = 1; } else { i = 0; /* '-' or '--' allow to escape the ESSID string, allowing * to set it to the string "any" or "off". * This is a big ugly, but it will do for now */ if((!strcmp(args[0], "-")) || (!strcmp(args[0], "--"))) { if(++i >= count) return(IWERR_ARG_NUM); } /* Check the size of what the user passed us to avoid * buffer overflows */ if(strlen(args[i]) > IW_ESSID_MAX_SIZE) { errmax = IW_ESSID_MAX_SIZE; return(IWERR_ARG_SIZE); } else { int temp; wrq.u.essid.flags = 1; strcpy(essid, args[i]); /* Size checked, all clear */ i++; /* Check for ESSID index */ if((i < count) && (sscanf(args[i], "[%i]", &temp) == 1) && (temp > 0) && (temp < IW_ENCODE_INDEX)) { wrq.u.essid.flags = temp; ++i; } } } /* Get version from kernel, device may not have range... */ we_kernel_version = iw_get_kernel_we_version(); /* Finally set the ESSID value */ wrq.u.essid.pointer = (caddr_t) essid; wrq.u.essid.length = strlen(essid); if(we_kernel_version < 21) wrq.u.essid.length++; if(iw_set_ext(skfd, ifname, SIOCSIWESSID, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i); }
/* * Set Modulation */ static int set_modulation_info(int skfd, char * ifname, char * args[], /* Command line args */ int count) /* Args count */ { struct iwreq wrq; int i = 1; /* Avoid "Unused parameter" warning */ args = args; count = count; if(!strcasecmp(args[0], "auto")) wrq.u.param.fixed = 0; /* i.e. use any modulation */ else { if(!strcasecmp(args[0], "fixed")) { /* Get old modulation */ if(iw_get_ext(skfd, ifname, SIOCGIWMODUL, &wrq) < 0) return(IWERR_GET_EXT); wrq.u.param.fixed = 1; } else { int k; /* Allow multiple modulations, combine them together */ wrq.u.param.value = 0x0; i = 0; do { for(k = 0; k < IW_SIZE_MODUL_LIST; k++) { if(!strcasecmp(args[i], iw_modul_list[k].cmd)) { wrq.u.param.value |= iw_modul_list[k].mask; ++i; break; } } } /* For as long as current arg matched and not out of args */ while((i < count) && (k < IW_SIZE_MODUL_LIST)); /* Check we got something */ if(i == 0) { errarg = 0; return(IWERR_ARG_TYPE); } /* Check for an additional argument */ if((i < count) && (!strcasecmp(args[i], "auto"))) { wrq.u.param.fixed = 0; ++i; } if((i < count) && (!strcasecmp(args[i], "fixed"))) { wrq.u.param.fixed = 1; ++i; } } } if(iw_set_ext(skfd, ifname, SIOCSIWMODUL, &wrq) < 0) return(IWERR_SET_EXT); /* Var args */ return(i); }