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.
19 #include <sys/utsname.h>
23 #define MAX_OPT_DEPTH 10
24 #define EUNBALBRACK 10001
25 #define EUNDEFVAR 10002
26 #define EUNBALPER 10000
28 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
29 #define MAX_INTERFACE_LENGTH 10
32 #define debug_noise(fmt, args...)
34 /* Forward declaration */
35 struct interface_defn_t;
37 typedef int (execfn)(char *command);
42 int (*up)(struct interface_defn_t *ifd, execfn *e);
43 int (*down)(struct interface_defn_t *ifd, execfn *e);
46 struct address_family_t
50 const struct method_t *method;
55 struct mapping_defn_t *next;
74 struct interface_defn_t
76 const struct address_family_t *address_family;
77 const struct method_t *method;
82 struct variable_t *option;
85 struct interfaces_file_t
87 llist_t *autointerfaces;
89 struct mapping_defn_t *mappings;
92 static unsigned option_mask;
93 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
99 OPT_no_mappings = 0x10,
101 #define DO_ALL (option_mask & OPT_do_all)
102 #define NO_ACT (option_mask & OPT_no_act)
103 #define VERBOSE (option_mask & OPT_verbose)
104 #define FORCE (option_mask & OPT_force)
105 #define NO_MAPPINGS (option_mask & OPT_no_mappings)
107 static char **__myenviron;
109 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
111 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
113 static unsigned int count_bits(unsigned int a)
116 result = (a & 0x55) + ((a >> 1) & 0x55);
117 result = (result & 0x33) + ((result >> 2) & 0x33);
118 return ((result & 0x0F) + ((result >> 4) & 0x0F));
121 static int count_netmask_bits(char *dotted_quad)
123 unsigned int result, a, b, c, d;
124 /* Found a netmask... Check if it is dotted quad */
125 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
127 result = count_bits(a);
128 result += count_bits(b);
129 result += count_bits(c);
130 result += count_bits(d);
135 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
137 if (*pos + str_length >= *len) {
140 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
142 *len = *len * 2 + str_length + 1;
145 while (str_length-- >= 1) {
146 (*buf)[(*pos)++] = *str;
152 static int strncmpz(char *l, char *r, size_t llen)
154 int i = strncmp(l, r, llen);
163 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
167 if (strncmpz(id, "iface", idlen) == 0) {
169 static char label_buf[20];
170 strncpy(label_buf, ifd->iface, 19);
172 result = strchr(label_buf, ':');
177 } else if (strncmpz(id, "label", idlen) == 0) {
180 for (i = 0; i < ifd->n_options; i++) {
181 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
182 return ifd->option[i].value;
190 static char *parse(char *command, struct interface_defn_t *ifd)
194 size_t pos = 0, len = 0;
195 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
196 int okay[MAX_OPT_DEPTH] = { 1 };
202 addstr(&result, &len, &pos, command, 1);
207 addstr(&result, &len, &pos, command + 1, 1);
210 addstr(&result, &len, &pos, command, 1);
215 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
216 old_pos[opt_depth] = pos;
221 addstr(&result, &len, &pos, "[", 1);
226 if (command[1] == ']' && opt_depth > 1) {
228 if (!okay[opt_depth]) {
229 pos = old_pos[opt_depth];
234 addstr(&result, &len, &pos, "]", 1);
244 nextpercent = strchr(command, '%');
251 varvalue = get_var(command, nextpercent - command, ifd);
254 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
256 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
257 /* Sigh... Add a special case for 'ip' to convert from
258 * dotted quad to bit count style netmasks. */
259 if (strncmp(command, "bnmask", 6)==0) {
261 varvalue = get_var("netmask", 7, ifd);
262 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
264 sprintf(argument, "%d", res);
265 addstr(&result, &len, &pos, argument, strlen(argument));
266 command = nextpercent + 1;
271 okay[opt_depth - 1] = 0;
274 command = nextpercent + 1;
295 /* execute() returns 1 for success and 0 for failure */
296 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
301 out = parse(command, ifd);
315 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
316 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
318 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
320 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
321 result += execute("ip link set %iface% up", ifd, exec);
322 return ((result == 2) ? 2 : 0);
324 return execute("ifconfig %iface% add ::1", ifd, exec);
328 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
330 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
331 return execute("ip link set %iface% down", ifd, exec);
333 return execute("ifconfig %iface% del ::1", ifd, exec);
337 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
340 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
341 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
342 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
343 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
345 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
346 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
347 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
349 return ((result == 3) ? 3 : 0);
352 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
354 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
355 return execute("ip link set %iface% down", ifd, exec);
357 return execute("ifconfig %iface% down", ifd, exec);
361 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
362 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
365 result = execute("ip tunnel add %iface% mode sit remote "
366 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
367 result += execute("ip link set %iface% up", ifd, exec);
368 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
369 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
370 return ((result == 4) ? 4 : 0);
373 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
375 return execute("ip tunnel del %iface%", ifd, exec);
379 static const struct method_t methods6[] = {
380 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
381 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
383 { "static", static_up6, static_down6, },
384 { "loopback", loopback_up6, loopback_down6, },
387 static const struct address_family_t addr_inet6 = {
389 sizeof(methods6) / sizeof(struct method_t),
392 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
394 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
395 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
397 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
399 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
400 result += execute("ip link set %iface% up", ifd, exec);
401 return ((result == 2) ? 2 : 0);
403 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
407 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
409 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
411 result = execute("ip addr flush dev %iface%", ifd, exec);
412 result += execute("ip link set %iface% down", ifd, exec);
413 return ((result == 2) ? 2 : 0);
415 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
419 static int static_up(struct interface_defn_t *ifd, execfn *exec)
422 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
423 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
424 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
425 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
426 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
427 return ((result == 3) ? 3 : 0);
429 result = execute("ifconfig %iface% %address% netmask %netmask% "
430 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
431 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
433 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
434 return ((result == 2) ? 2 : 0);
438 static int static_down(struct interface_defn_t *ifd, execfn *exec)
441 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
442 result = execute("ip addr flush dev %iface%", ifd, exec);
443 result += execute("ip link set %iface% down", ifd, exec);
445 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
446 result += execute("ifconfig %iface% down", ifd, exec);
448 return ((result == 2) ? 2 : 0);
451 static int execable(char *program)
454 if (0 == stat(program, &buf)) {
455 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
462 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
464 if (execable("/sbin/udhcpc")) {
465 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
466 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
467 } else if (execable("/sbin/pump")) {
468 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
469 } else if (execable("/sbin/dhclient")) {
470 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
471 } else if (execable("/sbin/dhcpcd")) {
472 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
473 "[[-l %leasetime%]] %iface%", ifd, exec));
478 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
481 if (execable("/sbin/udhcpc")) {
482 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
483 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
484 * sequentially so we don't need to sleep */
485 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
486 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
487 } else if (execable("/sbin/pump")) {
488 result = execute("pump -i %iface% -k", ifd, exec);
489 } else if (execable("/sbin/dhclient")) {
490 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
491 } else if (execable("/sbin/dhcpcd")) {
492 result = execute("dhcpcd -k %iface%", ifd, exec);
494 return (result || static_down(ifd, exec));
497 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
499 return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
500 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
501 "--returniffail --serverbcast", ifd, exec);
504 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
506 return execute("pon [[%provider%]]", ifd, exec);
509 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
511 return execute("poff [[%provider%]]", ifd, exec);
514 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
516 return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
517 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec);
520 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
522 return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
523 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
526 static const struct method_t methods[] = {
527 { "wvdial", wvdial_up, wvdial_down, },
528 { "ppp", ppp_up, ppp_down, },
529 { "static", static_up, static_down, },
530 { "bootp", bootp_up, static_down, },
531 { "dhcp", dhcp_up, dhcp_down, },
532 { "loopback", loopback_up, loopback_down, },
535 static const struct address_family_t addr_inet = {
537 sizeof(methods) / sizeof(struct method_t),
541 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
543 static char *next_word(char **buf)
545 unsigned short length;
548 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
552 /* Skip over leading whitespace */
554 while (isspace(*word)) {
558 /* Skip over comments */
563 /* Find the length of this word */
564 length = strcspn(word, " \t\n");
568 *buf = word + length;
569 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
578 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
582 for (i = 0; af[i]; i++) {
583 if (strcmp(af[i]->name, name) == 0) {
590 static const struct method_t *get_method(const struct address_family_t *af, char *name)
594 for (i = 0; i < af->n_methods; i++) {
595 if (strcmp(af->method[i].name, name) == 0) {
596 return &af->method[i];
602 static const llist_t *find_list_string(const llist_t *list, const char *string)
605 if (strcmp(list->data, string) == 0) {
613 static struct interfaces_file_t *read_interfaces(const char *filename)
615 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
616 struct mapping_defn_t *currmap = NULL;
618 struct interface_defn_t *currif = NULL;
619 struct interfaces_file_t *defn;
624 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
626 defn = xzalloc(sizeof(struct interfaces_file_t));
628 f = xfopen(filename, "r");
630 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
633 firstword = next_word(&buf_ptr);
634 if (firstword == NULL) {
636 continue; /* blank line */
639 if (strcmp(firstword, "mapping") == 0) {
640 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
641 currmap = xzalloc(sizeof(struct mapping_defn_t));
643 while ((firstword = next_word(&buf_ptr)) != NULL) {
644 if (currmap->max_matches == currmap->n_matches) {
645 currmap->max_matches = currmap->max_matches * 2 + 1;
646 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
649 currmap->match[currmap->n_matches++] = xstrdup(firstword);
651 currmap->max_mappings = 0;
652 currmap->n_mappings = 0;
653 currmap->mapping = NULL;
654 currmap->script = NULL;
656 struct mapping_defn_t **where = &defn->mappings;
657 while (*where != NULL) {
658 where = &(*where)->next;
661 currmap->next = NULL;
663 debug_noise("Added mapping\n");
665 currently_processing = MAPPING;
666 } else if (strcmp(firstword, "iface") == 0) {
667 static const struct address_family_t *const addr_fams[] = {
668 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
671 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
678 char *address_family_name;
682 currif = xzalloc(sizeof(struct interface_defn_t));
683 iface_name = next_word(&buf_ptr);
684 address_family_name = next_word(&buf_ptr);
685 method_name = next_word(&buf_ptr);
687 if (buf_ptr == NULL) {
688 bb_error_msg("too few parameters for line \"%s\"", buf);
692 /* ship any trailing whitespace */
693 while (isspace(*buf_ptr)) {
697 if (buf_ptr[0] != '\0') {
698 bb_error_msg("too many parameters \"%s\"", buf);
702 currif->iface = xstrdup(iface_name);
704 currif->address_family = get_address_family(addr_fams, address_family_name);
705 if (!currif->address_family) {
706 bb_error_msg("unknown address type \"%s\"", address_family_name);
710 currif->method = get_method(currif->address_family, method_name);
711 if (!currif->method) {
712 bb_error_msg("unknown method \"%s\"", method_name);
716 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
717 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
718 if ((strcmp(tmp->iface, currif->iface) == 0) &&
719 (tmp->address_family == currif->address_family)) {
720 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
724 llist_add_to_end(&(defn->ifaces), (char*)currif);
726 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
727 currently_processing = IFACE;
728 } else if (strcmp(firstword, "auto") == 0) {
729 while ((firstword = next_word(&buf_ptr)) != NULL) {
731 /* Check the interface isnt already listed */
732 if (find_list_string(defn->autointerfaces, firstword)) {
733 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
736 /* Add the interface to the list */
737 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
738 debug_noise("\nauto %s\n", firstword);
740 currently_processing = NONE;
742 switch (currently_processing) {
747 if (strlen(buf_ptr) == 0) {
748 bb_error_msg("option with empty value \"%s\"", buf);
752 if (strcmp(firstword, "up") != 0
753 && strcmp(firstword, "down") != 0
754 && strcmp(firstword, "pre-up") != 0
755 && strcmp(firstword, "post-down") != 0) {
756 for (i = 0; i < currif->n_options; i++) {
757 if (strcmp(currif->option[i].name, firstword) == 0) {
758 bb_error_msg("duplicate option \"%s\"", buf);
764 if (currif->n_options >= currif->max_options) {
765 struct variable_t *opt;
767 currif->max_options = currif->max_options + 10;
768 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
769 currif->option = opt;
771 currif->option[currif->n_options].name = xstrdup(firstword);
772 currif->option[currif->n_options].value = xstrdup(buf_ptr);
773 if (!currif->option[currif->n_options].name) {
777 if (!currif->option[currif->n_options].value) {
781 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
782 currif->option[currif->n_options].value);
786 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
787 if (strcmp(firstword, "script") == 0) {
788 if (currmap->script != NULL) {
789 bb_error_msg("duplicate script in mapping \"%s\"", buf);
792 currmap->script = xstrdup(next_word(&buf_ptr));
794 } else if (strcmp(firstword, "map") == 0) {
795 if (currmap->max_mappings == currmap->n_mappings) {
796 currmap->max_mappings = currmap->max_mappings * 2 + 1;
797 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
799 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
800 currmap->n_mappings++;
802 bb_error_msg("misplaced option \"%s\"", buf);
809 bb_error_msg("misplaced option \"%s\"", buf);
815 if (ferror(f) != 0) {
816 bb_perror_msg_and_die("%s", filename);
823 static char *setlocalenv(char *format, const char *name, const char *value)
829 result = xasprintf(format, name, value);
831 for (here = there = result; *there != '=' && *there; there++) {
835 *there = toupper(*there);
837 if (isalnum(*there) || *there == '_') {
842 memmove(here, there, strlen(there) + 1);
847 static void set_environ(struct interface_defn_t *iface, const char *mode)
851 const int n_env_entries = iface->n_options + 5;
854 if (__myenviron != NULL) {
855 for (ppch = __myenviron; *ppch; ppch++) {
861 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
862 environend = __myenviron;
864 for (i = 0; i < iface->n_options; i++) {
865 if (strcmp(iface->option[i].name, "up") == 0
866 || strcmp(iface->option[i].name, "down") == 0
867 || strcmp(iface->option[i].name, "pre-up") == 0
868 || strcmp(iface->option[i].name, "post-down") == 0) {
871 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
874 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
875 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
876 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
877 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
878 /* FIXME: we must not impose our own PATH, it is admin's job!
879 Use PATH from parent env */
880 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
883 static int doit(char *str)
885 if (option_mask & (OPT_no_act|OPT_verbose)) {
888 if (!(option_mask & OPT_no_act)) {
893 switch (child = fork()) {
894 case -1: /* failure */
897 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
900 waitpid(child, &status, 0);
901 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
908 static int execute_all(struct interface_defn_t *ifd, const char *opt)
912 for (i = 0; i < ifd->n_options; i++) {
913 if (strcmp(ifd->option[i].name, opt) == 0) {
914 if (!doit(ifd->option[i].value)) {
920 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
921 if (doit(buf) != 1) {
927 static int check(char *str) {
931 static int iface_up(struct interface_defn_t *iface)
933 if (!iface->method->up(iface,check)) return -1;
934 set_environ(iface, "start");
935 if (!execute_all(iface, "pre-up")) return 0;
936 if (!iface->method->up(iface, doit)) return 0;
937 if (!execute_all(iface, "up")) return 0;
941 static int iface_down(struct interface_defn_t *iface)
943 if (!iface->method->down(iface,check)) return -1;
944 set_environ(iface, "stop");
945 if (!execute_all(iface, "down")) return 0;
946 if (!iface->method->down(iface, doit)) return 0;
947 if (!execute_all(iface, "post-down")) return 0;
951 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
952 static int popen2(FILE **in, FILE **out, char *command, ...)
955 char *argv[11] = { command };
957 int infd[2], outfd[2];
961 va_start(ap, command);
962 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
965 argv[argc] = NULL; /* make sure */
968 if (pipe(infd) != 0) {
972 if (pipe(outfd) != 0) {
979 switch (pid = fork()) {
980 case -1: /* failure */
993 execvp(command, argv);
995 default: /* parent */
996 *in = fdopen(infd[1], "w");
997 *out = fdopen(outfd[0], "r");
1005 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1011 char *logical = xstrdup(physical);
1013 /* Run the mapping script. */
1014 pid = popen2(&in, &out, map->script, physical, NULL);
1016 /* popen2() returns 0 on failure. */
1020 /* Write mappings to stdin of mapping script. */
1021 for (i = 0; i < map->n_mappings; i++) {
1022 fprintf(in, "%s\n", map->mapping[i]);
1025 waitpid(pid, &status, 0);
1027 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1028 /* If the mapping script exited successfully, try to
1029 * grab a line of output and use that as the name of the
1030 * logical interface. */
1031 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1033 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1034 /* If we are able to read a line of output from the script,
1035 * remove any trailing whitespace and use this value
1036 * as the name of the logical interface. */
1037 char *pch = new_logical + strlen(new_logical) - 1;
1039 while (pch >= new_logical && isspace(*pch))
1043 logical = new_logical;
1045 /* If we are UNABLE to read a line of output, discard our
1046 * freshly allocated memory. */
1055 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1057 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1059 unsigned short iface_len = strlen(iface);
1060 llist_t *search = state_list;
1063 if ((strncmp(search->data, iface, iface_len) == 0) &&
1064 (search->data[iface_len] == '=')) {
1067 search = search->link;
1072 int ifupdown_main(int argc, char **argv)
1074 static const char statefile[] = "/var/run/ifstate";
1076 int (*cmds)(struct interface_defn_t *) = NULL;
1077 struct interfaces_file_t *defn;
1078 llist_t *state_list = NULL;
1079 llist_t *target_list = NULL;
1080 const char *interfaces = "/etc/network/interfaces";
1081 int any_failures = 0;
1084 if (bb_applet_name[2] == 'u') {
1088 /* ifdown command */
1092 option_mask = bb_getopt_ulflags(argc, argv, OPTION_STR, &interfaces);
1093 if (argc - optind > 0) {
1094 if (DO_ALL) bb_show_usage();
1096 if (!DO_ALL) bb_show_usage();
1098 debug_noise("reading %s file:\n", interfaces);
1099 defn = read_interfaces(interfaces);
1100 debug_noise("\ndone reading %s\n\n", interfaces);
1106 /* Create a list of interfaces to work on */
1108 if (cmds == iface_up) {
1109 target_list = defn->autointerfaces;
1112 const llist_t *list = state_list;
1114 llist_add_to_end(&target_list, xstrdup(list->data));
1117 target_list = defn->autointerfaces;
1120 llist_add_to_end(&target_list, argv[optind]);
1124 /* Update the interfaces */
1125 while (target_list) {
1126 llist_t *iface_list;
1127 struct interface_defn_t *currif;
1134 iface = xstrdup(target_list->data);
1135 target_list = target_list->link;
1137 pch = strchr(iface, '=');
1140 liface = xstrdup(pch + 1);
1142 liface = xstrdup(iface);
1146 const llist_t *iface_state = find_iface_state(state_list, iface);
1148 if (cmds == iface_up) {
1151 bb_error_msg("interface %s already configured", iface);
1157 bb_error_msg("interface %s not configured", iface);
1163 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1164 if ((cmds == iface_up) && !NO_MAPPINGS) {
1165 struct mapping_defn_t *currmap;
1167 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1168 for (i = 0; i < currmap->n_matches; i++) {
1169 if (fnmatch(currmap->match[i], liface, 0) != 0)
1172 printf("Running mapping script %s on %s\n", currmap->script, liface);
1174 liface = run_mapping(iface, currmap);
1182 iface_list = defn->ifaces;
1183 while (iface_list) {
1184 currif = (struct interface_defn_t *) iface_list->data;
1185 if (strcmp(liface, currif->iface) == 0) {
1186 char *oldiface = currif->iface;
1189 currif->iface = iface;
1191 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1193 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1194 cmds_ret = cmds(currif);
1195 if (cmds_ret == -1) {
1196 bb_error_msg("don't seem to have all the variables for %s/%s",
1197 liface, currif->address_family->name);
1199 } else if (cmds_ret == 0) {
1203 currif->iface = oldiface;
1205 iface_list = iface_list->link;
1211 if (!okay && !FORCE) {
1212 bb_error_msg("ignoring unknown interface %s", liface);
1215 llist_t *iface_state = find_iface_state(state_list, iface);
1217 if (cmds == iface_up) {
1218 char *newiface = xasprintf("%s=%s", iface, liface);
1219 if (iface_state == NULL) {
1220 llist_add_to_end(&state_list, newiface);
1222 free(iface_state->data);
1223 iface_state->data = newiface;
1226 /* Remove an interface from the linked list */
1227 free(llist_pop(&iface_state));
1232 /* Actually write the new state */
1234 FILE *state_fp = NULL;
1236 state_fp = xfopen(statefile, "w");
1237 while (state_list) {
1238 if (state_list->data) {
1239 fputs(state_list->data, state_fp);
1240 fputc('\n', state_fp);
1242 state_list = state_list->link;