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

📄 cardif_macosx.c

📁 linux 下通过802.1认证的安装包
💻 C
📖 第 1 页 / 共 3 页
字号:
 *
 * 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_GetBSSID(context *thisint, char *bssid_dest)
{
  if (wireless == NULL) return -1;

  if (thisint == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid interface data structure passed to %s!\n", __FUNCTION__);
      return -1;
    }

  if (bssid_dest == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid bssid_dest in %s!\n", __FUNCTION__);
      return -1;
    }

  if (wireless->get_bssid == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->get_bssid(thisint, bssid_dest);
}

/******************************************
 *
 * Set the flag in the state machine that indicates if this interface is up
 * or down.  If there isn't an interface, we should return an error.
 *
 ******************************************/
int cardif_get_if_state(context *thisint)
{
  int flags;

  if (!_getiff(thisint->intName, &flags))
      return XENONE;

  return (flags & IFF_UP) != 0;
}

/******************************************
 *
 * 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(context *ctx)
{
  char nomac[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  int retval;
  struct darwin_sock_data *sockData;
  struct sockaddr_ndrv  ndrv;
  uint16_t pad;

  if (ctx == NULL) return XEMALLOC;

  sockData = (struct darwin_sock_data *)ctx->sockData;

  if (ctx->send_size == 0) return XENONE;

  if (ctx->conn == NULL)
    {
      debug_printf(DEBUG_NORMAL, "No connection information to use during "
		   "authentication!\n");
      return XENONE;
    }

  // The frame we are handed in shouldn't have a src/dest, so put it in.
  memcpy(&ctx->sendframe[0], &ctx->dest_mac[0], 6);
  memcpy(&ctx->sendframe[6], &ctx->source_mac[0], 6);

  if (memcmp(nomac, (char *)&ctx->conn->dest_mac[0], 6) != 0)
    {
      debug_printf(DEBUG_INT, "Static MAC address defined!  Using it!\n");
      memcpy(&ctx->sendframe[0], &ctx->conn->dest_mac[0], 6);
    }

  // Make sure the frame is large enough.
  if ((ctx->intType != ETH_802_11_INT) && (ctx->send_size < 64))
    {
      pad = 64 - ctx->send_size;
      debug_printf(DEBUG_INT, "Padding frame to 64 bytes by adding %d byte"
                   "(s).\n", pad);
      memset(&ctx->sendframe[ctx->send_size+1], 0x00, pad);
      ctx->send_size += pad;
    }

  debug_printf(DEBUG_INT, "Frame to be sent (%d) : \n", 
	       ctx->send_size);
  debug_hex_dump(DEBUG_INT, ctx->sendframe, ctx->send_size);

  memset(&ndrv, 0x00, sizeof(ndrv));
  ndrv.snd_len = sizeof(ndrv);
  ndrv.snd_family = AF_NDRV;

  snmp_dot1xSuppEapolFramesTx();
  retval = sendto(sockData->sockInt, ctx->sendframe, ctx->send_size,
		  0, (struct sockaddr *)&ndrv, sizeof(ndrv));

  if (retval != ctx->send_size) 
      debug_printf(DEBUG_NORMAL, "Couldn't send frame! %d: %s\n", errno, 
		   strerror(errno));

  memset(ctx->sendframe, 0x00, FRAMESIZE);
  ctx->send_size = 0;

  if (ctx->recvframe != NULL)
    {
      memset(ctx->recvframe, 0x00, FRAMESIZE);
      ctx->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(context *thisint)
{
  int newsize=0;
  char dot1x_default_dest[6] = {0x01, 0x80, 0xc2, 0x00, 0x00, 0x03};
  struct darwin_sock_data *sockData;
  uint8_t *resultframe;
  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= (struct darwin_sock_data *) thisint->sockData;

  errno = 0;
  resultsize = FRAMESIZE;

  FREE(thisint->recvframe);

  resultframe = malloc(FRAMESIZE);
  if (resultframe == NULL)
    {
      debug_printf(DEBUG_INT, "Couldn't allocate memory for incoming frame!\n");
      return -1;
    }

  memset(resultframe, 0x00, FRAMESIZE);

  newsize = recv(sockData->sockInt, resultframe, resultsize, 0);
  if (newsize <= 0)
    {
      if (errno != EAGAIN)
	{
	  debug_printf(DEBUG_NORMAL, "Error (%d) : %s  (%s:%d)\n", errno,
		       strerror(errno), __FUNCTION__, __LINE__);
	}
      return XENOFRAMES;
    } else {
      debug_printf(DEBUG_INT, "Got Frame : \n");
      debug_hex_dump(DEBUG_INT, 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:
	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);
      thisint->recvframe = resultframe;
      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(context *thisint)
{
  if (wireless == NULL) return -1;

  if (wireless->wpa_state == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->wpa_state(thisint, 1);
}

/**************************************************************
 *
 * Clear the state needed to associate to a WPA enabled AP, and actually
 * do a WPA authentication.
 *
 **************************************************************/
int cardif_disable_wpa_state(context *thisint)
{
  if (wireless == NULL) return -1;

  if (wireless->wpa_state == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->wpa_state(thisint, 0);
}

/**************************************************************
 *
 * Enable WPA (if it is supported.)
 *
 **************************************************************/
int cardif_enable_wpa(context *thisint)
{
  if (wireless == NULL) return -1;

  if (wireless->wpa == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->wpa(thisint, TRUE);
}

/**************************************************************
 *
 * Call this when we roam to a different AP, or disassociate from an AP.
 *
 **************************************************************/
int cardif_roam(context *thisint)
{
  if (wireless == NULL) return -1;

  debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
  return -1;
}

/******************************************
 *
 * Validate an interface, based on if it has a MAC address.
 *
 ******************************************/
int cardif_validate(char *interface)
{
  char mac[6];
  if (_getmac(mac, interface) == XENONE)
      return TRUE;
  else
      return FALSE;
}

/******************************************
 *
 * (en)/(dis)able countermeasures on this interface.
 *
 ******************************************/
int cardif_countermeasures(context *intdata, char endis)
{
  if (wireless == NULL) return -1;

  debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
  return -1;
}

/******************************************
 *
 * (en)/(dis)able receiving of unencrypted frames on this interface.
 *
 ******************************************/
int cardif_drop_unencrypted(context *intdata, char endis)
{
  if (wireless == NULL) return -1;
  
  debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
  return -1;
}

/*******************************************************
 *
 * Check to see if an interface is wireless.  On freebsd, we look in
 * /proc/net/wireless to see if the interface is registered with the
 * wireless extensions.
 *
 *******************************************************/
int cardif_int_is_wireless(context *ctx)
{
  // XXX Fix this to be a real check.  Probably need to walk the ioreg, and 
  // compare the MAC address in the Airport section to the MAC address of
  // the interface itself.
  if (strcmp(ctx->intName, "en1") == 0) return TRUE;

  debug_printf(DEBUG_INT, "Interface is wired.\n");
  return FALSE;
}

int cardif_get_wpa_ie(context *intdata, char *iedata, int *ielen)
{
  if (intdata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Error!  Invalid interface data structure! "
		   "(%s:%d)\n", __FUNCTION__, __LINE__);
      return XEMALLOC;
    }

  if (iedata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid bucket for IE data! (%s:%d)\n",
		   __FUNCTION__, __LINE__);
      return XEMALLOC;
    }

  if (wireless->get_wpa_ie == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->get_wpa_ie(intdata, iedata, ielen);
}

int cardif_get_wpa2_ie(context *intdata, char *iedata, int *ielen)
{
  if (intdata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Error!  Invalid interface data structure! "
		   "(%s:%d)\n", __FUNCTION__, __LINE__);
      return XEMALLOC;
    }

  if (iedata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid bucket for IE data! (%s:%d)\n",
		   __FUNCTION__, __LINE__);
      return XEMALLOC;
    }

  if (wireless->get_wpa2_ie == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return -1;
    }

  return wireless->get_wpa2_ie(intdata, iedata, ielen);
}

