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

📄 cardif_linux.c

📁 Linux上的802.1x 的supplicant的实现。很多supplicant程序都是基于它开发的
💻 C
📖 第 1 页 / 共 4 页
字号:
  if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE))    return XEMALLOC;  memset(&ifr, 0x00, sizeof(ifr));  strncpy(ifr.ifr_name, thisint->intName, sizeof(ifr.ifr_name));  retVal = ioctl(sockData->sockInt, SIOCGIFFLAGS, &ifr);  if (retVal < 0)    {      debug_printf(DEBUG_NORMAL, "Interface %s not found!\n", thisint->intName);      return FALSE;    }    if ((ifr.ifr_flags & IFF_UP) == IFF_UP)    {      return TRUE;    } else {      SET_FLAG(thisint->flags, WAS_DOWN);      return FALSE;    }  return XENONE;}/****************************************** * * Send a frame out of the network card interface.  If there isn't an  * interface, we should return an error.  We should return a different error * if we have a problem sending the frame. * ******************************************/int cardif_sendframe(struct interface_data *thisint){  char nomac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};  int retval;  struct lin_sock_data *sockData;  struct config_network *network_data;  uint16_t pad;  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  sockData = thisint->sockData;  if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE))    return XEMALLOC;  if (thisint->send_size == 0)     {      debug_printf(DEBUG_INT, "%s:%d -- Nothing to send!\n",		   __FUNCTION__, __LINE__);      return XENONE;    }  network_data = config_get_network_config();    if (network_data == NULL)    {      debug_printf(DEBUG_NORMAL, "Invalid network configuration structure! "		   "(%s:%d)\n", __FUNCTION__, __LINE__);      return XEBADCONFIG;    }  // The frame we are handed in shouldn't have a src/dest, so put it in.  memcpy(&thisint->sendframe[0], &thisint->dest_mac[0], 6);  memcpy(&thisint->sendframe[6], &thisint->source_mac[0], 6);  if (memcmp(nomac, (char *)&network_data->dest_mac[0], 6) != 0)    {      debug_printf(DEBUG_INT, "Static MAC address defined!  Using it!\n");      memcpy(&thisint->sendframe[0], &network_data->dest_mac[0], 6);    }  // Make sure the frame is large enough.  if ((!TEST_FLAG(thisint->flags, IS_WIRELESS)) && (thisint->send_size < 64))    {      pad = 64 - thisint->send_size;      debug_printf(DEBUG_INT, "Padding frame to 64 bytes by adding %d byte"                   "(s).\n", pad);      memset(&thisint->sendframe[thisint->send_size+1], 0x00, pad);      thisint->send_size += pad;    }  debug_printf(DEBUG_EVERYTHING, "Frame to be sent (%d) : \n",	       thisint->send_size);  debug_hex_dump(DEBUG_EVERYTHING, thisint->sendframe, thisint->send_size);  snmp_dot1xSuppEapolFramesTx();  retval = sendto(sockData->sockInt, thisint->sendframe, thisint->send_size, 0,		  NULL, 0);  if (retval <= 0)    {      debug_printf(DEBUG_NORMAL, "Couldn't send frame! (%s)\n", strerror(errno));    }  thisint->send_size = 0;    // Clear out the receive buffer so we don't accidently try to process it  // again.  bzero(thisint->recvframe, 1520);  thisint->recv_size = 0;  return retval;}/****************************************** *  * Get a frame from the network.  Make sure to check the frame, to determine  * if it is something we care about, and act accordingly. * ******************************************/int cardif_getframe(struct interface_data *thisint){  int newsize=0;  char dot1x_default_dest[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03};  struct lin_sock_data *sockData;  uint8_t resultframe[1520];  int resultsize;  struct config_globals *globals;  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  globals = config_get_globals();  if (!xsup_assert((globals != NULL), "globals != NULL", FALSE))    return XEMALLOC;  sockData = thisint->sockData;  if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE))    return XEMALLOC;  errno = 0;  resultsize = 1520; /* overflows resultframe if too large, or should we increase resultframe instead? */  newsize = recvfrom(sockData->sockInt, resultframe, resultsize, 0, 0, 0);  if (newsize <= 0)    {      if ((errno != EAGAIN) && (errno != ENETDOWN))	{	  debug_printf(DEBUG_NORMAL, "Error (%d) : %s  (%s:%d)\n", errno,		       strerror(errno), __FUNCTION__, __LINE__);	}      return XENOFRAMES;    } else {      debug_printf(DEBUG_EVERYTHING, "Got Frame (%d) : \n", newsize);      debug_hex_dump(DEBUG_EVERYTHING, resultframe, newsize);    }  snmp_dot1xSuppEapolFramesRx();  // Make sure that the frame we got is for us..  if ((memcmp(&thisint->source_mac[0], &resultframe[0], 6) == 0) ||      ((memcmp(&resultframe[0], &dot1x_default_dest[0], 6) == 0) &&       (memcmp(&resultframe[6], &thisint->source_mac[0], 6) != 0)))    {      // Since we now know this frame is for us, record the address it      // came from.      snmp_dot1xSuppLastEapolFrameSource((uint8_t *)&resultframe[6]);      resultsize = newsize;      switch (globals->destination)	{	case DEST_AUTO:	  // If it is a wired interface, only change the destination if	  // the recieved frame destination isn't the multicast address.	  if (!TEST_FLAG(thisint->flags, IS_WIRELESS))	    {	      if (memcmp(&resultframe[0], dot1x_default_dest, 6) == 0)		{		  break;		}	      // Otherwise, fall through.	    }	case DEST_SOURCE:	  if (memcmp(thisint->dest_mac, &resultframe[6], 6) != 0)	    {	      debug_printf(DEBUG_INT, "Changing destination mac to source.\n");	    }	  memcpy(thisint->dest_mac, &resultframe[6], 6);	  break;	case DEST_MULTICAST:	  memcpy(thisint->dest_mac, dot1x_default_dest, 6);	  break;	case DEST_BSSID:	  cardif_GetBSSID(thisint, thisint->dest_mac);	  break;	default:	  debug_printf(DEBUG_NORMAL, "Unknown destination mode!\n");	  break;	}      thisint->recv_size = newsize;      memcpy(thisint->recvframe, resultframe, newsize);      return newsize;    }  // Otherwise it isn't for us.   debug_printf(DEBUG_INT, "Got a frame, not for us.\n");  return XENOFRAMES;}/************************************************************** * * Set the state needed to associate to a WPA enabled AP, and actually * do a WPA authentication. * **************************************************************/int cardif_enable_wpa_state(struct interface_data *thisint){  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  debug_printf(DEBUG_INT, "WPA: Enabling WPA state on interface %s.\n",thisint->intName);  return wireless->wpa_state(thisint, TRUE);}/************************************************************** * * Clear the state needed to associate to a WPA enabled AP, and actually * do a WPA authentication. * **************************************************************/int cardif_disable_wpa_state(struct interface_data *thisint){  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  return wireless->wpa_state(thisint, FALSE);}/************************************************************** * * Enable WPA (if it is supported.) * **************************************************************/int cardif_enable_wpa(struct interface_data *thisint){  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  debug_printf(DEBUG_INT, "WPA: Enabling WPA on interface %s.\n",thisint->intName);  return wireless->wpa(thisint, TRUE);}/************************************************************** * * Call this when we roam to a different AP, or disassociate from an AP. * **************************************************************/int cardif_wep_associate(struct interface_data *thisint, int zeros){  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  if (!config_ssid_using_wep())    {      debug_printf(DEBUG_INT, "Doing WPA/WPA2 mode! Not "		   "setting/unsetting keys.\n");      return 0;    }  return wireless->wep_associate(thisint, zeros); }/****************************************** *  * Return true if there is a frame in the queue to be processed. * ******************************************/int cardif_frameavail(struct interface_data *thisint){  int newsize=0;  char resultframe[1520];  struct lin_sock_data *sockData;  if (!xsup_assert((thisint != NULL), "thisint != NULL", FALSE))    return XEMALLOC;  sockData = thisint->sockData;  if (!xsup_assert((sockData != NULL), "sockData != NULL", FALSE))    return XEMALLOC;  newsize = recvfrom(sockData->sockInt, &resultframe, 1520, MSG_PEEK, 0, 0);  if (newsize > 0) return TRUE;  return FALSE;}/****************************************** * * Validate an interface, based on if it has a MAC address. * ******************************************/int cardif_validate(char *interface){  int sd, res;  struct ifreq ifr;  if (!xsup_assert((interface != NULL), "interface != NULL", FALSE))    return XEMALLOC;  memset(&ifr, 0x00, sizeof(ifr));  strncpy(ifr.ifr_name, interface, sizeof(interface)+1);  sd = socket(PF_PACKET, SOCK_RAW, 0);  if (sd < 0)    return FALSE;  res = ioctl(sd, SIOCGIFHWADDR, &ifr);  close(sd);  if (res < 0)    {      debug_printf(DEBUG_NORMAL, "Couldn't get information for interface %s!\n",interface);    } else {      switch (ifr.ifr_hwaddr.sa_family)	{	case ARPHRD_ETHER:	case ARPHRD_IEEE80211:	  return TRUE;	}    }  return FALSE;}/****************************************** * * (en)/(dis)able countermeasures on this interface. * ******************************************/int cardif_countermeasures(struct interface_data *intdata, char endis){  if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  return wireless->countermeasures(intdata, endis);}/****************************************** * * (en)/(dis)able receiving of unencrypted frames on this interface. * ******************************************/int cardif_drop_unencrypted(struct interface_data *intdata, char endis){  if (!xsup_assert((intdata != NULL), "intdata != NULL", FALSE))    return XEMALLOC;  if (wireless == NULL) return -1;  if (config_ssid_using_wep()) return XENONE;    return wireless->drop_unencrypted(intdata, endis);}/****************************************** * * Get the name of an interface, based on an index value. * ******************************************/#define PROC_DEV_FILE  "/proc/net/dev"int cardif_get_int(int index, char *retInterface){  FILE *fp;  int hits;  char line[1000], *lineptr;  if (!xsup_assert((retInterface != NULL), "retInterface != NULL", FALSE))    return XEMALLOC;  hits = 0;  fp = fopen(PROC_DEV_FILE, "r");  if (fp == NULL)    {      debug_printf(DEBUG_NORMAL, "Couldn't access /proc/net/dev!\n");      exit(250);    }

⌨️ 快捷键说明

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