blob: 6c4ae27f2230c83ed1faceccc2a315751648cbf5 [file] [log] [blame]
Eric Andersen8320b422003-04-02 10:13:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00002/*
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02003 * ifup/ifdown for busybox
4 * Copyright (c) 2002 Glenn McGrath
5 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02007 * Based on ifupdown v 0.6.4 by Anthony Towns
8 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00009 *
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +020010 * Changes to upstream version
11 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
12 * Lines in the interfaces file cannot wrap.
13 * To adhere to the FHS, the default state file is /var/run/ifstate
14 * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build
15 * configuration.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000016 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020017 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000018 */
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010019//config:config IFUP
Denys Vlasenkob097a842018-12-28 03:20:17 +010020//config: bool "ifup (14 kb)"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010021//config: default y
22//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: Activate the specified interfaces. This applet makes use
24//config: of either "ifconfig" and "route" or the "ip" command to actually
25//config: configure network interfaces. Therefore, you will probably also want
26//config: to enable either IFCONFIG and ROUTE, or enable
27//config: FEATURE_IFUPDOWN_IP and the various IP options. Of
28//config: course you could use non-busybox versions of these programs, so
29//config: against my better judgement (since this will surely result in plenty
30//config: of support questions on the mailing list), I do not force you to
31//config: enable these additional options. It is up to you to supply either
32//config: "ifconfig", "route" and "run-parts" or the "ip" command, either
33//config: via busybox or via standalone utilities.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010034//config:
35//config:config IFDOWN
Denys Vlasenkob097a842018-12-28 03:20:17 +010036//config: bool "ifdown (13 kb)"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010037//config: default y
38//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020039//config: Deactivate the specified interfaces.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010040//config:
41//config:config IFUPDOWN_IFSTATE_PATH
42//config: string "Absolute path to ifstate file"
43//config: default "/var/run/ifstate"
44//config: depends on IFUP || IFDOWN
45//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020046//config: ifupdown keeps state information in a file called ifstate.
47//config: Typically it is located in /var/run/ifstate, however
48//config: some distributions tend to put it in other places
49//config: (debian, for example, uses /etc/network/run/ifstate).
50//config: This config option defines location of ifstate.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010051//config:
52//config:config FEATURE_IFUPDOWN_IP
Denys Vlasenko100aea82016-12-23 15:18:31 +010053//config: bool "Use ip tool (else ifconfig/route is used)"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010054//config: default y
55//config: depends on IFUP || IFDOWN
56//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020057//config: Use the iproute "ip" command to implement "ifup" and "ifdown", rather
58//config: than the default of using the older "ifconfig" and "route" utilities.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010059//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020060//config: If Y: you must install either the full-blown iproute2 package
Denys Vlasenko68b653b2017-07-27 10:53:09 +020061//config: or enable "ip" applet in busybox, or the "ifup" and "ifdown" applets
Denys Vlasenko72089cf2017-07-21 09:50:55 +020062//config: will not work.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010063//config:
Denys Vlasenko72089cf2017-07-21 09:50:55 +020064//config: If N: you must install either the full-blown ifconfig and route
Denys Vlasenko68b653b2017-07-27 10:53:09 +020065//config: utilities, or enable these applets in busybox.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010066//config:
67//config:config FEATURE_IFUPDOWN_IPV4
Denys Vlasenkof5604222017-01-10 14:58:54 +010068//config: bool "Support IPv4"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010069//config: default y
70//config: depends on IFUP || IFDOWN
71//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020072//config: If you want ifup/ifdown to talk IPv4, leave this on.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010073//config:
74//config:config FEATURE_IFUPDOWN_IPV6
Denys Vlasenkof5604222017-01-10 14:58:54 +010075//config: bool "Support IPv6"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010076//config: default y
77//config: depends on (IFUP || IFDOWN) && FEATURE_IPV6
78//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020079//config: If you need support for IPv6, turn this option on.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010080//config:
81//UNUSED:
82////////:config FEATURE_IFUPDOWN_IPX
Denys Vlasenkof5604222017-01-10 14:58:54 +010083////////: bool "Support IPX"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010084////////: default y
85////////: depends on IFUP || IFDOWN
86////////: help
87////////: If this option is selected you can use busybox to work with IPX
88////////: networks.
89//config:
90//config:config FEATURE_IFUPDOWN_MAPPING
91//config: bool "Enable mapping support"
92//config: default y
93//config: depends on IFUP || IFDOWN
94//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020095//config: This enables support for the "mapping" stanza, unless you have
96//config: a weird network setup you don't need it.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010097//config:
98//config:config FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkof5604222017-01-10 14:58:54 +010099//config: bool "Support external DHCP clients"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100100//config: default n
101//config: depends on IFUP || IFDOWN
102//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200103//config: This enables support for the external dhcp clients. Clients are
104//config: tried in the following order: dhcpcd, dhclient, pump and udhcpc.
105//config: Otherwise, if udhcpc applet is enabled, it is used.
106//config: Otherwise, ifup/ifdown will have no support for DHCP.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100107
Denys Vlasenko205d48e2017-01-29 14:57:33 +0100108// APPLET_ODDNAME:name main location suid_type help
109//applet:IF_IFUP( APPLET_ODDNAME(ifup, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifup))
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100110//applet:IF_IFDOWN(APPLET_ODDNAME(ifdown, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifdown))
111
112//kbuild:lib-$(CONFIG_IFUP) += ifupdown.o
113//kbuild:lib-$(CONFIG_IFDOWN) += ifupdown.o
114
Pere Orga5bc8c002011-04-11 03:29:49 +0200115//usage:#define ifup_trivial_usage
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100116//usage: "[-n"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] -a | IFACE..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200117//usage:#define ifup_full_usage "\n\n"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100118//usage: " -a Configure all interfaces"
Denys Vlasenkobcf47ea2016-09-25 02:05:24 +0200119//usage: "\n -i FILE Use FILE instead of /etc/network/interfaces"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100120//usage: "\n -n Dry run"
Pere Orga5bc8c002011-04-11 03:29:49 +0200121//usage: IF_FEATURE_IFUPDOWN_MAPPING(
122//usage: "\n (note: doesn't disable mappings)"
123//usage: "\n -m Don't run any mappings"
124//usage: )
125//usage: "\n -v Print out what would happen before doing it"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100126//usage: "\n -f Force"
Pere Orga5bc8c002011-04-11 03:29:49 +0200127//usage:
128//usage:#define ifdown_trivial_usage
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100129//usage: "[-n"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] -a | IFACE..."
Pere Orga5bc8c002011-04-11 03:29:49 +0200130//usage:#define ifdown_full_usage "\n\n"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100131//usage: " -a Deconfigure all interfaces"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100132//usage: "\n -i FILE Use FILE instead of /etc/network/interfaces"
133//usage: "\n -n Dry run"
Pere Orga5bc8c002011-04-11 03:29:49 +0200134//usage: IF_FEATURE_IFUPDOWN_MAPPING(
135//usage: "\n (note: doesn't disable mappings)"
136//usage: "\n -m Don't run any mappings"
137//usage: )
138//usage: "\n -v Print out what would happen before doing it"
Denys Vlasenkoa2f18d92020-12-18 04:12:51 +0100139//usage: "\n -f Force"
Pere Orga5bc8c002011-04-11 03:29:49 +0200140
Kaarle Ritvanen123fdda2018-03-28 00:00:03 +0200141#include <net/if.h>
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200142#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200143#include "common_bufsiz.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200144/* After libbb.h, since it needs sys/types.h on some systems */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000145#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000146#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000147
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000148#define MAX_OPT_DEPTH 10
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000149
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000150#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +0000151#define MAX_INTERFACE_LENGTH 10
152#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000153
Wade Berrier142c5cb2008-11-14 21:18:45 +0000154#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
Denys Vlasenko3720a612016-10-09 23:04:16 +0200155#define IFSTATE_FILE_PATH CONFIG_IFUPDOWN_IFSTATE_PATH
Wade Berrier142c5cb2008-11-14 21:18:45 +0000156
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000157#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +0000158
159/* Forward declaration */
160struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000161
Denis Vlasenko097c3242006-11-27 16:59:15 +0000162typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000163
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000164struct method_t {
165 const char *name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200166 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
167 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
Eric Andersen8320b422003-04-02 10:13:26 +0000168};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000169
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000170struct address_family_t {
171 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000172 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +0000173 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +0000174};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000175
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000176struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +0000177 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000178
179 int max_matches;
180 int n_matches;
181 char **match;
182
183 char *script;
184
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000185 int n_mappings;
186 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +0000187};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000188
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000189struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000190 char *name;
191 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +0000192};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000193
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000194struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +0000195 const struct address_family_t *address_family;
196 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000197
Rob Landleye813ddb2006-02-28 03:53:14 +0000198 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000199 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000200 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000201};
202
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000203struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000204 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000205 llist_t *ifaces;
206 struct mapping_defn_t *mappings;
207};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000208
Denys Vlasenko62152da2009-06-05 21:53:11 +0200209
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000210#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000211enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200212 OPT_do_all = 0x1,
213 OPT_no_act = 0x2,
214 OPT_verbose = 0x4,
215 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000216 OPT_no_mappings = 0x10,
217};
Denys Vlasenko62152da2009-06-05 21:53:11 +0200218#define DO_ALL (option_mask32 & OPT_do_all)
219#define NO_ACT (option_mask32 & OPT_no_act)
220#define VERBOSE (option_mask32 & OPT_verbose)
221#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000222#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000223
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000224
Denys Vlasenko62152da2009-06-05 21:53:11 +0200225struct globals {
226 char **my_environ;
227 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100228 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100229} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200230#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200231#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200232
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000233
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200234static const char keywords_up_down[] ALIGN1 =
235 "up\0"
236 "down\0"
237 "pre-up\0"
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200238 "post-down\0"
239;
240
241
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000242#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
243
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000244static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000245{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000246 /* xasprintf trick will be smaller, but we are often
247 * called with str_length == 1 - don't want to have
248 * THAT much of malloc/freeing! */
249 char *buf = *bufp;
250 int len = (buf ? strlen(buf) : 0);
251 str_length++;
252 buf = xrealloc(buf, len + str_length);
253 /* copies at most str_length-1 chars! */
254 safe_strncpy(buf + len, str, str_length);
255 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000256}
257
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000258static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000259{
260 int i = strncmp(l, r, llen);
261
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000262 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200263 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000264 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000265}
266
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000267static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000268{
269 int i;
270
271 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200272 // ubuntu's ifup doesn't do this:
273 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000274 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200275 //free(label_buf);
276 //label_buf = xstrdup(ifd->iface);
277 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000278 //result = strchrnul(label_buf, ':');
279 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200280 //return label_buf;
281
282 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000283 }
284 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000285 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000286 }
287 for (i = 0; i < ifd->n_options; i++) {
288 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
289 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000290 }
291 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000292 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000293}
294
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200295# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000296static int count_netmask_bits(const char *dotted_quad)
297{
298// int result;
299// unsigned a, b, c, d;
300// /* Found a netmask... Check if it is dotted quad */
301// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
302// return -1;
303// if ((a|b|c|d) >> 8)
304// return -1; /* one of numbers is >= 256 */
305// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
306// d = ~d; /* 11110000 -> 00001111 */
307
308 /* Shorter version */
309 int result;
310 struct in_addr ip;
311 unsigned d;
312
313 if (inet_aton(dotted_quad, &ip) == 0)
314 return -1; /* malformed dotted IP */
315 d = ntohl(ip.s_addr); /* IP in host order */
316 d = ~d; /* 11110000 -> 00001111 */
317 if (d & (d+1)) /* check that it is in 00001111 form */
318 return -1; /* no it is not */
319 result = 32;
320 while (d) {
321 d >>= 1;
322 result--;
323 }
324 return result;
325}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200326# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000327
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000328static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000330 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
Denys Vlasenko3f210442012-11-09 17:16:40 +0100331 smallint okay[MAX_OPT_DEPTH] = { 1 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000333 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000334
335 while (*command) {
336 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000337 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000338 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000339 command++;
340 break;
341 case '\\':
Denys Vlasenko3f210442012-11-09 17:16:40 +0100342 if (command[1])
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000343 command++;
Denys Vlasenko3f210442012-11-09 17:16:40 +0100344 addstr(&result, command, 1);
345 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000346 break;
347 case '[':
348 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000349 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000350 okay[opt_depth] = 1;
351 opt_depth++;
352 command += 2;
353 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100354 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000355 command++;
356 }
357 break;
358 case ']':
359 if (command[1] == ']' && opt_depth > 1) {
360 opt_depth--;
361 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000362 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000364 command += 2;
365 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100366 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000367 command++;
368 }
369 break;
370 case '%':
371 {
372 char *nextpercent;
373 char *varvalue;
374
375 command++;
376 nextpercent = strchr(command, '%');
377 if (!nextpercent) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100378 /* Unterminated %var% */
Denis Vlasenko05341252006-09-26 20:35:30 +0000379 free(result);
380 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000381 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000382
383 varvalue = get_var(command, nextpercent - command, ifd);
384
385 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200386# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000387 /* "hwaddress <class> <address>":
388 * unlike ifconfig, ip doesnt want <class>
389 * (usually "ether" keyword). Skip it. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100390 if (is_prefixed_with(command, "hwaddress")) {
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000391 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
392 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200393# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000394 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000395 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200396# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000397 /* Sigh... Add a special case for 'ip' to convert from
398 * dotted quad to bit count style netmasks. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100399 if (is_prefixed_with(command, "bnmask")) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000400 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000401 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000402 if (varvalue) {
403 res = count_netmask_bits(varvalue);
404 if (res > 0) {
405 const char *argument = utoa(res);
406 addstr(&result, argument, strlen(argument));
407 command = nextpercent + 1;
408 break;
409 }
Eric Andersen8320b422003-04-02 10:13:26 +0000410 }
Eric Andersen8320b422003-04-02 10:13:26 +0000411 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200412# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000413 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000414 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000415
416 command = nextpercent + 1;
417 }
418 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000419 }
420 }
421
422 if (opt_depth > 1) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100423 /* Unbalanced bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000425 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000426 }
427
428 if (!okay[0]) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100429 /* Undefined variable and we aren't in a bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000430 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000431 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000432 }
433
Denis Vlasenko05341252006-09-26 20:35:30 +0000434 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000435}
436
Denis Vlasenko05341252006-09-26 20:35:30 +0000437/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000438static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000439{
440 char *out;
441 int ret;
442
443 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000444 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000445 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000446 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000447 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000448 /* out == "": parsed ok but not all needed variables known, skip */
449 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450
451 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000452 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000453 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000454 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000455 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200457
458#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
459
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000460
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000461#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200462
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200463static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200465# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000466 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000467 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000468 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000469 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200470# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000471 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200472# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000473}
474
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200475static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000476{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200477# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000478 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200479# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000480 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200481# endif
482}
483
484static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
485{
486 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000487}
488
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200489static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000490{
Eric Andersen8320b422003-04-02 10:13:26 +0000491 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200492# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000493 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000494 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100495 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
496 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]][[ metric %metric%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200497# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000498 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000499 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000500 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200501# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000502 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000503}
504
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200505static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000506{
Kaarle Ritvanen123fdda2018-03-28 00:00:03 +0200507 if (!if_nametoindex(ifd->iface))
508 return 1; /* already gone */
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200509# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000510 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200511# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000512 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200513# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514}
515
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200516# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200517static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000518{
Eric Andersen8320b422003-04-02 10:13:26 +0000519 int result;
520 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000521 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000522 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000523 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100524 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
525 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000526 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527}
528
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200529static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000530{
Denis Vlasenko05341252006-09-26 20:35:30 +0000531 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200533# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000534
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100535static const struct method_t methods6[] ALIGN_PTR = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200536# if ENABLE_FEATURE_IFUPDOWN_IP
537 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
538# endif
539 { "static" , static_up6 , static_down6 , },
540 { "manual" , manual_up_down6 , manual_up_down6 , },
541 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000542};
543
Denis Vlasenko05341252006-09-26 20:35:30 +0000544static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000546 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000547 methods6
548};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200549
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000550#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200552
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000553#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200554
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200555static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200557# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000558 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000559 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000560 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000561 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200562# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000563 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200564# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565}
566
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200567static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200569# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000570 int result;
571 result = execute("ip addr flush dev %iface%", ifd, exec);
572 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000573 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200574# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000575 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200576# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577}
578
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200579static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580{
Eric Andersen8320b422003-04-02 10:13:26 +0000581 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200582# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000583 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
584 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000585 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Alex Dowad65c501a2015-08-07 11:53:26 +0200586 result += execute("[[ip route add default via %gateway% dev %iface%[[ metric %metric%]]]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000587 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200588# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000589 /* ifconfig said to set iface up before it processes hw %hwaddress%,
590 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000591 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000592 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000593 result += execute("ifconfig %iface% %address% netmask %netmask%"
Denys Vlasenko92d98f02016-09-20 17:31:11 +0200594 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]]",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000595 ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000596 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000597 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200598# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599}
600
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200601static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000602{
Eric Andersen8320b422003-04-02 10:13:26 +0000603 int result;
Kaarle Ritvanen123fdda2018-03-28 00:00:03 +0200604
605 if (!if_nametoindex(ifd->iface))
606 return 2; /* already gone */
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200607# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenko92d98f02016-09-20 17:31:11 +0200608 /* Optional "label LBL" is necessary if interface is an alias (eth0:0),
609 * otherwise "ip addr flush dev eth0:0" flushes all addresses on eth0.
610 */
611 result = execute("ip addr flush dev %iface%[[ label %label%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000612 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200613# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000614 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
615 /* Bringing the interface down deletes the routes in itself.
616 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
617 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000618 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200619# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000620 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621}
622
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200623# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200624struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000625 const char *name;
626 const char *startcmd;
627 const char *stopcmd;
628};
629
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100630static const struct dhcp_client_t ext_dhcp_clients[] ALIGN_PTR = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000631 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200632 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000633 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000634 },
635 { "dhclient",
636 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
637 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
638 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000639 { "pump",
640 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
641 "pump -i %iface% -k",
642 },
643 { "udhcpc",
Natanael Copa10623912012-09-19 16:55:08 +0200644 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -x hostname:%hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000645 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000646 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000647 },
648};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200649# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000650
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200651# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200652static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000653{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000654 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200655# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000656 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000657 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000658 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200659# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000660 /* needed if we have hwaddress on dhcp iface */
661 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000662 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200663# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000664 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200665 if (executable_exists(ext_dhcp_clients[i].name))
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000666 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
667 }
James Byrne69374872019-07-02 11:35:03 +0200668 bb_simple_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000669 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000670}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200671# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200672static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000673{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200674# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000675 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000676 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000677 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200678# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000679 /* needed if we have hwaddress on dhcp iface */
680 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000681 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200682# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000683 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Natanael Copa10623912012-09-19 16:55:08 +0200684 "-i %iface%[[ -x hostname:%hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000685 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000686}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200687# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200688static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000689 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000690{
691 return 0; /* no dhcp support */
692}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200693# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000694
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200695# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200696static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000697{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000698 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000699 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000700
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000701 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200702 if (executable_exists(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200703 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000704 if (result)
705 break;
706 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000707 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000708
709 if (!result)
James Byrne69374872019-07-02 11:35:03 +0200710 bb_simple_error_msg("warning: no dhcp clients found and stopped");
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000711
712 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
713 and it may come back up because udhcpc is still shutting down */
714 usleep(100000);
715 result += static_down(ifd, exec);
716 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000717}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200718# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200719static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000720{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000721 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200722 result = execute(
723 "test -f /var/run/udhcpc.%iface%.pid && "
724 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
725 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000726 /* Also bring the hardware interface down since
727 killing the dhcp client alone doesn't do it.
728 This enables consecutive ifup->ifdown->ifup */
729 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
730 and it may come back up because udhcpc is still shutting down */
731 usleep(100000);
732 result += static_down(ifd, exec);
733 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200735# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200736static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000737 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000738{
739 return 0; /* no dhcp support */
740}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200741# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000742
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200743static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000744{
745 return 1;
746}
747
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200748static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000749{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000750 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000751 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
752 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000753}
754
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200755static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000756{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000757 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000758}
759
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200760static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000761{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000762 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000763}
764
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200765static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000766{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000767 return execute("start-stop-daemon --start -x wvdial "
768 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000769}
770
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200771static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000772{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000773 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000774 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775}
776
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100777static const struct method_t methods[] ALIGN_PTR = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200778 { "manual" , manual_up_down, manual_up_down, },
779 { "wvdial" , wvdial_up , wvdial_down , },
780 { "ppp" , ppp_up , ppp_down , },
781 { "static" , static_up , static_down , },
782 { "bootp" , bootp_up , static_down , },
783 { "dhcp" , dhcp_up , dhcp_down , },
784 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000785};
786
Denis Vlasenko05341252006-09-26 20:35:30 +0000787static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000788 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000789 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000790 methods
791};
792
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200793#endif /* FEATURE_IFUPDOWN_IPV4 */
794
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100795static int FAST_FUNC link_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
796{
797 return 1;
798}
799
Denys Vlasenkoca466f32022-02-06 19:53:10 +0100800static const struct method_t link_methods[] ALIGN_PTR = {
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100801 { "none", link_up_down, link_up_down }
802};
803
804static const struct address_family_t addr_link = {
805 "link", ARRAY_SIZE(link_methods), link_methods
806};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000807
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200808/* Returns pointer to the next word, or NULL.
809 * In 1st case, advances *buf to the word after this one.
810 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000811static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000813 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000814 char *word;
815
Glenn L McGrath85737042003-01-14 23:26:57 +0000816 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000817 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000818
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000819 /* Stop on EOL */
820 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000821 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000822
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000823 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000824 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000825
826 /* Unless we are already at NUL, store NUL and advance */
827 if (word[length] != '\0')
828 word[length++] = '\0';
829
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200830 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000831
832 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833}
834
Denis Vlasenko05341252006-09-26 20:35:30 +0000835static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836{
837 int i;
838
Denis Vlasenko736230e2006-11-20 19:40:36 +0000839 if (!name)
840 return NULL;
841
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000842 for (i = 0; af[i]; i++) {
843 if (strcmp(af[i]->name, name) == 0) {
844 return af[i];
845 }
846 }
847 return NULL;
848}
849
Denis Vlasenko05341252006-09-26 20:35:30 +0000850static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000851{
852 int i;
853
Denis Vlasenko736230e2006-11-20 19:40:36 +0000854 if (!name)
855 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000856 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857 for (i = 0; i < af->n_methods; i++) {
858 if (strcmp(af->method[i].name, name) == 0) {
859 return &af->method[i];
860 }
861 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000862 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000863}
864
Denys Vlasenkofb036632013-02-28 19:01:28 +0100865static struct interfaces_file_t *read_interfaces(const char *filename, struct interfaces_file_t *defn)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000866{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000867 /* Let's try to be compatible.
868 *
869 * "man 5 interfaces" says:
870 * Lines starting with "#" are ignored. Note that end-of-line
871 * comments are NOT supported, comments must be on a line of their own.
872 * A line may be extended across multiple lines by making
873 * the last character a backslash.
874 *
875 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000876 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000877 * be ignored. Blank lines are ignored. Lines may be indented freely.
878 * A "\" character at the very end of the line indicates the next line
879 * should be treated as a continuation of the current one.
Denys Vlasenkofb036632013-02-28 19:01:28 +0100880 *
881 * Lines beginning with "source" are used to include stanzas from
882 * other files, so configuration can be split into many files.
883 * The word "source" is followed by the path of file to be sourced.
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000884 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000885#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000886 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000887#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000888 struct interface_defn_t *currif = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000889 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000890 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000891 char *first_word;
892 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
894
Denys Vlasenkofb036632013-02-28 19:01:28 +0100895 if (!defn)
896 defn = xzalloc(sizeof(*defn));
897
898 debug_noise("reading %s file:\n", filename);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000899 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000900
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000901 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000902#if ENABLE_DESKTOP
903 /* Trailing "\" concatenates lines */
904 char *p;
905 while ((p = last_char_is(buf, '\\')) != NULL) {
906 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000907 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000908 if (!rest_of_line)
909 break;
910 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000911 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000912 free(rest_of_line);
913 buf = p;
914 }
915#endif
916 rest_of_line = buf;
917 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000918 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000919 free(buf);
920 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000921 }
922
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000923 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000924#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000925 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000927 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000928 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000929 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000931 /*currmap->n_mappings = 0;*/
932 /*currmap->mapping = NULL;*/
933 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000934 {
Eric Andersen8320b422003-04-02 10:13:26 +0000935 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936 while (*where != NULL) {
937 where = &(*where)->next;
938 }
939 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000940 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000941 }
Eric Andersen8320b422003-04-02 10:13:26 +0000942 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000943#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000944 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000945 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000946 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000947#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000948 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000950#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000951 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000952#endif
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100953 &addr_link,
Denis Vlasenko05341252006-09-26 20:35:30 +0000954 NULL
955 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000956 char *iface_name;
957 char *address_family_name;
958 char *method_name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000960 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000961 iface_name = next_word(&rest_of_line);
962 address_family_name = next_word(&rest_of_line);
963 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000965 if (method_name == NULL)
966 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000967
968 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000969 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000970
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000971 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000972 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000973
974 currif->iface = xstrdup(iface_name);
975
976 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000977 if (!currif->address_family)
978 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000979
980 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000981 if (!currif->method)
982 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Nicolas Cavallarie5aba882016-03-01 18:59:08 +0100983#if 0
984// Allegedly, Debian allows a duplicate definition:
985// iface eth0 inet static
986// address 192.168.0.15
987// netmask 255.255.0.0
988// gateway 192.168.0.1
989//
990// iface eth0 inet static
991// address 10.0.0.1
992// netmask 255.255.255.0
993//
994// This adds *two* addresses to eth0 (probably requires use of "ip", not "ifconfig"
995//
Denys Vlasenko35e063e2016-03-03 02:19:16 +0100996 llist_t *iface_list;
Denis Vlasenko05341252006-09-26 20:35:30 +0000997 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
998 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000999 if ((strcmp(tmp->iface, currif->iface) == 0)
1000 && (tmp->address_family == currif->address_family)
1001 ) {
1002 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +00001003 }
1004 }
Nicolas Cavallarie5aba882016-03-01 18:59:08 +01001005#endif
Denis Vlasenko05341252006-09-26 20:35:30 +00001006 llist_add_to_end(&(defn->ifaces), (char*)currif);
1007
1008 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001010 } else if (strcmp(first_word, "auto") == 0) {
1011 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001012
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001013 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +00001014 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001015 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001016 }
1017
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001018 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001019 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
1020 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021 }
1022 currently_processing = NONE;
Denys Vlasenkofb036632013-02-28 19:01:28 +01001023 } else if (strcmp(first_word, "source") == 0) {
1024 read_interfaces(next_word(&rest_of_line), defn);
Brandon Maier14454b32018-09-21 12:54:05 -05001025 } else if (is_prefixed_with(first_word, "source-dir")) {
1026 const char *dirpath;
1027 DIR *dir;
1028 struct dirent *entry;
1029
1030 dirpath = next_word(&rest_of_line);
1031 dir = xopendir(dirpath);
1032 while ((entry = readdir(dir)) != NULL) {
1033 char *path;
1034 if (entry->d_name[0] == '.')
1035 continue;
1036 path = concat_path_file(dirpath, entry->d_name);
1037 read_interfaces(path, defn);
1038 free(path);
1039 }
1040 closedir(dir);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041 } else {
1042 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001043 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001044 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001045 bb_error_msg_and_die("option with empty value \"%s\"", buf);
1046
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001047 if (strcmp(first_word, "post-up") == 0)
1048 first_word += 5; /* "up" */
1049 else if (strcmp(first_word, "pre-down") == 0)
1050 first_word += 4; /* "down" */
1051
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001052 /* If not one of "up", "down",... words... */
1053 if (index_in_strings(keywords_up_down, first_word) < 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001054 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001055 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001056 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001057 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +00001058 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001059 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001060 debug_noise("\t%s=%s\n", first_word, rest_of_line);
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001061 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001062 currif->option[currif->n_options].name = xstrdup(first_word);
1063 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +00001064 currif->n_options++;
1065 break;
1066 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001067#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001068 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001069 if (currmap->script != NULL)
1070 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001071 currmap->script = xstrdup(next_word(&rest_of_line));
1072 } else if (strcmp(first_word, "map") == 0) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001073 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001074 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +00001075 currmap->n_mappings++;
1076 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001077 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +00001078 }
1079#endif
1080 break;
1081 case NONE:
1082 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001083 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001084 }
1085 }
Glenn L McGrath85737042003-01-14 23:26:57 +00001086 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001087 } /* while (fgets) */
1088
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001089 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +00001090 /* ferror does NOT set errno! */
1091 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001092 }
1093 fclose(f);
Denys Vlasenkofb036632013-02-28 19:01:28 +01001094 debug_noise("\ndone reading %s\n\n", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001095
1096 return defn;
1097}
1098
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001099static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100{
1101 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +02001102 char *dst;
1103 char *src;
1104 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001105
Rob Landleyd921b2e2006-08-03 15:41:12 +00001106 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107
Denys Vlasenko62152da2009-06-05 21:53:11 +02001108 for (dst = src = result; (c = *src) != '=' && c; src++) {
1109 if (c == '-')
1110 c = '_';
1111 if (c >= 'a' && c <= 'z')
1112 c -= ('a' - 'A');
1113 if (isalnum(c) || c == '_')
1114 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001116 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117
1118 return result;
1119}
1120
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001121static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001123 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +02001124 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001125
Denys Vlasenko62152da2009-06-05 21:53:11 +02001126 if (G.my_environ != NULL) {
1127 for (pp = G.my_environ; *pp; pp++) {
1128 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001130 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001131 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001132
1133 /* note: last element will stay NULL: */
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001134 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
Denys Vlasenko62152da2009-06-05 21:53:11 +02001135 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136
1137 for (i = 0; i < iface->n_options; i++) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001138 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001139 continue;
1140 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001141 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142 }
1143
Denys Vlasenko62152da2009-06-05 21:53:11 +02001144 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
1145 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
1146 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
1147 *pp++ = setlocalenv("%s=%s", "MODE", mode);
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001148 *pp++ = setlocalenv("%s=%s", "PHASE", opt);
Denys Vlasenko62152da2009-06-05 21:53:11 +02001149 if (G.startup_PATH)
1150 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001151}
1152
1153static int doit(char *str)
1154{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001155 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001156 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001157 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001158 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001159 pid_t child;
1160 int status;
1161
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001162 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001163 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001164 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001165 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001166 if (child == 0) { /* child */
1167 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001168 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001169 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001170 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001171 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1172 return 0;
1173 }
1174 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001175 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176}
1177
Rob Landleye813ddb2006-02-28 03:53:14 +00001178static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179{
Denys Vlasenko405095d2019-04-28 17:55:27 +02001180 /* 'opt' is always short, the longest value is "post-down".
1181 * Can use on-stack buffer instead of xasprintf'ed one.
1182 */
1183 char buf[sizeof("run-parts /etc/network/if-%s.d")
1184 + sizeof("post-down")
1185 /*paranoia:*/ + 8
1186 ];
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 int i;
Denys Vlasenko405095d2019-04-28 17:55:27 +02001188
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001189 for (i = 0; i < ifd->n_options; i++) {
1190 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001191 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 return 0;
1193 }
1194 }
1195 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001196
Denys Vlasenkobcf47ea2016-09-25 02:05:24 +02001197 /* Tested on Debian Squeeze: "standard" ifup runs this without
1198 * checking that directory exists. If it doesn't, run-parts
1199 * complains, and this message _is_ annoyingly visible.
1200 * Don't "fix" this (unless newer Debian does).
1201 */
Denys Vlasenko405095d2019-04-28 17:55:27 +02001202 sprintf(buf, "run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001203 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204}
1205
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001206static int check(char *str)
1207{
Eric Andersen8320b422003-04-02 10:13:26 +00001208 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209}
1210
Eric Andersen8320b422003-04-02 10:13:26 +00001211static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001212{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001213 if (!iface->method->up(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001214 set_environ(iface, "start", "pre-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001215 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001216 if (!iface->method->up(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001217 set_environ(iface, "start", "post-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001218 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001219 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001220}
1221
1222static int iface_down(struct interface_defn_t *iface)
1223{
Denys Vlasenkoa0fd4a72012-09-02 14:29:14 +02001224 if (!iface->method->down(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001225 set_environ(iface, "stop", "pre-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001226 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001227 if (!iface->method->down(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001228 set_environ(iface, "stop", "post-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001229 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001230 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231}
1232
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001233#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001234static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001235{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001236 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001237 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 pid_t pid;
1239
Denis Vlasenko37188322008-02-16 13:20:56 +00001240 xpiped_pair(infd);
1241 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001242
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001243 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001244 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001245
Pascal Bellard926031b2010-07-04 15:32:38 +02001246 if (pid == 0) {
1247 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001248 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001249 close(infd.wr);
1250 close(outfd.rd);
1251 xmove_fd(infd.rd, 0);
1252 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001253 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001255 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001256 close(infd.rd);
1257 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001258 *in = xfdopen_for_write(infd.wr);
1259 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001260 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001261}
1262
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001263static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001264{
1265 FILE *in, *out;
1266 int i, status;
1267 pid_t pid;
1268
Rob Landleyd921b2e2006-08-03 15:41:12 +00001269 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001271 /* Run the mapping script. Never fails. */
1272 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001273
1274 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001275 for (i = 0; i < map->n_mappings; i++) {
1276 fprintf(in, "%s\n", map->mapping[i]);
1277 }
1278 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001279 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001280
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001281 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001282 /* If the mapping script exited successfully, try to
1283 * grab a line of output and use that as the name of the
1284 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001285 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001286
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001287 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001288 /* If we are able to read a line of output from the script,
1289 * remove any trailing whitespace and use this value
1290 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001291 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001292
1293 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001294 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001295
1296 free(logical);
1297 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298 }
1299 }
Eric Andersen8a931792003-07-03 10:20:29 +00001300
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001301 fclose(out);
1302
Eric Andersen8a931792003-07-03 10:20:29 +00001303 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001304}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001305#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001307static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001308{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001309 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001310
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001311 while (search) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001312 char *after_iface = is_prefixed_with(search->data, iface);
1313 if (after_iface
1314 && *after_iface == '='
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001315 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001316 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001317 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001318 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001319 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001320 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001321}
1322
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001323/* read the previous state from the state file */
1324static llist_t *read_iface_state(void)
1325{
1326 llist_t *state_list = NULL;
Denys Vlasenko3720a612016-10-09 23:04:16 +02001327 FILE *state_fp = fopen_for_read(IFSTATE_FILE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001328
1329 if (state_fp) {
1330 char *start, *end_ptr;
1331 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1332 /* We should only need to check for a single character */
1333 end_ptr = start + strcspn(start, " \t\n");
1334 *end_ptr = '\0';
1335 llist_add_to(&state_list, start);
1336 }
1337 fclose(state_fp);
1338 }
1339 return state_list;
1340}
1341
Denys Vlasenko3720a612016-10-09 23:04:16 +02001342/* read the previous state from the state file */
1343static FILE *open_new_state_file(void)
1344{
1345 int fd, flags, cnt;
1346
1347 cnt = 0;
1348 flags = (O_WRONLY | O_CREAT | O_EXCL);
1349 for (;;) {
1350 fd = open(IFSTATE_FILE_PATH".new", flags, 0666);
1351 if (fd >= 0)
1352 break;
1353 if (errno != EEXIST
1354 || flags == (O_WRONLY | O_CREAT | O_TRUNC)
1355 ) {
1356 bb_perror_msg_and_die("can't open '%s'",
1357 IFSTATE_FILE_PATH".new");
1358 }
1359 /* Someone else created the .new file */
Denys Vlasenko030fe312020-12-11 16:48:47 +01001360 if (cnt > 30) {
Denys Vlasenko3720a612016-10-09 23:04:16 +02001361 /* Waited for 30*30/2 = 450 milliseconds, still EEXIST.
1362 * Assuming a stale file, rewriting it.
1363 */
1364 flags = (O_WRONLY | O_CREAT | O_TRUNC);
1365 continue;
1366 }
Denys Vlasenko030fe312020-12-11 16:48:47 +01001367 msleep(cnt);
1368 cnt++;
Denys Vlasenko3720a612016-10-09 23:04:16 +02001369 }
1370
1371 return xfdopen_for_write(fd);
1372}
1373
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001374int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001375int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001376{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001377 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001378 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001379 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001380 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001381 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001382
Denys Vlasenko62152da2009-06-05 21:53:11 +02001383 INIT_G();
1384
1385 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001386 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001387
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +01001388 if (ENABLE_IFUP
1389 && (!ENABLE_IFDOWN || applet_name[2] == 'u')
1390 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001391 /* ifup command */
1392 cmds = iface_up;
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +01001393 } else {
1394 cmds = iface_down;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001395 }
1396
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001397 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001398 argv += optind;
1399 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001400 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001401 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001402 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001403 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001404
Denys Vlasenkofb036632013-02-28 19:01:28 +01001405 defn = read_interfaces(interfaces, NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001406
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001407 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001408 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001409 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001410 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001411 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001412 }
1413
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001414 /* Update the interfaces */
1415 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001416 llist_t *iface_list;
1417 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001418 char *iface;
1419 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001420 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001421 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001422 int cmds_ret;
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001423 bool curr_failure = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001424
Rob Landleyd921b2e2006-08-03 15:41:12 +00001425 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001426 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001427
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001428 pch = strchr(iface, '=');
1429 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001430 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001431 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001432 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001433 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001434 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001435
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001436 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001437 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001438 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001439
1440 if (cmds == iface_up) {
1441 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001442 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001443 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001444 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001445 }
1446 } else {
1447 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001448 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001449 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001450 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001451 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001452 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001453 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001454 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001455
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001456#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001457 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001458 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001459
1460 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001461 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001462 for (i = 0; i < currmap->n_matches; i++) {
1463 if (fnmatch(currmap->match[i], liface, 0) != 0)
1464 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001465 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001466 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001467 }
Eric Andersen8a931792003-07-03 10:20:29 +00001468 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001469 break;
1470 }
1471 }
1472 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001473#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001474
Eric Andersen8320b422003-04-02 10:13:26 +00001475 iface_list = defn->ifaces;
1476 while (iface_list) {
1477 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001478 if (strcmp(liface, currif->iface) == 0) {
1479 char *oldiface = currif->iface;
1480
1481 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001482 currif->iface = iface;
1483
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001484 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001485
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001486 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001487 cmds_ret = cmds(currif);
1488 if (cmds_ret == -1) {
Denys Vlasenko6d8ea1d2015-03-22 17:08:51 +01001489 bb_error_msg("don't have all variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001490 liface, currif->address_family->name);
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001491 any_failures = curr_failure = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001492 } else if (cmds_ret == 0) {
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001493 any_failures = curr_failure = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001494 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001495
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001496 currif->iface = oldiface;
1497 }
Eric Andersen8320b422003-04-02 10:13:26 +00001498 iface_list = iface_list->link;
1499 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001500 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001501 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001502 }
1503
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001504 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001505 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001506 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001507 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001508 /* update the state file */
Denys Vlasenko3720a612016-10-09 23:04:16 +02001509 FILE *new_state_fp = open_new_state_file();
Denis Vlasenkof92df582007-05-02 22:22:23 +00001510 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001511 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001512 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001513
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001514 if (cmds == iface_up && !curr_failure) {
Denys Vlasenkod6f50002011-12-15 12:39:25 +01001515 char *newiface = xasprintf("%s=%s", iface, liface);
1516 if (!iface_state) {
Rob Landley8bb50782006-05-26 23:44:51 +00001517 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001518 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001519 free(iface_state->data);
1520 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001521 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001522 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001523 /* Remove an interface from state_list */
1524 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001525 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001526 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001527
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001528 /* Actually write the new state */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001529 state = state_list;
1530 while (state) {
1531 if (state->data) {
Denys Vlasenko3720a612016-10-09 23:04:16 +02001532 fprintf(new_state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001533 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001534 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001535 }
Denys Vlasenko3720a612016-10-09 23:04:16 +02001536 fclose(new_state_fp);
1537 xrename(IFSTATE_FILE_PATH".new", IFSTATE_FILE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001538 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001539 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001540 next:
1541 free(iface);
1542 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001543 }
1544
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001545 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001546}