/**************************************************************
 *
 * This function should clear out all keys that have been applied to the card.
 * It should be independant of the type (WEP/TKIP/CCMP) of key that was
 * applied.
 *
 **************************************************************/
int cardif_clear_keys(context *intdata)
{
  if (wireless == NULL) return -1;

  if (wireless->delete_key == NULL)
    {
      debug_printf(DEBUG_NORMAL, "%s not implemented!\n", __FUNCTION__);
      return -1;
    }

  wireless->delete_key(intdata, 1, 0);
  wireless->delete_key(intdata, 2, 0);
  return 0;
}

/***********************************************************************
 *
 *  The Darwin wireless extensions don't seem to have an event generation
 *  function.  So, we need to manually trap, and process events such as
 *  SSID and BSSID changes, association state changes, etc.
 *
 ***********************************************************************/
int cardif_macosx_manual_events(context *ctx)
{
#ifdef DARWIN_WIRELESS
  char bssid[6];
  char ssid[99];
  uint8_t na[6] = {0x44, 0x44, 0x44, 0x44, 0x44, 0x44};
  wireless_ctx *wctx;

  debug_printf(DEBUG_INT, "[OS X Wireless] %s\n", __FUNCTION__);

  if (ctx->intType == ETH_802_11_INT)
    {
      wctx = (wireless_ctx *)ctx->intTypeData;

      if (cardif_GetBSSID(ctx, (char *)&bssid) == XENONE)
	{
	  // Check and see if the BSSID changed.
	  if (memcmp(ctx->dest_mac, bssid, 6) != 0)
	    {
	      debug_printf(DEBUG_INT, "[OS X Wireless] BSSID change!\n");
	      debug_printf(DEBUG_INT, "[OS X Wireless] Clearing keys.\n");
	      cardif_clear_keys(ctx);
	      debug_printf(DEBUG_INT, "[OS X Wireless] New BSSID : ");
	      debug_hex_printf(DEBUG_INT, bssid, 6);
	      memcpy(ctx->dest_mac, bssid, 6);

	      if (memcmp(na, ctx->dest_mac, 6) == 0)
		{
		  // We aren't associated.
		  UNSET_FLAG(wctx->flags, WIRELESS_SM_ASSOCIATED);
		  UNSET_FLAG(wctx->flags, WIRELESS_SM_STALE_ASSOCIATION);
		}
	      else
		{
		  if (!TEST_FLAG(wctx->flags, WIRELESS_SM_STALE_ASSOCIATION))
		    SET_FLAG(wctx->flags, WIRELESS_SM_ASSOCIATED);
		}
	    }
	}
      
      if (cardif_GetSSID(ctx, (char *)&ssid, 99) == XENONE)
	{
	  if (strcmp(wctx->cur_essid, ssid) != 0)
	    {
	      debug_printf(DEBUG_INT, "[OS X Wireless] SSID change!\n");
	      debug_printf(DEBUG_INT, "[OS X Wireless] New SSID : %s\n", ssid);
	      debug_printf(DEBUG_INT, "[OS X Wireless] Old SSID : %s\n", wctx->cur_essid);
	      
	      FREE(wctx->cur_essid);
	      
	      wctx->cur_essid = strdup(ssid);
	      
	      if (config_build(ctx, wctx->cur_essid) == FALSE)
		{
		  debug_printf(DEBUG_NORMAL, "Couldn't build a valid "
			       "configuration for ESSID '%s'!\n", ssid);
		}
	    }
	}
    }
#endif

  return 0;
}

void cardif_reassociate(context *intdata, uint8_t reason)
{
  if (intdata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid interface struct passed to %s!\n",
                   __FUNCTION__);
      return;
    }

  if (wireless == NULL) return;

  if (wireless->associate == NULL)
    {
      debug_printf(DEBUG_INT, "%s not implemented.\n", __FUNCTION__);
      return;
    }

  wireless->associate(intdata);
}

void cardif_try_associate(context *intdata)
{
  if (intdata == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid interface struct passed to %s!\n",
                   __FUNCTION__);

⌨️ 快捷键说明

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