blob: 276ca5f22426b17c5f99545754532d0fc51d8ff2 [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 Andersen5e136f22004-07-20 06:35:54 +0000554static int bootp_down(struct interface_defn_t *ifd, execfn *exec)
555{
556#ifdef CONFIG_FEATURE_IFUPDOWN_IP
557 return(execute("ip link set %iface% down", ifd, exec));
558#else
559 return(execute("ifconfig %iface% down", ifd, exec));
560#endif
561}
562
Eric Andersen8320b422003-04-02 10:13:26 +0000563static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000564{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000565 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000566 if (execable("/sbin/udhcpc")) {
Glenn L McGrath4d405bb2004-07-23 01:10:22 +0000567 execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersenac594252004-07-26 12:05:44 +0000568 execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000569 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000570 result = execute("pump -i %iface% -k", ifd, exec);
571 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000572 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000573 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000574 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000575 }
Eric Andersen238e3542004-04-12 20:57:17 +0000576 return (result || bootp_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577}
578
Eric Andersen8320b422003-04-02 10:13:26 +0000579static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580{
Eric Andersen8320b422003-04-02 10:13:26 +0000581 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
582 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
583 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000584}
585
Eric Andersen8320b422003-04-02 10:13:26 +0000586static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000587{
Eric Andersen8320b422003-04-02 10:13:26 +0000588 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000589}
590
Eric Andersen8320b422003-04-02 10:13:26 +0000591static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000592{
Eric Andersen8320b422003-04-02 10:13:26 +0000593 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594}
595
Eric Andersen8320b422003-04-02 10:13:26 +0000596static int wvdial_up(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 --start -x /usr/bin/wvdial "
599 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000600}
601
Eric Andersen8320b422003-04-02 10:13:26 +0000602static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000603{
Eric Andersen8320b422003-04-02 10:13:26 +0000604 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
605 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000606}
607
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000608static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000609{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000610 { "wvdial", wvdial_up, wvdial_down, },
611 { "ppp", ppp_up, ppp_down, },
612 { "static", static_up, static_down, },
613 { "bootp", bootp_up, bootp_down, },
614 { "dhcp", dhcp_up, dhcp_down, },
615 { "loopback", loopback_up, loopback_down, },
616};
617
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000618struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000619{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000620 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000621 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000622 methods
623};
624
625#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
626
Glenn L McGrath85737042003-01-14 23:26:57 +0000627static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000628{
Glenn L McGrath85737042003-01-14 23:26:57 +0000629 unsigned short length;
630 char *word;
631
632 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000634 }
635
Glenn L McGrath85737042003-01-14 23:26:57 +0000636 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000637 word = *buf;
638 while (isspace(*word)) {
639 ++word;
640 }
641
642 /* Skip over comments */
643 if (*word == '#') {
644 return(NULL);
645 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646
Glenn L McGrath85737042003-01-14 23:26:57 +0000647 /* Find the length of this word */
648 length = strcspn(word, " \t\n");
649 if (length == 0) {
650 return(NULL);
651 }
652 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000653 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
654 if (**buf) {
655 **buf = '\0';
656 (*buf)++;
657 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000658
659 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660}
661
Eric Andersen8320b422003-04-02 10:13:26 +0000662static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000663{
664 int i;
665
666 for (i = 0; af[i]; i++) {
667 if (strcmp(af[i]->name, name) == 0) {
668 return af[i];
669 }
670 }
671 return NULL;
672}
673
Eric Andersen8320b422003-04-02 10:13:26 +0000674static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000675{
676 int i;
677
678 for (i = 0; i < af->n_methods; i++) {
679 if (strcmp(af->method[i].name, name) == 0) {
680 return &af->method[i];
681 }
682 }
683 return(NULL);
684}
685
Eric Andersen8320b422003-04-02 10:13:26 +0000686static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687{
688 if (strcmp(ifa->iface, ifb->iface) != 0) {
689 return(0);
690 }
691 if (ifa->address_family != ifb->address_family) {
692 return(0);
693 }
694 return(1);
695}
696
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000697static const llist_t *find_list_string(const llist_t *list, const char *string)
698{
699 while (list) {
700 if (strcmp(list->data, string) == 0) {
701 return(list);
702 }
703 list = list->link;
704 }
705 return(NULL);
706}
707
Eric Andersen8320b422003-04-02 10:13:26 +0000708static struct interfaces_file_t *read_interfaces(char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000710#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000711 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000712#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000713 struct interface_defn_t *currif = NULL;
714 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000715 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000716 char *firstword;
717 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718
719 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
720
Eric Andersen8320b422003-04-02 10:13:26 +0000721 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722 defn->autointerfaces = NULL;
723 defn->mappings = NULL;
724 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000725
Manuel Novoa III cad53642003-03-19 09:13:01 +0000726 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000727
Eric Andersen8320b422003-04-02 10:13:26 +0000728 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000729 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000730
Glenn L McGrath85737042003-01-14 23:26:57 +0000731 firstword = next_word(&buf_ptr);
732 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000733 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734 continue; /* blank line */
735 }
736
737 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000738#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000739 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000740 currmap->max_matches = 0;
741 currmap->n_matches = 0;
742 currmap->match = NULL;
743
Glenn L McGrath85737042003-01-14 23:26:57 +0000744 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745 if (currmap->max_matches == currmap->n_matches) {
746 currmap->max_matches = currmap->max_matches * 2 + 1;
747 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
748 }
749
Manuel Novoa III cad53642003-03-19 09:13:01 +0000750 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751 }
752 currmap->max_mappings = 0;
753 currmap->n_mappings = 0;
754 currmap->mapping = NULL;
755 currmap->script = NULL;
756 {
Eric Andersen8320b422003-04-02 10:13:26 +0000757 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000758 while (*where != NULL) {
759 where = &(*where)->next;
760 }
761 *where = currmap;
762 currmap->next = NULL;
763 }
Eric Andersen8320b422003-04-02 10:13:26 +0000764 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000765#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000766 currently_processing = MAPPING;
767 } else if (strcmp(firstword, "iface") == 0) {
768 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000769 char *iface_name;
770 char *address_family_name;
771 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000772 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000773#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
774 &addr_inet,
775#endif
776#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
777 &addr_inet6,
778#endif
779#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
780 &addr_ipx,
781#endif
782 NULL
783 };
784
Eric Andersen8320b422003-04-02 10:13:26 +0000785 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000786 iface_name = next_word(&buf_ptr);
787 address_family_name = next_word(&buf_ptr);
788 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000789
Glenn L McGrath85737042003-01-14 23:26:57 +0000790 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000791 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000792 return NULL;
793 }
794
Eric Andersen3c8bca32003-06-20 10:02:29 +0000795 /* ship any trailing whitespace */
796 while (isspace(*buf_ptr)) {
797 ++buf_ptr;
798 }
799
Glenn L McGrath85737042003-01-14 23:26:57 +0000800 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000801 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802 return NULL;
803 }
804
Manuel Novoa III cad53642003-03-19 09:13:01 +0000805 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806
807 currif->address_family = get_address_family(addr_fams, address_family_name);
808 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000809 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000810 return NULL;
811 }
812
813 currif->method = get_method(currif->address_family, method_name);
814 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000815 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000816 return NULL;
817 }
818
819 currif->automatic = 1;
820 currif->max_options = 0;
821 currif->n_options = 0;
822 currif->option = NULL;
823
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 {
Eric Andersen8320b422003-04-02 10:13:26 +0000825 struct interface_defn_t *tmp;
826 llist_t *iface_list;
827 iface_list = defn->ifaces;
828 while (iface_list) {
829 tmp = (struct interface_defn_t *) iface_list->data;
830 if (duplicate_if(tmp, currif)) {
831 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000832 return NULL;
833 }
Eric Andersen8320b422003-04-02 10:13:26 +0000834 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835 }
836
Eric Andersen8320b422003-04-02 10:13:26 +0000837 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838 }
Eric Andersen8320b422003-04-02 10:13:26 +0000839 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840 }
841 currently_processing = IFACE;
842 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000843 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000844
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000845 /* Check the interface isnt already listed */
846 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000847 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000848 }
849
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000850 /* Add the interface to the list */
Eric Andersen8320b422003-04-02 10:13:26 +0000851 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
852 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000853 }
854 currently_processing = NONE;
855 } else {
856 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000857 case IFACE:
858 {
859 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000860
Eric Andersen8320b422003-04-02 10:13:26 +0000861 if (bb_strlen(buf_ptr) == 0) {
862 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000863 return NULL;
864 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000865
Eric Andersen8320b422003-04-02 10:13:26 +0000866 if (strcmp(firstword, "up") != 0
867 && strcmp(firstword, "down") != 0
868 && strcmp(firstword, "pre-up") != 0
869 && strcmp(firstword, "post-down") != 0) {
870 for (i = 0; i < currif->n_options; i++) {
871 if (strcmp(currif->option[i].name, firstword) == 0) {
872 bb_error_msg("duplicate option \"%s\"", buf);
873 return NULL;
874 }
875 }
876 }
877 }
878 if (currif->n_options >= currif->max_options) {
879 struct variable_t *opt;
880
881 currif->max_options = currif->max_options + 10;
882 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
883 currif->option = opt;
884 }
885 currif->option[currif->n_options].name = bb_xstrdup(firstword);
886 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
887 if (!currif->option[currif->n_options].name) {
888 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000889 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000890 }
891 if (!currif->option[currif->n_options].value) {
892 perror(filename);
893 return NULL;
894 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000895 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000896 currif->option[currif->n_options].value);
897 currif->n_options++;
898 break;
899 case MAPPING:
900#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
901 if (strcmp(firstword, "script") == 0) {
902 if (currmap->script != NULL) {
903 bb_error_msg("duplicate script in mapping \"%s\"", buf);
904 return NULL;
905 } else {
906 currmap->script = bb_xstrdup(next_word(&buf_ptr));
907 }
908 } else if (strcmp(firstword, "map") == 0) {
909 if (currmap->max_mappings == currmap->n_mappings) {
910 currmap->max_mappings = currmap->max_mappings * 2 + 1;
911 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
912 }
913 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
914 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000915 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000916 bb_error_msg("misplaced option \"%s\"", buf);
917 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918 }
Eric Andersen8320b422003-04-02 10:13:26 +0000919#endif
920 break;
921 case NONE:
922 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000923 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000925 }
926 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000927 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000928 }
929 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000930 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000931 }
932 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000933
934 return defn;
935}
936
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000937static char *setlocalenv(char *format, char *name, char *value)
938{
939 char *result;
940 char *here;
941 char *there;
942
Manuel Novoa III cad53642003-03-19 09:13:01 +0000943 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000944
945 sprintf(result, format, name, value);
946
947 for (here = there = result; *there != '=' && *there; there++) {
948 if (*there == '-')
949 *there = '_';
950 if (isalpha(*there))
951 *there = toupper(*there);
952
953 if (isalnum(*there) || *there == '_') {
954 *here = *there;
955 here++;
956 }
957 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000958 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959
960 return result;
961}
962
Eric Andersen8320b422003-04-02 10:13:26 +0000963static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964{
965 char **environend;
966 int i;
967 const int n_env_entries = iface->n_options + 5;
968 char **ppch;
969
970 if (environ != NULL) {
971 for (ppch = environ; *ppch; ppch++) {
972 free(*ppch);
973 *ppch = NULL;
974 }
975 free(environ);
976 environ = NULL;
977 }
978 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
979 environend = environ;
980 *environend = NULL;
981
982 for (i = 0; i < iface->n_options; i++) {
983 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000984 || strcmp(iface->option[i].name, "down") == 0
985 || strcmp(iface->option[i].name, "pre-up") == 0
986 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987 continue;
988 }
989 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
990 *environend = NULL;
991 }
992
993 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
994 *environend = NULL;
995 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
996 *environend = NULL;
997 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
998 *environend = NULL;
999 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
1000 *environend = NULL;
1001 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
1002 *environend = NULL;
1003}
1004
1005static int doit(char *str)
1006{
1007 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +00001008 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009 }
1010 if (!no_act) {
1011 pid_t child;
1012 int status;
1013
1014 fflush(NULL);
1015 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001016 case -1: /* failure */
1017 return 0;
1018 case 0: /* child */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001019 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ);
Eric Andersen8320b422003-04-02 10:13:26 +00001020 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021 }
1022 waitpid(child, &status, 0);
1023 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1024 return 0;
1025 }
1026 }
1027 return (1);
1028}
1029
Eric Andersen8320b422003-04-02 10:13:26 +00001030static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031{
1032 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001033 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001034 for (i = 0; i < ifd->n_options; i++) {
1035 if (strcmp(ifd->option[i].name, opt) == 0) {
1036 if (!(*exec) (ifd->option[i].value)) {
1037 return 0;
1038 }
1039 }
1040 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001041
Eric Anderseneb213bd2003-09-12 08:39:05 +00001042 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001043 if ((*exec)(buf) != 1) {
1044 return 0;
1045 }
1046 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047}
1048
Eric Andersen8320b422003-04-02 10:13:26 +00001049static int check(char *str) {
1050 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001051}
1052
Eric Andersen8320b422003-04-02 10:13:26 +00001053static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001054{
Eric Andersen8320b422003-04-02 10:13:26 +00001055 if (!iface->method->up(iface,check)) return -1;
1056 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001057 if (!execute_all(iface, doit, "pre-up")) return 0;
1058 if (!iface->method->up(iface, doit)) return 0;
1059 if (!execute_all(iface, doit, "up")) return 0;
1060 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001061}
1062
1063static int iface_down(struct interface_defn_t *iface)
1064{
Eric Andersen8320b422003-04-02 10:13:26 +00001065 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001066 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001067 if (!execute_all(iface, doit, "down")) return 0;
1068 if (!iface->method->down(iface, doit)) return 0;
1069 if (!execute_all(iface, doit, "post-down")) return 0;
1070 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001071}
1072
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001073#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001074static int popen2(FILE **in, FILE **out, char *command, ...)
1075{
1076 va_list ap;
1077 char *argv[11] = { command };
1078 int argc;
1079 int infd[2], outfd[2];
1080 pid_t pid;
1081
1082 argc = 1;
1083 va_start(ap, command);
1084 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1085 argc++;
1086 }
1087 argv[argc] = NULL; /* make sure */
1088 va_end(ap);
1089
1090 if (pipe(infd) != 0) {
1091 return 0;
1092 }
1093
1094 if (pipe(outfd) != 0) {
1095 close(infd[0]);
1096 close(infd[1]);
1097 return 0;
1098 }
1099
1100 fflush(NULL);
1101 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001102 case -1: /* failure */
1103 close(infd[0]);
1104 close(infd[1]);
1105 close(outfd[0]);
1106 close(outfd[1]);
1107 return 0;
1108 case 0: /* child */
1109 dup2(infd[0], 0);
1110 dup2(outfd[1], 1);
1111 close(infd[0]);
1112 close(infd[1]);
1113 close(outfd[0]);
1114 close(outfd[1]);
1115 execvp(command, argv);
1116 exit(127);
1117 default: /* parent */
1118 *in = fdopen(infd[1], "w");
1119 *out = fdopen(outfd[0], "r");
1120 close(infd[0]);
1121 close(outfd[1]);
1122 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001123 }
1124 /* unreached */
1125}
1126
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001127static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001128{
1129 FILE *in, *out;
1130 int i, status;
1131 pid_t pid;
1132
Eric Andersen8a931792003-07-03 10:20:29 +00001133 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134
Eric Andersen8a931792003-07-03 10:20:29 +00001135 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001137
1138 /* popen2() returns 0 on failure. */
1139 if (pid == 0)
1140 return logical;
1141
1142 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001143 for (i = 0; i < map->n_mappings; i++) {
1144 fprintf(in, "%s\n", map->mapping[i]);
1145 }
1146 fclose(in);
1147 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001148
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001150 /* If the mapping script exited successfully, try to
1151 * grab a line of output and use that as the name of the
1152 * logical interface. */
1153 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154
Eric Andersen233b1702003-06-05 19:37:01 +00001155 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001156 /* If we are able to read a line of output from the script,
1157 * remove any trailing whitespace and use this value
1158 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001159 char *pch = new_logical + bb_strlen(new_logical) - 1;
1160
1161 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001163
1164 free(logical);
1165 logical = new_logical;
1166 } else {
1167 /* If we are UNABLE to read a line of output, discard are
1168 * freshly allocated memory. */
1169 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 }
1171 }
Eric Andersen8a931792003-07-03 10:20:29 +00001172
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001173 fclose(out);
1174
Eric Andersen8a931792003-07-03 10:20:29 +00001175 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176}
Eric Andersen8a931792003-07-03 10:20:29 +00001177#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001178
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001179static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001180{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001181 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001182 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001183
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001184 while (search) {
1185 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001186 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001187 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001189 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001190 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001191 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192}
1193
1194extern int ifupdown_main(int argc, char **argv)
1195{
Eric Andersen8320b422003-04-02 10:13:26 +00001196 int (*cmds) (struct interface_defn_t *) = NULL;
1197 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001198 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001199 llist_t *state_list = NULL;
1200 llist_t *target_list = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201 char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001202 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001204#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001206#endif
1207 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001208 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001209 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210 int i;
1211
Manuel Novoa III cad53642003-03-19 09:13:01 +00001212 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001213 /* ifup command */
1214 cmds = iface_up;
1215 } else {
1216 /* ifdown command */
1217 cmds = iface_down;
1218 }
1219
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001220#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001221 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001222#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001223 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001224#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001225 {
1226 switch (i) {
1227 case 'i': /* interfaces */
1228 interfaces = bb_xstrdup(optarg);
1229 break;
1230 case 'v': /* verbose */
1231 verbose = 1;
1232 break;
1233 case 'a': /* all */
1234 do_all = 1;
1235 break;
1236 case 'n': /* no-act */
1237 no_act = 1;
1238 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001239#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001240 case 'm': /* no-mappings */
1241 run_mappings = 0;
1242 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001243#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001244 case 'f': /* force */
1245 force = 1;
1246 break;
1247 default:
1248 bb_show_usage();
1249 break;
1250 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252
1253 if (argc - optind > 0) {
1254 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001255 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001256 }
1257 } else {
1258 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001259 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001261 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262
Eric Andersen8320b422003-04-02 10:13:26 +00001263 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001264 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001265 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266
Eric Andersen3c8bca32003-06-20 10:02:29 +00001267 if (!defn) {
1268 exit(EXIT_FAILURE);
1269 }
1270
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001271 if (no_act) {
1272 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273 }
1274
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276 if (do_all) {
1277 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001278 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001279 } else {
1280#if 0
1281 /* iface_down */
1282 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001283 const llist_t *list = state_list;
1284 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001285 new_item = xmalloc(sizeof(llist_t));
1286 new_item->data = strdup(list->data);
1287 new_item->link = NULL;
1288 list = target_list;
1289 if (list == NULL)
1290 target_list = new_item;
1291 else {
1292 while (list->link) {
1293 list = list->link;
1294 }
1295 list = new_item;
1296 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001297 list = list->link;
1298 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001299 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001300#else
1301
1302 /* iface_down */
1303 const llist_t *list = state_list;
1304 while (list) {
1305 target_list = llist_add_to_end(target_list, strdup(list->data));
1306 list = list->link;
1307 }
1308 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001309#endif
1310 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001311 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001312 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001313 }
1314
1315
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316 /* Update the interfaces */
1317 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001318 llist_t *iface_list;
1319 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001320 char *iface;
1321 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001322 char *pch;
1323 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001324 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001325
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001326 iface = strdup(target_list->data);
1327 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001328
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001329 pch = strchr(iface, '=');
1330 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331 *pch = '\0';
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001332 liface = strdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001334 liface = strdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001335 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001336
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001337 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001338 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001339
1340 if (cmds == iface_up) {
1341 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001342 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001343 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001344 continue;
1345 }
1346 } else {
1347 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001348 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001349 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001350 continue;
1351 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001352 }
1353 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001354
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001355#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001356 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001357 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001358
1359 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1360
1361 for (i = 0; i < currmap->n_matches; i++) {
1362 if (fnmatch(currmap->match[i], liface, 0) != 0)
1363 continue;
1364 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001365 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001366 }
Eric Andersen8a931792003-07-03 10:20:29 +00001367 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001368 break;
1369 }
1370 }
1371 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001372#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001373
Eric Andersen8320b422003-04-02 10:13:26 +00001374
1375 iface_list = defn->ifaces;
1376 while (iface_list) {
1377 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001378 if (strcmp(liface, currif->iface) == 0) {
1379 char *oldiface = currif->iface;
1380
1381 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001382 currif->iface = iface;
1383
Eric Andersen8320b422003-04-02 10:13:26 +00001384 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001385
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001386 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001387 cmds_ret = cmds(currif);
1388 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001389 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001390 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001391 any_failures += 1;
1392 } else if (cmds_ret == 0) {
1393 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001394 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001395
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001396 currif->iface = oldiface;
1397 }
Eric Andersen8320b422003-04-02 10:13:26 +00001398 iface_list = iface_list->link;
1399 }
1400 if (verbose) {
1401 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001402 }
1403
1404 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001405 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001406 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001407 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001408 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001409
1410 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001411 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001412 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001413 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001414 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001415 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001416 free(iface_state->data);
1417 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001418 }
1419 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001420 /* Remove an interface from the linked list */
1421 if (iface_state) {
1422 /* This needs to be done better */
1423 free(iface_state->data);
1424 free(iface_state->link);
1425 if (iface_state->link) {
1426 iface_state->data = iface_state->link->data;
1427 iface_state->link = iface_state->link->link;
1428 } else {
1429 iface_state->data = NULL;
1430 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001431 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001432 }
1433 }
1434 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001435 }
1436
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001437 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001438 if (!no_act) {
1439
1440 if (state_fp)
1441 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001442 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001443
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001444 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001445 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001446 }
1447
1448 rewind(state_fp);
1449
1450 while (state_list) {
1451 if (state_list->data) {
1452 fputs(state_list->data, state_fp);
1453 fputc('\n', state_fp);
1454 }
1455 state_list = state_list->link;
1456 }
1457 fflush(state_fp);
1458 }
1459
1460 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001461 if (state_fp != NULL) {
1462 fclose(state_fp);
1463 state_fp = NULL;
1464 }
1465
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001466 if (any_failures)
1467 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001468 return 0;
1469}