2020-07-19 00:02:25 +00:00
|
|
|
/*
|
|
|
|
* libifupdown/interface-file.h
|
|
|
|
* Purpose: /etc/network/interfaces parser
|
|
|
|
*
|
|
|
|
* 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-19 00:02:25 +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.
|
|
|
|
*/
|
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
#include <ctype.h>
|
2020-08-20 09:34:59 +00:00
|
|
|
#include <stdarg.h>
|
2020-07-19 00:02:25 +00:00
|
|
|
#include <stdio.h>
|
2020-07-19 00:48:49 +00:00
|
|
|
#include <string.h>
|
2020-08-19 07:57:11 +00:00
|
|
|
#include "libifupdown/libifupdown.h"
|
|
|
|
|
|
|
|
/* internally rewrite problematic ifupdown2 tokens to ifupdown-ng equivalents */
|
|
|
|
struct remap_token {
|
|
|
|
const char *token;
|
|
|
|
const char *alternative;
|
|
|
|
};
|
|
|
|
|
2020-08-19 08:46:04 +00:00
|
|
|
/* this list must be in alphabetical order for bsearch */
|
2020-08-19 07:57:11 +00:00
|
|
|
static const struct remap_token tokens[] = {
|
2020-10-03 23:35:41 +00:00
|
|
|
{"bond-ad-sys-priority", "bond-ad-actor-sys-prio"}, /* ifupdown2 */
|
|
|
|
{"bond-slaves", "bond-members"}, /* legacy ifupdown, ifupdown2 */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"driver-message-level", "ethtool-msglvl"}, /* Debian ethtool integration */
|
2020-08-19 08:46:04 +00:00
|
|
|
{"endpoint", "tunnel-remote"}, /* legacy ifupdown */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"ethernet-autoneg", "ethtool-ethernet-autoneg"}, /* Debian ethtool integration */
|
|
|
|
{"ethernet-pause-autoneg", "ethtool-pause-autoneg"}, /* Debian ethtool integration */
|
|
|
|
{"ethernet-pause-rx", "ethtool-pause-rx"}, /* Debian ethtool integration */
|
|
|
|
{"ethernet-pause-tx", "ethtool-pause-tx"}, /* Debian ethtool integration */
|
|
|
|
{"ethernet-port", "ethtool-ethernet-port"}, /* Debian ethtool integration */
|
|
|
|
{"ethernet-wol", "ethtool-ethernet-wol"}, /* Debian ethtool integration */
|
|
|
|
{"gro-offload", "ethtool-offload-gro"}, /* ifupdown2 */
|
|
|
|
{"gso-offload", "ethtool-offload-gso"}, /* ifupdown2 */
|
2020-09-21 15:30:12 +00:00
|
|
|
{"hardware-dma-ring-rx", "ethtool-dma-ring-rx"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-dma-ring-rx-jumbo", "ethtool-dma-ring-rx-jumbo"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-dma-ring-rx-mini", "ethtool-dma-ring-rx-mini"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-dma-ring-tx", "ethtool-dma-ring-tx"}, /* Debian ethtool integration */
|
2020-09-21 15:56:48 +00:00
|
|
|
{"hardware-irq-coalesce-adaptive-rx", "ethtool-coalesce-adaptive-rx"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-adaptive-tx", "ethtool-coalesce-adaptive-tx"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-pkt-rate-high", "ethtool-coalesce-pkt-rate-high"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-pkt-rate-low", "ethtool-coalesce-pkt-rate-low"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-frames", "ethtool-coalesce-rx-frames"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-frames-high", "ethtool-coalesce-rx-frames-high"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-frames-irq", "ethtool-coalesce-rx-frames-irq"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-frames-low", "ethtool-coalesce-rx-frames-low"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-usecs", "ethtool-coalesce-rx-usecs"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-usecs-high", "ethtool-coalesce-rx-usecs-high"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-usecs-irq", "ethtool-coalesce-rx-usecs-irq"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-rx-usecs-low", "ethtool-coalesce-rx-usecs-low"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-sample-interval", "ethtool-coalesce-sample-interval"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-stats-block-usecs", "ethtool-coalesce-stats-block-usecs"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-frames", "ethtool-coalesce-tx-frames"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-frames-high", "ethtool-coalesce-tx-frames-high"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-frames-irq", "ethtool-coalesce-tx-frames-irq"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-frames-low", "ethtool-coalesce-tx-frames-low"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-usecs", "ethtool-coalesce-tx-usecs"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-usecs-high", "ethtool-coalesce-tx-usecs-high"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-usecs-irq", "ethtool-coalesce-tx-usecs-irq"}, /* Debian ethtool integration */
|
|
|
|
{"hardware-irq-coalesce-tx-usecs-low", "ethtool-coalesce-tx-usecs-low"}, /* Debian ethtool integration */
|
2020-09-19 14:07:32 +00:00
|
|
|
{"link-autoneg", "ethtool-ethernet-autoneg"}, /* ifupdown2 */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"link-duplex", "ethtool-link-duplex"}, /* Debian ethtool integration */
|
|
|
|
{"link-fec", "ethtool-link-fec"}, /* ifupdown2 */
|
|
|
|
{"link-speed", "ethtool-link-speed"}, /* Debian ethtool integration */
|
2020-08-19 08:46:04 +00:00
|
|
|
{"local", "tunnel-local"}, /* legacy ifupdown */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"lro-offload", "ethtool-offload-lro"}, /* ifupdown2 */
|
2020-08-19 09:12:43 +00:00
|
|
|
{"mode", "tunnel-mode"}, /* legacy ifupdown */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"offload-gro", "ethtool-offload-gro"}, /* Debian ethtool integration */
|
|
|
|
{"offload-gso", "ethtool-offload-gso"}, /* Debian ethtool integration */
|
|
|
|
{"offload-lro", "ethtool-offload-lro"}, /* Debian ethtool integration */
|
|
|
|
{"offload-rx", "ethtool-offload-rx"}, /* Debian ethtool integration */
|
|
|
|
{"offload-sg", "ethtool-offload-sg"}, /* Debian ethtool integration */
|
|
|
|
{"offload-tso", "ethtool-offload-tso"}, /* Debian ethtool integration */
|
|
|
|
{"offload-tx", "ethtool-offload-tx"}, /* Debian ethtool integration */
|
|
|
|
{"offload-ufo", "ethtool-offload-ufo"}, /* Debian ethtool integration */
|
2020-10-06 01:38:02 +00:00
|
|
|
{"pointopoint", "point-to-point"}, /* legacy ifupdown, ifupdown2 */
|
2020-08-19 12:03:48 +00:00
|
|
|
{"provider", "ppp-provider"}, /* legacy ifupdown, ifupdown2 */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"rx-offload", "ethtool-offload-rx"}, /* ifupdown2 */
|
|
|
|
{"tso-offload", "ethtool-offload-tso"}, /* ifupdown2 */
|
2020-08-19 09:29:13 +00:00
|
|
|
{"ttl", "tunnel-ttl"}, /* legacy ifupdown */
|
2020-08-19 08:46:04 +00:00
|
|
|
{"tunnel-endpoint", "tunnel-remote"}, /* ifupdown2 */
|
|
|
|
{"tunnel-physdev", "tunnel-dev"}, /* ifupdown2 */
|
2020-09-19 13:58:51 +00:00
|
|
|
{"tx-offload", "ethtool-offload-tx"}, /* ifupdown2 */
|
|
|
|
{"ufo-offload", "ethtool-offload-ufo"}, /* ifupdown2 */
|
2020-08-19 08:46:04 +00:00
|
|
|
{"vrf", "vrf-member"}, /* ifupdown2 */
|
2020-10-02 00:20:10 +00:00
|
|
|
{"vxlan-local-tunnelip", "vxlan-local-ip"}, /* ifupdown2 */
|
|
|
|
{"vxlan-remoteip", "vxlan-remote-ip"}, /* ifupdown2 */
|
|
|
|
{"vxlan-svcnodeip", "vxlan-remote-group"}, /* ifupdown2 */
|
2020-08-19 07:57:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
token_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char *key = a;
|
|
|
|
const struct remap_token *token = b;
|
|
|
|
|
|
|
|
return strcmp(key, token->token);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
maybe_remap_token(const char *token)
|
|
|
|
{
|
|
|
|
const struct remap_token *tok = NULL;
|
|
|
|
static char tokbuf[4096];
|
|
|
|
|
|
|
|
tok = bsearch(token, tokens, ARRAY_SIZE(tokens), sizeof(*tokens), token_cmp);
|
|
|
|
strlcpy(tokbuf, tok != NULL ? tok->alternative : token, sizeof tokbuf);
|
|
|
|
|
|
|
|
return tokbuf;
|
|
|
|
}
|
2020-07-19 00:02:25 +00:00
|
|
|
|
2020-08-20 09:41:07 +00:00
|
|
|
static void
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(struct lif_interface_file_parse_state *state, const char *errfmt, ...)
|
2020-08-20 09:41:07 +00:00
|
|
|
{
|
|
|
|
char errbuf[4096];
|
|
|
|
|
|
|
|
va_list va;
|
|
|
|
va_start(va, errfmt);
|
|
|
|
vsnprintf(errbuf, sizeof errbuf, errfmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
2020-10-14 09:48:20 +00:00
|
|
|
fprintf(stderr, "%s:%zu: %s\n", state->cur_filename, state->cur_lineno, errbuf);
|
2020-08-20 09:41:07 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 10:04:34 +00:00
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_address(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
|
|
|
(void) token;
|
|
|
|
|
|
|
|
char *addr = lif_next_token(&bufp);
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "%s '%s' without interface", token, addr);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Ignore this address, but don't fail hard */
|
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_address_add(state->cur_iface, addr);
|
2020-08-20 10:04:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:41:07 +00:00
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_auto(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 09:41:07 +00:00
|
|
|
{
|
|
|
|
(void) token;
|
|
|
|
|
|
|
|
char *ifname = lif_next_token(&bufp);
|
2020-10-14 10:00:39 +00:00
|
|
|
if (!*ifname && state->cur_iface == NULL)
|
2020-09-25 00:15:16 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "auto without interface");
|
2020-09-25 00:15:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-08-20 09:41:07 +00:00
|
|
|
else
|
|
|
|
{
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface = lif_interface_collection_find(state->collection, ifname);
|
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 09:41:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (!state->cur_iface->is_template)
|
|
|
|
state->cur_iface->is_auto = true;
|
2020-09-23 17:38:29 +00:00
|
|
|
|
2020-08-20 09:41:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-20 10:04:34 +00:00
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_gateway(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
|
|
|
(void) token;
|
|
|
|
|
|
|
|
char *addr = lif_next_token(&bufp);
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "%s '%s' without interface", token, addr);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Ignore this gateway, but don't fail hard */
|
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_use_executor(state->cur_iface, "static");
|
|
|
|
lif_dict_add(&state->cur_iface->vars, token, strdup(addr));
|
2020-08-20 10:04:34 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_generic(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
(void) state;
|
2020-08-20 10:04:34 +00:00
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
token = maybe_remap_token(token);
|
|
|
|
|
2020-08-30 20:18:07 +00:00
|
|
|
/* Skip any leading whitespaces in value for <token> */
|
|
|
|
while (isspace (*bufp))
|
|
|
|
bufp++;
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_dict_add(&state->cur_iface->vars, token, strdup(bufp));
|
2020-08-20 10:04:34 +00:00
|
|
|
|
|
|
|
/* Check if token looks like <word1>-<word*> and assume <word1> is an addon */
|
|
|
|
char *word_end = strchr(token, '-');
|
|
|
|
if (word_end != NULL)
|
|
|
|
{
|
|
|
|
/* Copy word1 to not mangle *token */
|
|
|
|
char *addon = strndup(token, word_end - token);
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_use_executor(state->cur_iface, addon);
|
2020-08-20 10:04:34 +00:00
|
|
|
free(addon);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-14 09:48:20 +00:00
|
|
|
static bool handle_inherit(struct lif_interface_file_parse_state *state, char *token, char *bufp);
|
2020-08-20 10:04:34 +00:00
|
|
|
|
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_iface(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
|
|
|
char *ifname = lif_next_token(&bufp);
|
|
|
|
if (!*ifname)
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "%s without any other tokens", token);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* This is broken but not fatal */
|
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface = lif_interface_collection_find(state->collection, ifname);
|
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "could not upsert interface %s", ifname);
|
2020-08-20 10:04:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
/* mark the state->cur_iface as a template iface if `template` keyword
|
2020-09-23 17:29:01 +00:00
|
|
|
* is used.
|
|
|
|
*/
|
|
|
|
if (!strcmp(token, "template"))
|
2020-09-23 17:38:29 +00:00
|
|
|
{
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->is_auto = false;
|
|
|
|
state->cur_iface->is_template = true;
|
2020-09-23 17:38:29 +00:00
|
|
|
}
|
2020-09-23 17:29:01 +00:00
|
|
|
|
2020-08-20 10:04:34 +00:00
|
|
|
/* in original ifupdown config, we can have "inet loopback"
|
|
|
|
* or "inet dhcp" or such to designate hints. lets pick up
|
|
|
|
* those hints here.
|
|
|
|
*/
|
|
|
|
token = lif_next_token(&bufp);
|
|
|
|
while (*token)
|
|
|
|
{
|
|
|
|
if (!strcmp(token, "dhcp"))
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_use_executor(state->cur_iface, "dhcp");
|
2020-08-20 10:04:34 +00:00
|
|
|
else if (!strcmp(token, "ppp"))
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_use_executor(state->cur_iface, "ppp");
|
2020-08-20 10:04:34 +00:00
|
|
|
else if (!strcmp(token, "inherits"))
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
if (!handle_inherit(state, token, bufp))
|
2020-08-20 10:04:34 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
token = lif_next_token(&bufp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_inherit(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
|
|
|
char *target = lif_next_token(&bufp);
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "%s '%s' without interface", token, target);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* This is broken but not fatal */
|
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*target)
|
|
|
|
{
|
2020-10-14 10:00:39 +00:00
|
|
|
report_error(state, "iface %s: unspecified inherit target", state->cur_iface->ifname);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Mark this interface as errornous but carry on */
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->has_config_error = true;
|
2020-09-25 00:15:16 +00:00
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 09:48:20 +00:00
|
|
|
struct lif_interface *parent = lif_interface_collection_find(state->collection, target);
|
2020-09-25 00:15:16 +00:00
|
|
|
if (parent == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "iface %s: could not inherit from %s: not found",
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->ifname, target);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Mark this interface as errornous but carry on */
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->has_config_error = true;
|
2020-09-25 00:15:16 +00:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lif_config.allow_any_iface_as_template && !parent->is_template)
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "iface %s: could not inherit from %ss: inheritence from non-template interface not allowed",
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->ifname, target);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Mark this interface as errornous but carry on */
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->has_config_error = true;
|
2020-09-25 00:15:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (!lif_interface_collection_inherit(state->cur_iface, parent))
|
2020-09-25 00:15:16 +00:00
|
|
|
{
|
2020-10-14 10:00:39 +00:00
|
|
|
report_error(state, "iface %s: could not inherit from %s", state->cur_iface->ifname, target);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Mark this interface as errornous but carry on */
|
2020-10-14 10:00:39 +00:00
|
|
|
state->cur_iface->has_config_error = true;
|
2020-09-25 00:15:16 +00:00
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:46:31 +00:00
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_source(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 09:46:31 +00:00
|
|
|
{
|
|
|
|
(void) token;
|
|
|
|
|
|
|
|
char *source_filename = lif_next_token(&bufp);
|
|
|
|
if (!*source_filename)
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "missing filename to source");
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Broken but not fatal */
|
|
|
|
return true;
|
2020-08-20 09:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 09:48:20 +00:00
|
|
|
return lif_interface_file_parse(state, source_filename);
|
2020-08-20 09:46:31 +00:00
|
|
|
}
|
|
|
|
|
2020-08-20 10:04:34 +00:00
|
|
|
static bool
|
2020-10-14 09:48:20 +00:00
|
|
|
handle_use(struct lif_interface_file_parse_state *state, char *token, char *bufp)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
|
|
|
char *executor = lif_next_token(&bufp);
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
if (state->cur_iface == NULL)
|
2020-08-20 10:04:34 +00:00
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
report_error(state, "%s '%s' without interface", token, executor);
|
2020-09-25 00:15:16 +00:00
|
|
|
/* Broken but not fatal */
|
|
|
|
return true;
|
2020-08-20 10:04:34 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 10:00:39 +00:00
|
|
|
lif_interface_use_executor(state->cur_iface, executor);
|
2020-08-20 10:04:34 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-08-20 09:34:59 +00:00
|
|
|
/* map keywords to parser functions */
|
|
|
|
struct parser_keyword {
|
|
|
|
const char *token;
|
2020-10-14 09:48:20 +00:00
|
|
|
bool (*handle)(struct lif_interface_file_parse_state *state, char *token, char *bufp);
|
2020-08-20 09:34:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static const struct parser_keyword keywords[] = {
|
2020-08-20 10:04:34 +00:00
|
|
|
{"address", handle_address},
|
2020-08-20 09:41:07 +00:00
|
|
|
{"auto", handle_auto},
|
2020-08-20 10:04:34 +00:00
|
|
|
{"gateway", handle_gateway},
|
|
|
|
{"iface", handle_iface},
|
|
|
|
{"inherit", handle_inherit},
|
2020-09-23 17:26:28 +00:00
|
|
|
{"interface", handle_iface},
|
2020-08-20 09:46:31 +00:00
|
|
|
{"source", handle_source},
|
2020-09-23 17:26:28 +00:00
|
|
|
{"template", handle_iface},
|
2020-08-20 10:04:34 +00:00
|
|
|
{"use", handle_use},
|
2020-08-20 09:34:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
keyword_cmp(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
const char *key = a;
|
|
|
|
const struct parser_keyword *token = b;
|
|
|
|
|
|
|
|
return strcmp(key, token->token);
|
|
|
|
}
|
|
|
|
|
2020-07-19 00:02:25 +00:00
|
|
|
bool
|
2020-10-14 09:48:20 +00:00
|
|
|
lif_interface_file_parse(struct lif_interface_file_parse_state *state, const char *filename)
|
2020-07-19 00:02:25 +00:00
|
|
|
{
|
2020-10-14 10:07:14 +00:00
|
|
|
struct lif_dict_entry *entry = lif_dict_find(&state->loaded, filename);
|
|
|
|
if (entry != NULL)
|
|
|
|
{
|
|
|
|
report_error(state, "skipping already included file %s", filename);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
FILE *f = fopen(filename, "r");
|
|
|
|
if (f == NULL)
|
|
|
|
return false;
|
|
|
|
|
2020-10-14 09:48:20 +00:00
|
|
|
const char *old_filename = state->cur_filename;
|
|
|
|
state->cur_filename = filename;
|
|
|
|
|
|
|
|
size_t old_lineno = state->cur_lineno;
|
|
|
|
state->cur_lineno = 0;
|
|
|
|
|
2020-10-14 10:07:14 +00:00
|
|
|
lif_dict_add(&state->loaded, filename, NULL);
|
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
char linebuf[4096];
|
|
|
|
while (lif_fgetline(linebuf, sizeof linebuf, f) != NULL)
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
state->cur_lineno++;
|
2020-08-20 09:34:59 +00:00
|
|
|
|
2020-07-19 00:48:49 +00:00
|
|
|
char *bufp = linebuf;
|
2020-07-24 10:12:42 +00:00
|
|
|
char *token = lif_next_token(&bufp);
|
2020-07-19 00:48:49 +00:00
|
|
|
|
|
|
|
if (!*token || !isalpha(*token))
|
|
|
|
continue;
|
|
|
|
|
2020-08-20 09:34:59 +00:00
|
|
|
const struct parser_keyword *parserkw =
|
|
|
|
bsearch(token, keywords, ARRAY_SIZE(keywords), sizeof(*keywords), keyword_cmp);
|
|
|
|
|
|
|
|
if (parserkw != NULL)
|
|
|
|
{
|
2020-10-14 09:48:20 +00:00
|
|
|
if (!parserkw->handle(state, token, bufp))
|
2020-08-20 09:34:59 +00:00
|
|
|
goto parse_error;
|
|
|
|
}
|
2020-10-14 09:48:20 +00:00
|
|
|
else if (!handle_generic(state, token, bufp))
|
2020-08-20 10:04:34 +00:00
|
|
|
goto parse_error;
|
2020-07-19 00:48:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
2020-10-14 09:48:20 +00:00
|
|
|
state->cur_filename = old_filename;
|
|
|
|
state->cur_lineno = old_lineno;
|
2020-07-19 00:02:25 +00:00
|
|
|
return true;
|
2020-07-19 00:48:49 +00:00
|
|
|
|
|
|
|
parse_error:
|
|
|
|
fclose(f);
|
2020-10-14 09:48:20 +00:00
|
|
|
state->cur_filename = old_filename;
|
|
|
|
state->cur_lineno = old_lineno;
|
2020-07-19 00:48:49 +00:00
|
|
|
return false;
|
2020-07-19 00:02:25 +00:00
|
|
|
}
|