blob: 3e18df19efe49d9b28fa2c9e21a1053d2391ee51 [file] [log] [blame]
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001/*
2 * ifupdown for busybox
3 * Based on ifupdown by Anthony Towns
4 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
5 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00006 * Changes to upstream version
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00007 * Remove checks for kernel version, assume kernel version 2.2.0 or better
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00008 * Lines in the interfaces file cannot wrap.
9 * The default state file is moved to /var/run/ifstate
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26#include <sys/stat.h>
27#include <sys/utsname.h>
28#include <sys/wait.h>
29
30#include <ctype.h>
31#include <errno.h>
32#include <fcntl.h>
33#include <fnmatch.h>
34#include <getopt.h>
35#include <stdarg.h>
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <unistd.h>
40
Glenn L McGrath9af8a722002-11-11 07:03:02 +000041#include "libbb.h"
42//#include "busybox.h"
43//#include "config.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000044
45#define IFUPDOWN_VERSION "0.6.4"
46
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000047typedef struct interface_defn_s interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048
49typedef int (execfn)(char *command);
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000050typedef int (command_set)(interface_defn_t *ifd, execfn *e);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000051
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000052typedef struct method_s {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000053 char *name;
54 command_set *up;
55 command_set *down;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000056} method_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000057
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000058typedef struct address_family_s {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000059 char *name;
60 int n_methods;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000061 method_t *method;
62} address_family_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000063
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000064typedef struct mapping_defn_s {
65 struct mapping_defn_s *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
67 int max_matches;
68 int n_matches;
69 char **match;
70
71 char *script;
72
73 int max_mappings;
74 int n_mappings;
75 char **mapping;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000076} mapping_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000078typedef struct variable_s {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000079 char *name;
80 char *value;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000081} variable_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000082
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000083struct interface_defn_s {
84 struct interface_defn_s *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000085
86 char *iface;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000087 address_family_t *address_family;
88 method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000089
90 int automatic;
91
92 int max_options;
93 int n_options;
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000094 variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000095};
96
Glenn L McGrath0325a1c2002-12-07 07:45:42 +000097typedef struct interfaces_file_s {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000098 int max_autointerfaces;
99 int n_autointerfaces;
100 char **autointerfaces;
101
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000102 interface_defn_t *ifaces;
103 mapping_defn_t *mappings;
104} interfaces_file_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000105
106#define MAX_OPT_DEPTH 10
107#define EUNBALBRACK 10001
108#define EUNDEFVAR 10002
109#define MAX_VARNAME 32
110#define EUNBALPER 10000
111
112static int no_act = 0;
113static int verbose = 0;
114static char **environ = NULL;
115
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000116static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
117{
118 if (*pos + str_length >= *len) {
119 char *newbuf;
120
121 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
122 *buf = newbuf;
123 *len = *len * 2 + str_length + 1;
124 }
125
126 while (str_length-- >= 1) {
127 (*buf)[(*pos)++] = *str;
128 str++;
129 }
130 (*buf)[*pos] = '\0';
131}
132
133static int strncmpz(char *l, char *r, size_t llen)
134{
135 int i = strncmp(l, r, llen);
136
137 if (i == 0) {
138 return(-r[llen]);
139 } else {
140 return(i);
141 }
142}
143
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000144static char *get_var(char *id, size_t idlen, interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000145{
146 int i;
147
148 if (strncmpz(id, "iface", idlen) == 0) {
149 return (ifd->iface);
150 } else {
151 for (i = 0; i < ifd->n_options; i++) {
152 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
153 return (ifd->option[i].value);
154 }
155 }
156 }
157
158 return(NULL);
159}
160
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000161static char *parse(char *command, interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000162{
163
164 char *result = NULL;
165 size_t pos = 0, len = 0;
166 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
167 int okay[MAX_OPT_DEPTH] = { 1 };
168 int opt_depth = 1;
169
170 while (*command) {
171 switch (*command) {
172
173 default:
174 addstr(&result, &len, &pos, command, 1);
175 command++;
176 break;
177 case '\\':
178 if (command[1]) {
179 addstr(&result, &len, &pos, command + 1, 1);
180 command += 2;
181 } else {
182 addstr(&result, &len, &pos, command, 1);
183 command++;
184 }
185 break;
186 case '[':
187 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
188 old_pos[opt_depth] = pos;
189 okay[opt_depth] = 1;
190 opt_depth++;
191 command += 2;
192 } else {
193 addstr(&result, &len, &pos, "[", 1);
194 command++;
195 }
196 break;
197 case ']':
198 if (command[1] == ']' && opt_depth > 1) {
199 opt_depth--;
200 if (!okay[opt_depth]) {
201 pos = old_pos[opt_depth];
202 result[pos] = '\0';
203 }
204 command += 2;
205 } else {
206 addstr(&result, &len, &pos, "]", 1);
207 command++;
208 }
209 break;
210 case '%':
211 {
212 char *nextpercent;
213 char *varvalue;
214
215 command++;
216 nextpercent = strchr(command, '%');
217 if (!nextpercent) {
218 errno = EUNBALPER;
219 free(result);
220 return (NULL);
221 }
222
223 varvalue = get_var(command, nextpercent - command, ifd);
224
225 if (varvalue) {
226 addstr(&result, &len, &pos, varvalue, xstrlen(varvalue));
227 } else {
228 okay[opt_depth - 1] = 0;
229 }
230
231 command = nextpercent + 1;
232 }
233 break;
234 }
235 }
236
237 if (opt_depth > 1) {
238 errno = EUNBALBRACK;
239 free(result);
240 return(NULL);
241 }
242
243 if (!okay[0]) {
244 errno = EUNDEFVAR;
245 free(result);
246 return(NULL);
247 }
248
249 return(result);
250}
251
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000252static int execute(char *command, interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000253{
254 char *out;
255 int ret;
256
257 out = parse(command, ifd);
258 if (!out) {
259 return(0);
260 }
261
262 ret = (*exec) (out);
263
264 free(out);
265 return(ret);
266}
267
268#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000269static int static_up_ipx(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000270{
271 if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) {
272 return(0);
273 }
274 return(1);
275}
276
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000277static int static_down_ipx(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000278{
279 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
280 return(0);
281 }
282 return(1);
283}
284
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000285static int dynamic_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000286{
287 if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) {
288 return(0);
289 }
290 return(1);
291}
292
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000293static int dynamic_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294{
295 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
296 return(0);
297 }
298 return(1);
299}
300
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000301static method_t methods_ipx[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000302 { "dynamic", dynamic_up, dynamic_down, },
303 { "static", static_up_ipx, static_down_ipx, },
304};
305
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000306address_family_t addr_ipx = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000307 "ipx",
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000308 sizeof(methods_ipx) / sizeof(method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000309 methods_ipx
310};
311#endif /* IFUP_FEATURE_IPX */
312
313#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000314static int loopback_up6(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315{
316 if (!execute("ifconfig %iface% add ::1", ifd, exec)) {
317 return(0);
318 }
319 return(1);
320}
321
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000322static int loopback_down6(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000323{
Glenn L McGrath2801eb92002-11-26 02:23:41 +0000324 if (!execute("ifconfig %iface% del ::1", ifd, exec)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000325 return(0);
326 }
327 return(1);
328}
329
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000330static int static_up6(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331{
332 if (!execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec)) {
333 return(0);
334 }
335 if (!execute("ifconfig %iface% add %address%/%netmask%", ifd, exec)) {
336 return(0);
337 }
338 if (!execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec)) {
339 return(0);
340 }
341 return(1);
342}
343
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000344static int static_down6(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000345{
346 if (!execute("ifconfig %iface% down", ifd, exec)) {
347 return(0);
348 }
349 return(1);
350}
351
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000352static int v4tunnel_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000353{
354 if (!execute("ip tunnel add %iface% mode sit remote %endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec)) {
355 return(0);
356 }
357 if (!execute("ip link set %iface% up", ifd, exec)) {
358 return(0);
359 }
360 if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) {
361 return(0);
362 }
363 if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) {
364 return(0);
365 }
366 return(1);
367}
368
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000369static int v4tunnel_down(interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370{
371 if (!execute("ip tunnel del %iface%", ifd, exec)) {
372 return(0);
373 }
374 return(1);
375}
376
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000377static method_t methods6[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
379 { "static", static_up6, static_down6, },
380 { "loopback", loopback_up6, loopback_down6, },
381};
382
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000383address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384 "inet6",
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000385 sizeof(methods6) / sizeof(method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000386 methods6
387};
388#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
389
390#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000391static int loopback_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392{
393 if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) {
394 return(0);
395 }
396 return(1);
397}
398
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000399static int loopback_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400{
Glenn L McGrath1d658262002-12-07 00:48:54 +0000401 if (!execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000402 return(0);
403 }
404 return(1);
405}
406
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000407static int static_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000408{
409 if (!execute("ifconfig %iface% %address% netmask %netmask% [[broadcast %broadcast%]] [[pointopoint %pointopoint%]] [[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
410 ifd, exec)) {
411 return(0);
412 }
413 if (!execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec)) {
414 return(0);
415 }
416 return(1);
417}
418
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000419static int static_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000420{
421 if (!execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec)) {
422 return(0);
423 }
424 if (!execute("ifconfig %iface% down", ifd, exec)) {
425 return(0);
426 }
427 return(1);
428}
429
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000430static int execable(char *program)
431{
432 struct stat buf;
433 if (0 == stat(program, &buf)) {
434 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
435 return(1);
436 }
437 }
438 return(0);
439}
440
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000441static int dhcp_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000442{
443 if (execable("/sbin/dhclient")) {
444 if (!execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)) {
445 return(0);
446 }
447 } else if (execable("/sbin/pump")) {
448 if (!execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)) {
449 return(0);
450 }
451 } else if (execable("/sbin/udhcpc")) {
452 if (!execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)) {
453 return 0;
454 }
455 } else if (execable("/sbin/dhcpcd")) {
456 if (!execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%", ifd, exec)) {
457 return(0);
458 }
459 }
460 return(1);
461}
462
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000463static int dhcp_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000464{
465 if (execable("/sbin/dhclient")) {
Glenn L McGrathd1431072002-11-19 09:58:56 +0000466 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000467 return(0);
468 }
469 } else if (execable("/sbin/pump")) {
470 if (!execute("pump -i %iface% -k", ifd, exec)) {
471 return(0);
472 }
473 } else if (execable("/sbin/udhcpc")) {
Glenn L McGrathd1431072002-11-19 09:58:56 +0000474 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000475 return(0);
476 }
477 } else if (execable("/sbin/dhcpcd")) {
478 if (!execute("dhcpcd -k %iface%", ifd, exec)) {
479 return(0);
480 }
481 }
482 if (!execute("ifconfig %iface% down", ifd, exec)) {
483 return(0);
484 }
485 return(1);
486}
487
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000488static int bootp_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000489{
490 if (!execute("bootpc [[--bootfile %bootfile%]] --dev %iface% [[--server %server%]] [[--hwaddr %hwaddr%]] --returniffail --serverbcast", ifd, exec)) {
491 return 0;
492 }
493 return 1;
494}
495
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000496static int bootp_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000497{
498 if (!execute("ifconfig down %iface%", ifd, exec)) {
499 return 0;
500 }
501 return 1;
502}
503
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000504static int ppp_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000505{
506 if (!execute("pon [[%provider%]]", ifd, exec)) {
507 return 0;
508 }
509 return 1;
510}
511
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000512static int ppp_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513{
514 if (!execute("poff [[%provider%]]", ifd, exec)) {
515 return 0;
516 }
517 return 1;
518}
519
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000520static int wvdial_up(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000521{
522 if (!execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)) {
523 return 0;
524 }
525 return 1;
526}
527
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000528static int wvdial_down(interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529{
530 if (!execute ("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -s 2", ifd, exec)) {
531 return 0;
532 }
533 return 1;
534}
535
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000536static method_t methods[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000537 { "wvdial", wvdial_up, wvdial_down, },
538 { "ppp", ppp_up, ppp_down, },
539 { "static", static_up, static_down, },
540 { "bootp", bootp_up, bootp_down, },
541 { "dhcp", dhcp_up, dhcp_down, },
542 { "loopback", loopback_up, loopback_down, },
543};
544
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000545address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546 "inet",
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000547 sizeof(methods) / sizeof(method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548 methods
549};
550
551#endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
552
553static char *next_word(char *buf, char *word, int maxlen)
554{
555 if (!buf)
556 return NULL;
557 if (!*buf)
558 return NULL;
559
560 while (!isspace(*buf) && *buf) {
561 if (maxlen-- > 1)
562 *word++ = *buf;
563 buf++;
564 }
565 if (maxlen > 0) {
566 *word = '\0';
567 }
568
569 while (isspace(*buf) && *buf) {
570 buf++;
571 }
572
573 return buf;
574}
575
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000576static address_family_t *get_address_family(address_family_t *af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577{
578 int i;
579
580 for (i = 0; af[i]; i++) {
581 if (strcmp(af[i]->name, name) == 0) {
582 return af[i];
583 }
584 }
585 return NULL;
586}
587
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000588static method_t *get_method(address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000589{
590 int i;
591
592 for (i = 0; i < af->n_methods; i++) {
593 if (strcmp(af->method[i].name, name) == 0) {
594 return &af->method[i];
595 }
596 }
597 return(NULL);
598}
599
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000600static int duplicate_if(interface_defn_t *ifa, interface_defn_t *ifb)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000601{
602 if (strcmp(ifa->iface, ifb->iface) != 0) {
603 return(0);
604 }
605 if (ifa->address_family != ifb->address_family) {
606 return(0);
607 }
608 return(1);
609}
610
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000611static interfaces_file_t *read_interfaces(char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000612{
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000613 interface_defn_t *currif = NULL;
614 interfaces_file_t *defn;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000615#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000616 mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000617#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000618 FILE *f;
619 char firstword[80];
620 char *buf = NULL;
621 char *rest;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000622// int line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000623
624 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
625
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000626 defn = xmalloc(sizeof(interfaces_file_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000627 defn->max_autointerfaces = defn->n_autointerfaces = 0;
628 defn->autointerfaces = NULL;
629 defn->mappings = NULL;
630 defn->ifaces = NULL;
631 f = fopen(filename, "r");
632 if (f == NULL) {
633 return NULL;
634 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000635
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000636 while ((buf = get_line_from_file(f)) != NULL) {
637 char *end;
638
639 if (buf[0] == '#') {
640 continue;
641 }
642 end = last_char_is(buf, '\n');
643 if (end) {
644 *end = '\0';
645 }
646 while ((end = last_char_is(buf, ' ')) != NULL) {
647 *end = '\0';
648 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000649 rest = next_word(buf, firstword, 80);
650 if (rest == NULL) {
651 continue; /* blank line */
652 }
653
654 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000655#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000656 currmap = xmalloc(sizeof(mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000657 currmap->max_matches = 0;
658 currmap->n_matches = 0;
659 currmap->match = NULL;
660
661 while ((rest = next_word(rest, firstword, 80))) {
662 if (currmap->max_matches == currmap->n_matches) {
663 currmap->max_matches = currmap->max_matches * 2 + 1;
664 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
665 }
666
667 currmap->match[currmap->n_matches++] = xstrdup(firstword);
668 }
669 currmap->max_mappings = 0;
670 currmap->n_mappings = 0;
671 currmap->mapping = NULL;
672 currmap->script = NULL;
673 {
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000674 mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000675 while (*where != NULL) {
676 where = &(*where)->next;
677 }
678 *where = currmap;
679 currmap->next = NULL;
680 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000681#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000682 currently_processing = MAPPING;
683 } else if (strcmp(firstword, "iface") == 0) {
684 {
685 char iface_name[80];
686 char address_family_name[80];
687 char method_name[80];
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000688 address_family_t *addr_fams[] = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
690 &addr_inet,
691#endif
692#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
693 &addr_inet6,
694#endif
695#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
696 &addr_ipx,
697#endif
698 NULL
699 };
700
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000701 currif = xmalloc(sizeof(interface_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000702
703 rest = next_word(rest, iface_name, 80);
704 rest = next_word(rest, address_family_name, 80);
705 rest = next_word(rest, method_name, 80);
706
707 if (rest == NULL) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000708 error_msg("too few parameters for line \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709 return NULL;
710 }
711
712 if (rest[0] != '\0') {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000713 error_msg("too many parameters \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714 return NULL;
715 }
716
717 currif->iface = xstrdup(iface_name);
718
719 currif->address_family = get_address_family(addr_fams, address_family_name);
720 if (!currif->address_family) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000721 error_msg("unknown address type \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722 return NULL;
723 }
724
725 currif->method = get_method(currif->address_family, method_name);
726 if (!currif->method) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000727 error_msg("unknown method \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 return NULL;
729 }
730
731 currif->automatic = 1;
732 currif->max_options = 0;
733 currif->n_options = 0;
734 currif->option = NULL;
735
736
737 {
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000738 interface_defn_t **where = &defn->ifaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000739
740 while (*where != NULL) {
741 if (duplicate_if(*where, currif)) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000742 error_msg("duplicate interface \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000743 return NULL;
744 }
745 where = &(*where)->next;
746 }
747
748 *where = currif;
749 currif->next = NULL;
750 }
751 }
752 currently_processing = IFACE;
753 } else if (strcmp(firstword, "auto") == 0) {
754 while ((rest = next_word(rest, firstword, 80))) {
755 int i;
756
757 for (i = 0; i < defn->n_autointerfaces; i++) {
758 if (strcmp(firstword, defn->autointerfaces[i]) == 0) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000759 perror_msg("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760 return NULL;
761 }
762 }
763
764 if (defn->n_autointerfaces == defn->max_autointerfaces) {
765 char **tmp;
766
767 defn->max_autointerfaces *= 2;
768 defn->max_autointerfaces++;
769 tmp = xrealloc(defn->autointerfaces, sizeof(*tmp) * defn->max_autointerfaces);
770 defn->autointerfaces = tmp;
771 }
772
773 defn->autointerfaces[defn->n_autointerfaces] = xstrdup(firstword);
774 defn->n_autointerfaces++;
775 }
776 currently_processing = NONE;
777 } else {
778 switch (currently_processing) {
779 case IFACE:
780 {
781 int i;
782
783 if (xstrlen(rest) == 0) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000784 error_msg("option with empty value \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000785 return NULL;
786 }
787
788 if (strcmp(firstword, "up") != 0
789 && strcmp(firstword, "down") != 0
790 && strcmp(firstword, "pre-up") != 0
791 && strcmp(firstword, "post-down") != 0) {
792 for (i = 0; i < currif->n_options; i++) {
793 if (strcmp(currif->option[i].name, firstword) == 0) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000794 error_msg("duplicate option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000795 return NULL;
796 }
797 }
798 }
799 }
800 if (currif->n_options >= currif->max_options) {
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000801 variable_t *opt;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000802
803 currif->max_options = currif->max_options + 10;
804 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
805 currif->option = opt;
806 }
807 currif->option[currif->n_options].name = xstrdup(firstword);
808 currif->option[currif->n_options].value = xstrdup(rest);
809 if (!currif->option[currif->n_options].name) {
810 perror(filename);
811 return NULL;
812 }
813 if (!currif->option[currif->n_options].value) {
814 perror(filename);
815 return NULL;
816 }
817 currif->n_options++;
818 break;
819 case MAPPING:
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000820#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000821 if (strcmp(firstword, "script") == 0) {
822 if (currmap->script != NULL) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000823 error_msg("duplicate script in mapping \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 return NULL;
825 } else {
826 currmap->script = xstrdup(rest);
827 }
828 } else if (strcmp(firstword, "map") == 0) {
829 if (currmap->max_mappings == currmap->n_mappings) {
830 currmap->max_mappings = currmap->max_mappings * 2 + 1;
831 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
832 }
833 currmap->mapping[currmap->n_mappings] = xstrdup(rest);
834 currmap->n_mappings++;
835 } else {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000836 error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000837 return NULL;
838 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000839#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840 break;
841 case NONE:
842 default:
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000843 error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000844 return NULL;
845 }
846 }
847 }
848 if (ferror(f) != 0) {
849 perror_msg("%s", filename);
850 return NULL;
851 }
852 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000853
854 return defn;
855}
856
857static int check(char *str)
858{
859 return (str != NULL);
860}
861
862static char *setlocalenv(char *format, char *name, char *value)
863{
864 char *result;
865 char *here;
866 char *there;
867
868 result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1);
869
870 sprintf(result, format, name, value);
871
872 for (here = there = result; *there != '=' && *there; there++) {
873 if (*there == '-')
874 *there = '_';
875 if (isalpha(*there))
876 *there = toupper(*there);
877
878 if (isalnum(*there) || *there == '_') {
879 *here = *there;
880 here++;
881 }
882 }
883 memmove(here, there, xstrlen(there) + 1);
884
885 return result;
886}
887
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000888static void set_environ(interface_defn_t *iface, char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000889{
890 char **environend;
891 int i;
892 const int n_env_entries = iface->n_options + 5;
893 char **ppch;
894
895 if (environ != NULL) {
896 for (ppch = environ; *ppch; ppch++) {
897 free(*ppch);
898 *ppch = NULL;
899 }
900 free(environ);
901 environ = NULL;
902 }
903 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
904 environend = environ;
905 *environend = NULL;
906
907 for (i = 0; i < iface->n_options; i++) {
908 if (strcmp(iface->option[i].name, "up") == 0
909 || strcmp(iface->option[i].name, "down") == 0
910 || strcmp(iface->option[i].name, "pre-up") == 0
911 || strcmp(iface->option[i].name, "post-down") == 0) {
912 continue;
913 }
914 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
915 *environend = NULL;
916 }
917
918 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
919 *environend = NULL;
920 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
921 *environend = NULL;
922 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
923 *environend = NULL;
924 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
925 *environend = NULL;
926 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
927 *environend = NULL;
928}
929
930static int doit(char *str)
931{
932 if (verbose || no_act) {
933 error_msg("%s", str);
934 }
935 if (!no_act) {
936 pid_t child;
937 int status;
938
939 fflush(NULL);
940 switch (child = fork()) {
941 case -1: /* failure */
942 return 0;
943 case 0: /* child */
944 execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
945 exit(127);
946 default: /* parent */
947 }
948 waitpid(child, &status, 0);
949 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
950 return 0;
951 }
952 }
953 return (1);
954}
955
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000956static int execute_all(interface_defn_t *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000957{
958 int i;
Glenn L McGrath9af8a722002-11-11 07:03:02 +0000959 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000960
961 for (i = 0; i < ifd->n_options; i++) {
962 if (strcmp(ifd->option[i].name, opt) == 0) {
963 if (!(*exec) (ifd->option[i].value)) {
964 return 0;
965 }
966 }
967 }
968
Glenn L McGrath9af8a722002-11-11 07:03:02 +0000969 buf = xmalloc(xstrlen(opt) + 19);
970 sprintf(buf, "/etc/network/if-%s.d", opt);
971 run_parts(&buf, 0);
972 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000973 return (1);
974}
975
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000976static int iface_up(interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000977{
978 if (!iface->method->up(iface, check)) {
979 return (-1);
980 }
981
982 set_environ(iface, "start");
983 if (!execute_all(iface, doit, "pre-up")) {
984 return (0);
985 }
986 if (!iface->method->up(iface, doit)) {
987 return (0);
988 }
989 if (!execute_all(iface, doit, "up")) {
990 return (0);
991 }
992
993 return (1);
994}
995
Glenn L McGrath0325a1c2002-12-07 07:45:42 +0000996static int iface_down(interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000997{
998 if (!iface->method->down(iface, check)) {
999 return (-1);
1000 }
1001 set_environ(iface, "stop");
1002 if (!execute_all(iface, doit, "down")) {
1003 return (0);
1004 }
1005 if (!iface->method->down(iface, doit)) {
1006 return (0);
1007 }
1008 if (!execute_all(iface, doit, "post-down")) {
1009 return (0);
1010 }
1011 return (1);
1012}
1013
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001014#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001015static int popen2(FILE **in, FILE **out, char *command, ...)
1016{
1017 va_list ap;
1018 char *argv[11] = { command };
1019 int argc;
1020 int infd[2], outfd[2];
1021 pid_t pid;
1022
1023 argc = 1;
1024 va_start(ap, command);
1025 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1026 argc++;
1027 }
1028 argv[argc] = NULL; /* make sure */
1029 va_end(ap);
1030
1031 if (pipe(infd) != 0) {
1032 return 0;
1033 }
1034
1035 if (pipe(outfd) != 0) {
1036 close(infd[0]);
1037 close(infd[1]);
1038 return 0;
1039 }
1040
1041 fflush(NULL);
1042 switch (pid = fork()) {
1043 case -1: /* failure */
1044 close(infd[0]);
1045 close(infd[1]);
1046 close(outfd[0]);
1047 close(outfd[1]);
1048 return 0;
1049 case 0: /* child */
1050 dup2(infd[0], 0);
1051 dup2(outfd[1], 1);
1052 close(infd[0]);
1053 close(infd[1]);
1054 close(outfd[0]);
1055 close(outfd[1]);
1056 execvp(command, argv);
1057 exit(127);
1058 default: /* parent */
1059 *in = fdopen(infd[1], "w");
1060 *out = fdopen(outfd[0], "r");
1061 close(infd[0]);
1062 close(outfd[1]);
1063 return pid;
1064 }
1065 /* unreached */
1066}
1067
Glenn L McGrath0325a1c2002-12-07 07:45:42 +00001068static int run_mapping(char *physical, char *logical, int len, mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001069{
1070 FILE *in, *out;
1071 int i, status;
1072 pid_t pid;
1073
1074
1075 pid = popen2(&in, &out, map->script, physical, NULL);
1076 if (pid == 0) {
1077 return 0;
1078 }
1079 for (i = 0; i < map->n_mappings; i++) {
1080 fprintf(in, "%s\n", map->mapping[i]);
1081 }
1082 fclose(in);
1083 waitpid(pid, &status, 0);
1084 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1085 if (fgets(logical, len, out)) {
1086 char *pch = logical + xstrlen(logical) - 1;
1087
1088 while (pch >= logical && isspace(*pch))
1089 *(pch--) = '\0';
1090 }
1091 }
1092 fclose(out);
1093
1094 return 1;
1095}
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001096#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001097
1098static int lookfor_iface(char **ifaces, int n_ifaces, char *iface)
1099{
1100 int i;
1101
1102 for (i = 0; i < n_ifaces; i++) {
1103 if (strncmp(iface, ifaces[i], xstrlen(iface)) == 0) {
1104 if (ifaces[i][xstrlen(iface)] == '=') {
1105 return i;
1106 }
1107 }
1108 }
1109
1110 return(-1);
1111}
1112
1113static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, char *new_iface)
1114{
1115 if (*max_ifaces == *n_ifaces) {
1116 *max_ifaces = (*max_ifaces * 2) + 1;
1117 *ifaces = xrealloc(*ifaces, sizeof(**ifaces) * *max_ifaces);
1118 }
1119
1120 (*ifaces)[(*n_ifaces)++] = new_iface;
1121}
1122
1123extern int ifupdown_main(int argc, char **argv)
1124{
Glenn L McGrath0325a1c2002-12-07 07:45:42 +00001125 int (*cmds) (interface_defn_t *) = NULL;
1126 interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127 FILE *state_fp = NULL;
1128 char **target_iface = NULL;
1129 char **state = NULL; /* list of iface=liface */
1130 char *interfaces = "/etc/network/interfaces";
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001131 char *statefile = "/var/run/ifstate";
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001133#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134 int run_mappings = 1;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001135#endif
1136 int do_all = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001137 int force = 0;
1138 int n_target_ifaces = 0;
1139 int n_state = 0;
1140 int max_state = 0;
1141 int i;
1142
1143 if (applet_name[2] == 'u') {
1144 /* ifup command */
1145 cmds = iface_up;
1146 } else {
1147 /* ifdown command */
1148 cmds = iface_down;
1149 }
1150
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001151#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001152 while ((i = getopt(argc, argv, "i:hvnamf")) != -1) {
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001153#else
1154 while ((i = getopt(argc, argv, "i:hvnaf")) != -1) {
1155#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001156 switch (i) {
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001157 case 'i': /* interfaces */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158 interfaces = xstrdup(optarg);
1159 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001160 case 'v': /* verbose */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161 verbose = 1;
1162 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001163 case 'a': /* all */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 do_all = 1;
1165 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001166 case 'n': /* no-act */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001167 no_act = 1;
1168 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001169#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001170 case 'm': /* no-mappings */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001171 run_mappings = 0;
1172 break;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001173#endif
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001174 case 'f': /* force */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175 force = 1;
1176 break;
1177 default:
1178 show_usage();
1179 break;
1180 }
1181 }
1182
1183 if (argc - optind > 0) {
1184 if (do_all) {
1185 show_usage();
1186 }
1187 } else {
1188 if (!do_all) {
1189 show_usage();
1190 }
1191 }
1192
1193 defn = read_interfaces(interfaces);
1194 if (!defn) {
1195 error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces);
1196 }
1197
1198 state_fp = fopen(statefile, no_act ? "r" : "a+");
1199 if (state_fp == NULL && !no_act) {
1200 perror_msg_and_die("failed to open statefile %s", statefile);
1201 }
1202
1203 if (state_fp != NULL) {
Glenn L McGrath398ff9d2002-12-06 11:51:46 +00001204 char *start;
1205 while ((start = get_line_from_file(state_fp)) != NULL) {
1206 char *end_ptr;
1207 /* We should only need to check for a single character */
1208 end_ptr = start + strcspn(start, " \t\n");
1209 *end_ptr = '\0';
1210 add_to_state(&state, &n_state, &max_state, start);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211 }
1212 }
1213
1214 if (do_all) {
1215 if (cmds == iface_up) {
1216 target_iface = defn->autointerfaces;
1217 n_target_ifaces = defn->n_autointerfaces;
1218 } else if (cmds == iface_down) {
1219 target_iface = state;
1220 n_target_ifaces = n_state;
1221 }
1222 } else {
1223 target_iface = argv + optind;
1224 n_target_ifaces = argc - optind;
1225 }
1226
1227
1228 for (i = 0; i < n_target_ifaces; i++) {
Glenn L McGrath0325a1c2002-12-07 07:45:42 +00001229 interface_defn_t *currif;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 char iface[80];
1231 char liface[80];
1232 char *pch;
1233 int okay = 0;
1234
1235 strncpy(iface, target_iface[i], sizeof(iface));
1236 iface[sizeof(iface) - 1] = '\0';
1237
1238 if ((pch = strchr(iface, '='))) {
1239 *pch = '\0';
1240 strncpy(liface, pch + 1, sizeof(liface));
1241 liface[sizeof(liface) - 1] = '\0';
1242 } else {
1243 strncpy(liface, iface, sizeof(liface));
1244 liface[sizeof(liface) - 1] = '\0';
1245 }
1246 if (!force) {
1247 int already_up = lookfor_iface(state, n_state, iface);;
1248
1249 if (cmds == iface_up) {
1250 /* ifup */
1251 if (already_up != -1) {
1252 error_msg("interface %s already configured", iface);
1253 continue;
1254 }
1255 } else {
1256 /* ifdown */
1257 if (already_up == -1) {
1258 error_msg("interface %s not configured", iface);
1259 continue;
1260 }
1261 strncpy(liface, strchr(state[already_up], '=') + 1, 80);
1262 liface[79] = 0;
1263 }
1264 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001265#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 if ((cmds == iface_up) && run_mappings) {
Glenn L McGrath0325a1c2002-12-07 07:45:42 +00001267 mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001268
1269 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1270
1271 for (i = 0; i < currmap->n_matches; i++) {
1272 if (fnmatch(currmap->match[i], liface, 0) != 0)
1273 continue;
1274 if (verbose) {
1275 error_msg("Running mapping script %s on %s", currmap->script, liface);
1276 }
1277 run_mapping(iface, liface, sizeof(liface), currmap);
1278 break;
1279 }
1280 }
1281 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001282#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001283
1284 for (currif = defn->ifaces; currif; currif = currif->next) {
1285 if (strcmp(liface, currif->iface) == 0) {
1286 char *oldiface = currif->iface;
1287
1288 okay = 1;
1289
1290 currif->iface = iface;
1291
1292 if (verbose) {
1293 error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1294 }
1295
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001296 if (cmds(currif) == -1) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001297 printf
1298 ("Don't seem to be have all the variables for %s/%s.\n",
1299 liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001300 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001301
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001302 currif->iface = oldiface;
1303 }
1304 }
1305
1306 if (!okay && !force) {
1307 error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1308 } else {
1309 int already_up = lookfor_iface(state, n_state, iface);
1310
1311 if (cmds == iface_up) {
1312 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1);
1313 sprintf(newiface, "%s=%s", iface, liface);
1314 if (already_up == -1) {
1315 add_to_state(&state, &n_state, &max_state, newiface);
1316 } else {
1317 free(state[already_up]);
1318 state[already_up] = newiface;
1319 }
1320 } else if (cmds == iface_down) {
1321 if (already_up != -1) {
1322 state[already_up] = state[--n_state];
1323 }
1324 }
1325 }
1326 if (state_fp != NULL && !no_act) {
Glenn L McGrath92273712002-12-06 08:10:40 +00001327 unsigned short j;
1328
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001329 if (ftruncate(fileno(state_fp), 0) < 0) {
1330 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1331 }
1332
1333 rewind(state_fp);
Glenn L McGrath92273712002-12-06 08:10:40 +00001334 for (j = 0; j < n_state; j++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001335 fputs(state[i], state_fp);
1336 fputc('\n', state_fp);
1337 }
1338 fflush(state_fp);
1339 }
1340 }
1341
1342 if (state_fp != NULL) {
1343 fclose(state_fp);
1344 state_fp = NULL;
1345 }
1346
1347 return 0;
1348}