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 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
98 OPT_no_mappings = 0x10,
100 #define DO_ALL (option_mask32 & OPT_do_all)
101 #define NO_ACT (option_mask32 & OPT_no_act)
102 #define VERBOSE (option_mask32 & OPT_verbose)
103 #define FORCE (option_mask32 & OPT_force)
104 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
106 static char **__myenviron;
108 static char *startup_PATH;
110 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
112 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
114 static unsigned int count_bits(unsigned int a)
117 result = (a & 0x55) + ((a >> 1) & 0x55);
118 result = (result & 0x33) + ((result >> 2) & 0x33);
119 return ((result & 0x0F) + ((result >> 4) & 0x0F));
122 static int count_netmask_bits(char *dotted_quad)
124 unsigned int result, a, b, c, d;
125 /* Found a netmask... Check if it is dotted quad */
126 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
128 result = count_bits(a);
129 result += count_bits(b);
130 result += count_bits(c);
131 result += count_bits(d);
136 static void addstr(char **buf, size_t *len, size_t *pos, const char *str, size_t str_length)
138 if (*pos + str_length >= *len) {
141 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
143 *len = *len * 2 + str_length + 1;
146 while (str_length-- >= 1) {
147 (*buf)[(*pos)++] = *str;
153 static int strncmpz(const char *l, const char *r, size_t llen)
155 int i = strncmp(l, r, llen);
164 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
168 if (strncmpz(id, "iface", idlen) == 0) {
170 static char label_buf[20];
171 strncpy(label_buf, ifd->iface, 19);
173 result = strchr(label_buf, ':');
178 } else if (strncmpz(id, "label", idlen) == 0) {
181 for (i = 0; i < ifd->n_options; i++) {
182 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
183 return ifd->option[i].value;
191 static char *parse(const char *command, struct interface_defn_t *ifd)
195 size_t pos = 0, len = 0;
196 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
197 int okay[MAX_OPT_DEPTH] = { 1 };
203 addstr(&result, &len, &pos, command, 1);
208 addstr(&result, &len, &pos, command + 1, 1);
211 addstr(&result, &len, &pos, command, 1);
216 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
217 old_pos[opt_depth] = pos;
222 addstr(&result, &len, &pos, "[", 1);
227 if (command[1] == ']' && opt_depth > 1) {
229 if (!okay[opt_depth]) {
230 pos = old_pos[opt_depth];
235 addstr(&result, &len, &pos, "]", 1);
245 nextpercent = strchr(command, '%');
252 varvalue = get_var(command, nextpercent - command, ifd);
255 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
257 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
258 /* Sigh... Add a special case for 'ip' to convert from
259 * dotted quad to bit count style netmasks. */
260 if (strncmp(command, "bnmask", 6)==0) {
262 varvalue = get_var("netmask", 7, ifd);
263 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
265 sprintf(argument, "%d", res);
266 addstr(&result, &len, &pos, argument, strlen(argument));
267 command = nextpercent + 1;
272 okay[opt_depth - 1] = 0;
275 command = nextpercent + 1;
296 /* execute() returns 1 for success and 0 for failure */
297 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
302 out = parse(command, ifd);
316 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
317 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
319 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
321 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
322 result += execute("ip link set %iface% up", ifd, exec);
323 return ((result == 2) ? 2 : 0);
325 return execute("ifconfig %iface% add ::1", ifd, exec);
329 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
331 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
332 return execute("ip link set %iface% down", ifd, exec);
334 return execute("ifconfig %iface% del ::1", ifd, exec);
338 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
341 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
342 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
343 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
344 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
346 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
347 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
348 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
350 return ((result == 3) ? 3 : 0);
353 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
355 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
356 return execute("ip link set %iface% down", ifd, exec);
358 return execute("ifconfig %iface% down", ifd, exec);
362 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
363 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
366 result = execute("ip tunnel add %iface% mode sit remote "
367 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
368 result += execute("ip link set %iface% up", ifd, exec);
369 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
370 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
371 return ((result == 4) ? 4 : 0);
374 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
376 return execute("ip tunnel del %iface%", ifd, exec);
380 static const struct method_t methods6[] = {
381 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
382 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
384 { "static", static_up6, static_down6, },
385 { "loopback", loopback_up6, loopback_down6, },
388 static const struct address_family_t addr_inet6 = {
390 sizeof(methods6) / sizeof(struct method_t),
393 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
395 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
396 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
398 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
400 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
401 result += execute("ip link set %iface% up", ifd, exec);
402 return ((result == 2) ? 2 : 0);
404 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
408 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
410 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
412 result = execute("ip addr flush dev %iface%", ifd, exec);
413 result += execute("ip link set %iface% down", ifd, exec);
414 return ((result == 2) ? 2 : 0);
416 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
420 static int static_up(struct interface_defn_t *ifd, execfn *exec)
423 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
424 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
425 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
426 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
427 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
428 return ((result == 3) ? 3 : 0);
430 result = execute("ifconfig %iface% %address% netmask %netmask% "
431 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
432 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
434 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
435 return ((result == 2) ? 2 : 0);
439 static int static_down(struct interface_defn_t *ifd, execfn *exec)
442 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
443 result = execute("ip addr flush dev %iface%", ifd, exec);
444 result += execute("ip link set %iface% down", ifd, exec);
446 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
447 result += execute("ifconfig %iface% down", ifd, exec);
449 return ((result == 2) ? 2 : 0);
452 #ifndef CONFIG_APP_UDHCPC
456 const char *startcmd;
460 static const struct dhcp_client_t ext_dhcp_clients[] = {
462 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]",
463 "kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
466 "pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]",
467 "pump -i %iface% -k",
470 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
471 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
474 "dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%",
480 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
482 #ifdef CONFIG_APP_UDHCPC
483 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
484 "-i %iface% [[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]",
487 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
488 for (i = 0; i < nclients; i++) {
489 if (exists_execable(ext_dhcp_clients[i].name))
490 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
492 bb_error_msg("no dhcp clients found");
497 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
499 #ifdef CONFIG_APP_UDHCPC
500 return execute("kill -TERM "
501 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
503 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
504 for (i = 0; i < nclients; i++) {
505 if (exists_execable(ext_dhcp_clients[i].name))
506 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
508 bb_error_msg("no dhcp clients found, using static interface shutdown");
509 return static_down(ifd, exec);
513 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
518 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
520 return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
521 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
522 "--returniffail --serverbcast", ifd, exec);
525 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
527 return execute("pon [[%provider%]]", ifd, exec);
530 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
532 return execute("poff [[%provider%]]", ifd, exec);
535 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
537 return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
538 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec);
541 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
543 return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
544 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
547 static const struct method_t methods[] = {
548 { "manual", manual_up_down, manual_up_down, },
549 { "wvdial", wvdial_up, wvdial_down, },
550 { "ppp", ppp_up, ppp_down, },
551 { "static", static_up, static_down, },
552 { "bootp", bootp_up, static_down, },
553 { "dhcp", dhcp_up, dhcp_down, },
554 { "loopback", loopback_up, loopback_down, },
557 static const struct address_family_t addr_inet = {
559 sizeof(methods) / sizeof(struct method_t),
563 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
565 static char *next_word(char **buf)
567 unsigned short length;
570 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
574 /* Skip over leading whitespace */
575 word = skip_whitespace(*buf);
577 /* Skip over comments */
582 /* Find the length of this word */
583 length = strcspn(word, " \t\n");
587 *buf = word + length;
588 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
597 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
601 for (i = 0; af[i]; i++) {
602 if (strcmp(af[i]->name, name) == 0) {
609 static const struct method_t *get_method(const struct address_family_t *af, char *name)
613 for (i = 0; i < af->n_methods; i++) {
614 if (strcmp(af->method[i].name, name) == 0) {
615 return &af->method[i];
621 static const llist_t *find_list_string(const llist_t *list, const char *string)
624 if (strcmp(list->data, string) == 0) {
632 static struct interfaces_file_t *read_interfaces(const char *filename)
634 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
635 struct mapping_defn_t *currmap = NULL;
637 struct interface_defn_t *currif = NULL;
638 struct interfaces_file_t *defn;
643 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
645 defn = xzalloc(sizeof(struct interfaces_file_t));
647 f = xfopen(filename, "r");
649 while ((buf = xmalloc_getline(f)) != NULL) {
652 firstword = next_word(&buf_ptr);
653 if (firstword == NULL) {
655 continue; /* blank line */
658 if (strcmp(firstword, "mapping") == 0) {
659 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
660 currmap = xzalloc(sizeof(struct mapping_defn_t));
662 while ((firstword = next_word(&buf_ptr)) != NULL) {
663 if (currmap->max_matches == currmap->n_matches) {
664 currmap->max_matches = currmap->max_matches * 2 + 1;
665 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
668 currmap->match[currmap->n_matches++] = xstrdup(firstword);
670 currmap->max_mappings = 0;
671 currmap->n_mappings = 0;
672 currmap->mapping = NULL;
673 currmap->script = NULL;
675 struct mapping_defn_t **where = &defn->mappings;
676 while (*where != NULL) {
677 where = &(*where)->next;
680 currmap->next = NULL;
682 debug_noise("Added mapping\n");
684 currently_processing = MAPPING;
685 } else if (strcmp(firstword, "iface") == 0) {
686 static const struct address_family_t *const addr_fams[] = {
687 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
690 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
697 char *address_family_name;
701 currif = xzalloc(sizeof(struct interface_defn_t));
702 iface_name = next_word(&buf_ptr);
703 address_family_name = next_word(&buf_ptr);
704 method_name = next_word(&buf_ptr);
706 if (buf_ptr == NULL) {
707 bb_error_msg("too few parameters for line \"%s\"", buf);
711 /* ship any trailing whitespace */
712 buf_ptr = skip_whitespace(buf_ptr);
714 if (buf_ptr[0] != '\0') {
715 bb_error_msg("too many parameters \"%s\"", buf);
719 currif->iface = xstrdup(iface_name);
721 currif->address_family = get_address_family(addr_fams, address_family_name);
722 if (!currif->address_family) {
723 bb_error_msg("unknown address type \"%s\"", address_family_name);
727 currif->method = get_method(currif->address_family, method_name);
728 if (!currif->method) {
729 bb_error_msg("unknown method \"%s\"", method_name);
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);
741 llist_add_to_end(&(defn->ifaces), (char*)currif);
743 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
744 currently_processing = IFACE;
745 } else if (strcmp(firstword, "auto") == 0) {
746 while ((firstword = next_word(&buf_ptr)) != NULL) {
748 /* Check the interface isnt already listed */
749 if (find_list_string(defn->autointerfaces, firstword)) {
750 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
753 /* Add the interface to the list */
754 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
755 debug_noise("\nauto %s\n", firstword);
757 currently_processing = NONE;
759 switch (currently_processing) {
764 if (strlen(buf_ptr) == 0) {
765 bb_error_msg("option with empty value \"%s\"", buf);
769 if (strcmp(firstword, "up") != 0
770 && strcmp(firstword, "down") != 0
771 && strcmp(firstword, "pre-up") != 0
772 && strcmp(firstword, "post-down") != 0) {
773 for (i = 0; i < currif->n_options; i++) {
774 if (strcmp(currif->option[i].name, firstword) == 0) {
775 bb_error_msg("duplicate option \"%s\"", buf);
781 if (currif->n_options >= currif->max_options) {
782 struct variable_t *opt;
784 currif->max_options = currif->max_options + 10;
785 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
786 currif->option = opt;
788 currif->option[currif->n_options].name = xstrdup(firstword);
789 currif->option[currif->n_options].value = xstrdup(buf_ptr);
790 if (!currif->option[currif->n_options].name) {
794 if (!currif->option[currif->n_options].value) {
798 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
799 currif->option[currif->n_options].value);
803 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
804 if (strcmp(firstword, "script") == 0) {
805 if (currmap->script != NULL) {
806 bb_error_msg("duplicate script in mapping \"%s\"", buf);
809 currmap->script = xstrdup(next_word(&buf_ptr));
811 } else if (strcmp(firstword, "map") == 0) {
812 if (currmap->max_mappings == currmap->n_mappings) {
813 currmap->max_mappings = currmap->max_mappings * 2 + 1;
814 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
816 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
817 currmap->n_mappings++;
819 bb_error_msg("misplaced option \"%s\"", buf);
826 bb_error_msg("misplaced option \"%s\"", buf);
832 if (ferror(f) != 0) {
833 bb_perror_msg_and_die("%s", filename);
840 static char *setlocalenv(char *format, const char *name, const char *value)
846 result = xasprintf(format, name, value);
848 for (here = there = result; *there != '=' && *there; there++) {
852 *there = toupper(*there);
854 if (isalnum(*there) || *there == '_') {
859 memmove(here, there, strlen(there) + 1);
864 static void set_environ(struct interface_defn_t *iface, const char *mode)
868 const int n_env_entries = iface->n_options + 5;
871 if (__myenviron != NULL) {
872 for (ppch = __myenviron; *ppch; ppch++) {
878 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
879 environend = __myenviron;
881 for (i = 0; i < iface->n_options; i++) {
882 if (strcmp(iface->option[i].name, "up") == 0
883 || strcmp(iface->option[i].name, "down") == 0
884 || strcmp(iface->option[i].name, "pre-up") == 0
885 || strcmp(iface->option[i].name, "post-down") == 0) {
888 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
891 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
892 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
893 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
894 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
895 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
898 static int doit(char *str)
900 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
903 if (!(option_mask32 & OPT_no_act)) {
908 switch (child = fork()) {
909 case -1: /* failure */
912 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
915 waitpid(child, &status, 0);
916 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
923 static int execute_all(struct interface_defn_t *ifd, const char *opt)
927 for (i = 0; i < ifd->n_options; i++) {
928 if (strcmp(ifd->option[i].name, opt) == 0) {
929 if (!doit(ifd->option[i].value)) {
935 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
936 if (doit(buf) != 1) {
942 static int check(char *str) {
946 static int iface_up(struct interface_defn_t *iface)
948 if (!iface->method->up(iface,check)) return -1;
949 set_environ(iface, "start");
950 if (!execute_all(iface, "pre-up")) return 0;
951 if (!iface->method->up(iface, doit)) return 0;
952 if (!execute_all(iface, "up")) return 0;
956 static int iface_down(struct interface_defn_t *iface)
958 if (!iface->method->down(iface,check)) return -1;
959 set_environ(iface, "stop");
960 if (!execute_all(iface, "down")) return 0;
961 if (!iface->method->down(iface, doit)) return 0;
962 if (!execute_all(iface, "post-down")) return 0;
966 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
967 static int popen2(FILE **in, FILE **out, char *command, ...)
970 char *argv[11] = { command };
972 int infd[2], outfd[2];
976 va_start(ap, command);
977 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
980 argv[argc] = NULL; /* make sure */
983 if (pipe(infd) != 0) {
987 if (pipe(outfd) != 0) {
994 switch (pid = fork()) {
995 case -1: /* failure */
1008 execvp(command, argv);
1010 default: /* parent */
1011 *in = fdopen(infd[1], "w");
1012 *out = fdopen(outfd[0], "r");
1020 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1026 char *logical = xstrdup(physical);
1028 /* Run the mapping script. */
1029 pid = popen2(&in, &out, map->script, physical, NULL);
1031 /* popen2() returns 0 on failure. */
1035 /* Write mappings to stdin of mapping script. */
1036 for (i = 0; i < map->n_mappings; i++) {
1037 fprintf(in, "%s\n", map->mapping[i]);
1040 waitpid(pid, &status, 0);
1042 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1043 /* If the mapping script exited successfully, try to
1044 * grab a line of output and use that as the name of the
1045 * logical interface. */
1046 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1048 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1049 /* If we are able to read a line of output from the script,
1050 * remove any trailing whitespace and use this value
1051 * as the name of the logical interface. */
1052 char *pch = new_logical + strlen(new_logical) - 1;
1054 while (pch >= new_logical && isspace(*pch))
1058 logical = new_logical;
1060 /* If we are UNABLE to read a line of output, discard our
1061 * freshly allocated memory. */
1070 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1072 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1074 unsigned short iface_len = strlen(iface);
1075 llist_t *search = state_list;
1078 if ((strncmp(search->data, iface, iface_len) == 0) &&
1079 (search->data[iface_len] == '=')) {
1082 search = search->link;
1087 int ifupdown_main(int argc, char **argv)
1089 static const char statefile[] = "/var/run/ifstate";
1091 int (*cmds)(struct interface_defn_t *) = NULL;
1092 struct interfaces_file_t *defn;
1093 llist_t *state_list = NULL;
1094 llist_t *target_list = NULL;
1095 const char *interfaces = "/etc/network/interfaces";
1096 int any_failures = 0;
1098 if (applet_name[2] == 'u') {
1102 /* ifdown command */
1106 getopt32(argc, argv, OPTION_STR, &interfaces);
1107 if (argc - optind > 0) {
1108 if (DO_ALL) bb_show_usage();
1110 if (!DO_ALL) bb_show_usage();
1112 debug_noise("reading %s file:\n", interfaces);
1113 defn = read_interfaces(interfaces);
1114 debug_noise("\ndone reading %s\n\n", interfaces);
1120 startup_PATH = getenv("PATH");
1121 if (!startup_PATH) startup_PATH = "";
1123 /* Create a list of interfaces to work on */
1125 if (cmds == iface_up) {
1126 target_list = defn->autointerfaces;
1129 const llist_t *list = state_list;
1131 llist_add_to_end(&target_list, xstrdup(list->data));
1134 target_list = defn->autointerfaces;
1137 llist_add_to_end(&target_list, argv[optind]);
1141 /* Update the interfaces */
1142 while (target_list) {
1143 llist_t *iface_list;
1144 struct interface_defn_t *currif;
1151 iface = xstrdup(target_list->data);
1152 target_list = target_list->link;
1154 pch = strchr(iface, '=');
1157 liface = xstrdup(pch + 1);
1159 liface = xstrdup(iface);
1163 const llist_t *iface_state = find_iface_state(state_list, iface);
1165 if (cmds == iface_up) {
1168 bb_error_msg("interface %s already configured", iface);
1174 bb_error_msg("interface %s not configured", iface);
1180 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1181 if ((cmds == iface_up) && !NO_MAPPINGS) {
1182 struct mapping_defn_t *currmap;
1184 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1186 for (i = 0; i < currmap->n_matches; i++) {
1187 if (fnmatch(currmap->match[i], liface, 0) != 0)
1190 printf("Running mapping script %s on %s\n", currmap->script, liface);
1192 liface = run_mapping(iface, currmap);
1200 iface_list = defn->ifaces;
1201 while (iface_list) {
1202 currif = (struct interface_defn_t *) iface_list->data;
1203 if (strcmp(liface, currif->iface) == 0) {
1204 char *oldiface = currif->iface;
1207 currif->iface = iface;
1209 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1211 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1212 cmds_ret = cmds(currif);
1213 if (cmds_ret == -1) {
1214 bb_error_msg("don't seem to have all the variables for %s/%s",
1215 liface, currif->address_family->name);
1217 } else if (cmds_ret == 0) {
1221 currif->iface = oldiface;
1223 iface_list = iface_list->link;
1229 if (!okay && !FORCE) {
1230 bb_error_msg("ignoring unknown interface %s", liface);
1233 llist_t *iface_state = find_iface_state(state_list, iface);
1235 if (cmds == iface_up) {
1236 char *newiface = xasprintf("%s=%s", iface, liface);
1237 if (iface_state == NULL) {
1238 llist_add_to_end(&state_list, newiface);
1240 free(iface_state->data);
1241 iface_state->data = newiface;
1244 /* Remove an interface from the linked list */
1245 free(llist_pop(&iface_state));
1250 /* Actually write the new state */
1252 FILE *state_fp = NULL;
1254 state_fp = xfopen(statefile, "w");
1255 while (state_list) {
1256 if (state_list->data) {
1257 fputs(state_list->data, state_fp);
1258 fputc('\n', state_fp);
1260 state_list = state_list->link;