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
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) {
138 static char label_buf[20];
139 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
140 result = strchr(label_buf, ':');
146 if (strncmpz(id, "label", idlen) == 0) {
149 for (i = 0; i < ifd->n_options; i++) {
150 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
151 return ifd->option[i].value;
157 #if ENABLE_FEATURE_IFUPDOWN_IP
158 static int count_netmask_bits(const char *dotted_quad)
161 // unsigned a, b, c, d;
162 // /* Found a netmask... Check if it is dotted quad */
163 // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
165 // if ((a|b|c|d) >> 8)
166 // return -1; /* one of numbers is >= 256 */
167 // d |= (a << 24) | (b << 16) | (c << 8); /* IP */
168 // d = ~d; /* 11110000 -> 00001111 */
170 /* Shorter version */
175 if (inet_aton(dotted_quad, &ip) == 0)
176 return -1; /* malformed dotted IP */
177 d = ntohl(ip.s_addr); /* IP in host order */
178 d = ~d; /* 11110000 -> 00001111 */
179 if (d & (d+1)) /* check that it is in 00001111 form */
180 return -1; /* no it is not */
190 static char *parse(const char *command, struct interface_defn_t *ifd)
192 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
193 int okay[MAX_OPT_DEPTH] = { 1 };
200 addstr(&result, command, 1);
205 addstr(&result, command + 1, 1);
208 addstr(&result, command, 1);
213 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
214 old_pos[opt_depth] = result ? strlen(result) : 0;
219 addstr(&result, "[", 1);
224 if (command[1] == ']' && opt_depth > 1) {
226 if (!okay[opt_depth]) {
227 result[old_pos[opt_depth]] = '\0';
231 addstr(&result, "]", 1);
241 nextpercent = strchr(command, '%');
248 varvalue = get_var(command, nextpercent - command, ifd);
251 addstr(&result, varvalue, strlen(varvalue));
253 #if ENABLE_FEATURE_IFUPDOWN_IP
254 /* Sigh... Add a special case for 'ip' to convert from
255 * dotted quad to bit count style netmasks. */
256 if (strncmp(command, "bnmask", 6) == 0) {
258 varvalue = get_var("netmask", 7, ifd);
260 res = count_netmask_bits(varvalue);
262 const char *argument = utoa(res);
263 addstr(&result, argument, strlen(argument));
264 command = nextpercent + 1;
270 okay[opt_depth - 1] = 0;
273 command = nextpercent + 1;
294 /* execute() returns 1 for success and 0 for failure */
295 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
300 out = parse(command, ifd);
305 /* out == "": parsed ok but not all needed variables known, skip */
306 ret = out[0] ? (*exec)(out) : 1;
316 #if ENABLE_FEATURE_IFUPDOWN_IPV6
317 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
319 #if ENABLE_FEATURE_IFUPDOWN_IP
321 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
322 result += execute("ip link set %iface% up", ifd, exec);
323 return ((result == 2) ? 2 : 0);
325 return execute("ifconfig %iface% add ::1", ifd, exec);
329 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
331 #if ENABLE_FEATURE_IFUPDOWN_IP
332 return execute("ip link set %iface% down", ifd, exec);
334 return execute("ifconfig %iface% del ::1", ifd, exec);
338 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
341 #if ENABLE_FEATURE_IFUPDOWN_IP
342 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
343 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
344 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
345 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
347 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
348 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
349 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
351 return ((result == 3) ? 3 : 0);
354 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
356 #if ENABLE_FEATURE_IFUPDOWN_IP
357 return execute("ip link set %iface% down", ifd, exec);
359 return execute("ifconfig %iface% down", ifd, exec);
363 #if ENABLE_FEATURE_IFUPDOWN_IP
364 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
367 result = execute("ip tunnel add %iface% mode sit remote "
368 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
369 result += execute("ip link set %iface% up", ifd, exec);
370 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
371 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
372 return ((result == 4) ? 4 : 0);
375 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
377 return execute("ip tunnel del %iface%", ifd, exec);
381 static const struct method_t methods6[] = {
382 #if ENABLE_FEATURE_IFUPDOWN_IP
383 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
385 { "static", static_up6, static_down6, },
386 { "loopback", loopback_up6, loopback_down6, },
389 static const struct address_family_t addr_inet6 = {
391 ARRAY_SIZE(methods6),
394 #endif /* FEATURE_IFUPDOWN_IPV6 */
396 #if ENABLE_FEATURE_IFUPDOWN_IPV4
397 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
399 #if ENABLE_FEATURE_IFUPDOWN_IP
401 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
402 result += execute("ip link set %iface% up", ifd, exec);
403 return ((result == 2) ? 2 : 0);
405 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
409 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
411 #if ENABLE_FEATURE_IFUPDOWN_IP
413 result = execute("ip addr flush dev %iface%", ifd, exec);
414 result += execute("ip link set %iface% down", ifd, exec);
415 return ((result == 2) ? 2 : 0);
417 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
421 static int static_up(struct interface_defn_t *ifd, execfn *exec)
424 #if ENABLE_FEATURE_IFUPDOWN_IP
425 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
426 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
427 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
428 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
429 return ((result == 3) ? 3 : 0);
431 /* ifconfig said to set iface up before it processes hw %hwaddress%,
432 * which then of course fails. Thus we run two separate ifconfig */
433 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
435 result += execute("ifconfig %iface% %address% netmask %netmask%"
436 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
438 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
439 return ((result == 3) ? 3 : 0);
443 static int static_down(struct interface_defn_t *ifd, execfn *exec)
446 #if ENABLE_FEATURE_IFUPDOWN_IP
447 result = execute("ip addr flush dev %iface%", ifd, exec);
448 result += execute("ip link set %iface% down", ifd, exec);
450 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
451 result += execute("ifconfig %iface% down", ifd, exec);
453 return ((result == 2) ? 2 : 0);
456 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
460 const char *startcmd;
464 static const struct dhcp_client_t ext_dhcp_clients[] = {
466 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
470 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
471 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
474 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
475 "pump -i %iface% -k",
478 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
479 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
482 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
484 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
486 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
488 #if ENABLE_FEATURE_IFUPDOWN_IP
489 /* ip doesn't up iface when it configures it (unlike ifconfig) */
490 if (!execute("ip link set %iface% up", ifd, exec))
493 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
494 if (exists_execable(ext_dhcp_clients[i].name))
495 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
497 bb_error_msg("no dhcp clients found");
499 #elif ENABLE_APP_UDHCPC
500 #if ENABLE_FEATURE_IFUPDOWN_IP
501 /* ip doesn't up iface when it configures it (unlike ifconfig) */
502 if (!execute("ip link set %iface% up", ifd, exec))
505 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
506 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
509 return 0; /* no dhcp support */
513 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
515 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
517 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
518 if (exists_execable(ext_dhcp_clients[i].name))
519 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
521 bb_error_msg("no dhcp clients found, using static interface shutdown");
522 return static_down(ifd, exec);
523 #elif ENABLE_APP_UDHCPC
524 return execute("kill "
525 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
527 return 0; /* no dhcp support */
531 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
536 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
538 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
539 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
540 " --returniffail --serverbcast", ifd, exec);
543 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
545 return execute("pon[[ %provider%]]", ifd, exec);
548 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
550 return execute("poff[[ %provider%]]", ifd, exec);
553 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
555 return execute("start-stop-daemon --start -x wvdial "
556 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
559 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
561 return execute("start-stop-daemon --stop -x wvdial "
562 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
565 static const struct method_t methods[] = {
566 { "manual", manual_up_down, manual_up_down, },
567 { "wvdial", wvdial_up, wvdial_down, },
568 { "ppp", ppp_up, ppp_down, },
569 { "static", static_up, static_down, },
570 { "bootp", bootp_up, static_down, },
571 { "dhcp", dhcp_up, dhcp_down, },
572 { "loopback", loopback_up, loopback_down, },
575 static const struct address_family_t addr_inet = {
581 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
583 static char *next_word(char **buf)
585 unsigned short length;
588 if (!buf || !*buf || !**buf) {
592 /* Skip over leading whitespace */
593 word = skip_whitespace(*buf);
595 /* Skip over comments */
600 /* Find the length of this word */
601 length = strcspn(word, " \t\n");
605 *buf = word + length;
606 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
615 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
622 for (i = 0; af[i]; i++) {
623 if (strcmp(af[i]->name, name) == 0) {
630 static const struct method_t *get_method(const struct address_family_t *af, char *name)
637 for (i = 0; i < af->n_methods; i++) {
638 if (strcmp(af->method[i].name, name) == 0) {
639 return &af->method[i];
645 static const llist_t *find_list_string(const llist_t *list, const char *string)
651 if (strcmp(list->data, string) == 0) {
659 static struct interfaces_file_t *read_interfaces(const char *filename)
661 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
662 struct mapping_defn_t *currmap = NULL;
664 struct interface_defn_t *currif = NULL;
665 struct interfaces_file_t *defn;
670 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
672 defn = xzalloc(sizeof(struct interfaces_file_t));
674 f = xfopen(filename, "r");
676 while ((buf = xmalloc_getline(f)) != NULL) {
679 firstword = next_word(&buf_ptr);
680 if (firstword == NULL) {
682 continue; /* blank line */
685 if (strcmp(firstword, "mapping") == 0) {
686 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
687 currmap = xzalloc(sizeof(struct mapping_defn_t));
689 while ((firstword = next_word(&buf_ptr)) != NULL) {
690 if (currmap->max_matches == currmap->n_matches) {
691 currmap->max_matches = currmap->max_matches * 2 + 1;
692 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
695 currmap->match[currmap->n_matches++] = xstrdup(firstword);
697 currmap->max_mappings = 0;
698 currmap->n_mappings = 0;
699 currmap->mapping = NULL;
700 currmap->script = NULL;
702 struct mapping_defn_t **where = &defn->mappings;
703 while (*where != NULL) {
704 where = &(*where)->next;
707 currmap->next = NULL;
709 debug_noise("Added mapping\n");
711 currently_processing = MAPPING;
712 } else if (strcmp(firstword, "iface") == 0) {
713 static const struct address_family_t *const addr_fams[] = {
714 #if ENABLE_FEATURE_IFUPDOWN_IPV4
717 #if ENABLE_FEATURE_IFUPDOWN_IPV6
724 char *address_family_name;
728 currif = xzalloc(sizeof(struct interface_defn_t));
729 iface_name = next_word(&buf_ptr);
730 address_family_name = next_word(&buf_ptr);
731 method_name = next_word(&buf_ptr);
733 if (buf_ptr == NULL) {
734 bb_error_msg("too few parameters for line \"%s\"", buf);
738 /* ship any trailing whitespace */
739 buf_ptr = skip_whitespace(buf_ptr);
741 if (buf_ptr[0] != '\0') {
742 bb_error_msg("too many parameters \"%s\"", buf);
746 currif->iface = xstrdup(iface_name);
748 currif->address_family = get_address_family(addr_fams, address_family_name);
749 if (!currif->address_family) {
750 bb_error_msg("unknown address type \"%s\"", address_family_name);
754 currif->method = get_method(currif->address_family, method_name);
755 if (!currif->method) {
756 bb_error_msg("unknown method \"%s\"", method_name);
760 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
761 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
762 if ((strcmp(tmp->iface, currif->iface) == 0) &&
763 (tmp->address_family == currif->address_family)) {
764 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
768 llist_add_to_end(&(defn->ifaces), (char*)currif);
770 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
771 currently_processing = IFACE;
772 } else if (strcmp(firstword, "auto") == 0) {
773 while ((firstword = next_word(&buf_ptr)) != NULL) {
775 /* Check the interface isnt already listed */
776 if (find_list_string(defn->autointerfaces, firstword)) {
777 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
780 /* Add the interface to the list */
781 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
782 debug_noise("\nauto %s\n", firstword);
784 currently_processing = NONE;
786 switch (currently_processing) {
791 if (strlen(buf_ptr) == 0) {
792 bb_error_msg("option with empty value \"%s\"", buf);
796 if (strcmp(firstword, "up") != 0
797 && strcmp(firstword, "down") != 0
798 && strcmp(firstword, "pre-up") != 0
799 && strcmp(firstword, "post-down") != 0) {
800 for (i = 0; i < currif->n_options; i++) {
801 if (strcmp(currif->option[i].name, firstword) == 0) {
802 bb_error_msg("duplicate option \"%s\"", buf);
808 if (currif->n_options >= currif->max_options) {
809 struct variable_t *opt;
811 currif->max_options = currif->max_options + 10;
812 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
813 currif->option = opt;
815 currif->option[currif->n_options].name = xstrdup(firstword);
816 currif->option[currif->n_options].value = xstrdup(buf_ptr);
817 if (!currif->option[currif->n_options].name) {
821 if (!currif->option[currif->n_options].value) {
825 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
826 currif->option[currif->n_options].value);
830 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
831 if (strcmp(firstword, "script") == 0) {
832 if (currmap->script != NULL) {
833 bb_error_msg("duplicate script in mapping \"%s\"", buf);
836 currmap->script = xstrdup(next_word(&buf_ptr));
838 } else if (strcmp(firstword, "map") == 0) {
839 if (currmap->max_mappings == currmap->n_mappings) {
840 currmap->max_mappings = currmap->max_mappings * 2 + 1;
841 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
843 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
844 currmap->n_mappings++;
846 bb_error_msg("misplaced option \"%s\"", buf);
853 bb_error_msg("misplaced option \"%s\"", buf);
859 if (ferror(f) != 0) {
860 /* ferror does NOT set errno! */
861 bb_error_msg_and_die("%s: I/O error", filename);
868 static char *setlocalenv(const char *format, const char *name, const char *value)
874 result = xasprintf(format, name, value);
876 for (here = there = result; *there != '=' && *there; there++) {
880 *there = toupper(*there);
882 if (isalnum(*there) || *there == '_') {
887 memmove(here, there, strlen(there) + 1);
892 static void set_environ(struct interface_defn_t *iface, const char *mode)
896 const int n_env_entries = iface->n_options + 5;
899 if (my_environ != NULL) {
900 for (ppch = my_environ; *ppch; ppch++) {
906 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
907 environend = my_environ;
909 for (i = 0; i < iface->n_options; i++) {
910 if (strcmp(iface->option[i].name, "up") == 0
911 || strcmp(iface->option[i].name, "down") == 0
912 || strcmp(iface->option[i].name, "pre-up") == 0
913 || strcmp(iface->option[i].name, "post-down") == 0
917 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
920 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
921 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
922 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
923 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
924 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
927 static int doit(char *str)
929 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
932 if (!(option_mask32 & OPT_no_act)) {
939 case -1: /* failure */
942 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
945 waitpid(child, &status, 0);
946 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
953 static int execute_all(struct interface_defn_t *ifd, const char *opt)
957 for (i = 0; i < ifd->n_options; i++) {
958 if (strcmp(ifd->option[i].name, opt) == 0) {
959 if (!doit(ifd->option[i].value)) {
965 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
966 /* heh, we don't bother free'ing it */
970 static int check(char *str)
975 static int iface_up(struct interface_defn_t *iface)
977 if (!iface->method->up(iface, check)) return -1;
978 set_environ(iface, "start");
979 if (!execute_all(iface, "pre-up")) return 0;
980 if (!iface->method->up(iface, doit)) return 0;
981 if (!execute_all(iface, "up")) return 0;
985 static int iface_down(struct interface_defn_t *iface)
987 if (!iface->method->down(iface,check)) return -1;
988 set_environ(iface, "stop");
989 if (!execute_all(iface, "down")) return 0;
990 if (!iface->method->down(iface, doit)) return 0;
991 if (!execute_all(iface, "post-down")) return 0;
995 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
996 static int popen2(FILE **in, FILE **out, char *command, ...)
999 char *argv[11] = { command };
1001 int infd[2], outfd[2];
1005 va_start(ap, command);
1006 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1009 argv[argc] = NULL; /* make sure */
1012 if (pipe(infd) != 0) {
1016 if (pipe(outfd) != 0) {
1023 switch (pid = fork()) {
1024 case -1: /* failure */
1037 BB_EXECVP(command, argv);
1039 default: /* parent */
1040 *in = fdopen(infd[1], "w");
1041 *out = fdopen(outfd[0], "r");
1049 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1055 char *logical = xstrdup(physical);
1057 /* Run the mapping script. */
1058 pid = popen2(&in, &out, map->script, physical, NULL);
1060 /* popen2() returns 0 on failure. */
1064 /* Write mappings to stdin of mapping script. */
1065 for (i = 0; i < map->n_mappings; i++) {
1066 fprintf(in, "%s\n", map->mapping[i]);
1069 waitpid(pid, &status, 0);
1071 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1072 /* If the mapping script exited successfully, try to
1073 * grab a line of output and use that as the name of the
1074 * logical interface. */
1075 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
1077 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1078 /* If we are able to read a line of output from the script,
1079 * remove any trailing whitespace and use this value
1080 * as the name of the logical interface. */
1081 char *pch = new_logical + strlen(new_logical) - 1;
1083 while (pch >= new_logical && isspace(*pch))
1087 logical = new_logical;
1089 /* If we are UNABLE to read a line of output, discard our
1090 * freshly allocated memory. */
1099 #endif /* FEATURE_IFUPDOWN_MAPPING */
1101 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1103 unsigned short iface_len = strlen(iface);
1104 llist_t *search = state_list;
1107 if ((strncmp(search->data, iface, iface_len) == 0)
1108 && (search->data[iface_len] == '=')) {
1111 search = search->link;
1116 /* read the previous state from the state file */
1117 static llist_t *read_iface_state(void)
1119 llist_t *state_list = NULL;
1120 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
1123 char *start, *end_ptr;
1124 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1125 /* We should only need to check for a single character */
1126 end_ptr = start + strcspn(start, " \t\n");
1128 llist_add_to(&state_list, start);
1136 int ifupdown_main(int argc, char **argv);
1137 int ifupdown_main(int argc, char **argv)
1139 int (*cmds)(struct interface_defn_t *) = NULL;
1140 struct interfaces_file_t *defn;
1141 llist_t *target_list = NULL;
1142 const char *interfaces = "/etc/network/interfaces";
1143 bool any_failures = 0;
1146 if (applet_name[2] == 'u') {
1151 getopt32(argv, OPTION_STR, &interfaces);
1152 if (argc - optind > 0) {
1153 if (DO_ALL) bb_show_usage();
1155 if (!DO_ALL) bb_show_usage();
1158 debug_noise("reading %s file:\n", interfaces);
1159 defn = read_interfaces(interfaces);
1160 debug_noise("\ndone reading %s\n\n", interfaces);
1163 return EXIT_FAILURE;
1166 startup_PATH = getenv("PATH");
1167 if (!startup_PATH) startup_PATH = "";
1169 /* Create a list of interfaces to work on */
1171 target_list = defn->autointerfaces;
1173 llist_add_to_end(&target_list, argv[optind]);
1176 /* Update the interfaces */
1177 while (target_list) {
1178 llist_t *iface_list;
1179 struct interface_defn_t *currif;
1186 iface = xstrdup(target_list->data);
1187 target_list = target_list->link;
1189 pch = strchr(iface, '=');
1192 liface = xstrdup(pch + 1);
1194 liface = xstrdup(iface);
1198 llist_t *state_list = read_iface_state();
1199 const llist_t *iface_state = find_iface_state(state_list, iface);
1201 if (cmds == iface_up) {
1204 bb_error_msg("interface %s already configured", iface);
1210 bb_error_msg("interface %s not configured", iface);
1214 llist_free(state_list, free);
1217 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1218 if ((cmds == iface_up) && !NO_MAPPINGS) {
1219 struct mapping_defn_t *currmap;
1221 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1223 for (i = 0; i < currmap->n_matches; i++) {
1224 if (fnmatch(currmap->match[i], liface, 0) != 0)
1227 printf("Running mapping script %s on %s\n", currmap->script, liface);
1229 liface = run_mapping(iface, currmap);
1236 iface_list = defn->ifaces;
1237 while (iface_list) {
1238 currif = (struct interface_defn_t *) iface_list->data;
1239 if (strcmp(liface, currif->iface) == 0) {
1240 char *oldiface = currif->iface;
1243 currif->iface = iface;
1245 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1247 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1248 cmds_ret = cmds(currif);
1249 if (cmds_ret == -1) {
1250 bb_error_msg("don't seem to have all the variables for %s/%s",
1251 liface, currif->address_family->name);
1253 } else if (cmds_ret == 0) {
1257 currif->iface = oldiface;
1259 iface_list = iface_list->link;
1265 if (!okay && !FORCE) {
1266 bb_error_msg("ignoring unknown interface %s", liface);
1268 } else if (!NO_ACT) {
1269 /* update the state file */
1272 llist_t *state_list = read_iface_state();
1273 llist_t *iface_state = find_iface_state(state_list, iface);
1275 if (cmds == iface_up) {
1276 char * const newiface = xasprintf("%s=%s", iface, liface);
1277 if (iface_state == NULL) {
1278 llist_add_to_end(&state_list, newiface);
1280 free(iface_state->data);
1281 iface_state->data = newiface;
1284 /* Remove an interface from state_list */
1285 llist_unlink(&state_list, iface_state);
1286 free(llist_pop(&iface_state));
1289 /* Actually write the new state */
1290 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
1294 fprintf(state_fp, "%s\n", state->data);
1296 state = state->link;
1299 llist_free(state_list, free);
1303 return any_failures;