blob: 7a9709e403bc557de85f0963c494cbc06bc3c169 [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 *
Rob Landley1b751c82005-10-28 09:24:33 +000017 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000018 */
19
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000021#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
24
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;
109};
110#define G (*(struct globals*)&bb_common_bufsiz1)
111#define INIT_G() do { } while (0)
112
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000113
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000114#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
115
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000116static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000117{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000118 /* xasprintf trick will be smaller, but we are often
119 * called with str_length == 1 - don't want to have
120 * THAT much of malloc/freeing! */
121 char *buf = *bufp;
122 int len = (buf ? strlen(buf) : 0);
123 str_length++;
124 buf = xrealloc(buf, len + str_length);
125 /* copies at most str_length-1 chars! */
126 safe_strncpy(buf + len, str, str_length);
127 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000128}
129
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000130static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000131{
132 int i = strncmp(l, r, llen);
133
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000134 if (i == 0)
Denys Vlasenko62152da2009-06-05 21:53:11 +0200135 return - (unsigned char)r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000136 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000137}
138
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000139static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000140{
141 int i;
142
143 if (strncmpz(id, "iface", idlen) == 0) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200144 // ubuntu's ifup doesn't do this:
145 //static char *label_buf;
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000146 //char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200147 //free(label_buf);
148 //label_buf = xstrdup(ifd->iface);
149 // Remove virtual iface suffix
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000150 //result = strchrnul(label_buf, ':');
151 //*result = '\0';
Denys Vlasenko62152da2009-06-05 21:53:11 +0200152 //return label_buf;
153
154 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000155 }
156 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000157 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000158 }
159 for (i = 0; i < ifd->n_options; i++) {
160 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
161 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000162 }
163 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000164 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000165}
166
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000167#if ENABLE_FEATURE_IFUPDOWN_IP
168static int count_netmask_bits(const char *dotted_quad)
169{
170// int result;
171// unsigned a, b, c, d;
172// /* Found a netmask... Check if it is dotted quad */
173// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
174// return -1;
175// if ((a|b|c|d) >> 8)
176// return -1; /* one of numbers is >= 256 */
177// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
178// d = ~d; /* 11110000 -> 00001111 */
179
180 /* Shorter version */
181 int result;
182 struct in_addr ip;
183 unsigned d;
184
185 if (inet_aton(dotted_quad, &ip) == 0)
186 return -1; /* malformed dotted IP */
187 d = ntohl(ip.s_addr); /* IP in host order */
188 d = ~d; /* 11110000 -> 00001111 */
189 if (d & (d+1)) /* check that it is in 00001111 form */
190 return -1; /* no it is not */
191 result = 32;
192 while (d) {
193 d >>= 1;
194 result--;
195 }
196 return result;
197}
198#endif
199
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000200static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000201{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000202 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
203 int okay[MAX_OPT_DEPTH] = { 1 };
204 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000205 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000206
207 while (*command) {
208 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000209 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000210 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000211 command++;
212 break;
213 case '\\':
214 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000215 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000216 command += 2;
217 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000218 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000219 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000220 }
221 break;
222 case '[':
223 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000224 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000225 okay[opt_depth] = 1;
226 opt_depth++;
227 command += 2;
228 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000229 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000230 command++;
231 }
232 break;
233 case ']':
234 if (command[1] == ']' && opt_depth > 1) {
235 opt_depth--;
236 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000237 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000238 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000239 command += 2;
240 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000241 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000242 command++;
243 }
244 break;
245 case '%':
246 {
247 char *nextpercent;
248 char *varvalue;
249
250 command++;
251 nextpercent = strchr(command, '%');
252 if (!nextpercent) {
253 errno = EUNBALPER;
254 free(result);
255 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000256 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000257
258 varvalue = get_var(command, nextpercent - command, ifd);
259
260 if (varvalue) {
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000261#if ENABLE_FEATURE_IFUPDOWN_IP
262 /* "hwaddress <class> <address>":
263 * unlike ifconfig, ip doesnt want <class>
264 * (usually "ether" keyword). Skip it. */
265 if (strncmp(command, "hwaddress", 9) == 0) {
266 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
267 }
268#endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000269 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000270 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000271#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000272 /* Sigh... Add a special case for 'ip' to convert from
273 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000274 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000275 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000276 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000277 if (varvalue) {
278 res = count_netmask_bits(varvalue);
279 if (res > 0) {
280 const char *argument = utoa(res);
281 addstr(&result, argument, strlen(argument));
282 command = nextpercent + 1;
283 break;
284 }
Eric Andersen8320b422003-04-02 10:13:26 +0000285 }
Eric Andersen8320b422003-04-02 10:13:26 +0000286 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000287#endif
288 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000289 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000290
291 command = nextpercent + 1;
292 }
293 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294 }
295 }
296
297 if (opt_depth > 1) {
298 errno = EUNBALBRACK;
299 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000300 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000301 }
302
303 if (!okay[0]) {
304 errno = EUNDEFVAR;
305 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000306 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000307 }
308
Denis Vlasenko05341252006-09-26 20:35:30 +0000309 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000310}
311
Denis Vlasenko05341252006-09-26 20:35:30 +0000312/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000313static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000314{
315 char *out;
316 int ret;
317
318 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000319 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000320 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000321 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000322 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000323 /* out == "": parsed ok but not all needed variables known, skip */
324 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000325
326 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000327 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000328 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000329 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000330 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000332#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000333
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000334#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200335static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000336{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000337#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000338 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000339 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000340 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000341 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000342#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000343 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000344#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000345}
346
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200347static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000348{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000349#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000350 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000351#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000352 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000353#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000354}
355
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200356static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357{
Eric Andersen8320b422003-04-02 10:13:26 +0000358 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000359#if ENABLE_FEATURE_IFUPDOWN_IP
360 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000361 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000362 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
363 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000364#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000365 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000366 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000367 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000368#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000369 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370}
371
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200372static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000373{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000374#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000375 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000376#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000377 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000378#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
380
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000381#if ENABLE_FEATURE_IFUPDOWN_IP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200382static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000383{
Eric Andersen8320b422003-04-02 10:13:26 +0000384 int result;
385 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000386 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000387 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000388 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000389 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000390 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391}
392
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200393static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394{
Denis Vlasenko05341252006-09-26 20:35:30 +0000395 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000396}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000397#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000398
Denis Vlasenko05341252006-09-26 20:35:30 +0000399static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000400#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000401 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000402#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000403 { "static", static_up6, static_down6, },
404 { "loopback", loopback_up6, loopback_down6, },
405};
406
Denis Vlasenko05341252006-09-26 20:35:30 +0000407static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000408 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000409 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000410 methods6
411};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000412#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000413
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000414#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200415static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000416{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000417#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000418 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000419 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000420 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000421 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000422#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000423 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000424#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000425}
426
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200427static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000428{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000429#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000430 int result;
431 result = execute("ip addr flush dev %iface%", ifd, exec);
432 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000433 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000434#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000435 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437}
438
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200439static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000440{
Eric Andersen8320b422003-04-02 10:13:26 +0000441 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000442#if ENABLE_FEATURE_IFUPDOWN_IP
443 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
444 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000445 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000446 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000447 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000448#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000449 /* ifconfig said to set iface up before it processes hw %hwaddress%,
450 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000451 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000452 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000453 result += execute("ifconfig %iface% %address% netmask %netmask%"
454 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000455 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000456 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000457 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000458#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000459}
460
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200461static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000462{
Eric Andersen8320b422003-04-02 10:13:26 +0000463 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000464#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000465 result = execute("ip addr flush dev %iface%", ifd, exec);
466 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000467#else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000468 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
469 /* Bringing the interface down deletes the routes in itself.
470 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
471 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000472 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000473#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000474 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475}
476
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000477#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200478struct dhcp_client_t {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000479 const char *name;
480 const char *startcmd;
481 const char *stopcmd;
482};
483
484static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000485 { "dhcpcd",
486 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
487 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000488 },
489 { "dhclient",
490 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
491 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
492 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000493 { "pump",
494 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
495 "pump -i %iface% -k",
496 },
497 { "udhcpc",
Wade Berrier142c5cb2008-11-14 21:18:45 +0000498 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000499 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000500 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000501 },
502};
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000503#endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000504
Denis Vlasenko85c24712008-03-17 09:04:04 +0000505#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200506static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000508 unsigned i;
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000509#if ENABLE_FEATURE_IFUPDOWN_IP
510 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000511 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000512 return 0;
513#else
514 /* needed if we have hwaddress on dhcp iface */
515 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000516 return 0;
517#endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000518 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000519 if (exists_execable(ext_dhcp_clients[i].name))
520 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
521 }
522 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000523 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000524}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000525#elif ENABLE_APP_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200526static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000527{
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000528#if ENABLE_FEATURE_IFUPDOWN_IP
529 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000530 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000531 return 0;
532#else
533 /* needed if we have hwaddress on dhcp iface */
534 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000535 return 0;
536#endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000537 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000538 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000539 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000540}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000541#else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200542static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000543 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000544{
545 return 0; /* no dhcp support */
546}
547#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548
Denis Vlasenko85c24712008-03-17 09:04:04 +0000549#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200550static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000552 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000553 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000554
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000555 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000556 if (exists_execable(ext_dhcp_clients[i].name)) {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200557 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000558 if (result)
559 break;
560 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000561 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000562
563 if (!result)
564 bb_error_msg("warning: no dhcp clients found and stopped");
565
566 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
567 and it may come back up because udhcpc is still shutting down */
568 usleep(100000);
569 result += static_down(ifd, exec);
570 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000571}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000572#elif ENABLE_APP_UDHCPC
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200573static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000574{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000575 int result;
576 result = execute("kill "
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000577 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000578 /* Also bring the hardware interface down since
579 killing the dhcp client alone doesn't do it.
580 This enables consecutive ifup->ifdown->ifup */
581 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
582 and it may come back up because udhcpc is still shutting down */
583 usleep(100000);
584 result += static_down(ifd, exec);
585 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000586}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000587#else
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200588static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000589 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000590{
591 return 0; /* no dhcp support */
592}
593#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200595static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000596{
597 return 1;
598}
599
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200600static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000601{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000602 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000603 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
604 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000605}
606
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200607static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000608{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000609 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000610}
611
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200612static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000613{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000614 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615}
616
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200617static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000618{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000619 return execute("start-stop-daemon --start -x wvdial "
620 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621}
622
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200623static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000624{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000625 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000626 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627}
628
Denis Vlasenko05341252006-09-26 20:35:30 +0000629static const struct method_t methods[] = {
Denys Vlasenko62152da2009-06-05 21:53:11 +0200630 { "manual" , manual_up_down, manual_up_down, },
631 { "wvdial" , wvdial_up , wvdial_down , },
632 { "ppp" , ppp_up , ppp_down , },
633 { "static" , static_up , static_down , },
634 { "bootp" , bootp_up , static_down , },
635 { "dhcp" , dhcp_up , dhcp_down , },
636 { "loopback", loopback_up , loopback_down , },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000637};
638
Denis Vlasenko05341252006-09-26 20:35:30 +0000639static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000640 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000641 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000642 methods
643};
644
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000645#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646
Glenn L McGrath85737042003-01-14 23:26:57 +0000647static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000648{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000649 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000650 char *word;
651
Glenn L McGrath85737042003-01-14 23:26:57 +0000652 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000653 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000654
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000655 /* Stop on EOL */
656 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000657 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000658
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000659 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000660 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000661
662 /* Unless we are already at NUL, store NUL and advance */
663 if (word[length] != '\0')
664 word[length++] = '\0';
665
Glenn L McGrath85737042003-01-14 23:26:57 +0000666 *buf = word + length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000667
668 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669}
670
Denis Vlasenko05341252006-09-26 20:35:30 +0000671static 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 +0000672{
673 int i;
674
Denis Vlasenko736230e2006-11-20 19:40:36 +0000675 if (!name)
676 return NULL;
677
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000678 for (i = 0; af[i]; i++) {
679 if (strcmp(af[i]->name, name) == 0) {
680 return af[i];
681 }
682 }
683 return NULL;
684}
685
Denis Vlasenko05341252006-09-26 20:35:30 +0000686static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687{
688 int i;
689
Denis Vlasenko736230e2006-11-20 19:40:36 +0000690 if (!name)
691 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000692 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693 for (i = 0; i < af->n_methods; i++) {
694 if (strcmp(af->method[i].name, name) == 0) {
695 return &af->method[i];
696 }
697 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000698 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000699}
700
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000701static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000702{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000703 /* Let's try to be compatible.
704 *
705 * "man 5 interfaces" says:
706 * Lines starting with "#" are ignored. Note that end-of-line
707 * comments are NOT supported, comments must be on a line of their own.
708 * A line may be extended across multiple lines by making
709 * the last character a backslash.
710 *
711 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000712 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000713 * be ignored. Blank lines are ignored. Lines may be indented freely.
714 * A "\" character at the very end of the line indicates the next line
715 * should be treated as a continuation of the current one.
716 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000717#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000718 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000719#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000720 struct interface_defn_t *currif = NULL;
721 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000723 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000724 char *first_word;
725 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000726 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
727
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000728 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000729 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000730
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000731 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000732#if ENABLE_DESKTOP
733 /* Trailing "\" concatenates lines */
734 char *p;
735 while ((p = last_char_is(buf, '\\')) != NULL) {
736 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000737 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000738 if (!rest_of_line)
739 break;
740 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000741 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000742 free(rest_of_line);
743 buf = p;
744 }
745#endif
746 rest_of_line = buf;
747 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000748 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000749 free(buf);
750 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751 }
752
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000753 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000754#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000755 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000756
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000757 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000758 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000759 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000761 /*currmap->max_mappings = 0; - done by xzalloc */
762 /*currmap->n_mappings = 0;*/
763 /*currmap->mapping = NULL;*/
764 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000765 {
Eric Andersen8320b422003-04-02 10:13:26 +0000766 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000767 while (*where != NULL) {
768 where = &(*where)->next;
769 }
770 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000771 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000772 }
Eric Andersen8320b422003-04-02 10:13:26 +0000773 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000774#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000776 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000777 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000778#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000779 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000780#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000781#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000782 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000784 NULL
785 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000786 char *iface_name;
787 char *address_family_name;
788 char *method_name;
789 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000790
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000791 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000792 iface_name = next_word(&rest_of_line);
793 address_family_name = next_word(&rest_of_line);
794 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000795
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000796 if (method_name == NULL)
797 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000798
799 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000800 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000801
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000802 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000803 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000804
805 currif->iface = xstrdup(iface_name);
806
807 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000808 if (!currif->address_family)
809 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000810
811 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000812 if (!currif->method)
813 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000814
815 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
816 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000817 if ((strcmp(tmp->iface, currif->iface) == 0)
818 && (tmp->address_family == currif->address_family)
819 ) {
820 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000821 }
822 }
823 llist_add_to_end(&(defn->ifaces), (char*)currif);
824
825 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000826 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000827 } else if (strcmp(first_word, "auto") == 0) {
828 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000829
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000830 /* Check the interface isnt already listed */
Denis Vlasenko0b791d92009-04-13 20:52:00 +0000831 if (llist_find_str(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000832 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833 }
834
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000835 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000836 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
837 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838 }
839 currently_processing = NONE;
840 } else {
841 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000842 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000843 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000844 bb_error_msg_and_die("option with empty value \"%s\"", buf);
845
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000846 if (strcmp(first_word, "up") != 0
847 && strcmp(first_word, "down") != 0
848 && strcmp(first_word, "pre-up") != 0
849 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000850 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000851 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000852 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000853 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000854 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000855 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000856 }
857 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000858 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000859 currif->option = xrealloc(currif->option,
860 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000861 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000862 debug_noise("\t%s=%s\n", first_word, rest_of_line);
863 currif->option[currif->n_options].name = xstrdup(first_word);
864 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000865 currif->n_options++;
866 break;
867 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000868#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000869 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000870 if (currmap->script != NULL)
871 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000872 currmap->script = xstrdup(next_word(&rest_of_line));
873 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000874 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000875 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000876 currmap->mapping = xrealloc(currmap->mapping,
877 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000878 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000879 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000880 currmap->n_mappings++;
881 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000882 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000883 }
884#endif
885 break;
886 case NONE:
887 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000888 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000889 }
890 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000891 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000892 } /* while (fgets) */
893
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000894 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000895 /* ferror does NOT set errno! */
896 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000897 }
898 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000899
900 return defn;
901}
902
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000903static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000904{
905 char *result;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200906 char *dst;
907 char *src;
908 char c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000909
Rob Landleyd921b2e2006-08-03 15:41:12 +0000910 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000911
Denys Vlasenko62152da2009-06-05 21:53:11 +0200912 for (dst = src = result; (c = *src) != '=' && c; src++) {
913 if (c == '-')
914 c = '_';
915 if (c >= 'a' && c <= 'z')
916 c -= ('a' - 'A');
917 if (isalnum(c) || c == '_')
918 *dst++ = c;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000919 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200920 overlapping_strcpy(dst, src);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000921
922 return result;
923}
924
Rob Landleye813ddb2006-02-28 03:53:14 +0000925static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927 int i;
Denys Vlasenko62152da2009-06-05 21:53:11 +0200928 char **pp;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000929
Denys Vlasenko62152da2009-06-05 21:53:11 +0200930 if (G.my_environ != NULL) {
931 for (pp = G.my_environ; *pp; pp++) {
932 free(*pp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000933 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200934 free(G.my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200936
937 /* note: last element will stay NULL: */
938 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
939 pp = G.my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000940
941 for (i = 0; i < iface->n_options; i++) {
942 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000943 || strcmp(iface->option[i].name, "down") == 0
944 || strcmp(iface->option[i].name, "pre-up") == 0
945 || strcmp(iface->option[i].name, "post-down") == 0
946 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000947 continue;
948 }
Denys Vlasenko62152da2009-06-05 21:53:11 +0200949 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000950 }
951
Denys Vlasenko62152da2009-06-05 21:53:11 +0200952 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
953 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
954 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
955 *pp++ = setlocalenv("%s=%s", "MODE", mode);
956 if (G.startup_PATH)
957 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958}
959
960static int doit(char *str)
961{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000962 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000963 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000965 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000966 pid_t child;
967 int status;
968
969 fflush(NULL);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000970 child = vfork();
Denis Vlasenko2375d752006-12-19 23:15:46 +0000971 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000972 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000973 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000974 case 0: /* child */
Denys Vlasenko62152da2009-06-05 21:53:11 +0200975 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000976 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000977 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000978 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000979 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
980 return 0;
981 }
982 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000983 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000984}
985
Rob Landleye813ddb2006-02-28 03:53:14 +0000986static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987{
988 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000989 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000990 for (i = 0; i < ifd->n_options; i++) {
991 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000992 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000993 return 0;
994 }
995 }
996 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000997
Rob Landleyd921b2e2006-08-03 15:41:12 +0000998 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000999 /* heh, we don't bother free'ing it */
1000 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001001}
1002
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001003static int check(char *str)
1004{
Eric Andersen8320b422003-04-02 10:13:26 +00001005 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001006}
1007
Eric Andersen8320b422003-04-02 10:13:26 +00001008static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001010 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +00001011 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +00001012 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001013 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001014 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001015 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001016}
1017
1018static int iface_down(struct interface_defn_t *iface)
1019{
Eric Andersen8320b422003-04-02 10:13:26 +00001020 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +00001022 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001023 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001024 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001025 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026}
1027
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001028#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001029static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001030{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001031 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001032 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033 pid_t pid;
1034
Denis Vlasenko37188322008-02-16 13:20:56 +00001035 xpiped_pair(infd);
1036 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037
1038 fflush(NULL);
Denis Vlasenko82604e92008-07-01 15:59:42 +00001039 pid = vfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001040
Denis Vlasenko82604e92008-07-01 15:59:42 +00001041 switch (pid) {
1042 case -1: /* failure */
1043 bb_perror_msg_and_die("vfork");
1044 case 0: /* child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001045 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001046 close(infd.wr);
1047 close(outfd.rd);
1048 xmove_fd(infd.rd, 0);
1049 xmove_fd(outfd.wr, 1);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001050 BB_EXECVP(command, argv);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001051 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001052 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001053 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001054 close(infd.rd);
1055 close(outfd.wr);
1056 *in = fdopen(infd.wr, "w");
1057 *out = fdopen(outfd.rd, "r");
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001058 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001059}
1060
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001061static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062{
1063 FILE *in, *out;
1064 int i, status;
1065 pid_t pid;
1066
Rob Landleyd921b2e2006-08-03 15:41:12 +00001067 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001069 /* Run the mapping script. Never fails. */
1070 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001071
1072 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073 for (i = 0; i < map->n_mappings; i++) {
1074 fprintf(in, "%s\n", map->mapping[i]);
1075 }
1076 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001077 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001078
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001079 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001080 /* If the mapping script exited successfully, try to
1081 * grab a line of output and use that as the name of the
1082 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001083 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001084
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001085 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001086 /* If we are able to read a line of output from the script,
1087 * remove any trailing whitespace and use this value
1088 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001089 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001090
1091 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001092 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001093
1094 free(logical);
1095 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096 }
1097 }
Eric Andersen8a931792003-07-03 10:20:29 +00001098
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001099 fclose(out);
1100
Eric Andersen8a931792003-07-03 10:20:29 +00001101 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001102}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001103#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001104
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001105static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001106{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001107 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001108 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001110 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001111 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001112 && (search->data[iface_len] == '=')
1113 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001114 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001116 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001118 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119}
1120
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001121/* read the previous state from the state file */
1122static llist_t *read_iface_state(void)
1123{
1124 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001125 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001126
1127 if (state_fp) {
1128 char *start, *end_ptr;
1129 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1130 /* We should only need to check for a single character */
1131 end_ptr = start + strcspn(start, " \t\n");
1132 *end_ptr = '\0';
1133 llist_add_to(&state_list, start);
1134 }
1135 fclose(state_fp);
1136 }
1137 return state_list;
1138}
1139
1140
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001141int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00001142int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001144 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001145 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001147 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001148 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149
Denys Vlasenko62152da2009-06-05 21:53:11 +02001150 INIT_G();
1151
1152 G.startup_PATH = getenv("PATH");
1153
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001154 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001155 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 /* ifup command */
1157 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158 }
1159
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001160 getopt32(argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001162 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001163 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001164 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001165 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166
Eric Andersen8320b422003-04-02 10:13:26 +00001167 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001169 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001171 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001172 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001173 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001175 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176 }
1177
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178 /* Update the interfaces */
1179 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001180 llist_t *iface_list;
1181 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001182 char *iface;
1183 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001185 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001186 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187
Rob Landleyd921b2e2006-08-03 15:41:12 +00001188 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001189 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001190
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001191 pch = strchr(iface, '=');
1192 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001194 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001195 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001196 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001197 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001198
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001199 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001200 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001201 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202
1203 if (cmds == iface_up) {
1204 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001205 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001206 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207 continue;
1208 }
1209 } else {
1210 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001211 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001212 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 continue;
1214 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001215 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001216 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001217 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001218
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001219#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001220 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001221 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222
1223 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001224 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225 for (i = 0; i < currmap->n_matches; i++) {
1226 if (fnmatch(currmap->match[i], liface, 0) != 0)
1227 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001228 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001229 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 }
Eric Andersen8a931792003-07-03 10:20:29 +00001231 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 break;
1233 }
1234 }
1235 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001236#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001237
Eric Andersen8320b422003-04-02 10:13:26 +00001238 iface_list = defn->ifaces;
1239 while (iface_list) {
1240 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001241 if (strcmp(liface, currif->iface) == 0) {
1242 char *oldiface = currif->iface;
1243
1244 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001245 currif->iface = iface;
1246
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001247 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001248
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001249 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001250 cmds_ret = cmds(currif);
1251 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001252 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001253 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001254 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001255 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001256 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001258
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001259 currif->iface = oldiface;
1260 }
Eric Andersen8320b422003-04-02 10:13:26 +00001261 iface_list = iface_list->link;
1262 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001263 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001264 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265 }
1266
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001267 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001268 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001269 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001270 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001271 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001272 FILE *state_fp;
1273 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001274 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276
1277 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001278 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001279 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001280 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001281 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001282 free(iface_state->data);
1283 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001284 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001285 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001286 /* Remove an interface from state_list */
1287 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001288 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001289 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001290
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001291 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001292 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001293 state = state_list;
1294 while (state) {
1295 if (state->data) {
1296 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001297 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001298 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001299 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001300 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001301 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001302 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001303 }
1304
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001305 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306}