1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
5 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
7 * Based on ifupdown v 0.6.4 by Anthony Towns
8 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
10 * Changes to upstream version
11 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
12 * Lines in the interfaces file cannot wrap.
13 * To adhere to the FHS, the default state file is /var/run/ifstate.
15 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18 /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
21 #include <sys/utsname.h>
25 #define MAX_OPT_DEPTH 10
26 #define EUNBALBRACK 10001
27 #define EUNDEFVAR 10002
28 #define EUNBALPER 10000
30 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
31 #define MAX_INTERFACE_LENGTH 10
34 #define debug_noise(fmt, args...)
36 /* Forward declaration */
37 struct interface_defn_t;
39 typedef int (execfn)(char *command);
44 int (*up)(struct interface_defn_t *ifd, execfn *e);
45 int (*down)(struct interface_defn_t *ifd, execfn *e);
48 struct address_family_t
52 struct method_t *method;
57 struct mapping_defn_t *next;
76 struct interface_defn_t
78 struct address_family_t *address_family;
79 struct method_t *method;
84 struct variable_t *option;
87 struct interfaces_file_t
89 llist_t *autointerfaces;
91 struct mapping_defn_t *mappings;
94 static char no_act = 0;
95 static char verbose = 0;
96 static char **__myenviron = NULL;
98 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
100 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
102 static unsigned int count_bits(unsigned int a)
105 result = (a & 0x55) + ((a >> 1) & 0x55);
106 result = (result & 0x33) + ((result >> 2) & 0x33);
107 return((result & 0x0F) + ((result >> 4) & 0x0F));
110 static int count_netmask_bits(char *dotted_quad)
112 unsigned int result, a, b, c, d;
113 /* Found a netmask... Check if it is dotted quad */
114 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
116 result = count_bits(a);
117 result += count_bits(b);
118 result += count_bits(c);
119 result += count_bits(d);
120 return ((int)result);
124 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
126 if (*pos + str_length >= *len) {
129 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
131 *len = *len * 2 + str_length + 1;
134 while (str_length-- >= 1) {
135 (*buf)[(*pos)++] = *str;
141 static int strncmpz(char *l, char *r, size_t llen)
143 int i = strncmp(l, r, llen);
152 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
156 if (strncmpz(id, "iface", idlen) == 0) {
158 static char label_buf[20];
159 strncpy(label_buf, ifd->iface, 19);
161 result = strchr(label_buf, ':');
166 } else if (strncmpz(id, "label", idlen) == 0) {
169 for (i = 0; i < ifd->n_options; i++) {
170 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
171 return (ifd->option[i].value);
179 static char *parse(char *command, struct interface_defn_t *ifd)
183 size_t pos = 0, len = 0;
184 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
185 int okay[MAX_OPT_DEPTH] = { 1 };
192 addstr(&result, &len, &pos, command, 1);
197 addstr(&result, &len, &pos, command + 1, 1);
200 addstr(&result, &len, &pos, command, 1);
205 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
206 old_pos[opt_depth] = pos;
211 addstr(&result, &len, &pos, "[", 1);
216 if (command[1] == ']' && opt_depth > 1) {
218 if (!okay[opt_depth]) {
219 pos = old_pos[opt_depth];
224 addstr(&result, &len, &pos, "]", 1);
234 nextpercent = strchr(command, '%');
241 varvalue = get_var(command, nextpercent - command, ifd);
244 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
246 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
247 /* Sigh... Add a special case for 'ip' to convert from
248 * dotted quad to bit count style netmasks. */
249 if (strncmp(command, "bnmask", 6)==0) {
251 varvalue = get_var("netmask", 7, ifd);
252 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
254 sprintf(argument, "%d", res);
255 addstr(&result, &len, &pos, argument, strlen(argument));
256 command = nextpercent + 1;
261 okay[opt_depth - 1] = 0;
264 command = nextpercent + 1;
285 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
290 out = parse(command, ifd);
304 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
305 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
307 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
309 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
310 result += execute("ip link set %iface% up", ifd, exec);
311 return ((result == 2) ? 2 : 0);
313 return( execute("ifconfig %iface% add ::1", ifd, exec));
317 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
319 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
320 return(execute("ip link set %iface% down", ifd, exec));
322 return(execute("ifconfig %iface% del ::1", ifd, exec));
326 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
329 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
330 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
331 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
332 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
334 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
335 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
336 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
338 return ((result == 3) ? 3 : 0);
341 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
343 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
344 return(execute("ip link set %iface% down", ifd, exec));
346 return(execute("ifconfig %iface% down", ifd, exec));
350 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
351 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
354 result = execute("ip tunnel add %iface% mode sit remote "
355 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
356 result += execute("ip link set %iface% up", ifd, exec);
357 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
358 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
359 return ((result == 4) ? 4 : 0);
362 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
364 return( execute("ip tunnel del %iface%", ifd, exec));
368 static struct method_t methods6[] = {
369 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
370 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
372 { "static", static_up6, static_down6, },
373 { "loopback", loopback_up6, loopback_down6, },
376 static struct address_family_t addr_inet6 = {
378 sizeof(methods6) / sizeof(struct method_t),
381 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
383 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
384 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
386 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
388 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
389 result += execute("ip link set %iface% up", ifd, exec);
390 return ((result == 2) ? 2 : 0);
392 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
396 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
398 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
400 result = execute("ip addr flush dev %iface%", ifd, exec);
401 result += execute("ip link set %iface% down", ifd, exec);
402 return ((result == 2) ? 2 : 0);
404 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
408 static int static_up(struct interface_defn_t *ifd, execfn *exec)
411 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
412 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
413 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
414 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
415 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
416 return ((result == 3) ? 3 : 0);
418 result = execute("ifconfig %iface% %address% netmask %netmask% "
419 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
420 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
422 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
423 return ((result == 2) ? 2 : 0);
427 static int static_down(struct interface_defn_t *ifd, execfn *exec)
430 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
431 result = execute("ip addr flush dev %iface%", ifd, exec);
432 result += execute("ip link set %iface% down", ifd, exec);
434 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
435 result += execute("ifconfig %iface% down", ifd, exec);
437 return ((result == 2) ? 2 : 0);
440 static int execable(char *program)
443 if (0 == stat(program, &buf)) {
444 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
451 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
453 if (execable("/sbin/udhcpc")) {
454 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
455 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
456 } else if (execable("/sbin/pump")) {
457 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
458 } else if (execable("/sbin/dhclient")) {
459 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
460 } else if (execable("/sbin/dhcpcd")) {
461 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
462 "[[-l %leasetime%]] %iface%", ifd, exec));
467 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
470 if (execable("/sbin/udhcpc")) {
471 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
472 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
473 * sequentially so we don't need to sleep */
474 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
475 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
476 } else if (execable("/sbin/pump")) {
477 result = execute("pump -i %iface% -k", ifd, exec);
478 } else if (execable("/sbin/dhclient")) {
479 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
480 } else if (execable("/sbin/dhcpcd")) {
481 result = execute("dhcpcd -k %iface%", ifd, exec);
483 return (result || static_down(ifd, exec));
486 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
488 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
489 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
490 "--returniffail --serverbcast", ifd, exec));
493 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
495 return( execute("pon [[%provider%]]", ifd, exec));
498 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
500 return( execute("poff [[%provider%]]", ifd, exec));
503 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
505 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
506 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
509 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
511 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
512 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
515 static struct method_t methods[] =
517 { "wvdial", wvdial_up, wvdial_down, },
518 { "ppp", ppp_up, ppp_down, },
519 { "static", static_up, static_down, },
520 { "bootp", bootp_up, static_down, },
521 { "dhcp", dhcp_up, dhcp_down, },
522 { "loopback", loopback_up, loopback_down, },
525 static struct address_family_t addr_inet =
528 sizeof(methods) / sizeof(struct method_t),
532 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
534 static char *next_word(char **buf)
536 unsigned short length;
539 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
543 /* Skip over leading whitespace */
545 while (isspace(*word)) {
549 /* Skip over comments */
554 /* Find the length of this word */
555 length = strcspn(word, " \t\n");
559 *buf = word + length;
560 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
569 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
573 for (i = 0; af[i]; i++) {
574 if (strcmp(af[i]->name, name) == 0) {
581 static struct method_t *get_method(struct address_family_t *af, char *name)
585 for (i = 0; i < af->n_methods; i++) {
586 if (strcmp(af->method[i].name, name) == 0) {
587 return &af->method[i];
593 static const llist_t *find_list_string(const llist_t *list, const char *string)
596 if (strcmp(list->data, string) == 0) {
604 static struct interfaces_file_t *read_interfaces(const char *filename)
606 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
607 struct mapping_defn_t *currmap = NULL;
609 struct interface_defn_t *currif = NULL;
610 struct interfaces_file_t *defn;
615 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
617 defn = xzalloc(sizeof(struct interfaces_file_t));
619 f = xfopen(filename, "r");
621 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
624 firstword = next_word(&buf_ptr);
625 if (firstword == NULL) {
627 continue; /* blank line */
630 if (strcmp(firstword, "mapping") == 0) {
631 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
632 currmap = xzalloc(sizeof(struct mapping_defn_t));
634 while ((firstword = next_word(&buf_ptr)) != NULL) {
635 if (currmap->max_matches == currmap->n_matches) {
636 currmap->max_matches = currmap->max_matches * 2 + 1;
637 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
640 currmap->match[currmap->n_matches++] = xstrdup(firstword);
642 currmap->max_mappings = 0;
643 currmap->n_mappings = 0;
644 currmap->mapping = NULL;
645 currmap->script = NULL;
647 struct mapping_defn_t **where = &defn->mappings;
648 while (*where != NULL) {
649 where = &(*where)->next;
652 currmap->next = NULL;
654 debug_noise("Added mapping\n");
656 currently_processing = MAPPING;
657 } else if (strcmp(firstword, "iface") == 0) {
660 char *address_family_name;
662 struct address_family_t *addr_fams[] = {
663 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
666 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
672 currif = xzalloc(sizeof(struct interface_defn_t));
673 iface_name = next_word(&buf_ptr);
674 address_family_name = next_word(&buf_ptr);
675 method_name = next_word(&buf_ptr);
677 if (buf_ptr == NULL) {
678 bb_error_msg("too few parameters for line \"%s\"", buf);
682 /* ship any trailing whitespace */
683 while (isspace(*buf_ptr)) {
687 if (buf_ptr[0] != '\0') {
688 bb_error_msg("too many parameters \"%s\"", buf);
692 currif->iface = xstrdup(iface_name);
694 currif->address_family = get_address_family(addr_fams, address_family_name);
695 if (!currif->address_family) {
696 bb_error_msg("unknown address type \"%s\"", address_family_name);
700 currif->method = get_method(currif->address_family, method_name);
701 if (!currif->method) {
702 bb_error_msg("unknown method \"%s\"", method_name);
709 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
710 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
711 if ((strcmp(tmp->iface, currif->iface) == 0) &&
712 (tmp->address_family == currif->address_family)) {
713 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
718 llist_add_to_end(&(defn->ifaces), (char*)currif);
720 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
722 currently_processing = IFACE;
723 } else if (strcmp(firstword, "auto") == 0) {
724 while ((firstword = next_word(&buf_ptr)) != NULL) {
726 /* Check the interface isnt already listed */
727 if (find_list_string(defn->autointerfaces, firstword)) {
728 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
731 /* Add the interface to the list */
732 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
733 debug_noise("\nauto %s\n", firstword);
735 currently_processing = NONE;
737 switch (currently_processing) {
742 if (strlen(buf_ptr) == 0) {
743 bb_error_msg("option with empty value \"%s\"", buf);
747 if (strcmp(firstword, "up") != 0
748 && strcmp(firstword, "down") != 0
749 && strcmp(firstword, "pre-up") != 0
750 && strcmp(firstword, "post-down") != 0) {
751 for (i = 0; i < currif->n_options; i++) {
752 if (strcmp(currif->option[i].name, firstword) == 0) {
753 bb_error_msg("duplicate option \"%s\"", buf);
759 if (currif->n_options >= currif->max_options) {
760 struct variable_t *opt;
762 currif->max_options = currif->max_options + 10;
763 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
764 currif->option = opt;
766 currif->option[currif->n_options].name = xstrdup(firstword);
767 currif->option[currif->n_options].value = xstrdup(buf_ptr);
768 if (!currif->option[currif->n_options].name) {
772 if (!currif->option[currif->n_options].value) {
776 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
777 currif->option[currif->n_options].value);
781 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
782 if (strcmp(firstword, "script") == 0) {
783 if (currmap->script != NULL) {
784 bb_error_msg("duplicate script in mapping \"%s\"", buf);
787 currmap->script = xstrdup(next_word(&buf_ptr));
789 } else if (strcmp(firstword, "map") == 0) {
790 if (currmap->max_mappings == currmap->n_mappings) {
791 currmap->max_mappings = currmap->max_mappings * 2 + 1;
792 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
794 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
795 currmap->n_mappings++;
797 bb_error_msg("misplaced option \"%s\"", buf);
804 bb_error_msg("misplaced option \"%s\"", buf);
810 if (ferror(f) != 0) {
811 bb_perror_msg_and_die("%s", filename);
818 static char *setlocalenv(char *format, const char *name, const char *value)
824 result = xasprintf(format, name, value);
826 for (here = there = result; *there != '=' && *there; there++) {
830 *there = toupper(*there);
832 if (isalnum(*there) || *there == '_') {
837 memmove(here, there, strlen(there) + 1);
842 static void set_environ(struct interface_defn_t *iface, const char *mode)
846 const int n_env_entries = iface->n_options + 5;
849 if (__myenviron != NULL) {
850 for (ppch = __myenviron; *ppch; ppch++) {
856 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
857 environend = __myenviron;
859 for (i = 0; i < iface->n_options; i++) {
860 if (strcmp(iface->option[i].name, "up") == 0
861 || strcmp(iface->option[i].name, "down") == 0
862 || strcmp(iface->option[i].name, "pre-up") == 0
863 || strcmp(iface->option[i].name, "post-down") == 0) {
866 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
869 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
870 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
871 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
872 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
873 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
876 static int doit(char *str)
878 if (verbose || no_act) {
886 switch (child = fork()) {
887 case -1: /* failure */
890 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
893 waitpid(child, &status, 0);
894 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
901 static int execute_all(struct interface_defn_t *ifd, const char *opt)
905 for (i = 0; i < ifd->n_options; i++) {
906 if (strcmp(ifd->option[i].name, opt) == 0) {
907 if (!doit(ifd->option[i].value)) {
913 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
914 if (doit(buf) != 1) {
920 static int check(char *str) {
924 static int iface_up(struct interface_defn_t *iface)
926 if (!iface->method->up(iface,check)) return -1;
927 set_environ(iface, "start");
928 if (!execute_all(iface, "pre-up")) return 0;
929 if (!iface->method->up(iface, doit)) return 0;
930 if (!execute_all(iface, "up")) return 0;
934 static int iface_down(struct interface_defn_t *iface)
936 if (!iface->method->down(iface,check)) return -1;
937 set_environ(iface, "stop");
938 if (!execute_all(iface, "down")) return 0;
939 if (!iface->method->down(iface, doit)) return 0;
940 if (!execute_all(iface, "post-down")) return 0;
944 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
945 static int popen2(FILE **in, FILE **out, char *command, ...)
948 char *argv[11] = { command };
950 int infd[2], outfd[2];
954 va_start(ap, command);
955 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
958 argv[argc] = NULL; /* make sure */
961 if (pipe(infd) != 0) {
965 if (pipe(outfd) != 0) {
972 switch (pid = fork()) {
973 case -1: /* failure */
986 execvp(command, argv);
988 default: /* parent */
989 *in = fdopen(infd[1], "w");
990 *out = fdopen(outfd[0], "r");
998 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1004 char *logical = xstrdup(physical);
1006 /* Run the mapping script. */
1007 pid = popen2(&in, &out, map->script, physical, NULL);
1009 /* popen2() returns 0 on failure. */
1013 /* Write mappings to stdin of mapping script. */
1014 for (i = 0; i < map->n_mappings; i++) {
1015 fprintf(in, "%s\n", map->mapping[i]);
1018 waitpid(pid, &status, 0);
1020 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1021 /* If the mapping script exited successfully, try to
1022 * grab a line of output and use that as the name of the
1023 * logical interface. */
1024 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1026 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1027 /* If we are able to read a line of output from the script,
1028 * remove any trailing whitespace and use this value
1029 * as the name of the logical interface. */
1030 char *pch = new_logical + strlen(new_logical) - 1;
1032 while (pch >= new_logical && isspace(*pch))
1036 logical = new_logical;
1038 /* If we are UNABLE to read a line of output, discard our
1039 * freshly allocated memory. */
1048 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1050 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1052 unsigned short iface_len = strlen(iface);
1053 llist_t *search = state_list;
1056 if ((strncmp(search->data, iface, iface_len) == 0) &&
1057 (search->data[iface_len] == '=')) {
1060 search = search->link;
1065 int ifupdown_main(int argc, char **argv)
1067 int (*cmds) (struct interface_defn_t *) = NULL;
1068 struct interfaces_file_t *defn;
1069 llist_t *state_list = NULL;
1070 llist_t *target_list = NULL;
1071 const char *interfaces = "/etc/network/interfaces";
1072 const char *statefile = "/var/run/ifstate";
1074 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1075 int run_mappings = 1;
1079 int any_failures = 0;
1082 if (bb_applet_name[2] == 'u') {
1086 /* ifdown command */
1090 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1091 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1093 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1097 case 'i': /* interfaces */
1098 interfaces = optarg;
1100 case 'v': /* verbose */
1106 case 'n': /* no-act */
1109 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1110 case 'm': /* no-mappings */
1114 case 'f': /* force */
1123 if (argc - optind > 0) {
1133 debug_noise("reading %s file:\n", interfaces);
1134 defn = read_interfaces(interfaces);
1135 debug_noise("\ndone reading %s\n\n", interfaces);
1141 /* Create a list of interfaces to work on */
1143 if (cmds == iface_up) {
1144 target_list = defn->autointerfaces;
1147 const llist_t *list = state_list;
1149 llist_add_to_end(&target_list, xstrdup(list->data));
1152 target_list = defn->autointerfaces;
1155 llist_add_to_end(&target_list, argv[optind]);
1159 /* Update the interfaces */
1160 while (target_list) {
1161 llist_t *iface_list;
1162 struct interface_defn_t *currif;
1169 iface = xstrdup(target_list->data);
1170 target_list = target_list->link;
1172 pch = strchr(iface, '=');
1175 liface = xstrdup(pch + 1);
1177 liface = xstrdup(iface);
1181 const llist_t *iface_state = find_iface_state(state_list, iface);
1183 if (cmds == iface_up) {
1186 bb_error_msg("interface %s already configured", iface);
1192 bb_error_msg("interface %s not configured", iface);
1198 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1199 if ((cmds == iface_up) && run_mappings) {
1200 struct mapping_defn_t *currmap;
1202 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1204 for (i = 0; i < currmap->n_matches; i++) {
1205 if (fnmatch(currmap->match[i], liface, 0) != 0)
1208 printf("Running mapping script %s on %s\n", currmap->script, liface);
1210 liface = run_mapping(iface, currmap);
1218 iface_list = defn->ifaces;
1219 while (iface_list) {
1220 currif = (struct interface_defn_t *) iface_list->data;
1221 if (strcmp(liface, currif->iface) == 0) {
1222 char *oldiface = currif->iface;
1225 currif->iface = iface;
1227 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1229 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1230 cmds_ret = cmds(currif);
1231 if (cmds_ret == -1) {
1232 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1233 liface, currif->address_family->name);
1235 } else if (cmds_ret == 0) {
1239 currif->iface = oldiface;
1241 iface_list = iface_list->link;
1247 if (!okay && !force) {
1248 bb_error_msg("Ignoring unknown interface %s", liface);
1251 llist_t *iface_state = find_iface_state(state_list, iface);
1253 if (cmds == iface_up) {
1254 char *newiface = xasprintf("%s=%s", iface, liface);
1255 if (iface_state == NULL) {
1256 llist_add_to_end(&state_list, newiface);
1258 free(iface_state->data);
1259 iface_state->data = newiface;
1262 /* Remove an interface from the linked list */
1263 free(llist_pop(&iface_state));
1268 /* Actually write the new state */
1270 FILE *state_fp = NULL;
1272 state_fp = xfopen(statefile, "w");
1273 while (state_list) {
1274 if (state_list->data) {
1275 fputs(state_list->data, state_fp);
1276 fputc('\n', state_fp);
1278 state_list = state_list->link;