1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
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.
15 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18 #include <sys/utsname.h>
24 #define MAX_OPT_DEPTH 10
25 #define EUNBALBRACK 10001
26 #define EUNDEFVAR 10002
27 #define EUNBALPER 10000
29 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
30 #define MAX_INTERFACE_LENGTH 10
33 #define debug_noise(args...) /*fprintf(stderr, args)*/
35 /* Forward declaration */
36 struct interface_defn_t;
38 typedef int execfn(char *command);
42 int (*up)(struct interface_defn_t *ifd, execfn *e);
43 int (*down)(struct interface_defn_t *ifd, execfn *e);
46 struct address_family_t {
49 const struct method_t *method;
52 struct mapping_defn_t {
53 struct mapping_defn_t *next;
71 struct interface_defn_t {
72 const struct address_family_t *address_family;
73 const struct method_t *method;
78 struct variable_t *option;
81 struct interfaces_file_t {
82 llist_t *autointerfaces;
84 struct mapping_defn_t *mappings;
87 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
93 OPT_no_mappings = 0x10,
95 #define DO_ALL (option_mask32 & OPT_do_all)
96 #define NO_ACT (option_mask32 & OPT_no_act)
97 #define VERBOSE (option_mask32 & OPT_verbose)
98 #define FORCE (option_mask32 & OPT_force)
99 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
101 static char **my_environ;
103 static const char *startup_PATH;
105 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
107 static void addstr(char **bufp, const char *str, size_t str_length)
109 /* xasprintf trick will be smaller, but we are often
110 * called with str_length == 1 - don't want to have
111 * THAT much of malloc/freeing! */
113 int len = (buf ? strlen(buf) : 0);
115 buf = xrealloc(buf, len + str_length);
116 /* copies at most str_length-1 chars! */
117 safe_strncpy(buf + len, str, str_length);
121 static int strncmpz(const char *l, const char *r, size_t llen)
123 int i = strncmp(l, r, llen);
130 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
134 if (strncmpz(id, "iface", idlen) == 0) {
136 static char label_buf[20];
137 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
138 result = strchr(label_buf, ':');
144 if (strncmpz(id, "label", idlen) == 0) {
147 for (i = 0; i < ifd->n_options; i++) {
148 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
149 return ifd->option[i].value;
155 #if ENABLE_FEATURE_IFUPDOWN_IP
156 static int count_netmask_bits(const char *dotted_quad)
159 // unsigned a, b, c, d;
160 // /* Found a netmask... Check if it is dotted quad */
161 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
163 // if ((a|b|c|d) >> 8)
164 // return -1; /* one of numbers is >= 256 */
165 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
166 // d = ~d; /* 11110000 -> 00001111 */
168 /* Shorter version */
173 if (inet_aton(dotted_quad, &ip) == 0)
174 return -1; /* malformed dotted IP */
175 d = ntohl(ip.s_addr); /* IP in host order */
176 d = ~d; /* 11110000 -> 00001111 */
177 if (d & (d+1)) /* check that it is in 00001111 form */
178 return -1; /* no it is not */
188 static char *parse(const char *command, struct interface_defn_t *ifd)
190 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
191 int okay[MAX_OPT_DEPTH] = { 1 };
198 addstr(&result, command, 1);
203 addstr(&result, command + 1, 1);
206 addstr(&result, command, 1);
211 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
212 old_pos[opt_depth] = result ? strlen(result) : 0;
217 addstr(&result, "[", 1);
222 if (command[1] == ']' && opt_depth > 1) {
224 if (!okay[opt_depth]) {
225 result[old_pos[opt_depth]] = '\0';
229 addstr(&result, "]", 1);
239 nextpercent = strchr(command, '%');
246 varvalue = get_var(command, nextpercent - command, ifd);
249 addstr(&result, varvalue, strlen(varvalue));
251 #if ENABLE_FEATURE_IFUPDOWN_IP
252 /* Sigh... Add a special case for 'ip' to convert from
253 * dotted quad to bit count style netmasks. */
254 if (strncmp(command, "bnmask", 6) == 0) {
256 varvalue = get_var("netmask", 7, ifd);
258 res = count_netmask_bits(varvalue);
260 const char *argument = utoa(res);
261 addstr(&result, argument, strlen(argument));
262 command = nextpercent + 1;
268 okay[opt_depth - 1] = 0;
271 command = nextpercent + 1;
292 /* execute() returns 1 for success and 0 for failure */
293 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
298 out = parse(command, ifd);
303 /* out == "": parsed ok but not all needed variables known, skip */
304 ret = out[0] ? (*exec)(out) : 1;
314 #if ENABLE_FEATURE_IFUPDOWN_IPV6
315 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
317 #if ENABLE_FEATURE_IFUPDOWN_IP
319 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
320 result += execute("ip link set %iface% up", ifd, exec);
321 return ((result == 2) ? 2 : 0);
323 return execute("ifconfig %iface% add ::1", ifd, exec);
327 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
329 #if ENABLE_FEATURE_IFUPDOWN_IP
330 return execute("ip link set %iface% down", ifd, exec);
332 return execute("ifconfig %iface% del ::1", ifd, exec);
336 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
339 #if ENABLE_FEATURE_IFUPDOWN_IP
340 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
341 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
342 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
343 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
345 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
346 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
347 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
349 return ((result == 3) ? 3 : 0);
352 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
354 #if ENABLE_FEATURE_IFUPDOWN_IP
355 return execute("ip link set %iface% down", ifd, exec);
357 return execute("ifconfig %iface% down", ifd, exec);
361 #if ENABLE_FEATURE_IFUPDOWN_IP
362 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
365 result = execute("ip tunnel add %iface% mode sit remote "
366 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
367 result += execute("ip link set %iface% up", ifd, exec);
368 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
369 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
370 return ((result == 4) ? 4 : 0);
373 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
375 return execute("ip tunnel del %iface%", ifd, exec);
379 static const struct method_t methods6[] = {
380 #if ENABLE_FEATURE_IFUPDOWN_IP
381 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
383 { "static", static_up6, static_down6, },
384 { "loopback", loopback_up6, loopback_down6, },
387 static const struct address_family_t addr_inet6 = {
389 ARRAY_SIZE(methods6),
392 #endif /* FEATURE_IFUPDOWN_IPV6 */
394 #if ENABLE_FEATURE_IFUPDOWN_IPV4
395 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
397 #if ENABLE_FEATURE_IFUPDOWN_IP
399 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
400 result += execute("ip link set %iface% up", ifd, exec);
401 return ((result == 2) ? 2 : 0);
403 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
407 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
409 #if ENABLE_FEATURE_IFUPDOWN_IP
411 result = execute("ip addr flush dev %iface%", ifd, exec);
412 result += execute("ip link set %iface% down", ifd, exec);
413 return ((result == 2) ? 2 : 0);
415 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
419 static int static_up(struct interface_defn_t *ifd, execfn *exec)
422 #if ENABLE_FEATURE_IFUPDOWN_IP
423 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
424 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
425 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
426 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
427 return ((result == 3) ? 3 : 0);
429 /* ifconfig said to set iface up before it processes hw %hwaddress%,
430 * which then of course fails. Thus we run two separate ifconfig */
431 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
433 result += execute("ifconfig %iface% %address% netmask %netmask%"
434 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
436 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
437 return ((result == 3) ? 3 : 0);
441 static int static_down(struct interface_defn_t *ifd, execfn *exec)
444 #if ENABLE_FEATURE_IFUPDOWN_IP
445 result = execute("ip addr flush dev %iface%", ifd, exec);
446 result += execute("ip link set %iface% down", ifd, exec);
448 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
449 result += execute("ifconfig %iface% down", ifd, exec);
451 return ((result == 2) ? 2 : 0);
454 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
458 const char *startcmd;
462 static const struct dhcp_client_t ext_dhcp_clients[] = {
464 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
468 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
469 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
472 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
473 "pump -i %iface% -k",
476 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
477 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
480 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
482 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
484 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
486 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
487 if (exists_execable(ext_dhcp_clients[i].name))
488 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
490 bb_error_msg("no dhcp clients found");
492 #elif ENABLE_APP_UDHCPC
493 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
494 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
497 return 0; /* no dhcp support */
501 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
503 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
505 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
506 if (exists_execable(ext_dhcp_clients[i].name))
507 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
509 bb_error_msg("no dhcp clients found, using static interface shutdown");
510 return static_down(ifd, exec);
511 #elif ENABLE_APP_UDHCPC
512 return execute("kill "
513 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
515 return 0; /* no dhcp support */
519 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
524 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
526 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
527 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
528 "--returniffail --serverbcast", ifd, exec);
531 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
533 return execute("pon[[ %provider%]]", ifd, exec);
536 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
538 return execute("poff[[ %provider%]]", ifd, exec);
541 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
543 return execute("start-stop-daemon --start -x wvdial "
544 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
547 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
549 return execute("start-stop-daemon --stop -x wvdial "
550 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
553 static const struct method_t methods[] = {
554 { "manual", manual_up_down, manual_up_down, },
555 { "wvdial", wvdial_up, wvdial_down, },
556 { "ppp", ppp_up, ppp_down, },
557 { "static", static_up, static_down, },
558 { "bootp", bootp_up, static_down, },
559 { "dhcp", dhcp_up, dhcp_down, },
560 { "loopback", loopback_up, loopback_down, },
563 static const struct address_family_t addr_inet = {
569 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
571 static char *next_word(char **buf)
573 unsigned short length;
576 if (!buf || !*buf || !**buf) {
580 /* Skip over leading whitespace */
581 word = skip_whitespace(*buf);
583 /* Skip over comments */
588 /* Find the length of this word */
589 length = strcspn(word, " \t\n");
593 *buf = word + length;
594 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
603 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
610 for (i = 0; af[i]; i++) {
611 if (strcmp(af[i]->name, name) == 0) {
618 static const struct method_t *get_method(const struct address_family_t *af, char *name)
625 for (i = 0; i < af->n_methods; i++) {
626 if (strcmp(af->method[i].name, name) == 0) {
627 return &af->method[i];
633 static const llist_t *find_list_string(const llist_t *list, const char *string)
639 if (strcmp(list->data, string) == 0) {
647 static struct interfaces_file_t *read_interfaces(const char *filename)
649 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
650 struct mapping_defn_t *currmap = NULL;
652 struct interface_defn_t *currif = NULL;
653 struct interfaces_file_t *defn;
658 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
660 defn = xzalloc(sizeof(struct interfaces_file_t));
662 f = xfopen(filename, "r");
664 while ((buf = xmalloc_getline(f)) != NULL) {
667 firstword = next_word(&buf_ptr);
668 if (firstword == NULL) {
670 continue; /* blank line */
673 if (strcmp(firstword, "mapping") == 0) {
674 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
675 currmap = xzalloc(sizeof(struct mapping_defn_t));
677 while ((firstword = next_word(&buf_ptr)) != NULL) {
678 if (currmap->max_matches == currmap->n_matches) {
679 currmap->max_matches = currmap->max_matches * 2 + 1;
680 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
683 currmap->match[currmap->n_matches++] = xstrdup(firstword);
685 currmap->max_mappings = 0;
686 currmap->n_mappings = 0;
687 currmap->mapping = NULL;
688 currmap->script = NULL;
690 struct mapping_defn_t **where = &defn->mappings;
691 while (*where != NULL) {
692 where = &(*where)->next;
695 currmap->next = NULL;
697 debug_noise("Added mapping\n");
699 currently_processing = MAPPING;
700 } else if (strcmp(firstword, "iface") == 0) {
701 static const struct address_family_t *const addr_fams[] = {
702 #if ENABLE_FEATURE_IFUPDOWN_IPV4
705 #if ENABLE_FEATURE_IFUPDOWN_IPV6
712 char *address_family_name;
716 currif = xzalloc(sizeof(struct interface_defn_t));
717 iface_name = next_word(&buf_ptr);
718 address_family_name = next_word(&buf_ptr);
719 method_name = next_word(&buf_ptr);
721 if (buf_ptr == NULL) {
722 bb_error_msg("too few parameters for line \"%s\"", buf);
726 /* ship any trailing whitespace */
727 buf_ptr = skip_whitespace(buf_ptr);
729 if (buf_ptr[0] != '\0') {
730 bb_error_msg("too many parameters \"%s\"", buf);
734 currif->iface = xstrdup(iface_name);
736 currif->address_family = get_address_family(addr_fams, address_family_name);
737 if (!currif->address_family) {
738 bb_error_msg("unknown address type \"%s\"", address_family_name);
742 currif->method = get_method(currif->address_family, method_name);
743 if (!currif->method) {
744 bb_error_msg("unknown method \"%s\"", method_name);
748 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
749 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
750 if ((strcmp(tmp->iface, currif->iface) == 0) &&
751 (tmp->address_family == currif->address_family)) {
752 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
756 llist_add_to_end(&(defn->ifaces), (char*)currif);
758 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
759 currently_processing = IFACE;
760 } else if (strcmp(firstword, "auto") == 0) {
761 while ((firstword = next_word(&buf_ptr)) != NULL) {
763 /* Check the interface isnt already listed */
764 if (find_list_string(defn->autointerfaces, firstword)) {
765 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
768 /* Add the interface to the list */
769 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
770 debug_noise("\nauto %s\n", firstword);
772 currently_processing = NONE;
774 switch (currently_processing) {
779 if (strlen(buf_ptr) == 0) {
780 bb_error_msg("option with empty value \"%s\"", buf);
784 if (strcmp(firstword, "up") != 0
785 && strcmp(firstword, "down") != 0
786 && strcmp(firstword, "pre-up") != 0
787 && strcmp(firstword, "post-down") != 0) {
788 for (i = 0; i < currif->n_options; i++) {
789 if (strcmp(currif->option[i].name, firstword) == 0) {
790 bb_error_msg("duplicate option \"%s\"", buf);
796 if (currif->n_options >= currif->max_options) {
797 struct variable_t *opt;
799 currif->max_options = currif->max_options + 10;
800 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
801 currif->option = opt;
803 currif->option[currif->n_options].name = xstrdup(firstword);
804 currif->option[currif->n_options].value = xstrdup(buf_ptr);
805 if (!currif->option[currif->n_options].name) {
809 if (!currif->option[currif->n_options].value) {
813 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
814 currif->option[currif->n_options].value);
818 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
819 if (strcmp(firstword, "script") == 0) {
820 if (currmap->script != NULL) {
821 bb_error_msg("duplicate script in mapping \"%s\"", buf);
824 currmap->script = xstrdup(next_word(&buf_ptr));
826 } else if (strcmp(firstword, "map") == 0) {
827 if (currmap->max_mappings == currmap->n_mappings) {
828 currmap->max_mappings = currmap->max_mappings * 2 + 1;
829 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
831 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
832 currmap->n_mappings++;
834 bb_error_msg("misplaced option \"%s\"", buf);
841 bb_error_msg("misplaced option \"%s\"", buf);
847 if (ferror(f) != 0) {
848 /* ferror does NOT set errno! */
849 bb_error_msg_and_die("%s: I/O error", filename);
856 static char *setlocalenv(const char *format, const char *name, const char *value)
862 result = xasprintf(format, name, value);
864 for (here = there = result; *there != '=' && *there; there++) {
868 *there = toupper(*there);
870 if (isalnum(*there) || *there == '_') {
875 memmove(here, there, strlen(there) + 1);
880 static void set_environ(struct interface_defn_t *iface, const char *mode)
884 const int n_env_entries = iface->n_options + 5;
887 if (my_environ != NULL) {
888 for (ppch = my_environ; *ppch; ppch++) {
894 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
895 environend = my_environ;
897 for (i = 0; i < iface->n_options; i++) {
898 if (strcmp(iface->option[i].name, "up") == 0
899 || strcmp(iface->option[i].name, "down") == 0
900 || strcmp(iface->option[i].name, "pre-up") == 0
901 || strcmp(iface->option[i].name, "post-down") == 0
905 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
908 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
909 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
910 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
911 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
912 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
915 static int doit(char *str)
917 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
920 if (!(option_mask32 & OPT_no_act)) {
927 case -1: /* failure */
930 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
933 waitpid(child, &status, 0);
934 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
941 static int execute_all(struct interface_defn_t *ifd, const char *opt)
945 for (i = 0; i < ifd->n_options; i++) {
946 if (strcmp(ifd->option[i].name, opt) == 0) {
947 if (!doit(ifd->option[i].value)) {
953 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
954 /* heh, we don't bother free'ing it */
958 static int check(char *str)
963 static int iface_up(struct interface_defn_t *iface)
965 if (!iface->method->up(iface, check)) return -1;
966 set_environ(iface, "start");
967 if (!execute_all(iface, "pre-up")) return 0;
968 if (!iface->method->up(iface, doit)) return 0;
969 if (!execute_all(iface, "up")) return 0;
973 static int iface_down(struct interface_defn_t *iface)
975 if (!iface->method->down(iface,check)) return -1;
976 set_environ(iface, "stop");
977 if (!execute_all(iface, "down")) return 0;
978 if (!iface->method->down(iface, doit)) return 0;
979 if (!execute_all(iface, "post-down")) return 0;
983 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
984 static int popen2(FILE **in, FILE **out, char *command, ...)
987 char *argv[11] = { command };
989 int infd[2], outfd[2];
993 va_start(ap, command);
994 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
997 argv[argc] = NULL; /* make sure */
1000 if (pipe(infd) != 0) {
1004 if (pipe(outfd) != 0) {
1011 switch (pid = fork()) {
1012 case -1: /* failure */
1025 BB_EXECVP(command, argv);
1027 default: /* parent */
1028 *in = fdopen(infd[1], "w");
1029 *out = fdopen(outfd[0], "r");
1037 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1043 char *logical = xstrdup(physical);
1045 /* Run the mapping script. */
1046 pid = popen2(&in, &out, map->script, physical, NULL);
1048 /* popen2() returns 0 on failure. */
1052 /* Write mappings to stdin of mapping script. */
1053 for (i = 0; i < map->n_mappings; i++) {
1054 fprintf(in, "%s\n", map->mapping[i]);
1057 waitpid(pid, &status, 0);
1059 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1060 /* If the mapping script exited successfully, try to
1061 * grab a line of output and use that as the name of the
1062 * logical interface. */
1063 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
1065 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1066 /* If we are able to read a line of output from the script,
1067 * remove any trailing whitespace and use this value
1068 * as the name of the logical interface. */
1069 char *pch = new_logical + strlen(new_logical) - 1;
1071 while (pch >= new_logical && isspace(*pch))
1075 logical = new_logical;
1077 /* If we are UNABLE to read a line of output, discard our
1078 * freshly allocated memory. */
1087 #endif /* FEATURE_IFUPDOWN_MAPPING */
1089 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1091 unsigned short iface_len = strlen(iface);
1092 llist_t *search = state_list;
1095 if ((strncmp(search->data, iface, iface_len) == 0)
1096 && (search->data[iface_len] == '=')) {
1099 search = search->link;
1104 /* read the previous state from the state file */
1105 static llist_t *read_iface_state(void)
1107 llist_t *state_list = NULL;
1108 FILE *state_fp = fopen("/var/run/ifstate", "r");
1111 char *start, *end_ptr;
1112 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1113 /* We should only need to check for a single character */
1114 end_ptr = start + strcspn(start, " \t\n");
1116 llist_add_to(&state_list, start);
1124 int ifupdown_main(int argc, char **argv);
1125 int ifupdown_main(int argc, char **argv)
1127 int (*cmds)(struct interface_defn_t *) = NULL;
1128 struct interfaces_file_t *defn;
1129 llist_t *target_list = NULL;
1130 const char *interfaces = "/etc/network/interfaces";
1131 bool any_failures = 0;
1134 if (applet_name[2] == 'u') {
1139 getopt32(argc, argv, OPTION_STR, &interfaces);
1140 if (argc - optind > 0) {
1141 if (DO_ALL) bb_show_usage();
1143 if (!DO_ALL) bb_show_usage();
1146 debug_noise("reading %s file:\n", interfaces);
1147 defn = read_interfaces(interfaces);
1148 debug_noise("\ndone reading %s\n\n", interfaces);
1151 return EXIT_FAILURE;
1154 startup_PATH = getenv("PATH");
1155 if (!startup_PATH) startup_PATH = "";
1157 /* Create a list of interfaces to work on */
1159 target_list = defn->autointerfaces;
1161 llist_add_to_end(&target_list, argv[optind]);
1164 /* Update the interfaces */
1165 while (target_list) {
1166 llist_t *iface_list;
1167 struct interface_defn_t *currif;
1174 iface = xstrdup(target_list->data);
1175 target_list = target_list->link;
1177 pch = strchr(iface, '=');
1180 liface = xstrdup(pch + 1);
1182 liface = xstrdup(iface);
1186 llist_t *state_list = read_iface_state();
1187 const llist_t *iface_state = find_iface_state(state_list, iface);
1189 if (cmds == iface_up) {
1192 bb_error_msg("interface %s already configured", iface);
1198 bb_error_msg("interface %s not configured", iface);
1202 llist_free(state_list, free);
1205 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1206 if ((cmds == iface_up) && !NO_MAPPINGS) {
1207 struct mapping_defn_t *currmap;
1209 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1211 for (i = 0; i < currmap->n_matches; i++) {
1212 if (fnmatch(currmap->match[i], liface, 0) != 0)
1215 printf("Running mapping script %s on %s\n", currmap->script, liface);
1217 liface = run_mapping(iface, currmap);
1224 iface_list = defn->ifaces;
1225 while (iface_list) {
1226 currif = (struct interface_defn_t *) iface_list->data;
1227 if (strcmp(liface, currif->iface) == 0) {
1228 char *oldiface = currif->iface;
1231 currif->iface = iface;
1233 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1235 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1236 cmds_ret = cmds(currif);
1237 if (cmds_ret == -1) {
1238 bb_error_msg("don't seem to have all the variables for %s/%s",
1239 liface, currif->address_family->name);
1241 } else if (cmds_ret == 0) {
1245 currif->iface = oldiface;
1247 iface_list = iface_list->link;
1253 if (!okay && !FORCE) {
1254 bb_error_msg("ignoring unknown interface %s", liface);
1256 } else if (!NO_ACT) {
1257 /* update the state file */
1260 llist_t *state_list = read_iface_state();
1261 llist_t *iface_state = find_iface_state(state_list, iface);
1263 if (cmds == iface_up) {
1264 char * const newiface = xasprintf("%s=%s", iface, liface);
1265 if (iface_state == NULL) {
1266 llist_add_to_end(&state_list, newiface);
1268 free(iface_state->data);
1269 iface_state->data = newiface;
1272 /* Remove an interface from state_list */
1273 llist_unlink(&state_list, iface_state);
1274 free(llist_pop(&iface_state));
1277 /* Actually write the new state */
1278 state_fp = xfopen("/var/run/ifstate", "w");
1282 fprintf(state_fp, "%s\n", state->data);
1284 state = state->link;
1287 llist_free(state_list, free);
1291 return any_failures;