Skip to content

Commit

Permalink
Workaround non-standardized TUN message types.
Browse files Browse the repository at this point in the history
Also minor refactoring.
  • Loading branch information
Caleb James DeLisle committed Dec 10, 2012
1 parent f96202e commit b0472e8
Show file tree
Hide file tree
Showing 23 changed files with 200 additions and 71 deletions.
6 changes: 1 addition & 5 deletions admin/Admin.h
Expand Up @@ -21,12 +21,8 @@
#include "memory/Allocator.h" #include "memory/Allocator.h"
#include "util/log/Log.h" #include "util/log/Log.h"
#include "util/UniqueName.h" #include "util/UniqueName.h"

#ifdef FreeBSD
#include <netinet/in.h>
#endif

#include "util/events/EventBase.h" #include "util/events/EventBase.h"

#include <stdbool.h> #include <stdbool.h>


#define Admin_FUNCTION(name) void (* name)(Dict* input, void* context, String* txid) #define Admin_FUNCTION(name) void (* name)(Dict* input, void* context, String* txid)
Expand Down
2 changes: 1 addition & 1 deletion admin/angel/Core.c
Expand Up @@ -256,7 +256,7 @@ int Core_main(int argc, char** argv)


SerializationModule_register(registry, logger, alloc); SerializationModule_register(registry, logger, alloc);


struct IpTunnel* ipTun = IpTunnel_new(logger, eventBase, alloc, rand); struct IpTunnel* ipTun = IpTunnel_new(logger, eventBase, alloc, rand, admin);


struct Ducttape* dt = Ducttape_register(privateKey, struct Ducttape* dt = Ducttape_register(privateKey,
registry, registry,
Expand Down
10 changes: 10 additions & 0 deletions interface/CMakeLists.txt
Expand Up @@ -42,6 +42,16 @@ target_link_libraries(interface
cjdns-util-platform-socket cjdns-util-platform-socket
) )


if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/TUNMessageType_${SYSTEM}.c)
set(TUNMessageType TUNMessageType_${SYSTEM}.c)
else()
set(TUNMessageType TUNMessageType.c)
endif()

add_library(cjdns-interface-tun-messagetype
${TUNMessageType}
)

add_library(cjdns-interface-pipeinterface add_library(cjdns-interface-pipeinterface
PipeInterface.c PipeInterface.c
) )
Expand Down
16 changes: 0 additions & 16 deletions interface/TUNInterface.c
Expand Up @@ -48,17 +48,6 @@ static void handleEvent(void* vcontext)
} }
msg->length = length; msg->length = length;


