blob: 9b34986964649320e952035cc614f7987ba980d7 [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
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000090 int n_mappings;
91 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000092};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000093
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000094struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000095 char *name;
96 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000097};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000098
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000099struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +0000100 const struct address_family_t *address_family;
101 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000102
Rob Landleye813ddb2006-02-28 03:53:14 +0000103 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000104 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000105 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000106};
107
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000108struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000109 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000110 llist_t *ifaces;
111 struct mapping_defn_t *mappings;
112};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000113
Denys Vlasenko62152da2009-06-05 21:53:11 +0200114
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000115#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000116enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200117 OPT_do_all = 0x1,
118 OPT_no_act = 0x2,
119 OPT_verbose = 0x4,
120 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000121 OPT_no_mappings = 0x10,
122};
Denys Vlasenko62152da2009-06-05 21:53:11 +0200123#define DO_ALL (option_mask32 & OPT_do_all)
124#define NO_ACT (option_mask32 & OPT_no_act)
125#define VERBOSE (option_mask32 & OPT_verbose)
126#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000127#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000128
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000129
Denys Vlasenko62152da2009-06-05 21:53:11 +0200130struct globals {
131 char **my_environ;
132 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100133 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100134} FIX_ALIASING;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200135#define G (*(struct globals*)&bb_common_bufsiz1)
136#define INIT_G() do { } while (0)
137
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000138
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200139static const char keywords_up_down[] ALIGN1 =
140 "up\0"
141 "down\0"
142 "pre-up\0"
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200143 "post-down\0"
144;
145
146
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000147#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
148
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000149static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000150{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000151 /* xasprintf trick will be smaller, but we are often
152 * called with str_length == 1 - don't want to have
153 * THAT much of malloc/freeing! */
154 char *buf = *bufp;
155 int len = (buf ? strlen(buf) : 0);
156 str_length++;
157 buf = xrealloc(buf, len + str_length);
158 /* copies at most str_length-1 chars! */
159 safe_strncpy(buf + len, str, str_length);
160 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000161}
162
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000163static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000164{
165 int i = strncmp(l, r, llen);
166
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000167 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200168 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000169 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000170}
171
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000172static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000173{
174 int i;
175
176 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200177 // ubuntu's ifup doesn't do this:
178 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000179 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200180 //free(label_buf);
181 //label_buf = xstrdup(ifd->iface);
182 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000183 //result = strchrnul(label_buf, ':');
184 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200185 //return label_buf;
186
187 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000188 }
189 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000190 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000191 }
192 for (i = 0; i < ifd->n_options; i++) {
193 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
194 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000195 }
196 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000197 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000198}
199
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200200# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000201static int count_netmask_bits(const char *dotted_quad)
202{
203// int result;
204// unsigned a, b, c, d;
205// /* Found a netmask... Check if it is dotted quad */
206// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
207// return -1;
208// if ((a|b|c|d) >> 8)
209// return -1; /* one of numbers is >= 256 */
210// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
211// d = ~d; /* 11110000 -> 00001111 */
212
213 /* Shorter version */
214 int result;
215 struct in_addr ip;
216 unsigned d;
217
218 if (inet_aton(dotted_quad, &ip) == 0)
219 return -1; /* malformed dotted IP */
220 d = ntohl(ip.s_addr); /* IP in host order */
221 d = ~d; /* 11110000 -> 00001111 */
222 if (d & (d+1)) /* check that it is in 00001111 form */
223 return -1; /* no it is not */
224 result = 32;
225 while (d) {
226 d >>= 1;
227 result--;
228 }
229 return result;
230}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200231# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000232
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000233static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000234{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000235 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
236 int okay[MAX_OPT_DEPTH] = { 1 };
237 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000238 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000239
240 while (*command) {
241 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000242 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000243 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000244 command++;
245 break;
246 case '\\':
247 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000248 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000249 command += 2;
250 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000251 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000252 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000253 }
254 break;
255 case '[':
256 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000257 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000258 okay[opt_depth] = 1;
259 opt_depth++;
260 command += 2;
261 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000262 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000263 command++;
264 }
265 break;
266 case ']':
267 if (command[1] == ']' && opt_depth > 1) {
268 opt_depth--;
269 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000270 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000271 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000272 command += 2;
273 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000274 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000275 command++;
276 }
277 break;
278 case '%':
279 {
280 char *nextpercent;
281 char *varvalue;
282
283 command++;
284 nextpercent = strchr(command, '%');
285 if (!nextpercent) {
286 errno = EUNBALPER;
287 free(result);
288 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000289 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000290
291 varvalue = get_var(command, nextpercent - command, ifd);
292
293 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200294# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000295 /* "hwaddress <class> <address>":
296 * unlike ifconfig, ip doesnt want <class>
297 * (usually "ether" keyword). Skip it. */
298 if (strncmp(command, "hwaddress", 9) == 0) {
299 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
300 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200301# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000302 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000303 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200304# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000305 /* Sigh... Add a special case for 'ip' to convert from
306 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000307 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000308 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000309 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000310 if (varvalue) {
311 res = count_netmask_bits(varvalue);
312 if (res > 0) {
313 const char *argument = utoa(res);
314 addstr(&result, argument, strlen(argument));
315 command = nextpercent + 1;
316 break;
317 }
Eric Andersen8320b422003-04-02 10:13:26 +0000318 }
Eric Andersen8320b422003-04-02 10:13:26 +0000319 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200320# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000321 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000322 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000323
324 command = nextpercent + 1;
325 }
326 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000327 }
328 }
329
330 if (opt_depth > 1) {
331 errno = EUNBALBRACK;
332 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000333 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000334 }
335
336 if (!okay[0]) {
337 errno = EUNDEFVAR;
338 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000339 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340 }
341
Denis Vlasenko05341252006-09-26 20:35:30 +0000342 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000343}
344
Denis Vlasenko05341252006-09-26 20:35:30 +0000345/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000346static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347{
348 char *out;
349 int ret;
350
351 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000352 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000353 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000354 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000355 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000356 /* out == "": parsed ok but not all needed variables known, skip */
357 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000358
359 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000360 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000361 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000362 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000363 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200365
366#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
367
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000368
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000369#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200370
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200371static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200373# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000374 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000375 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000376 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000377 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200378# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000379 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200380# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000381}
382
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200383static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200385# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000386 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200387# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000388 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200389# endif
390}
391
392static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
393{
394 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000395}
396
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200397static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000398{
Eric Andersen8320b422003-04-02 10:13:26 +0000399 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200400# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000401 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000402 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000403 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
Natanael Copacfcb1da2012-02-17 19:36:26 +0000404 result += execute("[[ip route add ::/0 via %gateway%]][[ prio %metric%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200405# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000406 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000407 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000408 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200409# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000410 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000411}
412
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200413static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000414{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200415# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000416 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200417# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000418 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200419# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000420}
421
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200422# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200423static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424{
Eric Andersen8320b422003-04-02 10:13:26 +0000425 int result;
426 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000427 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000428 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000429 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000430 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000431 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000432}
433
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200434static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000435{
Denis Vlasenko05341252006-09-26 20:35:30 +0000436 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200438# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000439
Denis Vlasenko05341252006-09-26 20:35:30 +0000440static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200441# if ENABLE_FEATURE_IFUPDOWN_IP
442 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
443# endif
444 { "static" , static_up6 , static_down6 , },
445 { "manual" , manual_up_down6 , manual_up_down6 , },
446 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000447};
448
Denis Vlasenko05341252006-09-26 20:35:30 +0000449static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000451 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452 methods6
453};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200454
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000455#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200457
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000458#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200459
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200460static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000461{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200462# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000463 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000464 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000465 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000466 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200467# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000468 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200469# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000470}
471
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200472static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000473{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200474# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000475 int result;
476 result = execute("ip addr flush dev %iface%", ifd, exec);
477 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000478 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200479# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000480 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200481# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000482}
483
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200484static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000485{
Eric Andersen8320b422003-04-02 10:13:26 +0000486 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200487# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000488 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
489 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000490 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000491 result += execute("[[ip route add default via %gateway% dev %iface%[[ prio %metric%]]]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000492 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200493# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000494 /* ifconfig said to set iface up before it processes hw %hwaddress%,
495 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000496 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000497 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000498 result += execute("ifconfig %iface% %address% netmask %netmask%"
499 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000500 ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000501 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000502 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200503# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000504}
505
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200506static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507{
Eric Andersen8320b422003-04-02 10:13:26 +0000508 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200509# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000510 result = execute("ip addr flush dev %iface%", ifd, exec);
511 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200512# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000513 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
514 /* Bringing the interface down deletes the routes in itself.
515 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
516 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000517 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200518# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000519 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000520}
521
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200522# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200523struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000524 const char *name;
525 const char *startcmd;
526 const char *stopcmd;
527};
528
529static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000530 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200531 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000532 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000533 },
534 { "dhclient",
535 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
536 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
537 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000538 { "pump",
539 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
540 "pump -i %iface% -k",
541 },
542 { "udhcpc",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200543 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000544 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000545 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000546 },
547};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200548# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000549
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200550# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200551static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000552{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000553 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200554# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000555 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000556 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000557 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200558# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000559 /* needed if we have hwaddress on dhcp iface */
560 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000561 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200562# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000563 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000564 if (exists_execable(ext_dhcp_clients[i].name))
565 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
566 }
567 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000568 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000569}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200570# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200571static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000572{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200573# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000574 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000575 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000576 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200577# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000578 /* needed if we have hwaddress on dhcp iface */
579 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000580 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200581# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000582 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200583 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000584 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000585}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200586# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200587static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000588 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000589{
590 return 0; /* no dhcp support */
591}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200592# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000593
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200594# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200595static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000597 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000598 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000599
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000600 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000601 if (exists_execable(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200602 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000603 if (result)
604 break;
605 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000606 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000607
608 if (!result)
609 bb_error_msg("warning: no dhcp clients found and stopped");
610
611 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
612 and it may come back up because udhcpc is still shutting down */
613 usleep(100000);
614 result += static_down(ifd, exec);
615 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000616}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200617# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200618static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000619{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000620 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200621 result = execute(
622 "test -f /var/run/udhcpc.%iface%.pid && "
623 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
624 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000625 /* Also bring the hardware interface down since
626 killing the dhcp client alone doesn't do it.
627 This enables consecutive ifup->ifdown->ifup */
628 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
629 and it may come back up because udhcpc is still shutting down */
630 usleep(100000);
631 result += static_down(ifd, exec);
632 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200634# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200635static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000636 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000637{
638 return 0; /* no dhcp support */
639}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200640# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000641
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200642static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000643{
644 return 1;
645}
646
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200647static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000648{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000649 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000650 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
651 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000652}
653
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200654static int FAST_FUNC ppp_up(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("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657}
658
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200659static int FAST_FUNC ppp_down(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("poff[[ %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_up(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 --start -x wvdial "
667 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668}
669
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200670static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000671{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000672 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000673 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674}
675
Denis Vlasenko05341252006-09-26 20:35:30 +0000676static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200677 { "manual" , manual_up_down, manual_up_down, },
678 { "wvdial" , wvdial_up , wvdial_down , },
679 { "ppp" , ppp_up , ppp_down , },
680 { "static" , static_up , static_down , },
681 { "bootp" , bootp_up , static_down , },
682 { "dhcp" , dhcp_up , dhcp_down , },
683 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000684};
685
Denis Vlasenko05341252006-09-26 20:35:30 +0000686static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000688 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689 methods
690};
691
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200692#endif /* FEATURE_IFUPDOWN_IPV4 */
693
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000694
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200695/* Returns pointer to the next word, or NULL.
696 * In 1st case, advances *buf to the word after this one.
697 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000698static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000699{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000700 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000701 char *word;
702
Glenn L McGrath85737042003-01-14 23:26:57 +0000703 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000704 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000705
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000706 /* Stop on EOL */
707 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000708 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000710 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000711 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000712
713 /* Unless we are already at NUL, store NUL and advance */
714 if (word[length] != '\0')
715 word[length++] = '\0';
716
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200717 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000718
719 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000720}
721
Denis Vlasenko05341252006-09-26 20:35:30 +0000722static 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 +0000723{
724 int i;
725
Denis Vlasenko736230e2006-11-20 19:40:36 +0000726 if (!name)
727 return NULL;
728
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000729 for (i = 0; af[i]; i++) {
730 if (strcmp(af[i]->name, name) == 0) {
731 return af[i];
732 }
733 }
734 return NULL;
735}
736
Denis Vlasenko05341252006-09-26 20:35:30 +0000737static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000738{
739 int i;
740
Denis Vlasenko736230e2006-11-20 19:40:36 +0000741 if (!name)
742 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000743 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000744 for (i = 0; i < af->n_methods; i++) {
745 if (strcmp(af->method[i].name, name) == 0) {
746 return &af->method[i];
747 }
748 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000749 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000750}
751
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000752static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000753{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000754 /* Let's try to be compatible.
755 *
756 * "man 5 interfaces" says:
757 * Lines starting with "#" are ignored. Note that end-of-line
758 * comments are NOT supported, comments must be on a line of their own.
759 * A line may be extended across multiple lines by making
760 * the last character a backslash.
761 *
762 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000763 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000764 * be ignored. Blank lines are ignored. Lines may be indented freely.
765 * A "\" character at the very end of the line indicates the next line
766 * should be treated as a continuation of the current one.
767 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000768#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000769 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000770#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000771 struct interface_defn_t *currif = NULL;
772 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000773 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000774 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000775 char *first_word;
776 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000777 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
778
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000779 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000780 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000781
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000782 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000783#if ENABLE_DESKTOP
784 /* Trailing "\" concatenates lines */
785 char *p;
786 while ((p = last_char_is(buf, '\\')) != NULL) {
787 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000788 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000789 if (!rest_of_line)
790 break;
791 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000792 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000793 free(rest_of_line);
794 buf = p;
795 }
796#endif
797 rest_of_line = buf;
798 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000799 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000800 free(buf);
801 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802 }
803
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000804 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000805#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000806 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000807
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000808 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000809 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000810 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000811 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000812 /*currmap->n_mappings = 0;*/
813 /*currmap->mapping = NULL;*/
814 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 {
Eric Andersen8320b422003-04-02 10:13:26 +0000816 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000817 while (*where != NULL) {
818 where = &(*where)->next;
819 }
820 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000821 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000822 }
Eric Andersen8320b422003-04-02 10:13:26 +0000823 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000824#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000826 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000827 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000828#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000829 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000830#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000831#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000832 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000834 NULL
835 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000836 char *iface_name;
837 char *address_family_name;
838 char *method_name;
839 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000841 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000842 iface_name = next_word(&rest_of_line);
843 address_family_name = next_word(&rest_of_line);
844 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000845
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000846 if (method_name == NULL)
847 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000848
849 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000850 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000851
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000852 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000853 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000854
855 currif->iface = xstrdup(iface_name);
856
857 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000858 if (!currif->address_family)
859 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000860
861 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000862 if (!currif->method)
863 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000864
865 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
866 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000867 if ((strcmp(tmp->iface, currif->iface) == 0)
868 && (tmp->address_family == currif->address_family)
869 ) {
870 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000871 }
872 }
873 llist_add_to_end(&(defn->ifaces), (char*)currif);
874
875 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000876 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000877 } else if (strcmp(first_word, "auto") == 0) {
878 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000879
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000880 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000881 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000882 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 }
884
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000885 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000886 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
887 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000888 }
889 currently_processing = NONE;
890 } else {
891 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000892 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000893 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000894 bb_error_msg_and_die("option with empty value \"%s\"", buf);
895
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200896 if (strcmp(first_word, "post-up") == 0)
897 first_word += 5; /* "up" */
898 else if (strcmp(first_word, "pre-down") == 0)
899 first_word += 4; /* "down" */
900
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200901 /* If not one of "up", "down",... words... */
902 if (index_in_strings(keywords_up_down, first_word) < 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000903 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000904 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000905 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000906 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000907 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000908 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000909 debug_noise("\t%s=%s\n", first_word, rest_of_line);
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200910 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000911 currif->option[currif->n_options].name = xstrdup(first_word);
912 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000913 currif->n_options++;
914 break;
915 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000916#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000917 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000918 if (currmap->script != NULL)
919 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000920 currmap->script = xstrdup(next_word(&rest_of_line));
921 } else if (strcmp(first_word, "map") == 0) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200922 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000923 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000924 currmap->n_mappings++;
925 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000926 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000927 }
928#endif
929 break;
930 case NONE:
931 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000932 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000933 }
934 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000935 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000936 } /* while (fgets) */
937
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000939 /* ferror does NOT set errno! */
940 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000941 }
942 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943
944 return defn;
945}
946
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000947static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948{
949 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200950 char *dst;
951 char *src;
952 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000953
Rob Landleyd921b2e2006-08-03 15:41:12 +0000954 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000955
Denys Vlasenko62152da2009-06-05 21:53:11 +0200956 for (dst = src = result; (c = *src) != '=' && c; src++) {
957 if (c == '-')
958 c = '_';
959 if (c >= 'a' && c <= 'z')
960 c -= ('a' - 'A');
961 if (isalnum(c) || c == '_')
962 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000963 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200964 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000965
966 return result;
967}
968
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200969static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000970{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000971 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200972 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000973
Denys Vlasenko62152da2009-06-05 21:53:11 +0200974 if (G.my_environ != NULL) {
975 for (pp = G.my_environ; *pp; pp++) {
976 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000977 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200978 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000979 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200980
981 /* note: last element will stay NULL: */
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200982 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
Denys Vlasenko62152da2009-06-05 21:53:11 +0200983 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000984
985 for (i = 0; i < iface->n_options; i++) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200986 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987 continue;
988 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200989 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000990 }
991
Denys Vlasenko62152da2009-06-05 21:53:11 +0200992 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
993 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
994 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
995 *pp++ = setlocalenv("%s=%s", "MODE", mode);
Andreas Oberritter3a9365e2012-04-23 22:08:39 +0200996 *pp++ = setlocalenv("%s=%s", "PHASE", opt);
Denys Vlasenko62152da2009-06-05 21:53:11 +0200997 if (G.startup_PATH)
998 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000999}
1000
1001static int doit(char *str)
1002{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001003 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001004 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001006 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001007 pid_t child;
1008 int status;
1009
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001010 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001011 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001012 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001013 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001014 if (child == 0) { /* child */
1015 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001016 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001017 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001018 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001019 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1020 return 0;
1021 }
1022 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001023 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001024}
1025
Rob Landleye813ddb2006-02-28 03:53:14 +00001026static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001027{
1028 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001029 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001030 for (i = 0; i < ifd->n_options; i++) {
1031 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001032 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033 return 0;
1034 }
1035 }
1036 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001037
Rob Landleyd921b2e2006-08-03 15:41:12 +00001038 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001039 /* heh, we don't bother free'ing it */
1040 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041}
1042
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001043static int check(char *str)
1044{
Eric Andersen8320b422003-04-02 10:13:26 +00001045 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001046}
1047
Eric Andersen8320b422003-04-02 10:13:26 +00001048static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001049{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001050 if (!iface->method->up(iface, check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001051 set_environ(iface, "start", "pre-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001052 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001053 if (!iface->method->up(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001054 set_environ(iface, "start", "post-up");
Rob Landleye813ddb2006-02-28 03:53:14 +00001055 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001056 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001057}
1058
1059static int iface_down(struct interface_defn_t *iface)
1060{
Eric Andersen8320b422003-04-02 10:13:26 +00001061 if (!iface->method->down(iface,check)) return -1;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001062 set_environ(iface, "stop", "pre-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001063 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001064 if (!iface->method->down(iface, doit)) return 0;
Andreas Oberritter3a9365e2012-04-23 22:08:39 +02001065 set_environ(iface, "stop", "post-down");
Rob Landleye813ddb2006-02-28 03:53:14 +00001066 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001067 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068}
1069
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001070#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001071static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001072{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001073 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001074 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075 pid_t pid;
1076
Denis Vlasenko37188322008-02-16 13:20:56 +00001077 xpiped_pair(infd);
1078 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001079
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001080 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001081 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001082
Pascal Bellard926031b2010-07-04 15:32:38 +02001083 if (pid == 0) {
1084 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001085 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001086 close(infd.wr);
1087 close(outfd.rd);
1088 xmove_fd(infd.rd, 0);
1089 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001090 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001091 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001092 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001093 close(infd.rd);
1094 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001095 *in = xfdopen_for_write(infd.wr);
1096 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001097 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001098}
1099
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001100static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001101{
1102 FILE *in, *out;
1103 int i, status;
1104 pid_t pid;
1105
Rob Landleyd921b2e2006-08-03 15:41:12 +00001106 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001108 /* Run the mapping script. Never fails. */
1109 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001110
1111 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001112 for (i = 0; i < map->n_mappings; i++) {
1113 fprintf(in, "%s\n", map->mapping[i]);
1114 }
1115 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001116 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001117
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001118 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001119 /* If the mapping script exited successfully, try to
1120 * grab a line of output and use that as the name of the
1121 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001122 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001123
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001124 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001125 /* If we are able to read a line of output from the script,
1126 * remove any trailing whitespace and use this value
1127 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001128 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001129
1130 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001131 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001132
1133 free(logical);
1134 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135 }
1136 }
Eric Andersen8a931792003-07-03 10:20:29 +00001137
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138 fclose(out);
1139
Eric Andersen8a931792003-07-03 10:20:29 +00001140 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001141}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001142#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001144static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001146 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001147 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001149 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001150 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001151 && (search->data[iface_len] == '=')
1152 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001153 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001155 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001157 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158}
1159
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001160/* read the previous state from the state file */
1161static llist_t *read_iface_state(void)
1162{
1163 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001164 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001165
1166 if (state_fp) {
1167 char *start, *end_ptr;
1168 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1169 /* We should only need to check for a single character */
1170 end_ptr = start + strcspn(start, " \t\n");
1171 *end_ptr = '\0';
1172 llist_add_to(&state_list, start);
1173 }
1174 fclose(state_fp);
1175 }
1176 return state_list;
1177}
1178
1179
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001180int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001181int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001183 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001184 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001185 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001186 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001187 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188
Denys Vlasenko62152da2009-06-05 21:53:11 +02001189 INIT_G();
1190
1191 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001192 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001193
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001194 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001195 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001196 /* ifup command */
1197 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001198 }
1199
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001200 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001201 argv += optind;
1202 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001203 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001204 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001205 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001206 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207
Eric Andersen8320b422003-04-02 10:13:26 +00001208 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001210 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001212 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001213 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001214 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001215 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001216 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001217 }
1218
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001219 /* Update the interfaces */
1220 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001221 llist_t *iface_list;
1222 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001223 char *iface;
1224 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001226 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001227 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228
Rob Landleyd921b2e2006-08-03 15:41:12 +00001229 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001230 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001232 pch = strchr(iface, '=');
1233 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001234 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001235 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001236 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001237 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001239
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001240 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001241 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001243
1244 if (cmds == iface_up) {
1245 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001246 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001247 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001248 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001249 }
1250 } else {
1251 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001252 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001253 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001254 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001255 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001256 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001257 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001259
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001260#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001261 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001262 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001263
1264 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001265 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 for (i = 0; i < currmap->n_matches; i++) {
1267 if (fnmatch(currmap->match[i], liface, 0) != 0)
1268 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001269 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001270 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001271 }
Eric Andersen8a931792003-07-03 10:20:29 +00001272 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273 break;
1274 }
1275 }
1276 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001277#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001278
Eric Andersen8320b422003-04-02 10:13:26 +00001279 iface_list = defn->ifaces;
1280 while (iface_list) {
1281 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 if (strcmp(liface, currif->iface) == 0) {
1283 char *oldiface = currif->iface;
1284
1285 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001286 currif->iface = iface;
1287
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001288 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001289
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001290 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001291 cmds_ret = cmds(currif);
1292 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001293 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001294 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001295 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001296 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001297 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001299
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001300 currif->iface = oldiface;
1301 }
Eric Andersen8320b422003-04-02 10:13:26 +00001302 iface_list = iface_list->link;
1303 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001304 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001305 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306 }
1307
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001308 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001309 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001310 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001311 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001312 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001313 FILE *state_fp;
1314 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001315 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001317
Lauri Hintsala454fe292011-12-14 16:49:58 +02001318 if (cmds == iface_up && !any_failures) {
Denys Vlasenkod6f50002011-12-15 12:39:25 +01001319 char *newiface = xasprintf("%s=%s", iface, liface);
1320 if (!iface_state) {
Rob Landley8bb50782006-05-26 23:44:51 +00001321 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001322 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001323 free(iface_state->data);
1324 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001325 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001326 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001327 /* Remove an interface from state_list */
1328 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001329 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001330 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001332 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001333 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001334 state = state_list;
1335 while (state) {
1336 if (state->data) {
1337 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001338 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001339 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001340 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001341 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001342 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001343 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001344 next:
1345 free(iface);
1346 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001347 }
1348
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001349 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001350}