2016-01-27 21:55:51 +03:00
|
|
|
/* $NetBSD: ifwatchd.c,v 1.27 2016/01/27 18:55:51 riastradh Exp $ */
|
2001-11-19 13:20:34 +03:00
|
|
|
|
2002-04-14 15:41:42 +04:00
|
|
|
/*-
|
2003-06-24 01:50:12 +04:00
|
|
|
* Copyright (c) 2002, 2003 The NetBSD Foundation, Inc.
|
2001-11-19 12:43:03 +03:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
2002-04-14 15:41:42 +04:00
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
2003-11-12 16:31:07 +03:00
|
|
|
* by Martin Husemann <martin@NetBSD.org>.
|
2002-04-14 15:41:42 +04:00
|
|
|
*
|
2001-11-19 12:43:03 +03:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
2002-04-14 15:41:42 +04:00
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2001-11-19 12:43:03 +03:00
|
|
|
*
|
2002-04-14 15:41:42 +04:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
2001-11-19 12:43:03 +03:00
|
|
|
*/
|
|
|
|
|
2002-01-10 23:21:50 +03:00
|
|
|
/*
|
|
|
|
* Define this for special treatment of sys/net/if_spppsubr.c based interfaces.
|
|
|
|
*/
|
|
|
|
#define SPPP_IF_SUPPORT
|
2007-12-22 03:57:15 +03:00
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
2001-12-10 17:54:09 +03:00
|
|
|
#include <sys/ioctl.h>
|
2001-11-19 12:43:03 +03:00
|
|
|
#include <sys/socket.h>
|
2001-11-19 13:20:34 +03:00
|
|
|
#include <sys/queue.h>
|
2002-04-16 01:08:41 +04:00
|
|
|
#include <sys/wait.h>
|
2001-11-19 12:43:03 +03:00
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_dl.h>
|
2004-11-25 09:57:38 +03:00
|
|
|
#include <net/if_media.h>
|
2002-01-10 23:21:50 +03:00
|
|
|
#ifdef SPPP_IF_SUPPORT
|
|
|
|
#include <net/if_sppp.h>
|
|
|
|
#endif
|
2001-11-19 12:43:03 +03:00
|
|
|
#include <net/route.h>
|
2001-11-19 13:20:34 +03:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2002-04-15 21:32:18 +04:00
|
|
|
#include <paths.h>
|
2001-11-19 12:43:03 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <netdb.h>
|
2001-12-10 17:54:09 +03:00
|
|
|
#include <err.h>
|
|
|
|
#include <ifaddrs.h>
|
2003-06-24 01:50:12 +04:00
|
|
|
#include <syslog.h>
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2004-01-05 01:19:51 +03:00
|
|
|
enum event { ARRIVAL, DEPARTURE, UP, DOWN, CARRIER, NO_CARRIER };
|
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
/* local functions */
|
2011-08-30 22:57:38 +04:00
|
|
|
__dead static void usage(void);
|
2001-11-19 12:43:03 +03:00
|
|
|
static void dispatch(void*, size_t);
|
2003-03-05 12:03:49 +03:00
|
|
|
static void check_addrs(char *cp, int addrs, enum event ev);
|
|
|
|
static void invoke_script(struct sockaddr *sa, struct sockaddr *dst, enum event ev, int ifindex, const char *ifname_hint);
|
2001-11-19 12:43:03 +03:00
|
|
|
static void list_interfaces(const char *ifnames);
|
2003-03-05 12:03:49 +03:00
|
|
|
static void check_announce(struct if_announcemsghdr *ifan);
|
2004-01-05 01:19:51 +03:00
|
|
|
static void check_carrier(int if_index, int carrier);
|
2001-11-19 12:43:03 +03:00
|
|
|
static void rescan_interfaces(void);
|
|
|
|
static void free_interfaces(void);
|
|
|
|
static int find_interface(int index);
|
2001-12-10 17:54:09 +03:00
|
|
|
static void run_initial_ups(void);
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2002-01-10 23:21:50 +03:00
|
|
|
#ifdef SPPP_IF_SUPPORT
|
2003-12-27 03:05:46 +03:00
|
|
|
static int check_is_connected(const char * ifname, int def_retvalue);
|
|
|
|
#define if_is_connected(X) (check_is_connected((X), 1))
|
|
|
|
#define if_is_not_connected(X) (!check_is_connected((X), 0))
|
2002-01-10 23:21:50 +03:00
|
|
|
#else
|
|
|
|
#define if_is_connected(X) 1
|
2003-12-27 03:05:46 +03:00
|
|
|
#define if_is_not_connected(X) 1
|
2002-01-10 23:21:50 +03:00
|
|
|
#endif
|
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
/* global variables */
|
2003-06-24 01:50:12 +04:00
|
|
|
static int verbose = 0, quiet = 0;
|
2001-12-10 17:54:09 +03:00
|
|
|
static int inhibit_initial = 0;
|
2003-03-05 12:03:49 +03:00
|
|
|
static const char *arrival_script = NULL;
|
|
|
|
static const char *departure_script = NULL;
|
2001-11-19 12:43:03 +03:00
|
|
|
static const char *up_script = NULL;
|
|
|
|
static const char *down_script = NULL;
|
2004-01-05 01:19:51 +03:00
|
|
|
static const char *carrier_script = NULL;
|
|
|
|
static const char *no_carrier_script = NULL;
|
|
|
|
static const char DummyTTY[] = _PATH_DEVNULL;
|
|
|
|
static const char DummySpeed[] = "9600";
|
2003-03-05 12:03:49 +03:00
|
|
|
static const char **scripts[] = {
|
|
|
|
&arrival_script,
|
|
|
|
&departure_script,
|
|
|
|
&up_script,
|
2004-01-05 01:19:51 +03:00
|
|
|
&down_script,
|
|
|
|
&carrier_script,
|
|
|
|
&no_carrier_script
|
2003-05-17 23:14:25 +04:00
|
|
|
};
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
struct interface_data {
|
|
|
|
SLIST_ENTRY(interface_data) next;
|
|
|
|
int index;
|
2004-01-05 01:19:51 +03:00
|
|
|
int last_carrier_status;
|
2001-11-19 12:43:03 +03:00
|
|
|
char * ifname;
|
|
|
|
};
|
2011-08-30 22:57:38 +04:00
|
|
|
static SLIST_HEAD(,interface_data) ifs = SLIST_HEAD_INITIALIZER(ifs);
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c, s, n;
|
|
|
|
int errs = 0;
|
|
|
|
char msg[2048], *msgp;
|
|
|
|
|
2003-06-24 01:50:12 +04:00
|
|
|
openlog(argv[0], LOG_PID|LOG_CONS, LOG_DAEMON);
|
2007-12-22 03:57:15 +03:00
|
|
|
while ((c = getopt(argc, argv, "qvhic:n:u:d:A:D:")) != -1) {
|
2001-11-19 12:43:03 +03:00
|
|
|
switch (c) {
|
|
|
|
case 'h':
|
|
|
|
usage();
|
|
|
|
return 0;
|
2004-01-05 01:19:51 +03:00
|
|
|
|
2001-12-10 17:54:09 +03:00
|
|
|
case 'i':
|
|
|
|
inhibit_initial = 1;
|
|
|
|
break;
|
2004-01-05 01:19:51 +03:00
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
2004-01-05 01:19:51 +03:00
|
|
|
|
2003-06-24 01:50:12 +04:00
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2004-01-05 01:19:51 +03:00
|
|
|
case 'c':
|
|
|
|
carrier_script = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
no_carrier_script = optarg;
|
|
|
|
break;
|
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
case 'u':
|
|
|
|
up_script = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
down_script = optarg;
|
|
|
|
break;
|
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
case 'A':
|
|
|
|
arrival_script = optarg;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'D':
|
|
|
|
departure_script = optarg;
|
|
|
|
break;
|
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
default:
|
|
|
|
errs++;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-22 03:57:15 +03:00
|
|
|
}
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
if (errs)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
argv += optind;
|
|
|
|
argc -= optind;
|
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
if (argc <= 0)
|
2001-11-19 12:43:03 +03:00
|
|
|
usage();
|
|
|
|
|
|
|
|
if (verbose) {
|
|
|
|
printf("up_script: %s\ndown_script: %s\n",
|
|
|
|
up_script, down_script);
|
2003-03-05 12:03:49 +03:00
|
|
|
printf("arrival_script: %s\ndeparture_script: %s\n",
|
|
|
|
arrival_script, departure_script);
|
2004-01-05 01:19:51 +03:00
|
|
|
printf("carrier_script: %s\nno_carrier_script: %s\n",
|
|
|
|
carrier_script, no_carrier_script);
|
2001-11-19 12:43:03 +03:00
|
|
|
printf("verbosity = %d\n", verbose);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (argc > 0) {
|
2003-05-17 23:14:25 +04:00
|
|
|
list_interfaces(argv[0]);
|
|
|
|
argv++;
|
|
|
|
argc--;
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!verbose)
|
2007-12-22 03:57:15 +03:00
|
|
|
daemon(0, 0);
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
s = socket(PF_ROUTE, SOCK_RAW, 0);
|
|
|
|
if (s < 0) {
|
2003-06-24 01:50:12 +04:00
|
|
|
syslog(LOG_ERR, "error opening routing socket: %m");
|
2001-11-19 12:43:03 +03:00
|
|
|
perror("open routing socket");
|
2002-04-16 01:08:41 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
2003-03-06 16:33:29 +03:00
|
|
|
if (!inhibit_initial)
|
|
|
|
run_initial_ups();
|
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
for (;;) {
|
|
|
|
n = read(s, msg, sizeof msg);
|
|
|
|
msgp = msg;
|
2007-12-22 03:57:15 +03:00
|
|
|
for (msgp = msg; n > 0;
|
|
|
|
n -= ((struct rt_msghdr*)msgp)->rtm_msglen,
|
|
|
|
msgp += ((struct rt_msghdr*)msgp)->rtm_msglen)
|
2001-11-19 12:43:03 +03:00
|
|
|
dispatch(msgp, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(s);
|
|
|
|
free_interfaces();
|
2003-06-24 01:50:12 +04:00
|
|
|
closelog();
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2002-04-16 01:08:41 +04:00
|
|
|
return EXIT_SUCCESS;
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-12-22 03:57:15 +03:00
|
|
|
usage(void)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
2007-12-22 03:57:15 +03:00
|
|
|
fprintf(stderr,
|
2001-11-19 12:43:03 +03:00
|
|
|
"usage:\n"
|
2003-07-04 16:45:05 +04:00
|
|
|
"\tifwatchd [-hiqv] [-A arrival-script] [-D departure-script]\n"
|
2004-01-05 01:19:51 +03:00
|
|
|
"\t\t [-d down-script] [-u up-script]\n"
|
|
|
|
"\t\t [-c carrier-script] [-n no-carrier-script] ifname(s)\n"
|
2001-11-19 12:43:03 +03:00
|
|
|
"\twhere:\n"
|
2003-04-16 13:09:52 +04:00
|
|
|
"\t -A <cmd> specify command to run on interface arrival event\n"
|
2004-01-05 01:19:51 +03:00
|
|
|
"\t -c <cmd> specify command to run on interface carrier-detect event\n"
|
2003-04-16 13:09:52 +04:00
|
|
|
"\t -D <cmd> specify command to run on interface departure event\n"
|
|
|
|
"\t -d <cmd> specify command to run on interface down event\n"
|
2004-01-05 01:19:51 +03:00
|
|
|
"\t -n <cmd> specify command to run on interface no-carrier-detect event\n"
|
2001-11-19 12:43:03 +03:00
|
|
|
"\t -h show this help message\n"
|
2001-12-10 17:54:09 +03:00
|
|
|
"\t -i no (!) initial run of the up script if the interface\n"
|
|
|
|
"\t is already up on ifwatchd startup\n"
|
2003-07-04 18:11:36 +04:00
|
|
|
"\t -q quiet mode, don't syslog informational messages\n"
|
2001-11-19 12:43:03 +03:00
|
|
|
"\t -u <cmd> specify command to run on interface up event\n"
|
2003-07-04 18:11:36 +04:00
|
|
|
"\t -v verbose/debug output, don't run in background\n");
|
2002-04-16 01:08:41 +04:00
|
|
|
exit(EXIT_FAILURE);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dispatch(void *msg, size_t len)
|
|
|
|
{
|
|
|
|
struct rt_msghdr *hd = msg;
|
2004-01-05 01:19:51 +03:00
|
|
|
struct if_msghdr *ifmp;
|
2001-11-19 12:43:03 +03:00
|
|
|
struct ifa_msghdr *ifam;
|
2003-03-05 12:03:49 +03:00
|
|
|
enum event ev;
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
switch (hd->rtm_type) {
|
|
|
|
case RTM_NEWADDR:
|
2003-03-05 12:03:49 +03:00
|
|
|
ev = UP;
|
2001-11-19 12:43:03 +03:00
|
|
|
goto work;
|
|
|
|
case RTM_DELADDR:
|
2003-03-05 12:03:49 +03:00
|
|
|
ev = DOWN;
|
2001-11-19 12:43:03 +03:00
|
|
|
goto work;
|
|
|
|
case RTM_IFANNOUNCE:
|
|
|
|
rescan_interfaces();
|
2003-03-05 12:03:49 +03:00
|
|
|
check_announce((struct if_announcemsghdr *)msg);
|
|
|
|
return;
|
2004-01-05 01:19:51 +03:00
|
|
|
case RTM_IFINFO:
|
|
|
|
ifmp = (struct if_msghdr*)msg;
|
|
|
|
check_carrier(ifmp->ifm_index, ifmp->ifm_data.ifi_link_state);
|
|
|
|
return;
|
2008-05-24 21:45:14 +04:00
|
|
|
case RTM_ADD:
|
|
|
|
case RTM_DELETE:
|
|
|
|
case RTM_CHANGE:
|
|
|
|
case RTM_LOSING:
|
|
|
|
case RTM_REDIRECT:
|
|
|
|
case RTM_MISS:
|
|
|
|
case RTM_IEEE80211:
|
|
|
|
return;
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
if (verbose)
|
2008-05-24 21:45:14 +04:00
|
|
|
printf("unknown message ignored (%d)\n", hd->rtm_type);
|
2001-11-19 12:43:03 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
work:
|
|
|
|
ifam = (struct ifa_msghdr *)msg;
|
2003-03-05 12:03:49 +03:00
|
|
|
check_addrs((char *)(ifam + 1), ifam->ifam_addrs, ev);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-12-22 03:57:15 +03:00
|
|
|
check_addrs(char *cp, int addrs, enum event ev)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
2002-01-10 22:38:51 +03:00
|
|
|
struct sockaddr *sa, *ifa = NULL, *brd = NULL;
|
2003-12-27 03:05:46 +03:00
|
|
|
char ifname_buf[IFNAMSIZ];
|
|
|
|
const char *ifname;
|
2016-01-27 21:55:51 +03:00
|
|
|
int ifndx = 0;
|
|
|
|
unsigned i;
|
2001-11-19 12:43:03 +03:00
|
|
|
|
|
|
|
if (addrs == 0)
|
2003-05-17 23:14:25 +04:00
|
|
|
return;
|
2001-11-19 12:43:03 +03:00
|
|
|
for (i = 1; i; i <<= 1) {
|
2007-12-22 03:57:15 +03:00
|
|
|
if ((i & addrs) == 0)
|
|
|
|
continue;
|
|
|
|
sa = (struct sockaddr *)cp;
|
|
|
|
if (i == RTA_IFP) {
|
|
|
|
struct sockaddr_dl * li = (struct sockaddr_dl*)sa;
|
|
|
|
ifndx = li->sdl_index;
|
|
|
|
if (!find_interface(ifndx)) {
|
|
|
|
if (verbose)
|
|
|
|
printf("ignoring change on interface #%d\n", ifndx);
|
|
|
|
return;
|
2003-05-17 23:14:25 +04:00
|
|
|
}
|
2007-12-22 03:57:15 +03:00
|
|
|
} else if (i == RTA_IFA)
|
|
|
|
ifa = sa;
|
|
|
|
else if (i == RTA_BRD)
|
|
|
|
brd = sa;
|
2011-02-04 17:31:23 +03:00
|
|
|
RT_ADVANCE(cp, sa);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
2003-12-27 03:05:46 +03:00
|
|
|
if (ifa != NULL) {
|
|
|
|
ifname = if_indextoname(ifndx, ifname_buf);
|
|
|
|
if (ifname == NULL || ev < UP)
|
|
|
|
invoke_script(ifa, brd, ev, ifndx, ifname);
|
|
|
|
else if (ev == UP) {
|
|
|
|
if (if_is_connected(ifname))
|
|
|
|
invoke_script(ifa, brd, ev, ifndx, ifname);
|
|
|
|
} else if (ev == DOWN) {
|
|
|
|
if (if_is_not_connected(ifname))
|
|
|
|
invoke_script(ifa, brd, ev, ifndx, ifname);
|
|
|
|
}
|
|
|
|
}
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-12-22 03:57:15 +03:00
|
|
|
invoke_script(struct sockaddr *sa, struct sockaddr *dest, enum event ev,
|
|
|
|
int ifindex, const char *ifname_hint)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
2003-03-05 12:03:49 +03:00
|
|
|
char addr[NI_MAXHOST], daddr[NI_MAXHOST], ifname_buf[IFNAMSIZ];
|
2009-04-15 12:33:04 +04:00
|
|
|
const char * volatile ifname;
|
2001-11-19 12:43:03 +03:00
|
|
|
const char *script;
|
2002-04-16 01:08:41 +04:00
|
|
|
int status;
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
if (sa != NULL && sa->sa_len == 0) {
|
2003-05-17 23:14:25 +04:00
|
|
|
fprintf(stderr, "illegal socket address (sa_len == 0)\n");
|
|
|
|
return;
|
2003-03-05 12:03:49 +03:00
|
|
|
}
|
|
|
|
if (sa != NULL && sa->sa_family == AF_INET6) {
|
2002-04-16 00:42:37 +04:00
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
|
|
|
|
(void) memcpy(&sin6, (struct sockaddr_in6 *)sa, sizeof (sin6));
|
|
|
|
if (IN6_IS_ADDR_LINKLOCAL(&sin6.sin6_addr))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
addr[0] = daddr[0] = 0;
|
2001-11-19 12:43:03 +03:00
|
|
|
ifname = if_indextoname(ifindex, ifname_buf);
|
2003-03-05 12:03:49 +03:00
|
|
|
ifname = ifname ? ifname : ifname_hint;
|
|
|
|
if (ifname == NULL)
|
2003-05-17 23:14:25 +04:00
|
|
|
return;
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
if (sa != NULL) {
|
2003-05-17 23:14:25 +04:00
|
|
|
if (getnameinfo(sa, sa->sa_len, addr, sizeof addr, NULL, 0,
|
|
|
|
NI_NUMERICHOST)) {
|
|
|
|
if (verbose)
|
|
|
|
printf("getnameinfo failed\n");
|
|
|
|
return; /* this address can not be handled */
|
|
|
|
}
|
2003-03-05 12:03:49 +03:00
|
|
|
}
|
2002-01-10 22:38:51 +03:00
|
|
|
if (dest != NULL) {
|
2003-05-17 23:14:25 +04:00
|
|
|
if (getnameinfo(dest, dest->sa_len, daddr, sizeof daddr,
|
|
|
|
NULL, 0, NI_NUMERICHOST)) {
|
|
|
|
if (verbose)
|
|
|
|
printf("getnameinfo failed\n");
|
|
|
|
return; /* this address can not be handled */
|
|
|
|
}
|
2002-01-10 22:38:51 +03:00
|
|
|
}
|
2001-11-19 12:43:03 +03:00
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
script = *scripts[ev];
|
2001-11-19 12:43:03 +03:00
|
|
|
if (script == NULL) return;
|
|
|
|
|
|
|
|
if (verbose)
|
2003-05-17 23:14:25 +04:00
|
|
|
(void) printf("calling: %s %s %s %s %s %s\n",
|
|
|
|
script, ifname, DummyTTY, DummySpeed, addr, daddr);
|
2003-06-24 01:50:12 +04:00
|
|
|
if (!quiet)
|
|
|
|
syslog(LOG_INFO, "calling: %s %s %s %s %s %s\n",
|
|
|
|
script, ifname, DummyTTY, DummySpeed, addr, daddr);
|
2002-04-16 01:08:41 +04:00
|
|
|
|
|
|
|
switch (vfork()) {
|
|
|
|
case -1:
|
2003-05-17 23:14:25 +04:00
|
|
|
fprintf(stderr, "cannot fork\n");
|
|
|
|
break;
|
2002-04-16 01:08:41 +04:00
|
|
|
case 0:
|
2003-06-24 01:50:12 +04:00
|
|
|
if (execl(script, script, ifname, DummyTTY, DummySpeed,
|
|
|
|
addr, daddr, NULL) == -1) {
|
|
|
|
syslog(LOG_ERR, "could not execute \"%s\": %m",
|
|
|
|
script);
|
|
|
|
perror(script);
|
|
|
|
}
|
2003-05-17 23:14:25 +04:00
|
|
|
_exit(EXIT_FAILURE);
|
2002-04-16 01:08:41 +04:00
|
|
|
default:
|
2003-05-17 23:14:25 +04:00
|
|
|
(void) wait(&status);
|
2002-04-16 01:08:41 +04:00
|
|
|
}
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
|
2004-01-05 01:19:51 +03:00
|
|
|
static void
|
|
|
|
list_interfaces(const char *ifnames)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
|
|
|
char * names = strdup(ifnames);
|
|
|
|
char * name, *lasts;
|
|
|
|
static const char sep[] = " \t";
|
|
|
|
struct interface_data * p;
|
|
|
|
|
2003-05-17 23:14:25 +04:00
|
|
|
for (name = strtok_r(names, sep, &lasts);
|
|
|
|
name != NULL;
|
|
|
|
name = strtok_r(NULL, sep, &lasts)) {
|
|
|
|
p = malloc(sizeof(*p));
|
|
|
|
SLIST_INSERT_HEAD(&ifs, p, next);
|
2004-01-05 01:19:51 +03:00
|
|
|
p->last_carrier_status = -1;
|
2003-05-17 23:14:25 +04:00
|
|
|
p->ifname = strdup(name);
|
|
|
|
p->index = if_nametoindex(p->ifname);
|
2003-06-24 01:50:12 +04:00
|
|
|
if (!quiet)
|
|
|
|
syslog(LOG_INFO, "watching interface %s", p->ifname);
|
2003-05-17 23:14:25 +04:00
|
|
|
if (verbose)
|
|
|
|
printf("interface \"%s\" has index %d\n",
|
|
|
|
p->ifname, p->index);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
free(names);
|
|
|
|
}
|
|
|
|
|
2004-01-05 01:19:51 +03:00
|
|
|
static void
|
|
|
|
check_carrier(int if_index, int carrier_status)
|
|
|
|
{
|
|
|
|
struct interface_data * p;
|
|
|
|
enum event ev;
|
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ifs, next)
|
|
|
|
if (p->index == if_index)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (p == NULL)
|
|
|
|
return;
|
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
/*
|
2004-01-05 01:19:51 +03:00
|
|
|
* Treat it as an event worth handling if:
|
|
|
|
* - the carrier status changed, or
|
|
|
|
* - this is the first time we've been called, and
|
|
|
|
* inhibit_initial is not set
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ((carrier_status != p->last_carrier_status) ||
|
|
|
|
((p->last_carrier_status == -1) && !inhibit_initial)) {
|
|
|
|
switch (carrier_status) {
|
|
|
|
case LINK_STATE_UP:
|
|
|
|
ev = CARRIER;
|
|
|
|
break;
|
|
|
|
case LINK_STATE_DOWN:
|
|
|
|
ev = NO_CARRIER;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (verbose)
|
|
|
|
printf("unknown link status ignored\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
invoke_script(NULL, NULL, ev, if_index, p->ifname);
|
|
|
|
p->last_carrier_status = carrier_status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-05 12:03:49 +03:00
|
|
|
static void
|
|
|
|
check_announce(struct if_announcemsghdr *ifan)
|
|
|
|
{
|
|
|
|
struct interface_data * p;
|
|
|
|
const char *ifname = ifan->ifan_name;
|
|
|
|
|
|
|
|
SLIST_FOREACH(p, &ifs, next) {
|
2007-12-22 03:57:15 +03:00
|
|
|
if (strcmp(p->ifname, ifname) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch (ifan->ifan_what) {
|
|
|
|
case IFAN_ARRIVAL:
|
|
|
|
invoke_script(NULL, NULL, ARRIVAL, p->index,
|
|
|
|
NULL);
|
|
|
|
break;
|
|
|
|
case IFAN_DEPARTURE:
|
|
|
|
invoke_script(NULL, NULL, DEPARTURE, p->index,
|
|
|
|
p->ifname);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (verbose)
|
|
|
|
(void) printf("unknown announce: "
|
|
|
|
"what=%d\n", ifan->ifan_what);
|
|
|
|
break;
|
2003-03-05 12:03:49 +03:00
|
|
|
}
|
2007-12-22 03:57:15 +03:00
|
|
|
return;
|
2003-03-05 12:03:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
static void
|
|
|
|
rescan_interfaces(void)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
|
|
|
struct interface_data * p;
|
2007-12-22 03:57:15 +03:00
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
SLIST_FOREACH(p, &ifs, next) {
|
2003-05-17 23:14:25 +04:00
|
|
|
p->index = if_nametoindex(p->ifname);
|
|
|
|
if (verbose)
|
|
|
|
printf("interface \"%s\" has index %d\n", p->ifname,
|
|
|
|
p->index);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
static void
|
|
|
|
free_interfaces(void)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
|
|
|
struct interface_data * p;
|
|
|
|
|
|
|
|
while (!SLIST_EMPTY(&ifs)) {
|
2003-05-17 23:14:25 +04:00
|
|
|
p = SLIST_FIRST(&ifs);
|
|
|
|
SLIST_REMOVE_HEAD(&ifs, next);
|
|
|
|
free(p->ifname);
|
|
|
|
free(p);
|
2001-11-19 12:43:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
static int
|
2009-04-15 12:33:04 +04:00
|
|
|
find_interface(int idx)
|
2001-11-19 12:43:03 +03:00
|
|
|
{
|
|
|
|
struct interface_data * p;
|
2007-12-22 03:57:15 +03:00
|
|
|
|
2001-11-19 12:43:03 +03:00
|
|
|
SLIST_FOREACH(p, &ifs, next)
|
2009-04-15 12:33:04 +04:00
|
|
|
if (p->index == idx)
|
2003-05-17 23:14:25 +04:00
|
|
|
return 1;
|
2001-11-19 12:43:03 +03:00
|
|
|
return 0;
|
|
|
|
}
|
2001-12-10 17:54:09 +03:00
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
static void
|
|
|
|
run_initial_ups(void)
|
2001-12-10 17:54:09 +03:00
|
|
|
{
|
|
|
|
struct interface_data * ifd;
|
|
|
|
struct ifaddrs *res = NULL, *p;
|
2004-11-25 09:57:38 +03:00
|
|
|
int s;
|
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
|
|
|
return;
|
2001-12-10 17:54:09 +03:00
|
|
|
|
2007-12-22 03:57:15 +03:00
|
|
|
if (getifaddrs(&res) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
for (p = res; p; p = p->ifa_next) {
|
|
|
|
SLIST_FOREACH(ifd, &ifs, next) {
|
|
|
|
if (strcmp(ifd->ifname, p->ifa_name) == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ifd == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (p->ifa_addr && p->ifa_addr->sa_family == AF_LINK)
|
|
|
|
invoke_script(NULL, NULL, ARRIVAL, ifd->index,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
if ((p->ifa_flags & IFF_UP) == 0)
|
|
|
|
continue;
|
|
|
|
if (p->ifa_addr == NULL)
|
|
|
|
continue;
|
|
|
|
if (p->ifa_addr->sa_family == AF_LINK) {
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
|
|
|
|
memset(&ifmr, 0, sizeof(ifmr));
|
|
|
|
strncpy(ifmr.ifm_name, ifd->ifname,
|
|
|
|
sizeof(ifmr.ifm_name));
|
|
|
|
if (ioctl(s, SIOCGIFMEDIA, &ifmr) != -1
|
|
|
|
&& (ifmr.ifm_status & IFM_AVALID)
|
|
|
|
&& (ifmr.ifm_status & IFM_ACTIVE)) {
|
|
|
|
invoke_script(NULL, NULL, CARRIER,
|
2003-05-17 23:14:25 +04:00
|
|
|
ifd->index, ifd->ifname);
|
2007-12-22 03:57:15 +03:00
|
|
|
ifd->last_carrier_status =
|
|
|
|
LINK_STATE_UP;
|
|
|
|
}
|
|
|
|
continue;
|
2003-03-06 16:33:29 +03:00
|
|
|
}
|
2007-12-22 03:57:15 +03:00
|
|
|
if (if_is_connected(ifd->ifname))
|
|
|
|
invoke_script(p->ifa_addr, p->ifa_dstaddr, UP,
|
|
|
|
ifd->index, ifd->ifname);
|
2001-12-10 17:54:09 +03:00
|
|
|
}
|
2007-12-22 03:57:15 +03:00
|
|
|
freeifaddrs(res);
|
|
|
|
out:
|
2004-11-25 09:57:38 +03:00
|
|
|
close(s);
|
2001-12-10 17:54:09 +03:00
|
|
|
}
|
2002-01-10 23:21:50 +03:00
|
|
|
|
|
|
|
#ifdef SPPP_IF_SUPPORT
|
|
|
|
/*
|
|
|
|
* Special case support for in-kernel PPP interfaces.
|
|
|
|
* If these are IFF_UP, but have not yet connected or completed authentication
|
|
|
|
* we don't want to call the up script in the initial interface scan (there
|
|
|
|
* will be an UP event generated later, when IPCP completes, anyway).
|
|
|
|
*
|
|
|
|
* If this is no if_spppsubr.c based interface, this ioctl just fails and we
|
|
|
|
* treat is as connected.
|
|
|
|
*/
|
|
|
|
static int
|
2007-12-22 03:57:15 +03:00
|
|
|
check_is_connected(const char *ifname, int def_retval)
|
2002-01-10 23:21:50 +03:00
|
|
|
{
|
2009-04-15 12:33:04 +04:00
|
|
|
int s, error;
|
2003-12-27 03:05:46 +03:00
|
|
|
struct spppstatus oldstatus;
|
|
|
|
struct spppstatusncp status;
|
2002-01-10 23:21:50 +03:00
|
|
|
|
|
|
|
memset(&status, 0, sizeof status);
|
|
|
|
strncpy(status.ifname, ifname, sizeof status.ifname);
|
2003-12-27 03:05:46 +03:00
|
|
|
memset(&oldstatus, 0, sizeof oldstatus);
|
|
|
|
strncpy(oldstatus.ifname, ifname, sizeof oldstatus.ifname);
|
|
|
|
|
2002-01-10 23:21:50 +03:00
|
|
|
s = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (s < 0)
|
2003-05-17 23:14:25 +04:00
|
|
|
return 1; /* no idea how to handle this... */
|
2009-04-15 12:33:04 +04:00
|
|
|
error = ioctl(s, SPPPGETSTATUSNCP, &status);
|
|
|
|
if (error != 0) {
|
|
|
|
error = ioctl(s, SPPPGETSTATUS, &oldstatus);
|
|
|
|
if (error != 0) {
|
2003-12-27 03:05:46 +03:00
|
|
|
/* not if_spppsubr.c based - return default */
|
|
|
|
close(s);
|
|
|
|
return def_retval;
|
|
|
|
} else {
|
|
|
|
/* can't query NCPs, so use default */
|
|
|
|
status.phase = oldstatus.phase;
|
|
|
|
status.ncpup = def_retval;
|
|
|
|
}
|
|
|
|
}
|
2002-01-10 23:21:50 +03:00
|
|
|
close(s);
|
2003-12-27 03:05:46 +03:00
|
|
|
|
|
|
|
return status.phase == SPPP_PHASE_NETWORK && status.ncpup > 0;
|
2002-01-10 23:21:50 +03:00
|
|
|
}
|
|
|
|
#endif
|