From 764746cc4c3a3c0a140cff7d2ac3d8a1eb3949f5 Mon Sep 17 00:00:00 2001 From: Mark Qvist Date: Thu, 28 May 2020 11:32:46 +0200 Subject: [PATCH] Fixed inadvertently setting interface flags when setting MTU --- TAP.c | 73 +++++++++++++++++++++++++++++------------------------ tncattach.c | 2 +- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/TAP.c b/TAP.c index 1b6c2fd..77e432c 100644 --- a/TAP.c +++ b/TAP.c @@ -65,52 +65,59 @@ int open_tap(void) { } if (!noup) { - ifr.ifr_flags |= IFF_UP | IFF_RUNNING; - if (ioctl(inet, SIOCSIFFLAGS, &ifr) < 0) { - perror("Could not bring up interface"); + if (ioctl(inet, SIOCGIFFLAGS, &ifr) < 0) { + perror("Could not get interface flags from kernel"); close(inet); cleanup(); exit(1); } else { - if (set_ipv4) { - struct ifreq a_ifr; - struct sockaddr_in addr, snm; + ifr.ifr_flags |= IFF_UP | IFF_RUNNING; + if (ioctl(inet, SIOCSIFFLAGS, &ifr) < 0) { + perror("Could not bring up interface"); + close(inet); + cleanup(); + exit(1); + } else { + if (set_ipv4) { + struct ifreq a_ifr; + struct sockaddr_in addr, snm; - memset(&a_ifr, 0, sizeof(a_ifr)); - memset(&addr, 0, sizeof(addr)); - memset(&snm, 0, sizeof(addr)); - strncpy(a_ifr.ifr_name, ifr.ifr_name, IFNAMSIZ); - addr.sin_family = AF_INET; - snm.sin_family = AF_INET; + memset(&a_ifr, 0, sizeof(a_ifr)); + memset(&addr, 0, sizeof(addr)); + memset(&snm, 0, sizeof(addr)); + strncpy(a_ifr.ifr_name, ifr.ifr_name, IFNAMSIZ); + addr.sin_family = AF_INET; + snm.sin_family = AF_INET; - int addr_conversion = inet_pton(AF_INET, ipv4_addr, &(addr.sin_addr)); - if (addr_conversion != 1) { - printf("Error: Invalid IPv4 address specified\r\n"); - close(inet); - cleanup(); - exit(1); - } else { - a_ifr.ifr_addr = *(struct sockaddr*)&addr; - if (ioctl(inet, SIOCSIFADDR, &a_ifr) < 0) { - perror("Could not set IP-address"); + int addr_conversion = inet_pton(AF_INET, ipv4_addr, &(addr.sin_addr)); + if (addr_conversion != 1) { + printf("Error: Invalid IPv4 address specified\r\n"); close(inet); cleanup(); exit(1); } else { - if (set_netmask) { - int snm_conversion = inet_pton(AF_INET, netmask, &(snm.sin_addr)); - if (snm_conversion != 1) { - printf("Error: Invalid subnet mask specified\r\n"); - close(inet); - cleanup(); - exit(1); - } else { - a_ifr.ifr_addr = *(struct sockaddr*)&snm; - if (ioctl(inet, SIOCSIFNETMASK, &a_ifr) < 0) { - perror("Could not set subnet mask"); + a_ifr.ifr_addr = *(struct sockaddr*)&addr; + if (ioctl(inet, SIOCSIFADDR, &a_ifr) < 0) { + perror("Could not set IP-address"); + close(inet); + cleanup(); + exit(1); + } else { + if (set_netmask) { + int snm_conversion = inet_pton(AF_INET, netmask, &(snm.sin_addr)); + if (snm_conversion != 1) { + printf("Error: Invalid subnet mask specified\r\n"); close(inet); cleanup(); exit(1); + } else { + a_ifr.ifr_addr = *(struct sockaddr*)&snm; + if (ioctl(inet, SIOCSIFNETMASK, &a_ifr) < 0) { + perror("Could not set subnet mask"); + close(inet); + cleanup(); + exit(1); + } } } } diff --git a/tncattach.c b/tncattach.c index 67c4742..1382879 100644 --- a/tncattach.c +++ b/tncattach.c @@ -223,7 +223,7 @@ void read_loop(void) { exit(1); } -const char *argp_program_version = "tncattach 0.1.3"; +const char *argp_program_version = "tncattach 0.1.4"; const char *argp_program_bug_address = ""; static char doc[] = "\r\nAttach TNC devices as system network interfaces\vTo attach the TNC connected to /dev/ttyUSB0 as an ethernet device with an MTU of 512 bytes and assign an IPv4 address, while filtering IPv6 traffic, use:\r\n\r\n\ttncattach /dev/ttyUSB0 115200 -m 512 -e --noipv6 --ipv4 10.0.0.1/24\r\n\r\nStation identification can be performed automatically. Use the --id and --idinterval options. Identification beacons will be transmitted if the amount of time since the last identification is greater than the configured interval and there is any data to send. Channel capacity will therefore not be wasted on IDs for stations that are not actively transmitting."; static char args_doc[] = "port baudrate";