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