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

📄 cardif_generic.c

📁 linux 下通过802.1认证的安装包
💻 C
📖 第 1 页 / 共 2 页
字号:
/*******************************************************************
 * A generic PCAP handler for support of various OSes.  (Doesn't
 * provide any kind of wireless support!!!!!!)
 *
 * File: cardif_generic.c
 *
 * Authors: Chris.Hessing@utah.edu
 *
 * $Id: cardif_generic.c,v 1.9.2.11 2006/12/29 19:28:16 chessing Exp $
 * $Date: 2006/12/29 19:28:16 $
 * $Log: cardif_generic.c,v $
 * Revision 1.9.2.11  2006/12/29 19:28:16  chessing
 * Changes to bring Mac/Linux versions in sync with Windows work.
 *
 * Revision 1.9.2.10  2006/12/08 21:11:54  chessing
 * Windows can now do wired authentication with the included protocol driver.
 *
 * Revision 1.9.2.9  2006/12/05 18:58:02  chessing
 * Added the binary driver needed for the continued work on Windows, along with the patch file to recreate the source from the Windows DDK.  Also included a README file that contains information about the reasoning for not including the full source to the protocol layer.
 *
 * Updated the win_list_ifs to use the new protocol layer to enumerate interfaces instead of needed WinPCap.
 *
 * Revision 1.9.2.8  2006/12/02 00:19:57  chessing
 * Wired auth on Windows should work now.  Need to test.
 *
 * Revision 1.9.2.7  2006/11/30 00:00:46  chessing
 * Code to being to add support for Windows.
 *
 * Revision 1.9.2.6  2006/11/10 23:31:35  chessing
 * Replaced all malloc()+memset() with Malloc().
 *
 * Revision 1.9.2.5  2006/10/05 22:42:27  chessing
 * Added special LEAP association mode.
 *
 * Revision 1.9.2.4  2006/09/28 03:23:36  galimorerpg
 * Removed frameavail() from cardif_generic.c
 *
 * Revision 1.9.2.3  2006/09/23 00:04:42  chessing
 * Changed free()+=NULL to FREE().
 *
 * Revision 1.9.2.2  2006/08/20 04:42:23  chessing
 * Can build under OS-X now.  Need to add wireless support.
 *
 * Revision 1.9.2.1  2006/08/20 03:02:46  chessing
 * Started to add Mac OS X support.
 *
 * Revision 1.9  2006/06/02 00:17:22  galimorerpg
 * More "-L" fixups for building with broken linkers.
 * Added missing stubs to cardif_generic to prevent linker bail on missing symbols.
 *
 * Revision 1.8  2006/06/01 23:30:46  galimorerpg
 * Removed whitespace between -L and path to fix building with grumpy linkers.
 *
 * Fixups to help cardif_generic.c build.
 *
 * Revision 1.7  2006/06/01 22:49:49  galimorerpg
 * Converted all instances of u_char to uint8_t
 * Fixed a bad #include in the generic frame handler.
 *
 * Revision 1.6  2006/05/13 05:56:44  chessing
 * Removed last pieces of code that relied on SIGALRM.  Active scan timeout is now configurable so that people that wish to hammer on their cards now have the option to do that. ;)
 *
 * Revision 1.5  2006/01/19 05:37:04  chessing
 * WPA2 is working correctly.  Added the ability to query the card to gather encryption/authentication capabilities.  1.2.3 is now ready to go.
 *
 * Revision 1.4  2005/08/09 01:39:14  chessing
 * Cleaned out old commit notes from the released version.  Added a few small features including the ability to disable the friendly warnings that are spit out.  (Such as the warning that is displayed when keys aren't rotated after 10 minutes.)  We should also be able to start when the interface is down.  Last, but not least, we can handle empty network configs.  (This may be useful for situations where there isn't a good reason to have a default network defined.)
 *
 *
 *******************************************************************/

#ifdef GENERIC_FRAMER
#include <pcap.h>
#include <stdlib.h>
#include <string.h>

#ifndef WINDOWS
#include <unistd.h>
#else
#include <packet32.h>
#include <Ntddndis.h>
#endif

