summaryrefslogtreecommitdiff
path: root/ext/tap-mac/tuntap/src/tuntap_mgr.cc
blob: f41394e9d223dbfe2161434beb6022642d17e35d (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
 * ip tunnel/ethertap device for MacOSX.
 *
 * tuntap_manager definition.
 */
/*
 * Copyright (c) 2011 Mattias Nissler <mattias.nissler@gmx.de>
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted
 * provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright notice, this list of
 *      conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright notice, this list of
 *      conditions and the following disclaimer in the documentation and/or other materials provided
 *      with the distribution.
 *   3. The name of the author may not be used to endorse or promote products derived from this
 *      software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "tuntap.h"
#include "mem.h"

extern "C" {

#include <sys/conf.h>
#include <sys/param.h>
#include <sys/syslog.h>
#include <sys/systm.h>

#include <vm/vm_kern.h>

#include <miscfs/devfs/devfs.h>

}

#if 0
#define dprintf(...)			log(LOG_INFO, __VA_ARGS__)
#else
#define dprintf(...)
#endif

/* cdevsw for tuntap_manager */
static struct cdevsw mgr_cdevsw =
{
	tuntap_manager::cdev_open,
	tuntap_manager::cdev_close,
	tuntap_manager::cdev_read,
	tuntap_manager::cdev_write,
	tuntap_manager::cdev_ioctl,
	eno_stop,
	eno_reset,
	NULL,
	tuntap_manager::cdev_select,
	eno_mmap,
	eno_strat,
	eno_getc,
	eno_putc,
	0
};

/* tuntap_manager members */
tuntap_manager *tuntap_manager::mgr_map[MAX_CDEV];

bool tuntap_manager::statics_initialized = false;

/* static initializer */
void
tuntap_manager::initialize_statics()
{
	dprintf("initializing mgr_map\n");

	/* initialize the major-to-manager map */
	for (int i = 0; i < MAX_CDEV; i++)
		mgr_map[i] = NULL;

	statics_initialized = true;
}

bool
tuntap_manager::initialize(unsigned int count, char *family)
{
	this->count = count;
	this->family = family;
	this->tuntaps = NULL;

	if (!statics_initialized)
		initialize_statics();

	/* make sure noone can access the character devices until we are done */
	auto_lock l(&cdev_gate);

	/* register the switch for the tap character devices */
	dev_major = cdevsw_add(-1, &mgr_cdevsw);
	if (dev_major == -1) {
		log(LOG_ERR, "%s: could not register character device switch.\n", family);
		return false;
	}

	/* allocate memory for the interface instance table */
	tuntaps = (tuntap_interface **) mem_alloc(count * sizeof(tuntap_interface *));
	if (tuntaps == NULL)
	{
		log(LOG_ERR, "%s: no memory!\n", family);
		return false;
	}

	bzero(tuntaps, count * sizeof(tuntap_interface *));

	/* Create the interfaces. This will only add the character devices. The network devices will
	 * be created upon open()ing the corresponding character devices.
	 */
	for (int i = 0; i < (int) count; i++)
	{
		tuntaps[i] = create_interface();

		if (tuntaps[i] != NULL)
		{
			if (tuntaps[i]->initialize(dev_major, i))
			{
				continue;
			}

			/* error here. current interface needs to be shut down */
			i++;
		}

		/* something went wrong. clean up. */
		while (--i >= 0)
		{
			tuntaps[i]->shutdown();
			delete  tuntaps[i];
		}

		return false;
	}

	/* register the new family in the mgr switch */
	mgr_map[dev_major] = this;

	log(LOG_INFO, "%s kernel extension version %s <mattias.nissler@gmx.de>\n",
			family, TUNTAP_VERSION);

	return true;
}

bool
tuntap_manager::shutdown()
{
	bool ok = true;

	/* we halt the whole thing while we check whether we can shutdown */
	auto_lock l(&cdev_gate);

	/* anyone in? */
	if (cdev_gate.is_anyone_in()) {
		dprintf("tuntap_mgr: won't shutdown, threads still behind the gate.");
		ok = false;
	} else {
		/* query the interfaces to see if shutting down is ok */
		if (tuntaps != NULL) {
			for (unsigned int i = 0; i < count; i++) {
				if (tuntaps[i] != NULL)
					ok &= tuntaps[i]->idle();
			}

			/* if yes, do it now */
			if (ok) {
				for (unsigned int i = 0; i < count; i++) {
					if (tuntaps[i] != NULL) {
						tuntaps[i]->shutdown();
						delete tuntaps[i];
						tuntaps[i] = NULL;
					}
				}
			}
		}
	}

	/* unregister the character device switch */
	if (ok) {
		if (dev_major != -1 && cdevsw_remove(dev_major, &mgr_cdevsw) == -1) {
			log(LOG_WARNING,
				"%s: character device switch got lost. strange.\n", family);
		}
		mgr_map[dev_major] = NULL;
		dev_major = -1;

		/* at this point there is still a chance that some thread hangs at the cdev_gate in
		 * one of the cdev service functions. I can't imagine any way that would aviod this.
		 * So lets unblock the gate such that they fail.
		 */
		unsigned int old_number;
		do {
			old_number = cdev_gate.get_ticket_number();

			dprintf("tuntap_manager: waiting for other threads to give up.\n");

			/* wait one second */
			cdev_gate.sleep(&cdev_gate, 1000000);

		} while (cdev_gate.get_ticket_number() != old_number);

		/* I hope it is safe to unload now. */

	} else {
		log(LOG_WARNING, "%s: won't unload, at least one interface is busy.\n", family);
	}

	dprintf("tuntap manager: shutdown %s\n", ok ? "ok" : "failed");

	return ok;
}

tuntap_manager::~tuntap_manager()
{
	dprintf("freeing interface table\n");

	/* free memory */
	if (tuntaps != NULL)
		mem_free(tuntaps, count * sizeof(tuntap_interface *));
}

/* service method dispatchers */
int
tuntap_manager::cdev_open(dev_t dev, int flags, int devtype, proc_t p)
{
	return (mgr_map[major(dev)] == NULL ? ENOENT
			: mgr_map[major(dev)]->do_cdev_open(dev, flags, devtype, p));
}

int
tuntap_manager::cdev_close(dev_t dev, int flags, int devtype, proc_t p)
{
	return (mgr_map[major(dev)] == NULL ? EBADF
			: mgr_map[major(dev)]->do_cdev_close(dev, flags, devtype, p));
}

int
tuntap_manager::cdev_read(dev_t dev, uio_t uio, int ioflag)
{
	return (mgr_map[major(dev)] == NULL ? EBADF
			: mgr_map[major(dev)]->do_cdev_read(dev, uio, ioflag));
}

int
tuntap_manager::cdev_write(dev_t dev, uio_t uio, int ioflag)
{
	return (mgr_map[major(dev)] == NULL ? EBADF
			: mgr_map[major(dev)]->do_cdev_write(dev, uio, ioflag));
}

int
tuntap_manager::cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, proc_t p)
{
	return (mgr_map[major(dev)] == NULL ? EBADF
			: mgr_map[major(dev)]->do_cdev_ioctl(dev, cmd, data, fflag, p));
}

