Skip to content
Browse files

Added automatic setting of MTU.

  • Loading branch information...
1 parent 9c5ea8c commit cb806ed3d0f63d831586b99c45dd8d3f78e7c401 Caleb James DeLisle committed Aug 10, 2012
View
26 cjdroute.c
@@ -58,6 +58,25 @@
#define DEFAULT_TUN_DEV "cjdroute0"
+/**
+ * The worst possible packet overhead.
+ * assuming the packet needs to be handed off to another node
+ * because we have no route to the destination.
+ * and the CryptoAuths to both the destination and the handoff node are both timed out.
+ */
+#define WORST_CASE_OVERHEAD ( \
+ /* TODO: Headers_IPv4_SIZE */ 20 \
+ + Headers_UDPHeader_SIZE \
+ + /* Nonce */ 4 \
+ + /* Poly1306 authenticator */ 16 \
+ + Headers_SwitchHeader_SIZE \
+ + Headers_CryptoAuth_SIZE \
+ + Headers_IP6Header_SIZE \
+ + Headers_CryptoAuth_SIZE \
+)
+
+/** The default MTU, assuming the external MTU is 1492 (common for PPPoE DSL) */
+#define DEFAULT_MTU (1492 - WORST_CASE_OVERHEAD)
struct User
{
@@ -523,6 +542,13 @@ int main(int argc, char** argv)
Log_warn(logger, "Unable to configure ip address [%s]", jmp.message);
}
+ Jmp_try(jmp) {
+ TUNConfigurator_setMTU(
+ assignedTunName, DEFAULT_MTU, logger, &jmp.handler);
+ } Jmp_catch {
+ Log_warn(logger, "Unable to set MTU [%s], skipping.", jmp.message);
+ }
+
struct TUNInterface* tun = TUNInterface_new(tunPtr, eventBase, allocator);
routerIf = &tun->iface;
View
25 interface/TUNConfigurator.h
@@ -26,6 +26,12 @@
#define TUNConfigurator_IFNAMSIZ 16
/**
+ * The error code for any function which fails to get a socket for configuring an interface.
+ * Multiple functions may return this error.
+ */
+#define TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET -2
+
+/**
* Open the TUN device.
*
* @param interfaceName the interface name you *want* to use or NULL to let the kernel decide.
@@ -56,12 +62,31 @@ void* TUNConfigurator_initTun(const char* interfaceName,
* @param logger
* @param eh if this function fails, it will raise one of the following.
* TUNConfigurator_setIpAddress_INTERNAL Catch all exception code for failures.
+ * TUNConfigurator_setIpAddress_ADMIN_SOCKET Error getting admin socket for interface.
*/
#define TUNConfigurator_setIpAddress_INTERNAL -1
+#define TUNConfigurator_setIpAddress_ADMIN_SOCKET TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET
void TUNConfigurator_setIpAddress(const char* interfaceName,
const uint8_t address[16],
int prefixLen,
struct Log* logger,
struct Except* eh);
+/**
+ * Set the MTU of an interface.
+ *
+ * @param interfaceName the name of the interface to set the MTU for.
+ * @param mtu the desired MTU.
+ * @param logger where to write information.
+ * @param eh if this function fails, it will raise one of the following.
+ * TUNConfigurator_setMTU_INTERNAL Catch all exception code for failures.
+ * TUNConfigurator_setMTU_ADMIN_SOCKET Error getting admin socket for interface.
+ */
+#define TUNConfigurator_setMTU_INTERNAL -1
+#define TUNConfigurator_setMTU_ADMIN_SOCKET TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET
+void TUNConfigurator_setMTU(const char* interfaceName,
+ uint32_t mtu,
+ struct Log* logger,
+ struct Except* eh);
+
#endif
View
8 interface/TUNConfigurator_Illumos.c
@@ -244,3 +244,11 @@ void* TUNConfigurator_initTun(const char* interfaceName,
intptr_t ret = (intptr_t) tunFd;
return (void*) ret;
}
+
+void TUNConfigurator_setMTU(const char* interfaceName,
+ uint32_t mtu,
+ struct Log* logger,
+ struct Except* eh)
+{
+ Except_raise(eh, TUNConfigurator_setMTU_INTERNAL, "Not implemented in IlLumos");
+}
View
58 interface/TUNConfigurator_Linux.c
@@ -93,29 +93,46 @@ void* TUNConfigurator_initTun(const char* interfaceName,
return (void*) tunPtr;
}
-void TUNConfigurator_setIpAddress(const char* interfaceName,
- const uint8_t address[16],
- int prefixLen,
- struct Log* logger,
- struct Except* eh)
+/**
+ * Get a socket and ifRequest for a given interface by name.
+ *
+ * @param interfaceName the name of the interface, eg: tun0
+ * @param eg an exception handler in case something goes wrong.
+ * this will send a TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET for all errors.
+ * @param ifRequestOut an ifreq which will be populated with the interface index of the interface.
+ * @return a socket for interacting with this interface.
+ */
+static int socketForIfName(const char* interfaceName,
+ struct Except* eh,
+ struct ifreq* ifRequestOut)
{
int s;
- struct ifreq ifRequest;
- struct in6_ifreq ifr6;
if ((s = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) {
- Except_raise(eh, TUNConfigurator_setIpAddress_INTERNAL,
+ Except_raise(eh, TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET,
"socket() failed: [%s]", strerror(errno));
}
- strncpy(ifRequest.ifr_name, interfaceName, IFNAMSIZ);
+ strncpy(ifRequestOut->ifr_name, interfaceName, IFNAMSIZ);
- if (ioctl(s, SIOCGIFINDEX, &ifRequest) < 0) {
+ if (ioctl(s, SIOCGIFINDEX, ifRequestOut) < 0) {
int err = errno;
close(s);
- Except_raise(eh, TUNConfigurator_setIpAddress_INTERNAL,
+ Except_raise(eh, TUNConfigurator_ERROR_GETTING_ADMIN_SOCKET,
"ioctl(SIOCGIFINDEX) failed: [%s]", strerror(err));
}
+ return s;
+}
+
+void TUNConfigurator_setIpAddress(const char* interfaceName,
+ const uint8_t address[16],
+ int prefixLen,
+ struct Log* logger,
+ struct Except* eh)
+{
+ struct ifreq ifRequest;
+ struct in6_ifreq ifr6;
+ int s = socketForIfName(interfaceName, eh, &ifRequest);
ifr6.ifr6_ifindex = ifRequest.ifr_ifindex;
ifr6.ifr6_prefixlen = prefixLen;
@@ -138,3 +155,22 @@ void TUNConfigurator_setIpAddress(const char* interfaceName,
"ioctl(SIOCSIFADDR) failed: [%s]", strerror(err));
}
}
+
+void TUNConfigurator_setMTU(const char* interfaceName,
+ uint32_t mtu,
+ struct Log* logger,
+ struct Except* eh)
+{
+ Log_info(logger, "Setting MTU for device [%s] to [%u] bytes.", interfaceName, mtu);
+
+ struct ifreq ifRequest;
+ int s = socketForIfName(interfaceName, eh, &ifRequest);
+
+ ifRequest.ifr_mtu = mtu;
+ if (ioctl(s, SIOCSIFMTU, &ifRequest) < 0) {
+ int err = errno;
+ close(s);
+ Except_raise(eh, TUNConfigurator_setMTU_INTERNAL,
+ "ioctl(SIOCSIFMTU) failed: [%s]", strerror(err));
+ }
+}
View
8 interface/TUNConfigurator_OSX.c
@@ -204,3 +204,11 @@ void TUNConfigurator_setIpAddress(const char* interfaceName,
close(s);
}
+
+void TUNConfigurator_setMTU(const char* interfaceName,
+ uint32_t mtu,
+ struct Log* logger,
+ struct Except* eh)
+{
+ Except_raise(eh, TUNConfigurator_setMTU_INTERNAL, "Not implemented in OSX");
+}

0 comments on commit cb806ed

Please sign in to comment.
Something went wrong with that request. Please try again.