Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014-2017, Travelping GmbH <info@travelping.com> |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 3 | * Copyright (C) 2020, Harald Welte <laforge@gnumonks.org> |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU Affero General Public License as |
| 7 | * published by the Free Software Foundation, either version 3 of the |
| 8 | * License, or (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU Affero General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Affero General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #if defined(__linux__) |
| 21 | |
| 22 | #ifdef HAVE_CONFIG_H |
| 23 | # include "config.h" |
| 24 | #endif |
| 25 | |
| 26 | #ifndef _GNU_SOURCE |
| 27 | # define _GNU_SOURCE |
| 28 | #endif |
| 29 | |
| 30 | #include <errno.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sched.h> |
| 35 | #include <signal.h> |
| 36 | #include <sys/types.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/socket.h> |
| 39 | #include <sys/mount.h> |
| 40 | #include <sys/param.h> |
| 41 | #include <fcntl.h> |
| 42 | #include <errno.h> |
| 43 | |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 44 | #include <osmocom/core/utils.h> |
| 45 | |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 46 | #include "netns.h" |
| 47 | |
| 48 | #define NETNS_PATH "/var/run/netns" |
| 49 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 50 | /*! default namespace of the GGSN process */ |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 51 | static int default_nsfd = -1; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 52 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 53 | /*! switch to a (non-default) namespace, store existing signal mask in oldmask. |
| 54 | * \param[in] nsfd file descriptor representing the namespace to whch we shall switch |
| 55 | * \param[out] oldmask caller-provided memory location to which old signal mask is stored |
| 56 | * \ returns 0 on success or negative (errno) in case of error */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 57 | int switch_ns(int nsfd, sigset_t *oldmask) |
| 58 | { |
| 59 | sigset_t intmask; |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 60 | int rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 61 | |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 62 | OSMO_ASSERT(default_nsfd >= 0); |
| 63 | |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 64 | if (sigfillset(&intmask) < 0) |
| 65 | return -errno; |
| 66 | if ((rc = sigprocmask(SIG_BLOCK, &intmask, oldmask)) != 0) |
| 67 | return -rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 68 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 69 | if (setns(nsfd, CLONE_NEWNET) < 0) { |
| 70 | /* restore old mask if we couldn't switch the netns */ |
| 71 | sigprocmask(SIG_SETMASK, oldmask, NULL); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 72 | return -errno; |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 73 | } |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 74 | return 0; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 75 | } |
| 76 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 77 | /*! switch back to the default namespace, restoring signal mask. |
| 78 | * \param[in] oldmask signal mask to restore after returning to default namespace |
| 79 | * \returns 0 on successs; negative errno value in case of error */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 80 | int restore_ns(sigset_t *oldmask) |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 81 | { |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 82 | OSMO_ASSERT(default_nsfd >= 0); |
| 83 | |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 84 | int rc; |
| 85 | if (setns(default_nsfd, CLONE_NEWNET) < 0) |
| 86 | return -errno; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 87 | |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 88 | if ((rc = sigprocmask(SIG_SETMASK, oldmask, NULL)) != 0) |
| 89 | return -rc; |
| 90 | return 0; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 93 | /*! open a file from within specified network namespace */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 94 | int open_ns(int nsfd, const char *pathname, int flags) |
| 95 | { |
| 96 | sigset_t intmask, oldmask; |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 97 | int ret; |
| 98 | int fd = -1; |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 99 | int rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 100 | |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 101 | OSMO_ASSERT(default_nsfd >= 0); |
| 102 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 103 | /* mask off all signals, store old signal mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 104 | if (sigfillset(&intmask) < 0) |
| 105 | return -errno; |
| 106 | if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0) |
| 107 | return -rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 108 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 109 | /* associate the calling thread with namespace file descriptor */ |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 110 | if (setns(nsfd, CLONE_NEWNET) < 0) { |
| 111 | ret = -errno; |
| 112 | goto restore_sigmask; |
| 113 | } |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 114 | /* open the requested file/path */ |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 115 | if ((fd = open(pathname, flags)) < 0) { |
| 116 | ret = -errno; |
| 117 | goto restore_defaultns; |
| 118 | } |
| 119 | ret = fd; |
| 120 | |
| 121 | restore_defaultns: |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 122 | /* return back to default namespace */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 123 | if (setns(default_nsfd, CLONE_NEWNET) < 0) { |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 124 | if (fd >= 0) |
| 125 | close(fd); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 126 | return -errno; |
| 127 | } |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 128 | |
| 129 | restore_sigmask: |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 130 | /* restore process mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 131 | if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) { |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 132 | if (fd >= 0) |
| 133 | close(fd); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 134 | return -rc; |
| 135 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 136 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 137 | return ret; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 140 | /*! create a socket in another namespace. |
| 141 | * Switches temporarily to namespace indicated by nsfd, creates a socket in |
| 142 | * that namespace and then returns to the default namespace. |
| 143 | * \param[in] nsfd File descriptor of the namspace in which to create socket |
| 144 | * \param[in] domain Domain of the socket (AF_INET, ...) |
| 145 | * \param[in] type Type of the socket (SOCK_STREAM, ...) |
| 146 | * \param[in] protocol Protocol of the socket (IPPROTO_TCP, ...) |
| 147 | * \returns 0 on success; negative errno in case of error */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 148 | int socket_ns(int nsfd, int domain, int type, int protocol) |
| 149 | { |
| 150 | sigset_t intmask, oldmask; |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 151 | int ret; |
| 152 | int sk = -1; |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 153 | int rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 154 | |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 155 | OSMO_ASSERT(default_nsfd >= 0); |
| 156 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 157 | /* mask off all signals, store old signal mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 158 | if (sigfillset(&intmask) < 0) |
| 159 | return -errno; |
| 160 | if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0) |
| 161 | return -rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 162 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 163 | /* associate the calling thread with namespace file descriptor */ |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 164 | if (setns(nsfd, CLONE_NEWNET) < 0) { |
| 165 | ret = -errno; |
| 166 | goto restore_sigmask; |
| 167 | } |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 168 | |
| 169 | /* create socket of requested domain/type/proto */ |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 170 | if ((sk = socket(domain, type, protocol)) < 0) { |
| 171 | ret = -errno; |
| 172 | goto restore_defaultns; |
| 173 | } |
| 174 | ret = sk; |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 175 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 176 | restore_defaultns: |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 177 | /* return back to default namespace */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 178 | if (setns(default_nsfd, CLONE_NEWNET) < 0) { |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 179 | if (sk >= 0) |
| 180 | close(sk); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 181 | return -errno; |
| 182 | } |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 183 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 184 | restore_sigmask: |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 185 | /* restore process mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 186 | if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) { |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 187 | if (sk >= 0) |
| 188 | close(sk); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 189 | return -rc; |
| 190 | } |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 191 | return ret; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 192 | } |
| 193 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 194 | /*! initialize this network namespace helper module. |
| 195 | * Must be called before using any other functions of this file. |
| 196 | * \returns 0 on success; negative errno in case of error */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 197 | int init_netns() |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 198 | { |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 199 | /* store the default namespace for later reference */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 200 | if ((default_nsfd = open("/proc/self/ns/net", O_RDONLY)) < 0) |
| 201 | return -errno; |
| 202 | return 0; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 203 | } |
| 204 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 205 | /*! create obtain file descriptor for network namespace of give name. |
| 206 | * Creates /var/run/netns if it doesn't exist already. |
| 207 | * \param[in] name Name of the network namespace (in /var/run/netns/) |
| 208 | * \returns File descriptor of network namespace; negative errno in case of error */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 209 | int get_nsfd(const char *name) |
| 210 | { |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 211 | int ret = 0; |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 212 | int rc; |
| 213 | int fd; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 214 | sigset_t intmask, oldmask; |
| 215 | char path[MAXPATHLEN] = NETNS_PATH; |
| 216 | |
Harald Welte | 61b010c | 2020-04-11 12:14:56 +0200 | [diff] [blame] | 217 | OSMO_ASSERT(default_nsfd >= 0); |
| 218 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 219 | /* create /var/run/netns, if it doesn't exist already */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 220 | rc = mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH); |
| 221 | if (rc < 0 && errno != EEXIST) |
| 222 | return rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 223 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 224 | /* create /var/run/netns/[name], if it doesn't exist already */ |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 225 | snprintf(path, sizeof(path), "%s/%s", NETNS_PATH, name); |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 226 | fd = open(path, O_RDONLY|O_CREAT|O_EXCL, 0); |
| 227 | if (fd < 0) { |
| 228 | if (errno == EEXIST) { |
| 229 | if ((fd = open(path, O_RDONLY)) < 0) |
| 230 | return -errno; |
| 231 | return fd; |
| 232 | } |
| 233 | return -errno; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 234 | } |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 235 | if (close(fd) < 0) |
| 236 | return -errno; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 237 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 238 | /* mask off all signals, store old signal mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 239 | if (sigfillset(&intmask) < 0) |
| 240 | return -errno; |
| 241 | if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0) |
| 242 | return -rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 243 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 244 | /* create a new network namespace */ |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 245 | if (unshare(CLONE_NEWNET) < 0) { |
| 246 | ret = -errno; |
| 247 | goto restore_sigmask; |
| 248 | } |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 249 | if (mount("/proc/self/ns/net", path, "none", MS_BIND, NULL) < 0) |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 250 | ret = -errno; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 251 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 252 | /* switch back to default namespace */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 253 | if (setns(default_nsfd, CLONE_NEWNET) < 0) |
| 254 | return -errno; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 255 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 256 | restore_sigmask: |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 257 | /* restore process mask */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 258 | if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) |
| 259 | return -rc; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 260 | |
Harald Welte | 8398bcc | 2020-04-11 12:08:17 +0200 | [diff] [blame] | 261 | /* might have been set above in case mount fails */ |
| 262 | if (ret < 0) |
| 263 | return ret; |
| 264 | |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 265 | /* finally, open the created namespace file descriptor from default ns */ |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 266 | if ((fd = open(path, O_RDONLY)) < 0) |
| 267 | return -errno; |
Harald Welte | 20d9d15 | 2020-04-11 11:55:53 +0200 | [diff] [blame] | 268 | |
Pau Espin Pedrol | ad6eaa2 | 2020-02-25 12:17:29 +0100 | [diff] [blame] | 269 | return fd; |
Andreas Schultz | b629240 | 2018-10-05 13:58:45 +0100 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | #endif |