1
0
mirror of https://git.FreeBSD.org/ports.git synced 2024-11-23 00:43:28 +00:00

Add ifstated, a port of src/usr.sbin/ifstated from OpenBSD. It has been

modified to use kqueue/kevent and sysctl.

The ifstated daemon runs commands in response to network state changes,
which it determines by monitoring interface link state or running exter-
nal tests.  For example, it can be used with carp(4) to change running
services or to ensure that carp(4) interfaces stay in sync, or with pf(4)
to test server or link availability and modify translation or routing
rules.

PR:		ports/74096
Submitted by:	Matthew George <mdg@secureworks.net>
This commit is contained in:
Pav Lucistnik 2004-11-21 03:08:36 +00:00
parent 46bfe4af92
commit b10532a581
Notes: svn2git 2021-03-31 03:12:20 +00:00
svn path=/head/; revision=122052
11 changed files with 577 additions and 0 deletions

View File

@ -218,6 +218,7 @@
SUBDIR += icqlib0
SUBDIR += icqnix
SUBDIR += ifstat
SUBDIR += ifstated
SUBDIR += imapproxy
SUBDIR += imcom
SUBDIR += ip6_int

44
net/ifstated/Makefile Normal file
View File

@ -0,0 +1,44 @@
# New ports collection makefile for: ifstated
# Date created: 18 November 2004
# Whom: Matthew George <mdg@secureworks.net>
#
# $FreeBSD$
#
PORTNAME= ifstated
PORTVERSION= 20041117
CATEGORIES= net
MASTER_SITES= ftp://ftp.secureworks.net/pub/ifstated/
MAINTAINER= mdg@secureworks.net
COMMENT= Interface State daemon
INSTALL_TARGET= install install-man
MAN8= ifstated.8
PLIST_FILES= sbin/ifstated \
etc/ifstated.conf etc/rc.d/ifstated.sh
USE_REINPLACE= yes
PKGMESSAGE= ${WRKDIR}/pkg-message
USE_RC_SUBR= yes
RC_SCRIPTS_SUB= PREFIX=${PREFIX} RC_SUBR=${RC_SUBR}
post-patch:
@${SED} ${RC_SCRIPTS_SUB:S/$/!g/:S/^/ -e s!%%/:S/=/%%!/} \
${FILESDIR}/ifstated.sh > ${WRKDIR}/ifstated.sh
@${SED} -e 's|%%PREFIX%%|${PREFIX}|g' ${FILESDIR}/pkg-message.in > ${PKGMESSAGE}
@${REINPLACE_CMD} -e 's|%%PREFIX%%|${PREFIX}|g' ${WRKSRC}/ifstated.c ${WRKSRC}/ifstated.8
post-install:
${INSTALL_SCRIPT} ${WRKDIR}/ifstated.sh ${PREFIX}/etc/rc.d/ifstated.sh
@${CAT} ${PKGMESSAGE}
.include <bsd.port.pre.mk>
.if ${OSVERSION} < 502113
IGNORE= need link state change notification changes from May 2004
.endif
.include <bsd.port.post.mk>

2
net/ifstated/distinfo Normal file
View File

@ -0,0 +1,2 @@
MD5 (ifstated-20041117.tar.gz) = 3255839ec706b5c40b7445677a8e7aa9
SIZE (ifstated-20041117.tar.gz) = 12270

View File

@ -0,0 +1,31 @@
#!/bin/sh
#
# $FreeBSD$
#
# PROVIDE: ifstated
# REQUIRE: NETWORKING SERVERS
# BEFORE: DAEMON
# KEYWORD: FreeBSD shutdown
#
# Add the following lines to /etc/rc.conf to enable ifstated:
# ifstated_enable (bool): Set to "NO" by default.
# Set it to "YES" to enable ifstated
# ifstated_flags (str): Set to "-f %%PREFIX%%/etc/ifstated.conf" by default.
# Extra flags passed to start command
#
. %%RC_SUBR%%
name="ifstated"
rcvar=`set_rcvar`
command="%%PREFIX%%/sbin/ifstated"
[ -z "$ifstated_enable" ] && ifstated_enable="NO"
[ -z "$ifstated_flags" ] && ifstated_flags="-f %%PREFIX%%/etc/ifstated.conf"
load_rc_config $name
run_rc_command "$1"

View File

