3 * Copyright (c) 2002 Glenn McGrath <bug1@optushome.com.au>
5 * Based on ifupdown v 0.6.4 by Anthony Towns
6 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
8 * Changes to upstream version
9 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
10 * Lines in the interfaces file cannot wrap.
11 * To adhere to the FHS, the default state file is /var/run/ifstate.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 #include <sys/utsname.h>
45 #define MAX_OPT_DEPTH 10
46 #define EUNBALBRACK 10001
47 #define EUNDEFVAR 10002
48 #define EUNBALPER 10000
50 typedef struct interface_defn_s interface_defn_t;
52 typedef int (execfn)(char *command);
53 typedef int (command_set)(interface_defn_t *ifd, execfn *e);
55 typedef struct method_s {
61 typedef struct address_family_s {
67 typedef struct mapping_defn_s {
68 struct mapping_defn_s *next;
81 typedef struct variable_s {
86 struct interface_defn_s {
87 struct interface_defn_s *next;
90 address_family_t *address_family;
100 typedef struct interfaces_file_s {
101 llist_t *autointerfaces;
102 interface_defn_t *ifaces;
103 mapping_defn_t *mappings;
106 static char no_act = 0;
107 static char verbose = 0;
108 static char **environ = NULL;
110 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
111 static int count_netmask_bits(char *dotted_quad)
113 unsigned int a, b, c, d;
114 unsigned int res, result;
115 /* Found a netmask... Check if it is dotted quad */
116 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
118 res = (a & 0x55) + ((a >> 1) & 0x55);
119 res = (res & 0x33) + ((res >> 2) & 0x33);
120 result = (res & 0x0F) + ((res >> 4) & 0x0F);
122 res = (b & 0x55) + ((b >> 1) & 0x55);
123 res = (res & 0x33) + ((res >> 2) & 0x33);
124 result += (res & 0x0F) + ((res >> 4) & 0x0F);
126 res = (c & 0x55) + ((c >> 1) & 0x55);
127 res = (res & 0x33) + ((res >> 2) & 0x33);
128 result += (res & 0x0F) + ((res >> 4) & 0x0F);
130 res = (d & 0x55) + ((d >> 1) & 0x55);
131 res = (res & 0x33) + ((res >> 2) & 0x33);
132 result += (res & 0x0F) + ((res >> 4) & 0x0F);
133 return ((int)result);
137 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
139 if (*pos + str_length >= *len) {
142 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
144 *len = *len * 2 + str_length + 1;
147 while (str_length-- >= 1) {
148 (*buf)[(*pos)++] = *str;
154 static int strncmpz(char *l, char *r, size_t llen)
156 int i = strncmp(l, r, llen);
165 static char *get_var(char *id, size_t idlen, interface_defn_t *ifd)
169 if (strncmpz(id, "iface", 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);
182 static char *parse(char *command, interface_defn_t *ifd)
186 size_t pos = 0, len = 0;
187 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
188 int okay[MAX_OPT_DEPTH] = { 1 };
195 addstr(&result, &len, &pos, command, 1);
200 addstr(&result, &len, &pos, command + 1, 1);
203 addstr(&result, &len, &pos, command, 1);
208 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
209 old_pos[opt_depth] = pos;
214 addstr(&result, &len, &pos, "[", 1);
219 if (command[1] == ']' && opt_depth > 1) {
221 if (!okay[opt_depth]) {
222 pos = old_pos[opt_depth];
227 addstr(&result, &len, &pos, "]", 1);
237 nextpercent = strchr(command, '%');
244 varvalue = get_var(command, nextpercent - command, ifd);
247 addstr(&result, &len, &pos, varvalue, xstrlen(varvalue));
249 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
250 /* Sigh... Add a special case for 'ip' to convert from
251 * dotted quad to bit count style netmasks. */
252 if (strncmp(command, "bnmask", 6)==0) {
254 varvalue = get_var("netmask", 7, ifd);
255 if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
257 sprintf(argument, "%d", res);
258 addstr(&result, &len, &pos, argument, xstrlen(argument));
259 command = nextpercent + 1;
264 okay[opt_depth - 1] = 0;
267 command = nextpercent + 1;
288 static int execute(char *command, interface_defn_t *ifd, execfn *exec)
293 out = parse(command, ifd);
304 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
305 static int static_up_ipx(interface_defn_t *ifd, execfn *exec)
307 if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) {
313 static int static_down_ipx(interface_defn_t *ifd, execfn *exec)
315 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
321 static int dynamic_up(interface_defn_t *ifd, execfn *exec)
323 if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) {
329 static int dynamic_down(interface_defn_t *ifd, execfn *exec)
331 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
337 static method_t methods_ipx[] = {
338 { "dynamic", dynamic_up, dynamic_down, },
339 { "static", static_up_ipx, static_down_ipx, },
342 address_family_t addr_ipx = {
344 sizeof(methods_ipx) / sizeof(method_t),
347 #endif /* IFUP_FEATURE_IPX */
349 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
350 static int loopback_up6(interface_defn_t *ifd, execfn *exec)
352 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
353 if (!execute("ip link set %iface% up", ifd, exec))
355 if (!execute("ip addr add ::1 dev %iface%", ifd, exec))
358 if (!execute("ifconfig %iface% add ::1", ifd, exec))
364 static int loopback_down6(interface_defn_t *ifd, execfn *exec)
366 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
367 if (!execute("ip link set %iface% down", ifd, exec))
370 if (!execute("ifconfig %iface% del ::1", ifd, exec))
376 static int static_up6(interface_defn_t *ifd, execfn *exec)
378 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
379 if (!execute("ip link set %iface% up", ifd, exec))
381 if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec))
383 if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec))
386 if (!execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec)) {
389 if (!execute("ifconfig %iface% add %address%/%netmask%", ifd, exec)) {
392 if (!execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec)) {
399 static int static_down6(interface_defn_t *ifd, execfn *exec)
401 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
402 if (!execute("ip link set %iface% down", ifd, exec))
405 if (!execute("ifconfig %iface% down", ifd, exec)) {
412 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
413 static int v4tunnel_up(interface_defn_t *ifd, execfn *exec)
415 if (!execute("ip tunnel add %iface% mode sit remote %endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec)) {
418 if (!execute("ip link set %iface% up", ifd, exec)) {
421 if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) {
424 if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) {
430 static int v4tunnel_down(interface_defn_t * ifd, execfn * exec)
432 if (!execute("ip tunnel del %iface%", ifd, exec)) {
439 static method_t methods6[] = {
440 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
441 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
443 { "static", static_up6, static_down6, },
444 { "loopback", loopback_up6, loopback_down6, },
447 address_family_t addr_inet6 = {
449 sizeof(methods6) / sizeof(method_t),
452 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
454 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
455 static int loopback_up(interface_defn_t *ifd, execfn *exec)
457 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
458 if (!execute("ip link set %iface% up", ifd, exec))
460 if (!execute("ip addr add 127.0.0.1 dev %iface%", ifd, exec))
463 if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) {
470 static int loopback_down(interface_defn_t *ifd, execfn *exec)
472 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
473 if (!execute("ip addr flush dev %iface%", ifd, exec))
475 if (!execute("ip link set %iface% down", ifd, exec))
478 if (!execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)) {
485 static int static_up(interface_defn_t *ifd, execfn *exec)
487 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
488 if (!execute("ip link set %iface% up", ifd, exec))
490 if (!execute("ip addr add %address%/%bnmask% dev %iface%", ifd, exec))
492 if (!execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec))
495 if (!execute("ifconfig %iface% %address% netmask %netmask% [[broadcast %broadcast%]] [[pointopoint %pointopoint%]] [[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
499 if (!execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec)) {
506 static int static_down(interface_defn_t *ifd, execfn *exec)
508 #ifdef CONFIG_FEATURE_IFUPDOWN_IP
509 //if (!execute("[[ ip route del default via %gateway% dev %iface% ]]", ifd, exec))
511 if (!execute("ip addr flush dev %iface%", ifd, exec))
513 if (!execute("ip link set %iface% down", ifd, exec))
516 if (!execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec)) {
519 if (!execute("ifconfig %iface% down", ifd, exec)) {
526 static int execable(char *program)
529 if (0 == stat(program, &buf)) {
530 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
537 static int dhcp_up(interface_defn_t *ifd, execfn *exec)
539 if (execable("/sbin/dhclient")) {
540 if (!execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)) {
543 } else if (execable("/sbin/pump")) {
544 if (!execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)) {
547 } else if (execable("/sbin/udhcpc")) {
548 if (!execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)) {
551 } else if (execable("/sbin/dhcpcd")) {
552 if (!execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%", ifd, exec)) {
559 static int dhcp_down(interface_defn_t *ifd, execfn *exec)
561 if (execable("/sbin/dhclient")) {
562 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
565 } else if (execable("/sbin/pump")) {
566 if (!execute("pump -i %iface% -k", ifd, exec)) {
569 } else if (execable("/sbin/udhcpc")) {
570 if (!execute("kill -9 `cat /var/run/udhcpc.%iface%.pid`", ifd, exec)) {
573 } else if (execable("/sbin/dhcpcd")) {
574 if (!execute("dhcpcd -k %iface%", ifd, exec)) {
578 if (!execute("ifconfig %iface% down", ifd, exec)) {
584 static int bootp_up(interface_defn_t *ifd, execfn *exec)
586 if (!execute("bootpc [[--bootfile %bootfile%]] --dev %iface% [[--server %server%]] [[--hwaddr %hwaddr%]] --returniffail --serverbcast", ifd, exec)) {
592 static int bootp_down(interface_defn_t *ifd, execfn *exec)
594 if (!execute("ifconfig down %iface%", ifd, exec)) {
600 static int ppp_up(interface_defn_t *ifd, execfn *exec)
602 if (!execute("pon [[%provider%]]", ifd, exec)) {
608 static int ppp_down(interface_defn_t *ifd, execfn *exec)
610 if (!execute("poff [[%provider%]]", ifd, exec)) {
616 static int wvdial_up(interface_defn_t *ifd, execfn *exec)
618 if (!execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)) {
624 static int wvdial_down(interface_defn_t *ifd, execfn *exec)
626 if (!execute ("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -s 2", ifd, exec)) {
632 static method_t methods[] = {
633 { "wvdial", wvdial_up, wvdial_down, },
634 { "ppp", ppp_up, ppp_down, },
635 { "static", static_up, static_down, },
636 { "bootp", bootp_up, bootp_down, },
637 { "dhcp", dhcp_up, dhcp_down, },
638 { "loopback", loopback_up, loopback_down, },
641 address_family_t addr_inet = {
643 sizeof(methods) / sizeof(method_t),
647 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
649 static char *next_word(char **buf)
651 unsigned short length;
654 if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
658 /* Skip over leading whitespace */
659 word = *buf + strspn(*buf, " \t\n");
661 /* Find the length of this word */
662 length = strcspn(word, " \t\n");
666 *buf = word + length;
673 static address_family_t *get_address_family(address_family_t *af[], char *name)
677 for (i = 0; af[i]; i++) {
678 if (strcmp(af[i]->name, name) == 0) {
685 static method_t *get_method(address_family_t *af, char *name)
689 for (i = 0; i < af->n_methods; i++) {
690 if (strcmp(af->method[i].name, name) == 0) {
691 return &af->method[i];
697 static int duplicate_if(interface_defn_t *ifa, interface_defn_t *ifb)
699 if (strcmp(ifa->iface, ifb->iface) != 0) {
702 if (ifa->address_family != ifb->address_family) {
708 static const llist_t *find_list_string(const llist_t *list, const char *string)
711 if (strcmp(list->data, string) == 0) {
719 static interfaces_file_t *read_interfaces(char *filename)
721 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
722 mapping_defn_t *currmap = NULL;
724 interface_defn_t *currif = NULL;
725 interfaces_file_t *defn;
730 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
732 defn = xmalloc(sizeof(interfaces_file_t));
733 defn->autointerfaces = NULL;
734 defn->mappings = NULL;
737 f = xfopen(filename, "r");
739 while ((buf = get_line_from_file(f)) != NULL) {
742 /* Ignore comments */
747 firstword = next_word(&buf_ptr);
748 if (firstword == NULL) {
749 continue; /* blank line */
752 if (strcmp(firstword, "mapping") == 0) {
753 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
754 currmap = xmalloc(sizeof(mapping_defn_t));
755 currmap->max_matches = 0;
756 currmap->n_matches = 0;
757 currmap->match = NULL;
759 while ((firstword = next_word(&buf_ptr)) != NULL) {
760 if (currmap->max_matches == currmap->n_matches) {
761 currmap->max_matches = currmap->max_matches * 2 + 1;
762 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
765 currmap->match[currmap->n_matches++] = xstrdup(firstword);
767 currmap->max_mappings = 0;
768 currmap->n_mappings = 0;
769 currmap->mapping = NULL;
770 currmap->script = NULL;
772 mapping_defn_t **where = &defn->mappings;
773 while (*where != NULL) {
774 where = &(*where)->next;
777 currmap->next = NULL;
780 currently_processing = MAPPING;
781 } else if (strcmp(firstword, "iface") == 0) {
784 char *address_family_name;
786 address_family_t *addr_fams[] = {
787 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
790 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
793 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
799 currif = xmalloc(sizeof(interface_defn_t));
800 iface_name = next_word(&buf_ptr);
801 address_family_name = next_word(&buf_ptr);
802 method_name = next_word(&buf_ptr);
804 if (buf_ptr == NULL) {
805 error_msg("too few parameters for line \"%s\"", buf);
809 if (buf_ptr[0] != '\0') {
810 error_msg("too many parameters \"%s\"", buf);
814 currif->iface = xstrdup(iface_name);
816 currif->address_family = get_address_family(addr_fams, address_family_name);
817 if (!currif->address_family) {
818 error_msg("unknown address type \"%s\"", buf);
822 currif->method = get_method(currif->address_family, method_name);
823 if (!currif->method) {
824 error_msg("unknown method \"%s\"", buf);
828 currif->automatic = 1;
829 currif->max_options = 0;
830 currif->n_options = 0;
831 currif->option = NULL;
835 interface_defn_t **where = &defn->ifaces;
837 while (*where != NULL) {
838 if (duplicate_if(*where, currif)) {
839 error_msg("duplicate interface \"%s\"", buf);
842 where = &(*where)->next;
849 currently_processing = IFACE;
850 } else if (strcmp(firstword, "auto") == 0) {
851 while ((firstword = next_word(&buf_ptr)) != NULL) {
853 /* Check the interface isnt already listed */
854 if (find_list_string(defn->autointerfaces, firstword)) {
855 perror_msg_and_die("interface declared auto twice \"%s\"", buf);
858 /* Add the interface to the list */
859 defn->autointerfaces = llist_add_to(defn->autointerfaces, strdup(firstword));
861 currently_processing = NONE;
863 switch (currently_processing) {
868 if (xstrlen(buf_ptr) == 0) {
869 error_msg("option with empty value \"%s\"", buf);
873 if (strcmp(firstword, "up") != 0
874 && strcmp(firstword, "down") != 0
875 && strcmp(firstword, "pre-up") != 0
876 && strcmp(firstword, "post-down") != 0) {
877 for (i = 0; i < currif->n_options; i++) {
878 if (strcmp(currif->option[i].name, firstword) == 0) {
879 error_msg("duplicate option \"%s\"", buf);
885 if (currif->n_options >= currif->max_options) {
888 currif->max_options = currif->max_options + 10;
889 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
890 currif->option = opt;
892 currif->option[currif->n_options].name = xstrdup(firstword);
893 currif->option[currif->n_options].value = xstrdup(next_word(&buf_ptr));
894 if (!currif->option[currif->n_options].name) {
898 if (!currif->option[currif->n_options].value) {
905 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
906 if (strcmp(firstword, "script") == 0) {
907 if (currmap->script != NULL) {
908 error_msg("duplicate script in mapping \"%s\"", buf);
911 currmap->script = xstrdup(next_word(&buf_ptr));
913 } else if (strcmp(firstword, "map") == 0) {
914 if (currmap->max_mappings == currmap->n_mappings) {
915 currmap->max_mappings = currmap->max_mappings * 2 + 1;
916 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
918 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
919 currmap->n_mappings++;
921 error_msg("misplaced option \"%s\"", buf);
928 error_msg("misplaced option \"%s\"", buf);
934 if (ferror(f) != 0) {
935 perror_msg_and_die("%s", filename);
942 static char *setlocalenv(char *format, char *name, char *value)
948 result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1);
950 sprintf(result, format, name, value);
952 for (here = there = result; *there != '=' && *there; there++) {
956 *there = toupper(*there);
958 if (isalnum(*there) || *there == '_') {
963 memmove(here, there, xstrlen(there) + 1);
968 static void set_environ(interface_defn_t *iface, char *mode)
972 const int n_env_entries = iface->n_options + 5;
975 if (environ != NULL) {
976 for (ppch = environ; *ppch; ppch++) {
983 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
984 environend = environ;
987 for (i = 0; i < iface->n_options; i++) {
988 if (strcmp(iface->option[i].name, "up") == 0
989 || strcmp(iface->option[i].name, "down") == 0
990 || strcmp(iface->option[i].name, "pre-up") == 0
991 || strcmp(iface->option[i].name, "post-down") == 0) {
994 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
998 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
1000 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
1002 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
1004 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
1006 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
1010 static int doit(char *str)
1012 if (verbose || no_act) {
1013 error_msg("%s", str);
1020 switch (child = fork()) {
1021 case -1: /* failure */
1024 execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
1027 waitpid(child, &status, 0);
1028 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1035 static int execute_all(interface_defn_t *ifd, execfn *exec, const char *opt)
1040 for (i = 0; i < ifd->n_options; i++) {
1041 if (strcmp(ifd->option[i].name, opt) == 0) {
1042 if (!(*exec) (ifd->option[i].value)) {
1048 buf = xmalloc(xstrlen(opt) + 19);
1049 sprintf(buf, "/etc/network/if-%s.d", opt);
1055 static int iface_up(interface_defn_t *iface)
1057 set_environ(iface, "start");
1058 if (!execute_all(iface, doit, "pre-up")) {
1061 if (!iface->method->up(iface, doit)) {
1064 if (!execute_all(iface, doit, "up")) {
1071 static int iface_down(interface_defn_t *iface)
1073 set_environ(iface, "stop");
1074 if (!execute_all(iface, doit, "down")) {
1077 if (!iface->method->down(iface, doit)) {
1080 if (!execute_all(iface, doit, "post-down")) {
1086 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1087 static int popen2(FILE **in, FILE **out, char *command, ...)
1090 char *argv[11] = { command };
1092 int infd[2], outfd[2];
1096 va_start(ap, command);
1097 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1100 argv[argc] = NULL; /* make sure */
1103 if (pipe(infd) != 0) {
1107 if (pipe(outfd) != 0) {
1114 switch (pid = fork()) {
1115 case -1: /* failure */
1128 execvp(command, argv);
1130 default: /* parent */
1131 *in = fdopen(infd[1], "w");
1132 *out = fdopen(outfd[0], "r");
1140 static int run_mapping(char *physical, char *logical, int len, mapping_defn_t * map)
1147 pid = popen2(&in, &out, map->script, physical, NULL);
1151 for (i = 0; i < map->n_mappings; i++) {
1152 fprintf(in, "%s\n", map->mapping[i]);
1155 waitpid(pid, &status, 0);
1156 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1157 if (fgets(logical, len, out)) {
1158 char *pch = logical + xstrlen(logical) - 1;
1160 while (pch >= logical && isspace(*pch))
1168 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
1170 static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1172 unsigned short iface_len = xstrlen(iface);
1173 llist_t *search = state_list;
1176 if ((strncmp(search->data, iface, iface_len) == 0) &&
1177 (search->data[iface_len] == '=')) {
1180 search = search->link;
1185 extern int ifupdown_main(int argc, char **argv)
1187 int (*cmds) (interface_defn_t *) = NULL;
1188 interfaces_file_t *defn;
1189 FILE *state_fp = NULL;
1190 llist_t *state_list = NULL;
1191 llist_t *target_list = NULL;
1192 char *interfaces = "/etc/network/interfaces";
1193 const char *statefile = "/var/run/ifstate";
1195 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1196 int run_mappings = 1;
1202 if (applet_name[2] == 'u') {
1206 /* ifdown command */
1210 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1211 while ((i = getopt(argc, argv, "i:hvnamf")) != -1) {
1213 while ((i = getopt(argc, argv, "i:hvnaf")) != -1) {
1216 case 'i': /* interfaces */
1217 interfaces = xstrdup(optarg);
1219 case 'v': /* verbose */
1225 case 'n': /* no-act */
1228 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1229 case 'm': /* no-mappings */
1233 case 'f': /* force */
1242 if (argc - optind > 0) {
1252 defn = read_interfaces(interfaces);
1255 state_fp = fopen(statefile, "r");
1258 /* Create a list of interfaces to work on */
1260 if (cmds == iface_up) {
1261 target_list = defn->autointerfaces;
1262 } else if (cmds == iface_down) {
1263 const llist_t *list = state_list;
1265 target_list = llist_add_to(target_list, strdup(list->data));
1268 target_list = defn->autointerfaces;
1271 target_list = llist_add_to(target_list, argv[optind]);
1275 /* Update the interfaces */
1276 while (target_list) {
1277 interface_defn_t *currif;
1283 iface = strdup(target_list->data);
1284 target_list = target_list->link;
1286 pch = strchr(iface, '=');
1289 liface = strdup(pch + 1);
1291 liface = strdup(iface);
1295 const llist_t *iface_state = find_iface_state(state_list, iface);
1297 if (cmds == iface_up) {
1300 error_msg("interface %s already configured", iface);
1306 error_msg("interface %s not configured", iface);
1312 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1313 if ((cmds == iface_up) && run_mappings) {
1314 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 error_msg("Running mapping script %s on %s", currmap->script, liface);
1324 run_mapping(iface, liface, sizeof(liface), currmap);
1331 for (currif = defn->ifaces; currif; currif = currif->next) {
1332 if (strcmp(liface, currif->iface) == 0) {
1333 char *oldiface = currif->iface;
1336 currif->iface = iface;
1339 error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1342 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1343 if (cmds(currif) == -1) {
1345 ("Don't seem to be have all the variables for %s/%s.\n",
1346 liface, currif->address_family->name);
1349 currif->iface = oldiface;
1353 if (!okay && !force) {
1354 error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1356 llist_t *iface_state = find_iface_state(state_list, iface);
1358 if (cmds == iface_up) {
1359 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1);
1360 sprintf(newiface, "%s=%s", iface, liface);
1361 if (iface_state == NULL) {
1362 state_list = llist_add_to(state_list, newiface);
1364 free(iface_state->data);
1365 iface_state->data = newiface;
1367 } else if (cmds == iface_down) {
1368 /* Remove an interface from the linked list */
1370 /* This needs to be done better */
1371 free(iface_state->data);
1372 free(iface_state->link);
1373 if (iface_state->link) {
1374 iface_state->data = iface_state->link->data;
1375 iface_state->link = iface_state->link->link;
1377 iface_state->data = NULL;
1378 iface_state->link = NULL;
1385 /* Actually write the new state */
1390 state_fp = xfopen(statefile, "a+");
1392 if (ftruncate(fileno(state_fp), 0) < 0) {
1393 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1398 while (state_list) {
1399 if (state_list->data) {
1400 fputs(state_list->data, state_fp);
1401 fputc('\n', state_fp);
1403 state_list = state_list->link;
1409 if (state_fp != NULL) {