From b648872c1fc030ed4949aae636c565908c0fe283 Mon Sep 17 00:00:00 2001 From: Logan oos Even <46396513+Logan007@users.noreply.github.com> Date: Sat, 10 Jul 2021 20:37:58 +0200 Subject: [PATCH] modified route setup (#717) --- src/edge.c | 8 +++++++- src/edge_utils.c | 25 ++++++++++++++----------- win32/n2n_win32.h | 3 ++- win32/wintap.c | 24 ++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/edge.c b/src/edge.c index 3b670b4..4234cfe 100644 --- a/src/edge.c +++ b/src/edge.c @@ -52,6 +52,7 @@ int fetch_and_eventually_process_data (n2n_edge_t *eee, SOCKET sock, time_t now); int resolve_create_thread (n2n_resolve_parameter_t **param, struct peer_info *sn_list); int resolve_check (n2n_resolve_parameter_t *param, uint8_t resolution_request, time_t now); +int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes); /* ***************************************************** */ @@ -1137,7 +1138,7 @@ int main (int argc, char* argv[]) { } } - if(runlevel == 4) { /* configure the TUNTAP device */ + if(runlevel == 4) { /* configure the TUNTAP device, including routes */ if(tuntap_open(&tuntap, eee->tuntap_priv_conf.tuntap_dev_name, eee->tuntap_priv_conf.ip_mode, eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask, eee->tuntap_priv_conf.device_mac, eee->tuntap_priv_conf.mtu @@ -1151,6 +1152,11 @@ int main (int argc, char* argv[]) { eee->tuntap_priv_conf.ip_addr, eee->tuntap_priv_conf.netmask, macaddr_str(mac_buf, eee->device.mac_addr)); + // routes + if(edge_init_routes(eee, eee->conf.routes, eee->conf.num_routes) < 0) { + traceEvent(TRACE_ERROR, "routes setup failed"); + exit(1); + } runlevel = 5; // no more answers required seek_answer = 0; diff --git a/src/edge_utils.c b/src/edge_utils.c index 5931b4d..8b27675 100644 --- a/src/edge_utils.c +++ b/src/edge_utils.c @@ -41,7 +41,7 @@ static void check_peer_registration_needed (n2n_edge_t *eee, const n2n_sock_t *peer); static int edge_init_sockets (n2n_edge_t *eee); -static int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes); +int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes); static void edge_cleanup_routes (n2n_edge_t *eee); static void check_known_peer_sock_change (n2n_edge_t *eee, @@ -417,11 +417,6 @@ n2n_edge_t* edge_init (const n2n_edge_conf_t *conf, int *rv) { goto edge_init_error; } - if(edge_init_routes(eee, eee->conf.routes, eee->conf.num_routes) < 0) { - traceEvent(TRACE_ERROR, "routes setup failed"); - goto edge_init_error; - } - eee->network_traffic_filter = create_network_traffic_filter(); network_traffic_filter_add_rule(eee->network_traffic_filter, eee->conf.network_traffic_filter_rules); @@ -3385,12 +3380,14 @@ static int edge_init_routes_linux (n2n_edge_t *eee, n2n_route_t *routes, uint16_ /* ************************************** */ -static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) { +static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes, uint8_t verb /* 0 = add, 1 = delete */) { #ifdef WIN32 int i; struct in_addr net_addr, gateway; char c_net_addr[32]; char c_gateway[32]; + char c_interface[32]; + char c_verb[32]; char cmd[256]; for(i = 0; i < num_routes; i++) { @@ -3404,12 +3401,13 @@ static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t memcpy(&gateway, &(route->gateway), sizeof(gateway)); _snprintf(c_net_addr, sizeof(c_net_addr), inet_ntoa(net_addr)); _snprintf(c_gateway, sizeof(c_gateway), inet_ntoa(gateway)); - _snprintf(cmd, sizeof(cmd), "route add %s/%d %s > nul", c_net_addr, route->net_bitlen, c_gateway); + _snprintf(c_interface, sizeof(c_interface), "if %u", eee->device.if_idx); + _snprintf(c_verb, sizeof(c_verb), verb ? "delete" : "add"); + _snprintf(cmd, sizeof(cmd), "route %s %s/%d %s %s > nul", c_verb, c_net_addr, route->net_bitlen, c_gateway, c_interface); traceEvent(TRACE_NORMAL, "ROUTE CMD = '%s'\n", cmd); system(cmd); } } - #endif // WIN32 return (0); @@ -3420,13 +3418,13 @@ static int edge_init_routes_win (n2n_edge_t *eee, n2n_route_t *routes, uint16_t /* Add the user-provided routes to the linux routing table. Network routes * are bound to the n2n TAP device, so they are automatically removed when * the TAP device is destroyed. */ -static int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) { +int edge_init_routes (n2n_edge_t *eee, n2n_route_t *routes, uint16_t num_routes) { #ifdef __linux__ return edge_init_routes_linux(eee, routes, num_routes); #endif #ifdef WIN32 - return edge_init_routes_win(eee, routes, num_routes); + return edge_init_routes_win(eee, routes, num_routes, 0 /* add */); #endif return 0; } @@ -3441,6 +3439,11 @@ static void edge_cleanup_routes (n2n_edge_t *eee) { free(eee->sn_route_to_clean); } #endif + +#ifdef WIN32 + edge_init_routes_win(eee, eee->conf.routes, eee->conf.num_routes, 1 /* del */); +#endif + } /* ************************************** */ diff --git a/win32/n2n_win32.h b/win32/n2n_win32.h index e47d690..2f5dd3c 100644 --- a/win32/n2n_win32.h +++ b/win32/n2n_win32.h @@ -24,7 +24,7 @@ #include #include #include - +#include #include "wintap.h" @@ -70,6 +70,7 @@ typedef struct tuntap_dev { HANDLE device_handle; char *device_name; char *ifName; + int if_idx; OVERLAPPED overlap_read, overlap_write; n2n_mac_t mac_addr; uint32_t ip_addr; diff --git a/win32/wintap.c b/win32/wintap.c index bb9304d..0f9a497 100644 --- a/win32/wintap.c +++ b/win32/wintap.c @@ -253,6 +253,30 @@ int open_wintap(struct tuntap_dev *device, /* ************************************** */ + /* interface index, required for routing */ + + ULONG buffer_len = 0; + IP_ADAPTER_INFO *buffer; + + // get required buffer size and allocate buffer + GetAdaptersInfo(NULL, &buffer_len); + buffer = malloc(buffer_len); + + // find device by name and get its index + if(buffer && !GetAdaptersInfo(buffer, &buffer_len)) { + IP_ADAPTER_INFO *i; + for(i = buffer; i != NULL; i = i->Next) { + if(!strcmp(device->device_name, i->AdapterName)) { + device->if_idx = i->Index; + break; + } + } + } + + free(buffer); + + /* ************************************** */ + if(device_mac[0]) set_interface_mac(device, device_mac);