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 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
489 if (exists_execable(ext_dhcp_clients[i].name))
490 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
492 bb_error_msg("no dhcp clients found");
494 #elif ENABLE_APP_UDHCPC
495 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
496 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
499 return 0; /* no dhcp support */
503 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
505 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
507 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
508 if (exists_execable(ext_dhcp_clients[i].name))
509 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
511 bb_error_msg("no dhcp clients found, using static interface shutdown");
512 return static_down(ifd, exec);
513 #elif ENABLE_APP_UDHCPC
514 return execute("kill "
515 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
517 return 0; /* no dhcp support */
521 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
526 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
528 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
529 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
530 "--returniffail --serverbcast", ifd, exec);
533 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
535 return execute("pon[[ %provider%]]", ifd, exec);
538 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
540 return execute("poff[[ %provider%]]", ifd, exec);
543 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
545 return execute("start-stop-daemon --start -x wvdial "
546 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
549 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
551 return execute("start-stop-daemon --stop -x wvdial "
552 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
555 static const struct method_t methods[] = {
556 { "manual", manual_up_down, manual_up_down, },
557 { "wvdial", wvdial_up, wvdial_down, },
558 { "ppp", ppp_up, ppp_down, },
559 { "static", static_up, static_down, },
560 { "bootp", bootp_up, static_down, },
561 { "dhcp", dhcp_up, dhcp_down, },
562 { "loopback", loopback_up, loopback_down, },
565 static const struct address_family_t addr_inet = {
571 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
573 static char *next_word(char **buf)
575 unsigned short length;
578 if (!buf || !*buf || !**buf) {
582 /* Skip over leading whitespace */
583 word = skip_whitespace(*buf);
585 /* Skip over comments */
590 /* Find the length of this word */
591 length = strcspn(word, " \t\n");
595 *buf = word + length;
596 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
605 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
612 for (i = 0; af[i]; i++) {
613 if (strcmp(af[i]->name, name) == 0) {
620 static const struct method_t *get_method(const struct address_family_t *af, char *name)
627 for (i = 0; i < af->n_methods; i++) {
628 if (strcmp(af->method[i].name, name) == 0) {
629 return &af->method[i];
635 static const llist_t *find_list_string(const llist_t *list, const char *string)
641 if (strcmp(list->data, string) == 0) {
649 static struct interfaces_file_t *read_interfaces(const char *filename)
651 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
652 struct mapping_defn_t *currmap = NULL;
654 struct interface_defn_t *currif = NULL;
655 struct interfaces_file_t *defn;
660 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
662 defn = xzalloc(sizeof(struct interfaces_file_t));
664 f = xfopen(filename, "r");
666 while ((buf = xmalloc_getline(f)) != NULL) {
669 firstword = next_word(&buf_ptr);
670 if (firstword == NULL) {
672 continue; /* blank line */
675 if (strcmp(firstword, "mapping") == 0) {
676 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
677 currmap = xzalloc(sizeof(struct mapping_defn_t));
679 while ((firstword = next_word(&buf_ptr)) != NULL) {
680 if (currmap->max_matches == currmap->n_matches) {
681 currmap->max_matches = currmap->max_matches * 2 + 1;
682 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
685 currmap->match[currmap->n_matches++] = xstrdup(firstword);
687 currmap->max_mappings = 0;
688 currmap->n_mappings = 0;
689 currmap->mapping = NULL;
690 currmap->script = NULL;
692 struct mapping_defn_t **where = &defn->mappings;
693 while (*where != NULL) {
694 where = &(*where)->next;
697 currmap->next = NULL;
699 debug_noise("Added mapping\n");
701 currently_processing = MAPPING;
702 } else if (strcmp(firstword, "iface") == 0) {
703 static const struct address_family_t *const addr_fams[] = {
704 #if ENABLE_FEATURE_IFUPDOWN_IPV4
707 #if ENABLE_FEATURE_IFUPDOWN_IPV6
714 char *address_family_name;
718 currif = xzalloc(sizeof(struct interface_defn_t));
719 iface_name = next_word(&buf_ptr);
720 address_family_name = next_word(&buf_ptr);
721 method_name = next_word(&buf_ptr);
723 if (buf_ptr == NULL) {
724 bb_error_msg("too few parameters for line \"%s\"", buf);
728 /* ship any trailing whitespace */
729 buf_ptr = skip_whitespace(buf_ptr);
731 if (buf_ptr[0] != '\0') {
732 bb_error_msg("too many parameters \"%s\"", buf);
736 currif->iface = xstrdup(iface_name);
738 currif->address_family = get_address_family(addr_fams, address_family_name);
739 if (!currif->address_family) {
740 bb_error_msg("unknown address type \"%s\"", address_family_name);
744 currif->method = get_method(currif->address_family, method_name);
745 if (!currif->method) {
746 bb_error_msg("unknown method \"%s\"", method_name);
750 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
751 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
752 if ((strcmp(tmp->iface, currif->iface) == 0) &&
753 (tmp->address_family == currif->address_family)) {
754 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
758 llist_add_to_end(&(defn->ifaces), (char*)currif);
760 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
761 currently_processing = IFACE;
762 } else if (strcmp(firstword, "auto") == 0) {
763 while ((firstword = next_word(&buf_ptr)) != NULL) {
765 /* Check the interface isnt already listed */
766 if (find_list_string(defn->autointerfaces, firstword)) {
767 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
770 /* Add the interface to the list */
771 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
772 debug_noise("\nauto %s\n", firstword);
774 currently_processing = NONE;
776 switch (currently_processing) {
781 if (strlen(buf_ptr) == 0) {
782 bb_error_msg("option with empty value \"%s\"", buf);
786 if (strcmp(firstword, "up") != 0
787 && strcmp(firstword, "down") != 0
788 && strcmp(firstword, "pre-up") != 0
789 && strcmp(firstword, "post-down") != 0) {
790 for (i = 0; i < currif->n_options; i++) {
791 if (strcmp(currif->option[i].name, firstword) == 0) {
792 bb_error_msg("duplicate option \"%s\"", buf);
798 if (currif->n_options >= currif->max_options) {
799 struct variable_t *opt;
801 currif->max_options = currif->max_options + 10;
802 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
803 currif->option = opt;
805 currif->option[currif->n_options].name = xstrdup(firstword);
806 currif->option[currif->n_options].value = xstrdup(buf_ptr);
807 if (!currif->option[currif->n_options].name) {
811 if (!currif->option[currif->n_options].value) {
815 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
816 currif->option[currif->n_options].value);
820 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
821 if (strcmp(firstword, "script") == 0) {
822 if (currmap->script != NULL) {
823 bb_error_msg("duplicate script in mapping \"%s\"", buf);
826 currmap->script = xstrdup(next_word(&buf_ptr));
828 } else if (strcmp(firstword, "map") == 0) {
829 if (currmap->max_mappings == currmap->n_mappings) {
830 currmap->max_mappings = currmap->max_mappings * 2 + 1;
831 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
833 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
834 currmap->n_mappings++;
836 bb_error_msg("misplaced option \"%s\"", buf);
843 bb_error_msg("misplaced option \"%s\"", buf);
849 if (ferror(f) != 0) {
850 /* ferror does NOT set errno! */
851 bb_error_msg_and_die("%s: I/O error", filename);
858 static char *setlocalenv(const char *format, const char *name, const char *value)
864 result = xasprintf(format, name, value);
866 for (here = there = result; *there != '=' && *there; there++) {
870 *there = toupper(*there);
872 if (isalnum(*there) || *there == '_') {
877 memmove(here, there, strlen(there) + 1);
882 static void set_environ(struct interface_defn_t *iface, const char *mode)
886 const int n_env_entries = iface->n_options + 5;
889 if (my_environ != NULL) {
890 for (ppch = my_environ; *ppch; ppch++) {
896 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
897 environend = my_environ;
899 for (i = 0; i < iface->n_options; i++) {
900 if (strcmp(iface->option[i].name, "up") == 0
901 || strcmp(iface->option[i].name, "down") == 0
902 || strcmp(iface->option[i].name, "pre-up") == 0
903 || strcmp(iface->option[i].name, "post-down") == 0
907 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
910 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
911 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
912 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
913 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
914 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
917 static int doit(char *str)
919 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
922 if (!(option_mask32 & OPT_no_act)) {
929 case -1: /* failure */
932 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
935 waitpid(child, &status, 0);
936 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
943 static int execute_all(struct interface_defn_t *ifd, const char *opt)
947 for (i = 0; i < ifd->n_options; i++) {
948 if (strcmp(ifd->option[i].name, opt) == 0) {
949 if (!doit(ifd->option[i].value)) {
955 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
956 /* heh, we don't bother free'ing it */
960 static int check(char *str)
965 static int iface_up(struct interface_defn_t *iface)
967 if (!iface->method->up(iface, check)) return -1;
968 set_environ(iface, "start");
969 if (!execute_all(iface, "pre-up")) return 0;
970 if (!iface->method->up(iface, doit)) return 0;
971 if (!execute_all(iface, "up")) return 0;
975 static int iface_down(struct interface_defn_t *iface)
977 if (!iface->method->down(iface,check)) return -1;
978 set_environ(iface, "stop");
979 if (!execute_all(iface, "down")) return 0;
980 if (!iface->method->down(iface, doit)) return 0;
981 if (!execute_all(iface, "post-down")) return 0;
985 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
986 static int popen2(FILE **in, FILE **out, char *command, ...)
989 char *argv[11] = { command };
991 int infd[2], outfd[2];
995 va_start(ap, command);
996 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
999 argv[argc] = NULL; /* make sure */
1002 if (pipe(infd) != 0) {
1006 if (pipe(outfd) != 0) {
1013 switch (pid = fork()) {
1014 case -1: /* failure */
1027 BB_EXECVP(command, argv);
1029 default: /* parent */
1030 *in = fdopen(infd[1], "w");
1031 *out = fdopen(outfd[0], "r");
1039 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1045 char *logical = xstrdup(physical);
1047 /* Run the mapping script. */
1048 pid = popen2(&in, &out, map->script, physical, NULL);
1050 /* popen2() returns 0 on failure. */
1054 /* Write mappings to stdin of mapping script. */
1055 for (i = 0; i < map->n_mappings; i++) {
1056 fprintf(in, "%s\n", map->mapping[i]);
1059 waitpid(pid, &status, 0);
1061 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1062 /* If the mapping script exited successfully, try to
1063 * grab a line of output and use that as the name of the
1064 * logical interface. */
1065 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
1067 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1068 /* If we are able to read a line of output from the script,
1069 * remove any trailing whitespace and use this value
1070 * as the name of the logical interface. */
1071 char *pch = new_logical + strlen(new_logical) - 1;
1073 while (pch >= new_logical && isspace(*pch))
1077 logical = new_logical;
1079 /* If we are UNABLE to read a line of output, discard our
1080 * freshly allocated memory. */
1089 #endif /* FEATURE_IFUPDOWN_MAPPING */
1091 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1093 unsigned short iface_len = strlen(iface);
1094 llist_t *search = state_list;
1097 if ((strncmp(search->data, iface, iface_len) == 0)
1098 && (search->data[iface_len] == '=')) {
1101 search = search->link;
1106 /* read the previous state from the state file */
1107 static llist_t *read_iface_state(void)
1109 llist_t *state_list = NULL;
1110 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
1113 char *start, *end_ptr;
1114 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1115 /* We should only need to check for a single character */
1116 end_ptr = start + strcspn(start, " \t\n");
1118 llist_add_to(&state_list, start);
1126 int ifupdown_main(int argc, char **argv);
1127 int ifupdown_main(int argc, char **argv)
1129 int (*cmds)(struct interface_defn_t *) = NULL;
1130 struct interfaces_file_t *defn;
1131 llist_t *target_list = NULL;
1132 const char *interfaces = "/etc/network/interfaces";
1133 bool any_failures = 0;
1136 if (applet_name[2] == 'u') {
1141 getopt32(argc, argv, OPTION_STR, &interfaces);
1142 if (argc - optind > 0) {
1143 if (DO_ALL) bb_show_usage();
1145 if (!DO_ALL) bb_show_usage();
1148 debug_noise("reading %s file:\n", interfaces);
1149 defn = read_interfaces(interfaces);
1150 debug_noise("\ndone reading %s\n\n", interfaces);
1153 return EXIT_FAILURE;
1156 startup_PATH = getenv("PATH");
1157 if (!startup_PATH) startup_PATH = "";
1159 /* Create a list of interfaces to work on */
1161 target_list = defn->autointerfaces;
1163 llist_add_to_end(&target_list, argv[optind]);
1166 /* Update the interfaces */
1167 while (target_list) {
1168 llist_t *iface_list;
1169 struct interface_defn_t *currif;
1176 iface = xstrdup(target_list->data);
1177 target_list = target_list->link;
1179 pch = strchr(iface, '=');
1182 liface = xstrdup(pch + 1);
1184 liface = xstrdup(iface);
1188 llist_t *state_list = read_iface_state();
1189 const llist_t *iface_state = find_iface_state(state_list, iface);
1191 if (cmds == iface_up) {
1194 bb_error_msg("interface %s already configured", iface);
1200 bb_error_msg("interface %s not configured", iface);
1204 llist_free(state_list, free);
1207 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1208 if ((cmds == iface_up) && !NO_MAPPINGS) {
1209 struct mapping_defn_t *currmap;
1211 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1213 for (i = 0; i < currmap->n_matches; i++) {
1214 if (fnmatch(currmap->match[i], liface, 0) != 0)
1217 printf("Running mapping script %s on %s\n", currmap->script, liface);
1219 liface = run_mapping(iface, currmap);
1226 iface_list = defn->ifaces;
1227 while (iface_list) {
1228 currif = (struct interface_defn_t *) iface_list->data;
1229 if (strcmp(liface, currif->iface) == 0) {
1230 char *oldiface = currif->iface;
1233 currif->iface = iface;
1235 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1237 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1238 cmds_ret = cmds(currif);
1239 if (cmds_ret == -1) {
1240 bb_error_msg("don't seem to have all the variables for %s/%s",
1241 liface, currif->address_family->name);
1243 } else if (cmds_ret == 0) {
1247 currif->iface = oldiface;
1249 iface_list = iface_list->link;
1255 if (!okay && !FORCE) {
1256 bb_error_msg("ignoring unknown interface %s", liface);
1258 } else if (!NO_ACT) {
1259 /* update the state file */
1262 llist_t *state_list = read_iface_state();
1263 llist_t *iface_state = find_iface_state(state_list, iface);
1265 if (cmds == iface_up) {
1266 char * const newiface = xasprintf("%s=%s", iface, liface);
1267 if (iface_state == NULL) {
1268 llist_add_to_end(&state_list, newiface);
1270 free(iface_state->data);
1271 iface_state->data = newiface;
1274 /* Remove an interface from state_list */
1275 llist_unlink(&state_list, iface_state);
1276 free(llist_pop(&iface_state));
1279 /* Actually write the new state */
1280 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
1284 fprintf(state_fp, "%s\n", state->data);
1286 state = state->link;
1289 llist_free(state_list, free);
1293 return any_failures;