Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 2 | /* |
| 3 | * ifupdown for busybox |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2002 Glenn McGrath |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org> |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 6 | * |
| 7 | * Based on ifupdown v 0.6.4 by Anthony Towns |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 8 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au> |
| 9 | * |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 10 | * Changes to upstream version |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 11 | * Remove checks for kernel version, assume kernel version 2.2.0 or better. |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 12 | * Lines in the interfaces file cannot wrap. |
Denis Vlasenko | 2a86a61 | 2007-07-19 21:49:30 +0000 | [diff] [blame] | 13 | * To adhere to the FHS, the default state file is /var/run/ifstate |
| 14 | * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build |
| 15 | * configuration. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 16 | * |
Rob Landley | 1b751c8 | 2005-10-28 09:24:33 +0000 | [diff] [blame] | 17 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 20 | #include <sys/utsname.h> |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 21 | #include <fnmatch.h> |
| 22 | #include <getopt.h> |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 24 | #include "libbb.h" |
| 25 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 26 | #define MAX_OPT_DEPTH 10 |
| 27 | #define EUNBALBRACK 10001 |
| 28 | #define EUNDEFVAR 10002 |
| 29 | #define EUNBALPER 10000 |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 30 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 31 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 32 | #define MAX_INTERFACE_LENGTH 10 |
| 33 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 34 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 35 | #define debug_noise(args...) /*fprintf(stderr, args)*/ |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 36 | |
| 37 | /* Forward declaration */ |
| 38 | struct interface_defn_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 097c324 | 2006-11-27 16:59:15 +0000 | [diff] [blame] | 40 | typedef int execfn(char *command); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 42 | struct method_t { |
| 43 | const char *name; |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 44 | int (*up)(struct interface_defn_t *ifd, execfn *e); |
| 45 | int (*down)(struct interface_defn_t *ifd, execfn *e); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 46 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 48 | struct address_family_t { |
| 49 | const char *name; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 50 | int n_methods; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 51 | const struct method_t *method; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 52 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 54 | struct mapping_defn_t { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 55 | struct mapping_defn_t *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 56 | |
| 57 | int max_matches; |
| 58 | int n_matches; |
| 59 | char **match; |
| 60 | |
| 61 | char *script; |
| 62 | |
| 63 | int max_mappings; |
| 64 | int n_mappings; |
| 65 | char **mapping; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 66 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 68 | struct variable_t { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 69 | char *name; |
| 70 | char *value; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 71 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 72 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 73 | struct interface_defn_t { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 74 | const struct address_family_t *address_family; |
| 75 | const struct method_t *method; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 76 | |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 77 | char *iface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 78 | int max_options; |
| 79 | int n_options; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 80 | struct variable_t *option; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 83 | struct interfaces_file_t { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 84 | llist_t *autointerfaces; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 85 | llist_t *ifaces; |
| 86 | struct mapping_defn_t *mappings; |
| 87 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 88 | |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 89 | #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:" |
| 90 | enum { |
| 91 | OPT_do_all = 0x1, |
| 92 | OPT_no_act = 0x2, |
| 93 | OPT_verbose = 0x4, |
| 94 | OPT_force = 0x8, |
| 95 | OPT_no_mappings = 0x10, |
| 96 | }; |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 97 | #define DO_ALL (option_mask32 & OPT_do_all) |
| 98 | #define NO_ACT (option_mask32 & OPT_no_act) |
| 99 | #define VERBOSE (option_mask32 & OPT_verbose) |
| 100 | #define FORCE (option_mask32 & OPT_force) |
| 101 | #define NO_MAPPINGS (option_mask32 & OPT_no_mappings) |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 102 | |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 103 | static char **my_environ; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 104 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 105 | static const char *startup_PATH; |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 106 | |
Mike Frysinger | b049c0e | 2006-06-20 23:03:27 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 |
| 108 | |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 109 | static void addstr(char **bufp, const char *str, size_t str_length) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 110 | { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 111 | /* xasprintf trick will be smaller, but we are often |
| 112 | * called with str_length == 1 - don't want to have |
| 113 | * THAT much of malloc/freeing! */ |
| 114 | char *buf = *bufp; |
| 115 | int len = (buf ? strlen(buf) : 0); |
| 116 | str_length++; |
| 117 | buf = xrealloc(buf, len + str_length); |
| 118 | /* copies at most str_length-1 chars! */ |
| 119 | safe_strncpy(buf + len, str, str_length); |
| 120 | *bufp = buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 123 | static int strncmpz(const char *l, const char *r, size_t llen) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 124 | { |
| 125 | int i = strncmp(l, r, llen); |
| 126 | |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 127 | if (i == 0) |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 128 | return -r[llen]; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 129 | return i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 132 | static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 133 | { |
| 134 | int i; |
| 135 | |
| 136 | if (strncmpz(id, "iface", idlen) == 0) { |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 137 | static char *label_buf; |
| 138 | //char *result; |
| 139 | |
| 140 | free(label_buf); |
| 141 | label_buf = xstrdup(ifd->iface); |
| 142 | // Remove virtual iface suffix - why? |
| 143 | // ubuntu's ifup doesn't do this |
| 144 | //result = strchrnul(label_buf, ':'); |
| 145 | //*result = '\0'; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 146 | return label_buf; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 147 | } |
| 148 | if (strncmpz(id, "label", idlen) == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 149 | return ifd->iface; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 150 | } |
| 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; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 156 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 159 | #if ENABLE_FEATURE_IFUPDOWN_IP |
| 160 | static int count_netmask_bits(const char *dotted_quad) |
| 161 | { |
| 162 | // int result; |
| 163 | // unsigned a, b, c, d; |
| 164 | // /* Found a netmask... Check if it is dotted quad */ |
| 165 | // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
| 166 | // return -1; |
| 167 | // if ((a|b|c|d) >> 8) |
| 168 | // return -1; /* one of numbers is >= 256 */ |
| 169 | // d |= (a << 24) | (b << 16) | (c << 8); /* IP */ |
| 170 | // d = ~d; /* 11110000 -> 00001111 */ |
| 171 | |
| 172 | /* Shorter version */ |
| 173 | int result; |
| 174 | struct in_addr ip; |
| 175 | unsigned d; |
| 176 | |
| 177 | if (inet_aton(dotted_quad, &ip) == 0) |
| 178 | return -1; /* malformed dotted IP */ |
| 179 | d = ntohl(ip.s_addr); /* IP in host order */ |
| 180 | d = ~d; /* 11110000 -> 00001111 */ |
| 181 | if (d & (d+1)) /* check that it is in 00001111 form */ |
| 182 | return -1; /* no it is not */ |
| 183 | result = 32; |
| 184 | while (d) { |
| 185 | d >>= 1; |
| 186 | result--; |
| 187 | } |
| 188 | return result; |
| 189 | } |
| 190 | #endif |
| 191 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 192 | static char *parse(const char *command, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 193 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 194 | size_t old_pos[MAX_OPT_DEPTH] = { 0 }; |
| 195 | int okay[MAX_OPT_DEPTH] = { 1 }; |
| 196 | int opt_depth = 1; |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 197 | char *result = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 198 | |
| 199 | while (*command) { |
| 200 | switch (*command) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 201 | default: |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 202 | addstr(&result, command, 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 203 | command++; |
| 204 | break; |
| 205 | case '\\': |
| 206 | if (command[1]) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 207 | addstr(&result, command + 1, 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 208 | command += 2; |
| 209 | } else { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 210 | addstr(&result, command, 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 211 | command++; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 212 | } |
| 213 | break; |
| 214 | case '[': |
| 215 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 216 | old_pos[opt_depth] = result ? strlen(result) : 0; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 217 | okay[opt_depth] = 1; |
| 218 | opt_depth++; |
| 219 | command += 2; |
| 220 | } else { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 221 | addstr(&result, "[", 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 222 | command++; |
| 223 | } |
| 224 | break; |
| 225 | case ']': |
| 226 | if (command[1] == ']' && opt_depth > 1) { |
| 227 | opt_depth--; |
| 228 | if (!okay[opt_depth]) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 229 | result[old_pos[opt_depth]] = '\0'; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 230 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 231 | command += 2; |
| 232 | } else { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 233 | addstr(&result, "]", 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 234 | command++; |
| 235 | } |
| 236 | break; |
| 237 | case '%': |
| 238 | { |
| 239 | char *nextpercent; |
| 240 | char *varvalue; |
| 241 | |
| 242 | command++; |
| 243 | nextpercent = strchr(command, '%'); |
| 244 | if (!nextpercent) { |
| 245 | errno = EUNBALPER; |
| 246 | free(result); |
| 247 | return NULL; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 248 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 249 | |
| 250 | varvalue = get_var(command, nextpercent - command, ifd); |
| 251 | |
| 252 | if (varvalue) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 253 | addstr(&result, varvalue, strlen(varvalue)); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 254 | } else { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 255 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 256 | /* Sigh... Add a special case for 'ip' to convert from |
| 257 | * dotted quad to bit count style netmasks. */ |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 258 | if (strncmp(command, "bnmask", 6) == 0) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 259 | unsigned res; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 260 | varvalue = get_var("netmask", 7, ifd); |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 261 | if (varvalue) { |
| 262 | res = count_netmask_bits(varvalue); |
| 263 | if (res > 0) { |
| 264 | const char *argument = utoa(res); |
| 265 | addstr(&result, argument, strlen(argument)); |
| 266 | command = nextpercent + 1; |
| 267 | break; |
| 268 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 269 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 270 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 271 | #endif |
| 272 | okay[opt_depth - 1] = 0; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 273 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 274 | |
| 275 | command = nextpercent + 1; |
| 276 | } |
| 277 | break; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | if (opt_depth > 1) { |
| 282 | errno = EUNBALBRACK; |
| 283 | free(result); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 284 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | if (!okay[0]) { |
| 288 | errno = EUNDEFVAR; |
| 289 | free(result); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 290 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 293 | return result; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 294 | } |
| 295 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 296 | /* execute() returns 1 for success and 0 for failure */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 297 | static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 298 | { |
| 299 | char *out; |
| 300 | int ret; |
| 301 | |
| 302 | out = parse(command, ifd); |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 303 | if (!out) { |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 304 | /* parse error? */ |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 305 | return 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 306 | } |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 307 | /* out == "": parsed ok but not all needed variables known, skip */ |
| 308 | ret = out[0] ? (*exec)(out) : 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 309 | |
| 310 | free(out); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 311 | if (ret != 1) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 312 | return 0; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 313 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 314 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 315 | } |
Bernhard Reutner-Fischer | d42ef28 | 2005-12-14 14:13:15 +0000 | [diff] [blame] | 316 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 317 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 318 | #if ENABLE_FEATURE_IFUPDOWN_IPV6 |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 319 | static int loopback_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 320 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 321 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 322 | int result; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 323 | result = execute("ip addr add ::1 dev %iface%", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 324 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 325 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 326 | #else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 327 | return execute("ifconfig %iface% add ::1", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 328 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 331 | static int loopback_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 332 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 333 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 334 | return execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 335 | #else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 336 | return execute("ifconfig %iface% del ::1", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 337 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 340 | static int static_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 341 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 342 | int result; |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 343 | #if ENABLE_FEATURE_IFUPDOWN_IP |
| 344 | result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec); |
| 345 | result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec); |
| 346 | /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */ |
| 347 | result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 348 | #else |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 349 | result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 350 | result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 351 | result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 352 | #endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 353 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 356 | static int static_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 357 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 358 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 359 | return execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 360 | #else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 361 | return execute("ifconfig %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 362 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 365 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 366 | static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 367 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 368 | int result; |
| 369 | result = execute("ip tunnel add %iface% mode sit remote " |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 370 | "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 371 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 62b031f | 2003-08-29 07:47:52 +0000 | [diff] [blame] | 372 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 373 | result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 374 | return ((result == 4) ? 4 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 377 | static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 378 | { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 379 | return execute("ip tunnel del %iface%", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 380 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 381 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 382 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 383 | static const struct method_t methods6[] = { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 384 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 385 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 386 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 387 | { "static", static_up6, static_down6, }, |
| 388 | { "loopback", loopback_up6, loopback_down6, }, |
| 389 | }; |
| 390 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 391 | static const struct address_family_t addr_inet6 = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 392 | "inet6", |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 393 | ARRAY_SIZE(methods6), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 394 | methods6 |
| 395 | }; |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 396 | #endif /* FEATURE_IFUPDOWN_IPV6 */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 397 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 398 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 399 | static int loopback_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 400 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 401 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 402 | int result; |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 403 | result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 404 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 405 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 406 | #else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 407 | return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 408 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 411 | static int loopback_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 412 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 413 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 414 | int result; |
| 415 | result = execute("ip addr flush dev %iface%", ifd, exec); |
| 416 | result += execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 417 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 418 | #else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 419 | return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 420 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 423 | static int static_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 424 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 425 | int result; |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 426 | #if ENABLE_FEATURE_IFUPDOWN_IP |
| 427 | result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] " |
| 428 | "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec); |
| 429 | result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec); |
| 430 | result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 431 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 432 | #else |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 433 | /* ifconfig said to set iface up before it processes hw %hwaddress%, |
| 434 | * which then of course fails. Thus we run two separate ifconfig */ |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 435 | result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up", |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 436 | ifd, exec); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 437 | result += execute("ifconfig %iface% %address% netmask %netmask%" |
| 438 | "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ", |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 439 | ifd, exec); |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 440 | result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec); |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 441 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 442 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 445 | static int static_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 446 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 447 | int result; |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 448 | #if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 449 | result = execute("ip addr flush dev %iface%", ifd, exec); |
| 450 | result += execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 451 | #else |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 452 | result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 453 | result += execute("ifconfig %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 454 | #endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 455 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 458 | #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 459 | struct dhcp_client_t |
| 460 | { |
| 461 | const char *name; |
| 462 | const char *startcmd; |
| 463 | const char *stopcmd; |
| 464 | }; |
| 465 | |
| 466 | static const struct dhcp_client_t ext_dhcp_clients[] = { |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 467 | { "dhcpcd", |
| 468 | "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%", |
| 469 | "dhcpcd -k %iface%", |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 470 | }, |
| 471 | { "dhclient", |
| 472 | "dhclient -pf /var/run/dhclient.%iface%.pid %iface%", |
| 473 | "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", |
| 474 | }, |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 475 | { "pump", |
| 476 | "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]", |
| 477 | "pump -i %iface% -k", |
| 478 | }, |
| 479 | { "udhcpc", |
| 480 | "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]", |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 481 | "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 482 | }, |
| 483 | }; |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 484 | #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 485 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 486 | static int dhcp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 487 | { |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 488 | #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 489 | int i; |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 490 | #if ENABLE_FEATURE_IFUPDOWN_IP |
| 491 | /* ip doesn't up iface when it configures it (unlike ifconfig) */ |
| 492 | if (!execute("ip link set %iface% up", ifd, exec)) |
| 493 | return 0; |
| 494 | #endif |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 495 | for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 496 | if (exists_execable(ext_dhcp_clients[i].name)) |
| 497 | return execute(ext_dhcp_clients[i].startcmd, ifd, exec); |
| 498 | } |
| 499 | bb_error_msg("no dhcp clients found"); |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 500 | return 0; |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 501 | #elif ENABLE_APP_UDHCPC |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 502 | #if ENABLE_FEATURE_IFUPDOWN_IP |
| 503 | /* ip doesn't up iface when it configures it (unlike ifconfig) */ |
| 504 | if (!execute("ip link set %iface% up", ifd, exec)) |
| 505 | return 0; |
| 506 | #endif |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 507 | return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid " |
| 508 | "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]", |
| 509 | ifd, exec); |
| 510 | #else |
| 511 | return 0; /* no dhcp support */ |
Denis Vlasenko | 4b924f3 | 2007-05-30 00:29:55 +0000 | [diff] [blame] | 512 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 513 | } |
| 514 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 515 | static int dhcp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 516 | { |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 517 | #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 518 | int i; |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 519 | for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 520 | if (exists_execable(ext_dhcp_clients[i].name)) |
| 521 | return execute(ext_dhcp_clients[i].stopcmd, ifd, exec); |
| 522 | } |
| 523 | bb_error_msg("no dhcp clients found, using static interface shutdown"); |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 524 | return static_down(ifd, exec); |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 525 | #elif ENABLE_APP_UDHCPC |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 526 | return execute("kill " |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 527 | "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec); |
| 528 | #else |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 529 | return 0; /* no dhcp support */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 530 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 531 | } |
| 532 | |
Denis Vlasenko | 1c3577f | 2006-10-02 20:57:10 +0000 | [diff] [blame] | 533 | static int manual_up_down(struct interface_defn_t *ifd, execfn *exec) |
| 534 | { |
| 535 | return 1; |
| 536 | } |
| 537 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 538 | static int bootp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 539 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 540 | return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%" |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 541 | "[[ --server %server%]][[ --hwaddr %hwaddr%]]" |
| 542 | " --returniffail --serverbcast", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 545 | static int ppp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 546 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 547 | return execute("pon[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 550 | static int ppp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 551 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 552 | return execute("poff[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 553 | } |
| 554 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 555 | static int wvdial_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 556 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 557 | return execute("start-stop-daemon --start -x wvdial " |
| 558 | "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 559 | } |
| 560 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 561 | static int wvdial_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 562 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 563 | return execute("start-stop-daemon --stop -x wvdial " |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 564 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 567 | static const struct method_t methods[] = { |
Denis Vlasenko | 1c3577f | 2006-10-02 20:57:10 +0000 | [diff] [blame] | 568 | { "manual", manual_up_down, manual_up_down, }, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 569 | { "wvdial", wvdial_up, wvdial_down, }, |
| 570 | { "ppp", ppp_up, ppp_down, }, |
| 571 | { "static", static_up, static_down, }, |
Eric Andersen | 373bc1e | 2004-07-30 14:31:01 +0000 | [diff] [blame] | 572 | { "bootp", bootp_up, static_down, }, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 573 | { "dhcp", dhcp_up, dhcp_down, }, |
| 574 | { "loopback", loopback_up, loopback_down, }, |
| 575 | }; |
| 576 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 577 | static const struct address_family_t addr_inet = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 578 | "inet", |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 579 | ARRAY_SIZE(methods), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 580 | methods |
| 581 | }; |
| 582 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 583 | #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 584 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 585 | static char *next_word(char **buf) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 586 | { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 587 | unsigned short length; |
| 588 | char *word; |
| 589 | |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 590 | if (!buf || !*buf || !**buf) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 591 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 592 | } |
| 593 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 594 | /* Skip over leading whitespace */ |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 595 | word = skip_whitespace(*buf); |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 596 | |
| 597 | /* Skip over comments */ |
| 598 | if (*word == '#') { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 599 | return NULL; |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 600 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 601 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 602 | /* Find the length of this word */ |
| 603 | length = strcspn(word, " \t\n"); |
| 604 | if (length == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 605 | return NULL; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 606 | } |
| 607 | *buf = word + length; |
Eric Andersen | 2894266 | 2003-04-19 23:15:06 +0000 | [diff] [blame] | 608 | /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */ |
| 609 | if (**buf) { |
| 610 | **buf = '\0'; |
| 611 | (*buf)++; |
| 612 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 613 | |
| 614 | return word; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 617 | static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 618 | { |
| 619 | int i; |
| 620 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 621 | if (!name) |
| 622 | return NULL; |
| 623 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 624 | for (i = 0; af[i]; i++) { |
| 625 | if (strcmp(af[i]->name, name) == 0) { |
| 626 | return af[i]; |
| 627 | } |
| 628 | } |
| 629 | return NULL; |
| 630 | } |
| 631 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 632 | static const struct method_t *get_method(const struct address_family_t *af, char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 633 | { |
| 634 | int i; |
| 635 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 636 | if (!name) |
| 637 | return NULL; |
Bernhard Reutner-Fischer | e747f62 | 2007-10-06 20:47:53 +0000 | [diff] [blame] | 638 | /* TODO: use index_in_str_array() */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 639 | for (i = 0; i < af->n_methods; i++) { |
| 640 | if (strcmp(af->method[i].name, name) == 0) { |
| 641 | return &af->method[i]; |
| 642 | } |
| 643 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 644 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 647 | static const llist_t *find_list_string(const llist_t *list, const char *string) |
| 648 | { |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 649 | if (string == NULL) |
| 650 | return NULL; |
| 651 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 652 | while (list) { |
| 653 | if (strcmp(list->data, string) == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 654 | return list; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 655 | } |
| 656 | list = list->link; |
| 657 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 658 | return NULL; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 661 | static struct interfaces_file_t *read_interfaces(const char *filename) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 662 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 663 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 664 | struct mapping_defn_t *currmap = NULL; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 665 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 666 | struct interface_defn_t *currif = NULL; |
| 667 | struct interfaces_file_t *defn; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 668 | FILE *f; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 669 | char *firstword; |
| 670 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 671 | |
| 672 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
| 673 | |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 674 | defn = xzalloc(sizeof(struct interfaces_file_t)); |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 675 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 676 | f = xfopen(filename, "r"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 677 | |
Denis Vlasenko | 2d5ca60 | 2006-10-12 22:43:20 +0000 | [diff] [blame] | 678 | while ((buf = xmalloc_getline(f)) != NULL) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 679 | char *buf_ptr = buf; |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 680 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 681 | firstword = next_word(&buf_ptr); |
| 682 | if (firstword == NULL) { |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 683 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 684 | continue; /* blank line */ |
| 685 | } |
| 686 | |
| 687 | if (strcmp(firstword, "mapping") == 0) { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 688 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 689 | currmap = xzalloc(sizeof(struct mapping_defn_t)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 690 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 691 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 692 | if (currmap->max_matches == currmap->n_matches) { |
| 693 | currmap->max_matches = currmap->max_matches * 2 + 1; |
| 694 | currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); |
| 695 | } |
| 696 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 697 | currmap->match[currmap->n_matches++] = xstrdup(firstword); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 698 | } |
| 699 | currmap->max_mappings = 0; |
| 700 | currmap->n_mappings = 0; |
| 701 | currmap->mapping = NULL; |
| 702 | currmap->script = NULL; |
| 703 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 704 | struct mapping_defn_t **where = &defn->mappings; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 705 | while (*where != NULL) { |
| 706 | where = &(*where)->next; |
| 707 | } |
| 708 | *where = currmap; |
| 709 | currmap->next = NULL; |
| 710 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 711 | debug_noise("Added mapping\n"); |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 712 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 713 | currently_processing = MAPPING; |
| 714 | } else if (strcmp(firstword, "iface") == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 715 | static const struct address_family_t *const addr_fams[] = { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 716 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 717 | &addr_inet, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 718 | #endif |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 719 | #if ENABLE_FEATURE_IFUPDOWN_IPV6 |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 720 | &addr_inet6, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 721 | #endif |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 722 | NULL |
| 723 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 724 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 725 | char *iface_name; |
| 726 | char *address_family_name; |
| 727 | char *method_name; |
| 728 | llist_t *iface_list; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 729 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 730 | currif = xzalloc(sizeof(struct interface_defn_t)); |
| 731 | iface_name = next_word(&buf_ptr); |
| 732 | address_family_name = next_word(&buf_ptr); |
| 733 | method_name = next_word(&buf_ptr); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 734 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 735 | if (buf_ptr == NULL) { |
| 736 | bb_error_msg("too few parameters for line \"%s\"", buf); |
| 737 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 738 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 739 | |
| 740 | /* ship any trailing whitespace */ |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 741 | buf_ptr = skip_whitespace(buf_ptr); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 742 | |
| 743 | if (buf_ptr[0] != '\0') { |
| 744 | bb_error_msg("too many parameters \"%s\"", buf); |
| 745 | return NULL; |
| 746 | } |
| 747 | |
| 748 | currif->iface = xstrdup(iface_name); |
| 749 | |
| 750 | currif->address_family = get_address_family(addr_fams, address_family_name); |
| 751 | if (!currif->address_family) { |
| 752 | bb_error_msg("unknown address type \"%s\"", address_family_name); |
| 753 | return NULL; |
| 754 | } |
| 755 | |
| 756 | currif->method = get_method(currif->address_family, method_name); |
| 757 | if (!currif->method) { |
| 758 | bb_error_msg("unknown method \"%s\"", method_name); |
| 759 | return NULL; |
| 760 | } |
| 761 | |
| 762 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { |
| 763 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; |
| 764 | if ((strcmp(tmp->iface, currif->iface) == 0) && |
| 765 | (tmp->address_family == currif->address_family)) { |
| 766 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); |
| 767 | return NULL; |
| 768 | } |
| 769 | } |
| 770 | llist_add_to_end(&(defn->ifaces), (char*)currif); |
| 771 | |
| 772 | debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 773 | currently_processing = IFACE; |
| 774 | } else if (strcmp(firstword, "auto") == 0) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 775 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 776 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 777 | /* Check the interface isnt already listed */ |
| 778 | if (find_list_string(defn->autointerfaces, firstword)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 779 | bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 780 | } |
| 781 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 782 | /* Add the interface to the list */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 783 | llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword)); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 784 | debug_noise("\nauto %s\n", firstword); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 785 | } |
| 786 | currently_processing = NONE; |
| 787 | } else { |
| 788 | switch (currently_processing) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 789 | case IFACE: |
| 790 | { |
| 791 | int i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 792 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 793 | if (strlen(buf_ptr) == 0) { |
| 794 | bb_error_msg("option with empty value \"%s\"", buf); |
| 795 | return NULL; |
| 796 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 797 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 798 | if (strcmp(firstword, "up") != 0 |
| 799 | && strcmp(firstword, "down") != 0 |
| 800 | && strcmp(firstword, "pre-up") != 0 |
| 801 | && strcmp(firstword, "post-down") != 0) { |
| 802 | for (i = 0; i < currif->n_options; i++) { |
| 803 | if (strcmp(currif->option[i].name, firstword) == 0) { |
| 804 | bb_error_msg("duplicate option \"%s\"", buf); |
| 805 | return NULL; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 809 | } |
| 810 | if (currif->n_options >= currif->max_options) { |
| 811 | struct variable_t *opt; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 812 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 813 | currif->max_options = currif->max_options + 10; |
| 814 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); |
| 815 | currif->option = opt; |
| 816 | } |
| 817 | currif->option[currif->n_options].name = xstrdup(firstword); |
| 818 | currif->option[currif->n_options].value = xstrdup(buf_ptr); |
| 819 | if (!currif->option[currif->n_options].name) { |
| 820 | perror(filename); |
| 821 | return NULL; |
| 822 | } |
| 823 | if (!currif->option[currif->n_options].value) { |
| 824 | perror(filename); |
| 825 | return NULL; |
| 826 | } |
| 827 | debug_noise("\t%s=%s\n", currif->option[currif->n_options].name, |
| 828 | currif->option[currif->n_options].value); |
| 829 | currif->n_options++; |
| 830 | break; |
| 831 | case MAPPING: |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 832 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 833 | if (strcmp(firstword, "script") == 0) { |
| 834 | if (currmap->script != NULL) { |
| 835 | bb_error_msg("duplicate script in mapping \"%s\"", buf); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 836 | return NULL; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 837 | } else { |
| 838 | currmap->script = xstrdup(next_word(&buf_ptr)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 839 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 840 | } else if (strcmp(firstword, "map") == 0) { |
| 841 | if (currmap->max_mappings == currmap->n_mappings) { |
| 842 | currmap->max_mappings = currmap->max_mappings * 2 + 1; |
| 843 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); |
| 844 | } |
| 845 | currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr)); |
| 846 | currmap->n_mappings++; |
| 847 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 848 | bb_error_msg("misplaced option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 849 | return NULL; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 850 | } |
| 851 | #endif |
| 852 | break; |
| 853 | case NONE: |
| 854 | default: |
| 855 | bb_error_msg("misplaced option \"%s\"", buf); |
| 856 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 857 | } |
| 858 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 859 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 860 | } |
| 861 | if (ferror(f) != 0) { |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 862 | /* ferror does NOT set errno! */ |
| 863 | bb_error_msg_and_die("%s: I/O error", filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 864 | } |
| 865 | fclose(f); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 866 | |
| 867 | return defn; |
| 868 | } |
| 869 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 870 | static char *setlocalenv(const char *format, const char *name, const char *value) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 871 | { |
| 872 | char *result; |
| 873 | char *here; |
| 874 | char *there; |
| 875 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 876 | result = xasprintf(format, name, value); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 877 | |
| 878 | for (here = there = result; *there != '=' && *there; there++) { |
| 879 | if (*there == '-') |
| 880 | *there = '_'; |
| 881 | if (isalpha(*there)) |
| 882 | *there = toupper(*there); |
| 883 | |
| 884 | if (isalnum(*there) || *there == '_') { |
| 885 | *here = *there; |
| 886 | here++; |
| 887 | } |
| 888 | } |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 889 | memmove(here, there, strlen(there) + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 890 | |
| 891 | return result; |
| 892 | } |
| 893 | |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 894 | static void set_environ(struct interface_defn_t *iface, const char *mode) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 895 | { |
| 896 | char **environend; |
| 897 | int i; |
| 898 | const int n_env_entries = iface->n_options + 5; |
| 899 | char **ppch; |
| 900 | |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 901 | if (my_environ != NULL) { |
| 902 | for (ppch = my_environ; *ppch; ppch++) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 903 | free(*ppch); |
| 904 | *ppch = NULL; |
| 905 | } |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 906 | free(my_environ); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 907 | } |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 908 | my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ )); |
| 909 | environend = my_environ; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 910 | |
| 911 | for (i = 0; i < iface->n_options; i++) { |
| 912 | if (strcmp(iface->option[i].name, "up") == 0 |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 913 | || strcmp(iface->option[i].name, "down") == 0 |
| 914 | || strcmp(iface->option[i].name, "pre-up") == 0 |
| 915 | || strcmp(iface->option[i].name, "post-down") == 0 |
| 916 | ) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 917 | continue; |
| 918 | } |
| 919 | *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 923 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 924 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 925 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 926 | *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | static int doit(char *str) |
| 930 | { |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 931 | if (option_mask32 & (OPT_no_act|OPT_verbose)) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 932 | puts(str); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 933 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 934 | if (!(option_mask32 & OPT_no_act)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 935 | pid_t child; |
| 936 | int status; |
| 937 | |
| 938 | fflush(NULL); |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 939 | child = fork(); |
| 940 | switch (child) { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 941 | case -1: /* failure */ |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 942 | return 0; |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 943 | case 0: /* child */ |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 944 | execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 945 | exit(127); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 946 | } |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 947 | safe_waitpid(child, &status, 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 948 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 949 | return 0; |
| 950 | } |
| 951 | } |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 952 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 955 | static int execute_all(struct interface_defn_t *ifd, const char *opt) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 956 | { |
| 957 | int i; |
Eric Andersen | eb213bd | 2003-09-12 08:39:05 +0000 | [diff] [blame] | 958 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 959 | for (i = 0; i < ifd->n_options; i++) { |
| 960 | if (strcmp(ifd->option[i].name, opt) == 0) { |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 961 | if (!doit(ifd->option[i].value)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 962 | return 0; |
| 963 | } |
| 964 | } |
| 965 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 966 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 967 | buf = xasprintf("run-parts /etc/network/if-%s.d", opt); |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 968 | /* heh, we don't bother free'ing it */ |
| 969 | return doit(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 970 | } |
| 971 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 972 | static int check(char *str) |
| 973 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 974 | return str != NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 977 | static int iface_up(struct interface_defn_t *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 978 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 979 | if (!iface->method->up(iface, check)) return -1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 980 | set_environ(iface, "start"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 981 | if (!execute_all(iface, "pre-up")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 982 | if (!iface->method->up(iface, doit)) return 0; |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 983 | if (!execute_all(iface, "up")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 984 | return 1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | static int iface_down(struct interface_defn_t *iface) |
| 988 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 989 | if (!iface->method->down(iface,check)) return -1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 990 | set_environ(iface, "stop"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 991 | if (!execute_all(iface, "down")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 992 | if (!iface->method->down(iface, doit)) return 0; |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 993 | if (!execute_all(iface, "post-down")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 994 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 995 | } |
| 996 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 997 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 998 | static int popen2(FILE **in, FILE **out, char *command, ...) |
| 999 | { |
| 1000 | va_list ap; |
| 1001 | char *argv[11] = { command }; |
| 1002 | int argc; |
| 1003 | int infd[2], outfd[2]; |
| 1004 | pid_t pid; |
| 1005 | |
| 1006 | argc = 1; |
| 1007 | va_start(ap, command); |
| 1008 | while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) { |
| 1009 | argc++; |
| 1010 | } |
| 1011 | argv[argc] = NULL; /* make sure */ |
| 1012 | va_end(ap); |
| 1013 | |
| 1014 | if (pipe(infd) != 0) { |
| 1015 | return 0; |
| 1016 | } |
| 1017 | |
| 1018 | if (pipe(outfd) != 0) { |
| 1019 | close(infd[0]); |
| 1020 | close(infd[1]); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | fflush(NULL); |
| 1025 | switch (pid = fork()) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1026 | case -1: /* failure */ |
| 1027 | close(infd[0]); |
| 1028 | close(infd[1]); |
| 1029 | close(outfd[0]); |
| 1030 | close(outfd[1]); |
| 1031 | return 0; |
| 1032 | case 0: /* child */ |
| 1033 | dup2(infd[0], 0); |
| 1034 | dup2(outfd[1], 1); |
| 1035 | close(infd[0]); |
| 1036 | close(infd[1]); |
| 1037 | close(outfd[0]); |
| 1038 | close(outfd[1]); |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 1039 | BB_EXECVP(command, argv); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1040 | exit(127); |
| 1041 | default: /* parent */ |
| 1042 | *in = fdopen(infd[1], "w"); |
| 1043 | *out = fdopen(outfd[0], "r"); |
| 1044 | close(infd[0]); |
| 1045 | close(outfd[1]); |
| 1046 | return pid; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1047 | } |
| 1048 | /* unreached */ |
| 1049 | } |
| 1050 | |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 1051 | static char *run_mapping(char *physical, struct mapping_defn_t * map) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1052 | { |
| 1053 | FILE *in, *out; |
| 1054 | int i, status; |
| 1055 | pid_t pid; |
| 1056 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1057 | char *logical = xstrdup(physical); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1058 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1059 | /* Run the mapping script. */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1060 | pid = popen2(&in, &out, map->script, physical, NULL); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1061 | |
| 1062 | /* popen2() returns 0 on failure. */ |
| 1063 | if (pid == 0) |
| 1064 | return logical; |
| 1065 | |
| 1066 | /* Write mappings to stdin of mapping script. */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1067 | for (i = 0; i < map->n_mappings; i++) { |
| 1068 | fprintf(in, "%s\n", map->mapping[i]); |
| 1069 | } |
| 1070 | fclose(in); |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1071 | safe_waitpid(pid, &status, 0); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1072 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1073 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1074 | /* If the mapping script exited successfully, try to |
| 1075 | * grab a line of output and use that as the name of the |
| 1076 | * logical interface. */ |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 1077 | char *new_logical = xmalloc(MAX_INTERFACE_LENGTH); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1078 | |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 1079 | if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1080 | /* If we are able to read a line of output from the script, |
| 1081 | * remove any trailing whitespace and use this value |
| 1082 | * as the name of the logical interface. */ |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1083 | char *pch = new_logical + strlen(new_logical) - 1; |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 1084 | |
| 1085 | while (pch >= new_logical && isspace(*pch)) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1086 | *(pch--) = '\0'; |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1087 | |
| 1088 | free(logical); |
| 1089 | logical = new_logical; |
| 1090 | } else { |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 1091 | /* If we are UNABLE to read a line of output, discard our |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1092 | * freshly allocated memory. */ |
| 1093 | free(new_logical); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1094 | } |
| 1095 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1096 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1097 | fclose(out); |
| 1098 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1099 | return logical; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1100 | } |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1101 | #endif /* FEATURE_IFUPDOWN_MAPPING */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1102 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1103 | static llist_t *find_iface_state(llist_t *state_list, const char *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1104 | { |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1105 | unsigned short iface_len = strlen(iface); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1106 | llist_t *search = state_list; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1107 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1108 | while (search) { |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 1109 | if ((strncmp(search->data, iface, iface_len) == 0) |
| 1110 | && (search->data[iface_len] == '=')) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1111 | return search; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1112 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1113 | search = search->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1114 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1115 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1116 | } |
| 1117 | |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1118 | /* read the previous state from the state file */ |
| 1119 | static llist_t *read_iface_state(void) |
| 1120 | { |
| 1121 | llist_t *state_list = NULL; |
Denis Vlasenko | 2a86a61 | 2007-07-19 21:49:30 +0000 | [diff] [blame] | 1122 | FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r"); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1123 | |
| 1124 | if (state_fp) { |
| 1125 | char *start, *end_ptr; |
| 1126 | while ((start = xmalloc_fgets(state_fp)) != NULL) { |
| 1127 | /* We should only need to check for a single character */ |
| 1128 | end_ptr = start + strcspn(start, " \t\n"); |
| 1129 | *end_ptr = '\0'; |
| 1130 | llist_add_to(&state_list, start); |
| 1131 | } |
| 1132 | fclose(state_fp); |
| 1133 | } |
| 1134 | return state_list; |
| 1135 | } |
| 1136 | |
| 1137 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1138 | int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 1139 | int ifupdown_main(int argc, char **argv) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1140 | { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1141 | int (*cmds)(struct interface_defn_t *) = NULL; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1142 | struct interfaces_file_t *defn; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1143 | llist_t *target_list = NULL; |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 1144 | const char *interfaces = "/etc/network/interfaces"; |
Bernhard Reutner-Fischer | 16deb86 | 2007-03-19 19:54:56 +0000 | [diff] [blame] | 1145 | bool any_failures = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1146 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1147 | cmds = iface_down; |
Denis Vlasenko | 8f8f268 | 2006-10-03 21:00:43 +0000 | [diff] [blame] | 1148 | if (applet_name[2] == 'u') { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1149 | /* ifup command */ |
| 1150 | cmds = iface_up; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 1153 | getopt32(argv, OPTION_STR, &interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1154 | if (argc - optind > 0) { |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1155 | if (DO_ALL) bb_show_usage(); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1156 | } else { |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1157 | if (!DO_ALL) bb_show_usage(); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1158 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1159 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1160 | debug_noise("reading %s file:\n", interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1161 | defn = read_interfaces(interfaces); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1162 | debug_noise("\ndone reading %s\n\n", interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1163 | |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 1164 | if (!defn) { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1165 | return EXIT_FAILURE; |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 1168 | startup_PATH = getenv("PATH"); |
| 1169 | if (!startup_PATH) startup_PATH = ""; |
| 1170 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1171 | /* Create a list of interfaces to work on */ |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1172 | if (DO_ALL) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1173 | target_list = defn->autointerfaces; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1174 | } else { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1175 | llist_add_to_end(&target_list, argv[optind]); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1178 | /* Update the interfaces */ |
| 1179 | while (target_list) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1180 | llist_t *iface_list; |
| 1181 | struct interface_defn_t *currif; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1182 | char *iface; |
| 1183 | char *liface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1184 | char *pch; |
Bernhard Reutner-Fischer | 16deb86 | 2007-03-19 19:54:56 +0000 | [diff] [blame] | 1185 | bool okay = 0; |
| 1186 | unsigned cmds_ret; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1187 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1188 | iface = xstrdup(target_list->data); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1189 | target_list = target_list->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1190 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1191 | pch = strchr(iface, '='); |
| 1192 | if (pch) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1193 | *pch = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1194 | liface = xstrdup(pch + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1195 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1196 | liface = xstrdup(iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1197 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1198 | |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1199 | if (!FORCE) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1200 | llist_t *state_list = read_iface_state(); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1201 | const llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1202 | |
| 1203 | if (cmds == iface_up) { |
| 1204 | /* ifup */ |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1205 | if (iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1206 | bb_error_msg("interface %s already configured", iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1207 | continue; |
| 1208 | } |
| 1209 | } else { |
| 1210 | /* ifdown */ |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 1211 | if (!iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1212 | bb_error_msg("interface %s not configured", iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1213 | continue; |
| 1214 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1215 | } |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1216 | llist_free(state_list, free); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1217 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1218 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1219 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1220 | if ((cmds == iface_up) && !NO_MAPPINGS) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1221 | struct mapping_defn_t *currmap; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1222 | |
| 1223 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
Denis Vlasenko | 4e33e07 | 2006-10-16 18:24:57 +0000 | [diff] [blame] | 1224 | int i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1225 | for (i = 0; i < currmap->n_matches; i++) { |
| 1226 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
| 1227 | continue; |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1228 | if (VERBOSE) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1229 | printf("Running mapping script %s on %s\n", currmap->script, liface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1230 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1231 | liface = run_mapping(iface, currmap); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1232 | break; |
| 1233 | } |
| 1234 | } |
| 1235 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1236 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1237 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1238 | iface_list = defn->ifaces; |
| 1239 | while (iface_list) { |
| 1240 | currif = (struct interface_defn_t *) iface_list->data; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1241 | if (strcmp(liface, currif->iface) == 0) { |
| 1242 | char *oldiface = currif->iface; |
| 1243 | |
| 1244 | okay = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1245 | currif->iface = iface; |
| 1246 | |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 1247 | debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1248 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1249 | /* Call the cmds function pointer, does either iface_up() or iface_down() */ |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 1250 | cmds_ret = cmds(currif); |
| 1251 | if (cmds_ret == -1) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1252 | bb_error_msg("don't seem to have all the variables for %s/%s", |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1253 | liface, currif->address_family->name); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1254 | any_failures = 1; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 1255 | } else if (cmds_ret == 0) { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1256 | any_failures = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1257 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1258 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1259 | currif->iface = oldiface; |
| 1260 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1261 | iface_list = iface_list->link; |
| 1262 | } |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1263 | if (VERBOSE) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1264 | bb_putchar('\n'); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1267 | if (!okay && !FORCE) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1268 | bb_error_msg("ignoring unknown interface %s", liface); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1269 | any_failures = 1; |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1270 | } else if (!NO_ACT) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1271 | /* update the state file */ |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1272 | FILE *state_fp; |
| 1273 | llist_t *state; |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1274 | llist_t *state_list = read_iface_state(); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1275 | llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1276 | |
| 1277 | if (cmds == iface_up) { |
Bernhard Reutner-Fischer | 16deb86 | 2007-03-19 19:54:56 +0000 | [diff] [blame] | 1278 | char * const newiface = xasprintf("%s=%s", iface, liface); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1279 | if (iface_state == NULL) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1280 | llist_add_to_end(&state_list, newiface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1281 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1282 | free(iface_state->data); |
| 1283 | iface_state->data = newiface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1284 | } |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1285 | } else { |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 1286 | /* Remove an interface from state_list */ |
| 1287 | llist_unlink(&state_list, iface_state); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 1288 | free(llist_pop(&iface_state)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1289 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1290 | |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1291 | /* Actually write the new state */ |
Denis Vlasenko | 2a86a61 | 2007-07-19 21:49:30 +0000 | [diff] [blame] | 1292 | state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w"); |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1293 | state = state_list; |
| 1294 | while (state) { |
| 1295 | if (state->data) { |
| 1296 | fprintf(state_fp, "%s\n", state->data); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1297 | } |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1298 | state = state->link; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1299 | } |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1300 | fclose(state_fp); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1301 | llist_free(state_list, free); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1302 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1303 | } |
| 1304 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1305 | return any_failures; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1306 | } |