blob: d477ff6d11d8494d0c5e4a9e8c8230b7fe1aaeb4 [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/*
3 * ifupdown 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
Pere Orga5bc8c002011-04-11 03:29:49 +020020//usage:#define ifup_trivial_usage
21//usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
22//usage:#define ifup_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020023//usage: " -a De/configure all interfaces automatically"
Pere Orga5bc8c002011-04-11 03:29:49 +020024//usage: "\n -i FILE Use FILE for interface definitions"
25//usage: "\n -n Print out what would happen, but don't do it"
26//usage: IF_FEATURE_IFUPDOWN_MAPPING(
27//usage: "\n (note: doesn't disable mappings)"
28//usage: "\n -m Don't run any mappings"
29//usage: )
30//usage: "\n -v Print out what would happen before doing it"
31//usage: "\n -f Force de/configuration"
32//usage:
33//usage:#define ifdown_trivial_usage
34//usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
35//usage:#define ifdown_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020036//usage: " -a De/configure all interfaces automatically"
Pere Orga5bc8c002011-04-11 03:29:49 +020037//usage: "\n -i FILE Use FILE for interface definitions"
38//usage: "\n -n Print out what would happen, but don't do it"
39//usage: IF_FEATURE_IFUPDOWN_MAPPING(
40//usage: "\n (note: doesn't disable mappings)"
41//usage: "\n -m Don't run any mappings"
42//usage: )
43//usage: "\n -v Print out what would happen before doing it"
44//usage: "\n -f Force de/configuration"
45
Denys Vlasenko043b1e52009-09-06 12:47:55 +020046#include "libbb.h"
47/* After libbb.h, since it needs sys/types.h on some systems */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000049#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000050
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000051#define MAX_OPT_DEPTH 10
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000052
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000053#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000054#define MAX_INTERFACE_LENGTH 10
55#endif
Eric Andersen8320b422003-04-02 10:13:26 +000056
Wade Berrier142c5cb2008-11-14 21:18:45 +000057#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
58
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000059#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000060
61/* Forward declaration */
62struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000063
Denis Vlasenko097c3242006-11-27 16:59:15 +000064typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000065
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000066struct method_t {
67 const char *name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020068 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
69 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
Eric Andersen8320b422003-04-02 10:13:26 +000070};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000071
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000072struct address_family_t {
73 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000074 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000075 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000076};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000078struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000079 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080
81 int max_matches;
82 int n_matches;
83 char **match;
84
85 char *script;
86
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000087 int n_mappings;
88 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000089};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000090
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000091struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000092 char *name;
93 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000094};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000095
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000096struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000097 const struct address_family_t *address_family;
98 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000099
Rob Landleye813ddb2006-02-28 03:53:14 +0000100 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000102 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000103};
104
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000105struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000106 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000107 llist_t *ifaces;
108 struct mapping_defn_t *mappings;
109};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000110
Denys Vlasenko62152da2009-06-05 21:53:11 +0200111
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000112#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000113enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200114 OPT_do_all = 0x1,
115 OPT_no_act = 0x2,
116 OPT_verbose = 0x4,
117 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000118 OPT_no_mappings = 0x10,
119};
Denys Vlasenko62152da2009-06-05 21:53:11 +0200120#define DO_ALL (option_mask32 & OPT_do_all)
121#define NO_ACT (option_mask32 & OPT_no_act)
122#define VERBOSE (option_mask32 & OPT_verbose)
123#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000124#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000125
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000126
Denys Vlasenko62152da2009-06-05 21:53:11 +0200127struct globals {
128 char **my_environ;
129 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100130 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100131} FIX_ALIASING;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200132#define G (*(struct globals*)&bb_common_bufsiz1)
133#define INIT_G() do { } while (0)
134
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000135
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200136static const char keywords_up_down[] ALIGN1 =
137 "up\0"
138 "down\0"
139 "pre-up\0"
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200140 "post-down\0"
141;
142
143
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000144#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
145
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000146static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000147{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000148 /* xasprintf trick will be smaller, but we are often
149 * called with str_length == 1 - don't want to have
150 * THAT much of malloc/freeing! */
151 char *buf = *bufp;
152 int len = (buf ? strlen(buf) : 0);
153 str_length++;
154 buf = xrealloc(buf, len + str_length);
155 /* copies at most str_length-1 chars! */
156 safe_strncpy(buf + len, str, str_length);
157 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000158}
159
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000160static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000161{
162 int i = strncmp(l, r, llen);
163
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000164 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200165 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000166 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000167}
168
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000169static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000170{
171 int i;
172
173 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200174 // ubuntu's ifup doesn't do this:
175 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000176 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200177 //free(label_buf);
178 //label_buf = xstrdup(ifd->iface);
179 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000180 //result = strchrnul(label_buf, ':');
181 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200182 //return label_buf;
183
184 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000185 }
186 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000187 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000188 }
189 for (i = 0; i < ifd->n_options; i++) {
190 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
191 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192 }
193 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000194 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000195}
196
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200197# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000198static int count_netmask_bits(const char *dotted_quad)
199{
200// int result;
201// unsigned a, b, c, d;
202// /* Found a netmask... Check if it is dotted quad */
203// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
204// return -1;
205// if ((a|b|c|d) >> 8)
206// return -1; /* one of numbers is >= 256 */
207// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
208// d = ~d; /* 11110000 -> 00001111 */
209
210 /* Shorter version */
211 int result;
212 struct in_addr ip;
213 unsigned d;
214
215 if (inet_aton(dotted_quad, &ip) == 0)
216 return -1; /* malformed dotted IP */
217 d = ntohl(ip.s_addr); /* IP in host order */
218 d = ~d; /* 11110000 -> 00001111 */
219 if (d & (d+1)) /* check that it is in 00001111 form */
220 return -1; /* no it is not */
221 result = 32;
222 while (d) {
223 d >>= 1;
224 result--;
225 }
226 return result;
227}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200228# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000229
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000230static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000231{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000232 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
Denys Vlasenko3f210442012-11-09 17:16:40 +0100233 smallint okay[MAX_OPT_DEPTH] = { 1 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000234 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000235 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000236
237 while (*command) {
238 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000239 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000240 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000241 command++;
242 break;
243 case '\\':
Denys Vlasenko3f210442012-11-09 17:16:40 +0100244 if (command[1])
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000245 command++;
Denys Vlasenko3f210442012-11-09 17:16:40 +0100246 addstr(&result, command, 1);
247 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000248 break;
249 case '[':
250 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000251 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000252 okay[opt_depth] = 1;
253 opt_depth++;
254 command += 2;
255 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100256 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000257 command++;
258 }
259 break;
260 case ']':
261 if (command[1] == ']' && opt_depth > 1) {
262 opt_depth--;
263 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000264 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000265 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000266 command += 2;
267 } else {
Denys Vlasenko11181332012-11-05 13:22:56 +0100268 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000269 command++;
270 }
271 break;
272 case '%':
273 {
274 char *nextpercent;
275 char *varvalue;
276
277 command++;
278 nextpercent = strchr(command, '%');
279 if (!nextpercent) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100280 /* Unterminated %var% */
Denis Vlasenko05341252006-09-26 20:35:30 +0000281 free(result);
282 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000283 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000284
285 varvalue = get_var(command, nextpercent - command, ifd);
286
287 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200288# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000289 /* "hwaddress <class> <address>":
290 * unlike ifconfig, ip doesnt want <class>
291 * (usually "ether" keyword). Skip it. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100292 if (is_prefixed_with(command, "hwaddress")) {
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000293 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
294 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200295# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000296 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000297 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200298# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000299 /* Sigh... Add a special case for 'ip' to convert from
300 * dotted quad to bit count style netmasks. */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100301 if (is_prefixed_with(command, "bnmask")) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000302 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000303 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000304 if (varvalue) {
305 res = count_netmask_bits(varvalue);
306 if (res > 0) {
307 const char *argument = utoa(res);
308 addstr(&result, argument, strlen(argument));
309 command = nextpercent + 1;
310 break;
311 }
Eric Andersen8320b422003-04-02 10:13:26 +0000312 }
Eric Andersen8320b422003-04-02 10:13:26 +0000313 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200314# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000315 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000316 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000317
318 command = nextpercent + 1;
319 }
320 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000321 }
322 }
323
324 if (opt_depth > 1) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100325 /* Unbalanced bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000326 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000327 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000328 }
329
330 if (!okay[0]) {
Denys Vlasenkoa803bfa2012-11-05 14:18:36 +0100331 /* Undefined variable and we aren't in a bracket */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000333 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000334 }
335
Denis Vlasenko05341252006-09-26 20:35:30 +0000336 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000337}
338
Denis Vlasenko05341252006-09-26 20:35:30 +0000339/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000340static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000341{
342 char *out;
343 int ret;
344
345 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000346 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000347 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000348 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000350 /* out == "": parsed ok but not all needed variables known, skip */
351 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352
353 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000354 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000355 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000356 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000357 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000358}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200359
360#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
361
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000363#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200364
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200365static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200367# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000368 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000369 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000370 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000371 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200372# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000373 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200374# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375}
376
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200377static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200379# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000380 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200381# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000382 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200383# endif
384}
385
386static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
387{
388 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000389}
390
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200391static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392{
Eric Andersen8320b422003-04-02 10:13:26 +0000393 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200394# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000395 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000396 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100397 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
398 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]][[ metric %metric%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200399# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000400 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000401 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000402 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200403# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000404 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000405}
406
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200407static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000408{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200409# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000410 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200411# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000412 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200413# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000414}
415
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200416# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200417static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418{
Eric Andersen8320b422003-04-02 10:13:26 +0000419 int result;
420 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000421 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000422 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000423 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Timo Teräs02852432015-10-26 17:06:12 +0100424 /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */
425 result += execute("[[ip route add ::/0 via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000426 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000427}
428
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200429static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000430{
Denis Vlasenko05341252006-09-26 20:35:30 +0000431 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000432}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200433# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434
Denis Vlasenko05341252006-09-26 20:35:30 +0000435static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200436# if ENABLE_FEATURE_IFUPDOWN_IP
437 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
438# endif
439 { "static" , static_up6 , static_down6 , },
440 { "manual" , manual_up_down6 , manual_up_down6 , },
441 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000442};
443
Denis Vlasenko05341252006-09-26 20:35:30 +0000444static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000445 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000446 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000447 methods6
448};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200449
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000450#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000451
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200452
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000453#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200454
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200455static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200457# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000458 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000459 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000460 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000461 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200462# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000463 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200464# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000465}
466
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200467static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000468{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200469# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000470 int result;
471 result = execute("ip addr flush dev %iface%", ifd, exec);
472 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000473 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200474# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000475 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200476# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000477}
478
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200479static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480{
Eric Andersen8320b422003-04-02 10:13:26 +0000481 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200482# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000483 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
484 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000485 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Alex Dowad65c501a2015-08-07 11:53:26 +0200486 result += execute("[[ip route add default via %gateway% dev %iface%[[ metric %metric%]]]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000487 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200488# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000489 /* ifconfig said to set iface up before it processes hw %hwaddress%,
490 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000491 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000492 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000493 result += execute("ifconfig %iface% %address% netmask %netmask%"
494 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000495 ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000496 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000497 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200498# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000499}
500
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200501static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000502{
Eric Andersen8320b422003-04-02 10:13:26 +0000503 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200504# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000505 result = execute("ip addr flush dev %iface%", ifd, exec);
506 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200507# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000508 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
509 /* Bringing the interface down deletes the routes in itself.
510 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
511 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000512 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200513# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000514 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515}
516
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200517# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200518struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000519 const char *name;
520 const char *startcmd;
521 const char *stopcmd;
522};
523
524static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000525 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200526 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000527 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000528 },
529 { "dhclient",
530 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
531 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
532 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000533 { "pump",
534 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
535 "pump -i %iface% -k",
536 },
537 { "udhcpc",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200538 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000539 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000540 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000541 },
542};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200543# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000544
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200545# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200546static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000547{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000548 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200549# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000550 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000551 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000552 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200553# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000554 /* needed if we have hwaddress on dhcp iface */
555 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000556 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200557# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000558 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200559 if (executable_exists(ext_dhcp_clients[i].name))
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000560 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
561 }
562 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000563 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000564}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200565# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200566static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000567{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200568# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000569 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000570 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000571 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200572# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000573 /* needed if we have hwaddress on dhcp iface */
574 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000575 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200576# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000577 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200578 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000579 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200581# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200582static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000583 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000584{
585 return 0; /* no dhcp support */
586}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200587# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000588
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200589# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200590static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000591{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000592 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000593 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000594
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000595 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denys Vlasenkoe765b5a2014-05-02 17:15:58 +0200596 if (executable_exists(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200597 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000598 if (result)
599 break;
600 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000601 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000602
603 if (!result)
604 bb_error_msg("warning: no dhcp clients found and stopped");
605
606 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
607 and it may come back up because udhcpc is still shutting down */
608 usleep(100000);
609 result += static_down(ifd, exec);
610 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000611}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200612# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200613static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000614{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000615 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200616 result = execute(
617 "test -f /var/run/udhcpc.%iface%.pid && "
618 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
619 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000620 /* Also bring the hardware interface down since
621 killing the dhcp client alone doesn't do it.
622 This enables consecutive ifup->ifdown->ifup */
623 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
624 and it may come back up because udhcpc is still shutting down */
625 usleep(100000);
626 result += static_down(ifd, exec);
627 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000628}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200629# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200630static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000631 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000632{
633 return 0; /* no dhcp support */
634}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200635# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000636
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200637static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000638{
639 return 1;
640}
641
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200642static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000643{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000644 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000645 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
646 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000647}
648
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200649static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000650{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000651 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000652}
653
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200654static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000655{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000656 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657}
658
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200659static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000661 return execute("start-stop-daemon --start -x wvdial "
662 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000663}
664
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200665static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000666{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000667 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000668 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669}
670
Denis Vlasenko05341252006-09-26 20:35:30 +0000671static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200672 { "manual" , manual_up_down, manual_up_down, },
673 { "wvdial" , wvdial_up , wvdial_down , },
674 { "ppp" , ppp_up , ppp_down , },
675 { "static" , static_up , static_down , },
676 { "bootp" , bootp_up , static_down , },
677 { "dhcp" , dhcp_up , dhcp_down , },
678 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000679};
680
Denis Vlasenko05341252006-09-26 20:35:30 +0000681static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000682 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000683 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000684 methods
685};
686
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200687#endif /* FEATURE_IFUPDOWN_IPV4 */
688
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100689static int FAST_FUNC link_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
690{
691 return 1;
692}
693
694static const struct method_t link_methods[] = {
695 { "none", link_up_down, link_up_down }
696};
697
698static const struct address_family_t addr_link = {
699 "link", ARRAY_SIZE(link_methods), link_methods
700};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000701
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200702/* Returns pointer to the next word, or NULL.
703 * In 1st case, advances *buf to the word after this one.
704 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000705static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000706{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000707 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000708 char *word;
709
Glenn L McGrath85737042003-01-14 23:26:57 +0000710 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000711 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000712
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000713 /* Stop on EOL */
714 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000715 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000716
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000717 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000718 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000719
720 /* Unless we are already at NUL, store NUL and advance */
721 if (word[length] != '\0')
722 word[length++] = '\0';
723
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200724 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000725
726 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000727}
728
Denis Vlasenko05341252006-09-26 20:35:30 +0000729static 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 +0000730{
731 int i;
732
Denis Vlasenko736230e2006-11-20 19:40:36 +0000733 if (!name)
734 return NULL;
735
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000736 for (i = 0; af[i]; i++) {
737 if (strcmp(af[i]->name, name) == 0) {
738 return af[i];
739 }
740 }
741 return NULL;
742}
743
Denis Vlasenko05341252006-09-26 20:35:30 +0000744static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745{
746 int i;
747
Denis Vlasenko736230e2006-11-20 19:40:36 +0000748 if (!name)
749 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000750 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751 for (i = 0; i < af->n_methods; i++) {
752 if (strcmp(af->method[i].name, name) == 0) {
753 return &af->method[i];
754 }
755 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000756 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000757}
758
Denys Vlasenkofb036632013-02-28 19:01:28 +0100759static struct interfaces_file_t *read_interfaces(const char *filename, struct interfaces_file_t *defn)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000761 /* Let's try to be compatible.
762 *
763 * "man 5 interfaces" says:
764 * Lines starting with "#" are ignored. Note that end-of-line
765 * comments are NOT supported, comments must be on a line of their own.
766 * A line may be extended across multiple lines by making
767 * the last character a backslash.
768 *
769 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000770 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000771 * be ignored. Blank lines are ignored. Lines may be indented freely.
772 * A "\" character at the very end of the line indicates the next line
773 * should be treated as a continuation of the current one.
Denys Vlasenkofb036632013-02-28 19:01:28 +0100774 *
775 * Lines beginning with "source" are used to include stanzas from
776 * other files, so configuration can be split into many files.
777 * The word "source" is followed by the path of file to be sourced.
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000778 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000779#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000780 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000781#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000782 struct interface_defn_t *currif = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000784 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000785 char *first_word;
786 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000787 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
788
Denys Vlasenkofb036632013-02-28 19:01:28 +0100789 if (!defn)
790 defn = xzalloc(sizeof(*defn));
791
792 debug_noise("reading %s file:\n", filename);
Denis Vlasenko5415c852008-07-21 23:05:26 +0000793 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000795 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000796#if ENABLE_DESKTOP
797 /* Trailing "\" concatenates lines */
798 char *p;
799 while ((p = last_char_is(buf, '\\')) != NULL) {
800 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000801 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000802 if (!rest_of_line)
803 break;
804 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000805 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000806 free(rest_of_line);
807 buf = p;
808 }
809#endif
810 rest_of_line = buf;
811 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000812 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000813 free(buf);
814 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 }
816
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000817 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000818#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000819 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000820
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000821 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000822 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000823 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000825 /*currmap->n_mappings = 0;*/
826 /*currmap->mapping = NULL;*/
827 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000828 {
Eric Andersen8320b422003-04-02 10:13:26 +0000829 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000830 while (*where != NULL) {
831 where = &(*where)->next;
832 }
833 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000834 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835 }
Eric Andersen8320b422003-04-02 10:13:26 +0000836 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000837#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000839 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000840 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000841#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000842 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000843#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000844#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000845 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000846#endif
Kaarle Ritvanenfe8390e2014-02-03 15:56:20 +0100847 &addr_link,
Denis Vlasenko05341252006-09-26 20:35:30 +0000848 NULL
849 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000850 char *iface_name;
851 char *address_family_name;
852 char *method_name;
853 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000854
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000855 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000856 iface_name = next_word(&rest_of_line);
857 address_family_name = next_word(&rest_of_line);
858 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000860 if (method_name == NULL)
861 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000862
863 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000864 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000865
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000866 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000867 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000868
869 currif->iface = xstrdup(iface_name);
870
871 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000872 if (!currif->address_family)
873 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000874
875 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000876 if (!currif->method)
877 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000878
879 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
880 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000881 if ((strcmp(tmp->iface, currif->iface) == 0)
882 && (tmp->address_family == currif->address_family)
883 ) {
884 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000885 }
886 }
887 llist_add_to_end(&(defn->ifaces), (char*)currif);
888
889 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000890 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000891 } else if (strcmp(first_word, "auto") == 0) {
892 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000894 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000895 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000896 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000897 }
898
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000899 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000900 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
901 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000902 }
903 currently_processing = NONE;
Denys Vlasenkofb036632013-02-28 19:01:28 +0100904 } else if (strcmp(first_word, "source") == 0) {
905 read_interfaces(next_word(&rest_of_line), defn);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906 } else {
907 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000908 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000909 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000910 bb_error_msg_and_die("option with empty value \"%s\"", buf);
911
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200912 if (strcmp(first_word, "post-up") == 0)
913 first_word += 5; /* "up" */
914 else if (strcmp(first_word, "pre-down") == 0)
915 first_word += 4; /* "down" */
916
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200917 /* If not one of "up", "down",... words... */
918 if (index_in_strings(keywords_up_down, first_word) < 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000919 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000920 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000921 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000922 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000923 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000924 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000925 debug_noise("\t%s=%s\n", first_word, rest_of_line);
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200926 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000927 currif->option[currif->n_options].name = xstrdup(first_word);
928 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000929 currif->n_options++;
930 break;
931 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000932#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000933 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000934 if (currmap->script != NULL)
935 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000936 currmap->script = xstrdup(next_word(&rest_of_line));
937 } else if (strcmp(first_word, "map") == 0) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200938 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000939 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000940 currmap->n_mappings++;
941 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000942 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000943 }
944#endif
945 break;
946 case NONE:
947 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000948 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949 }
950 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000951 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000952 } /* while (fgets) */
953
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000954 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000955 /* ferror does NOT set errno! */
956 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000957 }
958 fclose(f);
Denys Vlasenkofb036632013-02-28 19:01:28 +0100959 debug_noise("\ndone reading %s\n\n", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000960
961 return defn;
962}
963
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000964static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000965{
966 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200967 char *dst;
968 char *src;
969 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000970
Rob Landleyd921b2e2006-08-03 15:41:12 +0000971 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000972
Denys Vlasenko62152da2009-06-05 21:53:11 +0200973 for (dst = src = result; (c = *src) != '=' && c; src++) {
974 if (c == '-')
975 c = '_';
976 if (c >= 'a' && c <= 'z')
977 c -= ('a' - 'A');
978 if (isalnum(c) || c == '_')
979 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000980 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200981 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000982
983 return result;
984}
985
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200986static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000988 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200989 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000990
Denys Vlasenko62152da2009-06-05 21:53:11 +0200991 if (G.my_environ != NULL) {
992 for (pp = G.my_environ; *pp; pp++) {
993 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000994 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200995 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000996 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200997
998 /* note: last element will stay NULL: */
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200999 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
Denys Vlasenko62152da2009-06-05 21:53:11 +02001000 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001001
1002 for (i = 0; i < iface->n_options; i++) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +02001003 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001004 continue;
1005 }
Denys Vlasenko62152da2009-06-05 21:53:11 +02001006 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001007 }
1008
Denys Vlasenko62152da2009-06-05 21:53:11 +02001009 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
1010 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
1011 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
1012 *pp++ = setlocalenv("%s=%s", "MODE", mode);
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001013 *pp++ = setlocalenv("%s=%s", "PHASE", opt);
Denys Vlasenko62152da2009-06-05 21:53:11 +02001014 if (G.startup_PATH)
1015 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001016}
1017
1018static int doit(char *str)
1019{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001020 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001021 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001022 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001023 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001024 pid_t child;
1025 int status;
1026
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001027 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001028 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001029 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001030 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001031 if (child == 0) { /* child */
1032 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001033 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001034 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001035 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001036 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1037 return 0;
1038 }
1039 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001040 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041}
1042
Rob Landleye813ddb2006-02-28 03:53:14 +00001043static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001044{
1045 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001046 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047 for (i = 0; i < ifd->n_options; i++) {
1048 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001049 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050 return 0;
1051 }
1052 }
1053 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001054
Rob Landleyd921b2e2006-08-03 15:41:12 +00001055 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001056 /* heh, we don't bother free'ing it */
1057 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001058}
1059
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001060static int check(char *str)
1061{
Eric Andersen8320b422003-04-02 10:13:26 +00001062 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001063}
1064
Eric Andersen8320b422003-04-02 10:13:26 +00001065static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001066{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001067 if (!iface->method->up(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001068 set_environ(iface, "start", "pre-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001069 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001070 if (!iface->method->up(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001071 set_environ(iface, "start", "post-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001072 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001073 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001074}
1075
1076static int iface_down(struct interface_defn_t *iface)
1077{
Denys Vlasenkoa0fd4a72012-09-02 14:29:14 +02001078 if (!iface->method->down(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001079 set_environ(iface, "stop", "pre-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001080 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001081 if (!iface->method->down(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001082 set_environ(iface, "stop", "post-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001083 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001084 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085}
1086
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001087#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001088static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001089{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001090 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001091 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001092 pid_t pid;
1093
Denis Vlasenko37188322008-02-16 13:20:56 +00001094 xpiped_pair(infd);
1095 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001097 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001098 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001099
Pascal Bellard926031b2010-07-04 15:32:38 +02001100 if (pid == 0) {
1101 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001102 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001103 close(infd.wr);
1104 close(outfd.rd);
1105 xmove_fd(infd.rd, 0);
1106 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001107 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001108 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001109 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001110 close(infd.rd);
1111 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001112 *in = xfdopen_for_write(infd.wr);
1113 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001114 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115}
1116
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001117static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001118{
1119 FILE *in, *out;
1120 int i, status;
1121 pid_t pid;
1122
Rob Landleyd921b2e2006-08-03 15:41:12 +00001123 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001125 /* Run the mapping script. Never fails. */
1126 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001127
1128 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129 for (i = 0; i < map->n_mappings; i++) {
1130 fprintf(in, "%s\n", map->mapping[i]);
1131 }
1132 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001133 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001134
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001136 /* If the mapping script exited successfully, try to
1137 * grab a line of output and use that as the name of the
1138 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001139 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001140
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001141 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001142 /* If we are able to read a line of output from the script,
1143 * remove any trailing whitespace and use this value
1144 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001145 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001146
1147 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001149
1150 free(logical);
1151 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001152 }
1153 }
Eric Andersen8a931792003-07-03 10:20:29 +00001154
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001155 fclose(out);
1156
Eric Andersen8a931792003-07-03 10:20:29 +00001157 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001159#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001161static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001163 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001165 while (search) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +01001166 char *after_iface = is_prefixed_with(search->data, iface);
1167 if (after_iface
1168 && *after_iface == '='
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001169 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001170 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001171 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001172 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001173 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001174 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175}
1176
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001177/* read the previous state from the state file */
1178static llist_t *read_iface_state(void)
1179{
1180 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001181 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001182
1183 if (state_fp) {
1184 char *start, *end_ptr;
1185 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1186 /* We should only need to check for a single character */
1187 end_ptr = start + strcspn(start, " \t\n");
1188 *end_ptr = '\0';
1189 llist_add_to(&state_list, start);
1190 }
1191 fclose(state_fp);
1192 }
1193 return state_list;
1194}
1195
1196
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001197int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001198int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001200 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001201 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001202 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001203 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001204 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205
Denys Vlasenko62152da2009-06-05 21:53:11 +02001206 INIT_G();
1207
1208 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001209 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001210
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001211 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001212 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 /* ifup command */
1214 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001215 }
1216
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001217 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001218 argv += optind;
1219 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001220 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001221 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001222 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001223 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001224
Denys Vlasenkofb036632013-02-28 19:01:28 +01001225 defn = read_interfaces(interfaces, NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001226
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001227 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001228 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001229 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001231 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 }
1233
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001234 /* Update the interfaces */
1235 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001236 llist_t *iface_list;
1237 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001238 char *iface;
1239 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001240 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001241 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001242 int cmds_ret;
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001243 bool curr_failure = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001244
Rob Landleyd921b2e2006-08-03 15:41:12 +00001245 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001246 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001248 pch = strchr(iface, '=');
1249 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001251 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001253 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001255
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001256 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001257 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001258 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001259
1260 if (cmds == iface_up) {
1261 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001262 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001263 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001264 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265 }
1266 } else {
1267 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001268 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001269 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001270 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001271 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001273 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001276#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001277 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001278 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001279
1280 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001281 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 for (i = 0; i < currmap->n_matches; i++) {
1283 if (fnmatch(currmap->match[i], liface, 0) != 0)
1284 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001285 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001286 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001287 }
Eric Andersen8a931792003-07-03 10:20:29 +00001288 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001289 break;
1290 }
1291 }
1292 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001293#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001294
Eric Andersen8320b422003-04-02 10:13:26 +00001295 iface_list = defn->ifaces;
1296 while (iface_list) {
1297 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298 if (strcmp(liface, currif->iface) == 0) {
1299 char *oldiface = currif->iface;
1300
1301 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 currif->iface = iface;
1303
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001304 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001305
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001306 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001307 cmds_ret = cmds(currif);
1308 if (cmds_ret == -1) {
Denys Vlasenko6d8ea1d2015-03-22 17:08:51 +01001309 bb_error_msg("don't have all variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001310 liface, currif->address_family->name);
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001311 any_failures = curr_failure = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001312 } else if (cmds_ret == 0) {
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001313 any_failures = curr_failure = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001314 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001315
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001316 currif->iface = oldiface;
1317 }
Eric Andersen8320b422003-04-02 10:13:26 +00001318 iface_list = iface_list->link;
1319 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001320 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001321 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001322 }
1323
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001324 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001325 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001326 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001327 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001328 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001329 FILE *state_fp;
1330 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001331 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001332 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333
Frank Bergmann6ca03f22015-03-13 10:05:08 +01001334 if (cmds == iface_up && !curr_failure) {
Denys Vlasenkod6f50002011-12-15 12:39:25 +01001335 char *newiface = xasprintf("%s=%s", iface, liface);
1336 if (!iface_state) {
Rob Landley8bb50782006-05-26 23:44:51 +00001337 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001338 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001339 free(iface_state->data);
1340 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001341 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001342 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001343 /* Remove an interface from state_list */
1344 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001345 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001346 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001347
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001348 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001349 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001350 state = state_list;
1351 while (state) {
1352 if (state->data) {
1353 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001354 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001355 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001356 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001357 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001358 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001359 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001360 next:
1361 free(iface);
1362 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001363 }
1364
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001365 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001366}