#include "../../cardif/cardif.h"
#include "../../../lib/libxsupconfig/xsupconfig.h"
#include "../../xsup_common.h"
#include "../../context.h"
#include "../../xsup_debug.h"
#include "../../xsup_err.h"
#include "../../cardif/generic/cardif_generic.h"

#ifndef ETH_P_EAPOL
#define ETH_P_EAPOL 0x888e
#endif

#ifdef WINDOWS
char *getmac(char *devname)
{
  PPACKET_OID_DATA    pOidData;
  CHAR *pStr = NULL;
  LPADAPTER adapt = NULL;
  char *mac = NULL;

  // Create an adapter object.
  adapt = PacketOpenAdapter(devname);	
  if (adapt == NULL)
  {
	  debug_printf(DEBUG_NORMAL, "Couldn't create the adapter object needed to query the MAC address!\n");
	  return NULL;
  }

  pStr = malloc(sizeof(PACKET_OID_DATA)+128);
  ZeroMemory(pStr, sizeof(PACKET_OID_DATA)+128);
  pOidData = (PPACKET_OID_DATA) pStr;
  pOidData->Oid = OID_802_3_CURRENT_ADDRESS;
  pOidData->Length = 6;
  
  if (PacketRequest(adapt, FALSE, pOidData) != TRUE)
  {
	  debug_printf(DEBUG_NORMAL, "Unable to request the MAC address for this interface!\n");
	  PacketCloseAdapter(adapt);
	  return NULL;
  }

  mac = Malloc(6);
  if (mac == NULL)
  {
	  debug_printf(DEBUG_NORMAL, "Couldn't allocate memory to store interface MAC address!\n");
	  PacketCloseAdapter(adapt);
	  return NULL;
  }

  memcpy(mac, pOidData->Data, 6);
  
  debug_printf(DEBUG_INT, "MAC : %02X:%02X:%02X:%02X:%02X:%02X\n", pOidData->Data[0], pOidData->Data[1], pOidData->Data[2],
				pOidData->Data[3], pOidData->Data[4], pOidData->Data[5]);

  PacketCloseAdapter(adapt);

  return mac;
}
#else
#error You need to implement a MAC address discovery function for operation on this OS!
#endif

/***********************************************
 *
 * Return a handle to a pcap descriptor.  If NULL is returned,
 * we have an error.
 *
 ***********************************************/
pcap_t *setup_pcap(char *dev_to_use, char *src_mac, int buf_size, int timeout, 
		   char pcapErr[PCAP_ERRBUF_SIZE])
{
  char pcap_err[PCAP_ERRBUF_SIZE];   // pcap error buffer.
  pcap_t *pcap_descr = NULL;
  bpf_u_int32 pcap_maskp;
  bpf_u_int32 pcap_netp;
  char pcap_filter[100];
  struct bpf_program pcap_fp;
  char *errbuf=NULL;

  pcap_lookupnet(dev_to_use, &pcap_netp, &pcap_maskp, pcap_err);

  pcap_descr = pcap_open_live(dev_to_use, buf_size, 1, timeout, pcap_err);
  if (pcap_descr == NULL)
    {
      debug_printf(DEBUG_NORMAL, "pcap_open_live(): %s\n", pcap_err);
      return NULL;
    }

  sprintf(pcap_filter, "ether dst %02x:%02x:%02x:%02x:%02x:%02x or ether dst 01:80:c2:00:00:03 and ether proto 0x888e", (uint8_t)src_mac[0], (uint8_t)src_mac[1], (uint8_t)src_mac[2],
	  (uint8_t)src_mac[3], (uint8_t)src_mac[4], (uint8_t)src_mac[5]);

  debug_printf(DEBUG_INT, "PCAP Filter : %s\n", pcap_filter);
  
  if (pcap_compile(pcap_descr, &pcap_fp, pcap_filter, 0, pcap_netp) == -1)
    {
      debug_printf(DEBUG_NORMAL, "Error running pcap compile!\n");
      return NULL;
    }

  if (pcap_setfilter(pcap_descr, &pcap_fp) == -1)
    {
      debug_printf(DEBUG_NORMAL, "Error setting filter!\n");
      return NULL;
    }

  return pcap_descr;
}

