blob: 78baa66905bda35389b8c562fc2106e254ee589b [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 Vlasenkoa8c696b2016-11-14 18:31:07 +01003 * ifup/ifdown for busybox
Denis Vlasenko0beaff82007-09-21 13:16:32 +00004 * Copyright (c) 2002 Glenn McGrath
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
7 * Based on ifupdown v 0.6.4 by Anthony Towns
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00008 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000010 * Changes to upstream version
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000011 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000012 * Lines in the interfaces file cannot wrap.
Denis Vlasenko2a86a612007-07-19 21:49:30 +000013 * 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 */
19
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010020//config:config IFUP
21//config: bool "ifup"
22//config: default y
23//config: help
24//config: Activate the specified interfaces. This applet makes use
25//config: of either "ifconfig" and "route" or the "ip" command to actually
26//config: configure network interfaces. Therefore, you will probably also want
27//config: to enable either IFCONFIG and ROUTE, or enable
28//config: FEATURE_IFUPDOWN_IP and the various IP options. Of
29//config: course you could use non-busybox versions of these programs, so
30//config: against my better judgement (since this will surely result in plenty
31//config: of support questions on the mailing list), I do not force you to
32//config: enable these additional options. It is up to you to supply either
33//config: "ifconfig", "route" and "run-parts" or the "ip" command, either
34//config: via busybox or via standalone utilities.
35//config:
36//config:config IFDOWN
37//config: bool "ifdown"
38//config: default y
39//config: help
40//config: Deactivate the specified interfaces.
41//config:
42//config:config IFUPDOWN_IFSTATE_PATH
43//config: string "Absolute path to ifstate file"
44//config: default "/var/run/ifstate"
45//config: depends on IFUP || IFDOWN
46//config: help
47//config: ifupdown keeps state information in a file called ifstate.
48//config: Typically it is located in /var/run/ifstate, however
49//config: some distributions tend to put it in other places
50//config: (debian, for example, uses /etc/network/run/ifstate).
51//config: This config option defines location of ifstate.
52//config:
53//config:config FEATURE_IFUPDOWN_IP
Denys Vlasenko100aea82016-12-23 15:18:31 +010054//config: bool "Use ip tool (else ifconfig/route is used)"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010055//config: default y
56//config: depends on IFUP || IFDOWN
57//config: help
58//config: Use the iproute "ip" command to implement "ifup" and "ifdown", rather
Denys Vlasenko100aea82016-12-23 15:18:31 +010059//config: than the default of using the older "ifconfig" and "route" utilities.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010060//config:
Denys Vlasenko100aea82016-12-23 15:18:31 +010061//config: If Y: you must install either the full-blown iproute2 package
62//config: or enable "ip" applet in Busybox, or the "ifup" and "ifdown" applets
63//config: will not work.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010064//config:
Denys Vlasenko100aea82016-12-23 15:18:31 +010065//config: If N: you must install either the full-blown ifconfig and route
66//config: utilities, or enable these applets in Busybox.
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010067//config:
68//config:config FEATURE_IFUPDOWN_IPV4
Denys Vlasenkof5604222017-01-10 14:58:54 +010069//config: bool "Support IPv4"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010070//config: default y
71//config: depends on IFUP || IFDOWN
72//config: help
73//config: If you want ifup/ifdown to talk IPv4, leave this on.
74//config:
75//config:config FEATURE_IFUPDOWN_IPV6
Denys Vlasenkof5604222017-01-10 14:58:54 +010076//config: bool "Support IPv6"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010077//config: default y
78//config: depends on (IFUP || IFDOWN) && FEATURE_IPV6
79//config: help
80//config: If you need support for IPv6, turn this option on.
81//config:
82//UNUSED:
83////////:config FEATURE_IFUPDOWN_IPX
Denys Vlasenkof5604222017-01-10 14:58:54 +010084////////: bool "Support IPX"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +010085////////: default y
86////////: depends on IFUP || IFDOWN
87////////: help
88////////: If this option is selected you can use busybox to work with IPX
89////////: networks.
90//config:
91//config:config FEATURE_IFUPDOWN_MAPPING
92//config: bool "Enable mapping support"
93//config: default y
94//config: depends on IFUP || IFDOWN
95//config: help
96//config: This enables support for the "mapping" stanza, unless you have
97//config: a weird network setup you don't need it.
98//config:
99//config:config FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkof5604222017-01-10 14:58:54 +0100100//config: bool "Support external DHCP clients"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100101//config: default n
102//config: depends on IFUP || IFDOWN
103//config: help
104//config: This enables support for the external dhcp clients. Clients are
105//config: tried in the following order: dhcpcd, dhclient, pump and udhcpc.
106//config: Otherwise, if udhcpc applet is enabled, it is used.
107//config: Otherwise, ifup/ifdown will have no support for DHCP.
108
109//applet:IF_IFUP(APPLET_ODDNAME(ifup, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifup))
110//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
116//usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
117//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"
Pere Orga5bc8c002011-04-11 03:29:49 +0200120//usage: "\n -n Print out what would happen, but don't do it"
121//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 Vlasenkoa8c696b2016-11-14 18:31:07 +0100126//usage: "\n -f Force configuration"
Pere Orga5bc8c002011-04-11 03:29:49 +0200127//usage:
128//usage:#define ifdown_trivial_usage
129//usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
130//usage:#define ifdown_full_usage "\n\n"
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +0100131//usage: " -a Deconfigure all interfaces"
Pere Orga5bc8c002011-04-11 03:29:49 +0200132//usage: "\n -i FILE Use FILE for interface definitions"
133//usage: "\n -n Print out what would happen, but don't do it"
134//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 Vlasenkoa8c696b2016-11-14 18:31:07 +0100139//usage: "\n -f Force deconfiguration"
Pere Orga5bc8c002011-04-11 03:29:49 +0200140
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200141#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200142#include "common_bufsiz.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200143/* After libbb.h, since it needs sys/types.h on some systems */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000144#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000145#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000146
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000147#define MAX_OPT_DEPTH 10
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000148
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000149#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +0000150#define MAX_INTERFACE_LENGTH 10
151#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000152
Wade Berrier142c5cb2008-11-14 21:18:45 +0000153#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
Denys Vlasenko3720a612016-10-09 23:04:16 +0200154#define IFSTATE_FILE_PATH CONFIG_IFUPDOWN_IFSTATE_PATH
Wade Berrier142c5cb2008-11-14 21:18:45 +0000155
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000156#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +0000157
158/* Forward declaration */
159struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000160
Denis Vlasenko097c3242006-11-27 16:59:15 +0000161typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000162
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000163struct method_t {
164 const char *name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200165 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
166 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
Eric Andersen8320b422003-04-02 10:13:26 +0000167};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000168
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000169struct address_family_t {
170 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000171 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +0000172 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +0000173};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000174
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000175struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +0000176 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000177
178 int max_matches;
179 int n_matches;
180 char **match;
181
182 char *script;
183
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000184 int n_mappings;
185 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +0000186};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000187
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000188struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000189 char *name;
190 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +0000191};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000193struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +0000194 const struct address_family_t *address_family;
195 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000196
Rob Landleye813ddb2006-02-28 03:53:14 +0000197 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000198 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000199 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000200};
201
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000202struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000203 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000204 llist_t *ifaces;
205 struct mapping_defn_t *mappings;
206};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000207
Denys Vlasenko62152da2009-06-05 21:53:11 +0200208
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000209#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000210enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200211 OPT_do_all = 0x1,
212 OPT_no_act = 0x2,
213 OPT_verbose = 0x4,
214 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000215 OPT_no_mappings = 0x10,
216};
Denys Vlasenko62152da2009-06-05 21:53:11 +0200217#define DO_ALL (option_mask32 & OPT_do_all)
218#define NO_ACT (option_mask32 & OPT_no_act)
219#define VERBOSE (option_mask32 & OPT_verbose)
220#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000221#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000222
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000223
Denys Vlasenko62152da2009-06-05 21:53:11 +0200224struct globals {
225 char **my_environ;
226 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100227 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100228} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200229#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200230#define INIT_G() do { setup_common_bufsiz(); } while (0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200231
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000232
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200233static const char keywords_up_down[] ALIGN1 =
234 "up\0"
235 "down\0"
236 "pre-up\0"
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200237 "post-down\0"
238;
239
240
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000241#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
242
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000243static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000244{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000245 /* xasprintf trick will be smaller, but we are often
246 * called with str_length == 1 - don't want to have
247 * THAT much of malloc/freeing! */
248 char *buf = *bufp;
249 int len = (buf ? strlen(buf) : 0);
250 str_length++;
251 buf = xrealloc(buf, len + str_length);
252 /* copies at most str_length-1 chars! */
253 safe_strncpy(buf + len, str, str_length);
254 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000255}
256
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000257static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000258{
259 int i = strncmp(l, r, llen);
260
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000261 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200262 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000263 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000264}
265
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000266static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000267{
268 int i;
269
270 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200271 // ubuntu's ifup doesn't do this:
272 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000273 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200274 //free(label_buf);
275 //label_buf = xstrdup(ifd->iface);
276 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000277 //result = strchrnul(label_buf, ':');
278 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200279 //return label_buf;
280
281 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000282 }
283 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000284 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000285 }
286 for (i = 0; i < ifd->n_options; i++) {
287 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
288 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000289 }
290 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000291 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000292}
293
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200294# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000295static int count_netmask_bits(const char *dotted_quad)
296{
297// int result;
298// unsigned a, b, c, d;
299// /* Found a netmask... Check if it is dotted quad */
300// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
301// return -1;
302// if ((a|b|c|d) >> 8)
303// return -1; /* one of numbers is >= 256 */
304// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
305// d = ~d; /* 11110000 -> 00001111 */
306
307 /* Shorter version */
308 int result;
309 struct in_addr ip;
310 unsigned d;
311
312 if (inet_aton(dotted_quad, &ip) == 0)
313 return -1; /* malformed dotted IP */
314 d = ntohl(ip.s_addr); /* IP in host order */
315 d = ~d; /* 11110000 -> 00001111 */
316 if (d & (d+1)) /* check that it is in 00001111 form */
317 return -1; /* no it is not */
318 result = 32;
319 while (d) {
320 d >>= 1;
321 result--;
322 }
323 return result;
324}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200325# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000326
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000327static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000328{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
Denys Vlasenko3f210442012-11-09 17:16:40 +0100330 smallint okay[MAX_OPT_DEPTH] = { 1 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000332 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000333
334 while (*command) {
335 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000336 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000337 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000338 command++;
339 break;
340 case '\\':
Denys Vlasenko3f210442012-11-09 17:16:40 +0100341 if (command[1])
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342 command++;
Denys Vlasenko3f210442012-11-09 17:16:40 +0100343 addstr(&result, command, 1);
344 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000345 break;
346 case '[':
347 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000348 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000349 okay[opt_depth] = 1;
350 opt_depth++;
351 command += 2;
352 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100353 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000354 command++;
355 }
356 break;
357 case ']':
358 if (command[1] == ']' && opt_depth > 1) {
359 opt_depth--;
360 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000361 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000363 command += 2;
364 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100365 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000366 command++;
367 }
368 break;
369 case '%':
370 {
371 char *nextpercent;
372 char *varvalue;
373
374 command++;
375 nextpercent = strchr(command, '%');
376 if (!nextpercent) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100377 /* Unterminated %var% */
Denis Vlasenko05341252006-09-26 20:35:30 +0000378 free(result);
379 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000380 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000381
382 varvalue = get_var(command, nextpercent - command, ifd);
383
384 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200385# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000386 /* "hwaddress <class> <address>":
387 * unlike ifconfig, ip doesnt want <class>
388 * (usually "ether" keyword). Skip it. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100389 if (is_prefixed_with(command, "hwaddress")) {
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000390 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
391 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200392# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000393 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000394 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200395# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000396 /* Sigh... Add a special case for 'ip' to convert from
397 * dotted quad to bit count style netmasks. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100398 if (is_prefixed_with(command, "bnmask")) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000399 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000400 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000401 if (varvalue) {
402 res = count_netmask_bits(varvalue);
403 if (res > 0) {
404 const char *argument = utoa(res);
405 addstr(&result, argument, strlen(argument));
406 command = nextpercent + 1;
407 break;
408 }
Eric Andersen8320b422003-04-02 10:13:26 +0000409 }
Eric Andersen8320b422003-04-02 10:13:26 +0000410 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200411# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000412 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000413 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000414
415 command = nextpercent + 1;
416 }
417 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418 }
419 }
420
421 if (opt_depth > 1) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100422 /* Unbalanced bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000423 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000424 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000425 }
426
427 if (!okay[0]) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100428 /* Undefined variable and we aren't in a bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000430 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431 }
432
Denis Vlasenko05341252006-09-26 20:35:30 +0000433 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434}
435
Denis Vlasenko05341252006-09-26 20:35:30 +0000436/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000437static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000438{
439 char *out;
440 int ret;
441
442 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000443 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000444 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000445 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000447 /* out == "": parsed ok but not all needed variables known, skip */
448 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000449
450 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000451 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000452 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000453 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000454 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000455}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200456
457#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
458
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000459
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000460#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200461
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200462static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000463{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200464# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000465 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000466 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000467 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000468 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200469# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000470 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200471# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472}
473
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200474static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200476# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000477 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200478# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000479 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200480# endif
481}
482
483static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
484{
485 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000486}
487
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200488static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000489{
Eric Andersen8320b422003-04-02 10:13:26 +0000490 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200491# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000492 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000493 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100494 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
495 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]][[ metric %metric%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200496# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000497 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000498 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000499 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200500# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000501 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000502}
503
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200504static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000505{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200506# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000507 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200508# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000509 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200510# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000511}
512
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200513# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200514static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515{
Eric Andersen8320b422003-04-02 10:13:26 +0000516 int result;
517 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000518 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000519 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000520 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100521 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
522 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000523 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000524}
525
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200526static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527{
Denis Vlasenko05341252006-09-26 20:35:30 +0000528 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200530# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000531
Denis Vlasenko05341252006-09-26 20:35:30 +0000532static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200533# if ENABLE_FEATURE_IFUPDOWN_IP
534 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
535# endif
536 { "static" , static_up6 , static_down6 , },
537 { "manual" , manual_up_down6 , manual_up_down6 , },
538 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539};
540
Denis Vlasenko05341252006-09-26 20:35:30 +0000541static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000542 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000543 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000544 methods6
545};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200546
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000547#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200549
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000550#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200551
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200552static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200554# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000555 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000556 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000557 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000558 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200559# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000560 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200561# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562}
563
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200564static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200566# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000567 int result;
568 result = execute("ip addr flush dev %iface%", ifd, exec);
569 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000570 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200571# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000572 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200573# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000574}
575
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200576static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577{
Eric Andersen8320b422003-04-02 10:13:26 +0000578 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200579# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000580 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
581 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000582 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Alex Dowad65c501a2015-08-07 11:53:26 +0200583 result += execute("[[ip route add default via %gateway% dev %iface%[[ metric %metric%]]]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000584 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200585# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000586 /* ifconfig said to set iface up before it processes hw %hwaddress%,
587 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000588 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000589 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000590 result += execute("ifconfig %iface% %address% netmask %netmask%"
Denys Vlasenko92d98f02016-09-20 17:31:11 +0200591 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]]",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000592 ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000593 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000594 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200595# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596}
597
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200598static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599{
Eric Andersen8320b422003-04-02 10:13:26 +0000600 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200601# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenko92d98f02016-09-20 17:31:11 +0200602 /* Optional "label LBL" is necessary if interface is an alias (eth0:0),
603 * otherwise "ip addr flush dev eth0:0" flushes all addresses on eth0.
604 */
605 result = execute("ip addr flush dev %iface%[[ label %label%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000606 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200607# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000608 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
609 /* Bringing the interface down deletes the routes in itself.
610 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
611 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000612 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200613# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000614 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615}
616
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200617# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200618struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000619 const char *name;
620 const char *startcmd;
621 const char *stopcmd;
622};
623
624static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000625 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200626 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000627 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000628 },
629 { "dhclient",
630 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
631 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
632 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000633 { "pump",
634 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
635 "pump -i %iface% -k",
636 },
637 { "udhcpc",
Natanael Copa10623912012-09-19 16:55:08 +0200638 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -x hostname:%hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000639 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000640 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000641 },
642};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200643# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000644
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200645# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200646static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000647{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000648 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200649# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000650 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000651 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000652 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200653# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000654 /* needed if we have hwaddress on dhcp iface */
655 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000656 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200657# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000658 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200659 if (executable_exists(ext_dhcp_clients[i].name))
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000660 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
661 }
662 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000663 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000664}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200665# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200666static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000667{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200668# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000669 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000670 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000671 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200672# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000673 /* needed if we have hwaddress on dhcp iface */
674 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000675 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200676# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000677 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Natanael Copa10623912012-09-19 16:55:08 +0200678 "-i %iface%[[ -x hostname:%hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000679 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000680}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200681# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200682static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000683 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000684{
685 return 0; /* no dhcp support */
686}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200687# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200689# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200690static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000691{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000692 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000693 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000694
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000695 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200696 if (executable_exists(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200697 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000698 if (result)
699 break;
700 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000701 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000702
703 if (!result)
704 bb_error_msg("warning: no dhcp clients found and stopped");
705
706 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
707 and it may come back up because udhcpc is still shutting down */
708 usleep(100000);
709 result += static_down(ifd, exec);
710 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000711}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200712# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200713static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000714{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000715 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200716 result = execute(
717 "test -f /var/run/udhcpc.%iface%.pid && "
718 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
719 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000720 /* Also bring the hardware interface down since
721 killing the dhcp client alone doesn't do it.
722 This enables consecutive ifup->ifdown->ifup */
723 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
724 and it may come back up because udhcpc is still shutting down */
725 usleep(100000);
726 result += static_down(ifd, exec);
727 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200729# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200730static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000731 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000732{
733 return 0; /* no dhcp support */
734}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200735# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000736
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200737static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000738{
739 return 1;
740}
741
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200742static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000743{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000744 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000745 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
746 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000747}
748
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200749static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000750{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000751 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000752}
753
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200754static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000755{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000756 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000757}
758
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200759static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000761 return execute("start-stop-daemon --start -x wvdial "
762 "-p /var/run/wvdial.%iface% -b -m --[[ %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_down(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 --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000768 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000769}
770
Denis Vlasenko05341252006-09-26 20:35:30 +0000771static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200772 { "manual" , manual_up_down, manual_up_down, },
773 { "wvdial" , wvdial_up , wvdial_down , },
774 { "ppp" , ppp_up , ppp_down , },
775 { "static" , static_up , static_down , },
776 { "bootp" , bootp_up , static_down , },
777 { "dhcp" , dhcp_up , dhcp_down , },
778 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000779};
780
Denis Vlasenko05341252006-09-26 20:35:30 +0000781static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000782 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000783 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000784 methods
785};
786
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200787#endif /* FEATURE_IFUPDOWN_IPV4 */
788
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100789static int FAST_FUNC link_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
790{
791 return 1;
792}
793
794static const struct method_t link_methods[] = {
795 { "none", link_up_down, link_up_down }
796};
797
798static const struct address_family_t addr_link = {
799 "link", ARRAY_SIZE(link_methods), link_methods
800};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000801
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200802/* Returns pointer to the next word, or NULL.
803 * In 1st case, advances *buf to the word after this one.
804 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000805static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000807 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000808 char *word;
809
Glenn L McGrath85737042003-01-14 23:26:57 +0000810 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000811 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000812
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000813 /* Stop on EOL */
814 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000815 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000816
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000817 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000818 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000819
820 /* Unless we are already at NUL, store NUL and advance */
821 if (word[length] != '\0')
822 word[length++] = '\0';
823
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200824 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000825
826 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000827}
828
Denis Vlasenko05341252006-09-26 20:35:30 +0000829static 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 +0000830{
831 int i;
832
Denis Vlasenko736230e2006-11-20 19:40:36 +0000833 if (!name)
834 return NULL;
835
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836 for (i = 0; af[i]; i++) {
837 if (strcmp(af[i]->name, name) == 0) {
838 return af[i];
839 }
840 }
841 return NULL;
842}
843
Denis Vlasenko05341252006-09-26 20:35:30 +0000844static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000845{
846 int i;
847
Denis Vlasenko736230e2006-11-20 19:40:36 +0000848 if (!name)
849 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000850 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000851 for (i = 0; i < af->n_methods; i++) {
852 if (strcmp(af->method[i].name, name) == 0) {
853 return &af->method[i];
854 }
855 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000856 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857}
858
Denys Vlasenkofb036632013-02-28 19:01:28 +0100859static struct interfaces_file_t *read_interfaces(const char *filename, struct interfaces_file_t *defn)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000860{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000861 /* Let's try to be compatible.
862 *
863 * "man 5 interfaces" says:
864 * Lines starting with "#" are ignored. Note that end-of-line
865 * comments are NOT supported, comments must be on a line of their own.
866 * A line may be extended across multiple lines by making
867 * the last character a backslash.
868 *
869 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000870 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000871 * be ignored. Blank lines are ignored. Lines may be indented freely.
872 * A "\" character at the very end of the line indicates the next line
873 * should be treated as a continuation of the current one.
Denys Vlasenkofb036632013-02-28 19:01:28 +0100874 *
875 * Lines beginning with "source" are used to include stanzas from
876 * other files, so configuration can be split into many files.
877 * The word "source" is followed by the path of file to be sourced.
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000878 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000879#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000880 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000881#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000882 struct interface_defn_t *currif = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000884 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000885 char *first_word;
886 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000887 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
888
Denys Vlasenkofb036632013-02-28 19:01:28 +0100889 if (!defn)
890 defn = xzalloc(sizeof(*defn));
891
892 debug_noise("reading %s file:\n", filename);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000893 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000894
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000895 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000896#if ENABLE_DESKTOP
897 /* Trailing "\" concatenates lines */
898 char *p;
899 while ((p = last_char_is(buf, '\\')) != NULL) {
900 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000901 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000902 if (!rest_of_line)
903 break;
904 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000905 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000906 free(rest_of_line);
907 buf = p;
908 }
909#endif
910 rest_of_line = buf;
911 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000912 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000913 free(buf);
914 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000915 }
916
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000917 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000918#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000919 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000920
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000921 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000922 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000923 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000925 /*currmap->n_mappings = 0;*/
926 /*currmap->mapping = NULL;*/
927 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000928 {
Eric Andersen8320b422003-04-02 10:13:26 +0000929 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930 while (*where != NULL) {
931 where = &(*where)->next;
932 }
933 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000934 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935 }
Eric Andersen8320b422003-04-02 10:13:26 +0000936 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000937#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000939 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000940 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000941#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000942 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000944#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000945 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000946#endif
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100947 &addr_link,
Denis Vlasenko05341252006-09-26 20:35:30 +0000948 NULL
949 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000950 char *iface_name;
951 char *address_family_name;
952 char *method_name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000953
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000954 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000955 iface_name = next_word(&rest_of_line);
956 address_family_name = next_word(&rest_of_line);
957 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000959 if (method_name == NULL)
960 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000961
962 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000963 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000964
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000965 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000966 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000967
968 currif->iface = xstrdup(iface_name);
969
970 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000971 if (!currif->address_family)
972 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000973
974 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000975 if (!currif->method)
976 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Nicolas Cavallarie5aba882016-03-01 18:59:08 +0100977#if 0
978// Allegedly, Debian allows a duplicate definition:
979// iface eth0 inet static
980// address 192.168.0.15
981// netmask 255.255.0.0
982// gateway 192.168.0.1
983//
984// iface eth0 inet static
985// address 10.0.0.1
986// netmask 255.255.255.0
987//
988// This adds *two* addresses to eth0 (probably requires use of "ip", not "ifconfig"
989//
Denys Vlasenko35e063e2016-03-03 02:19:16 +0100990 llist_t *iface_list;
Denis Vlasenko05341252006-09-26 20:35:30 +0000991 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
992 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000993 if ((strcmp(tmp->iface, currif->iface) == 0)
994 && (tmp->address_family == currif->address_family)
995 ) {
996 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000997 }
998 }
Nicolas Cavallarie5aba882016-03-01 18:59:08 +0100999#endif
Denis Vlasenko05341252006-09-26 20:35:30 +00001000 llist_add_to_end(&(defn->ifaces), (char*)currif);
1001
1002 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001003 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001004 } else if (strcmp(first_word, "auto") == 0) {
1005 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001006
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001007 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +00001008 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001009 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001010 }
1011
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001012 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001013 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
1014 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001015 }
1016 currently_processing = NONE;
Denys Vlasenkofb036632013-02-28 19:01:28 +01001017 } else if (strcmp(first_word, "source") == 0) {
1018 read_interfaces(next_word(&rest_of_line), defn);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001019 } else {
1020 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001021 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001022 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001023 bb_error_msg_and_die("option with empty value \"%s\"", buf);
1024
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001025 if (strcmp(first_word, "post-up") == 0)
1026 first_word += 5; /* "up" */
1027 else if (strcmp(first_word, "pre-down") == 0)
1028 first_word += 4; /* "down" */
1029
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001030 /* If not one of "up", "down",... words... */
1031 if (index_in_strings(keywords_up_down, first_word) < 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001032 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001033 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001034 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001035 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +00001036 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001037 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001038 debug_noise("\t%s=%s\n", first_word, rest_of_line);
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001039 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001040 currif->option[currif->n_options].name = xstrdup(first_word);
1041 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +00001042 currif->n_options++;
1043 break;
1044 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001045#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001046 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001047 if (currmap->script != NULL)
1048 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001049 currmap->script = xstrdup(next_word(&rest_of_line));
1050 } else if (strcmp(first_word, "map") == 0) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001051 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001052 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +00001053 currmap->n_mappings++;
1054 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001055 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +00001056 }
1057#endif
1058 break;
1059 case NONE:
1060 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001061 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062 }
1063 }
Glenn L McGrath85737042003-01-14 23:26:57 +00001064 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001065 } /* while (fgets) */
1066
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +00001068 /* ferror does NOT set errno! */
1069 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070 }
1071 fclose(f);
Denys Vlasenkofb036632013-02-28 19:01:28 +01001072 debug_noise("\ndone reading %s\n\n", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073
1074 return defn;
1075}
1076
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001077static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078{
1079 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +02001080 char *dst;
1081 char *src;
1082 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001083
Rob Landleyd921b2e2006-08-03 15:41:12 +00001084 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085
Denys Vlasenko62152da2009-06-05 21:53:11 +02001086 for (dst = src = result; (c = *src) != '=' && c; src++) {
1087 if (c == '-')
1088 c = '_';
1089 if (c >= 'a' && c <= 'z')
1090 c -= ('a' - 'A');
1091 if (isalnum(c) || c == '_')
1092 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001093 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001094 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001095
1096 return result;
1097}
1098
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001099static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001101 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +02001102 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001103
Denys Vlasenko62152da2009-06-05 21:53:11 +02001104 if (G.my_environ != NULL) {
1105 for (pp = G.my_environ; *pp; pp++) {
1106 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001108 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001110
1111 /* note: last element will stay NULL: */
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001112 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
Denys Vlasenko62152da2009-06-05 21:53:11 +02001113 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114
1115 for (i = 0; i < iface->n_options; i++) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001116 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117 continue;
1118 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001119 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001120 }
1121
Denys Vlasenko62152da2009-06-05 21:53:11 +02001122 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
1123 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
1124 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
1125 *pp++ = setlocalenv("%s=%s", "MODE", mode);
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001126 *pp++ = setlocalenv("%s=%s", "PHASE", opt);
Denys Vlasenko62152da2009-06-05 21:53:11 +02001127 if (G.startup_PATH)
1128 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129}
1130
1131static int doit(char *str)
1132{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001133 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001134 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001136 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137 pid_t child;
1138 int status;
1139
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001140 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001141 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001142 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001143 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001144 if (child == 0) { /* child */
1145 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001146 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001148 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1150 return 0;
1151 }
1152 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001153 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154}
1155
Rob Landleye813ddb2006-02-28 03:53:14 +00001156static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001157{
1158 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001159 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160 for (i = 0; i < ifd->n_options; i++) {
1161 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001162 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001163 return 0;
1164 }
1165 }
1166 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001167
Denys Vlasenkobcf47ea2016-09-25 02:05:24 +02001168 /* Tested on Debian Squeeze: "standard" ifup runs this without
1169 * checking that directory exists. If it doesn't, run-parts
1170 * complains, and this message _is_ annoyingly visible.
1171 * Don't "fix" this (unless newer Debian does).
1172 */
Rob Landleyd921b2e2006-08-03 15:41:12 +00001173 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001174 /* heh, we don't bother free'ing it */
1175 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176}
1177
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001178static int check(char *str)
1179{
Eric Andersen8320b422003-04-02 10:13:26 +00001180 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001181}
1182
Eric Andersen8320b422003-04-02 10:13:26 +00001183static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001185 if (!iface->method->up(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001186 set_environ(iface, "start", "pre-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001187 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001188 if (!iface->method->up(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001189 set_environ(iface, "start", "post-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001190 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001191 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001192}
1193
1194static int iface_down(struct interface_defn_t *iface)
1195{
Denys Vlasenkoa0fd4a72012-09-02 14:29:14 +02001196 if (!iface->method->down(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001197 set_environ(iface, "stop", "pre-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001198 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001199 if (!iface->method->down(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001200 set_environ(iface, "stop", "post-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001201 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001202 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203}
1204
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001205#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001206static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001208 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001209 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210 pid_t pid;
1211
Denis Vlasenko37188322008-02-16 13:20:56 +00001212 xpiped_pair(infd);
1213 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001214
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001215 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001216 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001217
Pascal Bellard926031b2010-07-04 15:32:38 +02001218 if (pid == 0) {
1219 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001220 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001221 close(infd.wr);
1222 close(outfd.rd);
1223 xmove_fd(infd.rd, 0);
1224 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001225 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001226 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001227 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001228 close(infd.rd);
1229 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001230 *in = xfdopen_for_write(infd.wr);
1231 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001232 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233}
1234
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001235static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001236{
1237 FILE *in, *out;
1238 int i, status;
1239 pid_t pid;
1240
Rob Landleyd921b2e2006-08-03 15:41:12 +00001241 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001242
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001243 /* Run the mapping script. Never fails. */
1244 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001245
1246 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247 for (i = 0; i < map->n_mappings; i++) {
1248 fprintf(in, "%s\n", map->mapping[i]);
1249 }
1250 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001251 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001252
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001253 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001254 /* If the mapping script exited successfully, try to
1255 * grab a line of output and use that as the name of the
1256 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001257 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001259 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001260 /* If we are able to read a line of output from the script,
1261 * remove any trailing whitespace and use this value
1262 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001263 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001264
1265 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001267
1268 free(logical);
1269 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270 }
1271 }
Eric Andersen8a931792003-07-03 10:20:29 +00001272
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273 fclose(out);
1274
Eric Andersen8a931792003-07-03 10:20:29 +00001275 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001277#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001278
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001279static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001280{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001281 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001283 while (search) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001284 char *after_iface = is_prefixed_with(search->data, iface);
1285 if (after_iface
1286 && *after_iface == '='
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001287 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001288 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001289 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001290 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001291 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001292 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293}
1294
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001295/* read the previous state from the state file */
1296static llist_t *read_iface_state(void)
1297{
1298 llist_t *state_list = NULL;
Denys Vlasenko3720a612016-10-09 23:04:16 +02001299 FILE *state_fp = fopen_for_read(IFSTATE_FILE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001300
1301 if (state_fp) {
1302 char *start, *end_ptr;
1303 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1304 /* We should only need to check for a single character */
1305 end_ptr = start + strcspn(start, " \t\n");
1306 *end_ptr = '\0';
1307 llist_add_to(&state_list, start);
1308 }
1309 fclose(state_fp);
1310 }
1311 return state_list;
1312}
1313
Denys Vlasenko3720a612016-10-09 23:04:16 +02001314/* read the previous state from the state file */
1315static FILE *open_new_state_file(void)
1316{
1317 int fd, flags, cnt;
1318
1319 cnt = 0;
1320 flags = (O_WRONLY | O_CREAT | O_EXCL);
1321 for (;;) {
1322 fd = open(IFSTATE_FILE_PATH".new", flags, 0666);
1323 if (fd >= 0)
1324 break;
1325 if (errno != EEXIST
1326 || flags == (O_WRONLY | O_CREAT | O_TRUNC)
1327 ) {
1328 bb_perror_msg_and_die("can't open '%s'",
1329 IFSTATE_FILE_PATH".new");
1330 }
1331 /* Someone else created the .new file */
1332 if (cnt > 30 * 1000) {
1333 /* Waited for 30*30/2 = 450 milliseconds, still EEXIST.
1334 * Assuming a stale file, rewriting it.
1335 */
1336 flags = (O_WRONLY | O_CREAT | O_TRUNC);
1337 continue;
1338 }
1339 usleep(cnt);
1340 cnt += 1000;
1341 }
1342
1343 return xfdopen_for_write(fd);
1344}
1345
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001346int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001347int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001348{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001349 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001350 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001351 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001352 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001353 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001354
Denys Vlasenko62152da2009-06-05 21:53:11 +02001355 INIT_G();
1356
1357 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001358 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001359
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +01001360 if (ENABLE_IFUP
1361 && (!ENABLE_IFDOWN || applet_name[2] == 'u')
1362 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001363 /* ifup command */
1364 cmds = iface_up;
Denys Vlasenkoa8c696b2016-11-14 18:31:07 +01001365 } else {
1366 cmds = iface_down;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001367 }
1368
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001369 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001370 argv += optind;
1371 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001372 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001373 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001374 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001375 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001376
Denys Vlasenkofb036632013-02-28 19:01:28 +01001377 defn = read_interfaces(interfaces, NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001378
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001379 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001380 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001381 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001382 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001383 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001384 }
1385
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001386 /* Update the interfaces */
1387 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001388 llist_t *iface_list;
1389 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001390 char *iface;
1391 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001392 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001393 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001394 int cmds_ret;
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001395 bool curr_failure = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001396
Rob Landleyd921b2e2006-08-03 15:41:12 +00001397 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001398 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001399
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001400 pch = strchr(iface, '=');
1401 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001402 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001403 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001404 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001405 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001406 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001407
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001408 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001409 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001410 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001411
1412 if (cmds == iface_up) {
1413 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001414 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001415 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001416 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001417 }
1418 } else {
1419 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001420 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001421 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001422 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001423 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001424 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001425 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001426 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001427
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001428#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001429 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001430 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001431
1432 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001433 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001434 for (i = 0; i < currmap->n_matches; i++) {
1435 if (fnmatch(currmap->match[i], liface, 0) != 0)
1436 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001437 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001438 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001439 }
Eric Andersen8a931792003-07-03 10:20:29 +00001440 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001441 break;
1442 }
1443 }
1444 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001445#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001446
Eric Andersen8320b422003-04-02 10:13:26 +00001447 iface_list = defn->ifaces;
1448 while (iface_list) {
1449 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001450 if (strcmp(liface, currif->iface) == 0) {
1451 char *oldiface = currif->iface;
1452
1453 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001454 currif->iface = iface;
1455
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001456 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001457
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001458 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001459 cmds_ret = cmds(currif);
1460 if (cmds_ret == -1) {
Denys Vlasenko6d8ea1d2015-03-22 17:08:51 +01001461 bb_error_msg("don't have all variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001462 liface, currif->address_family->name);
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001463 any_failures = curr_failure = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001464 } else if (cmds_ret == 0) {
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001465 any_failures = curr_failure = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001466 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001467
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001468 currif->iface = oldiface;
1469 }
Eric Andersen8320b422003-04-02 10:13:26 +00001470 iface_list = iface_list->link;
1471 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001472 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001473 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001474 }
1475
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001476 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001477 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001478 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001479 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001480 /* update the state file */
Denys Vlasenko3720a612016-10-09 23:04:16 +02001481 FILE *new_state_fp = open_new_state_file();
Denis Vlasenkof92df582007-05-02 22:22:23 +00001482 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001483 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001484 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001485
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001486 if (cmds == iface_up && !curr_failure) {
Denys Vlasenkod6f50002011-12-15 12:39:25 +01001487 char *newiface = xasprintf("%s=%s", iface, liface);
1488 if (!iface_state) {
Rob Landley8bb50782006-05-26 23:44:51 +00001489 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001490 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001491 free(iface_state->data);
1492 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001493 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001494 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001495 /* Remove an interface from state_list */
1496 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001497 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001498 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001499
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001500 /* Actually write the new state */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001501 state = state_list;
1502 while (state) {
1503 if (state->data) {
Denys Vlasenko3720a612016-10-09 23:04:16 +02001504 fprintf(new_state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001505 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001506 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001507 }
Denys Vlasenko3720a612016-10-09 23:04:16 +02001508 fclose(new_state_fp);
1509 xrename(IFSTATE_FILE_PATH".new", IFSTATE_FILE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001510 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001511 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001512 next:
1513 free(iface);
1514 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001515 }
1516
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001517 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001518}