blob: 9e6fc6291d17aa94a4d8196b57847ce60b62ea6d [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);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000056
Eric Andersen8320b422003-04-02 10:13:26 +000057struct method_t
58{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000059 char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000060 int (*up)(struct interface_defn_t *ifd, execfn *e);
61 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000062};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000063
Eric Andersen8320b422003-04-02 10:13:26 +000064struct address_family_t
65{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066 char *name;
67 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000068 struct method_t *method;
69};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070
Eric Andersen8320b422003-04-02 10:13:26 +000071struct mapping_defn_t
72{
73 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000074
75 int max_matches;
76 int n_matches;
77 char **match;
78
79 char *script;
80
81 int max_mappings;
82 int n_mappings;
83 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000084};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000085
Eric Andersen8320b422003-04-02 10:13:26 +000086struct variable_t
87{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000088 char *name;
89 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000090};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000091
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000093{
Eric Andersen8320b422003-04-02 10:13:26 +000094 struct address_family_t *address_family;
95 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000096
Rob Landleye813ddb2006-02-28 03:53:14 +000097 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000098 int max_options;
99 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000100 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101};
102
Eric Andersen8320b422003-04-02 10:13:26 +0000103struct interfaces_file_t
104{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000105 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000106 llist_t *ifaces;
107 struct mapping_defn_t *mappings;
108};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000109
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000110static char no_act = 0;
111static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000112static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000113
Eric Andersen66a3af92003-01-27 17:41:19 +0000114#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000115
116static unsigned int count_bits(unsigned int a)
117{
118 unsigned int result;
119 result = (a & 0x55) + ((a >> 1) & 0x55);
120 result = (result & 0x33) + ((result >> 2) & 0x33);
121 return((result & 0x0F) + ((result >> 4) & 0x0F));
122}
123
Eric Andersen66a3af92003-01-27 17:41:19 +0000124static int count_netmask_bits(char *dotted_quad)
125{
Eric Andersen8320b422003-04-02 10:13:26 +0000126 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000127 /* Found a netmask... Check if it is dotted quad */
128 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
129 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000130 result = count_bits(a);
131 result += count_bits(b);
132 result += count_bits(c);
133 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000134 return ((int)result);
135}
136#endif
137
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000138#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 || \
139 ENABLE_FEATURE_IFUPDOWN_IPX
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000140static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
141{
142 if (*pos + str_length >= *len) {
143 char *newbuf;
144
145 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
146 *buf = newbuf;
147 *len = *len * 2 + str_length + 1;
148 }
149
150 while (str_length-- >= 1) {
151 (*buf)[(*pos)++] = *str;
152 str++;
153 }
154 (*buf)[*pos] = '\0';
155}
156
157static int strncmpz(char *l, char *r, size_t llen)
158{
159 int i = strncmp(l, r, llen);
160
161 if (i == 0) {
162 return(-r[llen]);
163 } else {
164 return(i);
165 }
166}
167
Eric Andersen8320b422003-04-02 10:13:26 +0000168static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000169{
170 int i;
171
172 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000173 char *result;
174 static char label_buf[20];
175 strncpy(label_buf, ifd->iface, 19);
176 label_buf[19]=0;
177 result = strchr(label_buf, ':');
178 if (result) {
179 *result=0;
180 }
181 return( label_buf);
182 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000183 return (ifd->iface);
184 } else {
185 for (i = 0; i < ifd->n_options; i++) {
186 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
187 return (ifd->option[i].value);
188 }
189 }
190 }
191
192 return(NULL);
193}
194
Eric Andersen8320b422003-04-02 10:13:26 +0000195static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000196{
197
198 char *result = NULL;
199 size_t pos = 0, len = 0;
200 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
201 int okay[MAX_OPT_DEPTH] = { 1 };
202 int opt_depth = 1;
203
204 while (*command) {
205 switch (*command) {
206
Eric Andersen8320b422003-04-02 10:13:26 +0000207 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000208 addstr(&result, &len, &pos, command, 1);
209 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000210 break;
211 case '\\':
212 if (command[1]) {
213 addstr(&result, &len, &pos, command + 1, 1);
214 command += 2;
215 } else {
216 addstr(&result, &len, &pos, command, 1);
217 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000218 }
Eric Andersen8320b422003-04-02 10:13:26 +0000219 break;
220 case '[':
221 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
222 old_pos[opt_depth] = pos;
223 okay[opt_depth] = 1;
224 opt_depth++;
225 command += 2;
226 } else {
227 addstr(&result, &len, &pos, "[", 1);
228 command++;
229 }
230 break;
231 case ']':
232 if (command[1] == ']' && opt_depth > 1) {
233 opt_depth--;
234 if (!okay[opt_depth]) {
235 pos = old_pos[opt_depth];
236 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000237 }
Eric Andersen8320b422003-04-02 10:13:26 +0000238 command += 2;
239 } else {
240 addstr(&result, &len, &pos, "]", 1);
241 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000242 }
Eric Andersen8320b422003-04-02 10:13:26 +0000243 break;
244 case '%':
245 {
246 char *nextpercent;
247 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000248
Eric Andersen8320b422003-04-02 10:13:26 +0000249 command++;
250 nextpercent = strchr(command, '%');
251 if (!nextpercent) {
252 errno = EUNBALPER;
253 free(result);
254 return (NULL);
255 }
256
257 varvalue = get_var(command, nextpercent - command, ifd);
258
259 if (varvalue) {
260 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
261 } else {
262#ifdef CONFIG_FEATURE_IFUPDOWN_IP
263 /* Sigh... Add a special case for 'ip' to convert from
264 * dotted quad to bit count style netmasks. */
265 if (strncmp(command, "bnmask", 6)==0) {
266 int res;
267 varvalue = get_var("netmask", 7, ifd);
268 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
269 char argument[255];
270 sprintf(argument, "%d", res);
271 addstr(&result, &len, &pos, argument, bb_strlen(argument));
272 command = nextpercent + 1;
273 break;
274 }
275 }
276#endif
277 okay[opt_depth - 1] = 0;
278 }
279
280 command = nextpercent + 1;
281 }
282 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000283 }
284 }
285
286 if (opt_depth > 1) {
287 errno = EUNBALBRACK;
288 free(result);
289 return(NULL);
290 }
291
292 if (!okay[0]) {
293 errno = EUNDEFVAR;
294 free(result);
295 return(NULL);
296 }
297
298 return(result);
299}
300
Eric Andersen8320b422003-04-02 10:13:26 +0000301static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000302{
303 char *out;
304 int ret;
305
306 out = parse(command, ifd);
307 if (!out) {
308 return(0);
309 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000310 ret = (*exec) (out);
311
312 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000313 if (ret != 1) {
314 return(0);
315 }
Eric Andersen8320b422003-04-02 10:13:26 +0000316 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000317}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000318#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000319
320#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000321static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000322{
Eric Andersen8320b422003-04-02 10:13:26 +0000323 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000324}
325
Eric Andersen8320b422003-04-02 10:13:26 +0000326static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000327{
Eric Andersen8320b422003-04-02 10:13:26 +0000328 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329}
330
Eric Andersen8320b422003-04-02 10:13:26 +0000331static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332{
Eric Andersen8320b422003-04-02 10:13:26 +0000333 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000334}
335
Eric Andersen8320b422003-04-02 10:13:26 +0000336static int dynamic_down(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 del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339}
340
Eric Andersen8320b422003-04-02 10:13:26 +0000341static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342 { "dynamic", dynamic_up, dynamic_down, },
343 { "static", static_up_ipx, static_down_ipx, },
344};
345
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000346static struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000348 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349 methods_ipx
350};
351#endif /* IFUP_FEATURE_IPX */
352
353#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000354static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000355{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000356#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000357 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000358 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000359 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000360 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000361#else
Eric Andersen8320b422003-04-02 10:13:26 +0000362 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000363#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364}
365
Eric Andersen8320b422003-04-02 10:13:26 +0000366static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000368#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000369 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000370#else
Eric Andersen8320b422003-04-02 10:13:26 +0000371 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000372#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000373}
374
Eric Andersen8320b422003-04-02 10:13:26 +0000375static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000376{
Eric Andersen8320b422003-04-02 10:13:26 +0000377 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000378#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000379 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
380 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000381 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000382#else
Eric Andersen8320b422003-04-02 10:13:26 +0000383 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
384 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
385 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000386#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000387 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000388}
389
Eric Andersen8320b422003-04-02 10:13:26 +0000390static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000392#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000393 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000394#else
Eric Andersen8320b422003-04-02 10:13:26 +0000395 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000396#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397}
398
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000399#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000400static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000401{
Eric Andersen8320b422003-04-02 10:13:26 +0000402 int result;
403 result = execute("ip tunnel add %iface% mode sit remote "
404 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000405 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000406 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000407 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000408 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409}
410
Eric Andersen8320b422003-04-02 10:13:26 +0000411static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000412{
Eric Andersen8320b422003-04-02 10:13:26 +0000413 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000414}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000415#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000416
Eric Andersen8320b422003-04-02 10:13:26 +0000417static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000419 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000420#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000421 { "static", static_up6, static_down6, },
422 { "loopback", loopback_up6, loopback_down6, },
423};
424
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000425static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000426 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000427 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000428 methods6
429};
430#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
431
432#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000433static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000435#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000436 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000437 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000438 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000439 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000440#else
Eric Andersen8320b422003-04-02 10:13:26 +0000441 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000442#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000443}
444
Eric Andersen8320b422003-04-02 10:13:26 +0000445static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000447#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000448 int result;
449 result = execute("ip addr flush dev %iface%", ifd, exec);
450 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000451 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000452#else
Eric Andersen8320b422003-04-02 10:13:26 +0000453 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000454#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000455}
456
Eric Andersen8320b422003-04-02 10:13:26 +0000457static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000458{
Eric Andersen8320b422003-04-02 10:13:26 +0000459 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000460#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000461 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000462 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
463 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000464 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000465 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000466#else
Eric Andersen8320b422003-04-02 10:13:26 +0000467 result = execute("ifconfig %iface% %address% netmask %netmask% "
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000468 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
469 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
Eric Andersen8320b422003-04-02 10:13:26 +0000470 ifd, exec);
471 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000472 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000473#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000474}
475
Eric Andersen8320b422003-04-02 10:13:26 +0000476static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000477{
Eric Andersen8320b422003-04-02 10:13:26 +0000478 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000479#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000480 result = execute("ip addr flush dev %iface%", ifd, exec);
481 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000482#else
Eric Andersen8320b422003-04-02 10:13:26 +0000483 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
484 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000485#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000486 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000487}
488
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000489static int execable(char *program)
490{
491 struct stat buf;
492 if (0 == stat(program, &buf)) {
493 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
494 return(1);
495 }
496 }
497 return(0);
498}
499
Eric Andersen8320b422003-04-02 10:13:26 +0000500static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000501{
Eric Andersen8320b422003-04-02 10:13:26 +0000502 if (execable("/sbin/udhcpc")) {
503 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
504 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000505 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000506 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
507 } else if (execable("/sbin/dhclient")) {
508 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000509 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000510 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
511 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000512 }
Eric Andersen8320b422003-04-02 10:13:26 +0000513 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514}
515
Eric Andersen8320b422003-04-02 10:13:26 +0000516static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000517{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000518 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000519 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000520 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
521 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
522 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000523 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000524 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000525 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000526 result = execute("pump -i %iface% -k", ifd, exec);
527 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000528 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000530 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000531 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000532 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000533}
534
Eric Andersen8320b422003-04-02 10:13:26 +0000535static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000536{
Eric Andersen8320b422003-04-02 10:13:26 +0000537 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
538 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
539 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000540}
541
Eric Andersen8320b422003-04-02 10:13:26 +0000542static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000543{
Eric Andersen8320b422003-04-02 10:13:26 +0000544 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545}
546
Eric Andersen8320b422003-04-02 10:13:26 +0000547static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548{
Eric Andersen8320b422003-04-02 10:13:26 +0000549 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000550}
551
Eric Andersen8320b422003-04-02 10:13:26 +0000552static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553{
Eric Andersen8320b422003-04-02 10:13:26 +0000554 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
555 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556}
557
Eric Andersen8320b422003-04-02 10:13:26 +0000558static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000559{
Eric Andersen8320b422003-04-02 10:13:26 +0000560 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
561 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562}
563
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000564static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000565{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000566 { "wvdial", wvdial_up, wvdial_down, },
567 { "ppp", ppp_up, ppp_down, },
568 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000569 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000570 { "dhcp", dhcp_up, dhcp_down, },
571 { "loopback", loopback_up, loopback_down, },
572};
573
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000574static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000575{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000577 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000578 methods
579};
580
581#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
582
Glenn L McGrath85737042003-01-14 23:26:57 +0000583static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000584{
Glenn L McGrath85737042003-01-14 23:26:57 +0000585 unsigned short length;
586 char *word;
587
588 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000589 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000590 }
591
Glenn L McGrath85737042003-01-14 23:26:57 +0000592 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000593 word = *buf;
594 while (isspace(*word)) {
595 ++word;
596 }
597
598 /* Skip over comments */
599 if (*word == '#') {
600 return(NULL);
601 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000602
Glenn L McGrath85737042003-01-14 23:26:57 +0000603 /* Find the length of this word */
604 length = strcspn(word, " \t\n");
605 if (length == 0) {
606 return(NULL);
607 }
608 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000609 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
610 if (**buf) {
611 **buf = '\0';
612 (*buf)++;
613 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000614
615 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000616}
617
Eric Andersen8320b422003-04-02 10:13:26 +0000618static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000619{
620 int i;
621
622 for (i = 0; af[i]; i++) {
623 if (strcmp(af[i]->name, name) == 0) {
624 return af[i];
625 }
626 }
627 return NULL;
628}
629
Eric Andersen8320b422003-04-02 10:13:26 +0000630static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000631{
632 int i;
633
634 for (i = 0; i < af->n_methods; i++) {
635 if (strcmp(af->method[i].name, name) == 0) {
636 return &af->method[i];
637 }
638 }
639 return(NULL);
640}
641
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000642static const llist_t *find_list_string(const llist_t *list, const char *string)
643{
644 while (list) {
645 if (strcmp(list->data, string) == 0) {
646 return(list);
647 }
648 list = list->link;
649 }
650 return(NULL);
651}
652
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000653static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000655#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000656 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000657#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000658 struct interface_defn_t *currif = NULL;
659 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000661 char *firstword;
662 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000663
664 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
665
Eric Andersen8320b422003-04-02 10:13:26 +0000666 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000667 defn->autointerfaces = NULL;
668 defn->mappings = NULL;
669 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000670
Manuel Novoa III cad53642003-03-19 09:13:01 +0000671 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000672
Eric Andersen8320b422003-04-02 10:13:26 +0000673 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000674 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000675
Glenn L McGrath85737042003-01-14 23:26:57 +0000676 firstword = next_word(&buf_ptr);
677 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000678 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000679 continue; /* blank line */
680 }
681
682 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000683#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000684 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000685 currmap->max_matches = 0;
686 currmap->n_matches = 0;
687 currmap->match = NULL;
688
Glenn L McGrath85737042003-01-14 23:26:57 +0000689 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000690 if (currmap->max_matches == currmap->n_matches) {
691 currmap->max_matches = currmap->max_matches * 2 + 1;
692 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
693 }
694
Manuel Novoa III cad53642003-03-19 09:13:01 +0000695 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000696 }
697 currmap->max_mappings = 0;
698 currmap->n_mappings = 0;
699 currmap->mapping = NULL;
700 currmap->script = NULL;
701 {
Eric Andersen8320b422003-04-02 10:13:26 +0000702 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000703 while (*where != NULL) {
704 where = &(*where)->next;
705 }
706 *where = currmap;
707 currmap->next = NULL;
708 }
Eric Andersen8320b422003-04-02 10:13:26 +0000709 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000710#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000711 currently_processing = MAPPING;
712 } else if (strcmp(firstword, "iface") == 0) {
713 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000714 char *iface_name;
715 char *address_family_name;
716 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000717 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
719 &addr_inet,
720#endif
721#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
722 &addr_inet6,
723#endif
724#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
725 &addr_ipx,
726#endif
727 NULL
728 };
729
Eric Andersen8320b422003-04-02 10:13:26 +0000730 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000731 iface_name = next_word(&buf_ptr);
732 address_family_name = next_word(&buf_ptr);
733 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734
Glenn L McGrath85737042003-01-14 23:26:57 +0000735 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000736 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000737 return NULL;
738 }
739
Eric Andersen3c8bca32003-06-20 10:02:29 +0000740 /* ship any trailing whitespace */
741 while (isspace(*buf_ptr)) {
742 ++buf_ptr;
743 }
744
Glenn L McGrath85737042003-01-14 23:26:57 +0000745 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000746 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000747 return NULL;
748 }
749
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751
752 currif->address_family = get_address_family(addr_fams, address_family_name);
753 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000754 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000755 return NULL;
756 }
757
758 currif->method = get_method(currif->address_family, method_name);
759 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000760 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000761 return NULL;
762 }
763
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000764 currif->max_options = 0;
765 currif->n_options = 0;
766 currif->option = NULL;
767
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000768 {
Eric Andersen8320b422003-04-02 10:13:26 +0000769 llist_t *iface_list;
Rob Landleye813ddb2006-02-28 03:53:14 +0000770 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
771 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
772 if ((strcmp(tmp->iface, currif->iface) == 0) ||
773 (tmp->address_family == currif->address_family)) {
Eric Andersen8320b422003-04-02 10:13:26 +0000774 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775 return NULL;
776 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000777 }
778
Eric Andersen8320b422003-04-02 10:13:26 +0000779 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000780 }
Eric Andersen8320b422003-04-02 10:13:26 +0000781 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000782 }
783 currently_processing = IFACE;
784 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000785 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000786
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000787 /* Check the interface isnt already listed */
788 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000789 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000790 }
791
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000792 /* Add the interface to the list */
Rob Landley1b751c82005-10-28 09:24:33 +0000793 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000794 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000795 }
796 currently_processing = NONE;
797 } else {
798 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000799 case IFACE:
800 {
801 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802
Eric Andersen8320b422003-04-02 10:13:26 +0000803 if (bb_strlen(buf_ptr) == 0) {
804 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000805 return NULL;
806 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000807
Eric Andersen8320b422003-04-02 10:13:26 +0000808 if (strcmp(firstword, "up") != 0
809 && strcmp(firstword, "down") != 0
810 && strcmp(firstword, "pre-up") != 0
811 && strcmp(firstword, "post-down") != 0) {
812 for (i = 0; i < currif->n_options; i++) {
813 if (strcmp(currif->option[i].name, firstword) == 0) {
814 bb_error_msg("duplicate option \"%s\"", buf);
815 return NULL;
816 }
817 }
818 }
819 }
820 if (currif->n_options >= currif->max_options) {
821 struct variable_t *opt;
822
823 currif->max_options = currif->max_options + 10;
824 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
825 currif->option = opt;
826 }
827 currif->option[currif->n_options].name = bb_xstrdup(firstword);
828 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
829 if (!currif->option[currif->n_options].name) {
830 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000832 }
833 if (!currif->option[currif->n_options].value) {
834 perror(filename);
835 return NULL;
836 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000837 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000838 currif->option[currif->n_options].value);
839 currif->n_options++;
840 break;
841 case MAPPING:
842#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
843 if (strcmp(firstword, "script") == 0) {
844 if (currmap->script != NULL) {
845 bb_error_msg("duplicate script in mapping \"%s\"", buf);
846 return NULL;
847 } else {
848 currmap->script = bb_xstrdup(next_word(&buf_ptr));
849 }
850 } else if (strcmp(firstword, "map") == 0) {
851 if (currmap->max_mappings == currmap->n_mappings) {
852 currmap->max_mappings = currmap->max_mappings * 2 + 1;
853 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
854 }
855 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
856 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000858 bb_error_msg("misplaced option \"%s\"", buf);
859 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000860 }
Eric Andersen8320b422003-04-02 10:13:26 +0000861#endif
862 break;
863 case NONE:
864 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000865 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000866 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000867 }
868 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000869 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000870 }
871 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000872 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000873 }
874 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000875
876 return defn;
877}
878
Rob Landleye813ddb2006-02-28 03:53:14 +0000879static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000880{
881 char *result;
882 char *here;
883 char *there;
884
Manuel Novoa III cad53642003-03-19 09:13:01 +0000885 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000886
887 sprintf(result, format, name, value);
888
889 for (here = there = result; *there != '=' && *there; there++) {
890 if (*there == '-')
891 *there = '_';
892 if (isalpha(*there))
893 *there = toupper(*there);
894
895 if (isalnum(*there) || *there == '_') {
896 *here = *there;
897 here++;
898 }
899 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000900 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000901
902 return result;
903}
904
Rob Landleye813ddb2006-02-28 03:53:14 +0000905static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906{
907 char **environend;
908 int i;
909 const int n_env_entries = iface->n_options + 5;
910 char **ppch;
911
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000912 if (__myenviron != NULL) {
913 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 free(*ppch);
915 *ppch = NULL;
916 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000917 free(__myenviron);
918 __myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000919 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000920 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
921 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000922 *environend = NULL;
923
924 for (i = 0; i < iface->n_options; i++) {
925 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000926 || strcmp(iface->option[i].name, "down") == 0
927 || strcmp(iface->option[i].name, "pre-up") == 0
928 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000929 continue;
930 }
931 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
932 *environend = NULL;
933 }
934
935 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
936 *environend = NULL;
937 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
938 *environend = NULL;
939 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
940 *environend = NULL;
941 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
942 *environend = NULL;
943 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
944 *environend = NULL;
945}
946
947static int doit(char *str)
948{
949 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000950 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951 }
952 if (!no_act) {
953 pid_t child;
954 int status;
955
956 fflush(NULL);
957 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000958 case -1: /* failure */
959 return 0;
960 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000961 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000962 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000963 }
964 waitpid(child, &status, 0);
965 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
966 return 0;
967 }
968 }
969 return (1);
970}
971
Rob Landleye813ddb2006-02-28 03:53:14 +0000972static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000973{
974 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000975 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000976 for (i = 0; i < ifd->n_options; i++) {
977 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000978 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000979 return 0;
980 }
981 }
982 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000983
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000984 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Rob Landleye813ddb2006-02-28 03:53:14 +0000985 if (doit(buf) != 1) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000986 return 0;
987 }
988 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000989}
990
Eric Andersen8320b422003-04-02 10:13:26 +0000991static int check(char *str) {
992 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000993}
994
Eric Andersen8320b422003-04-02 10:13:26 +0000995static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000996{
Eric Andersen8320b422003-04-02 10:13:26 +0000997 if (!iface->method->up(iface,check)) return -1;
998 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000999 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001000 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001001 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001002 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001003}
1004
1005static int iface_down(struct interface_defn_t *iface)
1006{
Eric Andersen8320b422003-04-02 10:13:26 +00001007 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001008 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +00001009 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001010 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001011 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001012 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001013}
1014
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001015#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001016static int popen2(FILE **in, FILE **out, char *command, ...)
1017{
1018 va_list ap;
1019 char *argv[11] = { command };
1020 int argc;
1021 int infd[2], outfd[2];
1022 pid_t pid;
1023
1024 argc = 1;
1025 va_start(ap, command);
1026 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1027 argc++;
1028 }
1029 argv[argc] = NULL; /* make sure */
1030 va_end(ap);
1031
1032 if (pipe(infd) != 0) {
1033 return 0;
1034 }
1035
1036 if (pipe(outfd) != 0) {
1037 close(infd[0]);
1038 close(infd[1]);
1039 return 0;
1040 }
1041
1042 fflush(NULL);
1043 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001044 case -1: /* failure */
1045 close(infd[0]);
1046 close(infd[1]);
1047 close(outfd[0]);
1048 close(outfd[1]);
1049 return 0;
1050 case 0: /* child */
1051 dup2(infd[0], 0);
1052 dup2(outfd[1], 1);
1053 close(infd[0]);
1054 close(infd[1]);
1055 close(outfd[0]);
1056 close(outfd[1]);
1057 execvp(command, argv);
1058 exit(127);
1059 default: /* parent */
1060 *in = fdopen(infd[1], "w");
1061 *out = fdopen(outfd[0], "r");
1062 close(infd[0]);
1063 close(outfd[1]);
1064 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065 }
1066 /* unreached */
1067}
1068
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001069static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070{
1071 FILE *in, *out;
1072 int i, status;
1073 pid_t pid;
1074
Eric Andersen8a931792003-07-03 10:20:29 +00001075 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001076
Eric Andersen8a931792003-07-03 10:20:29 +00001077 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001079
1080 /* popen2() returns 0 on failure. */
1081 if (pid == 0)
1082 return logical;
1083
1084 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085 for (i = 0; i < map->n_mappings; i++) {
1086 fprintf(in, "%s\n", map->mapping[i]);
1087 }
1088 fclose(in);
1089 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001090
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001091 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001092 /* If the mapping script exited successfully, try to
1093 * grab a line of output and use that as the name of the
1094 * logical interface. */
1095 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096
Eric Andersen233b1702003-06-05 19:37:01 +00001097 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001098 /* If we are able to read a line of output from the script,
1099 * remove any trailing whitespace and use this value
1100 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001101 char *pch = new_logical + bb_strlen(new_logical) - 1;
1102
1103 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001104 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001105
1106 free(logical);
1107 logical = new_logical;
1108 } else {
1109 /* If we are UNABLE to read a line of output, discard are
1110 * freshly allocated memory. */
1111 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001112 }
1113 }
Eric Andersen8a931792003-07-03 10:20:29 +00001114
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115 fclose(out);
1116
Eric Andersen8a931792003-07-03 10:20:29 +00001117 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001118}
Eric Andersen8a931792003-07-03 10:20:29 +00001119#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001120
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001121static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001123 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001124 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001125
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001126 while (search) {
1127 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001128 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001129 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001130 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001131 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001133 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134}
1135
1136extern int ifupdown_main(int argc, char **argv)
1137{
Eric Andersen8320b422003-04-02 10:13:26 +00001138 int (*cmds) (struct interface_defn_t *) = NULL;
1139 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001140 llist_t *state_list = NULL;
1141 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001142 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001143 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001144
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001145#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001146 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001147#endif
1148 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001150 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001151 int i;
1152
Manuel Novoa III cad53642003-03-19 09:13:01 +00001153 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 /* ifup command */
1155 cmds = iface_up;
1156 } else {
1157 /* ifdown command */
1158 cmds = iface_down;
1159 }
1160
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001161#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001162 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001163#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001164 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001165#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001166 {
1167 switch (i) {
1168 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001169 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001170 break;
1171 case 'v': /* verbose */
1172 verbose = 1;
1173 break;
1174 case 'a': /* all */
1175 do_all = 1;
1176 break;
1177 case 'n': /* no-act */
1178 no_act = 1;
1179 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001180#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001181 case 'm': /* no-mappings */
1182 run_mappings = 0;
1183 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001184#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001185 case 'f': /* force */
1186 force = 1;
1187 break;
1188 default:
1189 bb_show_usage();
1190 break;
1191 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193
1194 if (argc - optind > 0) {
1195 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001196 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001197 }
1198 } else {
1199 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001200 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001202 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203
Eric Andersen8320b422003-04-02 10:13:26 +00001204 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001206 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207
Eric Andersen3c8bca32003-06-20 10:02:29 +00001208 if (!defn) {
1209 exit(EXIT_FAILURE);
1210 }
1211
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001212 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 if (do_all) {
1214 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001215 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001216 } else {
1217#if 0
1218 /* iface_down */
1219 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001220 const llist_t *list = state_list;
1221 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001222 new_item = xmalloc(sizeof(llist_t));
Rob Landley1b751c82005-10-28 09:24:33 +00001223 new_item->data = bb_xstrdup(list->data);
Eric Andersen8320b422003-04-02 10:13:26 +00001224 new_item->link = NULL;
1225 list = target_list;
1226 if (list == NULL)
1227 target_list = new_item;
1228 else {
1229 while (list->link) {
1230 list = list->link;
1231 }
1232 list = new_item;
1233 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001234 list = list->link;
1235 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001236 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001237#else
1238
1239 /* iface_down */
1240 const llist_t *list = state_list;
1241 while (list) {
Rob Landley1b751c82005-10-28 09:24:33 +00001242 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001243 list = list->link;
1244 }
1245 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001246#endif
1247 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001248 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001249 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 }
1251
1252
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001253 /* Update the interfaces */
1254 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001255 llist_t *iface_list;
1256 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001257 char *iface;
1258 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001259 char *pch;
1260 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001261 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262
Rob Landley1b751c82005-10-28 09:24:33 +00001263 iface = bb_xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001264 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001266 pch = strchr(iface, '=');
1267 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001268 *pch = '\0';
Rob Landley1b751c82005-10-28 09:24:33 +00001269 liface = bb_xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270 } else {
Rob Landley1b751c82005-10-28 09:24:33 +00001271 liface = bb_xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001273
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276
1277 if (cmds == iface_up) {
1278 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001279 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001280 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001281 continue;
1282 }
1283 } else {
1284 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001285 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001286 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001287 continue;
1288 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001289 }
1290 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001291
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001292#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001294 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001295
1296 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1297
1298 for (i = 0; i < currmap->n_matches; i++) {
1299 if (fnmatch(currmap->match[i], liface, 0) != 0)
1300 continue;
1301 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001302 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001303 }
Eric Andersen8a931792003-07-03 10:20:29 +00001304 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001305 break;
1306 }
1307 }
1308 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001309#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001310
Eric Andersen8320b422003-04-02 10:13:26 +00001311
1312 iface_list = defn->ifaces;
1313 while (iface_list) {
1314 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001315 if (strcmp(liface, currif->iface) == 0) {
1316 char *oldiface = currif->iface;
1317
1318 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001319 currif->iface = iface;
1320
Eric Andersen8320b422003-04-02 10:13:26 +00001321 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001322
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001323 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001324 cmds_ret = cmds(currif);
1325 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001326 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001327 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001328 any_failures += 1;
1329 } else if (cmds_ret == 0) {
1330 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001332
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333 currif->iface = oldiface;
1334 }
Eric Andersen8320b422003-04-02 10:13:26 +00001335 iface_list = iface_list->link;
1336 }
1337 if (verbose) {
1338 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001339 }
1340
1341 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001342 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001343 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001345 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001346
1347 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001348 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001349 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001350 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001351 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001352 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001353 free(iface_state->data);
1354 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001355 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001356 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001357 /* Remove an interface from the linked list */
1358 if (iface_state) {
Rob Landley12c24292006-03-01 21:36:18 +00001359 llist_t *l = iface_state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001360 free(iface_state->data);
Rob Landleye7c43b62006-03-01 16:39:45 +00001361 iface_state->data = NULL;
1362 iface_state->link = NULL;
Rob Landley12c24292006-03-01 21:36:18 +00001363 if (l) {
1364 iface_state->data = l->data;
1365 iface_state->link = l->link;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001366 }
Rob Landley12c24292006-03-01 21:36:18 +00001367 free(l);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001368 }
1369 }
1370 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001371 }
1372
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001373 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001374 if (!no_act) {
Rob Landley4217bab2006-02-26 04:42:05 +00001375 FILE *state_fp = NULL;
Eric Andersen66a3af92003-01-27 17:41:19 +00001376
Rob Landleye813ddb2006-02-28 03:53:14 +00001377 state_fp = bb_xfopen(statefile, "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001378 while (state_list) {
1379 if (state_list->data) {
1380 fputs(state_list->data, state_fp);
1381 fputc('\n', state_fp);
1382 }
1383 state_list = state_list->link;
1384 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001385 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001386 }
1387
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001388 if (any_failures)
1389 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001390 return 0;
1391}