#ifdef OSX
// OSX tags the message with the AF type (AF_INET or AF_INET6) rather than the ethertype.
// pop it and scrap it then tag it the same as we do for Illumos.
TUNInterface_popMessageType(msg);
#endif
#if defined(Illumos) || defined(OSX)
// Illumos does not send packet info, it only supports ip4 and ip6 over tun.
uint16_t ethertype = ((msg->bytes[0] >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
TUNInterface_pushMessageType(msg, ethertype);
#endif

struct Interface* iface = &tun->pub.iface; struct Interface* iface = &tun->pub.iface;
if (iface->receiveMessage) { if (iface->receiveMessage) {
iface->receiveMessage(msg, iface); iface->receiveMessage(msg, iface);
Expand All @@ -67,11 +56,6 @@ static void handleEvent(void* vcontext)


static uint8_t sendMessage(struct Message* message, struct Interface* iface) static uint8_t sendMessage(struct Message* message, struct Interface* iface)
{ {
#ifdef Illumos
// Illumos does not support packet info.
Message_shift(message, -4);
#endif

struct TUNInterface_pvt* tun = Identity_cast((struct TUNInterface_pvt*) iface); struct TUNInterface_pvt* tun = Identity_cast((struct TUNInterface_pvt*) iface);


ssize_t ret = write(tun->tunSocket, message->bytes, message->length); ssize_t ret = write(tun->tunSocket, message->bytes, message->length);
Expand Down
19 changes: 1 addition & 18 deletions interface/TUNInterface.h
Expand Up @@ -15,11 +15,9 @@
#ifndef TUNInterface_H #ifndef TUNInterface_H
#define TUNInterface_H #define TUNInterface_H


#include "benc/String.h"
#include "interface/Interface.h" #include "interface/Interface.h"
#include "memory/Allocator.h" #include "memory/Allocator.h"
#include "util/events/EventBase.h" #include "util/events/EventBase.h"
#include "wire/Message.h"


/** /**
* An interface which connects to the TUN/TAP device for * An interface which connects to the TUN/TAP device for
Expand All @@ -39,21 +37,6 @@ struct TUNInterface
* @return a TUNInterface structure. * @return a TUNInterface structure.
*/ */
struct TUNInterface* TUNInterface_new(void* tunSocket, struct TUNInterface* TUNInterface_new(void* tunSocket,
struct event_base* base, struct EventBase* base,
struct Allocator* allocator); struct Allocator* allocator);


static inline void TUNInterface_pushMessageType(struct Message* message, uint16_t ethertype)
{
Message_shift(message, 4);
((uint16_t*) message->bytes)[0] = 0;
((uint16_t*) message->bytes)[1] = ethertype;
}

static inline uint16_t TUNInterface_popMessageType(struct Message* message)
{
Message_shift(message, -4);
return ((uint16_t*) message->bytes)[-1];
}

#endif #endif
31 changes: 31 additions & 0 deletions interface/TUNMessageType.c
@@ -0,0 +1,31 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "interface/TUNMessageType.h"
#include "wire/Message.h"

#include <stdint.h>

void TUNMessageType_push(struct Message* message, uint16_t ethertype)
{
Message_shift(message, 4);
((uint16_t*) message->bytes)[0] = 0;
((uint16_t*) message->bytes)[1] = ethertype;
}

uint16_t TUNMessageType_pop(struct Message* message)
{
Message_shift(message, -4);
return ((uint16_t*) message->bytes)[-1];
}
26 changes: 26 additions & 0 deletions interface/TUNMessageType.h
@@ -0,0 +1,26 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TUNMessageType_H
#define TUNMessageType_H

#include "wire/Message.h"

#include <stdint.h>

void TUNMessageType_push(struct Message* message, uint16_t ethertype);

uint16_t TUNMessageType_pop(struct Message* message);

#endif
39 changes: 39 additions & 0 deletions interface/TUNMessageType_Illumos.c
@@ -0,0 +1,39 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wire/Message.h"
#include "interface/TUNMessageType.h"
#include "wire/Ethernet.h"

#include <stdint.h>

/**
* Illumos has no consept of packet info, it only supports IPv4 and IPv6
* through TUN devices and it detects it by reading the version byte.
*/

static inline ethertypeForPacketType(uint8_t highByte)
{
return ((message->bytes[0] >> 4) == 6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
}

void TUNMessageType_push(struct Message* message, uint16_t ethertype)
{
Assert_true(ethertype == ethertypeForPacketType(message->bytes[0]));
}

uint16_t TUNMessageType_pop(struct Message* message)
{
return ethertypeForPacketType(message->bytes[0]);
}
42 changes: 42 additions & 0 deletions interface/TUNMessageType_OSX.c
@@ -0,0 +1,42 @@
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "wire/Message.h"
#include "interface/TUNMessageType.h"
#include "wire/Ethernet.h"

#include <stdint.h>
#include <netinet/in.h>

/**
* OSX is broken and expects you to send the platform dependent
* address family type rather than the ethertype.
*/

void TUNMessageType_push(struct Message* message, uint16_t ethertype)
{
uint16_t afType = (ethertype == Ethernet_TYPE_IP6) ? AF_INET6 : AF_INET;

Message_shift(message, 4);
((uint16_t*) message->bytes)[0] = 0;
((uint16_t*) message->bytes)[1] = afType;
}

uint16_t TUNMessageType_pop(struct Message* message)
{
Message_shift(message, -4);
uint16_t afType = ((uint16_t*) message->bytes)[-1];

return (afType == AF_INET6) ? Ethernet_TYPE_IP6 : Ethernet_TYPE_IP4;
}
1 change: 1 addition & 0 deletions interface/test/CMakeLists.txt
Expand Up @@ -23,6 +23,7 @@ set(Test_LIBRARIES
cjdns-admin cjdns-admin
cjdns-admin-client cjdns-admin-client
cjdns-memory-canary cjdns-memory-canary
cjdns-interface-tun-messagetype
cjdns-interface-pipeinterface cjdns-interface-pipeinterface
cjdadmintest cjdadmintest
interface interface
Expand Down
5 changes: 3 additions & 2 deletions interface/test/TUNInterface_ipv4_root_test.c
Expand Up @@ -22,6 +22,7 @@
#include "benc/Int.h" #include "benc/Int.h"
#include "interface/UDPInterface_pvt.h" #include "interface/UDPInterface_pvt.h"
#include "interface/TUNInterface.h" #include "interface/TUNInterface.h"
#include "interface/TUNMessageType.h"
#include "interface/TUNConfigurator.h" #include "interface/TUNConfigurator.h"
#include "memory/Allocator.h" #include "memory/Allocator.h"
#include "memory/MallocAllocator.h" #include "memory/MallocAllocator.h"
Expand Down Expand Up @@ -63,7 +64,7 @@ static int receivedMessageTUNCount = 0;
static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface) static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
{ {
receivedMessageTUNCount++; receivedMessageTUNCount++;
uint16_t ethertype = TUNInterface_popMessageType(msg); uint16_t ethertype = TUNMessageType_pop(msg);
if (ethertype != Ethernet_TYPE_IP4) { if (ethertype != Ethernet_TYPE_IP4) {
printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype)); printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
return 0; return 0;
Expand All @@ -79,7 +80,7 @@ static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
Bits_memcpyConst(header->destAddr, testAddrA, 4); Bits_memcpyConst(header->destAddr, testAddrA, 4);
Bits_memcpyConst(header->sourceAddr, testAddrB, 4); Bits_memcpyConst(header->sourceAddr, testAddrB, 4);


TUNInterface_pushMessageType(msg, ethertype); TUNMessageType_push(msg, ethertype);


return iface->sendMessage(msg, iface); return iface->sendMessage(msg, iface);
} }
Expand Down
5 changes: 3 additions & 2 deletions interface/test/TUNInterface_ipv6_root_test.c
Expand Up @@ -22,6 +22,7 @@
#include "benc/Int.h" #include "benc/Int.h"
#include "interface/UDPInterface_pvt.h" #include "interface/UDPInterface_pvt.h"
#include "interface/TUNInterface.h" #include "interface/TUNInterface.h"
#include "interface/TUNMessageType.h"
#include "interface/TUNConfigurator.h" #include "interface/TUNConfigurator.h"
#include "memory/Allocator.h" #include "memory/Allocator.h"
#include "memory/MallocAllocator.h" #include "memory/MallocAllocator.h"
Expand Down Expand Up @@ -62,7 +63,7 @@ static int receivedMessageTUNCount = 0;
static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface) static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
{ {
receivedMessageTUNCount++; receivedMessageTUNCount++;
uint16_t ethertype = TUNInterface_popMessageType(msg); uint16_t ethertype = TUNMessageType_pop(msg);
if (ethertype != Ethernet_TYPE_IP6) { if (ethertype != Ethernet_TYPE_IP6) {
printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype)); printf("Spurious packet with ethertype [%u]\n", Endian_bigEndianToHost16(ethertype));
return 0; return 0;
Expand All @@ -82,7 +83,7 @@ static uint8_t receiveMessageTUN(struct Message* msg, struct Interface* iface)
Bits_memcpyConst(header->destinationAddr, testAddrA, 16); Bits_memcpyConst(header->destinationAddr, testAddrA, 16);
Bits_memcpyConst(header->sourceAddr, testAddrB, 16); Bits_memcpyConst(header->sourceAddr, testAddrB, 16);


TUNInterface_pushMessageType(msg, ethertype); TUNMessageType_push(msg, ethertype);


return iface->sendMessage(msg, iface); return iface->sendMessage(msg, iface);
} }
Expand Down
1 change: 1 addition & 0 deletions net/CMakeLists.txt
Expand Up @@ -18,6 +18,7 @@ add_library(cjdnet
) )


target_link_libraries(cjdnet target_link_libraries(cjdnet
cjdns-interface-tun-messagetype
cjdbenc cjdbenc
crypto crypto
interface interface
Expand Down
6 changes: 3 additions & 3 deletions net/Ducttape.c
Expand Up @@ -21,7 +21,7 @@
#include "dht/DHTModuleRegistry.h" #include "dht/DHTModuleRegistry.h"
#include "dht/dhtcore/Node.h" #include "dht/dhtcore/Node.h"
#include "dht/dhtcore/RouterModule.h" #include "dht/dhtcore/RouterModule.h"
#include "interface/TUNInterface.h" #include "interface/TUNMessageType.h"
#include "interface/Interface.h" #include "interface/Interface.h"
#include "interface/SessionManager.h" #include "interface/SessionManager.h"
#include "util/log/Log.h" #include "util/log/Log.h"
Expand Down Expand Up @@ -299,7 +299,7 @@ static inline uint8_t incomingForMe(struct Message* message,
Bits_memmoveConst(message->bytes, context->ip6Header, Headers_IP6Header_SIZE); Bits_memmoveConst(message->bytes, context->ip6Header, Headers_IP6Header_SIZE);
} }


TUNInterface_pushMessageType(message, Ethernet_TYPE_IP6); TUNMessageType_push(message, Ethernet_TYPE_IP6);


context->userIf->sendMessage(message, context->userIf); context->userIf->sendMessage(message, context->userIf);
return Error_NONE; return Error_NONE;
Expand Down Expand Up @@ -407,7 +407,7 @@ static inline uint8_t incomingFromTun(struct Message* message,
{ {
struct Ducttape_pvt* context = Identity_cast((struct Ducttape_pvt*) iface->receiverContext); struct Ducttape_pvt* context = Identity_cast((struct Ducttape_pvt*) iface->receiverContext);


uint16_t ethertype = TUNInterface_popMessageType(message); uint16_t ethertype = TUNMessageType_pop(message);


struct Headers_IP6Header* header = (struct Headers_IP6Header*) message->bytes; struct Headers_IP6Header* header = (struct Headers_IP6Header*) message->bytes;


Expand Down
2 changes: 1 addition & 1 deletion test/TestFramework.c
Expand Up @@ -72,7 +72,7 @@ struct TestFramework* TestFramework_setUp(char* privateKey,


SerializationModule_register(registry, logger, allocator); SerializationModule_register(registry, logger, allocator);


struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand); struct IpTunnel* ipTun = IpTunnel_new(logger, base, allocator, rand, NULL);


struct Ducttape* dt = struct Ducttape* dt =
Ducttape_register((uint8_t*)privateKey, registry, routerModule, Ducttape_register((uint8_t*)privateKey, registry, routerModule,
Expand Down
10 changes: 5 additions & 5 deletions test/threeNodes_test.c
Expand Up @@ -27,7 +27,7 @@
#include "net/Ducttape_pvt.h" #include "net/Ducttape_pvt.h"
#include "wire/Headers.h" #include "wire/Headers.h"
#include "wire/Ethernet.h" #include "wire/Ethernet.h"
#include "interface/TUNInterface.h" #include "interface/TUNMessageType.h"


#include <stdio.h> #include <stdio.h>


Expand All @@ -36,23 +36,23 @@
#define TUNA 1 #define TUNA 1
uint8_t incomingTunC(struct Message* msg, struct Interface* iface) uint8_t incomingTunC(struct Message* msg, struct Interface* iface)
{ {
Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6); Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE); Message_shift(msg, -Headers_IP6Header_SIZE);
printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length); printf("Message from TUN in node C [%s] [%d]\n", msg->bytes, msg->length);
*((int*)iface->senderContext) = TUNC; *((int*)iface->senderContext) = TUNC;
return 0; return 0;
} }
uint8_t incomingTunB(struct Message* msg, struct Interface* iface) uint8_t incomingTunB(struct Message* msg, struct Interface* iface)
{ {
Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6); Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE); Message_shift(msg, -Headers_IP6Header_SIZE);
printf("Message from TUN in node B [%s]\n", msg->bytes); printf("Message from TUN in node B [%s]\n", msg->bytes);
*((int*)iface->senderContext) = TUNB; *((int*)iface->senderContext) = TUNB;
return 0; return 0;
} }
uint8_t incomingTunA(struct Message* msg, struct Interface* iface) uint8_t incomingTunA(struct Message* msg, struct Interface* iface)
{ {
Assert_true(TUNInterface_popMessageType(msg) == Ethernet_TYPE_IP6); Assert_true(TUNMessageType_pop(msg) == Ethernet_TYPE_IP6);
Message_shift(msg, -Headers_IP6Header_SIZE); Message_shift(msg, -Headers_IP6Header_SIZE);
uint8_t buff[1024]; uint8_t buff[1024];
Hex_encode(buff, 1024, msg->bytes, msg->length); Hex_encode(buff, 1024, msg->bytes, msg->length);
Expand Down Expand Up @@ -164,7 +164,7 @@ void sendMessage(struct ThreeNodes* tn,
Assert_always(false); Assert_always(false);
} }


TUNInterface_pushMessageType(msg, Ethernet_TYPE_IP6); TUNMessageType_push(msg, Ethernet_TYPE_IP6);
fromIf->receiveMessage(msg, fromIf); fromIf->receiveMessage(msg, fromIf);


if (to == tn->nodeA) { if (to == tn->nodeA) {
Expand Down

0 comments on commit b0472e8

Please sign in to comment.