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"
23 //usage: " -a De/configure all interfaces automatically"
24 //usage: "\n -i FILE Use FILE for interface definitions"
25 //usage: "\n -n Print out what would happen, but don't do it"
26 //usage: IF_FEATURE_IFUPDOWN_MAPPING(
27 //usage: "\n (note: doesn't disable mappings)"
28 //usage: "\n -m Don't run any mappings"
30 //usage: "\n -v Print out what would happen before doing it"
31 //usage: "\n -f Force de/configuration"
33 //usage:#define ifdown_trivial_usage
34 //usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..."
35 //usage:#define ifdown_full_usage "\n\n"
36 //usage: " -a De/configure all interfaces automatically"
37 //usage: "\n -i FILE Use FILE for interface definitions"
38 //usage: "\n -n Print out what would happen, but don't do it"
39 //usage: IF_FEATURE_IFUPDOWN_MAPPING(
40 //usage: "\n (note: doesn't disable mappings)"
41 //usage: "\n -m Don't run any mappings"
43 //usage: "\n -v Print out what would happen before doing it"
44 //usage: "\n -f Force de/configuration"
47 /* After libbb.h, since it needs sys/types.h on some systems */
48 #include <sys/utsname.h>
51 #define MAX_OPT_DEPTH 10
52 #define EUNBALBRACK 10001
53 #define EUNDEFVAR 10002
54 #define EUNBALPER 10000
56 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
57 #define MAX_INTERFACE_LENGTH 10
60 #define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
62 #define debug_noise(args...) /*fprintf(stderr, args)*/
64 /* Forward declaration */
65 struct interface_defn_t;
67 typedef int execfn(char *command);
71 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
72 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
75 struct address_family_t {
78 const struct method_t *method;
81 struct mapping_defn_t {
82 struct mapping_defn_t *next;
99 struct interface_defn_t {
100 const struct address_family_t *address_family;
101 const struct method_t *method;
105 struct variable_t *option;
108 struct interfaces_file_t {
109 llist_t *autointerfaces;
111 struct mapping_defn_t *mappings;
115 #define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
121 OPT_no_mappings = 0x10,
123 #define DO_ALL (option_mask32 & OPT_do_all)
124 #define NO_ACT (option_mask32 & OPT_no_act)
125 #define VERBOSE (option_mask32 & OPT_verbose)
126 #define FORCE (option_mask32 & OPT_force)
127 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
132 const char *startup_PATH;
135 #define G (*(struct globals*)&bb_common_bufsiz1)
136 #define INIT_G() do { } while (0)
139 static const char keywords_up_down[] ALIGN1 =
149 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
151 static void addstr(char **bufp, const char *str, size_t str_length)
153 /* xasprintf trick will be smaller, but we are often
154 * called with str_length == 1 - don't want to have
155 * THAT much of malloc/freeing! */
157 int len = (buf ? strlen(buf) : 0);
159 buf = xrealloc(buf, len + str_length);
160 /* copies at most str_length-1 chars! */
161 safe_strncpy(buf + len, str, str_length);
165 static int strncmpz(const char *l, const char *r, size_t llen)
167 int i = strncmp(l, r, llen);
170 return - (unsigned char)r[llen];
174 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
178 if (strncmpz(id, "iface", idlen) == 0) {
179 // ubuntu's ifup doesn't do this:
180 //static char *label_buf;
183 //label_buf = xstrdup(ifd->iface);
184 // Remove virtual iface suffix
185 //result = strchrnul(label_buf, ':');
191 if (strncmpz(id, "label", idlen) == 0) {
194 for (i = 0; i < ifd->n_options; i++) {
195 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
196 return ifd->option[i].value;
202 # if ENABLE_FEATURE_IFUPDOWN_IP
203 static int count_netmask_bits(const char *dotted_quad)
206 // unsigned a, b, c, d;
207 // /* Found a netmask... Check if it is dotted quad */
208 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
210 // if ((a|b|c|d) >> 8)
211 // return -1; /* one of numbers is >= 256 */
212 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
213 // d = ~d; /* 11110000 -> 00001111 */
215 /* Shorter version */
220 if (inet_aton(dotted_quad, &ip) == 0)
221 return -1; /* malformed dotted IP */
222 d = ntohl(ip.s_addr); /* IP in host order */
223 d = ~d; /* 11110000 -> 00001111 */
224 if (d & (d+1)) /* check that it is in 00001111 form */
225 return -1; /* no it is not */
235 static char *parse(const char *command, struct interface_defn_t *ifd)
237 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
238 int okay[MAX_OPT_DEPTH] = { 1 };
245 addstr(&result, command, 1);
250 addstr(&result, command + 1, 1);
253 addstr(&result, command, 1);
258 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
259 old_pos[opt_depth] = result ? strlen(result) : 0;
264 addstr(&result, "[", 1);
269 if (command[1] == ']' && opt_depth > 1) {
271 if (!okay[opt_depth]) {
272 result[old_pos[opt_depth]] = '\0';
276 addstr(&result, "]", 1);
286 nextpercent = strchr(command, '%');
293 varvalue = get_var(command, nextpercent - command, ifd);
296 # if ENABLE_FEATURE_IFUPDOWN_IP
297 /* "hwaddress <class> <address>":
298 * unlike ifconfig, ip doesnt want <class>
299 * (usually "ether" keyword). Skip it. */
300 if (strncmp(command, "hwaddress", 9) == 0) {
301 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
304 addstr(&result, varvalue, strlen(varvalue));
306 # if ENABLE_FEATURE_IFUPDOWN_IP
307 /* Sigh... Add a special case for 'ip' to convert from
308 * dotted quad to bit count style netmasks. */
309 if (strncmp(command, "bnmask", 6) == 0) {
311 varvalue = get_var("netmask", 7, ifd);
313 res = count_netmask_bits(varvalue);
315 const char *argument = utoa(res);
316 addstr(&result, argument, strlen(argument));
317 command = nextpercent + 1;
323 okay[opt_depth - 1] = 0;
326 command = nextpercent + 1;
347 /* execute() returns 1 for success and 0 for failure */
348 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
353 out = parse(command, ifd);
358 /* out == "": parsed ok but not all needed variables known, skip */
359 ret = out[0] ? (*exec)(out) : 1;
368 #endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
371 #if ENABLE_FEATURE_IFUPDOWN_IPV6
373 static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
375 # if ENABLE_FEATURE_IFUPDOWN_IP
377 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
378 result += execute("ip link set %iface% up", ifd, exec);
379 return ((result == 2) ? 2 : 0);
381 return execute("ifconfig %iface% add ::1", ifd, exec);
385 static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
387 # if ENABLE_FEATURE_IFUPDOWN_IP
388 return execute("ip link set %iface% down", ifd, exec);
390 return execute("ifconfig %iface% del ::1", ifd, exec);
394 static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
399 static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
402 # if ENABLE_FEATURE_IFUPDOWN_IP
403 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
404 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
405 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
406 result += execute("[[ip route add ::/0 via %gateway%]][[ prio %metric%]]", ifd, exec);
408 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
409 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
410 result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec);
412 return ((result == 3) ? 3 : 0);
415 static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
417 # if ENABLE_FEATURE_IFUPDOWN_IP
418 return execute("ip link set %iface% down", ifd, exec);
420 return execute("ifconfig %iface% down", ifd, exec);
424 # if ENABLE_FEATURE_IFUPDOWN_IP
425 static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
428 result = execute("ip tunnel add %iface% mode sit remote "
429 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
430 result += execute("ip link set %iface% up", ifd, exec);
431 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
432 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
433 return ((result == 4) ? 4 : 0);
436 static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
438 return execute("ip tunnel del %iface%", ifd, exec);
442 static const struct method_t methods6[] = {
443 # if ENABLE_FEATURE_IFUPDOWN_IP
444 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
446 { "static" , static_up6 , static_down6 , },
447 { "manual" , manual_up_down6 , manual_up_down6 , },
448 { "loopback" , loopback_up6 , loopback_down6 , },
451 static const struct address_family_t addr_inet6 = {
453 ARRAY_SIZE(methods6),
457 #endif /* FEATURE_IFUPDOWN_IPV6 */
460 #if ENABLE_FEATURE_IFUPDOWN_IPV4
462 static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
464 # if ENABLE_FEATURE_IFUPDOWN_IP
466 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
467 result += execute("ip link set %iface% up", ifd, exec);
468 return ((result == 2) ? 2 : 0);
470 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
474 static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
476 # 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);
480 return ((result == 2) ? 2 : 0);
482 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
486 static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
489 # if ENABLE_FEATURE_IFUPDOWN_IP
490 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
491 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
492 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
493 result += execute("[[ip route add default via %gateway% dev %iface%[[ prio %metric%]]]]", ifd, exec);
494 return ((result == 3) ? 3 : 0);
496 /* ifconfig said to set iface up before it processes hw %hwaddress%,
497 * which then of course fails. Thus we run two separate ifconfig */
498 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
500 result += execute("ifconfig %iface% %address% netmask %netmask%"
501 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
503 result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec);
504 return ((result == 3) ? 3 : 0);
508 static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
511 # if ENABLE_FEATURE_IFUPDOWN_IP
512 result = execute("ip addr flush dev %iface%", ifd, exec);
513 result += execute("ip link set %iface% down", ifd, exec);
515 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
516 /* Bringing the interface down deletes the routes in itself.
517 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
519 result += execute("ifconfig %iface% down", ifd, exec);
521 return ((result == 2) ? 2 : 0);
524 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
525 struct dhcp_client_t {
527 const char *startcmd;
531 static const struct dhcp_client_t ext_dhcp_clients[] = {
533 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
537 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
538 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
541 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
542 "pump -i %iface% -k",
545 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
546 "[[ -s %script%]][[ %udhcpc_opts%]]",
547 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
550 # endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
552 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
553 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
556 # if ENABLE_FEATURE_IFUPDOWN_IP
557 /* ip doesn't up iface when it configures it (unlike ifconfig) */
558 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
561 /* needed if we have hwaddress on dhcp iface */
562 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
565 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
566 if (exists_execable(ext_dhcp_clients[i].name))
567 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
569 bb_error_msg("no dhcp clients found");
573 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
575 # if ENABLE_FEATURE_IFUPDOWN_IP
576 /* ip doesn't up iface when it configures it (unlike ifconfig) */
577 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
580 /* needed if we have hwaddress on dhcp iface */
581 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
584 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
585 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
589 static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
590 execfn *exec UNUSED_PARAM)
592 return 0; /* no dhcp support */
596 # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
597 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
602 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
603 if (exists_execable(ext_dhcp_clients[i].name)) {
604 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
611 bb_error_msg("warning: no dhcp clients found and stopped");
613 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
614 and it may come back up because udhcpc is still shutting down */
616 result += static_down(ifd, exec);
617 return ((result == 3) ? 3 : 0);
620 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
624 "test -f /var/run/udhcpc.%iface%.pid && "
625 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
627 /* Also bring the hardware interface down since
628 killing the dhcp client alone doesn't do it.
629 This enables consecutive ifup->ifdown->ifup */
630 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
631 and it may come back up because udhcpc is still shutting down */
633 result += static_down(ifd, exec);
634 return ((result == 3) ? 3 : 0);
637 static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
638 execfn *exec UNUSED_PARAM)
640 return 0; /* no dhcp support */
644 static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
649 static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
651 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
652 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
653 " --returniffail --serverbcast", ifd, exec);
656 static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
658 return execute("pon[[ %provider%]]", ifd, exec);
661 static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
663 return execute("poff[[ %provider%]]", ifd, exec);
666 static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
668 return execute("start-stop-daemon --start -x wvdial "
669 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
672 static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
674 return execute("start-stop-daemon --stop -x wvdial "
675 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
678 static const struct method_t methods[] = {
679 { "manual" , manual_up_down, manual_up_down, },
680 { "wvdial" , wvdial_up , wvdial_down , },
681 { "ppp" , ppp_up , ppp_down , },
682 { "static" , static_up , static_down , },
683 { "bootp" , bootp_up , static_down , },
684 { "dhcp" , dhcp_up , dhcp_down , },
685 { "loopback", loopback_up , loopback_down , },
688 static const struct address_family_t addr_inet = {
694 #endif /* FEATURE_IFUPDOWN_IPV4 */
697 /* Returns pointer to the next word, or NULL.
698 * In 1st case, advances *buf to the word after this one.
700 static char *next_word(char **buf)
705 /* Skip over leading whitespace */
706 word = skip_whitespace(*buf);
712 /* Find the length of this word (can't be 0) */
713 length = strcspn(word, " \t\n");
715 /* Unless we are already at NUL, store NUL and advance */
716 if (word[length] != '\0')
717 word[length++] = '\0';
719 *buf = skip_whitespace(word + length);
724 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
731 for (i = 0; af[i]; i++) {
732 if (strcmp(af[i]->name, name) == 0) {
739 static const struct method_t *get_method(const struct address_family_t *af, char *name)
745 /* TODO: use index_in_str_array() */
746 for (i = 0; i < af->n_methods; i++) {
747 if (strcmp(af->method[i].name, name) == 0) {
748 return &af->method[i];
754 static struct interfaces_file_t *read_interfaces(const char *filename)
756 /* Let's try to be compatible.
758 * "man 5 interfaces" says:
759 * Lines starting with "#" are ignored. Note that end-of-line
760 * comments are NOT supported, comments must be on a line of their own.
761 * A line may be extended across multiple lines by making
762 * the last character a backslash.
764 * Seen elsewhere in example config file:
765 * A first non-blank "#" character makes the rest of the line
766 * be ignored. Blank lines are ignored. Lines may be indented freely.
767 * A "\" character at the very end of the line indicates the next line
768 * should be treated as a continuation of the current one.
770 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
771 struct mapping_defn_t *currmap = NULL;
773 struct interface_defn_t *currif = NULL;
774 struct interfaces_file_t *defn;
779 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
781 defn = xzalloc(sizeof(*defn));
782 f = xfopen_for_read(filename);
784 while ((buf = xmalloc_fgetline(f)) != NULL) {
786 /* Trailing "\" concatenates lines */
788 while ((p = last_char_is(buf, '\\')) != NULL) {
790 rest_of_line = xmalloc_fgetline(f);
793 p = xasprintf("%s%s", buf, rest_of_line);
800 first_word = next_word(&rest_of_line);
801 if (!first_word || *first_word == '#') {
803 continue; /* blank/comment line */
806 if (strcmp(first_word, "mapping") == 0) {
807 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
808 currmap = xzalloc(sizeof(*currmap));
810 while ((first_word = next_word(&rest_of_line)) != NULL) {
811 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
812 currmap->match[currmap->n_matches++] = xstrdup(first_word);
814 /*currmap->n_mappings = 0;*/
815 /*currmap->mapping = NULL;*/
816 /*currmap->script = NULL;*/
818 struct mapping_defn_t **where = &defn->mappings;
819 while (*where != NULL) {
820 where = &(*where)->next;
823 /*currmap->next = NULL;*/
825 debug_noise("Added mapping\n");
827 currently_processing = MAPPING;
828 } else if (strcmp(first_word, "iface") == 0) {
829 static const struct address_family_t *const addr_fams[] = {
830 #if ENABLE_FEATURE_IFUPDOWN_IPV4
833 #if ENABLE_FEATURE_IFUPDOWN_IPV6
839 char *address_family_name;
843 currif = xzalloc(sizeof(*currif));
844 iface_name = next_word(&rest_of_line);
845 address_family_name = next_word(&rest_of_line);
846 method_name = next_word(&rest_of_line);
848 if (method_name == NULL)
849 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
851 /* ship any trailing whitespace */
852 rest_of_line = skip_whitespace(rest_of_line);
854 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
855 bb_error_msg_and_die("too many parameters \"%s\"", buf);
857 currif->iface = xstrdup(iface_name);
859 currif->address_family = get_address_family(addr_fams, address_family_name);
860 if (!currif->address_family)
861 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
863 currif->method = get_method(currif->address_family, method_name);
865 bb_error_msg_and_die("unknown method \"%s\"", method_name);
867 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
868 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
869 if ((strcmp(tmp->iface, currif->iface) == 0)
870 && (tmp->address_family == currif->address_family)
872 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
875 llist_add_to_end(&(defn->ifaces), (char*)currif);
877 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
878 currently_processing = IFACE;
879 } else if (strcmp(first_word, "auto") == 0) {
880 while ((first_word = next_word(&rest_of_line)) != NULL) {
882 /* Check the interface isnt already listed */
883 if (llist_find_str(defn->autointerfaces, first_word)) {
884 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
887 /* Add the interface to the list */
888 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
889 debug_noise("\nauto %s\n", first_word);
891 currently_processing = NONE;
893 switch (currently_processing) {
895 if (rest_of_line[0] == '\0')
896 bb_error_msg_and_die("option with empty value \"%s\"", buf);
898 /* If not one of "up", "down",... words... */
899 if (index_in_strings(keywords_up_down, first_word) < 0) {
901 for (i = 0; i < currif->n_options; i++) {
902 if (strcmp(currif->option[i].name, first_word) == 0)
903 bb_error_msg_and_die("duplicate option \"%s\"", buf);
906 debug_noise("\t%s=%s\n", first_word, rest_of_line);
907 currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
908 currif->option[currif->n_options].name = xstrdup(first_word);
909 currif->option[currif->n_options].value = xstrdup(rest_of_line);
913 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
914 if (strcmp(first_word, "script") == 0) {
915 if (currmap->script != NULL)
916 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
917 currmap->script = xstrdup(next_word(&rest_of_line));
918 } else if (strcmp(first_word, "map") == 0) {
919 currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
920 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
921 currmap->n_mappings++;
923 bb_error_msg_and_die("misplaced option \"%s\"", buf);
929 bb_error_msg_and_die("misplaced option \"%s\"", buf);
933 } /* while (fgets) */
935 if (ferror(f) != 0) {
936 /* ferror does NOT set errno! */
937 bb_error_msg_and_die("%s: I/O error", filename);
944 static char *setlocalenv(const char *format, const char *name, const char *value)
951 result = xasprintf(format, name, value);
953 for (dst = src = result; (c = *src) != '=' && c; src++) {
956 if (c >= 'a' && c <= 'z')
958 if (isalnum(c) || c == '_')
961 overlapping_strcpy(dst, src);
966 static void set_environ(struct interface_defn_t *iface, const char *mode)
971 if (G.my_environ != NULL) {
972 for (pp = G.my_environ; *pp; pp++) {
978 /* note: last element will stay NULL: */
979 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
982 for (i = 0; i < iface->n_options; i++) {
983 if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
986 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
989 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
990 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
991 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
992 *pp++ = setlocalenv("%s=%s", "MODE", mode);
994 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
997 static int doit(char *str)
999 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
1002 if (!(option_mask32 & OPT_no_act)) {
1008 if (child < 0) /* failure */
1010 if (child == 0) { /* child */
1011 execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
1014 safe_waitpid(child, &status, 0);
1015 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1022 static int execute_all(struct interface_defn_t *ifd, const char *opt)
1026 for (i = 0; i < ifd->n_options; i++) {
1027 if (strcmp(ifd->option[i].name, opt) == 0) {
1028 if (!doit(ifd->option[i].value)) {
1034 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1035 /* heh, we don't bother free'ing it */
1039 static int check(char *str)
1044 static int iface_up(struct interface_defn_t *iface)
1046 if (!iface->method->up(iface, check)) return -1;
1047 set_environ(iface, "start");
1048 if (!execute_all(iface, "pre-up")) return 0;
1049 if (!iface->method->up(iface, doit)) return 0;
1050 if (!execute_all(iface, "up")) return 0;
1051 if (!execute_all(iface, "post-up")) return 0;
1055 static int iface_down(struct interface_defn_t *iface)
1057 if (!iface->method->down(iface,check)) return -1;
1058 set_environ(iface, "stop");
1059 if (!execute_all(iface, "pre-down")) return 0;
1060 if (!execute_all(iface, "down")) return 0;
1061 if (!iface->method->down(iface, doit)) return 0;
1062 if (!execute_all(iface, "post-down")) return 0;
1066 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1067 static int popen2(FILE **in, FILE **out, char *command, char *param)
1069 char *argv[3] = { command, param, NULL };
1070 struct fd_pair infd, outfd;
1081 /* NB: close _first_, then move fds! */
1084 xmove_fd(infd.rd, 0);
1085 xmove_fd(outfd.wr, 1);
1086 BB_EXECVP_or_die(argv);
1091 *in = xfdopen_for_write(infd.wr);
1092 *out = xfdopen_for_read(outfd.rd);
1096 static char *run_mapping(char *physical, struct mapping_defn_t *map)
1102 char *logical = xstrdup(physical);
1104 /* Run the mapping script. Never fails. */
1105 pid = popen2(&in, &out, map->script, physical);
1107 /* Write mappings to stdin of mapping script. */
1108 for (i = 0; i < map->n_mappings; i++) {
1109 fprintf(in, "%s\n", map->mapping[i]);
1112 safe_waitpid(pid, &status, 0);
1114 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1115 /* If the mapping script exited successfully, try to
1116 * grab a line of output and use that as the name of the
1117 * logical interface. */
1118 char *new_logical = xmalloc_fgetline(out);
1121 /* If we are able to read a line of output from the script,
1122 * remove any trailing whitespace and use this value
1123 * as the name of the logical interface. */
1124 char *pch = new_logical + strlen(new_logical) - 1;
1126 while (pch >= new_logical && isspace(*pch))
1130 logical = new_logical;
1138 #endif /* FEATURE_IFUPDOWN_MAPPING */
1140 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1142 unsigned iface_len = strlen(iface);
1143 llist_t *search = state_list;
1146 if ((strncmp(search->data, iface, iface_len) == 0)
1147 && (search->data[iface_len] == '=')
1151 search = search->link;
1156 /* read the previous state from the state file */
1157 static llist_t *read_iface_state(void)
1159 llist_t *state_list = NULL;
1160 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1163 char *start, *end_ptr;
1164 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1165 /* We should only need to check for a single character */
1166 end_ptr = start + strcspn(start, " \t\n");
1168 llist_add_to(&state_list, start);
1176 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1177 int ifupdown_main(int argc UNUSED_PARAM, char **argv)
1179 int (*cmds)(struct interface_defn_t *);
1180 struct interfaces_file_t *defn;
1181 llist_t *target_list = NULL;
1182 const char *interfaces = "/etc/network/interfaces";
1183 bool any_failures = 0;
1187 G.startup_PATH = getenv("PATH");
1188 G.shell = xstrdup(get_shell_name());
1191 if (applet_name[2] == 'u') {
1196 getopt32(argv, OPTION_STR, &interfaces);
1199 if (DO_ALL) bb_show_usage();
1201 if (!DO_ALL) bb_show_usage();
1204 debug_noise("reading %s file:\n", interfaces);
1205 defn = read_interfaces(interfaces);
1206 debug_noise("\ndone reading %s\n\n", interfaces);
1208 /* Create a list of interfaces to work on */
1210 target_list = defn->autointerfaces;
1212 llist_add_to_end(&target_list, argv[0]);
1215 /* Update the interfaces */
1216 while (target_list) {
1217 llist_t *iface_list;
1218 struct interface_defn_t *currif;
1225 iface = xstrdup(target_list->data);
1226 target_list = target_list->link;
1228 pch = strchr(iface, '=');
1231 liface = xstrdup(pch + 1);
1233 liface = xstrdup(iface);
1237 llist_t *state_list = read_iface_state();
1238 const llist_t *iface_state = find_iface_state(state_list, iface);
1240 if (cmds == iface_up) {
1243 bb_error_msg("interface %s already configured", iface);
1249 bb_error_msg("interface %s not configured", iface);
1253 llist_free(state_list, free);
1256 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1257 if ((cmds == iface_up) && !NO_MAPPINGS) {
1258 struct mapping_defn_t *currmap;
1260 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1262 for (i = 0; i < currmap->n_matches; i++) {
1263 if (fnmatch(currmap->match[i], liface, 0) != 0)
1266 printf("Running mapping script %s on %s\n", currmap->script, liface);
1268 liface = run_mapping(iface, currmap);
1275 iface_list = defn->ifaces;
1276 while (iface_list) {
1277 currif = (struct interface_defn_t *) iface_list->data;
1278 if (strcmp(liface, currif->iface) == 0) {
1279 char *oldiface = currif->iface;
1282 currif->iface = iface;
1284 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1286 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1287 cmds_ret = cmds(currif);
1288 if (cmds_ret == -1) {
1289 bb_error_msg("don't seem to have all the variables for %s/%s",
1290 liface, currif->address_family->name);
1292 } else if (cmds_ret == 0) {
1296 currif->iface = oldiface;
1298 iface_list = iface_list->link;
1304 if (!okay && !FORCE) {
1305 bb_error_msg("ignoring unknown interface %s", liface);
1307 } else if (!NO_ACT) {
1308 /* update the state file */
1311 llist_t *state_list = read_iface_state();
1312 llist_t *iface_state = find_iface_state(state_list, iface);
1314 if (cmds == iface_up && !any_failures) {
1315 char *newiface = xasprintf("%s=%s", iface, liface);
1317 llist_add_to_end(&state_list, newiface);
1319 free(iface_state->data);
1320 iface_state->data = newiface;
1323 /* Remove an interface from state_list */
1324 llist_unlink(&state_list, iface_state);
1325 free(llist_pop(&iface_state));
1328 /* Actually write the new state */
1329 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1333 fprintf(state_fp, "%s\n", state->data);
1335 state = state->link;
1338 llist_free(state_list, free);
1345 return any_failures;