blob: 3820330389003de30df04507471421ae3ad1bddc [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
52#define EUNBALBRACK 10001
53#define EUNDEFVAR 10002
54#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000055
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000056#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000057#define MAX_INTERFACE_LENGTH 10
58#endif
Eric Andersen8320b422003-04-02 10:13:26 +000059
Wade Berrier142c5cb2008-11-14 21:18:45 +000060#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
61
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000062#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000063
64/* Forward declaration */
65struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
Denis Vlasenko097c3242006-11-27 16:59:15 +000067typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000068
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000069struct method_t {
70 const char *name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020071 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
72 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
Eric Andersen8320b422003-04-02 10:13:26 +000073};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000074
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000075struct address_family_t {
76 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000078 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000079};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000081struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000082 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000083
84 int max_matches;
85 int n_matches;
86 char **match;
87
88 char *script;
89
90 int max_mappings;
91 int n_mappings;
92 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000093};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000094
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000095struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000096 char *name;
97 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000098};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000099
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000100struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +0000101 const struct address_family_t *address_family;
102 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000103
Rob Landleye813ddb2006-02-28 03:53:14 +0000104 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000105 int max_options;
106 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000107 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000108};
109
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000110struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000111 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000112 llist_t *ifaces;
113 struct mapping_defn_t *mappings;
114};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000115
Denys Vlasenko62152da2009-06-05 21:53:11 +0200116
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000117#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000118enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200119 OPT_do_all = 0x1,
120 OPT_no_act = 0x2,
121 OPT_verbose = 0x4,
122 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000123 OPT_no_mappings = 0x10,
124};
Denys Vlasenko62152da2009-06-05 21:53:11 +0200125#define DO_ALL (option_mask32 & OPT_do_all)
126#define NO_ACT (option_mask32 & OPT_no_act)
127#define VERBOSE (option_mask32 & OPT_verbose)
128#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000129#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000130
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000131
Denys Vlasenko62152da2009-06-05 21:53:11 +0200132struct globals {
133 char **my_environ;
134 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100135 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100136} FIX_ALIASING;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200137#define G (*(struct globals*)&bb_common_bufsiz1)
138#define INIT_G() do { } while (0)
139
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000140
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000141#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
142
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000143static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000144{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000145 /* xasprintf trick will be smaller, but we are often
146 * called with str_length == 1 - don't want to have
147 * THAT much of malloc/freeing! */
148 char *buf = *bufp;
149 int len = (buf ? strlen(buf) : 0);
150 str_length++;
151 buf = xrealloc(buf, len + str_length);
152 /* copies at most str_length-1 chars! */
153 safe_strncpy(buf + len, str, str_length);
154 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000155}
156
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000157static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000158{
159 int i = strncmp(l, r, llen);
160
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000161 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200162 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000163 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000164}
165
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000166static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000167{
168 int i;
169
170 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200171 // ubuntu's ifup doesn't do this:
172 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000173 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200174 //free(label_buf);
175 //label_buf = xstrdup(ifd->iface);
176 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000177 //result = strchrnul(label_buf, ':');
178 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200179 //return label_buf;
180
181 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000182 }
183 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000184 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000185 }
186 for (i = 0; i < ifd->n_options; i++) {
187 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
188 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000189 }
190 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000191 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192}
193
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200194# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000195static int count_netmask_bits(const char *dotted_quad)
196{
197// int result;
198// unsigned a, b, c, d;
199// /* Found a netmask... Check if it is dotted quad */
200// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
201// return -1;
202// if ((a|b|c|d) >> 8)
203// return -1; /* one of numbers is >= 256 */
204// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
205// d = ~d; /* 11110000 -> 00001111 */
206
207 /* Shorter version */
208 int result;
209 struct in_addr ip;
210 unsigned d;
211
212 if (inet_aton(dotted_quad, &ip) == 0)
213 return -1; /* malformed dotted IP */
214 d = ntohl(ip.s_addr); /* IP in host order */
215 d = ~d; /* 11110000 -> 00001111 */
216 if (d & (d+1)) /* check that it is in 00001111 form */
217 return -1; /* no it is not */
218 result = 32;
219 while (d) {
220 d >>= 1;
221 result--;
222 }
223 return result;
224}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200225# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000226
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000227static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000228{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000229 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
230 int okay[MAX_OPT_DEPTH] = { 1 };
231 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000232 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000233
234 while (*command) {
235 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000236 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000237 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000238 command++;
239 break;
240 case '\\':
241 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000242 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000243 command += 2;
244 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000245 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000246 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000247 }
248 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 {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000256 addstr(&result, "[", 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 {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000268 addstr(&result, "]", 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) {
280 errno = EUNBALPER;
281 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. */
292 if (strncmp(command, "hwaddress", 9) == 0) {
293 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. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000301 if (strncmp(command, "bnmask", 6) == 0) {
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) {
325 errno = EUNBALBRACK;
326 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]) {
331 errno = EUNDEFVAR;
332 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);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000397 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
398 result += execute("[[ip route add ::/0 via %gateway%]]", 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);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000402 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", 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);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000424 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000425 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000426}
427
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200428static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429{
Denis Vlasenko05341252006-09-26 20:35:30 +0000430 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200432# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000433
Denis Vlasenko05341252006-09-26 20:35:30 +0000434static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200435# if ENABLE_FEATURE_IFUPDOWN_IP
436 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
437# endif
438 { "static" , static_up6 , static_down6 , },
439 { "manual" , manual_up_down6 , manual_up_down6 , },
440 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441};
442
Denis Vlasenko05341252006-09-26 20:35:30 +0000443static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000444 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000445 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446 methods6
447};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200448
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000449#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200451
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000452#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200453
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200454static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000455{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200456# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000457 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000458 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000459 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000460 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200461# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000462 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200463# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464}
465
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200466static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000467{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200468# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000469 int result;
470 result = execute("ip addr flush dev %iface%", ifd, exec);
471 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000472 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200473# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000474 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200475# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000476}
477
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200478static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000479{
Eric Andersen8320b422003-04-02 10:13:26 +0000480 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200481# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000482 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
483 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000484 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000485 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000486 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200487# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000488 /* ifconfig said to set iface up before it processes hw %hwaddress%,
489 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000490 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000491 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000492 result += execute("ifconfig %iface% %address% netmask %netmask%"
493 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000494 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000495 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000496 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200497# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000498}
499
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200500static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000501{
Eric Andersen8320b422003-04-02 10:13:26 +0000502 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200503# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000504 result = execute("ip addr flush dev %iface%", ifd, exec);
505 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200506# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000507 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
508 /* Bringing the interface down deletes the routes in itself.
509 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
510 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000511 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200512# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000513 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514}
515
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200516# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200517struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000518 const char *name;
519 const char *startcmd;
520 const char *stopcmd;
521};
522
523static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000524 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200525 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000526 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000527 },
528 { "dhclient",
529 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
530 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
531 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000532 { "pump",
533 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
534 "pump -i %iface% -k",
535 },
536 { "udhcpc",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200537 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000538 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000539 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000540 },
541};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200542# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000543
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200544# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200545static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000547 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200548# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000549 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000550 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000551 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200552# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000553 /* needed if we have hwaddress on dhcp iface */
554 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000555 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200556# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000557 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000558 if (exists_execable(ext_dhcp_clients[i].name))
559 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
560 }
561 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000562 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000563}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200564# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200565static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000566{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200567# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000568 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000569 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000570 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200571# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000572 /* needed if we have hwaddress on dhcp iface */
573 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000574 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200575# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000576 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200577 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000578 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000579}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200580# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200581static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000582 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000583{
584 return 0; /* no dhcp support */
585}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200586# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000587
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200588# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200589static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000590{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000591 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000592 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000593
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000594 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000595 if (exists_execable(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200596 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000597 if (result)
598 break;
599 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000600 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000601
602 if (!result)
603 bb_error_msg("warning: no dhcp clients found and stopped");
604
605 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
606 and it may come back up because udhcpc is still shutting down */
607 usleep(100000);
608 result += static_down(ifd, exec);
609 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000610}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200611# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200612static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000613{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000614 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200615 result = execute(
616 "test -f /var/run/udhcpc.%iface%.pid && "
617 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
618 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000619 /* Also bring the hardware interface down since
620 killing the dhcp client alone doesn't do it.
621 This enables consecutive ifup->ifdown->ifup */
622 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
623 and it may come back up because udhcpc is still shutting down */
624 usleep(100000);
625 result += static_down(ifd, exec);
626 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200628# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200629static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000630 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000631{
632 return 0; /* no dhcp support */
633}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200634# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000635
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200636static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000637{
638 return 1;
639}
640
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200641static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000642{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000643 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000644 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
645 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646}
647
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200648static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000649{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000650 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000651}
652
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200653static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000655 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000656}
657
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200658static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000659{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000660 return execute("start-stop-daemon --start -x wvdial "
661 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662}
663
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200664static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000665{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000666 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000667 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668}
669
Denis Vlasenko05341252006-09-26 20:35:30 +0000670static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200671 { "manual" , manual_up_down, manual_up_down, },
672 { "wvdial" , wvdial_up , wvdial_down , },
673 { "ppp" , ppp_up , ppp_down , },
674 { "static" , static_up , static_down , },
675 { "bootp" , bootp_up , static_down , },
676 { "dhcp" , dhcp_up , dhcp_down , },
677 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000678};
679
Denis Vlasenko05341252006-09-26 20:35:30 +0000680static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000682 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000683 methods
684};
685
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200686#endif /* FEATURE_IFUPDOWN_IPV4 */
687
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200689/* Returns pointer to the next word, or NULL.
690 * In 1st case, advances *buf to the word after this one.
691 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000692static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000694 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000695 char *word;
696
Glenn L McGrath85737042003-01-14 23:26:57 +0000697 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000698 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000699
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000700 /* Stop on EOL */
701 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000702 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000703
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000704 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000705 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000706
707 /* Unless we are already at NUL, store NUL and advance */
708 if (word[length] != '\0')
709 word[length++] = '\0';
710
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200711 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000712
713 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714}
715
Denis Vlasenko05341252006-09-26 20:35:30 +0000716static 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 +0000717{
718 int i;
719
Denis Vlasenko736230e2006-11-20 19:40:36 +0000720 if (!name)
721 return NULL;
722
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000723 for (i = 0; af[i]; i++) {
724 if (strcmp(af[i]->name, name) == 0) {
725 return af[i];
726 }
727 }
728 return NULL;
729}
730
Denis Vlasenko05341252006-09-26 20:35:30 +0000731static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000732{
733 int i;
734
Denis Vlasenko736230e2006-11-20 19:40:36 +0000735 if (!name)
736 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000737 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000738 for (i = 0; i < af->n_methods; i++) {
739 if (strcmp(af->method[i].name, name) == 0) {
740 return &af->method[i];
741 }
742 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000743 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000744}
745
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000746static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000747{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000748 /* Let's try to be compatible.
749 *
750 * "man 5 interfaces" says:
751 * Lines starting with "#" are ignored. Note that end-of-line
752 * comments are NOT supported, comments must be on a line of their own.
753 * A line may be extended across multiple lines by making
754 * the last character a backslash.
755 *
756 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000757 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000758 * be ignored. Blank lines are ignored. Lines may be indented freely.
759 * A "\" character at the very end of the line indicates the next line
760 * should be treated as a continuation of the current one.
761 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000762#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000763 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000764#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000765 struct interface_defn_t *currif = NULL;
766 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000767 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000768 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000769 char *first_word;
770 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000771 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
772
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000773 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000774 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000776 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000777#if ENABLE_DESKTOP
778 /* Trailing "\" concatenates lines */
779 char *p;
780 while ((p = last_char_is(buf, '\\')) != NULL) {
781 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000782 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000783 if (!rest_of_line)
784 break;
785 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000786 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000787 free(rest_of_line);
788 buf = p;
789 }
790#endif
791 rest_of_line = buf;
792 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000793 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000794 free(buf);
795 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000796 }
797
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000798 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000799#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000800 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000801
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000802 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000803 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000804 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000805 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000806 /*currmap->max_mappings = 0; - done by xzalloc */
807 /*currmap->n_mappings = 0;*/
808 /*currmap->mapping = NULL;*/
809 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000810 {
Eric Andersen8320b422003-04-02 10:13:26 +0000811 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812 while (*where != NULL) {
813 where = &(*where)->next;
814 }
815 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000816 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000817 }
Eric Andersen8320b422003-04-02 10:13:26 +0000818 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000819#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000820 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000821 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000822 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000823#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000824 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000826#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000827 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000828#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000829 NULL
830 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000831 char *iface_name;
832 char *address_family_name;
833 char *method_name;
834 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000836 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000837 iface_name = next_word(&rest_of_line);
838 address_family_name = next_word(&rest_of_line);
839 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000841 if (method_name == NULL)
842 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000843
844 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000845 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000846
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000847 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000848 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000849
850 currif->iface = xstrdup(iface_name);
851
852 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000853 if (!currif->address_family)
854 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000855
856 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000857 if (!currif->method)
858 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000859
860 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
861 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000862 if ((strcmp(tmp->iface, currif->iface) == 0)
863 && (tmp->address_family == currif->address_family)
864 ) {
865 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000866 }
867 }
868 llist_add_to_end(&(defn->ifaces), (char*)currif);
869
870 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000871 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000872 } else if (strcmp(first_word, "auto") == 0) {
873 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000874
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000875 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000876 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000877 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000878 }
879
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000880 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000881 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
882 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 }
884 currently_processing = NONE;
885 } else {
886 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000887 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000888 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000889 bb_error_msg_and_die("option with empty value \"%s\"", buf);
890
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000891 if (strcmp(first_word, "up") != 0
892 && strcmp(first_word, "down") != 0
893 && strcmp(first_word, "pre-up") != 0
894 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000895 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000896 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000897 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000898 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000899 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000900 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000901 }
902 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000903 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000904 currif->option = xrealloc(currif->option,
905 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000906 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000907 debug_noise("\t%s=%s\n", first_word, rest_of_line);
908 currif->option[currif->n_options].name = xstrdup(first_word);
909 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000910 currif->n_options++;
911 break;
912 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000913#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000914 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000915 if (currmap->script != NULL)
916 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000917 currmap->script = xstrdup(next_word(&rest_of_line));
918 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000919 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000920 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000921 currmap->mapping = xrealloc(currmap->mapping,
922 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000923 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000924 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000925 currmap->n_mappings++;
926 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000927 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000928 }
929#endif
930 break;
931 case NONE:
932 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000933 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000934 }
935 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000936 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000937 } /* while (fgets) */
938
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000939 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000940 /* ferror does NOT set errno! */
941 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000942 }
943 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000944
945 return defn;
946}
947
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000948static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949{
950 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200951 char *dst;
952 char *src;
953 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000954
Rob Landleyd921b2e2006-08-03 15:41:12 +0000955 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000956
Denys Vlasenko62152da2009-06-05 21:53:11 +0200957 for (dst = src = result; (c = *src) != '=' && c; src++) {
958 if (c == '-')
959 c = '_';
960 if (c >= 'a' && c <= 'z')
961 c -= ('a' - 'A');
962 if (isalnum(c) || c == '_')
963 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200965 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000966
967 return result;
968}
969
Rob Landleye813ddb2006-02-28 03:53:14 +0000970static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000971{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000972 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200973 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000974
Denys Vlasenko62152da2009-06-05 21:53:11 +0200975 if (G.my_environ != NULL) {
976 for (pp = G.my_environ; *pp; pp++) {
977 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000978 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200979 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000980 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200981
982 /* note: last element will stay NULL: */
983 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
984 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000985
986 for (i = 0; i < iface->n_options; i++) {
987 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000988 || strcmp(iface->option[i].name, "down") == 0
989 || strcmp(iface->option[i].name, "pre-up") == 0
990 || strcmp(iface->option[i].name, "post-down") == 0
991 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000992 continue;
993 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200994 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000995 }
996
Denys Vlasenko62152da2009-06-05 21:53:11 +0200997 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
998 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
999 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
1000 *pp++ = setlocalenv("%s=%s", "MODE", mode);
1001 if (G.startup_PATH)
1002 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001003}
1004
1005static int doit(char *str)
1006{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001007 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001008 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001010 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001011 pid_t child;
1012 int status;
1013
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001014 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001015 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001016 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001017 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001018 if (child == 0) { /* child */
1019 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001020 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001022 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001023 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1024 return 0;
1025 }
1026 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001027 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001028}
1029
Rob Landleye813ddb2006-02-28 03:53:14 +00001030static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031{
1032 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001033 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001034 for (i = 0; i < ifd->n_options; i++) {
1035 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001036 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037 return 0;
1038 }
1039 }
1040 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001041
Rob Landleyd921b2e2006-08-03 15:41:12 +00001042 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001043 /* heh, we don't bother free'ing it */
1044 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045}
1046
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001047static int check(char *str)
1048{
Eric Andersen8320b422003-04-02 10:13:26 +00001049 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050}
1051
Eric Andersen8320b422003-04-02 10:13:26 +00001052static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001053{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001054 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +00001055 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +00001056 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001057 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001058 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001059 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001060}
1061
1062static int iface_down(struct interface_defn_t *iface)
1063{
Eric Andersen8320b422003-04-02 10:13:26 +00001064 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +00001066 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001067 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001068 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001069 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070}
1071
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001072#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001073static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001074{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001075 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001076 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001077 pid_t pid;
1078
Denis Vlasenko37188322008-02-16 13:20:56 +00001079 xpiped_pair(infd);
1080 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001081
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001082 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001083 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001084
Pascal Bellard926031b2010-07-04 15:32:38 +02001085 if (pid == 0) {
1086 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001087 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001088 close(infd.wr);
1089 close(outfd.rd);
1090 xmove_fd(infd.rd, 0);
1091 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001092 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001093 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001094 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001095 close(infd.rd);
1096 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001097 *in = xfdopen_for_write(infd.wr);
1098 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001099 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100}
1101
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001102static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001103{
1104 FILE *in, *out;
1105 int i, status;
1106 pid_t pid;
1107
Rob Landleyd921b2e2006-08-03 15:41:12 +00001108 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001110 /* Run the mapping script. Never fails. */
1111 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001112
1113 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114 for (i = 0; i < map->n_mappings; i++) {
1115 fprintf(in, "%s\n", map->mapping[i]);
1116 }
1117 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001118 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001119
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001120 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001121 /* If the mapping script exited successfully, try to
1122 * grab a line of output and use that as the name of the
1123 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001124 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001125
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001126 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001127 /* If we are able to read a line of output from the script,
1128 * remove any trailing whitespace and use this value
1129 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001130 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001131
1132 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001133 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001134
1135 free(logical);
1136 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137 }
1138 }
Eric Andersen8a931792003-07-03 10:20:29 +00001139
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001140 fclose(out);
1141
Eric Andersen8a931792003-07-03 10:20:29 +00001142 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001144#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001148 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001149 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001150
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001151 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001152 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001153 && (search->data[iface_len] == '=')
1154 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001155 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001157 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001159 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160}
1161
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001162/* read the previous state from the state file */
1163static llist_t *read_iface_state(void)
1164{
1165 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001166 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001167
1168 if (state_fp) {
1169 char *start, *end_ptr;
1170 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1171 /* We should only need to check for a single character */
1172 end_ptr = start + strcspn(start, " \t\n");
1173 *end_ptr = '\0';
1174 llist_add_to(&state_list, start);
1175 }
1176 fclose(state_fp);
1177 }
1178 return state_list;
1179}
1180
1181
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001182int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001183int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001185 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001186 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001187 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001188 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001189 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001190
Denys Vlasenko62152da2009-06-05 21:53:11 +02001191 INIT_G();
1192
1193 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001194 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001195
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001196 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001197 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001198 /* ifup command */
1199 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001200 }
1201
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001202 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001203 argv += optind;
1204 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001205 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001206 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001207 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001208 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209
Eric Andersen8320b422003-04-02 10:13:26 +00001210 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001212 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001214 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001215 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001216 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001217 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001218 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219 }
1220
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001221 /* Update the interfaces */
1222 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001223 llist_t *iface_list;
1224 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001225 char *iface;
1226 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001227 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001228 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001229 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230
Rob Landleyd921b2e2006-08-03 15:41:12 +00001231 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001232 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001234 pch = strchr(iface, '=');
1235 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001236 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001237 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001239 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001240 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001241
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001242 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001243 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001244 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001245
1246 if (cmds == iface_up) {
1247 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001248 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001249 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001250 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 }
1252 } else {
1253 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001254 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001255 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001256 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001259 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001261
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001262#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001263 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001264 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265
1266 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001267 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001268 for (i = 0; i < currmap->n_matches; i++) {
1269 if (fnmatch(currmap->match[i], liface, 0) != 0)
1270 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001271 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001272 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273 }
Eric Andersen8a931792003-07-03 10:20:29 +00001274 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001275 break;
1276 }
1277 }
1278 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001279#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001280
Eric Andersen8320b422003-04-02 10:13:26 +00001281 iface_list = defn->ifaces;
1282 while (iface_list) {
1283 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001284 if (strcmp(liface, currif->iface) == 0) {
1285 char *oldiface = currif->iface;
1286
1287 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001288 currif->iface = iface;
1289
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001290 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001291
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001292 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001293 cmds_ret = cmds(currif);
1294 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001295 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001296 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001297 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001298 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001299 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001300 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001301
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 currif->iface = oldiface;
1303 }
Eric Andersen8320b422003-04-02 10:13:26 +00001304 iface_list = iface_list->link;
1305 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001306 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001307 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001308 }
1309
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001310 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001311 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001312 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001313 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001314 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001315 FILE *state_fp;
1316 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001317 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001318 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001319
1320 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001321 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001322 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001323 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001324 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001325 free(iface_state->data);
1326 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001328 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001329 /* Remove an interface from state_list */
1330 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001331 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001332 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001334 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001335 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001336 state = state_list;
1337 while (state) {
1338 if (state->data) {
1339 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001340 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001341 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001342 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001343 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001344 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001345 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001346 next:
1347 free(iface);
1348 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001349 }
1350
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001351 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001352}