2020-07-23 16:44:45 +00:00
|
|
|
/*
|
|
|
|
* cmd/ifupdown.c
|
|
|
|
* Purpose: bring interfaces up or down
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Ariadne Conill <ariadne@dereferenced.org>
|
2020-09-25 00:15:16 +00:00
|
|
|
* Copyright (c) 2020 Maximilian Wilhelm <max@sdn.clinic>
|
2020-07-23 16:44:45 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <getopt.h>
|
2020-07-29 09:01:49 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
2020-07-23 16:44:45 +00:00
|
|
|
#include "libifupdown/libifupdown.h"
|
2020-07-24 09:07:19 +00:00
|
|
|
#include "cmd/multicall.h"
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
static bool up;
|
|
|
|
|
|
|
|
bool
|
|
|
|
is_ifdown()
|
|
|
|
{
|
|
|
|
if (strstr(argv0, "ifdown") != NULL)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
int
|
|
|
|
acquire_state_lock(const char *state_path, const char *lifname)
|
|
|
|
{
|
|
|
|
if (exec_opts.mock || exec_opts.no_lock)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
char lockpath[4096] = {};
|
|
|
|
|
|
|
|
snprintf(lockpath, sizeof lockpath, "%s.%s.lock", state_path, lifname);
|
|
|
|
|
|
|
|
int fd = open(lockpath, O_CREAT | O_WRONLY | O_TRUNC);
|
|
|
|
if (fd < 0)
|
|
|
|
{
|
2020-07-29 09:18:12 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
fprintf(stderr, "%s: while opening lockfile %s: %s\n", argv0, lockpath, strerror(errno));
|
2020-07-29 09:17:12 +00:00
|
|
|
return -2;
|
2020-07-29 09:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int flags = fcntl(fd, F_GETFD);
|
|
|
|
if (flags < 0)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
2020-07-29 09:18:12 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
fprintf(stderr, "%s: while getting flags for lockfile: %s\n", argv0, strerror(errno));
|
2020-07-29 09:17:12 +00:00
|
|
|
return -2;
|
2020-07-29 09:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags |= FD_CLOEXEC;
|
|
|
|
if (fcntl(fd, F_SETFD, flags) == -1)
|
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
2020-07-29 09:18:12 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
fprintf(stderr, "%s: while setting lockfile close-on-exec: %s\n", argv0, strerror(errno));
|
2020-07-29 09:17:12 +00:00
|
|
|
return -2;
|
2020-07-29 09:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct flock fl = {
|
|
|
|
.l_type = F_WRLCK,
|
|
|
|
.l_whence = SEEK_SET
|
|
|
|
};
|
|
|
|
|
|
|
|
if (exec_opts.verbose)
|
2020-07-29 09:13:34 +00:00
|
|
|
fprintf(stderr, "%s: acquiring lock on %s\n", argv0, lockpath);
|
2020-07-29 09:10:55 +00:00
|
|
|
|
2020-07-29 09:13:34 +00:00
|
|
|
if (fcntl(fd, F_SETLK, &fl) == -1)
|
2020-07-29 09:10:55 +00:00
|
|
|
{
|
|
|
|
close(fd);
|
|
|
|
|
2020-07-29 09:17:12 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
fprintf(stderr, "%s: while locking lockfile: %s\n", argv0, strerror(errno));
|
2020-07-29 09:13:34 +00:00
|
|
|
return -2;
|
2020-07-29 09:10:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2020-09-09 01:10:20 +00:00
|
|
|
bool
|
2020-10-21 14:46:47 +00:00
|
|
|
skip_interface(struct lif_interface *iface, const char *ifname, struct lif_dict *state, bool update_state)
|
2020-09-09 01:10:20 +00:00
|
|
|
{
|
2020-09-23 17:42:09 +00:00
|
|
|
if (iface->is_template)
|
2020-09-23 17:52:22 +00:00
|
|
|
{
|
2020-09-23 17:42:09 +00:00
|
|
|
fprintf(stderr, "%s: cannot change state on %s (template interface)\n", argv0, ifname);
|
2020-09-23 17:52:22 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-09-23 17:42:09 +00:00
|
|
|
|
2020-09-25 00:15:16 +00:00
|
|
|
if (iface->has_config_error)
|
|
|
|
{
|
2020-09-25 00:26:21 +00:00
|
|
|
if (exec_opts.force)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: (de)configuring interface %s despite config errors\n", argv0, ifname);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: skipping interface %s due to config errors\n", argv0, ifname);
|
|
|
|
return true;
|
|
|
|
}
|
2020-09-25 00:15:16 +00:00
|
|
|
}
|
|
|
|
|
2020-09-25 00:26:21 +00:00
|
|
|
if (exec_opts.force)
|
|
|
|
return false;
|
|
|
|
|
2020-09-09 01:10:20 +00:00
|
|
|
if (up && iface->refcount > 0)
|
|
|
|
{
|
2020-09-10 01:38:29 +00:00
|
|
|
if (exec_opts.verbose)
|
2020-10-21 14:46:47 +00:00
|
|
|
fprintf(stderr, "%s: skipping %sinterface %s (already configured), use --force to force configuration\n",
|
|
|
|
argv0, iface->is_auto ? "auto " : "", ifname);
|
|
|
|
|
|
|
|
if (update_state)
|
|
|
|
{
|
|
|
|
iface->is_explicit = true;
|
|
|
|
lif_state_upsert(state, ifname, iface);
|
|
|
|
}
|
|
|
|
|
2020-09-09 01:10:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!up && iface->refcount == 0)
|
|
|
|
{
|
2020-09-10 01:38:29 +00:00
|
|
|
if (exec_opts.verbose)
|
2020-10-21 14:46:47 +00:00
|
|
|
fprintf(stderr, "%s: skipping %sinterface %s (already deconfigured), use --force to force deconfiguration\n",
|
|
|
|
argv0, iface->is_auto ? "auto " : "", ifname);
|
2020-09-09 01:10:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
bool
|
2020-10-21 14:42:47 +00:00
|
|
|
change_interface(struct lif_interface *iface, struct lif_dict *collection, struct lif_dict *state, const char *ifname, bool update_state)
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-29 09:10:55 +00:00
|
|
|
int lockfd = acquire_state_lock(exec_opts.state_file, ifname);
|
|
|
|
|
2020-07-29 09:13:34 +00:00
|
|
|
if (lockfd == -2)
|
|
|
|
{
|
2020-07-29 09:17:12 +00:00
|
|
|
fprintf(stderr, "%s: could not acquire exclusive lock for %s: %s\n", argv0, ifname, strerror(errno));
|
2020-07-29 09:13:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:46:47 +00:00
|
|
|
if (skip_interface(iface, ifname, state, update_state))
|
2020-09-09 01:10:20 +00:00
|
|
|
{
|
|
|
|
if (lockfd != -1)
|
|
|
|
close(lockfd);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
if (exec_opts.verbose)
|
|
|
|
{
|
2020-07-24 09:57:52 +00:00
|
|
|
fprintf(stderr, "%s: changing state of interface %s to '%s'\n",
|
2020-07-23 16:57:46 +00:00
|
|
|
argv0, ifname, up ? "up" : "down");
|
|
|
|
}
|
|
|
|
|
2020-07-24 09:42:34 +00:00
|
|
|
if (!lif_lifecycle_run(&exec_opts, iface, collection, state, ifname, up))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-23 16:57:46 +00:00
|
|
|
fprintf(stderr, "%s: failed to change interface %s state to '%s'\n",
|
2020-07-23 16:44:45 +00:00
|
|
|
argv0, ifname, up ? "up" : "down");
|
2020-07-29 09:10:55 +00:00
|
|
|
|
|
|
|
if (lockfd != -1)
|
|
|
|
close(lockfd);
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
if (lockfd != -1)
|
|
|
|
close(lockfd);
|
|
|
|
|
2020-10-21 14:42:47 +00:00
|
|
|
if (up && update_state)
|
|
|
|
{
|
|
|
|
iface->is_explicit = true;
|
|
|
|
lif_state_upsert(state, ifname, iface);
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
bool
|
|
|
|
change_auto_interfaces(struct lif_dict *collection, struct lif_dict *state, struct match_options *opts)
|
|
|
|
{
|
|
|
|
struct lif_node *iter;
|
|
|
|
|
|
|
|
LIF_DICT_FOREACH(iter, collection)
|
|
|
|
{
|
|
|
|
struct lif_dict_entry *entry = iter->data;
|
|
|
|
struct lif_interface *iface = entry->data;
|
|
|
|
|
|
|
|
if (opts->is_auto && !iface->is_auto)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts->exclude_pattern != NULL &&
|
|
|
|
!fnmatch(opts->exclude_pattern, iface->ifname, 0))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (opts->include_pattern != NULL &&
|
|
|
|
fnmatch(opts->include_pattern, iface->ifname, 0))
|
|
|
|
continue;
|
|
|
|
|
2020-10-21 14:42:47 +00:00
|
|
|
if (!change_interface(iface, collection, state, iface->ifname, false))
|
2020-07-23 16:57:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-09-09 20:18:16 +00:00
|
|
|
int
|
|
|
|
update_state_file_and_exit(int rc, struct lif_dict *state)
|
|
|
|
{
|
|
|
|
if (exec_opts.mock)
|
|
|
|
{
|
|
|
|
exit(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lif_state_write_path(state, exec_opts.state_file))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not update %s\n", argv0, exec_opts.state_file);
|
|
|
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
exit(rc);
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:44:45 +00:00
|
|
|
int
|
2020-07-24 09:07:19 +00:00
|
|
|
ifupdown_main(int argc, char *argv[])
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
|
|
|
up = !is_ifdown();
|
|
|
|
|
|
|
|
struct lif_dict state = {};
|
|
|
|
struct lif_dict collection = {};
|
2020-10-14 09:50:58 +00:00
|
|
|
struct lif_interface_file_parse_state parse_state = {
|
|
|
|
.collection = &collection,
|
|
|
|
};
|
2020-07-23 16:44:45 +00:00
|
|
|
|
2020-08-20 09:45:56 +00:00
|
|
|
lif_interface_collection_init(&collection);
|
|
|
|
|
2020-07-29 09:10:55 +00:00
|
|
|
if (!lif_state_read_path(&state, exec_opts.state_file))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-29 09:10:55 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.state_file);
|
2020-07-23 16:44:45 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:50:58 +00:00
|
|
|
if (!lif_interface_file_parse(&parse_state, exec_opts.interfaces_file))
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-26 08:50:01 +00:00
|
|
|
fprintf(stderr, "%s: could not parse %s\n", argv0, exec_opts.interfaces_file);
|
2020-07-24 10:48:50 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-09-10 00:11:10 +00:00
|
|
|
if (lif_lifecycle_count_rdepends(&exec_opts, &collection) == -1)
|
2020-09-09 23:39:26 +00:00
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not validate dependency tree\n", argv0);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-10-18 01:19:28 +00:00
|
|
|
if(!lif_compat_apply(&collection))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: failed to apply compatibility glue\n", argv0);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:48:50 +00:00
|
|
|
if (!lif_state_sync(&state, &collection))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: could not sync state\n", argv0);
|
2020-07-23 16:44:45 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2020-07-23 16:57:46 +00:00
|
|
|
if (match_opts.is_auto)
|
|
|
|
{
|
|
|
|
if (!change_auto_interfaces(&collection, &state, &match_opts))
|
2020-09-09 20:18:16 +00:00
|
|
|
return update_state_file_and_exit(EXIT_FAILURE, &state);
|
2020-07-23 16:57:46 +00:00
|
|
|
|
2020-09-09 20:18:16 +00:00
|
|
|
return update_state_file_and_exit(EXIT_SUCCESS, &state);
|
2020-07-23 16:57:46 +00:00
|
|
|
}
|
|
|
|
else if (optind >= argc)
|
2020-08-12 05:14:55 +00:00
|
|
|
generic_usage(self_applet, EXIT_FAILURE);
|
2020-07-23 16:44:45 +00:00
|
|
|
|
|
|
|
int idx = optind;
|
|
|
|
for (; idx < argc; idx++)
|
|
|
|
{
|
|
|
|
char lifbuf[4096];
|
|
|
|
strlcpy(lifbuf, argv[idx], sizeof lifbuf);
|
|
|
|
|
|
|
|
char *ifname = lifbuf;
|
|
|
|
char *lifname = lifbuf;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
if ((p = strchr(lifbuf, '=')) != NULL)
|
|
|
|
{
|
|
|
|
*p++ = '\0';
|
|
|
|
lifname = p;
|
|
|
|
}
|
|
|
|
|
2020-07-24 06:15:26 +00:00
|
|
|
struct lif_interface *iface = lif_state_lookup(&state, &collection, argv[idx]);
|
|
|
|
if (iface == NULL)
|
2020-07-23 16:44:45 +00:00
|
|
|
{
|
2020-07-24 06:15:26 +00:00
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&collection, lifname);
|
|
|
|
|
|
|
|
if (entry == NULL)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "%s: unknown interface %s\n", argv0, argv[idx]);
|
2020-09-09 20:18:16 +00:00
|
|
|
return update_state_file_and_exit(EXIT_FAILURE, &state);
|
2020-07-24 06:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iface = entry->data;
|
2020-07-23 16:44:45 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 14:42:47 +00:00
|
|
|
if (!change_interface(iface, &collection, &state, ifname, true))
|
2020-09-09 20:18:16 +00:00
|
|
|
return update_state_file_and_exit(EXIT_FAILURE, &state);
|
2020-07-23 17:01:05 +00:00
|
|
|
}
|
2020-07-23 16:44:45 +00:00
|
|
|
|
2020-09-09 20:18:16 +00:00
|
|
|
return update_state_file_and_exit(EXIT_SUCCESS, &state);
|
2020-07-23 16:44:45 +00:00
|
|
|
}
|
2020-07-24 09:07:19 +00:00
|
|
|
|
|
|
|
struct if_applet ifup_applet = {
|
|
|
|
.name = "ifup",
|
2020-08-12 05:14:55 +00:00
|
|
|
.desc = "bring interfaces up",
|
2020-07-24 09:07:19 +00:00
|
|
|
.main = ifupdown_main,
|
2020-08-12 05:14:55 +00:00
|
|
|
.usage = "ifup [options] <interfaces>",
|
2020-08-18 20:14:33 +00:00
|
|
|
.manpage = "8 ifup",
|
2020-08-12 05:14:55 +00:00
|
|
|
.groups = { &global_option_group, &match_option_group, &exec_option_group, },
|
2020-07-24 09:07:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct if_applet ifdown_applet = {
|
|
|
|
.name = "ifdown",
|
2020-08-12 05:14:55 +00:00
|
|
|
.desc = "take interfaces down",
|
2020-07-24 09:07:19 +00:00
|
|
|
.main = ifupdown_main,
|
2020-08-12 05:14:55 +00:00
|
|
|
.usage = "ifdown [options] <interfaces>",
|
2020-08-18 20:14:33 +00:00
|
|
|
.manpage = "8 ifdown",
|
2020-08-12 05:14:55 +00:00
|
|
|
.groups = { &global_option_group, &match_option_group, &exec_option_group, },
|
2020-07-24 09:07:19 +00:00
|
|
|
};
|