blob: d23c9f70aedd713b173526ceff2edcabc7cdd809 [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 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
Glenn L McGrath0177ce12004-07-21 23:56:31 +000030/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
31
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000032#include <sys/stat.h>
33#include <sys/utsname.h>
34#include <sys/wait.h>
35
36#include <ctype.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <fnmatch.h>
40#include <getopt.h>
41#include <stdarg.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
Glenn L McGrath9af8a722002-11-11 07:03:02 +000047#include "libbb.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000049#define MAX_OPT_DEPTH 10
50#define EUNBALBRACK 10001
51#define EUNDEFVAR 10002
52#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000053
Eric Andersen233b1702003-06-05 19:37:01 +000054#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
55#define MAX_INTERFACE_LENGTH 10
56#endif
Eric Andersen8320b422003-04-02 10:13:26 +000057
58#if 0
59#define debug_noise(fmt, args...) printf(fmt, ## args)
60#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000062#endif
63
64/* Forward declaration */
65struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
67typedef int (execfn)(char *command);
Eric Andersen8320b422003-04-02 10:13:26 +000068typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000069
Eric Andersen8320b422003-04-02 10:13:26 +000070struct method_t
71{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000072 char *name;
73 command_set *up;
74 command_set *down;
Eric Andersen8320b422003-04-02 10:13:26 +000075};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000076
Eric Andersen8320b422003-04-02 10:13:26 +000077struct address_family_t
78{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000079 char *name;
80 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000081 struct method_t *method;
82};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000083
Eric Andersen8320b422003-04-02 10:13:26 +000084struct mapping_defn_t
85{
86 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000087
88 int max_matches;
89 int n_matches;
90 char **match;
91
92 char *script;
93
94 int max_mappings;
95 int n_mappings;
96 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000097};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000098
Eric Andersen8320b422003-04-02 10:13:26 +000099struct variable_t
100{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101 char *name;
102 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +0000103};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000104
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000105struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +0000106{
107 struct interface_defn_t *prev;
108 struct interface_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000109
110 char *iface;
Eric Andersen8320b422003-04-02 10:13:26 +0000111 struct address_family_t *address_family;
112 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000113
114 int automatic;
115
116 int max_options;
117 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000118 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000119};
120
Eric Andersen8320b422003-04-02 10:13:26 +0000121struct interfaces_file_t
122{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000123 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000124 llist_t *ifaces;
125 struct mapping_defn_t *mappings;
126};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000127
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000128static char no_act = 0;
129static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000130static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000131
Eric Andersen66a3af92003-01-27 17:41:19 +0000132#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000133
134static unsigned int count_bits(unsigned int a)
135{
136 unsigned int result;
137 result = (a & 0x55) + ((a >> 1) & 0x55);
138 result = (result & 0x33) + ((result >> 2) & 0x33);
139 return((result & 0x0F) + ((result >> 4) & 0x0F));
140}
141
Eric Andersen66a3af92003-01-27 17:41:19 +0000142static int count_netmask_bits(char *dotted_quad)
143{
Eric Andersen8320b422003-04-02 10:13:26 +0000144 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000145 /* Found a netmask... Check if it is dotted quad */
146 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
147 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000148 result = count_bits(a);
149 result += count_bits(b);
150 result += count_bits(c);
151 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000152 return ((int)result);
153}
154#endif
155
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000156static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
157{
158 if (*pos + str_length >= *len) {
159 char *newbuf;
160
161 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
162 *buf = newbuf;
163 *len = *len * 2 + str_length + 1;
164 }
165
166 while (str_length-- >= 1) {
167 (*buf)[(*pos)++] = *str;
168 str++;
169 }
170 (*buf)[*pos] = '\0';
171}
172
173static int strncmpz(char *l, char *r, size_t llen)
174{
175 int i = strncmp(l, r, llen);
176
177 if (i == 0) {
178 return(-r[llen]);
179 } else {
180 return(i);
181 }
182}
183
Eric Andersen8320b422003-04-02 10:13:26 +0000184static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000185{
186 int i;
187
188 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000189 char *result;
190 static char label_buf[20];
191 strncpy(label_buf, ifd->iface, 19);
192 label_buf[19]=0;
193 result = strchr(label_buf, ':');
194 if (result) {
195 *result=0;
196 }
197 return( label_buf);
198 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000199 return (ifd->iface);
200 } else {
201 for (i = 0; i < ifd->n_options; i++) {
202 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
203 return (ifd->option[i].value);
204 }
205 }
206 }
207
208 return(NULL);
209}
210
Eric Andersen8320b422003-04-02 10:13:26 +0000211static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000212{
213
214 char *result = NULL;
215 size_t pos = 0, len = 0;
216 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
217 int okay[MAX_OPT_DEPTH] = { 1 };
218 int opt_depth = 1;
219
220 while (*command) {
221 switch (*command) {
222
Eric Andersen8320b422003-04-02 10:13:26 +0000223 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000224 addstr(&result, &len, &pos, command, 1);
225 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000226 break;
227 case '\\':
228 if (command[1]) {
229 addstr(&result, &len, &pos, command + 1, 1);
230 command += 2;
231 } else {
232 addstr(&result, &len, &pos, command, 1);
233 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000234 }
Eric Andersen8320b422003-04-02 10:13:26 +0000235 break;
236 case '[':
237 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
238 old_pos[opt_depth] = pos;
239 okay[opt_depth] = 1;
240 opt_depth++;
241 command += 2;
242 } else {
243 addstr(&result, &len, &pos, "[", 1);
244 command++;
245 }
246 break;
247 case ']':
248 if (command[1] == ']' && opt_depth > 1) {
249 opt_depth--;
250 if (!okay[opt_depth]) {
251 pos = old_pos[opt_depth];
252 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000253 }
Eric Andersen8320b422003-04-02 10:13:26 +0000254 command += 2;
255 } else {
256 addstr(&result, &len, &pos, "]", 1);
257 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000258 }
Eric Andersen8320b422003-04-02 10:13:26 +0000259 break;
260 case '%':
261 {
262 char *nextpercent;
263 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000264
Eric Andersen8320b422003-04-02 10:13:26 +0000265 command++;
266 nextpercent = strchr(command, '%');
267 if (!nextpercent) {
268 errno = EUNBALPER;
269 free(result);
270 return (NULL);
271 }
272
273 varvalue = get_var(command, nextpercent - command, ifd);
274
275 if (varvalue) {
276 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
277 } else {
278#ifdef CONFIG_FEATURE_IFUPDOWN_IP
279 /* Sigh... Add a special case for 'ip' to convert from
280 * dotted quad to bit count style netmasks. */
281 if (strncmp(command, "bnmask", 6)==0) {
282 int res;
283 varvalue = get_var("netmask", 7, ifd);
284 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
285 char argument[255];
286 sprintf(argument, "%d", res);
287 addstr(&result, &len, &pos, argument, bb_strlen(argument));
288 command = nextpercent + 1;
289 break;
290 }
291 }
292#endif
293 okay[opt_depth - 1] = 0;
294 }
295
296 command = nextpercent + 1;
297 }
298 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000299 }
300 }
301
302 if (opt_depth > 1) {
303 errno = EUNBALBRACK;
304 free(result);
305 return(NULL);
306 }
307
308 if (!okay[0]) {
309 errno = EUNDEFVAR;
310 free(result);
311 return(NULL);
312 }
313
314 return(result);
315}
316
Eric Andersen8320b422003-04-02 10:13:26 +0000317static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000318{
319 char *out;
320 int ret;
321
322 out = parse(command, ifd);
323 if (!out) {
324 return(0);
325 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000326 ret = (*exec) (out);
327
328 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000329 if (ret != 1) {
330 return(0);
331 }
Eric Andersen8320b422003-04-02 10:13:26 +0000332 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000333}
334
335#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000336static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000337{
Eric Andersen8320b422003-04-02 10:13:26 +0000338 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339}
340
Eric Andersen8320b422003-04-02 10:13:26 +0000341static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342{
Eric Andersen8320b422003-04-02 10:13:26 +0000343 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000344}
345
Eric Andersen8320b422003-04-02 10:13:26 +0000346static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347{
Eric Andersen8320b422003-04-02 10:13:26 +0000348 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349}
350
Eric Andersen8320b422003-04-02 10:13:26 +0000351static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352{
Eric Andersen8320b422003-04-02 10:13:26 +0000353 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000354}
355
Eric Andersen8320b422003-04-02 10:13:26 +0000356static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357 { "dynamic", dynamic_up, dynamic_down, },
358 { "static", static_up_ipx, static_down_ipx, },
359};
360
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000361static struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000363 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364 methods_ipx
365};
366#endif /* IFUP_FEATURE_IPX */
367
368#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000369static int loopback_up6(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 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000373 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000374 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000375 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000376#else
Eric Andersen8320b422003-04-02 10:13:26 +0000377 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000378#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
380
Eric Andersen8320b422003-04-02 10:13:26 +0000381static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000383#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000384 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000385#else
Eric Andersen8320b422003-04-02 10:13:26 +0000386 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000387#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000388}
389
Eric Andersen8320b422003-04-02 10:13:26 +0000390static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391{
Eric Andersen8320b422003-04-02 10:13:26 +0000392 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000393#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000394 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
395 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000396 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000397#else
Eric Andersen8320b422003-04-02 10:13:26 +0000398 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
399 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
400 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000401#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000402 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000403}
404
Eric Andersen8320b422003-04-02 10:13:26 +0000405static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000406{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000407#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000408 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000409#else
Eric Andersen8320b422003-04-02 10:13:26 +0000410 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000411#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000412}
413
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000414#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000415static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000416{
Eric Andersen8320b422003-04-02 10:13:26 +0000417 int result;
418 result = execute("ip tunnel add %iface% mode sit remote "
419 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000420 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000421 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000422 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000423 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424}
425
Eric Andersen8320b422003-04-02 10:13:26 +0000426static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000427{
Eric Andersen8320b422003-04-02 10:13:26 +0000428 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000430#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431
Eric Andersen8320b422003-04-02 10:13:26 +0000432static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000433#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000435#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000436 { "static", static_up6, static_down6, },
437 { "loopback", loopback_up6, loopback_down6, },
438};
439
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000440static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000442 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000443 methods6
444};
445#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
446
447#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000448static int loopback_up(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;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000452 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000453 result += execute("ip link set %iface% up", 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 up", 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 loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000461{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000462#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000463 int result;
464 result = execute("ip addr flush dev %iface%", ifd, exec);
465 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000466 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000467#else
Eric Andersen8320b422003-04-02 10:13:26 +0000468 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000469#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000470}
471
Eric Andersen8320b422003-04-02 10:13:26 +0000472static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000473{
Eric Andersen8320b422003-04-02 10:13:26 +0000474 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000475#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000476 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000477 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
478 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000479 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000480 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000481#else
Eric Andersen8320b422003-04-02 10:13:26 +0000482 result = execute("ifconfig %iface% %address% netmask %netmask% "
483 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
484 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
485 ifd, exec);
486 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000487 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000488#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000489}
490
Eric Andersen8320b422003-04-02 10:13:26 +0000491static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000492{
Eric Andersen8320b422003-04-02 10:13:26 +0000493 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000494#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000495 result = execute("ip addr flush dev %iface%", ifd, exec);
496 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000497#else
Eric Andersen8320b422003-04-02 10:13:26 +0000498 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
499 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000500#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000501 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000502}
503
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000504static int execable(char *program)
505{
506 struct stat buf;
507 if (0 == stat(program, &buf)) {
508 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
509 return(1);
510 }
511 }
512 return(0);
513}
514
Eric Andersen8320b422003-04-02 10:13:26 +0000515static int dhcp_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 if (execable("/sbin/udhcpc")) {
518 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
519 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000520 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000521 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
522 } else if (execable("/sbin/dhclient")) {
523 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000524 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000525 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
526 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527 }
Eric Andersen8320b422003-04-02 10:13:26 +0000528 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529}
530
Eric Andersen8320b422003-04-02 10:13:26 +0000531static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000533 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000534 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000535 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
536 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
537 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000538 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000539 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000540 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000541 result = execute("pump -i %iface% -k", ifd, exec);
542 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000543 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000544 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000545 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000547 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548}
549
Eric Andersen8320b422003-04-02 10:13:26 +0000550static int bootp_up(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("bootpc [[--bootfile %bootfile%]] --dev %iface% "
553 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
554 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000555}
556
Eric Andersen8320b422003-04-02 10:13:26 +0000557static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000558{
Eric Andersen8320b422003-04-02 10:13:26 +0000559 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000560}
561
Eric Andersen8320b422003-04-02 10:13:26 +0000562static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000563{
Eric Andersen8320b422003-04-02 10:13:26 +0000564 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565}
566
Eric Andersen8320b422003-04-02 10:13:26 +0000567static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568{
Eric Andersen8320b422003-04-02 10:13:26 +0000569 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
570 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000571}
572
Eric Andersen8320b422003-04-02 10:13:26 +0000573static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000574{
Eric Andersen8320b422003-04-02 10:13:26 +0000575 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
576 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577}
578
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000579static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000580{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000581 { "wvdial", wvdial_up, wvdial_down, },
582 { "ppp", ppp_up, ppp_down, },
583 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000584 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000585 { "dhcp", dhcp_up, dhcp_down, },
586 { "loopback", loopback_up, loopback_down, },
587};
588
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000589static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000590{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000591 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000592 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000593 methods
594};
595
596#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
597
Glenn L McGrath85737042003-01-14 23:26:57 +0000598static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599{
Glenn L McGrath85737042003-01-14 23:26:57 +0000600 unsigned short length;
601 char *word;
602
603 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000604 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000605 }
606
Glenn L McGrath85737042003-01-14 23:26:57 +0000607 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000608 word = *buf;
609 while (isspace(*word)) {
610 ++word;
611 }
612
613 /* Skip over comments */
614 if (*word == '#') {
615 return(NULL);
616 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000617
Glenn L McGrath85737042003-01-14 23:26:57 +0000618 /* Find the length of this word */
619 length = strcspn(word, " \t\n");
620 if (length == 0) {
621 return(NULL);
622 }
623 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000624 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
625 if (**buf) {
626 **buf = '\0';
627 (*buf)++;
628 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000629
630 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000631}
632
Eric Andersen8320b422003-04-02 10:13:26 +0000633static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000634{
635 int i;
636
637 for (i = 0; af[i]; i++) {
638 if (strcmp(af[i]->name, name) == 0) {
639 return af[i];
640 }
641 }
642 return NULL;
643}
644
Eric Andersen8320b422003-04-02 10:13:26 +0000645static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646{
647 int i;
648
649 for (i = 0; i < af->n_methods; i++) {
650 if (strcmp(af->method[i].name, name) == 0) {
651 return &af->method[i];
652 }
653 }
654 return(NULL);
655}
656
Eric Andersen8320b422003-04-02 10:13:26 +0000657static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000658{
659 if (strcmp(ifa->iface, ifb->iface) != 0) {
660 return(0);
661 }
662 if (ifa->address_family != ifb->address_family) {
663 return(0);
664 }
665 return(1);
666}
667
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000668static const llist_t *find_list_string(const llist_t *list, const char *string)
669{
670 while (list) {
671 if (strcmp(list->data, string) == 0) {
672 return(list);
673 }
674 list = list->link;
675 }
676 return(NULL);
677}
678
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000679static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000680{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000681#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000682 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000683#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000684 struct interface_defn_t *currif = NULL;
685 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000686 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000687 char *firstword;
688 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689
690 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
691
Eric Andersen8320b422003-04-02 10:13:26 +0000692 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693 defn->autointerfaces = NULL;
694 defn->mappings = NULL;
695 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000696
Manuel Novoa III cad53642003-03-19 09:13:01 +0000697 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000698
Eric Andersen8320b422003-04-02 10:13:26 +0000699 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000700 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000701
Glenn L McGrath85737042003-01-14 23:26:57 +0000702 firstword = next_word(&buf_ptr);
703 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000704 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000705 continue; /* blank line */
706 }
707
708 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000709#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000710 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000711 currmap->max_matches = 0;
712 currmap->n_matches = 0;
713 currmap->match = NULL;
714
Glenn L McGrath85737042003-01-14 23:26:57 +0000715 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000716 if (currmap->max_matches == currmap->n_matches) {
717 currmap->max_matches = currmap->max_matches * 2 + 1;
718 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
719 }
720
Manuel Novoa III cad53642003-03-19 09:13:01 +0000721 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722 }
723 currmap->max_mappings = 0;
724 currmap->n_mappings = 0;
725 currmap->mapping = NULL;
726 currmap->script = NULL;
727 {
Eric Andersen8320b422003-04-02 10:13:26 +0000728 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000729 while (*where != NULL) {
730 where = &(*where)->next;
731 }
732 *where = currmap;
733 currmap->next = NULL;
734 }
Eric Andersen8320b422003-04-02 10:13:26 +0000735 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000736#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000737 currently_processing = MAPPING;
738 } else if (strcmp(firstword, "iface") == 0) {
739 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000740 char *iface_name;
741 char *address_family_name;
742 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000743 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000744#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
745 &addr_inet,
746#endif
747#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
748 &addr_inet6,
749#endif
750#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
751 &addr_ipx,
752#endif
753 NULL
754 };
755
Eric Andersen8320b422003-04-02 10:13:26 +0000756 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000757 iface_name = next_word(&buf_ptr);
758 address_family_name = next_word(&buf_ptr);
759 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760
Glenn L McGrath85737042003-01-14 23:26:57 +0000761 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000762 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000763 return NULL;
764 }
765
Eric Andersen3c8bca32003-06-20 10:02:29 +0000766 /* ship any trailing whitespace */
767 while (isspace(*buf_ptr)) {
768 ++buf_ptr;
769 }
770
Glenn L McGrath85737042003-01-14 23:26:57 +0000771 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000772 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000773 return NULL;
774 }
775
Manuel Novoa III cad53642003-03-19 09:13:01 +0000776 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000777
778 currif->address_family = get_address_family(addr_fams, address_family_name);
779 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000780 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000781 return NULL;
782 }
783
784 currif->method = get_method(currif->address_family, method_name);
785 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000786 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000787 return NULL;
788 }
789
790 currif->automatic = 1;
791 currif->max_options = 0;
792 currif->n_options = 0;
793 currif->option = NULL;
794
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000795 {
Eric Andersen8320b422003-04-02 10:13:26 +0000796 struct interface_defn_t *tmp;
797 llist_t *iface_list;
798 iface_list = defn->ifaces;
799 while (iface_list) {
800 tmp = (struct interface_defn_t *) iface_list->data;
801 if (duplicate_if(tmp, currif)) {
802 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000803 return NULL;
804 }
Eric Andersen8320b422003-04-02 10:13:26 +0000805 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806 }
807
Eric Andersen8320b422003-04-02 10:13:26 +0000808 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809 }
Eric Andersen8320b422003-04-02 10:13:26 +0000810 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000811 }
812 currently_processing = IFACE;
813 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000814 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000816 /* Check the interface isnt already listed */
817 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000818 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000819 }
820
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000821 /* Add the interface to the list */
Eric Andersen8320b422003-04-02 10:13:26 +0000822 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
823 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 }
825 currently_processing = NONE;
826 } else {
827 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000828 case IFACE:
829 {
830 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831
Eric Andersen8320b422003-04-02 10:13:26 +0000832 if (bb_strlen(buf_ptr) == 0) {
833 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000834 return NULL;
835 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836
Eric Andersen8320b422003-04-02 10:13:26 +0000837 if (strcmp(firstword, "up") != 0
838 && strcmp(firstword, "down") != 0
839 && strcmp(firstword, "pre-up") != 0
840 && strcmp(firstword, "post-down") != 0) {
841 for (i = 0; i < currif->n_options; i++) {
842 if (strcmp(currif->option[i].name, firstword) == 0) {
843 bb_error_msg("duplicate option \"%s\"", buf);
844 return NULL;
845 }
846 }
847 }
848 }
849 if (currif->n_options >= currif->max_options) {
850 struct variable_t *opt;
851
852 currif->max_options = currif->max_options + 10;
853 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
854 currif->option = opt;
855 }
856 currif->option[currif->n_options].name = bb_xstrdup(firstword);
857 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
858 if (!currif->option[currif->n_options].name) {
859 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000860 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000861 }
862 if (!currif->option[currif->n_options].value) {
863 perror(filename);
864 return NULL;
865 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000866 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000867 currif->option[currif->n_options].value);
868 currif->n_options++;
869 break;
870 case MAPPING:
871#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
872 if (strcmp(firstword, "script") == 0) {
873 if (currmap->script != NULL) {
874 bb_error_msg("duplicate script in mapping \"%s\"", buf);
875 return NULL;
876 } else {
877 currmap->script = bb_xstrdup(next_word(&buf_ptr));
878 }
879 } else if (strcmp(firstword, "map") == 0) {
880 if (currmap->max_mappings == currmap->n_mappings) {
881 currmap->max_mappings = currmap->max_mappings * 2 + 1;
882 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
883 }
884 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
885 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000886 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000887 bb_error_msg("misplaced option \"%s\"", buf);
888 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000889 }
Eric Andersen8320b422003-04-02 10:13:26 +0000890#endif
891 break;
892 case NONE:
893 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000894 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000895 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000896 }
897 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000898 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000899 }
900 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000901 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000902 }
903 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000904
905 return defn;
906}
907
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000908static char *setlocalenv(char *format, char *name, char *value)
909{
910 char *result;
911 char *here;
912 char *there;
913
Manuel Novoa III cad53642003-03-19 09:13:01 +0000914 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000915
916 sprintf(result, format, name, value);
917
918 for (here = there = result; *there != '=' && *there; there++) {
919 if (*there == '-')
920 *there = '_';
921 if (isalpha(*there))
922 *there = toupper(*there);
923
924 if (isalnum(*there) || *there == '_') {
925 *here = *there;
926 here++;
927 }
928 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000929 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930
931 return result;
932}
933
Eric Andersen8320b422003-04-02 10:13:26 +0000934static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935{
936 char **environend;
937 int i;
938 const int n_env_entries = iface->n_options + 5;
939 char **ppch;
940
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000941 if (__myenviron != NULL) {
942 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943 free(*ppch);
944 *ppch = NULL;
945 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000946 free(__myenviron);
947 __myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000949 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
950 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951 *environend = NULL;
952
953 for (i = 0; i < iface->n_options; i++) {
954 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000955 || strcmp(iface->option[i].name, "down") == 0
956 || strcmp(iface->option[i].name, "pre-up") == 0
957 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958 continue;
959 }
960 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
961 *environend = NULL;
962 }
963
964 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
965 *environend = NULL;
966 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
967 *environend = NULL;
968 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
969 *environend = NULL;
970 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
971 *environend = NULL;
972 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
973 *environend = NULL;
974}
975
976static int doit(char *str)
977{
978 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000979 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000980 }
981 if (!no_act) {
982 pid_t child;
983 int status;
984
985 fflush(NULL);
986 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000987 case -1: /* failure */
988 return 0;
989 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000990 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000991 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000992 }
993 waitpid(child, &status, 0);
994 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
995 return 0;
996 }
997 }
998 return (1);
999}
1000
Eric Andersen8320b422003-04-02 10:13:26 +00001001static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001002{
1003 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001004 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005 for (i = 0; i < ifd->n_options; i++) {
1006 if (strcmp(ifd->option[i].name, opt) == 0) {
1007 if (!(*exec) (ifd->option[i].value)) {
1008 return 0;
1009 }
1010 }
1011 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001012
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00001013 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001014 if ((*exec)(buf) != 1) {
1015 return 0;
1016 }
1017 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001018}
1019
Eric Andersen8320b422003-04-02 10:13:26 +00001020static int check(char *str) {
1021 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001022}
1023
Eric Andersen8320b422003-04-02 10:13:26 +00001024static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001025{
Eric Andersen8320b422003-04-02 10:13:26 +00001026 if (!iface->method->up(iface,check)) return -1;
1027 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001028 if (!execute_all(iface, doit, "pre-up")) return 0;
1029 if (!iface->method->up(iface, doit)) return 0;
1030 if (!execute_all(iface, doit, "up")) return 0;
1031 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001032}
1033
1034static int iface_down(struct interface_defn_t *iface)
1035{
Eric Andersen8320b422003-04-02 10:13:26 +00001036 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001038 if (!execute_all(iface, doit, "down")) return 0;
1039 if (!iface->method->down(iface, doit)) return 0;
1040 if (!execute_all(iface, doit, "post-down")) return 0;
1041 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001042}
1043
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001044#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045static int popen2(FILE **in, FILE **out, char *command, ...)
1046{
1047 va_list ap;
1048 char *argv[11] = { command };
1049 int argc;
1050 int infd[2], outfd[2];
1051 pid_t pid;
1052
1053 argc = 1;
1054 va_start(ap, command);
1055 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1056 argc++;
1057 }
1058 argv[argc] = NULL; /* make sure */
1059 va_end(ap);
1060
1061 if (pipe(infd) != 0) {
1062 return 0;
1063 }
1064
1065 if (pipe(outfd) != 0) {
1066 close(infd[0]);
1067 close(infd[1]);
1068 return 0;
1069 }
1070
1071 fflush(NULL);
1072 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001073 case -1: /* failure */
1074 close(infd[0]);
1075 close(infd[1]);
1076 close(outfd[0]);
1077 close(outfd[1]);
1078 return 0;
1079 case 0: /* child */
1080 dup2(infd[0], 0);
1081 dup2(outfd[1], 1);
1082 close(infd[0]);
1083 close(infd[1]);
1084 close(outfd[0]);
1085 close(outfd[1]);
1086 execvp(command, argv);
1087 exit(127);
1088 default: /* parent */
1089 *in = fdopen(infd[1], "w");
1090 *out = fdopen(outfd[0], "r");
1091 close(infd[0]);
1092 close(outfd[1]);
1093 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001094 }
1095 /* unreached */
1096}
1097
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001098static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001099{
1100 FILE *in, *out;
1101 int i, status;
1102 pid_t pid;
1103
Eric Andersen8a931792003-07-03 10:20:29 +00001104 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001105
Eric Andersen8a931792003-07-03 10:20:29 +00001106 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001108
1109 /* popen2() returns 0 on failure. */
1110 if (pid == 0)
1111 return logical;
1112
1113 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114 for (i = 0; i < map->n_mappings; i++) {
1115 fprintf(in, "%s\n", map->mapping[i]);
1116 }
1117 fclose(in);
1118 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001119
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001120 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001121 /* If the mapping script exited successfully, try to
1122 * grab a line of output and use that as the name of the
1123 * logical interface. */
1124 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001125
Eric Andersen233b1702003-06-05 19:37:01 +00001126 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001127 /* If we are able to read a line of output from the script,
1128 * remove any trailing whitespace and use this value
1129 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001130 char *pch = new_logical + bb_strlen(new_logical) - 1;
1131
1132 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001133 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001134
1135 free(logical);
1136 logical = new_logical;
1137 } else {
1138 /* If we are UNABLE to read a line of output, discard are
1139 * freshly allocated memory. */
1140 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001141 }
1142 }
Eric Andersen8a931792003-07-03 10:20:29 +00001143
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001144 fclose(out);
1145
Eric Andersen8a931792003-07-03 10:20:29 +00001146 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147}
Eric Andersen8a931792003-07-03 10:20:29 +00001148#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001150static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001151{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001152 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001153 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001155 while (search) {
1156 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001157 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001158 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001159 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001160 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001162 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001163}
1164
1165extern int ifupdown_main(int argc, char **argv)
1166{
Eric Andersen8320b422003-04-02 10:13:26 +00001167 int (*cmds) (struct interface_defn_t *) = NULL;
1168 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001169 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001170 llist_t *state_list = NULL;
1171 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001172 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001173 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001175#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001177#endif
1178 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001180 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001181 int i;
1182
Manuel Novoa III cad53642003-03-19 09:13:01 +00001183 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184 /* ifup command */
1185 cmds = iface_up;
1186 } else {
1187 /* ifdown command */
1188 cmds = iface_down;
1189 }
1190
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001191#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001192 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001193#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001194 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001195#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001196 {
1197 switch (i) {
1198 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001199 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001200 break;
1201 case 'v': /* verbose */
1202 verbose = 1;
1203 break;
1204 case 'a': /* all */
1205 do_all = 1;
1206 break;
1207 case 'n': /* no-act */
1208 no_act = 1;
1209 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001210#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001211 case 'm': /* no-mappings */
1212 run_mappings = 0;
1213 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001214#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001215 case 'f': /* force */
1216 force = 1;
1217 break;
1218 default:
1219 bb_show_usage();
1220 break;
1221 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001223
1224 if (argc - optind > 0) {
1225 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001226 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001227 }
1228 } else {
1229 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001230 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001232 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233
Eric Andersen8320b422003-04-02 10:13:26 +00001234 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001235 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001236 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001237
Eric Andersen3c8bca32003-06-20 10:02:29 +00001238 if (!defn) {
1239 exit(EXIT_FAILURE);
1240 }
1241
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 if (no_act) {
1243 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001244 }
1245
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001246 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247 if (do_all) {
1248 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001249 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001250 } else {
1251#if 0
1252 /* iface_down */
1253 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001254 const llist_t *list = state_list;
1255 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001256 new_item = xmalloc(sizeof(llist_t));
1257 new_item->data = strdup(list->data);
1258 new_item->link = NULL;
1259 list = target_list;
1260 if (list == NULL)
1261 target_list = new_item;
1262 else {
1263 while (list->link) {
1264 list = list->link;
1265 }
1266 list = new_item;
1267 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001268 list = list->link;
1269 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001270 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001271#else
1272
1273 /* iface_down */
1274 const llist_t *list = state_list;
1275 while (list) {
1276 target_list = llist_add_to_end(target_list, strdup(list->data));
1277 list = list->link;
1278 }
1279 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001280#endif
1281 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001283 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001284 }
1285
1286
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001287 /* Update the interfaces */
1288 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001289 llist_t *iface_list;
1290 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001291 char *iface;
1292 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293 char *pch;
1294 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001295 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001296
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001297 iface = strdup(target_list->data);
1298 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001299
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001300 pch = strchr(iface, '=');
1301 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 *pch = '\0';
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001303 liface = strdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001304 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001305 liface = strdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001307
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001308 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001309 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001310
1311 if (cmds == iface_up) {
1312 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001313 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001314 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001315 continue;
1316 }
1317 } else {
1318 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001319 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001320 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001321 continue;
1322 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001323 }
1324 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001325
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001326#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001328 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001329
1330 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1331
1332 for (i = 0; i < currmap->n_matches; i++) {
1333 if (fnmatch(currmap->match[i], liface, 0) != 0)
1334 continue;
1335 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001336 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001337 }
Eric Andersen8a931792003-07-03 10:20:29 +00001338 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001339 break;
1340 }
1341 }
1342 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001343#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344
Eric Andersen8320b422003-04-02 10:13:26 +00001345
1346 iface_list = defn->ifaces;
1347 while (iface_list) {
1348 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001349 if (strcmp(liface, currif->iface) == 0) {
1350 char *oldiface = currif->iface;
1351
1352 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001353 currif->iface = iface;
1354
Eric Andersen8320b422003-04-02 10:13:26 +00001355 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001356
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001357 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001358 cmds_ret = cmds(currif);
1359 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001360 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001361 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001362 any_failures += 1;
1363 } else if (cmds_ret == 0) {
1364 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001365 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001366
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001367 currif->iface = oldiface;
1368 }
Eric Andersen8320b422003-04-02 10:13:26 +00001369 iface_list = iface_list->link;
1370 }
1371 if (verbose) {
1372 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001373 }
1374
1375 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001376 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001377 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001378 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001379 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001380
1381 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001382 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001383 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001384 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001385 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001386 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001387 free(iface_state->data);
1388 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001389 }
1390 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001391 /* Remove an interface from the linked list */
1392 if (iface_state) {
1393 /* This needs to be done better */
1394 free(iface_state->data);
1395 free(iface_state->link);
1396 if (iface_state->link) {
1397 iface_state->data = iface_state->link->data;
1398 iface_state->link = iface_state->link->link;
1399 } else {
1400 iface_state->data = NULL;
1401 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001402 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001403 }
1404 }
1405 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001406 }
1407
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001408 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001409 if (!no_act) {
1410
1411 if (state_fp)
1412 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001413 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001414
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001415 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001416 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001417 }
1418
1419 rewind(state_fp);
1420
1421 while (state_list) {
1422 if (state_list->data) {
1423 fputs(state_list->data, state_fp);
1424 fputc('\n', state_fp);
1425 }
1426 state_list = state_list->link;
1427 }
1428 fflush(state_fp);
1429 }
1430
1431 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001432 if (state_fp != NULL) {
1433 fclose(state_fp);
1434 state_fp = NULL;
1435 }
1436
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001437 if (any_failures)
1438 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001439 return 0;
1440}