blob: 7706a84b7e1134bec09b58f88ead0b513cf75819 [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
Denys Vlasenko043b1e52009-09-06 12:47:55 +020020#include "libbb.h"
21/* After libbb.h, since it needs sys/types.h on some systems */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000023#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000024
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000025#define MAX_OPT_DEPTH 10
26#define EUNBALBRACK 10001
27#define EUNDEFVAR 10002
28#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000029
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000030#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000031#define MAX_INTERFACE_LENGTH 10
32#endif
Eric Andersen8320b422003-04-02 10:13:26 +000033
Wade Berrier142c5cb2008-11-14 21:18:45 +000034#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
35
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000036#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000037
38/* Forward declaration */
39struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000040
Denis Vlasenko097c3242006-11-27 16:59:15 +000041typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000042
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000043struct method_t {
44 const char *name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020045 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
46 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
Eric Andersen8320b422003-04-02 10:13:26 +000047};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000049struct address_family_t {
50 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000051 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000052 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000053};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000055struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000056 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000057
58 int max_matches;
59 int n_matches;
60 char **match;
61
62 char *script;
63
64 int max_mappings;
65 int n_mappings;
66 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000067};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000068
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000069struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070 char *name;
71 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000072};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000073
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000074struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000075 const struct address_family_t *address_family;
76 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077
Rob Landleye813ddb2006-02-28 03:53:14 +000078 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000079 int max_options;
80 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000081 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000082};
83
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000084struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000085 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000086 llist_t *ifaces;
87 struct mapping_defn_t *mappings;
88};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000089
Denys Vlasenko62152da2009-06-05 21:53:11 +020090
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000091#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000092enum {
Denys Vlasenko62152da2009-06-05 21:53:11 +020093 OPT_do_all = 0x1,
94 OPT_no_act = 0x2,
95 OPT_verbose = 0x4,
96 OPT_force = 0x8,
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000097 OPT_no_mappings = 0x10,
98};
Denys Vlasenko62152da2009-06-05 21:53:11 +020099#define DO_ALL (option_mask32 & OPT_do_all)
100#define NO_ACT (option_mask32 & OPT_no_act)
101#define VERBOSE (option_mask32 & OPT_verbose)
102#define FORCE (option_mask32 & OPT_force)
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000103#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000104
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000105
Denys Vlasenko62152da2009-06-05 21:53:11 +0200106struct globals {
107 char **my_environ;
108 const char *startup_PATH;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100109 char *shell;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100110} FIX_ALIASING;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200111#define G (*(struct globals*)&bb_common_bufsiz1)
112#define INIT_G() do { } while (0)
113
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000114
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000115#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
116
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000117static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000118{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000119 /* xasprintf trick will be smaller, but we are often
120 * called with str_length == 1 - don't want to have
121 * THAT much of malloc/freeing! */
122 char *buf = *bufp;
123 int len = (buf ? strlen(buf) : 0);
124 str_length++;
125 buf = xrealloc(buf, len + str_length);
126 /* copies at most str_length-1 chars! */
127 safe_strncpy(buf + len, str, str_length);
128 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000129}
130
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000131static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000132{
133 int i = strncmp(l, r, llen);
134
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000135 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200136 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000137 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000138}
139
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000140static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000141{
142 int i;
143
144 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200145 // ubuntu's ifup doesn't do this:
146 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000147 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200148 //free(label_buf);
149 //label_buf = xstrdup(ifd->iface);
150 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000151 //result = strchrnul(label_buf, ':');
152 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200153 //return label_buf;
154
155 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000156 }
157 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000158 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000159 }
160 for (i = 0; i < ifd->n_options; i++) {
161 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
162 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000163 }
164 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000165 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000166}
167
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200168# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000169static int count_netmask_bits(const char *dotted_quad)
170{
171// int result;
172// unsigned a, b, c, d;
173// /* Found a netmask... Check if it is dotted quad */
174// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
175// return -1;
176// if ((a|b|c|d) >> 8)
177// return -1; /* one of numbers is >= 256 */
178// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
179// d = ~d; /* 11110000 -> 00001111 */
180
181 /* Shorter version */
182 int result;
183 struct in_addr ip;
184 unsigned d;
185
186 if (inet_aton(dotted_quad, &ip) == 0)
187 return -1; /* malformed dotted IP */
188 d = ntohl(ip.s_addr); /* IP in host order */
189 d = ~d; /* 11110000 -> 00001111 */
190 if (d & (d+1)) /* check that it is in 00001111 form */
191 return -1; /* no it is not */
192 result = 32;
193 while (d) {
194 d >>= 1;
195 result--;
196 }
197 return result;
198}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200199# endif
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000200
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000201static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000202{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000203 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
204 int okay[MAX_OPT_DEPTH] = { 1 };
205 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000206 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000207
208 while (*command) {
209 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000210 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000211 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000212 command++;
213 break;
214 case '\\':
215 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000216 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000217 command += 2;
218 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000219 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000220 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000221 }
222 break;
223 case '[':
224 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000225 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000226 okay[opt_depth] = 1;
227 opt_depth++;
228 command += 2;
229 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000230 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000231 command++;
232 }
233 break;
234 case ']':
235 if (command[1] == ']' && opt_depth > 1) {
236 opt_depth--;
237 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000238 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000239 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000240 command += 2;
241 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000242 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000243 command++;
244 }
245 break;
246 case '%':
247 {
248 char *nextpercent;
249 char *varvalue;
250
251 command++;
252 nextpercent = strchr(command, '%');
253 if (!nextpercent) {
254 errno = EUNBALPER;
255 free(result);
256 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000257 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000258
259 varvalue = get_var(command, nextpercent - command, ifd);
260
261 if (varvalue) {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200262# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000263 /* "hwaddress <class> <address>":
264 * unlike ifconfig, ip doesnt want <class>
265 * (usually "ether" keyword). Skip it. */
266 if (strncmp(command, "hwaddress", 9) == 0) {
267 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
268 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200269# endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000270 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000271 } else {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200272# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000273 /* Sigh... Add a special case for 'ip' to convert from
274 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000275 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000276 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000277 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000278 if (varvalue) {
279 res = count_netmask_bits(varvalue);
280 if (res > 0) {
281 const char *argument = utoa(res);
282 addstr(&result, argument, strlen(argument));
283 command = nextpercent + 1;
284 break;
285 }
Eric Andersen8320b422003-04-02 10:13:26 +0000286 }
Eric Andersen8320b422003-04-02 10:13:26 +0000287 }
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200288# endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000289 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000290 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000291
292 command = nextpercent + 1;
293 }
294 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000295 }
296 }
297
298 if (opt_depth > 1) {
299 errno = EUNBALBRACK;
300 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000301 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000302 }
303
304 if (!okay[0]) {
305 errno = EUNDEFVAR;
306 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000307 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000308 }
309
Denis Vlasenko05341252006-09-26 20:35:30 +0000310 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000311}
312
Denis Vlasenko05341252006-09-26 20:35:30 +0000313/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000314static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315{
316 char *out;
317 int ret;
318
319 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000320 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000321 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000322 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000323 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000324 /* out == "": parsed ok but not all needed variables known, skip */
325 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000326
327 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000328 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000329 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000330 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000331 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200333
334#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
335
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000336
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000337#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200338
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200339static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200341# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000342 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000343 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000344 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000345 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200346# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000347 return execute("ifconfig %iface% add ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200348# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349}
350
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200351static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200353# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000354 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200355# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000356 return execute("ifconfig %iface% del ::1", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200357# endif
358}
359
360static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
361{
362 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363}
364
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200365static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366{
Eric Andersen8320b422003-04-02 10:13:26 +0000367 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200368# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000369 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000370 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000371 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
372 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200373# else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000374 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000375 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000376 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200377# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000378 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
380
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200381static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200383# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000384 return execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200385# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000386 return execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200387# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000388}
389
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200390# if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200391static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392{
Eric Andersen8320b422003-04-02 10:13:26 +0000393 int result;
394 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000395 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000396 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000397 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000398 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000399 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400}
401
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200402static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000403{
Denis Vlasenko05341252006-09-26 20:35:30 +0000404 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000405}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200406# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000407
Denis Vlasenko05341252006-09-26 20:35:30 +0000408static const struct method_t methods6[] = {
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200409# if ENABLE_FEATURE_IFUPDOWN_IP
410 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
411# endif
412 { "static" , static_up6 , static_down6 , },
413 { "manual" , manual_up_down6 , manual_up_down6 , },
414 { "loopback" , loopback_up6 , loopback_down6 , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000415};
416
Denis Vlasenko05341252006-09-26 20:35:30 +0000417static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000419 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000420 methods6
421};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200422
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000423#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200425
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000426#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200427
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200428static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200430# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000431 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000432 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000433 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000434 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200435# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000436 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200437# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000438}
439
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200440static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200442# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000443 int result;
444 result = execute("ip addr flush dev %iface%", ifd, exec);
445 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000446 return ((result == 2) ? 2 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200447# else
Denis Vlasenko05341252006-09-26 20:35:30 +0000448 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200449# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450}
451
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200452static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000453{
Eric Andersen8320b422003-04-02 10:13:26 +0000454 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200455# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000456 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
457 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000458 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000459 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000460 return ((result == 3) ? 3 : 0);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200461# else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000462 /* ifconfig said to set iface up before it processes hw %hwaddress%,
463 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000464 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000465 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000466 result += execute("ifconfig %iface% %address% netmask %netmask%"
467 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000468 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000469 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000470 return ((result == 3) ? 3 : 0);
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 static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475{
Eric Andersen8320b422003-04-02 10:13:26 +0000476 int result;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200477# if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000478 result = execute("ip addr flush dev %iface%", ifd, exec);
479 result += execute("ip link set %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200480# else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000481 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
482 /* Bringing the interface down deletes the routes in itself.
483 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
484 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000485 result += execute("ifconfig %iface% down", ifd, exec);
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200486# endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000487 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000488}
489
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200490# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200491struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000492 const char *name;
493 const char *startcmd;
494 const char *stopcmd;
495};
496
497static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000498 { "dhcpcd",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200499 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000500 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000501 },
502 { "dhclient",
503 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
504 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
505 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000506 { "pump",
507 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
508 "pump -i %iface% -k",
509 },
510 { "udhcpc",
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200511 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000512 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000513 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000514 },
515};
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200516# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000517
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200518# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200519static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000520{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000521 unsigned i;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200522# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000523 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000524 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000525 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200526# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000527 /* needed if we have hwaddress on dhcp iface */
528 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000529 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200530# endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000531 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000532 if (exists_execable(ext_dhcp_clients[i].name))
533 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
534 }
535 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000536 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000537}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200538# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200539static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000540{
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200541# if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000542 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000543 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000544 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200545# else
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000546 /* needed if we have hwaddress on dhcp iface */
547 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000548 return 0;
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200549# endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000550 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denys Vlasenko2ab3acb2010-09-03 16:58:37 +0200551 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000552 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200554# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200555static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000556 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000557{
558 return 0; /* no dhcp support */
559}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200560# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000561
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200562# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200563static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000564{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000565 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000566 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000567
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000568 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000569 if (exists_execable(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200570 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000571 if (result)
572 break;
573 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000574 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000575
576 if (!result)
577 bb_error_msg("warning: no dhcp clients found and stopped");
578
579 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
580 and it may come back up because udhcpc is still shutting down */
581 usleep(100000);
582 result += static_down(ifd, exec);
583 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000584}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200585# elif ENABLE_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200586static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000587{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000588 int result;
Denys Vlasenko78abf882010-05-02 20:30:28 +0200589 result = execute(
590 "test -f /var/run/udhcpc.%iface%.pid && "
591 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
592 ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000593 /* Also bring the hardware interface down since
594 killing the dhcp client alone doesn't do it.
595 This enables consecutive ifup->ifdown->ifup */
596 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
597 and it may come back up because udhcpc is still shutting down */
598 usleep(100000);
599 result += static_down(ifd, exec);
600 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000601}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200602# else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200603static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000604 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000605{
606 return 0; /* no dhcp support */
607}
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200608# endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000609
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200610static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000611{
612 return 1;
613}
614
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200615static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000616{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000617 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000618 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
619 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000620}
621
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200622static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000623{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000624 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000625}
626
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200627static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000628{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000629 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000630}
631
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200632static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000634 return execute("start-stop-daemon --start -x wvdial "
635 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000636}
637
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200638static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000639{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000640 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000641 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000642}
643
Denis Vlasenko05341252006-09-26 20:35:30 +0000644static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200645 { "manual" , manual_up_down, manual_up_down, },
646 { "wvdial" , wvdial_up , wvdial_down , },
647 { "ppp" , ppp_up , ppp_down , },
648 { "static" , static_up , static_down , },
649 { "bootp" , bootp_up , static_down , },
650 { "dhcp" , dhcp_up , dhcp_down , },
651 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000652};
653
Denis Vlasenko05341252006-09-26 20:35:30 +0000654static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000655 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000656 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657 methods
658};
659
Denys Vlasenko66cb7be2010-10-29 02:42:20 +0200660#endif /* FEATURE_IFUPDOWN_IPV4 */
661
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200663/* Returns pointer to the next word, or NULL.
664 * In 1st case, advances *buf to the word after this one.
665 */
Glenn L McGrath85737042003-01-14 23:26:57 +0000666static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000667{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000668 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000669 char *word;
670
Glenn L McGrath85737042003-01-14 23:26:57 +0000671 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000672 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000673
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000674 /* Stop on EOL */
675 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000676 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000677
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000678 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000679 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000680
681 /* Unless we are already at NUL, store NUL and advance */
682 if (word[length] != '\0')
683 word[length++] = '\0';
684
Denys Vlasenko2ace0ad2009-10-16 23:56:10 +0200685 *buf = skip_whitespace(word + length);
Glenn L McGrath85737042003-01-14 23:26:57 +0000686
687 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688}
689
Denis Vlasenko05341252006-09-26 20:35:30 +0000690static 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 +0000691{
692 int i;
693
Denis Vlasenko736230e2006-11-20 19:40:36 +0000694 if (!name)
695 return NULL;
696
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000697 for (i = 0; af[i]; i++) {
698 if (strcmp(af[i]->name, name) == 0) {
699 return af[i];
700 }
701 }
702 return NULL;
703}
704
Denis Vlasenko05341252006-09-26 20:35:30 +0000705static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000706{
707 int i;
708
Denis Vlasenko736230e2006-11-20 19:40:36 +0000709 if (!name)
710 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000711 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000712 for (i = 0; i < af->n_methods; i++) {
713 if (strcmp(af->method[i].name, name) == 0) {
714 return &af->method[i];
715 }
716 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000717 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718}
719
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000720static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000721{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000722 /* Let's try to be compatible.
723 *
724 * "man 5 interfaces" says:
725 * Lines starting with "#" are ignored. Note that end-of-line
726 * comments are NOT supported, comments must be on a line of their own.
727 * A line may be extended across multiple lines by making
728 * the last character a backslash.
729 *
730 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000731 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000732 * be ignored. Blank lines are ignored. Lines may be indented freely.
733 * A "\" character at the very end of the line indicates the next line
734 * should be treated as a continuation of the current one.
735 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000736#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000737 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000738#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000739 struct interface_defn_t *currif = NULL;
740 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000741 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000742 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000743 char *first_word;
744 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
746
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000747 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000748 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000749
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000750 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000751#if ENABLE_DESKTOP
752 /* Trailing "\" concatenates lines */
753 char *p;
754 while ((p = last_char_is(buf, '\\')) != NULL) {
755 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000756 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000757 if (!rest_of_line)
758 break;
759 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000760 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000761 free(rest_of_line);
762 buf = p;
763 }
764#endif
765 rest_of_line = buf;
766 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000767 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000768 free(buf);
769 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000770 }
771
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000772 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000773#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000774 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000776 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000777 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000778 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000779 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000780 /*currmap->max_mappings = 0; - done by xzalloc */
781 /*currmap->n_mappings = 0;*/
782 /*currmap->mapping = NULL;*/
783 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000784 {
Eric Andersen8320b422003-04-02 10:13:26 +0000785 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000786 while (*where != NULL) {
787 where = &(*where)->next;
788 }
789 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000790 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000791 }
Eric Andersen8320b422003-04-02 10:13:26 +0000792 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000793#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000795 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000796 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000797#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000798 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000799#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000800#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000801 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000803 NULL
804 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000805 char *iface_name;
806 char *address_family_name;
807 char *method_name;
808 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000810 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000811 iface_name = next_word(&rest_of_line);
812 address_family_name = next_word(&rest_of_line);
813 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000814
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000815 if (method_name == NULL)
816 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000817
818 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000819 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000820
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000821 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000822 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000823
824 currif->iface = xstrdup(iface_name);
825
826 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000827 if (!currif->address_family)
828 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000829
830 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000831 if (!currif->method)
832 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000833
834 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
835 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000836 if ((strcmp(tmp->iface, currif->iface) == 0)
837 && (tmp->address_family == currif->address_family)
838 ) {
839 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000840 }
841 }
842 llist_add_to_end(&(defn->ifaces), (char*)currif);
843
844 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000845 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000846 } else if (strcmp(first_word, "auto") == 0) {
847 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000848
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000849 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000850 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000851 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000852 }
853
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000854 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000855 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
856 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857 }
858 currently_processing = NONE;
859 } else {
860 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000861 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000862 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000863 bb_error_msg_and_die("option with empty value \"%s\"", buf);
864
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000865 if (strcmp(first_word, "up") != 0
866 && strcmp(first_word, "down") != 0
867 && strcmp(first_word, "pre-up") != 0
868 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000869 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000870 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000871 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000872 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000873 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000874 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000875 }
876 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000877 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000878 currif->option = xrealloc(currif->option,
879 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000880 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000881 debug_noise("\t%s=%s\n", first_word, rest_of_line);
882 currif->option[currif->n_options].name = xstrdup(first_word);
883 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000884 currif->n_options++;
885 break;
886 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000887#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000888 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000889 if (currmap->script != NULL)
890 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000891 currmap->script = xstrdup(next_word(&rest_of_line));
892 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000893 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000894 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000895 currmap->mapping = xrealloc(currmap->mapping,
896 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000897 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000898 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000899 currmap->n_mappings++;
900 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000901 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000902 }
903#endif
904 break;
905 case NONE:
906 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000907 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000908 }
909 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000910 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000911 } /* while (fgets) */
912
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000913 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000914 /* ferror does NOT set errno! */
915 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000916 }
917 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918
919 return defn;
920}
921
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000922static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000923{
924 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200925 char *dst;
926 char *src;
927 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000928
Rob Landleyd921b2e2006-08-03 15:41:12 +0000929 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930
Denys Vlasenko62152da2009-06-05 21:53:11 +0200931 for (dst = src = result; (c = *src) != '=' && c; src++) {
932 if (c == '-')
933 c = '_';
934 if (c >= 'a' && c <= 'z')
935 c -= ('a' - 'A');
936 if (isalnum(c) || c == '_')
937 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200939 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000940
941 return result;
942}
943
Rob Landleye813ddb2006-02-28 03:53:14 +0000944static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000945{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000946 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200947 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948
Denys Vlasenko62152da2009-06-05 21:53:11 +0200949 if (G.my_environ != NULL) {
950 for (pp = G.my_environ; *pp; pp++) {
951 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000952 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200953 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000954 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200955
956 /* note: last element will stay NULL: */
957 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
958 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959
960 for (i = 0; i < iface->n_options; i++) {
961 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000962 || strcmp(iface->option[i].name, "down") == 0
963 || strcmp(iface->option[i].name, "pre-up") == 0
964 || strcmp(iface->option[i].name, "post-down") == 0
965 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000966 continue;
967 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200968 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000969 }
970
Denys Vlasenko62152da2009-06-05 21:53:11 +0200971 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
972 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
973 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
974 *pp++ = setlocalenv("%s=%s", "MODE", mode);
975 if (G.startup_PATH)
976 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000977}
978
979static int doit(char *str)
980{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000981 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000982 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000983 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000984 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000985 pid_t child;
986 int status;
987
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100988 fflush_all();
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000989 child = vfork();
Denys Vlasenko681efe22011-03-08 21:00:36 +0100990 if (child < 0) /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000991 return 0;
Denys Vlasenko681efe22011-03-08 21:00:36 +0100992 if (child == 0) { /* child */
993 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000994 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000995 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000996 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000997 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
998 return 0;
999 }
1000 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001001 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001002}
1003
Rob Landleye813ddb2006-02-28 03:53:14 +00001004static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005{
1006 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001007 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001008 for (i = 0; i < ifd->n_options; i++) {
1009 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001010 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001011 return 0;
1012 }
1013 }
1014 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001015
Rob Landleyd921b2e2006-08-03 15:41:12 +00001016 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001017 /* heh, we don't bother free'ing it */
1018 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001019}
1020
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001021static int check(char *str)
1022{
Eric Andersen8320b422003-04-02 10:13:26 +00001023 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001024}
1025
Eric Andersen8320b422003-04-02 10:13:26 +00001026static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001027{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001028 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +00001029 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +00001030 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001031 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001032 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001033 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001034}
1035
1036static int iface_down(struct interface_defn_t *iface)
1037{
Eric Andersen8320b422003-04-02 10:13:26 +00001038 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001039 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +00001040 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001041 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001042 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001043 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001044}
1045
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001046#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001047static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001048{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001049 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001050 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001051 pid_t pid;
1052
Denis Vlasenko37188322008-02-16 13:20:56 +00001053 xpiped_pair(infd);
1054 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001055
Denys Vlasenko8131eea2009-11-02 14:19:51 +01001056 fflush_all();
Pascal Bellard926031b2010-07-04 15:32:38 +02001057 pid = xvfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001058
Pascal Bellard926031b2010-07-04 15:32:38 +02001059 if (pid == 0) {
1060 /* Child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001061 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001062 close(infd.wr);
1063 close(outfd.rd);
1064 xmove_fd(infd.rd, 0);
1065 xmove_fd(outfd.wr, 1);
Pascal Bellard21e8e8d2010-07-04 00:57:03 +02001066 BB_EXECVP_or_die(argv);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001068 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001069 close(infd.rd);
1070 close(outfd.wr);
Denys Vlasenkoa7ccdee2009-11-15 23:28:11 +01001071 *in = xfdopen_for_write(infd.wr);
1072 *out = xfdopen_for_read(outfd.rd);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001073 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001074}
1075
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001076static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001077{
1078 FILE *in, *out;
1079 int i, status;
1080 pid_t pid;
1081
Rob Landleyd921b2e2006-08-03 15:41:12 +00001082 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001083
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001084 /* Run the mapping script. Never fails. */
1085 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001086
1087 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001088 for (i = 0; i < map->n_mappings; i++) {
1089 fprintf(in, "%s\n", map->mapping[i]);
1090 }
1091 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001092 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001093
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001094 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001095 /* If the mapping script exited successfully, try to
1096 * grab a line of output and use that as the name of the
1097 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001098 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001099
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001100 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001101 /* If we are able to read a line of output from the script,
1102 * remove any trailing whitespace and use this value
1103 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001104 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001105
1106 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001108
1109 free(logical);
1110 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001111 }
1112 }
Eric Andersen8a931792003-07-03 10:20:29 +00001113
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114 fclose(out);
1115
Eric Andersen8a931792003-07-03 10:20:29 +00001116 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001118#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001120static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001121{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001122 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001123 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001125 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001126 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001127 && (search->data[iface_len] == '=')
1128 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001129 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001130 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001131 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001133 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134}
1135
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001136/* read the previous state from the state file */
1137static llist_t *read_iface_state(void)
1138{
1139 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001140 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001141
1142 if (state_fp) {
1143 char *start, *end_ptr;
1144 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1145 /* We should only need to check for a single character */
1146 end_ptr = start + strcspn(start, " \t\n");
1147 *end_ptr = '\0';
1148 llist_add_to(&state_list, start);
1149 }
1150 fclose(state_fp);
1151 }
1152 return state_list;
1153}
1154
1155
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001156int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001157int ifupdown_main(int argc UNUSED_PARAM, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001159 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001160 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001161 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001162 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001163 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164
Denys Vlasenko62152da2009-06-05 21:53:11 +02001165 INIT_G();
1166
1167 G.startup_PATH = getenv("PATH");
Denys Vlasenko681efe22011-03-08 21:00:36 +01001168 G.shell = xstrdup(get_shell_name());
Denys Vlasenko62152da2009-06-05 21:53:11 +02001169
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001170 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001171 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172 /* ifup command */
1173 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174 }
1175
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001176 getopt32(argv, OPTION_STR, &interfaces);
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001177 argv += optind;
1178 if (argv[0]) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001179 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001180 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001181 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001182 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001183
Eric Andersen8320b422003-04-02 10:13:26 +00001184 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001185 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001186 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001188 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001189 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001190 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001191 } else {
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001192 llist_add_to_end(&target_list, argv[0]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193 }
1194
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001195 /* Update the interfaces */
1196 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001197 llist_t *iface_list;
1198 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001199 char *iface;
1200 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001202 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001203 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204
Rob Landleyd921b2e2006-08-03 15:41:12 +00001205 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001206 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001208 pch = strchr(iface, '=');
1209 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001211 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001212 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001213 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001214 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001215
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001216 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001217 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001218 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219
1220 if (cmds == iface_up) {
1221 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001222 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001223 bb_error_msg("interface %s already configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001224 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225 }
1226 } else {
1227 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001228 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001229 bb_error_msg("interface %s not configured", iface);
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001230 goto next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001233 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001234 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001235
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001236#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001237 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001238 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239
1240 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001241 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001242 for (i = 0; i < currmap->n_matches; i++) {
1243 if (fnmatch(currmap->match[i], liface, 0) != 0)
1244 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001245 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001246 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247 }
Eric Andersen8a931792003-07-03 10:20:29 +00001248 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001249 break;
1250 }
1251 }
1252 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001253#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254
Eric Andersen8320b422003-04-02 10:13:26 +00001255 iface_list = defn->ifaces;
1256 while (iface_list) {
1257 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258 if (strcmp(liface, currif->iface) == 0) {
1259 char *oldiface = currif->iface;
1260
1261 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262 currif->iface = iface;
1263
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001264 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001266 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001267 cmds_ret = cmds(currif);
1268 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001269 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001270 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001271 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001272 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001273 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001275
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276 currif->iface = oldiface;
1277 }
Eric Andersen8320b422003-04-02 10:13:26 +00001278 iface_list = iface_list->link;
1279 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001280 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001281 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 }
1283
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001284 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001285 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001286 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001287 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001288 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001289 FILE *state_fp;
1290 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001291 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001292 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293
1294 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001295 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001296 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001297 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001299 free(iface_state->data);
1300 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001301 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001302 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001303 /* Remove an interface from state_list */
1304 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001305 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001307
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001308 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001309 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001310 state = state_list;
1311 while (state) {
1312 if (state->data) {
1313 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001314 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001315 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001317 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001318 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001319 }
Alexey Fomenko1df0af72011-03-02 04:13:21 +01001320 next:
1321 free(iface);
1322 free(liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001323 }
1324
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001325 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001326}