blob: 8df2e7f5dce72968fc4d1affd327b77ec2ffaba6 [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 *
6 * Remove checks for kernel version, assume kernel version 2.2.0 or better
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23#include <sys/stat.h>
24#include <sys/utsname.h>
25#include <sys/wait.h>
26
27#include <ctype.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <fnmatch.h>
31#include <getopt.h>
32#include <stdarg.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <unistd.h>
37
38#include "busybox.h"
39#include "config.h"
40
41#define IFUPDOWN_VERSION "0.6.4"
42
43typedef struct interface_defn interface_defn;
44
45typedef int (execfn)(char *command);
46typedef int (command_set)(interface_defn *ifd, execfn *e);
47
48typedef struct method {
49 char *name;
50 command_set *up;
51 command_set *down;
52} method;
53
54typedef struct address_family {
55 char *name;
56 int n_methods;
57 method *method;
58} address_family;
59
Glenn L McGrath49a28b32002-11-10 13:17:08 +000060#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000061typedef struct mapping_defn {
62 struct mapping_defn *next;
63
64 int max_matches;
65 int n_matches;
66 char **match;
67
68 char *script;
69
70 int max_mappings;
71 int n_mappings;
72 char **mapping;
73} mapping_defn;
Glenn L McGrath49a28b32002-11-10 13:17:08 +000074#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075
76typedef struct variable {
77 char *name;
78 char *value;
79} variable;
80
81struct interface_defn {
82 struct interface_defn *next;
83
84 char *iface;
85 address_family *address_family;
86 method *method;
87
88 int automatic;
89
90 int max_options;
91 int n_options;
92 variable *option;
93};
94
95typedef struct interfaces_file {
96 int max_autointerfaces;
97 int n_autointerfaces;
98 char **autointerfaces;
99
100 interface_defn *ifaces;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000101#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000102 mapping_defn *mappings;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000103#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000104} interfaces_file;
105
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
144static char *get_var(char *id, size_t idlen, interface_defn *ifd)
145{
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
161static char *parse(char *command, interface_defn *ifd)
162{
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
252static int execute(char *command, interface_defn *ifd, execfn *exec)
253{
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
269static int static_up_ipx(interface_defn *ifd, execfn *exec)
270{
271 if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) {
272 return(0);
273 }
274 return(1);
275}
276
277static int static_down_ipx(interface_defn *ifd, execfn *exec)
278{
279 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
280 return(0);
281 }
282 return(1);
283}
284
285static int dynamic_up(interface_defn *ifd, execfn *exec)
286{
287 if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) {
288 return(0);
289 }
290 return(1);
291}
292
293static int dynamic_down(interface_defn *ifd, execfn *exec)
294{
295 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
296 return(0);
297 }
298 return(1);
299}
300
301static method methods_ipx[] = {
302 { "dynamic", dynamic_up, dynamic_down, },
303 { "static", static_up_ipx, static_down_ipx, },
304};
305
306address_family addr_ipx = {
307 "ipx",
308 sizeof(methods_ipx) / sizeof(struct method),
309 methods_ipx
310};
311#endif /* IFUP_FEATURE_IPX */
312
313#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
314static int loopback_up6(interface_defn *ifd, execfn *exec)
315{
316 if (!execute("ifconfig %iface% add ::1", ifd, exec)) {
317 return(0);
318 }
319 return(1);
320}
321
322static int loopback_down6(interface_defn *ifd, execfn *exec)
323{
324 if (!execute("ifconfig %iface% del ::1", ifd, exec))
325 return(0);
326 }
327 return(1);
328}
329
330static int static_up6(interface_defn *ifd, execfn *exec)
331{
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
344static int static_down6(interface_defn *ifd, execfn *exec)
345{
346 if (!execute("ifconfig %iface% down", ifd, exec)) {
347 return(0);
348 }
349 return(1);
350}
351
352static int v4tunnel_up(interface_defn *ifd, execfn *exec)
353{
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
369static int v4tunnel_down(interface_defn * ifd, execfn * exec)
370{
371 if (!execute("ip tunnel del %iface%", ifd, exec)) {
372 return(0);
373 }
374 return(1);
375}
376
377static method methods6[] = {
378 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
379 { "static", static_up6, static_down6, },
380 { "loopback", loopback_up6, loopback_down6, },
381};
382
383address_family addr_inet6 = {
384 "inet6",
385 sizeof(methods6) / sizeof(struct method),
386 methods6
387};
388#endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
389
390#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
391static int loopback_up(interface_defn *ifd, execfn *exec)
392{
393 if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) {
394 return(0);
395 }
396 return(1);
397}
398
399static int loopback_down(interface_defn *ifd, execfn *exec)
400{
401 if (!execute("ifconfig %iface% down", ifd, exec)) {
402 return(0);
403 }
404 return(1);
405}
406
407static int static_up(interface_defn *ifd, execfn *exec)
408{
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
419static int static_down(interface_defn *ifd, execfn *exec)
420{
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 McGrath021fa7d2002-11-09 09:34:15 +0000441static int dhcp_up(interface_defn *ifd, execfn *exec)
442{
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
463static int dhcp_down(interface_defn *ifd, execfn *exec)
464{
465 if (execable("/sbin/dhclient")) {
466 if (!execute("cat /var/run/dhclient.%iface%.pid | xargs -i kill -TERM {}", ifd, exec)) {
467 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")) {
474 if (!execute("cat /var/run/udhcpc.%iface%.pid | xargs -i kill -TERM {}", ifd, exec)) {
475 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
488static int bootp_up(interface_defn *ifd, execfn *exec)
489{
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
496static int bootp_down(interface_defn *ifd, execfn *exec)
497{
498 if (!execute("ifconfig down %iface%", ifd, exec)) {
499 return 0;
500 }
501 return 1;
502}
503
504static int ppp_up(interface_defn *ifd, execfn *exec)
505{
506 if (!execute("pon [[%provider%]]", ifd, exec)) {
507 return 0;
508 }
509 return 1;
510}
511
512static int ppp_down(interface_defn *ifd, execfn *exec)
513{
514 if (!execute("poff [[%provider%]]", ifd, exec)) {
515 return 0;
516 }
517 return 1;
518}
519
520static int wvdial_up(interface_defn *ifd, execfn *exec)
521{
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
528static int wvdial_down(interface_defn *ifd, execfn *exec)
529{
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
536static method methods[] = {
537 { "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
545address_family addr_inet = {
546 "inet",
547 sizeof(methods) / sizeof(struct method),
548 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
576static address_family *get_address_family(address_family *af[], char *name)
577{
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
588static method *get_method(address_family *af, char *name)
589{
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
600static int duplicate_if(interface_defn *ifa, interface_defn *ifb)
601{
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
611static int get_line(char **result, size_t * result_len, FILE * f, int *line)
612{
613 size_t pos;
614
615 do {
616 pos = 0;
617 do {
618 if (*result_len - pos < 10) {
619 char *newstr = xrealloc(*result, *result_len * 2 + 80);
620 *result = newstr;
621 *result_len = *result_len * 2 + 80;
622 }
623
624 if (!fgets(*result + pos, *result_len - pos, f)) {
625 if (ferror(f) == 0 && pos == 0)
626 return 0;
627 if (ferror(f) != 0)
628 return 0;
629 }
630 pos += xstrlen(*result + pos);
631 } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n');
632
633 if (pos != 0 && (*result)[pos - 1] == '\n') {
634 (*result)[--pos] = '\0';
635 }
636
637 (*line)++;
638 {
639 int first = 0;
640
641 while (isspace((*result)[first]) && (*result)[first]) {
642 first++;
643 }
644
645 memmove(*result, *result + first, pos - first + 1);
646 pos -= first;
647 }
648 } while ((*result)[0] == '#');
649
650 while ((*result)[pos - 1] == '\\') {
651 (*result)[--pos] = '\0';
652 do {
653 if (*result_len - pos < 10) {
654 char *newstr = xrealloc(*result, *result_len * 2 + 80);
655 *result = newstr;
656 *result_len = *result_len * 2 + 80;
657 }
658
659 if (!fgets(*result + pos, *result_len - pos, f)) {
660 if (ferror(f) == 0 && pos == 0)
661 return 0;
662 if (ferror(f) != 0)
663 return 0;
664 }
665 pos += xstrlen(*result + pos);
666 } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n');
667
668 if (pos != 0 && (*result)[pos - 1] == '\n') {
669 (*result)[--pos] = '\0';
670 }
671 (*line)++;
672 }
673
674 while (isspace((*result)[pos - 1])) { /* remove trailing whitespace */
675 pos--;
676 }
677 (*result)[pos] = '\0';
678
679 return 1;
680}
681
682static interfaces_file *read_interfaces(char *filename)
683{
684 interface_defn *currif = NULL;
685 interfaces_file *defn;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000686#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687 mapping_defn *currmap = NULL;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000688#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689 FILE *f;
690 char firstword[80];
691 char *buf = NULL;
692 char *rest;
693 int line;
694 size_t buf_len = 0;
695
696 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
697
698 defn = xmalloc(sizeof(interfaces_file));
699 defn->max_autointerfaces = defn->n_autointerfaces = 0;
700 defn->autointerfaces = NULL;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000701#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000702 defn->mappings = NULL;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000703#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000704 defn->ifaces = NULL;
705 f = fopen(filename, "r");
706 if (f == NULL) {
707 return NULL;
708 }
709 line = 0;
710
711 while (get_line(&buf, &buf_len, f, &line)) {
712 rest = next_word(buf, firstword, 80);
713 if (rest == NULL) {
714 continue; /* blank line */
715 }
716
717 if (strcmp(firstword, "mapping") == 0) {
Glenn L McGratheebe31d2002-11-10 13:20:35 +0000718#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000719 currmap = xmalloc(sizeof(mapping_defn));
720 currmap->max_matches = 0;
721 currmap->n_matches = 0;
722 currmap->match = NULL;
723
724 while ((rest = next_word(rest, firstword, 80))) {
725 if (currmap->max_matches == currmap->n_matches) {
726 currmap->max_matches = currmap->max_matches * 2 + 1;
727 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
728 }
729
730 currmap->match[currmap->n_matches++] = xstrdup(firstword);
731 }
732 currmap->max_mappings = 0;
733 currmap->n_mappings = 0;
734 currmap->mapping = NULL;
735 currmap->script = NULL;
736 {
737 mapping_defn **where = &defn->mappings;
738 while (*where != NULL) {
739 where = &(*where)->next;
740 }
741 *where = currmap;
742 currmap->next = NULL;
743 }
744 currently_processing = MAPPING;
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000745#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746 } else if (strcmp(firstword, "iface") == 0) {
747 {
748 char iface_name[80];
749 char address_family_name[80];
750 char method_name[80];
751 address_family *addr_fams[] = {
752#ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
753 &addr_inet,
754#endif
755#ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
756 &addr_inet6,
757#endif
758#ifdef CONFIG_FEATURE_IFUPDOWN_IPX
759 &addr_ipx,
760#endif
761 NULL
762 };
763
764 currif = xmalloc(sizeof(interface_defn));
765
766 rest = next_word(rest, iface_name, 80);
767 rest = next_word(rest, address_family_name, 80);
768 rest = next_word(rest, method_name, 80);
769
770 if (rest == NULL) {
771 error_msg("%s:%d: too few parameters for iface line", filename, line);
772 return NULL;
773 }
774
775 if (rest[0] != '\0') {
776 error_msg("%s:%d: too many parameters for iface line", filename, line);
777 return NULL;
778 }
779
780 currif->iface = xstrdup(iface_name);
781
782 currif->address_family = get_address_family(addr_fams, address_family_name);
783 if (!currif->address_family) {
784 error_msg("%s:%d: unknown address type", filename, line);
785 return NULL;
786 }
787
788 currif->method = get_method(currif->address_family, method_name);
789 if (!currif->method) {
790 error_msg("%s:%d: unknown method", filename, line);
791 return NULL;
792 }
793
794 currif->automatic = 1;
795 currif->max_options = 0;
796 currif->n_options = 0;
797 currif->option = NULL;
798
799
800 {
801 interface_defn **where = &defn->ifaces;
802
803 while (*where != NULL) {
804 if (duplicate_if(*where, currif)) {
805 error_msg("%s:%d: duplicate interface", filename, line);
806 return NULL;
807 }
808 where = &(*where)->next;
809 }
810
811 *where = currif;
812 currif->next = NULL;
813 }
814 }
815 currently_processing = IFACE;
816 } else if (strcmp(firstword, "auto") == 0) {
817 while ((rest = next_word(rest, firstword, 80))) {
818 int i;
819
820 for (i = 0; i < defn->n_autointerfaces; i++) {
821 if (strcmp(firstword, defn->autointerfaces[i]) == 0) {
822 perror_msg("%s:%d: interface declared auto twice", filename, line);
823 return NULL;
824 }
825 }
826
827 if (defn->n_autointerfaces == defn->max_autointerfaces) {
828 char **tmp;
829
830 defn->max_autointerfaces *= 2;
831 defn->max_autointerfaces++;
832 tmp = xrealloc(defn->autointerfaces, sizeof(*tmp) * defn->max_autointerfaces);
833 defn->autointerfaces = tmp;
834 }
835
836 defn->autointerfaces[defn->n_autointerfaces] = xstrdup(firstword);
837 defn->n_autointerfaces++;
838 }
839 currently_processing = NONE;
840 } else {
841 switch (currently_processing) {
842 case IFACE:
843 {
844 int i;
845
846 if (xstrlen(rest) == 0) {
847 error_msg("%s:%d: option with empty value", filename, line);
848 return NULL;
849 }
850
851 if (strcmp(firstword, "up") != 0
852 && strcmp(firstword, "down") != 0
853 && strcmp(firstword, "pre-up") != 0
854 && strcmp(firstword, "post-down") != 0) {
855 for (i = 0; i < currif->n_options; i++) {
856 if (strcmp(currif->option[i].name, firstword) == 0) {
857 error_msg("%s:%d: duplicate option", filename, line);
858 return NULL;
859 }
860 }
861 }
862 }
863 if (currif->n_options >= currif->max_options) {
864 variable *opt;
865
866 currif->max_options = currif->max_options + 10;
867 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
868 currif->option = opt;
869 }
870 currif->option[currif->n_options].name = xstrdup(firstword);
871 currif->option[currif->n_options].value = xstrdup(rest);
872 if (!currif->option[currif->n_options].name) {
873 perror(filename);
874 return NULL;
875 }
876 if (!currif->option[currif->n_options].value) {
877 perror(filename);
878 return NULL;
879 }
880 currif->n_options++;
881 break;
882 case MAPPING:
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000883#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000884 if (strcmp(firstword, "script") == 0) {
885 if (currmap->script != NULL) {
886 error_msg("%s:%d: duplicate script in mapping", filename, line);
887 return NULL;
888 } else {
889 currmap->script = xstrdup(rest);
890 }
891 } else if (strcmp(firstword, "map") == 0) {
892 if (currmap->max_mappings == currmap->n_mappings) {
893 currmap->max_mappings = currmap->max_mappings * 2 + 1;
894 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
895 }
896 currmap->mapping[currmap->n_mappings] = xstrdup(rest);
897 currmap->n_mappings++;
898 } else {
899 error_msg("%s:%d: misplaced option", filename, line);
900 return NULL;
901 }
Glenn L McGrath49a28b32002-11-10 13:17:08 +0000902#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000903 break;
904 case NONE:
905 default:
906 error_msg("%s:%d: misplaced option", filename, line);
907 return NULL;
908 }
909 }
910 }
911 if (ferror(f) != 0) {
912 perror_msg("%s", filename);
913 return NULL;
914 }
915 fclose(f);
916 line = -1;
917
918 return defn;
919}
920
921static int check(char *str)
922{
923 return (str != NULL);
924}
925
926static char *setlocalenv(char *format, char *name, char *value)
927{
928 char *result;
929 char *here;
930 char *there;
931
932 result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1);
933
934 sprintf(result, format, name, value);
935
936 for (here = there = result; *there != '=' && *there; there++) {
937 if (*there == '-')
938 *there = '_';
939 if (isalpha(*there))
940 *there = toupper(*there);
941
942 if (isalnum(*there) || *there == '_') {
943 *here = *there;
944 here++;
945 }
946 }
947 memmove(here, there, xstrlen(there) + 1);
948
949 return result;
950}
951
952static void set_environ(interface_defn *iface, char *mode)
953{
954 char **environend;
955 int i;
956 const int n_env_entries = iface->n_options + 5;
957 char **ppch;
958
959 if (environ != NULL) {
960 for (ppch = environ; *ppch; ppch++) {
961 free(*ppch);
962 *ppch = NULL;
963 }
964 free(environ);
965 environ = NULL;
966 }
967 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
968 environend = environ;
969 *environend = NULL;
970
971 for (i = 0; i < iface->n_options; i++) {
972 if (strcmp(iface->option[i].name, "up") == 0
973 || strcmp(iface->option[i].name, "down") == 0
974 || strcmp(iface->option[i].name, "pre-up") == 0
975 || strcmp(iface->option[i].name, "post-down") == 0) {
976 continue;
977 }
978 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
979 *environend = NULL;
980 }
981
982 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
983 *environend = NULL;
984 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
985 *environend = NULL;
986 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
987 *environend = NULL;
988 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
989 *environend = NULL;
990 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
991 *environend = NULL;
992}
993
994static int doit(char *str)
995{
996 if (verbose || no_act) {
997 error_msg("%s", str);
998 }
999 if (!no_act) {
1000 pid_t child;
1001 int status;
1002
1003 fflush(NULL);
1004 switch (child = fork()) {
1005 case -1: /* failure */
1006 return 0;
1007 case 0: /* child */
1008 execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
1009 exit(127);
1010 default: /* parent */
1011 }
1012 waitpid(child, &status, 0);
1013 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1014 return 0;
1015 }
1016 }
1017 return (1);
1018}
1019
Glenn L McGrath545106f2002-11-11 06:21:00 +00001020static int execute_all(interface_defn *ifd, execfn *exec, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021{
1022 int i;
Glenn L McGrath545106f2002-11-11 06:21:00 +00001023// char buf[100];
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001024
1025 for (i = 0; i < ifd->n_options; i++) {
1026 if (strcmp(ifd->option[i].name, opt) == 0) {
1027 if (!(*exec) (ifd->option[i].value)) {
1028 return 0;
1029 }
1030 }
1031 }
1032
Glenn L McGrath545106f2002-11-11 06:21:00 +00001033 runparts("/etc/network/if-"opt".d");
1034// sprintf(buf, "run-parts /etc/network/if-%s.d", opt);
1035// (*exec) (buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001036
1037 return (1);
1038}
1039
1040static int iface_up(interface_defn *iface)
1041{
1042 if (!iface->method->up(iface, check)) {
1043 return (-1);
1044 }
1045
1046 set_environ(iface, "start");
1047 if (!execute_all(iface, doit, "pre-up")) {
1048 return (0);
1049 }
1050 if (!iface->method->up(iface, doit)) {
1051 return (0);
1052 }
1053 if (!execute_all(iface, doit, "up")) {
1054 return (0);
1055 }
1056
1057 return (1);
1058}
1059
1060static int iface_down(interface_defn *iface)
1061{
1062 if (!iface->method->down(iface, check)) {
1063 return (-1);
1064 }
1065 set_environ(iface, "stop");
1066 if (!execute_all(iface, doit, "down")) {
1067 return (0);
1068 }
1069 if (!iface->method->down(iface, doit)) {
1070 return (0);
1071 }
1072 if (!execute_all(iface, doit, "post-down")) {
1073 return (0);
1074 }
1075 return (1);
1076}
1077
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001078#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001079static int popen2(FILE **in, FILE **out, char *command, ...)
1080{
1081 va_list ap;
1082 char *argv[11] = { command };
1083 int argc;
1084 int infd[2], outfd[2];
1085 pid_t pid;
1086
1087 argc = 1;
1088 va_start(ap, command);
1089 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1090 argc++;
1091 }
1092 argv[argc] = NULL; /* make sure */
1093 va_end(ap);
1094
1095 if (pipe(infd) != 0) {
1096 return 0;
1097 }
1098
1099 if (pipe(outfd) != 0) {
1100 close(infd[0]);
1101 close(infd[1]);
1102 return 0;
1103 }
1104
1105 fflush(NULL);
1106 switch (pid = fork()) {
1107 case -1: /* failure */
1108 close(infd[0]);
1109 close(infd[1]);
1110 close(outfd[0]);
1111 close(outfd[1]);
1112 return 0;
1113 case 0: /* child */
1114 dup2(infd[0], 0);
1115 dup2(outfd[1], 1);
1116 close(infd[0]);
1117 close(infd[1]);
1118 close(outfd[0]);
1119 close(outfd[1]);
1120 execvp(command, argv);
1121 exit(127);
1122 default: /* parent */
1123 *in = fdopen(infd[1], "w");
1124 *out = fdopen(outfd[0], "r");
1125 close(infd[0]);
1126 close(outfd[1]);
1127 return pid;
1128 }
1129 /* unreached */
1130}
1131
1132static int run_mapping(char *physical, char *logical, int len, mapping_defn * map)
1133{
1134 FILE *in, *out;
1135 int i, status;
1136 pid_t pid;
1137
1138
1139 pid = popen2(&in, &out, map->script, physical, NULL);
1140 if (pid == 0) {
1141 return 0;
1142 }
1143 for (i = 0; i < map->n_mappings; i++) {
1144 fprintf(in, "%s\n", map->mapping[i]);
1145 }
1146 fclose(in);
1147 waitpid(pid, &status, 0);
1148 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1149 if (fgets(logical, len, out)) {
1150 char *pch = logical + xstrlen(logical) - 1;
1151
1152 while (pch >= logical && isspace(*pch))
1153 *(pch--) = '\0';
1154 }
1155 }
1156 fclose(out);
1157
1158 return 1;
1159}
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001160#endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1161
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162
1163static int lookfor_iface(char **ifaces, int n_ifaces, char *iface)
1164{
1165 int i;
1166
1167 for (i = 0; i < n_ifaces; i++) {
1168 if (strncmp(iface, ifaces[i], xstrlen(iface)) == 0) {
1169 if (ifaces[i][xstrlen(iface)] == '=') {
1170 return i;
1171 }
1172 }
1173 }
1174
1175 return(-1);
1176}
1177
1178static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, char *new_iface)
1179{
1180 if (*max_ifaces == *n_ifaces) {
1181 *max_ifaces = (*max_ifaces * 2) + 1;
1182 *ifaces = xrealloc(*ifaces, sizeof(**ifaces) * *max_ifaces);
1183 }
1184
1185 (*ifaces)[(*n_ifaces)++] = new_iface;
1186}
1187
1188extern int ifupdown_main(int argc, char **argv)
1189{
1190 int (*cmds) (interface_defn *) = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001191 interfaces_file *defn;
1192 FILE *state_fp = NULL;
1193 char **target_iface = NULL;
1194 char **state = NULL; /* list of iface=liface */
1195 char *interfaces = "/etc/network/interfaces";
1196 char *statefile = "/etc/network/ifstate";
1197
1198 int do_all = 0;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001199#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001200 int run_mappings = 1;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001201#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202 int force = 0;
1203 int n_target_ifaces = 0;
1204 int n_state = 0;
1205 int max_state = 0;
1206 int i;
1207
1208 if (applet_name[2] == 'u') {
1209 /* ifup command */
1210 cmds = iface_up;
1211 } else {
1212 /* ifdown command */
1213 cmds = iface_down;
1214 }
1215
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001216 while ((i = getopt(argc, argv, "i:hvnamf")) != -1) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001217 switch (i) {
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001218 case 'i': /* interfaces */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219 interfaces = xstrdup(optarg);
1220 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001221 case 'v': /* verbose */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 verbose = 1;
1223 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001224 case 'a': /* all */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001225 do_all = 1;
1226 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001227 case 'n': /* no-act */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228 no_act = 1;
1229 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001230#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1231 case 'm': /* no-mappings */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 run_mappings = 0;
1233 break;
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001234#endif
1235 case 'f': /* force */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001236 force = 1;
1237 break;
1238 default:
1239 show_usage();
1240 break;
1241 }
1242 }
1243
1244 if (argc - optind > 0) {
1245 if (do_all) {
1246 show_usage();
1247 }
1248 } else {
1249 if (!do_all) {
1250 show_usage();
1251 }
1252 }
1253
1254 defn = read_interfaces(interfaces);
1255 if (!defn) {
1256 error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces);
1257 }
1258
1259 state_fp = fopen(statefile, no_act ? "r" : "a+");
1260 if (state_fp == NULL && !no_act) {
1261 perror_msg_and_die("failed to open statefile %s", statefile);
1262 }
1263
1264 if (state_fp != NULL) {
1265 char buf[80];
1266 char *p;
1267
1268 if (!no_act) {
1269 int flags;
1270 struct flock lock;
1271 const int state_fd = fileno(state_fp);
1272
1273 flags = fcntl(state_fd, F_GETFD);
1274 if ((flags < 0) || (fcntl(state_fd, F_SETFD, flags | FD_CLOEXEC) < 0)) {
1275 perror_msg_and_die("failed to set FD_CLOEXEC on statefile %s", statefile);
1276 }
1277
1278 lock.l_type = F_WRLCK;
1279 lock.l_whence = SEEK_SET;
1280 lock.l_start = 0;
1281 lock.l_len = 0;
1282
1283 if (fcntl(state_fd, F_SETLKW, &lock) < 0) {
1284 perror_msg_and_die("failed to lock statefile %s", statefile);
1285 }
1286 }
1287
1288 rewind(state_fp);
1289 while ((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
1290 char *pch;
1291
1292 pch = buf + xstrlen(buf) - 1;
1293 while (pch > buf && isspace(*pch)) {
1294 pch--;
1295 }
1296 *(pch + 1) = '\0';
1297
1298 pch = buf;
1299 while (isspace(*pch)) {
1300 pch++;
1301 }
1302
1303 add_to_state(&state, &n_state, &max_state, xstrdup(pch));
1304 }
1305 }
1306
1307 if (do_all) {
1308 if (cmds == iface_up) {
1309 target_iface = defn->autointerfaces;
1310 n_target_ifaces = defn->n_autointerfaces;
1311 } else if (cmds == iface_down) {
1312 target_iface = state;
1313 n_target_ifaces = n_state;
1314 }
1315 } else {
1316 target_iface = argv + optind;
1317 n_target_ifaces = argc - optind;
1318 }
1319
1320
1321 for (i = 0; i < n_target_ifaces; i++) {
1322 interface_defn *currif;
1323 char iface[80];
1324 char liface[80];
1325 char *pch;
1326 int okay = 0;
1327
1328 strncpy(iface, target_iface[i], sizeof(iface));
1329 iface[sizeof(iface) - 1] = '\0';
1330
1331 if ((pch = strchr(iface, '='))) {
1332 *pch = '\0';
1333 strncpy(liface, pch + 1, sizeof(liface));
1334 liface[sizeof(liface) - 1] = '\0';
1335 } else {
1336 strncpy(liface, iface, sizeof(liface));
1337 liface[sizeof(liface) - 1] = '\0';
1338 }
1339 if (!force) {
1340 int already_up = lookfor_iface(state, n_state, iface);;
1341
1342 if (cmds == iface_up) {
1343 /* ifup */
1344 if (already_up != -1) {
1345 error_msg("interface %s already configured", iface);
1346 continue;
1347 }
1348 } else {
1349 /* ifdown */
1350 if (already_up == -1) {
1351 error_msg("interface %s not configured", iface);
1352 continue;
1353 }
1354 strncpy(liface, strchr(state[already_up], '=') + 1, 80);
1355 liface[79] = 0;
1356 }
1357 }
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001358#ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001359 if ((cmds == iface_up) && run_mappings) {
1360 mapping_defn *currmap;
1361
1362 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1363
1364 for (i = 0; i < currmap->n_matches; i++) {
1365 if (fnmatch(currmap->match[i], liface, 0) != 0)
1366 continue;
1367 if (verbose) {
1368 error_msg("Running mapping script %s on %s", currmap->script, liface);
1369 }
1370 run_mapping(iface, liface, sizeof(liface), currmap);
1371 break;
1372 }
1373 }
1374 }
Glenn L McGrath49a28b32002-11-10 13:17:08 +00001375#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001376
1377 for (currif = defn->ifaces; currif; currif = currif->next) {
1378 if (strcmp(liface, currif->iface) == 0) {
1379 char *oldiface = currif->iface;
1380
1381 okay = 1;
1382
1383 currif->iface = iface;
1384
1385 if (verbose) {
1386 error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1387 }
1388
1389 switch (cmds(currif)) {
1390 case -1:
1391 printf
1392 ("Don't seem to be have all the variables for %s/%s.\n",
1393 liface, currif->address_family->name);
1394 break;
1395 case 0:
1396 /* this wasn't entirely successful, should it be added to
1397 * the state file?
1398 */
1399 case 1:
1400 /* successful */
1401 }
1402 currif->iface = oldiface;
1403 }
1404 }
1405
1406 if (!okay && !force) {
1407 error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1408 } else {
1409 int already_up = lookfor_iface(state, n_state, iface);
1410
1411 if (cmds == iface_up) {
1412 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1);
1413 sprintf(newiface, "%s=%s", iface, liface);
1414 if (already_up == -1) {
1415 add_to_state(&state, &n_state, &max_state, newiface);
1416 } else {
1417 free(state[already_up]);
1418 state[already_up] = newiface;
1419 }
1420 } else if (cmds == iface_down) {
1421 if (already_up != -1) {
1422 state[already_up] = state[--n_state];
1423 }
1424 }
1425 }
1426 if (state_fp != NULL && !no_act) {
1427 if (ftruncate(fileno(state_fp), 0) < 0) {
1428 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1429 }
1430
1431 rewind(state_fp);
1432 for (i = 0; i < n_state; i++) {
1433 fputs(state[i], state_fp);
1434 fputc('\n', state_fp);
1435 }
1436 fflush(state_fp);
1437 }
1438 }
1439
1440 if (state_fp != NULL) {
1441 fclose(state_fp);
1442 state_fp = NULL;
1443 }
1444
1445 return 0;
1446}