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