blob: 1e6c562e0afd632297b8a537d2516d01c04c1749 [file] [log] [blame]
Denis Vlasenko71c16572009-04-26 01:08:51 +00001/* vi: set sw=4 ts=4: */
2/*
Maxim Kryzhanovsky2004fa12010-03-30 15:49:57 +02003 * ifplugd for busybox, based on ifplugd 0.28 (written by Lennart Poettering).
Denis Vlasenko71c16572009-04-26 01:08:51 +00004 *
Denis Vlasenkof4e45632009-04-26 01:17:44 +00005 * Copyright (C) 2009 Maksym Kryzhanovskyy <xmaks@email.cz>
Denis Vlasenko71c16572009-04-26 01:08:51 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenko71c16572009-04-26 01:08:51 +00008 */
Pere Orga5bc8c002011-04-11 03:29:49 +02009
10//usage:#define ifplugd_trivial_usage
11//usage: "[OPTIONS]"
12//usage:#define ifplugd_full_usage "\n\n"
13//usage: "Network interface plug detection daemon\n"
Pere Orga5bc8c002011-04-11 03:29:49 +020014//usage: "\n -n Don't daemonize"
15//usage: "\n -s Don't log to syslog"
16//usage: "\n -i IFACE Interface"
17//usage: "\n -f/-F Treat link detection error as link down/link up"
18//usage: "\n (otherwise exit on error)"
19//usage: "\n -a Don't up interface at each link probe"
20//usage: "\n -M Monitor creation/destruction of interface"
21//usage: "\n (otherwise it must exist)"
22//usage: "\n -r PROG Script to run"
23//usage: "\n -x ARG Extra argument for script"
24//usage: "\n -I Don't exit on nonzero exit code from script"
Denys Vlasenko21f620f2012-06-03 10:26:16 +020025//usage: "\n -p Don't run \"up\" script on startup"
26//usage: "\n -q Don't run \"down\" script on exit"
27//usage: "\n -l Always run script on startup"
Pere Orga5bc8c002011-04-11 03:29:49 +020028//usage: "\n -t SECS Poll time in seconds"
29//usage: "\n -u SECS Delay before running script after link up"
30//usage: "\n -d SECS Delay after link down"
31//usage: "\n -m MODE API mode (mii, priv, ethtool, wlan, iff, auto)"
32//usage: "\n -k Kill running daemon"
33
Denis Vlasenko71c16572009-04-26 01:08:51 +000034#include "libbb.h"
35
Denys Vlasenko0568b6e2009-08-08 03:20:12 +020036#include "fix_u32.h"
Denis Vlasenko71c16572009-04-26 01:08:51 +000037#include <linux/if.h>
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +020038#include <linux/mii.h>
Denis Vlasenko71c16572009-04-26 01:08:51 +000039#include <linux/ethtool.h>
Dan Fandrichf533ec82011-06-10 05:17:59 +020040#ifdef HAVE_NET_ETHERNET_H
Denys Vlasenko5fa6d1a2015-10-05 11:15:43 +020041/* musl breakage:
42 * In file included from /usr/include/net/ethernet.h:10,
43 * from networking/ifplugd.c:41:
44 * /usr/include/netinet/if_ether.h:96: error: redefinition of 'struct ethhdr'
45 *
46 * Build succeeds without it on musl. Commented it out.
47 * If on your system you need it, consider removing <linux/ethtool.h>
48 * and copy-pasting its definitions here (<linux/ethtool.h> is what pulls in
49 * conflicting definition of struct ethhdr on musl).
50 */
51/* # include <net/ethernet.h> */
Dan Fandrichf533ec82011-06-10 05:17:59 +020052#endif
Denis Vlasenko71c16572009-04-26 01:08:51 +000053#include <linux/netlink.h>
54#include <linux/rtnetlink.h>
55#include <linux/sockios.h>
56#include <syslog.h>
57
58#define __user
59#include <linux/wireless.h>
60
Denis Vlasenkof4e45632009-04-26 01:17:44 +000061/*
Maxim Kryzhanovsky2004fa12010-03-30 15:49:57 +020062From initial port to busybox, removed most of the redundancy by
63converting implementation of a polymorphic interface to the strict
64functional style. The main role is run a script when link state
65changed, other activities like audio signal or detailed reports
66are on the script itself.
Denis Vlasenkof4e45632009-04-26 01:17:44 +000067
68One questionable point of the design is netlink usage:
69
70We have 1 second timeout by default to poll the link status,
71it is short enough so that there are no real benefits in
72using netlink to get "instantaneous" interface creation/deletion
73notifications. We can check for interface existence by just
74doing some fast ioctl using its name.
75
76Netlink code then can be just dropped (1k or more?)
77*/
78
79
Denis Vlasenko71c16572009-04-26 01:08:51 +000080#define IFPLUGD_ENV_PREVIOUS "IFPLUGD_PREVIOUS"
81#define IFPLUGD_ENV_CURRENT "IFPLUGD_CURRENT"
82
83enum {
84 FLAG_NO_AUTO = 1 << 0, // -a, Do not enable interface automatically
85 FLAG_NO_DAEMON = 1 << 1, // -n, Do not daemonize
86 FLAG_NO_SYSLOG = 1 << 2, // -s, Do not use syslog, use stderr instead
87 FLAG_IGNORE_FAIL = 1 << 3, // -f, Ignore detection failure, retry instead (failure is treated as DOWN)
88 FLAG_IGNORE_FAIL_POSITIVE = 1 << 4, // -F, Ignore detection failure, retry instead (failure is treated as UP)
89 FLAG_IFACE = 1 << 5, // -i, Specify ethernet interface
90 FLAG_RUN = 1 << 6, // -r, Specify program to execute
91 FLAG_IGNORE_RETVAL = 1 << 7, // -I, Don't exit on nonzero return value of program executed
92 FLAG_POLL_TIME = 1 << 8, // -t, Specify poll time in seconds
93 FLAG_DELAY_UP = 1 << 9, // -u, Specify delay for configuring interface
94 FLAG_DELAY_DOWN = 1 << 10, // -d, Specify delay for deconfiguring interface
95 FLAG_API_MODE = 1 << 11, // -m, Force API mode (mii, priv, ethtool, wlan, auto)
96 FLAG_NO_STARTUP = 1 << 12, // -p, Don't run script on daemon startup
97 FLAG_NO_SHUTDOWN = 1 << 13, // -q, Don't run script on daemon quit
98 FLAG_INITIAL_DOWN = 1 << 14, // -l, Run "down" script on startup if no cable is detected
99 FLAG_EXTRA_ARG = 1 << 15, // -x, Specify an extra argument for action script
100 FLAG_MONITOR = 1 << 16, // -M, Use interface monitoring
101#if ENABLE_FEATURE_PIDFILE
102 FLAG_KILL = 1 << 17, // -k, Kill a running daemon
103#endif
104};
105#if ENABLE_FEATURE_PIDFILE
106# define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:Mk"
107#else
108# define OPTION_STR "+ansfFi:r:It:u:d:m:pqlx:M"
109#endif
110
Denis Vlasenko71c16572009-04-26 01:08:51 +0000111enum { // interface status
112 IFSTATUS_ERR = -1,
113 IFSTATUS_DOWN = 0,
114 IFSTATUS_UP = 1,
115};
116
117enum { // constant fds
118 ioctl_fd = 3,
119 netlink_fd = 4,
120};
121
122struct globals {
123 smallint iface_last_status;
Denys Vlasenko19afe842010-05-08 23:26:16 +0200124 smallint iface_prev_status;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000125 smallint iface_exists;
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200126 smallint api_method_num;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000127
128 /* Used in getopt32, must have sizeof == sizeof(int) */
129 unsigned poll_time;
130 unsigned delay_up;
131 unsigned delay_down;
132
133 const char *iface;
134 const char *api_mode;
135 const char *script_name;
136 const char *extra_arg;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000137};
138#define G (*ptr_to_globals)
139#define INIT_G() do { \
140 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
141 G.iface_last_status = -1; \
142 G.iface_exists = 1; \
143 G.poll_time = 1; \
144 G.delay_down = 5; \
145 G.iface = "eth0"; \
146 G.api_mode = "a"; \
147 G.script_name = "/etc/ifplugd/ifplugd.action"; \
148} while (0)
149
150
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200151/* Utility routines */
152
153static void set_ifreq_to_ifname(struct ifreq *ifreq)
Denys Vlasenko19afe842010-05-08 23:26:16 +0200154{
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200155 memset(ifreq, 0, sizeof(struct ifreq));
156 strncpy_IFNAMSIZ(ifreq->ifr_name, G.iface);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000157}
158
Denys Vlasenkof422a722010-01-08 12:27:57 +0100159static int network_ioctl(int request, void* data, const char *errmsg)
Denis Vlasenko71c16572009-04-26 01:08:51 +0000160{
Denys Vlasenkof422a722010-01-08 12:27:57 +0100161 int r = ioctl(ioctl_fd, request, data);
162 if (r < 0 && errmsg)
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200163 bb_perror_msg("%s failed", errmsg);
Denys Vlasenkof422a722010-01-08 12:27:57 +0100164 return r;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000165}
166
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200167/* Link detection routines and table */
Denis Vlasenko71c16572009-04-26 01:08:51 +0000168
169static smallint detect_link_mii(void)
170{
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100171 /* char buffer instead of bona-fide struct avoids aliasing warning */
172 char buf[sizeof(struct ifreq)];
Denys Vlasenkob3b6c8b2011-01-20 11:29:00 +0100173 struct ifreq *const ifreq = (void *)buf;
174
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100175 struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000176
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100177 set_ifreq_to_ifname(ifreq);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000178
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100179 if (network_ioctl(SIOCGMIIPHY, ifreq, "SIOCGMIIPHY") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000180 return IFSTATUS_ERR;
181 }
182
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200183 mii->reg_num = 1;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000184
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100185 if (network_ioctl(SIOCGMIIREG, ifreq, "SIOCGMIIREG") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000186 return IFSTATUS_ERR;
187 }
188
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200189 return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000190}
191
192static smallint detect_link_priv(void)
193{
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100194 /* char buffer instead of bona-fide struct avoids aliasing warning */
195 char buf[sizeof(struct ifreq)];
Denys Vlasenkob3b6c8b2011-01-20 11:29:00 +0100196 struct ifreq *const ifreq = (void *)buf;
197
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100198 struct mii_ioctl_data *mii = (void *)&ifreq->ifr_data;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000199
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100200 set_ifreq_to_ifname(ifreq);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000201
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100202 if (network_ioctl(SIOCDEVPRIVATE, ifreq, "SIOCDEVPRIVATE") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000203 return IFSTATUS_ERR;
204 }
205
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200206 mii->reg_num = 1;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000207
Denys Vlasenko53f30b42011-01-20 01:20:36 +0100208 if (network_ioctl(SIOCDEVPRIVATE+1, ifreq, "SIOCDEVPRIVATE+1") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000209 return IFSTATUS_ERR;
210 }
211
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200212 return (mii->val_out & 0x0004) ? IFSTATUS_UP : IFSTATUS_DOWN;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000213}
214
215static smallint detect_link_ethtool(void)
216{
217 struct ifreq ifreq;
218 struct ethtool_value edata;
219
220 set_ifreq_to_ifname(&ifreq);
221
222 edata.cmd = ETHTOOL_GLINK;
Denys Vlasenko337a31b2009-10-23 18:31:02 +0200223 ifreq.ifr_data = (void*) &edata;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000224
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200225 if (network_ioctl(SIOCETHTOOL, &ifreq, "ETHTOOL_GLINK") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000226 return IFSTATUS_ERR;
227 }
228
229 return edata.data ? IFSTATUS_UP : IFSTATUS_DOWN;
230}
231
232static smallint detect_link_iff(void)
233{
234 struct ifreq ifreq;
235
236 set_ifreq_to_ifname(&ifreq);
237
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200238 if (network_ioctl(SIOCGIFFLAGS, &ifreq, "SIOCGIFFLAGS") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000239 return IFSTATUS_ERR;
240 }
241
Denys Vlasenkof422a722010-01-08 12:27:57 +0100242 /* If IFF_UP is not set (interface is down), IFF_RUNNING is never set
243 * regardless of link status. Simply continue to report last status -
244 * no point in reporting spurious link downs if interface is disabled
245 * by admin. When/if it will be brought up,
246 * we'll report real link status.
247 */
248 if (!(ifreq.ifr_flags & IFF_UP) && G.iface_last_status != IFSTATUS_ERR)
249 return G.iface_last_status;
250
Denis Vlasenko71c16572009-04-26 01:08:51 +0000251 return (ifreq.ifr_flags & IFF_RUNNING) ? IFSTATUS_UP : IFSTATUS_DOWN;
252}
253
254static smallint detect_link_wlan(void)
255{
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200256 int i;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000257 struct iwreq iwrequest;
258 uint8_t mac[ETH_ALEN];
259
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200260 memset(&iwrequest, 0, sizeof(iwrequest));
Denis Vlasenko80e57eb2009-04-26 01:43:36 +0000261 strncpy_IFNAMSIZ(iwrequest.ifr_ifrn.ifrn_name, G.iface);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000262
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200263 if (network_ioctl(SIOCGIWAP, &iwrequest, "SIOCGIWAP") < 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000264 return IFSTATUS_ERR;
265 }
266
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200267 memcpy(mac, &iwrequest.u.ap_addr.sa_data, ETH_ALEN);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000268
269 if (mac[0] == 0xFF || mac[0] == 0x44 || mac[0] == 0x00) {
Maxim Kryzhanovskyfcb84c82010-03-29 09:09:05 +0200270 for (i = 1; i < ETH_ALEN; ++i) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000271 if (mac[i] != mac[0])
272 return IFSTATUS_UP;
273 }
274 return IFSTATUS_DOWN;
275 }
276
277 return IFSTATUS_UP;
278}
279
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200280enum { // api mode
281 API_ETHTOOL, // 'e'
282 API_MII, // 'm'
283 API_PRIVATE, // 'p'
284 API_WLAN, // 'w'
285 API_IFF, // 'i'
286 API_AUTO, // 'a'
287};
288
289static const char api_modes[] ALIGN1 = "empwia";
290
291static const struct {
292 const char *name;
293 smallint (*func)(void);
294} method_table[] = {
295 { "SIOCETHTOOL" , &detect_link_ethtool },
296 { "SIOCGMIIPHY" , &detect_link_mii },
297 { "SIOCDEVPRIVATE" , &detect_link_priv },
298 { "wireless extension", &detect_link_wlan },
299 { "IFF_RUNNING" , &detect_link_iff },
300};
301
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200302static const char *strstatus(int status)
303{
304 if (status == IFSTATUS_ERR)
305 return "error";
306 return "down\0up" + (status * 5);
307}
308
309static int run_script(const char *action)
310{
311 char *env_PREVIOUS, *env_CURRENT;
312 char *argv[5];
313 int r;
314
315 bb_error_msg("executing '%s %s %s'", G.script_name, G.iface, action);
316
317 argv[0] = (char*) G.script_name;
318 argv[1] = (char*) G.iface;
319 argv[2] = (char*) action;
320 argv[3] = (char*) G.extra_arg;
321 argv[4] = NULL;
322
323 env_PREVIOUS = xasprintf("%s=%s", IFPLUGD_ENV_PREVIOUS, strstatus(G.iface_prev_status));
324 putenv(env_PREVIOUS);
325 env_CURRENT = xasprintf("%s=%s", IFPLUGD_ENV_CURRENT, strstatus(G.iface_last_status));
326 putenv(env_CURRENT);
327
328 /* r < 0 - can't exec, 0 <= r < 0x180 - exited, >=0x180 - killed by sig (r-0x180) */
329 r = spawn_and_wait(argv);
330
331 unsetenv(IFPLUGD_ENV_PREVIOUS);
332 unsetenv(IFPLUGD_ENV_CURRENT);
333 free(env_PREVIOUS);
334 free(env_CURRENT);
335
336 bb_error_msg("exit code: %d", r & 0xff);
337 return (option_mask32 & FLAG_IGNORE_RETVAL) ? 0 : r;
338}
339
340static void up_iface(void)
341{
342 struct ifreq ifrequest;
343
344 if (!G.iface_exists)
345 return;
346
347 set_ifreq_to_ifname(&ifrequest);
348 if (network_ioctl(SIOCGIFFLAGS, &ifrequest, "getting interface flags") < 0) {
349 G.iface_exists = 0;
350 return;
351 }
352
353 if (!(ifrequest.ifr_flags & IFF_UP)) {
354 ifrequest.ifr_flags |= IFF_UP;
355 /* Let user know we mess up with interface */
356 bb_error_msg("upping interface");
357 if (network_ioctl(SIOCSIFFLAGS, &ifrequest, "setting interface flags") < 0)
358 xfunc_die();
359 }
360
361#if 0 /* why do we mess with IP addr? It's not our business */
362 if (network_ioctl(SIOCGIFADDR, &ifrequest, "can't get interface address") < 0) {
363 } else if (ifrequest.ifr_addr.sa_family != AF_INET) {
364 bb_perror_msg("the interface is not IP-based");
365 } else {
366 ((struct sockaddr_in*)(&ifrequest.ifr_addr))->sin_addr.s_addr = INADDR_ANY;
367 network_ioctl(SIOCSIFADDR, &ifrequest, "can't set interface address");
368 }
369 network_ioctl(SIOCGIFFLAGS, &ifrequest, "can't get interface flags");
370#endif
371}
372
373static void maybe_up_new_iface(void)
374{
375 if (!(option_mask32 & FLAG_NO_AUTO))
376 up_iface();
377
378#if 0 /* bloat */
379 struct ifreq ifrequest;
380 struct ethtool_drvinfo driver_info;
381
382 set_ifreq_to_ifname(&ifrequest);
383 driver_info.cmd = ETHTOOL_GDRVINFO;
384 ifrequest.ifr_data = &driver_info;
385 if (network_ioctl(SIOCETHTOOL, &ifrequest, NULL) == 0) {
386 char buf[sizeof("/xx:xx:xx:xx:xx:xx")];
387
388 /* Get MAC */
389 buf[0] = '\0';
390 set_ifreq_to_ifname(&ifrequest);
391 if (network_ioctl(SIOCGIFHWADDR, &ifrequest, NULL) == 0) {
392 sprintf(buf, "/%02X:%02X:%02X:%02X:%02X:%02X",
393 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[0]),
394 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[1]),
395 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[2]),
396 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[3]),
397 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[4]),
398 (uint8_t)(ifrequest.ifr_hwaddr.sa_data[5]));
399 }
400
401 bb_error_msg("using interface %s%s with driver<%s> (version: %s)",
402 G.iface, buf, driver_info.driver, driver_info.version);
403 }
404#endif
405 if (G.api_mode[0] == 'a')
406 G.api_method_num = API_AUTO;
407}
408
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200409static smallint detect_link(void)
Denis Vlasenko71c16572009-04-26 01:08:51 +0000410{
Denis Vlasenko71c16572009-04-26 01:08:51 +0000411 smallint status;
412
413 if (!G.iface_exists)
414 return (option_mask32 & FLAG_MONITOR) ? IFSTATUS_DOWN : IFSTATUS_ERR;
415
Denys Vlasenkof422a722010-01-08 12:27:57 +0100416 /* Some drivers can't detect link status when the interface is down.
417 * I imagine detect_link_iff() is the most vulnerable.
418 * That's why -a "noauto" in an option, not a hardwired behavior.
419 */
Denis Vlasenko71c16572009-04-26 01:08:51 +0000420 if (!(option_mask32 & FLAG_NO_AUTO))
421 up_iface();
Denis Vlasenko71c16572009-04-26 01:08:51 +0000422
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200423 if (G.api_method_num == API_AUTO) {
424 int i;
425 smallint sv_logmode;
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200426
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200427 sv_logmode = logmode;
428 for (i = 0; i < ARRAY_SIZE(method_table); i++) {
429 logmode = LOGMODE_NONE;
430 status = method_table[i].func();
431 logmode = sv_logmode;
432 if (status != IFSTATUS_ERR) {
433 G.api_method_num = i;
434 bb_error_msg("using %s detection mode", method_table[i].name);
435 break;
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200436 }
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200437 }
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200438 } else {
439 status = method_table[G.api_method_num].func();
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200440 }
441
Denis Vlasenko71c16572009-04-26 01:08:51 +0000442 if (status == IFSTATUS_ERR) {
443 if (option_mask32 & FLAG_IGNORE_FAIL)
444 status = IFSTATUS_DOWN;
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200445 else if (option_mask32 & FLAG_IGNORE_FAIL_POSITIVE)
Denis Vlasenko71c16572009-04-26 01:08:51 +0000446 status = IFSTATUS_UP;
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200447 else if (G.api_mode[0] == 'a')
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200448 bb_error_msg("can't detect link status");
Denis Vlasenko71c16572009-04-26 01:08:51 +0000449 }
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200450
Denis Vlasenko71c16572009-04-26 01:08:51 +0000451 if (status != G.iface_last_status) {
Denys Vlasenko19afe842010-05-08 23:26:16 +0200452 G.iface_prev_status = G.iface_last_status;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000453 G.iface_last_status = status;
454 }
455
456 return status;
457}
458
459static NOINLINE int check_existence_through_netlink(void)
460{
Denys Vlasenko37201212010-04-02 07:04:44 +0200461 int iface_len;
Tito Ragusada331d72013-08-04 18:08:40 +0200462 /* Buffer was 1K, but on linux-3.9.9 it was reported to be too small.
463 * netlink.h: "limit to 8K to avoid MSG_TRUNC when PAGE_SIZE is very large".
464 * Note: on error returns (-1) we exit, no need to free replybuf.
465 */
466 enum { BUF_SIZE = 8 * 1024 };
467 char *replybuf = xmalloc(BUF_SIZE);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000468
Denys Vlasenko37201212010-04-02 07:04:44 +0200469 iface_len = strlen(G.iface);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000470 while (1) {
471 struct nlmsghdr *mhdr;
472 ssize_t bytes;
473
Tito Ragusada331d72013-08-04 18:08:40 +0200474 bytes = recv(netlink_fd, replybuf, BUF_SIZE, MSG_DONTWAIT);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000475 if (bytes < 0) {
476 if (errno == EAGAIN)
Tito Ragusada331d72013-08-04 18:08:40 +0200477 goto ret;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000478 if (errno == EINTR)
479 continue;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000480 bb_perror_msg("netlink: recv");
481 return -1;
482 }
483
484 mhdr = (struct nlmsghdr*)replybuf;
485 while (bytes > 0) {
Maxim Kryzhanovsky2004fa12010-03-30 15:49:57 +0200486 if (!NLMSG_OK(mhdr, bytes)) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000487 bb_error_msg("netlink packet too small or truncated");
488 return -1;
489 }
490
491 if (mhdr->nlmsg_type == RTM_NEWLINK || mhdr->nlmsg_type == RTM_DELLINK) {
492 struct rtattr *attr;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000493 int attr_len;
494
Denis Vlasenko71c16572009-04-26 01:08:51 +0000495 if (mhdr->nlmsg_len < NLMSG_LENGTH(sizeof(struct ifinfomsg))) {
496 bb_error_msg("netlink packet too small or truncated");
497 return -1;
498 }
499
Maxim Kryzhanovsky2004fa12010-03-30 15:49:57 +0200500 attr = IFLA_RTA(NLMSG_DATA(mhdr));
501 attr_len = IFLA_PAYLOAD(mhdr);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000502
503 while (RTA_OK(attr, attr_len)) {
504 if (attr->rta_type == IFLA_IFNAME) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000505 int len = RTA_PAYLOAD(attr);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000506 if (len > IFNAMSIZ)
507 len = IFNAMSIZ;
Denys Vlasenko37201212010-04-02 07:04:44 +0200508 if (iface_len <= len
509 && strncmp(G.iface, RTA_DATA(attr), len) == 0
510 ) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000511 G.iface_exists = (mhdr->nlmsg_type == RTM_NEWLINK);
512 }
513 }
514 attr = RTA_NEXT(attr, attr_len);
515 }
516 }
517
518 mhdr = NLMSG_NEXT(mhdr, bytes);
519 }
520 }
521
Tito Ragusada331d72013-08-04 18:08:40 +0200522 ret:
523 free(replybuf);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000524 return G.iface_exists;
525}
526
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200527#if ENABLE_FEATURE_PIDFILE
Denis Vlasenko71c16572009-04-26 01:08:51 +0000528static NOINLINE pid_t read_pid(const char *filename)
529{
530 int len;
531 char buf[128];
532
533 len = open_read_close(filename, buf, 127);
534 if (len > 0) {
535 buf[len] = '\0';
536 /* returns ULONG_MAX on error => -1 */
537 return bb_strtoul(buf, NULL, 10);
538 }
539 return 0;
540}
Denys Vlasenko9c35a1c2009-05-01 09:04:25 +0200541#endif
Denis Vlasenko71c16572009-04-26 01:08:51 +0000542
543int ifplugd_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
544int ifplugd_main(int argc UNUSED_PARAM, char **argv)
545{
546 int iface_status;
547 int delay_time;
548 const char *iface_status_str;
549 struct pollfd netlink_pollfd[1];
550 unsigned opts;
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200551 const char *api_mode_found;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000552#if ENABLE_FEATURE_PIDFILE
553 char *pidfile_name;
554 pid_t pid_from_pidfile;
555#endif
556
557 INIT_G();
558
559 opt_complementary = "t+:u+:d+";
560 opts = getopt32(argv, OPTION_STR,
561 &G.iface, &G.script_name, &G.poll_time, &G.delay_up,
562 &G.delay_down, &G.api_mode, &G.extra_arg);
Denys Vlasenko5a34d022009-11-07 17:30:14 +0100563 G.poll_time *= 1000;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000564
565 applet_name = xasprintf("ifplugd(%s)", G.iface);
566
567#if ENABLE_FEATURE_PIDFILE
Anthony G. Basile12677ac2012-12-10 14:49:39 -0500568 pidfile_name = xasprintf(CONFIG_PID_FILE_PATH "/ifplugd.%s.pid", G.iface);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000569 pid_from_pidfile = read_pid(pidfile_name);
570
571 if (opts & FLAG_KILL) {
572 if (pid_from_pidfile > 0)
Denys Vlasenko216e9522013-02-28 12:50:09 +0100573 /* Upstream tool use SIGINT for -k */
574 kill(pid_from_pidfile, SIGINT);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000575 return EXIT_SUCCESS;
576 }
577
578 if (pid_from_pidfile > 0 && kill(pid_from_pidfile, 0) == 0)
579 bb_error_msg_and_die("daemon already running");
580#endif
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200581
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200582 api_mode_found = strchr(api_modes, G.api_mode[0]);
583 if (!api_mode_found)
Denis Vlasenko71c16572009-04-26 01:08:51 +0000584 bb_error_msg_and_die("unknown API mode '%s'", G.api_mode);
Maksym Kryzhanovskyy9388b4e2010-07-08 02:47:25 +0200585 G.api_method_num = api_mode_found - api_modes;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000586
587 if (!(opts & FLAG_NO_DAEMON))
588 bb_daemonize_or_rexec(DAEMON_CHDIR_ROOT, argv);
589
590 xmove_fd(xsocket(AF_INET, SOCK_DGRAM, 0), ioctl_fd);
591 if (opts & FLAG_MONITOR) {
Maksym Kryzhanovskyy4f0279b2010-07-22 02:18:05 +0200592 struct sockaddr_nl addr;
593 int fd = xsocket(PF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
594
595 memset(&addr, 0, sizeof(addr));
596 addr.nl_family = AF_NETLINK;
597 addr.nl_groups = RTMGRP_LINK;
598 addr.nl_pid = getpid();
599
600 xbind(fd, (struct sockaddr*)&addr, sizeof(addr));
601 xmove_fd(fd, netlink_fd);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000602 }
603
604 write_pidfile(pidfile_name);
605
606 /* this can't be moved before socket creation */
607 if (!(opts & FLAG_NO_SYSLOG)) {
608 openlog(applet_name, 0, LOG_DAEMON);
609 logmode |= LOGMODE_SYSLOG;
610 }
611
612 bb_signals(0
613 | (1 << SIGINT )
614 | (1 << SIGTERM)
615 | (1 << SIGQUIT)
616 | (1 << SIGHUP ) /* why we ignore it? */
617 /* | (1 << SIGCHLD) - run_script does not use it anymore */
618 , record_signo);
619
620 bb_error_msg("started: %s", bb_banner);
621
622 if (opts & FLAG_MONITOR) {
623 struct ifreq ifrequest;
624 set_ifreq_to_ifname(&ifrequest);
Denys Vlasenkof422a722010-01-08 12:27:57 +0100625 G.iface_exists = (network_ioctl(SIOCGIFINDEX, &ifrequest, NULL) == 0);
Denis Vlasenko71c16572009-04-26 01:08:51 +0000626 }
627
628 if (G.iface_exists)
629 maybe_up_new_iface();
630
631 iface_status = detect_link();
632 if (iface_status == IFSTATUS_ERR)
633 goto exiting;
634 iface_status_str = strstatus(iface_status);
635
636 if (opts & FLAG_MONITOR) {
637 bb_error_msg("interface %s",
638 G.iface_exists ? "exists"
639 : "doesn't exist, waiting");
640 }
641 /* else we assume it always exists, but don't mislead user
642 * by potentially lying that it really exists */
643
644 if (G.iface_exists) {
645 bb_error_msg("link is %s", iface_status_str);
646 }
647
648 if ((!(opts & FLAG_NO_STARTUP)
649 && iface_status == IFSTATUS_UP
650 )
651 || (opts & FLAG_INITIAL_DOWN)
652 ) {
653 if (run_script(iface_status_str) != 0)
654 goto exiting;
655 }
656
657 /* Main loop */
658 netlink_pollfd[0].fd = netlink_fd;
659 netlink_pollfd[0].events = POLLIN;
660 delay_time = 0;
661 while (1) {
662 int iface_status_old;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000663
664 switch (bb_got_signal) {
665 case SIGINT:
666 case SIGTERM:
667 bb_got_signal = 0;
668 goto cleanup;
669 case SIGQUIT:
670 bb_got_signal = 0;
671 goto exiting;
672 default:
673 bb_got_signal = 0;
674 break;
675 }
676
677 if (poll(netlink_pollfd,
678 (opts & FLAG_MONITOR) ? 1 : 0,
Denys Vlasenko5a34d022009-11-07 17:30:14 +0100679 G.poll_time
Denis Vlasenko71c16572009-04-26 01:08:51 +0000680 ) < 0
681 ) {
682 if (errno == EINTR)
683 continue;
684 bb_perror_msg("poll");
685 goto exiting;
686 }
687
Denis Vlasenko71c16572009-04-26 01:08:51 +0000688 if ((opts & FLAG_MONITOR)
689 && (netlink_pollfd[0].revents & POLLIN)
690 ) {
Denys Vlasenko50e4cc22014-02-10 09:44:25 +0100691 int iface_exists_old;
692
693 iface_exists_old = G.iface_exists;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000694 G.iface_exists = check_existence_through_netlink();
695 if (G.iface_exists < 0) /* error */
696 goto exiting;
697 if (iface_exists_old != G.iface_exists) {
698 bb_error_msg("interface %sappeared",
699 G.iface_exists ? "" : "dis");
700 if (G.iface_exists)
701 maybe_up_new_iface();
702 }
703 }
704
705 /* note: if !G.iface_exists, returns DOWN */
Denys Vlasenko50e4cc22014-02-10 09:44:25 +0100706 iface_status_old = iface_status;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000707 iface_status = detect_link();
708 if (iface_status == IFSTATUS_ERR) {
709 if (!(opts & FLAG_MONITOR))
710 goto exiting;
711 iface_status = IFSTATUS_DOWN;
712 }
713 iface_status_str = strstatus(iface_status);
714
715 if (iface_status_old != iface_status) {
716 bb_error_msg("link is %s", iface_status_str);
717
718 if (delay_time) {
719 /* link restored its old status before
Denys Vlasenko50e4cc22014-02-10 09:44:25 +0100720 * we ran script. don't run the script: */
Denis Vlasenko71c16572009-04-26 01:08:51 +0000721 delay_time = 0;
722 } else {
723 delay_time = monotonic_sec();
724 if (iface_status == IFSTATUS_UP)
725 delay_time += G.delay_up;
726 if (iface_status == IFSTATUS_DOWN)
727 delay_time += G.delay_down;
Denys Vlasenko50e4cc22014-02-10 09:44:25 +0100728#if 0 /* if you are back in 1970... */
729 if (delay_time == 0) {
730 sleep(1);
731 delay_time = 1;
732 }
733#endif
Denis Vlasenko71c16572009-04-26 01:08:51 +0000734 }
735 }
736
737 if (delay_time && (int)(monotonic_sec() - delay_time) >= 0) {
Denis Vlasenko71c16572009-04-26 01:08:51 +0000738 if (run_script(iface_status_str) != 0)
739 goto exiting;
Denys Vlasenko50e4cc22014-02-10 09:44:25 +0100740 delay_time = 0;
Denis Vlasenko71c16572009-04-26 01:08:51 +0000741 }
742 } /* while (1) */
743
744 cleanup:
745 if (!(opts & FLAG_NO_SHUTDOWN)
746 && (iface_status == IFSTATUS_UP
747 || (iface_status == IFSTATUS_DOWN && delay_time)
748 )
749 ) {
750 setenv(IFPLUGD_ENV_PREVIOUS, strstatus(iface_status), 1);
751 setenv(IFPLUGD_ENV_CURRENT, strstatus(-1), 1);
752 run_script("down\0up"); /* reusing string */
753 }
754
755 exiting:
756 remove_pidfile(pidfile_name);
757 bb_error_msg_and_die("exiting");
758}