blob: dfda206708a89c8278351125ae21e4a4fa324ee2 [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"
143 "pre-down\0"
144 "post-up\0"
145 "post-down\0"
146;
147
148
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000149#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
150
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000151static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000152{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000153 /* xasprintf trick will be smaller, but we are often
154 * called with str_length == 1 - don't want to have
155 * THAT much of malloc/freeing! */
156 char *buf = *bufp;
157 int len = (buf ? strlen(buf) : 0);
158 str_length++;
159 buf = xrealloc(buf, len + str_length);
160 /* copies at most str_length-1 chars! */
161 safe_strncpy(buf + len, str, str_length);
162 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000163}
164
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000165static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000166{
167 int i = strncmp(l, r, llen);
168
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000169 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200170 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000171 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000172}
173
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000174static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000175{
176 int i;
177
178 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200179 // ubuntu's ifup doesn't do this:
180 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000181 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200182 //free(label_buf);
183 //label_buf = xstrdup(ifd->iface);
184 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000185 //result = strchrnul(label_buf, ':');
186 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200187 //return label_buf;
188
189 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000190 }
191 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000192 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000193 }
194 for (i = 0; i < ifd->n_options; i++) {
195 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
196 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000197 }
198 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000199 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000200}
201
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200202# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000203static int count_netmask_bits(const char *dotted_quad)
204{
205// int result;
206// unsigned a, b, c, d;
207// /* Found a netmask... Check if it is dotted quad */
208// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
209// return -1;
210// if ((a|b|c|d) >> 8)
211// return -1; /* one of numbers is >= 256 */
212// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
213// d = ~d; /* 11110000 -> 00001111 */
214
215 /* Shorter version */
216 int result;
217 struct in_addr ip;
218 unsigned d;
219
220 if (inet_aton(dotted_quad, &ip) == 0)
221 return -1; /* malformed dotted IP */
222 d = ntohl(ip.s_addr); /* IP in host order */
223 d = ~d; /* 11110000 -> 00001111 */
224 if (d & (d+1)) /* check that it is in 00001111 form */
225 return -1; /* no it is not */
226 result = 32;
227 while (d) {
228 d >>= 1;
229 result--;
230 }
231 return result;
232}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200233# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000234
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000235static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000236{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000237 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
238 int okay[MAX_OPT_DEPTH] = { 1 };
239 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000240 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000241
242 while (*command) {
243 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000244 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000245 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000246 command++;
247 break;
248 case '\\':
249 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000250 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000251 command += 2;
252 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000253 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000254 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000255 }
256 break;
257 case '[':
258 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000259 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000260 okay[opt_depth] = 1;
261 opt_depth++;
262 command += 2;
263 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000264 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000265 command++;
266 }
267 break;
268 case ']':
269 if (command[1] == ']' && opt_depth > 1) {
270 opt_depth--;
271 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000272 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000273 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000274 command += 2;
275 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000276 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000277 command++;
278 }
279 break;
280 case '%':
281 {
282 char *nextpercent;
283 char *varvalue;
284
285 command++;
286 nextpercent = strchr(command, '%');
287 if (!nextpercent) {
288 errno = EUNBALPER;
289 free(result);
290 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000291 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000292
293 varvalue = get_var(command, nextpercent - command, ifd);
294
295 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200296# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000297 /* "hwaddress <class> <address>":
298 * unlike ifconfig, ip doesnt want <class>
299 * (usually "ether" keyword). Skip it. */
300 if (strncmp(command, "hwaddress", 9) == 0) {
301 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
302 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200303# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000304 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000305 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200306# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000307 /* Sigh... Add a special case for 'ip' to convert from
308 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000309 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000310 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000311 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000312 if (varvalue) {
313 res = count_netmask_bits(varvalue);
314 if (res > 0) {
315 const char *argument = utoa(res);
316 addstr(&result, argument, strlen(argument));
317 command = nextpercent + 1;
318 break;
319 }
Eric Andersen8320b422003-04-02 10:13:26 +0000320 }
Eric Andersen8320b422003-04-02 10:13:26 +0000321 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200322# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000323 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000324 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000325
326 command = nextpercent + 1;
327 }
328 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329 }
330 }
331
332 if (opt_depth > 1) {
333 errno = EUNBALBRACK;
334 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000335 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000336 }
337
338 if (!okay[0]) {
339 errno = EUNDEFVAR;
340 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000341 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342 }
343
Denis Vlasenko05341252006-09-26 20:35:30 +0000344 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000345}
346
Denis Vlasenko05341252006-09-26 20:35:30 +0000347/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000348static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349{
350 char *out;
351 int ret;
352
353 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000354 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000355 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000356 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000358 /* out == "": parsed ok but not all needed variables known, skip */
359 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000360
361 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000362 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000363 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000364 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000365 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200367
368#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
369
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000371#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200372
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200373static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000374{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200375# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000376 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000377 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000378 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000379 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200380# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000381 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200382# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000383}
384
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200385static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000386{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200387# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000388 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200389# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000390 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200391# endif
392}
393
394static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
395{
396 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397}
398
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200399static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400{
Eric Andersen8320b422003-04-02 10:13:26 +0000401 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200402# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000403 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000404 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000405 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
Natanael Copacfcb1da2012-02-17 19:36:26 +0000406 result += execute("[[ip route add ::/0 via %gateway%]][[ prio %metric%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200407# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000408 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000409 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000410 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200411# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000412 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000413}
414
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200415static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000416{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200417# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000418 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200419# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000420 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200421# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000422}
423
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200424# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200425static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000426{
Eric Andersen8320b422003-04-02 10:13:26 +0000427 int result;
428 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000429 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000430 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000431 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000432 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000433 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434}
435
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200436static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437{
Denis Vlasenko05341252006-09-26 20:35:30 +0000438 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000439}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200440# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441
Denis Vlasenko05341252006-09-26 20:35:30 +0000442static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200443# if ENABLE_FEATURE_IFUPDOWN_IP
444 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
445# endif
446 { "static" , static_up6 , static_down6 , },
447 { "manual" , manual_up_down6 , manual_up_down6 , },
448 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000449};
450
Denis Vlasenko05341252006-09-26 20:35:30 +0000451static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000453 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000454 methods6
455};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200456
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000457#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000458
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200459
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000460#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200461
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200462static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000463{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200464# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000465 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000466 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000467 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000468 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200469# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000470 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200471# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472}
473
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200474static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200476# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000477 int result;
478 result = execute("ip addr flush dev %iface%", ifd, exec);
479 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000480 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200481# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000482 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200483# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000484}
485
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200486static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000487{
Eric Andersen8320b422003-04-02 10:13:26 +0000488 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200489# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000490 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
491 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000492 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000493 result += execute("[[ip route add default via %gateway% dev %iface%[[ prio %metric%]]]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000494 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200495# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000496 /* ifconfig said to set iface up before it processes hw %hwaddress%,
497 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000498 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000499 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000500 result += execute("ifconfig %iface% %address% netmask %netmask%"
501 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000502 ifd, exec);
Natanael Copacfcb1da2012-02-17 19:36:26 +0000503 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000504 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200505# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000506}
507
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200508static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000509{
Eric Andersen8320b422003-04-02 10:13:26 +0000510 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200511# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000512 result = execute("ip addr flush dev %iface%", ifd, exec);
513 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200514# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000515 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
516 /* Bringing the interface down deletes the routes in itself.
517 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
518 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000519 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200520# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000521 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000522}
523
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200524# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200525struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000526 const char *name;
527 const char *startcmd;
528 const char *stopcmd;
529};
530
531static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000532 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200533 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000534 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000535 },
536 { "dhclient",
537 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
538 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
539 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000540 { "pump",
541 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
542 "pump -i %iface% -k",
543 },
544 { "udhcpc",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200545 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000546 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000547 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000548 },
549};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200550# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000551
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200552# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200553static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000554{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000555 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200556# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000557 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000558 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000559 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200560# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000561 /* needed if we have hwaddress on dhcp iface */
562 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000563 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200564# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000565 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000566 if (exists_execable(ext_dhcp_clients[i].name))
567 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
568 }
569 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000570 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000571}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200572# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200573static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000574{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200575# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000576 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000577 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000578 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200579# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000580 /* needed if we have hwaddress on dhcp iface */
581 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000582 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200583# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000584 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200585 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000586 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000587}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200588# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200589static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000590 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000591{
592 return 0; /* no dhcp support */
593}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200594# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000595
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200596# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200597static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000598{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000599 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000600 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000601
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000602 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000603 if (exists_execable(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200604 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000605 if (result)
606 break;
607 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000608 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000609
610 if (!result)
611 bb_error_msg("warning: no dhcp clients found and stopped");
612
613 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
614 and it may come back up because udhcpc is still shutting down */
615 usleep(100000);
616 result += static_down(ifd, exec);
617 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000618}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200619# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200620static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000621{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000622 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200623 result = execute(
624 "test -f /var/run/udhcpc.%iface%.pid && "
625 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
626 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000627 /* Also bring the hardware interface down since
628 killing the dhcp client alone doesn't do it.
629 This enables consecutive ifup->ifdown->ifup */
630 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
631 and it may come back up because udhcpc is still shutting down */
632 usleep(100000);
633 result += static_down(ifd, exec);
634 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000635}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200636# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200637static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000638 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000639{
640 return 0; /* no dhcp support */
641}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200642# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000643
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200644static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000645{
646 return 1;
647}
648
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200649static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000650{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000651 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000652 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
653 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654}
655
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200656static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000658 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000659}
660
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200661static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000663 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000664}
665
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200666static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000667{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000668 return execute("start-stop-daemon --start -x wvdial "
669 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000670}
671
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200672static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000673{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000674 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000675 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000676}
677
Denis Vlasenko05341252006-09-26 20:35:30 +0000678static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200679 { "manual" , manual_up_down, manual_up_down, },
680 { "wvdial" , wvdial_up , wvdial_down , },
681 { "ppp" , ppp_up , ppp_down , },
682 { "static" , static_up , static_down , },
683 { "bootp" , bootp_up , static_down , },
684 { "dhcp" , dhcp_up , dhcp_down , },
685 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000686};
687
Denis Vlasenko05341252006-09-26 20:35:30 +0000688static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000690 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000691 methods
692};
693
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200694#endif /* FEATURE_IFUPDOWN_IPV4 */
695
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000696
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200697/* Returns pointer to the next word, or NULL.
698 * In 1st case, advances *buf to the word after this one.
699 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000700static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000701{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000702 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000703 char *word;
704
Glenn L McGrath85737042003-01-14 23:26:57 +0000705 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000706 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000707
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000708 /* Stop on EOL */
709 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000710 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000711
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000712 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000713 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000714
715 /* Unless we are already at NUL, store NUL and advance */
716 if (word[length] != '\0')
717 word[length++] = '\0';
718
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200719 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000720
721 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722}
723
Denis Vlasenko05341252006-09-26 20:35:30 +0000724static 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 +0000725{
726 int i;
727
Denis Vlasenko736230e2006-11-20 19:40:36 +0000728 if (!name)
729 return NULL;
730
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000731 for (i = 0; af[i]; i++) {
732 if (strcmp(af[i]->name, name) == 0) {
733 return af[i];
734 }
735 }
736 return NULL;
737}
738
Denis Vlasenko05341252006-09-26 20:35:30 +0000739static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000740{
741 int i;
742
Denis Vlasenko736230e2006-11-20 19:40:36 +0000743 if (!name)
744 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000745 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746 for (i = 0; i < af->n_methods; i++) {
747 if (strcmp(af->method[i].name, name) == 0) {
748 return &af->method[i];
749 }
750 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000751 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000752}
753
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000754static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000755{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000756 /* Let's try to be compatible.
757 *
758 * "man 5 interfaces" says:
759 * Lines starting with "#" are ignored. Note that end-of-line
760 * comments are NOT supported, comments must be on a line of their own.
761 * A line may be extended across multiple lines by making
762 * the last character a backslash.
763 *
764 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000765 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000766 * be ignored. Blank lines are ignored. Lines may be indented freely.
767 * A "\" character at the very end of the line indicates the next line
768 * should be treated as a continuation of the current one.
769 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000770#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000771 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000772#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000773 struct interface_defn_t *currif = NULL;
774 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000776 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000777 char *first_word;
778 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000779 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
780
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000781 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000782 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000784 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000785#if ENABLE_DESKTOP
786 /* Trailing "\" concatenates lines */
787 char *p;
788 while ((p = last_char_is(buf, '\\')) != NULL) {
789 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000790 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000791 if (!rest_of_line)
792 break;
793 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000794 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000795 free(rest_of_line);
796 buf = p;
797 }
798#endif
799 rest_of_line = buf;
800 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000801 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000802 free(buf);
803 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000804 }
805
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000806 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000807#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000808 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000810 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000811 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000812 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000813 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000814 /*currmap->n_mappings = 0;*/
815 /*currmap->mapping = NULL;*/
816 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000817 {
Eric Andersen8320b422003-04-02 10:13:26 +0000818 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000819 while (*where != NULL) {
820 where = &(*where)->next;
821 }
822 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000823 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 }
Eric Andersen8320b422003-04-02 10:13:26 +0000825 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000826#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000827 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000828 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000829 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000830#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000831 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000832#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000833#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000834 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000836 NULL
837 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000838 char *iface_name;
839 char *address_family_name;
840 char *method_name;
841 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000842
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000843 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000844 iface_name = next_word(&rest_of_line);
845 address_family_name = next_word(&rest_of_line);
846 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000847
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000848 if (method_name == NULL)
849 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000850
851 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000852 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000853
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000854 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000855 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000856
857 currif->iface = xstrdup(iface_name);
858
859 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000860 if (!currif->address_family)
861 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000862
863 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000864 if (!currif->method)
865 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000866
867 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
868 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000869 if ((strcmp(tmp->iface, currif->iface) == 0)
870 && (tmp->address_family == currif->address_family)
871 ) {
872 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000873 }
874 }
875 llist_add_to_end(&(defn->ifaces), (char*)currif);
876
877 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000878 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000879 } else if (strcmp(first_word, "auto") == 0) {
880 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000881
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000882 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000883 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000884 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885 }
886
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000887 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000888 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
889 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000890 }
891 currently_processing = NONE;
892 } else {
893 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000894 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000895 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000896 bb_error_msg_and_die("option with empty value \"%s\"", buf);
897
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200898 /* If not one of "up", "down",... words... */
899 if (index_in_strings(keywords_up_down, first_word) < 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000900 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000901 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000902 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000903 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000904 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000905 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000906 debug_noise("\t%s=%s\n", first_word, rest_of_line);
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200907 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000908 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) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200919 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000920 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000921 currmap->n_mappings++;
922 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000923 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000924 }
925#endif
926 break;
927 case NONE:
928 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000929 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930 }
931 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000932 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000933 } /* while (fgets) */
934
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000936 /* ferror does NOT set errno! */
937 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938 }
939 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000940
941 return defn;
942}
943
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000944static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000945{
946 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200947 char *dst;
948 char *src;
949 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000950
Rob Landleyd921b2e2006-08-03 15:41:12 +0000951 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000952
Denys Vlasenko62152da2009-06-05 21:53:11 +0200953 for (dst = src = result; (c = *src) != '=' && c; src++) {
954 if (c == '-')
955 c = '_';
956 if (c >= 'a' && c <= 'z')
957 c -= ('a' - 'A');
958 if (isalnum(c) || c == '_')
959 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000960 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200961 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000962
963 return result;
964}
965
Rob Landleye813ddb2006-02-28 03:53:14 +0000966static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000967{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000968 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200969 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000970
Denys Vlasenko62152da2009-06-05 21:53:11 +0200971 if (G.my_environ != NULL) {
972 for (pp = G.my_environ; *pp; pp++) {
973 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000974 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200975 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000976 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200977
978 /* note: last element will stay NULL: */
979 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
980 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000981
982 for (i = 0; i < iface->n_options; i++) {
Denys Vlasenko9c28fb82011-10-19 02:37:08 +0200983 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000984 continue;
985 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200986 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987 }
988
Denys Vlasenko62152da2009-06-05 21:53:11 +0200989 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
990 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
991 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
992 *pp++ = setlocalenv("%s=%s", "MODE", mode);
993 if (G.startup_PATH)
994 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000995}
996
997static int doit(char *str)
998{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000999 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001000 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001001 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001002 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001003 pid_t child;
1004 int status;
1005
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001006 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001007 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +01001008 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +00001009 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +01001010 if (child == 0) { /* child */
1011 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +00001012 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001013 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001014 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001015 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1016 return 0;
1017 }
1018 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001019 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020}
1021
Rob Landleye813ddb2006-02-28 03:53:14 +00001022static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001023{
1024 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001025 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026 for (i = 0; i < ifd->n_options; i++) {
1027 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001028 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001029 return 0;
1030 }
1031 }
1032 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001033
Rob Landleyd921b2e2006-08-03 15:41:12 +00001034 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001035 /* heh, we don't bother free'ing it */
1036 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037}
1038
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001039static int check(char *str)
1040{
Eric Andersen8320b422003-04-02 10:13:26 +00001041 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001042}
1043
Eric Andersen8320b422003-04-02 10:13:26 +00001044static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001046 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +00001047 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +00001048 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001049 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001050 if (!execute_all(iface, "up")) return 0;
Peter Korsgaarddd824432011-10-19 02:35:54 +02001051 if (!execute_all(iface, "post-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001052 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001053}
1054
1055static int iface_down(struct interface_defn_t *iface)
1056{
Eric Andersen8320b422003-04-02 10:13:26 +00001057 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001058 set_environ(iface, "stop");
Peter Korsgaarddd824432011-10-19 02:35:54 +02001059 if (!execute_all(iface, "pre-down")) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001060 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001061 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001062 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001063 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001064}
1065
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001066#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001067static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001069 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001070 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001071 pid_t pid;
1072
Denis Vlasenko37188322008-02-16 13:20:56 +00001073 xpiped_pair(infd);
1074 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001076 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001077 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001078
Pascal Bellard926031b2010-07-04 15:32:38 +02001079 if (pid == 0) {
1080 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001081 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001082 close(infd.wr);
1083 close(outfd.rd);
1084 xmove_fd(infd.rd, 0);
1085 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001086 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001087 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001088 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001089 close(infd.rd);
1090 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001091 *in = xfdopen_for_write(infd.wr);
1092 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001093 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001094}
1095
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001096static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001097{
1098 FILE *in, *out;
1099 int i, status;
1100 pid_t pid;
1101
Rob Landleyd921b2e2006-08-03 15:41:12 +00001102 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001103
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001104 /* Run the mapping script. Never fails. */
1105 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001106
1107 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001108 for (i = 0; i < map->n_mappings; i++) {
1109 fprintf(in, "%s\n", map->mapping[i]);
1110 }
1111 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001112 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001113
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001115 /* If the mapping script exited successfully, try to
1116 * grab a line of output and use that as the name of the
1117 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001118 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001120 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001121 /* If we are able to read a line of output from the script,
1122 * remove any trailing whitespace and use this value
1123 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001124 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001125
1126 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001128
1129 free(logical);
1130 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001131 }
1132 }
Eric Andersen8a931792003-07-03 10:20:29 +00001133
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134 fclose(out);
1135
Eric Andersen8a931792003-07-03 10:20:29 +00001136 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001138#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001139
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001140static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001141{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001142 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001143 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001144
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001145 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001146 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001147 && (search->data[iface_len] == '=')
1148 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001149 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001150 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001151 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001152 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001153 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154}
1155
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001156/* read the previous state from the state file */
1157static llist_t *read_iface_state(void)
1158{
1159 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001160 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001161
1162 if (state_fp) {
1163 char *start, *end_ptr;
1164 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1165 /* We should only need to check for a single character */
1166 end_ptr = start + strcspn(start, " \t\n");
1167 *end_ptr = '\0';
1168 llist_add_to(&state_list, start);
1169 }
1170 fclose(state_fp);
1171 }
1172 return state_list;
1173}
1174
1175
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001176int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001177int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001178{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001179 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001180 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001181 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001182 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001183 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184
Denys Vlasenko62152da2009-06-05 21:53:11 +02001185 INIT_G();
1186
1187 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001188 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001189
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001190 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001191 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 /* ifup command */
1193 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194 }
1195
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001196 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001197 argv += optind;
1198 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001199 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001200 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001201 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001202 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203
Eric Andersen8320b422003-04-02 10:13:26 +00001204 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001206 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001208 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001209 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001210 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001212 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 }
1214
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001215 /* Update the interfaces */
1216 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001217 llist_t *iface_list;
1218 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001219 char *iface;
1220 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001222 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001223 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001224
Rob Landleyd921b2e2006-08-03 15:41:12 +00001225 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001226 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001227
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001228 pch = strchr(iface, '=');
1229 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001231 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001233 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001234 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001235
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001236 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001237 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001238 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239
1240 if (cmds == iface_up) {
1241 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001243 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001244 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001245 }
1246 } else {
1247 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001248 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001249 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001250 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001253 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001255
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001256#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001257 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001258 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001259
1260 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001261 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262 for (i = 0; i < currmap->n_matches; i++) {
1263 if (fnmatch(currmap->match[i], liface, 0) != 0)
1264 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001265 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001266 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001267 }
Eric Andersen8a931792003-07-03 10:20:29 +00001268 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001269 break;
1270 }
1271 }
1272 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001273#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274
Eric Andersen8320b422003-04-02 10:13:26 +00001275 iface_list = defn->ifaces;
1276 while (iface_list) {
1277 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001278 if (strcmp(liface, currif->iface) == 0) {
1279 char *oldiface = currif->iface;
1280
1281 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 currif->iface = iface;
1283
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001284 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001285
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001286 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001287 cmds_ret = cmds(currif);
1288 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001289 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001290 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001291 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001292 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001293 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001294 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001295
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001296 currif->iface = oldiface;
1297 }
Eric Andersen8320b422003-04-02 10:13:26 +00001298 iface_list = iface_list->link;
1299 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001300 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001301 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 }
1303
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001304 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001305 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001306 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001307 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001308 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001309 FILE *state_fp;
1310 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001311 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001312 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001313
Lauri Hintsala454fe292011-12-14 16:49:58 +02001314 if (cmds == iface_up && !any_failures) {
Denys Vlasenkod6f50002011-12-15 12:39:25 +01001315 char *newiface = xasprintf("%s=%s", iface, liface);
1316 if (!iface_state) {
Rob Landley8bb50782006-05-26 23:44:51 +00001317 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001318 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001319 free(iface_state->data);
1320 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001321 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001322 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001323 /* Remove an interface from state_list */
1324 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001325 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001326 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001328 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001329 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001330 state = state_list;
1331 while (state) {
1332 if (state->data) {
1333 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001334 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001335 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001336 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001337 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001338 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001339 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001340 next:
1341 free(iface);
1342 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001343 }
1344
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001345 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001346}