@ -0,0 +1,24 @@
--- Makefile 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ Makefile 18 Nov 2004 23:33:55 -0000 1.4
@@ -1,4 +1,5 @@
# $OpenBSD: Makefile,v 1.3 2004/02/05 02:26:23 mcbride Exp $
+# $Id: Makefile,v 1.4 2004/11/18 23:33:55 mdg Exp $
PROG= ifstated
SRCS= ifstated.c parse.y
@@ -7,6 +8,14 @@
CLFAGS+= -Wmissing-declarations -Wredundant-decls
CFLAGS+= -Wshadow -Wpointer-arith -Wcast-qual
MAN= ifstated.8
-LDADD+=-lutil -levent
+
+
+install:
+ $(INSTALL) -m 555 -g bin -o bin ifstated $(PREFIX)/sbin
+ $(INSTALL) -m 644 -g wheel -o root etc/ifstated.conf $(PREFIX)/etc
+
+install-man:
+ $(INSTALL) -m 444 -g bin -o bin ifstated.8 $(PREFIX)/man/man8
+
.include <bsd.prog.mk>

View File

@ -0,0 +1,34 @@
--- etc/ifstated.conf 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ etc/ifstated.conf 18 Nov 2004 21:48:39 -0000 1.2
@@ -1,4 +1,5 @@
# $OpenBSD: ifstated.conf,v 1.4 2004/04/28 01:01:27 deraadt Exp $
+# $Id: ifstated.conf,v 1.2 2004/11/18 21:48:39 mdg Exp $
# This is a sample config for a pair of firewalls with two interfaces
#
# carp0 and carp1 have ip addresses on 192.168.3.0/24 and 192.168.6.0/24
@@ -7,7 +8,7 @@
# net.inet.carp.preempt must be enabled (set to 1) for this to work correctly.
# Uncomment one of the following lines to force primary/backup status.
-# init-state primary
+init-state primary
# init-state backup
carp_up = "((carp0 link up) and (carp1 link up))"
@@ -18,12 +19,12 @@
# The "net" addresses are other addresses which can be used to determine
# whether we have connectivity. Make sure the hosts are always up, or
# test multiple ip's, 'or'-ing the tests.
-net = '( "ping -q -c 1 -w 1 192.168.6.8 > /dev/null" every 10 and \
- "ping -q -c 1 -w 1 192.168.3.8 > /dev/null" every 10)'
+net = '( "ping -q -c 1 -t 1 192.168.6.8 > /dev/null" every 10 and \
+ "ping -q -c 1 -t 1 192.168.3.8 > /dev/null" every 10)'
# The peer addresses below are the real ip addresses of the OTHER firewall
-peer = '( "ping -q -c 1 -w 1 192.168.6.7 > /dev/null" every 10 and \
- "ping -q -c 1 -w 1 192.168.3.7 > /dev/null" every 10)'
+peer = '( "ping -q -c 1 -t 1 192.168.6.7 > /dev/null" every 10 and \
+ "ping -q -c 1 -t 1 192.168.3.7 > /dev/null" every 10)'
state auto {
if $carp_up {

View File

@ -0,0 +1,17 @@
--- ifstated.8 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ ifstated.8 18 Nov 2004 22:14:58 -0000 1.3
@@ -1,4 +1,5 @@
.\" $OpenBSD: ifstated.8,v 1.5 2004/09/27 22:26:26 jaredy Exp $
+.\" $Id: ifstated.8,v 1.3 2004/11/18 22:14:58 mdg Exp $
.\"
.\" Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
.\"
@@ -79,7 +80,7 @@
reloads the configuration file.
.Sh FILES
.Bl -tag -width "/etc/ifstated.conf" -compact
-.It Pa /etc/ifstated.conf
+.It Pa %%PREFIX%%/etc/ifstated.conf
.Nm
configuration file.
.El

View File

@ -0,0 +1,350 @@
--- ifstated.c 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ ifstated.c 18 Nov 2004 21:48:39 -0000 1.2
@@ -1,4 +1,5 @@
/* $OpenBSD: ifstated.c,v 1.20 2004/10/05 21:17:02 mpf Exp $ */
+/* $Id: ifstated.c,v 1.2 2004/11/18 21:48:39 mdg Exp $ */
/*
* Copyright (c) 2004 Marco Pfatschbacher <mpf@openbsd.org>
@@ -23,12 +24,15 @@
*/
#include <sys/types.h>
+#include <sys/event.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/wait.h>
+#include <sys/sysctl.h>
#include <net/if.h>
+#include <net/if_mib.h>
#include <net/route.h>
#include <netinet/in.h>
@@ -38,8 +42,6 @@
#include <fcntl.h>
#include <signal.h>
#include <err.h>
-#include <event.h>
-#include <util.h>
#include <unistd.h>
#include <syslog.h>
#include <stdarg.h>
@@ -52,15 +54,15 @@
int opts = 0;
int opt_debug = 0;
int opt_inhibit = 0;
-char *configfile = "/etc/ifstated.conf";
-struct event rt_msg_ev, sighup_ev, startup_ev, sigchld_ev;
+char *configfile = "%%PREFIX%%/etc/ifstated.conf";
+int kq;
+struct kevent kev;
-void startup_handler(int, short, void *);
-void sighup_handler(int, short, void *);
+void startup_handler(void);
+void sighup_handler(void);
int load_config(void);
void sigchld_handler(int, short, void *);
-void rt_msg_handler(int, short, void *);
-void external_handler(int, short, void *);
+void rt_msg_handler(int fd);
void external_async_exec(struct ifsd_external *);
void check_external_status(struct ifsd_state *);
void external_evtimer_setup(struct ifsd_state *, int);
@@ -76,6 +78,8 @@
void remove_expression(struct ifsd_expression *, struct ifsd_state *);
void log_init(int);
void logit(int level, const char *fmt, ...);
+int get_ifcount(void);
+int get_ifmib_general(int, struct ifmibdata *);
void
usage(void)
@@ -90,7 +94,7 @@
int
main(int argc, char *argv[])
{
- struct timeval tv;
+ struct timespec ts;
int ch;
while ((ch = getopt(argc, argv, "dD:f:hniv")) != -1) {
@@ -137,26 +141,54 @@
setproctitle(NULL);
}
- event_init();
+ kq = kqueue();
+
log_init(opt_debug);
- signal_set(&sigchld_ev, SIGCHLD, sigchld_handler, &sigchld_ev);
- signal_add(&sigchld_ev, NULL);
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
+
+ EV_SET(&kev, SIGCHLD, EVFILT_SIGNAL, EV_ADD, 0, 0, (void *)sigchld_handler);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
/* Loading the config needs to happen in the event loop */
- tv.tv_usec = 0;
- tv.tv_sec = 0;
- evtimer_set(&startup_ev, startup_handler, &startup_ev);
- evtimer_add(&startup_ev, &tv);
- event_loop(0);
+ EV_SET(&kev, IFSD_EVTIMER_STARTUP, EVFILT_TIMER, EV_ADD|EV_ONESHOT, 0, 0, (void *)startup_handler);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
+
+ /* event loop */
+ for(;;)
+ {
+ /* wait indefinitely for an event */
+ kevent(kq, NULL, 0, &kev, 1, NULL);
+
+ void (*handler)(void);
+ void (*rt_handler)(int);
+ if (kev.filter == EVFILT_READ)
+ {
+ rt_handler = kev.udata;
+ rt_handler(kev.ident);
+ }
+ else if ((kev.filter == EVFILT_TIMER) && (kev.ident == IFSD_EVTIMER_EXTERNAL))
+ {
+ external_async_exec((struct ifsd_external *)kev.udata);
+ }
+ else
+ {
+ handler = kev.udata;
+ handler();
+ }
+ }
+
+ /* NOTREACHED */
exit(0);
}
void
-startup_handler(int fd, short event, void *arg)
+startup_handler()
{
int rt_fd;
+ struct timespec ts;
if (load_config() != 0) {
logit(IFSD_LOG_NORMAL, "unable to load config");
@@ -166,18 +198,20 @@
if ((rt_fd = socket(PF_ROUTE, SOCK_RAW, 0)) < 0)
err(1, "no routing socket");
- event_set(&rt_msg_ev, rt_fd, EV_READ|EV_PERSIST,
- rt_msg_handler, &rt_msg_ev);
- event_add(&rt_msg_ev, NULL);
+ ts.tv_sec = 0;
+ ts.tv_nsec = 0;
- signal_set(&sighup_ev, SIGHUP, sighup_handler, &sighup_ev);
- signal_add(&sighup_ev, NULL);
+ EV_SET(&kev, rt_fd, EVFILT_READ, EV_ADD, 0, 0, (void *)rt_msg_handler);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
+
+ EV_SET(&kev, SIGHUP, EVFILT_SIGNAL, EV_ADD, 0, 0, (void *)sighup_handler);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
logit(IFSD_LOG_NORMAL, "started");
}
void
-sighup_handler(int fd, short event, void *arg)
+sighup_handler()
{
logit(IFSD_LOG_NORMAL, "reloading config");
if (load_config() != 0)
@@ -208,7 +242,7 @@
}
void
-rt_msg_handler(int fd, short event, void *arg)
+rt_msg_handler(int fd)
{
char msg[2048];
struct rt_msghdr *rtm = (struct rt_msghdr *)&msg;
@@ -246,22 +280,6 @@
}
void
-external_handler(int fd, short event, void *arg)
-{
- struct ifsd_external *external = (struct ifsd_external *)arg;
- struct timeval tv;
-
- /* re-schedule */
- tv.tv_usec = 0;
- tv.tv_sec = external->frequency;
- evtimer_set(&external->ev, external_handler, external);
- evtimer_add(&external->ev, &tv);
-
- /* execute */
- external_async_exec(external);
-}
-
-void
external_async_exec(struct ifsd_external *external)
{
char *argp[] = {"sh", "-c", NULL, NULL};
@@ -355,23 +373,25 @@
external_evtimer_setup(struct ifsd_state *state, int action)
{
struct ifsd_external *external;
+ struct timespec ts;
+ int freq;
+
+ ts.tv_nsec = 0;
+ ts.tv_sec = 0;
if (state != NULL) {
switch (action) {
case IFSD_EVTIMER_ADD:
TAILQ_FOREACH(external,
&state->external_tests, entries) {
- struct timeval tv;
-
+
/* run it once right away */
external_async_exec(external);
/* schedule it for later */
- tv.tv_usec = 0;
- tv.tv_sec = external->frequency;
- evtimer_set(&external->ev, external_handler,
- external);
- evtimer_add(&external->ev, &tv);
+ freq = (external->frequency * 1000);
+ EV_SET(&kev, IFSD_EVTIMER_EXTERNAL, EVFILT_TIMER, EV_ADD, 0, freq, (void *)external);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
}
break;
case IFSD_EVTIMER_DEL:
@@ -381,7 +401,9 @@
kill(external->pid, SIGKILL);
external->pid = 0;
}
- evtimer_del(&external->ev);
+ freq = (external->frequency * 1000);
+ EV_SET(&kev, IFSD_EVTIMER_EXTERNAL, EVFILT_TIMER, EV_DELETE, 0, freq, (void *)external);
+ kevent(kq, &kev, 1, NULL, 0, &ts);
}
break;
}
@@ -505,7 +527,6 @@
logit(IFSD_LOG_NORMAL, "changing state to %s",
conf->nextstate->name);
if (conf->curstate != NULL) {
- evtimer_del(&conf->curstate->ev);
external_evtimer_setup(conf->curstate,
IFSD_EVTIMER_DEL);
}
@@ -551,6 +572,48 @@
}
}
+
+int
+get_ifcount(void)
+{
+ int name[5], count;
+ size_t len;
+
+ name[0] = CTL_NET;
+ name[1] = PF_LINK;
+ name[2] = NETLINK_GENERIC;
+ name[3] = IFMIB_SYSTEM;
+ name[4] = IFMIB_IFCOUNT;
+
+ len = sizeof(int);
+
+ if (sysctl(name, 5, &count, &len, NULL, 0) != -1)
+ return(count);
+ else
+ return(-1);
+}
+
+
+int
+get_ifmib_general(int row, struct ifmibdata *ifmd)
+{
+ int name[6];
+ size_t len;
+
+ name[0] = CTL_NET;
+ name[1] = PF_LINK;
+ name[2] = NETLINK_GENERIC;
+ name[3] = IFMIB_IFDATA;
+ name[4] = row;
+ name[5] = IFDATA_GENERAL;
+
+ len = sizeof(*ifmd);
+
+ return sysctl(name, 6, ifmd, &len, (void *)0, 0);
+}
+
+
+
/*
* Fetch the current link states.
*/
@@ -560,29 +623,34 @@
struct ifaddrs *ifap, *ifa;
char *oname = NULL;
int sock = socket(AF_INET, SOCK_DGRAM, 0);
+ int ifcount = get_ifcount();
+ int i;
- if (getifaddrs(&ifap) != 0)
+ if (getifaddrs(&ifap) != 0 || ifcount == -1)
err(1, "getifaddrs");
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
- struct ifreq ifr;
- struct if_data ifrdat;
+ struct ifmibdata ifmd;
+ struct if_data ifdata;
if (oname && !strcmp(oname, ifa->ifa_name))
continue;
oname = ifa->ifa_name;
- strlcpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name));
- ifr.ifr_data = (caddr_t)&ifrdat;
-
- if (ioctl(sock, SIOCGIFDATA, (caddr_t)&ifr) == -1)
- continue;
+ for (i = 1; i <= ifcount; i++)
+ {
+ get_ifmib_general(i, &ifmd);
+ if (! strcmp(ifmd.ifmd_name, oname))
+ break;
+ }
+
+ ifdata = ifmd.ifmd_data;
scan_ifstate(if_nametoindex(ifa->ifa_name),
- ifrdat.ifi_link_state, &conf->always);
+ ifdata.ifi_link_state, &conf->always);
if (conf->curstate != NULL)
scan_ifstate(if_nametoindex(ifa->ifa_name),
- ifrdat.ifi_link_state, conf->curstate);
+ ifdata.ifi_link_state, conf->curstate);
}
freeifaddrs(ifap);
close(sock);
@@ -664,7 +732,6 @@
TAILQ_REMOVE(&state->external_tests,
expression->u.external, entries);
free(expression->u.external->command);
- event_del(&expression->u.external->ev);
free(expression->u.external);
}
break;