int
tuntap_manager::cdev_select(dev_t dev, int which, void *wql, proc_t p)
{
	return (mgr_map[major(dev)] == NULL ? EBADF
			: mgr_map[major(dev)]->do_cdev_select(dev, which, wql, p));
}

/* character device service methods */
int
tuntap_manager::do_cdev_open(dev_t dev, int flags, int devtype, proc_t p)
{
	int dmin = minor(dev);
	int error = ENOENT;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_open(flags, devtype, p);

	cdev_gate.exit();

	return error;
}

int
tuntap_manager::do_cdev_close(dev_t dev, int flags, int devtype, proc_t p)
{
	int dmin = minor(dev);
	int error = EBADF;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_close(flags, devtype, p);

	cdev_gate.exit();

	return error;
}

int
tuntap_manager::do_cdev_read(dev_t dev, uio_t uio, int ioflag)
{
	int dmin = minor(dev);
	int error = EBADF;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_read(uio, ioflag);

	cdev_gate.exit();
	
	return error;
}

int
tuntap_manager::do_cdev_write(dev_t dev, uio_t uio, int ioflag)
{
	int dmin = minor(dev);
	int error = EBADF;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_write(uio, ioflag);

	cdev_gate.exit();
	
	return error;
}

int
tuntap_manager::do_cdev_ioctl(dev_t dev, u_long cmd, caddr_t data, int fflag, proc_t p)
{
	int dmin = minor(dev);
	int error = EBADF;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_ioctl(cmd, data, fflag, p);

	cdev_gate.exit();
	
	return error;
}

int
tuntap_manager::do_cdev_select(dev_t dev, int which, void *wql, proc_t p)
{
	int dmin = minor(dev);
	int error = EBADF;

	cdev_gate.enter();

	if (dmin < (int) count && dmin >= 0 && tuntaps[dmin] != NULL)
		error = tuntaps[dmin]->cdev_select(which, wql, p);

	cdev_gate.exit();
	
	return error;
}