📄 cardif_linux.c
字号:
// We set the key index to 0x80, to force key 0 to be set to all 0s, // and to have key 0 be set as the default transmit key. set_wireless_key(thisint, (char *)&zerokey, keylen, 0x80); set_wireless_key(thisint, (char *)&zerokey, keylen, 0x01); set_wireless_key(thisint, (char *)&zerokey, keylen, 0x02); set_wireless_key(thisint, (char *)&zerokey, keylen, 0x03);}/************************************************************** * * Tell the wireless card to start scanning for wireless networks. * **************************************************************/int cardif_start_wireless_scan(struct interface_data *thisint){#ifdef EXPERIMENTAL struct lin_sock_data *sockData; struct iwreq iwr; if (!(thisint->flags & IS_WIRELESS)) { debug_printf(DEBUG_INT, "%s is not a wireless interface!\n", thisint->intName); return XENOWIRELESS; } if (thisint->flags & SCANNING) { debug_printf(DEBUG_INT, "Already scanning!\n"); return XENONE; } sockData = thisint->sockData; if (sockData->scan_timer > 0) { debug_printf(DEBUG_INT, "Scan timer has not expired! Not scanning!\n"); return XENONE; } debug_printf(DEBUG_INT, "Issuing scan request for interface %s!\n", thisint->intName); // Clear out our active list of SSIDs. cardif_linux_ssids_clear(thisint); iwr.u.param.flags = IW_SCAN_DEFAULT; iwr.u.param.value = 0; strcpy((char *)&iwr.ifr_name, thisint->intName); if (ioctl(sockData->sockInt, SIOCSIWSCAN, &iwr) < 0) { debug_printf(DEBUG_NORMAL, "Error with SCAN ioctl! (Perhaps your card doesn't support scanning?)\n"); return -1; } debug_printf(DEBUG_EVERYTHING, "Flags before setting scan flag : %02X\n", thisint->flags); SET_FLAG(thisint->flags, SCANNING); // We are scanning. debug_printf(DEBUG_EVERYTHING, "Flags after setting scan flag : %02X\n", thisint->flags); sockData->scan_timer = SCAN_TIMEOUT;#endif return XENONE;}/************************************************************** * * Check to see if the BSSID value is valid. If it is, return TRUE. If * it isn't return FALSE. * **************************************************************/int cardif_valid_dest(struct interface_data *thisint){ char baddest[6], newdest[6]; if ((thisint->flags & IS_WIRELESS) && (GetBSSID(thisint, (char *)&newdest) == XENONE)) { memset((char *)&baddest, 0x00, 6); if (memcmp(thisint->dest_mac, baddest, 6) == 0) { debug_printf(DEBUG_INT, "All 0s for dest mac!\n");#ifdef EXPERIMENTAL cardif_start_wireless_scan(thisint);#endif return FALSE; } memset((char *)&baddest, 0x44, 6); if (memcmp(thisint->dest_mac, baddest, 6) == 0) { debug_printf(DEBUG_INT, "All 4s for dest mac!\n");#ifdef EXPERIMENTAL cardif_start_wireless_scan(thisint);#endif return FALSE; } memset((char *)&baddest, 0xff, 6); if (memcmp(thisint->dest_mac, baddest, 6) == 0) { debug_printf(DEBUG_INT, "All Fs for dest mac!\n");#ifdef EXPERIMENTAL cardif_start_wireless_scan(thisint);#endif return FALSE; } } return TRUE;}/************************************************************** * * If we determine that this interface is a wireless interface, then * we should call this, to have the destination address changed to the * AP that we are talking to. Otherwise, we will always send frames to * the multicast address, instead of the AP. (And, most APs won't answer * to the multicast address.) * **************************************************************/int cardif_check_dest(struct interface_data *thisint){ char newdest[6], *newssid; int changed = FALSE; bzero((char *)&newdest, 6); // If we are on wireless, figure out the target MAC address. if ((thisint->flags & IS_WIRELESS) && (GetBSSID(thisint, (char *)&newdest) == XENONE)) { if (memcmp(thisint->dest_mac, newdest, 6) != 0) { debug_printf(DEBUG_INT, "The card reported that the destination MAC address is now "); debug_hex_printf(DEBUG_INT, (char *)&newdest, 6); memcpy((char *)&thisint->dest_mac[0], (char *)&newdest, 6); changed = TRUE; // Since we changed destination addresses, we need to see if // we should reset keys. cardif_reset_keys(thisint); } if (cardif_valid_dest(thisint) == FALSE) { cardif_reset_keys(thisint); if ((!(thisint->flags & SCANNING)) && (!(thisint->flags & SCAN_DONE))) cardif_start_wireless_scan(thisint); if (thisint->flags & SCAN_DONE) cardif_linux_ssids_join_network(thisint); } // If we were able to get a BSSID, we should also try to get an SSID. newssid = malloc(100); if (newssid == NULL) { debug_printf(DEBUG_NORMAL, "Couldn't malloc newssid in cardif_linux.\n"); return XEMALLOC; } bzero(newssid, 100); GetSSID(thisint, newssid); if ((thisint->cur_essid == NULL) || (strncmp(newssid, thisint->cur_essid, 100) != 0)) { if (thisint->cur_essid != NULL) free(thisint->cur_essid); thisint->cur_essid = newssid; debug_printf(DEBUG_INT, "Working with ESSID : %s\n", thisint->cur_essid); } else { if (newssid != NULL) { free(newssid); newssid = NULL; } } } return changed;}/****************************************** * * Return the socket number for functions that need it. * ******************************************/int cardif_get_socket(struct interface_data *thisint){ struct lin_sock_data *sockData; sockData = thisint->sockData; return sockData->sockInt;}/****************************************** * * Clean up anything that was created during the initialization and operation * of the interface. This will be called before the program terminates. * ******************************************/int cardif_deinit(struct interface_data *thisint){ struct ifreq ifr; uint16_t int16; struct lin_sock_data *sockData; sockData = thisint->sockData; debug_printf(DEBUG_EVERYTHING, "Cleaning up interface %s...\n",thisint->intName); // Check if we want ALLMULTI mode, and enable it. if (config_get_allmulti() == 1) { // Tell the ifreq struct which interface we want to use. strncpy((char *)&ifr.ifr_name, thisint->intName, sizeof(ifr.ifr_name)); if (ioctl(sockData->sockInt, SIOCGIFFLAGS, &ifr) < 0) { debug_printf(DEBUG_NORMAL, "Couldn't get interface flags!\n"); } else { // Check if allmulti was disabled when we started. If it was, // then disable it again, so everything is good. if (!(thisint->flags & ALLMULTI)) { debug_printf(DEBUG_INT, "Turning off ALLMULTI mode!\n"); int16 = ifr.ifr_flags; // ANDing the flags with 0xfdff will turn off the ALLMULTI flag. ifr.ifr_flags = (int16 & 0xfdff); if (ioctl(sockData->sockInt, SIOCSIFFLAGS, &ifr) < 0) { debug_printf(DEBUG_NORMAL, "Couldn't set ALLMULTI mode on this interface! We will continue anyway!\n"); } } } } close(sockData->sockInt); // Now clean up the memory. if (thisint->sockData != NULL) { free(thisint->sockData); thisint->sockData = NULL; } return XENONE;}/****************************************** * * Set a wireless key. Also, based on the index, we may change the transmit * key. * ******************************************/int set_wireless_key(struct interface_data *thisint, u_char *key, int keylen, int index){ int rc = 0; int skfd; struct iwreq wrq; if (!(thisint->flags & IS_WIRELESS)) { if ((cardif_int_is_wireless(thisint->intName) != TRUE) || (thisint->userdata->type == WIRED) || (thisint->userdata->wireless_ctrl == CTL_NO)) { debug_printf(DEBUG_NORMAL, "Interface isn't wireless, but an attempt to set a key was made!\n"); return XENOWIRELESS; } else { thisint->flags |= IS_WIRELESS; } } skfd = socket(AF_INET, SOCK_DGRAM, 0); if (skfd < 0) return -1; strncpy(wrq.ifr_name, thisint->intName, IFNAMSIZ); wrq.u.data.flags = ((index & 0x7f) + 1) & IW_ENCODE_INDEX; wrq.u.data.flags |= IW_ENCODE_OPEN; wrq.u.data.length = keylen; wrq.u.data.pointer = (caddr_t)key; if ((rc = ioctl(skfd, SIOCSIWENCODE, &wrq)) < 0) { debug_printf(DEBUG_NORMAL, "Failed to set WEP key [%d], error %d : %s\n", (index & 0x7f) + 1, errno, strerror(errno)); rc = XENOKEYSUPPORT; } else { debug_printf(DEBUG_INT, "Successfully set WEP key [%d]\n", (index & 0x7f)+1); if (index & 0x80) { // This is a unicast key, use it for transmissions. strncpy(wrq.ifr_name, thisint->intName, IFNAMSIZ); wrq.u.data.flags = (((index & 0x7f) + 1) & IW_ENCODE_INDEX) | IW_ENCODE_NOKEY; wrq.u.data.flags |= IW_ENCODE_OPEN; wrq.u.data.length = 0; wrq.u.data.pointer = (caddr_t)NULL; if (ioctl(skfd, SIOCSIWENCODE, &wrq) < 0) { debug_printf(DEBUG_NORMAL, "Failed to set the WEP transmit key ID [%d]\n", (index & 0x7f)+1); rc = XENOKEYSUPPORT; } else { debug_printf(DEBUG_INT, "Successfully set the WEP transmit key [%d]\n", (index & 0x7f)+1); } } } close(skfd); return rc;}/********************************************************** * * Set the SSID of the wireless card. * **********************************************************/int SetSSID(struct interface_data *thisint, char *ssid_name){ struct iwreq iwr; struct lin_sock_data *sockData; if (thisint == NULL) { debug_printf(DEBUG_NORMAL, "Invalid interface struct passed in to %s!\n", __FUNCTION__); return XEGENERROR; } sockData = thisint->sockData; if (!(thisint->flags & IS_WIRELESS)) { // 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. if (!(thisint->flags & WAS_DOWN)) { return XENOWIRELESS; } } // If we get here, and isWireless == FALSE, then we need to double // check that our interface is really not wireless. if (!(thisint->flags & IS_WIRELESS)) { if (cardif_int_is_wireless(thisint->intName) == TRUE) { thisint->flags |= IS_WIRELESS; } else { thisint->flags &= (~IS_WIRELESS); } if (!(thisint->flags & IS_WIRELESS)) { thisint->flags &= (~WAS_DOWN); } } // Specify the interface name we are asking about. strncpy(iwr.ifr_name, thisint->intName, sizeof(iwr.ifr_name)); iwr.u.essid.pointer = (caddr_t) ssid_name; iwr.u.essid.length = strlen(ssid_name); iwr.u.essid.flags = 1; if (ioctl(sockData->sockInt, SIOCSIWESSID, &iwr) < 0) return XENOWIRELESS; thisint->flags &= (~WAS_DOWN); return XENONE;}/****************************************** * * Do whatever we need to do in order to associate based on the flags in * the ssids_list struct. * ******************************************/void cardif_associate(struct interface_data *intdata, void *newssid){#ifdef EXPERIMENTAL struct ssids_list *ssid; ssid = (struct ssids_list *)newssid; if (intdata == NULL) { debug_printf(DEBUG_NORMAL, "Invalid interface struct passed to %s!\n", __FUNCTION__); return; } if (ssid == NULL) { debug_printf(DEBUG_NORMAL, "Invalid SSID information passed to %s!\n", __FUNCTION__); return; } debug_printf(DEBUG_INT, "Setting SSID to %s\n", ssid->ssidName); if (ssid->flags & WEP) { debug_printf(DEBUG_INT, "Setting open encryption.\n"); cardif_force_enc_on_open(intdata); } if (ssid->flags & WPA_IE) { debug_printf(DEBUG_INT, "Enabling WPA!\n"); cardif_linux_wpa_set_wpa_ie(intdata, NULL, 0); cardif_linux_wpa_enable(intdata); } // Otherwise, set the SSID. if (SetSSID(intdata, ssid->ssidName) != XENONE) { debug_printf(DEBUG_NORMAL, "Couldn't associate to %s!\n", ssid->ssidName); return; }#endif}/****************************************** *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -