aboutsummaryrefslogtreecommitdiffstats
path: root/lib/netns.c
blob: 6e0e1791cd662941c4f63bcaad60253783b26b01 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
 * Copyright (C) 2014-2017, Travelping GmbH <info@travelping.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#if defined(__linux__)

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sched.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/mount.h>
#include <sys/param.h>
#include <fcntl.h>
#include <errno.h>

#include <osmocom/core/utils.h>

#include "netns.h"

#define NETNS_PATH "/var/run/netns"

/*! default namespace of the GGSN process */
static int default_nsfd = -1;

/*! switch to a (non-default) namespace, store existing signal mask in oldmask.
 *  \param[in] nsfd file descriptor representing the namespace to whch we shall switch
 *  \param[out] oldmask caller-provided memory location to which old signal mask is stored
 *  \ returns 0 on success or negative (errno) in case of error */
int switch_ns(int nsfd, sigset_t *oldmask)
{
	sigset_t intmask;
	int rc;

	OSMO_ASSERT(default_nsfd >= 0);

	if (sigfillset(&intmask) < 0)
		return -errno;
	if ((rc = sigprocmask(SIG_BLOCK, &intmask, oldmask)) != 0)
		return -rc;

	if (setns(nsfd, CLONE_NEWNET) < 0)
		return -errno;
	return 0;
}

/*! switch back to the default namespace, restoring signal mask.
 *  \param[in] oldmask signal mask to restore after returning to default namespace
 *  \returns 0 on successs; negative errno value in case of error */
int restore_ns(sigset_t *oldmask)
{
	OSMO_ASSERT(default_nsfd >= 0);

	int rc;
	if (setns(default_nsfd, CLONE_NEWNET) < 0)
		return -errno;

	if ((rc = sigprocmask(SIG_SETMASK, oldmask, NULL)) != 0)
		return -rc;
	return 0;
}

/*! open a file from within specified network namespace */
int open_ns(int nsfd, const char *pathname, int flags)
{
	sigset_t intmask, oldmask;
	int fd;
	int rc;

	OSMO_ASSERT(default_nsfd >= 0);

	/* mask off all signals, store old signal mask */
	if (sigfillset(&intmask) < 0)
		return -errno;
	if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
		return -rc;

	/* associate the calling thread with namespace file descriptor */
	if (setns(nsfd, CLONE_NEWNET) < 0)
		return -errno;
	/* open the requested file/path */
	if ((fd = open(pathname, flags)) < 0)
		return -errno;
	/* return back to default namespace */
	if (setns(default_nsfd, CLONE_NEWNET) < 0) {
		close(fd);
		return -errno;
	}
	/* restore process mask */
	if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) {
		close(fd);
		return -rc;
	}

	return fd;
}

/*! create a socket in another namespace.
 *  Switches temporarily to namespace indicated by nsfd, creates a socket in
 *  that namespace and then returns to the default namespace.
 *  \param[in] nsfd File descriptor of the namspace in which to create socket
 *  \param[in] domain Domain of the socket (AF_INET, ...)
 *  \param[in] type Type of the socket (SOCK_STREAM, ...)
 *  \param[in] protocol Protocol of the socket (IPPROTO_TCP, ...)
 *  \returns 0 on success; negative errno in case of error */
int socket_ns(int nsfd, int domain, int type, int protocol)
{
	sigset_t intmask, oldmask;
	int sk;
	int rc;

	OSMO_ASSERT(default_nsfd >= 0);

	/* mask off all signals, store old signal mask */
	if (sigfillset(&intmask) < 0)
		return -errno;
	if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
		return -rc;

	/* associate the calling thread with namespace file descriptor */
	if (setns(nsfd, CLONE_NEWNET) < 0)
		return -errno;

	/* create socket of requested domain/type/proto */
	if ((sk = socket(domain, type, protocol)) < 0)
		return -errno;

	/* return back to default namespace */
	if (setns(default_nsfd, CLONE_NEWNET) < 0) {
		close(sk);
		return -errno;
	}

	/* restore process mask */
	if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0) {
		close(sk);
		return -rc;
	}
	return sk;
}

/*! initialize this network namespace helper module.
 *  Must be called before using any other functions of this file.
 *  \returns 0 on success; negative errno in case of error */
int init_netns()
{
	/* store the default namespace for later reference */
	if ((default_nsfd = open("/proc/self/ns/net", O_RDONLY)) < 0)
		return -errno;
	return 0;
}

/*! create obtain file descriptor for network namespace of give name.
 *  Creates /var/run/netns  if it doesn't exist already.
 *  \param[in] name Name of the network namespace (in /var/run/netns/)
 *  \returns File descriptor of network namespace; negative errno in case of error */
int get_nsfd(const char *name)
{
	int rc;
	int fd;
	sigset_t intmask, oldmask;
	char path[MAXPATHLEN] = NETNS_PATH;

	OSMO_ASSERT(default_nsfd >= 0);

	/* create /var/run/netns, if it doesn't exist already */
	rc = mkdir(path, S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
	if (rc < 0 && errno != EEXIST)
		return rc;

	/* create /var/run/netns/[name], if it doesn't exist already */
	snprintf(path, sizeof(path), "%s/%s", NETNS_PATH, name);
	fd = open(path, O_RDONLY|O_CREAT|O_EXCL, 0);
	if (fd < 0) {
		if (errno == EEXIST) {
			if ((fd = open(path, O_RDONLY)) < 0)
				return -errno;
			return fd;
		}
		return -errno;
	}
	if (close(fd) < 0)
		return -errno;

	/* mask off all signals, store old signal mask */
	if (sigfillset(&intmask) < 0)
		return -errno;
	if ((rc = sigprocmask(SIG_BLOCK, &intmask, &oldmask)) != 0)
		return -rc;

	/* create a new network namespace */
	if (unshare(CLONE_NEWNET) < 0)
		return -errno;
	if (mount("/proc/self/ns/net", path, "none", MS_BIND, NULL) < 0)
		return -errno;

	/* switch back to default namespace */
	if (setns(default_nsfd, CLONE_NEWNET) < 0)
		return -errno;

	/* restore process mask */
	if ((rc = sigprocmask(SIG_SETMASK, &oldmask, NULL)) != 0)
		return -rc;

	/* finally, open the created namespace file descriptor from default ns */
	if ((fd = open(path, O_RDONLY)) < 0)
		return -errno;

	return fd;
}

#endif