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 * 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.
30 /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
33 #include <sys/utsname.h>
49 #define MAX_OPT_DEPTH 10
50 #define EUNBALBRACK 10001
51 #define EUNDEFVAR 10002
52 #define EUNBALPER 10000
54 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
55 #define MAX_INTERFACE_LENGTH 10
59 #define debug_noise(fmt, args...) printf(fmt, ## args)
61 #define debug_noise(fmt, args...)
64 /* Forward declaration */
65 struct interface_defn_t;
67 typedef int (execfn)(char *command);
68 typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
70 extern llist_t *llist_add_to_end(llist_t *list_head, char *data)
72 llist_t *new_item, *tmp, *prev;
74 new_item = xmalloc(sizeof(llist_t));
75 new_item->data = data;
76 new_item->link = NULL;
85 prev->link = new_item;
100 struct address_family_t
104 struct method_t *method;
107 struct mapping_defn_t
109 struct mapping_defn_t *next;
128 struct interface_defn_t
130 struct interface_defn_t *prev;
131 struct interface_defn_t *next;
134 struct address_family_t *address_family;
135 struct method_t *method;
141 struct variable_t *option;
144 struct interfaces_file_t
146 llist_t *autointerfaces;
148 struct mapping_defn_t *mappings;
151 static char no_act = 0;
152 static char verbose = 0;
153 static char **__myenviron = NULL;
155 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
157 static unsigned int count_bits(unsigned int a)
160 result = (a & 0x55) + ((a >> 1) & 0x55);
161 result = (result & 0x33) + ((result >> 2) & 0x33);
162 return((result & 0x0F) + ((result >> 4) & 0x0F));
165 static int count_netmask_bits(char *dotted_quad)
167 unsigned int result, a, b, c, d;
168 /* Found a netmask... Check if it is dotted quad */
169 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
171 result = count_bits(a);
172 result += count_bits(b);
173 result += count_bits(c);
174 result += count_bits(d);
175 return ((int)result);
179 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
181 if (*pos + str_length >= *len) {
184 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
186 *len = *len * 2 + str_length + 1;
189 while (str_length-- >= 1) {
190 (*buf)[(*pos)++] = *str;
196 static int strncmpz(char *l, char *r, size_t llen)
198 int i = strncmp(l, r, llen);
207 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
211 if (strncmpz(id, "iface", idlen) == 0) {
213 static char label_buf[20];
214 strncpy(label_buf, ifd->iface, 19);
216 result = strchr(label_buf, ':');
221 } else if (strncmpz(id, "label", idlen) == 0) {
224 for (i = 0; i < ifd->n_options; i++) {
225 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
226 return (ifd->option[i].value);
234 static char *parse(char *command, struct interface_defn_t *ifd)
238 size_t pos = 0, len = 0;
239 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
240 int okay[MAX_OPT_DEPTH] = { 1 };
247 addstr(&result, &len, &pos, command, 1);
252 addstr(&result, &len, &pos, command + 1, 1);
255 addstr(&result, &len, &pos, command, 1);
260 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
261 old_pos[opt_depth] = pos;
266 addstr(&result, &len, &pos, "[", 1);
271 if (command[1] == ']' && opt_depth > 1) {
273 if (!okay[opt_depth]) {
274 pos = old_pos[opt_depth];
279 addstr(&result, &len, &pos, "]", 1);
289 nextpercent = strchr(command, '%');
296 varvalue = get_var(command, nextpercent - command, ifd);
299 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
301 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
302 /* Sigh... Add a special case for 'ip' to convert from
303 * dotted quad to bit count style netmasks. */
304 if (strncmp(command, "bnmask", 6)==0) {
306 varvalue = get_var("netmask", 7, ifd);
307 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
309 sprintf(argument, "%d", res);
310 addstr(&result, &len, &pos, argument, bb_strlen(argument));
311 command = nextpercent + 1;
316 okay[opt_depth - 1] = 0;
319 command = nextpercent + 1;
340 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
345 out = parse(command, ifd);
358 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
359 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
361 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
364 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
366 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
369 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
371 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
374 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
376 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
379 static struct method_t methods_ipx[] = {
380 { "dynamic", dynamic_up, dynamic_down, },
381 { "static", static_up_ipx, static_down_ipx, },
384 struct address_family_t addr_ipx = {
386 sizeof(methods_ipx) / sizeof(struct method_t),
389 #endif /* IFUP_FEATURE_IPX */
391 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
392 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
394 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
396 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
397 result += execute("ip link set %iface% up", ifd, exec);
398 return ((result == 2) ? 2 : 0);
400 return( execute("ifconfig %iface% add ::1", ifd, exec));
404 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
406 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
407 return(execute("ip link set %iface% down", ifd, exec));
409 return(execute("ifconfig %iface% del ::1", ifd, exec));
413 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
416 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
417 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
418 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
419 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
421 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
422 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
423 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
425 return ((result == 3) ? 3 : 0);
428 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
430 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
431 return(execute("ip link set %iface% down", ifd, exec));
433 return(execute("ifconfig %iface% down", ifd, exec));
437 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
438 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
441 result = execute("ip tunnel add %iface% mode sit remote "
442 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
443 result += execute("ip link set %iface% up", ifd, exec);
444 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
445 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
446 return ((result == 4) ? 4 : 0);
449 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
451 return( execute("ip tunnel del %iface%", ifd, exec));
455 static struct method_t methods6[] = {
456 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
457 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
459 { "static", static_up6, static_down6, },
460 { "loopback", loopback_up6, loopback_down6, },
463 static struct address_family_t addr_inet6 = {
465 sizeof(methods6) / sizeof(struct method_t),
468 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
470 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
471 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
473 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
475 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
476 result += execute("ip link set %iface% up", ifd, exec);
477 return ((result == 2) ? 2 : 0);
479 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
483 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
485 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
487 result = execute("ip addr flush dev %iface%", ifd, exec);
488 result += execute("ip link set %iface% down", ifd, exec);
489 return ((result == 2) ? 2 : 0);
491 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
495 static int static_up(struct interface_defn_t *ifd, execfn *exec)
498 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
499 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
500 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
501 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
502 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
503 return ((result == 3) ? 3 : 0);
505 result = execute("ifconfig %iface% %address% netmask %netmask% "
506 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
507 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
509 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
510 return ((result == 2) ? 2 : 0);
514 static int static_down(struct interface_defn_t *ifd, execfn *exec)
517 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
518 result = execute("ip addr flush dev %iface%", ifd, exec);
519 result += execute("ip link set %iface% down", ifd, exec);
521 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
522 result += execute("ifconfig %iface% down", ifd, exec);
524 return ((result == 2) ? 2 : 0);
527 static int execable(char *program)
530 if (0 == stat(program, &buf)) {
531 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
538 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
540 if (execable("/sbin/udhcpc")) {
541 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
542 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
543 } else if (execable("/sbin/pump")) {
544 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
545 } else if (execable("/sbin/dhclient")) {
546 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
547 } else if (execable("/sbin/dhcpcd")) {
548 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
549 "[[-l %leasetime%]] %iface%", ifd, exec));
554 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
557 if (execable("/sbin/udhcpc")) {
558 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
559 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
560 * sequentially so we don't need to sleep */
561 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
562 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
563 } else if (execable("/sbin/pump")) {
564 result = execute("pump -i %iface% -k", ifd, exec);
565 } else if (execable("/sbin/dhclient")) {
566 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
567 } else if (execable("/sbin/dhcpcd")) {
568 result = execute("dhcpcd -k %iface%", ifd, exec);
570 return (result || static_down(ifd, exec));
573 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
575 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
576 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
577 "--returniffail --serverbcast", ifd, exec));
580 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
582 return( execute("pon [[%provider%]]", ifd, exec));
585 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
587 return( execute("poff [[%provider%]]", ifd, exec));
590 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
592 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
593 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
596 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
598 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
599 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
602 static struct method_t methods[] =
604 { "wvdial", wvdial_up, wvdial_down, },
605 { "ppp", ppp_up, ppp_down, },
606 { "static", static_up, static_down, },
607 { "bootp", bootp_up, static_down, },
608 { "dhcp", dhcp_up, dhcp_down, },
609 { "loopback", loopback_up, loopback_down, },
612 static struct address_family_t addr_inet =
615 sizeof(methods) / sizeof(struct method_t),
619 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
621 static char *next_word(char **buf)
623 unsigned short length;
626 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
630 /* Skip over leading whitespace */
632 while (isspace(*word)) {
636 /* Skip over comments */
641 /* Find the length of this word */
642 length = strcspn(word, " \t\n");
646 *buf = word + length;
647 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
656 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
660 for (i = 0; af[i]; i++) {
661 if (strcmp(af[i]->name, name) == 0) {
668 static struct method_t *get_method(struct address_family_t *af, char *name)
672 for (i = 0; i < af->n_methods; i++) {
673 if (strcmp(af->method[i].name, name) == 0) {
674 return &af->method[i];
680 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
682 if (strcmp(ifa->iface, ifb->iface) != 0) {
685 if (ifa->address_family != ifb->address_family) {
691 static const llist_t *find_list_string(const llist_t *list, const char *string)
694 if (strcmp(list->data, string) == 0) {
702 static struct interfaces_file_t *read_interfaces(const char *filename)
704 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
705 struct mapping_defn_t *currmap = NULL;
707 struct interface_defn_t *currif = NULL;
708 struct interfaces_file_t *defn;
713 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
715 defn = xmalloc(sizeof(struct interfaces_file_t));
716 defn->autointerfaces = NULL;
717 defn->mappings = NULL;
720 f = bb_xfopen(filename, "r");
722 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
725 firstword = next_word(&buf_ptr);
726 if (firstword == NULL) {
728 continue; /* blank line */
731 if (strcmp(firstword, "mapping") == 0) {
732 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
733 currmap = xmalloc(sizeof(struct mapping_defn_t));
734 currmap->max_matches = 0;
735 currmap->n_matches = 0;
736 currmap->match = NULL;
738 while ((firstword = next_word(&buf_ptr)) != NULL) {
739 if (currmap->max_matches == currmap->n_matches) {
740 currmap->max_matches = currmap->max_matches * 2 + 1;
741 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
744 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
746 currmap->max_mappings = 0;
747 currmap->n_mappings = 0;
748 currmap->mapping = NULL;
749 currmap->script = NULL;
751 struct mapping_defn_t **where = &defn->mappings;
752 while (*where != NULL) {
753 where = &(*where)->next;
756 currmap->next = NULL;
758 debug_noise("Added mapping\n");
760 currently_processing = MAPPING;
761 } else if (strcmp(firstword, "iface") == 0) {
764 char *address_family_name;
766 struct address_family_t *addr_fams[] = {
767 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
770 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
773 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
779 currif = xmalloc(sizeof(struct interface_defn_t));
780 iface_name = next_word(&buf_ptr);
781 address_family_name = next_word(&buf_ptr);
782 method_name = next_word(&buf_ptr);
784 if (buf_ptr == NULL) {
785 bb_error_msg("too few parameters for line \"%s\"", buf);
789 /* ship any trailing whitespace */
790 while (isspace(*buf_ptr)) {
794 if (buf_ptr[0] != '\0') {
795 bb_error_msg("too many parameters \"%s\"", buf);
799 currif->iface = bb_xstrdup(iface_name);
801 currif->address_family = get_address_family(addr_fams, address_family_name);
802 if (!currif->address_family) {
803 bb_error_msg("unknown address type \"%s\"", address_family_name);
807 currif->method = get_method(currif->address_family, method_name);
808 if (!currif->method) {
809 bb_error_msg("unknown method \"%s\"", method_name);
813 currif->automatic = 1;
814 currif->max_options = 0;
815 currif->n_options = 0;
816 currif->option = NULL;
819 struct interface_defn_t *tmp;
821 iface_list = defn->ifaces;
823 tmp = (struct interface_defn_t *) iface_list->data;
824 if (duplicate_if(tmp, currif)) {
825 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
828 iface_list = iface_list->link;
831 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
833 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
835 currently_processing = IFACE;
836 } else if (strcmp(firstword, "auto") == 0) {
837 while ((firstword = next_word(&buf_ptr)) != NULL) {
839 /* Check the interface isnt already listed */
840 if (find_list_string(defn->autointerfaces, firstword)) {
841 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
844 /* Add the interface to the list */
845 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
846 debug_noise("\nauto %s\n", firstword);
848 currently_processing = NONE;
850 switch (currently_processing) {
855 if (bb_strlen(buf_ptr) == 0) {
856 bb_error_msg("option with empty value \"%s\"", buf);
860 if (strcmp(firstword, "up") != 0
861 && strcmp(firstword, "down") != 0
862 && strcmp(firstword, "pre-up") != 0
863 && strcmp(firstword, "post-down") != 0) {
864 for (i = 0; i < currif->n_options; i++) {
865 if (strcmp(currif->option[i].name, firstword) == 0) {
866 bb_error_msg("duplicate option \"%s\"", buf);
872 if (currif->n_options >= currif->max_options) {
873 struct variable_t *opt;
875 currif->max_options = currif->max_options + 10;
876 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
877 currif->option = opt;
879 currif->option[currif->n_options].name = bb_xstrdup(firstword);
880 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
881 if (!currif->option[currif->n_options].name) {
885 if (!currif->option[currif->n_options].value) {
889 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
890 currif->option[currif->n_options].value);
894 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
895 if (strcmp(firstword, "script") == 0) {
896 if (currmap->script != NULL) {
897 bb_error_msg("duplicate script in mapping \"%s\"", buf);
900 currmap->script = bb_xstrdup(next_word(&buf_ptr));
902 } else if (strcmp(firstword, "map") == 0) {
903 if (currmap->max_mappings == currmap->n_mappings) {
904 currmap->max_mappings = currmap->max_mappings * 2 + 1;
905 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
907 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
908 currmap->n_mappings++;
910 bb_error_msg("misplaced option \"%s\"", buf);
917 bb_error_msg("misplaced option \"%s\"", buf);
923 if (ferror(f) != 0) {
924 bb_perror_msg_and_die("%s", filename);
931 static char *setlocalenv(char *format, char *name, char *value)
937 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
939 sprintf(result, format, name, value);
941 for (here = there = result; *there != '=' && *there; there++) {
945 *there = toupper(*there);
947 if (isalnum(*there) || *there == '_') {
952 memmove(here, there, bb_strlen(there) + 1);
957 static void set_environ(struct interface_defn_t *iface, char *mode)
961 const int n_env_entries = iface->n_options + 5;
964 if (__myenviron != NULL) {
965 for (ppch = __myenviron; *ppch; ppch++) {
972 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
973 environend = __myenviron;
976 for (i = 0; i < iface->n_options; i++) {
977 if (strcmp(iface->option[i].name, "up") == 0
978 || strcmp(iface->option[i].name, "down") == 0
979 || strcmp(iface->option[i].name, "pre-up") == 0
980 || strcmp(iface->option[i].name, "post-down") == 0) {
983 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
987 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
989 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
991 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
993 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
995 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
999 static int doit(char *str)
1001 if (verbose || no_act) {
1002 printf("%s\n", str);
1009 switch (child = fork()) {
1010 case -1: /* failure */
1013 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
1016 waitpid(child, &status, 0);
1017 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1024 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
1028 for (i = 0; i < ifd->n_options; i++) {
1029 if (strcmp(ifd->option[i].name, opt) == 0) {
1030 if (!(*exec) (ifd->option[i].value)) {
1036 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
1037 if ((*exec)(buf) != 1) {
1043 static int check(char *str) {
1047 static int iface_up(struct interface_defn_t *iface)
1049 if (!iface->method->up(iface,check)) return -1;
1050 set_environ(iface, "start");
1051 if (!execute_all(iface, doit, "pre-up")) return 0;
1052 if (!iface->method->up(iface, doit)) return 0;
1053 if (!execute_all(iface, doit, "up")) return 0;
1057 static int iface_down(struct interface_defn_t *iface)
1059 if (!iface->method->down(iface,check)) return -1;
1060 set_environ(iface, "stop");
1061 if (!execute_all(iface, doit, "down")) return 0;
1062 if (!iface->method->down(iface, doit)) return 0;
1063 if (!execute_all(iface, doit, "post-down")) return 0;
1067 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1068 static int popen2(FILE **in, FILE **out, char *command, ...)
1071 char *argv[11] = { command };
1073 int infd[2], outfd[2];
1077 va_start(ap, command);
1078 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1081 argv[argc] = NULL; /* make sure */
1084 if (pipe(infd) != 0) {
1088 if (pipe(outfd) != 0) {
1095 switch (pid = fork()) {
1096 case -1: /* failure */
1109 execvp(command, argv);
1111 default: /* parent */
1112 *in = fdopen(infd[1], "w");
1113 *out = fdopen(outfd[0], "r");
1121 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1127 char *logical = bb_xstrdup(physical);
1129 /* Run the mapping script. */
1130 pid = popen2(&in, &out, map->script, physical, NULL);
1132 /* popen2() returns 0 on failure. */
1136 /* Write mappings to stdin of mapping script. */
1137 for (i = 0; i < map->n_mappings; i++) {
1138 fprintf(in, "%s\n", map->mapping[i]);
1141 waitpid(pid, &status, 0);
1143 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1144 /* If the mapping script exited successfully, try to
1145 * grab a line of output and use that as the name of the
1146 * logical interface. */
1147 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1149 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1150 /* If we are able to read a line of output from the script,
1151 * remove any trailing whitespace and use this value
1152 * as the name of the logical interface. */
1153 char *pch = new_logical + bb_strlen(new_logical) - 1;
1155 while (pch >= new_logical && isspace(*pch))
1159 logical = new_logical;
1161 /* If we are UNABLE to read a line of output, discard are
1162 * freshly allocated memory. */
1171 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1173 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1175 unsigned short iface_len = bb_strlen(iface);
1176 llist_t *search = state_list;
1179 if ((strncmp(search->data, iface, iface_len) == 0) &&
1180 (search->data[iface_len] == '=')) {
1183 search = search->link;
1188 extern int ifupdown_main(int argc, char **argv)
1190 int (*cmds) (struct interface_defn_t *) = NULL;
1191 struct interfaces_file_t *defn;
1192 FILE *state_fp = NULL;
1193 llist_t *state_list = NULL;
1194 llist_t *target_list = NULL;
1195 const char *interfaces = "/etc/network/interfaces";
1196 const char *statefile = "/var/run/ifstate";
1198 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1199 int run_mappings = 1;
1203 int any_failures = 0;
1206 if (bb_applet_name[2] == 'u') {
1210 /* ifdown command */
1214 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1215 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1217 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1221 case 'i': /* interfaces */
1222 interfaces = optarg;
1224 case 'v': /* verbose */
1230 case 'n': /* no-act */
1233 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1234 case 'm': /* no-mappings */
1238 case 'f': /* force */
1247 if (argc - optind > 0) {
1257 debug_noise("reading %s file:\n", interfaces);
1258 defn = read_interfaces(interfaces);
1259 debug_noise("\ndone reading %s\n\n", interfaces);
1266 state_fp = fopen(statefile, "r");
1269 /* Create a list of interfaces to work on */
1271 if (cmds == iface_up) {
1272 target_list = defn->autointerfaces;
1277 const llist_t *list = state_list;
1279 new_item = xmalloc(sizeof(llist_t));
1280 new_item->data = strdup(list->data);
1281 new_item->link = NULL;
1284 target_list = new_item;
1286 while (list->link) {
1293 target_list = defn->autointerfaces;
1297 const llist_t *list = state_list;
1299 target_list = llist_add_to_end(target_list, strdup(list->data));
1302 target_list = defn->autointerfaces;
1306 target_list = llist_add_to_end(target_list, argv[optind]);
1310 /* Update the interfaces */
1311 while (target_list) {
1312 llist_t *iface_list;
1313 struct interface_defn_t *currif;
1320 iface = strdup(target_list->data);
1321 target_list = target_list->link;
1323 pch = strchr(iface, '=');
1326 liface = strdup(pch + 1);
1328 liface = strdup(iface);
1332 const llist_t *iface_state = find_iface_state(state_list, iface);
1334 if (cmds == iface_up) {
1337 bb_error_msg("interface %s already configured", iface);
1343 bb_error_msg("interface %s not configured", iface);
1349 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1350 if ((cmds == iface_up) && run_mappings) {
1351 struct mapping_defn_t *currmap;
1353 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1355 for (i = 0; i < currmap->n_matches; i++) {
1356 if (fnmatch(currmap->match[i], liface, 0) != 0)
1359 printf("Running mapping script %s on %s\n", currmap->script, liface);
1361 liface = run_mapping(iface, currmap);
1369 iface_list = defn->ifaces;
1370 while (iface_list) {
1371 currif = (struct interface_defn_t *) iface_list->data;
1372 if (strcmp(liface, currif->iface) == 0) {
1373 char *oldiface = currif->iface;
1376 currif->iface = iface;
1378 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1380 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1381 cmds_ret = cmds(currif);
1382 if (cmds_ret == -1) {
1383 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1384 liface, currif->address_family->name);
1386 } else if (cmds_ret == 0) {
1390 currif->iface = oldiface;
1392 iface_list = iface_list->link;
1398 if (!okay && !force) {
1399 bb_error_msg("Ignoring unknown interface %s", liface);
1402 llist_t *iface_state = find_iface_state(state_list, iface);
1404 if (cmds == iface_up) {
1405 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1406 sprintf(newiface, "%s=%s", iface, liface);
1407 if (iface_state == NULL) {
1408 state_list = llist_add_to_end(state_list, newiface);
1410 free(iface_state->data);
1411 iface_state->data = newiface;
1413 } else if (cmds == iface_down) {
1414 /* Remove an interface from the linked list */
1416 /* This needs to be done better */
1417 free(iface_state->data);
1418 free(iface_state->link);
1419 if (iface_state->link) {
1420 iface_state->data = iface_state->link->data;
1421 iface_state->link = iface_state->link->link;
1423 iface_state->data = NULL;
1424 iface_state->link = NULL;
1431 /* Actually write the new state */
1436 state_fp = bb_xfopen(statefile, "a+");
1438 if (ftruncate(fileno(state_fp), 0) < 0) {
1439 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1444 while (state_list) {
1445 if (state_list->data) {
1446 fputs(state_list->data, state_fp);
1447 fputc('\n', state_fp);
1449 state_list = state_list->link;
1455 if (state_fp != NULL) {