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.
19 #include <sys/utsname.h>
23 #define MAX_OPT_DEPTH 10
24 #define EUNBALBRACK 10001
25 #define EUNDEFVAR 10002
26 #define EUNBALPER 10000
28 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
29 #define MAX_INTERFACE_LENGTH 10
32 #define debug_noise(fmt, args...)
34 /* Forward declaration */
35 struct interface_defn_t;
37 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
50 const struct method_t *method;
55 struct mapping_defn_t *next;
74 struct interface_defn_t
76 const struct address_family_t *address_family;
77 const struct method_t *method;
82 struct variable_t *option;
85 struct interfaces_file_t
87 llist_t *autointerfaces;
89 struct mapping_defn_t *mappings;
92 static unsigned option_mask;
93 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
99 OPT_no_mappings = 0x10,
101 #define DO_ALL (option_mask & OPT_do_all)
102 #define NO_ACT (option_mask & OPT_no_act)
103 #define VERBOSE (option_mask & OPT_verbose)
104 #define FORCE (option_mask & OPT_force)
105 #define NO_MAPPINGS (option_mask & OPT_no_mappings)
107 static char **__myenviron;
109 static char *startup_PATH;
111 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
113 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
115 static unsigned int count_bits(unsigned int a)
118 result = (a & 0x55) + ((a >> 1) & 0x55);
119 result = (result & 0x33) + ((result >> 2) & 0x33);
120 return ((result & 0x0F) + ((result >> 4) & 0x0F));
123 static int count_netmask_bits(char *dotted_quad)
125 unsigned int result, a, b, c, d;
126 /* Found a netmask... Check if it is dotted quad */
127 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
129 result = count_bits(a);
130 result += count_bits(b);
131 result += count_bits(c);
132 result += count_bits(d);
137 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
139 if (*pos + str_length >= *len) {
142 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
144 *len = *len * 2 + str_length + 1;
147 while (str_length-- >= 1) {
148 (*buf)[(*pos)++] = *str;
154 static int strncmpz(char *l, char *r, size_t llen)
156 int i = strncmp(l, r, llen);
165 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
169 if (strncmpz(id, "iface", idlen) == 0) {
171 static char label_buf[20];
172 strncpy(label_buf, ifd->iface, 19);
174 result = strchr(label_buf, ':');
179 } else if (strncmpz(id, "label", idlen) == 0) {
182 for (i = 0; i < ifd->n_options; i++) {
183 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
184 return ifd->option[i].value;
192 static char *parse(char *command, struct interface_defn_t *ifd)
196 size_t pos = 0, len = 0;
197 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
198 int okay[MAX_OPT_DEPTH] = { 1 };
204 addstr(&result, &len, &pos, command, 1);
209 addstr(&result, &len, &pos, command + 1, 1);
212 addstr(&result, &len, &pos, command, 1);
217 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
218 old_pos[opt_depth] = pos;
223 addstr(&result, &len, &pos, "[", 1);
228 if (command[1] == ']' && opt_depth > 1) {
230 if (!okay[opt_depth]) {
231 pos = old_pos[opt_depth];
236 addstr(&result, &len, &pos, "]", 1);
246 nextpercent = strchr(command, '%');
253 varvalue = get_var(command, nextpercent - command, ifd);
256 addstr(&result, &len, &pos, varvalue, strlen(varvalue));
258 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
259 /* Sigh... Add a special case for 'ip' to convert from
260 * dotted quad to bit count style netmasks. */
261 if (strncmp(command, "bnmask", 6)==0) {
263 varvalue = get_var("netmask", 7, ifd);
264 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
266 sprintf(argument, "%d", res);
267 addstr(&result, &len, &pos, argument, strlen(argument));
268 command = nextpercent + 1;
273 okay[opt_depth - 1] = 0;
276 command = nextpercent + 1;
297 /* execute() returns 1 for success and 0 for failure */
298 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
303 out = parse(command, ifd);
317 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
318 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
320 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
322 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
323 result += execute("ip link set %iface% up", ifd, exec);
324 return ((result == 2) ? 2 : 0);
326 return execute("ifconfig %iface% add ::1", ifd, exec);
330 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
332 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
333 return execute("ip link set %iface% down", ifd, exec);
335 return execute("ifconfig %iface% del ::1", ifd, exec);
339 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
342 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
343 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
344 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
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 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
357 return execute("ip link set %iface% down", ifd, exec);
359 return execute("ifconfig %iface% down", ifd, exec);
363 #ifdef CONFIG_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 #ifdef CONFIG_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 sizeof(methods6) / sizeof(struct method_t),
394 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
396 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
397 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
399 #ifdef CONFIG_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 #ifdef CONFIG_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 #ifdef CONFIG_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 result = execute("ifconfig %iface% %address% netmask %netmask% "
432 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
433 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
435 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
436 return ((result == 2) ? 2 : 0);
440 static int static_down(struct interface_defn_t *ifd, execfn *exec)
443 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
444 result = execute("ip addr flush dev %iface%", ifd, exec);
445 result += execute("ip link set %iface% down", ifd, exec);
447 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
448 result += execute("ifconfig %iface% down", ifd, exec);
450 return ((result == 2) ? 2 : 0);
453 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
455 if (execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface% "
456 "[[-H %hostname%]] [[-c %clientid%]] [[-s %script%]]", ifd, exec))
459 /* 2006-09-30: The following are deprecated, and should eventually be
460 * removed. For non-busybox (i.e., other than udhcpc) clients, use
461 * 'iface foo inet manual' in /etc/network/interfaces, and supply
462 * start/stop commands explicitly via up/down. */
464 if (execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]",
465 ifd, exec)) return 1;
466 if (execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
467 ifd, exec)) return 1;
468 if (execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
469 "[[-l %leasetime%]] %iface%", ifd, exec)) return 1;
474 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
476 if (execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
477 ifd, exec)) return 1;
479 /* 2006-09-30: The following are deprecated, and should eventually be
480 * removed. For non-busybox (i.e., other than udhcpc) clients, use
481 * 'iface foo inet manual' in /etc/network/interfaces, and supply
482 * start/stop commands explicitly via up/down. */
484 if (execute("pump -i %iface% -k", ifd, exec)) return 1;
485 if (execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
486 ifd, exec)) return 1;
487 if (execute("dhcpcd -k %iface%", ifd, exec)) return 1;
489 return static_down(ifd, exec);
492 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
497 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
499 return execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
500 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
501 "--returniffail --serverbcast", ifd, exec);
504 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
506 return execute("pon [[%provider%]]", ifd, exec);
509 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
511 return execute("poff [[%provider%]]", ifd, exec);
514 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
516 return execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
517 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec);
520 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
522 return execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
523 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
526 static const struct method_t methods[] = {
527 { "manual", manual_up_down, manual_up_down, },
528 { "wvdial", wvdial_up, wvdial_down, },
529 { "ppp", ppp_up, ppp_down, },
530 { "static", static_up, static_down, },
531 { "bootp", bootp_up, static_down, },
532 { "dhcp", dhcp_up, dhcp_down, },
533 { "loopback", loopback_up, loopback_down, },
536 static const struct address_family_t addr_inet = {
538 sizeof(methods) / sizeof(struct method_t),
542 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
544 static char *next_word(char **buf)
546 unsigned short length;
549 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
553 /* Skip over leading whitespace */
555 while (isspace(*word)) {
559 /* Skip over comments */
564 /* Find the length of this word */
565 length = strcspn(word, " \t\n");
569 *buf = word + length;
570 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
579 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
583 for (i = 0; af[i]; i++) {
584 if (strcmp(af[i]->name, name) == 0) {
591 static const struct method_t *get_method(const struct address_family_t *af, char *name)
595 for (i = 0; i < af->n_methods; i++) {
596 if (strcmp(af->method[i].name, name) == 0) {
597 return &af->method[i];
603 static const llist_t *find_list_string(const llist_t *list, const char *string)
606 if (strcmp(list->data, string) == 0) {
614 static struct interfaces_file_t *read_interfaces(const char *filename)
616 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
617 struct mapping_defn_t *currmap = NULL;
619 struct interface_defn_t *currif = NULL;
620 struct interfaces_file_t *defn;
625 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
627 defn = xzalloc(sizeof(struct interfaces_file_t));
629 f = xfopen(filename, "r");
631 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
634 firstword = next_word(&buf_ptr);
635 if (firstword == NULL) {
637 continue; /* blank line */
640 if (strcmp(firstword, "mapping") == 0) {
641 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
642 currmap = xzalloc(sizeof(struct mapping_defn_t));
644 while ((firstword = next_word(&buf_ptr)) != NULL) {
645 if (currmap->max_matches == currmap->n_matches) {
646 currmap->max_matches = currmap->max_matches * 2 + 1;
647 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
650 currmap->match[currmap->n_matches++] = xstrdup(firstword);
652 currmap->max_mappings = 0;
653 currmap->n_mappings = 0;
654 currmap->mapping = NULL;
655 currmap->script = NULL;
657 struct mapping_defn_t **where = &defn->mappings;
658 while (*where != NULL) {
659 where = &(*where)->next;
662 currmap->next = NULL;
664 debug_noise("Added mapping\n");
666 currently_processing = MAPPING;
667 } else if (strcmp(firstword, "iface") == 0) {
668 static const struct address_family_t *const addr_fams[] = {
669 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
672 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
679 char *address_family_name;
683 currif = xzalloc(sizeof(struct interface_defn_t));
684 iface_name = next_word(&buf_ptr);
685 address_family_name = next_word(&buf_ptr);
686 method_name = next_word(&buf_ptr);
688 if (buf_ptr == NULL) {
689 bb_error_msg("too few parameters for line \"%s\"", buf);
693 /* ship any trailing whitespace */
694 while (isspace(*buf_ptr)) {
698 if (buf_ptr[0] != '\0') {
699 bb_error_msg("too many parameters \"%s\"", buf);
703 currif->iface = xstrdup(iface_name);
705 currif->address_family = get_address_family(addr_fams, address_family_name);
706 if (!currif->address_family) {
707 bb_error_msg("unknown address type \"%s\"", address_family_name);
711 currif->method = get_method(currif->address_family, method_name);
712 if (!currif->method) {
713 bb_error_msg("unknown method \"%s\"", method_name);
717 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
718 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
719 if ((strcmp(tmp->iface, currif->iface) == 0) &&
720 (tmp->address_family == currif->address_family)) {
721 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
725 llist_add_to_end(&(defn->ifaces), (char*)currif);
727 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
728 currently_processing = IFACE;
729 } else if (strcmp(firstword, "auto") == 0) {
730 while ((firstword = next_word(&buf_ptr)) != NULL) {
732 /* Check the interface isnt already listed */
733 if (find_list_string(defn->autointerfaces, firstword)) {
734 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
737 /* Add the interface to the list */
738 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
739 debug_noise("\nauto %s\n", firstword);
741 currently_processing = NONE;
743 switch (currently_processing) {
748 if (strlen(buf_ptr) == 0) {
749 bb_error_msg("option with empty value \"%s\"", buf);
753 if (strcmp(firstword, "up") != 0
754 && strcmp(firstword, "down") != 0
755 && strcmp(firstword, "pre-up") != 0
756 && strcmp(firstword, "post-down") != 0) {
757 for (i = 0; i < currif->n_options; i++) {
758 if (strcmp(currif->option[i].name, firstword) == 0) {
759 bb_error_msg("duplicate option \"%s\"", buf);
765 if (currif->n_options >= currif->max_options) {
766 struct variable_t *opt;
768 currif->max_options = currif->max_options + 10;
769 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
770 currif->option = opt;
772 currif->option[currif->n_options].name = xstrdup(firstword);
773 currif->option[currif->n_options].value = xstrdup(buf_ptr);
774 if (!currif->option[currif->n_options].name) {
778 if (!currif->option[currif->n_options].value) {
782 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
783 currif->option[currif->n_options].value);
787 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
788 if (strcmp(firstword, "script") == 0) {
789 if (currmap->script != NULL) {
790 bb_error_msg("duplicate script in mapping \"%s\"", buf);
793 currmap->script = xstrdup(next_word(&buf_ptr));
795 } else if (strcmp(firstword, "map") == 0) {
796 if (currmap->max_mappings == currmap->n_mappings) {
797 currmap->max_mappings = currmap->max_mappings * 2 + 1;
798 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
800 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
801 currmap->n_mappings++;
803 bb_error_msg("misplaced option \"%s\"", buf);
810 bb_error_msg("misplaced option \"%s\"", buf);
816 if (ferror(f) != 0) {
817 bb_perror_msg_and_die("%s", filename);
824 static char *setlocalenv(char *format, const char *name, const char *value)
830 result = xasprintf(format, name, value);
832 for (here = there = result; *there != '=' && *there; there++) {
836 *there = toupper(*there);
838 if (isalnum(*there) || *there == '_') {
843 memmove(here, there, strlen(there) + 1);
848 static void set_environ(struct interface_defn_t *iface, const char *mode)
852 const int n_env_entries = iface->n_options + 5;
855 if (__myenviron != NULL) {
856 for (ppch = __myenviron; *ppch; ppch++) {
862 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
863 environend = __myenviron;
865 for (i = 0; i < iface->n_options; i++) {
866 if (strcmp(iface->option[i].name, "up") == 0
867 || strcmp(iface->option[i].name, "down") == 0
868 || strcmp(iface->option[i].name, "pre-up") == 0
869 || strcmp(iface->option[i].name, "post-down") == 0) {
872 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
875 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
876 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
877 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
878 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
879 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
882 static int doit(char *str)
884 if (option_mask & (OPT_no_act|OPT_verbose)) {
887 if (!(option_mask & OPT_no_act)) {
892 switch (child = fork()) {
893 case -1: /* failure */
896 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
899 waitpid(child, &status, 0);
900 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
907 static int execute_all(struct interface_defn_t *ifd, const char *opt)
911 for (i = 0; i < ifd->n_options; i++) {
912 if (strcmp(ifd->option[i].name, opt) == 0) {
913 if (!doit(ifd->option[i].value)) {
919 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
920 if (doit(buf) != 1) {
926 static int check(char *str) {
930 static int iface_up(struct interface_defn_t *iface)
932 if (!iface->method->up(iface,check)) return -1;
933 set_environ(iface, "start");
934 if (!execute_all(iface, "pre-up")) return 0;
935 if (!iface->method->up(iface, doit)) return 0;
936 if (!execute_all(iface, "up")) return 0;
940 static int iface_down(struct interface_defn_t *iface)
942 if (!iface->method->down(iface,check)) return -1;
943 set_environ(iface, "stop");
944 if (!execute_all(iface, "down")) return 0;
945 if (!iface->method->down(iface, doit)) return 0;
946 if (!execute_all(iface, "post-down")) return 0;
950 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
951 static int popen2(FILE **in, FILE **out, char *command, ...)
954 char *argv[11] = { command };
956 int infd[2], outfd[2];
960 va_start(ap, command);
961 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
964 argv[argc] = NULL; /* make sure */
967 if (pipe(infd) != 0) {
971 if (pipe(outfd) != 0) {
978 switch (pid = fork()) {
979 case -1: /* failure */
992 execvp(command, argv);
994 default: /* parent */
995 *in = fdopen(infd[1], "w");
996 *out = fdopen(outfd[0], "r");
1004 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1010 char *logical = xstrdup(physical);
1012 /* Run the mapping script. */
1013 pid = popen2(&in, &out, map->script, physical, NULL);
1015 /* popen2() returns 0 on failure. */
1019 /* Write mappings to stdin of mapping script. */
1020 for (i = 0; i < map->n_mappings; i++) {
1021 fprintf(in, "%s\n", map->mapping[i]);
1024 waitpid(pid, &status, 0);
1026 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1027 /* If the mapping script exited successfully, try to
1028 * grab a line of output and use that as the name of the
1029 * logical interface. */
1030 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1032 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1033 /* If we are able to read a line of output from the script,
1034 * remove any trailing whitespace and use this value
1035 * as the name of the logical interface. */
1036 char *pch = new_logical + strlen(new_logical) - 1;
1038 while (pch >= new_logical && isspace(*pch))
1042 logical = new_logical;
1044 /* If we are UNABLE to read a line of output, discard our
1045 * freshly allocated memory. */
1054 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1056 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1058 unsigned short iface_len = strlen(iface);
1059 llist_t *search = state_list;
1062 if ((strncmp(search->data, iface, iface_len) == 0) &&
1063 (search->data[iface_len] == '=')) {
1066 search = search->link;
1071 int ifupdown_main(int argc, char **argv)
1073 static const char statefile[] = "/var/run/ifstate";
1075 int (*cmds)(struct interface_defn_t *) = NULL;
1076 struct interfaces_file_t *defn;
1077 llist_t *state_list = NULL;
1078 llist_t *target_list = NULL;
1079 const char *interfaces = "/etc/network/interfaces";
1080 int any_failures = 0;
1083 if (bb_applet_name[2] == 'u') {
1087 /* ifdown command */
1091 option_mask = getopt32(argc, argv, OPTION_STR, &interfaces);
1092 if (argc - optind > 0) {
1093 if (DO_ALL) bb_show_usage();
1095 if (!DO_ALL) bb_show_usage();
1097 debug_noise("reading %s file:\n", interfaces);
1098 defn = read_interfaces(interfaces);
1099 debug_noise("\ndone reading %s\n\n", interfaces);
1105 startup_PATH = getenv("PATH");
1106 if (!startup_PATH) startup_PATH = "";
1108 /* Create a list of interfaces to work on */
1110 if (cmds == iface_up) {
1111 target_list = defn->autointerfaces;
1114 const llist_t *list = state_list;
1116 llist_add_to_end(&target_list, xstrdup(list->data));
1119 target_list = defn->autointerfaces;
1122 llist_add_to_end(&target_list, argv[optind]);
1126 /* Update the interfaces */
1127 while (target_list) {
1128 llist_t *iface_list;
1129 struct interface_defn_t *currif;
1136 iface = xstrdup(target_list->data);
1137 target_list = target_list->link;
1139 pch = strchr(iface, '=');
1142 liface = xstrdup(pch + 1);
1144 liface = xstrdup(iface);
1148 const llist_t *iface_state = find_iface_state(state_list, iface);
1150 if (cmds == iface_up) {
1153 bb_error_msg("interface %s already configured", iface);
1159 bb_error_msg("interface %s not configured", iface);
1165 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1166 if ((cmds == iface_up) && !NO_MAPPINGS) {
1167 struct mapping_defn_t *currmap;
1169 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1170 for (i = 0; i < currmap->n_matches; i++) {
1171 if (fnmatch(currmap->match[i], liface, 0) != 0)
1174 printf("Running mapping script %s on %s\n", currmap->script, liface);
1176 liface = run_mapping(iface, currmap);
1184 iface_list = defn->ifaces;
1185 while (iface_list) {
1186 currif = (struct interface_defn_t *) iface_list->data;
1187 if (strcmp(liface, currif->iface) == 0) {
1188 char *oldiface = currif->iface;
1191 currif->iface = iface;
1193 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1195 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1196 cmds_ret = cmds(currif);
1197 if (cmds_ret == -1) {
1198 bb_error_msg("don't seem to have all the variables for %s/%s",
1199 liface, currif->address_family->name);
1201 } else if (cmds_ret == 0) {
1205 currif->iface = oldiface;
1207 iface_list = iface_list->link;
1213 if (!okay && !FORCE) {
1214 bb_error_msg("ignoring unknown interface %s", liface);
1217 llist_t *iface_state = find_iface_state(state_list, iface);
1219 if (cmds == iface_up) {
1220 char *newiface = xasprintf("%s=%s", iface, liface);
1221 if (iface_state == NULL) {
1222 llist_add_to_end(&state_list, newiface);
1224 free(iface_state->data);
1225 iface_state->data = newiface;
1228 /* Remove an interface from the linked list */
1229 free(llist_pop(&iface_state));
1234 /* Actually write the new state */
1236 FILE *state_fp = NULL;
1238 state_fp = xfopen(statefile, "w");
1239 while (state_list) {
1240 if (state_list->data) {
1241 fputs(state_list->data, state_fp);
1242 fputc('\n', state_fp);
1244 state_list = state_list->link;