1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
5 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
7 * Based on ifupdown v 0.6.4 by Anthony Towns
8 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
10 * Changes to upstream version
11 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
12 * Lines in the interfaces file cannot wrap.
13 * To adhere to the FHS, the default state file is /var/run/ifstate.
15 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18 /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
21 #include <sys/utsname.h>
37 #define MAX_OPT_DEPTH 10
38 #define EUNBALBRACK 10001
39 #define EUNDEFVAR 10002
40 #define EUNBALPER 10000
42 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43 #define MAX_INTERFACE_LENGTH 10
47 #define debug_noise(fmt, args...) printf(fmt, ## args)
49 #define debug_noise(fmt, args...)
52 /* Forward declaration */
53 struct interface_defn_t;
55 typedef int (execfn)(char *command);
60 int (*up)(struct interface_defn_t *ifd, execfn *e);
61 int (*down)(struct interface_defn_t *ifd, execfn *e);
64 struct address_family_t
68 struct method_t *method;
73 struct mapping_defn_t *next;
92 struct interface_defn_t
94 struct address_family_t *address_family;
95 struct method_t *method;
100 struct variable_t *option;
103 struct interfaces_file_t
105 llist_t *autointerfaces;
107 struct mapping_defn_t *mappings;
110 static char no_act = 0;
111 static char verbose = 0;
112 static char **__myenviron = NULL;
114 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
116 static unsigned int count_bits(unsigned int a)
119 result = (a & 0x55) + ((a >> 1) & 0x55);
120 result = (result & 0x33) + ((result >> 2) & 0x33);
121 return((result & 0x0F) + ((result >> 4) & 0x0F));
124 static int count_netmask_bits(char *dotted_quad)
126 unsigned int result, a, b, c, d;
127 /* Found a netmask... Check if it is dotted quad */
128 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
130 result = count_bits(a);
131 result += count_bits(b);
132 result += count_bits(c);
133 result += count_bits(d);
134 return ((int)result);
138 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 || \
139 ENABLE_FEATURE_IFUPDOWN_IPX
140 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
142 if (*pos + str_length >= *len) {
145 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
147 *len = *len * 2 + str_length + 1;
150 while (str_length-- >= 1) {
151 (*buf)[(*pos)++] = *str;
157 static int strncmpz(char *l, char *r, size_t llen)
159 int i = strncmp(l, r, llen);
168 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
172 if (strncmpz(id, "iface", idlen) == 0) {
174 static char label_buf[20];
175 strncpy(label_buf, ifd->iface, 19);
177 result = strchr(label_buf, ':');
182 } else if (strncmpz(id, "label", idlen) == 0) {
185 for (i = 0; i < ifd->n_options; i++) {
186 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
187 return (ifd->option[i].value);
195 static char *parse(char *command, struct interface_defn_t *ifd)
199 size_t pos = 0, len = 0;
200 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
201 int okay[MAX_OPT_DEPTH] = { 1 };
208 addstr(&result, &len, &pos, command, 1);
213 addstr(&result, &len, &pos, command + 1, 1);
216 addstr(&result, &len, &pos, command, 1);
221 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
222 old_pos[opt_depth] = pos;
227 addstr(&result, &len, &pos, "[", 1);
232 if (command[1] == ']' && opt_depth > 1) {
234 if (!okay[opt_depth]) {
235 pos = old_pos[opt_depth];
240 addstr(&result, &len, &pos, "]", 1);
250 nextpercent = strchr(command, '%');
257 varvalue = get_var(command, nextpercent - command, ifd);
260 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
262 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
263 /* Sigh... Add a special case for 'ip' to convert from
264 * dotted quad to bit count style netmasks. */
265 if (strncmp(command, "bnmask", 6)==0) {
267 varvalue = get_var("netmask", 7, ifd);
268 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
270 sprintf(argument, "%d", res);
271 addstr(&result, &len, &pos, argument, bb_strlen(argument));
272 command = nextpercent + 1;
277 okay[opt_depth - 1] = 0;
280 command = nextpercent + 1;
301 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
306 out = parse(command, ifd);
320 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
321 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
323 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
326 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
328 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
331 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
333 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
336 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
338 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
341 static struct method_t methods_ipx[] = {
342 { "dynamic", dynamic_up, dynamic_down, },
343 { "static", static_up_ipx, static_down_ipx, },
346 static struct address_family_t addr_ipx = {
348 sizeof(methods_ipx) / sizeof(struct method_t),
351 #endif /* IFUP_FEATURE_IPX */
353 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
354 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
356 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
358 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
359 result += execute("ip link set %iface% up", ifd, exec);
360 return ((result == 2) ? 2 : 0);
362 return( execute("ifconfig %iface% add ::1", ifd, exec));
366 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
368 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
369 return(execute("ip link set %iface% down", ifd, exec));
371 return(execute("ifconfig %iface% del ::1", ifd, exec));
375 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
378 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
379 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
380 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
381 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
383 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
384 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
385 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
387 return ((result == 3) ? 3 : 0);
390 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
392 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
393 return(execute("ip link set %iface% down", ifd, exec));
395 return(execute("ifconfig %iface% down", ifd, exec));
399 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
400 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
403 result = execute("ip tunnel add %iface% mode sit remote "
404 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
405 result += execute("ip link set %iface% up", ifd, exec);
406 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
407 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
408 return ((result == 4) ? 4 : 0);
411 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
413 return( execute("ip tunnel del %iface%", ifd, exec));
417 static struct method_t methods6[] = {
418 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
419 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
421 { "static", static_up6, static_down6, },
422 { "loopback", loopback_up6, loopback_down6, },
425 static struct address_family_t addr_inet6 = {
427 sizeof(methods6) / sizeof(struct method_t),
430 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
432 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
433 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
435 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
437 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
438 result += execute("ip link set %iface% up", ifd, exec);
439 return ((result == 2) ? 2 : 0);
441 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
445 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
447 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
449 result = execute("ip addr flush dev %iface%", ifd, exec);
450 result += execute("ip link set %iface% down", ifd, exec);
451 return ((result == 2) ? 2 : 0);
453 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
457 static int static_up(struct interface_defn_t *ifd, execfn *exec)
460 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
461 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
462 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
463 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
464 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
465 return ((result == 3) ? 3 : 0);
467 result = execute("ifconfig %iface% %address% netmask %netmask% "
468 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
469 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
471 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
472 return ((result == 2) ? 2 : 0);
476 static int static_down(struct interface_defn_t *ifd, execfn *exec)
479 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
480 result = execute("ip addr flush dev %iface%", ifd, exec);
481 result += execute("ip link set %iface% down", ifd, exec);
483 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
484 result += execute("ifconfig %iface% down", ifd, exec);
486 return ((result == 2) ? 2 : 0);
489 static int execable(char *program)
492 if (0 == stat(program, &buf)) {
493 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
500 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
502 if (execable("/sbin/udhcpc")) {
503 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
504 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
505 } else if (execable("/sbin/pump")) {
506 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
507 } else if (execable("/sbin/dhclient")) {
508 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
509 } else if (execable("/sbin/dhcpcd")) {
510 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
511 "[[-l %leasetime%]] %iface%", ifd, exec));
516 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
519 if (execable("/sbin/udhcpc")) {
520 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
521 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
522 * sequentially so we don't need to sleep */
523 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
524 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
525 } else if (execable("/sbin/pump")) {
526 result = execute("pump -i %iface% -k", ifd, exec);
527 } else if (execable("/sbin/dhclient")) {
528 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
529 } else if (execable("/sbin/dhcpcd")) {
530 result = execute("dhcpcd -k %iface%", ifd, exec);
532 return (result || static_down(ifd, exec));
535 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
537 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
538 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
539 "--returniffail --serverbcast", ifd, exec));
542 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
544 return( execute("pon [[%provider%]]", ifd, exec));
547 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
549 return( execute("poff [[%provider%]]", ifd, exec));
552 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
554 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
555 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
558 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
560 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
561 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
564 static struct method_t methods[] =
566 { "wvdial", wvdial_up, wvdial_down, },
567 { "ppp", ppp_up, ppp_down, },
568 { "static", static_up, static_down, },
569 { "bootp", bootp_up, static_down, },
570 { "dhcp", dhcp_up, dhcp_down, },
571 { "loopback", loopback_up, loopback_down, },
574 static struct address_family_t addr_inet =
577 sizeof(methods) / sizeof(struct method_t),
581 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
583 static char *next_word(char **buf)
585 unsigned short length;
588 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
592 /* Skip over leading whitespace */
594 while (isspace(*word)) {
598 /* Skip over comments */
603 /* Find the length of this word */
604 length = strcspn(word, " \t\n");
608 *buf = word + length;
609 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
618 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
622 for (i = 0; af[i]; i++) {
623 if (strcmp(af[i]->name, name) == 0) {
630 static struct method_t *get_method(struct address_family_t *af, char *name)
634 for (i = 0; i < af->n_methods; i++) {
635 if (strcmp(af->method[i].name, name) == 0) {
636 return &af->method[i];
642 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 #ifdef CONFIG_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 = xmalloc(sizeof(struct interfaces_file_t));
667 defn->autointerfaces = NULL;
668 defn->mappings = NULL;
671 f = bb_xfopen(filename, "r");
673 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
676 firstword = next_word(&buf_ptr);
677 if (firstword == NULL) {
679 continue; /* blank line */
682 if (strcmp(firstword, "mapping") == 0) {
683 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
684 currmap = xmalloc(sizeof(struct mapping_defn_t));
685 currmap->max_matches = 0;
686 currmap->n_matches = 0;
687 currmap->match = NULL;
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++] = bb_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) {
715 char *address_family_name;
717 struct address_family_t *addr_fams[] = {
718 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
721 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
724 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
730 currif = xmalloc(sizeof(struct interface_defn_t));
731 iface_name = next_word(&buf_ptr);
732 address_family_name = next_word(&buf_ptr);
733 method_name = next_word(&buf_ptr);
735 if (buf_ptr == NULL) {
736 bb_error_msg("too few parameters for line \"%s\"", buf);
740 /* ship any trailing whitespace */
741 while (isspace(*buf_ptr)) {
745 if (buf_ptr[0] != '\0') {
746 bb_error_msg("too many parameters \"%s\"", buf);
750 currif->iface = bb_xstrdup(iface_name);
752 currif->address_family = get_address_family(addr_fams, address_family_name);
753 if (!currif->address_family) {
754 bb_error_msg("unknown address type \"%s\"", address_family_name);
758 currif->method = get_method(currif->address_family, method_name);
759 if (!currif->method) {
760 bb_error_msg("unknown method \"%s\"", method_name);
764 currif->max_options = 0;
765 currif->n_options = 0;
766 currif->option = NULL;
770 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
771 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
772 if ((strcmp(tmp->iface, currif->iface) == 0) &&
773 (tmp->address_family == currif->address_family)) {
774 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
779 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
781 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
783 currently_processing = IFACE;
784 } else if (strcmp(firstword, "auto") == 0) {
785 while ((firstword = next_word(&buf_ptr)) != NULL) {
787 /* Check the interface isnt already listed */
788 if (find_list_string(defn->autointerfaces, firstword)) {
789 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
792 /* Add the interface to the list */
793 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
794 debug_noise("\nauto %s\n", firstword);
796 currently_processing = NONE;
798 switch (currently_processing) {
803 if (bb_strlen(buf_ptr) == 0) {
804 bb_error_msg("option with empty value \"%s\"", buf);
808 if (strcmp(firstword, "up") != 0
809 && strcmp(firstword, "down") != 0
810 && strcmp(firstword, "pre-up") != 0
811 && strcmp(firstword, "post-down") != 0) {
812 for (i = 0; i < currif->n_options; i++) {
813 if (strcmp(currif->option[i].name, firstword) == 0) {
814 bb_error_msg("duplicate option \"%s\"", buf);
820 if (currif->n_options >= currif->max_options) {
821 struct variable_t *opt;
823 currif->max_options = currif->max_options + 10;
824 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
825 currif->option = opt;
827 currif->option[currif->n_options].name = bb_xstrdup(firstword);
828 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
829 if (!currif->option[currif->n_options].name) {
833 if (!currif->option[currif->n_options].value) {
837 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
838 currif->option[currif->n_options].value);
842 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
843 if (strcmp(firstword, "script") == 0) {
844 if (currmap->script != NULL) {
845 bb_error_msg("duplicate script in mapping \"%s\"", buf);
848 currmap->script = bb_xstrdup(next_word(&buf_ptr));
850 } else if (strcmp(firstword, "map") == 0) {
851 if (currmap->max_mappings == currmap->n_mappings) {
852 currmap->max_mappings = currmap->max_mappings * 2 + 1;
853 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
855 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
856 currmap->n_mappings++;
858 bb_error_msg("misplaced option \"%s\"", buf);
865 bb_error_msg("misplaced option \"%s\"", buf);
871 if (ferror(f) != 0) {
872 bb_perror_msg_and_die("%s", filename);
879 static char *setlocalenv(char *format, const char *name, const char *value)
885 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
887 sprintf(result, format, name, value);
889 for (here = there = result; *there != '=' && *there; there++) {
893 *there = toupper(*there);
895 if (isalnum(*there) || *there == '_') {
900 memmove(here, there, bb_strlen(there) + 1);
905 static void set_environ(struct interface_defn_t *iface, const char *mode)
909 const int n_env_entries = iface->n_options + 5;
912 if (__myenviron != NULL) {
913 for (ppch = __myenviron; *ppch; ppch++) {
920 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
921 environend = __myenviron;
924 for (i = 0; i < iface->n_options; i++) {
925 if (strcmp(iface->option[i].name, "up") == 0
926 || strcmp(iface->option[i].name, "down") == 0
927 || strcmp(iface->option[i].name, "pre-up") == 0
928 || strcmp(iface->option[i].name, "post-down") == 0) {
931 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
935 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
937 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
939 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
941 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
943 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
947 static int doit(char *str)
949 if (verbose || no_act) {
957 switch (child = fork()) {
958 case -1: /* failure */
961 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
964 waitpid(child, &status, 0);
965 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
972 static int execute_all(struct interface_defn_t *ifd, const char *opt)
976 for (i = 0; i < ifd->n_options; i++) {
977 if (strcmp(ifd->option[i].name, opt) == 0) {
978 if (!doit(ifd->option[i].value)) {
984 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
985 if (doit(buf) != 1) {
991 static int check(char *str) {
995 static int iface_up(struct interface_defn_t *iface)
997 if (!iface->method->up(iface,check)) return -1;
998 set_environ(iface, "start");
999 if (!execute_all(iface, "pre-up")) return 0;
1000 if (!iface->method->up(iface, doit)) return 0;
1001 if (!execute_all(iface, "up")) return 0;
1005 static int iface_down(struct interface_defn_t *iface)
1007 if (!iface->method->down(iface,check)) return -1;
1008 set_environ(iface, "stop");
1009 if (!execute_all(iface, "down")) return 0;
1010 if (!iface->method->down(iface, doit)) return 0;
1011 if (!execute_all(iface, "post-down")) return 0;
1015 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1016 static int popen2(FILE **in, FILE **out, char *command, ...)
1019 char *argv[11] = { command };
1021 int infd[2], outfd[2];
1025 va_start(ap, command);
1026 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1029 argv[argc] = NULL; /* make sure */
1032 if (pipe(infd) != 0) {
1036 if (pipe(outfd) != 0) {
1043 switch (pid = fork()) {
1044 case -1: /* failure */
1057 execvp(command, argv);
1059 default: /* parent */
1060 *in = fdopen(infd[1], "w");
1061 *out = fdopen(outfd[0], "r");
1069 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1075 char *logical = bb_xstrdup(physical);
1077 /* Run the mapping script. */
1078 pid = popen2(&in, &out, map->script, physical, NULL);
1080 /* popen2() returns 0 on failure. */
1084 /* Write mappings to stdin of mapping script. */
1085 for (i = 0; i < map->n_mappings; i++) {
1086 fprintf(in, "%s\n", map->mapping[i]);
1089 waitpid(pid, &status, 0);
1091 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1092 /* If the mapping script exited successfully, try to
1093 * grab a line of output and use that as the name of the
1094 * logical interface. */
1095 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1097 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1098 /* If we are able to read a line of output from the script,
1099 * remove any trailing whitespace and use this value
1100 * as the name of the logical interface. */
1101 char *pch = new_logical + bb_strlen(new_logical) - 1;
1103 while (pch >= new_logical && isspace(*pch))
1107 logical = new_logical;
1109 /* If we are UNABLE to read a line of output, discard are
1110 * freshly allocated memory. */
1119 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1121 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1123 unsigned short iface_len = bb_strlen(iface);
1124 llist_t *search = state_list;
1127 if ((strncmp(search->data, iface, iface_len) == 0) &&
1128 (search->data[iface_len] == '=')) {
1131 search = search->link;
1136 int ifupdown_main(int argc, char **argv)
1138 int (*cmds) (struct interface_defn_t *) = NULL;
1139 struct interfaces_file_t *defn;
1140 llist_t *state_list = NULL;
1141 llist_t *target_list = NULL;
1142 const char *interfaces = "/etc/network/interfaces";
1143 const char *statefile = "/var/run/ifstate";
1145 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1146 int run_mappings = 1;
1150 int any_failures = 0;
1153 if (bb_applet_name[2] == 'u') {
1157 /* ifdown command */
1161 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1162 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1164 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1168 case 'i': /* interfaces */
1169 interfaces = optarg;
1171 case 'v': /* verbose */
1177 case 'n': /* no-act */
1180 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1181 case 'm': /* no-mappings */
1185 case 'f': /* force */
1194 if (argc - optind > 0) {
1204 debug_noise("reading %s file:\n", interfaces);
1205 defn = read_interfaces(interfaces);
1206 debug_noise("\ndone reading %s\n\n", interfaces);
1212 /* Create a list of interfaces to work on */
1214 if (cmds == iface_up) {
1215 target_list = defn->autointerfaces;
1220 const llist_t *list = state_list;
1222 new_item = xmalloc(sizeof(llist_t));
1223 new_item->data = bb_xstrdup(list->data);
1224 new_item->link = NULL;
1227 target_list = new_item;
1229 while (list->link) {
1236 target_list = defn->autointerfaces;
1240 const llist_t *list = state_list;
1242 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
1245 target_list = defn->autointerfaces;
1249 target_list = llist_add_to_end(target_list, argv[optind]);
1253 /* Update the interfaces */
1254 while (target_list) {
1255 llist_t *iface_list;
1256 struct interface_defn_t *currif;
1263 iface = bb_xstrdup(target_list->data);
1264 target_list = target_list->link;
1266 pch = strchr(iface, '=');
1269 liface = bb_xstrdup(pch + 1);
1271 liface = bb_xstrdup(iface);
1275 const llist_t *iface_state = find_iface_state(state_list, iface);
1277 if (cmds == iface_up) {
1280 bb_error_msg("interface %s already configured", iface);
1286 bb_error_msg("interface %s not configured", iface);
1292 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1293 if ((cmds == iface_up) && run_mappings) {
1294 struct mapping_defn_t *currmap;
1296 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1298 for (i = 0; i < currmap->n_matches; i++) {
1299 if (fnmatch(currmap->match[i], liface, 0) != 0)
1302 printf("Running mapping script %s on %s\n", currmap->script, liface);
1304 liface = run_mapping(iface, currmap);
1312 iface_list = defn->ifaces;
1313 while (iface_list) {
1314 currif = (struct interface_defn_t *) iface_list->data;
1315 if (strcmp(liface, currif->iface) == 0) {
1316 char *oldiface = currif->iface;
1319 currif->iface = iface;
1321 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1323 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1324 cmds_ret = cmds(currif);
1325 if (cmds_ret == -1) {
1326 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1327 liface, currif->address_family->name);
1329 } else if (cmds_ret == 0) {
1333 currif->iface = oldiface;
1335 iface_list = iface_list->link;
1341 if (!okay && !force) {
1342 bb_error_msg("Ignoring unknown interface %s", liface);
1345 llist_t *iface_state = find_iface_state(state_list, iface);
1347 if (cmds == iface_up) {
1348 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1349 sprintf(newiface, "%s=%s", iface, liface);
1350 if (iface_state == NULL) {
1351 state_list = llist_add_to_end(state_list, newiface);
1353 free(iface_state->data);
1354 iface_state->data = newiface;
1357 /* Remove an interface from the linked list */
1359 llist_t *l = iface_state->link;
1360 free(iface_state->data);
1361 iface_state->data = NULL;
1362 iface_state->link = NULL;
1364 iface_state->data = l->data;
1365 iface_state->link = l->link;
1373 /* Actually write the new state */
1375 FILE *state_fp = NULL;
1377 state_fp = bb_xfopen(statefile, "w");
1378 while (state_list) {
1379 if (state_list->data) {
1380 fputs(state_list->data, state_fp);
1381 fputc('\n', state_fp);
1383 state_list = state_list->link;