| 1 |
/* Implementation for netns_socket API |
| 2 |
* |
| 3 |
* This file is part of rsyslog. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* -or- |
| 11 |
* see COPYING.ASL20 in the source distribution |
| 12 |
* |
| 13 |
* Unless required by applicable law or agreed to in writing, software |
| 14 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
* See the License for the specific language governing permissions and |
| 17 |
* limitations under the License. |
| 18 |
*/ |
| 19 |
#include "config.h" |
| 20 |
#include <stdio.h> |
| 21 |
#include <errno.h> |
| 22 |
#include <fcntl.h> |
| 23 |
#include <sched.h> |
| 24 |
#include <string.h> |
| 25 |
#include <sys/socket.h> |
| 26 |
#include <sys/types.h> |
| 27 |
|
| 28 |
#include "rsyslog.h" |
| 29 |
#include "debug.h" |
| 30 |
#include "errmsg.h" |
| 31 |
#include "netns_socket.h" |
| 32 |
|
| 33 |
|
| 34 |
/* Change to the given network namespace. |
| 35 |
* This function based on previous implementation |
| 36 |
* of tools/omfwd.c function changeToNs. |
| 37 |
*/ |
| 38 |
rsRetVal netns_switch(const char *ns) { |
| 39 |
DEFiRet; |
| 40 |
#ifdef HAVE_SETNS |
| 41 |
int ns_fd = -1; |
| 42 |
char *nsPath = NULL; |
| 43 |
|
| 44 |
if (ns && *ns) { |
| 45 |
/* Build network namespace path */ |
| 46 |
if (asprintf(&nsPath, "/var/run/netns/%s", ns) == -1) { |
| 47 |
// Some implementations say nsPath would be undefined on failure |
| 48 |
nsPath = NULL; |
| 49 |
LogError(0, RS_RET_OUT_OF_MEMORY, "%s: asprintf failed", __func__); |
| 50 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 51 |
} |
| 52 |
|
| 53 |
/* Open file descriptor of destination network namespace */ |
| 54 |
ns_fd = open(nsPath, O_RDONLY); |
| 55 |
if (ns_fd < 0) { |
| 56 |
LogError(errno, RS_RET_IO_ERROR, "%s: could not open namespace '%s'", __func__, ns); |
| 57 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 58 |
} |
| 59 |
/* Change to the destination network namespace */ |
| 60 |
if (setns(ns_fd, CLONE_NEWNET) != 0) { |
| 61 |
LogError(errno, RS_RET_IO_ERROR, "%s: could not change to namespace '%s'", __func__, ns); |
| 62 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 63 |
} |
| 64 |
dbgprintf("%s: changed to network namespace '%s'\n", __func__, ns); |
| 65 |
} |
| 66 |
finalize_it: |
| 67 |
free(nsPath); |
| 68 |
if ((ns_fd >= 0) && (close(ns_fd) != 0)) { |
| 69 |
LogError(errno, RS_RET_IO_ERROR, "%s: failed to close namespace '%s'", __func__, ns); |
| 70 |
} |
| 71 |
#else // ndef HAVE_SETNS |
| 72 |
if (ns && *ns) { |
| 73 |
LogError(ENOSYS, RS_RET_VALUE_NOT_SUPPORTED, "%s: could not change to namespace '%s'", __func__, ns); |
| 74 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 75 |
} |
| 76 |
finalize_it: |
| 77 |
#endif // ndef HAVE_SETNS |
| 78 |
RETiRet; |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
/* Return to the startup network namespace. |
| 83 |
* This function based on code in tools/omfwd.c |
| 84 |
*/ |
| 85 |
rsRetVal ATTR_NONNULL() netns_restore(int *fd) { |
| 86 |
DEFiRet; |
| 87 |
|
| 88 |
#ifdef HAVE_SETNS |
| 89 |
if (*fd >= 0) { |
| 90 |
if (setns(*fd, CLONE_NEWNET) != 0) { |
| 91 |
LogError(errno, RS_RET_IO_ERROR, "%s: could not return to startup namespace", __func__); |
| 92 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 93 |
} |
| 94 |
dbgprintf("%s: returned to startup network namespace\n", __func__); |
| 95 |
} |
| 96 |
|
| 97 |
finalize_it: |
| 98 |
#endif // def HAVE_SETNS |
| 99 |
if (*fd >= 0 && close(*fd) != 0) { |
| 100 |
LogError(errno, RS_RET_IO_ERROR, "%s: could not close startup namespace fd", __func__); |
| 101 |
} |
| 102 |
*fd = -1; |
| 103 |
RETiRet; |
| 104 |
} |
| 105 |
|
| 106 |
/* Save the current network namespace fd |
| 107 |
*/ |
| 108 |
rsRetVal ATTR_NONNULL() netns_save(int *fd) { |
| 109 |
DEFiRet; |
| 110 |
|
| 111 |
/* |
| 112 |
* The fd must always point to either a valid fd |
| 113 |
* or to -1. We expect it to be -1 on entry here. |
| 114 |
* To avoid bugs, or possible descriptor leaks, |
| 115 |
* check that it is always -1 on entry. |
| 116 |
*/ |
| 117 |
#ifdef HAVE_SETNS |
| 118 |
if (*fd != -1) { |
| 119 |
LogError(0, RS_RET_CODE_ERR, "%s: called with uninitialized descriptor", __func__); |
| 120 |
ABORT_FINALIZE(RS_RET_CODE_ERR); |
| 121 |
} |
| 122 |
*fd = open("/proc/self/ns/net", O_RDONLY); |
| 123 |
if (*fd == -1) { |
| 124 |
LogError(errno, RS_RET_IO_ERROR, "%s: could not access startup namespace", __func__); |
| 125 |
ABORT_FINALIZE(RS_RET_IO_ERROR); |
| 126 |
} |
| 127 |
dbgprintf("%s: saved startup network namespace\n", __func__); |
| 128 |
finalize_it: |
| 129 |
#endif // def HAVE_SETNS |
| 130 |
RETiRet; |
| 131 |
} |
| 132 |
|
| 133 |
rsRetVal netns_socket(int *fdp, int domain, int type, int protocol, const char *ns) { |
| 134 |
DEFiRet; |
| 135 |
int fd = -1; |
| 136 |
|
| 137 |
#ifndef HAVE_SETNS |
| 138 |
if (ns && *ns) { |
| 139 |
LogError(0, RS_RET_VALUE_NOT_SUPPORTED, "Network namespaces are not supported"); |
| 140 |
ABORT_FINALIZE(RS_RET_VALUE_NOT_SUPPORTED); |
| 141 |
} |
| 142 |
#else /* def HAVE_SETNS */ |
| 143 |
rsRetVal iRet_restore; |
| 144 |
int ns_fd = -1; |
| 145 |
|
| 146 |
if (ns && *ns) { |
| 147 |
CHKiRet(netns_save(&ns_fd)); |
| 148 |
CHKiRet(netns_switch(ns)); |
| 149 |
} |
| 150 |
#endif /* def HAVE_SETNS */ |
| 151 |
*fdp = fd = socket(domain, type, protocol); |
| 152 |
if (fd == -1) { |
| 153 |
LogError(errno, RS_RET_NO_SOCKET, "%s: socket(%d, %d, %d) failed", __func__, domain, type, protocol); |
| 154 |
ABORT_FINALIZE(RS_RET_NO_SOCKET); |
| 155 |
} |
| 156 |
finalize_it: |
| 157 |
#ifdef HAVE_SETNS |
| 158 |
iRet_restore = netns_restore(&ns_fd); |
| 159 |
if (iRet == RS_RET_OK) iRet = iRet_restore; |
| 160 |
#endif /* def HAVE_SETNS */ |
| 161 |
if (iRet != RS_RET_OK && fd != -1) { |
| 162 |
(void)close(fd); |
| 163 |
*fdp = -1; |
| 164 |
} |
| 165 |
RETiRet; |
| 166 |
} |