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