⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cardif_linux_rtnetlink.c

📁 Linux上的802.1x 的supplicant的实现。很多supplicant程序都是基于它开发的
💻 C
📖 第 1 页 / 共 4 页
字号:
  buffer = (char *)malloc(buf_size);  if (!buffer)    {      debug_printf(DEBUG_NORMAL, "Couldn't allocate memory for scan buffer!"		   "\n");      return XEMALLOC;    }  iwr.u.data.pointer = buffer;  iwr.u.data.flags = 0;  iwr.u.data.length = buf_size;  Strncpy(iwr.ifr_name, idata->intName, sizeof(iwr.ifr_name));    if (ioctl(sockData->sockInt, SIOCGIWSCAN, &iwr) < 0)    {      if (errno == E2BIG)	{	  // Our return results are too big for our default buffer.  So,	  // allocate more and try again!	  	  free(buffer);	  buf_size *= 2;	  buffer = (char *)malloc(buf_size);	  if (!buffer)	    {	      debug_printf(DEBUG_NORMAL, "Couldn't allocate memory for scan "			   "buffer!\n");	      return XEMALLOC;	    }	  iwr.u.data.pointer = buffer;	  iwr.u.data.length = buf_size;	  while (ioctl(sockData->sockInt, SIOCGIWSCAN, &iwr) < 0)	    {	      free(buffer);	      	      if (buf_size > 60000)		{		  debug_printf(DEBUG_NORMAL, "Buffer size to allocate has "			       "become unreasonable!  (If you really have "			       "that many SSIDs, you won't get much data "			       "across the network anyway!)\n");		  return -1;		}	      buf_size *= 2;	      buffer = (char *)malloc(buf_size);	      if (!buffer)		{		  debug_printf(DEBUG_NORMAL, "Couldn't allocate memory for scan buffer!\n");		  return XEMALLOC;		}	      iwr.u.data.pointer = buffer;	      iwr.u.data.length = buf_size;	    }	}      else	{	  if (errno == EAGAIN)	    {	      debug_printf(DEBUG_INT, "No data available! (%s)\n", 			   strerror(errno));	      return XENONE;	    }	  else	    {	      debug_printf(DEBUG_NORMAL, "Error with scan results!\n");	      debug_printf(DEBUG_NORMAL, "Error was : %s\n", strerror(errno));	      UNSET_FLAG(idata->flags, SCANNING);	      return -1;	    }	}    }  debug_printf(DEBUG_NORMAL, "Scan complete.\n");  // Cancel the scancheck timer, so it doesn't continue to fire.  timer_cancel(SCANCHECK_TIMER);  // Then harvest the data.  debug_printf(DEBUG_INT, "Reaping data. (Size : %d)\n", iwr.u.data.length);  debug_hex_dump(DEBUG_INT, (uint8_t *)buffer, iwr.u.data.length);  cardif_linux_rtnetlink_reap(idata, (char *)buffer, iwr.u.data.length);  UNSET_FLAG(idata->flags, SCANNING);  // Clean up after ourselves.  free(buffer);  buffer = NULL;  return XDATA;}/*********************************************************** * * Check the MAC that we were given.  If it is all 0s, 4s, or Fs then the * event is a disassociation.  If it isn't then it is an association. * ***********************************************************/int cardif_linux_rtnetlink_validate(struct interface_data *idata, uint8_t *mac){  char newmac[6];  if (!xsup_assert((idata != NULL), "idata != NULL", FALSE))    return XEMALLOC;  if (!xsup_assert((mac != NULL), "mac != NULL", FALSE))    return XEMALLOC;  // Is it a disassociation?  memset(newmac, 0x00, 6);  if (memcmp(newmac, mac, 6) == 0)    {      return FALSE;    }  memset(newmac, 0x44, 6);  if (memcmp(newmac, mac, 6) == 0)    {      return FALSE;    }  memset(newmac, 0xff, 6);  if (memcmp(newmac, mac, 6) == 0)    {      return FALSE;    }    // Otherwise, it was an association  return TRUE;}/********************************************************************** * * Process a SIOCGIWAP event. * **********************************************************************/void cardif_linux_rtnetlink_process_SIOCGIWAP(struct interface_data *idata,					      struct iw_event *iwe,					      struct wireless_state *ws){  char mac[6];  int assoc;  struct config_globals *globals;  if (!xsup_assert((idata != NULL), "idata != NULL", FALSE))    return;  if (!xsup_assert((iwe != NULL), "iwe != NULL", FALSE))    return;  // *ws is allowed to be NULL, so don't check it here.  memcpy(mac, iwe->u.ap_addr.sa_data, 6);  debug_printf(DEBUG_INT, "AP MAC : ");  debug_hex_printf(DEBUG_INT, (uint8_t *)mac, 6);  if (!ws)    {      config_ssid_add_bssid(mac);    } else {      globals = config_get_globals();      if (!xsup_assert((globals != NULL), "globals != NULL", FALSE))	return;      assoc = cardif_linux_rtnetlink_validate(idata, (uint8_t *)&mac);      if (assoc)	{	  // We have changed to associated mode.  Populate the destination	  // MAC with the BSSID, as long as we are in auto mode.	  	  ws->associated = TRUE;	  	  if (globals->destination == DEST_AUTO)	    memcpy(idata->dest_mac, mac, 6);	  	} else {	  ws->associated = FALSE;	}      debug_printf(DEBUG_INT, "(%s)  ws->associated == %d\n",		   __FUNCTION__, ws->associated);    }}/********************************************************************** * * Process a SIOCGIWESSID event. * **********************************************************************/void cardif_linux_rtnetlink_process_SIOCGIWESSID(struct interface_data *ctx,						 struct iw_event *iwe,						 struct wireless_state *ws){  char essid[IW_ESSID_MAX_SIZE+1];  if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))    return;  if (!xsup_assert((iwe != NULL), "iwe != NULL", FALSE))    return;  // *ws is allowed to be NULL, so don't check it here!  bzero(essid, IW_ESSID_MAX_SIZE+1);    memcpy(essid, iwe->u.essid.pointer, iwe->u.essid.length);  essid[iwe->u.essid.length] = '\0';  if (!ws)    {      debug_printf(DEBUG_INT, "ESSID : %s\n", essid);            config_ssid_add_ssid_name(essid);    } else {      debug_printf(DEBUG_NORMAL, "Got a get SSID event!? "		   "Notify your wireless driver maintainer.\n");    }}/***************************************************************** * * Process an SIOCSIWESSID. * *****************************************************************/void cardif_linux_rtnetlink_process_SIOCSIWESSID(struct interface_data *ctx,						 struct iw_event *iwe,						 struct wireless_state *ws){  char essid[IW_ESSID_MAX_SIZE+1];  struct config_network *network_data;  char wpaie[24];  if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))    return;  if (!xsup_assert((iwe != NULL), "iwe != NULL", FALSE))    return;  // *ws is allowed to be NULL, so don't check it!  bzero(essid, IW_ESSID_MAX_SIZE+1);    memcpy(essid, iwe->u.essid.pointer, iwe->u.essid.length);  essid[iwe->u.essid.length] = '\0';  if (!ws)    {      debug_printf(DEBUG_NORMAL, "Got an SSID set request from a scan!? "		   "Notify your driver maintainer!\n");    } else {      network_data = config_get_network_config();            if (network_data == NULL)	{	  debug_printf(DEBUG_CONFIG, "Invalid network configuration "		       "structure! (%s:%d)\n", __FUNCTION__, __LINE__);	}            debug_printf(DEBUG_INT, "ESSID set .. name : %s\n", essid);            if (strcmp(essid, ctx->cur_essid) != 0)	{	  if (config_ssid_ssid_known(essid) != TRUE)	    {	      // We only want to set this to TRUE if we don't already know	      // something about the SSID we connected to.	      ws->ssid_change = TRUE;	    }	  	  if (config_build(ctx, essid) == FALSE)	    {	      debug_printf(DEBUG_NORMAL, "Couldn't build a valid configuration"			   " for ESSID %s!\n", essid);	      // If we didn't initiate the set, then clear keys.	      if (!TEST_FLAG(ctx->flags, SSID_SET))		{		  cardif_clear_keys(ctx);		  memset(wpaie, 0x00, sizeof(wpaie));		  // We will also need to clear the WPA IE.		  if (cardif_linux_wext_set_wpa_ie(ctx, (unsigned char *)wpaie,						   0) < 0)		    {		      debug_printf(DEBUG_NORMAL, "Couldn't clear WPA IE!  You "				   "may not be able to associate.\n");		    }		}	    }	  	  // We changed ssids, so record the new one.	  if (ctx->cur_essid != NULL)	    {	      free(ctx->cur_essid);	      ctx->cur_essid = strdup(essid);	    }	  	  if (network_data)	    eap_clear_active_method(network_data->activemethod);	}    }  // Unset the SSID_SET flag, if we set it.  UNSET_FLAG(ctx->flags, SSID_SET);}/********************************************************************** * * Scan through whatever was returned by the IWEVGENIE event, and pull * out any interesting IEs. * **********************************************************************/void cardif_linux_rtnetlink_parse_ies(struct interface_data *ctx,				      uint8_t *iedata, int ielen){  int i = 0;  if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))    return;  if (!xsup_assert((iedata != NULL), "iedata != NULL", FALSE))    return;  if (!xsup_assert((ielen > 0), "ielen > 0", FALSE))    return;  if (!xsup_assert((ielen < 256), "ielen < 256", FALSE))    return;  while (i < ielen)    {      if (iedata[i] == WPA_EID)	{	  if (wpa_parse_ie((char *)&iedata[i]) > 0)	    {	      // We have a valid IE, save it.	      config_ssid_update_abilities(WPA_IE);	      config_ssid_add_wpa_ie((uint8_t *)&iedata[i], iedata[i+1]+2);	    }	}      if (iedata[i] == WPA2_EID)	{	  if (wpa2_parse_ie((char *)&iedata[i]) > 0)	    {	      // We have a valid IE, save it.	      config_ssid_update_abilities(RSN_IE);	      config_ssid_add_rsn_ie((uint8_t *)&iedata[i], iedata[i+1]+2);	    }	}      i += (iedata[i+1]+2);    }}/********************************************************************** * * Process an IWEVGENIE event. * **********************************************************************/void cardif_linux_rtnetlink_process_IWEVGENIE(struct interface_data *ctx,					      struct iw_event *iwe){  if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))    return;  if (!xsup_assert((iwe != NULL), "iwe != NULL", FALSE))    return;  debug_printf(DEBUG_INT, "IWEVGENIE (%d)\n", iwe->u.data.length);  debug_printf_nl(DEBUG_INT, "IE : ");  debug_hex_printf(DEBUG_INT, iwe->u.data.pointer, iwe->u.data.length);  cardif_linux_rtnetlink_parse_ies(ctx, iwe->u.data.pointer, 				   iwe->u.data.length);}/********************************************************************** * * Process an IWEVCUSTOM event. * **********************************************************************/void cardif_linux_rtnetlink_process_IWEVCUSTOM(struct interface_data *ctx,					       struct iw_event *iwe,					       struct wireless_state *ws){  char custom[IW_CUSTOM_MAX+1];  char temp[IW_CUSTOM_MAX+1];  if (!xsup_assert((ctx != NULL), "ctx != NULL", FALSE))    return;  if (!xsup_assert((iwe != NULL), "iwe != NULL", FALSE))    return;  if (!ws)    {      strncpy(custom, iwe->u.data.pointer, iwe->u.data.length);            debug_printf(DEBUG_INT, "IWEVCUSTOM : %s\n", custom);            if (strncmp("wpa_ie=", custom, 7) == 0)	{	  config_ssid_update_abilities(WPA_IE);	  debug_printf(DEBUG_INT, "AP appears to support WPA!\n");	  	  process_hex(&custom[7], (iwe->len -7), temp);	  wpa_parse_ie(temp);	  	  config_ssid_add_wpa_ie((uint8_t *)temp, ((iwe->u.data.length - 7)/2));	}            if (strncmp("rsn_ie=", custom, 7) == 0)	{	  config_ssid_update_abilities(RSN_IE);	  debug_printf(DEBUG_INT, "AP appears to support WPA2/802.11i!\n");	  	  process_hex(&custom[7], (iwe->len -7), temp);	  wpa2_parse_ie(temp);	  	  config_ssid_add_rsn_ie((uint8_t *)temp, ((iwe->u.data.length - 7)/2));	}    } else {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -