blob: 9169481751303e8112402d92dad0643da9225292 [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;
Eric Andersen42e3b002005-04-16 08:02:15 +0000153#ifndef __USE_GNU
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000154static char **environ = NULL;
Eric Andersen42e3b002005-04-16 08:02:15 +0000155#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000156
Eric Andersen66a3af92003-01-27 17:41:19 +0000157#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000158
159static unsigned int count_bits(unsigned int a)
160{
161 unsigned int result;
162 result = (a & 0x55) + ((a >> 1) & 0x55);
163 result = (result & 0x33) + ((result >> 2) & 0x33);
164 return((result & 0x0F) + ((result >> 4) & 0x0F));
165}
166
Eric Andersen66a3af92003-01-27 17:41:19 +0000167static int count_netmask_bits(char *dotted_quad)
168{
Eric Andersen8320b422003-04-02 10:13:26 +0000169 unsigned int result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000170 /* Found a netmask... Check if it is dotted quad */
171 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
172 return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000173 result = count_bits(a);
174 result += count_bits(b);
175 result += count_bits(c);
176 result += count_bits(d);
Eric Andersen66a3af92003-01-27 17:41:19 +0000177 return ((int)result);
178}
179#endif
180
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000181static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
182{
183 if (*pos + str_length >= *len) {
184 char *newbuf;
185
186 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
187 *buf = newbuf;
188 *len = *len * 2 + str_length + 1;
189 }
190
191 while (str_length-- >= 1) {
192 (*buf)[(*pos)++] = *str;
193 str++;
194 }
195 (*buf)[*pos] = '\0';
196}
197
198static int strncmpz(char *l, char *r, size_t llen)
199{
200 int i = strncmp(l, r, llen);
201
202 if (i == 0) {
203 return(-r[llen]);
204 } else {
205 return(i);
206 }
207}
208
Eric Andersen8320b422003-04-02 10:13:26 +0000209static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000210{
211 int i;
212
213 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000214 char *result;
215 static char label_buf[20];
216 strncpy(label_buf, ifd->iface, 19);
217 label_buf[19]=0;
218 result = strchr(label_buf, ':');
219 if (result) {
220 *result=0;
221 }
222 return( label_buf);
223 } else if (strncmpz(id, "label", idlen) == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000224 return (ifd->iface);
225 } else {
226 for (i = 0; i < ifd->n_options; i++) {
227 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
228 return (ifd->option[i].value);
229 }
230 }
231 }
232
233 return(NULL);
234}
235
Eric Andersen8320b422003-04-02 10:13:26 +0000236static char *parse(char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000237{
238
239 char *result = NULL;
240 size_t pos = 0, len = 0;
241 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
242 int okay[MAX_OPT_DEPTH] = { 1 };
243 int opt_depth = 1;
244
245 while (*command) {
246 switch (*command) {
247
Eric Andersen8320b422003-04-02 10:13:26 +0000248 default:
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000249 addstr(&result, &len, &pos, command, 1);
250 command++;
Eric Andersen8320b422003-04-02 10:13:26 +0000251 break;
252 case '\\':
253 if (command[1]) {
254 addstr(&result, &len, &pos, command + 1, 1);
255 command += 2;
256 } else {
257 addstr(&result, &len, &pos, command, 1);
258 command++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000259 }
Eric Andersen8320b422003-04-02 10:13:26 +0000260 break;
261 case '[':
262 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
263 old_pos[opt_depth] = pos;
264 okay[opt_depth] = 1;
265 opt_depth++;
266 command += 2;
267 } else {
268 addstr(&result, &len, &pos, "[", 1);
269 command++;
270 }
271 break;
272 case ']':
273 if (command[1] == ']' && opt_depth > 1) {
274 opt_depth--;
275 if (!okay[opt_depth]) {
276 pos = old_pos[opt_depth];
277 result[pos] = '\0';
Eric Andersen66a3af92003-01-27 17:41:19 +0000278 }
Eric Andersen8320b422003-04-02 10:13:26 +0000279 command += 2;
280 } else {
281 addstr(&result, &len, &pos, "]", 1);
282 command++;
Eric Andersen66a3af92003-01-27 17:41:19 +0000283 }
Eric Andersen8320b422003-04-02 10:13:26 +0000284 break;
285 case '%':
286 {
287 char *nextpercent;
288 char *varvalue;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000289
Eric Andersen8320b422003-04-02 10:13:26 +0000290 command++;
291 nextpercent = strchr(command, '%');
292 if (!nextpercent) {
293 errno = EUNBALPER;
294 free(result);
295 return (NULL);
296 }
297
298 varvalue = get_var(command, nextpercent - command, ifd);
299
300 if (varvalue) {
301 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
302 } else {
303#ifdef CONFIG_FEATURE_IFUPDOWN_IP
304 /* Sigh... Add a special case for 'ip' to convert from
305 * dotted quad to bit count style netmasks. */
306 if (strncmp(command, "bnmask", 6)==0) {
307 int res;
308 varvalue = get_var("netmask", 7, ifd);
309 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
310 char argument[255];
311 sprintf(argument, "%d", res);
312 addstr(&result, &len, &pos, argument, bb_strlen(argument));
313 command = nextpercent + 1;
314 break;
315 }
316 }
317#endif
318 okay[opt_depth - 1] = 0;
319 }
320
321 command = nextpercent + 1;
322 }
323 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000324 }
325 }
326
327 if (opt_depth > 1) {
328 errno = EUNBALBRACK;
329 free(result);
330 return(NULL);
331 }
332
333 if (!okay[0]) {
334 errno = EUNDEFVAR;
335 free(result);
336 return(NULL);
337 }
338
339 return(result);
340}
341
Eric Andersen8320b422003-04-02 10:13:26 +0000342static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000343{
344 char *out;
345 int ret;
346
347 out = parse(command, ifd);
348 if (!out) {
349 return(0);
350 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000351 ret = (*exec) (out);
352
353 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000354 if (ret != 1) {
355 return(0);
356 }
Eric Andersen8320b422003-04-02 10:13:26 +0000357 return(1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000358}
359
360#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Eric Andersen8320b422003-04-02 10:13:26 +0000361static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362{
Eric Andersen8320b422003-04-02 10:13:26 +0000363 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364}
365
Eric Andersen8320b422003-04-02 10:13:26 +0000366static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367{
Eric Andersen8320b422003-04-02 10:13:26 +0000368 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000369}
370
Eric Andersen8320b422003-04-02 10:13:26 +0000371static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372{
Eric Andersen8320b422003-04-02 10:13:26 +0000373 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000374}
375
Eric Andersen8320b422003-04-02 10:13:26 +0000376static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377{
Eric Andersen8320b422003-04-02 10:13:26 +0000378 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
380
Eric Andersen8320b422003-04-02 10:13:26 +0000381static struct method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382 { "dynamic", dynamic_up, dynamic_down, },
383 { "static", static_up_ipx, static_down_ipx, },
384};
385
Eric Andersen8320b422003-04-02 10:13:26 +0000386struct address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387 "ipx",
Eric Andersen8320b422003-04-02 10:13:26 +0000388 sizeof(methods_ipx) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000389 methods_ipx
390};
391#endif /* IFUP_FEATURE_IPX */
392
393#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000394static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000395{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000396#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000397 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000398 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000399 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000400 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000401#else
Eric Andersen8320b422003-04-02 10:13:26 +0000402 return( execute("ifconfig %iface% add ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000403#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000404}
405
Eric Andersen8320b422003-04-02 10:13:26 +0000406static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000407{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000408#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000409 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000410#else
Eric Andersen8320b422003-04-02 10:13:26 +0000411 return(execute("ifconfig %iface% del ::1", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000412#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000413}
414
Eric Andersen8320b422003-04-02 10:13:26 +0000415static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000416{
Eric Andersen8320b422003-04-02 10:13:26 +0000417 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000419 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
420 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000421 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000422#else
Eric Andersen8320b422003-04-02 10:13:26 +0000423 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
424 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
425 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000426#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000427 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000428}
429
Eric Andersen8320b422003-04-02 10:13:26 +0000430static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000432#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000433 return(execute("ip link set %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000434#else
Eric Andersen8320b422003-04-02 10:13:26 +0000435 return(execute("ifconfig %iface% down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000437}
438
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000439#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000440static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441{
Eric Andersen8320b422003-04-02 10:13:26 +0000442 int result;
443 result = execute("ip tunnel add %iface% mode sit remote "
444 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000445 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000446 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000447 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000448 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000449}
450
Eric Andersen8320b422003-04-02 10:13:26 +0000451static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452{
Eric Andersen8320b422003-04-02 10:13:26 +0000453 return( execute("ip tunnel del %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000454}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000455#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456
Eric Andersen8320b422003-04-02 10:13:26 +0000457static struct method_t methods6[] = {
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000458#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000459 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000460#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000461 { "static", static_up6, static_down6, },
462 { "loopback", loopback_up6, loopback_down6, },
463};
464
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000465static struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000466 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000467 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000468 methods6
469};
470#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
471
472#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000473static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000474{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000475#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000476 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000477 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000478 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000479 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000480#else
Eric Andersen8320b422003-04-02 10:13:26 +0000481 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000482#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000483}
484
Eric Andersen8320b422003-04-02 10:13:26 +0000485static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000486{
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000487#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000488 int result;
489 result = execute("ip addr flush dev %iface%", ifd, exec);
490 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000491 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000492#else
Eric Andersen8320b422003-04-02 10:13:26 +0000493 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000494#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000495}
496
Eric Andersen8320b422003-04-02 10:13:26 +0000497static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000498{
Eric Andersen8320b422003-04-02 10:13:26 +0000499 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000500#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8a931792003-07-03 10:20:29 +0000501 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000502 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
503 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000504 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000505 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000506#else
Eric Andersen8320b422003-04-02 10:13:26 +0000507 result = execute("ifconfig %iface% %address% netmask %netmask% "
508 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
509 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
510 ifd, exec);
511 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000512 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000513#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514}
515
Eric Andersen8320b422003-04-02 10:13:26 +0000516static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000517{
Eric Andersen8320b422003-04-02 10:13:26 +0000518 int result;
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000519#ifdef CONFIG_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000520 result = execute("ip addr flush dev %iface%", ifd, exec);
521 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000522#else
Eric Andersen8320b422003-04-02 10:13:26 +0000523 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
524 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000525#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000526 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527}
528
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000529static int execable(char *program)
530{
531 struct stat buf;
532 if (0 == stat(program, &buf)) {
533 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
534 return(1);
535 }
536 }
537 return(0);
538}
539
Eric Andersen8320b422003-04-02 10:13:26 +0000540static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000541{
Eric Andersen8320b422003-04-02 10:13:26 +0000542 if (execable("/sbin/udhcpc")) {
543 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
544 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000546 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
547 } else if (execable("/sbin/dhclient")) {
548 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000549 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000550 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
551 "[[-l %leasetime%]] %iface%", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000552 }
Eric Andersen8320b422003-04-02 10:13:26 +0000553 return(0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000554}
555
Eric Andersen8320b422003-04-02 10:13:26 +0000556static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000557{
Eric Andersen3c8bca32003-06-20 10:02:29 +0000558 int result = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000559 if (execable("/sbin/udhcpc")) {
Eric Andersen15b58852004-07-30 14:45:08 +0000560 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
561 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
562 * sequentially so we don't need to sleep */
Eric Andersen373bc1e2004-07-30 14:31:01 +0000563 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Eric Andersen15b58852004-07-30 14:45:08 +0000564 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000566 result = execute("pump -i %iface% -k", ifd, exec);
567 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000568 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000569 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000570 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000571 }
Eric Andersen373bc1e2004-07-30 14:31:01 +0000572 return (result || static_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000573}
574
Eric Andersen8320b422003-04-02 10:13:26 +0000575static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576{
Eric Andersen8320b422003-04-02 10:13:26 +0000577 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
578 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
579 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580}
581
Eric Andersen8320b422003-04-02 10:13:26 +0000582static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000583{
Eric Andersen8320b422003-04-02 10:13:26 +0000584 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000585}
586
Eric Andersen8320b422003-04-02 10:13:26 +0000587static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000588{
Eric Andersen8320b422003-04-02 10:13:26 +0000589 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000590}
591
Eric Andersen8320b422003-04-02 10:13:26 +0000592static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000593{
Eric Andersen8320b422003-04-02 10:13:26 +0000594 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
595 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596}
597
Eric Andersen8320b422003-04-02 10:13:26 +0000598static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599{
Eric Andersen8320b422003-04-02 10:13:26 +0000600 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
601 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000602}
603
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000604static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000605{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000606 { "wvdial", wvdial_up, wvdial_down, },
607 { "ppp", ppp_up, ppp_down, },
608 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000609 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000610 { "dhcp", dhcp_up, dhcp_down, },
611 { "loopback", loopback_up, loopback_down, },
612};
613
Eric Andersen14f5c8d2005-04-16 19:39:00 +0000614static struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000615{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000616 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000617 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000618 methods
619};
620
621#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
622
Glenn L McGrath85737042003-01-14 23:26:57 +0000623static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000624{
Glenn L McGrath85737042003-01-14 23:26:57 +0000625 unsigned short length;
626 char *word;
627
628 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000629 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000630 }
631
Glenn L McGrath85737042003-01-14 23:26:57 +0000632 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000633 word = *buf;
634 while (isspace(*word)) {
635 ++word;
636 }
637
638 /* Skip over comments */
639 if (*word == '#') {
640 return(NULL);
641 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000642
Glenn L McGrath85737042003-01-14 23:26:57 +0000643 /* Find the length of this word */
644 length = strcspn(word, " \t\n");
645 if (length == 0) {
646 return(NULL);
647 }
648 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000649 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
650 if (**buf) {
651 **buf = '\0';
652 (*buf)++;
653 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000654
655 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000656}
657
Eric Andersen8320b422003-04-02 10:13:26 +0000658static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000659{
660 int i;
661
662 for (i = 0; af[i]; i++) {
663 if (strcmp(af[i]->name, name) == 0) {
664 return af[i];
665 }
666 }
667 return NULL;
668}
669
Eric Andersen8320b422003-04-02 10:13:26 +0000670static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000671{
672 int i;
673
674 for (i = 0; i < af->n_methods; i++) {
675 if (strcmp(af->method[i].name, name) == 0) {
676 return &af->method[i];
677 }
678 }
679 return(NULL);
680}
681
Eric Andersen8320b422003-04-02 10:13:26 +0000682static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000683{
684 if (strcmp(ifa->iface, ifb->iface) != 0) {
685 return(0);
686 }
687 if (ifa->address_family != ifb->address_family) {
688 return(0);
689 }
690 return(1);
691}
692
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000693static const llist_t *find_list_string(const llist_t *list, const char *string)
694{
695 while (list) {
696 if (strcmp(list->data, string) == 0) {
697 return(list);
698 }
699 list = list->link;
700 }
701 return(NULL);
702}
703
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000704static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000705{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000706#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000707 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000708#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000709 struct interface_defn_t *currif = NULL;
710 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000711 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000712 char *firstword;
713 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714
715 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
716
Eric Andersen8320b422003-04-02 10:13:26 +0000717 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000718 defn->autointerfaces = NULL;
719 defn->mappings = NULL;
720 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000721
Manuel Novoa III cad53642003-03-19 09:13:01 +0000722 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000723
Eric Andersen8320b422003-04-02 10:13:26 +0000724 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000725 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000726
Glenn L McGrath85737042003-01-14 23:26:57 +0000727 firstword = next_word(&buf_ptr);
728 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000729 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000730 continue; /* blank line */
731 }
732
733 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000734#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000735 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000736 currmap->max_matches = 0;
737 currmap->n_matches = 0;
738 currmap->match = NULL;
739
Glenn L McGrath85737042003-01-14 23:26:57 +0000740 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000741 if (currmap->max_matches == currmap->n_matches) {
742 currmap->max_matches = currmap->max_matches * 2 + 1;
743 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
744 }
745
Manuel Novoa III cad53642003-03-19 09:13:01 +0000746 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000747 }
748 currmap->max_mappings = 0;
749 currmap->n_mappings = 0;
750 currmap->mapping = NULL;
751 currmap->script = NULL;
752 {
Eric Andersen8320b422003-04-02 10:13:26 +0000753 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000754 while (*where != NULL) {
755 where = &(*where)->next;
756 }
757 *where = currmap;
758 currmap->next = NULL;
759 }
Eric Andersen8320b422003-04-02 10:13:26 +0000760 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000761#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000762 currently_processing = MAPPING;
763 } else if (strcmp(firstword, "iface") == 0) {
764 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000765 char *iface_name;
766 char *address_family_name;
767 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000768 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000769#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
770 &addr_inet,
771#endif
772#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
773 &addr_inet6,
774#endif
775#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
776 &addr_ipx,
777#endif
778 NULL
779 };
780
Eric Andersen8320b422003-04-02 10:13:26 +0000781 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000782 iface_name = next_word(&buf_ptr);
783 address_family_name = next_word(&buf_ptr);
784 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000785
Glenn L McGrath85737042003-01-14 23:26:57 +0000786 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000787 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000788 return NULL;
789 }
790
Eric Andersen3c8bca32003-06-20 10:02:29 +0000791 /* ship any trailing whitespace */
792 while (isspace(*buf_ptr)) {
793 ++buf_ptr;
794 }
795
Glenn L McGrath85737042003-01-14 23:26:57 +0000796 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000797 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000798 return NULL;
799 }
800
Manuel Novoa III cad53642003-03-19 09:13:01 +0000801 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802
803 currif->address_family = get_address_family(addr_fams, address_family_name);
804 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000805 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000806 return NULL;
807 }
808
809 currif->method = get_method(currif->address_family, method_name);
810 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000811 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000812 return NULL;
813 }
814
815 currif->automatic = 1;
816 currif->max_options = 0;
817 currif->n_options = 0;
818 currif->option = NULL;
819
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000820 {
Eric Andersen8320b422003-04-02 10:13:26 +0000821 struct interface_defn_t *tmp;
822 llist_t *iface_list;
823 iface_list = defn->ifaces;
824 while (iface_list) {
825 tmp = (struct interface_defn_t *) iface_list->data;
826 if (duplicate_if(tmp, currif)) {
827 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000828 return NULL;
829 }
Eric Andersen8320b422003-04-02 10:13:26 +0000830 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831 }
832
Eric Andersen8320b422003-04-02 10:13:26 +0000833 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000834 }
Eric Andersen8320b422003-04-02 10:13:26 +0000835 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836 }
837 currently_processing = IFACE;
838 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000839 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000841 /* Check the interface isnt already listed */
842 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000843 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000844 }
845
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000846 /* Add the interface to the list */
Eric Andersen8320b422003-04-02 10:13:26 +0000847 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
848 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000849 }
850 currently_processing = NONE;
851 } else {
852 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000853 case IFACE:
854 {
855 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000856
Eric Andersen8320b422003-04-02 10:13:26 +0000857 if (bb_strlen(buf_ptr) == 0) {
858 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859 return NULL;
860 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000861
Eric Andersen8320b422003-04-02 10:13:26 +0000862 if (strcmp(firstword, "up") != 0
863 && strcmp(firstword, "down") != 0
864 && strcmp(firstword, "pre-up") != 0
865 && strcmp(firstword, "post-down") != 0) {
866 for (i = 0; i < currif->n_options; i++) {
867 if (strcmp(currif->option[i].name, firstword) == 0) {
868 bb_error_msg("duplicate option \"%s\"", buf);
869 return NULL;
870 }
871 }
872 }
873 }
874 if (currif->n_options >= currif->max_options) {
875 struct variable_t *opt;
876
877 currif->max_options = currif->max_options + 10;
878 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
879 currif->option = opt;
880 }
881 currif->option[currif->n_options].name = bb_xstrdup(firstword);
882 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
883 if (!currif->option[currif->n_options].name) {
884 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000886 }
887 if (!currif->option[currif->n_options].value) {
888 perror(filename);
889 return NULL;
890 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000891 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000892 currif->option[currif->n_options].value);
893 currif->n_options++;
894 break;
895 case MAPPING:
896#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
897 if (strcmp(firstword, "script") == 0) {
898 if (currmap->script != NULL) {
899 bb_error_msg("duplicate script in mapping \"%s\"", buf);
900 return NULL;
901 } else {
902 currmap->script = bb_xstrdup(next_word(&buf_ptr));
903 }
904 } else if (strcmp(firstword, "map") == 0) {
905 if (currmap->max_mappings == currmap->n_mappings) {
906 currmap->max_mappings = currmap->max_mappings * 2 + 1;
907 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
908 }
909 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
910 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000911 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000912 bb_error_msg("misplaced option \"%s\"", buf);
913 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 }
Eric Andersen8320b422003-04-02 10:13:26 +0000915#endif
916 break;
917 case NONE:
918 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000919 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000920 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000921 }
922 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000923 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924 }
925 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000926 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927 }
928 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000929
930 return defn;
931}
932
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000933static char *setlocalenv(char *format, char *name, char *value)
934{
935 char *result;
936 char *here;
937 char *there;
938
Manuel Novoa III cad53642003-03-19 09:13:01 +0000939 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000940
941 sprintf(result, format, name, value);
942
943 for (here = there = result; *there != '=' && *there; there++) {
944 if (*there == '-')
945 *there = '_';
946 if (isalpha(*there))
947 *there = toupper(*there);
948
949 if (isalnum(*there) || *there == '_') {
950 *here = *there;
951 here++;
952 }
953 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000954 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000955
956 return result;
957}
958
Eric Andersen8320b422003-04-02 10:13:26 +0000959static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000960{
961 char **environend;
962 int i;
963 const int n_env_entries = iface->n_options + 5;
964 char **ppch;
965
966 if (environ != NULL) {
967 for (ppch = environ; *ppch; ppch++) {
968 free(*ppch);
969 *ppch = NULL;
970 }
971 free(environ);
972 environ = NULL;
973 }
974 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
975 environend = environ;
976 *environend = NULL;
977
978 for (i = 0; i < iface->n_options; i++) {
979 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000980 || strcmp(iface->option[i].name, "down") == 0
981 || strcmp(iface->option[i].name, "pre-up") == 0
982 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000983 continue;
984 }
985 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
986 *environend = NULL;
987 }
988
989 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
990 *environend = NULL;
991 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
992 *environend = NULL;
993 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
994 *environend = NULL;
995 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
996 *environend = NULL;
997 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
998 *environend = NULL;
999}
1000
1001static int doit(char *str)
1002{
1003 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +00001004 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005 }
1006 if (!no_act) {
1007 pid_t child;
1008 int status;
1009
1010 fflush(NULL);
1011 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001012 case -1: /* failure */
1013 return 0;
1014 case 0: /* child */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001015 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ);
Eric Andersen8320b422003-04-02 10:13:26 +00001016 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001017 }
1018 waitpid(child, &status, 0);
1019 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1020 return 0;
1021 }
1022 }
1023 return (1);
1024}
1025
Eric Andersen8320b422003-04-02 10:13:26 +00001026static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001027{
1028 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001029 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001030 for (i = 0; i < ifd->n_options; i++) {
1031 if (strcmp(ifd->option[i].name, opt) == 0) {
1032 if (!(*exec) (ifd->option[i].value)) {
1033 return 0;
1034 }
1035 }
1036 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001037
Eric Anderseneb213bd2003-09-12 08:39:05 +00001038 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001039 if ((*exec)(buf) != 1) {
1040 return 0;
1041 }
1042 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001043}
1044
Eric Andersen8320b422003-04-02 10:13:26 +00001045static int check(char *str) {
1046 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047}
1048
Eric Andersen8320b422003-04-02 10:13:26 +00001049static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050{
Eric Andersen8320b422003-04-02 10:13:26 +00001051 if (!iface->method->up(iface,check)) return -1;
1052 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001053 if (!execute_all(iface, doit, "pre-up")) return 0;
1054 if (!iface->method->up(iface, doit)) return 0;
1055 if (!execute_all(iface, doit, "up")) return 0;
1056 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001057}
1058
1059static int iface_down(struct interface_defn_t *iface)
1060{
Eric Andersen8320b422003-04-02 10:13:26 +00001061 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001063 if (!execute_all(iface, doit, "down")) return 0;
1064 if (!iface->method->down(iface, doit)) return 0;
1065 if (!execute_all(iface, doit, "post-down")) return 0;
1066 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067}
1068
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001069#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070static int popen2(FILE **in, FILE **out, char *command, ...)
1071{
1072 va_list ap;
1073 char *argv[11] = { command };
1074 int argc;
1075 int infd[2], outfd[2];
1076 pid_t pid;
1077
1078 argc = 1;
1079 va_start(ap, command);
1080 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1081 argc++;
1082 }
1083 argv[argc] = NULL; /* make sure */
1084 va_end(ap);
1085
1086 if (pipe(infd) != 0) {
1087 return 0;
1088 }
1089
1090 if (pipe(outfd) != 0) {
1091 close(infd[0]);
1092 close(infd[1]);
1093 return 0;
1094 }
1095
1096 fflush(NULL);
1097 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001098 case -1: /* failure */
1099 close(infd[0]);
1100 close(infd[1]);
1101 close(outfd[0]);
1102 close(outfd[1]);
1103 return 0;
1104 case 0: /* child */
1105 dup2(infd[0], 0);
1106 dup2(outfd[1], 1);
1107 close(infd[0]);
1108 close(infd[1]);
1109 close(outfd[0]);
1110 close(outfd[1]);
1111 execvp(command, argv);
1112 exit(127);
1113 default: /* parent */
1114 *in = fdopen(infd[1], "w");
1115 *out = fdopen(outfd[0], "r");
1116 close(infd[0]);
1117 close(outfd[1]);
1118 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119 }
1120 /* unreached */
1121}
1122
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001123static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124{
1125 FILE *in, *out;
1126 int i, status;
1127 pid_t pid;
1128
Eric Andersen8a931792003-07-03 10:20:29 +00001129 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001130
Eric Andersen8a931792003-07-03 10:20:29 +00001131 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001133
1134 /* popen2() returns 0 on failure. */
1135 if (pid == 0)
1136 return logical;
1137
1138 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001139 for (i = 0; i < map->n_mappings; i++) {
1140 fprintf(in, "%s\n", map->mapping[i]);
1141 }
1142 fclose(in);
1143 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001144
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001146 /* If the mapping script exited successfully, try to
1147 * grab a line of output and use that as the name of the
1148 * logical interface. */
1149 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001150
Eric Andersen233b1702003-06-05 19:37:01 +00001151 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001152 /* If we are able to read a line of output from the script,
1153 * remove any trailing whitespace and use this value
1154 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001155 char *pch = new_logical + bb_strlen(new_logical) - 1;
1156
1157 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001159
1160 free(logical);
1161 logical = new_logical;
1162 } else {
1163 /* If we are UNABLE to read a line of output, discard are
1164 * freshly allocated memory. */
1165 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166 }
1167 }
Eric Andersen8a931792003-07-03 10:20:29 +00001168
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001169 fclose(out);
1170
Eric Andersen8a931792003-07-03 10:20:29 +00001171 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172}
Eric Andersen8a931792003-07-03 10:20:29 +00001173#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001175static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001177 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001180 while (search) {
1181 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001182 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001183 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001185 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001186 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001187 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188}
1189
1190extern int ifupdown_main(int argc, char **argv)
1191{
Eric Andersen8320b422003-04-02 10:13:26 +00001192 int (*cmds) (struct interface_defn_t *) = NULL;
1193 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001195 llist_t *state_list = NULL;
1196 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001197 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001198 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001200#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001202#endif
1203 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001205 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001206 int i;
1207
Manuel Novoa III cad53642003-03-19 09:13:01 +00001208 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209 /* ifup command */
1210 cmds = iface_up;
1211 } else {
1212 /* ifdown command */
1213 cmds = iface_down;
1214 }
1215
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001216#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001217 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001218#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001219 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001220#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001221 {
1222 switch (i) {
1223 case 'i': /* interfaces */
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001224 interfaces = optarg;
Eric Andersen8320b422003-04-02 10:13:26 +00001225 break;
1226 case 'v': /* verbose */
1227 verbose = 1;
1228 break;
1229 case 'a': /* all */
1230 do_all = 1;
1231 break;
1232 case 'n': /* no-act */
1233 no_act = 1;
1234 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001235#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001236 case 'm': /* no-mappings */
1237 run_mappings = 0;
1238 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001239#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001240 case 'f': /* force */
1241 force = 1;
1242 break;
1243 default:
1244 bb_show_usage();
1245 break;
1246 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001248
1249 if (argc - optind > 0) {
1250 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001251 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252 }
1253 } else {
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 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001257 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258
Eric Andersen8320b422003-04-02 10:13:26 +00001259 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001261 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262
Eric Andersen3c8bca32003-06-20 10:02:29 +00001263 if (!defn) {
1264 exit(EXIT_FAILURE);
1265 }
1266
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001267 if (no_act) {
1268 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001269 }
1270
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001271 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 if (do_all) {
1273 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001274 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001275 } else {
1276#if 0
1277 /* iface_down */
1278 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001279 const llist_t *list = state_list;
1280 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001281 new_item = xmalloc(sizeof(llist_t));
1282 new_item->data = strdup(list->data);
1283 new_item->link = NULL;
1284 list = target_list;
1285 if (list == NULL)
1286 target_list = new_item;
1287 else {
1288 while (list->link) {
1289 list = list->link;
1290 }
1291 list = new_item;
1292 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001293 list = list->link;
1294 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001295 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001296#else
1297
1298 /* iface_down */
1299 const llist_t *list = state_list;
1300 while (list) {
1301 target_list = llist_add_to_end(target_list, strdup(list->data));
1302 list = list->link;
1303 }
1304 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001305#endif
1306 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001307 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001308 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001309 }
1310
1311
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001312 /* Update the interfaces */
1313 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001314 llist_t *iface_list;
1315 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001316 char *iface;
1317 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001318 char *pch;
1319 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001320 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001321
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001322 iface = strdup(target_list->data);
1323 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001324
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001325 pch = strchr(iface, '=');
1326 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327 *pch = '\0';
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001328 liface = strdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001329 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001330 liface = strdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001331 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001332
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001333 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001334 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001335
1336 if (cmds == iface_up) {
1337 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001338 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001339 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001340 continue;
1341 }
1342 } else {
1343 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001344 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001345 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001346 continue;
1347 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001348 }
1349 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001350
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001351#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001352 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001353 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001354
1355 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1356
1357 for (i = 0; i < currmap->n_matches; i++) {
1358 if (fnmatch(currmap->match[i], liface, 0) != 0)
1359 continue;
1360 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001361 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001362 }
Eric Andersen8a931792003-07-03 10:20:29 +00001363 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001364 break;
1365 }
1366 }
1367 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001368#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001369
Eric Andersen8320b422003-04-02 10:13:26 +00001370
1371 iface_list = defn->ifaces;
1372 while (iface_list) {
1373 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001374 if (strcmp(liface, currif->iface) == 0) {
1375 char *oldiface = currif->iface;
1376
1377 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001378 currif->iface = iface;
1379
Eric Andersen8320b422003-04-02 10:13:26 +00001380 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001381
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001382 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001383 cmds_ret = cmds(currif);
1384 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001385 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001386 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001387 any_failures += 1;
1388 } else if (cmds_ret == 0) {
1389 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001390 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001391
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001392 currif->iface = oldiface;
1393 }
Eric Andersen8320b422003-04-02 10:13:26 +00001394 iface_list = iface_list->link;
1395 }
1396 if (verbose) {
1397 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001398 }
1399
1400 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001401 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001402 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001403 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001404 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001405
1406 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001407 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001408 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001409 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001410 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001411 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001412 free(iface_state->data);
1413 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001414 }
1415 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001416 /* Remove an interface from the linked list */
1417 if (iface_state) {
1418 /* This needs to be done better */
1419 free(iface_state->data);
1420 free(iface_state->link);
1421 if (iface_state->link) {
1422 iface_state->data = iface_state->link->data;
1423 iface_state->link = iface_state->link->link;
1424 } else {
1425 iface_state->data = NULL;
1426 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001427 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001428 }
1429 }
1430 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001431 }
1432
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001433 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001434 if (!no_act) {
1435
1436 if (state_fp)
1437 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001438 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001439
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001440 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001441 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001442 }
1443
1444 rewind(state_fp);
1445
1446 while (state_list) {
1447 if (state_list->data) {
1448 fputs(state_list->data, state_fp);
1449 fputc('\n', state_fp);
1450 }
1451 state_list = state_list->link;
1452 }
1453 fflush(state_fp);
1454 }
1455
1456 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001457 if (state_fp != NULL) {
1458 fclose(state_fp);
1459 state_fp = NULL;
1460 }
1461
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001462 if (any_failures)
1463 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001464 return 0;
1465}