1 /* vi: set sw=4 ts=4: */
4 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
5 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
7 * Based on ifupdown v 0.6.4 by Anthony Towns
8 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
10 * Changes to upstream version
11 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
12 * Lines in the interfaces file cannot wrap.
13 * To adhere to the FHS, the default state file is /var/run/ifstate.
15 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
18 /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
21 #include <sys/utsname.h>
37 #define MAX_OPT_DEPTH 10
38 #define EUNBALBRACK 10001
39 #define EUNDEFVAR 10002
40 #define EUNBALPER 10000
42 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
43 #define MAX_INTERFACE_LENGTH 10
47 #define debug_noise(fmt, args...) printf(fmt, ## args)
49 #define debug_noise(fmt, args...)
52 /* Forward declaration */
53 struct interface_defn_t;
55 typedef int (execfn)(char *command);
56 typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
65 struct address_family_t
69 struct method_t *method;
74 struct mapping_defn_t *next;
93 struct interface_defn_t
95 struct interface_defn_t *prev;
96 struct interface_defn_t *next;
99 struct address_family_t *address_family;
100 struct method_t *method;
106 struct variable_t *option;
109 struct interfaces_file_t
111 llist_t *autointerfaces;
113 struct mapping_defn_t *mappings;
116 static char no_act = 0;
117 static char verbose = 0;
118 static char **__myenviron = NULL;
120 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
122 static unsigned int count_bits(unsigned int a)
125 result = (a & 0x55) + ((a >> 1) & 0x55);
126 result = (result & 0x33) + ((result >> 2) & 0x33);
127 return((result & 0x0F) + ((result >> 4) & 0x0F));
130 static int count_netmask_bits(char *dotted_quad)
132 unsigned int result, a, b, c, d;
133 /* Found a netmask... Check if it is dotted quad */
134 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
136 result = count_bits(a);
137 result += count_bits(b);
138 result += count_bits(c);
139 result += count_bits(d);
140 return ((int)result);
144 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 || \
145 ENABLE_FEATURE_IFUPDOWN_IPX
146 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
148 if (*pos + str_length >= *len) {
151 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
153 *len = *len * 2 + str_length + 1;
156 while (str_length-- >= 1) {
157 (*buf)[(*pos)++] = *str;
163 static int strncmpz(char *l, char *r, size_t llen)
165 int i = strncmp(l, r, llen);
174 static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
178 if (strncmpz(id, "iface", idlen) == 0) {
180 static char label_buf[20];
181 strncpy(label_buf, ifd->iface, 19);
183 result = strchr(label_buf, ':');
188 } else if (strncmpz(id, "label", idlen) == 0) {
191 for (i = 0; i < ifd->n_options; i++) {
192 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
193 return (ifd->option[i].value);
201 static char *parse(char *command, struct interface_defn_t *ifd)
205 size_t pos = 0, len = 0;
206 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
207 int okay[MAX_OPT_DEPTH] = { 1 };
214 addstr(&result, &len, &pos, command, 1);
219 addstr(&result, &len, &pos, command + 1, 1);
222 addstr(&result, &len, &pos, command, 1);
227 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
228 old_pos[opt_depth] = pos;
233 addstr(&result, &len, &pos, "[", 1);
238 if (command[1] == ']' && opt_depth > 1) {
240 if (!okay[opt_depth]) {
241 pos = old_pos[opt_depth];
246 addstr(&result, &len, &pos, "]", 1);
256 nextpercent = strchr(command, '%');
263 varvalue = get_var(command, nextpercent - command, ifd);
266 addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
268 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
269 /* Sigh... Add a special case for 'ip' to convert from
270 * dotted quad to bit count style netmasks. */
271 if (strncmp(command, "bnmask", 6)==0) {
273 varvalue = get_var("netmask", 7, ifd);
274 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
276 sprintf(argument, "%d", res);
277 addstr(&result, &len, &pos, argument, bb_strlen(argument));
278 command = nextpercent + 1;
283 okay[opt_depth - 1] = 0;
286 command = nextpercent + 1;
307 static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
312 out = parse(command, ifd);
326 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
327 static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
329 return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
332 static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
334 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
337 static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
339 return(execute("ipx_interface add %iface% %frame%", ifd, exec));
342 static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
344 return(execute("ipx_interface del %iface% %frame%", ifd, exec));
347 static struct method_t methods_ipx[] = {
348 { "dynamic", dynamic_up, dynamic_down, },
349 { "static", static_up_ipx, static_down_ipx, },
352 static struct address_family_t addr_ipx = {
354 sizeof(methods_ipx) / sizeof(struct method_t),
357 #endif /* IFUP_FEATURE_IPX */
359 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
360 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
362 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
364 result =execute("ip addr add ::1 dev %iface%", ifd, exec);
365 result += execute("ip link set %iface% up", ifd, exec);
366 return ((result == 2) ? 2 : 0);
368 return( execute("ifconfig %iface% add ::1", ifd, exec));
372 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
374 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
375 return(execute("ip link set %iface% down", ifd, exec));
377 return(execute("ifconfig %iface% del ::1", ifd, exec));
381 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
384 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
385 result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
386 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
387 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
389 result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
390 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
391 result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
393 return ((result == 3) ? 3 : 0);
396 static int static_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% down", ifd, exec));
405 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
406 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
409 result = execute("ip tunnel add %iface% mode sit remote "
410 "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
411 result += execute("ip link set %iface% up", ifd, exec);
412 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
413 result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
414 return ((result == 4) ? 4 : 0);
417 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
419 return( execute("ip tunnel del %iface%", ifd, exec));
423 static struct method_t methods6[] = {
424 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
425 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
427 { "static", static_up6, static_down6, },
428 { "loopback", loopback_up6, loopback_down6, },
431 static struct address_family_t addr_inet6 = {
433 sizeof(methods6) / sizeof(struct method_t),
436 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
438 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
439 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
441 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
443 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
444 result += execute("ip link set %iface% up", ifd, exec);
445 return ((result == 2) ? 2 : 0);
447 return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
451 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
453 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
455 result = execute("ip addr flush dev %iface%", ifd, exec);
456 result += execute("ip link set %iface% down", ifd, exec);
457 return ((result == 2) ? 2 : 0);
459 return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
463 static int static_up(struct interface_defn_t *ifd, execfn *exec)
466 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
467 result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
468 "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
469 result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
470 result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
471 return ((result == 3) ? 3 : 0);
473 result = execute("ifconfig %iface% %address% netmask %netmask% "
474 "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
475 "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
477 result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
478 return ((result == 2) ? 2 : 0);
482 static int static_down(struct interface_defn_t *ifd, execfn *exec)
485 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
486 result = execute("ip addr flush dev %iface%", ifd, exec);
487 result += execute("ip link set %iface% down", ifd, exec);
489 result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
490 result += execute("ifconfig %iface% down", ifd, exec);
492 return ((result == 2) ? 2 : 0);
495 static int execable(char *program)
498 if (0 == stat(program, &buf)) {
499 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
506 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
508 if (execable("/sbin/udhcpc")) {
509 return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
510 "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
511 } else if (execable("/sbin/pump")) {
512 return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
513 } else if (execable("/sbin/dhclient")) {
514 return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
515 } else if (execable("/sbin/dhcpcd")) {
516 return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
517 "[[-l %leasetime%]] %iface%", ifd, exec));
522 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
525 if (execable("/sbin/udhcpc")) {
526 /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
527 * and SIGTERM causes udhcpc to exit. Signals are queued and processed
528 * sequentially so we don't need to sleep */
529 result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
530 result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
531 } else if (execable("/sbin/pump")) {
532 result = execute("pump -i %iface% -k", ifd, exec);
533 } else if (execable("/sbin/dhclient")) {
534 result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
535 } else if (execable("/sbin/dhcpcd")) {
536 result = execute("dhcpcd -k %iface%", ifd, exec);
538 return (result || static_down(ifd, exec));
541 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
543 return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
544 "[[--server %server%]] [[--hwaddr %hwaddr%]] "
545 "--returniffail --serverbcast", ifd, exec));
548 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
550 return( execute("pon [[%provider%]]", ifd, exec));
553 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
555 return( execute("poff [[%provider%]]", ifd, exec));
558 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
560 return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
561 "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
564 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
566 return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
567 "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
570 static struct method_t methods[] =
572 { "wvdial", wvdial_up, wvdial_down, },
573 { "ppp", ppp_up, ppp_down, },
574 { "static", static_up, static_down, },
575 { "bootp", bootp_up, static_down, },
576 { "dhcp", dhcp_up, dhcp_down, },
577 { "loopback", loopback_up, loopback_down, },
580 static struct address_family_t addr_inet =
583 sizeof(methods) / sizeof(struct method_t),
587 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
589 static char *next_word(char **buf)
591 unsigned short length;
594 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
598 /* Skip over leading whitespace */
600 while (isspace(*word)) {
604 /* Skip over comments */
609 /* Find the length of this word */
610 length = strcspn(word, " \t\n");
614 *buf = word + length;
615 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
624 static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
628 for (i = 0; af[i]; i++) {
629 if (strcmp(af[i]->name, name) == 0) {
636 static struct method_t *get_method(struct address_family_t *af, char *name)
640 for (i = 0; i < af->n_methods; i++) {
641 if (strcmp(af->method[i].name, name) == 0) {
642 return &af->method[i];
648 static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
650 if (strcmp(ifa->iface, ifb->iface) != 0) {
653 if (ifa->address_family != ifb->address_family) {
659 static const llist_t *find_list_string(const llist_t *list, const char *string)
662 if (strcmp(list->data, string) == 0) {
670 static struct interfaces_file_t *read_interfaces(const char *filename)
672 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
673 struct mapping_defn_t *currmap = NULL;
675 struct interface_defn_t *currif = NULL;
676 struct interfaces_file_t *defn;
681 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
683 defn = xmalloc(sizeof(struct interfaces_file_t));
684 defn->autointerfaces = NULL;
685 defn->mappings = NULL;
688 f = bb_xfopen(filename, "r");
690 while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
693 firstword = next_word(&buf_ptr);
694 if (firstword == NULL) {
696 continue; /* blank line */
699 if (strcmp(firstword, "mapping") == 0) {
700 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
701 currmap = xmalloc(sizeof(struct mapping_defn_t));
702 currmap->max_matches = 0;
703 currmap->n_matches = 0;
704 currmap->match = NULL;
706 while ((firstword = next_word(&buf_ptr)) != NULL) {
707 if (currmap->max_matches == currmap->n_matches) {
708 currmap->max_matches = currmap->max_matches * 2 + 1;
709 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
712 currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
714 currmap->max_mappings = 0;
715 currmap->n_mappings = 0;
716 currmap->mapping = NULL;
717 currmap->script = NULL;
719 struct mapping_defn_t **where = &defn->mappings;
720 while (*where != NULL) {
721 where = &(*where)->next;
724 currmap->next = NULL;
726 debug_noise("Added mapping\n");
728 currently_processing = MAPPING;
729 } else if (strcmp(firstword, "iface") == 0) {
732 char *address_family_name;
734 struct address_family_t *addr_fams[] = {
735 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
738 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
741 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
747 currif = xmalloc(sizeof(struct interface_defn_t));
748 iface_name = next_word(&buf_ptr);
749 address_family_name = next_word(&buf_ptr);
750 method_name = next_word(&buf_ptr);
752 if (buf_ptr == NULL) {
753 bb_error_msg("too few parameters for line \"%s\"", buf);
757 /* ship any trailing whitespace */
758 while (isspace(*buf_ptr)) {
762 if (buf_ptr[0] != '\0') {
763 bb_error_msg("too many parameters \"%s\"", buf);
767 currif->iface = bb_xstrdup(iface_name);
769 currif->address_family = get_address_family(addr_fams, address_family_name);
770 if (!currif->address_family) {
771 bb_error_msg("unknown address type \"%s\"", address_family_name);
775 currif->method = get_method(currif->address_family, method_name);
776 if (!currif->method) {
777 bb_error_msg("unknown method \"%s\"", method_name);
781 currif->automatic = 1;
782 currif->max_options = 0;
783 currif->n_options = 0;
784 currif->option = NULL;
787 struct interface_defn_t *tmp;
789 iface_list = defn->ifaces;
791 tmp = (struct interface_defn_t *) iface_list->data;
792 if (duplicate_if(tmp, currif)) {
793 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
796 iface_list = iface_list->link;
799 defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
801 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
803 currently_processing = IFACE;
804 } else if (strcmp(firstword, "auto") == 0) {
805 while ((firstword = next_word(&buf_ptr)) != NULL) {
807 /* Check the interface isnt already listed */
808 if (find_list_string(defn->autointerfaces, firstword)) {
809 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
812 /* Add the interface to the list */
813 defn->autointerfaces = llist_add_to_end(defn->autointerfaces, bb_xstrdup(firstword));
814 debug_noise("\nauto %s\n", firstword);
816 currently_processing = NONE;
818 switch (currently_processing) {
823 if (bb_strlen(buf_ptr) == 0) {
824 bb_error_msg("option with empty value \"%s\"", buf);
828 if (strcmp(firstword, "up") != 0
829 && strcmp(firstword, "down") != 0
830 && strcmp(firstword, "pre-up") != 0
831 && strcmp(firstword, "post-down") != 0) {
832 for (i = 0; i < currif->n_options; i++) {
833 if (strcmp(currif->option[i].name, firstword) == 0) {
834 bb_error_msg("duplicate option \"%s\"", buf);
840 if (currif->n_options >= currif->max_options) {
841 struct variable_t *opt;
843 currif->max_options = currif->max_options + 10;
844 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
845 currif->option = opt;
847 currif->option[currif->n_options].name = bb_xstrdup(firstword);
848 currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
849 if (!currif->option[currif->n_options].name) {
853 if (!currif->option[currif->n_options].value) {
857 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
858 currif->option[currif->n_options].value);
862 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
863 if (strcmp(firstword, "script") == 0) {
864 if (currmap->script != NULL) {
865 bb_error_msg("duplicate script in mapping \"%s\"", buf);
868 currmap->script = bb_xstrdup(next_word(&buf_ptr));
870 } else if (strcmp(firstword, "map") == 0) {
871 if (currmap->max_mappings == currmap->n_mappings) {
872 currmap->max_mappings = currmap->max_mappings * 2 + 1;
873 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
875 currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
876 currmap->n_mappings++;
878 bb_error_msg("misplaced option \"%s\"", buf);
885 bb_error_msg("misplaced option \"%s\"", buf);
891 if (ferror(f) != 0) {
892 bb_perror_msg_and_die("%s", filename);
899 static char *setlocalenv(char *format, char *name, char *value)
905 result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
907 sprintf(result, format, name, value);
909 for (here = there = result; *there != '=' && *there; there++) {
913 *there = toupper(*there);
915 if (isalnum(*there) || *there == '_') {
920 memmove(here, there, bb_strlen(there) + 1);
925 static void set_environ(struct interface_defn_t *iface, char *mode)
929 const int n_env_entries = iface->n_options + 5;
932 if (__myenviron != NULL) {
933 for (ppch = __myenviron; *ppch; ppch++) {
940 __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
941 environend = __myenviron;
944 for (i = 0; i < iface->n_options; i++) {
945 if (strcmp(iface->option[i].name, "up") == 0
946 || strcmp(iface->option[i].name, "down") == 0
947 || strcmp(iface->option[i].name, "pre-up") == 0
948 || strcmp(iface->option[i].name, "post-down") == 0) {
951 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
955 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
957 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
959 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
961 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
963 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
967 static int doit(char *str)
969 if (verbose || no_act) {
977 switch (child = fork()) {
978 case -1: /* failure */
981 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
984 waitpid(child, &status, 0);
985 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
992 static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
996 for (i = 0; i < ifd->n_options; i++) {
997 if (strcmp(ifd->option[i].name, opt) == 0) {
998 if (!(*exec) (ifd->option[i].value)) {
1004 buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
1005 if ((*exec)(buf) != 1) {
1011 static int check(char *str) {
1015 static int iface_up(struct interface_defn_t *iface)
1017 if (!iface->method->up(iface,check)) return -1;
1018 set_environ(iface, "start");
1019 if (!execute_all(iface, doit, "pre-up")) return 0;
1020 if (!iface->method->up(iface, doit)) return 0;
1021 if (!execute_all(iface, doit, "up")) return 0;
1025 static int iface_down(struct interface_defn_t *iface)
1027 if (!iface->method->down(iface,check)) return -1;
1028 set_environ(iface, "stop");
1029 if (!execute_all(iface, doit, "down")) return 0;
1030 if (!iface->method->down(iface, doit)) return 0;
1031 if (!execute_all(iface, doit, "post-down")) return 0;
1035 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1036 static int popen2(FILE **in, FILE **out, char *command, ...)
1039 char *argv[11] = { command };
1041 int infd[2], outfd[2];
1045 va_start(ap, command);
1046 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1049 argv[argc] = NULL; /* make sure */
1052 if (pipe(infd) != 0) {
1056 if (pipe(outfd) != 0) {
1063 switch (pid = fork()) {
1064 case -1: /* failure */
1077 execvp(command, argv);
1079 default: /* parent */
1080 *in = fdopen(infd[1], "w");
1081 *out = fdopen(outfd[0], "r");
1089 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1095 char *logical = bb_xstrdup(physical);
1097 /* Run the mapping script. */
1098 pid = popen2(&in, &out, map->script, physical, NULL);
1100 /* popen2() returns 0 on failure. */
1104 /* Write mappings to stdin of mapping script. */
1105 for (i = 0; i < map->n_mappings; i++) {
1106 fprintf(in, "%s\n", map->mapping[i]);
1109 waitpid(pid, &status, 0);
1111 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1112 /* If the mapping script exited successfully, try to
1113 * grab a line of output and use that as the name of the
1114 * logical interface. */
1115 char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
1117 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1118 /* If we are able to read a line of output from the script,
1119 * remove any trailing whitespace and use this value
1120 * as the name of the logical interface. */
1121 char *pch = new_logical + bb_strlen(new_logical) - 1;
1123 while (pch >= new_logical && isspace(*pch))
1127 logical = new_logical;
1129 /* If we are UNABLE to read a line of output, discard are
1130 * freshly allocated memory. */
1139 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1141 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1143 unsigned short iface_len = bb_strlen(iface);
1144 llist_t *search = state_list;
1147 if ((strncmp(search->data, iface, iface_len) == 0) &&
1148 (search->data[iface_len] == '=')) {
1151 search = search->link;
1156 extern int ifupdown_main(int argc, char **argv)
1158 int (*cmds) (struct interface_defn_t *) = NULL;
1159 struct interfaces_file_t *defn;
1160 llist_t *state_list = NULL;
1161 llist_t *target_list = NULL;
1162 const char *interfaces = "/etc/network/interfaces";
1163 const char *statefile = "/var/run/ifstate";
1165 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1166 int run_mappings = 1;
1170 int any_failures = 0;
1173 if (bb_applet_name[2] == 'u') {
1177 /* ifdown command */
1181 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1182 while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
1184 while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
1188 case 'i': /* interfaces */
1189 interfaces = optarg;
1191 case 'v': /* verbose */
1197 case 'n': /* no-act */
1200 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1201 case 'm': /* no-mappings */
1205 case 'f': /* force */
1214 if (argc - optind > 0) {
1224 debug_noise("reading %s file:\n", interfaces);
1225 defn = read_interfaces(interfaces);
1226 debug_noise("\ndone reading %s\n\n", interfaces);
1232 /* Create a list of interfaces to work on */
1234 if (cmds == iface_up) {
1235 target_list = defn->autointerfaces;
1240 const llist_t *list = state_list;
1242 new_item = xmalloc(sizeof(llist_t));
1243 new_item->data = bb_xstrdup(list->data);
1244 new_item->link = NULL;
1247 target_list = new_item;
1249 while (list->link) {
1256 target_list = defn->autointerfaces;
1260 const llist_t *list = state_list;
1262 target_list = llist_add_to_end(target_list, bb_xstrdup(list->data));
1265 target_list = defn->autointerfaces;
1269 target_list = llist_add_to_end(target_list, argv[optind]);
1273 /* Update the interfaces */
1274 while (target_list) {
1275 llist_t *iface_list;
1276 struct interface_defn_t *currif;
1283 iface = bb_xstrdup(target_list->data);
1284 target_list = target_list->link;
1286 pch = strchr(iface, '=');
1289 liface = bb_xstrdup(pch + 1);
1291 liface = bb_xstrdup(iface);
1295 const llist_t *iface_state = find_iface_state(state_list, iface);
1297 if (cmds == iface_up) {
1300 bb_error_msg("interface %s already configured", iface);
1306 bb_error_msg("interface %s not configured", iface);
1312 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1313 if ((cmds == iface_up) && run_mappings) {
1314 struct mapping_defn_t *currmap;
1316 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1318 for (i = 0; i < currmap->n_matches; i++) {
1319 if (fnmatch(currmap->match[i], liface, 0) != 0)
1322 printf("Running mapping script %s on %s\n", currmap->script, liface);
1324 liface = run_mapping(iface, currmap);
1332 iface_list = defn->ifaces;
1333 while (iface_list) {
1334 currif = (struct interface_defn_t *) iface_list->data;
1335 if (strcmp(liface, currif->iface) == 0) {
1336 char *oldiface = currif->iface;
1339 currif->iface = iface;
1341 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1343 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1344 cmds_ret = cmds(currif);
1345 if (cmds_ret == -1) {
1346 bb_error_msg("Don't seem to have all the variables for %s/%s.",
1347 liface, currif->address_family->name);
1349 } else if (cmds_ret == 0) {
1353 currif->iface = oldiface;
1355 iface_list = iface_list->link;
1361 if (!okay && !force) {
1362 bb_error_msg("Ignoring unknown interface %s", liface);
1365 llist_t *iface_state = find_iface_state(state_list, iface);
1367 if (cmds == iface_up) {
1368 char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
1369 sprintf(newiface, "%s=%s", iface, liface);
1370 if (iface_state == NULL) {
1371 state_list = llist_add_to_end(state_list, newiface);
1373 free(iface_state->data);
1374 iface_state->data = newiface;
1376 } else if (cmds == iface_down) {
1377 /* Remove an interface from the linked list */
1379 /* This needs to be done better */
1380 free(iface_state->data);
1381 free(iface_state->link);
1382 if (iface_state->link) {
1383 iface_state->data = iface_state->link->data;
1384 iface_state->link = iface_state->link->link;
1386 iface_state->data = NULL;
1387 iface_state->link = NULL;
1394 /* Actually write the new state */
1396 FILE *state_fp = NULL;
1398 state_fp = bb_xfopen(statefile, "a+");
1400 if (ftruncate(fileno(state_fp), 0) < 0) {
1401 bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1406 while (state_list) {
1407 if (state_list->data) {
1408 fputs(state_list->data, state_fp);
1409 fputc('\n', state_fp);
1411 state_list = state_list->link;