From 18e4260cf294d3e5885273b8ad0ef470e8c6c61e Mon Sep 17 00:00:00 2001 From: rob Date: Sun, 20 Aug 2017 17:49:29 +0000 Subject: Stop tracking interface indexes. Suggested by deraadt. Tweaks and ok jca@ "just get it in" deraadt@ --- usr.sbin/ifstated/ifstated.c | 27 ++++++++++++++++----------- usr.sbin/ifstated/ifstated.h | 4 ++-- usr.sbin/ifstated/parse.y | 21 ++++++++++++--------- 3 files changed, 30 insertions(+), 22 deletions(-) (limited to 'usr.sbin/ifstated') diff --git a/usr.sbin/ifstated/ifstated.c b/usr.sbin/ifstated/ifstated.c index ba2b89f9321..87cbf7b78d6 100644 --- a/usr.sbin/ifstated/ifstated.c +++ b/usr.sbin/ifstated/ifstated.c @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.c,v 1.59 2017/08/14 03:15:28 rob Exp $ */ +/* $OpenBSD: ifstated.c,v 1.60 2017/08/20 17:49:29 rob Exp $ */ /* * Copyright (c) 2004 Marco Pfatschbacher @@ -61,8 +61,8 @@ void external_handler(int, short, void *); void external_exec(struct ifsd_external *, int); void check_external_status(struct ifsd_state *); void external_evtimer_setup(struct ifsd_state *, int); -void scan_ifstate(int, int, int); -int scan_ifstate_single(int, int, struct ifsd_state *); +void scan_ifstate(const char *, int, int); +int scan_ifstate_single(const char *, int, struct ifsd_state *); void fetch_ifstate(int); __dead void usage(void); void adjust_expressions(struct ifsd_expression_list *, int); @@ -233,6 +233,8 @@ rt_msg_handler(int fd, short event, void *arg) char msg[2048]; struct rt_msghdr *rtm = (struct rt_msghdr *)&msg; struct if_msghdr ifm; + char ifnamebuf[IFNAMSIZ]; + char *ifname; ssize_t len; if ((len = read(fd, msg, sizeof(msg))) == -1) { @@ -250,7 +252,10 @@ rt_msg_handler(int fd, short event, void *arg) switch (rtm->rtm_type) { case RTM_IFINFO: memcpy(&ifm, rtm, sizeof(ifm)); - scan_ifstate(ifm.ifm_index, ifm.ifm_data.ifi_link_state, 1); + ifname = if_indextoname(ifm.ifm_index, ifnamebuf); + /* ifname is NULL on interface departure */ + if (ifname != NULL) + scan_ifstate(ifname, ifm.ifm_data.ifi_link_state, 1); break; case RTM_DESYNC: fetch_ifstate(1); @@ -431,7 +436,7 @@ external_evtimer_setup(struct ifsd_state *state, int action) #define LINK_STATE_IS_DOWN(_s) (!LINK_STATE_IS_UP((_s))) int -scan_ifstate_single(int ifindex, int s, struct ifsd_state *state) +scan_ifstate_single(const char *ifname, int s, struct ifsd_state *state) { struct ifsd_ifstate *ifstate; struct ifsd_expression_list expressions; @@ -440,7 +445,7 @@ scan_ifstate_single(int ifindex, int s, struct ifsd_state *state) TAILQ_INIT(&expressions); TAILQ_FOREACH(ifstate, &state->interface_states, entries) { - if (ifstate->ifindex == ifindex) { + if (strcmp(ifstate->ifname, ifname) == 0) { if (ifstate->prevstate != s && (ifstate->prevstate != -1 || !opt_inhibit)) { struct ifsd_expression *expression; @@ -472,15 +477,15 @@ scan_ifstate_single(int ifindex, int s, struct ifsd_state *state) } void -scan_ifstate(int ifindex, int s, int do_eval) +scan_ifstate(const char *ifname, int s, int do_eval) { struct ifsd_state *state; int cur_eval = 0; - if (scan_ifstate_single(ifindex, s, &conf->initstate) && do_eval) + if (scan_ifstate_single(ifname, s, &conf->initstate) && do_eval) eval_state(&conf->initstate); TAILQ_FOREACH(state, &conf->states, entries) { - if (scan_ifstate_single(ifindex, s, state) && + if (scan_ifstate_single(ifname, s, state) && (do_eval && state == conf->curstate)) cur_eval = 1; } @@ -619,8 +624,8 @@ fetch_ifstate(int do_eval) for (ifa = ifap; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr->sa_family == AF_LINK) { struct if_data *ifdata = ifa->ifa_data; - scan_ifstate(if_nametoindex(ifa->ifa_name), - ifdata->ifi_link_state, do_eval); + scan_ifstate(ifa->ifa_name, ifdata->ifi_link_state, + do_eval); } } diff --git a/usr.sbin/ifstated/ifstated.h b/usr.sbin/ifstated/ifstated.h index 8b35959ed65..e2462be9032 100644 --- a/usr.sbin/ifstated/ifstated.h +++ b/usr.sbin/ifstated/ifstated.h @@ -1,4 +1,4 @@ -/* $OpenBSD: ifstated.h,v 1.18 2017/08/14 03:15:28 rob Exp $ */ +/* $OpenBSD: ifstated.h,v 1.19 2017/08/20 17:49:29 rob Exp $ */ /* * Copyright (c) 2004 Ryan McBride @@ -41,7 +41,7 @@ struct ifsd_ifstate { #define IFSD_LINKUP 2 int prevstate; u_int32_t refcount; - u_short ifindex; + char ifname[IFNAMSIZ]; }; struct ifsd_external { diff --git a/usr.sbin/ifstated/parse.y b/usr.sbin/ifstated/parse.y index 62ee4e2dfbc..0d8a1222333 100644 --- a/usr.sbin/ifstated/parse.y +++ b/usr.sbin/ifstated/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.45 2017/07/23 13:53:54 deraadt Exp $ */ +/* $OpenBSD: parse.y,v 1.46 2017/08/20 17:49:29 rob Exp $ */ /* * Copyright (c) 2004 Ryan McBride @@ -85,7 +85,7 @@ struct ifsd_state *curstate; void link_states(struct ifsd_action *); void set_expression_depth(struct ifsd_expression *, int); void init_state(struct ifsd_state *); -struct ifsd_ifstate *new_ifstate(u_short, int); +struct ifsd_ifstate *new_ifstate(char *, int); struct ifsd_external *new_external(char *, u_int32_t); typedef struct { @@ -93,7 +93,6 @@ typedef struct { int64_t number; char *string; struct in_addr addr; - u_short interface; struct ifsd_expression *expression; struct ifsd_ifstate *ifstate; @@ -114,7 +113,7 @@ typedef struct { %token STRING %token NUMBER %type string -%type interface +%type interface %type if_test %type ext_test %type expr term @@ -170,12 +169,12 @@ conf_main : INITSTATE STRING { ; interface : STRING { - if (($$ = if_nametoindex($1)) == 0) { + if (if_nametoindex($1) == 0) { yyerror("unknown interface %s", $1); free($1); YYERROR; } - free($1); + $$ = $1; } ; @@ -933,7 +932,7 @@ init_state(struct ifsd_state *state) } struct ifsd_ifstate * -new_ifstate(u_short ifindex, int s) +new_ifstate(char *ifname, int s) { struct ifsd_ifstate *ifstate = NULL; struct ifsd_state *state; @@ -944,12 +943,16 @@ new_ifstate(u_short ifindex, int s) state = &conf->initstate; TAILQ_FOREACH(ifstate, &state->interface_states, entries) - if (ifstate->ifindex == ifindex && ifstate->ifstate == s) + if (strcmp(ifstate->ifname, ifname) == 0 && + ifstate->ifstate == s) break; if (ifstate == NULL) { if ((ifstate = calloc(1, sizeof(*ifstate))) == NULL) err(1, NULL); - ifstate->ifindex = ifindex; + if (strlcpy(ifstate->ifname, ifname, + sizeof(ifstate->ifname)) >= sizeof(ifstate->ifname)) + errx(1, "ifname strlcpy truncation"); + free(ifname); ifstate->ifstate = s; TAILQ_INIT(&ifstate->expressions); TAILQ_INSERT_TAIL(&state->interface_states, ifstate, entries); -- cgit v1.2.3-59-g8ed1b