blob: 8ee4883640ab2877609ed7a0b0a7ee8de0eda7ce [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
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <sys/stat.h>
21#include <sys/utsname.h>
22#include <sys/wait.h>
23
24#include <ctype.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <fnmatch.h>
28#include <getopt.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000035#include "busybox.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000036
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000037#define MAX_OPT_DEPTH 10
38#define EUNBALBRACK 10001
39#define EUNDEFVAR 10002
40#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041
Eric Andersen233b1702003-06-05 19:37:01 +000042#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43#define MAX_INTERFACE_LENGTH 10
44#endif
Eric Andersen8320b422003-04-02 10:13:26 +000045
Eric Andersenc7bda1c2004-03-15 08:29:22 +000046#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000047
48/* Forward declaration */
49struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000050
51typedef int (execfn)(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000052
Eric Andersen8320b422003-04-02 10:13:26 +000053struct method_t
54{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000055 char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000056 int (*up)(struct interface_defn_t *ifd, execfn *e);
57 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000058};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000059
Eric Andersen8320b422003-04-02 10:13:26 +000060struct address_family_t
61{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000062 char *name;
63 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000064 struct method_t *method;
65};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
Eric Andersen8320b422003-04-02 10:13:26 +000067struct mapping_defn_t
68{
69 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070
71 int max_matches;
72 int n_matches;
73 char **match;
74
75 char *script;
76
77 int max_mappings;
78 int n_mappings;
79 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000080};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000081
Eric Andersen8320b422003-04-02 10:13:26 +000082struct variable_t
83{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000084 char *name;
85 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000086};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000087
Eric Andersenc7bda1c2004-03-15 08:29:22 +000088struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000089{
Eric Andersen8320b422003-04-02 10:13:26 +000090 struct address_family_t *address_family;
91 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000092
Rob Landleye813ddb2006-02-28 03:53:14 +000093 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000094 int max_options;
95 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000096 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000097};
98
Eric Andersen8320b422003-04-02 10:13:26 +000099struct interfaces_file_t
100{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000101 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000102 llist_t *ifaces;
103 struct mapping_defn_t *mappings;
104};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000105
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000106static char no_act = 0;
107static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000108static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000109
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000110#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
111
Eric Andersen66a3af92003-01-27 17:41:19 +0000112#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000113
114static unsigned int count_bits(unsigned int a)
115{
116 unsigned int result;
117 result = (a & 0x55) + ((a >> 1) & 0x55);
118 result = (result & 0x33) + ((result >> 2) & 0x33);
119 return((result & 0x0F) + ((result >> 4) & 0x0F));
120}
121
Eric Andersen66a3af92003-01-27 17:41:19 +0000122static int count_netmask_bits(char *dotted_quad)
123{
Eric Andersen8320b422003-04-02 10:13:26 +0000124 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000125 /* Found a netmask... Check if it is dotted quad */
126 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
127 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000128 result = count_bits(a);
129 result += count_bits(b);
130 result += count_bits(c);
131 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000132 return ((int)result);
133}
134#endif
135
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000136static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
137{
138 if (*pos + str_length >= *len) {
139 char *newbuf;
140
141 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
142 *buf = newbuf;
143 *len = *len * 2 + str_length + 1;
144 }
145
146 while (str_length-- >= 1) {
147 (*buf)[(*pos)++] = *str;
148 str++;
149 }
150 (*buf)[*pos] = '\0';
151}
152
153static int strncmpz(char *l, char *r, size_t llen)
154{
155 int i = strncmp(l, r, llen);
156
157 if (i == 0) {
158 return(-r[llen]);
159 } else {
160 return(i);
161 }
162}
163
Eric Andersen8320b422003-04-02 10:13:26 +0000164static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000165{
166 int i;
167
168 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000169 char *result;
170 static char label_buf[20];
171 strncpy(label_buf, ifd->iface, 19);
172 label_buf[19]=0;
173 result = strchr(label_buf, ':');
174 if (result) {
175 *result=0;
176 }
177 return( label_buf);
178 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000179 return (ifd->iface);
180 } else {
181 for (i = 0; i < ifd->n_options; i++) {
182 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
183 return (ifd->option[i].value);
184 }
185 }
186 }
187
188 return(NULL);
189}
190
Eric Andersen8320b422003-04-02 10:13:26 +0000191static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192{
193
194 char *result = NULL;
195 size_t pos = 0, len = 0;
196 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
197 int okay[MAX_OPT_DEPTH] = { 1 };
198 int opt_depth = 1;
199
200 while (*command) {
201 switch (*command) {
202
Eric Andersen8320b422003-04-02 10:13:26 +0000203 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000204 addstr(&result, &len, &pos, command, 1);
205 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000206 break;
207 case '\\':
208 if (command[1]) {
209 addstr(&result, &len, &pos, command + 1, 1);
210 command += 2;
211 } else {
212 addstr(&result, &len, &pos, command, 1);
213 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000214 }
Eric Andersen8320b422003-04-02 10:13:26 +0000215 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';
Eric Andersen66a3af92003-01-27 17:41:19 +0000233 }
Eric Andersen8320b422003-04-02 10:13:26 +0000234 command += 2;
235 } else {
236 addstr(&result, &len, &pos, "]", 1);
237 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000238 }
Eric Andersen8320b422003-04-02 10:13:26 +0000239 break;
240 case '%':
241 {
242 char *nextpercent;
243 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000244
Eric Andersen8320b422003-04-02 10:13:26 +0000245 command++;
246 nextpercent = strchr(command, '%');
247 if (!nextpercent) {
248 errno = EUNBALPER;
249 free(result);
250 return (NULL);
251 }
252
253 varvalue = get_var(command, nextpercent - command, ifd);
254
255 if (varvalue) {
Rob Landleya3896512006-05-07 20:20:34 +0000256 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000257 } else {
258#ifdef CONFIG_FEATURE_IFUPDOWN_IP
259 /* 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);
Rob Landleya3896512006-05-07 20:20:34 +0000267 addstr(&result, &len, &pos, argument, strlen(argument));
Eric Andersen8320b422003-04-02 10:13:26 +0000268 command = nextpercent + 1;
269 break;
270 }
271 }
272#endif
273 okay[opt_depth - 1] = 0;
274 }
275
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);
285 return(NULL);
286 }
287
288 if (!okay[0]) {
289 errno = EUNDEFVAR;
290 free(result);
291 return(NULL);
292 }
293
294 return(result);
295}
296
Eric Andersen8320b422003-04-02 10:13:26 +0000297static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000298{
299 char *out;
300 int ret;
301
302 out = parse(command, ifd);
303 if (!out) {
304 return(0);
305 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000306 ret = (*exec) (out);
307
308 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000309 if (ret != 1) {
310 return(0);
311 }
Eric Andersen8320b422003-04-02 10:13:26 +0000312 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000313}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000314#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000317static int loopback_up6(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 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000321 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000322 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000323 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000324#else
Eric Andersen8320b422003-04-02 10:13:26 +0000325 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000326#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000327}
328
Eric Andersen8320b422003-04-02 10:13:26 +0000329static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000330{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000331#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000332 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000333#else
Eric Andersen8320b422003-04-02 10:13:26 +0000334 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000335#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000336}
337
Eric Andersen8320b422003-04-02 10:13:26 +0000338static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339{
Eric Andersen8320b422003-04-02 10:13:26 +0000340 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000341#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000342 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
343 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000344 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000345#else
Eric Andersen8320b422003-04-02 10:13:26 +0000346 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
347 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
348 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000349#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000350 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000351}
352
Eric Andersen8320b422003-04-02 10:13:26 +0000353static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000354{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000355#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000356 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000357#else
Eric Andersen8320b422003-04-02 10:13:26 +0000358 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000359#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000360}
361
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000362#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000363static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364{
Eric Andersen8320b422003-04-02 10:13:26 +0000365 int result;
366 result = execute("ip tunnel add %iface% mode sit remote "
367 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000368 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000369 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000370 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000371 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372}
373
Eric Andersen8320b422003-04-02 10:13:26 +0000374static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375{
Eric Andersen8320b422003-04-02 10:13:26 +0000376 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000378#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379
Eric Andersen8320b422003-04-02 10:13:26 +0000380static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000381#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000383#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384 { "static", static_up6, static_down6, },
385 { "loopback", loopback_up6, loopback_down6, },
386};
387
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000388static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000389 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000390 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391 methods6
392};
393#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
394
395#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000396static int loopback_up(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;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000400 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000401 result += execute("ip link set %iface% up", 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 up", 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 loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000410#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000411 int result;
412 result = execute("ip addr flush dev %iface%", ifd, exec);
413 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000414 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000415#else
Eric Andersen8320b422003-04-02 10:13:26 +0000416 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418}
419
Eric Andersen8320b422003-04-02 10:13:26 +0000420static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000421{
Eric Andersen8320b422003-04-02 10:13:26 +0000422 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000423#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000424 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000425 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
426 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000427 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000428 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000429#else
Eric Andersen8320b422003-04-02 10:13:26 +0000430 result = execute("ifconfig %iface% %address% netmask %netmask% "
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000431 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
432 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
Eric Andersen8320b422003-04-02 10:13:26 +0000433 ifd, exec);
434 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000435 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437}
438
Eric Andersen8320b422003-04-02 10:13:26 +0000439static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000440{
Eric Andersen8320b422003-04-02 10:13:26 +0000441 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000442#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000443 result = execute("ip addr flush dev %iface%", ifd, exec);
444 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000445#else
Eric Andersen8320b422003-04-02 10:13:26 +0000446 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
447 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000448#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000449 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450}
451
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000452static int execable(char *program)
453{
454 struct stat buf;
455 if (0 == stat(program, &buf)) {
456 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
457 return(1);
458 }
459 }
460 return(0);
461}
462
Eric Andersen8320b422003-04-02 10:13:26 +0000463static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464{
Eric Andersen8320b422003-04-02 10:13:26 +0000465 if (execable("/sbin/udhcpc")) {
466 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
467 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000468 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000469 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
470 } else if (execable("/sbin/dhclient")) {
471 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000473 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
474 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475 }
Eric Andersen8320b422003-04-02 10:13:26 +0000476 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000477}
478
Eric Andersen8320b422003-04-02 10:13:26 +0000479static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000481 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000482 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000483 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
484 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
485 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000486 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000487 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000488 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000489 result = execute("pump -i %iface% -k", ifd, exec);
490 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000491 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000492 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000493 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000494 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000495 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000496}
497
Eric Andersen8320b422003-04-02 10:13:26 +0000498static int bootp_up(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("bootpc [[--bootfile %bootfile%]] --dev %iface% "
501 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
502 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000503}
504
Eric Andersen8320b422003-04-02 10:13:26 +0000505static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000506{
Eric Andersen8320b422003-04-02 10:13:26 +0000507 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000508}
509
Eric Andersen8320b422003-04-02 10:13:26 +0000510static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000511{
Eric Andersen8320b422003-04-02 10:13:26 +0000512 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513}
514
Eric Andersen8320b422003-04-02 10:13:26 +0000515static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000516{
Eric Andersen8320b422003-04-02 10:13:26 +0000517 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
518 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000519}
520
Eric Andersen8320b422003-04-02 10:13:26 +0000521static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000522{
Eric Andersen8320b422003-04-02 10:13:26 +0000523 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
524 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000525}
526
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000527static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000528{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529 { "wvdial", wvdial_up, wvdial_down, },
530 { "ppp", ppp_up, ppp_down, },
531 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000532 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000533 { "dhcp", dhcp_up, dhcp_down, },
534 { "loopback", loopback_up, loopback_down, },
535};
536
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000537static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000538{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000540 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000541 methods
542};
543
544#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
545
Glenn L McGrath85737042003-01-14 23:26:57 +0000546static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000547{
Glenn L McGrath85737042003-01-14 23:26:57 +0000548 unsigned short length;
549 char *word;
550
551 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000552 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553 }
554
Glenn L McGrath85737042003-01-14 23:26:57 +0000555 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000556 word = *buf;
557 while (isspace(*word)) {
558 ++word;
559 }
560
561 /* Skip over comments */
562 if (*word == '#') {
563 return(NULL);
564 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565
Glenn L McGrath85737042003-01-14 23:26:57 +0000566 /* Find the length of this word */
567 length = strcspn(word, " \t\n");
568 if (length == 0) {
569 return(NULL);
570 }
571 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000572 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
573 if (**buf) {
574 **buf = '\0';
575 (*buf)++;
576 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000577
578 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000579}
580
Eric Andersen8320b422003-04-02 10:13:26 +0000581static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000582{
583 int i;
584
585 for (i = 0; af[i]; i++) {
586 if (strcmp(af[i]->name, name) == 0) {
587 return af[i];
588 }
589 }
590 return NULL;
591}
592
Eric Andersen8320b422003-04-02 10:13:26 +0000593static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594{
595 int i;
596
597 for (i = 0; i < af->n_methods; i++) {
598 if (strcmp(af->method[i].name, name) == 0) {
599 return &af->method[i];
600 }
601 }
602 return(NULL);
603}
604
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000605static const llist_t *find_list_string(const llist_t *list, const char *string)
606{
607 while (list) {
608 if (strcmp(list->data, string) == 0) {
609 return(list);
610 }
611 list = list->link;
612 }
613 return(NULL);
614}
615
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000616static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000617{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000618#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000619 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000620#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000621 struct interface_defn_t *currif = NULL;
622 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000623 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000624 char *firstword;
625 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000626
627 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
628
Rob Landleya6e131d2006-05-29 06:43:55 +0000629 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000630
Manuel Novoa III cad53642003-03-19 09:13:01 +0000631 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000632
Eric Andersen8320b422003-04-02 10:13:26 +0000633 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000634 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000635
Glenn L McGrath85737042003-01-14 23:26:57 +0000636 firstword = next_word(&buf_ptr);
637 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000638 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000639 continue; /* blank line */
640 }
641
642 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000643#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000644 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000645
Glenn L McGrath85737042003-01-14 23:26:57 +0000646 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000647 if (currmap->max_matches == currmap->n_matches) {
648 currmap->max_matches = currmap->max_matches * 2 + 1;
649 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
650 }
651
Manuel Novoa III cad53642003-03-19 09:13:01 +0000652 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000653 }
654 currmap->max_mappings = 0;
655 currmap->n_mappings = 0;
656 currmap->mapping = NULL;
657 currmap->script = NULL;
658 {
Eric Andersen8320b422003-04-02 10:13:26 +0000659 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660 while (*where != NULL) {
661 where = &(*where)->next;
662 }
663 *where = currmap;
664 currmap->next = NULL;
665 }
Eric Andersen8320b422003-04-02 10:13:26 +0000666 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000667#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668 currently_processing = MAPPING;
669 } else if (strcmp(firstword, "iface") == 0) {
670 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000671 char *iface_name;
672 char *address_family_name;
673 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000674 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000675#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
676 &addr_inet,
677#endif
678#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
679 &addr_inet6,
680#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681 NULL
682 };
683
Rob Landleya6e131d2006-05-29 06:43:55 +0000684 currif = xzalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000685 iface_name = next_word(&buf_ptr);
686 address_family_name = next_word(&buf_ptr);
687 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688
Glenn L McGrath85737042003-01-14 23:26:57 +0000689 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000690 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000691 return NULL;
692 }
693
Eric Andersen3c8bca32003-06-20 10:02:29 +0000694 /* ship any trailing whitespace */
695 while (isspace(*buf_ptr)) {
696 ++buf_ptr;
697 }
698
Glenn L McGrath85737042003-01-14 23:26:57 +0000699 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000700 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000701 return NULL;
702 }
703
Manuel Novoa III cad53642003-03-19 09:13:01 +0000704 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000705
706 currif->address_family = get_address_family(addr_fams, address_family_name);
707 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000708 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709 return NULL;
710 }
711
712 currif->method = get_method(currif->address_family, method_name);
713 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000714 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000715 return NULL;
716 }
717
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000719 {
Eric Andersen8320b422003-04-02 10:13:26 +0000720 llist_t *iface_list;
Rob Landleye813ddb2006-02-28 03:53:14 +0000721 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
722 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Rob Landleya35886c2006-03-22 16:59:12 +0000723 if ((strcmp(tmp->iface, currif->iface) == 0) &&
Rob Landleye813ddb2006-02-28 03:53:14 +0000724 (tmp->address_family == currif->address_family)) {
Eric Andersen8320b422003-04-02 10:13:26 +0000725 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000726 return NULL;
727 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 }
729
Rob Landley8bb50782006-05-26 23:44:51 +0000730 llist_add_to_end(&(defn->ifaces), (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000731 }
Eric Andersen8320b422003-04-02 10:13:26 +0000732 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000733 }
734 currently_processing = IFACE;
735 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000736 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000737
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000738 /* Check the interface isnt already listed */
739 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000740 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000741 }
742
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000743 /* Add the interface to the list */
Rob Landley8bb50782006-05-26 23:44:51 +0000744 llist_add_to_end(&(defn->autointerfaces), bb_xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000745 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746 }
747 currently_processing = NONE;
748 } else {
749 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000750 case IFACE:
751 {
752 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000753
Rob Landleya3896512006-05-07 20:20:34 +0000754 if (strlen(buf_ptr) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000755 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000756 return NULL;
757 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000758
Eric Andersen8320b422003-04-02 10:13:26 +0000759 if (strcmp(firstword, "up") != 0
760 && strcmp(firstword, "down") != 0
761 && strcmp(firstword, "pre-up") != 0
762 && strcmp(firstword, "post-down") != 0) {
763 for (i = 0; i < currif->n_options; i++) {
764 if (strcmp(currif->option[i].name, firstword) == 0) {
765 bb_error_msg("duplicate option \"%s\"", buf);
766 return NULL;
767 }
768 }
769 }
770 }
771 if (currif->n_options >= currif->max_options) {
772 struct variable_t *opt;
773
774 currif->max_options = currif->max_options + 10;
775 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
776 currif->option = opt;
777 }
778 currif->option[currif->n_options].name = bb_xstrdup(firstword);
779 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
780 if (!currif->option[currif->n_options].name) {
781 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000782 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000783 }
784 if (!currif->option[currif->n_options].value) {
785 perror(filename);
786 return NULL;
787 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000788 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000789 currif->option[currif->n_options].value);
790 currif->n_options++;
791 break;
792 case MAPPING:
793#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
794 if (strcmp(firstword, "script") == 0) {
795 if (currmap->script != NULL) {
796 bb_error_msg("duplicate script in mapping \"%s\"", buf);
797 return NULL;
798 } else {
799 currmap->script = bb_xstrdup(next_word(&buf_ptr));
800 }
801 } else if (strcmp(firstword, "map") == 0) {
802 if (currmap->max_mappings == currmap->n_mappings) {
803 currmap->max_mappings = currmap->max_mappings * 2 + 1;
804 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
805 }
806 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
807 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000808 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000809 bb_error_msg("misplaced option \"%s\"", buf);
810 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000811 }
Eric Andersen8320b422003-04-02 10:13:26 +0000812#endif
813 break;
814 case NONE:
815 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000816 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000817 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000818 }
819 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000820 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000821 }
822 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000823 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 }
825 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000826
827 return defn;
828}
829
Rob Landleye813ddb2006-02-28 03:53:14 +0000830static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831{
832 char *result;
833 char *here;
834 char *there;
835
Rob Landleya6e131d2006-05-29 06:43:55 +0000836 result = bb_xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000837
838 for (here = there = result; *there != '=' && *there; there++) {
839 if (*there == '-')
840 *there = '_';
841 if (isalpha(*there))
842 *there = toupper(*there);
843
844 if (isalnum(*there) || *there == '_') {
845 *here = *there;
846 here++;
847 }
848 }
Rob Landleya3896512006-05-07 20:20:34 +0000849 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000850
851 return result;
852}
853
Rob Landleye813ddb2006-02-28 03:53:14 +0000854static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000855{
856 char **environend;
857 int i;
858 const int n_env_entries = iface->n_options + 5;
859 char **ppch;
860
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000861 if (__myenviron != NULL) {
862 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000863 free(*ppch);
864 *ppch = NULL;
865 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000866 free(__myenviron);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000867 }
Rob Landleya6e131d2006-05-29 06:43:55 +0000868 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000869 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000870
871 for (i = 0; i < iface->n_options; i++) {
872 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000873 || strcmp(iface->option[i].name, "down") == 0
874 || strcmp(iface->option[i].name, "pre-up") == 0
875 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000876 continue;
877 }
878 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000879 }
880
881 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000882 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000884 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885 *(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 +0000886}
887
888static int doit(char *str)
889{
890 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000891 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892 }
893 if (!no_act) {
894 pid_t child;
895 int status;
896
897 fflush(NULL);
898 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000899 case -1: /* failure */
900 return 0;
901 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000902 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000903 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000904 }
905 waitpid(child, &status, 0);
906 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
907 return 0;
908 }
909 }
910 return (1);
911}
912
Rob Landleye813ddb2006-02-28 03:53:14 +0000913static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914{
915 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000916 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000917 for (i = 0; i < ifd->n_options; i++) {
918 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000919 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000920 return 0;
921 }
922 }
923 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000924
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000925 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Rob Landleye813ddb2006-02-28 03:53:14 +0000926 if (doit(buf) != 1) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000927 return 0;
928 }
929 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930}
931
Eric Andersen8320b422003-04-02 10:13:26 +0000932static int check(char *str) {
933 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000934}
935
Eric Andersen8320b422003-04-02 10:13:26 +0000936static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000937{
Eric Andersen8320b422003-04-02 10:13:26 +0000938 if (!iface->method->up(iface,check)) return -1;
939 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000940 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000941 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000942 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000943 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000944}
945
946static int iface_down(struct interface_defn_t *iface)
947{
Eric Andersen8320b422003-04-02 10:13:26 +0000948 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000950 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000951 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000952 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000953 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000954}
955
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000956#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000957static int popen2(FILE **in, FILE **out, char *command, ...)
958{
959 va_list ap;
960 char *argv[11] = { command };
961 int argc;
962 int infd[2], outfd[2];
963 pid_t pid;
964
965 argc = 1;
966 va_start(ap, command);
967 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
968 argc++;
969 }
970 argv[argc] = NULL; /* make sure */
971 va_end(ap);
972
973 if (pipe(infd) != 0) {
974 return 0;
975 }
976
977 if (pipe(outfd) != 0) {
978 close(infd[0]);
979 close(infd[1]);
980 return 0;
981 }
982
983 fflush(NULL);
984 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000985 case -1: /* failure */
986 close(infd[0]);
987 close(infd[1]);
988 close(outfd[0]);
989 close(outfd[1]);
990 return 0;
991 case 0: /* child */
992 dup2(infd[0], 0);
993 dup2(outfd[1], 1);
994 close(infd[0]);
995 close(infd[1]);
996 close(outfd[0]);
997 close(outfd[1]);
998 execvp(command, argv);
999 exit(127);
1000 default: /* parent */
1001 *in = fdopen(infd[1], "w");
1002 *out = fdopen(outfd[0], "r");
1003 close(infd[0]);
1004 close(outfd[1]);
1005 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001006 }
1007 /* unreached */
1008}
1009
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001010static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001011{
1012 FILE *in, *out;
1013 int i, status;
1014 pid_t pid;
1015
Eric Andersen8a931792003-07-03 10:20:29 +00001016 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001017
Eric Andersen8a931792003-07-03 10:20:29 +00001018 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001019 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001020
1021 /* popen2() returns 0 on failure. */
1022 if (pid == 0)
1023 return logical;
1024
1025 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026 for (i = 0; i < map->n_mappings; i++) {
1027 fprintf(in, "%s\n", map->mapping[i]);
1028 }
1029 fclose(in);
1030 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001031
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001032 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001033 /* If the mapping script exited successfully, try to
1034 * grab a line of output and use that as the name of the
1035 * logical interface. */
1036 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037
Eric Andersen233b1702003-06-05 19:37:01 +00001038 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001039 /* If we are able to read a line of output from the script,
1040 * remove any trailing whitespace and use this value
1041 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001042 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001043
1044 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001046
1047 free(logical);
1048 logical = new_logical;
1049 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001050 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001051 * freshly allocated memory. */
1052 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001053 }
1054 }
Eric Andersen8a931792003-07-03 10:20:29 +00001055
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001056 fclose(out);
1057
Eric Andersen8a931792003-07-03 10:20:29 +00001058 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001059}
Eric Andersen8a931792003-07-03 10:20:29 +00001060#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001061
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001062static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001063{
Rob Landleya3896512006-05-07 20:20:34 +00001064 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001065 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001066
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001067 while (search) {
1068 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001069 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001070 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001071 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001072 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001074 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075}
1076
Rob Landleydfba7412006-03-06 20:47:33 +00001077int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078{
Eric Andersen8320b422003-04-02 10:13:26 +00001079 int (*cmds) (struct interface_defn_t *) = NULL;
1080 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001081 llist_t *state_list = NULL;
1082 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001083 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001084 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001086#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001087 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001088#endif
1089 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001090 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001091 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001092 int i;
1093
Manuel Novoa III cad53642003-03-19 09:13:01 +00001094 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001095 /* ifup command */
1096 cmds = iface_up;
1097 } else {
1098 /* ifdown command */
1099 cmds = iface_down;
1100 }
1101
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001102#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001103 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001104#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001105 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001106#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001107 {
1108 switch (i) {
1109 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001110 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001111 break;
1112 case 'v': /* verbose */
1113 verbose = 1;
1114 break;
1115 case 'a': /* all */
1116 do_all = 1;
1117 break;
1118 case 'n': /* no-act */
1119 no_act = 1;
1120 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001121#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001122 case 'm': /* no-mappings */
1123 run_mappings = 0;
1124 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001125#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001126 case 'f': /* force */
1127 force = 1;
1128 break;
1129 default:
1130 bb_show_usage();
1131 break;
1132 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001133 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134
1135 if (argc - optind > 0) {
1136 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001137 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138 }
1139 } else {
1140 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001141 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001143 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001144
Eric Andersen8320b422003-04-02 10:13:26 +00001145 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001146 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001147 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148
Eric Andersen3c8bca32003-06-20 10:02:29 +00001149 if (!defn) {
1150 exit(EXIT_FAILURE);
1151 }
1152
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001153 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 if (do_all) {
1155 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001156 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001157 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001158 /* iface_down */
1159 const llist_t *list = state_list;
1160 while (list) {
Rob Landley8bb50782006-05-26 23:44:51 +00001161 llist_add_to_end(&target_list, bb_xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001162 list = list->link;
1163 }
1164 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001165 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001167 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168 }
1169
1170
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001171 /* Update the interfaces */
1172 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001173 llist_t *iface_list;
1174 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001175 char *iface;
1176 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001177 char *pch;
1178 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001179 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001180
Rob Landley1b751c82005-10-28 09:24:33 +00001181 iface = bb_xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001182 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001183
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001184 pch = strchr(iface, '=');
1185 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001186 *pch = '\0';
Rob Landley1b751c82005-10-28 09:24:33 +00001187 liface = bb_xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188 } else {
Rob Landley1b751c82005-10-28 09:24:33 +00001189 liface = bb_xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001190 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001191
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001193 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194
1195 if (cmds == iface_up) {
1196 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001197 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001198 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199 continue;
1200 }
1201 } else {
1202 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001203 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001204 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 continue;
1206 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207 }
1208 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001209
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001210#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001212 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213
1214 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1215
1216 for (i = 0; i < currmap->n_matches; i++) {
1217 if (fnmatch(currmap->match[i], liface, 0) != 0)
1218 continue;
1219 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001220 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221 }
Eric Andersen8a931792003-07-03 10:20:29 +00001222 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001223 break;
1224 }
1225 }
1226 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001227#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228
Eric Andersen8320b422003-04-02 10:13:26 +00001229
1230 iface_list = defn->ifaces;
1231 while (iface_list) {
1232 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233 if (strcmp(liface, currif->iface) == 0) {
1234 char *oldiface = currif->iface;
1235
1236 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001237 currif->iface = iface;
1238
Eric Andersen8320b422003-04-02 10:13:26 +00001239 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001240
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001241 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001242 cmds_ret = cmds(currif);
1243 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001244 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001245 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001246 any_failures += 1;
1247 } else if (cmds_ret == 0) {
1248 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001249 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001250
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 currif->iface = oldiface;
1252 }
Eric Andersen8320b422003-04-02 10:13:26 +00001253 iface_list = iface_list->link;
1254 }
1255 if (verbose) {
1256 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257 }
1258
1259 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001260 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001261 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001263 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001264
1265 if (cmds == iface_up) {
Rob Landleya6e131d2006-05-29 06:43:55 +00001266 char *newiface = bb_xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001267 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001268 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001269 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001270 free(iface_state->data);
1271 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001273 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001274 /* Remove an interface from the linked list */
Rob Landleya6e131d2006-05-29 06:43:55 +00001275 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276 }
1277 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001278 }
1279
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001280 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001281 if (!no_act) {
Rob Landley4217bab2006-02-26 04:42:05 +00001282 FILE *state_fp = NULL;
Eric Andersen66a3af92003-01-27 17:41:19 +00001283
Rob Landleye813ddb2006-02-28 03:53:14 +00001284 state_fp = bb_xfopen(statefile, "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001285 while (state_list) {
1286 if (state_list->data) {
1287 fputs(state_list->data, state_fp);
1288 fputc('\n', state_fp);
1289 }
1290 state_list = state_list->link;
1291 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001292 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293 }
1294
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001295 if (any_failures)
1296 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001297 return 0;
1298}