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 #include <sys/utsname.h>
24 #define MAX_OPT_DEPTH 10
25 #define EUNBALBRACK 10001
26 #define EUNDEFVAR 10002
27 #define EUNBALPER 10000
29 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
30 #define MAX_INTERFACE_LENGTH 10
33 #define debug_noise(args...) /*fprintf(stderr, args)*/
35 /* Forward declaration */
36 struct interface_defn_t;
38 typedef int execfn(char *command);
42 int (*up)(struct interface_defn_t *ifd, execfn *e);
43 int (*down)(struct interface_defn_t *ifd, execfn *e);
46 struct address_family_t {
49 const struct method_t *method;
52 struct mapping_defn_t {
53 struct mapping_defn_t *next;
71 struct interface_defn_t {
72 const struct address_family_t *address_family;
73 const struct method_t *method;
78 struct variable_t *option;
81 struct interfaces_file_t {
82 llist_t *autointerfaces;
84 struct mapping_defn_t *mappings;
87 #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
93 OPT_no_mappings = 0x10,
95 #define DO_ALL (option_mask32 & OPT_do_all)
96 #define NO_ACT (option_mask32 & OPT_no_act)
97 #define VERBOSE (option_mask32 & OPT_verbose)
98 #define FORCE (option_mask32 & OPT_force)
99 #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
101 static char **my_environ;
103 static const char *startup_PATH;
105 #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
107 #if ENABLE_FEATURE_IFUPDOWN_IP
109 static unsigned count_bits(unsigned a)
112 result = (a & 0x55) + ((a >> 1) & 0x55);
113 result = (result & 0x33) + ((result >> 2) & 0x33);
114 return (result & 0x0F) + ((result >> 4) & 0x0F);
117 static int count_netmask_bits(char *dotted_quad)
119 unsigned result, a, b, c, d;
120 /* Found a netmask... Check if it is dotted quad */
121 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
123 // FIXME: will be confused by e.g. 255.0.255.0
124 result = count_bits(a);
125 result += count_bits(b);
126 result += count_bits(c);
127 result += count_bits(d);
132 static void addstr(char **bufp, const char *str, size_t str_length)
134 /* xasprintf trick will be smaller, but we are often
135 * called with str_length == 1 - don't want to have
136 * THAT much of malloc/freeing! */
138 int len = (buf ? strlen(buf) : 0);
140 buf = xrealloc(buf, len + str_length);
141 /* copies at most str_length-1 chars! */
142 safe_strncpy(buf + len, str, str_length);
146 static int strncmpz(const char *l, const char *r, size_t llen)
148 int i = strncmp(l, r, llen);
155 static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
159 if (strncmpz(id, "iface", idlen) == 0) {
161 static char label_buf[20];
162 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
163 result = strchr(label_buf, ':');
169 if (strncmpz(id, "label", idlen) == 0) {
172 for (i = 0; i < ifd->n_options; i++) {
173 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
174 return ifd->option[i].value;
180 static char *parse(const char *command, struct interface_defn_t *ifd)
182 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
183 int okay[MAX_OPT_DEPTH] = { 1 };
190 addstr(&result, command, 1);
195 addstr(&result, command + 1, 1);
198 addstr(&result, command, 1);
203 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
204 old_pos[opt_depth] = result ? strlen(result) : 0;
209 addstr(&result, "[", 1);
214 if (command[1] == ']' && opt_depth > 1) {
216 if (!okay[opt_depth]) {
217 result[old_pos[opt_depth]] = '\0';
221 addstr(&result, "]", 1);
231 nextpercent = strchr(command, '%');
238 varvalue = get_var(command, nextpercent - command, ifd);
241 addstr(&result, varvalue, strlen(varvalue));
243 #if ENABLE_FEATURE_IFUPDOWN_IP
244 /* Sigh... Add a special case for 'ip' to convert from
245 * dotted quad to bit count style netmasks. */
246 if (strncmp(command, "bnmask", 6) == 0) {
248 varvalue = get_var("netmask", 7, ifd);
249 if (varvalue && (res = count_netmask_bits(varvalue)) > 0) {
250 const char *argument = utoa(res);
251 addstr(&result, argument, strlen(argument));
252 command = nextpercent + 1;
257 okay[opt_depth - 1] = 0;
260 command = nextpercent + 1;
281 /* execute() returns 1 for success and 0 for failure */
282 static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
287 out = parse(command, ifd);
292 /* out == "": parsed ok but not all needed variables known, skip */
293 ret = out[0] ? (*exec)(out) : 1;
303 #if ENABLE_FEATURE_IFUPDOWN_IPV6
304 static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
306 #if ENABLE_FEATURE_IFUPDOWN_IP
308 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
309 result += execute("ip link set %iface% up", ifd, exec);
310 return ((result == 2) ? 2 : 0);
312 return execute("ifconfig %iface% add ::1", ifd, exec);
316 static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
318 #if ENABLE_FEATURE_IFUPDOWN_IP
319 return execute("ip link set %iface% down", ifd, exec);
321 return execute("ifconfig %iface% del ::1", ifd, exec);
325 static int static_up6(struct interface_defn_t *ifd, execfn *exec)
328 #if ENABLE_FEATURE_IFUPDOWN_IP
329 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
330 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
331 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
332 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
334 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
335 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
336 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
338 return ((result == 3) ? 3 : 0);
341 static int static_down6(struct interface_defn_t *ifd, execfn *exec)
343 #if ENABLE_FEATURE_IFUPDOWN_IP
344 return execute("ip link set %iface% down", ifd, exec);
346 return execute("ifconfig %iface% down", ifd, exec);
350 #if ENABLE_FEATURE_IFUPDOWN_IP
351 static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
354 result = execute("ip tunnel add %iface% mode sit remote "
355 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
356 result += execute("ip link set %iface% up", ifd, exec);
357 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
358 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
359 return ((result == 4) ? 4 : 0);
362 static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
364 return execute("ip tunnel del %iface%", ifd, exec);
368 static const struct method_t methods6[] = {
369 #if ENABLE_FEATURE_IFUPDOWN_IP
370 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
372 { "static", static_up6, static_down6, },
373 { "loopback", loopback_up6, loopback_down6, },
376 static const struct address_family_t addr_inet6 = {
378 ARRAY_SIZE(methods6),
381 #endif /* FEATURE_IFUPDOWN_IPV6 */
383 #if ENABLE_FEATURE_IFUPDOWN_IPV4
384 static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
386 #if ENABLE_FEATURE_IFUPDOWN_IP
388 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
389 result += execute("ip link set %iface% up", ifd, exec);
390 return ((result == 2) ? 2 : 0);
392 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
396 static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
398 #if ENABLE_FEATURE_IFUPDOWN_IP
400 result = execute("ip addr flush dev %iface%", ifd, exec);
401 result += execute("ip link set %iface% down", ifd, exec);
402 return ((result == 2) ? 2 : 0);
404 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
408 static int static_up(struct interface_defn_t *ifd, execfn *exec)
411 #if ENABLE_FEATURE_IFUPDOWN_IP
412 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
413 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
414 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
415 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
416 return ((result == 3) ? 3 : 0);
418 /* ifconfig said to set iface up before it processes hw %hwaddress%,
419 * which then of course fails. Thus we run two separate ifconfig */
420 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
422 result += execute("ifconfig %iface% %address% netmask %netmask%"
423 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
425 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
426 return ((result == 3) ? 3 : 0);
430 static int static_down(struct interface_defn_t *ifd, execfn *exec)
433 #if ENABLE_FEATURE_IFUPDOWN_IP
434 result = execute("ip addr flush dev %iface%", ifd, exec);
435 result += execute("ip link set %iface% down", ifd, exec);
437 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
438 result += execute("ifconfig %iface% down", ifd, exec);
440 return ((result == 2) ? 2 : 0);
443 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
447 const char *startcmd;
451 static const struct dhcp_client_t ext_dhcp_clients[] = {
453 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
457 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
458 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
461 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
462 "pump -i %iface% -k",
465 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
466 "kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
469 #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
471 static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
473 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
475 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
476 if (exists_execable(ext_dhcp_clients[i].name))
477 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
479 bb_error_msg("no dhcp clients found");
481 #elif ENABLE_APP_UDHCPC
482 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
483 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
486 return 0; /* no dhcp support */
490 static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
492 #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
494 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
495 if (exists_execable(ext_dhcp_clients[i].name))
496 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
498 bb_error_msg("no dhcp clients found, using static interface shutdown");
499 return static_down(ifd, exec);
500 #elif ENABLE_APP_UDHCPC
501 return execute("kill -TERM "
502 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
504 return 0; /* no support for dhcp */
508 static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
513 static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
515 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
516 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
517 "--returniffail --serverbcast", ifd, exec);
520 static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
522 return execute("pon[[ %provider%]]", ifd, exec);
525 static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
527 return execute("poff[[ %provider%]]", ifd, exec);
530 static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
532 return execute("start-stop-daemon --start -x wvdial "
533 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
536 static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
538 return execute("start-stop-daemon --stop -x wvdial "
539 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
542 static const struct method_t methods[] = {
543 { "manual", manual_up_down, manual_up_down, },
544 { "wvdial", wvdial_up, wvdial_down, },
545 { "ppp", ppp_up, ppp_down, },
546 { "static", static_up, static_down, },
547 { "bootp", bootp_up, static_down, },
548 { "dhcp", dhcp_up, dhcp_down, },
549 { "loopback", loopback_up, loopback_down, },
552 static const struct address_family_t addr_inet = {
558 #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
560 static char *next_word(char **buf)
562 unsigned short length;
565 if (!buf || !*buf || !**buf) {
569 /* Skip over leading whitespace */
570 word = skip_whitespace(*buf);
572 /* Skip over comments */
577 /* Find the length of this word */
578 length = strcspn(word, " \t\n");
582 *buf = word + length;
583 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
592 static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
599 for (i = 0; af[i]; i++) {
600 if (strcmp(af[i]->name, name) == 0) {
607 static const struct method_t *get_method(const struct address_family_t *af, char *name)
614 for (i = 0; i < af->n_methods; i++) {
615 if (strcmp(af->method[i].name, name) == 0) {
616 return &af->method[i];
622 static const llist_t *find_list_string(const llist_t *list, const char *string)
628 if (strcmp(list->data, string) == 0) {
636 static struct interfaces_file_t *read_interfaces(const char *filename)
638 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
639 struct mapping_defn_t *currmap = NULL;
641 struct interface_defn_t *currif = NULL;
642 struct interfaces_file_t *defn;
647 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
649 defn = xzalloc(sizeof(struct interfaces_file_t));
651 f = xfopen(filename, "r");
653 while ((buf = xmalloc_getline(f)) != NULL) {
656 firstword = next_word(&buf_ptr);
657 if (firstword == NULL) {
659 continue; /* blank line */
662 if (strcmp(firstword, "mapping") == 0) {
663 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
664 currmap = xzalloc(sizeof(struct mapping_defn_t));
666 while ((firstword = next_word(&buf_ptr)) != NULL) {
667 if (currmap->max_matches == currmap->n_matches) {
668 currmap->max_matches = currmap->max_matches * 2 + 1;
669 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
672 currmap->match[currmap->n_matches++] = xstrdup(firstword);
674 currmap->max_mappings = 0;
675 currmap->n_mappings = 0;
676 currmap->mapping = NULL;
677 currmap->script = NULL;
679 struct mapping_defn_t **where = &defn->mappings;
680 while (*where != NULL) {
681 where = &(*where)->next;
684 currmap->next = NULL;
686 debug_noise("Added mapping\n");
688 currently_processing = MAPPING;
689 } else if (strcmp(firstword, "iface") == 0) {
690 static const struct address_family_t *const addr_fams[] = {
691 #if ENABLE_FEATURE_IFUPDOWN_IPV4
694 #if ENABLE_FEATURE_IFUPDOWN_IPV6
701 char *address_family_name;
705 currif = xzalloc(sizeof(struct interface_defn_t));
706 iface_name = next_word(&buf_ptr);
707 address_family_name = next_word(&buf_ptr);
708 method_name = next_word(&buf_ptr);
710 if (buf_ptr == NULL) {
711 bb_error_msg("too few parameters for line \"%s\"", buf);
715 /* ship any trailing whitespace */
716 buf_ptr = skip_whitespace(buf_ptr);
718 if (buf_ptr[0] != '\0') {
719 bb_error_msg("too many parameters \"%s\"", buf);
723 currif->iface = xstrdup(iface_name);
725 currif->address_family = get_address_family(addr_fams, address_family_name);
726 if (!currif->address_family) {
727 bb_error_msg("unknown address type \"%s\"", address_family_name);
731 currif->method = get_method(currif->address_family, method_name);
732 if (!currif->method) {
733 bb_error_msg("unknown method \"%s\"", method_name);
737 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
738 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
739 if ((strcmp(tmp->iface, currif->iface) == 0) &&
740 (tmp->address_family == currif->address_family)) {
741 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
745 llist_add_to_end(&(defn->ifaces), (char*)currif);
747 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
748 currently_processing = IFACE;
749 } else if (strcmp(firstword, "auto") == 0) {
750 while ((firstword = next_word(&buf_ptr)) != NULL) {
752 /* Check the interface isnt already listed */
753 if (find_list_string(defn->autointerfaces, firstword)) {
754 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
757 /* Add the interface to the list */
758 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
759 debug_noise("\nauto %s\n", firstword);
761 currently_processing = NONE;
763 switch (currently_processing) {
768 if (strlen(buf_ptr) == 0) {
769 bb_error_msg("option with empty value \"%s\"", buf);
773 if (strcmp(firstword, "up") != 0
774 && strcmp(firstword, "down") != 0
775 && strcmp(firstword, "pre-up") != 0
776 && strcmp(firstword, "post-down") != 0) {
777 for (i = 0; i < currif->n_options; i++) {
778 if (strcmp(currif->option[i].name, firstword) == 0) {
779 bb_error_msg("duplicate option \"%s\"", buf);
785 if (currif->n_options >= currif->max_options) {
786 struct variable_t *opt;
788 currif->max_options = currif->max_options + 10;
789 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
790 currif->option = opt;
792 currif->option[currif->n_options].name = xstrdup(firstword);
793 currif->option[currif->n_options].value = xstrdup(buf_ptr);
794 if (!currif->option[currif->n_options].name) {
798 if (!currif->option[currif->n_options].value) {
802 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
803 currif->option[currif->n_options].value);
807 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
808 if (strcmp(firstword, "script") == 0) {
809 if (currmap->script != NULL) {
810 bb_error_msg("duplicate script in mapping \"%s\"", buf);
813 currmap->script = xstrdup(next_word(&buf_ptr));
815 } else if (strcmp(firstword, "map") == 0) {
816 if (currmap->max_mappings == currmap->n_mappings) {
817 currmap->max_mappings = currmap->max_mappings * 2 + 1;
818 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
820 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
821 currmap->n_mappings++;
823 bb_error_msg("misplaced option \"%s\"", buf);
830 bb_error_msg("misplaced option \"%s\"", buf);
836 if (ferror(f) != 0) {
837 bb_perror_msg_and_die("%s", filename);
844 static char *setlocalenv(const char *format, const char *name, const char *value)
850 result = xasprintf(format, name, value);
852 for (here = there = result; *there != '=' && *there; there++) {
856 *there = toupper(*there);
858 if (isalnum(*there) || *there == '_') {
863 memmove(here, there, strlen(there) + 1);
868 static void set_environ(struct interface_defn_t *iface, const char *mode)
872 const int n_env_entries = iface->n_options + 5;
875 if (my_environ != NULL) {
876 for (ppch = my_environ; *ppch; ppch++) {
882 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
883 environend = my_environ;
885 for (i = 0; i < iface->n_options; i++) {
886 if (strcmp(iface->option[i].name, "up") == 0
887 || strcmp(iface->option[i].name, "down") == 0
888 || strcmp(iface->option[i].name, "pre-up") == 0
889 || strcmp(iface->option[i].name, "post-down") == 0) {
892 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
895 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
896 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
897 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
898 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
899 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
902 static int doit(char *str)
904 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
907 if (!(option_mask32 & OPT_no_act)) {
914 case -1: /* failure */
917 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
920 waitpid(child, &status, 0);
921 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
928 static int execute_all(struct interface_defn_t *ifd, const char *opt)
932 for (i = 0; i < ifd->n_options; i++) {
933 if (strcmp(ifd->option[i].name, opt) == 0) {
934 if (!doit(ifd->option[i].value)) {
940 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
941 /* heh, we don't bother free'ing it */
945 static int check(char *str)
950 static int iface_up(struct interface_defn_t *iface)
952 if (!iface->method->up(iface, check)) return -1;
953 set_environ(iface, "start");
954 if (!execute_all(iface, "pre-up")) return 0;
955 if (!iface->method->up(iface, doit)) return 0;
956 if (!execute_all(iface, "up")) return 0;
960 static int iface_down(struct interface_defn_t *iface)
962 if (!iface->method->down(iface,check)) return -1;
963 set_environ(iface, "stop");
964 if (!execute_all(iface, "down")) return 0;
965 if (!iface->method->down(iface, doit)) return 0;
966 if (!execute_all(iface, "post-down")) return 0;
970 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
971 static int popen2(FILE **in, FILE **out, char *command, ...)
974 char *argv[11] = { command };
976 int infd[2], outfd[2];
980 va_start(ap, command);
981 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
984 argv[argc] = NULL; /* make sure */
987 if (pipe(infd) != 0) {
991 if (pipe(outfd) != 0) {
998 switch (pid = fork()) {
999 case -1: /* failure */
1012 BB_EXECVP(command, argv);
1014 default: /* parent */
1015 *in = fdopen(infd[1], "w");
1016 *out = fdopen(outfd[0], "r");
1024 static char *run_mapping(char *physical, struct mapping_defn_t * map)
1030 char *logical = xstrdup(physical);
1032 /* Run the mapping script. */
1033 pid = popen2(&in, &out, map->script, physical, NULL);
1035 /* popen2() returns 0 on failure. */
1039 /* Write mappings to stdin of mapping script. */
1040 for (i = 0; i < map->n_mappings; i++) {
1041 fprintf(in, "%s\n", map->mapping[i]);
1044 waitpid(pid, &status, 0);
1046 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1047 /* If the mapping script exited successfully, try to
1048 * grab a line of output and use that as the name of the
1049 * logical interface. */
1050 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
1052 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
1053 /* If we are able to read a line of output from the script,
1054 * remove any trailing whitespace and use this value
1055 * as the name of the logical interface. */
1056 char *pch = new_logical + strlen(new_logical) - 1;
1058 while (pch >= new_logical && isspace(*pch))
1062 logical = new_logical;
1064 /* If we are UNABLE to read a line of output, discard our
1065 * freshly allocated memory. */
1074 #endif /* FEATURE_IFUPDOWN_MAPPING */
1076 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1078 unsigned short iface_len = strlen(iface);
1079 llist_t *search = state_list;
1082 if ((strncmp(search->data, iface, iface_len) == 0)
1083 && (search->data[iface_len] == '=')) {
1086 search = search->link;
1091 /* read the previous state from the state file */
1092 static llist_t *read_iface_state(void)
1094 llist_t *state_list = NULL;
1095 FILE *state_fp = fopen("/var/run/ifstate", "r");
1098 char *start, *end_ptr;
1099 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1100 /* We should only need to check for a single character */
1101 end_ptr = start + strcspn(start, " \t\n");
1103 llist_add_to(&state_list, start);
1111 int ifupdown_main(int argc, char **argv);
1112 int ifupdown_main(int argc, char **argv)
1114 int (*cmds)(struct interface_defn_t *) = NULL;
1115 struct interfaces_file_t *defn;
1116 llist_t *target_list = NULL;
1117 const char *interfaces = "/etc/network/interfaces";
1118 bool any_failures = 0;
1121 if (applet_name[2] == 'u') {
1126 getopt32(argc, argv, OPTION_STR, &interfaces);
1127 if (argc - optind > 0) {
1128 if (DO_ALL) bb_show_usage();
1130 if (!DO_ALL) bb_show_usage();
1133 debug_noise("reading %s file:\n", interfaces);
1134 defn = read_interfaces(interfaces);
1135 debug_noise("\ndone reading %s\n\n", interfaces);
1138 return EXIT_FAILURE;
1141 startup_PATH = getenv("PATH");
1142 if (!startup_PATH) startup_PATH = "";
1144 /* Create a list of interfaces to work on */
1146 target_list = defn->autointerfaces;
1148 llist_add_to_end(&target_list, argv[optind]);
1151 /* Update the interfaces */
1152 while (target_list) {
1153 llist_t *iface_list;
1154 struct interface_defn_t *currif;
1161 iface = xstrdup(target_list->data);
1162 target_list = target_list->link;
1164 pch = strchr(iface, '=');
1167 liface = xstrdup(pch + 1);
1169 liface = xstrdup(iface);
1173 llist_t *state_list = read_iface_state();
1174 const llist_t *iface_state = find_iface_state(state_list, iface);
1176 if (cmds == iface_up) {
1179 bb_error_msg("interface %s already configured", iface);
1185 bb_error_msg("interface %s not configured", iface);
1189 llist_free(state_list, free);
1192 #if ENABLE_FEATURE_IFUPDOWN_MAPPING
1193 if ((cmds == iface_up) && !NO_MAPPINGS) {
1194 struct mapping_defn_t *currmap;
1196 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1198 for (i = 0; i < currmap->n_matches; i++) {
1199 if (fnmatch(currmap->match[i], liface, 0) != 0)
1202 printf("Running mapping script %s on %s\n", currmap->script, liface);
1204 liface = run_mapping(iface, currmap);
1211 iface_list = defn->ifaces;
1212 while (iface_list) {
1213 currif = (struct interface_defn_t *) iface_list->data;
1214 if (strcmp(liface, currif->iface) == 0) {
1215 char *oldiface = currif->iface;
1218 currif->iface = iface;
1220 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1222 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1223 cmds_ret = cmds(currif);
1224 if (cmds_ret == -1) {
1225 bb_error_msg("don't seem to have all the variables for %s/%s",
1226 liface, currif->address_family->name);
1228 } else if (cmds_ret == 0) {
1232 currif->iface = oldiface;
1234 iface_list = iface_list->link;
1240 if (!okay && !FORCE) {
1241 bb_error_msg("ignoring unknown interface %s", liface);
1243 } else if (!NO_ACT) {
1244 /* update the state file */
1247 llist_t *state_list = read_iface_state();
1248 llist_t *iface_state = find_iface_state(state_list, iface);
1250 if (cmds == iface_up) {
1251 char * const newiface = xasprintf("%s=%s", iface, liface);
1252 if (iface_state == NULL) {
1253 llist_add_to_end(&state_list, newiface);
1255 free(iface_state->data);
1256 iface_state->data = newiface;
1259 /* Remove an interface from state_list */
1260 llist_unlink(&state_list, iface_state);
1261 free(llist_pop(&iface_state));
1264 /* Actually write the new state */
1265 state_fp = xfopen("/var/run/ifstate", "w");
1269 fprintf(state_fp, "%s\n", state->data);
1271 state = state->link;
1274 llist_free(state_list, free);
1278 return any_failures;