/***********************************************
 *
 * Do whatever is needed to get the interface in to a state that we can send
 * and recieve frames on the network.  Any information that we need to later
 * use should be stored in the context structure.
 *
 ***********************************************/
int cardif_init(context *thisint, char driver)
{
  char pcap_err[PCAP_ERRBUF_SIZE];
  uint8_t *source_mac = NULL;
  struct gen_sock_data *sockData;

  debug_printf(DEBUG_INT, "Initializing interface %s..\n", thisint->intName);

  if (thisint->intName == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Invalid interface!\n");
      return -1;
    }

  // For this code, we only handle 1 interface, so the index doesn't matter.
  thisint->intIndex = 0;

  // Allocate memory for the things we need.
  thisint->sockData = (void *)Malloc(sizeof(struct gen_sock_data));
  if (thisint->sockData == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Error allocating memory!\n");
      return XEMALLOC;
    }

  sockData = thisint->sockData;

  source_mac = getmac(thisint->intName);

  if (source_mac == NULL) return XEMALLOC;

  if ((sockData->pcap_descr = setup_pcap(thisint->intName, 
					 (char *)source_mac, 
					 1700, 1000, 
					 pcap_err)) == NULL)
    {
      debug_printf(DEBUG_NORMAL, "Couldn't open interface %s at line %d in cardif_generic.c!\n",
		   thisint->intName, __LINE__);
      return -1;
    }



  // Store a copy of our source MAC for later use.
  memcpy((char *)&thisint->source_mac[0], (char *)&source_mac[0], 6);

  FREE(source_mac);

  // Allocate a send buffer.
  thisint->sendframe = Malloc(1524);
  if (thisint->sendframe == NULL)
  {
	  debug_printf(DEBUG_NORMAL, "Couldn't allocate memory for send frame buffer!\n");
	  return -1;
  }

  return XENONE;
}

/**************************************************************
 *
 * We don't know how to handle keys, so don't do anything.
 *
 **************************************************************/
void cardif_reset_keys(context *thisint)
{
  return;
}

/**************************************************************
 *
 * 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(context *thisint)
{
  // We probably don't need this either.
  return XENOWIRELESS;
}

/******************************************
 *
 * Return the socket number for functions that need it.
 *
 ******************************************/
int cardif_get_socket(context *thisint)
{
  // This has no meaning in the context of this driver!

  // Update this to use the "get selectable socket" pieces in current libpcap versions.
  return -1;
}

/******************************************
 *
 * 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(context *thisint)
{
  struct gen_sock_data *sockData;

  sockData = thisint->sockData;

  debug_printf(DEBUG_EVERYTHING, "Cleaning up interface %s...\n",thisint->intName);

  if (sockData->pcap_descr != NULL)
    {
      pcap_close(sockData->pcap_descr);
    }

  // Now clean up the memory.
  FREE(thisint->sockData);

  return XENONE;
}

/******************************************
 *
 * Set a wireless key.  Also, based on the index, we may change the transmit
 * key.
 *
 ******************************************/
int cardif_set_wireless_key(context *thisint, uint8_t *key, 
			    int keylen, int index)
{
  // We won't ever set a key, so return an error.
  return XENOWIRELESS;
}

/******************************************
 *
 * 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_GetSSID(context *thisint, char *ssid_name)
{
  // We don't have any wireless interfaces.
  return XENOWIRELESS;
}

/******************************************
 *
 * Normally set the SSID on the card.  But, cardif_generic doesn't understand
 * keying, so return XENOWIRELESS.
 *
 ******************************************/
int cardif_SetSSID(context *thisint, char *ssid_name)
{
  return XENOWIRELESS;
}

/******************************************
 *
 * Check the SSID against what we currently have, and determine if we need
 * to reset our configuration.
 *
 ******************************************/
int cardif_check_ssid(context *thisint)
{
  // We aren't wireless!
  return XENOWIRELESS;
}

/******************************************
 *
 * 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)
{
  // Not wireless

⌨️ 快捷键说明

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