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 GPLv2 or later, see file LICENSE in this source tree.
21 /* After libbb.h, since it needs sys/types.h on some systems */
22 #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 UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
36 #define debug_noise(args...) /*fprintf(stderr, args)*/
38 /* Forward declaration */
39 struct interface_defn_t;
41 typedef int execfn(char *command);
45 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
46 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
49 struct address_family_t {
52 const struct method_t *method;
55 struct mapping_defn_t {
56 struct mapping_defn_t *next;
74 struct interface_defn_t {
75 const struct address_family_t *address_family;
76 const struct method_t *method;
81 struct variable_t *option;
84 struct interfaces_file_t {
85 llist_t *autointerfaces;
87 struct mapping_defn_t *mappings;
91 #define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
97 OPT_no_mappings = 0x10,
99 #define DO_ALL (option_mask32 & OPT_do_all)
100 #define NO_ACT (option_mask32 & OPT_no_act)
101 #define VERBOSE (option_mask32 & OPT_verbose)
102 #define FORCE (option_mask32 & OPT_force)
103 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
108 const char *startup_PATH;
111 #define G (*(struct globals*)&bb_common_bufsiz1)
112 #define INIT_G() do { } while (0)
115 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
117 static void addstr(char **bufp, const char *str, size_t str_length)
119 /* xasprintf trick will be smaller, but we are often
120 * called with str_length == 1 - don't want to have
121 * THAT much of malloc/freeing! */
123 int len = (buf ? strlen(buf) : 0);
125 buf = xrealloc(buf, len + str_length);
126 /* copies at most str_length-1 chars! */
127 safe_strncpy(buf + len, str, str_length);
131 static int strncmpz(const char *l, const char *r, size_t llen)
133 int i = strncmp(l, r, llen);
136 return - (unsigned char)r[llen];
140 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
144 if (strncmpz(id, "iface", idlen) == 0) {
145 // ubuntu's ifup doesn't do this:
146 //static char *label_buf;
149 //label_buf = xstrdup(ifd->iface);
150 // Remove virtual iface suffix
151 //result = strchrnul(label_buf, ':');
157 if (strncmpz(id, "label", idlen) == 0) {
160 for (i = 0; i < ifd->n_options; i++) {
161 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
162 return ifd->option[i].value;
168 # if ENABLE_FEATURE_IFUPDOWN_IP
169 static int count_netmask_bits(const char *dotted_quad)
172 // unsigned a, b, c, d;
173 // /* Found a netmask... Check if it is dotted quad */
174 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
176 // if ((a|b|c|d) >> 8)
177 // return -1; /* one of numbers is >= 256 */
178 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
179 // d = ~d; /* 11110000 -> 00001111 */
181 /* Shorter version */
186 if (inet_aton(dotted_quad, &ip) == 0)
187 return -1; /* malformed dotted IP */
188 d = ntohl(ip.s_addr); /* IP in host order */
189 d = ~d; /* 11110000 -> 00001111 */
190 if (d & (d+1)) /* check that it is in 00001111 form */
191 return -1; /* no it is not */
201 static char *parse(const char *command, struct interface_defn_t *ifd)
203 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
204 int okay[MAX_OPT_DEPTH] = { 1 };
211 addstr(&result, command, 1);
216 addstr(&result, command + 1, 1);
219 addstr(&result, command, 1);
224 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
225 old_pos[opt_depth] = result ? strlen(result) : 0;
230 addstr(&result, "[", 1);
235 if (command[1] == ']' && opt_depth > 1) {
237 if (!okay[opt_depth]) {
238 result[old_pos[opt_depth]] = '\0';
242 addstr(&result, "]", 1);
252 nextpercent = strchr(command, '%');
259 varvalue = get_var(command, nextpercent - command, ifd);
262 # if ENABLE_FEATURE_IFUPDOWN_IP
263 /* "hwaddress <class> <address>":
264 * unlike ifconfig, ip doesnt want <class>
265 * (usually "ether" keyword). Skip it. */
266 if (strncmp(command, "hwaddress", 9) == 0) {
267 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
270 addstr(&result, varvalue, strlen(varvalue));
272 # if ENABLE_FEATURE_IFUPDOWN_IP
273 /* Sigh... Add a special case for 'ip' to convert from
274 * dotted quad to bit count style netmasks. */
275 if (strncmp(command, "bnmask", 6) == 0) {
277 varvalue = get_var("netmask", 7, ifd);
279 res = count_netmask_bits(varvalue);
281 const char *argument = utoa(res);
282 addstr(&result, argument, strlen(argument));
283 command = nextpercent + 1;
289 okay[opt_depth - 1] = 0;
292 command = nextpercent + 1;
313 /* execute() returns 1 for success and 0 for failure */
314 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
319 out = parse(command, ifd);
324 /* out == "": parsed ok but not all needed variables known, skip */
325 ret = out[0] ? (*exec)(out) : 1;
334 #endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
337 #if ENABLE_FEATURE_IFUPDOWN_IPV6
339 static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
341 # if ENABLE_FEATURE_IFUPDOWN_IP
343 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
344 result += execute("ip link set %iface% up", ifd, exec);
345 return ((result == 2) ? 2 : 0);
347 return execute("ifconfig %iface% add ::1", ifd, exec);
351 static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
353 # if ENABLE_FEATURE_IFUPDOWN_IP
354 return execute("ip link set %iface% down", ifd, exec);
356 return execute("ifconfig %iface% del ::1", ifd, exec);
360 static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
365 static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
368 # if ENABLE_FEATURE_IFUPDOWN_IP
369 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
370 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
371 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
372 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
374 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
375 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
376 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
378 return ((result == 3) ? 3 : 0);
381 static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
383 # if ENABLE_FEATURE_IFUPDOWN_IP
384 return execute("ip link set %iface% down", ifd, exec);
386 return execute("ifconfig %iface% down", ifd, exec);
390 # if ENABLE_FEATURE_IFUPDOWN_IP
391 static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
394 result = execute("ip tunnel add %iface% mode sit remote "
395 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
396 result += execute("ip link set %iface% up", ifd, exec);
397 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
398 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
399 return ((result == 4) ? 4 : 0);
402 static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
404 return execute("ip tunnel del %iface%", ifd, exec);
408 static const struct method_t methods6[] = {
409 # if ENABLE_FEATURE_IFUPDOWN_IP
410 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
412 { "static" , static_up6 , static_down6 , },
413 { "manual" , manual_up_down6 , manual_up_down6 , },
414 { "loopback" , loopback_up6 , loopback_down6 , },
417 static const struct address_family_t addr_inet6 = {
419 ARRAY_SIZE(methods6),
423 #endif /* FEATURE_IFUPDOWN_IPV6 */
426 #if ENABLE_FEATURE_IFUPDOWN_IPV4
428 static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
430 # if ENABLE_FEATURE_IFUPDOWN_IP
432 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
433 result += execute("ip link set %iface% up", ifd, exec);
434 return ((result == 2) ? 2 : 0);
436 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
440 static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
442 # if ENABLE_FEATURE_IFUPDOWN_IP
444 result = execute("ip addr flush dev %iface%", ifd, exec);
445 result += execute("ip link set %iface% down", ifd, exec);
446 return ((result == 2) ? 2 : 0);
448 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
452 static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
455 # if ENABLE_FEATURE_IFUPDOWN_IP
456 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
457 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
458 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
459 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
460 return ((result == 3) ? 3 : 0);
462 /* ifconfig said to set iface up before it processes hw %hwaddress%,
463 * which then of course fails. Thus we run two separate ifconfig */
464 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
466 result += execute("ifconfig %iface% %address% netmask %netmask%"
467 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
469 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
470 return ((result == 3) ? 3 : 0);
474 static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
477 # if ENABLE_FEATURE_IFUPDOWN_IP
478 result = execute("ip addr flush dev %iface%", ifd, exec);
479 result += execute("ip link set %iface% down", ifd, exec);
481 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
482 /* Bringing the interface down deletes the routes in itself.
483 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
485 result += execute("ifconfig %iface% down", ifd, exec);
487 return ((result == 2) ? 2 : 0);
490 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
491 struct dhcp_client_t {
493 const char *startcmd;
497 static const struct dhcp_client_t ext_dhcp_clients[] = {
499 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
503 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
504 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
507 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
508 "pump -i %iface% -k",
511 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
512 "[[ -s %script%]][[ %udhcpc_opts%]]",
513 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
516 # endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
518 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
519 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
522 # if ENABLE_FEATURE_IFUPDOWN_IP
523 /* ip doesn't up iface when it configures it (unlike ifconfig) */
524 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
527 /* needed if we have hwaddress on dhcp iface */
528 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
531 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
532 if (exists_execable(ext_dhcp_clients[i].name))
533 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
535 bb_error_msg("no dhcp clients found");
539 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
541 # if ENABLE_FEATURE_IFUPDOWN_IP
542 /* ip doesn't up iface when it configures it (unlike ifconfig) */
543 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
546 /* needed if we have hwaddress on dhcp iface */
547 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
550 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
551 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
555 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
556 execfn *exec UNUSED_PARAM)
558 return 0; /* no dhcp support */
562 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
563 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
568 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
569 if (exists_execable(ext_dhcp_clients[i].name)) {
570 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
577 bb_error_msg("warning: no dhcp clients found and stopped");
579 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
580 and it may come back up because udhcpc is still shutting down */
582 result += static_down(ifd, exec);
583 return ((result == 3) ? 3 : 0);
586 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
590 "test -f /var/run/udhcpc.%iface%.pid && "
591 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
593 /* Also bring the hardware interface down since
594 killing the dhcp client alone doesn't do it.
595 This enables consecutive ifup->ifdown->ifup */
596 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
597 and it may come back up because udhcpc is still shutting down */
599 result += static_down(ifd, exec);
600 return ((result == 3) ? 3 : 0);
603 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
604 execfn *exec UNUSED_PARAM)
606 return 0; /* no dhcp support */
610 static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
615 static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
617 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
618 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
619 " --returniffail --serverbcast", ifd, exec);
622 static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
624 return execute("pon[[ %provider%]]", ifd, exec);
627 static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
629 return execute("poff[[ %provider%]]", ifd, exec);
632 static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
634 return execute("start-stop-daemon --start -x wvdial "
635 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
638 static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
640 return execute("start-stop-daemon --stop -x wvdial "
641 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
644 static const struct method_t methods[] = {
645 { "manual" , manual_up_down, manual_up_down, },
646 { "wvdial" , wvdial_up , wvdial_down , },
647 { "ppp" , ppp_up , ppp_down , },
648 { "static" , static_up , static_down , },
649 { "bootp" , bootp_up , static_down , },
650 { "dhcp" , dhcp_up , dhcp_down , },
651 { "loopback", loopback_up , loopback_down , },
654 static const struct address_family_t addr_inet = {
660 #endif /* FEATURE_IFUPDOWN_IPV4 */
663 /* Returns pointer to the next word, or NULL.
664 * In 1st case, advances *buf to the word after this one.
666 static char *next_word(char **buf)
671 /* Skip over leading whitespace */
672 word = skip_whitespace(*buf);
678 /* Find the length of this word (can't be 0) */
679 length = strcspn(word, " \t\n");
681 /* Unless we are already at NUL, store NUL and advance */
682 if (word[length] != '\0')
683 word[length++] = '\0';
685 *buf = skip_whitespace(word + length);
690 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
697 for (i = 0; af[i]; i++) {
698 if (strcmp(af[i]->name, name) == 0) {
705 static const struct method_t *get_method(const struct address_family_t *af, char *name)
711 /* TODO: use index_in_str_array() */
712 for (i = 0; i < af->n_methods; i++) {
713 if (strcmp(af->method[i].name, name) == 0) {
714 return &af->method[i];
720 static struct interfaces_file_t *read_interfaces(const char *filename)
722 /* Let's try to be compatible.
724 * "man 5 interfaces" says:
725 * Lines starting with "#" are ignored. Note that end-of-line
726 * comments are NOT supported, comments must be on a line of their own.
727 * A line may be extended across multiple lines by making
728 * the last character a backslash.
730 * Seen elsewhere in example config file:
731 * A first non-blank "#" character makes the rest of the line
732 * be ignored. Blank lines are ignored. Lines may be indented freely.
733 * A "\" character at the very end of the line indicates the next line
734 * should be treated as a continuation of the current one.
736 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
737 struct mapping_defn_t *currmap = NULL;
739 struct interface_defn_t *currif = NULL;
740 struct interfaces_file_t *defn;
745 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
747 defn = xzalloc(sizeof(*defn));
748 f = xfopen_for_read(filename);
750 while ((buf = xmalloc_fgetline(f)) != NULL) {
752 /* Trailing "\" concatenates lines */
754 while ((p = last_char_is(buf, '\\')) != NULL) {
756 rest_of_line = xmalloc_fgetline(f);
759 p = xasprintf("%s%s", buf, rest_of_line);
766 first_word = next_word(&rest_of_line);
767 if (!first_word || *first_word == '#') {
769 continue; /* blank/comment line */
772 if (strcmp(first_word, "mapping") == 0) {
773 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
774 currmap = xzalloc(sizeof(*currmap));
776 while ((first_word = next_word(&rest_of_line)) != NULL) {
777 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
778 currmap->match[currmap->n_matches++] = xstrdup(first_word);
780 /*currmap->max_mappings = 0; - done by xzalloc */
781 /*currmap->n_mappings = 0;*/
782 /*currmap->mapping = NULL;*/
783 /*currmap->script = NULL;*/
785 struct mapping_defn_t **where = &defn->mappings;
786 while (*where != NULL) {
787 where = &(*where)->next;
790 /*currmap->next = NULL;*/
792 debug_noise("Added mapping\n");
794 currently_processing = MAPPING;
795 } else if (strcmp(first_word, "iface") == 0) {
796 static const struct address_family_t *const addr_fams[] = {
797 #if ENABLE_FEATURE_IFUPDOWN_IPV4
800 #if ENABLE_FEATURE_IFUPDOWN_IPV6
806 char *address_family_name;
810 currif = xzalloc(sizeof(*currif));
811 iface_name = next_word(&rest_of_line);
812 address_family_name = next_word(&rest_of_line);
813 method_name = next_word(&rest_of_line);
815 if (method_name == NULL)
816 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
818 /* ship any trailing whitespace */
819 rest_of_line = skip_whitespace(rest_of_line);
821 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
822 bb_error_msg_and_die("too many parameters \"%s\"", buf);
824 currif->iface = xstrdup(iface_name);
826 currif->address_family = get_address_family(addr_fams, address_family_name);
827 if (!currif->address_family)
828 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
830 currif->method = get_method(currif->address_family, method_name);
832 bb_error_msg_and_die("unknown method \"%s\"", method_name);
834 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
835 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
836 if ((strcmp(tmp->iface, currif->iface) == 0)
837 && (tmp->address_family == currif->address_family)
839 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
842 llist_add_to_end(&(defn->ifaces), (char*)currif);
844 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
845 currently_processing = IFACE;
846 } else if (strcmp(first_word, "auto") == 0) {
847 while ((first_word = next_word(&rest_of_line)) != NULL) {
849 /* Check the interface isnt already listed */
850 if (llist_find_str(defn->autointerfaces, first_word)) {
851 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
854 /* Add the interface to the list */
855 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
856 debug_noise("\nauto %s\n", first_word);
858 currently_processing = NONE;
860 switch (currently_processing) {
862 if (rest_of_line[0] == '\0')
863 bb_error_msg_and_die("option with empty value \"%s\"", buf);
865 if (strcmp(first_word, "up") != 0
866 && strcmp(first_word, "down") != 0
867 && strcmp(first_word, "pre-up") != 0
868 && strcmp(first_word, "post-down") != 0
871 for (i = 0; i < currif->n_options; i++) {
872 if (strcmp(currif->option[i].name, first_word) == 0)
873 bb_error_msg_and_die("duplicate option \"%s\"", buf);
876 if (currif->n_options >= currif->max_options) {
877 currif->max_options += 10;
878 currif->option = xrealloc(currif->option,
879 sizeof(*currif->option) * currif->max_options);
881 debug_noise("\t%s=%s\n", first_word, rest_of_line);
882 currif->option[currif->n_options].name = xstrdup(first_word);
883 currif->option[currif->n_options].value = xstrdup(rest_of_line);
887 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
888 if (strcmp(first_word, "script") == 0) {
889 if (currmap->script != NULL)
890 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
891 currmap->script = xstrdup(next_word(&rest_of_line));
892 } else if (strcmp(first_word, "map") == 0) {
893 if (currmap->n_mappings >= currmap->max_mappings) {
894 currmap->max_mappings = currmap->max_mappings * 2 + 1;
895 currmap->mapping = xrealloc(currmap->mapping,
896 sizeof(char *) * currmap->max_mappings);
898 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
899 currmap->n_mappings++;
901 bb_error_msg_and_die("misplaced option \"%s\"", buf);
907 bb_error_msg_and_die("misplaced option \"%s\"", buf);
911 } /* while (fgets) */
913 if (ferror(f) != 0) {
914 /* ferror does NOT set errno! */
915 bb_error_msg_and_die("%s: I/O error", filename);
922 static char *setlocalenv(const char *format, const char *name, const char *value)
929 result = xasprintf(format, name, value);
931 for (dst = src = result; (c = *src) != '=' && c; src++) {
934 if (c >= 'a' && c <= 'z')
936 if (isalnum(c) || c == '_')
939 overlapping_strcpy(dst, src);
944 static void set_environ(struct interface_defn_t *iface, const char *mode)
949 if (G.my_environ != NULL) {
950 for (pp = G.my_environ; *pp; pp++) {
956 /* note: last element will stay NULL: */
957 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
960 for (i = 0; i < iface->n_options; i++) {
961 if (strcmp(iface->option[i].name, "up") == 0
962 || strcmp(iface->option[i].name, "down") == 0
963 || strcmp(iface->option[i].name, "pre-up") == 0
964 || strcmp(iface->option[i].name, "post-down") == 0
968 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
971 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
972 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
973 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
974 *pp++ = setlocalenv("%s=%s", "MODE", mode);
976 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
979 static int doit(char *str)
981 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
984 if (!(option_mask32 & OPT_no_act)) {
990 if (child < 0) /* failure */
992 if (child == 0) { /* child */
993 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
996 safe_waitpid(child, &status, 0);
997 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1004 static int execute_all(struct interface_defn_t *ifd, const char *opt)
1008 for (i = 0; i < ifd->n_options; i++) {
1009 if (strcmp(ifd->option[i].name, opt) == 0) {
1010 if (!doit(ifd->option[i].value)) {
1016 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1017 /* heh, we don't bother free'ing it */
1021 static int check(char *str)
1026 static int iface_up(struct interface_defn_t *iface)
1028 if (!iface->method->up(iface, check)) return -1;
1029 set_environ(iface, "start");
1030 if (!execute_all(iface, "pre-up")) return 0;
1031 if (!iface->method->up(iface, doit)) return 0;
1032 if (!execute_all(iface, "up")) return 0;
1036 static int iface_down(struct interface_defn_t *iface)
1038 if (!iface->method->down(iface,check)) return -1;
1039 set_environ(iface, "stop");
1040 if (!execute_all(iface, "down")) return 0;
1041 if (!iface->method->down(iface, doit)) return 0;
1042 if (!execute_all(iface, "post-down")) return 0;
1046 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1047 static int popen2(FILE **in, FILE **out, char *command, char *param)
1049 char *argv[3] = { command, param, NULL };
1050 struct fd_pair infd, outfd;
1061 /* NB: close _first_, then move fds! */
1064 xmove_fd(infd.rd, 0);
1065 xmove_fd(outfd.wr, 1);
1066 BB_EXECVP_or_die(argv);
1071 *in = xfdopen_for_write(infd.wr);
1072 *out = xfdopen_for_read(outfd.rd);
1076 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1082 char *logical = xstrdup(physical);
1084 /* Run the mapping script. Never fails. */
1085 pid = popen2(&in, &out, map->script, physical);
1087 /* Write mappings to stdin of mapping script. */
1088 for (i = 0; i < map->n_mappings; i++) {
1089 fprintf(in, "%s\n", map->mapping[i]);
1092 safe_waitpid(pid, &status, 0);
1094 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1095 /* If the mapping script exited successfully, try to
1096 * grab a line of output and use that as the name of the
1097 * logical interface. */
1098 char *new_logical = xmalloc_fgetline(out);
1101 /* If we are able to read a line of output from the script,
1102 * remove any trailing whitespace and use this value
1103 * as the name of the logical interface. */
1104 char *pch = new_logical + strlen(new_logical) - 1;
1106 while (pch >= new_logical && isspace(*pch))
1110 logical = new_logical;
1118 #endif /* FEATURE_IFUPDOWN_MAPPING */
1120 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1122 unsigned iface_len = strlen(iface);
1123 llist_t *search = state_list;
1126 if ((strncmp(search->data, iface, iface_len) == 0)
1127 && (search->data[iface_len] == '=')
1131 search = search->link;
1136 /* read the previous state from the state file */
1137 static llist_t *read_iface_state(void)
1139 llist_t *state_list = NULL;
1140 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1143 char *start, *end_ptr;
1144 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1145 /* We should only need to check for a single character */
1146 end_ptr = start + strcspn(start, " \t\n");
1148 llist_add_to(&state_list, start);
1156 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1157 int ifupdown_main(int argc UNUSED_PARAM, char **argv)
1159 int (*cmds)(struct interface_defn_t *);
1160 struct interfaces_file_t *defn;
1161 llist_t *target_list = NULL;
1162 const char *interfaces = "/etc/network/interfaces";
1163 bool any_failures = 0;
1167 G.startup_PATH = getenv("PATH");
1168 G.shell = xstrdup(get_shell_name());
1171 if (applet_name[2] == 'u') {
1176 getopt32(argv, OPTION_STR, &interfaces);
1179 if (DO_ALL) bb_show_usage();
1181 if (!DO_ALL) bb_show_usage();
1184 debug_noise("reading %s file:\n", interfaces);
1185 defn = read_interfaces(interfaces);
1186 debug_noise("\ndone reading %s\n\n", interfaces);
1188 /* Create a list of interfaces to work on */
1190 target_list = defn->autointerfaces;
1192 llist_add_to_end(&target_list, argv[0]);
1195 /* Update the interfaces */
1196 while (target_list) {
1197 llist_t *iface_list;
1198 struct interface_defn_t *currif;
1205 iface = xstrdup(target_list->data);
1206 target_list = target_list->link;
1208 pch = strchr(iface, '=');
1211 liface = xstrdup(pch + 1);
1213 liface = xstrdup(iface);
1217 llist_t *state_list = read_iface_state();
1218 const llist_t *iface_state = find_iface_state(state_list, iface);
1220 if (cmds == iface_up) {
1223 bb_error_msg("interface %s already configured", iface);
1229 bb_error_msg("interface %s not configured", iface);
1233 llist_free(state_list, free);
1236 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1237 if ((cmds == iface_up) && !NO_MAPPINGS) {
1238 struct mapping_defn_t *currmap;
1240 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1242 for (i = 0; i < currmap->n_matches; i++) {
1243 if (fnmatch(currmap->match[i], liface, 0) != 0)
1246 printf("Running mapping script %s on %s\n", currmap->script, liface);
1248 liface = run_mapping(iface, currmap);
1255 iface_list = defn->ifaces;
1256 while (iface_list) {
1257 currif = (struct interface_defn_t *) iface_list->data;
1258 if (strcmp(liface, currif->iface) == 0) {
1259 char *oldiface = currif->iface;
1262 currif->iface = iface;
1264 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1266 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1267 cmds_ret = cmds(currif);
1268 if (cmds_ret == -1) {
1269 bb_error_msg("don't seem to have all the variables for %s/%s",
1270 liface, currif->address_family->name);
1272 } else if (cmds_ret == 0) {
1276 currif->iface = oldiface;
1278 iface_list = iface_list->link;
1284 if (!okay && !FORCE) {
1285 bb_error_msg("ignoring unknown interface %s", liface);
1287 } else if (!NO_ACT) {
1288 /* update the state file */
1291 llist_t *state_list = read_iface_state();
1292 llist_t *iface_state = find_iface_state(state_list, iface);
1294 if (cmds == iface_up) {
1295 char * const newiface = xasprintf("%s=%s", iface, liface);
1296 if (iface_state == NULL) {
1297 llist_add_to_end(&state_list, newiface);
1299 free(iface_state->data);
1300 iface_state->data = newiface;
1303 /* Remove an interface from state_list */
1304 llist_unlink(&state_list, iface_state);
1305 free(llist_pop(&iface_state));
1308 /* Actually write the new state */
1309 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1313 fprintf(state_fp, "%s\n", state->data);
1315 state = state->link;
1318 llist_free(state_list, free);
1325 return any_failures;