blob: 757827858d20eb1e1f29e114a16a79567b8f0bb2 [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
Glenn L McGrath9af8a722002-11-11 07:03:02 +000035#include "libbb.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
46#if 0
47#define debug_noise(fmt, args...) printf(fmt, ## args)
48#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000050#endif
51
52/* Forward declaration */
53struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
55typedef int (execfn)(char *command);
Eric Andersen8320b422003-04-02 10:13:26 +000056typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000057
Eric Andersen8320b422003-04-02 10:13:26 +000058struct method_t
59{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000060 char *name;
61 command_set *up;
62 command_set *down;
Eric Andersen8320b422003-04-02 10:13:26 +000063};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000064
Eric Andersen8320b422003-04-02 10:13:26 +000065struct address_family_t
66{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000067 char *name;
68 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000069 struct method_t *method;
70};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000071
Eric Andersen8320b422003-04-02 10:13:26 +000072struct mapping_defn_t
73{
74 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075
76 int max_matches;
77 int n_matches;
78 char **match;
79
80 char *script;
81
82 int max_mappings;
83 int n_mappings;
84 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000085};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000086
Eric Andersen8320b422003-04-02 10:13:26 +000087struct variable_t
88{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000089 char *name;
90 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000091};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000092
Eric Andersenc7bda1c2004-03-15 08:29:22 +000093struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000094{
95 struct interface_defn_t *prev;
96 struct interface_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000097
98 char *iface;
Eric Andersen8320b422003-04-02 10:13:26 +000099 struct address_family_t *address_family;
100 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101
102 int automatic;
103
104 int max_options;
105 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000106 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000107};
108
Eric Andersen8320b422003-04-02 10:13:26 +0000109struct interfaces_file_t
110{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000111 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000112 llist_t *ifaces;
113 struct mapping_defn_t *mappings;
114};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000115
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000116static char no_act = 0;
117static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000118static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000119
Eric Andersen66a3af92003-01-27 17:41:19 +0000120#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000121
122static unsigned int count_bits(unsigned int a)
123{
124 unsigned int result;
125 result = (a & 0x55) + ((a >> 1) & 0x55);
126 result = (result & 0x33) + ((result >> 2) & 0x33);
127 return((result & 0x0F) + ((result >> 4) & 0x0F));
128}
129
Eric Andersen66a3af92003-01-27 17:41:19 +0000130static int count_netmask_bits(char *dotted_quad)
131{
Eric Andersen8320b422003-04-02 10:13:26 +0000132 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000133 /* Found a netmask... Check if it is dotted quad */
134 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
135 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000136 result = count_bits(a);
137 result += count_bits(b);
138 result += count_bits(c);
139 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000140 return ((int)result);
141}
142#endif
143
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000144static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
145{
146 if (*pos + str_length >= *len) {
147 char *newbuf;
148
149 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
150 *buf = newbuf;
151 *len = *len * 2 + str_length + 1;
152 }
153
154 while (str_length-- >= 1) {
155 (*buf)[(*pos)++] = *str;
156 str++;
157 }
158 (*buf)[*pos] = '\0';
159}
160
161static int strncmpz(char *l, char *r, size_t llen)
162{
163 int i = strncmp(l, r, llen);
164
165 if (i == 0) {
166 return(-r[llen]);
167 } else {
168 return(i);
169 }
170}
171
Eric Andersen8320b422003-04-02 10:13:26 +0000172static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000173{
174 int i;
175
176 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000177 char *result;
178 static char label_buf[20];
179 strncpy(label_buf, ifd->iface, 19);
180 label_buf[19]=0;
181 result = strchr(label_buf, ':');
182 if (result) {
183 *result=0;
184 }
185 return( label_buf);
186 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000187 return (ifd->iface);
188 } else {
189 for (i = 0; i < ifd->n_options; i++) {
190 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
191 return (ifd->option[i].value);
192 }
193 }
194 }
195
196 return(NULL);
197}
198
Eric Andersen8320b422003-04-02 10:13:26 +0000199static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000200{
201
202 char *result = NULL;
203 size_t pos = 0, len = 0;
204 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
205 int okay[MAX_OPT_DEPTH] = { 1 };
206 int opt_depth = 1;
207
208 while (*command) {
209 switch (*command) {
210
Eric Andersen8320b422003-04-02 10:13:26 +0000211 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000212 addstr(&result, &len, &pos, command, 1);
213 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000214 break;
215 case '\\':
216 if (command[1]) {
217 addstr(&result, &len, &pos, command + 1, 1);
218 command += 2;
219 } else {
220 addstr(&result, &len, &pos, command, 1);
221 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000222 }
Eric Andersen8320b422003-04-02 10:13:26 +0000223 break;
224 case '[':
225 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
226 old_pos[opt_depth] = pos;
227 okay[opt_depth] = 1;
228 opt_depth++;
229 command += 2;
230 } else {
231 addstr(&result, &len, &pos, "[", 1);
232 command++;
233 }
234 break;
235 case ']':
236 if (command[1] == ']' && opt_depth > 1) {
237 opt_depth--;
238 if (!okay[opt_depth]) {
239 pos = old_pos[opt_depth];
240 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000241 }
Eric Andersen8320b422003-04-02 10:13:26 +0000242 command += 2;
243 } else {
244 addstr(&result, &len, &pos, "]", 1);
245 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000246 }
Eric Andersen8320b422003-04-02 10:13:26 +0000247 break;
248 case '%':
249 {
250 char *nextpercent;
251 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000252
Eric Andersen8320b422003-04-02 10:13:26 +0000253 command++;
254 nextpercent = strchr(command, '%');
255 if (!nextpercent) {
256 errno = EUNBALPER;
257 free(result);
258 return (NULL);
259 }
260
261 varvalue = get_var(command, nextpercent - command, ifd);
262
263 if (varvalue) {
264 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
265 } else {
266#ifdef CONFIG_FEATURE_IFUPDOWN_IP
267 /* Sigh... Add a special case for 'ip' to convert from
268 * dotted quad to bit count style netmasks. */
269 if (strncmp(command, "bnmask", 6)==0) {
270 int res;
271 varvalue = get_var("netmask", 7, ifd);
272 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
273 char argument[255];
274 sprintf(argument, "%d", res);
275 addstr(&result, &len, &pos, argument, bb_strlen(argument));
276 command = nextpercent + 1;
277 break;
278 }
279 }
280#endif
281 okay[opt_depth - 1] = 0;
282 }
283
284 command = nextpercent + 1;
285 }
286 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000287 }
288 }
289
290 if (opt_depth > 1) {
291 errno = EUNBALBRACK;
292 free(result);
293 return(NULL);
294 }
295
296 if (!okay[0]) {
297 errno = EUNDEFVAR;
298 free(result);
299 return(NULL);
300 }
301
302 return(result);
303}
304
Eric Andersen8320b422003-04-02 10:13:26 +0000305static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000306{
307 char *out;
308 int ret;
309
310 out = parse(command, ifd);
311 if (!out) {
312 return(0);
313 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000314 ret = (*exec) (out);
315
316 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000317 if (ret != 1) {
318 return(0);
319 }
Eric Andersen8320b422003-04-02 10:13:26 +0000320 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000321}
322
323#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000324static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000325{
Eric Andersen8320b422003-04-02 10:13:26 +0000326 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000327}
328
Eric Andersen8320b422003-04-02 10:13:26 +0000329static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000330{
Eric Andersen8320b422003-04-02 10:13:26 +0000331 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332}
333
Eric Andersen8320b422003-04-02 10:13:26 +0000334static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000335{
Eric Andersen8320b422003-04-02 10:13:26 +0000336 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000337}
338
Eric Andersen8320b422003-04-02 10:13:26 +0000339static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340{
Eric Andersen8320b422003-04-02 10:13:26 +0000341 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342}
343
Eric Andersen8320b422003-04-02 10:13:26 +0000344static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000345 { "dynamic", dynamic_up, dynamic_down, },
346 { "static", static_up_ipx, static_down_ipx, },
347};
348
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000349static struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000350 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000351 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352 methods_ipx
353};
354#endif /* IFUP_FEATURE_IPX */
355
356#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000357static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000358{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000359#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000360 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000361 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000362 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000363 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000364#else
Eric Andersen8320b422003-04-02 10:13:26 +0000365 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000366#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367}
368
Eric Andersen8320b422003-04-02 10:13:26 +0000369static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000371#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000372 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000373#else
Eric Andersen8320b422003-04-02 10:13:26 +0000374 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000375#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000376}
377
Eric Andersen8320b422003-04-02 10:13:26 +0000378static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379{
Eric Andersen8320b422003-04-02 10:13:26 +0000380 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000381#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000382 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
383 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000384 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000385#else
Eric Andersen8320b422003-04-02 10:13:26 +0000386 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
387 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
388 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000389#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000390 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391}
392
Eric Andersen8320b422003-04-02 10:13:26 +0000393static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000395#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000396 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000397#else
Eric Andersen8320b422003-04-02 10:13:26 +0000398 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000399#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400}
401
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000402#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000403static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000404{
Eric Andersen8320b422003-04-02 10:13:26 +0000405 int result;
406 result = execute("ip tunnel add %iface% mode sit remote "
407 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000408 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000409 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000410 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000411 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000412}
413
Eric Andersen8320b422003-04-02 10:13:26 +0000414static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000415{
Eric Andersen8320b422003-04-02 10:13:26 +0000416 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000417}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000419
Eric Andersen8320b422003-04-02 10:13:26 +0000420static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000421#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000422 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000423#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424 { "static", static_up6, static_down6, },
425 { "loopback", loopback_up6, loopback_down6, },
426};
427
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000428static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000430 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431 methods6
432};
433#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
434
435#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000436static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000438#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000439 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000440 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000441 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000442 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000443#else
Eric Andersen8320b422003-04-02 10:13:26 +0000444 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000445#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446}
447
Eric Andersen8320b422003-04-02 10:13:26 +0000448static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000449{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000450#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000451 int result;
452 result = execute("ip addr flush dev %iface%", ifd, exec);
453 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000454 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000455#else
Eric Andersen8320b422003-04-02 10:13:26 +0000456 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000457#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000458}
459
Eric Andersen8320b422003-04-02 10:13:26 +0000460static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000461{
Eric Andersen8320b422003-04-02 10:13:26 +0000462 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000463#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000464 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000465 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
466 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000467 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000468 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000469#else
Eric Andersen8320b422003-04-02 10:13:26 +0000470 result = execute("ifconfig %iface% %address% netmask %netmask% "
471 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
472 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
473 ifd, exec);
474 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000475 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000476#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000477}
478
Eric Andersen8320b422003-04-02 10:13:26 +0000479static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480{
Eric Andersen8320b422003-04-02 10:13:26 +0000481 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000482#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000483 result = execute("ip addr flush dev %iface%", ifd, exec);
484 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000485#else
Eric Andersen8320b422003-04-02 10:13:26 +0000486 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
487 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000488#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000489 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000490}
491
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000492static int execable(char *program)
493{
494 struct stat buf;
495 if (0 == stat(program, &buf)) {
496 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
497 return(1);
498 }
499 }
500 return(0);
501}
502
Eric Andersen8320b422003-04-02 10:13:26 +0000503static int dhcp_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 if (execable("/sbin/udhcpc")) {
506 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
507 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000508 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000509 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
510 } else if (execable("/sbin/dhclient")) {
511 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000512 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000513 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
514 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515 }
Eric Andersen8320b422003-04-02 10:13:26 +0000516 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000517}
518
Eric Andersen8320b422003-04-02 10:13:26 +0000519static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000520{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000521 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000522 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000523 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
524 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
525 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000526 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000527 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000528 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000529 result = execute("pump -i %iface% -k", ifd, exec);
530 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000531 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000533 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000534 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000535 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000536}
537
Eric Andersen8320b422003-04-02 10:13:26 +0000538static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539{
Eric Andersen8320b422003-04-02 10:13:26 +0000540 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
541 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
542 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000543}
544
Eric Andersen8320b422003-04-02 10:13:26 +0000545static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546{
Eric Andersen8320b422003-04-02 10:13:26 +0000547 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548}
549
Eric Andersen8320b422003-04-02 10:13:26 +0000550static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551{
Eric Andersen8320b422003-04-02 10:13:26 +0000552 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553}
554
Eric Andersen8320b422003-04-02 10:13:26 +0000555static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556{
Eric Andersen8320b422003-04-02 10:13:26 +0000557 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
558 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000559}
560
Eric Andersen8320b422003-04-02 10:13:26 +0000561static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562{
Eric Andersen8320b422003-04-02 10:13:26 +0000563 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
564 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565}
566
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000567static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000568{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000569 { "wvdial", wvdial_up, wvdial_down, },
570 { "ppp", ppp_up, ppp_down, },
571 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000572 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000573 { "dhcp", dhcp_up, dhcp_down, },
574 { "loopback", loopback_up, loopback_down, },
575};
576
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000577static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000578{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000579 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000580 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000581 methods
582};
583
584#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
585
Glenn L McGrath85737042003-01-14 23:26:57 +0000586static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000587{
Glenn L McGrath85737042003-01-14 23:26:57 +0000588 unsigned short length;
589 char *word;
590
591 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000592 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000593 }
594
Glenn L McGrath85737042003-01-14 23:26:57 +0000595 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000596 word = *buf;
597 while (isspace(*word)) {
598 ++word;
599 }
600
601 /* Skip over comments */
602 if (*word == '#') {
603 return(NULL);
604 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000605
Glenn L McGrath85737042003-01-14 23:26:57 +0000606 /* Find the length of this word */
607 length = strcspn(word, " \t\n");
608 if (length == 0) {
609 return(NULL);
610 }
611 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000612 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
613 if (**buf) {
614 **buf = '\0';
615 (*buf)++;
616 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000617
618 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000619}
620
Eric Andersen8320b422003-04-02 10:13:26 +0000621static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000622{
623 int i;
624
625 for (i = 0; af[i]; i++) {
626 if (strcmp(af[i]->name, name) == 0) {
627 return af[i];
628 }
629 }
630 return NULL;
631}
632
Eric Andersen8320b422003-04-02 10:13:26 +0000633static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000634{
635 int i;
636
637 for (i = 0; i < af->n_methods; i++) {
638 if (strcmp(af->method[i].name, name) == 0) {
639 return &af->method[i];
640 }
641 }
642 return(NULL);
643}
644
Eric Andersen8320b422003-04-02 10:13:26 +0000645static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646{
647 if (strcmp(ifa->iface, ifb->iface) != 0) {
648 return(0);
649 }
650 if (ifa->address_family != ifb->address_family) {
651 return(0);
652 }
653 return(1);
654}
655
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000656static const llist_t *find_list_string(const llist_t *list, const char *string)
657{
658 while (list) {
659 if (strcmp(list->data, string) == 0) {
660 return(list);
661 }
662 list = list->link;
663 }
664 return(NULL);
665}
666
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000667static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000669#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000670 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000671#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000672 struct interface_defn_t *currif = NULL;
673 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000675 char *firstword;
676 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000677
678 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
679
Eric Andersen8320b422003-04-02 10:13:26 +0000680 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681 defn->autointerfaces = NULL;
682 defn->mappings = NULL;
683 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000684
Manuel Novoa III cad53642003-03-19 09:13:01 +0000685 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000686
Eric Andersen8320b422003-04-02 10:13:26 +0000687 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000688 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000689
Glenn L McGrath85737042003-01-14 23:26:57 +0000690 firstword = next_word(&buf_ptr);
691 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000692 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693 continue; /* blank line */
694 }
695
696 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000697#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000698 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000699 currmap->max_matches = 0;
700 currmap->n_matches = 0;
701 currmap->match = NULL;
702
Glenn L McGrath85737042003-01-14 23:26:57 +0000703 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000704 if (currmap->max_matches == currmap->n_matches) {
705 currmap->max_matches = currmap->max_matches * 2 + 1;
706 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
707 }
708
Manuel Novoa III cad53642003-03-19 09:13:01 +0000709 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000710 }
711 currmap->max_mappings = 0;
712 currmap->n_mappings = 0;
713 currmap->mapping = NULL;
714 currmap->script = NULL;
715 {
Eric Andersen8320b422003-04-02 10:13:26 +0000716 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000717 while (*where != NULL) {
718 where = &(*where)->next;
719 }
720 *where = currmap;
721 currmap->next = NULL;
722 }
Eric Andersen8320b422003-04-02 10:13:26 +0000723 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000724#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000725 currently_processing = MAPPING;
726 } else if (strcmp(firstword, "iface") == 0) {
727 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000728 char *iface_name;
729 char *address_family_name;
730 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000731 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000732#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
733 &addr_inet,
734#endif
735#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
736 &addr_inet6,
737#endif
738#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
739 &addr_ipx,
740#endif
741 NULL
742 };
743
Eric Andersen8320b422003-04-02 10:13:26 +0000744 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000745 iface_name = next_word(&buf_ptr);
746 address_family_name = next_word(&buf_ptr);
747 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000748
Glenn L McGrath85737042003-01-14 23:26:57 +0000749 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751 return NULL;
752 }
753
Eric Andersen3c8bca32003-06-20 10:02:29 +0000754 /* ship any trailing whitespace */
755 while (isspace(*buf_ptr)) {
756 ++buf_ptr;
757 }
758
Glenn L McGrath85737042003-01-14 23:26:57 +0000759 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000760 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000761 return NULL;
762 }
763
Manuel Novoa III cad53642003-03-19 09:13:01 +0000764 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000765
766 currif->address_family = get_address_family(addr_fams, address_family_name);
767 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000768 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000769 return NULL;
770 }
771
772 currif->method = get_method(currif->address_family, method_name);
773 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000774 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775 return NULL;
776 }
777
778 currif->automatic = 1;
779 currif->max_options = 0;
780 currif->n_options = 0;
781 currif->option = NULL;
782
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783 {
Eric Andersen8320b422003-04-02 10:13:26 +0000784 struct interface_defn_t *tmp;
785 llist_t *iface_list;
786 iface_list = defn->ifaces;
787 while (iface_list) {
788 tmp = (struct interface_defn_t *) iface_list->data;
789 if (duplicate_if(tmp, currif)) {
790 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000791 return NULL;
792 }
Eric Andersen8320b422003-04-02 10:13:26 +0000793 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 }
795
Eric Andersen8320b422003-04-02 10:13:26 +0000796 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000797 }
Eric Andersen8320b422003-04-02 10:13:26 +0000798 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000799 }
800 currently_processing = IFACE;
801 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000802 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000803
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000804 /* Check the interface isnt already listed */
805 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000806 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000807 }
808
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000809 /* Add the interface to the list */
Rob Landley1b751c82005-10-28 09:24:33 +0000810 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000811 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812 }
813 currently_processing = NONE;
814 } else {
815 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000816 case IFACE:
817 {
818 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000819
Eric Andersen8320b422003-04-02 10:13:26 +0000820 if (bb_strlen(buf_ptr) == 0) {
821 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000822 return NULL;
823 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824
Eric Andersen8320b422003-04-02 10:13:26 +0000825 if (strcmp(firstword, "up") != 0
826 && strcmp(firstword, "down") != 0
827 && strcmp(firstword, "pre-up") != 0
828 && strcmp(firstword, "post-down") != 0) {
829 for (i = 0; i < currif->n_options; i++) {
830 if (strcmp(currif->option[i].name, firstword) == 0) {
831 bb_error_msg("duplicate option \"%s\"", buf);
832 return NULL;
833 }
834 }
835 }
836 }
837 if (currif->n_options >= currif->max_options) {
838 struct variable_t *opt;
839
840 currif->max_options = currif->max_options + 10;
841 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
842 currif->option = opt;
843 }
844 currif->option[currif->n_options].name = bb_xstrdup(firstword);
845 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
846 if (!currif->option[currif->n_options].name) {
847 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000848 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000849 }
850 if (!currif->option[currif->n_options].value) {
851 perror(filename);
852 return NULL;
853 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000854 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000855 currif->option[currif->n_options].value);
856 currif->n_options++;
857 break;
858 case MAPPING:
859#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
860 if (strcmp(firstword, "script") == 0) {
861 if (currmap->script != NULL) {
862 bb_error_msg("duplicate script in mapping \"%s\"", buf);
863 return NULL;
864 } else {
865 currmap->script = bb_xstrdup(next_word(&buf_ptr));
866 }
867 } else if (strcmp(firstword, "map") == 0) {
868 if (currmap->max_mappings == currmap->n_mappings) {
869 currmap->max_mappings = currmap->max_mappings * 2 + 1;
870 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
871 }
872 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
873 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000874 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000875 bb_error_msg("misplaced option \"%s\"", buf);
876 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000877 }
Eric Andersen8320b422003-04-02 10:13:26 +0000878#endif
879 break;
880 case NONE:
881 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000882 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000884 }
885 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000886 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000887 }
888 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000889 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000890 }
891 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892
893 return defn;
894}
895
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000896static char *setlocalenv(char *format, char *name, char *value)
897{
898 char *result;
899 char *here;
900 char *there;
901
Manuel Novoa III cad53642003-03-19 09:13:01 +0000902 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000903
904 sprintf(result, format, name, value);
905
906 for (here = there = result; *there != '=' && *there; there++) {
907 if (*there == '-')
908 *there = '_';
909 if (isalpha(*there))
910 *there = toupper(*there);
911
912 if (isalnum(*there) || *there == '_') {
913 *here = *there;
914 here++;
915 }
916 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000917 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918
919 return result;
920}
921
Eric Andersen8320b422003-04-02 10:13:26 +0000922static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000923{
924 char **environend;
925 int i;
926 const int n_env_entries = iface->n_options + 5;
927 char **ppch;
928
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000929 if (__myenviron != NULL) {
930 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000931 free(*ppch);
932 *ppch = NULL;
933 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000934 free(__myenviron);
935 __myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000937 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
938 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000939 *environend = NULL;
940
941 for (i = 0; i < iface->n_options; i++) {
942 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000943 || strcmp(iface->option[i].name, "down") == 0
944 || strcmp(iface->option[i].name, "pre-up") == 0
945 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000946 continue;
947 }
948 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
949 *environend = NULL;
950 }
951
952 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
953 *environend = NULL;
954 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
955 *environend = NULL;
956 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
957 *environend = NULL;
958 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
959 *environend = NULL;
960 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
961 *environend = NULL;
962}
963
964static int doit(char *str)
965{
966 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000967 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000968 }
969 if (!no_act) {
970 pid_t child;
971 int status;
972
973 fflush(NULL);
974 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000975 case -1: /* failure */
976 return 0;
977 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000978 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000979 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000980 }
981 waitpid(child, &status, 0);
982 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
983 return 0;
984 }
985 }
986 return (1);
987}
988
Eric Andersen8320b422003-04-02 10:13:26 +0000989static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000990{
991 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000992 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000993 for (i = 0; i < ifd->n_options; i++) {
994 if (strcmp(ifd->option[i].name, opt) == 0) {
995 if (!(*exec) (ifd->option[i].value)) {
996 return 0;
997 }
998 }
999 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001000
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00001001 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001002 if ((*exec)(buf) != 1) {
1003 return 0;
1004 }
1005 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001006}
1007
Eric Andersen8320b422003-04-02 10:13:26 +00001008static int check(char *str) {
1009 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001010}
1011
Eric Andersen8320b422003-04-02 10:13:26 +00001012static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001013{
Eric Andersen8320b422003-04-02 10:13:26 +00001014 if (!iface->method->up(iface,check)) return -1;
1015 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001016 if (!execute_all(iface, doit, "pre-up")) return 0;
1017 if (!iface->method->up(iface, doit)) return 0;
1018 if (!execute_all(iface, doit, "up")) return 0;
1019 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001020}
1021
1022static int iface_down(struct interface_defn_t *iface)
1023{
Eric Andersen8320b422003-04-02 10:13:26 +00001024 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001025 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001026 if (!execute_all(iface, doit, "down")) return 0;
1027 if (!iface->method->down(iface, doit)) return 0;
1028 if (!execute_all(iface, doit, "post-down")) return 0;
1029 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001030}
1031
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001032#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033static int popen2(FILE **in, FILE **out, char *command, ...)
1034{
1035 va_list ap;
1036 char *argv[11] = { command };
1037 int argc;
1038 int infd[2], outfd[2];
1039 pid_t pid;
1040
1041 argc = 1;
1042 va_start(ap, command);
1043 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1044 argc++;
1045 }
1046 argv[argc] = NULL; /* make sure */
1047 va_end(ap);
1048
1049 if (pipe(infd) != 0) {
1050 return 0;
1051 }
1052
1053 if (pipe(outfd) != 0) {
1054 close(infd[0]);
1055 close(infd[1]);
1056 return 0;
1057 }
1058
1059 fflush(NULL);
1060 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001061 case -1: /* failure */
1062 close(infd[0]);
1063 close(infd[1]);
1064 close(outfd[0]);
1065 close(outfd[1]);
1066 return 0;
1067 case 0: /* child */
1068 dup2(infd[0], 0);
1069 dup2(outfd[1], 1);
1070 close(infd[0]);
1071 close(infd[1]);
1072 close(outfd[0]);
1073 close(outfd[1]);
1074 execvp(command, argv);
1075 exit(127);
1076 default: /* parent */
1077 *in = fdopen(infd[1], "w");
1078 *out = fdopen(outfd[0], "r");
1079 close(infd[0]);
1080 close(outfd[1]);
1081 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001082 }
1083 /* unreached */
1084}
1085
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001086static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001087{
1088 FILE *in, *out;
1089 int i, status;
1090 pid_t pid;
1091
Eric Andersen8a931792003-07-03 10:20:29 +00001092 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001093
Eric Andersen8a931792003-07-03 10:20:29 +00001094 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001095 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001096
1097 /* popen2() returns 0 on failure. */
1098 if (pid == 0)
1099 return logical;
1100
1101 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001102 for (i = 0; i < map->n_mappings; i++) {
1103 fprintf(in, "%s\n", map->mapping[i]);
1104 }
1105 fclose(in);
1106 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001107
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001108 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001109 /* If the mapping script exited successfully, try to
1110 * grab a line of output and use that as the name of the
1111 * logical interface. */
1112 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001113
Eric Andersen233b1702003-06-05 19:37:01 +00001114 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001115 /* If we are able to read a line of output from the script,
1116 * remove any trailing whitespace and use this value
1117 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001118 char *pch = new_logical + bb_strlen(new_logical) - 1;
1119
1120 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001121 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001122
1123 free(logical);
1124 logical = new_logical;
1125 } else {
1126 /* If we are UNABLE to read a line of output, discard are
1127 * freshly allocated memory. */
1128 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129 }
1130 }
Eric Andersen8a931792003-07-03 10:20:29 +00001131
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 fclose(out);
1133
Eric Andersen8a931792003-07-03 10:20:29 +00001134 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135}
Eric Andersen8a931792003-07-03 10:20:29 +00001136#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001138static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001139{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001140 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001141 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001143 while (search) {
1144 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001145 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001148 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001150 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001151}
1152
1153extern int ifupdown_main(int argc, char **argv)
1154{
Eric Andersen8320b422003-04-02 10:13:26 +00001155 int (*cmds) (struct interface_defn_t *) = NULL;
1156 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001157 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001158 llist_t *state_list = NULL;
1159 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001160 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001161 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001163#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001165#endif
1166 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001167 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001168 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001169 int i;
1170
Manuel Novoa III cad53642003-03-19 09:13:01 +00001171 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172 /* ifup command */
1173 cmds = iface_up;
1174 } else {
1175 /* ifdown command */
1176 cmds = iface_down;
1177 }
1178
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001179#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001180 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001181#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001182 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001183#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001184 {
1185 switch (i) {
1186 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001187 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001188 break;
1189 case 'v': /* verbose */
1190 verbose = 1;
1191 break;
1192 case 'a': /* all */
1193 do_all = 1;
1194 break;
1195 case 'n': /* no-act */
1196 no_act = 1;
1197 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001198#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001199 case 'm': /* no-mappings */
1200 run_mappings = 0;
1201 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001202#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001203 case 'f': /* force */
1204 force = 1;
1205 break;
1206 default:
1207 bb_show_usage();
1208 break;
1209 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211
1212 if (argc - optind > 0) {
1213 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001214 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001215 }
1216 } else {
1217 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001218 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001220 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221
Eric Andersen8320b422003-04-02 10:13:26 +00001222 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001223 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001224 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225
Eric Andersen3c8bca32003-06-20 10:02:29 +00001226 if (!defn) {
1227 exit(EXIT_FAILURE);
1228 }
1229
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001230 if (no_act) {
1231 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 }
1233
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001234 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001235 if (do_all) {
1236 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001237 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001238 } else {
1239#if 0
1240 /* iface_down */
1241 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 const llist_t *list = state_list;
1243 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001244 new_item = xmalloc(sizeof(llist_t));
Rob Landley1b751c82005-10-28 09:24:33 +00001245 new_item->data = bb_xstrdup(list->data);
Eric Andersen8320b422003-04-02 10:13:26 +00001246 new_item->link = NULL;
1247 list = target_list;
1248 if (list == NULL)
1249 target_list = new_item;
1250 else {
1251 while (list->link) {
1252 list = list->link;
1253 }
1254 list = new_item;
1255 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001256 list = list->link;
1257 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001258 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001259#else
1260
1261 /* iface_down */
1262 const llist_t *list = state_list;
1263 while (list) {
Rob Landley1b751c82005-10-28 09:24:33 +00001264 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001265 list = list->link;
1266 }
1267 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001268#endif
1269 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001271 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 }
1273
1274
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275 /* Update the interfaces */
1276 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001277 llist_t *iface_list;
1278 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001279 char *iface;
1280 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001281 char *pch;
1282 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001283 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001284
Rob Landley1b751c82005-10-28 09:24:33 +00001285 iface = bb_xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001286 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001287
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001288 pch = strchr(iface, '=');
1289 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001290 *pch = '\0';
Rob Landley1b751c82005-10-28 09:24:33 +00001291 liface = bb_xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001292 } else {
Rob Landley1b751c82005-10-28 09:24:33 +00001293 liface = bb_xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001294 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001295
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001296 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001297 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298
1299 if (cmds == iface_up) {
1300 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001301 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001302 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001303 continue;
1304 }
1305 } else {
1306 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001307 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001308 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001309 continue;
1310 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001311 }
1312 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001313
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001314#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001315 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001316 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001317
1318 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1319
1320 for (i = 0; i < currmap->n_matches; i++) {
1321 if (fnmatch(currmap->match[i], liface, 0) != 0)
1322 continue;
1323 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001324 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001325 }
Eric Andersen8a931792003-07-03 10:20:29 +00001326 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327 break;
1328 }
1329 }
1330 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001331#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001332
Eric Andersen8320b422003-04-02 10:13:26 +00001333
1334 iface_list = defn->ifaces;
1335 while (iface_list) {
1336 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001337 if (strcmp(liface, currif->iface) == 0) {
1338 char *oldiface = currif->iface;
1339
1340 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001341 currif->iface = iface;
1342
Eric Andersen8320b422003-04-02 10:13:26 +00001343 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001345 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001346 cmds_ret = cmds(currif);
1347 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001348 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001349 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001350 any_failures += 1;
1351 } else if (cmds_ret == 0) {
1352 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001353 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001354
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001355 currif->iface = oldiface;
1356 }
Eric Andersen8320b422003-04-02 10:13:26 +00001357 iface_list = iface_list->link;
1358 }
1359 if (verbose) {
1360 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001361 }
1362
1363 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001364 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001365 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001366 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001367 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001368
1369 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001370 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001371 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001372 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001373 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001374 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001375 free(iface_state->data);
1376 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001377 }
1378 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001379 /* Remove an interface from the linked list */
1380 if (iface_state) {
1381 /* This needs to be done better */
1382 free(iface_state->data);
1383 free(iface_state->link);
1384 if (iface_state->link) {
1385 iface_state->data = iface_state->link->data;
1386 iface_state->link = iface_state->link->link;
1387 } else {
1388 iface_state->data = NULL;
1389 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001390 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001391 }
1392 }
1393 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001394 }
1395
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001396 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001397 if (!no_act) {
1398
1399 if (state_fp)
1400 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001401 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001402
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001403 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001404 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001405 }
1406
1407 rewind(state_fp);
1408
1409 while (state_list) {
1410 if (state_list->data) {
1411 fputs(state_list->data, state_fp);
1412 fputc('\n', state_fp);
1413 }
1414 state_list = state_list->link;
1415 }
1416 fflush(state_fp);
1417 }
1418
1419 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001420 if (state_fp != NULL) {
1421 fclose(state_fp);
1422 state_fp = NULL;
1423 }
1424
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001425 if (any_failures)
1426 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001427 return 0;
1428}