1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath
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
14 * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build
17 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
20 #include <sys/utsname.h>
25 #define MAX_OPT_DEPTH 10
26 #define EUNBALBRACK 10001
27 #define EUNDEFVAR 10002
28 #define EUNBALPER 10000
30 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
31 #define MAX_INTERFACE_LENGTH 10
34 #define debug_noise(args...) /*fprintf(stderr, args)*/
36 /* Forward declaration */
37 struct interface_defn_t;
39 typedef int execfn(char *command);
43 int (*up)(struct interface_defn_t *ifd, execfn *e);
44 int (*down)(struct interface_defn_t *ifd, execfn *e);
47 struct address_family_t {
50 const struct method_t *method;
53 struct mapping_defn_t {
54 struct mapping_defn_t *next;
72 struct interface_defn_t {
73 const struct address_family_t *address_family;
74 const struct method_t *method;
79 struct variable_t *option;
82 struct interfaces_file_t {
83 llist_t *autointerfaces;
85 struct mapping_defn_t *mappings;
88 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
94 OPT_no_mappings = 0x10,
96 #define DO_ALL (option_mask32 & OPT_do_all)
97 #define NO_ACT (option_mask32 & OPT_no_act)
98 #define VERBOSE (option_mask32 & OPT_verbose)
99 #define FORCE (option_mask32 & OPT_force)
100 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
102 static char **my_environ;
104 static const char *startup_PATH;
106 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
108 static void addstr(char **bufp, const char *str, size_t str_length)
110 /* xasprintf trick will be smaller, but we are often
111 * called with str_length == 1 - don't want to have
112 * THAT much of malloc/freeing! */
114 int len = (buf ? strlen(buf) : 0);
116 buf = xrealloc(buf, len + str_length);
117 /* copies at most str_length-1 chars! */
118 safe_strncpy(buf + len, str, str_length);
122 static int strncmpz(const char *l, const char *r, size_t llen)
124 int i = strncmp(l, r, llen);
131 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
135 if (strncmpz(id, "iface", idlen) == 0) {
136 static char *label_buf;
140 label_buf = xstrdup(ifd->iface);
141 // Remove virtual iface suffix - why?
142 // ubuntu's ifup doesn't do this
143 //result = strchrnul(label_buf, ':');
147 if (strncmpz(id, "label", idlen) == 0) {
150 for (i = 0; i < ifd->n_options; i++) {
151 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
152 return ifd->option[i].value;
158 #if ENABLE_FEATURE_IFUPDOWN_IP
159 static int count_netmask_bits(const char *dotted_quad)
162 // unsigned a, b, c, d;
163 // /* Found a netmask... Check if it is dotted quad */
164 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
166 // if ((a|b|c|d) >> 8)
167 // return -1; /* one of numbers is >= 256 */
168 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
169 // d = ~d; /* 11110000 -> 00001111 */
171 /* Shorter version */
176 if (inet_aton(dotted_quad, &ip) == 0)
177 return -1; /* malformed dotted IP */
178 d = ntohl(ip.s_addr); /* IP in host order */
179 d = ~d; /* 11110000 -> 00001111 */
180 if (d & (d+1)) /* check that it is in 00001111 form */
181 return -1; /* no it is not */
191 static char *parse(const char *command, struct interface_defn_t *ifd)
193 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
194 int okay[MAX_OPT_DEPTH] = { 1 };
201 addstr(&result, command, 1);
206 addstr(&result, command + 1, 1);
209 addstr(&result, command, 1);
214 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
215 old_pos[opt_depth] = result ? strlen(result) : 0;
220 addstr(&result, "[", 1);
225 if (command[1] == ']' && opt_depth > 1) {
227 if (!okay[opt_depth]) {
228 result[old_pos[opt_depth]] = '\0';
232 addstr(&result, "]", 1);
242 nextpercent = strchr(command, '%');
249 varvalue = get_var(command, nextpercent - command, ifd);
252 addstr(&result, varvalue, strlen(varvalue));
254 #if ENABLE_FEATURE_IFUPDOWN_IP
255 /* Sigh... Add a special case for 'ip' to convert from
256 * dotted quad to bit count style netmasks. */
257 if (strncmp(command, "bnmask", 6) == 0) {
259 varvalue = get_var("netmask", 7, ifd);
261 res = count_netmask_bits(varvalue);
263 const char *argument = utoa(res);
264 addstr(&result, argument, strlen(argument));
265 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(const char *command, struct interface_defn_t *ifd, execfn *exec)
301 out = parse(command, ifd);
306 /* out == "": parsed ok but not all needed variables known, skip */
307 ret = out[0] ? (*exec)(out) : 1;
317 #if ENABLE_FEATURE_IFUPDOWN_IPV6
318 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
320 #if ENABLE_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 #if ENABLE_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 #if ENABLE_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 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
346 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
348 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
349 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
350 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
352 return ((result == 3) ? 3 : 0);
355 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
357 #if ENABLE_FEATURE_IFUPDOWN_IP
358 return execute("ip link set %iface% down", ifd, exec);
360 return execute("ifconfig %iface% down", ifd, exec);
364 #if ENABLE_FEATURE_IFUPDOWN_IP
365 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
368 result = execute("ip tunnel add %iface% mode sit remote "
369 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
370 result += execute("ip link set %iface% up", ifd, exec);
371 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
372 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
373 return ((result == 4) ? 4 : 0);
376 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
378 return execute("ip tunnel del %iface%", ifd, exec);
382 static const struct method_t methods6[] = {
383 #if ENABLE_FEATURE_IFUPDOWN_IP
384 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
386 { "static", static_up6, static_down6, },
387 { "loopback", loopback_up6, loopback_down6, },
390 static const struct address_family_t addr_inet6 = {
392 ARRAY_SIZE(methods6),
395 #endif /* FEATURE_IFUPDOWN_IPV6 */
397 #if ENABLE_FEATURE_IFUPDOWN_IPV4
398 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
400 #if ENABLE_FEATURE_IFUPDOWN_IP
402 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
403 result += execute("ip link set %iface% up", ifd, exec);
404 return ((result == 2) ? 2 : 0);
406 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
410 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
412 #if ENABLE_FEATURE_IFUPDOWN_IP
414 result = execute("ip addr flush dev %iface%", ifd, exec);
415 result += execute("ip link set %iface% down", ifd, exec);
416 return ((result == 2) ? 2 : 0);
418 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
422 static int static_up(struct interface_defn_t *ifd, execfn *exec)
425 #if ENABLE_FEATURE_IFUPDOWN_IP
426 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
427 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
428 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
429 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
430 return ((result == 3) ? 3 : 0);
432 /* ifconfig said to set iface up before it processes hw %hwaddress%,
433 * which then of course fails. Thus we run two separate ifconfig */
434 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
436 result += execute("ifconfig %iface% %address% netmask %netmask%"
437 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
439 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
440 return ((result == 3) ? 3 : 0);
444 static int static_down(struct interface_defn_t *ifd, execfn *exec)
447 #if ENABLE_FEATURE_IFUPDOWN_IP
448 result = execute("ip addr flush dev %iface%", ifd, exec);
449 result += execute("ip link set %iface% down", ifd, exec);
451 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
452 result += execute("ifconfig %iface% down", ifd, exec);
454 return ((result == 2) ? 2 : 0);
457 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
461 const char *startcmd;
465 static const struct dhcp_client_t ext_dhcp_clients[] = {
467 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
471 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
472 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
475 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
476 "pump -i %iface% -k",
479 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
480 "[[ -s %script%]][[ %udhcpc_opts%]]",
481 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
484 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
486 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
487 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
490 #if ENABLE_FEATURE_IFUPDOWN_IP
491 /* ip doesn't up iface when it configures it (unlike ifconfig) */
492 if (!execute("ip link set %iface% up", ifd, exec))
495 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
496 if (exists_execable(ext_dhcp_clients[i].name))
497 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
499 bb_error_msg("no dhcp clients found");
502 #elif ENABLE_APP_UDHCPC
503 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
505 #if ENABLE_FEATURE_IFUPDOWN_IP
506 /* ip doesn't up iface when it configures it (unlike ifconfig) */
507 if (!execute("ip link set %iface% up", ifd, exec))
510 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
511 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
515 static int dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
516 execfn *exec UNUSED_PARAM)
518 return 0; /* no dhcp support */
522 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
523 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
526 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
527 if (exists_execable(ext_dhcp_clients[i].name))
528 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
530 bb_error_msg("no dhcp clients found, using static interface shutdown");
531 return static_down(ifd, exec);
533 #elif ENABLE_APP_UDHCPC
534 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
536 return execute("kill "
537 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
540 static int dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
541 execfn *exec UNUSED_PARAM)
543 return 0; /* no dhcp support */
547 static int manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
552 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
554 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
555 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
556 " --returniffail --serverbcast", ifd, exec);
559 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
561 return execute("pon[[ %provider%]]", ifd, exec);
564 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
566 return execute("poff[[ %provider%]]", ifd, exec);
569 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
571 return execute("start-stop-daemon --start -x wvdial "
572 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
575 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
577 return execute("start-stop-daemon --stop -x wvdial "
578 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
581 static const struct method_t methods[] = {
582 { "manual", manual_up_down, manual_up_down, },
583 { "wvdial", wvdial_up, wvdial_down, },
584 { "ppp", ppp_up, ppp_down, },
585 { "static", static_up, static_down, },
586 { "bootp", bootp_up, static_down, },
587 { "dhcp", dhcp_up, dhcp_down, },
588 { "loopback", loopback_up, loopback_down, },
591 static const struct address_family_t addr_inet = {
597 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
599 static char *next_word(char **buf)
604 /* Skip over leading whitespace */
605 word = skip_whitespace(*buf);
611 /* Find the length of this word (can't be 0) */
612 length = strcspn(word, " \t\n");
614 /* Unless we are already at NUL, store NUL and advance */
615 if (word[length] != '\0')
616 word[length++] = '\0';
618 *buf = word + length;
623 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
630 for (i = 0; af[i]; i++) {
631 if (strcmp(af[i]->name, name) == 0) {
638 static const struct method_t *get_method(const struct address_family_t *af, char *name)
644 /* TODO: use index_in_str_array() */
645 for (i = 0; i < af->n_methods; i++) {
646 if (strcmp(af->method[i].name, name) == 0) {
647 return &af->method[i];
653 static const llist_t *find_list_string(const llist_t *list, const char *string)
659 if (strcmp(list->data, string) == 0) {
667 static struct interfaces_file_t *read_interfaces(const char *filename)
669 /* Let's try to be compatible.
671 * "man 5 interfaces" says:
672 * Lines starting with "#" are ignored. Note that end-of-line
673 * comments are NOT supported, comments must be on a line of their own.
674 * A line may be extended across multiple lines by making
675 * the last character a backslash.
677 * Seen elsewhere in example config file:
678 * A first non-blank "#" character makes the rest of the line
679 * be ignored. Blank lines are ignored. Lines may be indented freely.
680 * A "\" character at the very end of the line indicates the next line
681 * should be treated as a continuation of the current one.
683 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
684 struct mapping_defn_t *currmap = NULL;
686 struct interface_defn_t *currif = NULL;
687 struct interfaces_file_t *defn;
692 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
694 defn = xzalloc(sizeof(*defn));
695 f = xfopen_for_read(filename);
697 while ((buf = xmalloc_fgetline(f)) != NULL) {
699 /* Trailing "\" concatenates lines */
701 while ((p = last_char_is(buf, '\\')) != NULL) {
703 rest_of_line = xmalloc_fgetline(f);
706 p = xasprintf("%s%s", buf, rest_of_line);
713 first_word = next_word(&rest_of_line);
714 if (!first_word || *first_word == '#') {
716 continue; /* blank/comment line */
719 if (strcmp(first_word, "mapping") == 0) {
720 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
721 currmap = xzalloc(sizeof(*currmap));
723 while ((first_word = next_word(&rest_of_line)) != NULL) {
724 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
725 currmap->match[currmap->n_matches++] = xstrdup(first_word);
727 /*currmap->max_mappings = 0; - done by xzalloc */
728 /*currmap->n_mappings = 0;*/
729 /*currmap->mapping = NULL;*/
730 /*currmap->script = NULL;*/
732 struct mapping_defn_t **where = &defn->mappings;
733 while (*where != NULL) {
734 where = &(*where)->next;
737 /*currmap->next = NULL;*/
739 debug_noise("Added mapping\n");
741 currently_processing = MAPPING;
742 } else if (strcmp(first_word, "iface") == 0) {
743 static const struct address_family_t *const addr_fams[] = {
744 #if ENABLE_FEATURE_IFUPDOWN_IPV4
747 #if ENABLE_FEATURE_IFUPDOWN_IPV6
753 char *address_family_name;
757 currif = xzalloc(sizeof(*currif));
758 iface_name = next_word(&rest_of_line);
759 address_family_name = next_word(&rest_of_line);
760 method_name = next_word(&rest_of_line);
762 if (method_name == NULL)
763 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
765 /* ship any trailing whitespace */
766 rest_of_line = skip_whitespace(rest_of_line);
768 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
769 bb_error_msg_and_die("too many parameters \"%s\"", buf);
771 currif->iface = xstrdup(iface_name);
773 currif->address_family = get_address_family(addr_fams, address_family_name);
774 if (!currif->address_family)
775 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
777 currif->method = get_method(currif->address_family, method_name);
779 bb_error_msg_and_die("unknown method \"%s\"", method_name);
781 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
782 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
783 if ((strcmp(tmp->iface, currif->iface) == 0)
784 && (tmp->address_family == currif->address_family)
786 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
789 llist_add_to_end(&(defn->ifaces), (char*)currif);
791 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
792 currently_processing = IFACE;
793 } else if (strcmp(first_word, "auto") == 0) {
794 while ((first_word = next_word(&rest_of_line)) != NULL) {
796 /* Check the interface isnt already listed */
797 if (find_list_string(defn->autointerfaces, first_word)) {
798 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
801 /* Add the interface to the list */
802 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
803 debug_noise("\nauto %s\n", first_word);
805 currently_processing = NONE;
807 switch (currently_processing) {
809 if (rest_of_line[0] == '\0')
810 bb_error_msg_and_die("option with empty value \"%s\"", buf);
812 if (strcmp(first_word, "up") != 0
813 && strcmp(first_word, "down") != 0
814 && strcmp(first_word, "pre-up") != 0
815 && strcmp(first_word, "post-down") != 0
818 for (i = 0; i < currif->n_options; i++) {
819 if (strcmp(currif->option[i].name, first_word) == 0)
820 bb_error_msg_and_die("duplicate option \"%s\"", buf);
823 if (currif->n_options >= currif->max_options) {
824 currif->max_options += 10;
825 currif->option = xrealloc(currif->option,
826 sizeof(*currif->option) * currif->max_options);
828 debug_noise("\t%s=%s\n", first_word, rest_of_line);
829 currif->option[currif->n_options].name = xstrdup(first_word);
830 currif->option[currif->n_options].value = xstrdup(rest_of_line);
834 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
835 if (strcmp(first_word, "script") == 0) {
836 if (currmap->script != NULL)
837 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
838 currmap->script = xstrdup(next_word(&rest_of_line));
839 } else if (strcmp(first_word, "map") == 0) {
840 if (currmap->n_mappings >= currmap->max_mappings) {
841 currmap->max_mappings = currmap->max_mappings * 2 + 1;
842 currmap->mapping = xrealloc(currmap->mapping,
843 sizeof(char *) * currmap->max_mappings);
845 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
846 currmap->n_mappings++;
848 bb_error_msg_and_die("misplaced option \"%s\"", buf);
854 bb_error_msg_and_die("misplaced option \"%s\"", buf);
858 } /* while (fgets) */
860 if (ferror(f) != 0) {
861 /* ferror does NOT set errno! */
862 bb_error_msg_and_die("%s: I/O error", filename);
869 static char *setlocalenv(const char *format, const char *name, const char *value)
875 result = xasprintf(format, name, value);
877 for (here = there = result; *there != '=' && *there; there++) {
881 *there = toupper(*there);
883 if (isalnum(*there) || *there == '_') {
888 memmove(here, there, strlen(there) + 1);
893 static void set_environ(struct interface_defn_t *iface, const char *mode)
897 const int n_env_entries = iface->n_options + 5;
900 if (my_environ != NULL) {
901 for (ppch = my_environ; *ppch; ppch++) {
907 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
908 environend = my_environ;
910 for (i = 0; i < iface->n_options; i++) {
911 if (strcmp(iface->option[i].name, "up") == 0
912 || strcmp(iface->option[i].name, "down") == 0
913 || strcmp(iface->option[i].name, "pre-up") == 0
914 || strcmp(iface->option[i].name, "post-down") == 0
918 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
921 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
922 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
923 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
924 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
925 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
928 static int doit(char *str)
930 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
933 if (!(option_mask32 & OPT_no_act)) {
940 case -1: /* failure */
943 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
946 safe_waitpid(child, &status, 0);
947 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
954 static int execute_all(struct interface_defn_t *ifd, const char *opt)
958 for (i = 0; i < ifd->n_options; i++) {
959 if (strcmp(ifd->option[i].name, opt) == 0) {
960 if (!doit(ifd->option[i].value)) {
966 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
967 /* heh, we don't bother free'ing it */
971 static int check(char *str)
976 static int iface_up(struct interface_defn_t *iface)
978 if (!iface->method->up(iface, check)) return -1;
979 set_environ(iface, "start");
980 if (!execute_all(iface, "pre-up")) return 0;
981 if (!iface->method->up(iface, doit)) return 0;
982 if (!execute_all(iface, "up")) return 0;
986 static int iface_down(struct interface_defn_t *iface)
988 if (!iface->method->down(iface,check)) return -1;
989 set_environ(iface, "stop");
990 if (!execute_all(iface, "down")) return 0;
991 if (!iface->method->down(iface, doit)) return 0;
992 if (!execute_all(iface, "post-down")) return 0;
996 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
997 static int popen2(FILE **in, FILE **out, char *command, char *param)
999 char *argv[3] = { command, param, NULL };
1000 struct fd_pair infd, outfd;
1010 case -1: /* failure */
1011 bb_perror_msg_and_die("vfork");
1013 /* NB: close _first_, then move fds! */
1016 xmove_fd(infd.rd, 0);
1017 xmove_fd(outfd.wr, 1);
1018 BB_EXECVP(command, argv);
1024 *in = fdopen(infd.wr, "w");
1025 *out = fdopen(outfd.rd, "r");
1029 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1035 char *logical = xstrdup(physical);
1037 /* Run the mapping script. Never fails. */
1038 pid = popen2(&in, &out, map->script, physical);
1040 /* Write mappings to stdin of mapping script. */
1041 for (i = 0; i < map->n_mappings; i++) {
1042 fprintf(in, "%s\n", map->mapping[i]);
1045 safe_waitpid(pid, &status, 0);
1047 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1048 /* If the mapping script exited successfully, try to
1049 * grab a line of output and use that as the name of the
1050 * logical interface. */
1051 char *new_logical = xmalloc_fgetline(out);
1054 /* If we are able to read a line of output from the script,
1055 * remove any trailing whitespace and use this value
1056 * as the name of the logical interface. */
1057 char *pch = new_logical + strlen(new_logical) - 1;
1059 while (pch >= new_logical && isspace(*pch))
1063 logical = new_logical;
1071 #endif /* FEATURE_IFUPDOWN_MAPPING */
1073 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1075 unsigned iface_len = strlen(iface);
1076 llist_t *search = state_list;
1079 if ((strncmp(search->data, iface, iface_len) == 0)
1080 && (search->data[iface_len] == '=')
1084 search = search->link;
1089 /* read the previous state from the state file */
1090 static llist_t *read_iface_state(void)
1092 llist_t *state_list = NULL;
1093 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1096 char *start, *end_ptr;
1097 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1098 /* We should only need to check for a single character */
1099 end_ptr = start + strcspn(start, " \t\n");
1101 llist_add_to(&state_list, start);
1109 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1110 int ifupdown_main(int argc, char **argv)
1112 int (*cmds)(struct interface_defn_t *);
1113 struct interfaces_file_t *defn;
1114 llist_t *target_list = NULL;
1115 const char *interfaces = "/etc/network/interfaces";
1116 bool any_failures = 0;
1119 if (applet_name[2] == 'u') {
1124 getopt32(argv, OPTION_STR, &interfaces);
1125 if (argc - optind > 0) {
1126 if (DO_ALL) bb_show_usage();
1128 if (!DO_ALL) bb_show_usage();
1131 debug_noise("reading %s file:\n", interfaces);
1132 defn = read_interfaces(interfaces);
1133 debug_noise("\ndone reading %s\n\n", interfaces);
1135 startup_PATH = getenv("PATH");
1136 if (!startup_PATH) startup_PATH = "";
1138 /* Create a list of interfaces to work on */
1140 target_list = defn->autointerfaces;
1142 llist_add_to_end(&target_list, argv[optind]);
1145 /* Update the interfaces */
1146 while (target_list) {
1147 llist_t *iface_list;
1148 struct interface_defn_t *currif;
1155 iface = xstrdup(target_list->data);
1156 target_list = target_list->link;
1158 pch = strchr(iface, '=');
1161 liface = xstrdup(pch + 1);
1163 liface = xstrdup(iface);
1167 llist_t *state_list = read_iface_state();
1168 const llist_t *iface_state = find_iface_state(state_list, iface);
1170 if (cmds == iface_up) {
1173 bb_error_msg("interface %s already configured", iface);
1179 bb_error_msg("interface %s not configured", iface);
1183 llist_free(state_list, free);
1186 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1187 if ((cmds == iface_up) && !NO_MAPPINGS) {
1188 struct mapping_defn_t *currmap;
1190 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1192 for (i = 0; i < currmap->n_matches; i++) {
1193 if (fnmatch(currmap->match[i], liface, 0) != 0)
1196 printf("Running mapping script %s on %s\n", currmap->script, liface);
1198 liface = run_mapping(iface, currmap);
1205 iface_list = defn->ifaces;
1206 while (iface_list) {
1207 currif = (struct interface_defn_t *) iface_list->data;
1208 if (strcmp(liface, currif->iface) == 0) {
1209 char *oldiface = currif->iface;
1212 currif->iface = iface;
1214 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1216 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1217 cmds_ret = cmds(currif);
1218 if (cmds_ret == -1) {
1219 bb_error_msg("don't seem to have all the variables for %s/%s",
1220 liface, currif->address_family->name);
1222 } else if (cmds_ret == 0) {
1226 currif->iface = oldiface;
1228 iface_list = iface_list->link;
1234 if (!okay && !FORCE) {
1235 bb_error_msg("ignoring unknown interface %s", liface);
1237 } else if (!NO_ACT) {
1238 /* update the state file */
1241 llist_t *state_list = read_iface_state();
1242 llist_t *iface_state = find_iface_state(state_list, iface);
1244 if (cmds == iface_up) {
1245 char * const newiface = xasprintf("%s=%s", iface, liface);
1246 if (iface_state == NULL) {
1247 llist_add_to_end(&state_list, newiface);
1249 free(iface_state->data);
1250 iface_state->data = newiface;
1253 /* Remove an interface from state_list */
1254 llist_unlink(&state_list, iface_state);
1255 free(llist_pop(&iface_state));
1258 /* Actually write the new state */
1259 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1263 fprintf(state_fp, "%s\n", state->data);
1265 state = state->link;
1268 llist_free(state_list, free);
1272 return any_failures;