blob: 32b92fb62bceaf3dfebff89e4adf84f35c0c36b4 [file] [log] [blame]
Eric Andersen8320b422003-04-02 10:13:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00002/*
3 * ifupdown for busybox
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
7 * Based on ifupdown v 0.6.4 by Anthony Towns
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00008 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000010 * Changes to upstream version
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000011 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000012 * Lines in the interfaces file cannot wrap.
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000013 * To adhere to the FHS, the default state file is /var/run/ifstate.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000014 *
Rob Landley1b751c82005-10-28 09:24:33 +000015 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000016 */
17
Glenn L McGrath0177ce12004-07-21 23:56:31 +000018/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
19
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <sys/stat.h>
21#include <sys/utsname.h>
22#include <sys/wait.h>
23
24#include <ctype.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <fnmatch.h>
28#include <getopt.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
Bernhard Reutner-Fischer421d9e52006-04-03 16:39:31 +000035#include "busybox.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000036
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000037#define MAX_OPT_DEPTH 10
38#define EUNBALBRACK 10001
39#define EUNDEFVAR 10002
40#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041
Eric Andersen233b1702003-06-05 19:37:01 +000042#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43#define MAX_INTERFACE_LENGTH 10
44#endif
Eric Andersen8320b422003-04-02 10:13:26 +000045
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
Rob Landleyb0a2d6c2006-04-14 20:07:33 +0000138#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000139static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
140{
141 if (*pos + str_length >= *len) {
142 char *newbuf;
143
144 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
145 *buf = newbuf;
146 *len = *len * 2 + str_length + 1;
147 }
148
149 while (str_length-- >= 1) {
150 (*buf)[(*pos)++] = *str;
151 str++;
152 }
153 (*buf)[*pos] = '\0';
154}
155
156static int strncmpz(char *l, char *r, size_t llen)
157{
158 int i = strncmp(l, r, llen);
159
160 if (i == 0) {
161 return(-r[llen]);
162 } else {
163 return(i);
164 }
165}
166
Eric Andersen8320b422003-04-02 10:13:26 +0000167static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000168{
169 int i;
170
171 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000172 char *result;
173 static char label_buf[20];
174 strncpy(label_buf, ifd->iface, 19);
175 label_buf[19]=0;
176 result = strchr(label_buf, ':');
177 if (result) {
178 *result=0;
179 }
180 return( label_buf);
181 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000182 return (ifd->iface);
183 } else {
184 for (i = 0; i < ifd->n_options; i++) {
185 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
186 return (ifd->option[i].value);
187 }
188 }
189 }
190
191 return(NULL);
192}
193
Eric Andersen8320b422003-04-02 10:13:26 +0000194static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000195{
196
197 char *result = NULL;
198 size_t pos = 0, len = 0;
199 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
200 int okay[MAX_OPT_DEPTH] = { 1 };
201 int opt_depth = 1;
202
203 while (*command) {
204 switch (*command) {
205
Eric Andersen8320b422003-04-02 10:13:26 +0000206 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000207 addstr(&result, &len, &pos, command, 1);
208 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000209 break;
210 case '\\':
211 if (command[1]) {
212 addstr(&result, &len, &pos, command + 1, 1);
213 command += 2;
214 } else {
215 addstr(&result, &len, &pos, command, 1);
216 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000217 }
Eric Andersen8320b422003-04-02 10:13:26 +0000218 break;
219 case '[':
220 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
221 old_pos[opt_depth] = pos;
222 okay[opt_depth] = 1;
223 opt_depth++;
224 command += 2;
225 } else {
226 addstr(&result, &len, &pos, "[", 1);
227 command++;
228 }
229 break;
230 case ']':
231 if (command[1] == ']' && opt_depth > 1) {
232 opt_depth--;
233 if (!okay[opt_depth]) {
234 pos = old_pos[opt_depth];
235 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000236 }
Eric Andersen8320b422003-04-02 10:13:26 +0000237 command += 2;
238 } else {
239 addstr(&result, &len, &pos, "]", 1);
240 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000241 }
Eric Andersen8320b422003-04-02 10:13:26 +0000242 break;
243 case '%':
244 {
245 char *nextpercent;
246 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000247
Eric Andersen8320b422003-04-02 10:13:26 +0000248 command++;
249 nextpercent = strchr(command, '%');
250 if (!nextpercent) {
251 errno = EUNBALPER;
252 free(result);
253 return (NULL);
254 }
255
256 varvalue = get_var(command, nextpercent - command, ifd);
257
258 if (varvalue) {
Rob Landleya3896512006-05-07 20:20:34 +0000259 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000260 } else {
261#ifdef CONFIG_FEATURE_IFUPDOWN_IP
262 /* Sigh... Add a special case for 'ip' to convert from
263 * dotted quad to bit count style netmasks. */
264 if (strncmp(command, "bnmask", 6)==0) {
265 int res;
266 varvalue = get_var("netmask", 7, ifd);
267 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
268 char argument[255];
269 sprintf(argument, "%d", res);
Rob Landleya3896512006-05-07 20:20:34 +0000270 addstr(&result, &len, &pos, argument, strlen(argument));
Eric Andersen8320b422003-04-02 10:13:26 +0000271 command = nextpercent + 1;
272 break;
273 }
274 }
275#endif
276 okay[opt_depth - 1] = 0;
277 }
278
279 command = nextpercent + 1;
280 }
281 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000282 }
283 }
284
285 if (opt_depth > 1) {
286 errno = EUNBALBRACK;
287 free(result);
288 return(NULL);
289 }
290
291 if (!okay[0]) {
292 errno = EUNDEFVAR;
293 free(result);
294 return(NULL);
295 }
296
297 return(result);
298}
299
Eric Andersen8320b422003-04-02 10:13:26 +0000300static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000301{
302 char *out;
303 int ret;
304
305 out = parse(command, ifd);
306 if (!out) {
307 return(0);
308 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000309 ret = (*exec) (out);
310
311 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000312 if (ret != 1) {
313 return(0);
314 }
Eric Andersen8320b422003-04-02 10:13:26 +0000315 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000317#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000318
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000319#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000320static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000321{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000322#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000323 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000324 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000325 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000326 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000327#else
Eric Andersen8320b422003-04-02 10:13:26 +0000328 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000329#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000330}
331
Eric Andersen8320b422003-04-02 10:13:26 +0000332static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000333{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000334#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000335 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000336#else
Eric Andersen8320b422003-04-02 10:13:26 +0000337 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000338#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339}
340
Eric Andersen8320b422003-04-02 10:13:26 +0000341static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342{
Eric Andersen8320b422003-04-02 10:13:26 +0000343 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000344#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000345 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
346 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000347 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000348#else
Eric Andersen8320b422003-04-02 10:13:26 +0000349 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
350 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
351 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000352#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000353 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000354}
355
Eric Andersen8320b422003-04-02 10:13:26 +0000356static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000358#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000359 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000360#else
Eric Andersen8320b422003-04-02 10:13:26 +0000361 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000362#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363}
364
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000365#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000366static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367{
Eric Andersen8320b422003-04-02 10:13:26 +0000368 int result;
369 result = execute("ip tunnel add %iface% mode sit remote "
370 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000371 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000372 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000373 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000374 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375}
376
Eric Andersen8320b422003-04-02 10:13:26 +0000377static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378{
Eric Andersen8320b422003-04-02 10:13:26 +0000379 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000380}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000381#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382
Eric Andersen8320b422003-04-02 10:13:26 +0000383static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000384#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000386#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387 { "static", static_up6, static_down6, },
388 { "loopback", loopback_up6, loopback_down6, },
389};
390
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000391static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000393 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394 methods6
395};
396#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
397
398#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000399static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000401#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000402 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000403 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000404 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000405 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000406#else
Eric Andersen8320b422003-04-02 10:13:26 +0000407 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000408#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409}
410
Eric Andersen8320b422003-04-02 10:13:26 +0000411static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000412{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000413#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000414 int result;
415 result = execute("ip addr flush dev %iface%", ifd, exec);
416 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000417 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#else
Eric Andersen8320b422003-04-02 10:13:26 +0000419 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000420#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000421}
422
Eric Andersen8320b422003-04-02 10:13:26 +0000423static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424{
Eric Andersen8320b422003-04-02 10:13:26 +0000425 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000426#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000427 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000428 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
429 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000430 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000431 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000432#else
Eric Andersen8320b422003-04-02 10:13:26 +0000433 result = execute("ifconfig %iface% %address% netmask %netmask% "
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000434 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
435 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
Eric Andersen8320b422003-04-02 10:13:26 +0000436 ifd, exec);
437 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000438 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000439#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000440}
441
Eric Andersen8320b422003-04-02 10:13:26 +0000442static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000443{
Eric Andersen8320b422003-04-02 10:13:26 +0000444 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000445#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000446 result = execute("ip addr flush dev %iface%", ifd, exec);
447 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000448#else
Eric Andersen8320b422003-04-02 10:13:26 +0000449 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
450 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000451#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000452 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000453}
454
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000455static int execable(char *program)
456{
457 struct stat buf;
458 if (0 == stat(program, &buf)) {
459 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
460 return(1);
461 }
462 }
463 return(0);
464}
465
Eric Andersen8320b422003-04-02 10:13:26 +0000466static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000467{
Eric Andersen8320b422003-04-02 10:13:26 +0000468 if (execable("/sbin/udhcpc")) {
469 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
470 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000471 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000472 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
473 } else if (execable("/sbin/dhclient")) {
474 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000476 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
477 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000478 }
Eric Andersen8320b422003-04-02 10:13:26 +0000479 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480}
481
Eric Andersen8320b422003-04-02 10:13:26 +0000482static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000483{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000484 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000485 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000486 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
487 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
488 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000489 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000490 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000491 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000492 result = execute("pump -i %iface% -k", ifd, exec);
493 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000494 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000495 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000496 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000497 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000498 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000499}
500
Eric Andersen8320b422003-04-02 10:13:26 +0000501static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000502{
Eric Andersen8320b422003-04-02 10:13:26 +0000503 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
504 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
505 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000506}
507
Eric Andersen8320b422003-04-02 10:13:26 +0000508static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000509{
Eric Andersen8320b422003-04-02 10:13:26 +0000510 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000511}
512
Eric Andersen8320b422003-04-02 10:13:26 +0000513static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514{
Eric Andersen8320b422003-04-02 10:13:26 +0000515 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000516}
517
Eric Andersen8320b422003-04-02 10:13:26 +0000518static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000519{
Eric Andersen8320b422003-04-02 10:13:26 +0000520 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
521 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000522}
523
Eric Andersen8320b422003-04-02 10:13:26 +0000524static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000525{
Eric Andersen8320b422003-04-02 10:13:26 +0000526 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
527 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000528}
529
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000530static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000531{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532 { "wvdial", wvdial_up, wvdial_down, },
533 { "ppp", ppp_up, ppp_down, },
534 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000535 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000536 { "dhcp", dhcp_up, dhcp_down, },
537 { "loopback", loopback_up, loopback_down, },
538};
539
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000540static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000541{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000542 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000543 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000544 methods
545};
546
547#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
548
Glenn L McGrath85737042003-01-14 23:26:57 +0000549static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000550{
Glenn L McGrath85737042003-01-14 23:26:57 +0000551 unsigned short length;
552 char *word;
553
554 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000555 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556 }
557
Glenn L McGrath85737042003-01-14 23:26:57 +0000558 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000559 word = *buf;
560 while (isspace(*word)) {
561 ++word;
562 }
563
564 /* Skip over comments */
565 if (*word == '#') {
566 return(NULL);
567 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568
Glenn L McGrath85737042003-01-14 23:26:57 +0000569 /* Find the length of this word */
570 length = strcspn(word, " \t\n");
571 if (length == 0) {
572 return(NULL);
573 }
574 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000575 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
576 if (**buf) {
577 **buf = '\0';
578 (*buf)++;
579 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000580
581 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000582}
583
Eric Andersen8320b422003-04-02 10:13:26 +0000584static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000585{
586 int i;
587
588 for (i = 0; af[i]; i++) {
589 if (strcmp(af[i]->name, name) == 0) {
590 return af[i];
591 }
592 }
593 return NULL;
594}
595
Eric Andersen8320b422003-04-02 10:13:26 +0000596static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000597{
598 int i;
599
600 for (i = 0; i < af->n_methods; i++) {
601 if (strcmp(af->method[i].name, name) == 0) {
602 return &af->method[i];
603 }
604 }
605 return(NULL);
606}
607
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000608static const llist_t *find_list_string(const llist_t *list, const char *string)
609{
610 while (list) {
611 if (strcmp(list->data, string) == 0) {
612 return(list);
613 }
614 list = list->link;
615 }
616 return(NULL);
617}
618
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000619static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000620{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000621#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000622 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000623#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000624 struct interface_defn_t *currif = NULL;
625 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000626 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000627 char *firstword;
628 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000629
630 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
631
Eric Andersen8320b422003-04-02 10:13:26 +0000632 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633 defn->autointerfaces = NULL;
634 defn->mappings = NULL;
635 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000636
Manuel Novoa III cad53642003-03-19 09:13:01 +0000637 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000638
Eric Andersen8320b422003-04-02 10:13:26 +0000639 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000640 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000641
Glenn L McGrath85737042003-01-14 23:26:57 +0000642 firstword = next_word(&buf_ptr);
643 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000644 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000645 continue; /* blank line */
646 }
647
648 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000649#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000650 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000651 currmap->max_matches = 0;
652 currmap->n_matches = 0;
653 currmap->match = NULL;
654
Glenn L McGrath85737042003-01-14 23:26:57 +0000655 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000656 if (currmap->max_matches == currmap->n_matches) {
657 currmap->max_matches = currmap->max_matches * 2 + 1;
658 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
659 }
660
Manuel Novoa III cad53642003-03-19 09:13:01 +0000661 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662 }
663 currmap->max_mappings = 0;
664 currmap->n_mappings = 0;
665 currmap->mapping = NULL;
666 currmap->script = NULL;
667 {
Eric Andersen8320b422003-04-02 10:13:26 +0000668 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669 while (*where != NULL) {
670 where = &(*where)->next;
671 }
672 *where = currmap;
673 currmap->next = NULL;
674 }
Eric Andersen8320b422003-04-02 10:13:26 +0000675 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000676#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000677 currently_processing = MAPPING;
678 } else if (strcmp(firstword, "iface") == 0) {
679 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000680 char *iface_name;
681 char *address_family_name;
682 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000683 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000684#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
685 &addr_inet,
686#endif
687#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
688 &addr_inet6,
689#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000690 NULL
691 };
692
Eric Andersen8320b422003-04-02 10:13:26 +0000693 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000694 iface_name = next_word(&buf_ptr);
695 address_family_name = next_word(&buf_ptr);
696 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000697
Glenn L McGrath85737042003-01-14 23:26:57 +0000698 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000699 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000700 return NULL;
701 }
702
Eric Andersen3c8bca32003-06-20 10:02:29 +0000703 /* ship any trailing whitespace */
704 while (isspace(*buf_ptr)) {
705 ++buf_ptr;
706 }
707
Glenn L McGrath85737042003-01-14 23:26:57 +0000708 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000709 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000710 return NULL;
711 }
712
Manuel Novoa III cad53642003-03-19 09:13:01 +0000713 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714
715 currif->address_family = get_address_family(addr_fams, address_family_name);
716 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000717 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718 return NULL;
719 }
720
721 currif->method = get_method(currif->address_family, method_name);
722 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000723 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000724 return NULL;
725 }
726
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000727 currif->max_options = 0;
728 currif->n_options = 0;
729 currif->option = NULL;
730
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000731 {
Eric Andersen8320b422003-04-02 10:13:26 +0000732 llist_t *iface_list;
Rob Landleye813ddb2006-02-28 03:53:14 +0000733 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
734 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Rob Landleya35886c2006-03-22 16:59:12 +0000735 if ((strcmp(tmp->iface, currif->iface) == 0) &&
Rob Landleye813ddb2006-02-28 03:53:14 +0000736 (tmp->address_family == currif->address_family)) {
Eric Andersen8320b422003-04-02 10:13:26 +0000737 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000738 return NULL;
739 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000740 }
741
Eric Andersen8320b422003-04-02 10:13:26 +0000742 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000743 }
Eric Andersen8320b422003-04-02 10:13:26 +0000744 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745 }
746 currently_processing = IFACE;
747 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000748 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000749
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000750 /* Check the interface isnt already listed */
751 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000752 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000753 }
754
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000755 /* Add the interface to the list */
Rob Landley1b751c82005-10-28 09:24:33 +0000756 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000757 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000758 }
759 currently_processing = NONE;
760 } else {
761 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000762 case IFACE:
763 {
764 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000765
Rob Landleya3896512006-05-07 20:20:34 +0000766 if (strlen(buf_ptr) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000767 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000768 return NULL;
769 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000770
Eric Andersen8320b422003-04-02 10:13:26 +0000771 if (strcmp(firstword, "up") != 0
772 && strcmp(firstword, "down") != 0
773 && strcmp(firstword, "pre-up") != 0
774 && strcmp(firstword, "post-down") != 0) {
775 for (i = 0; i < currif->n_options; i++) {
776 if (strcmp(currif->option[i].name, firstword) == 0) {
777 bb_error_msg("duplicate option \"%s\"", buf);
778 return NULL;
779 }
780 }
781 }
782 }
783 if (currif->n_options >= currif->max_options) {
784 struct variable_t *opt;
785
786 currif->max_options = currif->max_options + 10;
787 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
788 currif->option = opt;
789 }
790 currif->option[currif->n_options].name = bb_xstrdup(firstword);
791 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
792 if (!currif->option[currif->n_options].name) {
793 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000795 }
796 if (!currif->option[currif->n_options].value) {
797 perror(filename);
798 return NULL;
799 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000800 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000801 currif->option[currif->n_options].value);
802 currif->n_options++;
803 break;
804 case MAPPING:
805#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
806 if (strcmp(firstword, "script") == 0) {
807 if (currmap->script != NULL) {
808 bb_error_msg("duplicate script in mapping \"%s\"", buf);
809 return NULL;
810 } else {
811 currmap->script = bb_xstrdup(next_word(&buf_ptr));
812 }
813 } else if (strcmp(firstword, "map") == 0) {
814 if (currmap->max_mappings == currmap->n_mappings) {
815 currmap->max_mappings = currmap->max_mappings * 2 + 1;
816 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
817 }
818 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
819 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000820 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000821 bb_error_msg("misplaced option \"%s\"", buf);
822 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000823 }
Eric Andersen8320b422003-04-02 10:13:26 +0000824#endif
825 break;
826 case NONE:
827 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000828 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000829 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000830 }
831 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000832 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833 }
834 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000835 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836 }
837 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838
839 return defn;
840}
841
Rob Landleye813ddb2006-02-28 03:53:14 +0000842static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000843{
844 char *result;
845 char *here;
846 char *there;
847
Rob Landleya3896512006-05-07 20:20:34 +0000848 result = xmalloc(strlen(format) + strlen(name) + strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000849
850 sprintf(result, format, name, value);
851
852 for (here = there = result; *there != '=' && *there; there++) {
853 if (*there == '-')
854 *there = '_';
855 if (isalpha(*there))
856 *there = toupper(*there);
857
858 if (isalnum(*there) || *there == '_') {
859 *here = *there;
860 here++;
861 }
862 }
Rob Landleya3896512006-05-07 20:20:34 +0000863 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864
865 return result;
866}
867
Rob Landleye813ddb2006-02-28 03:53:14 +0000868static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000869{
870 char **environend;
871 int i;
872 const int n_env_entries = iface->n_options + 5;
873 char **ppch;
874
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000875 if (__myenviron != NULL) {
876 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000877 free(*ppch);
878 *ppch = NULL;
879 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000880 free(__myenviron);
881 __myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000882 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000883 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
884 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885 *environend = NULL;
886
887 for (i = 0; i < iface->n_options; i++) {
888 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000889 || strcmp(iface->option[i].name, "down") == 0
890 || strcmp(iface->option[i].name, "pre-up") == 0
891 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892 continue;
893 }
894 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
895 *environend = NULL;
896 }
897
898 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
899 *environend = NULL;
900 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
901 *environend = NULL;
902 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
903 *environend = NULL;
904 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
905 *environend = NULL;
906 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
907 *environend = NULL;
908}
909
910static int doit(char *str)
911{
912 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000913 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 }
915 if (!no_act) {
916 pid_t child;
917 int status;
918
919 fflush(NULL);
920 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000921 case -1: /* failure */
922 return 0;
923 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000924 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000925 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926 }
927 waitpid(child, &status, 0);
928 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
929 return 0;
930 }
931 }
932 return (1);
933}
934
Rob Landleye813ddb2006-02-28 03:53:14 +0000935static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936{
937 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000938 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000939 for (i = 0; i < ifd->n_options; i++) {
940 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000941 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000942 return 0;
943 }
944 }
945 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000946
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +0000947 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Rob Landleye813ddb2006-02-28 03:53:14 +0000948 if (doit(buf) != 1) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000949 return 0;
950 }
951 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000952}
953
Eric Andersen8320b422003-04-02 10:13:26 +0000954static int check(char *str) {
955 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000956}
957
Eric Andersen8320b422003-04-02 10:13:26 +0000958static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959{
Eric Andersen8320b422003-04-02 10:13:26 +0000960 if (!iface->method->up(iface,check)) return -1;
961 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000962 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000963 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000964 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000965 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000966}
967
968static int iface_down(struct interface_defn_t *iface)
969{
Eric Andersen8320b422003-04-02 10:13:26 +0000970 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000971 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000972 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000973 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000974 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000975 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000976}
977
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000978#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000979static int popen2(FILE **in, FILE **out, char *command, ...)
980{
981 va_list ap;
982 char *argv[11] = { command };
983 int argc;
984 int infd[2], outfd[2];
985 pid_t pid;
986
987 argc = 1;
988 va_start(ap, command);
989 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
990 argc++;
991 }
992 argv[argc] = NULL; /* make sure */
993 va_end(ap);
994
995 if (pipe(infd) != 0) {
996 return 0;
997 }
998
999 if (pipe(outfd) != 0) {
1000 close(infd[0]);
1001 close(infd[1]);
1002 return 0;
1003 }
1004
1005 fflush(NULL);
1006 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001007 case -1: /* failure */
1008 close(infd[0]);
1009 close(infd[1]);
1010 close(outfd[0]);
1011 close(outfd[1]);
1012 return 0;
1013 case 0: /* child */
1014 dup2(infd[0], 0);
1015 dup2(outfd[1], 1);
1016 close(infd[0]);
1017 close(infd[1]);
1018 close(outfd[0]);
1019 close(outfd[1]);
1020 execvp(command, argv);
1021 exit(127);
1022 default: /* parent */
1023 *in = fdopen(infd[1], "w");
1024 *out = fdopen(outfd[0], "r");
1025 close(infd[0]);
1026 close(outfd[1]);
1027 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001028 }
1029 /* unreached */
1030}
1031
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001032static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033{
1034 FILE *in, *out;
1035 int i, status;
1036 pid_t pid;
1037
Eric Andersen8a931792003-07-03 10:20:29 +00001038 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001039
Eric Andersen8a931792003-07-03 10:20:29 +00001040 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001042
1043 /* popen2() returns 0 on failure. */
1044 if (pid == 0)
1045 return logical;
1046
1047 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001048 for (i = 0; i < map->n_mappings; i++) {
1049 fprintf(in, "%s\n", map->mapping[i]);
1050 }
1051 fclose(in);
1052 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001053
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001054 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001055 /* If the mapping script exited successfully, try to
1056 * grab a line of output and use that as the name of the
1057 * logical interface. */
1058 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001059
Eric Andersen233b1702003-06-05 19:37:01 +00001060 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001061 /* If we are able to read a line of output from the script,
1062 * remove any trailing whitespace and use this value
1063 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001064 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001065
1066 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001068
1069 free(logical);
1070 logical = new_logical;
1071 } else {
1072 /* If we are UNABLE to read a line of output, discard are
1073 * freshly allocated memory. */
1074 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075 }
1076 }
Eric Andersen8a931792003-07-03 10:20:29 +00001077
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078 fclose(out);
1079
Eric Andersen8a931792003-07-03 10:20:29 +00001080 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001081}
Eric Andersen8a931792003-07-03 10:20:29 +00001082#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001083
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001084static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085{
Rob Landleya3896512006-05-07 20:20:34 +00001086 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001087 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001088
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001089 while (search) {
1090 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001091 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001092 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001093 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001094 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001095 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001096 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001097}
1098
Rob Landleydfba7412006-03-06 20:47:33 +00001099int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100{
Eric Andersen8320b422003-04-02 10:13:26 +00001101 int (*cmds) (struct interface_defn_t *) = NULL;
1102 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001103 llist_t *state_list = NULL;
1104 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001105 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001106 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001108#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001110#endif
1111 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001112 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001113 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114 int i;
1115
Manuel Novoa III cad53642003-03-19 09:13:01 +00001116 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117 /* ifup command */
1118 cmds = iface_up;
1119 } else {
1120 /* ifdown command */
1121 cmds = iface_down;
1122 }
1123
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001124#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001125 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001126#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001127 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001128#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001129 {
1130 switch (i) {
1131 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001132 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001133 break;
1134 case 'v': /* verbose */
1135 verbose = 1;
1136 break;
1137 case 'a': /* all */
1138 do_all = 1;
1139 break;
1140 case 'n': /* no-act */
1141 no_act = 1;
1142 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001143#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001144 case 'm': /* no-mappings */
1145 run_mappings = 0;
1146 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001147#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001148 case 'f': /* force */
1149 force = 1;
1150 break;
1151 default:
1152 bb_show_usage();
1153 break;
1154 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001155 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156
1157 if (argc - optind > 0) {
1158 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001159 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160 }
1161 } else {
1162 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001163 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001165 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166
Eric Andersen8320b422003-04-02 10:13:26 +00001167 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001169 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170
Eric Andersen3c8bca32003-06-20 10:02:29 +00001171 if (!defn) {
1172 exit(EXIT_FAILURE);
1173 }
1174
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001175 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176 if (do_all) {
1177 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001179 } else {
1180#if 0
1181 /* iface_down */
1182 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001183 const llist_t *list = state_list;
1184 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001185 new_item = xmalloc(sizeof(llist_t));
Rob Landley1b751c82005-10-28 09:24:33 +00001186 new_item->data = bb_xstrdup(list->data);
Eric Andersen8320b422003-04-02 10:13:26 +00001187 new_item->link = NULL;
1188 list = target_list;
1189 if (list == NULL)
1190 target_list = new_item;
1191 else {
1192 while (list->link) {
1193 list = list->link;
1194 }
1195 list = new_item;
1196 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001197 list = list->link;
1198 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001199 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001200#else
1201
1202 /* iface_down */
1203 const llist_t *list = state_list;
1204 while (list) {
Rob Landley1b751c82005-10-28 09:24:33 +00001205 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001206 list = list->link;
1207 }
1208 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001209#endif
1210 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001212 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 }
1214
1215
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001216 /* Update the interfaces */
1217 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001218 llist_t *iface_list;
1219 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001220 char *iface;
1221 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 char *pch;
1223 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001224 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225
Rob Landley1b751c82005-10-28 09:24:33 +00001226 iface = bb_xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001227 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001229 pch = strchr(iface, '=');
1230 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231 *pch = '\0';
Rob Landley1b751c82005-10-28 09:24:33 +00001232 liface = bb_xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233 } else {
Rob Landley1b751c82005-10-28 09:24:33 +00001234 liface = bb_xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001235 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001236
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001237 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001238 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239
1240 if (cmds == iface_up) {
1241 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001243 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001244 continue;
1245 }
1246 } else {
1247 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001248 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001249 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 continue;
1251 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252 }
1253 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001254
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001255#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001256 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001257 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258
1259 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1260
1261 for (i = 0; i < currmap->n_matches; i++) {
1262 if (fnmatch(currmap->match[i], liface, 0) != 0)
1263 continue;
1264 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001265 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 }
Eric Andersen8a931792003-07-03 10:20:29 +00001267 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001268 break;
1269 }
1270 }
1271 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001272#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273
Eric Andersen8320b422003-04-02 10:13:26 +00001274
1275 iface_list = defn->ifaces;
1276 while (iface_list) {
1277 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001278 if (strcmp(liface, currif->iface) == 0) {
1279 char *oldiface = currif->iface;
1280
1281 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001282 currif->iface = iface;
1283
Eric Andersen8320b422003-04-02 10:13:26 +00001284 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001285
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001286 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001287 cmds_ret = cmds(currif);
1288 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001289 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001290 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001291 any_failures += 1;
1292 } else if (cmds_ret == 0) {
1293 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001294 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001295
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001296 currif->iface = oldiface;
1297 }
Eric Andersen8320b422003-04-02 10:13:26 +00001298 iface_list = iface_list->link;
1299 }
1300 if (verbose) {
1301 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 }
1303
1304 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001305 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001306 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001307 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001308 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001309
1310 if (cmds == iface_up) {
Rob Landleya3896512006-05-07 20:20:34 +00001311 char *newiface = xmalloc(strlen(iface) + 1 + strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001312 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001313 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001314 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001315 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316 free(iface_state->data);
1317 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001318 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001319 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001320 /* Remove an interface from the linked list */
1321 if (iface_state) {
Rob Landley12c24292006-03-01 21:36:18 +00001322 llist_t *l = iface_state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001323 free(iface_state->data);
Rob Landleye7c43b62006-03-01 16:39:45 +00001324 iface_state->data = NULL;
1325 iface_state->link = NULL;
Rob Landley12c24292006-03-01 21:36:18 +00001326 if (l) {
1327 iface_state->data = l->data;
1328 iface_state->link = l->link;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001329 }
Rob Landley12c24292006-03-01 21:36:18 +00001330 free(l);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331 }
1332 }
1333 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001334 }
1335
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001336 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001337 if (!no_act) {
Rob Landley4217bab2006-02-26 04:42:05 +00001338 FILE *state_fp = NULL;
Eric Andersen66a3af92003-01-27 17:41:19 +00001339
Rob Landleye813ddb2006-02-28 03:53:14 +00001340 state_fp = bb_xfopen(statefile, "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001341 while (state_list) {
1342 if (state_list->data) {
1343 fputs(state_list->data, state_fp);
1344 fputc('\n', state_fp);
1345 }
1346 state_list = state_list->link;
1347 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001348 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001349 }
1350
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001351 if (any_failures)
1352 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001353 return 0;
1354}