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.
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;
110 #define G (*(struct globals*)&bb_common_bufsiz1)
111 #define INIT_G() do { } while (0)
114 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
116 static void addstr(char **bufp, const char *str, size_t str_length)
118 /* xasprintf trick will be smaller, but we are often
119 * called with str_length == 1 - don't want to have
120 * THAT much of malloc/freeing! */
122 int len = (buf ? strlen(buf) : 0);
124 buf = xrealloc(buf, len + str_length);
125 /* copies at most str_length-1 chars! */
126 safe_strncpy(buf + len, str, str_length);
130 static int strncmpz(const char *l, const char *r, size_t llen)
132 int i = strncmp(l, r, llen);
135 return - (unsigned char)r[llen];
139 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
143 if (strncmpz(id, "iface", idlen) == 0) {
144 // ubuntu's ifup doesn't do this:
145 //static char *label_buf;
148 //label_buf = xstrdup(ifd->iface);
149 // Remove virtual iface suffix
150 //result = strchrnul(label_buf, ':');
156 if (strncmpz(id, "label", idlen) == 0) {
159 for (i = 0; i < ifd->n_options; i++) {
160 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
161 return ifd->option[i].value;
167 #if ENABLE_FEATURE_IFUPDOWN_IP
168 static int count_netmask_bits(const char *dotted_quad)
171 // unsigned a, b, c, d;
172 // /* Found a netmask... Check if it is dotted quad */
173 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
175 // if ((a|b|c|d) >> 8)
176 // return -1; /* one of numbers is >= 256 */
177 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
178 // d = ~d; /* 11110000 -> 00001111 */
180 /* Shorter version */
185 if (inet_aton(dotted_quad, &ip) == 0)
186 return -1; /* malformed dotted IP */
187 d = ntohl(ip.s_addr); /* IP in host order */
188 d = ~d; /* 11110000 -> 00001111 */
189 if (d & (d+1)) /* check that it is in 00001111 form */
190 return -1; /* no it is not */
200 static char *parse(const char *command, struct interface_defn_t *ifd)
202 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
203 int okay[MAX_OPT_DEPTH] = { 1 };
210 addstr(&result, command, 1);
215 addstr(&result, command + 1, 1);
218 addstr(&result, command, 1);
223 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
224 old_pos[opt_depth] = result ? strlen(result) : 0;
229 addstr(&result, "[", 1);
234 if (command[1] == ']' && opt_depth > 1) {
236 if (!okay[opt_depth]) {
237 result[old_pos[opt_depth]] = '\0';
241 addstr(&result, "]", 1);
251 nextpercent = strchr(command, '%');
258 varvalue = get_var(command, nextpercent - command, ifd);
261 #if ENABLE_FEATURE_IFUPDOWN_IP
262 /* "hwaddress <class> <address>":
263 * unlike ifconfig, ip doesnt want <class>
264 * (usually "ether" keyword). Skip it. */
265 if (strncmp(command, "hwaddress", 9) == 0) {
266 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
269 addstr(&result, varvalue, strlen(varvalue));
271 #if ENABLE_FEATURE_IFUPDOWN_IP
272 /* Sigh... Add a special case for 'ip' to convert from
273 * dotted quad to bit count style netmasks. */
274 if (strncmp(command, "bnmask", 6) == 0) {
276 varvalue = get_var("netmask", 7, ifd);
278 res = count_netmask_bits(varvalue);
280 const char *argument = utoa(res);
281 addstr(&result, argument, strlen(argument));
282 command = nextpercent + 1;
288 okay[opt_depth - 1] = 0;
291 command = nextpercent + 1;
312 /* execute() returns 1 for success and 0 for failure */
313 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
318 out = parse(command, ifd);
323 /* out == "": parsed ok but not all needed variables known, skip */
324 ret = out[0] ? (*exec)(out) : 1;
334 #if ENABLE_FEATURE_IFUPDOWN_IPV6
335 static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
337 #if ENABLE_FEATURE_IFUPDOWN_IP
339 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
340 result += execute("ip link set %iface% up", ifd, exec);
341 return ((result == 2) ? 2 : 0);
343 return execute("ifconfig %iface% add ::1", ifd, exec);
347 static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
349 #if ENABLE_FEATURE_IFUPDOWN_IP
350 return execute("ip link set %iface% down", ifd, exec);
352 return execute("ifconfig %iface% del ::1", ifd, exec);
356 static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
359 #if ENABLE_FEATURE_IFUPDOWN_IP
360 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
361 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
362 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
363 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
365 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
366 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
367 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
369 return ((result == 3) ? 3 : 0);
372 static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
374 #if ENABLE_FEATURE_IFUPDOWN_IP
375 return execute("ip link set %iface% down", ifd, exec);
377 return execute("ifconfig %iface% down", ifd, exec);
381 #if ENABLE_FEATURE_IFUPDOWN_IP
382 static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
385 result = execute("ip tunnel add %iface% mode sit remote "
386 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
387 result += execute("ip link set %iface% up", ifd, exec);
388 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
389 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
390 return ((result == 4) ? 4 : 0);
393 static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
395 return execute("ip tunnel del %iface%", ifd, exec);
399 static const struct method_t methods6[] = {
400 #if ENABLE_FEATURE_IFUPDOWN_IP
401 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
403 { "static", static_up6, static_down6, },
404 { "loopback", loopback_up6, loopback_down6, },
407 static const struct address_family_t addr_inet6 = {
409 ARRAY_SIZE(methods6),
412 #endif /* FEATURE_IFUPDOWN_IPV6 */
414 #if ENABLE_FEATURE_IFUPDOWN_IPV4
415 static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
417 #if ENABLE_FEATURE_IFUPDOWN_IP
419 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
420 result += execute("ip link set %iface% up", ifd, exec);
421 return ((result == 2) ? 2 : 0);
423 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
427 static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
429 #if ENABLE_FEATURE_IFUPDOWN_IP
431 result = execute("ip addr flush dev %iface%", ifd, exec);
432 result += execute("ip link set %iface% down", ifd, exec);
433 return ((result == 2) ? 2 : 0);
435 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
439 static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
442 #if ENABLE_FEATURE_IFUPDOWN_IP
443 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
444 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
445 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
446 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
447 return ((result == 3) ? 3 : 0);
449 /* ifconfig said to set iface up before it processes hw %hwaddress%,
450 * which then of course fails. Thus we run two separate ifconfig */
451 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
453 result += execute("ifconfig %iface% %address% netmask %netmask%"
454 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
456 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
457 return ((result == 3) ? 3 : 0);
461 static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
464 #if ENABLE_FEATURE_IFUPDOWN_IP
465 result = execute("ip addr flush dev %iface%", ifd, exec);
466 result += execute("ip link set %iface% down", ifd, exec);
468 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
469 /* Bringing the interface down deletes the routes in itself.
470 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
472 result += execute("ifconfig %iface% down", ifd, exec);
474 return ((result == 2) ? 2 : 0);
477 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
478 struct dhcp_client_t {
480 const char *startcmd;
484 static const struct dhcp_client_t ext_dhcp_clients[] = {
486 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
490 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
491 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
494 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
495 "pump -i %iface% -k",
498 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
499 "[[ -s %script%]][[ %udhcpc_opts%]]",
500 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
503 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
505 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
506 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
509 #if ENABLE_FEATURE_IFUPDOWN_IP
510 /* ip doesn't up iface when it configures it (unlike ifconfig) */
511 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
514 /* needed if we have hwaddress on dhcp iface */
515 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
518 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
519 if (exists_execable(ext_dhcp_clients[i].name))
520 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
522 bb_error_msg("no dhcp clients found");
526 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
528 #if ENABLE_FEATURE_IFUPDOWN_IP
529 /* ip doesn't up iface when it configures it (unlike ifconfig) */
530 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
533 /* needed if we have hwaddress on dhcp iface */
534 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
537 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
538 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
542 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
543 execfn *exec UNUSED_PARAM)
545 return 0; /* no dhcp support */
549 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
550 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
555 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
556 if (exists_execable(ext_dhcp_clients[i].name)) {
557 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
564 bb_error_msg("warning: no dhcp clients found and stopped");
566 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
567 and it may come back up because udhcpc is still shutting down */
569 result += static_down(ifd, exec);
570 return ((result == 3) ? 3 : 0);
573 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
576 result = execute("kill "
577 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
578 /* Also bring the hardware interface down since
579 killing the dhcp client alone doesn't do it.
580 This enables consecutive ifup->ifdown->ifup */
581 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
582 and it may come back up because udhcpc is still shutting down */
584 result += static_down(ifd, exec);
585 return ((result == 3) ? 3 : 0);
588 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
589 execfn *exec UNUSED_PARAM)
591 return 0; /* no dhcp support */
595 static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
600 static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
602 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
603 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
604 " --returniffail --serverbcast", ifd, exec);
607 static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
609 return execute("pon[[ %provider%]]", ifd, exec);
612 static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
614 return execute("poff[[ %provider%]]", ifd, exec);
617 static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
619 return execute("start-stop-daemon --start -x wvdial "
620 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
623 static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
625 return execute("start-stop-daemon --stop -x wvdial "
626 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
629 static const struct method_t methods[] = {
630 { "manual" , manual_up_down, manual_up_down, },
631 { "wvdial" , wvdial_up , wvdial_down , },
632 { "ppp" , ppp_up , ppp_down , },
633 { "static" , static_up , static_down , },
634 { "bootp" , bootp_up , static_down , },
635 { "dhcp" , dhcp_up , dhcp_down , },
636 { "loopback", loopback_up , loopback_down , },
639 static const struct address_family_t addr_inet = {
645 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
647 /* Returns pointer to the next word, or NULL.
648 * In 1st case, advances *buf to the word after this one.
650 static char *next_word(char **buf)
655 /* Skip over leading whitespace */
656 word = skip_whitespace(*buf);
662 /* Find the length of this word (can't be 0) */
663 length = strcspn(word, " \t\n");
665 /* Unless we are already at NUL, store NUL and advance */
666 if (word[length] != '\0')
667 word[length++] = '\0';
669 *buf = skip_whitespace(word + length);
674 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
681 for (i = 0; af[i]; i++) {
682 if (strcmp(af[i]->name, name) == 0) {
689 static const struct method_t *get_method(const struct address_family_t *af, char *name)
695 /* TODO: use index_in_str_array() */
696 for (i = 0; i < af->n_methods; i++) {
697 if (strcmp(af->method[i].name, name) == 0) {
698 return &af->method[i];
704 static struct interfaces_file_t *read_interfaces(const char *filename)
706 /* Let's try to be compatible.
708 * "man 5 interfaces" says:
709 * Lines starting with "#" are ignored. Note that end-of-line
710 * comments are NOT supported, comments must be on a line of their own.
711 * A line may be extended across multiple lines by making
712 * the last character a backslash.
714 * Seen elsewhere in example config file:
715 * A first non-blank "#" character makes the rest of the line
716 * be ignored. Blank lines are ignored. Lines may be indented freely.
717 * A "\" character at the very end of the line indicates the next line
718 * should be treated as a continuation of the current one.
720 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
721 struct mapping_defn_t *currmap = NULL;
723 struct interface_defn_t *currif = NULL;
724 struct interfaces_file_t *defn;
729 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
731 defn = xzalloc(sizeof(*defn));
732 f = xfopen_for_read(filename);
734 while ((buf = xmalloc_fgetline(f)) != NULL) {
736 /* Trailing "\" concatenates lines */
738 while ((p = last_char_is(buf, '\\')) != NULL) {
740 rest_of_line = xmalloc_fgetline(f);
743 p = xasprintf("%s%s", buf, rest_of_line);
750 first_word = next_word(&rest_of_line);
751 if (!first_word || *first_word == '#') {
753 continue; /* blank/comment line */
756 if (strcmp(first_word, "mapping") == 0) {
757 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
758 currmap = xzalloc(sizeof(*currmap));
760 while ((first_word = next_word(&rest_of_line)) != NULL) {
761 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
762 currmap->match[currmap->n_matches++] = xstrdup(first_word);
764 /*currmap->max_mappings = 0; - done by xzalloc */
765 /*currmap->n_mappings = 0;*/
766 /*currmap->mapping = NULL;*/
767 /*currmap->script = NULL;*/
769 struct mapping_defn_t **where = &defn->mappings;
770 while (*where != NULL) {
771 where = &(*where)->next;
774 /*currmap->next = NULL;*/
776 debug_noise("Added mapping\n");
778 currently_processing = MAPPING;
779 } else if (strcmp(first_word, "iface") == 0) {
780 static const struct address_family_t *const addr_fams[] = {
781 #if ENABLE_FEATURE_IFUPDOWN_IPV4
784 #if ENABLE_FEATURE_IFUPDOWN_IPV6
790 char *address_family_name;
794 currif = xzalloc(sizeof(*currif));
795 iface_name = next_word(&rest_of_line);
796 address_family_name = next_word(&rest_of_line);
797 method_name = next_word(&rest_of_line);
799 if (method_name == NULL)
800 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
802 /* ship any trailing whitespace */
803 rest_of_line = skip_whitespace(rest_of_line);
805 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
806 bb_error_msg_and_die("too many parameters \"%s\"", buf);
808 currif->iface = xstrdup(iface_name);
810 currif->address_family = get_address_family(addr_fams, address_family_name);
811 if (!currif->address_family)
812 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
814 currif->method = get_method(currif->address_family, method_name);
816 bb_error_msg_and_die("unknown method \"%s\"", method_name);
818 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
819 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
820 if ((strcmp(tmp->iface, currif->iface) == 0)
821 && (tmp->address_family == currif->address_family)
823 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
826 llist_add_to_end(&(defn->ifaces), (char*)currif);
828 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
829 currently_processing = IFACE;
830 } else if (strcmp(first_word, "auto") == 0) {
831 while ((first_word = next_word(&rest_of_line)) != NULL) {
833 /* Check the interface isnt already listed */
834 if (llist_find_str(defn->autointerfaces, first_word)) {
835 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
838 /* Add the interface to the list */
839 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
840 debug_noise("\nauto %s\n", first_word);
842 currently_processing = NONE;
844 switch (currently_processing) {
846 if (rest_of_line[0] == '\0')
847 bb_error_msg_and_die("option with empty value \"%s\"", buf);
849 if (strcmp(first_word, "up") != 0
850 && strcmp(first_word, "down") != 0
851 && strcmp(first_word, "pre-up") != 0
852 && strcmp(first_word, "post-down") != 0
855 for (i = 0; i < currif->n_options; i++) {
856 if (strcmp(currif->option[i].name, first_word) == 0)
857 bb_error_msg_and_die("duplicate option \"%s\"", buf);
860 if (currif->n_options >= currif->max_options) {
861 currif->max_options += 10;
862 currif->option = xrealloc(currif->option,
863 sizeof(*currif->option) * currif->max_options);
865 debug_noise("\t%s=%s\n", first_word, rest_of_line);
866 currif->option[currif->n_options].name = xstrdup(first_word);
867 currif->option[currif->n_options].value = xstrdup(rest_of_line);
871 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
872 if (strcmp(first_word, "script") == 0) {
873 if (currmap->script != NULL)
874 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
875 currmap->script = xstrdup(next_word(&rest_of_line));
876 } else if (strcmp(first_word, "map") == 0) {
877 if (currmap->n_mappings >= currmap->max_mappings) {
878 currmap->max_mappings = currmap->max_mappings * 2 + 1;
879 currmap->mapping = xrealloc(currmap->mapping,
880 sizeof(char *) * currmap->max_mappings);
882 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
883 currmap->n_mappings++;
885 bb_error_msg_and_die("misplaced option \"%s\"", buf);
891 bb_error_msg_and_die("misplaced option \"%s\"", buf);
895 } /* while (fgets) */
897 if (ferror(f) != 0) {
898 /* ferror does NOT set errno! */
899 bb_error_msg_and_die("%s: I/O error", filename);
906 static char *setlocalenv(const char *format, const char *name, const char *value)
913 result = xasprintf(format, name, value);
915 for (dst = src = result; (c = *src) != '=' && c; src++) {
918 if (c >= 'a' && c <= 'z')
920 if (isalnum(c) || c == '_')
923 overlapping_strcpy(dst, src);
928 static void set_environ(struct interface_defn_t *iface, const char *mode)
933 if (G.my_environ != NULL) {
934 for (pp = G.my_environ; *pp; pp++) {
940 /* note: last element will stay NULL: */
941 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
944 for (i = 0; i < iface->n_options; i++) {
945 if (strcmp(iface->option[i].name, "up") == 0
946 || strcmp(iface->option[i].name, "down") == 0
947 || strcmp(iface->option[i].name, "pre-up") == 0
948 || strcmp(iface->option[i].name, "post-down") == 0
952 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
955 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
956 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
957 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
958 *pp++ = setlocalenv("%s=%s", "MODE", mode);
960 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
963 static int doit(char *str)
965 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
968 if (!(option_mask32 & OPT_no_act)) {
975 case -1: /* failure */
978 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
981 safe_waitpid(child, &status, 0);
982 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
989 static int execute_all(struct interface_defn_t *ifd, const char *opt)
993 for (i = 0; i < ifd->n_options; i++) {
994 if (strcmp(ifd->option[i].name, opt) == 0) {
995 if (!doit(ifd->option[i].value)) {
1001 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1002 /* heh, we don't bother free'ing it */
1006 static int check(char *str)
1011 static int iface_up(struct interface_defn_t *iface)
1013 if (!iface->method->up(iface, check)) return -1;
1014 set_environ(iface, "start");
1015 if (!execute_all(iface, "pre-up")) return 0;
1016 if (!iface->method->up(iface, doit)) return 0;
1017 if (!execute_all(iface, "up")) return 0;
1021 static int iface_down(struct interface_defn_t *iface)
1023 if (!iface->method->down(iface,check)) return -1;
1024 set_environ(iface, "stop");
1025 if (!execute_all(iface, "down")) return 0;
1026 if (!iface->method->down(iface, doit)) return 0;
1027 if (!execute_all(iface, "post-down")) return 0;
1031 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1032 static int popen2(FILE **in, FILE **out, char *command, char *param)
1034 char *argv[3] = { command, param, NULL };
1035 struct fd_pair infd, outfd;
1045 case -1: /* failure */
1046 bb_perror_msg_and_die("vfork");
1048 /* NB: close _first_, then move fds! */
1051 xmove_fd(infd.rd, 0);
1052 xmove_fd(outfd.wr, 1);
1053 BB_EXECVP(command, argv);
1059 *in = xfdopen_for_write(infd.wr);
1060 *out = xfdopen_for_read(outfd.rd);
1064 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1070 char *logical = xstrdup(physical);
1072 /* Run the mapping script. Never fails. */
1073 pid = popen2(&in, &out, map->script, physical);
1075 /* Write mappings to stdin of mapping script. */
1076 for (i = 0; i < map->n_mappings; i++) {
1077 fprintf(in, "%s\n", map->mapping[i]);
1080 safe_waitpid(pid, &status, 0);
1082 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1083 /* If the mapping script exited successfully, try to
1084 * grab a line of output and use that as the name of the
1085 * logical interface. */
1086 char *new_logical = xmalloc_fgetline(out);
1089 /* If we are able to read a line of output from the script,
1090 * remove any trailing whitespace and use this value
1091 * as the name of the logical interface. */
1092 char *pch = new_logical + strlen(new_logical) - 1;
1094 while (pch >= new_logical && isspace(*pch))
1098 logical = new_logical;
1106 #endif /* FEATURE_IFUPDOWN_MAPPING */
1108 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1110 unsigned iface_len = strlen(iface);
1111 llist_t *search = state_list;
1114 if ((strncmp(search->data, iface, iface_len) == 0)
1115 && (search->data[iface_len] == '=')
1119 search = search->link;
1124 /* read the previous state from the state file */
1125 static llist_t *read_iface_state(void)
1127 llist_t *state_list = NULL;
1128 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1131 char *start, *end_ptr;
1132 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1133 /* We should only need to check for a single character */
1134 end_ptr = start + strcspn(start, " \t\n");
1136 llist_add_to(&state_list, start);
1144 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1145 int ifupdown_main(int argc, char **argv)
1147 int (*cmds)(struct interface_defn_t *);
1148 struct interfaces_file_t *defn;
1149 llist_t *target_list = NULL;
1150 const char *interfaces = "/etc/network/interfaces";
1151 bool any_failures = 0;
1155 G.startup_PATH = getenv("PATH");
1158 if (applet_name[2] == 'u') {
1163 getopt32(argv, OPTION_STR, &interfaces);
1164 if (argc - optind > 0) {
1165 if (DO_ALL) bb_show_usage();
1167 if (!DO_ALL) bb_show_usage();
1170 debug_noise("reading %s file:\n", interfaces);
1171 defn = read_interfaces(interfaces);
1172 debug_noise("\ndone reading %s\n\n", interfaces);
1174 /* Create a list of interfaces to work on */
1176 target_list = defn->autointerfaces;
1178 llist_add_to_end(&target_list, argv[optind]);
1181 /* Update the interfaces */
1182 while (target_list) {
1183 llist_t *iface_list;
1184 struct interface_defn_t *currif;
1191 iface = xstrdup(target_list->data);
1192 target_list = target_list->link;
1194 pch = strchr(iface, '=');
1197 liface = xstrdup(pch + 1);
1199 liface = xstrdup(iface);
1203 llist_t *state_list = read_iface_state();
1204 const llist_t *iface_state = find_iface_state(state_list, iface);
1206 if (cmds == iface_up) {
1209 bb_error_msg("interface %s already configured", iface);
1215 bb_error_msg("interface %s not configured", iface);
1219 llist_free(state_list, free);
1222 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1223 if ((cmds == iface_up) && !NO_MAPPINGS) {
1224 struct mapping_defn_t *currmap;
1226 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1228 for (i = 0; i < currmap->n_matches; i++) {
1229 if (fnmatch(currmap->match[i], liface, 0) != 0)
1232 printf("Running mapping script %s on %s\n", currmap->script, liface);
1234 liface = run_mapping(iface, currmap);
1241 iface_list = defn->ifaces;
1242 while (iface_list) {
1243 currif = (struct interface_defn_t *) iface_list->data;
1244 if (strcmp(liface, currif->iface) == 0) {
1245 char *oldiface = currif->iface;
1248 currif->iface = iface;
1250 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1252 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1253 cmds_ret = cmds(currif);
1254 if (cmds_ret == -1) {
1255 bb_error_msg("don't seem to have all the variables for %s/%s",
1256 liface, currif->address_family->name);
1258 } else if (cmds_ret == 0) {
1262 currif->iface = oldiface;
1264 iface_list = iface_list->link;
1270 if (!okay && !FORCE) {
1271 bb_error_msg("ignoring unknown interface %s", liface);
1273 } else if (!NO_ACT) {
1274 /* update the state file */
1277 llist_t *state_list = read_iface_state();
1278 llist_t *iface_state = find_iface_state(state_list, iface);
1280 if (cmds == iface_up) {
1281 char * const newiface = xasprintf("%s=%s", iface, liface);
1282 if (iface_state == NULL) {
1283 llist_add_to_end(&state_list, newiface);
1285 free(iface_state->data);
1286 iface_state->data = newiface;
1289 /* Remove an interface from state_list */
1290 llist_unlink(&state_list, iface_state);
1291 free(llist_pop(&iface_state));
1294 /* Actually write the new state */
1295 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1299 fprintf(state_fp, "%s\n", state->data);
1301 state = state->link;
1304 llist_free(state_list, free);
1308 return any_failures;