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 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
116 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
118 static unsigned int count_bits(unsigned int a)
121 result = (a & 0x55) + ((a >> 1) & 0x55);
122 result = (result & 0x33) + ((result >> 2) & 0x33);
123 return((result & 0x0F) + ((result >> 4) & 0x0F));
126 static int count_netmask_bits(char *dotted_quad)
128 unsigned int result, a, b, c, d;
129 /* Found a netmask... Check if it is dotted quad */
130 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
132 result = count_bits(a);
133 result += count_bits(b);
134 result += count_bits(c);
135 result += count_bits(d);
136 return ((int)result);
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, 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, 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_IPV6
321 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
323 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
325 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
326 result += execute("ip link set %iface% up", ifd, exec);
327 return ((result == 2) ? 2 : 0);
329 return( execute("ifconfig %iface% add ::1", ifd, exec));
333 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
335 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
336 return(execute("ip link set %iface% down", ifd, exec));
338 return(execute("ifconfig %iface% del ::1", ifd, exec));
342 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
345 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
346 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
347 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
348 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
350 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
351 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
352 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
354 return ((result == 3) ? 3 : 0);
357 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
359 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
360 return(execute("ip link set %iface% down", ifd, exec));
362 return(execute("ifconfig %iface% down", ifd, exec));
366 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
367 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
370 result = execute("ip tunnel add %iface% mode sit remote "
371 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
372 result += execute("ip link set %iface% up", ifd, exec);
373 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
374 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
375 return ((result == 4) ? 4 : 0);
378 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
380 return( execute("ip tunnel del %iface%", ifd, exec));
384 static struct method_t methods6[] = {
385 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
386 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
388 { "static", static_up6, static_down6, },
389 { "loopback", loopback_up6, loopback_down6, },
392 static struct address_family_t addr_inet6 = {
394 sizeof(methods6) / sizeof(struct method_t),
397 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
399 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
400 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
402 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
404 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
405 result += execute("ip link set %iface% up", ifd, exec);
406 return ((result == 2) ? 2 : 0);
408 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
412 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
414 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
416 result = execute("ip addr flush dev %iface%", ifd, exec);
417 result += execute("ip link set %iface% down", ifd, exec);
418 return ((result == 2) ? 2 : 0);
420 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
424 static int static_up(struct interface_defn_t *ifd, execfn *exec)
427 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
428 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
429 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
430 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
431 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
432 return ((result == 3) ? 3 : 0);
434 result = execute("ifconfig %iface% %address% netmask %netmask% "
435 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
436 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
438 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
439 return ((result == 2) ? 2 : 0);
443 static int static_down(struct interface_defn_t *ifd, execfn *exec)
446 #ifdef CONFIG_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 static int execable(char *program)
459 if (0 == stat(program, &buf)) {
460 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
467 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
469 if (execable("/sbin/udhcpc")) {
470 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
471 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
472 } else if (execable("/sbin/pump")) {
473 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
474 } else if (execable("/sbin/dhclient")) {
475 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
476 } else if (execable("/sbin/dhcpcd")) {
477 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
478 "[[-l %leasetime%]] %iface%", ifd, exec));
483 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
486 if (execable("/sbin/udhcpc")) {
487 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
488 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
489 * sequentially so we don't need to sleep */
490 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
491 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
492 } else if (execable("/sbin/pump")) {
493 result = execute("pump -i %iface% -k", ifd, exec);
494 } else if (execable("/sbin/dhclient")) {
495 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
496 } else if (execable("/sbin/dhcpcd")) {
497 result = execute("dhcpcd -k %iface%", ifd, exec);
499 return (result || static_down(ifd, exec));
502 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
504 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
505 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
506 "--returniffail --serverbcast", ifd, exec));
509 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
511 return( execute("pon [[%provider%]]", ifd, exec));
514 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
516 return( execute("poff [[%provider%]]", ifd, exec));
519 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
521 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
522 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
525 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
527 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
528 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
531 static struct method_t methods[] =
533 { "wvdial", wvdial_up, wvdial_down, },
534 { "ppp", ppp_up, ppp_down, },
535 { "static", static_up, static_down, },
536 { "bootp", bootp_up, static_down, },
537 { "dhcp", dhcp_up, dhcp_down, },
538 { "loopback", loopback_up, loopback_down, },
541 static struct address_family_t addr_inet =
544 sizeof(methods) / sizeof(struct method_t),
548 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
550 static char *next_word(char **buf)
552 unsigned short length;
555 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
559 /* Skip over leading whitespace */
561 while (isspace(*word)) {
565 /* Skip over comments */
570 /* Find the length of this word */
571 length = strcspn(word, " \t\n");
575 *buf = word + length;
576 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
585 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
589 for (i = 0; af[i]; i++) {
590 if (strcmp(af[i]->name, name) == 0) {
597 static struct method_t *get_method(struct address_family_t *af, char *name)
601 for (i = 0; i < af->n_methods; i++) {
602 if (strcmp(af->method[i].name, name) == 0) {
603 return &af->method[i];
609 static const llist_t *find_list_string(const llist_t *list, const char *string)
612 if (strcmp(list->data, string) == 0) {
620 static struct interfaces_file_t *read_interfaces(const char *filename)
622 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
623 struct mapping_defn_t *currmap = NULL;
625 struct interface_defn_t *currif = NULL;
626 struct interfaces_file_t *defn;
631 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
633 defn = xzalloc(sizeof(struct interfaces_file_t));
635 f = bb_xfopen(filename, "r");
637 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
640 firstword = next_word(&buf_ptr);
641 if (firstword == NULL) {
643 continue; /* blank line */
646 if (strcmp(firstword, "mapping") == 0) {
647 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
648 currmap = xzalloc(sizeof(struct mapping_defn_t));
650 while ((firstword = next_word(&buf_ptr)) != NULL) {
651 if (currmap->max_matches == currmap->n_matches) {
652 currmap->max_matches = currmap->max_matches * 2 + 1;
653 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
656 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
658 currmap->max_mappings = 0;
659 currmap->n_mappings = 0;
660 currmap->mapping = NULL;
661 currmap->script = NULL;
663 struct mapping_defn_t **where = &defn->mappings;
664 while (*where != NULL) {
665 where = &(*where)->next;
668 currmap->next = NULL;
670 debug_noise("Added mapping\n");
672 currently_processing = MAPPING;
673 } else if (strcmp(firstword, "iface") == 0) {
676 char *address_family_name;
678 struct address_family_t *addr_fams[] = {
679 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
682 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
688 currif = xzalloc(sizeof(struct interface_defn_t));
689 iface_name = next_word(&buf_ptr);
690 address_family_name = next_word(&buf_ptr);
691 method_name = next_word(&buf_ptr);
693 if (buf_ptr == NULL) {
694 bb_error_msg("too few parameters for line \"%s\"", buf);
698 /* ship any trailing whitespace */
699 while (isspace(*buf_ptr)) {
703 if (buf_ptr[0] != '\0') {
704 bb_error_msg("too many parameters \"%s\"", buf);
708 currif->iface = bb_xstrdup(iface_name);
710 currif->address_family = get_address_family(addr_fams, address_family_name);
711 if (!currif->address_family) {
712 bb_error_msg("unknown address type \"%s\"", address_family_name);
716 currif->method = get_method(currif->address_family, method_name);
717 if (!currif->method) {
718 bb_error_msg("unknown method \"%s\"", method_name);
725 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
726 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
727 if ((strcmp(tmp->iface, currif->iface) == 0) &&
728 (tmp->address_family == currif->address_family)) {
729 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
734 llist_add_to_end(&(defn->ifaces), (char*)currif);
736 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
738 currently_processing = IFACE;
739 } else if (strcmp(firstword, "auto") == 0) {
740 while ((firstword = next_word(&buf_ptr)) != NULL) {
742 /* Check the interface isnt already listed */
743 if (find_list_string(defn->autointerfaces, firstword)) {
744 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
747 /* Add the interface to the list */
748 llist_add_to_end(&(defn->autointerfaces), bb_xstrdup(firstword));
749 debug_noise("\nauto %s\n", firstword);
751 currently_processing = NONE;
753 switch (currently_processing) {
758 if (strlen(buf_ptr) == 0) {
759 bb_error_msg("option with empty value \"%s\"", buf);
763 if (strcmp(firstword, "up") != 0
764 && strcmp(firstword, "down") != 0
765 && strcmp(firstword, "pre-up") != 0
766 && strcmp(firstword, "post-down") != 0) {
767 for (i = 0; i < currif->n_options; i++) {
768 if (strcmp(currif->option[i].name, firstword) == 0) {
769 bb_error_msg("duplicate option \"%s\"", buf);
775 if (currif->n_options >= currif->max_options) {
776 struct variable_t *opt;
778 currif->max_options = currif->max_options + 10;
779 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
780 currif->option = opt;
782 currif->option[currif->n_options].name = bb_xstrdup(firstword);
783 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
784 if (!currif->option[currif->n_options].name) {
788 if (!currif->option[currif->n_options].value) {
792 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
793 currif->option[currif->n_options].value);
797 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
798 if (strcmp(firstword, "script") == 0) {
799 if (currmap->script != NULL) {
800 bb_error_msg("duplicate script in mapping \"%s\"", buf);
803 currmap->script = bb_xstrdup(next_word(&buf_ptr));
805 } else if (strcmp(firstword, "map") == 0) {
806 if (currmap->max_mappings == currmap->n_mappings) {
807 currmap->max_mappings = currmap->max_mappings * 2 + 1;
808 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
810 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
811 currmap->n_mappings++;
813 bb_error_msg("misplaced option \"%s\"", buf);
820 bb_error_msg("misplaced option \"%s\"", buf);
826 if (ferror(f) != 0) {
827 bb_perror_msg_and_die("%s", filename);
834 static char *setlocalenv(char *format, const char *name, const char *value)
840 result = bb_xasprintf(format, name, value);
842 for (here = there = result; *there != '=' && *there; there++) {
846 *there = toupper(*there);
848 if (isalnum(*there) || *there == '_') {
853 memmove(here, there, strlen(there) + 1);
858 static void set_environ(struct interface_defn_t *iface, const char *mode)
862 const int n_env_entries = iface->n_options + 5;
865 if (__myenviron != NULL) {
866 for (ppch = __myenviron; *ppch; ppch++) {
872 __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
873 environend = __myenviron;
875 for (i = 0; i < iface->n_options; i++) {
876 if (strcmp(iface->option[i].name, "up") == 0
877 || strcmp(iface->option[i].name, "down") == 0
878 || strcmp(iface->option[i].name, "pre-up") == 0
879 || strcmp(iface->option[i].name, "post-down") == 0) {
882 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
885 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
886 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
887 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
888 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
889 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
892 static int doit(char *str)
894 if (verbose || no_act) {
902 switch (child = fork()) {
903 case -1: /* failure */
906 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
909 waitpid(child, &status, 0);
910 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
917 static int execute_all(struct interface_defn_t *ifd, const char *opt)
921 for (i = 0; i < ifd->n_options; i++) {
922 if (strcmp(ifd->option[i].name, opt) == 0) {
923 if (!doit(ifd->option[i].value)) {
929 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
930 if (doit(buf) != 1) {
936 static int check(char *str) {
940 static int iface_up(struct interface_defn_t *iface)
942 if (!iface->method->up(iface,check)) return -1;
943 set_environ(iface, "start");
944 if (!execute_all(iface, "pre-up")) return 0;
945 if (!iface->method->up(iface, doit)) return 0;
946 if (!execute_all(iface, "up")) return 0;
950 static int iface_down(struct interface_defn_t *iface)
952 if (!iface->method->down(iface,check)) return -1;
953 set_environ(iface, "stop");
954 if (!execute_all(iface, "down")) return 0;
955 if (!iface->method->down(iface, doit)) return 0;
956 if (!execute_all(iface, "post-down")) return 0;
960 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
961 static int popen2(FILE **in, FILE **out, char *command, ...)
964 char *argv[11] = { command };
966 int infd[2], outfd[2];
970 va_start(ap, command);
971 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
974 argv[argc] = NULL; /* make sure */
977 if (pipe(infd) != 0) {
981 if (pipe(outfd) != 0) {
988 switch (pid = fork()) {
989 case -1: /* failure */
1002 execvp(command, argv);
1004 default: /* parent */
1005 *in = fdopen(infd[1], "w");
1006 *out = fdopen(outfd[0], "r");
1014 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1020 char *logical = bb_xstrdup(physical);
1022 /* Run the mapping script. */
1023 pid = popen2(&in, &out, map->script, physical, NULL);
1025 /* popen2() returns 0 on failure. */
1029 /* Write mappings to stdin of mapping script. */
1030 for (i = 0; i < map->n_mappings; i++) {
1031 fprintf(in, "%s\n", map->mapping[i]);
1034 waitpid(pid, &status, 0);
1036 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1037 /* If the mapping script exited successfully, try to
1038 * grab a line of output and use that as the name of the
1039 * logical interface. */
1040 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1042 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1043 /* If we are able to read a line of output from the script,
1044 * remove any trailing whitespace and use this value
1045 * as the name of the logical interface. */
1046 char *pch = new_logical + strlen(new_logical) - 1;
1048 while (pch >= new_logical && isspace(*pch))
1052 logical = new_logical;
1054 /* If we are UNABLE to read a line of output, discard our
1055 * freshly allocated memory. */
1064 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1066 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1068 unsigned short iface_len = strlen(iface);
1069 llist_t *search = state_list;
1072 if ((strncmp(search->data, iface, iface_len) == 0) &&
1073 (search->data[iface_len] == '=')) {
1076 search = search->link;
1081 int ifupdown_main(int argc, char **argv)
1083 int (*cmds) (struct interface_defn_t *) = NULL;
1084 struct interfaces_file_t *defn;
1085 llist_t *state_list = NULL;
1086 llist_t *target_list = NULL;
1087 const char *interfaces = "/etc/network/interfaces";
1088 const char *statefile = "/var/run/ifstate";
1090 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1091 int run_mappings = 1;
1095 int any_failures = 0;
1098 if (bb_applet_name[2] == 'u') {
1102 /* ifdown command */
1106 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1107 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1109 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1113 case 'i': /* interfaces */
1114 interfaces = optarg;
1116 case 'v': /* verbose */
1122 case 'n': /* no-act */
1125 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1126 case 'm': /* no-mappings */
1130 case 'f': /* force */
1139 if (argc - optind > 0) {
1149 debug_noise("reading %s file:\n", interfaces);
1150 defn = read_interfaces(interfaces);
1151 debug_noise("\ndone reading %s\n\n", interfaces);
1157 /* Create a list of interfaces to work on */
1159 if (cmds == iface_up) {
1160 target_list = defn->autointerfaces;
1163 const llist_t *list = state_list;
1165 llist_add_to_end(&target_list, bb_xstrdup(list->data));
1168 target_list = defn->autointerfaces;
1171 llist_add_to_end(&target_list, argv[optind]);
1175 /* Update the interfaces */
1176 while (target_list) {
1177 llist_t *iface_list;
1178 struct interface_defn_t *currif;
1185 iface = bb_xstrdup(target_list->data);
1186 target_list = target_list->link;
1188 pch = strchr(iface, '=');
1191 liface = bb_xstrdup(pch + 1);
1193 liface = bb_xstrdup(iface);
1197 const llist_t *iface_state = find_iface_state(state_list, iface);
1199 if (cmds == iface_up) {
1202 bb_error_msg("interface %s already configured", iface);
1208 bb_error_msg("interface %s not configured", iface);
1214 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1215 if ((cmds == iface_up) && run_mappings) {
1216 struct mapping_defn_t *currmap;
1218 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1220 for (i = 0; i < currmap->n_matches; i++) {
1221 if (fnmatch(currmap->match[i], liface, 0) != 0)
1224 printf("Running mapping script %s on %s\n", currmap->script, liface);
1226 liface = run_mapping(iface, currmap);
1234 iface_list = defn->ifaces;
1235 while (iface_list) {
1236 currif = (struct interface_defn_t *) iface_list->data;
1237 if (strcmp(liface, currif->iface) == 0) {
1238 char *oldiface = currif->iface;
1241 currif->iface = iface;
1243 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1245 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1246 cmds_ret = cmds(currif);
1247 if (cmds_ret == -1) {
1248 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1249 liface, currif->address_family->name);
1251 } else if (cmds_ret == 0) {
1255 currif->iface = oldiface;
1257 iface_list = iface_list->link;
1263 if (!okay && !force) {
1264 bb_error_msg("Ignoring unknown interface %s", liface);
1267 llist_t *iface_state = find_iface_state(state_list, iface);
1269 if (cmds == iface_up) {
1270 char *newiface = bb_xasprintf("%s=%s", iface, liface);
1271 if (iface_state == NULL) {
1272 llist_add_to_end(&state_list, newiface);
1274 free(iface_state->data);
1275 iface_state->data = newiface;
1278 /* Remove an interface from the linked list */
1279 free(llist_pop(&iface_state));
1284 /* Actually write the new state */
1286 FILE *state_fp = NULL;
1288 state_fp = bb_xfopen(statefile, "w");
1289 while (state_list) {
1290 if (state_list->data) {
1291 fputs(state_list->data, state_fp);
1292 fputc('\n', state_fp);
1294 state_list = state_list->link;