1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath <bug1@optushome.com.au>
5 * Copyright (c) 2003 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 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <sys/utsname.h>
47 #define MAX_OPT_DEPTH 10
48 #define EUNBALBRACK 10001
49 #define EUNDEFVAR 10002
50 #define EUNBALPER 10000
54 #define debug_noise(fmt, args...) printf(fmt, ## args)
56 #define debug_noise(fmt, args...)
59 /* Forward declaration */
60 struct interface_defn_t;
62 typedef int (execfn)(char *command);
63 typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
65 extern llist_t *llist_add_to_end(llist_t *list_head, char *data)
67 llist_t *new_item, *tmp, *prev;
69 new_item = xmalloc(sizeof(llist_t));
70 new_item->data = data;
71 new_item->link = NULL;
80 prev->link = new_item;
95 struct address_family_t
99 struct method_t *method;
102 struct mapping_defn_t
104 struct mapping_defn_t *next;
123 struct interface_defn_t
125 struct interface_defn_t *prev;
126 struct interface_defn_t *next;
129 struct address_family_t *address_family;
130 struct method_t *method;
136 struct variable_t *option;
139 struct interfaces_file_t
141 llist_t *autointerfaces;
143 struct mapping_defn_t *mappings;
146 static char no_act = 0;
147 static char verbose = 0;
148 static char **environ = NULL;
150 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
152 static unsigned int count_bits(unsigned int a)
155 result = (a & 0x55) + ((a >> 1) & 0x55);
156 result = (result & 0x33) + ((result >> 2) & 0x33);
157 return((result & 0x0F) + ((result >> 4) & 0x0F));
160 static int count_netmask_bits(char *dotted_quad)
162 unsigned int result, a, b, c, d;
163 /* Found a netmask... Check if it is dotted quad */
164 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
166 result = count_bits(a);
167 result += count_bits(b);
168 result += count_bits(c);
169 result += count_bits(d);
170 return ((int)result);
174 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
176 if (*pos + str_length >= *len) {
179 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
181 *len = *len * 2 + str_length + 1;
184 while (str_length-- >= 1) {
185 (*buf)[(*pos)++] = *str;
191 static int strncmpz(char *l, char *r, size_t llen)
193 int i = strncmp(l, r, llen);
202 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
206 if (strncmpz(id, "iface", idlen) == 0) {
208 static char label_buf[20];
209 strncpy(label_buf, ifd->iface, 19);
211 result = strchr(label_buf, ':');
216 } else if (strncmpz(id, "label", idlen) == 0) {
219 for (i = 0; i < ifd->n_options; i++) {
220 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
221 return (ifd->option[i].value);
229 static char *parse(char *command, struct interface_defn_t *ifd)
233 size_t pos = 0, len = 0;
234 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
235 int okay[MAX_OPT_DEPTH] = { 1 };
242 addstr(&result, &len, &pos, command, 1);
247 addstr(&result, &len, &pos, command + 1, 1);
250 addstr(&result, &len, &pos, command, 1);
255 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
256 old_pos[opt_depth] = pos;
261 addstr(&result, &len, &pos, "[", 1);
266 if (command[1] == ']' && opt_depth > 1) {
268 if (!okay[opt_depth]) {
269 pos = old_pos[opt_depth];
274 addstr(&result, &len, &pos, "]", 1);
284 nextpercent = strchr(command, '%');
291 varvalue = get_var(command, nextpercent - command, ifd);
294 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
296 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
297 /* Sigh... Add a special case for 'ip' to convert from
298 * dotted quad to bit count style netmasks. */
299 if (strncmp(command, "bnmask", 6)==0) {
301 varvalue = get_var("netmask", 7, ifd);
302 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
304 sprintf(argument, "%d", res);
305 addstr(&result, &len, &pos, argument, bb_strlen(argument));
306 command = nextpercent + 1;
311 okay[opt_depth - 1] = 0;
314 command = nextpercent + 1;
335 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
340 out = parse(command, ifd);
350 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
351 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
353 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
356 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
358 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
361 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
363 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
366 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
368 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
371 static struct method_t methods_ipx[] = {
372 { "dynamic", dynamic_up, dynamic_down, },
373 { "static", static_up_ipx, static_down_ipx, },
376 struct address_family_t addr_ipx = {
378 sizeof(methods_ipx) / sizeof(struct method_t),
381 #endif /* IFUP_FEATURE_IPX */
383 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
384 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
386 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
388 result = execute("ip link set %iface% up", ifd, exec);
389 result +=execute("ip addr add ::1 dev %iface% label %label%", ifd, exec);
392 return( execute("ifconfig %iface% add ::1", ifd, exec));
396 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
398 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
399 return(execute("ip link set %iface% down", ifd, exec));
401 return(execute("ifconfig %iface% del ::1", ifd, exec));
405 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
408 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
409 result = execute("ip link set %iface% up", ifd, exec);
410 result += execute("ip addr add %address%/%netmask% dev %iface% label %label%", ifd, exec);
411 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
413 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
414 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
415 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
420 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
422 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
423 return(execute("ip link set %iface% down", ifd, exec));
425 return(execute("ifconfig %iface% down", ifd, exec));
429 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
430 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
433 result = execute("ip tunnel add %iface% mode sit remote "
434 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
435 result += execute("ip link set %iface% up", ifd, exec);
436 result += execute("ip addr add %address%/%netmask% dev %iface% label %label%", ifd, exec);
437 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
441 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
443 return( execute("ip tunnel del %iface%", ifd, exec));
447 static struct method_t methods6[] = {
448 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
449 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
451 { "static", static_up6, static_down6, },
452 { "loopback", loopback_up6, loopback_down6, },
455 struct address_family_t addr_inet6 = {
457 sizeof(methods6) / sizeof(struct method_t),
460 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
462 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
463 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
465 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
467 result += execute("ip link set %iface% up", ifd, exec);
468 result += execute("ip addr add 127.0.0.1/8 dev %iface% label %label%", ifd, exec);
471 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
475 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
477 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
479 result = execute("ip addr flush dev %iface%", ifd, exec);
480 result += execute("ip link set %iface% down", ifd, exec);
483 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
487 static int static_up(struct interface_defn_t *ifd, execfn *exec)
490 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
491 result = execute("ip link set %iface% up", ifd, exec);
492 result += execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
493 "dev %iface% label %label%", ifd, exec);
494 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
496 result = execute("ifconfig %iface% %address% netmask %netmask% "
497 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
498 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
500 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
505 static int static_down(struct interface_defn_t *ifd, execfn *exec)
508 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
509 result = execute("ip addr flush dev %iface%", ifd, exec);
510 result += execute("ip link set %iface% down", ifd, exec);
512 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
513 result += execute("ifconfig %iface% down", ifd, exec);
518 static int execable(char *program)
521 if (0 == stat(program, &buf)) {
522 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
529 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
531 if (execable("/sbin/udhcpc")) {
532 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
533 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
534 } else if (execable("/sbin/pump")) {
535 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
536 } else if (execable("/sbin/dhclient")) {
537 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
538 } else if (execable("/sbin/dhcpcd")) {
539 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
540 "[[-l %leasetime%]] %iface%", ifd, exec));
545 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
548 if (execable("/sbin/udhcpc")) {
549 execute("kill -9 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
551 } else if (execable("/sbin/pump")) {
552 result = execute("pump -i %iface% -k", ifd, exec);
553 } else if (execable("/sbin/dhclient")) {
554 execute("kill -9 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
556 } else if (execable("/sbin/dhcpcd")) {
557 result = execute("dhcpcd -k %iface%", ifd, exec);
559 return (result || execute("ifconfig %iface% down", ifd, exec));
562 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
564 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
565 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
566 "--returniffail --serverbcast", ifd, exec));
569 static int bootp_down(struct interface_defn_t *ifd, execfn *exec)
571 return( execute("ifconfig down %iface%", ifd, exec));
574 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
576 return( execute("pon [[%provider%]]", ifd, exec));
579 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
581 return( execute("poff [[%provider%]]", ifd, exec));
584 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
586 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
587 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
590 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
592 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
593 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
596 static struct method_t methods[] =
598 { "wvdial", wvdial_up, wvdial_down, },
599 { "ppp", ppp_up, ppp_down, },
600 { "static", static_up, static_down, },
601 { "bootp", bootp_up, bootp_down, },
602 { "dhcp", dhcp_up, dhcp_down, },
603 { "loopback", loopback_up, loopback_down, },
606 struct address_family_t addr_inet =
609 sizeof(methods) / sizeof(struct method_t),
613 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
615 static char *next_word(char **buf)
617 unsigned short length;
620 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
624 /* Skip over leading whitespace */
625 word = *buf + strspn(*buf, " \t\n");
627 /* Find the length of this word */
628 length = strcspn(word, " \t\n");
632 *buf = word + length;
633 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
642 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
646 for (i = 0; af[i]; i++) {
647 if (strcmp(af[i]->name, name) == 0) {
654 static struct method_t *get_method(struct address_family_t *af, char *name)
658 for (i = 0; i < af->n_methods; i++) {
659 if (strcmp(af->method[i].name, name) == 0) {
660 return &af->method[i];
666 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
668 if (strcmp(ifa->iface, ifb->iface) != 0) {
671 if (ifa->address_family != ifb->address_family) {
677 static const llist_t *find_list_string(const llist_t *list, const char *string)
680 if (strcmp(list->data, string) == 0) {
688 static struct interfaces_file_t *read_interfaces(char *filename)
690 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
691 struct mapping_defn_t *currmap = NULL;
693 struct interface_defn_t *currif = NULL;
694 struct interfaces_file_t *defn;
699 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
701 defn = xmalloc(sizeof(struct interfaces_file_t));
702 defn->autointerfaces = NULL;
703 defn->mappings = NULL;
706 f = bb_xfopen(filename, "r");
708 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
711 /* Ignore comments */
716 firstword = next_word(&buf_ptr);
717 if (firstword == NULL) {
718 continue; /* blank line */
721 if (strcmp(firstword, "mapping") == 0) {
722 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
723 currmap = xmalloc(sizeof(struct mapping_defn_t));
724 currmap->max_matches = 0;
725 currmap->n_matches = 0;
726 currmap->match = NULL;
728 while ((firstword = next_word(&buf_ptr)) != NULL) {
729 if (currmap->max_matches == currmap->n_matches) {
730 currmap->max_matches = currmap->max_matches * 2 + 1;
731 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
734 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
736 currmap->max_mappings = 0;
737 currmap->n_mappings = 0;
738 currmap->mapping = NULL;
739 currmap->script = NULL;
741 struct mapping_defn_t **where = &defn->mappings;
742 while (*where != NULL) {
743 where = &(*where)->next;
746 currmap->next = NULL;
748 debug_noise("Added mapping\n");
750 currently_processing = MAPPING;
751 } else if (strcmp(firstword, "iface") == 0) {
754 char *address_family_name;
756 struct address_family_t *addr_fams[] = {
757 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
760 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
763 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
769 currif = xmalloc(sizeof(struct interface_defn_t));
770 iface_name = next_word(&buf_ptr);
771 address_family_name = next_word(&buf_ptr);
772 method_name = next_word(&buf_ptr);
774 if (buf_ptr == NULL) {
775 bb_error_msg("too few parameters for line \"%s\"", buf);
779 if (buf_ptr[0] != '\0') {
780 bb_error_msg("too many parameters \"%s\"", buf);
784 currif->iface = bb_xstrdup(iface_name);
786 currif->address_family = get_address_family(addr_fams, address_family_name);
787 if (!currif->address_family) {
788 bb_error_msg("unknown address type \"%s\"", buf);
792 currif->method = get_method(currif->address_family, method_name);
793 if (!currif->method) {
794 bb_error_msg("unknown method \"%s\"", buf);
798 currif->automatic = 1;
799 currif->max_options = 0;
800 currif->n_options = 0;
801 currif->option = NULL;
804 struct interface_defn_t *tmp;
806 iface_list = defn->ifaces;
808 tmp = (struct interface_defn_t *) iface_list->data;
809 if (duplicate_if(tmp, currif)) {
810 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
813 iface_list = iface_list->link;
816 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
818 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
820 currently_processing = IFACE;
821 } else if (strcmp(firstword, "auto") == 0) {
822 while ((firstword = next_word(&buf_ptr)) != NULL) {
824 /* Check the interface isnt already listed */
825 if (find_list_string(defn->autointerfaces, firstword)) {
826 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
829 /* Add the interface to the list */
830 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
831 debug_noise("\nauto %s\n", firstword);
833 currently_processing = NONE;
835 switch (currently_processing) {
840 if (bb_strlen(buf_ptr) == 0) {
841 bb_error_msg("option with empty value \"%s\"", buf);
845 if (strcmp(firstword, "up") != 0
846 && strcmp(firstword, "down") != 0
847 && strcmp(firstword, "pre-up") != 0
848 && strcmp(firstword, "post-down") != 0) {
849 for (i = 0; i < currif->n_options; i++) {
850 if (strcmp(currif->option[i].name, firstword) == 0) {
851 bb_error_msg("duplicate option \"%s\"", buf);
857 if (currif->n_options >= currif->max_options) {
858 struct variable_t *opt;
860 currif->max_options = currif->max_options + 10;
861 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
862 currif->option = opt;
864 currif->option[currif->n_options].name = bb_xstrdup(firstword);
865 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
866 if (!currif->option[currif->n_options].name) {
870 if (!currif->option[currif->n_options].value) {
874 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
875 currif->option[currif->n_options].value);
879 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
880 if (strcmp(firstword, "script") == 0) {
881 if (currmap->script != NULL) {
882 bb_error_msg("duplicate script in mapping \"%s\"", buf);
885 currmap->script = bb_xstrdup(next_word(&buf_ptr));
887 } else if (strcmp(firstword, "map") == 0) {
888 if (currmap->max_mappings == currmap->n_mappings) {
889 currmap->max_mappings = currmap->max_mappings * 2 + 1;
890 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
892 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
893 currmap->n_mappings++;
895 bb_error_msg("misplaced option \"%s\"", buf);
902 bb_error_msg("misplaced option \"%s\"", buf);
908 if (ferror(f) != 0) {
909 bb_perror_msg_and_die("%s", filename);
916 static char *setlocalenv(char *format, char *name, char *value)
922 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
924 sprintf(result, format, name, value);
926 for (here = there = result; *there != '=' && *there; there++) {
930 *there = toupper(*there);
932 if (isalnum(*there) || *there == '_') {
937 memmove(here, there, bb_strlen(there) + 1);
942 static void set_environ(struct interface_defn_t *iface, char *mode)
946 const int n_env_entries = iface->n_options + 5;
949 if (environ != NULL) {
950 for (ppch = environ; *ppch; ppch++) {
957 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
958 environend = environ;
961 for (i = 0; i < iface->n_options; i++) {
962 if (strcmp(iface->option[i].name, "up") == 0
963 || strcmp(iface->option[i].name, "down") == 0
964 || strcmp(iface->option[i].name, "pre-up") == 0
965 || strcmp(iface->option[i].name, "post-down") == 0) {
968 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
972 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
974 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
976 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
978 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
980 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
984 static int doit(char *str)
986 if (verbose || no_act) {
994 switch (child = fork()) {
995 case -1: /* failure */
998 execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
1001 waitpid(child, &status, 0);
1002 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1009 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
1014 for (i = 0; i < ifd->n_options; i++) {
1015 if (strcmp(ifd->option[i].name, opt) == 0) {
1016 if (!(*exec) (ifd->option[i].value)) {
1022 buf = xmalloc(bb_strlen(opt) + 19);
1023 sprintf(buf, "/etc/network/if-%s.d", opt);
1029 static int check(char *str) {
1033 static int iface_up(struct interface_defn_t *iface)
1036 if (!iface->method->up(iface,check)) return -1;
1037 set_environ(iface, "start");
1038 result = execute_all(iface, doit, "pre-up");
1039 result += iface->method->up(iface, doit);
1040 result += execute_all(iface, doit, "up");
1044 static int iface_down(struct interface_defn_t *iface)
1047 if (!iface->method->down(iface,check)) return -1;
1048 set_environ(iface, "stop");
1049 result = execute_all(iface, doit, "down");
1050 result += iface->method->down(iface, doit);
1051 result += execute_all(iface, doit, "post-down");
1055 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1056 static int popen2(FILE **in, FILE **out, char *command, ...)
1059 char *argv[11] = { command };
1061 int infd[2], outfd[2];
1065 va_start(ap, command);
1066 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1069 argv[argc] = NULL; /* make sure */
1072 if (pipe(infd) != 0) {
1076 if (pipe(outfd) != 0) {
1083 switch (pid = fork()) {
1084 case -1: /* failure */
1097 execvp(command, argv);
1099 default: /* parent */
1100 *in = fdopen(infd[1], "w");
1101 *out = fdopen(outfd[0], "r");
1109 static int run_mapping(char *physical, char *logical, int len, struct mapping_defn_t * map)
1116 pid = popen2(&in, &out, map->script, physical, NULL);
1120 for (i = 0; i < map->n_mappings; i++) {
1121 fprintf(in, "%s\n", map->mapping[i]);
1124 waitpid(pid, &status, 0);
1125 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1126 if (fgets(logical, len, out)) {
1127 char *pch = logical + bb_strlen(logical) - 1;
1129 while (pch >= logical && isspace(*pch))
1137 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
1139 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1141 unsigned short iface_len = bb_strlen(iface);
1142 llist_t *search = state_list;
1145 if ((strncmp(search->data, iface, iface_len) == 0) &&
1146 (search->data[iface_len] == '=')) {
1149 search = search->link;
1154 extern int ifupdown_main(int argc, char **argv)
1156 int (*cmds) (struct interface_defn_t *) = NULL;
1157 struct interfaces_file_t *defn;
1158 FILE *state_fp = NULL;
1159 llist_t *state_list = NULL;
1160 llist_t *target_list = NULL;
1161 char *interfaces = "/etc/network/interfaces";
1162 const char *statefile = "/var/run/ifstate";
1164 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1165 int run_mappings = 1;
1171 if (bb_applet_name[2] == 'u') {
1175 /* ifdown command */
1179 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1180 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1182 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1186 case 'i': /* interfaces */
1187 interfaces = bb_xstrdup(optarg);
1189 case 'v': /* verbose */
1195 case 'n': /* no-act */
1198 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1199 case 'm': /* no-mappings */
1203 case 'f': /* force */
1212 if (argc - optind > 0) {
1222 debug_noise("reading %s file:\n", interfaces);
1223 defn = read_interfaces(interfaces);
1224 debug_noise("\ndone reading %s\n\n", interfaces);
1227 state_fp = fopen(statefile, "r");
1230 /* Create a list of interfaces to work on */
1232 if (cmds == iface_up) {
1233 target_list = defn->autointerfaces;
1238 const llist_t *list = state_list;
1240 new_item = xmalloc(sizeof(llist_t));
1241 new_item->data = strdup(list->data);
1242 new_item->link = NULL;
1245 target_list = new_item;
1247 while (list->link) {
1254 target_list = defn->autointerfaces;
1258 const llist_t *list = state_list;
1260 target_list = llist_add_to_end(target_list, strdup(list->data));
1263 target_list = defn->autointerfaces;
1267 target_list = llist_add_to_end(target_list, argv[optind]);
1271 /* Update the interfaces */
1272 while (target_list) {
1273 llist_t *iface_list;
1274 struct interface_defn_t *currif;
1280 iface = strdup(target_list->data);
1281 target_list = target_list->link;
1283 pch = strchr(iface, '=');
1286 liface = strdup(pch + 1);
1288 liface = strdup(iface);
1292 const llist_t *iface_state = find_iface_state(state_list, iface);
1294 if (cmds == iface_up) {
1297 bb_error_msg("interface %s already configured", iface);
1303 bb_error_msg("interface %s not configured", iface);
1309 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1310 if ((cmds == iface_up) && run_mappings) {
1311 struct mapping_defn_t *currmap;
1313 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1315 for (i = 0; i < currmap->n_matches; i++) {
1316 if (fnmatch(currmap->match[i], liface, 0) != 0)
1319 printf("Running mapping script %s on %s\n", currmap->script, liface);
1321 run_mapping(iface, liface, sizeof(liface), currmap);
1329 iface_list = defn->ifaces;
1330 while (iface_list) {
1331 currif = (struct interface_defn_t *) iface_list->data;
1332 if (strcmp(liface, currif->iface) == 0) {
1333 char *oldiface = currif->iface;
1336 currif->iface = iface;
1338 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1340 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1341 if (cmds(currif) == -1) {
1342 bb_error_msg("Don't seem to be have all the variables for %s/%s.",
1343 liface, currif->address_family->name);
1346 currif->iface = oldiface;
1348 iface_list = iface_list->link;
1354 if (!okay && !force) {
1355 bb_error_msg("Ignoring unknown interface %s", liface);
1357 llist_t *iface_state = find_iface_state(state_list, iface);
1359 if (cmds == iface_up) {
1360 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1361 sprintf(newiface, "%s=%s", iface, liface);
1362 if (iface_state == NULL) {
1363 state_list = llist_add_to_end(state_list, newiface);
1365 free(iface_state->data);
1366 iface_state->data = newiface;
1368 } else if (cmds == iface_down) {
1369 /* Remove an interface from the linked list */
1371 /* This needs to be done better */
1372 free(iface_state->data);
1373 free(iface_state->link);
1374 if (iface_state->link) {
1375 iface_state->data = iface_state->link->data;
1376 iface_state->link = iface_state->link->link;
1378 iface_state->data = NULL;
1379 iface_state->link = NULL;
1386 /* Actually write the new state */
1391 state_fp = bb_xfopen(statefile, "a+");
1393 if (ftruncate(fileno(state_fp), 0) < 0) {
1394 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1399 while (state_list) {
1400 if (state_list->data) {
1401 fputs(state_list->data, state_fp);
1402 fputc('\n', state_fp);
1404 state_list = state_list->link;
1410 if (state_fp != NULL) {