Log_Log("ARP", "Recieved a packet with HWSize != 6 (%i)", req4->HWSize);
return;
}
+ #if ARP_DETECT_SPOOFS
if( !MAC_EQU(req4->SourceMac, From) ) {
Log_Log("ARP", "ARP spoofing detected "
"(%02x%02x:%02x%02x:%02x%02x != %02x%02x:%02x%02x:%02x%02x)",
);
return;
}
+ #endif
- Log("[ARP ] Request ID %i", ntohs(req4->Request));
+ Log_Debug("ARP", "Request ID %i", ntohs(req4->Request));
switch( ntohs(req4->Request) )
{
case 1: // You want my IP?
- Log("[ARP ] ARP Request Address class %i", req4->SWSize);
+ Log_Debug("ARP", "ARP Request Address class %i", req4->SWSize);
// Check what type of IP it is
switch( req4->SWSize )
{
case 4:
- Log("[ARP ] From MAC %02x:%02x:%02x:%02x:%02x:%02x",
+ Log_Debug("ARP", "From MAC %02x:%02x:%02x:%02x:%02x:%02x",
req4->SourceMac.B[0], req4->SourceMac.B[1],
req4->SourceMac.B[2], req4->SourceMac.B[3],
req4->SourceMac.B[4], req4->SourceMac.B[5]);
- Log("[ARP ] to MAC %02x:%02x:%02x:%02x:%02x:%02x",
+ Log_Debug("ARP", "to MAC %02x:%02x:%02x:%02x:%02x:%02x",
req4->DestMac.B[0], req4->DestMac.B[1],
req4->DestMac.B[2], req4->DestMac.B[3],
req4->DestMac.B[4], req4->DestMac.B[5]);
- Log("[ARP ] ARP Request IPv4 Address %i.%i.%i.%i",
+ Log_Debug("ARP", "ARP Request IPv4 Address %i.%i.%i.%i",
req4->DestIP.B[0], req4->DestIP.B[1], req4->DestIP.B[2],
req4->DestIP.B[3]);
- Log("[ARP ] from %i.%i.%i.%i",
+ Log_Debug("ARP", "from %i.%i.%i.%i",
req4->SourceIP.B[0], req4->SourceIP.B[1],
req4->SourceIP.B[2], req4->SourceIP.B[3]);
iface = IPv4_GetInterface(Adapter, req4->DestIP, 0);
if( iface )
{
- Log("[ARP ] Caching sender's IP Address");
+ Log_Debug("ARP", "Caching sender's IP Address");
ARP_UpdateCache4(req4->SourceIP, req4->SourceMac);
req4->DestIP = req4->SourceIP;
req4->SourceIP = iface->IP4.Address;
req4->SourceMac = Adapter->MacAddr;
req4->Request = htons(2);
- Log("[ARP ] Hey, That's us!");
- Log("[ARP ] Sending back %02x:%02x:%02x:%02x:%02x:%02x",
+ Log_Debug("ARP", "Hey, That's us!");
+ Log_Debug("ARP", "Sending back %02x:%02x:%02x:%02x:%02x:%02x",
req4->SourceMac.B[0], req4->SourceMac.B[1],
req4->SourceMac.B[2], req4->SourceMac.B[3],
req4->SourceMac.B[4], req4->SourceMac.B[5]);
break;
case 6:
if( Length < sizeof(tArpRequest6) ) {
- Log("[ARP ] Recieved undersized packet (IPv6)");
+ Log_Debug("ARP", "Recieved undersized packet (IPv6)");
return ;
}
iface = IPv6_GetInterface(Adapter, req6->DestIP, 0);
}
break;
default:
- Log("[ARP ] Unknown Protocol Address size (%i)", req4->SWSize);
+ Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
return ;
}
break;
case 6:
if( Length < sizeof(tArpRequest6) ) {
- Log("[ARP ] Recieved undersized packet (IPv6)");
+ Log_Debug("ARP", "Recieved undersized packet (IPv6)");
return ;
}
ARP_UpdateCache6( req6->SourceIP, From );
break;
default:
- Log("[ARP ] Unknown Protocol Address size (%i)", req4->SWSize);
+ Log_Debug("ARP", "Unknown Protocol Address size (%i)", req4->SWSize);
return ;
}
#define IPSTACK_ROOT "/Devices/ip"
// === PROTOTYPES ===
-void PrintUsage(char *ProgName);
-void DumpInterfaces( int DumpAll );
- int AddInterface( char *Address );
- int DoAutoConfig( char *Device );
+void PrintUsage(const char *ProgName);
+void DumpInterfaces(void);
+void DumpInterface(const char *Name);
+ int AddInterface(const char *Device);
+ int DoAutoConfig(const char *Device);
// === CODE ===
/**
- * \fn int main(int argc, char *argv[])
- * \brief Entrypoint
+ * \brief Program entrypoint
*/
int main(int argc, char *argv[])
{
+ // No args, dump interfaces
if(argc == 1) {
- DumpInterfaces(0);
+ DumpInterfaces();
return 0;
}
+ // Add a new interface
if( strcmp(argv[1], "add") == 0 ) {
- if( argc < 3 ) {
- fprintf(stderr, "ERROR: `add` requires an argument\n");
+ if( argc < 4 ) {
+ fprintf(stderr, "ERROR: %s add require two arguments, %i passed\n", argv[0], argc-2);
PrintUsage(argv[0]);
return 0;
}
+ // TODO: Also set the IP address as the usage says it does
return AddInterface( argv[2] );
}
+ // Autoconfigure an interface
+ // NOTE: Debugging hack (see the function for more details)
if( strcmp(argv[1], "autoconf") == 0 ) {
DoAutoConfig(argv[2]);
return 0;
}
+ // Print usage instructions
PrintUsage(argv[0]);
return 0;
}
-void PrintUsage(char *ProgName)
+/**
+ * \brief Print usage instructions
+ */
+void PrintUsage(const char *ProgName)
{
- fprintf(stderr, "Usage: %s [add <ipaddr>]\n", ProgName);
+ fprintf(stderr, "Usage:\n");
+ fprintf(stderr, " %s add <device> <ip>/<prefix>\n", ProgName);
+ fprintf(stderr, " Add a new interface listening on <device> with the specified\n");
+ fprintf(stderr, " address.\n");
+ fprintf(stderr, " %s del <interface>\n", ProgName);
+ fprintf(stderr, " %s set <interface> <option> <value>\n", ProgName);
+ fprintf(stderr, " Set an option on an interface, a list of valid options follows\n");
+ fprintf(stderr, " gw IPv4 default gateway\n");
+ fprintf(stderr, " %s [<interface>]\n", ProgName);
+ fprintf(stderr, " Print the current interfaces (or only <interface> if passed)\n");
+ fprintf(stderr, "\n");
+ fprintf(stderr, "A note on Acess's IP Stack:\n");
+ fprintf(stderr, " Each interface corresponds to only one IP address (either IPv4\n");
+ fprintf(stderr, " or IPv6). A network device can have multiple interfaces bound\n");
+ fprintf(stderr, " to it, allowing multiple addresses for one network connection\n");
+ fprintf(stderr, "\n");
}
-void DumpInterfaces(int DumpAll)
+/**
+ * \brief Dump all interfaces
+ */
+void DumpInterfaces(void)
{
- int dp, fd;
- int type;
- char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
- char *filename = &path[sizeof(IPSTACK_ROOT)];
+ int dp;
+ char filename[FILENAME_MAX+1];
dp = open(IPSTACK_ROOT, OPENFLAG_READ);
while( readdir(dp, filename) )
{
if(filename[0] == '.') continue;
-
- fd = open(path, OPENFLAG_READ);
- if(fd == -1) {
- printf("%s:\tUnable to open ('%s')\n", filename, path);
- continue ;
- }
-
- type = ioctl(fd, 4, NULL);
-
- printf("%s:\t", filename);
+ DumpInterface(filename);
+ }
+
+ close(dp);
+}
+
+/**
+ * \brief Dump an interface
+ */
+void DumpInterface(const char *Name)
+{
+ int fd;
+ int type;
+ char path[sizeof(IPSTACK_ROOT)+1+FILENAME_MAX+1] = IPSTACK_ROOT"/";
+
+ strcat(path, Name);
+
+ fd = open(path, OPENFLAG_READ);
+ if(fd == -1) {
+ printf("%s:\tUnable to open ('%s')\n", Name, path);
+ return ;
+ }
+
+ type = ioctl(fd, 4, NULL);
+
+ printf("%s:\t", Name);
+ {
+ int call_num = ioctl(fd, 3, "get_device");
+ int len = ioctl(fd, call_num, NULL);
+ char *buf = malloc(len+1);
+ ioctl(fd, call_num, buf);
+ printf("'%s'\t", buf);
+ free(buf);
+ }
+ // Get the address type
+ switch(type)
+ {
+ case 0: // Disabled/Unset
+ printf("DISABLED\n");
+ break;
+ case 4: // IPv4
{
- int call = ioctl(fd, 3, "get_device");
- int len = ioctl(fd, call, NULL);
- char *buf = malloc(len+1);
- ioctl(fd, call, buf);
- printf("'%s'\t", buf);
- free(buf);
+ uint8_t ip[4];
+ int subnet;
+ printf("IPv4\n");
+ ioctl(fd, 5, ip); // Get IP Address
+ subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
+ printf("\tAddress: %i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
+ ioctl(fd, 8, ip); // Get Gateway
+ printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
}
- switch(type)
+ break;
+ case 6: // IPv6
{
- case 0:
- printf("DISABLED\n");
- break;
- case 4:
- {
- uint8_t ip[4];
- int subnet;
- printf("IPv4\n");
- ioctl(fd, 5, ip); // Get IP Address
- subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
- printf("\t%i.%i.%i.%i/%i\n", ip[0], ip[1], ip[2], ip[3], subnet);
- ioctl(fd, 8, ip); // Get Gateway
- printf("\tGateway: %i.%i.%i.%i\n", ip[0], ip[1], ip[2], ip[3]);
- }
- break;
- case 6:
- {
- uint16_t ip[8];
- int subnet;
- printf("IPv6\n");
- ioctl(fd, 5, ip); // Get IP Address
- subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
- printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
- ip[0], ip[1], ip[2], ip[3],
- ip[4], ip[5], ip[6], ip[7],
- subnet);
- }
- break;
- default:
- printf("UNKNOWN\n");
- break;
+ uint16_t ip[8];
+ int subnet;
+ printf("IPv6\n");
+ ioctl(fd, 5, ip); // Get IP Address
+ subnet = ioctl(fd, 7, NULL); // Get Subnet Bits
+ printf("\t%x:%x:%x:%x:%x:%x:%x:%x/%i\n",
+ ip[0], ip[1], ip[2], ip[3],
+ ip[4], ip[5], ip[6], ip[7],
+ subnet);
}
- printf("\n");
-
- close(fd);
+ break;
+ default: // Unknow
+ printf("UNKNOWN (%i)\n", type);
+ break;
}
-
- close(dp);
+ printf("\n");
+
+ close(fd);
}
-int AddInterface( char *Device )
+/**
+ * \brief Create a new interface using the passed device
+ * \param Device Network device to bind to
+ */
+int AddInterface(const char *Device)
{
int dp, ret;
dp = open(IPSTACK_ROOT, OPENFLAG_READ);
- ret = ioctl(dp, 4, Device);
+ ret = ioctl(dp, 4, (void*)Device);
printf("AddInterface: ret = 0x%x = %i\n", ret, ret);
close(dp);
return ret;
}
-int DoAutoConfig( char *Device )
+/**
+ * \note Debugging HACK!
+ * \brief Autoconfigure the specified device to 10.0.2.55/8 using
+ * 10.0.2.1 as the gateway.
+ */
+int DoAutoConfig(const char *Device)
{
int tmp, fd;
char path[sizeof(IPSTACK_ROOT)+5+1]; // ip000