Improve interface name handling

This commit is contained in:
Matthias Schiffer 2013-08-12 18:18:26 +02:00
parent d351a2403c
commit 78de22fd21
2 changed files with 24 additions and 18 deletions

View file

@ -609,6 +609,11 @@ void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char
if (conf->generate_key || conf->show_key) if (conf->generate_key || conf->show_key)
return; return;
if (conf->ifname) {
if (strchr(conf->ifname, '/'))
exit_error(ctx, "config error: invalid interface name");
}
if (conf->mode == MODE_TUN) { if (conf->mode == MODE_TUN) {
if (!conf->peers || conf->peers->next) if (!conf->peers || conf->peers->next)
exit_error(ctx, "config error: in TUN mode exactly one peer must be configured"); exit_error(ctx, "config error: in TUN mode exactly one peer must be configured");

View file

@ -157,22 +157,29 @@ void fastd_tuntap_open(fastd_context_t *ctx) {
pr_debug(ctx, "initializing tun/tap device..."); pr_debug(ctx, "initializing tun/tap device...");
char ifname[5+IFNAMSIZ] = "/dev/"; char ifname[5+IFNAMSIZ] = "/dev/";
const char *type;
switch (ctx->conf->mode) {
case MODE_TAP:
type = "tap";
break;
case MODE_TUN:
type = "tun";
break;
default:
exit_bug(ctx, "invalid mode");
}
if (ctx->conf->ifname) { if (ctx->conf->ifname) {
if (strncmp(ctx->conf->ifname, type, 3) != 0)
exit_error(ctx, "`%s' doesn't seem to be a %s device", ctx->conf->ifname, type);
strncat(ifname, ctx->conf->ifname, IFNAMSIZ-1); strncat(ifname, ctx->conf->ifname, IFNAMSIZ-1);
} }
else { else {
switch (ctx->conf->mode) { strncat(ifname, type, IFNAMSIZ-1);
case MODE_TAP:
strncat(ifname, "tap", IFNAMSIZ-1);
break;
case MODE_TUN:
strncat(ifname, "tun", IFNAMSIZ-1);
break;
default:
exit_bug(ctx, "invalid mode");
}
} }
if ((ctx->tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0) if ((ctx->tunfd = open(ifname, O_RDWR|O_CLOEXEC|O_NONBLOCK)) < 0)
@ -183,16 +190,10 @@ void fastd_tuntap_open(fastd_context_t *ctx) {
switch (ctx->conf->mode) { switch (ctx->conf->mode) {
case MODE_TAP: case MODE_TAP:
if (strncmp(ctx->ifname, "tap", 3) != 0)
exit_error(ctx, "opened device doesn't seem to be a tap device");
setup_tap(ctx); setup_tap(ctx);
break; break;
case MODE_TUN: case MODE_TUN:
if (strncmp(ctx->ifname, "tun", 3) != 0)
exit_error(ctx, "opened device doesn't seem to be a tun device");
setup_tun(ctx); setup_tun(ctx);
break; break;