blob: 9daa0f513789fa1eca2a8c6315d69d7fa4ac4817 [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")) {
567 execute("kill -9 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568 } else if (execable("/sbin/pump")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000569 result = execute("pump -i %iface% -k", ifd, exec);
570 } else if (execable("/sbin/dhclient")) {
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000571 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000572 } else if (execable("/sbin/dhcpcd")) {
Eric Andersen8320b422003-04-02 10:13:26 +0000573 result = execute("dhcpcd -k %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000574 }
Eric Andersen238e3542004-04-12 20:57:17 +0000575 return (result || bootp_down(ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576}
577
Eric Andersen8320b422003-04-02 10:13:26 +0000578static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000579{
Eric Andersen8320b422003-04-02 10:13:26 +0000580 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
581 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
582 "--returniffail --serverbcast", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000583}
584
Eric Andersen8320b422003-04-02 10:13:26 +0000585static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000586{
Eric Andersen8320b422003-04-02 10:13:26 +0000587 return( execute("pon [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000588}
589
Eric Andersen8320b422003-04-02 10:13:26 +0000590static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000591{
Eric Andersen8320b422003-04-02 10:13:26 +0000592 return( execute("poff [[%provider%]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000593}
594
Eric Andersen8320b422003-04-02 10:13:26 +0000595static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596{
Eric Andersen8320b422003-04-02 10:13:26 +0000597 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
598 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599}
600
Eric Andersen8320b422003-04-02 10:13:26 +0000601static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000602{
Eric Andersen8320b422003-04-02 10:13:26 +0000603 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
604 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000605}
606
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000607static struct method_t methods[] =
Eric Andersen8320b422003-04-02 10:13:26 +0000608{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000609 { "wvdial", wvdial_up, wvdial_down, },
610 { "ppp", ppp_up, ppp_down, },
611 { "static", static_up, static_down, },
612 { "bootp", bootp_up, bootp_down, },
613 { "dhcp", dhcp_up, dhcp_down, },
614 { "loopback", loopback_up, loopback_down, },
615};
616
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000617struct address_family_t addr_inet =
Eric Andersen8320b422003-04-02 10:13:26 +0000618{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000619 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000620 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621 methods
622};
623
624#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
625
Glenn L McGrath85737042003-01-14 23:26:57 +0000626static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627{
Glenn L McGrath85737042003-01-14 23:26:57 +0000628 unsigned short length;
629 char *word;
630
631 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000632 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000633 }
634
Glenn L McGrath85737042003-01-14 23:26:57 +0000635 /* Skip over leading whitespace */
Eric Andersen3c8bca32003-06-20 10:02:29 +0000636 word = *buf;
637 while (isspace(*word)) {
638 ++word;
639 }
640
641 /* Skip over comments */
642 if (*word == '#') {
643 return(NULL);
644 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000645
Glenn L McGrath85737042003-01-14 23:26:57 +0000646 /* Find the length of this word */
647 length = strcspn(word, " \t\n");
648 if (length == 0) {
649 return(NULL);
650 }
651 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000652 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
653 if (**buf) {
654 **buf = '\0';
655 (*buf)++;
656 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000657
658 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000659}
660
Eric Andersen8320b422003-04-02 10:13:26 +0000661static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662{
663 int i;
664
665 for (i = 0; af[i]; i++) {
666 if (strcmp(af[i]->name, name) == 0) {
667 return af[i];
668 }
669 }
670 return NULL;
671}
672
Eric Andersen8320b422003-04-02 10:13:26 +0000673static struct method_t *get_method(struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674{
675 int i;
676
677 for (i = 0; i < af->n_methods; i++) {
678 if (strcmp(af->method[i].name, name) == 0) {
679 return &af->method[i];
680 }
681 }
682 return(NULL);
683}
684
Eric Andersen8320b422003-04-02 10:13:26 +0000685static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000686{
687 if (strcmp(ifa->iface, ifb->iface) != 0) {
688 return(0);
689 }
690 if (ifa->address_family != ifb->address_family) {
691 return(0);
692 }
693 return(1);
694}
695
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000696static const llist_t *find_list_string(const llist_t *list, const char *string)
697{
698 while (list) {
699 if (strcmp(list->data, string) == 0) {
700 return(list);
701 }
702 list = list->link;
703 }
704 return(NULL);
705}
706
Eric Andersen8320b422003-04-02 10:13:26 +0000707static struct interfaces_file_t *read_interfaces(char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000708{
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000709#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000710 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000711#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000712 struct interface_defn_t *currif = NULL;
713 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000715 char *firstword;
716 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000717
718 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
719
Eric Andersen8320b422003-04-02 10:13:26 +0000720 defn = xmalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000721 defn->autointerfaces = NULL;
722 defn->mappings = NULL;
723 defn->ifaces = NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000724
Manuel Novoa III cad53642003-03-19 09:13:01 +0000725 f = bb_xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000726
Eric Andersen8320b422003-04-02 10:13:26 +0000727 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000728 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000729
Glenn L McGrath85737042003-01-14 23:26:57 +0000730 firstword = next_word(&buf_ptr);
731 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000732 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000733 continue; /* blank line */
734 }
735
736 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000737#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000738 currmap = xmalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000739 currmap->max_matches = 0;
740 currmap->n_matches = 0;
741 currmap->match = NULL;
742
Glenn L McGrath85737042003-01-14 23:26:57 +0000743 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000744 if (currmap->max_matches == currmap->n_matches) {
745 currmap->max_matches = currmap->max_matches * 2 + 1;
746 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
747 }
748
Manuel Novoa III cad53642003-03-19 09:13:01 +0000749 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000750 }
751 currmap->max_mappings = 0;
752 currmap->n_mappings = 0;
753 currmap->mapping = NULL;
754 currmap->script = NULL;
755 {
Eric Andersen8320b422003-04-02 10:13:26 +0000756 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000757 while (*where != NULL) {
758 where = &(*where)->next;
759 }
760 *where = currmap;
761 currmap->next = NULL;
762 }
Eric Andersen8320b422003-04-02 10:13:26 +0000763 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000764#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000765 currently_processing = MAPPING;
766 } else if (strcmp(firstword, "iface") == 0) {
767 {
Glenn L McGrath85737042003-01-14 23:26:57 +0000768 char *iface_name;
769 char *address_family_name;
770 char *method_name;
Eric Andersen8320b422003-04-02 10:13:26 +0000771 struct address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000772#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
773 &addr_inet,
774#endif
775#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
776 &addr_inet6,
777#endif
778#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
779 &addr_ipx,
780#endif
781 NULL
782 };
783
Eric Andersen8320b422003-04-02 10:13:26 +0000784 currif = xmalloc(sizeof(struct interface_defn_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000785 iface_name = next_word(&buf_ptr);
786 address_family_name = next_word(&buf_ptr);
787 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000788
Glenn L McGrath85737042003-01-14 23:26:57 +0000789 if (buf_ptr == NULL) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000790 bb_error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000791 return NULL;
792 }
793
Eric Andersen3c8bca32003-06-20 10:02:29 +0000794 /* ship any trailing whitespace */
795 while (isspace(*buf_ptr)) {
796 ++buf_ptr;
797 }
798
Glenn L McGrath85737042003-01-14 23:26:57 +0000799 if (buf_ptr[0] != '\0') {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000800 bb_error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000801 return NULL;
802 }
803
Manuel Novoa III cad53642003-03-19 09:13:01 +0000804 currif->iface = bb_xstrdup(iface_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000805
806 currif->address_family = get_address_family(addr_fams, address_family_name);
807 if (!currif->address_family) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000808 bb_error_msg("unknown address type \"%s\"", address_family_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809 return NULL;
810 }
811
812 currif->method = get_method(currif->address_family, method_name);
813 if (!currif->method) {
Eric Andersenfe9b9cd2004-06-29 00:48:30 +0000814 bb_error_msg("unknown method \"%s\"", method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 return NULL;
816 }
817
818 currif->automatic = 1;
819 currif->max_options = 0;
820 currif->n_options = 0;
821 currif->option = NULL;
822
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000823 {
Eric Andersen8320b422003-04-02 10:13:26 +0000824 struct interface_defn_t *tmp;
825 llist_t *iface_list;
826 iface_list = defn->ifaces;
827 while (iface_list) {
828 tmp = (struct interface_defn_t *) iface_list->data;
829 if (duplicate_if(tmp, currif)) {
830 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000831 return NULL;
832 }
Eric Andersen8320b422003-04-02 10:13:26 +0000833 iface_list = iface_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000834 }
835
Eric Andersen8320b422003-04-02 10:13:26 +0000836 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000837 }
Eric Andersen8320b422003-04-02 10:13:26 +0000838 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000839 }
840 currently_processing = IFACE;
841 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000842 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000843
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000844 /* Check the interface isnt already listed */
845 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000846 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000847 }
848
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000849 /* Add the interface to the list */
Eric Andersen8320b422003-04-02 10:13:26 +0000850 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
851 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000852 }
853 currently_processing = NONE;
854 } else {
855 switch (currently_processing) {
Eric Andersen8320b422003-04-02 10:13:26 +0000856 case IFACE:
857 {
858 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859
Eric Andersen8320b422003-04-02 10:13:26 +0000860 if (bb_strlen(buf_ptr) == 0) {
861 bb_error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000862 return NULL;
863 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864
Eric Andersen8320b422003-04-02 10:13:26 +0000865 if (strcmp(firstword, "up") != 0
866 && strcmp(firstword, "down") != 0
867 && strcmp(firstword, "pre-up") != 0
868 && strcmp(firstword, "post-down") != 0) {
869 for (i = 0; i < currif->n_options; i++) {
870 if (strcmp(currif->option[i].name, firstword) == 0) {
871 bb_error_msg("duplicate option \"%s\"", buf);
872 return NULL;
873 }
874 }
875 }
876 }
877 if (currif->n_options >= currif->max_options) {
878 struct variable_t *opt;
879
880 currif->max_options = currif->max_options + 10;
881 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
882 currif->option = opt;
883 }
884 currif->option[currif->n_options].name = bb_xstrdup(firstword);
885 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
886 if (!currif->option[currif->n_options].name) {
887 perror(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000888 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000889 }
890 if (!currif->option[currif->n_options].value) {
891 perror(filename);
892 return NULL;
893 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000894 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
Eric Andersen8320b422003-04-02 10:13:26 +0000895 currif->option[currif->n_options].value);
896 currif->n_options++;
897 break;
898 case MAPPING:
899#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
900 if (strcmp(firstword, "script") == 0) {
901 if (currmap->script != NULL) {
902 bb_error_msg("duplicate script in mapping \"%s\"", buf);
903 return NULL;
904 } else {
905 currmap->script = bb_xstrdup(next_word(&buf_ptr));
906 }
907 } else if (strcmp(firstword, "map") == 0) {
908 if (currmap->max_mappings == currmap->n_mappings) {
909 currmap->max_mappings = currmap->max_mappings * 2 + 1;
910 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
911 }
912 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
913 currmap->n_mappings++;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 } else {
Eric Andersen8320b422003-04-02 10:13:26 +0000915 bb_error_msg("misplaced option \"%s\"", buf);
916 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000917 }
Eric Andersen8320b422003-04-02 10:13:26 +0000918#endif
919 break;
920 case NONE:
921 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000922 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000923 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924 }
925 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000926 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927 }
928 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000929 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930 }
931 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000932
933 return defn;
934}
935
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936static char *setlocalenv(char *format, char *name, char *value)
937{
938 char *result;
939 char *here;
940 char *there;
941
Manuel Novoa III cad53642003-03-19 09:13:01 +0000942 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943
944 sprintf(result, format, name, value);
945
946 for (here = there = result; *there != '=' && *there; there++) {
947 if (*there == '-')
948 *there = '_';
949 if (isalpha(*there))
950 *there = toupper(*there);
951
952 if (isalnum(*there) || *there == '_') {
953 *here = *there;
954 here++;
955 }
956 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000957 memmove(here, there, bb_strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958
959 return result;
960}
961
Eric Andersen8320b422003-04-02 10:13:26 +0000962static void set_environ(struct interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000963{
964 char **environend;
965 int i;
966 const int n_env_entries = iface->n_options + 5;
967 char **ppch;
968
969 if (environ != NULL) {
970 for (ppch = environ; *ppch; ppch++) {
971 free(*ppch);
972 *ppch = NULL;
973 }
974 free(environ);
975 environ = NULL;
976 }
977 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
978 environend = environ;
979 *environend = NULL;
980
981 for (i = 0; i < iface->n_options; i++) {
982 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000983 || strcmp(iface->option[i].name, "down") == 0
984 || strcmp(iface->option[i].name, "pre-up") == 0
985 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000986 continue;
987 }
988 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
989 *environend = NULL;
990 }
991
992 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
993 *environend = NULL;
994 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
995 *environend = NULL;
996 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
997 *environend = NULL;
998 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
999 *environend = NULL;
1000 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
1001 *environend = NULL;
1002}
1003
1004static int doit(char *str)
1005{
1006 if (verbose || no_act) {
Eric Andersen8320b422003-04-02 10:13:26 +00001007 printf("%s\n", str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001008 }
1009 if (!no_act) {
1010 pid_t child;
1011 int status;
1012
1013 fflush(NULL);
1014 switch (child = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001015 case -1: /* failure */
1016 return 0;
1017 case 0: /* child */
Glenn L McGrathdc4e75e2003-09-02 02:36:18 +00001018 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ);
Eric Andersen8320b422003-04-02 10:13:26 +00001019 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020 }
1021 waitpid(child, &status, 0);
1022 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1023 return 0;
1024 }
1025 }
1026 return (1);
1027}
1028
Eric Andersen8320b422003-04-02 10:13:26 +00001029static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001030{
1031 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +00001032 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033 for (i = 0; i < ifd->n_options; i++) {
1034 if (strcmp(ifd->option[i].name, opt) == 0) {
1035 if (!(*exec) (ifd->option[i].value)) {
1036 return 0;
1037 }
1038 }
1039 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001040
Eric Anderseneb213bd2003-09-12 08:39:05 +00001041 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001042 if ((*exec)(buf) != 1) {
1043 return 0;
1044 }
1045 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001046}
1047
Eric Andersen8320b422003-04-02 10:13:26 +00001048static int check(char *str) {
1049 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050}
1051
Eric Andersen8320b422003-04-02 10:13:26 +00001052static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001053{
Eric Andersen8320b422003-04-02 10:13:26 +00001054 if (!iface->method->up(iface,check)) return -1;
1055 set_environ(iface, "start");
Eric Andersen658f8b12003-12-19 10:46:00 +00001056 if (!execute_all(iface, doit, "pre-up")) return 0;
1057 if (!iface->method->up(iface, doit)) return 0;
1058 if (!execute_all(iface, doit, "up")) return 0;
1059 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001060}
1061
1062static int iface_down(struct interface_defn_t *iface)
1063{
Eric Andersen8320b422003-04-02 10:13:26 +00001064 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065 set_environ(iface, "stop");
Eric Andersen658f8b12003-12-19 10:46:00 +00001066 if (!execute_all(iface, doit, "down")) return 0;
1067 if (!iface->method->down(iface, doit)) return 0;
1068 if (!execute_all(iface, doit, "post-down")) return 0;
1069 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070}
1071
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001072#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073static int popen2(FILE **in, FILE **out, char *command, ...)
1074{
1075 va_list ap;
1076 char *argv[11] = { command };
1077 int argc;
1078 int infd[2], outfd[2];
1079 pid_t pid;
1080
1081 argc = 1;
1082 va_start(ap, command);
1083 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1084 argc++;
1085 }
1086 argv[argc] = NULL; /* make sure */
1087 va_end(ap);
1088
1089 if (pipe(infd) != 0) {
1090 return 0;
1091 }
1092
1093 if (pipe(outfd) != 0) {
1094 close(infd[0]);
1095 close(infd[1]);
1096 return 0;
1097 }
1098
1099 fflush(NULL);
1100 switch (pid = fork()) {
Eric Andersen8320b422003-04-02 10:13:26 +00001101 case -1: /* failure */
1102 close(infd[0]);
1103 close(infd[1]);
1104 close(outfd[0]);
1105 close(outfd[1]);
1106 return 0;
1107 case 0: /* child */
1108 dup2(infd[0], 0);
1109 dup2(outfd[1], 1);
1110 close(infd[0]);
1111 close(infd[1]);
1112 close(outfd[0]);
1113 close(outfd[1]);
1114 execvp(command, argv);
1115 exit(127);
1116 default: /* parent */
1117 *in = fdopen(infd[1], "w");
1118 *out = fdopen(outfd[0], "r");
1119 close(infd[0]);
1120 close(outfd[1]);
1121 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122 }
1123 /* unreached */
1124}
1125
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001126static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127{
1128 FILE *in, *out;
1129 int i, status;
1130 pid_t pid;
1131
Eric Andersen8a931792003-07-03 10:20:29 +00001132 char *logical = bb_xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001133
Eric Andersen8a931792003-07-03 10:20:29 +00001134 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001135 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001136
1137 /* popen2() returns 0 on failure. */
1138 if (pid == 0)
1139 return logical;
1140
1141 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001142 for (i = 0; i < map->n_mappings; i++) {
1143 fprintf(in, "%s\n", map->mapping[i]);
1144 }
1145 fclose(in);
1146 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001147
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001149 /* If the mapping script exited successfully, try to
1150 * grab a line of output and use that as the name of the
1151 * logical interface. */
1152 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001153
Eric Andersen233b1702003-06-05 19:37:01 +00001154 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001155 /* If we are able to read a line of output from the script,
1156 * remove any trailing whitespace and use this value
1157 * as the name of the logical interface. */
Eric Andersen233b1702003-06-05 19:37:01 +00001158 char *pch = new_logical + bb_strlen(new_logical) - 1;
1159
1160 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001162
1163 free(logical);
1164 logical = new_logical;
1165 } else {
1166 /* If we are UNABLE to read a line of output, discard are
1167 * freshly allocated memory. */
1168 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001169 }
1170 }
Eric Andersen8a931792003-07-03 10:20:29 +00001171
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172 fclose(out);
1173
Eric Andersen8a931792003-07-03 10:20:29 +00001174 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175}
Eric Andersen8a931792003-07-03 10:20:29 +00001176#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001177
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179{
Manuel Novoa III cad53642003-03-19 09:13:01 +00001180 unsigned short iface_len = bb_strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001181 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001183 while (search) {
1184 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001185 (search->data[iface_len] == '=')) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001186 return(search);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001188 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001189 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001190 return(NULL);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001191}
1192
1193extern int ifupdown_main(int argc, char **argv)
1194{
Eric Andersen8320b422003-04-02 10:13:26 +00001195 int (*cmds) (struct interface_defn_t *) = NULL;
1196 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001197 FILE *state_fp = NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001198 llist_t *state_list = NULL;
1199 llist_t *target_list = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001200 char *interfaces = "/etc/network/interfaces";
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001201 const char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001203#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001205#endif
1206 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207 int force = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001208 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001209 int i;
1210
Manuel Novoa III cad53642003-03-19 09:13:01 +00001211 if (bb_applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001212 /* ifup command */
1213 cmds = iface_up;
1214 } else {
1215 /* ifdown command */
1216 cmds = iface_down;
1217 }
1218
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001219#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001220 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001221#else
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001222 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001223#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001224 {
1225 switch (i) {
1226 case 'i': /* interfaces */
1227 interfaces = bb_xstrdup(optarg);
1228 break;
1229 case 'v': /* verbose */
1230 verbose = 1;
1231 break;
1232 case 'a': /* all */
1233 do_all = 1;
1234 break;
1235 case 'n': /* no-act */
1236 no_act = 1;
1237 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001238#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +00001239 case 'm': /* no-mappings */
1240 run_mappings = 0;
1241 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001242#endif
Eric Andersen8320b422003-04-02 10:13:26 +00001243 case 'f': /* force */
1244 force = 1;
1245 break;
1246 default:
1247 bb_show_usage();
1248 break;
1249 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251
1252 if (argc - optind > 0) {
1253 if (do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001254 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001255 }
1256 } else {
1257 if (!do_all) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001258 bb_show_usage();
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001259 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001260 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001261
Eric Andersen8320b422003-04-02 10:13:26 +00001262 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001263 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001264 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265
Eric Andersen3c8bca32003-06-20 10:02:29 +00001266 if (!defn) {
1267 exit(EXIT_FAILURE);
1268 }
1269
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001270 if (no_act) {
1271 state_fp = fopen(statefile, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001272 }
1273
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001274 /* Create a list of interfaces to work on */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001275 if (do_all) {
1276 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001277 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001278 } else {
1279#if 0
1280 /* iface_down */
1281 llist_t *new_item;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001282 const llist_t *list = state_list;
1283 while (list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001284 new_item = xmalloc(sizeof(llist_t));
1285 new_item->data = strdup(list->data);
1286 new_item->link = NULL;
1287 list = target_list;
1288 if (list == NULL)
1289 target_list = new_item;
1290 else {
1291 while (list->link) {
1292 list = list->link;
1293 }
1294 list = new_item;
1295 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001296 list = list->link;
1297 }
Eric Andersen66a3af92003-01-27 17:41:19 +00001298 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001299#else
1300
1301 /* iface_down */
1302 const llist_t *list = state_list;
1303 while (list) {
1304 target_list = llist_add_to_end(target_list, strdup(list->data));
1305 list = list->link;
1306 }
1307 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001308#endif
1309 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001310 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001311 target_list = llist_add_to_end(target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001312 }
1313
1314
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001315 /* Update the interfaces */
1316 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001317 llist_t *iface_list;
1318 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001319 char *iface;
1320 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001321 char *pch;
1322 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001323 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001324
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001325 iface = strdup(target_list->data);
1326 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001327
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001328 pch = strchr(iface, '=');
1329 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001330 *pch = '\0';
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001331 liface = strdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001332 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001333 liface = strdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001334 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001335
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001336 if (!force) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001337 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001338
1339 if (cmds == iface_up) {
1340 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001341 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001342 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001343 continue;
1344 }
1345 } else {
1346 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001347 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001348 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001349 continue;
1350 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001351 }
1352 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001353
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001354#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001355 if ((cmds == iface_up) && run_mappings) {
Eric Andersen8320b422003-04-02 10:13:26 +00001356 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001357
1358 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1359
1360 for (i = 0; i < currmap->n_matches; i++) {
1361 if (fnmatch(currmap->match[i], liface, 0) != 0)
1362 continue;
1363 if (verbose) {
Eric Andersen8320b422003-04-02 10:13:26 +00001364 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001365 }
Eric Andersen8a931792003-07-03 10:20:29 +00001366 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001367 break;
1368 }
1369 }
1370 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001371#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001372
Eric Andersen8320b422003-04-02 10:13:26 +00001373
1374 iface_list = defn->ifaces;
1375 while (iface_list) {
1376 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001377 if (strcmp(liface, currif->iface) == 0) {
1378 char *oldiface = currif->iface;
1379
1380 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001381 currif->iface = iface;
1382
Eric Andersen8320b422003-04-02 10:13:26 +00001383 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001384
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001385 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001386 cmds_ret = cmds(currif);
1387 if (cmds_ret == -1) {
Glenn L McGrath469a1ea2004-07-21 12:21:39 +00001388 bb_error_msg("Don't seem to have all the variables for %s/%s.",
Eric Andersen8320b422003-04-02 10:13:26 +00001389 liface, currif->address_family->name);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001390 any_failures += 1;
1391 } else if (cmds_ret == 0) {
1392 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001393 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001394
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001395 currif->iface = oldiface;
1396 }
Eric Andersen8320b422003-04-02 10:13:26 +00001397 iface_list = iface_list->link;
1398 }
1399 if (verbose) {
1400 printf("\n");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001401 }
1402
1403 if (!okay && !force) {
Eric Andersen8320b422003-04-02 10:13:26 +00001404 bb_error_msg("Ignoring unknown interface %s", liface);
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001405 any_failures += 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001406 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001407 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001408
1409 if (cmds == iface_up) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001410 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001411 sprintf(newiface, "%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001412 if (iface_state == NULL) {
Eric Andersen8320b422003-04-02 10:13:26 +00001413 state_list = llist_add_to_end(state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001414 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001415 free(iface_state->data);
1416 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001417 }
1418 } else if (cmds == iface_down) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001419 /* Remove an interface from the linked list */
1420 if (iface_state) {
1421 /* This needs to be done better */
1422 free(iface_state->data);
1423 free(iface_state->link);
1424 if (iface_state->link) {
1425 iface_state->data = iface_state->link->data;
1426 iface_state->link = iface_state->link->link;
1427 } else {
1428 iface_state->data = NULL;
1429 iface_state->link = NULL;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001430 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001431 }
1432 }
1433 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001434 }
1435
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001436 /* Actually write the new state */
Eric Andersen66a3af92003-01-27 17:41:19 +00001437 if (!no_act) {
1438
1439 if (state_fp)
1440 fclose(state_fp);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001441 state_fp = bb_xfopen(statefile, "a+");
Eric Andersen66a3af92003-01-27 17:41:19 +00001442
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001443 if (ftruncate(fileno(state_fp), 0) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001444 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001445 }
1446
1447 rewind(state_fp);
1448
1449 while (state_list) {
1450 if (state_list->data) {
1451 fputs(state_list->data, state_fp);
1452 fputc('\n', state_fp);
1453 }
1454 state_list = state_list->link;
1455 }
1456 fflush(state_fp);
1457 }
1458
1459 /* Cleanup */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001460 if (state_fp != NULL) {
1461 fclose(state_fp);
1462 state_fp = NULL;
1463 }
1464
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001465 if (any_failures)
1466 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001467 return 0;
1468}