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>
37 #define MAX_OPT_DEPTH 10
38 #define EUNBALBRACK 10001
39 #define EUNDEFVAR 10002
40 #define EUNBALPER 10000
42 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43 #define MAX_INTERFACE_LENGTH 10
47 #define debug_noise(fmt, args...) printf(fmt, ## args)
49 #define debug_noise(fmt, args...)
52 /* Forward declaration */
53 struct interface_defn_t;
55 typedef int (execfn)(char *command);
60 int (*up)(struct interface_defn_t *ifd, execfn *e);
61 int (*down)(struct interface_defn_t *ifd, execfn *e);
64 struct address_family_t
68 struct method_t *method;
73 struct mapping_defn_t *next;
92 struct interface_defn_t
94 struct address_family_t *address_family;
95 struct method_t *method;
100 struct variable_t *option;
103 struct interfaces_file_t
105 llist_t *autointerfaces;
107 struct mapping_defn_t *mappings;
110 static char no_act = 0;
111 static char verbose = 0;
112 static char **__myenviron = NULL;
114 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
116 static unsigned int count_bits(unsigned int a)
119 result = (a & 0x55) + ((a >> 1) & 0x55);
120 result = (result & 0x33) + ((result >> 2) & 0x33);
121 return((result & 0x0F) + ((result >> 4) & 0x0F));
124 static int count_netmask_bits(char *dotted_quad)
126 unsigned int result, a, b, c, d;
127 /* Found a netmask... Check if it is dotted quad */
128 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
130 result = count_bits(a);
131 result += count_bits(b);
132 result += count_bits(c);
133 result += count_bits(d);
134 return ((int)result);
138 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
139 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
141 if (*pos + str_length >= *len) {
144 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
146 *len = *len * 2 + str_length + 1;
149 while (str_length-- >= 1) {
150 (*buf)[(*pos)++] = *str;
156 static int strncmpz(char *l, char *r, size_t llen)
158 int i = strncmp(l, r, llen);
167 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
171 if (strncmpz(id, "iface", idlen) == 0) {
173 static char label_buf[20];
174 strncpy(label_buf, ifd->iface, 19);
176 result = strchr(label_buf, ':');
181 } else if (strncmpz(id, "label", idlen) == 0) {
184 for (i = 0; i < ifd->n_options; i++) {
185 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
186 return (ifd->option[i].value);
194 static char *parse(char *command, struct interface_defn_t *ifd)
198 size_t pos = 0, len = 0;
199 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
200 int okay[MAX_OPT_DEPTH] = { 1 };
207 addstr(&result, &len, &pos, command, 1);
212 addstr(&result, &len, &pos, command + 1, 1);
215 addstr(&result, &len, &pos, command, 1);
220 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
221 old_pos[opt_depth] = pos;
226 addstr(&result, &len, &pos, "[", 1);
231 if (command[1] == ']' && opt_depth > 1) {
233 if (!okay[opt_depth]) {
234 pos = old_pos[opt_depth];
239 addstr(&result, &len, &pos, "]", 1);
249 nextpercent = strchr(command, '%');
256 varvalue = get_var(command, nextpercent - command, ifd);
259 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
261 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
262 /* Sigh... Add a special case for 'ip' to convert from
263 * dotted quad to bit count style netmasks. */
264 if (strncmp(command, "bnmask", 6)==0) {
266 varvalue = get_var("netmask", 7, ifd);
267 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
269 sprintf(argument, "%d", res);
270 addstr(&result, &len, &pos, argument, strlen(argument));
271 command = nextpercent + 1;
276 okay[opt_depth - 1] = 0;
279 command = nextpercent + 1;
300 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
305 out = parse(command, ifd);
319 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
320 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
322 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
324 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
325 result += execute("ip link set %iface% up", ifd, exec);
326 return ((result == 2) ? 2 : 0);
328 return( execute("ifconfig %iface% add ::1", ifd, exec));
332 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
334 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
335 return(execute("ip link set %iface% down", ifd, exec));
337 return(execute("ifconfig %iface% del ::1", ifd, exec));
341 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
344 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
345 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
346 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
347 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
349 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
350 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
351 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
353 return ((result == 3) ? 3 : 0);
356 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
358 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
359 return(execute("ip link set %iface% down", ifd, exec));
361 return(execute("ifconfig %iface% down", ifd, exec));
365 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
366 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
369 result = execute("ip tunnel add %iface% mode sit remote "
370 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
371 result += execute("ip link set %iface% up", ifd, exec);
372 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
373 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
374 return ((result == 4) ? 4 : 0);
377 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
379 return( execute("ip tunnel del %iface%", ifd, exec));
383 static struct method_t methods6[] = {
384 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
385 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
387 { "static", static_up6, static_down6, },
388 { "loopback", loopback_up6, loopback_down6, },
391 static struct address_family_t addr_inet6 = {
393 sizeof(methods6) / sizeof(struct method_t),
396 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
398 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
399 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
401 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
403 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
404 result += execute("ip link set %iface% up", ifd, exec);
405 return ((result == 2) ? 2 : 0);
407 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
411 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
413 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
415 result = execute("ip addr flush dev %iface%", ifd, exec);
416 result += execute("ip link set %iface% down", ifd, exec);
417 return ((result == 2) ? 2 : 0);
419 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
423 static int static_up(struct interface_defn_t *ifd, execfn *exec)
426 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
427 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
428 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
429 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
430 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
431 return ((result == 3) ? 3 : 0);
433 result = execute("ifconfig %iface% %address% netmask %netmask% "
434 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
435 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
437 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
438 return ((result == 2) ? 2 : 0);
442 static int static_down(struct interface_defn_t *ifd, execfn *exec)
445 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
446 result = execute("ip addr flush dev %iface%", ifd, exec);
447 result += execute("ip link set %iface% down", ifd, exec);
449 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
450 result += execute("ifconfig %iface% down", ifd, exec);
452 return ((result == 2) ? 2 : 0);
455 static int execable(char *program)
458 if (0 == stat(program, &buf)) {
459 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
466 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
468 if (execable("/sbin/udhcpc")) {
469 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
470 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
471 } else if (execable("/sbin/pump")) {
472 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
473 } else if (execable("/sbin/dhclient")) {
474 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
475 } else if (execable("/sbin/dhcpcd")) {
476 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
477 "[[-l %leasetime%]] %iface%", ifd, exec));
482 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
485 if (execable("/sbin/udhcpc")) {
486 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
487 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
488 * sequentially so we don't need to sleep */
489 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
490 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
491 } else if (execable("/sbin/pump")) {
492 result = execute("pump -i %iface% -k", ifd, exec);
493 } else if (execable("/sbin/dhclient")) {
494 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
495 } else if (execable("/sbin/dhcpcd")) {
496 result = execute("dhcpcd -k %iface%", ifd, exec);
498 return (result || static_down(ifd, exec));
501 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
503 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
504 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
505 "--returniffail --serverbcast", ifd, exec));
508 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
510 return( execute("pon [[%provider%]]", ifd, exec));
513 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
515 return( execute("poff [[%provider%]]", ifd, exec));
518 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
520 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
521 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
524 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
526 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
527 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
530 static struct method_t methods[] =
532 { "wvdial", wvdial_up, wvdial_down, },
533 { "ppp", ppp_up, ppp_down, },
534 { "static", static_up, static_down, },
535 { "bootp", bootp_up, static_down, },
536 { "dhcp", dhcp_up, dhcp_down, },
537 { "loopback", loopback_up, loopback_down, },
540 static struct address_family_t addr_inet =
543 sizeof(methods) / sizeof(struct method_t),
547 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
549 static char *next_word(char **buf)
551 unsigned short length;
554 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
558 /* Skip over leading whitespace */
560 while (isspace(*word)) {
564 /* Skip over comments */
569 /* Find the length of this word */
570 length = strcspn(word, " \t\n");
574 *buf = word + length;
575 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
584 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
588 for (i = 0; af[i]; i++) {
589 if (strcmp(af[i]->name, name) == 0) {
596 static struct method_t *get_method(struct address_family_t *af, char *name)
600 for (i = 0; i < af->n_methods; i++) {
601 if (strcmp(af->method[i].name, name) == 0) {
602 return &af->method[i];
608 static const llist_t *find_list_string(const llist_t *list, const char *string)
611 if (strcmp(list->data, string) == 0) {
619 static struct interfaces_file_t *read_interfaces(const char *filename)
621 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
622 struct mapping_defn_t *currmap = NULL;
624 struct interface_defn_t *currif = NULL;
625 struct interfaces_file_t *defn;
630 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
632 defn = xmalloc(sizeof(struct interfaces_file_t));
633 defn->autointerfaces = NULL;
634 defn->mappings = NULL;
637 f = bb_xfopen(filename, "r");
639 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
642 firstword = next_word(&buf_ptr);
643 if (firstword == NULL) {
645 continue; /* blank line */
648 if (strcmp(firstword, "mapping") == 0) {
649 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
650 currmap = xmalloc(sizeof(struct mapping_defn_t));
651 currmap->max_matches = 0;
652 currmap->n_matches = 0;
653 currmap->match = NULL;
655 while ((firstword = next_word(&buf_ptr)) != NULL) {
656 if (currmap->max_matches == currmap->n_matches) {
657 currmap->max_matches = currmap->max_matches * 2 + 1;
658 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
661 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
663 currmap->max_mappings = 0;
664 currmap->n_mappings = 0;
665 currmap->mapping = NULL;
666 currmap->script = NULL;
668 struct mapping_defn_t **where = &defn->mappings;
669 while (*where != NULL) {
670 where = &(*where)->next;
673 currmap->next = NULL;
675 debug_noise("Added mapping\n");
677 currently_processing = MAPPING;
678 } else if (strcmp(firstword, "iface") == 0) {
681 char *address_family_name;
683 struct address_family_t *addr_fams[] = {
684 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
687 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
693 currif = xmalloc(sizeof(struct interface_defn_t));
694 iface_name = next_word(&buf_ptr);
695 address_family_name = next_word(&buf_ptr);
696 method_name = next_word(&buf_ptr);
698 if (buf_ptr == NULL) {
699 bb_error_msg("too few parameters for line \"%s\"", buf);
703 /* ship any trailing whitespace */
704 while (isspace(*buf_ptr)) {
708 if (buf_ptr[0] != '\0') {
709 bb_error_msg("too many parameters \"%s\"", buf);
713 currif->iface = bb_xstrdup(iface_name);
715 currif->address_family = get_address_family(addr_fams, address_family_name);
716 if (!currif->address_family) {
717 bb_error_msg("unknown address type \"%s\"", address_family_name);
721 currif->method = get_method(currif->address_family, method_name);
722 if (!currif->method) {
723 bb_error_msg("unknown method \"%s\"", method_name);
727 currif->max_options = 0;
728 currif->n_options = 0;
729 currif->option = NULL;
733 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
734 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
735 if ((strcmp(tmp->iface, currif->iface) == 0) &&
736 (tmp->address_family == currif->address_family)) {
737 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
742 llist_add_to_end(&(defn->ifaces), (char*)currif);
744 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
746 currently_processing = IFACE;
747 } else if (strcmp(firstword, "auto") == 0) {
748 while ((firstword = next_word(&buf_ptr)) != NULL) {
750 /* Check the interface isnt already listed */
751 if (find_list_string(defn->autointerfaces, firstword)) {
752 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
755 /* Add the interface to the list */
756 llist_add_to_end(&(defn->autointerfaces), bb_xstrdup(firstword));
757 debug_noise("\nauto %s\n", firstword);
759 currently_processing = NONE;
761 switch (currently_processing) {
766 if (strlen(buf_ptr) == 0) {
767 bb_error_msg("option with empty value \"%s\"", buf);
771 if (strcmp(firstword, "up") != 0
772 && strcmp(firstword, "down") != 0
773 && strcmp(firstword, "pre-up") != 0
774 && strcmp(firstword, "post-down") != 0) {
775 for (i = 0; i < currif->n_options; i++) {
776 if (strcmp(currif->option[i].name, firstword) == 0) {
777 bb_error_msg("duplicate option \"%s\"", buf);
783 if (currif->n_options >= currif->max_options) {
784 struct variable_t *opt;
786 currif->max_options = currif->max_options + 10;
787 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
788 currif->option = opt;
790 currif->option[currif->n_options].name = bb_xstrdup(firstword);
791 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
792 if (!currif->option[currif->n_options].name) {
796 if (!currif->option[currif->n_options].value) {
800 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
801 currif->option[currif->n_options].value);
805 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
806 if (strcmp(firstword, "script") == 0) {
807 if (currmap->script != NULL) {
808 bb_error_msg("duplicate script in mapping \"%s\"", buf);
811 currmap->script = bb_xstrdup(next_word(&buf_ptr));
813 } else if (strcmp(firstword, "map") == 0) {
814 if (currmap->max_mappings == currmap->n_mappings) {
815 currmap->max_mappings = currmap->max_mappings * 2 + 1;
816 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
818 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
819 currmap->n_mappings++;
821 bb_error_msg("misplaced option \"%s\"", buf);
828 bb_error_msg("misplaced option \"%s\"", buf);
834 if (ferror(f) != 0) {
835 bb_perror_msg_and_die("%s", filename);
842 static char *setlocalenv(char *format, const char *name, const char *value)
848 result = xmalloc(strlen(format) + strlen(name) + strlen(value) + 1);
850 sprintf(result, format, name, value);
852 for (here = there = result; *there != '=' && *there; there++) {
856 *there = toupper(*there);
858 if (isalnum(*there) || *there == '_') {
863 memmove(here, there, strlen(there) + 1);
868 static void set_environ(struct interface_defn_t *iface, const char *mode)
872 const int n_env_entries = iface->n_options + 5;
875 if (__myenviron != NULL) {
876 for (ppch = __myenviron; *ppch; ppch++) {
883 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
884 environend = __myenviron;
887 for (i = 0; i < iface->n_options; i++) {
888 if (strcmp(iface->option[i].name, "up") == 0
889 || strcmp(iface->option[i].name, "down") == 0
890 || strcmp(iface->option[i].name, "pre-up") == 0
891 || strcmp(iface->option[i].name, "post-down") == 0) {
894 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
898 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
900 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
902 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
904 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
906 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
910 static int doit(char *str)
912 if (verbose || no_act) {
920 switch (child = fork()) {
921 case -1: /* failure */
924 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
927 waitpid(child, &status, 0);
928 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
935 static int execute_all(struct interface_defn_t *ifd, const char *opt)
939 for (i = 0; i < ifd->n_options; i++) {
940 if (strcmp(ifd->option[i].name, opt) == 0) {
941 if (!doit(ifd->option[i].value)) {
947 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
948 if (doit(buf) != 1) {
954 static int check(char *str) {
958 static int iface_up(struct interface_defn_t *iface)
960 if (!iface->method->up(iface,check)) return -1;
961 set_environ(iface, "start");
962 if (!execute_all(iface, "pre-up")) return 0;
963 if (!iface->method->up(iface, doit)) return 0;
964 if (!execute_all(iface, "up")) return 0;
968 static int iface_down(struct interface_defn_t *iface)
970 if (!iface->method->down(iface,check)) return -1;
971 set_environ(iface, "stop");
972 if (!execute_all(iface, "down")) return 0;
973 if (!iface->method->down(iface, doit)) return 0;
974 if (!execute_all(iface, "post-down")) return 0;
978 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
979 static int popen2(FILE **in, FILE **out, char *command, ...)
982 char *argv[11] = { command };
984 int infd[2], outfd[2];
988 va_start(ap, command);
989 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
992 argv[argc] = NULL; /* make sure */
995 if (pipe(infd) != 0) {
999 if (pipe(outfd) != 0) {
1006 switch (pid = fork()) {
1007 case -1: /* failure */
1020 execvp(command, argv);
1022 default: /* parent */
1023 *in = fdopen(infd[1], "w");
1024 *out = fdopen(outfd[0], "r");
1032 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1038 char *logical = bb_xstrdup(physical);
1040 /* Run the mapping script. */
1041 pid = popen2(&in, &out, map->script, physical, NULL);
1043 /* popen2() returns 0 on failure. */
1047 /* Write mappings to stdin of mapping script. */
1048 for (i = 0; i < map->n_mappings; i++) {
1049 fprintf(in, "%s\n", map->mapping[i]);
1052 waitpid(pid, &status, 0);
1054 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1055 /* If the mapping script exited successfully, try to
1056 * grab a line of output and use that as the name of the
1057 * logical interface. */
1058 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1060 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1061 /* If we are able to read a line of output from the script,
1062 * remove any trailing whitespace and use this value
1063 * as the name of the logical interface. */
1064 char *pch = new_logical + strlen(new_logical) - 1;
1066 while (pch >= new_logical && isspace(*pch))
1070 logical = new_logical;
1072 /* If we are UNABLE to read a line of output, discard are
1073 * freshly allocated memory. */
1082 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1084 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1086 unsigned short iface_len = strlen(iface);
1087 llist_t *search = state_list;
1090 if ((strncmp(search->data, iface, iface_len) == 0) &&
1091 (search->data[iface_len] == '=')) {
1094 search = search->link;
1099 int ifupdown_main(int argc, char **argv)
1101 int (*cmds) (struct interface_defn_t *) = NULL;
1102 struct interfaces_file_t *defn;
1103 llist_t *state_list = NULL;
1104 llist_t *target_list = NULL;
1105 const char *interfaces = "/etc/network/interfaces";
1106 const char *statefile = "/var/run/ifstate";
1108 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1109 int run_mappings = 1;
1113 int any_failures = 0;
1116 if (bb_applet_name[2] == 'u') {
1120 /* ifdown command */
1124 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1125 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1127 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1131 case 'i': /* interfaces */
1132 interfaces = optarg;
1134 case 'v': /* verbose */
1140 case 'n': /* no-act */
1143 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1144 case 'm': /* no-mappings */
1148 case 'f': /* force */
1157 if (argc - optind > 0) {
1167 debug_noise("reading %s file:\n", interfaces);
1168 defn = read_interfaces(interfaces);
1169 debug_noise("\ndone reading %s\n\n", interfaces);
1175 /* Create a list of interfaces to work on */
1177 if (cmds == iface_up) {
1178 target_list = defn->autointerfaces;
1183 const llist_t *list = state_list;
1185 new_item = xmalloc(sizeof(llist_t));
1186 new_item->data = bb_xstrdup(list->data);
1187 new_item->link = NULL;
1190 target_list = new_item;
1192 while (list->link) {
1199 target_list = defn->autointerfaces;
1203 const llist_t *list = state_list;
1205 llist_add_to_end(&target_list, bb_xstrdup(list->data));
1208 target_list = defn->autointerfaces;
1212 llist_add_to_end(&target_list, argv[optind]);
1216 /* Update the interfaces */
1217 while (target_list) {
1218 llist_t *iface_list;
1219 struct interface_defn_t *currif;
1226 iface = bb_xstrdup(target_list->data);
1227 target_list = target_list->link;
1229 pch = strchr(iface, '=');
1232 liface = bb_xstrdup(pch + 1);
1234 liface = bb_xstrdup(iface);
1238 const llist_t *iface_state = find_iface_state(state_list, iface);
1240 if (cmds == iface_up) {
1243 bb_error_msg("interface %s already configured", iface);
1249 bb_error_msg("interface %s not configured", iface);
1255 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1256 if ((cmds == iface_up) && run_mappings) {
1257 struct mapping_defn_t *currmap;
1259 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1261 for (i = 0; i < currmap->n_matches; i++) {
1262 if (fnmatch(currmap->match[i], liface, 0) != 0)
1265 printf("Running mapping script %s on %s\n", currmap->script, liface);
1267 liface = run_mapping(iface, currmap);
1275 iface_list = defn->ifaces;
1276 while (iface_list) {
1277 currif = (struct interface_defn_t *) iface_list->data;
1278 if (strcmp(liface, currif->iface) == 0) {
1279 char *oldiface = currif->iface;
1282 currif->iface = iface;
1284 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1286 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1287 cmds_ret = cmds(currif);
1288 if (cmds_ret == -1) {
1289 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1290 liface, currif->address_family->name);
1292 } else if (cmds_ret == 0) {
1296 currif->iface = oldiface;
1298 iface_list = iface_list->link;
1304 if (!okay && !force) {
1305 bb_error_msg("Ignoring unknown interface %s", liface);
1308 llist_t *iface_state = find_iface_state(state_list, iface);
1310 if (cmds == iface_up) {
1311 char *newiface = xmalloc(strlen(iface) + 1 + strlen(liface) + 1);
1312 sprintf(newiface, "%s=%s", iface, liface);
1313 if (iface_state == NULL) {
1314 llist_add_to_end(&state_list, newiface);
1316 free(iface_state->data);
1317 iface_state->data = newiface;
1320 /* Remove an interface from the linked list */
1322 llist_t *l = iface_state->link;
1323 free(iface_state->data);
1324 iface_state->data = NULL;
1325 iface_state->link = NULL;
1327 iface_state->data = l->data;
1328 iface_state->link = l->link;
1336 /* Actually write the new state */
1338 FILE *state_fp = NULL;
1340 state_fp = bb_xfopen(statefile, "w");
1341 while (state_list) {
1342 if (state_list->data) {
1343 fputs(state_list->data, state_fp);
1344 fputc('\n', state_fp);
1346 state_list = state_list->link;