View File

@ -0,0 +1,34 @@
--- ifstated.h 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ ifstated.h 18 Nov 2004 21:48:39 -0000 1.2
@@ -1,4 +1,5 @@
/* $OpenBSD: ifstated.h,v 1.4 2004/03/10 00:13:38 deraadt Exp $ */
+/* $Id: ifstated.h,v 1.2 2004/11/18 21:48:39 mdg Exp $ */
/*
* Copyright (c) 2004 Ryan McBride
@@ -47,7 +48,7 @@
struct ifsd_external {
TAILQ_ENTRY(ifsd_external) entries;
- struct event ev;
+ struct kevent kev;
struct ifsd_expression_list expressions;
char *command;
int prevstatus;
@@ -107,7 +108,7 @@
TAILQ_HEAD(ifsd_external_list, ifsd_external);
struct ifsd_state {
- struct event ev;
+ struct kevent kev;
struct ifsd_ifstate_list interface_states;
struct ifsd_external_list external_tests;
TAILQ_ENTRY(ifsd_state) entries;
@@ -138,6 +139,7 @@
};
enum { IFSD_EVTIMER_ADD, IFSD_EVTIMER_DEL };
+enum { IFSD_EVTIMER_STARTUP, IFSD_EVTIMER_EXTERNAL };
struct ifsd_config *parse_config(char *, int);
int cmdline_symset(char *);
void clear_config(struct ifsd_config *);

View File

@ -0,0 +1,25 @@
--- parse.y 18 Nov 2004 21:43:12 -0000 1.1.1.1
+++ parse.y 18 Nov 2004 21:48:39 -0000 1.2
@@ -1,4 +1,5 @@
/* $OpenBSD: parse.y,v 1.7 2004/04/28 01:00:50 deraadt Exp $ */
+/* $Id: parse.y,v 1.2 2004/11/18 21:48:39 mdg Exp $ */
/*
* Copyright (c) 2004 Ryan McBride <mcbride@openbsd.org>
@@ -24,6 +25,7 @@
#include <sys/types.h>
#include <sys/time.h>
#include <sys/socket.h>
+#include <sys/limits.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if.h>
@@ -35,7 +37,7 @@
#include <stdio.h>
#include <string.h>
#include <syslog.h>
-#include <event.h>
+#include <sys/event.h>
#include "ifstated.h"

15
net/ifstated/pkg-descr Normal file
View File

@ -0,0 +1,15 @@
This is a port of ifstated(8) from OpenBSD. From the manpage:
***
The ifstated daemon runs commands in response to network state changes,
which it determines by monitoring interface link state or running exter-
nal tests. For example, it can be used with carp(4) to change running
services or to ensure that carp(4) interfaces stay in sync, or with pf(4)
to test server or link availability and modify translation or routing
rules.
***
It has been modified to use FreeBSD's kqueue/kevent mechanism and sysctl.
Matthew George <mdg@secureworks.net>