From 77522a6b69ba08bedc12d80d7247c53b08088ada Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Fri, 10 Jan 2020 15:59:47 +0100 Subject: [PATCH] add srsUE parameter to change netns before creating TUN in GW --- srsue/hdr/stack/upper/gw.h | 2 ++ srsue/src/main.cc | 1 + srsue/src/stack/upper/gw.cc | 20 ++++++++++++++++---- srsue/ue.conf.example | 2 ++ 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/srsue/hdr/stack/upper/gw.h b/srsue/hdr/stack/upper/gw.h index 9ab03b895..df2c3746b 100644 --- a/srsue/hdr/stack/upper/gw.h +++ b/srsue/hdr/stack/upper/gw.h @@ -41,6 +41,7 @@ struct gw_args_t { std::string gw_level; int gw_hex_limit; } log; + std::string netns; std::string tun_dev_name; std::string tun_dev_netmask; }; @@ -78,6 +79,7 @@ private: bool running = false; bool run_enable = false; + int32_t netns_fd = 0; int32_t tun_fd = 0; struct ifreq ifr = {}; int32_t sock = 0; diff --git a/srsue/src/main.cc b/srsue/src/main.cc index 74cbfbc72..fa8fc344f 100644 --- a/srsue/src/main.cc +++ b/srsue/src/main.cc @@ -152,6 +152,7 @@ static int parse_args(all_args_t* args, int argc, char* argv[]) ("usim.pin", bpo::value(&args->stack.usim.pin), "PIN in case real SIM card is used") ("usim.reader", bpo::value(&args->stack.usim.reader)->default_value(""), "Force specific PCSC reader. Default: Try all available readers.") + ("gw.netns", bpo::value(&args->gw.netns)->default_value(""), "Network namespace to for TUN device (empty for default netns)") ("gw.ip_devname", bpo::value(&args->gw.tun_dev_name)->default_value("tun_srsue"), "Name of the tun_srsue device") ("gw.ip_netmask", bpo::value(&args->gw.tun_dev_netmask)->default_value("255.255.255.0"), "Netmask of the tun_srsue device") diff --git a/srsue/src/stack/upper/gw.cc b/srsue/src/stack/upper/gw.cc index 07d3830f2..5bc0d7bc6 100644 --- a/srsue/src/stack/upper/gw.cc +++ b/srsue/src/stack/upper/gw.cc @@ -342,12 +342,24 @@ int gw::init_if(char* err_str) return SRSLTE_ERROR_ALREADY_STARTED; } + // change into netns + if (!args.netns.empty()) { + std::string netns("/run/netns/"); + netns += args.netns; + netns_fd = open(netns.c_str(), O_RDONLY); + if (setns(netns_fd, CLONE_NEWNET) == -1) { + err_str = strerror(errno); + log.error("Failed to change netns: %s\n", err_str); + return SRSLTE_ERROR_CANT_START; + } + } + // Construct the TUN device tun_fd = open("/dev/net/tun", O_RDWR); log.info("TUN file descriptor = %d\n", tun_fd); if (0 > tun_fd) { err_str = strerror(errno); - log.debug("Failed to open TUN device: %s\n", err_str); + log.error("Failed to open TUN device: %s\n", err_str); return SRSLTE_ERROR_CANT_START; } @@ -358,7 +370,7 @@ int gw::init_if(char* err_str) ifr.ifr_ifrn.ifrn_name[IFNAMSIZ - 1] = 0; if (0 > ioctl(tun_fd, TUNSETIFF, &ifr)) { err_str = strerror(errno); - log.debug("Failed to set TUN device name: %s\n", err_str); + log.error("Failed to set TUN device name: %s\n", err_str); close(tun_fd); return SRSLTE_ERROR_CANT_START; } @@ -367,14 +379,14 @@ int gw::init_if(char* err_str) sock = socket(AF_INET, SOCK_DGRAM, 0); if (0 > ioctl(sock, SIOCGIFFLAGS, &ifr)) { err_str = strerror(errno); - log.debug("Failed to bring up socket: %s\n", err_str); + log.error("Failed to bring up socket: %s\n", err_str); close(tun_fd); return SRSLTE_ERROR_CANT_START; } ifr.ifr_flags |= IFF_UP | IFF_RUNNING; if (0 > ioctl(sock, SIOCSIFFLAGS, &ifr)) { err_str = strerror(errno); - log.debug("Failed to set socket flags: %s\n", err_str); + log.error("Failed to set socket flags: %s\n", err_str); close(tun_fd); return SRSLTE_ERROR_CANT_START; } diff --git a/srsue/ue.conf.example b/srsue/ue.conf.example index 5ae446691..0afa78bdb 100644 --- a/srsue/ue.conf.example +++ b/srsue/ue.conf.example @@ -167,10 +167,12 @@ imei = 353490069873319 ##################################################################### # GW configuration # +# netns: Network namespace to create TUN device. Default: empty # ip_devname: Name of the tun_srsue device. Default: tun_srsue # ip_netmask: Netmask of the tun_srsue device. Default: 255.255.255.0 ##################################################################### [gw] +#netns = #ip_devname = tun_srsue #ip_netmask = 255.255.255.0