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 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);
46 int (*down)(struct interface_defn_t *ifd, execfn *e);
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;
90 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
96 OPT_no_mappings = 0x10,
98 #define DO_ALL (option_mask32 & OPT_do_all)
99 #define NO_ACT (option_mask32 & OPT_no_act)
100 #define VERBOSE (option_mask32 & OPT_verbose)
101 #define FORCE (option_mask32 & OPT_force)
102 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
104 static char **my_environ;
106 static const char *startup_PATH;
108 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
110 static void addstr(char **bufp, const char *str, size_t str_length)
112 /* xasprintf trick will be smaller, but we are often
113 * called with str_length == 1 - don't want to have
114 * THAT much of malloc/freeing! */
116 int len = (buf ? strlen(buf) : 0);
118 buf = xrealloc(buf, len + str_length);
119 /* copies at most str_length-1 chars! */
120 safe_strncpy(buf + len, str, str_length);
124 static int strncmpz(const char *l, const char *r, size_t llen)
126 int i = strncmp(l, r, llen);
133 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
137 if (strncmpz(id, "iface", idlen) == 0) {
138 static char *label_buf;
142 label_buf = xstrdup(ifd->iface);
143 // Remove virtual iface suffix - why?
144 // ubuntu's ifup doesn't do this
145 //result = strchrnul(label_buf, ':');
149 if (strncmpz(id, "label", idlen) == 0) {
152 for (i = 0; i < ifd->n_options; i++) {
153 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
154 return ifd->option[i].value;
160 #if ENABLE_FEATURE_IFUPDOWN_IP
161 static int count_netmask_bits(const char *dotted_quad)
164 // unsigned a, b, c, d;
165 // /* Found a netmask... Check if it is dotted quad */
166 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
168 // if ((a|b|c|d) >> 8)
169 // return -1; /* one of numbers is >= 256 */
170 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
171 // d = ~d; /* 11110000 -> 00001111 */
173 /* Shorter version */
178 if (inet_aton(dotted_quad, &ip) == 0)
179 return -1; /* malformed dotted IP */
180 d = ntohl(ip.s_addr); /* IP in host order */
181 d = ~d; /* 11110000 -> 00001111 */
182 if (d & (d+1)) /* check that it is in 00001111 form */
183 return -1; /* no it is not */
193 static char *parse(const char *command, struct interface_defn_t *ifd)
195 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
196 int okay[MAX_OPT_DEPTH] = { 1 };
203 addstr(&result, command, 1);
208 addstr(&result, command + 1, 1);
211 addstr(&result, command, 1);
216 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
217 old_pos[opt_depth] = result ? strlen(result) : 0;
222 addstr(&result, "[", 1);
227 if (command[1] == ']' && opt_depth > 1) {
229 if (!okay[opt_depth]) {
230 result[old_pos[opt_depth]] = '\0';
234 addstr(&result, "]", 1);
244 nextpercent = strchr(command, '%');
251 varvalue = get_var(command, nextpercent - command, ifd);
254 #if ENABLE_FEATURE_IFUPDOWN_IP
255 /* "hwaddress <class> <address>":
256 * unlike ifconfig, ip doesnt want <class>
257 * (usually "ether" keyword). Skip it. */
258 if (strncmp(command, "hwaddress", 9) == 0) {
259 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
262 addstr(&result, varvalue, strlen(varvalue));
264 #if ENABLE_FEATURE_IFUPDOWN_IP
265 /* Sigh... Add a special case for 'ip' to convert from
266 * dotted quad to bit count style netmasks. */
267 if (strncmp(command, "bnmask", 6) == 0) {
269 varvalue = get_var("netmask", 7, ifd);
271 res = count_netmask_bits(varvalue);
273 const char *argument = utoa(res);
274 addstr(&result, argument, strlen(argument));
275 command = nextpercent + 1;
281 okay[opt_depth - 1] = 0;
284 command = nextpercent + 1;
305 /* execute() returns 1 for success and 0 for failure */
306 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
311 out = parse(command, ifd);
316 /* out == "": parsed ok but not all needed variables known, skip */
317 ret = out[0] ? (*exec)(out) : 1;
327 #if ENABLE_FEATURE_IFUPDOWN_IPV6
328 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
330 #if ENABLE_FEATURE_IFUPDOWN_IP
332 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
333 result += execute("ip link set %iface% up", ifd, exec);
334 return ((result == 2) ? 2 : 0);
336 return execute("ifconfig %iface% add ::1", ifd, exec);
340 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
342 #if ENABLE_FEATURE_IFUPDOWN_IP
343 return execute("ip link set %iface% down", ifd, exec);
345 return execute("ifconfig %iface% del ::1", ifd, exec);
349 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
352 #if ENABLE_FEATURE_IFUPDOWN_IP
353 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
354 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
355 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
356 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
358 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
359 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
360 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
362 return ((result == 3) ? 3 : 0);
365 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
367 #if ENABLE_FEATURE_IFUPDOWN_IP
368 return execute("ip link set %iface% down", ifd, exec);
370 return execute("ifconfig %iface% down", ifd, exec);
374 #if ENABLE_FEATURE_IFUPDOWN_IP
375 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
378 result = execute("ip tunnel add %iface% mode sit remote "
379 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
380 result += execute("ip link set %iface% up", ifd, exec);
381 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
382 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
383 return ((result == 4) ? 4 : 0);
386 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
388 return execute("ip tunnel del %iface%", ifd, exec);
392 static const struct method_t methods6[] = {
393 #if ENABLE_FEATURE_IFUPDOWN_IP
394 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
396 { "static", static_up6, static_down6, },
397 { "loopback", loopback_up6, loopback_down6, },
400 static const struct address_family_t addr_inet6 = {
402 ARRAY_SIZE(methods6),
405 #endif /* FEATURE_IFUPDOWN_IPV6 */
407 #if ENABLE_FEATURE_IFUPDOWN_IPV4
408 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
410 #if ENABLE_FEATURE_IFUPDOWN_IP
412 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
413 result += execute("ip link set %iface% up", ifd, exec);
414 return ((result == 2) ? 2 : 0);
416 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
420 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
422 #if ENABLE_FEATURE_IFUPDOWN_IP
424 result = execute("ip addr flush dev %iface%", ifd, exec);
425 result += execute("ip link set %iface% down", ifd, exec);
426 return ((result == 2) ? 2 : 0);
428 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
432 static int static_up(struct interface_defn_t *ifd, execfn *exec)
435 #if ENABLE_FEATURE_IFUPDOWN_IP
436 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
437 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
438 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
439 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
440 return ((result == 3) ? 3 : 0);
442 /* ifconfig said to set iface up before it processes hw %hwaddress%,
443 * which then of course fails. Thus we run two separate ifconfig */
444 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
446 result += execute("ifconfig %iface% %address% netmask %netmask%"
447 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
449 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
450 return ((result == 3) ? 3 : 0);
454 static int static_down(struct interface_defn_t *ifd, execfn *exec)
457 #if ENABLE_FEATURE_IFUPDOWN_IP
458 result = execute("ip addr flush dev %iface%", ifd, exec);
459 result += execute("ip link set %iface% down", ifd, exec);
461 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
462 /* Bringing the interface down deletes the routes in itself.
463 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
465 result += execute("ifconfig %iface% down", ifd, exec);
467 return ((result == 2) ? 2 : 0);
470 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
474 const char *startcmd;
478 static const struct dhcp_client_t ext_dhcp_clients[] = {
480 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
484 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
485 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
488 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
489 "pump -i %iface% -k",
492 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
493 "[[ -s %script%]][[ %udhcpc_opts%]]",
494 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
497 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
499 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
500 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
503 #if ENABLE_FEATURE_IFUPDOWN_IP
504 /* ip doesn't up iface when it configures it (unlike ifconfig) */
505 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
508 /* needed if we have hwaddress on dhcp iface */
509 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
512 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
513 if (exists_execable(ext_dhcp_clients[i].name))
514 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
516 bb_error_msg("no dhcp clients found");
519 #elif ENABLE_APP_UDHCPC
520 static int 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 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
532 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
536 static int dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
537 execfn *exec UNUSED_PARAM)
539 return 0; /* no dhcp support */
543 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
544 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
549 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
550 if (exists_execable(ext_dhcp_clients[i].name)) {
551 result += execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
558 bb_error_msg("warning: no dhcp clients found and stopped");
560 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
561 and it may come back up because udhcpc is still shutting down */
563 result += static_down(ifd, exec);
564 return ((result == 3) ? 3 : 0);
566 #elif ENABLE_APP_UDHCPC
567 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
570 result = execute("kill "
571 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
572 /* Also bring the hardware interface down since
573 killing the dhcp client alone doesn't do it.
574 This enables consecutive ifup->ifdown->ifup */
575 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
576 and it may come back up because udhcpc is still shutting down */
578 result += static_down(ifd, exec);
579 return ((result == 3) ? 3 : 0);
582 static int dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
583 execfn *exec UNUSED_PARAM)
585 return 0; /* no dhcp support */
589 static int manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
594 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
596 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
597 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
598 " --returniffail --serverbcast", ifd, exec);
601 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
603 return execute("pon[[ %provider%]]", ifd, exec);
606 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
608 return execute("poff[[ %provider%]]", ifd, exec);
611 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
613 return execute("start-stop-daemon --start -x wvdial "
614 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
617 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
619 return execute("start-stop-daemon --stop -x wvdial "
620 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
623 static const struct method_t methods[] = {
624 { "manual", manual_up_down, manual_up_down, },
625 { "wvdial", wvdial_up, wvdial_down, },
626 { "ppp", ppp_up, ppp_down, },
627 { "static", static_up, static_down, },
628 { "bootp", bootp_up, static_down, },
629 { "dhcp", dhcp_up, dhcp_down, },
630 { "loopback", loopback_up, loopback_down, },
633 static const struct address_family_t addr_inet = {
639 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
641 static char *next_word(char **buf)
646 /* Skip over leading whitespace */
647 word = skip_whitespace(*buf);
653 /* Find the length of this word (can't be 0) */
654 length = strcspn(word, " \t\n");
656 /* Unless we are already at NUL, store NUL and advance */
657 if (word[length] != '\0')
658 word[length++] = '\0';
660 *buf = word + length;
665 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
672 for (i = 0; af[i]; i++) {
673 if (strcmp(af[i]->name, name) == 0) {
680 static const struct method_t *get_method(const struct address_family_t *af, char *name)
686 /* TODO: use index_in_str_array() */
687 for (i = 0; i < af->n_methods; i++) {
688 if (strcmp(af->method[i].name, name) == 0) {
689 return &af->method[i];
695 static const llist_t *find_list_string(const llist_t *list, const char *string)
701 if (strcmp(list->data, string) == 0) {
709 static struct interfaces_file_t *read_interfaces(const char *filename)
711 /* Let's try to be compatible.
713 * "man 5 interfaces" says:
714 * Lines starting with "#" are ignored. Note that end-of-line
715 * comments are NOT supported, comments must be on a line of their own.
716 * A line may be extended across multiple lines by making
717 * the last character a backslash.
719 * Seen elsewhere in example config file:
720 * A first non-blank "#" character makes the rest of the line
721 * be ignored. Blank lines are ignored. Lines may be indented freely.
722 * A "\" character at the very end of the line indicates the next line
723 * should be treated as a continuation of the current one.
725 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
726 struct mapping_defn_t *currmap = NULL;
728 struct interface_defn_t *currif = NULL;
729 struct interfaces_file_t *defn;
734 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
736 defn = xzalloc(sizeof(*defn));
737 f = xfopen_for_read(filename);
739 while ((buf = xmalloc_fgetline(f)) != NULL) {
741 /* Trailing "\" concatenates lines */
743 while ((p = last_char_is(buf, '\\')) != NULL) {
745 rest_of_line = xmalloc_fgetline(f);
748 p = xasprintf("%s%s", buf, rest_of_line);
755 first_word = next_word(&rest_of_line);
756 if (!first_word || *first_word == '#') {
758 continue; /* blank/comment line */
761 if (strcmp(first_word, "mapping") == 0) {
762 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
763 currmap = xzalloc(sizeof(*currmap));
765 while ((first_word = next_word(&rest_of_line)) != NULL) {
766 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
767 currmap->match[currmap->n_matches++] = xstrdup(first_word);
769 /*currmap->max_mappings = 0; - done by xzalloc */
770 /*currmap->n_mappings = 0;*/
771 /*currmap->mapping = NULL;*/
772 /*currmap->script = NULL;*/
774 struct mapping_defn_t **where = &defn->mappings;
775 while (*where != NULL) {
776 where = &(*where)->next;
779 /*currmap->next = NULL;*/
781 debug_noise("Added mapping\n");
783 currently_processing = MAPPING;
784 } else if (strcmp(first_word, "iface") == 0) {
785 static const struct address_family_t *const addr_fams[] = {
786 #if ENABLE_FEATURE_IFUPDOWN_IPV4
789 #if ENABLE_FEATURE_IFUPDOWN_IPV6
795 char *address_family_name;
799 currif = xzalloc(sizeof(*currif));
800 iface_name = next_word(&rest_of_line);
801 address_family_name = next_word(&rest_of_line);
802 method_name = next_word(&rest_of_line);
804 if (method_name == NULL)
805 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
807 /* ship any trailing whitespace */
808 rest_of_line = skip_whitespace(rest_of_line);
810 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
811 bb_error_msg_and_die("too many parameters \"%s\"", buf);
813 currif->iface = xstrdup(iface_name);
815 currif->address_family = get_address_family(addr_fams, address_family_name);
816 if (!currif->address_family)
817 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
819 currif->method = get_method(currif->address_family, method_name);
821 bb_error_msg_and_die("unknown method \"%s\"", method_name);
823 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
824 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
825 if ((strcmp(tmp->iface, currif->iface) == 0)
826 && (tmp->address_family == currif->address_family)
828 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
831 llist_add_to_end(&(defn->ifaces), (char*)currif);
833 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
834 currently_processing = IFACE;
835 } else if (strcmp(first_word, "auto") == 0) {
836 while ((first_word = next_word(&rest_of_line)) != NULL) {
838 /* Check the interface isnt already listed */
839 if (find_list_string(defn->autointerfaces, first_word)) {
840 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
843 /* Add the interface to the list */
844 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
845 debug_noise("\nauto %s\n", first_word);
847 currently_processing = NONE;
849 switch (currently_processing) {
851 if (rest_of_line[0] == '\0')
852 bb_error_msg_and_die("option with empty value \"%s\"", buf);
854 if (strcmp(first_word, "up") != 0
855 && strcmp(first_word, "down") != 0
856 && strcmp(first_word, "pre-up") != 0
857 && strcmp(first_word, "post-down") != 0
860 for (i = 0; i < currif->n_options; i++) {
861 if (strcmp(currif->option[i].name, first_word) == 0)
862 bb_error_msg_and_die("duplicate option \"%s\"", buf);
865 if (currif->n_options >= currif->max_options) {
866 currif->max_options += 10;
867 currif->option = xrealloc(currif->option,
868 sizeof(*currif->option) * currif->max_options);
870 debug_noise("\t%s=%s\n", first_word, rest_of_line);
871 currif->option[currif->n_options].name = xstrdup(first_word);
872 currif->option[currif->n_options].value = xstrdup(rest_of_line);
876 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
877 if (strcmp(first_word, "script") == 0) {
878 if (currmap->script != NULL)
879 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
880 currmap->script = xstrdup(next_word(&rest_of_line));
881 } else if (strcmp(first_word, "map") == 0) {
882 if (currmap->n_mappings >= currmap->max_mappings) {
883 currmap->max_mappings = currmap->max_mappings * 2 + 1;
884 currmap->mapping = xrealloc(currmap->mapping,
885 sizeof(char *) * currmap->max_mappings);
887 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
888 currmap->n_mappings++;
890 bb_error_msg_and_die("misplaced option \"%s\"", buf);
896 bb_error_msg_and_die("misplaced option \"%s\"", buf);
900 } /* while (fgets) */
902 if (ferror(f) != 0) {
903 /* ferror does NOT set errno! */
904 bb_error_msg_and_die("%s: I/O error", filename);
911 static char *setlocalenv(const char *format, const char *name, const char *value)
917 result = xasprintf(format, name, value);
919 for (here = there = result; *there != '=' && *there; there++) {
923 *there = toupper(*there);
925 if (isalnum(*there) || *there == '_') {
930 memmove(here, there, strlen(there) + 1);
935 static void set_environ(struct interface_defn_t *iface, const char *mode)
939 const int n_env_entries = iface->n_options + 5;
942 if (my_environ != NULL) {
943 for (ppch = my_environ; *ppch; ppch++) {
949 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
950 environend = my_environ;
952 for (i = 0; i < iface->n_options; i++) {
953 if (strcmp(iface->option[i].name, "up") == 0
954 || strcmp(iface->option[i].name, "down") == 0
955 || strcmp(iface->option[i].name, "pre-up") == 0
956 || strcmp(iface->option[i].name, "post-down") == 0
960 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
963 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
964 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
965 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
966 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
967 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
970 static int doit(char *str)
972 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
975 if (!(option_mask32 & OPT_no_act)) {
982 case -1: /* failure */
985 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
988 safe_waitpid(child, &status, 0);
989 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
996 static int execute_all(struct interface_defn_t *ifd, const char *opt)
1000 for (i = 0; i < ifd->n_options; i++) {
1001 if (strcmp(ifd->option[i].name, opt) == 0) {
1002 if (!doit(ifd->option[i].value)) {
1008 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1009 /* heh, we don't bother free'ing it */
1013 static int check(char *str)
1018 static int iface_up(struct interface_defn_t *iface)
1020 if (!iface->method->up(iface, check)) return -1;
1021 set_environ(iface, "start");
1022 if (!execute_all(iface, "pre-up")) return 0;
1023 if (!iface->method->up(iface, doit)) return 0;
1024 if (!execute_all(iface, "up")) return 0;
1028 static int iface_down(struct interface_defn_t *iface)
1030 if (!iface->method->down(iface,check)) return -1;
1031 set_environ(iface, "stop");
1032 if (!execute_all(iface, "down")) return 0;
1033 if (!iface->method->down(iface, doit)) return 0;
1034 if (!execute_all(iface, "post-down")) return 0;
1038 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1039 static int popen2(FILE **in, FILE **out, char *command, char *param)
1041 char *argv[3] = { command, param, NULL };
1042 struct fd_pair infd, outfd;
1052 case -1: /* failure */
1053 bb_perror_msg_and_die("vfork");
1055 /* NB: close _first_, then move fds! */
1058 xmove_fd(infd.rd, 0);
1059 xmove_fd(outfd.wr, 1);
1060 BB_EXECVP(command, argv);
1066 *in = fdopen(infd.wr, "w");
1067 *out = fdopen(outfd.rd, "r");
1071 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1077 char *logical = xstrdup(physical);
1079 /* Run the mapping script. Never fails. */
1080 pid = popen2(&in, &out, map->script, physical);
1082 /* Write mappings to stdin of mapping script. */
1083 for (i = 0; i < map->n_mappings; i++) {
1084 fprintf(in, "%s\n", map->mapping[i]);
1087 safe_waitpid(pid, &status, 0);
1089 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1090 /* If the mapping script exited successfully, try to
1091 * grab a line of output and use that as the name of the
1092 * logical interface. */
1093 char *new_logical = xmalloc_fgetline(out);
1096 /* If we are able to read a line of output from the script,
1097 * remove any trailing whitespace and use this value
1098 * as the name of the logical interface. */
1099 char *pch = new_logical + strlen(new_logical) - 1;
1101 while (pch >= new_logical && isspace(*pch))
1105 logical = new_logical;
1113 #endif /* FEATURE_IFUPDOWN_MAPPING */
1115 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1117 unsigned iface_len = strlen(iface);
1118 llist_t *search = state_list;
1121 if ((strncmp(search->data, iface, iface_len) == 0)
1122 && (search->data[iface_len] == '=')
1126 search = search->link;
1131 /* read the previous state from the state file */
1132 static llist_t *read_iface_state(void)
1134 llist_t *state_list = NULL;
1135 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1138 char *start, *end_ptr;
1139 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1140 /* We should only need to check for a single character */
1141 end_ptr = start + strcspn(start, " \t\n");
1143 llist_add_to(&state_list, start);
1151 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1152 int ifupdown_main(int argc, char **argv)
1154 int (*cmds)(struct interface_defn_t *);
1155 struct interfaces_file_t *defn;
1156 llist_t *target_list = NULL;
1157 const char *interfaces = "/etc/network/interfaces";
1158 bool any_failures = 0;
1161 if (applet_name[2] == 'u') {
1166 getopt32(argv, OPTION_STR, &interfaces);
1167 if (argc - optind > 0) {
1168 if (DO_ALL) bb_show_usage();
1170 if (!DO_ALL) bb_show_usage();
1173 debug_noise("reading %s file:\n", interfaces);
1174 defn = read_interfaces(interfaces);
1175 debug_noise("\ndone reading %s\n\n", interfaces);
1177 startup_PATH = getenv("PATH");
1178 if (!startup_PATH) startup_PATH = "";
1180 /* Create a list of interfaces to work on */
1182 target_list = defn->autointerfaces;
1184 llist_add_to_end(&target_list, argv[optind]);
1187 /* Update the interfaces */
1188 while (target_list) {
1189 llist_t *iface_list;
1190 struct interface_defn_t *currif;
1197 iface = xstrdup(target_list->data);
1198 target_list = target_list->link;
1200 pch = strchr(iface, '=');
1203 liface = xstrdup(pch + 1);
1205 liface = xstrdup(iface);
1209 llist_t *state_list = read_iface_state();
1210 const llist_t *iface_state = find_iface_state(state_list, iface);
1212 if (cmds == iface_up) {
1215 bb_error_msg("interface %s already configured", iface);
1221 bb_error_msg("interface %s not configured", iface);
1225 llist_free(state_list, free);
1228 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1229 if ((cmds == iface_up) && !NO_MAPPINGS) {
1230 struct mapping_defn_t *currmap;
1232 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1234 for (i = 0; i < currmap->n_matches; i++) {
1235 if (fnmatch(currmap->match[i], liface, 0) != 0)
1238 printf("Running mapping script %s on %s\n", currmap->script, liface);
1240 liface = run_mapping(iface, currmap);
1247 iface_list = defn->ifaces;
1248 while (iface_list) {
1249 currif = (struct interface_defn_t *) iface_list->data;
1250 if (strcmp(liface, currif->iface) == 0) {
1251 char *oldiface = currif->iface;
1254 currif->iface = iface;
1256 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1258 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1259 cmds_ret = cmds(currif);
1260 if (cmds_ret == -1) {
1261 bb_error_msg("don't seem to have all the variables for %s/%s",
1262 liface, currif->address_family->name);
1264 } else if (cmds_ret == 0) {
1268 currif->iface = oldiface;
1270 iface_list = iface_list->link;
1276 if (!okay && !FORCE) {
1277 bb_error_msg("ignoring unknown interface %s", liface);
1279 } else if (!NO_ACT) {
1280 /* update the state file */
1283 llist_t *state_list = read_iface_state();
1284 llist_t *iface_state = find_iface_state(state_list, iface);
1286 if (cmds == iface_up) {
1287 char * const newiface = xasprintf("%s=%s", iface, liface);
1288 if (iface_state == NULL) {
1289 llist_add_to_end(&state_list, newiface);
1291 free(iface_state->data);
1292 iface_state->data = newiface;
1295 /* Remove an interface from state_list */
1296 llist_unlink(&state_list, iface_state);
1297 free(llist_pop(&iface_state));
1300 /* Actually write the new state */
1301 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1305 fprintf(state_fp, "%s\n", state->data);
1307 state = state->link;
1310 llist_free(state_list, free);
1314 return any_failures;