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 unsigned option_mask;
95 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
101 OPT_no_mappings = 0x10,
103 #define DO_ALL (option_mask & OPT_do_all)
104 #define NO_ACT (option_mask & OPT_no_act)
105 #define VERBOSE (option_mask & OPT_verbose)
106 #define FORCE (option_mask & OPT_force)
107 #define NO_MAPPINGS (option_mask & OPT_no_mappings)
109 static char **__myenviron = NULL;
111 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
113 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
115 static unsigned int count_bits(unsigned int a)
118 result = (a & 0x55) + ((a >> 1) & 0x55);
119 result = (result & 0x33) + ((result >> 2) & 0x33);
120 return((result & 0x0F) + ((result >> 4) & 0x0F));
123 static int count_netmask_bits(char *dotted_quad)
125 unsigned int result, a, b, c, d;
126 /* Found a netmask... Check if it is dotted quad */
127 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
129 result = count_bits(a);
130 result += count_bits(b);
131 result += count_bits(c);
132 result += count_bits(d);
133 return ((int)result);
137 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
139 if (*pos + str_length >= *len) {
142 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
144 *len = *len * 2 + str_length + 1;
147 while (str_length-- >= 1) {
148 (*buf)[(*pos)++] = *str;
154 static int strncmpz(char *l, char *r, size_t llen)
156 int i = strncmp(l, r, llen);
165 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
169 if (strncmpz(id, "iface", idlen) == 0) {
171 static char label_buf[20];
172 strncpy(label_buf, ifd->iface, 19);
174 result = strchr(label_buf, ':');
179 } else if (strncmpz(id, "label", idlen) == 0) {
182 for (i = 0; i < ifd->n_options; i++) {
183 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
184 return (ifd->option[i].value);
192 static char *parse(char *command, struct interface_defn_t *ifd)
196 size_t pos = 0, len = 0;
197 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
198 int okay[MAX_OPT_DEPTH] = { 1 };
205 addstr(&result, &len, &pos, command, 1);
210 addstr(&result, &len, &pos, command + 1, 1);
213 addstr(&result, &len, &pos, command, 1);
218 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
219 old_pos[opt_depth] = pos;
224 addstr(&result, &len, &pos, "[", 1);
229 if (command[1] == ']' && opt_depth > 1) {
231 if (!okay[opt_depth]) {
232 pos = old_pos[opt_depth];
237 addstr(&result, &len, &pos, "]", 1);
247 nextpercent = strchr(command, '%');
254 varvalue = get_var(command, nextpercent - command, ifd);
257 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
259 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
260 /* Sigh... Add a special case for 'ip' to convert from
261 * dotted quad to bit count style netmasks. */
262 if (strncmp(command, "bnmask", 6)==0) {
264 varvalue = get_var("netmask", 7, ifd);
265 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
267 sprintf(argument, "%d", res);
268 addstr(&result, &len, &pos, argument, strlen(argument));
269 command = nextpercent + 1;
274 okay[opt_depth - 1] = 0;
277 command = nextpercent + 1;
298 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
303 out = parse(command, ifd);
317 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
318 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
320 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
322 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
323 result += execute("ip link set %iface% up", ifd, exec);
324 return ((result == 2) ? 2 : 0);
326 return( execute("ifconfig %iface% add ::1", ifd, exec));
330 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
332 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
333 return(execute("ip link set %iface% down", ifd, exec));
335 return(execute("ifconfig %iface% del ::1", ifd, exec));
339 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
342 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
343 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
344 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
345 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
347 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
348 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
349 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
351 return ((result == 3) ? 3 : 0);
354 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
356 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
357 return(execute("ip link set %iface% down", ifd, exec));
359 return(execute("ifconfig %iface% down", ifd, exec));
363 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
364 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
367 result = execute("ip tunnel add %iface% mode sit remote "
368 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
369 result += execute("ip link set %iface% up", ifd, exec);
370 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
371 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
372 return ((result == 4) ? 4 : 0);
375 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
377 return( execute("ip tunnel del %iface%", ifd, exec));
381 static struct method_t methods6[] = {
382 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
383 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
385 { "static", static_up6, static_down6, },
386 { "loopback", loopback_up6, loopback_down6, },
389 static struct address_family_t addr_inet6 = {
391 sizeof(methods6) / sizeof(struct method_t),
394 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
396 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
397 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
399 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
401 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
402 result += execute("ip link set %iface% up", ifd, exec);
403 return ((result == 2) ? 2 : 0);
405 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
409 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
411 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
413 result = execute("ip addr flush dev %iface%", ifd, exec);
414 result += execute("ip link set %iface% down", ifd, exec);
415 return ((result == 2) ? 2 : 0);
417 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
421 static int static_up(struct interface_defn_t *ifd, execfn *exec)
424 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
425 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
426 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
427 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
428 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
429 return ((result == 3) ? 3 : 0);
431 result = execute("ifconfig %iface% %address% netmask %netmask% "
432 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
433 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
435 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
436 return ((result == 2) ? 2 : 0);
440 static int static_down(struct interface_defn_t *ifd, execfn *exec)
443 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
444 result = execute("ip addr flush dev %iface%", ifd, exec);
445 result += execute("ip link set %iface% down", ifd, exec);
447 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
448 result += execute("ifconfig %iface% down", ifd, exec);
450 return ((result == 2) ? 2 : 0);
453 static int execable(char *program)
456 if (0 == stat(program, &buf)) {
457 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
464 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
466 if (execable("/sbin/udhcpc")) {
467 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
468 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
469 } else if (execable("/sbin/pump")) {
470 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
471 } else if (execable("/sbin/dhclient")) {
472 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
473 } else if (execable("/sbin/dhcpcd")) {
474 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
475 "[[-l %leasetime%]] %iface%", ifd, exec));
480 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
483 if (execable("/sbin/udhcpc")) {
484 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
485 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
486 * sequentially so we don't need to sleep */
487 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
488 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
489 } else if (execable("/sbin/pump")) {
490 result = execute("pump -i %iface% -k", ifd, exec);
491 } else if (execable("/sbin/dhclient")) {
492 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
493 } else if (execable("/sbin/dhcpcd")) {
494 result = execute("dhcpcd -k %iface%", ifd, exec);
496 return (result || static_down(ifd, exec));
499 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
501 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
502 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
503 "--returniffail --serverbcast", ifd, exec));
506 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
508 return( execute("pon [[%provider%]]", ifd, exec));
511 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
513 return( execute("poff [[%provider%]]", ifd, exec));
516 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
518 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
519 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
522 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
524 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
525 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
528 static struct method_t methods[] =
530 { "wvdial", wvdial_up, wvdial_down, },
531 { "ppp", ppp_up, ppp_down, },
532 { "static", static_up, static_down, },
533 { "bootp", bootp_up, static_down, },
534 { "dhcp", dhcp_up, dhcp_down, },
535 { "loopback", loopback_up, loopback_down, },
538 static struct address_family_t addr_inet =
541 sizeof(methods) / sizeof(struct method_t),
545 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
547 static char *next_word(char **buf)
549 unsigned short length;
552 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
556 /* Skip over leading whitespace */
558 while (isspace(*word)) {
562 /* Skip over comments */
567 /* Find the length of this word */
568 length = strcspn(word, " \t\n");
572 *buf = word + length;
573 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
582 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
586 for (i = 0; af[i]; i++) {
587 if (strcmp(af[i]->name, name) == 0) {
594 static struct method_t *get_method(struct address_family_t *af, char *name)
598 for (i = 0; i < af->n_methods; i++) {
599 if (strcmp(af->method[i].name, name) == 0) {
600 return &af->method[i];
606 static const llist_t *find_list_string(const llist_t *list, const char *string)
609 if (strcmp(list->data, string) == 0) {
617 static struct interfaces_file_t *read_interfaces(const char *filename)
619 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
620 struct mapping_defn_t *currmap = NULL;
622 struct interface_defn_t *currif = NULL;
623 struct interfaces_file_t *defn;
628 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
630 defn = xzalloc(sizeof(struct interfaces_file_t));
632 f = xfopen(filename, "r");
634 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
637 firstword = next_word(&buf_ptr);
638 if (firstword == NULL) {
640 continue; /* blank line */
643 if (strcmp(firstword, "mapping") == 0) {
644 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
645 currmap = xzalloc(sizeof(struct mapping_defn_t));
647 while ((firstword = next_word(&buf_ptr)) != NULL) {
648 if (currmap->max_matches == currmap->n_matches) {
649 currmap->max_matches = currmap->max_matches * 2 + 1;
650 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
653 currmap->match[currmap->n_matches++] = xstrdup(firstword);
655 currmap->max_mappings = 0;
656 currmap->n_mappings = 0;
657 currmap->mapping = NULL;
658 currmap->script = NULL;
660 struct mapping_defn_t **where = &defn->mappings;
661 while (*where != NULL) {
662 where = &(*where)->next;
665 currmap->next = NULL;
667 debug_noise("Added mapping\n");
669 currently_processing = MAPPING;
670 } else if (strcmp(firstword, "iface") == 0) {
673 char *address_family_name;
675 struct address_family_t *addr_fams[] = {
676 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
679 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
685 currif = xzalloc(sizeof(struct interface_defn_t));
686 iface_name = next_word(&buf_ptr);
687 address_family_name = next_word(&buf_ptr);
688 method_name = next_word(&buf_ptr);
690 if (buf_ptr == NULL) {
691 bb_error_msg("too few parameters for line \"%s\"", buf);
695 /* ship any trailing whitespace */
696 while (isspace(*buf_ptr)) {
700 if (buf_ptr[0] != '\0') {
701 bb_error_msg("too many parameters \"%s\"", buf);
705 currif->iface = xstrdup(iface_name);
707 currif->address_family = get_address_family(addr_fams, address_family_name);
708 if (!currif->address_family) {
709 bb_error_msg("unknown address type \"%s\"", address_family_name);
713 currif->method = get_method(currif->address_family, method_name);
714 if (!currif->method) {
715 bb_error_msg("unknown method \"%s\"", method_name);
722 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
723 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
724 if ((strcmp(tmp->iface, currif->iface) == 0) &&
725 (tmp->address_family == currif->address_family)) {
726 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
731 llist_add_to_end(&(defn->ifaces), (char*)currif);
733 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
735 currently_processing = IFACE;
736 } else if (strcmp(firstword, "auto") == 0) {
737 while ((firstword = next_word(&buf_ptr)) != NULL) {
739 /* Check the interface isnt already listed */
740 if (find_list_string(defn->autointerfaces, firstword)) {
741 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
744 /* Add the interface to the list */
745 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
746 debug_noise("\nauto %s\n", firstword);
748 currently_processing = NONE;
750 switch (currently_processing) {
755 if (strlen(buf_ptr) == 0) {
756 bb_error_msg("option with empty value \"%s\"", buf);
760 if (strcmp(firstword, "up") != 0
761 && strcmp(firstword, "down") != 0
762 && strcmp(firstword, "pre-up") != 0
763 && strcmp(firstword, "post-down") != 0) {
764 for (i = 0; i < currif->n_options; i++) {
765 if (strcmp(currif->option[i].name, firstword) == 0) {
766 bb_error_msg("duplicate option \"%s\"", buf);
772 if (currif->n_options >= currif->max_options) {
773 struct variable_t *opt;
775 currif->max_options = currif->max_options + 10;
776 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
777 currif->option = opt;
779 currif->option[currif->n_options].name = xstrdup(firstword);
780 currif->option[currif->n_options].value = xstrdup(buf_ptr);
781 if (!currif->option[currif->n_options].name) {
785 if (!currif->option[currif->n_options].value) {
789 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
790 currif->option[currif->n_options].value);
794 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
795 if (strcmp(firstword, "script") == 0) {
796 if (currmap->script != NULL) {
797 bb_error_msg("duplicate script in mapping \"%s\"", buf);
800 currmap->script = xstrdup(next_word(&buf_ptr));
802 } else if (strcmp(firstword, "map") == 0) {
803 if (currmap->max_mappings == currmap->n_mappings) {
804 currmap->max_mappings = currmap->max_mappings * 2 + 1;
805 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
807 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
808 currmap->n_mappings++;
810 bb_error_msg("misplaced option \"%s\"", buf);
817 bb_error_msg("misplaced option \"%s\"", buf);
823 if (ferror(f) != 0) {
824 bb_perror_msg_and_die("%s", filename);
831 static char *setlocalenv(char *format, const char *name, const char *value)
837 result = xasprintf(format, name, value);
839 for (here = there = result; *there != '=' && *there; there++) {
843 *there = toupper(*there);
845 if (isalnum(*there) || *there == '_') {
850 memmove(here, there, strlen(there) + 1);
855 static void set_environ(struct interface_defn_t *iface, const char *mode)
859 const int n_env_entries = iface->n_options + 5;
862 if (__myenviron != NULL) {
863 for (ppch = __myenviron; *ppch; ppch++) {
869 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
870 environend = __myenviron;
872 for (i = 0; i < iface->n_options; i++) {
873 if (strcmp(iface->option[i].name, "up") == 0
874 || strcmp(iface->option[i].name, "down") == 0
875 || strcmp(iface->option[i].name, "pre-up") == 0
876 || strcmp(iface->option[i].name, "post-down") == 0) {
879 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
882 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
883 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
884 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
885 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
886 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
889 static int doit(char *str)
891 if (option_mask & (OPT_no_act|OPT_verbose)) {
894 if (!(option_mask & OPT_no_act)) {
899 switch (child = fork()) {
900 case -1: /* failure */
903 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
906 waitpid(child, &status, 0);
907 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
914 static int execute_all(struct interface_defn_t *ifd, const char *opt)
918 for (i = 0; i < ifd->n_options; i++) {
919 if (strcmp(ifd->option[i].name, opt) == 0) {
920 if (!doit(ifd->option[i].value)) {
926 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
927 if (doit(buf) != 1) {
933 static int check(char *str) {
937 static int iface_up(struct interface_defn_t *iface)
939 if (!iface->method->up(iface,check)) return -1;
940 set_environ(iface, "start");
941 if (!execute_all(iface, "pre-up")) return 0;
942 if (!iface->method->up(iface, doit)) return 0;
943 if (!execute_all(iface, "up")) return 0;
947 static int iface_down(struct interface_defn_t *iface)
949 if (!iface->method->down(iface,check)) return -1;
950 set_environ(iface, "stop");
951 if (!execute_all(iface, "down")) return 0;
952 if (!iface->method->down(iface, doit)) return 0;
953 if (!execute_all(iface, "post-down")) return 0;
957 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
958 static int popen2(FILE **in, FILE **out, char *command, ...)
961 char *argv[11] = { command };
963 int infd[2], outfd[2];
967 va_start(ap, command);
968 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
971 argv[argc] = NULL; /* make sure */
974 if (pipe(infd) != 0) {
978 if (pipe(outfd) != 0) {
985 switch (pid = fork()) {
986 case -1: /* failure */
999 execvp(command, argv);
1001 default: /* parent */
1002 *in = fdopen(infd[1], "w");
1003 *out = fdopen(outfd[0], "r");
1011 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1017 char *logical = xstrdup(physical);
1019 /* Run the mapping script. */
1020 pid = popen2(&in, &out, map->script, physical, NULL);
1022 /* popen2() returns 0 on failure. */
1026 /* Write mappings to stdin of mapping script. */
1027 for (i = 0; i < map->n_mappings; i++) {
1028 fprintf(in, "%s\n", map->mapping[i]);
1031 waitpid(pid, &status, 0);
1033 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1034 /* If the mapping script exited successfully, try to
1035 * grab a line of output and use that as the name of the
1036 * logical interface. */
1037 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1039 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1040 /* If we are able to read a line of output from the script,
1041 * remove any trailing whitespace and use this value
1042 * as the name of the logical interface. */
1043 char *pch = new_logical + strlen(new_logical) - 1;
1045 while (pch >= new_logical && isspace(*pch))
1049 logical = new_logical;
1051 /* If we are UNABLE to read a line of output, discard our
1052 * freshly allocated memory. */
1061 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1063 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1065 unsigned short iface_len = strlen(iface);
1066 llist_t *search = state_list;
1069 if ((strncmp(search->data, iface, iface_len) == 0) &&
1070 (search->data[iface_len] == '=')) {
1073 search = search->link;
1078 int ifupdown_main(int argc, char **argv)
1080 int (*cmds) (struct interface_defn_t *) = NULL;
1081 struct interfaces_file_t *defn;
1082 llist_t *state_list = NULL;
1083 llist_t *target_list = NULL;
1084 const char *interfaces = "/etc/network/interfaces";
1085 const char *statefile = "/var/run/ifstate";
1086 int any_failures = 0;
1089 if (bb_applet_name[2] == 'u') {
1093 /* ifdown command */
1097 option_mask = bb_getopt_ulflags(argc, argv, OPTION_STR, &interfaces);
1098 if (argc - optind > 0) {
1099 if (DO_ALL) bb_show_usage();
1101 if (!DO_ALL) bb_show_usage();
1103 debug_noise("reading %s file:\n", interfaces);
1104 defn = read_interfaces(interfaces);
1105 debug_noise("\ndone reading %s\n\n", interfaces);
1111 /* Create a list of interfaces to work on */
1113 if (cmds == iface_up) {
1114 target_list = defn->autointerfaces;
1117 const llist_t *list = state_list;
1119 llist_add_to_end(&target_list, xstrdup(list->data));
1122 target_list = defn->autointerfaces;
1125 llist_add_to_end(&target_list, argv[optind]);
1129 /* Update the interfaces */
1130 while (target_list) {
1131 llist_t *iface_list;
1132 struct interface_defn_t *currif;
1139 iface = xstrdup(target_list->data);
1140 target_list = target_list->link;
1142 pch = strchr(iface, '=');
1145 liface = xstrdup(pch + 1);
1147 liface = xstrdup(iface);
1151 const llist_t *iface_state = find_iface_state(state_list, iface);
1153 if (cmds == iface_up) {
1156 bb_error_msg("interface %s already configured", iface);
1162 bb_error_msg("interface %s not configured", iface);
1168 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1169 if ((cmds == iface_up) && !NO_MAPPINGS) {
1170 struct mapping_defn_t *currmap;
1172 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1174 for (i = 0; i < currmap->n_matches; i++) {
1175 if (fnmatch(currmap->match[i], liface, 0) != 0)
1178 printf("Running mapping script %s on %s\n", currmap->script, liface);
1180 liface = run_mapping(iface, currmap);
1188 iface_list = defn->ifaces;
1189 while (iface_list) {
1190 currif = (struct interface_defn_t *) iface_list->data;
1191 if (strcmp(liface, currif->iface) == 0) {
1192 char *oldiface = currif->iface;
1195 currif->iface = iface;
1197 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1199 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1200 cmds_ret = cmds(currif);
1201 if (cmds_ret == -1) {
1202 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1203 liface, currif->address_family->name);
1205 } else if (cmds_ret == 0) {
1209 currif->iface = oldiface;
1211 iface_list = iface_list->link;
1217 if (!okay && !FORCE) {
1218 bb_error_msg("Ignoring unknown interface %s", liface);
1221 llist_t *iface_state = find_iface_state(state_list, iface);
1223 if (cmds == iface_up) {
1224 char *newiface = xasprintf("%s=%s", iface, liface);
1225 if (iface_state == NULL) {
1226 llist_add_to_end(&state_list, newiface);
1228 free(iface_state->data);
1229 iface_state->data = newiface;
1232 /* Remove an interface from the linked list */
1233 free(llist_pop(&iface_state));
1238 /* Actually write the new state */
1240 FILE *state_fp = NULL;
1242 state_fp = xfopen(statefile, "w");
1243 while (state_list) {
1244 if (state_list->data) {
1245 fputs(state_list->data, state_fp);
1246 fputc('\n', state_fp);
1248 state_list = state_list->link;