iproute2: ip addr: Organize flag properties structurally

This creates a nice systematic way to check that the various flags are
mutable from userspace and that the address family is valid.

Mutability properties are preserved to avoid introducing any behavioral
change in this CL. However, previously, immutable flags were ignored and
fell through to this confusing error:

Error: either "local" is duplicate, or "dadfailed" is a garbage.

But now, they just warn more explicitly:

Warning: dadfailed option is not mutable from userspace
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Ian K. Coolidge 2020-05-27 11:03:45 -07:00 committed by David Ahern
parent bd4b8c632e
commit 9d59c86e57

View File

@ -1233,52 +1233,63 @@ static unsigned int get_ifa_flags(struct ifaddrmsg *ifa,
ifa->ifa_flags; ifa->ifa_flags;
} }
/* Mapping from argument to address flag mask */ /* Mapping from argument to address flag mask and attributes */
static const struct { static const struct ifa_flag_data_t {
const char *name; const char *name;
unsigned long value; unsigned long mask;
} ifa_flag_names[] = { bool readonly;
{ "secondary", IFA_F_SECONDARY }, bool v6only;
{ "temporary", IFA_F_SECONDARY }, } ifa_flag_data[] = {
{ "nodad", IFA_F_NODAD }, { .name = "secondary", .mask = IFA_F_SECONDARY, .readonly = true, .v6only = false},
{ "optimistic", IFA_F_OPTIMISTIC }, { .name = "temporary", .mask = IFA_F_SECONDARY, .readonly = true, .v6only = false},
{ "dadfailed", IFA_F_DADFAILED }, { .name = "nodad", .mask = IFA_F_NODAD, .readonly = false, .v6only = true},
{ "home", IFA_F_HOMEADDRESS }, { .name = "optimistic", .mask = IFA_F_OPTIMISTIC, .readonly = true, .v6only = true},
{ "deprecated", IFA_F_DEPRECATED }, { .name = "dadfailed", .mask = IFA_F_DADFAILED, .readonly = true, .v6only = true},
{ "tentative", IFA_F_TENTATIVE }, { .name = "home", .mask = IFA_F_HOMEADDRESS, .readonly = false, .v6only = true},
{ "permanent", IFA_F_PERMANENT }, { .name = "deprecated", .mask = IFA_F_DEPRECATED, .readonly = true, .v6only = true},
{ "mngtmpaddr", IFA_F_MANAGETEMPADDR }, { .name = "tentative", .mask = IFA_F_TENTATIVE, .readonly = true, .v6only = true},
{ "noprefixroute", IFA_F_NOPREFIXROUTE }, { .name = "permanent", .mask = IFA_F_PERMANENT, .readonly = true, .v6only = true},
{ "autojoin", IFA_F_MCAUTOJOIN }, { .name = "mngtmpaddr", .mask = IFA_F_MANAGETEMPADDR, .readonly = false, .v6only = true},
{ "stable-privacy", IFA_F_STABLE_PRIVACY }, { .name = "noprefixroute", .mask = IFA_F_NOPREFIXROUTE, .readonly = false, .v6only = true},
{ .name = "autojoin", .mask = IFA_F_MCAUTOJOIN, .readonly = false, .v6only = true},
{ .name = "stable-privacy", .mask = IFA_F_STABLE_PRIVACY, .readonly = true, .v6only = true},
}; };
/* Returns a pointer to the data structure for a particular interface flag, or null if no flag could be found */
static const struct ifa_flag_data_t* lookup_flag_data_by_name(const char* flag_name) {
for (int i = 0; i < ARRAY_SIZE(ifa_flag_data); ++i) {
if (strcmp(flag_name, ifa_flag_data[i].name) == 0)
return &ifa_flag_data[i];
}
return NULL;
}
static void print_ifa_flags(FILE *fp, const struct ifaddrmsg *ifa, static void print_ifa_flags(FILE *fp, const struct ifaddrmsg *ifa,
unsigned int flags) unsigned int flags)
{ {
unsigned int i; unsigned int i;
for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) { for (i = 0; i < ARRAY_SIZE(ifa_flag_data); i++) {
unsigned long mask = ifa_flag_names[i].value; const struct ifa_flag_data_t* flag_data = &ifa_flag_data[i];
if (mask == IFA_F_PERMANENT) { if (flag_data->mask == IFA_F_PERMANENT) {
if (!(flags & mask)) if (!(flags & flag_data->mask))
print_bool(PRINT_ANY, print_bool(PRINT_ANY,
"dynamic", "dynamic ", true); "dynamic", "dynamic ", true);
} else if (flags & mask) { } else if (flags & flag_data->mask) {
if (mask == IFA_F_SECONDARY && if (flag_data->mask == IFA_F_SECONDARY &&
ifa->ifa_family == AF_INET6) { ifa->ifa_family == AF_INET6) {
print_bool(PRINT_ANY, print_bool(PRINT_ANY,
"temporary", "temporary ", true); "temporary", "temporary ", true);
} else { } else {
print_string(PRINT_FP, NULL, print_string(PRINT_FP, NULL,
"%s ", ifa_flag_names[i].name); "%s ", flag_data->name);
print_bool(PRINT_JSON, print_bool(PRINT_JSON,
ifa_flag_names[i].name, NULL, true); flag_data->name, NULL, true);
} }
} }
flags &= ~mask; flags &= ~flag_data->mask;
} }
if (flags) { if (flags) {
@ -1297,7 +1308,6 @@ static void print_ifa_flags(FILE *fp, const struct ifaddrmsg *ifa,
static int get_filter(const char *arg) static int get_filter(const char *arg)
{ {
bool inv = false; bool inv = false;
unsigned int i;
if (arg[0] == '-') { if (arg[0] == '-') {
inv = true; inv = true;
@ -1313,18 +1323,16 @@ static int get_filter(const char *arg)
arg = "secondary"; arg = "secondary";
} }
for (i = 0; i < ARRAY_SIZE(ifa_flag_names); i++) { const struct ifa_flag_data_t* flag_data = lookup_flag_data_by_name(arg);
if (strcmp(arg, ifa_flag_names[i].name)) if (flag_data == NULL)
continue; return -1;
if (inv) if (inv)
filter.flags &= ~ifa_flag_names[i].value; filter.flags &= ~flag_data->mask;
else else
filter.flags |= ifa_flag_names[i].value; filter.flags |= flag_data->mask;
filter.flagmask |= ifa_flag_names[i].value; filter.flagmask |= flag_data->mask;
return 0; return 0;
}
return -1;
} }
static int ifa_label_match_rta(int ifindex, const struct rtattr *rta) static int ifa_label_match_rta(int ifindex, const struct rtattr *rta)
@ -2330,25 +2338,15 @@ static int ipaddr_modify(int cmd, int flags, int argc, char **argv)
preferred_lftp = *argv; preferred_lftp = *argv;
if (set_lifetime(&preferred_lft, *argv)) if (set_lifetime(&preferred_lft, *argv))
invarg("preferred_lft value", *argv); invarg("preferred_lft value", *argv);
} else if (strcmp(*argv, "home") == 0) { } else if (lookup_flag_data_by_name(*argv)) {
if (req.ifa.ifa_family == AF_INET6) const struct ifa_flag_data_t* flag_data = lookup_flag_data_by_name(*argv);
ifa_flags |= IFA_F_HOMEADDRESS; if (flag_data->readonly) {
else fprintf(stderr, "Warning: %s option is not mutable from userspace\n", flag_data->name);
fprintf(stderr, "Warning: home option can be set only for IPv6 addresses\n"); } else if (flag_data->v6only && req.ifa.ifa_family != AF_INET6) {
} else if (strcmp(*argv, "nodad") == 0) { fprintf(stderr, "Warning: %s option can be set only for IPv6 addresses\n", flag_data->name);
if (req.ifa.ifa_family == AF_INET6) } else {
ifa_flags |= IFA_F_NODAD; ifa_flags |= flag_data->mask;
else }
fprintf(stderr, "Warning: nodad option can be set only for IPv6 addresses\n");
} else if (strcmp(*argv, "mngtmpaddr") == 0) {
if (req.ifa.ifa_family == AF_INET6)
ifa_flags |= IFA_F_MANAGETEMPADDR;
else
fprintf(stderr, "Warning: mngtmpaddr option can be set only for IPv6 addresses\n");
} else if (strcmp(*argv, "noprefixroute") == 0) {
ifa_flags |= IFA_F_NOPREFIXROUTE;
} else if (strcmp(*argv, "autojoin") == 0) {
ifa_flags |= IFA_F_MCAUTOJOIN;
} else { } else {
if (strcmp(*argv, "local") == 0) if (strcmp(*argv, "local") == 0)
NEXT_ARG(); NEXT_ARG();