KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 1 | #define _GNU_SOURCE |
| 2 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 3 | #include <sched.h> |
| 4 | #include <stdio.h> |
| 5 | #include <sys/stat.h> |
| 6 | #include <fcntl.h> |
| 7 | #include <sys/types.h> |
| 8 | #include <unistd.h> |
| 9 | #include <sys/socket.h> |
| 10 | |
| 11 | #include <string.h> |
Willy Tarreau | 3fa0e2a | 2016-03-17 05:39:53 +0100 | [diff] [blame] | 12 | |
| 13 | #include <common/namespace.h> |
| 14 | #include <common/compiler.h> |
| 15 | #include <common/hash.h> |
| 16 | #include <common/errors.h> |
| 17 | #include <proto/log.h> |
| 18 | #include <types/global.h> |
| 19 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 20 | /* Opens the namespace <ns_name> and returns the FD or -1 in case of error |
| 21 | * (check errno). |
| 22 | */ |
| 23 | static int open_named_namespace(const char *ns_name) |
| 24 | { |
| 25 | if (chunk_printf(&trash, "/var/run/netns/%s", ns_name) < 0) |
| 26 | return -1; |
Krisztián Kovács (kkovacs) | 538aa71 | 2019-09-20 14:48:19 +0000 | [diff] [blame] | 27 | return open(trash.area, O_RDONLY | O_CLOEXEC); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static int default_namespace = -1; |
| 31 | |
| 32 | static int init_default_namespace() |
| 33 | { |
| 34 | if (chunk_printf(&trash, "/proc/%d/ns/net", getpid()) < 0) |
| 35 | return -1; |
Krisztián Kovács (kkovacs) | 538aa71 | 2019-09-20 14:48:19 +0000 | [diff] [blame] | 36 | default_namespace = open(trash.area, O_RDONLY | O_CLOEXEC); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 37 | return default_namespace; |
| 38 | } |
| 39 | |
| 40 | static struct eb_root namespace_tree_root = EB_ROOT; |
| 41 | |
| 42 | int netns_init(void) |
| 43 | { |
| 44 | int err_code = 0; |
| 45 | |
| 46 | /* if no namespaces have been defined in the config then |
| 47 | * there is no point in trying to initialize anything: |
| 48 | * my_socketat() will never be called with a valid namespace |
| 49 | * structure and thus switching back to the default namespace |
| 50 | * is not needed either */ |
| 51 | if (!eb_is_empty(&namespace_tree_root)) { |
| 52 | if (init_default_namespace() < 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 53 | ha_alert("Failed to open the default namespace.\n"); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 54 | err_code |= ERR_ALERT | ERR_FATAL; |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return err_code; |
| 59 | } |
| 60 | |
| 61 | struct netns_entry* netns_store_insert(const char *ns_name) |
| 62 | { |
| 63 | struct netns_entry *entry = NULL; |
| 64 | int fd = open_named_namespace(ns_name); |
| 65 | if (fd == -1) |
| 66 | goto out; |
| 67 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 68 | entry = calloc(1, sizeof(*entry)); |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 69 | if (!entry) |
| 70 | goto out; |
| 71 | entry->fd = fd; |
| 72 | entry->node.key = strdup(ns_name); |
| 73 | entry->name_len = strlen(ns_name); |
| 74 | ebis_insert(&namespace_tree_root, &entry->node); |
| 75 | out: |
| 76 | return entry; |
| 77 | } |
| 78 | |
| 79 | const struct netns_entry* netns_store_lookup(const char *ns_name, size_t ns_name_len) |
| 80 | { |
| 81 | struct ebpt_node *node; |
| 82 | |
| 83 | node = ebis_lookup_len(&namespace_tree_root, ns_name, ns_name_len); |
| 84 | if (node) |
| 85 | return ebpt_entry(node, struct netns_entry, node); |
| 86 | else |
| 87 | return NULL; |
| 88 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 89 | |
| 90 | /* Opens a socket in the namespace described by <ns> with the parameters <domain>, |
| 91 | * <type> and <protocol> and returns the FD or -1 in case of error (check errno). |
| 92 | */ |
| 93 | int my_socketat(const struct netns_entry *ns, int domain, int type, int protocol) |
| 94 | { |
| 95 | int sock; |
| 96 | |
Willy Tarreau | 70f289c | 2015-10-20 15:14:07 +0200 | [diff] [blame] | 97 | if (default_namespace >= 0 && ns && setns(ns->fd, CLONE_NEWNET) == -1) |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 98 | return -1; |
Willy Tarreau | 7520e4f | 2018-11-11 14:38:09 +0100 | [diff] [blame] | 99 | |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 100 | sock = socket(domain, type, protocol); |
| 101 | |
Willy Tarreau | 70f289c | 2015-10-20 15:14:07 +0200 | [diff] [blame] | 102 | if (default_namespace >= 0 && ns && setns(default_namespace, CLONE_NEWNET) == -1) { |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 103 | close(sock); |
| 104 | return -1; |
| 105 | } |
KOVACS Krisztian | b3e54fe | 2014-11-17 15:11:45 +0100 | [diff] [blame] | 106 | return sock; |
| 107 | } |
Willy Tarreau | dba5002 | 2016-12-21 18:51:45 +0100 | [diff] [blame] | 108 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 109 | REGISTER_BUILD_OPTS("Built with network namespace support."); |