blob: 34a43e6fa417b322b84bcd2662cfb8ac1e00bd88 [file] [log] [blame]
Eric Andersen8320b422003-04-02 10:13:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00002/*
3 * ifupdown for busybox
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
7 * Based on ifupdown v 0.6.4 by Anthony Towns
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00008 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000010 * Changes to upstream version
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000011 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000012 * Lines in the interfaces file cannot wrap.
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000013 * To adhere to the FHS, the default state file is /var/run/ifstate.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000014 *
Rob Landley1b751c82005-10-28 09:24:33 +000015 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000016 */
17
Glenn L McGrath0177ce12004-07-21 23:56:31 +000018/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
19
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <sys/stat.h>
21#include <sys/utsname.h>
22#include <sys/wait.h>
23
24#include <ctype.h>
25#include <errno.h>
26#include <fcntl.h>
27#include <fnmatch.h>
28#include <getopt.h>
29#include <stdarg.h>
30#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <unistd.h>
34
Glenn L McGrath9af8a722002-11-11 07:03:02 +000035#include "libbb.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000036
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000037#define MAX_OPT_DEPTH 10
38#define EUNBALBRACK 10001
39#define EUNDEFVAR 10002
40#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041
Eric Andersen233b1702003-06-05 19:37:01 +000042#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43#define MAX_INTERFACE_LENGTH 10
44#endif
Eric Andersen8320b422003-04-02 10:13:26 +000045
46#if 0
47#define debug_noise(fmt, args...) printf(fmt, ## args)
48#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000049#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000050#endif
51
52/* Forward declaration */
53struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
55typedef int (execfn)(char *command);
Eric Andersen8320b422003-04-02 10:13:26 +000056typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000057
Eric Andersen8320b422003-04-02 10:13:26 +000058struct method_t
59{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000060 char *name;
61 command_set *up;
62 command_set *down;
Eric Andersen8320b422003-04-02 10:13:26 +000063};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000064
Eric Andersen8320b422003-04-02 10:13:26 +000065struct address_family_t
66{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000067 char *name;
68 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +000069 struct method_t *method;
70};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000071
Eric Andersen8320b422003-04-02 10:13:26 +000072struct mapping_defn_t
73{
74 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075
76 int max_matches;
77 int n_matches;
78 char **match;
79
80 char *script;
81
82 int max_mappings;
83 int n_mappings;
84 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000085};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000086
Eric Andersen8320b422003-04-02 10:13:26 +000087struct variable_t
88{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000089 char *name;
90 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000091};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000092
Eric Andersenc7bda1c2004-03-15 08:29:22 +000093struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000094{
95 struct interface_defn_t *prev;
96 struct interface_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000097
98 char *iface;
Eric Andersen8320b422003-04-02 10:13:26 +000099 struct address_family_t *address_family;
100 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101
102 int automatic;
103
104 int max_options;
105 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000106 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000107};
108
Eric Andersen8320b422003-04-02 10:13:26 +0000109struct interfaces_file_t
110{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000111 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000112 llist_t *ifaces;
113 struct mapping_defn_t *mappings;
114};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000115
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000116static char no_act = 0;
117static char verbose = 0;
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000118static char **__myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000119
Eric Andersen66a3af92003-01-27 17:41:19 +0000120#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000121
122static unsigned int count_bits(unsigned int a)
123{
124 unsigned int result;
125 result = (a & 0x55) + ((a >> 1) & 0x55);
126 result = (result & 0x33) + ((result >> 2) & 0x33);
127 return((result & 0x0F) + ((result >> 4) & 0x0F));
128}
129
Eric Andersen66a3af92003-01-27 17:41:19 +0000130static int count_netmask_bits(char *dotted_quad)
131{
Eric Andersen8320b422003-04-02 10:13:26 +0000132 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000133 /* Found a netmask... Check if it is dotted quad */
134 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
135 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000136 result = count_bits(a);
137 result += count_bits(b);
138 result += count_bits(c);
139 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000140 return ((int)result);
141}
142#endif
143
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000144#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 || \
145 ENABLE_FEATURE_IFUPDOWN_IPX
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000146static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
147{
148 if (*pos + str_length >= *len) {
149 char *newbuf;
150
151 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
152 *buf = newbuf;
153 *len = *len * 2 + str_length + 1;
154 }
155
156 while (str_length-- >= 1) {
157 (*buf)[(*pos)++] = *str;
158 str++;
159 }
160 (*buf)[*pos] = '\0';
161}
162
163static int strncmpz(char *l, char *r, size_t llen)
164{
165 int i = strncmp(l, r, llen);
166
167 if (i == 0) {
168 return(-r[llen]);
169 } else {
170 return(i);
171 }
172}
173
Eric Andersen8320b422003-04-02 10:13:26 +0000174static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000175{
176 int i;
177
178 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000179 char *result;
180 static char label_buf[20];
181 strncpy(label_buf, ifd->iface, 19);
182 label_buf[19]=0;
183 result = strchr(label_buf, ':');
184 if (result) {
185 *result=0;
186 }
187 return( label_buf);
188 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000189 return (ifd->iface);
190 } else {
191 for (i = 0; i < ifd->n_options; i++) {
192 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
193 return (ifd->option[i].value);
194 }
195 }
196 }
197
198 return(NULL);
199}
200
Eric Andersen8320b422003-04-02 10:13:26 +0000201static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000202{
203
204 char *result = NULL;
205 size_t pos = 0, len = 0;
206 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
207 int okay[MAX_OPT_DEPTH] = { 1 };
208 int opt_depth = 1;
209
210 while (*command) {
211 switch (*command) {
212
Eric Andersen8320b422003-04-02 10:13:26 +0000213 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000214 addstr(&result, &len, &pos, command, 1);
215 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000216 break;
217 case '\\':
218 if (command[1]) {
219 addstr(&result, &len, &pos, command + 1, 1);
220 command += 2;
221 } else {
222 addstr(&result, &len, &pos, command, 1);
223 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000224 }
Eric Andersen8320b422003-04-02 10:13:26 +0000225 break;
226 case '[':
227 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
228 old_pos[opt_depth] = pos;
229 okay[opt_depth] = 1;
230 opt_depth++;
231 command += 2;
232 } else {
233 addstr(&result, &len, &pos, "[", 1);
234 command++;
235 }
236 break;
237 case ']':
238 if (command[1] == ']' && opt_depth > 1) {
239 opt_depth--;
240 if (!okay[opt_depth]) {
241 pos = old_pos[opt_depth];
242 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000243 }
Eric Andersen8320b422003-04-02 10:13:26 +0000244 command += 2;
245 } else {
246 addstr(&result, &len, &pos, "]", 1);
247 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000248 }
Eric Andersen8320b422003-04-02 10:13:26 +0000249 break;
250 case '%':
251 {
252 char *nextpercent;
253 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000254
Eric Andersen8320b422003-04-02 10:13:26 +0000255 command++;
256 nextpercent = strchr(command, '%');
257 if (!nextpercent) {
258 errno = EUNBALPER;
259 free(result);
260 return (NULL);
261 }
262
263 varvalue = get_var(command, nextpercent - command, ifd);
264
265 if (varvalue) {
266 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
267 } else {
268#ifdef CONFIG_FEATURE_IFUPDOWN_IP
269 /* Sigh... Add a special case for 'ip' to convert from
270 * dotted quad to bit count style netmasks. */
271 if (strncmp(command, "bnmask", 6)==0) {
272 int res;
273 varvalue = get_var("netmask", 7, ifd);
274 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
275 char argument[255];
276 sprintf(argument, "%d", res);
277 addstr(&result, &len, &pos, argument, bb_strlen(argument));
278 command = nextpercent + 1;
279 break;
280 }
281 }
282#endif
283 okay[opt_depth - 1] = 0;
284 }
285
286 command = nextpercent + 1;
287 }
288 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000289 }
290 }
291
292 if (opt_depth > 1) {
293 errno = EUNBALBRACK;
294 free(result);
295 return(NULL);
296 }
297
298 if (!okay[0]) {
299 errno = EUNDEFVAR;
300 free(result);
301 return(NULL);
302 }
303
304 return(result);
305}
306
Eric Andersen8320b422003-04-02 10:13:26 +0000307static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000308{
309 char *out;
310 int ret;
311
312 out = parse(command, ifd);
313 if (!out) {
314 return(0);
315 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316 ret = (*exec) (out);
317
318 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000319 if (ret != 1) {
320 return(0);
321 }
Eric Andersen8320b422003-04-02 10:13:26 +0000322 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000323}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000324#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000325
326#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000327static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000328{
Eric Andersen8320b422003-04-02 10:13:26 +0000329 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000330}
331
Eric Andersen8320b422003-04-02 10:13:26 +0000332static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000333{
Eric Andersen8320b422003-04-02 10:13:26 +0000334 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000335}
336
Eric Andersen8320b422003-04-02 10:13:26 +0000337static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000338{
Eric Andersen8320b422003-04-02 10:13:26 +0000339 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340}
341
Eric Andersen8320b422003-04-02 10:13:26 +0000342static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000343{
Eric Andersen8320b422003-04-02 10:13:26 +0000344 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000345}
346
Eric Andersen8320b422003-04-02 10:13:26 +0000347static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000348 { "dynamic", dynamic_up, dynamic_down, },
349 { "static", static_up_ipx, static_down_ipx, },
350};
351
"Vladimir N. Oleynik"e4baaa22005-09-22 12:59:26 +0000352static struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000353 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000354 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000355 methods_ipx
356};
357#endif /* IFUP_FEATURE_IPX */
358
359#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000360static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000361{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000362#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000363 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000364 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000365 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000366 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000367#else
Eric Andersen8320b422003-04-02 10:13:26 +0000368 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000369#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370}
371
Eric Andersen8320b422003-04-02 10:13:26 +0000372static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000373{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000374#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000375 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000376#else
Eric Andersen8320b422003-04-02 10:13:26 +0000377 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000378#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
380
Eric Andersen8320b422003-04-02 10:13:26 +0000381static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382{
Eric Andersen8320b422003-04-02 10:13:26 +0000383 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000384#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000385 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
386 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000387 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000388#else
Eric Andersen8320b422003-04-02 10:13:26 +0000389 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
390 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
391 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000392#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000393 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394}
395
Eric Andersen8320b422003-04-02 10:13:26 +0000396static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000398#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000399 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000400#else
Eric Andersen8320b422003-04-02 10:13:26 +0000401 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000402#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000403}
404
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000405#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000406static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000407{
Eric Andersen8320b422003-04-02 10:13:26 +0000408 int result;
409 result = execute("ip tunnel add %iface% mode sit remote "
410 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000411 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000412 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000413 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000414 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000415}
416
Eric Andersen8320b422003-04-02 10:13:26 +0000417static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418{
Eric Andersen8320b422003-04-02 10:13:26 +0000419 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000420}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000421#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000422
Eric Andersen8320b422003-04-02 10:13:26 +0000423static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000424#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000425 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000426#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000427 { "static", static_up6, static_down6, },
428 { "loopback", loopback_up6, loopback_down6, },
429};
430
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000431static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000432 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000433 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000434 methods6
435};
436#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
437
438#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000439static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000440{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000441#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000442 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000443 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000444 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000445 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000446#else
Eric Andersen8320b422003-04-02 10:13:26 +0000447 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000448#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000449}
450
Eric Andersen8320b422003-04-02 10:13:26 +0000451static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000453#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000454 int result;
455 result = execute("ip addr flush dev %iface%", ifd, exec);
456 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000457 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000458#else
Eric Andersen8320b422003-04-02 10:13:26 +0000459 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000460#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000461}
462
Eric Andersen8320b422003-04-02 10:13:26 +0000463static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464{
Eric Andersen8320b422003-04-02 10:13:26 +0000465 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000466#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000467 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000468 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
469 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000470 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000471 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000472#else
Eric Andersen8320b422003-04-02 10:13:26 +0000473 result = execute("ifconfig %iface% %address% netmask %netmask% "
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000474 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
475 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
Eric Andersen8320b422003-04-02 10:13:26 +0000476 ifd, exec);
477 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000478 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000479#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000480}
481
Eric Andersen8320b422003-04-02 10:13:26 +0000482static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000483{
Eric Andersen8320b422003-04-02 10:13:26 +0000484 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000485#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000486 result = execute("ip addr flush dev %iface%", ifd, exec);
487 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000488#else
Eric Andersen8320b422003-04-02 10:13:26 +0000489 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
490 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000491#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000492 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000493}
494
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000495static int execable(char *program)
496{
497 struct stat buf;
498 if (0 == stat(program, &buf)) {
499 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
500 return(1);
501 }
502 }
503 return(0);
504}
505
Eric Andersen8320b422003-04-02 10:13:26 +0000506static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507{
Eric Andersen8320b422003-04-02 10:13:26 +0000508 if (execable("/sbin/udhcpc")) {
509 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
510 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000511 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000512 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
513 } else if (execable("/sbin/dhclient")) {
514 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000516 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
517 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000518 }
Eric Andersen8320b422003-04-02 10:13:26 +0000519 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000520}
521
Eric Andersen8320b422003-04-02 10:13:26 +0000522static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000523{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000524 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000525 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000526 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
527 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
528 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000529 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000530 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000531 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000532 result = execute("pump -i %iface% -k", ifd, exec);
533 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000534 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000535 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000536 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000537 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000538 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539}
540
Eric Andersen8320b422003-04-02 10:13:26 +0000541static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000542{
Eric Andersen8320b422003-04-02 10:13:26 +0000543 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
544 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
545 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546}
547
Eric Andersen8320b422003-04-02 10:13:26 +0000548static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000549{
Eric Andersen8320b422003-04-02 10:13:26 +0000550 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551}
552
Eric Andersen8320b422003-04-02 10:13:26 +0000553static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000554{
Eric Andersen8320b422003-04-02 10:13:26 +0000555 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556}
557
Eric Andersen8320b422003-04-02 10:13:26 +0000558static int wvdial_up(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 --start -x /usr/bin/wvdial "
561 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562}
563
Eric Andersen8320b422003-04-02 10:13:26 +0000564static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565{
Eric Andersen8320b422003-04-02 10:13:26 +0000566 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
567 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568}
569
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000570static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000571{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000572 { "wvdial", wvdial_up, wvdial_down, },
573 { "ppp", ppp_up, ppp_down, },
574 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000575 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576 { "dhcp", dhcp_up, dhcp_down, },
577 { "loopback", loopback_up, loopback_down, },
578};
579
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000580static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000581{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000582 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000583 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000584 methods
585};
586
587#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
588
Glenn L McGrath85737042003-01-14 23:26:57 +0000589static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000590{
Glenn L McGrath85737042003-01-14 23:26:57 +0000591 unsigned short length;
592 char *word;
593
594 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000595 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596 }
597
Glenn L McGrath85737042003-01-14 23:26:57 +0000598 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000599 word = *buf;
600 while (isspace(*word)) {
601 ++word;
602 }
603
604 /* Skip over comments */
605 if (*word == '#') {
606 return(NULL);
607 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000608
Glenn L McGrath85737042003-01-14 23:26:57 +0000609 /* Find the length of this word */
610 length = strcspn(word, " \t\n");
611 if (length == 0) {
612 return(NULL);
613 }
614 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000615 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
616 if (**buf) {
617 **buf = '\0';
618 (*buf)++;
619 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000620
621 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000622}
623
Eric Andersen8320b422003-04-02 10:13:26 +0000624static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000625{
626 int i;
627
628 for (i = 0; af[i]; i++) {
629 if (strcmp(af[i]->name, name) == 0) {
630 return af[i];
631 }
632 }
633 return NULL;
634}
635
Eric Andersen8320b422003-04-02 10:13:26 +0000636static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000637{
638 int i;
639
640 for (i = 0; i < af->n_methods; i++) {
641 if (strcmp(af->method[i].name, name) == 0) {
642 return &af->method[i];
643 }
644 }
645 return(NULL);
646}
647
Eric Andersen8320b422003-04-02 10:13:26 +0000648static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000649{
650 if (strcmp(ifa->iface, ifb->iface) != 0) {
651 return(0);
652 }
653 if (ifa->address_family != ifb->address_family) {
654 return(0);
655 }
656 return(1);
657}
658
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000659static const llist_t *find_list_string(const llist_t *list, const char *string)
660{
661 while (list) {
662 if (strcmp(list->data, string) == 0) {
663 return(list);
664 }
665 list = list->link;
666 }
667 return(NULL);
668}
669
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000670static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000671{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000672#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000673 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000674#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000675 struct interface_defn_t *currif = NULL;
676 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000677 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000678 char *firstword;
679 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000680
681 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
682
Eric Andersen8320b422003-04-02 10:13:26 +0000683 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000684 defn->autointerfaces = NULL;
685 defn->mappings = NULL;
686 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000687
Manuel Novoa III cad53642003-03-19 09:13:01 +0000688 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689
Eric Andersen8320b422003-04-02 10:13:26 +0000690 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000691 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000692
Glenn L McGrath85737042003-01-14 23:26:57 +0000693 firstword = next_word(&buf_ptr);
694 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000695 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000696 continue; /* blank line */
697 }
698
699 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000700#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000701 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000702 currmap->max_matches = 0;
703 currmap->n_matches = 0;
704 currmap->match = NULL;
705
Glenn L McGrath85737042003-01-14 23:26:57 +0000706 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000707 if (currmap->max_matches == currmap->n_matches) {
708 currmap->max_matches = currmap->max_matches * 2 + 1;
709 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
710 }
711
Manuel Novoa III cad53642003-03-19 09:13:01 +0000712 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000713 }
714 currmap->max_mappings = 0;
715 currmap->n_mappings = 0;
716 currmap->mapping = NULL;
717 currmap->script = NULL;
718 {
Eric Andersen8320b422003-04-02 10:13:26 +0000719 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000720 while (*where != NULL) {
721 where = &(*where)->next;
722 }
723 *where = currmap;
724 currmap->next = NULL;
725 }
Eric Andersen8320b422003-04-02 10:13:26 +0000726 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000727#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 currently_processing = MAPPING;
729 } else if (strcmp(firstword, "iface") == 0) {
730 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000731 char *iface_name;
732 char *address_family_name;
733 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000734 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000735#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
736 &addr_inet,
737#endif
738#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
739 &addr_inet6,
740#endif
741#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
742 &addr_ipx,
743#endif
744 NULL
745 };
746
Eric Andersen8320b422003-04-02 10:13:26 +0000747 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000748 iface_name = next_word(&buf_ptr);
749 address_family_name = next_word(&buf_ptr);
750 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751
Glenn L McGrath85737042003-01-14 23:26:57 +0000752 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000753 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000754 return NULL;
755 }
756
Eric Andersen3c8bca32003-06-20 10:02:29 +0000757 /* ship any trailing whitespace */
758 while (isspace(*buf_ptr)) {
759 ++buf_ptr;
760 }
761
Glenn L McGrath85737042003-01-14 23:26:57 +0000762 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000763 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000764 return NULL;
765 }
766
Manuel Novoa III cad53642003-03-19 09:13:01 +0000767 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000768
769 currif->address_family = get_address_family(addr_fams, address_family_name);
770 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000771 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000772 return NULL;
773 }
774
775 currif->method = get_method(currif->address_family, method_name);
776 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000777 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000778 return NULL;
779 }
780
781 currif->automatic = 1;
782 currif->max_options = 0;
783 currif->n_options = 0;
784 currif->option = NULL;
785
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000786 {
Eric Andersen8320b422003-04-02 10:13:26 +0000787 struct interface_defn_t *tmp;
788 llist_t *iface_list;
789 iface_list = defn->ifaces;
790 while (iface_list) {
791 tmp = (struct interface_defn_t *) iface_list->data;
792 if (duplicate_if(tmp, currif)) {
793 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 return NULL;
795 }
Eric Andersen8320b422003-04-02 10:13:26 +0000796 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000797 }
798
Eric Andersen8320b422003-04-02 10:13:26 +0000799 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000800 }
Eric Andersen8320b422003-04-02 10:13:26 +0000801 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802 }
803 currently_processing = IFACE;
804 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000805 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000807 /* Check the interface isnt already listed */
808 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000809 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000810 }
811
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000812 /* Add the interface to the list */
Rob Landley1b751c82005-10-28 09:24:33 +0000813 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000814 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 }
816 currently_processing = NONE;
817 } else {
818 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000819 case IFACE:
820 {
821 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000822
Eric Andersen8320b422003-04-02 10:13:26 +0000823 if (bb_strlen(buf_ptr) == 0) {
824 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825 return NULL;
826 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000827
Eric Andersen8320b422003-04-02 10:13:26 +0000828 if (strcmp(firstword, "up") != 0
829 && strcmp(firstword, "down") != 0
830 && strcmp(firstword, "pre-up") != 0
831 && strcmp(firstword, "post-down") != 0) {
832 for (i = 0; i < currif->n_options; i++) {
833 if (strcmp(currif->option[i].name, firstword) == 0) {
834 bb_error_msg("duplicate option \"%s\"", buf);
835 return NULL;
836 }
837 }
838 }
839 }
840 if (currif->n_options >= currif->max_options) {
841 struct variable_t *opt;
842
843 currif->max_options = currif->max_options + 10;
844 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
845 currif->option = opt;
846 }
847 currif->option[currif->n_options].name = bb_xstrdup(firstword);
848 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
849 if (!currif->option[currif->n_options].name) {
850 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000851 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000852 }
853 if (!currif->option[currif->n_options].value) {
854 perror(filename);
855 return NULL;
856 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000857 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000858 currif->option[currif->n_options].value);
859 currif->n_options++;
860 break;
861 case MAPPING:
862#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
863 if (strcmp(firstword, "script") == 0) {
864 if (currmap->script != NULL) {
865 bb_error_msg("duplicate script in mapping \"%s\"", buf);
866 return NULL;
867 } else {
868 currmap->script = bb_xstrdup(next_word(&buf_ptr));
869 }
870 } else if (strcmp(firstword, "map") == 0) {
871 if (currmap->max_mappings == currmap->n_mappings) {
872 currmap->max_mappings = currmap->max_mappings * 2 + 1;
873 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
874 }
875 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
876 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000877 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000878 bb_error_msg("misplaced option \"%s\"", buf);
879 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000880 }
Eric Andersen8320b422003-04-02 10:13:26 +0000881#endif
882 break;
883 case NONE:
884 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000885 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000886 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000887 }
888 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000889 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000890 }
891 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000892 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893 }
894 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000895
896 return defn;
897}
898
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000899static char *setlocalenv(char *format, char *name, char *value)
900{
901 char *result;
902 char *here;
903 char *there;
904
Manuel Novoa III cad53642003-03-19 09:13:01 +0000905 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906
907 sprintf(result, format, name, value);
908
909 for (here = there = result; *there != '=' && *there; there++) {
910 if (*there == '-')
911 *there = '_';
912 if (isalpha(*there))
913 *there = toupper(*there);
914
915 if (isalnum(*there) || *there == '_') {
916 *here = *there;
917 here++;
918 }
919 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000920 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000921
922 return result;
923}
924
Eric Andersen8320b422003-04-02 10:13:26 +0000925static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926{
927 char **environend;
928 int i;
929 const int n_env_entries = iface->n_options + 5;
930 char **ppch;
931
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000932 if (__myenviron != NULL) {
933 for (ppch = __myenviron; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000934 free(*ppch);
935 *ppch = NULL;
936 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000937 free(__myenviron);
938 __myenviron = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000939 }
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000940 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
941 environend = __myenviron;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000942 *environend = NULL;
943
944 for (i = 0; i < iface->n_options; i++) {
945 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000946 || strcmp(iface->option[i].name, "down") == 0
947 || strcmp(iface->option[i].name, "pre-up") == 0
948 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949 continue;
950 }
951 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
952 *environend = NULL;
953 }
954
955 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
956 *environend = NULL;
957 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
958 *environend = NULL;
959 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
960 *environend = NULL;
961 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
962 *environend = NULL;
963 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
964 *environend = NULL;
965}
966
967static int doit(char *str)
968{
969 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +0000970 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000971 }
972 if (!no_act) {
973 pid_t child;
974 int status;
975
976 fflush(NULL);
977 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +0000978 case -1: /* failure */
979 return 0;
980 case 0: /* child */
Eric Andersen70a5a1a2005-04-27 11:44:11 +0000981 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
Eric Andersen8320b422003-04-02 10:13:26 +0000982 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000983 }
984 waitpid(child, &status, 0);
985 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
986 return 0;
987 }
988 }
989 return (1);
990}
991
Eric Andersen8320b422003-04-02 10:13:26 +0000992static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000993{
994 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000995 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000996 for (i = 0; i < ifd->n_options; i++) {
997 if (strcmp(ifd->option[i].name, opt) == 0) {
998 if (!(*exec) (ifd->option[i].value)) {
999 return 0;
1000 }
1001 }
1002 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001003
"Vladimir N. Oleynik"39a841c2005-09-29 16:18:57 +00001004 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001005 if ((*exec)(buf) != 1) {
1006 return 0;
1007 }
1008 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009}
1010
Eric Andersen8320b422003-04-02 10:13:26 +00001011static int check(char *str) {
1012 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001013}
1014
Eric Andersen8320b422003-04-02 10:13:26 +00001015static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001016{
Eric Andersen8320b422003-04-02 10:13:26 +00001017 if (!iface->method->up(iface,check)) return -1;
1018 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001019 if (!execute_all(iface, doit, "pre-up")) return 0;
1020 if (!iface->method->up(iface, doit)) return 0;
1021 if (!execute_all(iface, doit, "up")) return 0;
1022 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001023}
1024
1025static int iface_down(struct interface_defn_t *iface)
1026{
Eric Andersen8320b422003-04-02 10:13:26 +00001027 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001028 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001029 if (!execute_all(iface, doit, "down")) return 0;
1030 if (!iface->method->down(iface, doit)) return 0;
1031 if (!execute_all(iface, doit, "post-down")) return 0;
1032 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033}
1034
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001035#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001036static int popen2(FILE **in, FILE **out, char *command, ...)
1037{
1038 va_list ap;
1039 char *argv[11] = { command };
1040 int argc;
1041 int infd[2], outfd[2];
1042 pid_t pid;
1043
1044 argc = 1;
1045 va_start(ap, command);
1046 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1047 argc++;
1048 }
1049 argv[argc] = NULL; /* make sure */
1050 va_end(ap);
1051
1052 if (pipe(infd) != 0) {
1053 return 0;
1054 }
1055
1056 if (pipe(outfd) != 0) {
1057 close(infd[0]);
1058 close(infd[1]);
1059 return 0;
1060 }
1061
1062 fflush(NULL);
1063 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001064 case -1: /* failure */
1065 close(infd[0]);
1066 close(infd[1]);
1067 close(outfd[0]);
1068 close(outfd[1]);
1069 return 0;
1070 case 0: /* child */
1071 dup2(infd[0], 0);
1072 dup2(outfd[1], 1);
1073 close(infd[0]);
1074 close(infd[1]);
1075 close(outfd[0]);
1076 close(outfd[1]);
1077 execvp(command, argv);
1078 exit(127);
1079 default: /* parent */
1080 *in = fdopen(infd[1], "w");
1081 *out = fdopen(outfd[0], "r");
1082 close(infd[0]);
1083 close(outfd[1]);
1084 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085 }
1086 /* unreached */
1087}
1088
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001089static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001090{
1091 FILE *in, *out;
1092 int i, status;
1093 pid_t pid;
1094
Eric Andersen8a931792003-07-03 10:20:29 +00001095 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096
Eric Andersen8a931792003-07-03 10:20:29 +00001097 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001098 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001099
1100 /* popen2() returns 0 on failure. */
1101 if (pid == 0)
1102 return logical;
1103
1104 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001105 for (i = 0; i < map->n_mappings; i++) {
1106 fprintf(in, "%s\n", map->mapping[i]);
1107 }
1108 fclose(in);
1109 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001110
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001111 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001112 /* If the mapping script exited successfully, try to
1113 * grab a line of output and use that as the name of the
1114 * logical interface. */
1115 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001116
Eric Andersen233b1702003-06-05 19:37:01 +00001117 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001118 /* If we are able to read a line of output from the script,
1119 * remove any trailing whitespace and use this value
1120 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001121 char *pch = new_logical + bb_strlen(new_logical) - 1;
1122
1123 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001125
1126 free(logical);
1127 logical = new_logical;
1128 } else {
1129 /* If we are UNABLE to read a line of output, discard are
1130 * freshly allocated memory. */
1131 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 }
1133 }
Eric Andersen8a931792003-07-03 10:20:29 +00001134
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135 fclose(out);
1136
Eric Andersen8a931792003-07-03 10:20:29 +00001137 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138}
Eric Andersen8a931792003-07-03 10:20:29 +00001139#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001140
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001141static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001143 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001144 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146 while (search) {
1147 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001148 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001149 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001150 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001151 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001152 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001153 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154}
1155
1156extern int ifupdown_main(int argc, char **argv)
1157{
Eric Andersen8320b422003-04-02 10:13:26 +00001158 int (*cmds) (struct interface_defn_t *) = NULL;
1159 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001161 llist_t *state_list = NULL;
1162 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001163 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001164 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001165
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001166#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001167 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001168#endif
1169 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001171 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172 int i;
1173
Manuel Novoa III cad53642003-03-19 09:13:01 +00001174 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175 /* ifup command */
1176 cmds = iface_up;
1177 } else {
1178 /* ifdown command */
1179 cmds = iface_down;
1180 }
1181
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001182#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001183 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001184#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001185 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001186#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001187 {
1188 switch (i) {
1189 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001190 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001191 break;
1192 case 'v': /* verbose */
1193 verbose = 1;
1194 break;
1195 case 'a': /* all */
1196 do_all = 1;
1197 break;
1198 case 'n': /* no-act */
1199 no_act = 1;
1200 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001201#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001202 case 'm': /* no-mappings */
1203 run_mappings = 0;
1204 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001205#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001206 case 'f': /* force */
1207 force = 1;
1208 break;
1209 default:
1210 bb_show_usage();
1211 break;
1212 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001214
1215 if (argc - optind > 0) {
1216 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001217 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001218 }
1219 } else {
1220 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001221 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001223 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001224
Eric Andersen8320b422003-04-02 10:13:26 +00001225 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001226 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001227 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228
Eric Andersen3c8bca32003-06-20 10:02:29 +00001229 if (!defn) {
1230 exit(EXIT_FAILURE);
1231 }
1232
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001233 if (no_act) {
1234 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001235 }
1236
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001237 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 if (do_all) {
1239 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001240 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001241 } else {
1242#if 0
1243 /* iface_down */
1244 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001245 const llist_t *list = state_list;
1246 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001247 new_item = xmalloc(sizeof(llist_t));
Rob Landley1b751c82005-10-28 09:24:33 +00001248 new_item->data = bb_xstrdup(list->data);
Eric Andersen8320b422003-04-02 10:13:26 +00001249 new_item->link = NULL;
1250 list = target_list;
1251 if (list == NULL)
1252 target_list = new_item;
1253 else {
1254 while (list->link) {
1255 list = list->link;
1256 }
1257 list = new_item;
1258 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001259 list = list->link;
1260 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001261 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001262#else
1263
1264 /* iface_down */
1265 const llist_t *list = state_list;
1266 while (list) {
Rob Landley1b751c82005-10-28 09:24:33 +00001267 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001268 list = list->link;
1269 }
1270 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001271#endif
1272 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001274 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001275 }
1276
1277
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001278 /* Update the interfaces */
1279 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001280 llist_t *iface_list;
1281 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001282 char *iface;
1283 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001284 char *pch;
1285 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001286 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001287
Rob Landley1b751c82005-10-28 09:24:33 +00001288 iface = bb_xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001289 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001290
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001291 pch = strchr(iface, '=');
1292 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293 *pch = '\0';
Rob Landley1b751c82005-10-28 09:24:33 +00001294 liface = bb_xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001295 } else {
Rob Landley1b751c82005-10-28 09:24:33 +00001296 liface = bb_xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001297 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001298
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001299 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001300 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001301
1302 if (cmds == iface_up) {
1303 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001304 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001305 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001306 continue;
1307 }
1308 } else {
1309 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001310 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001311 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001312 continue;
1313 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001314 }
1315 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001317#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001318 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001319 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001320
1321 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1322
1323 for (i = 0; i < currmap->n_matches; i++) {
1324 if (fnmatch(currmap->match[i], liface, 0) != 0)
1325 continue;
1326 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001327 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001328 }
Eric Andersen8a931792003-07-03 10:20:29 +00001329 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001330 break;
1331 }
1332 }
1333 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001334#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001335
Eric Andersen8320b422003-04-02 10:13:26 +00001336
1337 iface_list = defn->ifaces;
1338 while (iface_list) {
1339 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001340 if (strcmp(liface, currif->iface) == 0) {
1341 char *oldiface = currif->iface;
1342
1343 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344 currif->iface = iface;
1345
Eric Andersen8320b422003-04-02 10:13:26 +00001346 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001347
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001348 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001349 cmds_ret = cmds(currif);
1350 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001351 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001352 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001353 any_failures += 1;
1354 } else if (cmds_ret == 0) {
1355 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001356 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001357
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001358 currif->iface = oldiface;
1359 }
Eric Andersen8320b422003-04-02 10:13:26 +00001360 iface_list = iface_list->link;
1361 }
1362 if (verbose) {
1363 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001364 }
1365
1366 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001367 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001368 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001369 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001370 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001371
1372 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001373 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001374 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001375 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001376 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001377 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001378 free(iface_state->data);
1379 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001380 }
1381 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001382 /* Remove an interface from the linked list */
1383 if (iface_state) {
1384 /* This needs to be done better */
1385 free(iface_state->data);
1386 free(iface_state->link);
1387 if (iface_state->link) {
1388 iface_state->data = iface_state->link->data;
1389 iface_state->link = iface_state->link->link;
1390 } else {
1391 iface_state->data = NULL;
1392 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001393 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001394 }
1395 }
1396 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001397 }
1398
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001399 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001400 if (!no_act) {
1401
1402 if (state_fp)
1403 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001404 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001405
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001406 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001407 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001408 }
1409
1410 rewind(state_fp);
1411
1412 while (state_list) {
1413 if (state_list->data) {
1414 fputs(state_list->data, state_fp);
1415 fputc('\n', state_fp);
1416 }
1417 state_list = state_list->link;
1418 }
1419 fflush(state_fp);
1420 }
1421
1422 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001423 if (state_fp != NULL) {
1424 fclose(state_fp);
1425 state_fp = NULL;
1426 }
1427
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001428 if (any_failures)
1429 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001430 return 0;
1431}