blob: b2ce2a091facd52762829e67eb11bc866b9c376f [file] [log] [blame]
Eric Andersen8320b422003-04-02 10:13:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00002/*
3 * ifupdown for busybox
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
7 * Based on ifupdown v 0.6.4 by Anthony Towns
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00008 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000010 * Changes to upstream version
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000011 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000012 * Lines in the interfaces file cannot wrap.
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000013 * To adhere to the FHS, the default state file is /var/run/ifstate.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000014 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 */
29
Glenn L McGrath0177ce12004-07-21 23:56:31 +000030/* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
31
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000032#include <sys/stat.h>
33#include <sys/utsname.h>
34#include <sys/wait.h>
35
36#include <ctype.h>
37#include <errno.h>
38#include <fcntl.h>
39#include <fnmatch.h>
40#include <getopt.h>
41#include <stdarg.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include <unistd.h>
46
Glenn L McGrath9af8a722002-11-11 07:03:02 +000047#include "libbb.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000049#define MAX_OPT_DEPTH 10
50#define EUNBALBRACK 10001
51#define EUNDEFVAR 10002
52#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000053
Eric Andersen233b1702003-06-05 19:37:01 +000054#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
55#define MAX_INTERFACE_LENGTH 10
56#endif
Eric Andersen8320b422003-04-02 10:13:26 +000057
58#if 0
59#define debug_noise(fmt, args...) printf(fmt, ## args)
60#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +000061#define debug_noise(fmt, args...)
Eric Andersen8320b422003-04-02 10:13:26 +000062#endif
63
64/* Forward declaration */
65struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
67typedef int (execfn)(char *command);
Eric Andersen8320b422003-04-02 10:13:26 +000068typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000069
Eric Andersen8320b422003-04-02 10:13:26 +000070extern llist_t *llist_add_to_end(llist_t *list_head, char *data)
71{
72 llist_t *new_item, *tmp, *prev;
73
74 new_item = xmalloc(sizeof(llist_t));
75 new_item->data = data;
76 new_item->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000077
Eric Andersen8320b422003-04-02 10:13:26 +000078 prev = NULL;
79 tmp = list_head;
80 while(tmp) {
81 prev = tmp;
82 tmp = tmp->link;
83 }
84 if (prev) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +000085 prev->link = new_item;
Eric Andersen8320b422003-04-02 10:13:26 +000086 } else {
87 list_head = new_item;
88 }
89
90 return(list_head);
91}
92
93struct method_t
94{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000095 char *name;
96 command_set *up;
97 command_set *down;
Eric Andersen8320b422003-04-02 10:13:26 +000098};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000099
Eric Andersen8320b422003-04-02 10:13:26 +0000100struct address_family_t
101{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000102 char *name;
103 int n_methods;
Eric Andersen8320b422003-04-02 10:13:26 +0000104 struct method_t *method;
105};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000106
Eric Andersen8320b422003-04-02 10:13:26 +0000107struct mapping_defn_t
108{
109 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000110
111 int max_matches;
112 int n_matches;
113 char **match;
114
115 char *script;
116
117 int max_mappings;
118 int n_mappings;
119 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +0000120};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000121
Eric Andersen8320b422003-04-02 10:13:26 +0000122struct variable_t
123{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000124 char *name;
125 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +0000126};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000127
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000128struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +0000129{
130 struct interface_defn_t *prev;
131 struct interface_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000132
133 char *iface;
Eric Andersen8320b422003-04-02 10:13:26 +0000134 struct address_family_t *address_family;
135 struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000136
137 int automatic;
138
139 int max_options;
140 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +0000141 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000142};
143
Eric Andersen8320b422003-04-02 10:13:26 +0000144struct interfaces_file_t
145{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000146 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +0000147 llist_t *ifaces;
148 struct mapping_defn_t *mappings;
149};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000150
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000151static char no_act = 0;
152static char verbose = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000153static char **environ = NULL;
154
Eric Andersen66a3af92003-01-27 17:41:19 +0000155#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000156
157static unsigned int count_bits(unsigned int a)
158{
159 unsigned int result;
160 result = (a & 0x55) + ((a >> 1) & 0x55);
161 result = (result & 0x33) + ((result >> 2) & 0x33);
162 return((result & 0x0F) + ((result >> 4) & 0x0F));
163}
164
Eric Andersen66a3af92003-01-27 17:41:19 +0000165static int count_netmask_bits(char *dotted_quad)
166{
Eric Andersen8320b422003-04-02 10:13:26 +0000167 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000168 /* Found a netmask... Check if it is dotted quad */
169 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
170 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000171 result = count_bits(a);
172 result += count_bits(b);
173 result += count_bits(c);
174 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000175 return ((int)result);
176}
177#endif
178
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000179static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
180{
181 if (*pos + str_length >= *len) {
182 char *newbuf;
183
184 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
185 *buf = newbuf;
186 *len = *len * 2 + str_length + 1;
187 }
188
189 while (str_length-- >= 1) {
190 (*buf)[(*pos)++] = *str;
191 str++;
192 }
193 (*buf)[*pos] = '\0';
194}
195
196static int strncmpz(char *l, char *r, size_t llen)
197{
198 int i = strncmp(l, r, llen);
199
200 if (i == 0) {
201 return(-r[llen]);
202 } else {
203 return(i);
204 }
205}
206
Eric Andersen8320b422003-04-02 10:13:26 +0000207static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000208{
209 int i;
210
211 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000212 char *result;
213 static char label_buf[20];
214 strncpy(label_buf, ifd->iface, 19);
215 label_buf[19]=0;
216 result = strchr(label_buf, ':');
217 if (result) {
218 *result=0;
219 }
220 return( label_buf);
221 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000222 return (ifd->iface);
223 } else {
224 for (i = 0; i < ifd->n_options; i++) {
225 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
226 return (ifd->option[i].value);
227 }
228 }
229 }
230
231 return(NULL);
232}
233
Eric Andersen8320b422003-04-02 10:13:26 +0000234static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000235{
236
237 char *result = NULL;
238 size_t pos = 0, len = 0;
239 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
240 int okay[MAX_OPT_DEPTH] = { 1 };
241 int opt_depth = 1;
242
243 while (*command) {
244 switch (*command) {
245
Eric Andersen8320b422003-04-02 10:13:26 +0000246 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000247 addstr(&result, &len, &pos, command, 1);
248 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000249 break;
250 case '\\':
251 if (command[1]) {
252 addstr(&result, &len, &pos, command + 1, 1);
253 command += 2;
254 } else {
255 addstr(&result, &len, &pos, command, 1);
256 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000257 }
Eric Andersen8320b422003-04-02 10:13:26 +0000258 break;
259 case '[':
260 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
261 old_pos[opt_depth] = pos;
262 okay[opt_depth] = 1;
263 opt_depth++;
264 command += 2;
265 } else {
266 addstr(&result, &len, &pos, "[", 1);
267 command++;
268 }
269 break;
270 case ']':
271 if (command[1] == ']' && opt_depth > 1) {
272 opt_depth--;
273 if (!okay[opt_depth]) {
274 pos = old_pos[opt_depth];
275 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000276 }
Eric Andersen8320b422003-04-02 10:13:26 +0000277 command += 2;
278 } else {
279 addstr(&result, &len, &pos, "]", 1);
280 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000281 }
Eric Andersen8320b422003-04-02 10:13:26 +0000282 break;
283 case '%':
284 {
285 char *nextpercent;
286 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000287
Eric Andersen8320b422003-04-02 10:13:26 +0000288 command++;
289 nextpercent = strchr(command, '%');
290 if (!nextpercent) {
291 errno = EUNBALPER;
292 free(result);
293 return (NULL);
294 }
295
296 varvalue = get_var(command, nextpercent - command, ifd);
297
298 if (varvalue) {
299 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
300 } else {
301#ifdef CONFIG_FEATURE_IFUPDOWN_IP
302 /* Sigh... Add a special case for 'ip' to convert from
303 * dotted quad to bit count style netmasks. */
304 if (strncmp(command, "bnmask", 6)==0) {
305 int res;
306 varvalue = get_var("netmask", 7, ifd);
307 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
308 char argument[255];
309 sprintf(argument, "%d", res);
310 addstr(&result, &len, &pos, argument, bb_strlen(argument));
311 command = nextpercent + 1;
312 break;
313 }
314 }
315#endif
316 okay[opt_depth - 1] = 0;
317 }
318
319 command = nextpercent + 1;
320 }
321 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000322 }
323 }
324
325 if (opt_depth > 1) {
326 errno = EUNBALBRACK;
327 free(result);
328 return(NULL);
329 }
330
331 if (!okay[0]) {
332 errno = EUNDEFVAR;
333 free(result);
334 return(NULL);
335 }
336
337 return(result);
338}
339
Eric Andersen8320b422003-04-02 10:13:26 +0000340static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000341{
342 char *out;
343 int ret;
344
345 out = parse(command, ifd);
346 if (!out) {
347 return(0);
348 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000349 ret = (*exec) (out);
350
351 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000352 if (ret != 1) {
353 return(0);
354 }
Eric Andersen8320b422003-04-02 10:13:26 +0000355 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000356}
357
358#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000359static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000360{
Eric Andersen8320b422003-04-02 10:13:26 +0000361 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362}
363
Eric Andersen8320b422003-04-02 10:13:26 +0000364static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000365{
Eric Andersen8320b422003-04-02 10:13:26 +0000366 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367}
368
Eric Andersen8320b422003-04-02 10:13:26 +0000369static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370{
Eric Andersen8320b422003-04-02 10:13:26 +0000371 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372}
373
Eric Andersen8320b422003-04-02 10:13:26 +0000374static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375{
Eric Andersen8320b422003-04-02 10:13:26 +0000376 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377}
378
Eric Andersen8320b422003-04-02 10:13:26 +0000379static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000380 { "dynamic", dynamic_up, dynamic_down, },
381 { "static", static_up_ipx, static_down_ipx, },
382};
383
Eric Andersen8320b422003-04-02 10:13:26 +0000384struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000386 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387 methods_ipx
388};
389#endif /* IFUP_FEATURE_IPX */
390
391#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000392static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000393{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000394#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000395 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000396 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000397 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000398 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000399#else
Eric Andersen8320b422003-04-02 10:13:26 +0000400 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000401#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000402}
403
Eric Andersen8320b422003-04-02 10:13:26 +0000404static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000405{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000406#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000407 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000408#else
Eric Andersen8320b422003-04-02 10:13:26 +0000409 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000410#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000411}
412
Eric Andersen8320b422003-04-02 10:13:26 +0000413static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000414{
Eric Andersen8320b422003-04-02 10:13:26 +0000415 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000416#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000417 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
418 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000419 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000420#else
Eric Andersen8320b422003-04-02 10:13:26 +0000421 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
422 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
423 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000424#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000425 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000426}
427
Eric Andersen8320b422003-04-02 10:13:26 +0000428static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000429{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000430#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000431 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000432#else
Eric Andersen8320b422003-04-02 10:13:26 +0000433 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000434#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000435}
436
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000437#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000438static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000439{
Eric Andersen8320b422003-04-02 10:13:26 +0000440 int result;
441 result = execute("ip tunnel add %iface% mode sit remote "
442 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000443 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000444 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000445 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000446 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000447}
448
Eric Andersen8320b422003-04-02 10:13:26 +0000449static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000450{
Eric Andersen8320b422003-04-02 10:13:26 +0000451 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000453#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000454
Eric Andersen8320b422003-04-02 10:13:26 +0000455static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000456#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000457 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000458#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000459 { "static", static_up6, static_down6, },
460 { "loopback", loopback_up6, loopback_down6, },
461};
462
Eric Andersen8320b422003-04-02 10:13:26 +0000463struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000465 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000466 methods6
467};
468#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
469
470#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000471static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000473#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000474 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000475 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000476 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000477 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000478#else
Eric Andersen8320b422003-04-02 10:13:26 +0000479 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000480#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000481}
482
Eric Andersen8320b422003-04-02 10:13:26 +0000483static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000484{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000485#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000486 int result;
487 result = execute("ip addr flush dev %iface%", ifd, exec);
488 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000489 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000490#else
Eric Andersen8320b422003-04-02 10:13:26 +0000491 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000492#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000493}
494
Eric Andersen8320b422003-04-02 10:13:26 +0000495static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000496{
Eric Andersen8320b422003-04-02 10:13:26 +0000497 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000498#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000499 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000500 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
501 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000502 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000503 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000504#else
Eric Andersen8320b422003-04-02 10:13:26 +0000505 result = execute("ifconfig %iface% %address% netmask %netmask% "
506 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
507 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
508 ifd, exec);
509 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000510 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000511#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000512}
513
Eric Andersen8320b422003-04-02 10:13:26 +0000514static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515{
Eric Andersen8320b422003-04-02 10:13:26 +0000516 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000517#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000518 result = execute("ip addr flush dev %iface%", ifd, exec);
519 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000520#else
Eric Andersen8320b422003-04-02 10:13:26 +0000521 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
522 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000523#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000524 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000525}
526
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000527static int execable(char *program)
528{
529 struct stat buf;
530 if (0 == stat(program, &buf)) {
531 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
532 return(1);
533 }
534 }
535 return(0);
536}
537
Eric Andersen8320b422003-04-02 10:13:26 +0000538static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539{
Eric Andersen8320b422003-04-02 10:13:26 +0000540 if (execable("/sbin/udhcpc")) {
541 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
542 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000543 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000544 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
545 } else if (execable("/sbin/dhclient")) {
546 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000547 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000548 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
549 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000550 }
Eric Andersen8320b422003-04-02 10:13:26 +0000551 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000552}
553
Eric Andersen8320b422003-04-02 10:13:26 +0000554static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000555{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000556 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000557 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000558 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
559 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
560 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000561 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000562 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000563 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000564 result = execute("pump -i %iface% -k", ifd, exec);
565 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000566 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000567 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000568 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000569 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000570 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000571}
572
Eric Andersen8320b422003-04-02 10:13:26 +0000573static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000574{
Eric Andersen8320b422003-04-02 10:13:26 +0000575 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
576 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
577 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000578}
579
Eric Andersen8320b422003-04-02 10:13:26 +0000580static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000581{
Eric Andersen8320b422003-04-02 10:13:26 +0000582 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000583}
584
Eric Andersen8320b422003-04-02 10:13:26 +0000585static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000586{
Eric Andersen8320b422003-04-02 10:13:26 +0000587 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000588}
589
Eric Andersen8320b422003-04-02 10:13:26 +0000590static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000591{
Eric Andersen8320b422003-04-02 10:13:26 +0000592 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
593 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594}
595
Eric Andersen8320b422003-04-02 10:13:26 +0000596static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000597{
Eric Andersen8320b422003-04-02 10:13:26 +0000598 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
599 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000600}
601
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000602static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000603{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000604 { "wvdial", wvdial_up, wvdial_down, },
605 { "ppp", ppp_up, ppp_down, },
606 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000607 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000608 { "dhcp", dhcp_up, dhcp_down, },
609 { "loopback", loopback_up, loopback_down, },
610};
611
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000612struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000613{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000614 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000615 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000616 methods
617};
618
619#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
620
Glenn L McGrath85737042003-01-14 23:26:57 +0000621static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000622{
Glenn L McGrath85737042003-01-14 23:26:57 +0000623 unsigned short length;
624 char *word;
625
626 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000628 }
629
Glenn L McGrath85737042003-01-14 23:26:57 +0000630 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000631 word = *buf;
632 while (isspace(*word)) {
633 ++word;
634 }
635
636 /* Skip over comments */
637 if (*word == '#') {
638 return(NULL);
639 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000640
Glenn L McGrath85737042003-01-14 23:26:57 +0000641 /* Find the length of this word */
642 length = strcspn(word, " \t\n");
643 if (length == 0) {
644 return(NULL);
645 }
646 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000647 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
648 if (**buf) {
649 **buf = '\0';
650 (*buf)++;
651 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000652
653 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654}
655
Eric Andersen8320b422003-04-02 10:13:26 +0000656static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657{
658 int i;
659
660 for (i = 0; af[i]; i++) {
661 if (strcmp(af[i]->name, name) == 0) {
662 return af[i];
663 }
664 }
665 return NULL;
666}
667
Eric Andersen8320b422003-04-02 10:13:26 +0000668static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000669{
670 int i;
671
672 for (i = 0; i < af->n_methods; i++) {
673 if (strcmp(af->method[i].name, name) == 0) {
674 return &af->method[i];
675 }
676 }
677 return(NULL);
678}
679
Eric Andersen8320b422003-04-02 10:13:26 +0000680static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681{
682 if (strcmp(ifa->iface, ifb->iface) != 0) {
683 return(0);
684 }
685 if (ifa->address_family != ifb->address_family) {
686 return(0);
687 }
688 return(1);
689}
690
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000691static const llist_t *find_list_string(const llist_t *list, const char *string)
692{
693 while (list) {
694 if (strcmp(list->data, string) == 0) {
695 return(list);
696 }
697 list = list->link;
698 }
699 return(NULL);
700}
701
Eric Andersen8320b422003-04-02 10:13:26 +0000702static struct interfaces_file_t *read_interfaces(char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000703{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000704#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000705 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000706#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000707 struct interface_defn_t *currif = NULL;
708 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000710 char *firstword;
711 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000712
713 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
714
Eric Andersen8320b422003-04-02 10:13:26 +0000715 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000716 defn->autointerfaces = NULL;
717 defn->mappings = NULL;
718 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000719
Manuel Novoa III cad53642003-03-19 09:13:01 +0000720 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000721
Eric Andersen8320b422003-04-02 10:13:26 +0000722 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000723 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000724
Glenn L McGrath85737042003-01-14 23:26:57 +0000725 firstword = next_word(&buf_ptr);
726 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000727 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 continue; /* blank line */
729 }
730
731 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000732#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000733 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734 currmap->max_matches = 0;
735 currmap->n_matches = 0;
736 currmap->match = NULL;
737
Glenn L McGrath85737042003-01-14 23:26:57 +0000738 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000739 if (currmap->max_matches == currmap->n_matches) {
740 currmap->max_matches = currmap->max_matches * 2 + 1;
741 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
742 }
743
Manuel Novoa III cad53642003-03-19 09:13:01 +0000744 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745 }
746 currmap->max_mappings = 0;
747 currmap->n_mappings = 0;
748 currmap->mapping = NULL;
749 currmap->script = NULL;
750 {
Eric Andersen8320b422003-04-02 10:13:26 +0000751 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000752 while (*where != NULL) {
753 where = &(*where)->next;
754 }
755 *where = currmap;
756 currmap->next = NULL;
757 }
Eric Andersen8320b422003-04-02 10:13:26 +0000758 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000759#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760 currently_processing = MAPPING;
761 } else if (strcmp(firstword, "iface") == 0) {
762 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000763 char *iface_name;
764 char *address_family_name;
765 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000766 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000767#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
768 &addr_inet,
769#endif
770#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
771 &addr_inet6,
772#endif
773#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
774 &addr_ipx,
775#endif
776 NULL
777 };
778
Eric Andersen8320b422003-04-02 10:13:26 +0000779 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000780 iface_name = next_word(&buf_ptr);
781 address_family_name = next_word(&buf_ptr);
782 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783
Glenn L McGrath85737042003-01-14 23:26:57 +0000784 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000785 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000786 return NULL;
787 }
788
Eric Andersen3c8bca32003-06-20 10:02:29 +0000789 /* ship any trailing whitespace */
790 while (isspace(*buf_ptr)) {
791 ++buf_ptr;
792 }
793
Glenn L McGrath85737042003-01-14 23:26:57 +0000794 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000795 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000796 return NULL;
797 }
798
Manuel Novoa III cad53642003-03-19 09:13:01 +0000799 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000800
801 currif->address_family = get_address_family(addr_fams, address_family_name);
802 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000803 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000804 return NULL;
805 }
806
807 currif->method = get_method(currif->address_family, method_name);
808 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000809 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000810 return NULL;
811 }
812
813 currif->automatic = 1;
814 currif->max_options = 0;
815 currif->n_options = 0;
816 currif->option = NULL;
817
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000818 {
Eric Andersen8320b422003-04-02 10:13:26 +0000819 struct interface_defn_t *tmp;
820 llist_t *iface_list;
821 iface_list = defn->ifaces;
822 while (iface_list) {
823 tmp = (struct interface_defn_t *) iface_list->data;
824 if (duplicate_if(tmp, currif)) {
825 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000826 return NULL;
827 }
Eric Andersen8320b422003-04-02 10:13:26 +0000828 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000829 }
830
Eric Andersen8320b422003-04-02 10:13:26 +0000831 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000832 }
Eric Andersen8320b422003-04-02 10:13:26 +0000833 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000834 }
835 currently_processing = IFACE;
836 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000837 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000839 /* Check the interface isnt already listed */
840 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000841 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000842 }
843
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000844 /* Add the interface to the list */
Eric Andersen8320b422003-04-02 10:13:26 +0000845 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
846 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000847 }
848 currently_processing = NONE;
849 } else {
850 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000851 case IFACE:
852 {
853 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000854
Eric Andersen8320b422003-04-02 10:13:26 +0000855 if (bb_strlen(buf_ptr) == 0) {
856 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000857 return NULL;
858 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859
Eric Andersen8320b422003-04-02 10:13:26 +0000860 if (strcmp(firstword, "up") != 0
861 && strcmp(firstword, "down") != 0
862 && strcmp(firstword, "pre-up") != 0
863 && strcmp(firstword, "post-down") != 0) {
864 for (i = 0; i < currif->n_options; i++) {
865 if (strcmp(currif->option[i].name, firstword) == 0) {
866 bb_error_msg("duplicate option \"%s\"", buf);
867 return NULL;
868 }
869 }
870 }
871 }
872 if (currif->n_options >= currif->max_options) {
873 struct variable_t *opt;
874
875 currif->max_options = currif->max_options + 10;
876 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
877 currif->option = opt;
878 }
879 currif->option[currif->n_options].name = bb_xstrdup(firstword);
880 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
881 if (!currif->option[currif->n_options].name) {
882 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000883 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000884 }
885 if (!currif->option[currif->n_options].value) {
886 perror(filename);
887 return NULL;
888 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000889 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000890 currif->option[currif->n_options].value);
891 currif->n_options++;
892 break;
893 case MAPPING:
894#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
895 if (strcmp(firstword, "script") == 0) {
896 if (currmap->script != NULL) {
897 bb_error_msg("duplicate script in mapping \"%s\"", buf);
898 return NULL;
899 } else {
900 currmap->script = bb_xstrdup(next_word(&buf_ptr));
901 }
902 } else if (strcmp(firstword, "map") == 0) {
903 if (currmap->max_mappings == currmap->n_mappings) {
904 currmap->max_mappings = currmap->max_mappings * 2 + 1;
905 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
906 }
907 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
908 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000909 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000910 bb_error_msg("misplaced option \"%s\"", buf);
911 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000912 }
Eric Andersen8320b422003-04-02 10:13:26 +0000913#endif
914 break;
915 case NONE:
916 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000917 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000919 }
920 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000921 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000922 }
923 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000924 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000925 }
926 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927
928 return defn;
929}
930
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000931static char *setlocalenv(char *format, char *name, char *value)
932{
933 char *result;
934 char *here;
935 char *there;
936
Manuel Novoa III cad53642003-03-19 09:13:01 +0000937 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938
939 sprintf(result, format, name, value);
940
941 for (here = there = result; *there != '=' && *there; there++) {
942 if (*there == '-')
943 *there = '_';
944 if (isalpha(*there))
945 *there = toupper(*there);
946
947 if (isalnum(*there) || *there == '_') {
948 *here = *there;
949 here++;
950 }
951 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000952 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000953
954 return result;
955}
956
Eric Andersen8320b422003-04-02 10:13:26 +0000957static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958{
959 char **environend;
960 int i;
961 const int n_env_entries = iface->n_options + 5;
962 char **ppch;
963
964 if (environ != NULL) {
965 for (ppch = environ; *ppch; ppch++) {
966 free(*ppch);
967 *ppch = NULL;
968 }
969 free(environ);
970 environ = NULL;
971 }
972 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
973 environend = environ;
974 *environend = NULL;
975
976 for (i = 0; i < iface->n_options; i++) {
977 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000978 || strcmp(iface->option[i].name, "down") == 0
979 || strcmp(iface->option[i].name, "pre-up") == 0
980 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000981 continue;
982 }
983 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
984 *environend = NULL;
985 }
986
987 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
988 *environend = NULL;
989 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
990 *environend = NULL;
991 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
992 *environend = NULL;
993 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
994 *environend = NULL;
995 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
996 *environend = NULL;
997}
998
999static int doit(char *str)
1000{
1001 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +00001002 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001003 }
1004 if (!no_act) {
1005 pid_t child;
1006 int status;
1007
1008 fflush(NULL);
1009 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001010 case -1: /* failure */
1011 return 0;
1012 case 0: /* child */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001013 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ);
Eric Andersen8320b422003-04-02 10:13:26 +00001014 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001015 }
1016 waitpid(child, &status, 0);
1017 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1018 return 0;
1019 }
1020 }
1021 return (1);
1022}
1023
Eric Andersen8320b422003-04-02 10:13:26 +00001024static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001025{
1026 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001027 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001028 for (i = 0; i < ifd->n_options; i++) {
1029 if (strcmp(ifd->option[i].name, opt) == 0) {
1030 if (!(*exec) (ifd->option[i].value)) {
1031 return 0;
1032 }
1033 }
1034 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001035
Eric Anderseneb213bd2003-09-12 08:39:05 +00001036 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001037 if ((*exec)(buf) != 1) {
1038 return 0;
1039 }
1040 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041}
1042
Eric Andersen8320b422003-04-02 10:13:26 +00001043static int check(char *str) {
1044 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045}
1046
Eric Andersen8320b422003-04-02 10:13:26 +00001047static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001048{
Eric Andersen8320b422003-04-02 10:13:26 +00001049 if (!iface->method->up(iface,check)) return -1;
1050 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001051 if (!execute_all(iface, doit, "pre-up")) return 0;
1052 if (!iface->method->up(iface, doit)) return 0;
1053 if (!execute_all(iface, doit, "up")) return 0;
1054 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001055}
1056
1057static int iface_down(struct interface_defn_t *iface)
1058{
Eric Andersen8320b422003-04-02 10:13:26 +00001059 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001060 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001061 if (!execute_all(iface, doit, "down")) return 0;
1062 if (!iface->method->down(iface, doit)) return 0;
1063 if (!execute_all(iface, doit, "post-down")) return 0;
1064 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065}
1066
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001067#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068static int popen2(FILE **in, FILE **out, char *command, ...)
1069{
1070 va_list ap;
1071 char *argv[11] = { command };
1072 int argc;
1073 int infd[2], outfd[2];
1074 pid_t pid;
1075
1076 argc = 1;
1077 va_start(ap, command);
1078 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1079 argc++;
1080 }
1081 argv[argc] = NULL; /* make sure */
1082 va_end(ap);
1083
1084 if (pipe(infd) != 0) {
1085 return 0;
1086 }
1087
1088 if (pipe(outfd) != 0) {
1089 close(infd[0]);
1090 close(infd[1]);
1091 return 0;
1092 }
1093
1094 fflush(NULL);
1095 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001096 case -1: /* failure */
1097 close(infd[0]);
1098 close(infd[1]);
1099 close(outfd[0]);
1100 close(outfd[1]);
1101 return 0;
1102 case 0: /* child */
1103 dup2(infd[0], 0);
1104 dup2(outfd[1], 1);
1105 close(infd[0]);
1106 close(infd[1]);
1107 close(outfd[0]);
1108 close(outfd[1]);
1109 execvp(command, argv);
1110 exit(127);
1111 default: /* parent */
1112 *in = fdopen(infd[1], "w");
1113 *out = fdopen(outfd[0], "r");
1114 close(infd[0]);
1115 close(outfd[1]);
1116 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117 }
1118 /* unreached */
1119}
1120
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001121static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122{
1123 FILE *in, *out;
1124 int i, status;
1125 pid_t pid;
1126
Eric Andersen8a931792003-07-03 10:20:29 +00001127 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001128
Eric Andersen8a931792003-07-03 10:20:29 +00001129 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001130 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001131
1132 /* popen2() returns 0 on failure. */
1133 if (pid == 0)
1134 return logical;
1135
1136 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137 for (i = 0; i < map->n_mappings; i++) {
1138 fprintf(in, "%s\n", map->mapping[i]);
1139 }
1140 fclose(in);
1141 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001142
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001144 /* If the mapping script exited successfully, try to
1145 * grab a line of output and use that as the name of the
1146 * logical interface. */
1147 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148
Eric Andersen233b1702003-06-05 19:37:01 +00001149 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001150 /* If we are able to read a line of output from the script,
1151 * remove any trailing whitespace and use this value
1152 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001153 char *pch = new_logical + bb_strlen(new_logical) - 1;
1154
1155 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001157
1158 free(logical);
1159 logical = new_logical;
1160 } else {
1161 /* If we are UNABLE to read a line of output, discard are
1162 * freshly allocated memory. */
1163 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 }
1165 }
Eric Andersen8a931792003-07-03 10:20:29 +00001166
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001167 fclose(out);
1168
Eric Andersen8a931792003-07-03 10:20:29 +00001169 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170}
Eric Andersen8a931792003-07-03 10:20:29 +00001171#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001173static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001175 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001176 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001177
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178 while (search) {
1179 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001180 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001181 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001183 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001185 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001186}
1187
1188extern int ifupdown_main(int argc, char **argv)
1189{
Eric Andersen8320b422003-04-02 10:13:26 +00001190 int (*cmds) (struct interface_defn_t *) = NULL;
1191 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001193 llist_t *state_list = NULL;
1194 llist_t *target_list = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001195 char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001196 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001197
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001198#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001200#endif
1201 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001203 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204 int i;
1205
Manuel Novoa III cad53642003-03-19 09:13:01 +00001206 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207 /* ifup command */
1208 cmds = iface_up;
1209 } else {
1210 /* ifdown command */
1211 cmds = iface_down;
1212 }
1213
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001214#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001215 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001216#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001217 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001218#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001219 {
1220 switch (i) {
1221 case 'i': /* interfaces */
1222 interfaces = bb_xstrdup(optarg);
1223 break;
1224 case 'v': /* verbose */
1225 verbose = 1;
1226 break;
1227 case 'a': /* all */
1228 do_all = 1;
1229 break;
1230 case 'n': /* no-act */
1231 no_act = 1;
1232 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001233#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001234 case 'm': /* no-mappings */
1235 run_mappings = 0;
1236 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001237#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001238 case 'f': /* force */
1239 force = 1;
1240 break;
1241 default:
1242 bb_show_usage();
1243 break;
1244 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001245 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001246
1247 if (argc - optind > 0) {
1248 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001249 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 }
1251 } else {
1252 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001253 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001255 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001256
Eric Andersen8320b422003-04-02 10:13:26 +00001257 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001259 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260
Eric Andersen3c8bca32003-06-20 10:02:29 +00001261 if (!defn) {
1262 exit(EXIT_FAILURE);
1263 }
1264
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001265 if (no_act) {
1266 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001267 }
1268
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001269 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270 if (do_all) {
1271 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001272 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001273 } else {
1274#if 0
1275 /* iface_down */
1276 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001277 const llist_t *list = state_list;
1278 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001279 new_item = xmalloc(sizeof(llist_t));
1280 new_item->data = strdup(list->data);
1281 new_item->link = NULL;
1282 list = target_list;
1283 if (list == NULL)
1284 target_list = new_item;
1285 else {
1286 while (list->link) {
1287 list = list->link;
1288 }
1289 list = new_item;
1290 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001291 list = list->link;
1292 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001293 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001294#else
1295
1296 /* iface_down */
1297 const llist_t *list = state_list;
1298 while (list) {
1299 target_list = llist_add_to_end(target_list, strdup(list->data));
1300 list = list->link;
1301 }
1302 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001303#endif
1304 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001305 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001306 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001307 }
1308
1309
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001310 /* Update the interfaces */
1311 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001312 llist_t *iface_list;
1313 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001314 char *iface;
1315 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001316 char *pch;
1317 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001318 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001319
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001320 iface = strdup(target_list->data);
1321 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001322
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001323 pch = strchr(iface, '=');
1324 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001325 *pch = '\0';
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001326 liface = strdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001328 liface = strdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001329 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001330
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001332 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333
1334 if (cmds == iface_up) {
1335 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001336 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001337 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001338 continue;
1339 }
1340 } else {
1341 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001342 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001343 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344 continue;
1345 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001346 }
1347 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001348
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001349#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001350 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001351 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001352
1353 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1354
1355 for (i = 0; i < currmap->n_matches; i++) {
1356 if (fnmatch(currmap->match[i], liface, 0) != 0)
1357 continue;
1358 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001359 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001360 }
Eric Andersen8a931792003-07-03 10:20:29 +00001361 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001362 break;
1363 }
1364 }
1365 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001366#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001367
Eric Andersen8320b422003-04-02 10:13:26 +00001368
1369 iface_list = defn->ifaces;
1370 while (iface_list) {
1371 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001372 if (strcmp(liface, currif->iface) == 0) {
1373 char *oldiface = currif->iface;
1374
1375 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001376 currif->iface = iface;
1377
Eric Andersen8320b422003-04-02 10:13:26 +00001378 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001379
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001380 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001381 cmds_ret = cmds(currif);
1382 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001383 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001384 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001385 any_failures += 1;
1386 } else if (cmds_ret == 0) {
1387 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001388 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001389
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001390 currif->iface = oldiface;
1391 }
Eric Andersen8320b422003-04-02 10:13:26 +00001392 iface_list = iface_list->link;
1393 }
1394 if (verbose) {
1395 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001396 }
1397
1398 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001399 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001400 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001401 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001402 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001403
1404 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001405 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001406 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001407 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001408 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001409 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001410 free(iface_state->data);
1411 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001412 }
1413 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001414 /* Remove an interface from the linked list */
1415 if (iface_state) {
1416 /* This needs to be done better */
1417 free(iface_state->data);
1418 free(iface_state->link);
1419 if (iface_state->link) {
1420 iface_state->data = iface_state->link->data;
1421 iface_state->link = iface_state->link->link;
1422 } else {
1423 iface_state->data = NULL;
1424 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001425 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001426 }
1427 }
1428 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001429 }
1430
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001431 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001432 if (!no_act) {
1433
1434 if (state_fp)
1435 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001436 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001437
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001438 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001439 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001440 }
1441
1442 rewind(state_fp);
1443
1444 while (state_list) {
1445 if (state_list->data) {
1446 fputs(state_list->data, state_fp);
1447 fputc('\n', state_fp);
1448 }
1449 state_list = state_list->link;
1450 }
1451 fflush(state_fp);
1452 }
1453
1454 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001455 if (state_fp != NULL) {
1456 fclose(state_fp);
1457 state_fp = NULL;
1458 }
1459
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001460 if (any_failures)
1461 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001462 return 0;
1463}