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>
26 #define MAX_OPT_DEPTH 10
27 #define EUNBALBRACK 10001
28 #define EUNDEFVAR 10002
29 #define EUNBALPER 10000
31 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
32 #define MAX_INTERFACE_LENGTH 10
35 #define debug_noise(args...) /*fprintf(stderr, args)*/
37 /* Forward declaration */
38 struct interface_defn_t;
40 typedef int execfn(char *command);
44 int (*up)(struct interface_defn_t *ifd, execfn *e);
45 int (*down)(struct interface_defn_t *ifd, execfn *e);
48 struct address_family_t {
51 const struct method_t *method;
54 struct mapping_defn_t {
55 struct mapping_defn_t *next;
73 struct interface_defn_t {
74 const struct address_family_t *address_family;
75 const struct method_t *method;
80 struct variable_t *option;
83 struct interfaces_file_t {
84 llist_t *autointerfaces;
86 struct mapping_defn_t *mappings;
89 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
95 OPT_no_mappings = 0x10,
97 #define DO_ALL (option_mask32 & OPT_do_all)
98 #define NO_ACT (option_mask32 & OPT_no_act)
99 #define VERBOSE (option_mask32 & OPT_verbose)
100 #define FORCE (option_mask32 & OPT_force)
101 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
103 static char **my_environ;
105 static const char *startup_PATH;
107 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
109 static void addstr(char **bufp, const char *str, size_t str_length)
111 /* xasprintf trick will be smaller, but we are often
112 * called with str_length == 1 - don't want to have
113 * THAT much of malloc/freeing! */
115 int len = (buf ? strlen(buf) : 0);
117 buf = xrealloc(buf, len + str_length);
118 /* copies at most str_length-1 chars! */
119 safe_strncpy(buf + len, str, str_length);
123 static int strncmpz(const char *l, const char *r, size_t llen)
125 int i = strncmp(l, r, llen);
132 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
136 if (strncmpz(id, "iface", idlen) == 0) {
137 static char *label_buf;
141 label_buf = xstrdup(ifd->iface);
142 // Remove virtual iface suffix - why?
143 // ubuntu's ifup doesn't do this
144 //result = strchrnul(label_buf, ':');
148 if (strncmpz(id, "label", idlen) == 0) {
151 for (i = 0; i < ifd->n_options; i++) {
152 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
153 return ifd->option[i].value;
159 #if ENABLE_FEATURE_IFUPDOWN_IP
160 static int count_netmask_bits(const char *dotted_quad)
163 // unsigned a, b, c, d;
164 // /* Found a netmask... Check if it is dotted quad */
165 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
167 // if ((a|b|c|d) >> 8)
168 // return -1; /* one of numbers is >= 256 */
169 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
170 // d = ~d; /* 11110000 -> 00001111 */
172 /* Shorter version */
177 if (inet_aton(dotted_quad, &ip) == 0)
178 return -1; /* malformed dotted IP */
179 d = ntohl(ip.s_addr); /* IP in host order */
180 d = ~d; /* 11110000 -> 00001111 */
181 if (d & (d+1)) /* check that it is in 00001111 form */
182 return -1; /* no it is not */
192 static char *parse(const char *command, struct interface_defn_t *ifd)
194 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
195 int okay[MAX_OPT_DEPTH] = { 1 };
202 addstr(&result, command, 1);
207 addstr(&result, command + 1, 1);
210 addstr(&result, command, 1);
215 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
216 old_pos[opt_depth] = result ? strlen(result) : 0;
221 addstr(&result, "[", 1);
226 if (command[1] == ']' && opt_depth > 1) {
228 if (!okay[opt_depth]) {
229 result[old_pos[opt_depth]] = '\0';
233 addstr(&result, "]", 1);
243 nextpercent = strchr(command, '%');
250 varvalue = get_var(command, nextpercent - command, ifd);
253 addstr(&result, varvalue, strlen(varvalue));
255 #if ENABLE_FEATURE_IFUPDOWN_IP
256 /* Sigh... Add a special case for 'ip' to convert from
257 * dotted quad to bit count style netmasks. */
258 if (strncmp(command, "bnmask", 6) == 0) {
260 varvalue = get_var("netmask", 7, ifd);
262 res = count_netmask_bits(varvalue);
264 const char *argument = utoa(res);
265 addstr(&result, argument, strlen(argument));
266 command = nextpercent + 1;
272 okay[opt_depth - 1] = 0;
275 command = nextpercent + 1;
296 /* execute() returns 1 for success and 0 for failure */
297 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
302 out = parse(command, ifd);
307 /* out == "": parsed ok but not all needed variables known, skip */
308 ret = out[0] ? (*exec)(out) : 1;
318 #if ENABLE_FEATURE_IFUPDOWN_IPV6
319 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
321 #if ENABLE_FEATURE_IFUPDOWN_IP
323 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
324 result += execute("ip link set %iface% up", ifd, exec);
325 return ((result == 2) ? 2 : 0);
327 return execute("ifconfig %iface% add ::1", ifd, exec);
331 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
333 #if ENABLE_FEATURE_IFUPDOWN_IP
334 return execute("ip link set %iface% down", ifd, exec);
336 return execute("ifconfig %iface% del ::1", ifd, exec);
340 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
343 #if ENABLE_FEATURE_IFUPDOWN_IP
344 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
345 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
346 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
347 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
349 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
350 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
351 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
353 return ((result == 3) ? 3 : 0);
356 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
358 #if ENABLE_FEATURE_IFUPDOWN_IP
359 return execute("ip link set %iface% down", ifd, exec);
361 return execute("ifconfig %iface% down", ifd, exec);
365 #if ENABLE_FEATURE_IFUPDOWN_IP
366 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
369 result = execute("ip tunnel add %iface% mode sit remote "
370 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
371 result += execute("ip link set %iface% up", ifd, exec);
372 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
373 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
374 return ((result == 4) ? 4 : 0);
377 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
379 return execute("ip tunnel del %iface%", ifd, exec);
383 static const struct method_t methods6[] = {
384 #if ENABLE_FEATURE_IFUPDOWN_IP
385 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
387 { "static", static_up6, static_down6, },
388 { "loopback", loopback_up6, loopback_down6, },
391 static const struct address_family_t addr_inet6 = {
393 ARRAY_SIZE(methods6),
396 #endif /* FEATURE_IFUPDOWN_IPV6 */
398 #if ENABLE_FEATURE_IFUPDOWN_IPV4
399 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
401 #if ENABLE_FEATURE_IFUPDOWN_IP
403 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
404 result += execute("ip link set %iface% up", ifd, exec);
405 return ((result == 2) ? 2 : 0);
407 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
411 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
413 #if ENABLE_FEATURE_IFUPDOWN_IP
415 result = execute("ip addr flush dev %iface%", ifd, exec);
416 result += execute("ip link set %iface% down", ifd, exec);
417 return ((result == 2) ? 2 : 0);
419 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
423 static int static_up(struct interface_defn_t *ifd, execfn *exec)
426 #if ENABLE_FEATURE_IFUPDOWN_IP
427 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
428 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
429 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
430 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
431 return ((result == 3) ? 3 : 0);
433 /* ifconfig said to set iface up before it processes hw %hwaddress%,
434 * which then of course fails. Thus we run two separate ifconfig */
435 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
437 result += execute("ifconfig %iface% %address% netmask %netmask%"
438 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
440 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
441 return ((result == 3) ? 3 : 0);
445 static int static_down(struct interface_defn_t *ifd, execfn *exec)
448 #if ENABLE_FEATURE_IFUPDOWN_IP
449 result = execute("ip addr flush dev %iface%", ifd, exec);
450 result += execute("ip link set %iface% down", ifd, exec);
452 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
453 result += execute("ifconfig %iface% down", ifd, exec);
455 return ((result == 2) ? 2 : 0);
458 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
462 const char *startcmd;
466 static const struct dhcp_client_t ext_dhcp_clients[] = {
468 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
472 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
473 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
476 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
477 "pump -i %iface% -k",
480 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
481 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
484 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
486 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
488 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
490 #if ENABLE_FEATURE_IFUPDOWN_IP
491 /* ip doesn't up iface when it configures it (unlike ifconfig) */
492 if (!execute("ip link set %iface% up", ifd, exec))
495 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
496 if (exists_execable(ext_dhcp_clients[i].name))
497 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
499 bb_error_msg("no dhcp clients found");
501 #elif ENABLE_APP_UDHCPC
502 #if ENABLE_FEATURE_IFUPDOWN_IP
503 /* ip doesn't up iface when it configures it (unlike ifconfig) */
504 if (!execute("ip link set %iface% up", ifd, exec))
507 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
508 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
511 return 0; /* no dhcp support */
515 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
517 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
519 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
520 if (exists_execable(ext_dhcp_clients[i].name))
521 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
523 bb_error_msg("no dhcp clients found, using static interface shutdown");
524 return static_down(ifd, exec);
525 #elif ENABLE_APP_UDHCPC
526 return execute("kill "
527 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
529 return 0; /* no dhcp support */
533 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
538 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
540 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
541 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
542 " --returniffail --serverbcast", ifd, exec);
545 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
547 return execute("pon[[ %provider%]]", ifd, exec);
550 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
552 return execute("poff[[ %provider%]]", ifd, exec);
555 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
557 return execute("start-stop-daemon --start -x wvdial "
558 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
561 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
563 return execute("start-stop-daemon --stop -x wvdial "
564 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
567 static const struct method_t methods[] = {
568 { "manual", manual_up_down, manual_up_down, },
569 { "wvdial", wvdial_up, wvdial_down, },
570 { "ppp", ppp_up, ppp_down, },
571 { "static", static_up, static_down, },
572 { "bootp", bootp_up, static_down, },
573 { "dhcp", dhcp_up, dhcp_down, },
574 { "loopback", loopback_up, loopback_down, },
577 static const struct address_family_t addr_inet = {
583 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
585 static char *next_word(char **buf)
590 /* Skip over leading whitespace */
591 word = skip_whitespace(*buf);
593 /* Stop on EOL/comments */
594 if (*word == '#' || *word == '\0')
597 /* Find the length of this word (can't be 0) */
598 length = strcspn(word, " \t\n");
600 /* Unless we are already at NUL, store NUL and advance */
601 if (word[length] != '\0')
602 word[length++] = '\0';
604 *buf = word + length;
609 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
616 for (i = 0; af[i]; i++) {
617 if (strcmp(af[i]->name, name) == 0) {
624 static const struct method_t *get_method(const struct address_family_t *af, char *name)
630 /* TODO: use index_in_str_array() */
631 for (i = 0; i < af->n_methods; i++) {
632 if (strcmp(af->method[i].name, name) == 0) {
633 return &af->method[i];
639 static const llist_t *find_list_string(const llist_t *list, const char *string)
645 if (strcmp(list->data, string) == 0) {
653 static struct interfaces_file_t *read_interfaces(const char *filename)
655 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
656 struct mapping_defn_t *currmap = NULL;
658 struct interface_defn_t *currif = NULL;
659 struct interfaces_file_t *defn;
664 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
666 defn = xzalloc(sizeof(struct interfaces_file_t));
668 f = xfopen(filename, "r");
670 while ((buf = xmalloc_getline(f)) != NULL) {
673 firstword = next_word(&buf_ptr);
674 if (firstword == NULL) {
676 continue; /* blank line */
679 if (strcmp(firstword, "mapping") == 0) {
680 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
681 currmap = xzalloc(sizeof(*currmap));
683 while ((firstword = next_word(&buf_ptr)) != NULL) {
684 if (currmap->n_matches >= currmap->max_matches) {
685 currmap->max_matches = currmap->max_matches * 2 + 1;
686 currmap->match = xrealloc(currmap->match, sizeof(*currmap->match) * currmap->max_matches);
688 currmap->match[currmap->n_matches++] = xstrdup(firstword);
690 /*currmap->max_mappings = 0; - done by xzalloc */
691 /*currmap->n_mappings = 0;*/
692 /*currmap->mapping = NULL;*/
693 /*currmap->script = NULL;*/
695 struct mapping_defn_t **where = &defn->mappings;
696 while (*where != NULL) {
697 where = &(*where)->next;
700 /*currmap->next = NULL;*/
702 debug_noise("Added mapping\n");
704 currently_processing = MAPPING;
705 } else if (strcmp(firstword, "iface") == 0) {
706 static const struct address_family_t *const addr_fams[] = {
707 #if ENABLE_FEATURE_IFUPDOWN_IPV4
710 #if ENABLE_FEATURE_IFUPDOWN_IPV6
717 char *address_family_name;
721 currif = xzalloc(sizeof(*currif));
722 iface_name = next_word(&buf_ptr);
723 address_family_name = next_word(&buf_ptr);
724 method_name = next_word(&buf_ptr);
726 if (method_name == NULL)
727 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
729 /* ship any trailing whitespace */
730 buf_ptr = skip_whitespace(buf_ptr);
732 if (buf_ptr[0] != '\0' /* && buf_ptr[0] != '#' */)
733 bb_error_msg_and_die("too many parameters \"%s\"", buf);
735 currif->iface = xstrdup(iface_name);
737 currif->address_family = get_address_family(addr_fams, address_family_name);
738 if (!currif->address_family)
739 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
741 currif->method = get_method(currif->address_family, method_name);
743 bb_error_msg_and_die("unknown method \"%s\"", method_name);
745 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
746 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
747 if ((strcmp(tmp->iface, currif->iface) == 0)
748 && (tmp->address_family == currif->address_family)
750 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
753 llist_add_to_end(&(defn->ifaces), (char*)currif);
755 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
756 currently_processing = IFACE;
757 } else if (strcmp(firstword, "auto") == 0) {
758 while ((firstword = next_word(&buf_ptr)) != NULL) {
760 /* Check the interface isnt already listed */
761 if (find_list_string(defn->autointerfaces, firstword)) {
762 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
765 /* Add the interface to the list */
766 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
767 debug_noise("\nauto %s\n", firstword);
769 currently_processing = NONE;
771 switch (currently_processing) {
773 if (buf_ptr[0] == '\0')
774 bb_error_msg_and_die("option with empty value \"%s\"", buf);
776 if (strcmp(firstword, "up") != 0
777 && strcmp(firstword, "down") != 0
778 && strcmp(firstword, "pre-up") != 0
779 && strcmp(firstword, "post-down") != 0
782 for (i = 0; i < currif->n_options; i++) {
783 if (strcmp(currif->option[i].name, firstword) == 0)
784 bb_error_msg_and_die("duplicate option \"%s\"", buf);
787 if (currif->n_options >= currif->max_options) {
788 currif->max_options += 10;
789 currif->option = xrealloc(currif->option, sizeof(*currif->option) * currif->max_options);
791 debug_noise("\t%s=%s\n", firstword, buf_ptr);
792 currif->option[currif->n_options].name = xstrdup(firstword);
793 currif->option[currif->n_options].value = xstrdup(buf_ptr);
797 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
798 if (strcmp(firstword, "script") == 0) {
799 if (currmap->script != NULL)
800 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
801 currmap->script = xstrdup(next_word(&buf_ptr));
802 } else if (strcmp(firstword, "map") == 0) {
803 if (currmap->n_mappings >= currmap->max_mappings) {
804 currmap->max_mappings = currmap->max_mappings * 2 + 1;
805 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
807 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
808 currmap->n_mappings++;
810 bb_error_msg_and_die("misplaced option \"%s\"", buf);
816 bb_error_msg_and_die("misplaced option \"%s\"", buf);
821 if (ferror(f) != 0) {
822 /* ferror does NOT set errno! */
823 bb_error_msg_and_die("%s: I/O error", filename);
830 static char *setlocalenv(const char *format, const char *name, const char *value)
836 result = xasprintf(format, name, value);
838 for (here = there = result; *there != '=' && *there; there++) {
842 *there = toupper(*there);
844 if (isalnum(*there) || *there == '_') {
849 memmove(here, there, strlen(there) + 1);
854 static void set_environ(struct interface_defn_t *iface, const char *mode)
858 const int n_env_entries = iface->n_options + 5;
861 if (my_environ != NULL) {
862 for (ppch = my_environ; *ppch; ppch++) {
868 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
869 environend = my_environ;
871 for (i = 0; i < iface->n_options; i++) {
872 if (strcmp(iface->option[i].name, "up") == 0
873 || strcmp(iface->option[i].name, "down") == 0
874 || strcmp(iface->option[i].name, "pre-up") == 0
875 || strcmp(iface->option[i].name, "post-down") == 0
879 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
882 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
883 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
884 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
885 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
886 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
889 static int doit(char *str)
891 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
894 if (!(option_mask32 & OPT_no_act)) {
901 case -1: /* failure */
904 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
907 safe_waitpid(child, &status, 0);
908 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
915 static int execute_all(struct interface_defn_t *ifd, const char *opt)
919 for (i = 0; i < ifd->n_options; i++) {
920 if (strcmp(ifd->option[i].name, opt) == 0) {
921 if (!doit(ifd->option[i].value)) {
927 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
928 /* heh, we don't bother free'ing it */
932 static int check(char *str)
937 static int iface_up(struct interface_defn_t *iface)
939 if (!iface->method->up(iface, check)) return -1;
940 set_environ(iface, "start");
941 if (!execute_all(iface, "pre-up")) return 0;
942 if (!iface->method->up(iface, doit)) return 0;
943 if (!execute_all(iface, "up")) return 0;
947 static int iface_down(struct interface_defn_t *iface)
949 if (!iface->method->down(iface,check)) return -1;
950 set_environ(iface, "stop");
951 if (!execute_all(iface, "down")) return 0;
952 if (!iface->method->down(iface, doit)) return 0;
953 if (!execute_all(iface, "post-down")) return 0;
957 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
958 static int popen2(FILE **in, FILE **out, char *command, ...)
961 char *argv[11] = { command };
963 int infd[2], outfd[2];
967 va_start(ap, command);
968 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
971 argv[argc] = NULL; /* make sure */
974 if (pipe(infd) != 0) {
978 if (pipe(outfd) != 0) {
985 switch (pid = fork()) {
986 case -1: /* failure */
999 BB_EXECVP(command, argv);
1001 default: /* parent */
1002 *in = fdopen(infd[1], "w");
1003 *out = fdopen(outfd[0], "r");
1011 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1017 char *logical = xstrdup(physical);
1019 /* Run the mapping script. */
1020 pid = popen2(&in, &out, map->script, physical, NULL);
1022 /* popen2() returns 0 on failure. */
1026 /* Write mappings to stdin of mapping script. */
1027 for (i = 0; i < map->n_mappings; i++) {
1028 fprintf(in, "%s\n", map->mapping[i]);
1031 safe_waitpid(pid, &status, 0);
1033 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1034 /* If the mapping script exited successfully, try to
1035 * grab a line of output and use that as the name of the
1036 * logical interface. */
1037 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
1039 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1040 /* If we are able to read a line of output from the script,
1041 * remove any trailing whitespace and use this value
1042 * as the name of the logical interface. */
1043 char *pch = new_logical + strlen(new_logical) - 1;
1045 while (pch >= new_logical && isspace(*pch))
1049 logical = new_logical;
1051 /* If we are UNABLE to read a line of output, discard our
1052 * freshly allocated memory. */
1061 #endif /* FEATURE_IFUPDOWN_MAPPING */
1063 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1065 unsigned short iface_len = strlen(iface);
1066 llist_t *search = state_list;
1069 if ((strncmp(search->data, iface, iface_len) == 0)
1070 && (search->data[iface_len] == '=')) {
1073 search = search->link;
1078 /* read the previous state from the state file */
1079 static llist_t *read_iface_state(void)
1081 llist_t *state_list = NULL;
1082 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
1085 char *start, *end_ptr;
1086 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1087 /* We should only need to check for a single character */
1088 end_ptr = start + strcspn(start, " \t\n");
1090 llist_add_to(&state_list, start);
1098 int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1099 int ifupdown_main(int argc, char **argv)
1101 int (*cmds)(struct interface_defn_t *);
1102 struct interfaces_file_t *defn;
1103 llist_t *target_list = NULL;
1104 const char *interfaces = "/etc/network/interfaces";
1105 bool any_failures = 0;
1108 if (applet_name[2] == 'u') {
1113 getopt32(argv, OPTION_STR, &interfaces);
1114 if (argc - optind > 0) {
1115 if (DO_ALL) bb_show_usage();
1117 if (!DO_ALL) bb_show_usage();
1120 debug_noise("reading %s file:\n", interfaces);
1121 defn = read_interfaces(interfaces);
1122 debug_noise("\ndone reading %s\n\n", interfaces);
1124 startup_PATH = getenv("PATH");
1125 if (!startup_PATH) startup_PATH = "";
1127 /* Create a list of interfaces to work on */
1129 target_list = defn->autointerfaces;
1131 llist_add_to_end(&target_list, argv[optind]);
1134 /* Update the interfaces */
1135 while (target_list) {
1136 llist_t *iface_list;
1137 struct interface_defn_t *currif;
1144 iface = xstrdup(target_list->data);
1145 target_list = target_list->link;
1147 pch = strchr(iface, '=');
1150 liface = xstrdup(pch + 1);
1152 liface = xstrdup(iface);
1156 llist_t *state_list = read_iface_state();
1157 const llist_t *iface_state = find_iface_state(state_list, iface);
1159 if (cmds == iface_up) {
1162 bb_error_msg("interface %s already configured", iface);
1168 bb_error_msg("interface %s not configured", iface);
1172 llist_free(state_list, free);
1175 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1176 if ((cmds == iface_up) && !NO_MAPPINGS) {
1177 struct mapping_defn_t *currmap;
1179 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1181 for (i = 0; i < currmap->n_matches; i++) {
1182 if (fnmatch(currmap->match[i], liface, 0) != 0)
1185 printf("Running mapping script %s on %s\n", currmap->script, liface);
1187 liface = run_mapping(iface, currmap);
1194 iface_list = defn->ifaces;
1195 while (iface_list) {
1196 currif = (struct interface_defn_t *) iface_list->data;
1197 if (strcmp(liface, currif->iface) == 0) {
1198 char *oldiface = currif->iface;
1201 currif->iface = iface;
1203 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1205 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1206 cmds_ret = cmds(currif);
1207 if (cmds_ret == -1) {
1208 bb_error_msg("don't seem to have all the variables for %s/%s",
1209 liface, currif->address_family->name);
1211 } else if (cmds_ret == 0) {
1215 currif->iface = oldiface;
1217 iface_list = iface_list->link;
1223 if (!okay && !FORCE) {
1224 bb_error_msg("ignoring unknown interface %s", liface);
1226 } else if (!NO_ACT) {
1227 /* update the state file */
1230 llist_t *state_list = read_iface_state();
1231 llist_t *iface_state = find_iface_state(state_list, iface);
1233 if (cmds == iface_up) {
1234 char * const newiface = xasprintf("%s=%s", iface, liface);
1235 if (iface_state == NULL) {
1236 llist_add_to_end(&state_list, newiface);
1238 free(iface_state->data);
1239 iface_state->data = newiface;
1242 /* Remove an interface from state_list */
1243 llist_unlink(&state_list, iface_state);
1244 free(llist_pop(&iface_state));
1247 /* Actually write the new state */
1248 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
1252 fprintf(state_fp, "%s\n", state->data);
1254 state = state->link;
1257 llist_free(state_list, free);
1261 return any_failures;