blob: 6a3fcc0043efdbaa32575b92572565d150446497 [file] [log] [blame]
#include <ctype.h>
#include <netlink/attr.h>
#include <errno.h>
#include <stdbool.h>
#include "iw.h"
#include "nl80211.h"
void mac_addr_n2a(char *mac_addr, unsigned char *arg)
{
int i, l;
l = 0;
for (i = 0; i < ETH_ALEN ; i++) {
if (i == 0) {
sprintf(mac_addr+l, "%02x", arg[i]);
l += 2;
} else {
sprintf(mac_addr+l, ":%02x", arg[i]);
l += 3;
}
}
}
int mac_addr_a2n(unsigned char *mac_addr, char *arg)
{
int i;
for (i = 0; i < ETH_ALEN ; i++) {
int temp;
char *cp = strchr(arg, ':');
if (cp) {
*cp = 0;
cp++;
}
if (sscanf(arg, "%x", &temp) != 1)
return -1;
if (temp < 0 || temp > 255)
return -1;
mac_addr[i] = temp;
if (!cp)
break;
arg = cp;
}
if (i < ETH_ALEN - 1)
return -1;
return 0;
}
static const char *ifmodes[NL80211_IFTYPE_MAX + 1] = {
"unspecified",
"IBSS",
"managed",
"AP",
"AP/VLAN",
"WDS",
"monitor",
"mesh point"
};
static char modebuf[100];
const char *iftype_name(enum nl80211_iftype iftype)
{
if (iftype <= NL80211_IFTYPE_MAX)
return ifmodes[iftype];
sprintf(modebuf, "Unknown mode (%d)", iftype);
return modebuf;
}
static const char *commands[NL80211_CMD_MAX + 1] = {
"unspecified",
"get_wiphy",
"set_wiphy",
"new_wiphy",
"del_wiphy",
"get_interface",
"set_interface",
"new_interface",
"del_interface",
"get_key",
"set_key",
"new_key",
"del_key",
"get_beacon",
"set_beacon",
"new_beacon",
"del_beacon",
"get_station",
"set_station",
"new_station",
"del_station",
"get_mpath",
"set_mpath",
"new_mpath",
"del_mpath",
"set_bss",
"set_reg",
"reg_set_reg",
"get_mesh_params",
"set_mesh_params",
"set_mgmt_extra_ie",
"get_reg",
"get_scan",
"trigger_scan",
"new_scan_results",
"scan_aborted",
"reg_change",
"authenticate",
"associate",
"deauthenticate",
"disassociate",
"michael_mic_failure",
"reg_beacon_hint",
"join_ibss",
"leave_ibss",
"testmode",
"connect",
"roam",
"disconnect",
"set_wiphy_netns"
};
static char cmdbuf[100];
const char *command_name(enum nl80211_commands cmd)
{
if (cmd <= NL80211_CMD_MAX)
return commands[cmd];
sprintf(cmdbuf, "Unknown command (%d)", cmd);
return cmdbuf;
}
int ieee80211_channel_to_frequency(int chan)
{
if (chan < 14)
return 2407 + chan * 5;
if (chan == 14)
return 2484;
/* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
return (chan + 1000) * 5;
}
int ieee80211_frequency_to_channel(int freq)
{
if (freq == 2484)
return 14;
if (freq < 2484)
return (freq - 2407) / 5;
/* FIXME: dot11ChannelStartingFactor (802.11-2007 17.3.8.3.2) */
return freq/5 - 1000;
}
void print_ssid_escaped(const uint8_t len, const uint8_t *data)
{
int i;
for (i = 0; i < len; i++) {
if (isprint(data[i]))
printf("%c", data[i]);
else
printf("\\x%.2x", data[i]);
}
}
static int hex2num(char digit)
{
if (!isxdigit(digit))
return -1;
if (isdigit(digit))
return digit - '0';
return tolower(digit) - 'a' + 10;
}
static int hex2byte(char *hex)
{
int d1, d2;
d1 = hex2num(hex[0]);
if (d1 < 0)
return -1;
d2 = hex2num(hex[1]);
if (d2 < 0)
return -1;
return (d1 << 4) | d2;
}
static char *hex2bin(char *hex, char *buf)
{
char *result = buf;
int d;
while (hex[0]) {
d = hex2byte(hex);
if (d < 0)
return NULL;
buf[0] = d;
buf++;
hex += 2;
}
return result;
}
int parse_keys(struct nl_msg *msg, char **argv, int argc)
{
struct nlattr *keys;
int i = 0;
bool have_default = false;
char keybuf[13];
if (!argc)
return 1;
NLA_PUT_FLAG(msg, NL80211_ATTR_PRIVACY);
keys = nla_nest_start(msg, NL80211_ATTR_KEYS);
if (!keys)
return -ENOBUFS;
do {
char *arg = *argv;
int pos = 0, keylen;
struct nlattr *key = nla_nest_start(msg, ++i);
char *keydata;
if (!key)
return -ENOBUFS;
if (arg[pos] == 'd') {
NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
pos++;
if (arg[pos] == ':')
pos++;
have_default = true;
}
if (!isdigit(arg[pos]))
goto explain;
NLA_PUT_U8(msg, NL80211_KEY_IDX, arg[pos++] - '0');
if (arg[pos++] != ':')
goto explain;
keydata = arg + pos;
switch (strlen(keydata)) {
case 10:
keydata = hex2bin(keydata, keybuf);
case 5:
NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC01);
keylen = 5;
break;
case 26:
keydata = hex2bin(keydata, keybuf);
case 13:
NLA_PUT_U32(msg, NL80211_KEY_CIPHER, 0x000FAC05);
keylen = 13;
break;
default:
goto explain;
}
if (!keydata)
goto explain;
NLA_PUT(msg, NL80211_KEY_DATA, keylen, keydata);
argv++;
argc--;
/* one key should be TX key */
if (!have_default && !argc)
NLA_PUT_FLAG(msg, NL80211_KEY_DEFAULT);
nla_nest_end(msg, key);
} while (argc);
nla_nest_end(msg, keys);
return 0;
nla_put_failure:
return -ENOBUFS;
explain:
fprintf(stderr, "key must be [d:]index:data where\n"
" 'd:' means default (transmit) key\n"
" 'index:' is a single digit (0-3)\n"
" 'data' must be 5 or 13 ascii chars\n"
" or 10 or 26 hex digits\n"
"for example: d:2:6162636465 is the same as d:2:abcde\n");
return 2;
}