blob: 6429c07e548ee6f09dc1188e18950d754281f4ba [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
Glenn L McGrath0177ce12004-07-21 23:56:31 +000018/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
19
Rob Landleyd921b2e2006-08-03 15:41:12 +000020#include "busybox.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000021#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022#include <fnmatch.h>
23#include <getopt.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000024
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000025#define MAX_OPT_DEPTH 10
26#define EUNBALBRACK 10001
27#define EUNDEFVAR 10002
28#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000029
Eric Andersen233b1702003-06-05 19:37:01 +000030#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
31#define MAX_INTERFACE_LENGTH 10
32#endif
Eric Andersen8320b422003-04-02 10:13:26 +000033
Eric Andersenc7bda1c2004-03-15 08:29:22 +000034#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000035
36/* Forward declaration */
37struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000038
39typedef int (execfn)(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000040
Eric Andersen8320b422003-04-02 10:13:26 +000041struct method_t
42{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000043 char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000044 int (*up)(struct interface_defn_t *ifd, execfn *e);
45 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000046};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000047
Eric Andersen8320b422003-04-02 10:13:26 +000048struct address_family_t
49{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000050 char *name;
51 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000052 struct method_t *method;
53};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
Eric Andersen8320b422003-04-02 10:13:26 +000055struct mapping_defn_t
56{
57 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000058
59 int max_matches;
60 int n_matches;
61 char **match;
62
63 char *script;
64
65 int max_mappings;
66 int n_mappings;
67 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000068};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000069
Eric Andersen8320b422003-04-02 10:13:26 +000070struct variable_t
71{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000072 char *name;
73 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000074};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075
Eric Andersenc7bda1c2004-03-15 08:29:22 +000076struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000077{
Eric Andersen8320b422003-04-02 10:13:26 +000078 struct address_family_t *address_family;
79 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080
Rob Landleye813ddb2006-02-28 03:53:14 +000081 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000082 int max_options;
83 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000084 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000085};
86
Eric Andersen8320b422003-04-02 10:13:26 +000087struct interfaces_file_t
88{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000089 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000090 llist_t *ifaces;
91 struct mapping_defn_t *mappings;
92};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000093
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000094static char no_act = 0;
95static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +000096static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000097
Mike Frysingerb049c0e2006-06-20 23:03:27 +000098#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
99
Eric Andersen66a3af92003-01-27 17:41:19 +0000100#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000101
102static unsigned int count_bits(unsigned int a)
103{
104 unsigned int result;
105 result = (a & 0x55) + ((a >> 1) & 0x55);
106 result = (result & 0x33) + ((result >> 2) & 0x33);
107 return((result & 0x0F) + ((result >> 4) & 0x0F));
108}
109
Eric Andersen66a3af92003-01-27 17:41:19 +0000110static int count_netmask_bits(char *dotted_quad)
111{
Eric Andersen8320b422003-04-02 10:13:26 +0000112 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000113 /* Found a netmask... Check if it is dotted quad */
114 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
115 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000116 result = count_bits(a);
117 result += count_bits(b);
118 result += count_bits(c);
119 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000120 return ((int)result);
121}
122#endif
123
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000124static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
125{
126 if (*pos + str_length >= *len) {
127 char *newbuf;
128
129 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
130 *buf = newbuf;
131 *len = *len * 2 + str_length + 1;
132 }
133
134 while (str_length-- >= 1) {
135 (*buf)[(*pos)++] = *str;
136 str++;
137 }
138 (*buf)[*pos] = '\0';
139}
140
141static int strncmpz(char *l, char *r, size_t llen)
142{
143 int i = strncmp(l, r, llen);
144
145 if (i == 0) {
146 return(-r[llen]);
147 } else {
148 return(i);
149 }
150}
151
Eric Andersen8320b422003-04-02 10:13:26 +0000152static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000153{
154 int i;
155
156 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000157 char *result;
158 static char label_buf[20];
159 strncpy(label_buf, ifd->iface, 19);
160 label_buf[19]=0;
161 result = strchr(label_buf, ':');
162 if (result) {
163 *result=0;
164 }
165 return( label_buf);
166 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000167 return (ifd->iface);
168 } else {
169 for (i = 0; i < ifd->n_options; i++) {
170 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
171 return (ifd->option[i].value);
172 }
173 }
174 }
175
176 return(NULL);
177}
178
Eric Andersen8320b422003-04-02 10:13:26 +0000179static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000180{
181
182 char *result = NULL;
183 size_t pos = 0, len = 0;
184 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
185 int okay[MAX_OPT_DEPTH] = { 1 };
186 int opt_depth = 1;
187
188 while (*command) {
189 switch (*command) {
190
Eric Andersen8320b422003-04-02 10:13:26 +0000191 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192 addstr(&result, &len, &pos, command, 1);
193 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000194 break;
195 case '\\':
196 if (command[1]) {
197 addstr(&result, &len, &pos, command + 1, 1);
198 command += 2;
199 } else {
200 addstr(&result, &len, &pos, command, 1);
201 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000202 }
Eric Andersen8320b422003-04-02 10:13:26 +0000203 break;
204 case '[':
205 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
206 old_pos[opt_depth] = pos;
207 okay[opt_depth] = 1;
208 opt_depth++;
209 command += 2;
210 } else {
211 addstr(&result, &len, &pos, "[", 1);
212 command++;
213 }
214 break;
215 case ']':
216 if (command[1] == ']' && opt_depth > 1) {
217 opt_depth--;
218 if (!okay[opt_depth]) {
219 pos = old_pos[opt_depth];
220 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000221 }
Eric Andersen8320b422003-04-02 10:13:26 +0000222 command += 2;
223 } else {
224 addstr(&result, &len, &pos, "]", 1);
225 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000226 }
Eric Andersen8320b422003-04-02 10:13:26 +0000227 break;
228 case '%':
229 {
230 char *nextpercent;
231 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000232
Eric Andersen8320b422003-04-02 10:13:26 +0000233 command++;
234 nextpercent = strchr(command, '%');
235 if (!nextpercent) {
236 errno = EUNBALPER;
237 free(result);
238 return (NULL);
239 }
240
241 varvalue = get_var(command, nextpercent - command, ifd);
242
243 if (varvalue) {
Rob Landleya3896512006-05-07 20:20:34 +0000244 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000245 } else {
246#ifdef CONFIG_FEATURE_IFUPDOWN_IP
247 /* Sigh... Add a special case for 'ip' to convert from
248 * dotted quad to bit count style netmasks. */
249 if (strncmp(command, "bnmask", 6)==0) {
250 int res;
251 varvalue = get_var("netmask", 7, ifd);
252 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
253 char argument[255];
254 sprintf(argument, "%d", res);
Rob Landleya3896512006-05-07 20:20:34 +0000255 addstr(&result, &len, &pos, argument, strlen(argument));
Eric Andersen8320b422003-04-02 10:13:26 +0000256 command = nextpercent + 1;
257 break;
258 }
259 }
260#endif
261 okay[opt_depth - 1] = 0;
262 }
263
264 command = nextpercent + 1;
265 }
266 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000267 }
268 }
269
270 if (opt_depth > 1) {
271 errno = EUNBALBRACK;
272 free(result);
273 return(NULL);
274 }
275
276 if (!okay[0]) {
277 errno = EUNDEFVAR;
278 free(result);
279 return(NULL);
280 }
281
282 return(result);
283}
284
Eric Andersen8320b422003-04-02 10:13:26 +0000285static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000286{
287 char *out;
288 int ret;
289
290 out = parse(command, ifd);
291 if (!out) {
292 return(0);
293 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294 ret = (*exec) (out);
295
296 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000297 if (ret != 1) {
298 return(0);
299 }
Eric Andersen8320b422003-04-02 10:13:26 +0000300 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000301}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000302#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000303
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000304#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000305static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000306{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000307#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000308 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000309 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000310 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000311 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000312#else
Eric Andersen8320b422003-04-02 10:13:26 +0000313 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000314#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315}
316
Eric Andersen8320b422003-04-02 10:13:26 +0000317static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000318{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000319#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000320 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000321#else
Eric Andersen8320b422003-04-02 10:13:26 +0000322 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000323#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000324}
325
Eric Andersen8320b422003-04-02 10:13:26 +0000326static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000327{
Eric Andersen8320b422003-04-02 10:13:26 +0000328 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000329#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000330 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
331 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000332 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000333#else
Eric Andersen8320b422003-04-02 10:13:26 +0000334 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
335 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
336 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000337#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000338 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339}
340
Eric Andersen8320b422003-04-02 10:13:26 +0000341static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000343#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000344 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000345#else
Eric Andersen8320b422003-04-02 10:13:26 +0000346 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000347#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000348}
349
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000350#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000351static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352{
Eric Andersen8320b422003-04-02 10:13:26 +0000353 int result;
354 result = execute("ip tunnel add %iface% mode sit remote "
355 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000356 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000357 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000358 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000359 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000360}
361
Eric Andersen8320b422003-04-02 10:13:26 +0000362static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363{
Eric Andersen8320b422003-04-02 10:13:26 +0000364 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000365}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000366#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367
Eric Andersen8320b422003-04-02 10:13:26 +0000368static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000369#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000371#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372 { "static", static_up6, static_down6, },
373 { "loopback", loopback_up6, loopback_down6, },
374};
375
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000376static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000378 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379 methods6
380};
381#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
382
383#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000384static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000386#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000387 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000388 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000389 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000390 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000391#else
Eric Andersen8320b422003-04-02 10:13:26 +0000392 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000393#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394}
395
Eric Andersen8320b422003-04-02 10:13:26 +0000396static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000398#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000399 int result;
400 result = execute("ip addr flush dev %iface%", ifd, exec);
401 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000402 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000403#else
Eric Andersen8320b422003-04-02 10:13:26 +0000404 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000405#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000406}
407
Eric Andersen8320b422003-04-02 10:13:26 +0000408static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409{
Eric Andersen8320b422003-04-02 10:13:26 +0000410 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000411#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000412 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000413 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
414 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000415 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000416 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417#else
Eric Andersen8320b422003-04-02 10:13:26 +0000418 result = execute("ifconfig %iface% %address% netmask %netmask% "
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000419 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
420 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
Eric Andersen8320b422003-04-02 10:13:26 +0000421 ifd, exec);
422 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000423 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000424#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000425}
426
Eric Andersen8320b422003-04-02 10:13:26 +0000427static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000428{
Eric Andersen8320b422003-04-02 10:13:26 +0000429 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000430#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000431 result = execute("ip addr flush dev %iface%", ifd, exec);
432 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000433#else
Eric Andersen8320b422003-04-02 10:13:26 +0000434 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
435 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000437 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000438}
439
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000440static int execable(char *program)
441{
442 struct stat buf;
443 if (0 == stat(program, &buf)) {
444 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
445 return(1);
446 }
447 }
448 return(0);
449}
450
Eric Andersen8320b422003-04-02 10:13:26 +0000451static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452{
Eric Andersen8320b422003-04-02 10:13:26 +0000453 if (execable("/sbin/udhcpc")) {
454 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
455 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000457 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
458 } else if (execable("/sbin/dhclient")) {
459 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000460 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000461 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
462 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000463 }
Eric Andersen8320b422003-04-02 10:13:26 +0000464 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000465}
466
Eric Andersen8320b422003-04-02 10:13:26 +0000467static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000468{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000469 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000470 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000471 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
472 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
473 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000474 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000475 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000476 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000477 result = execute("pump -i %iface% -k", ifd, exec);
478 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000479 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000481 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000482 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000483 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000484}
485
Eric Andersen8320b422003-04-02 10:13:26 +0000486static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000487{
Eric Andersen8320b422003-04-02 10:13:26 +0000488 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
489 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
490 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000491}
492
Eric Andersen8320b422003-04-02 10:13:26 +0000493static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000494{
Eric Andersen8320b422003-04-02 10:13:26 +0000495 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000496}
497
Eric Andersen8320b422003-04-02 10:13:26 +0000498static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000499{
Eric Andersen8320b422003-04-02 10:13:26 +0000500 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000501}
502
Eric Andersen8320b422003-04-02 10:13:26 +0000503static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000504{
Eric Andersen8320b422003-04-02 10:13:26 +0000505 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
506 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507}
508
Eric Andersen8320b422003-04-02 10:13:26 +0000509static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000510{
Eric Andersen8320b422003-04-02 10:13:26 +0000511 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
512 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513}
514
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000515static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000516{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000517 { "wvdial", wvdial_up, wvdial_down, },
518 { "ppp", ppp_up, ppp_down, },
519 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000520 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000521 { "dhcp", dhcp_up, dhcp_down, },
522 { "loopback", loopback_up, loopback_down, },
523};
524
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000525static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000526{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000528 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529 methods
530};
531
532#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
533
Glenn L McGrath85737042003-01-14 23:26:57 +0000534static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000535{
Glenn L McGrath85737042003-01-14 23:26:57 +0000536 unsigned short length;
537 char *word;
538
539 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000540 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000541 }
542
Glenn L McGrath85737042003-01-14 23:26:57 +0000543 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000544 word = *buf;
545 while (isspace(*word)) {
546 ++word;
547 }
548
549 /* Skip over comments */
550 if (*word == '#') {
551 return(NULL);
552 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553
Glenn L McGrath85737042003-01-14 23:26:57 +0000554 /* Find the length of this word */
555 length = strcspn(word, " \t\n");
556 if (length == 0) {
557 return(NULL);
558 }
559 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000560 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
561 if (**buf) {
562 **buf = '\0';
563 (*buf)++;
564 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000565
566 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000567}
568
Eric Andersen8320b422003-04-02 10:13:26 +0000569static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000570{
571 int i;
572
573 for (i = 0; af[i]; i++) {
574 if (strcmp(af[i]->name, name) == 0) {
575 return af[i];
576 }
577 }
578 return NULL;
579}
580
Eric Andersen8320b422003-04-02 10:13:26 +0000581static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000582{
583 int i;
584
585 for (i = 0; i < af->n_methods; i++) {
586 if (strcmp(af->method[i].name, name) == 0) {
587 return &af->method[i];
588 }
589 }
590 return(NULL);
591}
592
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000593static const llist_t *find_list_string(const llist_t *list, const char *string)
594{
595 while (list) {
596 if (strcmp(list->data, string) == 0) {
597 return(list);
598 }
599 list = list->link;
600 }
601 return(NULL);
602}
603
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000604static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000605{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000606#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000607 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000608#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000609 struct interface_defn_t *currif = NULL;
610 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000611 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000612 char *firstword;
613 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000614
615 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
616
Rob Landleya6e131d2006-05-29 06:43:55 +0000617 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000618
Rob Landleyd921b2e2006-08-03 15:41:12 +0000619 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000620
Eric Andersen8320b422003-04-02 10:13:26 +0000621 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000622 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000623
Glenn L McGrath85737042003-01-14 23:26:57 +0000624 firstword = next_word(&buf_ptr);
625 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000626 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627 continue; /* blank line */
628 }
629
630 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000631#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000632 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633
Glenn L McGrath85737042003-01-14 23:26:57 +0000634 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000635 if (currmap->max_matches == currmap->n_matches) {
636 currmap->max_matches = currmap->max_matches * 2 + 1;
637 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
638 }
639
Rob Landleyd921b2e2006-08-03 15:41:12 +0000640 currmap->match[currmap->n_matches++] = xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000641 }
642 currmap->max_mappings = 0;
643 currmap->n_mappings = 0;
644 currmap->mapping = NULL;
645 currmap->script = NULL;
646 {
Eric Andersen8320b422003-04-02 10:13:26 +0000647 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000648 while (*where != NULL) {
649 where = &(*where)->next;
650 }
651 *where = currmap;
652 currmap->next = NULL;
653 }
Eric Andersen8320b422003-04-02 10:13:26 +0000654 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000655#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000656 currently_processing = MAPPING;
657 } else if (strcmp(firstword, "iface") == 0) {
658 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000659 char *iface_name;
660 char *address_family_name;
661 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000662 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000663#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
664 &addr_inet,
665#endif
666#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
667 &addr_inet6,
668#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669 NULL
670 };
671
Rob Landleya6e131d2006-05-29 06:43:55 +0000672 currif = xzalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000673 iface_name = next_word(&buf_ptr);
674 address_family_name = next_word(&buf_ptr);
675 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000676
Glenn L McGrath85737042003-01-14 23:26:57 +0000677 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000678 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000679 return NULL;
680 }
681
Eric Andersen3c8bca32003-06-20 10:02:29 +0000682 /* ship any trailing whitespace */
683 while (isspace(*buf_ptr)) {
684 ++buf_ptr;
685 }
686
Glenn L McGrath85737042003-01-14 23:26:57 +0000687 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000688 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689 return NULL;
690 }
691
Rob Landleyd921b2e2006-08-03 15:41:12 +0000692 currif->iface = xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693
694 currif->address_family = get_address_family(addr_fams, address_family_name);
695 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000696 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000697 return NULL;
698 }
699
700 currif->method = get_method(currif->address_family, method_name);
701 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000702 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000703 return NULL;
704 }
705
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000706
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000707 {
Eric Andersen8320b422003-04-02 10:13:26 +0000708 llist_t *iface_list;
Rob Landleye813ddb2006-02-28 03:53:14 +0000709 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
710 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Rob Landleya35886c2006-03-22 16:59:12 +0000711 if ((strcmp(tmp->iface, currif->iface) == 0) &&
Rob Landleye813ddb2006-02-28 03:53:14 +0000712 (tmp->address_family == currif->address_family)) {
Eric Andersen8320b422003-04-02 10:13:26 +0000713 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714 return NULL;
715 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000716 }
717
Rob Landley8bb50782006-05-26 23:44:51 +0000718 llist_add_to_end(&(defn->ifaces), (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000719 }
Eric Andersen8320b422003-04-02 10:13:26 +0000720 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000721 }
722 currently_processing = IFACE;
723 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000724 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000725
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000726 /* Check the interface isnt already listed */
727 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000728 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000729 }
730
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000731 /* Add the interface to the list */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000732 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000733 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734 }
735 currently_processing = NONE;
736 } else {
737 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000738 case IFACE:
739 {
740 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000741
Rob Landleya3896512006-05-07 20:20:34 +0000742 if (strlen(buf_ptr) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000743 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000744 return NULL;
745 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746
Eric Andersen8320b422003-04-02 10:13:26 +0000747 if (strcmp(firstword, "up") != 0
748 && strcmp(firstword, "down") != 0
749 && strcmp(firstword, "pre-up") != 0
750 && strcmp(firstword, "post-down") != 0) {
751 for (i = 0; i < currif->n_options; i++) {
752 if (strcmp(currif->option[i].name, firstword) == 0) {
753 bb_error_msg("duplicate option \"%s\"", buf);
754 return NULL;
755 }
756 }
757 }
758 }
759 if (currif->n_options >= currif->max_options) {
760 struct variable_t *opt;
761
762 currif->max_options = currif->max_options + 10;
763 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
764 currif->option = opt;
765 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000766 currif->option[currif->n_options].name = xstrdup(firstword);
767 currif->option[currif->n_options].value = xstrdup(buf_ptr);
Eric Andersen8320b422003-04-02 10:13:26 +0000768 if (!currif->option[currif->n_options].name) {
769 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000770 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000771 }
772 if (!currif->option[currif->n_options].value) {
773 perror(filename);
774 return NULL;
775 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000776 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000777 currif->option[currif->n_options].value);
778 currif->n_options++;
779 break;
780 case MAPPING:
781#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
782 if (strcmp(firstword, "script") == 0) {
783 if (currmap->script != NULL) {
784 bb_error_msg("duplicate script in mapping \"%s\"", buf);
785 return NULL;
786 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000787 currmap->script = xstrdup(next_word(&buf_ptr));
Eric Andersen8320b422003-04-02 10:13:26 +0000788 }
789 } else if (strcmp(firstword, "map") == 0) {
790 if (currmap->max_mappings == currmap->n_mappings) {
791 currmap->max_mappings = currmap->max_mappings * 2 + 1;
792 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
793 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000794 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
Eric Andersen8320b422003-04-02 10:13:26 +0000795 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000796 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000797 bb_error_msg("misplaced option \"%s\"", buf);
798 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000799 }
Eric Andersen8320b422003-04-02 10:13:26 +0000800#endif
801 break;
802 case NONE:
803 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000804 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000805 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806 }
807 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000808 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809 }
810 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000811 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812 }
813 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000814
815 return defn;
816}
817
Rob Landleye813ddb2006-02-28 03:53:14 +0000818static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000819{
820 char *result;
821 char *here;
822 char *there;
823
Rob Landleyd921b2e2006-08-03 15:41:12 +0000824 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825
826 for (here = there = result; *there != '=' && *there; there++) {
827 if (*there == '-')
828 *there = '_';
829 if (isalpha(*there))
830 *there = toupper(*there);
831
832 if (isalnum(*there) || *there == '_') {
833 *here = *there;
834 here++;
835 }
836 }
Rob Landleya3896512006-05-07 20:20:34 +0000837 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838
839 return result;
840}
841
Rob Landleye813ddb2006-02-28 03:53:14 +0000842static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000843{
844 char **environend;
845 int i;
846 const int n_env_entries = iface->n_options + 5;
847 char **ppch;
848
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000849 if (__myenviron != NULL) {
850 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000851 free(*ppch);
852 *ppch = NULL;
853 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000854 free(__myenviron);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000855 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000856 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000857 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000858
859 for (i = 0; i < iface->n_options; i++) {
860 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000861 || strcmp(iface->option[i].name, "down") == 0
862 || strcmp(iface->option[i].name, "pre-up") == 0
863 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864 continue;
865 }
866 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000867 }
868
869 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000870 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000871 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000872 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000873 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000874}
875
876static int doit(char *str)
877{
878 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000879 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000880 }
881 if (!no_act) {
882 pid_t child;
883 int status;
884
885 fflush(NULL);
886 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000887 case -1: /* failure */
888 return 0;
889 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000890 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000891 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892 }
893 waitpid(child, &status, 0);
894 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
895 return 0;
896 }
897 }
898 return (1);
899}
900
Rob Landleye813ddb2006-02-28 03:53:14 +0000901static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000902{
903 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000904 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000905 for (i = 0; i < ifd->n_options; i++) {
906 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000907 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000908 return 0;
909 }
910 }
911 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000912
Rob Landleyd921b2e2006-08-03 15:41:12 +0000913 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Rob Landleye813ddb2006-02-28 03:53:14 +0000914 if (doit(buf) != 1) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000915 return 0;
916 }
917 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918}
919
Eric Andersen8320b422003-04-02 10:13:26 +0000920static int check(char *str) {
921 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000922}
923
Eric Andersen8320b422003-04-02 10:13:26 +0000924static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000925{
Eric Andersen8320b422003-04-02 10:13:26 +0000926 if (!iface->method->up(iface,check)) return -1;
927 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000928 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000929 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000930 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000931 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000932}
933
934static int iface_down(struct interface_defn_t *iface)
935{
Eric Andersen8320b422003-04-02 10:13:26 +0000936 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000937 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000938 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000939 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000940 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000941 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000942}
943
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000944#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000945static int popen2(FILE **in, FILE **out, char *command, ...)
946{
947 va_list ap;
948 char *argv[11] = { command };
949 int argc;
950 int infd[2], outfd[2];
951 pid_t pid;
952
953 argc = 1;
954 va_start(ap, command);
955 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
956 argc++;
957 }
958 argv[argc] = NULL; /* make sure */
959 va_end(ap);
960
961 if (pipe(infd) != 0) {
962 return 0;
963 }
964
965 if (pipe(outfd) != 0) {
966 close(infd[0]);
967 close(infd[1]);
968 return 0;
969 }
970
971 fflush(NULL);
972 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000973 case -1: /* failure */
974 close(infd[0]);
975 close(infd[1]);
976 close(outfd[0]);
977 close(outfd[1]);
978 return 0;
979 case 0: /* child */
980 dup2(infd[0], 0);
981 dup2(outfd[1], 1);
982 close(infd[0]);
983 close(infd[1]);
984 close(outfd[0]);
985 close(outfd[1]);
986 execvp(command, argv);
987 exit(127);
988 default: /* parent */
989 *in = fdopen(infd[1], "w");
990 *out = fdopen(outfd[0], "r");
991 close(infd[0]);
992 close(outfd[1]);
993 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000994 }
995 /* unreached */
996}
997
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000998static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000999{
1000 FILE *in, *out;
1001 int i, status;
1002 pid_t pid;
1003
Rob Landleyd921b2e2006-08-03 15:41:12 +00001004 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005
Eric Andersen8a931792003-07-03 10:20:29 +00001006 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001007 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001008
1009 /* popen2() returns 0 on failure. */
1010 if (pid == 0)
1011 return logical;
1012
1013 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001014 for (i = 0; i < map->n_mappings; i++) {
1015 fprintf(in, "%s\n", map->mapping[i]);
1016 }
1017 fclose(in);
1018 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001019
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001021 /* If the mapping script exited successfully, try to
1022 * grab a line of output and use that as the name of the
1023 * logical interface. */
1024 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001025
Eric Andersen233b1702003-06-05 19:37:01 +00001026 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001027 /* If we are able to read a line of output from the script,
1028 * remove any trailing whitespace and use this value
1029 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001030 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001031
1032 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001034
1035 free(logical);
1036 logical = new_logical;
1037 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001038 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001039 * freshly allocated memory. */
1040 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041 }
1042 }
Eric Andersen8a931792003-07-03 10:20:29 +00001043
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001044 fclose(out);
1045
Eric Andersen8a931792003-07-03 10:20:29 +00001046 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047}
Eric Andersen8a931792003-07-03 10:20:29 +00001048#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001049
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001050static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001051{
Rob Landleya3896512006-05-07 20:20:34 +00001052 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001053 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001054
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001055 while (search) {
1056 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001057 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001058 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001059 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001060 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001061 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001062 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001063}
1064
Rob Landleydfba7412006-03-06 20:47:33 +00001065int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001066{
Eric Andersen8320b422003-04-02 10:13:26 +00001067 int (*cmds) (struct interface_defn_t *) = NULL;
1068 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001069 llist_t *state_list = NULL;
1070 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001071 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001072 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001074#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001076#endif
1077 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001079 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001080 int i;
1081
Manuel Novoa III cad53642003-03-19 09:13:01 +00001082 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001083 /* ifup command */
1084 cmds = iface_up;
1085 } else {
1086 /* ifdown command */
1087 cmds = iface_down;
1088 }
1089
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001090#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001091 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001092#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001093 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001094#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001095 {
1096 switch (i) {
1097 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001098 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001099 break;
1100 case 'v': /* verbose */
1101 verbose = 1;
1102 break;
1103 case 'a': /* all */
1104 do_all = 1;
1105 break;
1106 case 'n': /* no-act */
1107 no_act = 1;
1108 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001109#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001110 case 'm': /* no-mappings */
1111 run_mappings = 0;
1112 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001113#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001114 case 'f': /* force */
1115 force = 1;
1116 break;
1117 default:
1118 bb_show_usage();
1119 break;
1120 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001121 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122
1123 if (argc - optind > 0) {
1124 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001125 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001126 }
1127 } else {
1128 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001129 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001130 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001131 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132
Eric Andersen8320b422003-04-02 10:13:26 +00001133 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001135 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136
Eric Andersen3c8bca32003-06-20 10:02:29 +00001137 if (!defn) {
1138 exit(EXIT_FAILURE);
1139 }
1140
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001141 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142 if (do_all) {
1143 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001144 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001145 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001146 /* iface_down */
1147 const llist_t *list = state_list;
1148 while (list) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001149 llist_add_to_end(&target_list, xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001150 list = list->link;
1151 }
1152 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001153 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001155 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 }
1157
1158
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001159 /* Update the interfaces */
1160 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001161 llist_t *iface_list;
1162 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001163 char *iface;
1164 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001165 char *pch;
1166 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001167 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168
Rob Landleyd921b2e2006-08-03 15:41:12 +00001169 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001170 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001171
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001172 pch = strchr(iface, '=');
1173 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001175 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001177 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001178 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001179
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001180 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001181 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182
1183 if (cmds == iface_up) {
1184 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001185 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001186 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 continue;
1188 }
1189 } else {
1190 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001191 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001192 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193 continue;
1194 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001195 }
1196 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001197
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001198#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001200 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201
1202 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1203
1204 for (i = 0; i < currmap->n_matches; i++) {
1205 if (fnmatch(currmap->match[i], liface, 0) != 0)
1206 continue;
1207 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001208 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209 }
Eric Andersen8a931792003-07-03 10:20:29 +00001210 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 break;
1212 }
1213 }
1214 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001215#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001216
Eric Andersen8320b422003-04-02 10:13:26 +00001217
1218 iface_list = defn->ifaces;
1219 while (iface_list) {
1220 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221 if (strcmp(liface, currif->iface) == 0) {
1222 char *oldiface = currif->iface;
1223
1224 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225 currif->iface = iface;
1226
Eric Andersen8320b422003-04-02 10:13:26 +00001227 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001229 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001230 cmds_ret = cmds(currif);
1231 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001232 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001233 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001234 any_failures += 1;
1235 } else if (cmds_ret == 0) {
1236 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001237 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001238
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239 currif->iface = oldiface;
1240 }
Eric Andersen8320b422003-04-02 10:13:26 +00001241 iface_list = iface_list->link;
1242 }
1243 if (verbose) {
1244 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001245 }
1246
1247 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001248 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001249 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001251 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252
1253 if (cmds == iface_up) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001254 char *newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001255 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001256 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001258 free(iface_state->data);
1259 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001261 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001262 /* Remove an interface from the linked list */
Rob Landleya6e131d2006-05-29 06:43:55 +00001263 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001264 }
1265 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 }
1267
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001268 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001269 if (!no_act) {
Rob Landley4217bab2006-02-26 04:42:05 +00001270 FILE *state_fp = NULL;
Eric Andersen66a3af92003-01-27 17:41:19 +00001271
Rob Landleyd921b2e2006-08-03 15:41:12 +00001272 state_fp = xfopen(statefile, "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001273 while (state_list) {
1274 if (state_list->data) {
1275 fputs(state_list->data, state_fp);
1276 fputc('\n', state_fp);
1277 }
1278 state_list = state_list->link;
1279 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001280 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001281 }
1282
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001283 if (any_failures)
1284 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001285 return 0;
1286}