3 * Based on ifupdown by Anthony Towns
4 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
6 * Remove checks for kernel version, assume kernel version 2.2.0 or better
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include <sys/utsname.h>
41 #define IFUPDOWN_VERSION "0.6.4"
43 typedef struct interface_defn interface_defn;
45 typedef int (execfn)(char *command);
46 typedef int (command_set)(interface_defn *ifd, execfn *e);
48 typedef struct method {
54 typedef struct address_family {
60 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
61 typedef struct mapping_defn {
62 struct mapping_defn *next;
76 typedef struct variable {
81 struct interface_defn {
82 struct interface_defn *next;
85 address_family *address_family;
95 typedef struct interfaces_file {
96 int max_autointerfaces;
98 char **autointerfaces;
100 interface_defn *ifaces;
101 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
102 mapping_defn *mappings;
106 #define MAX_OPT_DEPTH 10
107 #define EUNBALBRACK 10001
108 #define EUNDEFVAR 10002
109 #define MAX_VARNAME 32
110 #define EUNBALPER 10000
112 static int no_act = 0;
113 static int verbose = 0;
114 static char **environ = NULL;
116 static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
118 if (*pos + str_length >= *len) {
121 newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
123 *len = *len * 2 + str_length + 1;
126 while (str_length-- >= 1) {
127 (*buf)[(*pos)++] = *str;
133 static int strncmpz(char *l, char *r, size_t llen)
135 int i = strncmp(l, r, llen);
144 static char *get_var(char *id, size_t idlen, interface_defn *ifd)
148 if (strncmpz(id, "iface", idlen) == 0) {
151 for (i = 0; i < ifd->n_options; i++) {
152 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
153 return (ifd->option[i].value);
161 static char *parse(char *command, interface_defn *ifd)
165 size_t pos = 0, len = 0;
166 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
167 int okay[MAX_OPT_DEPTH] = { 1 };
174 addstr(&result, &len, &pos, command, 1);
179 addstr(&result, &len, &pos, command + 1, 1);
182 addstr(&result, &len, &pos, command, 1);
187 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
188 old_pos[opt_depth] = pos;
193 addstr(&result, &len, &pos, "[", 1);
198 if (command[1] == ']' && opt_depth > 1) {
200 if (!okay[opt_depth]) {
201 pos = old_pos[opt_depth];
206 addstr(&result, &len, &pos, "]", 1);
216 nextpercent = strchr(command, '%');
223 varvalue = get_var(command, nextpercent - command, ifd);
226 addstr(&result, &len, &pos, varvalue, xstrlen(varvalue));
228 okay[opt_depth - 1] = 0;
231 command = nextpercent + 1;
252 static int execute(char *command, interface_defn *ifd, execfn *exec)
257 out = parse(command, ifd);
268 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
269 static int static_up_ipx(interface_defn *ifd, execfn *exec)
271 if (!execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)) {
277 static int static_down_ipx(interface_defn *ifd, execfn *exec)
279 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
285 static int dynamic_up(interface_defn *ifd, execfn *exec)
287 if (!execute("ipx_interface add %iface% %frame%", ifd, exec)) {
293 static int dynamic_down(interface_defn *ifd, execfn *exec)
295 if (!execute("ipx_interface del %iface% %frame%", ifd, exec)) {
301 static method methods_ipx[] = {
302 { "dynamic", dynamic_up, dynamic_down, },
303 { "static", static_up_ipx, static_down_ipx, },
306 address_family addr_ipx = {
308 sizeof(methods_ipx) / sizeof(struct method),
311 #endif /* IFUP_FEATURE_IPX */
313 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
314 static int loopback_up6(interface_defn *ifd, execfn *exec)
316 if (!execute("ifconfig %iface% add ::1", ifd, exec)) {
322 static int loopback_down6(interface_defn *ifd, execfn *exec)
324 if (!execute("ifconfig %iface% del ::1", ifd, exec))
330 static int static_up6(interface_defn *ifd, execfn *exec)
332 if (!execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec)) {
335 if (!execute("ifconfig %iface% add %address%/%netmask%", ifd, exec)) {
338 if (!execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec)) {
344 static int static_down6(interface_defn *ifd, execfn *exec)
346 if (!execute("ifconfig %iface% down", ifd, exec)) {
352 static int v4tunnel_up(interface_defn *ifd, execfn *exec)
354 if (!execute("ip tunnel add %iface% mode sit remote %endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec)) {
357 if (!execute("ip link set %iface% up", ifd, exec)) {
360 if (!execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec)) {
363 if (!execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec)) {
369 static int v4tunnel_down(interface_defn * ifd, execfn * exec)
371 if (!execute("ip tunnel del %iface%", ifd, exec)) {
377 static method methods6[] = {
378 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
379 { "static", static_up6, static_down6, },
380 { "loopback", loopback_up6, loopback_down6, },
383 address_family addr_inet6 = {
385 sizeof(methods6) / sizeof(struct method),
388 #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
390 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
391 static int loopback_up(interface_defn *ifd, execfn *exec)
393 if (!execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)) {
399 static int loopback_down(interface_defn *ifd, execfn *exec)
401 if (!execute("ifconfig %iface% down", ifd, exec)) {
407 static int static_up(interface_defn *ifd, execfn *exec)
409 if (!execute("ifconfig %iface% %address% netmask %netmask% [[broadcast %broadcast%]] [[pointopoint %pointopoint%]] [[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
413 if (!execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec)) {
419 static int static_down(interface_defn *ifd, execfn *exec)
421 if (!execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec)) {
424 if (!execute("ifconfig %iface% down", ifd, exec)) {
430 static int execable(char *program)
433 if (0 == stat(program, &buf)) {
434 if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
441 static int dhcp_up(interface_defn *ifd, execfn *exec)
443 if (execable("/sbin/dhclient")) {
444 if (!execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)) {
447 } else if (execable("/sbin/pump")) {
448 if (!execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)) {
451 } else if (execable("/sbin/udhcpc")) {
452 if (!execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i %iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)) {
455 } else if (execable("/sbin/dhcpcd")) {
456 if (!execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] [[-l %leasetime%]] %iface%", ifd, exec)) {
463 static int dhcp_down(interface_defn *ifd, execfn *exec)
465 if (execable("/sbin/dhclient")) {
466 if (!execute("cat /var/run/dhclient.%iface%.pid | xargs -i kill -TERM {}", ifd, exec)) {
469 } else if (execable("/sbin/pump")) {
470 if (!execute("pump -i %iface% -k", ifd, exec)) {
473 } else if (execable("/sbin/udhcpc")) {
474 if (!execute("cat /var/run/udhcpc.%iface%.pid | xargs -i kill -TERM {}", ifd, exec)) {
477 } else if (execable("/sbin/dhcpcd")) {
478 if (!execute("dhcpcd -k %iface%", ifd, exec)) {
482 if (!execute("ifconfig %iface% down", ifd, exec)) {
488 static int bootp_up(interface_defn *ifd, execfn *exec)
490 if (!execute("bootpc [[--bootfile %bootfile%]] --dev %iface% [[--server %server%]] [[--hwaddr %hwaddr%]] --returniffail --serverbcast", ifd, exec)) {
496 static int bootp_down(interface_defn *ifd, execfn *exec)
498 if (!execute("ifconfig down %iface%", ifd, exec)) {
504 static int ppp_up(interface_defn *ifd, execfn *exec)
506 if (!execute("pon [[%provider%]]", ifd, exec)) {
512 static int ppp_down(interface_defn *ifd, execfn *exec)
514 if (!execute("poff [[%provider%]]", ifd, exec)) {
520 static int wvdial_up(interface_defn *ifd, execfn *exec)
522 if (!execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)) {
528 static int wvdial_down(interface_defn *ifd, execfn *exec)
530 if (!execute ("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial -p /var/run/wvdial.%iface% -s 2", ifd, exec)) {
536 static method methods[] = {
537 { "wvdial", wvdial_up, wvdial_down, },
538 { "ppp", ppp_up, ppp_down, },
539 { "static", static_up, static_down, },
540 { "bootp", bootp_up, bootp_down, },
541 { "dhcp", dhcp_up, dhcp_down, },
542 { "loopback", loopback_up, loopback_down, },
545 address_family addr_inet = {
547 sizeof(methods) / sizeof(struct method),
551 #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
553 static char *next_word(char *buf, char *word, int maxlen)
560 while (!isspace(*buf) && *buf) {
569 while (isspace(*buf) && *buf) {
576 static address_family *get_address_family(address_family *af[], char *name)
580 for (i = 0; af[i]; i++) {
581 if (strcmp(af[i]->name, name) == 0) {
588 static method *get_method(address_family *af, char *name)
592 for (i = 0; i < af->n_methods; i++) {
593 if (strcmp(af->method[i].name, name) == 0) {
594 return &af->method[i];
600 static int duplicate_if(interface_defn *ifa, interface_defn *ifb)
602 if (strcmp(ifa->iface, ifb->iface) != 0) {
605 if (ifa->address_family != ifb->address_family) {
611 static int get_line(char **result, size_t * result_len, FILE * f, int *line)
618 if (*result_len - pos < 10) {
619 char *newstr = xrealloc(*result, *result_len * 2 + 80);
621 *result_len = *result_len * 2 + 80;
624 if (!fgets(*result + pos, *result_len - pos, f)) {
625 if (ferror(f) == 0 && pos == 0)
630 pos += xstrlen(*result + pos);
631 } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n');
633 if (pos != 0 && (*result)[pos - 1] == '\n') {
634 (*result)[--pos] = '\0';
641 while (isspace((*result)[first]) && (*result)[first]) {
645 memmove(*result, *result + first, pos - first + 1);
648 } while ((*result)[0] == '#');
650 while ((*result)[pos - 1] == '\\') {
651 (*result)[--pos] = '\0';
653 if (*result_len - pos < 10) {
654 char *newstr = xrealloc(*result, *result_len * 2 + 80);
656 *result_len = *result_len * 2 + 80;
659 if (!fgets(*result + pos, *result_len - pos, f)) {
660 if (ferror(f) == 0 && pos == 0)
665 pos += xstrlen(*result + pos);
666 } while (pos == *result_len - 1 && (*result)[pos - 1] != '\n');
668 if (pos != 0 && (*result)[pos - 1] == '\n') {
669 (*result)[--pos] = '\0';
674 while (isspace((*result)[pos - 1])) { /* remove trailing whitespace */
677 (*result)[pos] = '\0';
682 static interfaces_file *read_interfaces(char *filename)
684 interface_defn *currif = NULL;
685 interfaces_file *defn;
686 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
687 mapping_defn *currmap = NULL;
696 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
698 defn = xmalloc(sizeof(interfaces_file));
699 defn->max_autointerfaces = defn->n_autointerfaces = 0;
700 defn->autointerfaces = NULL;
701 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
702 defn->mappings = NULL;
705 f = fopen(filename, "r");
711 while (get_line(&buf, &buf_len, f, &line)) {
712 rest = next_word(buf, firstword, 80);
714 continue; /* blank line */
717 if (strcmp(firstword, "mapping") == 0) {
718 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
719 currmap = xmalloc(sizeof(mapping_defn));
720 currmap->max_matches = 0;
721 currmap->n_matches = 0;
722 currmap->match = NULL;
724 while ((rest = next_word(rest, firstword, 80))) {
725 if (currmap->max_matches == currmap->n_matches) {
726 currmap->max_matches = currmap->max_matches * 2 + 1;
727 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
730 currmap->match[currmap->n_matches++] = xstrdup(firstword);
732 currmap->max_mappings = 0;
733 currmap->n_mappings = 0;
734 currmap->mapping = NULL;
735 currmap->script = NULL;
737 mapping_defn **where = &defn->mappings;
738 while (*where != NULL) {
739 where = &(*where)->next;
742 currmap->next = NULL;
744 currently_processing = MAPPING;
746 } else if (strcmp(firstword, "iface") == 0) {
749 char address_family_name[80];
750 char method_name[80];
751 address_family *addr_fams[] = {
752 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
755 #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
758 #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
764 currif = xmalloc(sizeof(interface_defn));
766 rest = next_word(rest, iface_name, 80);
767 rest = next_word(rest, address_family_name, 80);
768 rest = next_word(rest, method_name, 80);
771 error_msg("%s:%d: too few parameters for iface line", filename, line);
775 if (rest[0] != '\0') {
776 error_msg("%s:%d: too many parameters for iface line", filename, line);
780 currif->iface = xstrdup(iface_name);
782 currif->address_family = get_address_family(addr_fams, address_family_name);
783 if (!currif->address_family) {
784 error_msg("%s:%d: unknown address type", filename, line);
788 currif->method = get_method(currif->address_family, method_name);
789 if (!currif->method) {
790 error_msg("%s:%d: unknown method", filename, line);
794 currif->automatic = 1;
795 currif->max_options = 0;
796 currif->n_options = 0;
797 currif->option = NULL;
801 interface_defn **where = &defn->ifaces;
803 while (*where != NULL) {
804 if (duplicate_if(*where, currif)) {
805 error_msg("%s:%d: duplicate interface", filename, line);
808 where = &(*where)->next;
815 currently_processing = IFACE;
816 } else if (strcmp(firstword, "auto") == 0) {
817 while ((rest = next_word(rest, firstword, 80))) {
820 for (i = 0; i < defn->n_autointerfaces; i++) {
821 if (strcmp(firstword, defn->autointerfaces[i]) == 0) {
822 perror_msg("%s:%d: interface declared auto twice", filename, line);
827 if (defn->n_autointerfaces == defn->max_autointerfaces) {
830 defn->max_autointerfaces *= 2;
831 defn->max_autointerfaces++;
832 tmp = xrealloc(defn->autointerfaces, sizeof(*tmp) * defn->max_autointerfaces);
833 defn->autointerfaces = tmp;
836 defn->autointerfaces[defn->n_autointerfaces] = xstrdup(firstword);
837 defn->n_autointerfaces++;
839 currently_processing = NONE;
841 switch (currently_processing) {
846 if (xstrlen(rest) == 0) {
847 error_msg("%s:%d: option with empty value", filename, line);
851 if (strcmp(firstword, "up") != 0
852 && strcmp(firstword, "down") != 0
853 && strcmp(firstword, "pre-up") != 0
854 && strcmp(firstword, "post-down") != 0) {
855 for (i = 0; i < currif->n_options; i++) {
856 if (strcmp(currif->option[i].name, firstword) == 0) {
857 error_msg("%s:%d: duplicate option", filename, line);
863 if (currif->n_options >= currif->max_options) {
866 currif->max_options = currif->max_options + 10;
867 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
868 currif->option = opt;
870 currif->option[currif->n_options].name = xstrdup(firstword);
871 currif->option[currif->n_options].value = xstrdup(rest);
872 if (!currif->option[currif->n_options].name) {
876 if (!currif->option[currif->n_options].value) {
883 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
884 if (strcmp(firstword, "script") == 0) {
885 if (currmap->script != NULL) {
886 error_msg("%s:%d: duplicate script in mapping", filename, line);
889 currmap->script = xstrdup(rest);
891 } else if (strcmp(firstword, "map") == 0) {
892 if (currmap->max_mappings == currmap->n_mappings) {
893 currmap->max_mappings = currmap->max_mappings * 2 + 1;
894 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
896 currmap->mapping[currmap->n_mappings] = xstrdup(rest);
897 currmap->n_mappings++;
899 error_msg("%s:%d: misplaced option", filename, line);
906 error_msg("%s:%d: misplaced option", filename, line);
911 if (ferror(f) != 0) {
912 perror_msg("%s", filename);
921 static int check(char *str)
923 return (str != NULL);
926 static char *setlocalenv(char *format, char *name, char *value)
932 result = xmalloc(xstrlen(format) + xstrlen(name) + xstrlen(value) + 1);
934 sprintf(result, format, name, value);
936 for (here = there = result; *there != '=' && *there; there++) {
940 *there = toupper(*there);
942 if (isalnum(*there) || *there == '_') {
947 memmove(here, there, xstrlen(there) + 1);
952 static void set_environ(interface_defn *iface, char *mode)
956 const int n_env_entries = iface->n_options + 5;
959 if (environ != NULL) {
960 for (ppch = environ; *ppch; ppch++) {
967 environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
968 environend = environ;
971 for (i = 0; i < iface->n_options; i++) {
972 if (strcmp(iface->option[i].name, "up") == 0
973 || strcmp(iface->option[i].name, "down") == 0
974 || strcmp(iface->option[i].name, "pre-up") == 0
975 || strcmp(iface->option[i].name, "post-down") == 0) {
978 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
982 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
984 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
986 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
988 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
990 *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
994 static int doit(char *str)
996 if (verbose || no_act) {
997 error_msg("%s", str);
1004 switch (child = fork()) {
1005 case -1: /* failure */
1008 execle("/bin/sh", "/bin/sh", "-c", str, NULL, environ);
1010 default: /* parent */
1012 waitpid(child, &status, 0);
1013 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
1020 static int execute_all(interface_defn *ifd, execfn *exec, char *opt)
1025 for (i = 0; i < ifd->n_options; i++) {
1026 if (strcmp(ifd->option[i].name, opt) == 0) {
1027 if (!(*exec) (ifd->option[i].value)) {
1033 sprintf(buf, "run-parts /etc/network/if-%s.d", opt);
1039 static int iface_up(interface_defn *iface)
1041 if (!iface->method->up(iface, check)) {
1045 set_environ(iface, "start");
1046 if (!execute_all(iface, doit, "pre-up")) {
1049 if (!iface->method->up(iface, doit)) {
1052 if (!execute_all(iface, doit, "up")) {
1059 static int iface_down(interface_defn *iface)
1061 if (!iface->method->down(iface, check)) {
1064 set_environ(iface, "stop");
1065 if (!execute_all(iface, doit, "down")) {
1068 if (!iface->method->down(iface, doit)) {
1071 if (!execute_all(iface, doit, "post-down")) {
1077 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1078 static int popen2(FILE **in, FILE **out, char *command, ...)
1081 char *argv[11] = { command };
1083 int infd[2], outfd[2];
1087 va_start(ap, command);
1088 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
1091 argv[argc] = NULL; /* make sure */
1094 if (pipe(infd) != 0) {
1098 if (pipe(outfd) != 0) {
1105 switch (pid = fork()) {
1106 case -1: /* failure */
1119 execvp(command, argv);
1121 default: /* parent */
1122 *in = fdopen(infd[1], "w");
1123 *out = fdopen(outfd[0], "r");
1131 static int run_mapping(char *physical, char *logical, int len, mapping_defn * map)
1138 pid = popen2(&in, &out, map->script, physical, NULL);
1142 for (i = 0; i < map->n_mappings; i++) {
1143 fprintf(in, "%s\n", map->mapping[i]);
1146 waitpid(pid, &status, 0);
1147 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1148 if (fgets(logical, len, out)) {
1149 char *pch = logical + xstrlen(logical) - 1;
1151 while (pch >= logical && isspace(*pch))
1159 #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
1162 static int lookfor_iface(char **ifaces, int n_ifaces, char *iface)
1166 for (i = 0; i < n_ifaces; i++) {
1167 if (strncmp(iface, ifaces[i], xstrlen(iface)) == 0) {
1168 if (ifaces[i][xstrlen(iface)] == '=') {
1177 static void add_to_state(char ***ifaces, int *n_ifaces, int *max_ifaces, char *new_iface)
1179 if (*max_ifaces == *n_ifaces) {
1180 *max_ifaces = (*max_ifaces * 2) + 1;
1181 *ifaces = xrealloc(*ifaces, sizeof(**ifaces) * *max_ifaces);
1184 (*ifaces)[(*n_ifaces)++] = new_iface;
1187 extern int ifupdown_main(int argc, char **argv)
1189 int (*cmds) (interface_defn *) = NULL;
1190 interfaces_file *defn;
1191 FILE *state_fp = NULL;
1192 char **target_iface = NULL;
1193 char **state = NULL; /* list of iface=liface */
1194 char *interfaces = "/etc/network/interfaces";
1195 char *statefile = "/etc/network/ifstate";
1198 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1199 int run_mappings = 1;
1202 int n_target_ifaces = 0;
1207 if (applet_name[2] == 'u') {
1211 /* ifdown command */
1215 while ((i = getopt(argc, argv, "i:hvnamf")) != -1) {
1217 case 'i': /* interfaces */
1218 interfaces = xstrdup(optarg);
1220 case 'v': /* verbose */
1226 case 'n': /* no-act */
1229 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1230 case 'm': /* no-mappings */
1234 case 'f': /* force */
1243 if (argc - optind > 0) {
1253 defn = read_interfaces(interfaces);
1255 error_msg_and_die("couldn't read interfaces file \"%s\"", interfaces);
1258 state_fp = fopen(statefile, no_act ? "r" : "a+");
1259 if (state_fp == NULL && !no_act) {
1260 perror_msg_and_die("failed to open statefile %s", statefile);
1263 if (state_fp != NULL) {
1270 const int state_fd = fileno(state_fp);
1272 flags = fcntl(state_fd, F_GETFD);
1273 if ((flags < 0) || (fcntl(state_fd, F_SETFD, flags | FD_CLOEXEC) < 0)) {
1274 perror_msg_and_die("failed to set FD_CLOEXEC on statefile %s", statefile);
1277 lock.l_type = F_WRLCK;
1278 lock.l_whence = SEEK_SET;
1282 if (fcntl(state_fd, F_SETLKW, &lock) < 0) {
1283 perror_msg_and_die("failed to lock statefile %s", statefile);
1288 while ((p = fgets(buf, sizeof buf, state_fp)) != NULL) {
1291 pch = buf + xstrlen(buf) - 1;
1292 while (pch > buf && isspace(*pch)) {
1298 while (isspace(*pch)) {
1302 add_to_state(&state, &n_state, &max_state, xstrdup(pch));
1307 if (cmds == iface_up) {
1308 target_iface = defn->autointerfaces;
1309 n_target_ifaces = defn->n_autointerfaces;
1310 } else if (cmds == iface_down) {
1311 target_iface = state;
1312 n_target_ifaces = n_state;
1315 target_iface = argv + optind;
1316 n_target_ifaces = argc - optind;
1320 for (i = 0; i < n_target_ifaces; i++) {
1321 interface_defn *currif;
1327 strncpy(iface, target_iface[i], sizeof(iface));
1328 iface[sizeof(iface) - 1] = '\0';
1330 if ((pch = strchr(iface, '='))) {
1332 strncpy(liface, pch + 1, sizeof(liface));
1333 liface[sizeof(liface) - 1] = '\0';
1335 strncpy(liface, iface, sizeof(liface));
1336 liface[sizeof(liface) - 1] = '\0';
1339 int already_up = lookfor_iface(state, n_state, iface);;
1341 if (cmds == iface_up) {
1343 if (already_up != -1) {
1344 error_msg("interface %s already configured", iface);
1349 if (already_up == -1) {
1350 error_msg("interface %s not configured", iface);
1353 strncpy(liface, strchr(state[already_up], '=') + 1, 80);
1357 #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
1358 if ((cmds == iface_up) && run_mappings) {
1359 mapping_defn *currmap;
1361 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1363 for (i = 0; i < currmap->n_matches; i++) {
1364 if (fnmatch(currmap->match[i], liface, 0) != 0)
1367 error_msg("Running mapping script %s on %s", currmap->script, liface);
1369 run_mapping(iface, liface, sizeof(liface), currmap);
1376 for (currif = defn->ifaces; currif; currif = currif->next) {
1377 if (strcmp(liface, currif->iface) == 0) {
1378 char *oldiface = currif->iface;
1382 currif->iface = iface;
1385 error_msg("Configuring interface %s=%s (%s)", iface, liface, currif->address_family->name);
1388 switch (cmds(currif)) {
1391 ("Don't seem to be have all the variables for %s/%s.\n",
1392 liface, currif->address_family->name);
1395 /* this wasn't entirely successful, should it be added to
1401 currif->iface = oldiface;
1405 if (!okay && !force) {
1406 error_msg("Ignoring unknown interface %s=%s.", iface, liface);
1408 int already_up = lookfor_iface(state, n_state, iface);
1410 if (cmds == iface_up) {
1411 char *newiface = xmalloc(xstrlen(iface) + 1 + xstrlen(liface) + 1);
1412 sprintf(newiface, "%s=%s", iface, liface);
1413 if (already_up == -1) {
1414 add_to_state(&state, &n_state, &max_state, newiface);
1416 free(state[already_up]);
1417 state[already_up] = newiface;
1419 } else if (cmds == iface_down) {
1420 if (already_up != -1) {
1421 state[already_up] = state[--n_state];
1425 if (state_fp != NULL && !no_act) {
1426 if (ftruncate(fileno(state_fp), 0) < 0) {
1427 error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
1431 for (i = 0; i < n_state; i++) {
1432 fputs(state[i], state_fp);
1433 fputc('\n', state_fp);
1439 if (state_fp != NULL) {