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.
20 //usage:#define ifup_trivial_usage
21 //usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
22 //usage:#define ifup_full_usage "\n\n"
24 //usage: "\n -a De/configure all interfaces automatically"
25 //usage: "\n -i FILE Use FILE for interface definitions"
26 //usage: "\n -n Print out what would happen, but don't do it"
27 //usage: IF_FEATURE_IFUPDOWN_MAPPING(
28 //usage: "\n (note: doesn't disable mappings)"
29 //usage: "\n -m Don't run any mappings"
31 //usage: "\n -v Print out what would happen before doing it"
32 //usage: "\n -f Force de/configuration"
34 //usage:#define ifdown_trivial_usage
35 //usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
36 //usage:#define ifdown_full_usage "\n\n"
38 //usage: "\n -a De/configure all interfaces automatically"
39 //usage: "\n -i FILE Use FILE for interface definitions"
40 //usage: "\n -n Print out what would happen, but don't do it"
41 //usage: IF_FEATURE_IFUPDOWN_MAPPING(
42 //usage: "\n (note: doesn't disable mappings)"
43 //usage: "\n -m Don't run any mappings"
45 //usage: "\n -v Print out what would happen before doing it"
46 //usage: "\n -f Force de/configuration"
49 /* After libbb.h, since it needs sys/types.h on some systems */
50 #include <sys/utsname.h>
53 #define MAX_OPT_DEPTH 10
54 #define EUNBALBRACK 10001
55 #define EUNDEFVAR 10002
56 #define EUNBALPER 10000
58 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
59 #define MAX_INTERFACE_LENGTH 10
62 #define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
64 #define debug_noise(args...) /*fprintf(stderr, args)*/
66 /* Forward declaration */
67 struct interface_defn_t;
69 typedef int execfn(char *command);
73 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
74 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
77 struct address_family_t {
80 const struct method_t *method;
83 struct mapping_defn_t {
84 struct mapping_defn_t *next;
102 struct interface_defn_t {
103 const struct address_family_t *address_family;
104 const struct method_t *method;
109 struct variable_t *option;
112 struct interfaces_file_t {
113 llist_t *autointerfaces;
115 struct mapping_defn_t *mappings;
119 #define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
125 OPT_no_mappings = 0x10,
127 #define DO_ALL (option_mask32 & OPT_do_all)
128 #define NO_ACT (option_mask32 & OPT_no_act)
129 #define VERBOSE (option_mask32 & OPT_verbose)
130 #define FORCE (option_mask32 & OPT_force)
131 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
136 const char *startup_PATH;
139 #define G (*(struct globals*)&bb_common_bufsiz1)
140 #define INIT_G() do { } while (0)
143 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
145 static void addstr(char **bufp, const char *str, size_t str_length)
147 /* xasprintf trick will be smaller, but we are often
148 * called with str_length == 1 - don't want to have
149 * THAT much of malloc/freeing! */
151 int len = (buf ? strlen(buf) : 0);
153 buf = xrealloc(buf, len + str_length);
154 /* copies at most str_length-1 chars! */
155 safe_strncpy(buf + len, str, str_length);
159 static int strncmpz(const char *l, const char *r, size_t llen)
161 int i = strncmp(l, r, llen);
164 return - (unsigned char)r[llen];
168 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
172 if (strncmpz(id, "iface", idlen) == 0) {
173 // ubuntu's ifup doesn't do this:
174 //static char *label_buf;
177 //label_buf = xstrdup(ifd->iface);
178 // Remove virtual iface suffix
179 //result = strchrnul(label_buf, ':');
185 if (strncmpz(id, "label", idlen) == 0) {
188 for (i = 0; i < ifd->n_options; i++) {
189 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
190 return ifd->option[i].value;
196 # if ENABLE_FEATURE_IFUPDOWN_IP
197 static int count_netmask_bits(const char *dotted_quad)
200 // unsigned a, b, c, d;
201 // /* Found a netmask... Check if it is dotted quad */
202 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
204 // if ((a|b|c|d) >> 8)
205 // return -1; /* one of numbers is >= 256 */
206 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
207 // d = ~d; /* 11110000 -> 00001111 */
209 /* Shorter version */
214 if (inet_aton(dotted_quad, &ip) == 0)
215 return -1; /* malformed dotted IP */
216 d = ntohl(ip.s_addr); /* IP in host order */
217 d = ~d; /* 11110000 -> 00001111 */
218 if (d & (d+1)) /* check that it is in 00001111 form */
219 return -1; /* no it is not */
229 static char *parse(const char *command, struct interface_defn_t *ifd)
231 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
232 int okay[MAX_OPT_DEPTH] = { 1 };
239 addstr(&result, command, 1);
244 addstr(&result, command + 1, 1);
247 addstr(&result, command, 1);
252 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
253 old_pos[opt_depth] = result ? strlen(result) : 0;
258 addstr(&result, "[", 1);
263 if (command[1] == ']' && opt_depth > 1) {
265 if (!okay[opt_depth]) {
266 result[old_pos[opt_depth]] = '\0';
270 addstr(&result, "]", 1);
280 nextpercent = strchr(command, '%');
287 varvalue = get_var(command, nextpercent - command, ifd);
290 # if ENABLE_FEATURE_IFUPDOWN_IP
291 /* "hwaddress <class> <address>":
292 * unlike ifconfig, ip doesnt want <class>
293 * (usually "ether" keyword). Skip it. */
294 if (strncmp(command, "hwaddress", 9) == 0) {
295 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
298 addstr(&result, varvalue, strlen(varvalue));
300 # if ENABLE_FEATURE_IFUPDOWN_IP
301 /* Sigh... Add a special case for 'ip' to convert from
302 * dotted quad to bit count style netmasks. */
303 if (strncmp(command, "bnmask", 6) == 0) {
305 varvalue = get_var("netmask", 7, ifd);
307 res = count_netmask_bits(varvalue);
309 const char *argument = utoa(res);
310 addstr(&result, argument, strlen(argument));
311 command = nextpercent + 1;
317 okay[opt_depth - 1] = 0;
320 command = nextpercent + 1;
341 /* execute() returns 1 for success and 0 for failure */
342 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
347 out = parse(command, ifd);
352 /* out == "": parsed ok but not all needed variables known, skip */
353 ret = out[0] ? (*exec)(out) : 1;
362 #endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
365 #if ENABLE_FEATURE_IFUPDOWN_IPV6
367 static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
369 # if ENABLE_FEATURE_IFUPDOWN_IP
371 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
372 result += execute("ip link set %iface% up", ifd, exec);
373 return ((result == 2) ? 2 : 0);
375 return execute("ifconfig %iface% add ::1", ifd, exec);
379 static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
381 # if ENABLE_FEATURE_IFUPDOWN_IP
382 return execute("ip link set %iface% down", ifd, exec);
384 return execute("ifconfig %iface% del ::1", ifd, exec);
388 static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
393 static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
396 # if ENABLE_FEATURE_IFUPDOWN_IP
397 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
398 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
399 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
400 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
402 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
403 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
404 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
406 return ((result == 3) ? 3 : 0);
409 static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
411 # if ENABLE_FEATURE_IFUPDOWN_IP
412 return execute("ip link set %iface% down", ifd, exec);
414 return execute("ifconfig %iface% down", ifd, exec);
418 # if ENABLE_FEATURE_IFUPDOWN_IP
419 static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
422 result = execute("ip tunnel add %iface% mode sit remote "
423 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
424 result += execute("ip link set %iface% up", ifd, exec);
425 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
426 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
427 return ((result == 4) ? 4 : 0);
430 static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
432 return execute("ip tunnel del %iface%", ifd, exec);
436 static const struct method_t methods6[] = {
437 # if ENABLE_FEATURE_IFUPDOWN_IP
438 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
440 { "static" , static_up6 , static_down6 , },
441 { "manual" , manual_up_down6 , manual_up_down6 , },
442 { "loopback" , loopback_up6 , loopback_down6 , },
445 static const struct address_family_t addr_inet6 = {
447 ARRAY_SIZE(methods6),
451 #endif /* FEATURE_IFUPDOWN_IPV6 */
454 #if ENABLE_FEATURE_IFUPDOWN_IPV4
456 static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
458 # if ENABLE_FEATURE_IFUPDOWN_IP
460 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
461 result += execute("ip link set %iface% up", ifd, exec);
462 return ((result == 2) ? 2 : 0);
464 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
468 static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
470 # if ENABLE_FEATURE_IFUPDOWN_IP
472 result = execute("ip addr flush dev %iface%", ifd, exec);
473 result += execute("ip link set %iface% down", ifd, exec);
474 return ((result == 2) ? 2 : 0);
476 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
480 static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
483 # if ENABLE_FEATURE_IFUPDOWN_IP
484 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
485 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
486 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
487 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
488 return ((result == 3) ? 3 : 0);
490 /* ifconfig said to set iface up before it processes hw %hwaddress%,
491 * which then of course fails. Thus we run two separate ifconfig */
492 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
494 result += execute("ifconfig %iface% %address% netmask %netmask%"
495 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
497 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
498 return ((result == 3) ? 3 : 0);
502 static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
505 # if ENABLE_FEATURE_IFUPDOWN_IP
506 result = execute("ip addr flush dev %iface%", ifd, exec);
507 result += execute("ip link set %iface% down", ifd, exec);
509 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
510 /* Bringing the interface down deletes the routes in itself.
511 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
513 result += execute("ifconfig %iface% down", ifd, exec);
515 return ((result == 2) ? 2 : 0);
518 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
519 struct dhcp_client_t {
521 const char *startcmd;
525 static const struct dhcp_client_t ext_dhcp_clients[] = {
527 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
531 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
532 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
535 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
536 "pump -i %iface% -k",
539 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
540 "[[ -s %script%]][[ %udhcpc_opts%]]",
541 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
544 # endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
546 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
547 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
550 # if ENABLE_FEATURE_IFUPDOWN_IP
551 /* ip doesn't up iface when it configures it (unlike ifconfig) */
552 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
555 /* needed if we have hwaddress on dhcp iface */
556 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
559 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
560 if (exists_execable(ext_dhcp_clients[i].name))
561 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
563 bb_error_msg("no dhcp clients found");
567 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
569 # if ENABLE_FEATURE_IFUPDOWN_IP
570 /* ip doesn't up iface when it configures it (unlike ifconfig) */
571 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
574 /* needed if we have hwaddress on dhcp iface */
575 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
578 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
579 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
583 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
584 execfn *exec UNUSED_PARAM)
586 return 0; /* no dhcp support */
590 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
591 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
596 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
597 if (exists_execable(ext_dhcp_clients[i].name)) {
598 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
605 bb_error_msg("warning: no dhcp clients found and stopped");
607 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
608 and it may come back up because udhcpc is still shutting down */
610 result += static_down(ifd, exec);
611 return ((result == 3) ? 3 : 0);
614 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
618 "test -f /var/run/udhcpc.%iface%.pid && "
619 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
621 /* Also bring the hardware interface down since
622 killing the dhcp client alone doesn't do it.
623 This enables consecutive ifup->ifdown->ifup */
624 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
625 and it may come back up because udhcpc is still shutting down */
627 result += static_down(ifd, exec);
628 return ((result == 3) ? 3 : 0);
631 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
632 execfn *exec UNUSED_PARAM)
634 return 0; /* no dhcp support */
638 static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
643 static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
645 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
646 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
647 " --returniffail --serverbcast", ifd, exec);
650 static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
652 return execute("pon[[ %provider%]]", ifd, exec);
655 static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
657 return execute("poff[[ %provider%]]", ifd, exec);
660 static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
662 return execute("start-stop-daemon --start -x wvdial "
663 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
666 static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
668 return execute("start-stop-daemon --stop -x wvdial "
669 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
672 static const struct method_t methods[] = {
673 { "manual" , manual_up_down, manual_up_down, },
674 { "wvdial" , wvdial_up , wvdial_down , },
675 { "ppp" , ppp_up , ppp_down , },
676 { "static" , static_up , static_down , },
677 { "bootp" , bootp_up , static_down , },
678 { "dhcp" , dhcp_up , dhcp_down , },
679 { "loopback", loopback_up , loopback_down , },
682 static const struct address_family_t addr_inet = {
688 #endif /* FEATURE_IFUPDOWN_IPV4 */
691 /* Returns pointer to the next word, or NULL.
692 * In 1st case, advances *buf to the word after this one.
694 static char *next_word(char **buf)
699 /* Skip over leading whitespace */
700 word = skip_whitespace(*buf);
706 /* Find the length of this word (can't be 0) */
707 length = strcspn(word, " \t\n");
709 /* Unless we are already at NUL, store NUL and advance */
710 if (word[length] != '\0')
711 word[length++] = '\0';
713 *buf = skip_whitespace(word + length);
718 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
725 for (i = 0; af[i]; i++) {
726 if (strcmp(af[i]->name, name) == 0) {
733 static const struct method_t *get_method(const struct address_family_t *af, char *name)
739 /* TODO: use index_in_str_array() */
740 for (i = 0; i < af->n_methods; i++) {
741 if (strcmp(af->method[i].name, name) == 0) {
742 return &af->method[i];
748 static struct interfaces_file_t *read_interfaces(const char *filename)
750 /* Let's try to be compatible.
752 * "man 5 interfaces" says:
753 * Lines starting with "#" are ignored. Note that end-of-line
754 * comments are NOT supported, comments must be on a line of their own.
755 * A line may be extended across multiple lines by making
756 * the last character a backslash.
758 * Seen elsewhere in example config file:
759 * A first non-blank "#" character makes the rest of the line
760 * be ignored. Blank lines are ignored. Lines may be indented freely.
761 * A "\" character at the very end of the line indicates the next line
762 * should be treated as a continuation of the current one.
764 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
765 struct mapping_defn_t *currmap = NULL;
767 struct interface_defn_t *currif = NULL;
768 struct interfaces_file_t *defn;
773 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
775 defn = xzalloc(sizeof(*defn));
776 f = xfopen_for_read(filename);
778 while ((buf = xmalloc_fgetline(f)) != NULL) {
780 /* Trailing "\" concatenates lines */
782 while ((p = last_char_is(buf, '\\')) != NULL) {
784 rest_of_line = xmalloc_fgetline(f);
787 p = xasprintf("%s%s", buf, rest_of_line);
794 first_word = next_word(&rest_of_line);
795 if (!first_word || *first_word == '#') {
797 continue; /* blank/comment line */
800 if (strcmp(first_word, "mapping") == 0) {
801 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
802 currmap = xzalloc(sizeof(*currmap));
804 while ((first_word = next_word(&rest_of_line)) != NULL) {
805 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
806 currmap->match[currmap->n_matches++] = xstrdup(first_word);
808 /*currmap->max_mappings = 0; - done by xzalloc */
809 /*currmap->n_mappings = 0;*/
810 /*currmap->mapping = NULL;*/
811 /*currmap->script = NULL;*/
813 struct mapping_defn_t **where = &defn->mappings;
814 while (*where != NULL) {
815 where = &(*where)->next;
818 /*currmap->next = NULL;*/
820 debug_noise("Added mapping\n");
822 currently_processing = MAPPING;
823 } else if (strcmp(first_word, "iface") == 0) {
824 static const struct address_family_t *const addr_fams[] = {
825 #if ENABLE_FEATURE_IFUPDOWN_IPV4
828 #if ENABLE_FEATURE_IFUPDOWN_IPV6
834 char *address_family_name;
838 currif = xzalloc(sizeof(*currif));
839 iface_name = next_word(&rest_of_line);
840 address_family_name = next_word(&rest_of_line);
841 method_name = next_word(&rest_of_line);
843 if (method_name == NULL)
844 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
846 /* ship any trailing whitespace */
847 rest_of_line = skip_whitespace(rest_of_line);
849 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
850 bb_error_msg_and_die("too many parameters \"%s\"", buf);
852 currif->iface = xstrdup(iface_name);
854 currif->address_family = get_address_family(addr_fams, address_family_name);
855 if (!currif->address_family)
856 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
858 currif->method = get_method(currif->address_family, method_name);
860 bb_error_msg_and_die("unknown method \"%s\"", method_name);
862 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
863 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
864 if ((strcmp(tmp->iface, currif->iface) == 0)
865 && (tmp->address_family == currif->address_family)
867 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
870 llist_add_to_end(&(defn->ifaces), (char*)currif);
872 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
873 currently_processing = IFACE;
874 } else if (strcmp(first_word, "auto") == 0) {
875 while ((first_word = next_word(&rest_of_line)) != NULL) {
877 /* Check the interface isnt already listed */
878 if (llist_find_str(defn->autointerfaces, first_word)) {
879 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
882 /* Add the interface to the list */
883 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
884 debug_noise("\nauto %s\n", first_word);
886 currently_processing = NONE;
888 switch (currently_processing) {
890 if (rest_of_line[0] == '\0')
891 bb_error_msg_and_die("option with empty value \"%s\"", buf);
893 if (strcmp(first_word, "up") != 0
894 && strcmp(first_word, "down") != 0
895 && strcmp(first_word, "pre-up") != 0
896 && strcmp(first_word, "post-down") != 0
899 for (i = 0; i < currif->n_options; i++) {
900 if (strcmp(currif->option[i].name, first_word) == 0)
901 bb_error_msg_and_die("duplicate option \"%s\"", buf);
904 if (currif->n_options >= currif->max_options) {
905 currif->max_options += 10;
906 currif->option = xrealloc(currif->option,
907 sizeof(*currif->option) * currif->max_options);
909 debug_noise("\t%s=%s\n", first_word, rest_of_line);
910 currif->option[currif->n_options].name = xstrdup(first_word);
911 currif->option[currif->n_options].value = xstrdup(rest_of_line);
915 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
916 if (strcmp(first_word, "script") == 0) {
917 if (currmap->script != NULL)
918 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
919 currmap->script = xstrdup(next_word(&rest_of_line));
920 } else if (strcmp(first_word, "map") == 0) {
921 if (currmap->n_mappings >= currmap->max_mappings) {
922 currmap->max_mappings = currmap->max_mappings * 2 + 1;
923 currmap->mapping = xrealloc(currmap->mapping,
924 sizeof(char *) * currmap->max_mappings);
926 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
927 currmap->n_mappings++;
929 bb_error_msg_and_die("misplaced option \"%s\"", buf);
935 bb_error_msg_and_die("misplaced option \"%s\"", buf);
939 } /* while (fgets) */
941 if (ferror(f) != 0) {
942 /* ferror does NOT set errno! */
943 bb_error_msg_and_die("%s: I/O error", filename);
950 static char *setlocalenv(const char *format, const char *name, const char *value)
957 result = xasprintf(format, name, value);
959 for (dst = src = result; (c = *src) != '=' && c; src++) {
962 if (c >= 'a' && c <= 'z')
964 if (isalnum(c) || c == '_')
967 overlapping_strcpy(dst, src);
972 static void set_environ(struct interface_defn_t *iface, const char *mode)
977 if (G.my_environ != NULL) {
978 for (pp = G.my_environ; *pp; pp++) {
984 /* note: last element will stay NULL: */
985 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
988 for (i = 0; i < iface->n_options; i++) {
989 if (strcmp(iface->option[i].name, "up") == 0
990 || strcmp(iface->option[i].name, "down") == 0
991 || strcmp(iface->option[i].name, "pre-up") == 0
992 || strcmp(iface->option[i].name, "post-down") == 0
996 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
999 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
1000 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
1001 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
1002 *pp++ = setlocalenv("%s=%s", "MODE", mode);
1004 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
1007 static int doit(char *str)
1009 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
1012 if (!(option_mask32 & OPT_no_act)) {
1018 if (child < 0) /* failure */
1020 if (child == 0) { /* child */
1021 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
1024 safe_waitpid(child, &status, 0);
1025 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1032 static int execute_all(struct interface_defn_t *ifd, const char *opt)
1036 for (i = 0; i < ifd->n_options; i++) {
1037 if (strcmp(ifd->option[i].name, opt) == 0) {
1038 if (!doit(ifd->option[i].value)) {
1044 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1045 /* heh, we don't bother free'ing it */
1049 static int check(char *str)
1054 static int iface_up(struct interface_defn_t *iface)
1056 if (!iface->method->up(iface, check)) return -1;
1057 set_environ(iface, "start");
1058 if (!execute_all(iface, "pre-up")) return 0;
1059 if (!iface->method->up(iface, doit)) return 0;
1060 if (!execute_all(iface, "up")) return 0;
1064 static int iface_down(struct interface_defn_t *iface)
1066 if (!iface->method->down(iface,check)) return -1;
1067 set_environ(iface, "stop");
1068 if (!execute_all(iface, "down")) return 0;
1069 if (!iface->method->down(iface, doit)) return 0;
1070 if (!execute_all(iface, "post-down")) return 0;
1074 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1075 static int popen2(FILE **in, FILE **out, char *command, char *param)
1077 char *argv[3] = { command, param, NULL };
1078 struct fd_pair infd, outfd;
1089 /* NB: close _first_, then move fds! */
1092 xmove_fd(infd.rd, 0);
1093 xmove_fd(outfd.wr, 1);
1094 BB_EXECVP_or_die(argv);
1099 *in = xfdopen_for_write(infd.wr);
1100 *out = xfdopen_for_read(outfd.rd);
1104 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1110 char *logical = xstrdup(physical);
1112 /* Run the mapping script. Never fails. */
1113 pid = popen2(&in, &out, map->script, physical);
1115 /* Write mappings to stdin of mapping script. */
1116 for (i = 0; i < map->n_mappings; i++) {
1117 fprintf(in, "%s\n", map->mapping[i]);
1120 safe_waitpid(pid, &status, 0);
1122 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1123 /* If the mapping script exited successfully, try to
1124 * grab a line of output and use that as the name of the
1125 * logical interface. */
1126 char *new_logical = xmalloc_fgetline(out);
1129 /* If we are able to read a line of output from the script,
1130 * remove any trailing whitespace and use this value
1131 * as the name of the logical interface. */
1132 char *pch = new_logical + strlen(new_logical) - 1;
1134 while (pch >= new_logical && isspace(*pch))
1138 logical = new_logical;
1146 #endif /* FEATURE_IFUPDOWN_MAPPING */
1148 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1150 unsigned iface_len = strlen(iface);
1151 llist_t *search = state_list;
1154 if ((strncmp(search->data, iface, iface_len) == 0)
1155 && (search->data[iface_len] == '=')
1159 search = search->link;
1164 /* read the previous state from the state file */
1165 static llist_t *read_iface_state(void)
1167 llist_t *state_list = NULL;
1168 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1171 char *start, *end_ptr;
1172 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1173 /* We should only need to check for a single character */
1174 end_ptr = start + strcspn(start, " \t\n");
1176 llist_add_to(&state_list, start);
1184 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1185 int ifupdown_main(int argc UNUSED_PARAM, char **argv)
1187 int (*cmds)(struct interface_defn_t *);
1188 struct interfaces_file_t *defn;
1189 llist_t *target_list = NULL;
1190 const char *interfaces = "/etc/network/interfaces";
1191 bool any_failures = 0;
1195 G.startup_PATH = getenv("PATH");
1196 G.shell = xstrdup(get_shell_name());
1199 if (applet_name[2] == 'u') {
1204 getopt32(argv, OPTION_STR, &interfaces);
1207 if (DO_ALL) bb_show_usage();
1209 if (!DO_ALL) bb_show_usage();
1212 debug_noise("reading %s file:\n", interfaces);
1213 defn = read_interfaces(interfaces);
1214 debug_noise("\ndone reading %s\n\n", interfaces);
1216 /* Create a list of interfaces to work on */
1218 target_list = defn->autointerfaces;
1220 llist_add_to_end(&target_list, argv[0]);
1223 /* Update the interfaces */
1224 while (target_list) {
1225 llist_t *iface_list;
1226 struct interface_defn_t *currif;
1233 iface = xstrdup(target_list->data);
1234 target_list = target_list->link;
1236 pch = strchr(iface, '=');
1239 liface = xstrdup(pch + 1);
1241 liface = xstrdup(iface);
1245 llist_t *state_list = read_iface_state();
1246 const llist_t *iface_state = find_iface_state(state_list, iface);
1248 if (cmds == iface_up) {
1251 bb_error_msg("interface %s already configured", iface);
1257 bb_error_msg("interface %s not configured", iface);
1261 llist_free(state_list, free);
1264 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1265 if ((cmds == iface_up) && !NO_MAPPINGS) {
1266 struct mapping_defn_t *currmap;
1268 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1270 for (i = 0; i < currmap->n_matches; i++) {
1271 if (fnmatch(currmap->match[i], liface, 0) != 0)
1274 printf("Running mapping script %s on %s\n", currmap->script, liface);
1276 liface = run_mapping(iface, currmap);
1283 iface_list = defn->ifaces;
1284 while (iface_list) {
1285 currif = (struct interface_defn_t *) iface_list->data;
1286 if (strcmp(liface, currif->iface) == 0) {
1287 char *oldiface = currif->iface;
1290 currif->iface = iface;
1292 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1294 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1295 cmds_ret = cmds(currif);
1296 if (cmds_ret == -1) {
1297 bb_error_msg("don't seem to have all the variables for %s/%s",
1298 liface, currif->address_family->name);
1300 } else if (cmds_ret == 0) {
1304 currif->iface = oldiface;
1306 iface_list = iface_list->link;
1312 if (!okay && !FORCE) {
1313 bb_error_msg("ignoring unknown interface %s", liface);
1315 } else if (!NO_ACT) {
1316 /* update the state file */
1319 llist_t *state_list = read_iface_state();
1320 llist_t *iface_state = find_iface_state(state_list, iface);
1322 if (cmds == iface_up) {
1323 char * const newiface = xasprintf("%s=%s", iface, liface);
1324 if (iface_state == NULL) {
1325 llist_add_to_end(&state_list, newiface);
1327 free(iface_state->data);
1328 iface_state->data = newiface;
1331 /* Remove an interface from state_list */
1332 llist_unlink(&state_list, iface_state);
1333 free(llist_pop(&iface_state));
1336 /* Actually write the new state */
1337 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1341 fprintf(state_fp, "%s\n", state->data);
1343 state = state->link;
1346 llist_free(state_list, free);
1353 return any_failures;