blob: f34287580c14596fd30522221711699ef7e4e540 [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
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
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.
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000013 * To adhere to the FHS, the default state file is /var/run/ifstate.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000014 *
Rob Landley1b751c82005-10-28 09:24:33 +000015 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000016 */
17
Rob Landleyd921b2e2006-08-03 15:41:12 +000018#include "busybox.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000019#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <fnmatch.h>
21#include <getopt.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000023#define MAX_OPT_DEPTH 10
24#define EUNBALBRACK 10001
25#define EUNDEFVAR 10002
26#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000027
Eric Andersen233b1702003-06-05 19:37:01 +000028#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
29#define MAX_INTERFACE_LENGTH 10
30#endif
Eric Andersen8320b422003-04-02 10:13:26 +000031
Eric Andersenc7bda1c2004-03-15 08:29:22 +000032#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000033
34/* Forward declaration */
35struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000036
37typedef int (execfn)(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000038
Eric Andersen8320b422003-04-02 10:13:26 +000039struct method_t
40{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041 char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000042 int (*up)(struct interface_defn_t *ifd, execfn *e);
43 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000044};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000045
Eric Andersen8320b422003-04-02 10:13:26 +000046struct address_family_t
47{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048 char *name;
49 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000050 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000051};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000052
Eric Andersen8320b422003-04-02 10:13:26 +000053struct mapping_defn_t
54{
55 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000056
57 int max_matches;
58 int n_matches;
59 char **match;
60
61 char *script;
62
63 int max_mappings;
64 int n_mappings;
65 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000066};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000067
Eric Andersen8320b422003-04-02 10:13:26 +000068struct variable_t
69{
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
Eric Andersenc7bda1c2004-03-15 08:29:22 +000074struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000075{
Denis Vlasenko05341252006-09-26 20:35:30 +000076 const struct address_family_t *address_family;
77 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000078
Rob Landleye813ddb2006-02-28 03:53:14 +000079 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080 int max_options;
81 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000082 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000083};
84
Eric Andersen8320b422003-04-02 10:13:26 +000085struct interfaces_file_t
86{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000087 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000088 llist_t *ifaces;
89 struct mapping_defn_t *mappings;
90};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000091
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000092static unsigned option_mask;
93#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
94enum {
95 OPT_do_all = 0x1,
96 OPT_no_act = 0x2,
97 OPT_verbose = 0x4,
98 OPT_force = 0x8,
99 OPT_no_mappings = 0x10,
100};
101#define DO_ALL (option_mask & OPT_do_all)
102#define NO_ACT (option_mask & OPT_no_act)
103#define VERBOSE (option_mask & OPT_verbose)
104#define FORCE (option_mask & OPT_force)
105#define NO_MAPPINGS (option_mask & OPT_no_mappings)
106
Denis Vlasenko05341252006-09-26 20:35:30 +0000107static char **__myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000108
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000109static char *startup_PATH;
110
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000111#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
112
Eric Andersen66a3af92003-01-27 17:41:19 +0000113#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000114
115static unsigned int count_bits(unsigned int a)
116{
117 unsigned int result;
118 result = (a & 0x55) + ((a >> 1) & 0x55);
119 result = (result & 0x33) + ((result >> 2) & 0x33);
Denis Vlasenko05341252006-09-26 20:35:30 +0000120 return ((result & 0x0F) + ((result >> 4) & 0x0F));
Eric Andersen8320b422003-04-02 10:13:26 +0000121}
122
Eric Andersen66a3af92003-01-27 17:41:19 +0000123static int count_netmask_bits(char *dotted_quad)
124{
Eric Andersen8320b422003-04-02 10:13:26 +0000125 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000126 /* Found a netmask... Check if it is dotted quad */
127 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
128 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000129 result = count_bits(a);
130 result += count_bits(b);
131 result += count_bits(c);
132 result += count_bits(d);
Denis Vlasenko05341252006-09-26 20:35:30 +0000133 return (int)result;
Eric Andersen66a3af92003-01-27 17:41:19 +0000134}
135#endif
136
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000137static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
138{
139 if (*pos + str_length >= *len) {
140 char *newbuf;
141
142 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
143 *buf = newbuf;
144 *len = *len * 2 + str_length + 1;
145 }
146
147 while (str_length-- >= 1) {
148 (*buf)[(*pos)++] = *str;
149 str++;
150 }
151 (*buf)[*pos] = '\0';
152}
153
154static int strncmpz(char *l, char *r, size_t llen)
155{
156 int i = strncmp(l, r, llen);
157
158 if (i == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000159 return -r[llen];
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000160 } else {
Denis Vlasenko05341252006-09-26 20:35:30 +0000161 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000162 }
163}
164
Eric Andersen8320b422003-04-02 10:13:26 +0000165static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000166{
167 int i;
168
169 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000170 char *result;
171 static char label_buf[20];
172 strncpy(label_buf, ifd->iface, 19);
173 label_buf[19]=0;
174 result = strchr(label_buf, ':');
175 if (result) {
176 *result=0;
177 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000178 return label_buf;
Eric Andersen8320b422003-04-02 10:13:26 +0000179 } else if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000180 return ifd->iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000181 } else {
182 for (i = 0; i < ifd->n_options; i++) {
183 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000184 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000185 }
186 }
187 }
188
Denis Vlasenko05341252006-09-26 20:35:30 +0000189 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000190}
191
Eric Andersen8320b422003-04-02 10:13:26 +0000192static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000193{
194
195 char *result = NULL;
196 size_t pos = 0, len = 0;
197 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
198 int okay[MAX_OPT_DEPTH] = { 1 };
199 int opt_depth = 1;
200
201 while (*command) {
202 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000203 default:
204 addstr(&result, &len, &pos, command, 1);
205 command++;
206 break;
207 case '\\':
208 if (command[1]) {
209 addstr(&result, &len, &pos, command + 1, 1);
210 command += 2;
211 } else {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000212 addstr(&result, &len, &pos, command, 1);
213 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000214 }
215 break;
216 case '[':
217 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
218 old_pos[opt_depth] = pos;
219 okay[opt_depth] = 1;
220 opt_depth++;
221 command += 2;
222 } else {
223 addstr(&result, &len, &pos, "[", 1);
224 command++;
225 }
226 break;
227 case ']':
228 if (command[1] == ']' && opt_depth > 1) {
229 opt_depth--;
230 if (!okay[opt_depth]) {
231 pos = old_pos[opt_depth];
232 result[pos] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000233 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000234 command += 2;
235 } else {
236 addstr(&result, &len, &pos, "]", 1);
237 command++;
238 }
239 break;
240 case '%':
241 {
242 char *nextpercent;
243 char *varvalue;
244
245 command++;
246 nextpercent = strchr(command, '%');
247 if (!nextpercent) {
248 errno = EUNBALPER;
249 free(result);
250 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000251 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000252
253 varvalue = get_var(command, nextpercent - command, ifd);
254
255 if (varvalue) {
256 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000257 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000258#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000259 /* Sigh... Add a special case for 'ip' to convert from
260 * dotted quad to bit count style netmasks. */
261 if (strncmp(command, "bnmask", 6)==0) {
262 int res;
263 varvalue = get_var("netmask", 7, ifd);
264 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
265 char argument[255];
266 sprintf(argument, "%d", res);
267 addstr(&result, &len, &pos, argument, strlen(argument));
268 command = nextpercent + 1;
269 break;
Eric Andersen8320b422003-04-02 10:13:26 +0000270 }
Eric Andersen8320b422003-04-02 10:13:26 +0000271 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000272#endif
273 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000274 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000275
276 command = nextpercent + 1;
277 }
278 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000279 }
280 }
281
282 if (opt_depth > 1) {
283 errno = EUNBALBRACK;
284 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000285 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000286 }
287
288 if (!okay[0]) {
289 errno = EUNDEFVAR;
290 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000291 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000292 }
293
Denis Vlasenko05341252006-09-26 20:35:30 +0000294 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000295}
296
Denis Vlasenko05341252006-09-26 20:35:30 +0000297/* execute() returns 1 for success and 0 for failure */
Eric Andersen8320b422003-04-02 10:13:26 +0000298static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000299{
300 char *out;
301 int ret;
302
303 out = parse(command, ifd);
304 if (!out) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000305 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000306 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000307 ret = (*exec)(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000308
309 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000310 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000311 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000312 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000313 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000314}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000315#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000317#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000318static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000319{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000320#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000321 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000322 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000323 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000324 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000325#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000326 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000327#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000328}
329
Eric Andersen8320b422003-04-02 10:13:26 +0000330static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000332#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000333 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000334#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000335 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000336#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000337}
338
Eric Andersen8320b422003-04-02 10:13:26 +0000339static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340{
Eric Andersen8320b422003-04-02 10:13:26 +0000341 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000342#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000343 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
344 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000345 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000346#else
Eric Andersen8320b422003-04-02 10:13:26 +0000347 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
348 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
349 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000350#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000351 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352}
353
Eric Andersen8320b422003-04-02 10:13:26 +0000354static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000355{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000356#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000357 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000358#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000359 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000360#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000361}
362
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000363#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000364static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000365{
Eric Andersen8320b422003-04-02 10:13:26 +0000366 int result;
367 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenko05341252006-09-26 20:35:30 +0000368 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000369 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000370 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000371 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000372 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000373}
374
Eric Andersen8320b422003-04-02 10:13:26 +0000375static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000376{
Denis Vlasenko05341252006-09-26 20:35:30 +0000377 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000379#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000380
Denis Vlasenko05341252006-09-26 20:35:30 +0000381static const struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000382#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000383 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000384#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385 { "static", static_up6, static_down6, },
386 { "loopback", loopback_up6, loopback_down6, },
387};
388
Denis Vlasenko05341252006-09-26 20:35:30 +0000389static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000390 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000391 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392 methods6
393};
394#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
395
396#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000397static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000398{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000399#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000400 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000401 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000402 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000403 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000404#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000405 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000406#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000407}
408
Eric Andersen8320b422003-04-02 10:13:26 +0000409static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000410{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000411#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000412 int result;
413 result = execute("ip addr flush dev %iface%", ifd, exec);
414 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000415 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000416#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000417 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000419}
420
Eric Andersen8320b422003-04-02 10:13:26 +0000421static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000422{
Eric Andersen8320b422003-04-02 10:13:26 +0000423 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000424#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000425 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000426 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
427 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000428 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000429 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000430#else
Eric Andersen8320b422003-04-02 10:13:26 +0000431 result = execute("ifconfig %iface% %address% netmask %netmask% "
Denis Vlasenko05341252006-09-26 20:35:30 +0000432 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
433 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
434 ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000435 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000436 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000437#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000438}
439
Eric Andersen8320b422003-04-02 10:13:26 +0000440static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441{
Eric Andersen8320b422003-04-02 10:13:26 +0000442 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000443#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000444 result = execute("ip addr flush dev %iface%", ifd, exec);
445 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000446#else
Eric Andersen8320b422003-04-02 10:13:26 +0000447 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
448 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000449#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000450 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000451}
452
Eric Andersen8320b422003-04-02 10:13:26 +0000453static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000454{
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000455 if (execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface% "
456 "[[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]", ifd, exec))
457 return 1;
Denis Vlasenko06b543b2006-09-28 22:31:51 +0000458
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000459 /* 2006-09-30: The following are deprecated, and should eventually be
460 * removed. For non-busybox (i.e., other than udhcpc) clients, use
461 * 'iface foo inet manual' in /etc/network/interfaces, and supply
462 * start/stop commands explicitly via up/down. */
Denis Vlasenko06b543b2006-09-28 22:31:51 +0000463
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000464 if (execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]",
465 ifd, exec)) return 1;
466 if (execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
467 ifd, exec)) return 1;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000468 if (execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
469 "[[-l %leasetime%]] %iface%", ifd, exec)) return 1;
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000470
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000471 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472}
473
Eric Andersen8320b422003-04-02 10:13:26 +0000474static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475{
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000476 if (execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
477 ifd, exec)) return 1;
Denis Vlasenko06b543b2006-09-28 22:31:51 +0000478
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000479 /* 2006-09-30: The following are deprecated, and should eventually be
480 * removed. For non-busybox (i.e., other than udhcpc) clients, use
481 * 'iface foo inet manual' in /etc/network/interfaces, and supply
482 * start/stop commands explicitly via up/down. */
Denis Vlasenko06b543b2006-09-28 22:31:51 +0000483
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000484 if (execute("pump -i %iface% -k", ifd, exec)) return 1;
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000485 if (execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
486 ifd, exec)) return 1;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000487 if (execute("dhcpcd -k %iface%", ifd, exec)) return 1;
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000488
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000489 return static_down(ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000490}
491
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000492static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
493{
494 return 1;
495}
496
Eric Andersen8320b422003-04-02 10:13:26 +0000497static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000498{
Denis Vlasenko05341252006-09-26 20:35:30 +0000499 return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
500 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
501 "--returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000502}
503
Eric Andersen8320b422003-04-02 10:13:26 +0000504static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000505{
Denis Vlasenko05341252006-09-26 20:35:30 +0000506 return execute("pon [[%provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507}
508
Eric Andersen8320b422003-04-02 10:13:26 +0000509static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000510{
Denis Vlasenko05341252006-09-26 20:35:30 +0000511 return execute("poff [[%provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000512}
513
Eric Andersen8320b422003-04-02 10:13:26 +0000514static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515{
Denis Vlasenko05341252006-09-26 20:35:30 +0000516 return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
517 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000518}
519
Eric Andersen8320b422003-04-02 10:13:26 +0000520static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000521{
Denis Vlasenko05341252006-09-26 20:35:30 +0000522 return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
523 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000524}
525
Denis Vlasenko05341252006-09-26 20:35:30 +0000526static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000527 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000528 { "wvdial", wvdial_up, wvdial_down, },
529 { "ppp", ppp_up, ppp_down, },
530 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000531 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532 { "dhcp", dhcp_up, dhcp_down, },
533 { "loopback", loopback_up, loopback_down, },
534};
535
Denis Vlasenko05341252006-09-26 20:35:30 +0000536static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000537 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000538 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539 methods
540};
541
542#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
543
Glenn L McGrath85737042003-01-14 23:26:57 +0000544static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545{
Glenn L McGrath85737042003-01-14 23:26:57 +0000546 unsigned short length;
547 char *word;
548
549 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000550 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551 }
552
Glenn L McGrath85737042003-01-14 23:26:57 +0000553 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000554 word = *buf;
555 while (isspace(*word)) {
556 ++word;
557 }
558
559 /* Skip over comments */
560 if (*word == '#') {
Denis Vlasenko05341252006-09-26 20:35:30 +0000561 return NULL;
Eric Andersen3c8bca32003-06-20 10:02:29 +0000562 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000563
Glenn L McGrath85737042003-01-14 23:26:57 +0000564 /* Find the length of this word */
565 length = strcspn(word, " \t\n");
566 if (length == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000567 return NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000568 }
569 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000570 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
571 if (**buf) {
572 **buf = '\0';
573 (*buf)++;
574 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000575
576 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577}
578
Denis Vlasenko05341252006-09-26 20:35:30 +0000579static 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 +0000580{
581 int i;
582
583 for (i = 0; af[i]; i++) {
584 if (strcmp(af[i]->name, name) == 0) {
585 return af[i];
586 }
587 }
588 return NULL;
589}
590
Denis Vlasenko05341252006-09-26 20:35:30 +0000591static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000592{
593 int i;
594
595 for (i = 0; i < af->n_methods; i++) {
596 if (strcmp(af->method[i].name, name) == 0) {
597 return &af->method[i];
598 }
599 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000600 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000601}
602
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000603static const llist_t *find_list_string(const llist_t *list, const char *string)
604{
605 while (list) {
606 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000607 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000608 }
609 list = list->link;
610 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000611 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000612}
613
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000614static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000616#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000617 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000618#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000619 struct interface_defn_t *currif = NULL;
620 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000622 char *firstword;
623 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000624
625 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
626
Rob Landleya6e131d2006-05-29 06:43:55 +0000627 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000628
Rob Landleyd921b2e2006-08-03 15:41:12 +0000629 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000630
Eric Andersen8320b422003-04-02 10:13:26 +0000631 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000632 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000633
Glenn L McGrath85737042003-01-14 23:26:57 +0000634 firstword = next_word(&buf_ptr);
635 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000636 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000637 continue; /* blank line */
638 }
639
640 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000641#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000642 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000643
Glenn L McGrath85737042003-01-14 23:26:57 +0000644 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000645 if (currmap->max_matches == currmap->n_matches) {
646 currmap->max_matches = currmap->max_matches * 2 + 1;
647 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
648 }
649
Rob Landleyd921b2e2006-08-03 15:41:12 +0000650 currmap->match[currmap->n_matches++] = xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000651 }
652 currmap->max_mappings = 0;
653 currmap->n_mappings = 0;
654 currmap->mapping = NULL;
655 currmap->script = NULL;
656 {
Eric Andersen8320b422003-04-02 10:13:26 +0000657 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000658 while (*where != NULL) {
659 where = &(*where)->next;
660 }
661 *where = currmap;
662 currmap->next = NULL;
663 }
Eric Andersen8320b422003-04-02 10:13:26 +0000664 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000665#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000666 currently_processing = MAPPING;
667 } else if (strcmp(firstword, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000668 static const struct address_family_t *const addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000670 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000671#endif
672#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000673 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000675 NULL
676 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000677
Denis Vlasenko05341252006-09-26 20:35:30 +0000678 char *iface_name;
679 char *address_family_name;
680 char *method_name;
681 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000682
Denis Vlasenko05341252006-09-26 20:35:30 +0000683 currif = xzalloc(sizeof(struct interface_defn_t));
684 iface_name = next_word(&buf_ptr);
685 address_family_name = next_word(&buf_ptr);
686 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687
Denis Vlasenko05341252006-09-26 20:35:30 +0000688 if (buf_ptr == NULL) {
689 bb_error_msg("too few parameters for line \"%s\"", buf);
690 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000691 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000692
693 /* ship any trailing whitespace */
694 while (isspace(*buf_ptr)) {
695 ++buf_ptr;
696 }
697
698 if (buf_ptr[0] != '\0') {
699 bb_error_msg("too many parameters \"%s\"", buf);
700 return NULL;
701 }
702
703 currif->iface = xstrdup(iface_name);
704
705 currif->address_family = get_address_family(addr_fams, address_family_name);
706 if (!currif->address_family) {
707 bb_error_msg("unknown address type \"%s\"", address_family_name);
708 return NULL;
709 }
710
711 currif->method = get_method(currif->address_family, method_name);
712 if (!currif->method) {
713 bb_error_msg("unknown method \"%s\"", method_name);
714 return NULL;
715 }
716
717 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
718 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
719 if ((strcmp(tmp->iface, currif->iface) == 0) &&
720 (tmp->address_family == currif->address_family)) {
721 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
722 return NULL;
723 }
724 }
725 llist_add_to_end(&(defn->ifaces), (char*)currif);
726
727 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 currently_processing = IFACE;
729 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000730 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000731
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000732 /* Check the interface isnt already listed */
733 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000734 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000735 }
736
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000737 /* Add the interface to the list */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000738 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000739 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000740 }
741 currently_processing = NONE;
742 } else {
743 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000744 case IFACE:
745 {
746 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000747
Denis Vlasenko05341252006-09-26 20:35:30 +0000748 if (strlen(buf_ptr) == 0) {
749 bb_error_msg("option with empty value \"%s\"", buf);
750 return NULL;
751 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000752
Denis Vlasenko05341252006-09-26 20:35:30 +0000753 if (strcmp(firstword, "up") != 0
754 && strcmp(firstword, "down") != 0
755 && strcmp(firstword, "pre-up") != 0
756 && strcmp(firstword, "post-down") != 0) {
757 for (i = 0; i < currif->n_options; i++) {
758 if (strcmp(currif->option[i].name, firstword) == 0) {
759 bb_error_msg("duplicate option \"%s\"", buf);
760 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000761 }
762 }
763 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000764 }
765 if (currif->n_options >= currif->max_options) {
766 struct variable_t *opt;
Eric Andersen8320b422003-04-02 10:13:26 +0000767
Denis Vlasenko05341252006-09-26 20:35:30 +0000768 currif->max_options = currif->max_options + 10;
769 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
770 currif->option = opt;
771 }
772 currif->option[currif->n_options].name = xstrdup(firstword);
773 currif->option[currif->n_options].value = xstrdup(buf_ptr);
774 if (!currif->option[currif->n_options].name) {
775 perror(filename);
776 return NULL;
777 }
778 if (!currif->option[currif->n_options].value) {
779 perror(filename);
780 return NULL;
781 }
782 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
783 currif->option[currif->n_options].value);
784 currif->n_options++;
785 break;
786 case MAPPING:
Eric Andersen8320b422003-04-02 10:13:26 +0000787#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko05341252006-09-26 20:35:30 +0000788 if (strcmp(firstword, "script") == 0) {
789 if (currmap->script != NULL) {
790 bb_error_msg("duplicate script in mapping \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000791 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000792 } else {
793 currmap->script = xstrdup(next_word(&buf_ptr));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000795 } else if (strcmp(firstword, "map") == 0) {
796 if (currmap->max_mappings == currmap->n_mappings) {
797 currmap->max_mappings = currmap->max_mappings * 2 + 1;
798 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
799 }
800 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
801 currmap->n_mappings++;
802 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000803 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000804 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000805 }
806#endif
807 break;
808 case NONE:
809 default:
810 bb_error_msg("misplaced option \"%s\"", buf);
811 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812 }
813 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000814 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 }
816 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000817 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000818 }
819 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000820
821 return defn;
822}
823
Rob Landleye813ddb2006-02-28 03:53:14 +0000824static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825{
826 char *result;
827 char *here;
828 char *there;
829
Rob Landleyd921b2e2006-08-03 15:41:12 +0000830 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831
832 for (here = there = result; *there != '=' && *there; there++) {
833 if (*there == '-')
834 *there = '_';
835 if (isalpha(*there))
836 *there = toupper(*there);
837
838 if (isalnum(*there) || *there == '_') {
839 *here = *there;
840 here++;
841 }
842 }
Rob Landleya3896512006-05-07 20:20:34 +0000843 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000844
845 return result;
846}
847
Rob Landleye813ddb2006-02-28 03:53:14 +0000848static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000849{
850 char **environend;
851 int i;
852 const int n_env_entries = iface->n_options + 5;
853 char **ppch;
854
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000855 if (__myenviron != NULL) {
856 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857 free(*ppch);
858 *ppch = NULL;
859 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000860 free(__myenviron);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000861 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000862 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000863 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864
865 for (i = 0; i < iface->n_options; i++) {
866 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000867 || strcmp(iface->option[i].name, "down") == 0
868 || strcmp(iface->option[i].name, "pre-up") == 0
869 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000870 continue;
871 }
872 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000873 }
874
875 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000876 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000877 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000878 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000879 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000880}
881
882static int doit(char *str)
883{
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000884 if (option_mask & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000885 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000886 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000887 if (!(option_mask & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000888 pid_t child;
889 int status;
890
891 fflush(NULL);
892 switch (child = fork()) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000893 case -1: /* failure */
894 return 0;
895 case 0: /* child */
896 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
897 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000898 }
899 waitpid(child, &status, 0);
900 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
901 return 0;
902 }
903 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000904 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000905}
906
Rob Landleye813ddb2006-02-28 03:53:14 +0000907static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000908{
909 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000910 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000911 for (i = 0; i < ifd->n_options; i++) {
912 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000913 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 return 0;
915 }
916 }
917 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000918
Rob Landleyd921b2e2006-08-03 15:41:12 +0000919 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Rob Landleye813ddb2006-02-28 03:53:14 +0000920 if (doit(buf) != 1) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000921 return 0;
922 }
923 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924}
925
Eric Andersen8320b422003-04-02 10:13:26 +0000926static int check(char *str) {
927 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000928}
929
Eric Andersen8320b422003-04-02 10:13:26 +0000930static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000931{
Eric Andersen8320b422003-04-02 10:13:26 +0000932 if (!iface->method->up(iface,check)) return -1;
933 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000934 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000935 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000936 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000937 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000938}
939
940static int iface_down(struct interface_defn_t *iface)
941{
Eric Andersen8320b422003-04-02 10:13:26 +0000942 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000944 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000945 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000946 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000947 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948}
949
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000950#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951static int popen2(FILE **in, FILE **out, char *command, ...)
952{
953 va_list ap;
954 char *argv[11] = { command };
955 int argc;
956 int infd[2], outfd[2];
957 pid_t pid;
958
959 argc = 1;
960 va_start(ap, command);
961 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
962 argc++;
963 }
964 argv[argc] = NULL; /* make sure */
965 va_end(ap);
966
967 if (pipe(infd) != 0) {
968 return 0;
969 }
970
971 if (pipe(outfd) != 0) {
972 close(infd[0]);
973 close(infd[1]);
974 return 0;
975 }
976
977 fflush(NULL);
978 switch (pid = fork()) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000979 case -1: /* failure */
980 close(infd[0]);
981 close(infd[1]);
982 close(outfd[0]);
983 close(outfd[1]);
984 return 0;
985 case 0: /* child */
986 dup2(infd[0], 0);
987 dup2(outfd[1], 1);
988 close(infd[0]);
989 close(infd[1]);
990 close(outfd[0]);
991 close(outfd[1]);
992 execvp(command, argv);
993 exit(127);
994 default: /* parent */
995 *in = fdopen(infd[1], "w");
996 *out = fdopen(outfd[0], "r");
997 close(infd[0]);
998 close(outfd[1]);
999 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001000 }
1001 /* unreached */
1002}
1003
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001004static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005{
1006 FILE *in, *out;
1007 int i, status;
1008 pid_t pid;
1009
Rob Landleyd921b2e2006-08-03 15:41:12 +00001010 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001011
Eric Andersen8a931792003-07-03 10:20:29 +00001012 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001013 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001014
1015 /* popen2() returns 0 on failure. */
1016 if (pid == 0)
1017 return logical;
1018
1019 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020 for (i = 0; i < map->n_mappings; i++) {
1021 fprintf(in, "%s\n", map->mapping[i]);
1022 }
1023 fclose(in);
1024 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001025
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001027 /* If the mapping script exited successfully, try to
1028 * grab a line of output and use that as the name of the
1029 * logical interface. */
1030 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031
Eric Andersen233b1702003-06-05 19:37:01 +00001032 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001033 /* If we are able to read a line of output from the script,
1034 * remove any trailing whitespace and use this value
1035 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001036 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001037
1038 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001039 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001040
1041 free(logical);
1042 logical = new_logical;
1043 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001044 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001045 * freshly allocated memory. */
1046 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047 }
1048 }
Eric Andersen8a931792003-07-03 10:20:29 +00001049
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050 fclose(out);
1051
Eric Andersen8a931792003-07-03 10:20:29 +00001052 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001053}
Eric Andersen8a931792003-07-03 10:20:29 +00001054#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001055
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001056static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001057{
Rob Landleya3896512006-05-07 20:20:34 +00001058 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001059 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001060
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001061 while (search) {
1062 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001063 (search->data[iface_len] == '=')) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001064 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001066 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001068 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001069}
1070
Rob Landleydfba7412006-03-06 20:47:33 +00001071int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001072{
Denis Vlasenko05341252006-09-26 20:35:30 +00001073 static const char statefile[] = "/var/run/ifstate";
1074
1075 int (*cmds)(struct interface_defn_t *) = NULL;
Eric Andersen8320b422003-04-02 10:13:26 +00001076 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001077 llist_t *state_list = NULL;
1078 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001079 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001080 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001081 int i;
1082
Manuel Novoa III cad53642003-03-19 09:13:01 +00001083 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001084 /* ifup command */
1085 cmds = iface_up;
1086 } else {
1087 /* ifdown command */
1088 cmds = iface_down;
1089 }
1090
Denis Vlasenko67b23e62006-10-03 21:00:06 +00001091 option_mask = getopt32(argc, argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001092 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001093 if (DO_ALL) bb_show_usage();
1094 } else
1095 if (!DO_ALL) bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096
Eric Andersen8320b422003-04-02 10:13:26 +00001097 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001098 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001099 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100
Eric Andersen3c8bca32003-06-20 10:02:29 +00001101 if (!defn) {
1102 exit(EXIT_FAILURE);
1103 }
1104
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001105 startup_PATH = getenv("PATH");
1106 if (!startup_PATH) startup_PATH = "";
1107
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001108 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001109 if (DO_ALL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001110 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001111 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001112 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001113 /* iface_down */
1114 const llist_t *list = state_list;
1115 while (list) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001116 llist_add_to_end(&target_list, xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001117 list = list->link;
1118 }
1119 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001120 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001121 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001122 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001123 }
1124
1125
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001126 /* Update the interfaces */
1127 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001128 llist_t *iface_list;
1129 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001130 char *iface;
1131 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 char *pch;
1133 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001134 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135
Rob Landleyd921b2e2006-08-03 15:41:12 +00001136 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001137 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001139 pch = strchr(iface, '=');
1140 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001141 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001142 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001144 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001147 if (!FORCE) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001148 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149
1150 if (cmds == iface_up) {
1151 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001152 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001153 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 continue;
1155 }
1156 } else {
1157 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001158 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001159 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160 continue;
1161 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162 }
1163 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001164
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001165#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001166 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001167 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168
1169 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 for (i = 0; i < currmap->n_matches; i++) {
1171 if (fnmatch(currmap->match[i], liface, 0) != 0)
1172 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001173 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001174 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175 }
Eric Andersen8a931792003-07-03 10:20:29 +00001176 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001177 break;
1178 }
1179 }
1180 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001181#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182
Eric Andersen8320b422003-04-02 10:13:26 +00001183
1184 iface_list = defn->ifaces;
1185 while (iface_list) {
1186 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 if (strcmp(liface, currif->iface) == 0) {
1188 char *oldiface = currif->iface;
1189
1190 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001191 currif->iface = iface;
1192
Eric Andersen8320b422003-04-02 10:13:26 +00001193 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001195 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001196 cmds_ret = cmds(currif);
1197 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001198 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001199 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001200 any_failures += 1;
1201 } else if (cmds_ret == 0) {
1202 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001204
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 currif->iface = oldiface;
1206 }
Eric Andersen8320b422003-04-02 10:13:26 +00001207 iface_list = iface_list->link;
1208 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001209 if (VERBOSE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001210 puts("");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 }
1212
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001213 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001214 bb_error_msg("ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001215 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001216 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001217 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001218
1219 if (cmds == iface_up) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001220 char *newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001221 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001222 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001223 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001224 free(iface_state->data);
1225 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001226 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001227 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001228 /* Remove an interface from the linked list */
Rob Landleya6e131d2006-05-29 06:43:55 +00001229 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 }
1231 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 }
1233
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001234 /* Actually write the new state */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001235 if (!NO_ACT) {
Rob Landley4217bab2006-02-26 04:42:05 +00001236 FILE *state_fp = NULL;
Eric Andersen66a3af92003-01-27 17:41:19 +00001237
Rob Landleyd921b2e2006-08-03 15:41:12 +00001238 state_fp = xfopen(statefile, "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001239 while (state_list) {
1240 if (state_list->data) {
1241 fputs(state_list->data, state_fp);
1242 fputc('\n', state_fp);
1243 }
1244 state_list = state_list->link;
1245 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001246 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247 }
1248
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001249 if (any_failures)
1250 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 return 0;
1252}