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