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);
77 struct address_family_t
81 struct method_t *method;
86 struct mapping_defn_t *next;
105 struct interface_defn_t
107 struct interface_defn_t *prev;
108 struct interface_defn_t *next;
111 struct address_family_t *address_family;
112 struct method_t *method;
118 struct variable_t *option;
121 struct interfaces_file_t
123 llist_t *autointerfaces;
125 struct mapping_defn_t *mappings;
128 static char no_act = 0;
129 static char verbose = 0;
130 static char **__myenviron = NULL;
132 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
134 static unsigned int count_bits(unsigned int a)
137 result = (a & 0x55) + ((a >> 1) & 0x55);
138 result = (result & 0x33) + ((result >> 2) & 0x33);
139 return((result & 0x0F) + ((result >> 4) & 0x0F));
142 static int count_netmask_bits(char *dotted_quad)
144 unsigned int result, a, b, c, d;
145 /* Found a netmask... Check if it is dotted quad */
146 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
148 result = count_bits(a);
149 result += count_bits(b);
150 result += count_bits(c);
151 result += count_bits(d);
152 return ((int)result);
156 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
158 if (*pos + str_length >= *len) {
161 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
163 *len = *len * 2 + str_length + 1;
166 while (str_length-- >= 1) {
167 (*buf)[(*pos)++] = *str;
173 static int strncmpz(char *l, char *r, size_t llen)
175 int i = strncmp(l, r, llen);
184 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
188 if (strncmpz(id, "iface", idlen) == 0) {
190 static char label_buf[20];
191 strncpy(label_buf, ifd->iface, 19);
193 result = strchr(label_buf, ':');
198 } else if (strncmpz(id, "label", idlen) == 0) {
201 for (i = 0; i < ifd->n_options; i++) {
202 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
203 return (ifd->option[i].value);
211 static char *parse(char *command, struct interface_defn_t *ifd)
215 size_t pos = 0, len = 0;
216 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
217 int okay[MAX_OPT_DEPTH] = { 1 };
224 addstr(&result, &len, &pos, command, 1);
229 addstr(&result, &len, &pos, command + 1, 1);
232 addstr(&result, &len, &pos, command, 1);
237 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
238 old_pos[opt_depth] = pos;
243 addstr(&result, &len, &pos, "[", 1);
248 if (command[1] == ']' && opt_depth > 1) {
250 if (!okay[opt_depth]) {
251 pos = old_pos[opt_depth];
256 addstr(&result, &len, &pos, "]", 1);
266 nextpercent = strchr(command, '%');
273 varvalue = get_var(command, nextpercent - command, ifd);
276 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
278 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
279 /* Sigh... Add a special case for 'ip' to convert from
280 * dotted quad to bit count style netmasks. */
281 if (strncmp(command, "bnmask", 6)==0) {
283 varvalue = get_var("netmask", 7, ifd);
284 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
286 sprintf(argument, "%d", res);
287 addstr(&result, &len, &pos, argument, bb_strlen(argument));
288 command = nextpercent + 1;
293 okay[opt_depth - 1] = 0;
296 command = nextpercent + 1;
317 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
322 out = parse(command, ifd);
335 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
336 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
338 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
341 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
343 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
346 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
348 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
351 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
353 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
356 static struct method_t methods_ipx[] = {
357 { "dynamic", dynamic_up, dynamic_down, },
358 { "static", static_up_ipx, static_down_ipx, },
361 static struct address_family_t addr_ipx = {
363 sizeof(methods_ipx) / sizeof(struct method_t),
366 #endif /* IFUP_FEATURE_IPX */
368 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
369 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
371 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
373 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
374 result += execute("ip link set %iface% up", ifd, exec);
375 return ((result == 2) ? 2 : 0);
377 return( execute("ifconfig %iface% add ::1", ifd, exec));
381 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
383 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
384 return(execute("ip link set %iface% down", ifd, exec));
386 return(execute("ifconfig %iface% del ::1", ifd, exec));
390 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
393 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
394 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
395 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
396 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
398 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
399 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
400 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
402 return ((result == 3) ? 3 : 0);
405 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
407 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
408 return(execute("ip link set %iface% down", ifd, exec));
410 return(execute("ifconfig %iface% down", ifd, exec));
414 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
415 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
418 result = execute("ip tunnel add %iface% mode sit remote "
419 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
420 result += execute("ip link set %iface% up", ifd, exec);
421 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
422 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
423 return ((result == 4) ? 4 : 0);
426 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
428 return( execute("ip tunnel del %iface%", ifd, exec));
432 static struct method_t methods6[] = {
433 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
434 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
436 { "static", static_up6, static_down6, },
437 { "loopback", loopback_up6, loopback_down6, },
440 static struct address_family_t addr_inet6 = {
442 sizeof(methods6) / sizeof(struct method_t),
445 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
447 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
448 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
450 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
452 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
453 result += execute("ip link set %iface% up", ifd, exec);
454 return ((result == 2) ? 2 : 0);
456 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
460 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
462 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
464 result = execute("ip addr flush dev %iface%", ifd, exec);
465 result += execute("ip link set %iface% down", ifd, exec);
466 return ((result == 2) ? 2 : 0);
468 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
472 static int static_up(struct interface_defn_t *ifd, execfn *exec)
475 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
476 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
477 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
478 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
479 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
480 return ((result == 3) ? 3 : 0);
482 result = execute("ifconfig %iface% %address% netmask %netmask% "
483 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
484 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
486 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
487 return ((result == 2) ? 2 : 0);
491 static int static_down(struct interface_defn_t *ifd, execfn *exec)
494 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
495 result = execute("ip addr flush dev %iface%", ifd, exec);
496 result += execute("ip link set %iface% down", ifd, exec);
498 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
499 result += execute("ifconfig %iface% down", ifd, exec);
501 return ((result == 2) ? 2 : 0);
504 static int execable(char *program)
507 if (0 == stat(program, &buf)) {
508 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
515 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
517 if (execable("/sbin/udhcpc")) {
518 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
519 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
520 } else if (execable("/sbin/pump")) {
521 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
522 } else if (execable("/sbin/dhclient")) {
523 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
524 } else if (execable("/sbin/dhcpcd")) {
525 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
526 "[[-l %leasetime%]] %iface%", ifd, exec));
531 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
534 if (execable("/sbin/udhcpc")) {
535 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
536 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
537 * sequentially so we don't need to sleep */
538 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
539 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
540 } else if (execable("/sbin/pump")) {
541 result = execute("pump -i %iface% -k", ifd, exec);
542 } else if (execable("/sbin/dhclient")) {
543 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
544 } else if (execable("/sbin/dhcpcd")) {
545 result = execute("dhcpcd -k %iface%", ifd, exec);
547 return (result || static_down(ifd, exec));
550 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
552 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
553 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
554 "--returniffail --serverbcast", ifd, exec));
557 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
559 return( execute("pon [[%provider%]]", ifd, exec));
562 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
564 return( execute("poff [[%provider%]]", ifd, exec));
567 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
569 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
570 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
573 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
575 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
576 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
579 static struct method_t methods[] =
581 { "wvdial", wvdial_up, wvdial_down, },
582 { "ppp", ppp_up, ppp_down, },
583 { "static", static_up, static_down, },
584 { "bootp", bootp_up, static_down, },
585 { "dhcp", dhcp_up, dhcp_down, },
586 { "loopback", loopback_up, loopback_down, },
589 static struct address_family_t addr_inet =
592 sizeof(methods) / sizeof(struct method_t),
596 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
598 static char *next_word(char **buf)
600 unsigned short length;
603 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
607 /* Skip over leading whitespace */
609 while (isspace(*word)) {
613 /* Skip over comments */
618 /* Find the length of this word */
619 length = strcspn(word, " \t\n");
623 *buf = word + length;
624 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
633 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
637 for (i = 0; af[i]; i++) {
638 if (strcmp(af[i]->name, name) == 0) {
645 static struct method_t *get_method(struct address_family_t *af, char *name)
649 for (i = 0; i < af->n_methods; i++) {
650 if (strcmp(af->method[i].name, name) == 0) {
651 return &af->method[i];
657 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
659 if (strcmp(ifa->iface, ifb->iface) != 0) {
662 if (ifa->address_family != ifb->address_family) {
668 static const llist_t *find_list_string(const llist_t *list, const char *string)
671 if (strcmp(list->data, string) == 0) {
679 static struct interfaces_file_t *read_interfaces(const char *filename)
681 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
682 struct mapping_defn_t *currmap = NULL;
684 struct interface_defn_t *currif = NULL;
685 struct interfaces_file_t *defn;
690 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
692 defn = xmalloc(sizeof(struct interfaces_file_t));
693 defn->autointerfaces = NULL;
694 defn->mappings = NULL;
697 f = bb_xfopen(filename, "r");
699 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
702 firstword = next_word(&buf_ptr);
703 if (firstword == NULL) {
705 continue; /* blank line */
708 if (strcmp(firstword, "mapping") == 0) {
709 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
710 currmap = xmalloc(sizeof(struct mapping_defn_t));
711 currmap->max_matches = 0;
712 currmap->n_matches = 0;
713 currmap->match = NULL;
715 while ((firstword = next_word(&buf_ptr)) != NULL) {
716 if (currmap->max_matches == currmap->n_matches) {
717 currmap->max_matches = currmap->max_matches * 2 + 1;
718 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
721 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
723 currmap->max_mappings = 0;
724 currmap->n_mappings = 0;
725 currmap->mapping = NULL;
726 currmap->script = NULL;
728 struct mapping_defn_t **where = &defn->mappings;
729 while (*where != NULL) {
730 where = &(*where)->next;
733 currmap->next = NULL;
735 debug_noise("Added mapping\n");
737 currently_processing = MAPPING;
738 } else if (strcmp(firstword, "iface") == 0) {
741 char *address_family_name;
743 struct address_family_t *addr_fams[] = {
744 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
747 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
750 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
756 currif = xmalloc(sizeof(struct interface_defn_t));
757 iface_name = next_word(&buf_ptr);
758 address_family_name = next_word(&buf_ptr);
759 method_name = next_word(&buf_ptr);
761 if (buf_ptr == NULL) {
762 bb_error_msg("too few parameters for line \"%s\"", buf);
766 /* ship any trailing whitespace */
767 while (isspace(*buf_ptr)) {
771 if (buf_ptr[0] != '\0') {
772 bb_error_msg("too many parameters \"%s\"", buf);
776 currif->iface = bb_xstrdup(iface_name);
778 currif->address_family = get_address_family(addr_fams, address_family_name);
779 if (!currif->address_family) {
780 bb_error_msg("unknown address type \"%s\"", address_family_name);
784 currif->method = get_method(currif->address_family, method_name);
785 if (!currif->method) {
786 bb_error_msg("unknown method \"%s\"", method_name);
790 currif->automatic = 1;
791 currif->max_options = 0;
792 currif->n_options = 0;
793 currif->option = NULL;
796 struct interface_defn_t *tmp;
798 iface_list = defn->ifaces;
800 tmp = (struct interface_defn_t *) iface_list->data;
801 if (duplicate_if(tmp, currif)) {
802 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
805 iface_list = iface_list->link;
808 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
810 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
812 currently_processing = IFACE;
813 } else if (strcmp(firstword, "auto") == 0) {
814 while ((firstword = next_word(&buf_ptr)) != NULL) {
816 /* Check the interface isnt already listed */
817 if (find_list_string(defn->autointerfaces, firstword)) {
818 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
821 /* Add the interface to the list */
822 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
823 debug_noise("\nauto %s\n", firstword);
825 currently_processing = NONE;
827 switch (currently_processing) {
832 if (bb_strlen(buf_ptr) == 0) {
833 bb_error_msg("option with empty value \"%s\"", buf);
837 if (strcmp(firstword, "up") != 0
838 && strcmp(firstword, "down") != 0
839 && strcmp(firstword, "pre-up") != 0
840 && strcmp(firstword, "post-down") != 0) {
841 for (i = 0; i < currif->n_options; i++) {
842 if (strcmp(currif->option[i].name, firstword) == 0) {
843 bb_error_msg("duplicate option \"%s\"", buf);
849 if (currif->n_options >= currif->max_options) {
850 struct variable_t *opt;
852 currif->max_options = currif->max_options + 10;
853 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
854 currif->option = opt;
856 currif->option[currif->n_options].name = bb_xstrdup(firstword);
857 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
858 if (!currif->option[currif->n_options].name) {
862 if (!currif->option[currif->n_options].value) {
866 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
867 currif->option[currif->n_options].value);
871 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
872 if (strcmp(firstword, "script") == 0) {
873 if (currmap->script != NULL) {
874 bb_error_msg("duplicate script in mapping \"%s\"", buf);
877 currmap->script = bb_xstrdup(next_word(&buf_ptr));
879 } else if (strcmp(firstword, "map") == 0) {
880 if (currmap->max_mappings == currmap->n_mappings) {
881 currmap->max_mappings = currmap->max_mappings * 2 + 1;
882 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
884 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
885 currmap->n_mappings++;
887 bb_error_msg("misplaced option \"%s\"", buf);
894 bb_error_msg("misplaced option \"%s\"", buf);
900 if (ferror(f) != 0) {
901 bb_perror_msg_and_die("%s", filename);
908 static char *setlocalenv(char *format, char *name, char *value)
914 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
916 sprintf(result, format, name, value);
918 for (here = there = result; *there != '=' && *there; there++) {
922 *there = toupper(*there);
924 if (isalnum(*there) || *there == '_') {
929 memmove(here, there, bb_strlen(there) + 1);
934 static void set_environ(struct interface_defn_t *iface, char *mode)
938 const int n_env_entries = iface->n_options + 5;
941 if (__myenviron != NULL) {
942 for (ppch = __myenviron; *ppch; ppch++) {
949 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
950 environend = __myenviron;
953 for (i = 0; i < iface->n_options; i++) {
954 if (strcmp(iface->option[i].name, "up") == 0
955 || strcmp(iface->option[i].name, "down") == 0
956 || strcmp(iface->option[i].name, "pre-up") == 0
957 || strcmp(iface->option[i].name, "post-down") == 0) {
960 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
964 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
966 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
968 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
970 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
972 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
976 static int doit(char *str)
978 if (verbose || no_act) {
986 switch (child = fork()) {
987 case -1: /* failure */
990 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
993 waitpid(child, &status, 0);
994 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1001 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
1005 for (i = 0; i < ifd->n_options; i++) {
1006 if (strcmp(ifd->option[i].name, opt) == 0) {
1007 if (!(*exec) (ifd->option[i].value)) {
1013 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
1014 if ((*exec)(buf) != 1) {
1020 static int check(char *str) {
1024 static int iface_up(struct interface_defn_t *iface)
1026 if (!iface->method->up(iface,check)) return -1;
1027 set_environ(iface, "start");
1028 if (!execute_all(iface, doit, "pre-up")) return 0;
1029 if (!iface->method->up(iface, doit)) return 0;
1030 if (!execute_all(iface, doit, "up")) return 0;
1034 static int iface_down(struct interface_defn_t *iface)
1036 if (!iface->method->down(iface,check)) return -1;
1037 set_environ(iface, "stop");
1038 if (!execute_all(iface, doit, "down")) return 0;
1039 if (!iface->method->down(iface, doit)) return 0;
1040 if (!execute_all(iface, doit, "post-down")) return 0;
1044 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1045 static int popen2(FILE **in, FILE **out, char *command, ...)
1048 char *argv[11] = { command };
1050 int infd[2], outfd[2];
1054 va_start(ap, command);
1055 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1058 argv[argc] = NULL; /* make sure */
1061 if (pipe(infd) != 0) {
1065 if (pipe(outfd) != 0) {
1072 switch (pid = fork()) {
1073 case -1: /* failure */
1086 execvp(command, argv);
1088 default: /* parent */
1089 *in = fdopen(infd[1], "w");
1090 *out = fdopen(outfd[0], "r");
1098 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1104 char *logical = bb_xstrdup(physical);
1106 /* Run the mapping script. */
1107 pid = popen2(&in, &out, map->script, physical, NULL);
1109 /* popen2() returns 0 on failure. */
1113 /* Write mappings to stdin of mapping script. */
1114 for (i = 0; i < map->n_mappings; i++) {
1115 fprintf(in, "%s\n", map->mapping[i]);
1118 waitpid(pid, &status, 0);
1120 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1121 /* If the mapping script exited successfully, try to
1122 * grab a line of output and use that as the name of the
1123 * logical interface. */
1124 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1126 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1127 /* If we are able to read a line of output from the script,
1128 * remove any trailing whitespace and use this value
1129 * as the name of the logical interface. */
1130 char *pch = new_logical + bb_strlen(new_logical) - 1;
1132 while (pch >= new_logical && isspace(*pch))
1136 logical = new_logical;
1138 /* If we are UNABLE to read a line of output, discard are
1139 * freshly allocated memory. */
1148 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1150 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1152 unsigned short iface_len = bb_strlen(iface);
1153 llist_t *search = state_list;
1156 if ((strncmp(search->data, iface, iface_len) == 0) &&
1157 (search->data[iface_len] == '=')) {
1160 search = search->link;
1165 extern int ifupdown_main(int argc, char **argv)
1167 int (*cmds) (struct interface_defn_t *) = NULL;
1168 struct interfaces_file_t *defn;
1169 FILE *state_fp = NULL;
1170 llist_t *state_list = NULL;
1171 llist_t *target_list = NULL;
1172 const char *interfaces = "/etc/network/interfaces";
1173 const char *statefile = "/var/run/ifstate";
1175 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1176 int run_mappings = 1;
1180 int any_failures = 0;
1183 if (bb_applet_name[2] == 'u') {
1187 /* ifdown command */
1191 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1192 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1194 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1198 case 'i': /* interfaces */
1199 interfaces = optarg;
1201 case 'v': /* verbose */
1207 case 'n': /* no-act */
1210 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1211 case 'm': /* no-mappings */
1215 case 'f': /* force */
1224 if (argc - optind > 0) {
1234 debug_noise("reading %s file:\n", interfaces);
1235 defn = read_interfaces(interfaces);
1236 debug_noise("\ndone reading %s\n\n", interfaces);
1243 state_fp = fopen(statefile, "r");
1246 /* Create a list of interfaces to work on */
1248 if (cmds == iface_up) {
1249 target_list = defn->autointerfaces;
1254 const llist_t *list = state_list;
1256 new_item = xmalloc(sizeof(llist_t));
1257 new_item->data = strdup(list->data);
1258 new_item->link = NULL;
1261 target_list = new_item;
1263 while (list->link) {
1270 target_list = defn->autointerfaces;
1274 const llist_t *list = state_list;
1276 target_list = llist_add_to_end(target_list, strdup(list->data));
1279 target_list = defn->autointerfaces;
1283 target_list = llist_add_to_end(target_list, argv[optind]);
1287 /* Update the interfaces */
1288 while (target_list) {
1289 llist_t *iface_list;
1290 struct interface_defn_t *currif;
1297 iface = strdup(target_list->data);
1298 target_list = target_list->link;
1300 pch = strchr(iface, '=');
1303 liface = strdup(pch + 1);
1305 liface = strdup(iface);
1309 const llist_t *iface_state = find_iface_state(state_list, iface);
1311 if (cmds == iface_up) {
1314 bb_error_msg("interface %s already configured", iface);
1320 bb_error_msg("interface %s not configured", iface);
1326 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1327 if ((cmds == iface_up) && run_mappings) {
1328 struct mapping_defn_t *currmap;
1330 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1332 for (i = 0; i < currmap->n_matches; i++) {
1333 if (fnmatch(currmap->match[i], liface, 0) != 0)
1336 printf("Running mapping script %s on %s\n", currmap->script, liface);
1338 liface = run_mapping(iface, currmap);
1346 iface_list = defn->ifaces;
1347 while (iface_list) {
1348 currif = (struct interface_defn_t *) iface_list->data;
1349 if (strcmp(liface, currif->iface) == 0) {
1350 char *oldiface = currif->iface;
1353 currif->iface = iface;
1355 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1357 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1358 cmds_ret = cmds(currif);
1359 if (cmds_ret == -1) {
1360 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1361 liface, currif->address_family->name);
1363 } else if (cmds_ret == 0) {
1367 currif->iface = oldiface;
1369 iface_list = iface_list->link;
1375 if (!okay && !force) {
1376 bb_error_msg("Ignoring unknown interface %s", liface);
1379 llist_t *iface_state = find_iface_state(state_list, iface);
1381 if (cmds == iface_up) {
1382 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1383 sprintf(newiface, "%s=%s", iface, liface);
1384 if (iface_state == NULL) {
1385 state_list = llist_add_to_end(state_list, newiface);
1387 free(iface_state->data);
1388 iface_state->data = newiface;
1390 } else if (cmds == iface_down) {
1391 /* Remove an interface from the linked list */
1393 /* This needs to be done better */
1394 free(iface_state->data);
1395 free(iface_state->link);
1396 if (iface_state->link) {
1397 iface_state->data = iface_state->link->data;
1398 iface_state->link = iface_state->link->link;
1400 iface_state->data = NULL;
1401 iface_state->link = NULL;
1408 /* Actually write the new state */
1413 state_fp = bb_xfopen(statefile, "a+");
1415 if (ftruncate(fileno(state_fp), 0) < 0) {
1416 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1421 while (state_list) {
1422 if (state_list->data) {
1423 fputs(state_list->data, state_fp);
1424 fputc('\n', state_fp);
1426 state_list = state_list->link;
1432 if (state_fp != NULL) {