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 **environ = 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 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 bootp_down(struct interface_defn_t *ifd, execfn *exec)
556 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
557 return(execute("ip link set %iface% down", ifd, exec));
559 return(execute("ifconfig %iface% down", ifd, exec));
563 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
566 if (execable("/sbin/udhcpc")) {
567 execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
568 execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
569 } else if (execable("/sbin/pump")) {
570 result = execute("pump -i %iface% -k", ifd, exec);
571 } else if (execable("/sbin/dhclient")) {
572 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
573 } else if (execable("/sbin/dhcpcd")) {
574 result = execute("dhcpcd -k %iface%", ifd, exec);
576 return (result || bootp_down(ifd, exec));
579 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
581 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
582 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
583 "--returniffail --serverbcast", ifd, exec));
586 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
588 return( execute("pon [[%provider%]]", ifd, exec));
591 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
593 return( execute("poff [[%provider%]]", ifd, exec));
596 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
598 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
599 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
602 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
604 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
605 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
608 static struct method_t methods[] =
610 { "wvdial", wvdial_up, wvdial_down, },
611 { "ppp", ppp_up, ppp_down, },
612 { "static", static_up, static_down, },
613 { "bootp", bootp_up, bootp_down, },
614 { "dhcp", dhcp_up, dhcp_down, },
615 { "loopback", loopback_up, loopback_down, },
618 struct address_family_t addr_inet =
621 sizeof(methods) / sizeof(struct method_t),
625 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
627 static char *next_word(char **buf)
629 unsigned short length;
632 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
636 /* Skip over leading whitespace */
638 while (isspace(*word)) {
642 /* Skip over comments */
647 /* Find the length of this word */
648 length = strcspn(word, " \t\n");
652 *buf = word + length;
653 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
662 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
666 for (i = 0; af[i]; i++) {
667 if (strcmp(af[i]->name, name) == 0) {
674 static struct method_t *get_method(struct address_family_t *af, char *name)
678 for (i = 0; i < af->n_methods; i++) {
679 if (strcmp(af->method[i].name, name) == 0) {
680 return &af->method[i];
686 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
688 if (strcmp(ifa->iface, ifb->iface) != 0) {
691 if (ifa->address_family != ifb->address_family) {
697 static const llist_t *find_list_string(const llist_t *list, const char *string)
700 if (strcmp(list->data, string) == 0) {
708 static struct interfaces_file_t *read_interfaces(char *filename)
710 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
711 struct mapping_defn_t *currmap = NULL;
713 struct interface_defn_t *currif = NULL;
714 struct interfaces_file_t *defn;
719 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
721 defn = xmalloc(sizeof(struct interfaces_file_t));
722 defn->autointerfaces = NULL;
723 defn->mappings = NULL;
726 f = bb_xfopen(filename, "r");
728 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
731 firstword = next_word(&buf_ptr);
732 if (firstword == NULL) {
734 continue; /* blank line */
737 if (strcmp(firstword, "mapping") == 0) {
738 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
739 currmap = xmalloc(sizeof(struct mapping_defn_t));
740 currmap->max_matches = 0;
741 currmap->n_matches = 0;
742 currmap->match = NULL;
744 while ((firstword = next_word(&buf_ptr)) != NULL) {
745 if (currmap->max_matches == currmap->n_matches) {
746 currmap->max_matches = currmap->max_matches * 2 + 1;
747 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
750 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
752 currmap->max_mappings = 0;
753 currmap->n_mappings = 0;
754 currmap->mapping = NULL;
755 currmap->script = NULL;
757 struct mapping_defn_t **where = &defn->mappings;
758 while (*where != NULL) {
759 where = &(*where)->next;
762 currmap->next = NULL;
764 debug_noise("Added mapping\n");
766 currently_processing = MAPPING;
767 } else if (strcmp(firstword, "iface") == 0) {
770 char *address_family_name;
772 struct address_family_t *addr_fams[] = {
773 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
776 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
779 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
785 currif = xmalloc(sizeof(struct interface_defn_t));
786 iface_name = next_word(&buf_ptr);
787 address_family_name = next_word(&buf_ptr);
788 method_name = next_word(&buf_ptr);
790 if (buf_ptr == NULL) {
791 bb_error_msg("too few parameters for line \"%s\"", buf);
795 /* ship any trailing whitespace */
796 while (isspace(*buf_ptr)) {
800 if (buf_ptr[0] != '\0') {
801 bb_error_msg("too many parameters \"%s\"", buf);
805 currif->iface = bb_xstrdup(iface_name);
807 currif->address_family = get_address_family(addr_fams, address_family_name);
808 if (!currif->address_family) {
809 bb_error_msg("unknown address type \"%s\"", address_family_name);
813 currif->method = get_method(currif->address_family, method_name);
814 if (!currif->method) {
815 bb_error_msg("unknown method \"%s\"", method_name);
819 currif->automatic = 1;
820 currif->max_options = 0;
821 currif->n_options = 0;
822 currif->option = NULL;
825 struct interface_defn_t *tmp;
827 iface_list = defn->ifaces;
829 tmp = (struct interface_defn_t *) iface_list->data;
830 if (duplicate_if(tmp, currif)) {
831 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
834 iface_list = iface_list->link;
837 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
839 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
841 currently_processing = IFACE;
842 } else if (strcmp(firstword, "auto") == 0) {
843 while ((firstword = next_word(&buf_ptr)) != NULL) {
845 /* Check the interface isnt already listed */
846 if (find_list_string(defn->autointerfaces, firstword)) {
847 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
850 /* Add the interface to the list */
851 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
852 debug_noise("\nauto %s\n", firstword);
854 currently_processing = NONE;
856 switch (currently_processing) {
861 if (bb_strlen(buf_ptr) == 0) {
862 bb_error_msg("option with empty value \"%s\"", buf);
866 if (strcmp(firstword, "up") != 0
867 && strcmp(firstword, "down") != 0
868 && strcmp(firstword, "pre-up") != 0
869 && strcmp(firstword, "post-down") != 0) {
870 for (i = 0; i < currif->n_options; i++) {
871 if (strcmp(currif->option[i].name, firstword) == 0) {
872 bb_error_msg("duplicate option \"%s\"", buf);
878 if (currif->n_options >= currif->max_options) {
879 struct variable_t *opt;
881 currif->max_options = currif->max_options + 10;
882 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
883 currif->option = opt;
885 currif->option[currif->n_options].name = bb_xstrdup(firstword);
886 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
887 if (!currif->option[currif->n_options].name) {
891 if (!currif->option[currif->n_options].value) {
895 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
896 currif->option[currif->n_options].value);
900 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
901 if (strcmp(firstword, "script") == 0) {
902 if (currmap->script != NULL) {
903 bb_error_msg("duplicate script in mapping \"%s\"", buf);
906 currmap->script = bb_xstrdup(next_word(&buf_ptr));
908 } else if (strcmp(firstword, "map") == 0) {
909 if (currmap->max_mappings == currmap->n_mappings) {
910 currmap->max_mappings = currmap->max_mappings * 2 + 1;
911 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
913 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
914 currmap->n_mappings++;
916 bb_error_msg("misplaced option \"%s\"", buf);
923 bb_error_msg("misplaced option \"%s\"", buf);
929 if (ferror(f) != 0) {
930 bb_perror_msg_and_die("%s", filename);
937 static char *setlocalenv(char *format, char *name, char *value)
943 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
945 sprintf(result, format, name, value);
947 for (here = there = result; *there != '=' && *there; there++) {
951 *there = toupper(*there);
953 if (isalnum(*there) || *there == '_') {
958 memmove(here, there, bb_strlen(there) + 1);
963 static void set_environ(struct interface_defn_t *iface, char *mode)
967 const int n_env_entries = iface->n_options + 5;
970 if (environ != NULL) {
971 for (ppch = environ; *ppch; ppch++) {
978 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
979 environend = environ;
982 for (i = 0; i < iface->n_options; i++) {
983 if (strcmp(iface->option[i].name, "up") == 0
984 || strcmp(iface->option[i].name, "down") == 0
985 || strcmp(iface->option[i].name, "pre-up") == 0
986 || strcmp(iface->option[i].name, "post-down") == 0) {
989 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
993 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
995 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
997 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
999 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
1001 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
1005 static int doit(char *str)
1007 if (verbose || no_act) {
1008 printf("%s\n", str);
1015 switch (child = fork()) {
1016 case -1: /* failure */
1019 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ);
1022 waitpid(child, &status, 0);
1023 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1030 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
1034 for (i = 0; i < ifd->n_options; i++) {
1035 if (strcmp(ifd->option[i].name, opt) == 0) {
1036 if (!(*exec) (ifd->option[i].value)) {
1042 bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
1043 if ((*exec)(buf) != 1) {
1049 static int check(char *str) {
1053 static int iface_up(struct interface_defn_t *iface)
1055 if (!iface->method->up(iface,check)) return -1;
1056 set_environ(iface, "start");
1057 if (!execute_all(iface, doit, "pre-up")) return 0;
1058 if (!iface->method->up(iface, doit)) return 0;
1059 if (!execute_all(iface, doit, "up")) return 0;
1063 static int iface_down(struct interface_defn_t *iface)
1065 if (!iface->method->down(iface,check)) return -1;
1066 set_environ(iface, "stop");
1067 if (!execute_all(iface, doit, "down")) return 0;
1068 if (!iface->method->down(iface, doit)) return 0;
1069 if (!execute_all(iface, doit, "post-down")) return 0;
1073 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1074 static int popen2(FILE **in, FILE **out, char *command, ...)
1077 char *argv[11] = { command };
1079 int infd[2], outfd[2];
1083 va_start(ap, command);
1084 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1087 argv[argc] = NULL; /* make sure */
1090 if (pipe(infd) != 0) {
1094 if (pipe(outfd) != 0) {
1101 switch (pid = fork()) {
1102 case -1: /* failure */
1115 execvp(command, argv);
1117 default: /* parent */
1118 *in = fdopen(infd[1], "w");
1119 *out = fdopen(outfd[0], "r");
1127 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1133 char *logical = bb_xstrdup(physical);
1135 /* Run the mapping script. */
1136 pid = popen2(&in, &out, map->script, physical, NULL);
1138 /* popen2() returns 0 on failure. */
1142 /* Write mappings to stdin of mapping script. */
1143 for (i = 0; i < map->n_mappings; i++) {
1144 fprintf(in, "%s\n", map->mapping[i]);
1147 waitpid(pid, &status, 0);
1149 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1150 /* If the mapping script exited successfully, try to
1151 * grab a line of output and use that as the name of the
1152 * logical interface. */
1153 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1155 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1156 /* If we are able to read a line of output from the script,
1157 * remove any trailing whitespace and use this value
1158 * as the name of the logical interface. */
1159 char *pch = new_logical + bb_strlen(new_logical) - 1;
1161 while (pch >= new_logical && isspace(*pch))
1165 logical = new_logical;
1167 /* If we are UNABLE to read a line of output, discard are
1168 * freshly allocated memory. */
1177 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1179 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1181 unsigned short iface_len = bb_strlen(iface);
1182 llist_t *search = state_list;
1185 if ((strncmp(search->data, iface, iface_len) == 0) &&
1186 (search->data[iface_len] == '=')) {
1189 search = search->link;
1194 extern int ifupdown_main(int argc, char **argv)
1196 int (*cmds) (struct interface_defn_t *) = NULL;
1197 struct interfaces_file_t *defn;
1198 FILE *state_fp = NULL;
1199 llist_t *state_list = NULL;
1200 llist_t *target_list = NULL;
1201 char *interfaces = "/etc/network/interfaces";
1202 const char *statefile = "/var/run/ifstate";
1204 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1205 int run_mappings = 1;
1209 int any_failures = 0;
1212 if (bb_applet_name[2] == 'u') {
1216 /* ifdown command */
1220 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1221 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1223 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1227 case 'i': /* interfaces */
1228 interfaces = bb_xstrdup(optarg);
1230 case 'v': /* verbose */
1236 case 'n': /* no-act */
1239 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1240 case 'm': /* no-mappings */
1244 case 'f': /* force */
1253 if (argc - optind > 0) {
1263 debug_noise("reading %s file:\n", interfaces);
1264 defn = read_interfaces(interfaces);
1265 debug_noise("\ndone reading %s\n\n", interfaces);
1272 state_fp = fopen(statefile, "r");
1275 /* Create a list of interfaces to work on */
1277 if (cmds == iface_up) {
1278 target_list = defn->autointerfaces;
1283 const llist_t *list = state_list;
1285 new_item = xmalloc(sizeof(llist_t));
1286 new_item->data = strdup(list->data);
1287 new_item->link = NULL;
1290 target_list = new_item;
1292 while (list->link) {
1299 target_list = defn->autointerfaces;
1303 const llist_t *list = state_list;
1305 target_list = llist_add_to_end(target_list, strdup(list->data));
1308 target_list = defn->autointerfaces;
1312 target_list = llist_add_to_end(target_list, argv[optind]);
1316 /* Update the interfaces */
1317 while (target_list) {
1318 llist_t *iface_list;
1319 struct interface_defn_t *currif;
1326 iface = strdup(target_list->data);
1327 target_list = target_list->link;
1329 pch = strchr(iface, '=');
1332 liface = strdup(pch + 1);
1334 liface = strdup(iface);
1338 const llist_t *iface_state = find_iface_state(state_list, iface);
1340 if (cmds == iface_up) {
1343 bb_error_msg("interface %s already configured", iface);
1349 bb_error_msg("interface %s not configured", iface);
1355 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1356 if ((cmds == iface_up) && run_mappings) {
1357 struct mapping_defn_t *currmap;
1359 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1361 for (i = 0; i < currmap->n_matches; i++) {
1362 if (fnmatch(currmap->match[i], liface, 0) != 0)
1365 printf("Running mapping script %s on %s\n", currmap->script, liface);
1367 liface = run_mapping(iface, currmap);
1375 iface_list = defn->ifaces;
1376 while (iface_list) {
1377 currif = (struct interface_defn_t *) iface_list->data;
1378 if (strcmp(liface, currif->iface) == 0) {
1379 char *oldiface = currif->iface;
1382 currif->iface = iface;
1384 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1386 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1387 cmds_ret = cmds(currif);
1388 if (cmds_ret == -1) {
1389 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1390 liface, currif->address_family->name);
1392 } else if (cmds_ret == 0) {
1396 currif->iface = oldiface;
1398 iface_list = iface_list->link;
1404 if (!okay && !force) {
1405 bb_error_msg("Ignoring unknown interface %s", liface);
1408 llist_t *iface_state = find_iface_state(state_list, iface);
1410 if (cmds == iface_up) {
1411 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1412 sprintf(newiface, "%s=%s", iface, liface);
1413 if (iface_state == NULL) {
1414 state_list = llist_add_to_end(state_list, newiface);
1416 free(iface_state->data);
1417 iface_state->data = newiface;
1419 } else if (cmds == iface_down) {
1420 /* Remove an interface from the linked list */
1422 /* This needs to be done better */
1423 free(iface_state->data);
1424 free(iface_state->link);
1425 if (iface_state->link) {
1426 iface_state->data = iface_state->link->data;
1427 iface_state->link = iface_state->link->link;
1429 iface_state->data = NULL;
1430 iface_state->link = NULL;
1437 /* Actually write the new state */
1442 state_fp = bb_xfopen(statefile, "a+");
1444 if (ftruncate(fileno(state_fp), 0) < 0) {
1445 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1450 while (state_list) {
1451 if (state_list->data) {
1452 fputs(state_list->data, state_fp);
1453 fputc('\n', state_fp);
1455 state_list = state_list->link;
1461 if (state_fp != NULL) {