📄 cardif_linux_wext.c
字号:
if (!xsup_assert((ssid_name != NULL), "ssid_name != NULL", FALSE))
return XEGENERROR;
if (!xsup_assert((thisint->intTypeData != NULL), "thisint->intTypeData != NULL",
FALSE))
return XEGENERROR;
wctx = (wireless_ctx *)thisint->intTypeData;
memset(&iwr, 0x00, sizeof(iwr));
sockData = thisint->sockData;
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
if (sockData->sockInt <= 0)
return XENOSOCK;
if (thisint->intType != ETH_802_11_INT)
{
return XENOWIRELESS;
}
// Specify the interface name we are asking about.
Strncpy(iwr.ifr_name, sizeof(iwr.ifr_name), thisint->intName,
strlen(thisint->intName)+1);
if (strlen(iwr.ifr_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
memset(newssid, 0x00, 100);
strcpy(newssid, ssid_name);
iwr.u.essid.pointer = (caddr_t) newssid;
iwr.u.essid.length = strlen(newssid);
// Starting in WE 21, we don't NULL terminate SSIDs we set.
if (cardif_linux_rtnetlink_get_we_ver(thisint) < 21)
iwr.u.essid.length++;
iwr.u.essid.flags = 1;
if (ioctl(sockData->sockInt, SIOCSIWESSID, &iwr) < 0) return XENOWIRELESS;
// Allow us to correlate SSID set events.
SET_FLAG(wctx->flags, WIRELESS_SM_SSID_CHANGE);
debug_printf(DEBUG_INT, "Requested SSID be set to '%s'\n", newssid);
UNSET_FLAG(thisint->flags, WAS_DOWN);
return XENONE;
}
/**
* Set the Broadcast SSID (MAC address) of the AP we are connected to.
**/
int cardif_linux_wext_set_bssid(context *intdata, uint8_t *bssid)
{
struct iwreq wrq;
struct lin_sock_data *sockData;
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEGENERROR;
if (!xsup_assert((bssid != NULL), "bssid != NULL", FALSE))
return XEGENERROR;
debug_printf(DEBUG_INT, "Setting BSSID : ");
debug_hex_printf(DEBUG_INT, bssid, 6);
sockData = intdata->sockData;
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
memset(&wrq, 0x00, sizeof(wrq));
Strncpy((char *)&wrq.ifr_name, sizeof(wrq.ifr_name), intdata->intName,
strlen(intdata->intName)+1);
if (strlen(wrq.ifr_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
memcpy(&wrq.u.ap_addr.sa_data, bssid, 6);
wrq.u.ap_addr.sa_family = ARPHRD_ETHER;
if (ioctl(sockData->sockInt, SIOCSIWAP, &wrq) < 0)
{
// If we couldn't set the BSSID, it isn't the end of the world. The
// driver just may not need it.
debug_printf(DEBUG_NORMAL, "Error setting BSSID! We may not associate/"
"authenticate correctly!\n");
return XESOCKOP;
}
return XENONE;
}
/**
* Get the Broadcast SSID (MAC address) of the Access Point we are connected
* to. If this is not a wireless card, or the information is not available,
* we should return an error.
**/
int cardif_linux_wext_get_bssid(context *thisint,
char *bssid_dest)
{
struct iwreq iwr;
struct lin_sock_data *sockData;
if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))
return XEMALLOC;
if (!xsup_assert((bssid_dest != NULL), "bssid_dest != NULL", FALSE))
return XEMALLOC;
// If we are a wired interface, don't bother.
if (thisint->intType != ETH_802_11_INT) return XENONE;
sockData = thisint->sockData;
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
memset(&iwr, 0x00, sizeof(iwr));
// Specify the interface name we are asking about.
Strncpy(iwr.ifr_name, sizeof(iwr.ifr_name), thisint->intName,
strlen(thisint->intName)+1);
if (strlen(iwr.ifr_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
if (ioctl(sockData->sockInt, SIOCGIWAP, &iwr) < 0)
{
debug_printf(DEBUG_NORMAL, "Couldn't get MAC address for AP!\n");
return XENOWIRELESS;
}
memcpy(bssid_dest, iwr.u.ap_addr.sa_data, 6);
return XENONE;
}
/**
* Ask the wireless card for the ESSID that we are currently connected to. If
* this is not a wireless card, or the information is not available, we should
* return an error.
**/
int cardif_linux_wext_get_ssid(context *thisint, char *ssid_name,
unsigned int ssid_len)
{
struct iwreq iwr;
struct lin_sock_data *sockData;
char newssid[100];
if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))
return XEMALLOC;
if (!xsup_assert((ssid_name != NULL), "ssid_name != NULL", FALSE))
return XEMALLOC;
sockData = thisint->sockData;
memset(&iwr, 0x00, sizeof(iwr));
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
if (thisint->intType != ETH_802_11_INT)
{
// We want to verify that the interface is in fact, not wireless, and
// not that we are in a situation where the interface has just been
// down.
debug_printf(DEBUG_NORMAL, "This interface isn't wireless!\n");
return XENOWIRELESS;
}
// Specify the interface name we are asking about.
if (strlen(thisint->intName) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
Strncpy(iwr.ifr_name, sizeof(iwr.ifr_name), thisint->intName,
sizeof(iwr.ifr_name)+1);
memset(newssid, 0x00, IW_ESSID_MAX_SIZE+1);
iwr.u.essid.pointer = (caddr_t) newssid;
iwr.u.essid.length = 100;
iwr.u.essid.flags = 0;
if (ioctl(sockData->sockInt, SIOCGIWESSID, &iwr) < 0)
{
debug_printf(DEBUG_NORMAL, "Couldn't get ESSID!\n");
debug_printf(DEBUG_NORMAL, "Error (%d) : %s\n", errno, strerror(errno));
return XENOWIRELESS;
}
UNSET_FLAG(thisint->flags, WAS_DOWN);
Strncpy(ssid_name, ssid_len, newssid, iwr.u.essid.length+1);
if (strlen(ssid_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
return XENONE;
}
/**
* This function is called when we roam, or disassociate. It should
* reset the card to a state where it can associate with a new AP.
**/
int cardif_linux_wext_wep_associate(context *intdata,
int zero_keys)
{
uint8_t *bssid;
struct config_globals *globals;
wireless_ctx *wctx = NULL;
uint32_t alg;
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
if (!xsup_assert((intdata->intTypeData != NULL), "intdata->intTypeData != NULL",
FALSE))
return XEMALLOC;
wctx = (wireless_ctx *)intdata->intTypeData;
cardif_linux_wext_wpa_state(intdata, 0);
#if WIRELESS_EXT > 17
// Determine the type of association we want, and set it up if we are using
// the proper versions of WEXT.
switch (intdata->conn->association.association_type)
{
case ASSOC_TYPE_WPA1:
case ASSOC_TYPE_WPA2:
case ASSOC_TYPE_OPEN:
alg = IW_AUTH_ALG_OPEN_SYSTEM;
break;
case ASSOC_TYPE_SHARED:
alg = IW_AUTH_ALG_SHARED_KEY;
break;
case ASSOC_TYPE_LEAP:
alg = IW_AUTH_ALG_LEAP;
break;
default:
debug_printf(DEBUG_NORMAL, "Unknown 802.11 authetication alg. Defaulting "
"to Open System.\n");
debug_printf(DEBUG_NORMAL, "Type was %d.\n", intdata->conn->association.association_type);
alg = IW_AUTH_ALG_OPEN_SYSTEM;
break;
}
if (cardif_linux_wext_set_iwauth(intdata, IW_AUTH_80211_AUTH_ALG,
alg, "802.11 auth. alg to open") < 0)
{
debug_printf(DEBUG_NORMAL, "Couldn't set 802.11 auth. alg.\n");
}
#endif
cardif_linux_wext_set_ssid(intdata, wctx->cur_essid);
if (zero_keys == 0)
{
debug_printf(DEBUG_INT, "WEP: turning encryption off.\n");
return cardif_linux_wext_enc_disable(intdata);
} else if (zero_keys == 1)
{
debug_printf(DEBUG_INT, "WEP: zero keys.\n");
cardif_linux_wext_zero_keys(intdata);
return XENONE;
} else
{
debug_printf(DEBUG_NORMAL, "Invalid association value.\n");
}
bssid = config_ssid_get_mac(wctx);
if (bssid != NULL)
{
debug_printf(DEBUG_INT, "Dest. BSSID : ");
debug_hex_printf(DEBUG_INT, bssid, 6);
}
globals = config_get_globals();
if ((!globals) || (!TEST_FLAG(globals->flags, CONFIG_GLOBALS_FIRMWARE_ROAM)))
{
cardif_linux_wext_set_bssid(intdata, bssid);
}
return XENOTHING_TO_DO;
}
#if WIRELESS_EXT > 17
int cardif_linux_wext_mlme(context *thisint, uint16_t mlme_type,
uint16_t mlme_reason)
{
struct iwreq iwr;
struct lin_sock_data *sockData;
struct iw_mlme iwm;
if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))
return XEMALLOC;
sockData = thisint->sockData;
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
// If we get here, and isWireless == FALSE, then we need to double
// check that our interface is really not wireless.
if (thisint->intType != ETH_802_11_INT)
{
return XENOWIRELESS;
}
memset(&iwr, 0, sizeof(iwr));
// Specify the interface name we are asking about.
Strncpy(iwr.ifr_name, sizeof(iwr.ifr_name), thisint->intName,
sizeof(iwr.ifr_name)+1);
if (strlen(iwr.ifr_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
memset(&iwm, 0, sizeof(iwm));
// Set up our MLME struct.
iwm.cmd = mlme_type;
iwm.reason_code = mlme_reason;
iwm.addr.sa_family = ARPHRD_ETHER;
// Need to specify the MAC address that we want to do this MLME for.
memcpy(iwm.addr.sa_data, thisint->source_mac, 6);
iwr.u.data.pointer = (caddr_t)&iwm;
iwr.u.data.length = sizeof(iwm);
if (ioctl(sockData->sockInt, SIOCSIWMLME, &iwr))
{
debug_printf(DEBUG_NORMAL, "Couldn't issue MLME request!\n");
}
return XENONE;
}
#endif
// Forward decl to keep the compiler from screaming.
void cardif_linux_clear_keys(context *);
int cardif_linux_wext_disassociate(context *intdata, int reason)
{
#if WIRELESS_EXT > 17
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
cardif_linux_wext_mlme(intdata, IW_MLME_DEAUTH, reason);
cardif_linux_wext_mlme(intdata, IW_MLME_DISASSOC, reason);
#endif
cardif_linux_wext_set_ssid(intdata, "unlikelyssid");
cardif_linux_clear_keys(intdata);
return XENONE;
}
int cardif_linux_wext_set_key_ext(context *intdata, int alg,
unsigned char *addr, int keyidx, int settx,
char *seq, int seqlen, char *key,
int keylen)
{
#if WIRELESS_EXT > 17
struct iwreq wrq;
struct lin_sock_data *sockData;
struct iw_encode_ext *iwee;
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
sockData = intdata->sockData;
xsup_assert((sockData != NULL), "sockData != NULL", TRUE);
memset(&wrq, 0x00, sizeof(wrq));
Strncpy((char *)&wrq.ifr_name, sizeof(wrq.ifr_name), intdata->intName,
sizeof(intdata->intName)+1);
if (strlen(wrq.ifr_name) == 0)
{
debug_printf(DEBUG_NORMAL, "Invalid interface name in %s():%d\n",
__FUNCTION__, __LINE__);
return XEGENERROR;
}
// Allocate enough memory to hold our iw_encode_ext struct, and the
// key itself.
iwee = (struct iw_encode_ext *)Malloc(sizeof(struct iw_encode_ext) + keylen);
if (iwee == NULL)
{
debug_printf(DEBUG_NORMAL,
"Error with malloc of iwee in cardif_linux_wext_set_key_ext()\n");
return XEMALLOC;
}
iwee->alg = alg;
iwee->ext_flags = keyidx+1;
if ((seq != NULL) && (seqlen > 0))
{
printf("Setting seq!\n");
iwee->ext_flags |= IW_ENCODE_EXT_RX_SEQ_VALID;
memcpy(iwee->rx_seq, seq, seqlen);
}
if (settx)
{
printf("TX Key!\n");
iwee->ext_flags |= IW_ENCODE_EXT_SET_TX_KEY;
if ((addr != NULL) &&
(memcmp(addr, "\0xff\0xff\0xff\0xff\0xff\0xff", 6) != 0))
{
memcpy(iwee->addr.sa_data, addr, 6);
} else {
memcpy(iwee->addr.sa_data, intdata->dest_mac, 6);
}
} else {
iwee->ext_flags |= IW_ENCODE_EXT_GROUP_KEY;
memset(iwee->addr.sa_data, 0xff, 6);
}
iwee->addr.sa_family = ARPHRD_ETHER;
if (keylen != 0)
{
iwee->key_len = keylen;
memcpy(iwee->key, key, keylen);
printf("Key (%d) : \n", iwee->key_len);
debug_hex_printf(DEBUG_NORMAL, iwee->key, iwee->key_len);
}
debug_printf(DEBUG_INT, "Key Index : %d Length : %d\n", keyidx, keylen);
debug_printf(DEBUG_INT, "Destination MAC : ");
debug_hex_printf(DEBUG_INT, (uint8_t *)iwee->addr.sa_data, 6);
debug_printf(DEBUG_INT, "Setting key : ");
debug_hex_printf(DEBUG_INT, iwee->key, keylen);
wrq.u.encoding.pointer = (caddr_t)iwee;
wrq.u.encoding.flags = (keyidx + 1);
if (alg == IW_ENCODE_ALG_NONE)
wrq.u.encoding.flags |= (IW_ENCODE_DISABLED | IW_ENCODE_NOKEY);
wrq.u.encoding.length = sizeof(struct iw_encode_ext) + keylen +1;
if (ioctl(sockData->sockInt, SIOCSIWENCODEEXT, &wrq) < 0)
{
debug_printf(DEBUG_NORMAL, "Error setting key!! (IOCTL "
"failure.)\n");
debug_printf(DEBUG_NORMAL, "Error %d : %s\n", errno, strerror(errno));
}
FREE(iwee);
#else
debug_printf(DEBUG_NORMAL, "%s : Not supported by WE(%d)!\n", __FUNCTION__,
WIRELESS_EXT);
#endif
return XENONE;
}
int cardif_linux_wext_set_tkip_key(context *intdata,
unsigned char *addr, int keyidx, int settx,
char *key, int keylen)
{
#if WIRELESS_EXT > 17
char seq[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
return cardif_linux_wext_set_key_ext(intdata, IW_ENCODE_ALG_TKIP, addr,
keyidx, settx, seq, 6, key, keylen);
#else
debug_printf(DEBUG_NORMAL, "%s : Not supported by WE(%d)!\n", __FUNCTION__,
WIRELESS_EXT);
#endif
return XENONE;
}
int cardif_linux_wext_set_ccmp_key(context *intdata,
unsigned char *addr, int keyidx, int settx,
char *key, int keylen)
{
#if WIRELESS_EXT > 17
char seq[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
// According to 802.11i, section 8.3.3.4.3e says we should set the PN to
// 0 when a CCMP key is set.
return cardif_linux_wext_set_key_ext(intdata, IW_ENCODE_ALG_CCMP, addr,
keyidx, settx, seq, 6, key,
keylen);
#else
debug_printf(DEBUG_NORMAL, "%s : Not supported by WE(%d)!\n", __FUNCTION__,
WIRELESS_EXT);
#endif
return XENONE;
}
int cardif_linux_wext_wpa(context *intdata, char state)
{
#if WIRELESS_EXT > 17
int retval = 0;
if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))
return XEMALLOC;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -