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 |
Glenn L McGrath | c6992fe | 2004-04-25 05:11:19 +0000 | [diff] [blame] | 4 | * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au> |
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. |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 13 | * To adhere to the FHS, the default state file is /var/run/ifstate. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2 of the License, or |
| 18 | * (at your option) any later version. |
| 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. |
| 24 | * |
| 25 | * You should have received a copy of the GNU General Public License |
| 26 | * along with this program; if not, write to the Free Software |
| 27 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 28 | */ |
| 29 | |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 30 | /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */ |
| 31 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 32 | #include <sys/stat.h> |
| 33 | #include <sys/utsname.h> |
| 34 | #include <sys/wait.h> |
| 35 | |
| 36 | #include <ctype.h> |
| 37 | #include <errno.h> |
| 38 | #include <fcntl.h> |
| 39 | #include <fnmatch.h> |
| 40 | #include <getopt.h> |
| 41 | #include <stdarg.h> |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
| 45 | #include <unistd.h> |
| 46 | |
Glenn L McGrath | 9af8a72 | 2002-11-11 07:03:02 +0000 | [diff] [blame] | 47 | #include "libbb.h" |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 48 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 49 | #define MAX_OPT_DEPTH 10 |
| 50 | #define EUNBALBRACK 10001 |
| 51 | #define EUNDEFVAR 10002 |
| 52 | #define EUNBALPER 10000 |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 53 | |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 54 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
| 55 | #define MAX_INTERFACE_LENGTH 10 |
| 56 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 57 | |
| 58 | #if 0 |
| 59 | #define debug_noise(fmt, args...) printf(fmt, ## args) |
| 60 | #else |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 61 | #define debug_noise(fmt, args...) |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 62 | #endif |
| 63 | |
| 64 | /* Forward declaration */ |
| 65 | struct interface_defn_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 66 | |
| 67 | typedef int (execfn)(char *command); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 68 | typedef int (command_set)(struct interface_defn_t *ifd, execfn *e); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 69 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 70 | extern llist_t *llist_add_to_end(llist_t *list_head, char *data) |
| 71 | { |
| 72 | llist_t *new_item, *tmp, *prev; |
| 73 | |
| 74 | new_item = xmalloc(sizeof(llist_t)); |
| 75 | new_item->data = data; |
| 76 | new_item->link = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 77 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 78 | prev = NULL; |
| 79 | tmp = list_head; |
| 80 | while(tmp) { |
| 81 | prev = tmp; |
| 82 | tmp = tmp->link; |
| 83 | } |
| 84 | if (prev) { |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | prev->link = new_item; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 86 | } else { |
| 87 | list_head = new_item; |
| 88 | } |
| 89 | |
| 90 | return(list_head); |
| 91 | } |
| 92 | |
| 93 | struct method_t |
| 94 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 95 | char *name; |
| 96 | command_set *up; |
| 97 | command_set *down; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 98 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 99 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 100 | struct address_family_t |
| 101 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 102 | char *name; |
| 103 | int n_methods; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 104 | struct method_t *method; |
| 105 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 106 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 107 | struct mapping_defn_t |
| 108 | { |
| 109 | struct mapping_defn_t *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 110 | |
| 111 | int max_matches; |
| 112 | int n_matches; |
| 113 | char **match; |
| 114 | |
| 115 | char *script; |
| 116 | |
| 117 | int max_mappings; |
| 118 | int n_mappings; |
| 119 | char **mapping; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 120 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 121 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 122 | struct variable_t |
| 123 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 124 | char *name; |
| 125 | char *value; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 126 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 127 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 128 | struct interface_defn_t |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 129 | { |
| 130 | struct interface_defn_t *prev; |
| 131 | struct interface_defn_t *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 132 | |
| 133 | char *iface; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 134 | struct address_family_t *address_family; |
| 135 | struct method_t *method; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 136 | |
| 137 | int automatic; |
| 138 | |
| 139 | int max_options; |
| 140 | int n_options; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 141 | struct variable_t *option; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 142 | }; |
| 143 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 144 | struct interfaces_file_t |
| 145 | { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 146 | llist_t *autointerfaces; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 147 | llist_t *ifaces; |
| 148 | struct mapping_defn_t *mappings; |
| 149 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 150 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 151 | static char no_act = 0; |
| 152 | static char verbose = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 153 | static char **environ = NULL; |
| 154 | |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 155 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 156 | |
| 157 | static unsigned int count_bits(unsigned int a) |
| 158 | { |
| 159 | unsigned int result; |
| 160 | result = (a & 0x55) + ((a >> 1) & 0x55); |
| 161 | result = (result & 0x33) + ((result >> 2) & 0x33); |
| 162 | return((result & 0x0F) + ((result >> 4) & 0x0F)); |
| 163 | } |
| 164 | |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 165 | static int count_netmask_bits(char *dotted_quad) |
| 166 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 167 | unsigned int result, a, b, c, d; |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 168 | /* Found a netmask... Check if it is dotted quad */ |
| 169 | if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
| 170 | return -1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 171 | result = count_bits(a); |
| 172 | result += count_bits(b); |
| 173 | result += count_bits(c); |
| 174 | result += count_bits(d); |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 175 | return ((int)result); |
| 176 | } |
| 177 | #endif |
| 178 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 179 | static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length) |
| 180 | { |
| 181 | if (*pos + str_length >= *len) { |
| 182 | char *newbuf; |
| 183 | |
| 184 | newbuf = xrealloc(*buf, *len * 2 + str_length + 1); |
| 185 | *buf = newbuf; |
| 186 | *len = *len * 2 + str_length + 1; |
| 187 | } |
| 188 | |
| 189 | while (str_length-- >= 1) { |
| 190 | (*buf)[(*pos)++] = *str; |
| 191 | str++; |
| 192 | } |
| 193 | (*buf)[*pos] = '\0'; |
| 194 | } |
| 195 | |
| 196 | static int strncmpz(char *l, char *r, size_t llen) |
| 197 | { |
| 198 | int i = strncmp(l, r, llen); |
| 199 | |
| 200 | if (i == 0) { |
| 201 | return(-r[llen]); |
| 202 | } else { |
| 203 | return(i); |
| 204 | } |
| 205 | } |
| 206 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 207 | static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 208 | { |
| 209 | int i; |
| 210 | |
| 211 | if (strncmpz(id, "iface", idlen) == 0) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 212 | char *result; |
| 213 | static char label_buf[20]; |
| 214 | strncpy(label_buf, ifd->iface, 19); |
| 215 | label_buf[19]=0; |
| 216 | result = strchr(label_buf, ':'); |
| 217 | if (result) { |
| 218 | *result=0; |
| 219 | } |
| 220 | return( label_buf); |
| 221 | } else if (strncmpz(id, "label", idlen) == 0) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 222 | return (ifd->iface); |
| 223 | } else { |
| 224 | for (i = 0; i < ifd->n_options; i++) { |
| 225 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) { |
| 226 | return (ifd->option[i].value); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | return(NULL); |
| 232 | } |
| 233 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 234 | static char *parse(char *command, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 235 | { |
| 236 | |
| 237 | char *result = NULL; |
| 238 | size_t pos = 0, len = 0; |
| 239 | size_t old_pos[MAX_OPT_DEPTH] = { 0 }; |
| 240 | int okay[MAX_OPT_DEPTH] = { 1 }; |
| 241 | int opt_depth = 1; |
| 242 | |
| 243 | while (*command) { |
| 244 | switch (*command) { |
| 245 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 246 | default: |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 247 | addstr(&result, &len, &pos, command, 1); |
| 248 | command++; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 249 | break; |
| 250 | case '\\': |
| 251 | if (command[1]) { |
| 252 | addstr(&result, &len, &pos, command + 1, 1); |
| 253 | command += 2; |
| 254 | } else { |
| 255 | addstr(&result, &len, &pos, command, 1); |
| 256 | command++; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 257 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 258 | break; |
| 259 | case '[': |
| 260 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { |
| 261 | old_pos[opt_depth] = pos; |
| 262 | okay[opt_depth] = 1; |
| 263 | opt_depth++; |
| 264 | command += 2; |
| 265 | } else { |
| 266 | addstr(&result, &len, &pos, "[", 1); |
| 267 | command++; |
| 268 | } |
| 269 | break; |
| 270 | case ']': |
| 271 | if (command[1] == ']' && opt_depth > 1) { |
| 272 | opt_depth--; |
| 273 | if (!okay[opt_depth]) { |
| 274 | pos = old_pos[opt_depth]; |
| 275 | result[pos] = '\0'; |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 276 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 277 | command += 2; |
| 278 | } else { |
| 279 | addstr(&result, &len, &pos, "]", 1); |
| 280 | command++; |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 281 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 282 | break; |
| 283 | case '%': |
| 284 | { |
| 285 | char *nextpercent; |
| 286 | char *varvalue; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 287 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 288 | command++; |
| 289 | nextpercent = strchr(command, '%'); |
| 290 | if (!nextpercent) { |
| 291 | errno = EUNBALPER; |
| 292 | free(result); |
| 293 | return (NULL); |
| 294 | } |
| 295 | |
| 296 | varvalue = get_var(command, nextpercent - command, ifd); |
| 297 | |
| 298 | if (varvalue) { |
| 299 | addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue)); |
| 300 | } else { |
| 301 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 302 | /* Sigh... Add a special case for 'ip' to convert from |
| 303 | * dotted quad to bit count style netmasks. */ |
| 304 | if (strncmp(command, "bnmask", 6)==0) { |
| 305 | int res; |
| 306 | varvalue = get_var("netmask", 7, ifd); |
| 307 | if (varvalue && (res=count_netmask_bits(varvalue)) > 0) { |
| 308 | char argument[255]; |
| 309 | sprintf(argument, "%d", res); |
| 310 | addstr(&result, &len, &pos, argument, bb_strlen(argument)); |
| 311 | command = nextpercent + 1; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | #endif |
| 316 | okay[opt_depth - 1] = 0; |
| 317 | } |
| 318 | |
| 319 | command = nextpercent + 1; |
| 320 | } |
| 321 | break; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | |
| 325 | if (opt_depth > 1) { |
| 326 | errno = EUNBALBRACK; |
| 327 | free(result); |
| 328 | return(NULL); |
| 329 | } |
| 330 | |
| 331 | if (!okay[0]) { |
| 332 | errno = EUNDEFVAR; |
| 333 | free(result); |
| 334 | return(NULL); |
| 335 | } |
| 336 | |
| 337 | return(result); |
| 338 | } |
| 339 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 340 | static int execute(char *command, struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 341 | { |
| 342 | char *out; |
| 343 | int ret; |
| 344 | |
| 345 | out = parse(command, ifd); |
| 346 | if (!out) { |
| 347 | return(0); |
| 348 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 349 | ret = (*exec) (out); |
| 350 | |
| 351 | free(out); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 352 | if (ret != 1) { |
| 353 | return(0); |
| 354 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 355 | return(1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPX |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 359 | static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 360 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 361 | return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 364 | static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 365 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 366 | return(execute("ipx_interface del %iface% %frame%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 369 | static int dynamic_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 370 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 371 | return(execute("ipx_interface add %iface% %frame%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 374 | static int dynamic_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 375 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 376 | return(execute("ipx_interface del %iface% %frame%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 379 | static struct method_t methods_ipx[] = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 380 | { "dynamic", dynamic_up, dynamic_down, }, |
| 381 | { "static", static_up_ipx, static_down_ipx, }, |
| 382 | }; |
| 383 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 384 | struct address_family_t addr_ipx = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 385 | "ipx", |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 386 | sizeof(methods_ipx) / sizeof(struct method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 387 | methods_ipx |
| 388 | }; |
| 389 | #endif /* IFUP_FEATURE_IPX */ |
| 390 | |
| 391 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 392 | static int loopback_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 393 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 394 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 395 | int result; |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 396 | result =execute("ip addr add ::1 dev %iface%", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 397 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 398 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 399 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 400 | return( execute("ifconfig %iface% add ::1", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 401 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 404 | static int loopback_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 405 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 406 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 407 | return(execute("ip link set %iface% down", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 408 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 409 | return(execute("ifconfig %iface% del ::1", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 410 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 413 | static int static_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 414 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 415 | int result; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 416 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 417 | result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec); |
| 418 | result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 419 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 420 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 421 | result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec); |
| 422 | result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec); |
| 423 | result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 424 | #endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 425 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 428 | static int static_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 429 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 430 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 431 | return(execute("ip link set %iface% down", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 432 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 433 | return(execute("ifconfig %iface% down", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 434 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 437 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 438 | static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 439 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 440 | int result; |
| 441 | result = execute("ip tunnel add %iface% mode sit remote " |
| 442 | "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 443 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 62b031f | 2003-08-29 07:47:52 +0000 | [diff] [blame] | 444 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 445 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 446 | return ((result == 4) ? 4 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 449 | static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 450 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 451 | return( execute("ip tunnel del %iface%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 452 | } |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 453 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 454 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 455 | static struct method_t methods6[] = { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 456 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 457 | { "v4tunnel", v4tunnel_up, v4tunnel_down, }, |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 458 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 459 | { "static", static_up6, static_down6, }, |
| 460 | { "loopback", loopback_up6, loopback_down6, }, |
| 461 | }; |
| 462 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 463 | struct address_family_t addr_inet6 = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 464 | "inet6", |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 465 | sizeof(methods6) / sizeof(struct method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 466 | methods6 |
| 467 | }; |
| 468 | #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */ |
| 469 | |
| 470 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 471 | static int loopback_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 472 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 473 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 474 | int result; |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 475 | 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] | 476 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 477 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 478 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 479 | return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 480 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 483 | static int loopback_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 484 | { |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 485 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 486 | int result; |
| 487 | result = execute("ip addr flush dev %iface%", ifd, exec); |
| 488 | result += execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 489 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 490 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 491 | return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec)); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 492 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 493 | } |
| 494 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 495 | static int static_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 496 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 497 | int result; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 498 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 499 | result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] " |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 500 | "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec); |
| 501 | result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 502 | 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^] | 503 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 504 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 505 | result = execute("ifconfig %iface% %address% netmask %netmask% " |
| 506 | "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] " |
| 507 | "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up", |
| 508 | ifd, exec); |
| 509 | result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 510 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 511 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 514 | static int static_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 515 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 516 | int result; |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 517 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 518 | result = execute("ip addr flush dev %iface%", ifd, exec); |
| 519 | result += execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 520 | #else |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 521 | result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec); |
| 522 | result += execute("ifconfig %iface% down", ifd, exec); |
Glenn L McGrath | d66370c | 2003-01-13 21:40:38 +0000 | [diff] [blame] | 523 | #endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 524 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Glenn L McGrath | 49a28b3 | 2002-11-10 13:17:08 +0000 | [diff] [blame] | 527 | static int execable(char *program) |
| 528 | { |
| 529 | struct stat buf; |
| 530 | if (0 == stat(program, &buf)) { |
| 531 | if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) { |
| 532 | return(1); |
| 533 | } |
| 534 | } |
| 535 | return(0); |
| 536 | } |
| 537 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 538 | static int dhcp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 539 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 540 | if (execable("/sbin/udhcpc")) { |
| 541 | return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i " |
| 542 | "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 543 | } else if (execable("/sbin/pump")) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 544 | return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec)); |
| 545 | } else if (execable("/sbin/dhclient")) { |
| 546 | return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 547 | } else if (execable("/sbin/dhcpcd")) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 548 | return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] " |
| 549 | "[[-l %leasetime%]] %iface%", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 550 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 551 | return(0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 552 | } |
| 553 | |
Eric Andersen | 5e136f2 | 2004-07-20 06:35:54 +0000 | [diff] [blame] | 554 | static int bootp_down(struct interface_defn_t *ifd, execfn *exec) |
| 555 | { |
| 556 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP |
| 557 | return(execute("ip link set %iface% down", ifd, exec)); |
| 558 | #else |
| 559 | return(execute("ifconfig %iface% down", ifd, exec)); |
| 560 | #endif |
| 561 | } |
| 562 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 563 | static int dhcp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 564 | { |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 565 | int result = 0; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 566 | if (execable("/sbin/udhcpc")) { |
| 567 | execute("kill -9 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 568 | } else if (execable("/sbin/pump")) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 569 | result = execute("pump -i %iface% -k", ifd, exec); |
| 570 | } else if (execable("/sbin/dhclient")) { |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 571 | result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 572 | } else if (execable("/sbin/dhcpcd")) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 573 | result = execute("dhcpcd -k %iface%", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 574 | } |
Eric Andersen | 238e354 | 2004-04-12 20:57:17 +0000 | [diff] [blame] | 575 | return (result || bootp_down(ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 578 | static int bootp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 579 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 580 | return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% " |
| 581 | "[[--server %server%]] [[--hwaddr %hwaddr%]] " |
| 582 | "--returniffail --serverbcast", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 583 | } |
| 584 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 585 | static int ppp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 586 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 587 | return( execute("pon [[%provider%]]", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 588 | } |
| 589 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 590 | static int ppp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 591 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 592 | return( execute("poff [[%provider%]]", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 595 | static int wvdial_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 596 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 597 | return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial " |
| 598 | "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 601 | static int wvdial_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 602 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 603 | return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial " |
| 604 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 607 | static struct method_t methods[] = |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 608 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 609 | { "wvdial", wvdial_up, wvdial_down, }, |
| 610 | { "ppp", ppp_up, ppp_down, }, |
| 611 | { "static", static_up, static_down, }, |
| 612 | { "bootp", bootp_up, bootp_down, }, |
| 613 | { "dhcp", dhcp_up, dhcp_down, }, |
| 614 | { "loopback", loopback_up, loopback_down, }, |
| 615 | }; |
| 616 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 617 | struct address_family_t addr_inet = |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 618 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 619 | "inet", |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 620 | sizeof(methods) / sizeof(struct method_t), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 621 | methods |
| 622 | }; |
| 623 | |
| 624 | #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */ |
| 625 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 626 | static char *next_word(char **buf) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 627 | { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 628 | unsigned short length; |
| 629 | char *word; |
| 630 | |
| 631 | if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 632 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 635 | /* Skip over leading whitespace */ |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 636 | word = *buf; |
| 637 | while (isspace(*word)) { |
| 638 | ++word; |
| 639 | } |
| 640 | |
| 641 | /* Skip over comments */ |
| 642 | if (*word == '#') { |
| 643 | return(NULL); |
| 644 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 645 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 646 | /* Find the length of this word */ |
| 647 | length = strcspn(word, " \t\n"); |
| 648 | if (length == 0) { |
| 649 | return(NULL); |
| 650 | } |
| 651 | *buf = word + length; |
Eric Andersen | 2894266 | 2003-04-19 23:15:06 +0000 | [diff] [blame] | 652 | /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */ |
| 653 | if (**buf) { |
| 654 | **buf = '\0'; |
| 655 | (*buf)++; |
| 656 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 657 | |
| 658 | return word; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 661 | static struct address_family_t *get_address_family(struct address_family_t *af[], char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 662 | { |
| 663 | int i; |
| 664 | |
| 665 | for (i = 0; af[i]; i++) { |
| 666 | if (strcmp(af[i]->name, name) == 0) { |
| 667 | return af[i]; |
| 668 | } |
| 669 | } |
| 670 | return NULL; |
| 671 | } |
| 672 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 673 | static struct method_t *get_method(struct address_family_t *af, char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 674 | { |
| 675 | int i; |
| 676 | |
| 677 | for (i = 0; i < af->n_methods; i++) { |
| 678 | if (strcmp(af->method[i].name, name) == 0) { |
| 679 | return &af->method[i]; |
| 680 | } |
| 681 | } |
| 682 | return(NULL); |
| 683 | } |
| 684 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 685 | static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 686 | { |
| 687 | if (strcmp(ifa->iface, ifb->iface) != 0) { |
| 688 | return(0); |
| 689 | } |
| 690 | if (ifa->address_family != ifb->address_family) { |
| 691 | return(0); |
| 692 | } |
| 693 | return(1); |
| 694 | } |
| 695 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 696 | static const llist_t *find_list_string(const llist_t *list, const char *string) |
| 697 | { |
| 698 | while (list) { |
| 699 | if (strcmp(list->data, string) == 0) { |
| 700 | return(list); |
| 701 | } |
| 702 | list = list->link; |
| 703 | } |
| 704 | return(NULL); |
| 705 | } |
| 706 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 707 | static struct interfaces_file_t *read_interfaces(char *filename) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 708 | { |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 709 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 710 | struct mapping_defn_t *currmap = NULL; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 711 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 712 | struct interface_defn_t *currif = NULL; |
| 713 | struct interfaces_file_t *defn; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 714 | FILE *f; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 715 | char *firstword; |
| 716 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 717 | |
| 718 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
| 719 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 720 | defn = xmalloc(sizeof(struct interfaces_file_t)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 721 | defn->autointerfaces = NULL; |
| 722 | defn->mappings = NULL; |
| 723 | defn->ifaces = NULL; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 724 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 725 | f = bb_xfopen(filename, "r"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 726 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 727 | while ((buf = bb_get_chomped_line_from_file(f)) != NULL) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 728 | char *buf_ptr = buf; |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 729 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 730 | firstword = next_word(&buf_ptr); |
| 731 | if (firstword == NULL) { |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 732 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 733 | continue; /* blank line */ |
| 734 | } |
| 735 | |
| 736 | if (strcmp(firstword, "mapping") == 0) { |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 737 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 738 | currmap = xmalloc(sizeof(struct mapping_defn_t)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 739 | currmap->max_matches = 0; |
| 740 | currmap->n_matches = 0; |
| 741 | currmap->match = NULL; |
| 742 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 743 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 744 | if (currmap->max_matches == currmap->n_matches) { |
| 745 | currmap->max_matches = currmap->max_matches * 2 + 1; |
| 746 | currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches); |
| 747 | } |
| 748 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 749 | currmap->match[currmap->n_matches++] = bb_xstrdup(firstword); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 750 | } |
| 751 | currmap->max_mappings = 0; |
| 752 | currmap->n_mappings = 0; |
| 753 | currmap->mapping = NULL; |
| 754 | currmap->script = NULL; |
| 755 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 756 | struct mapping_defn_t **where = &defn->mappings; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 757 | while (*where != NULL) { |
| 758 | where = &(*where)->next; |
| 759 | } |
| 760 | *where = currmap; |
| 761 | currmap->next = NULL; |
| 762 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 763 | debug_noise("Added mapping\n"); |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 764 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 765 | currently_processing = MAPPING; |
| 766 | } else if (strcmp(firstword, "iface") == 0) { |
| 767 | { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 768 | char *iface_name; |
| 769 | char *address_family_name; |
| 770 | char *method_name; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 771 | struct address_family_t *addr_fams[] = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 772 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 |
| 773 | &addr_inet, |
| 774 | #endif |
| 775 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6 |
| 776 | &addr_inet6, |
| 777 | #endif |
| 778 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPX |
| 779 | &addr_ipx, |
| 780 | #endif |
| 781 | NULL |
| 782 | }; |
| 783 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 784 | currif = xmalloc(sizeof(struct interface_defn_t)); |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 785 | iface_name = next_word(&buf_ptr); |
| 786 | address_family_name = next_word(&buf_ptr); |
| 787 | method_name = next_word(&buf_ptr); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 788 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 789 | if (buf_ptr == NULL) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 790 | bb_error_msg("too few parameters for line \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 791 | return NULL; |
| 792 | } |
| 793 | |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 794 | /* ship any trailing whitespace */ |
| 795 | while (isspace(*buf_ptr)) { |
| 796 | ++buf_ptr; |
| 797 | } |
| 798 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 799 | if (buf_ptr[0] != '\0') { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 800 | bb_error_msg("too many parameters \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 801 | return NULL; |
| 802 | } |
| 803 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 804 | currif->iface = bb_xstrdup(iface_name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 805 | |
| 806 | currif->address_family = get_address_family(addr_fams, address_family_name); |
| 807 | if (!currif->address_family) { |
Eric Andersen | fe9b9cd | 2004-06-29 00:48:30 +0000 | [diff] [blame] | 808 | bb_error_msg("unknown address type \"%s\"", address_family_name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 809 | return NULL; |
| 810 | } |
| 811 | |
| 812 | currif->method = get_method(currif->address_family, method_name); |
| 813 | if (!currif->method) { |
Eric Andersen | fe9b9cd | 2004-06-29 00:48:30 +0000 | [diff] [blame] | 814 | bb_error_msg("unknown method \"%s\"", method_name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 815 | return NULL; |
| 816 | } |
| 817 | |
| 818 | currif->automatic = 1; |
| 819 | currif->max_options = 0; |
| 820 | currif->n_options = 0; |
| 821 | currif->option = NULL; |
| 822 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 823 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 824 | struct interface_defn_t *tmp; |
| 825 | llist_t *iface_list; |
| 826 | iface_list = defn->ifaces; |
| 827 | while (iface_list) { |
| 828 | tmp = (struct interface_defn_t *) iface_list->data; |
| 829 | if (duplicate_if(tmp, currif)) { |
| 830 | bb_error_msg("duplicate interface \"%s\"", tmp->iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 831 | return NULL; |
| 832 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 833 | iface_list = iface_list->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 836 | defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 837 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 838 | 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] | 839 | } |
| 840 | currently_processing = IFACE; |
| 841 | } else if (strcmp(firstword, "auto") == 0) { |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 842 | while ((firstword = next_word(&buf_ptr)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 843 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 844 | /* Check the interface isnt already listed */ |
| 845 | if (find_list_string(defn->autointerfaces, firstword)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 846 | bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 849 | /* Add the interface to the list */ |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 850 | defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword)); |
| 851 | debug_noise("\nauto %s\n", firstword); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 852 | } |
| 853 | currently_processing = NONE; |
| 854 | } else { |
| 855 | switch (currently_processing) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 856 | case IFACE: |
| 857 | { |
| 858 | int i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 859 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 860 | if (bb_strlen(buf_ptr) == 0) { |
| 861 | bb_error_msg("option with empty value \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 862 | return NULL; |
| 863 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 864 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 865 | if (strcmp(firstword, "up") != 0 |
| 866 | && strcmp(firstword, "down") != 0 |
| 867 | && strcmp(firstword, "pre-up") != 0 |
| 868 | && strcmp(firstword, "post-down") != 0) { |
| 869 | for (i = 0; i < currif->n_options; i++) { |
| 870 | if (strcmp(currif->option[i].name, firstword) == 0) { |
| 871 | bb_error_msg("duplicate option \"%s\"", buf); |
| 872 | return NULL; |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | if (currif->n_options >= currif->max_options) { |
| 878 | struct variable_t *opt; |
| 879 | |
| 880 | currif->max_options = currif->max_options + 10; |
| 881 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options); |
| 882 | currif->option = opt; |
| 883 | } |
| 884 | currif->option[currif->n_options].name = bb_xstrdup(firstword); |
| 885 | currif->option[currif->n_options].value = bb_xstrdup(buf_ptr); |
| 886 | if (!currif->option[currif->n_options].name) { |
| 887 | perror(filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 888 | return NULL; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 889 | } |
| 890 | if (!currif->option[currif->n_options].value) { |
| 891 | perror(filename); |
| 892 | return NULL; |
| 893 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 894 | debug_noise("\t%s=%s\n", currif->option[currif->n_options].name, |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 895 | currif->option[currif->n_options].value); |
| 896 | currif->n_options++; |
| 897 | break; |
| 898 | case MAPPING: |
| 899 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
| 900 | if (strcmp(firstword, "script") == 0) { |
| 901 | if (currmap->script != NULL) { |
| 902 | bb_error_msg("duplicate script in mapping \"%s\"", buf); |
| 903 | return NULL; |
| 904 | } else { |
| 905 | currmap->script = bb_xstrdup(next_word(&buf_ptr)); |
| 906 | } |
| 907 | } else if (strcmp(firstword, "map") == 0) { |
| 908 | if (currmap->max_mappings == currmap->n_mappings) { |
| 909 | currmap->max_mappings = currmap->max_mappings * 2 + 1; |
| 910 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings); |
| 911 | } |
| 912 | currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr)); |
| 913 | currmap->n_mappings++; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 914 | } else { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 915 | bb_error_msg("misplaced option \"%s\"", buf); |
| 916 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 917 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 918 | #endif |
| 919 | break; |
| 920 | case NONE: |
| 921 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 922 | bb_error_msg("misplaced option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 923 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 924 | } |
| 925 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 926 | free(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 927 | } |
| 928 | if (ferror(f) != 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 929 | bb_perror_msg_and_die("%s", filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 930 | } |
| 931 | fclose(f); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 932 | |
| 933 | return defn; |
| 934 | } |
| 935 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 936 | static char *setlocalenv(char *format, char *name, char *value) |
| 937 | { |
| 938 | char *result; |
| 939 | char *here; |
| 940 | char *there; |
| 941 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 942 | result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 943 | |
| 944 | sprintf(result, format, name, value); |
| 945 | |
| 946 | for (here = there = result; *there != '=' && *there; there++) { |
| 947 | if (*there == '-') |
| 948 | *there = '_'; |
| 949 | if (isalpha(*there)) |
| 950 | *there = toupper(*there); |
| 951 | |
| 952 | if (isalnum(*there) || *there == '_') { |
| 953 | *here = *there; |
| 954 | here++; |
| 955 | } |
| 956 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 957 | memmove(here, there, bb_strlen(there) + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 958 | |
| 959 | return result; |
| 960 | } |
| 961 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 962 | static void set_environ(struct interface_defn_t *iface, char *mode) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 963 | { |
| 964 | char **environend; |
| 965 | int i; |
| 966 | const int n_env_entries = iface->n_options + 5; |
| 967 | char **ppch; |
| 968 | |
| 969 | if (environ != NULL) { |
| 970 | for (ppch = environ; *ppch; ppch++) { |
| 971 | free(*ppch); |
| 972 | *ppch = NULL; |
| 973 | } |
| 974 | free(environ); |
| 975 | environ = NULL; |
| 976 | } |
| 977 | environ = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ )); |
| 978 | environend = environ; |
| 979 | *environend = NULL; |
| 980 | |
| 981 | for (i = 0; i < iface->n_options; i++) { |
| 982 | if (strcmp(iface->option[i].name, "up") == 0 |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 983 | || strcmp(iface->option[i].name, "down") == 0 |
| 984 | || strcmp(iface->option[i].name, "pre-up") == 0 |
| 985 | || strcmp(iface->option[i].name, "post-down") == 0) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 986 | continue; |
| 987 | } |
| 988 | *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); |
| 989 | *environend = NULL; |
| 990 | } |
| 991 | |
| 992 | *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface); |
| 993 | *environend = NULL; |
| 994 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
| 995 | *environend = NULL; |
| 996 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name); |
| 997 | *environend = NULL; |
| 998 | *(environend++) = setlocalenv("%s=%s", "MODE", mode); |
| 999 | *environend = NULL; |
| 1000 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"); |
| 1001 | *environend = NULL; |
| 1002 | } |
| 1003 | |
| 1004 | static int doit(char *str) |
| 1005 | { |
| 1006 | if (verbose || no_act) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1007 | printf("%s\n", str); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1008 | } |
| 1009 | if (!no_act) { |
| 1010 | pid_t child; |
| 1011 | int status; |
| 1012 | |
| 1013 | fflush(NULL); |
| 1014 | switch (child = fork()) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1015 | case -1: /* failure */ |
| 1016 | return 0; |
| 1017 | case 0: /* child */ |
Glenn L McGrath | dc4e75e | 2003-09-02 02:36:18 +0000 | [diff] [blame] | 1018 | execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, environ); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1019 | exit(127); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1020 | } |
| 1021 | waitpid(child, &status, 0); |
| 1022 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1023 | return 0; |
| 1024 | } |
| 1025 | } |
| 1026 | return (1); |
| 1027 | } |
| 1028 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1029 | static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1030 | { |
| 1031 | int i; |
Eric Andersen | eb213bd | 2003-09-12 08:39:05 +0000 | [diff] [blame] | 1032 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1033 | for (i = 0; i < ifd->n_options; i++) { |
| 1034 | if (strcmp(ifd->option[i].name, opt) == 0) { |
| 1035 | if (!(*exec) (ifd->option[i].value)) { |
| 1036 | return 0; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1040 | |
Eric Andersen | eb213bd | 2003-09-12 08:39:05 +0000 | [diff] [blame] | 1041 | bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1042 | if ((*exec)(buf) != 1) { |
| 1043 | return 0; |
| 1044 | } |
| 1045 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1046 | } |
| 1047 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1048 | static int check(char *str) { |
| 1049 | return str != NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1052 | static int iface_up(struct interface_defn_t *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1053 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1054 | if (!iface->method->up(iface,check)) return -1; |
| 1055 | set_environ(iface, "start"); |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1056 | if (!execute_all(iface, doit, "pre-up")) return 0; |
| 1057 | if (!iface->method->up(iface, doit)) return 0; |
| 1058 | if (!execute_all(iface, doit, "up")) return 0; |
| 1059 | return 1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | static int iface_down(struct interface_defn_t *iface) |
| 1063 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1064 | if (!iface->method->down(iface,check)) return -1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1065 | set_environ(iface, "stop"); |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1066 | if (!execute_all(iface, doit, "down")) return 0; |
| 1067 | if (!iface->method->down(iface, doit)) return 0; |
| 1068 | if (!execute_all(iface, doit, "post-down")) return 0; |
| 1069 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1072 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1073 | static int popen2(FILE **in, FILE **out, char *command, ...) |
| 1074 | { |
| 1075 | va_list ap; |
| 1076 | char *argv[11] = { command }; |
| 1077 | int argc; |
| 1078 | int infd[2], outfd[2]; |
| 1079 | pid_t pid; |
| 1080 | |
| 1081 | argc = 1; |
| 1082 | va_start(ap, command); |
| 1083 | while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) { |
| 1084 | argc++; |
| 1085 | } |
| 1086 | argv[argc] = NULL; /* make sure */ |
| 1087 | va_end(ap); |
| 1088 | |
| 1089 | if (pipe(infd) != 0) { |
| 1090 | return 0; |
| 1091 | } |
| 1092 | |
| 1093 | if (pipe(outfd) != 0) { |
| 1094 | close(infd[0]); |
| 1095 | close(infd[1]); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | fflush(NULL); |
| 1100 | switch (pid = fork()) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1101 | case -1: /* failure */ |
| 1102 | close(infd[0]); |
| 1103 | close(infd[1]); |
| 1104 | close(outfd[0]); |
| 1105 | close(outfd[1]); |
| 1106 | return 0; |
| 1107 | case 0: /* child */ |
| 1108 | dup2(infd[0], 0); |
| 1109 | dup2(outfd[1], 1); |
| 1110 | close(infd[0]); |
| 1111 | close(infd[1]); |
| 1112 | close(outfd[0]); |
| 1113 | close(outfd[1]); |
| 1114 | execvp(command, argv); |
| 1115 | exit(127); |
| 1116 | default: /* parent */ |
| 1117 | *in = fdopen(infd[1], "w"); |
| 1118 | *out = fdopen(outfd[0], "r"); |
| 1119 | close(infd[0]); |
| 1120 | close(outfd[1]); |
| 1121 | return pid; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1122 | } |
| 1123 | /* unreached */ |
| 1124 | } |
| 1125 | |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1126 | static char *run_mapping(char *physical, struct mapping_defn_t * map) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1127 | { |
| 1128 | FILE *in, *out; |
| 1129 | int i, status; |
| 1130 | pid_t pid; |
| 1131 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1132 | char *logical = bb_xstrdup(physical); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1133 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1134 | /* Run the mapping script. */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1135 | pid = popen2(&in, &out, map->script, physical, NULL); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1136 | |
| 1137 | /* popen2() returns 0 on failure. */ |
| 1138 | if (pid == 0) |
| 1139 | return logical; |
| 1140 | |
| 1141 | /* Write mappings to stdin of mapping script. */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1142 | for (i = 0; i < map->n_mappings; i++) { |
| 1143 | fprintf(in, "%s\n", map->mapping[i]); |
| 1144 | } |
| 1145 | fclose(in); |
| 1146 | waitpid(pid, &status, 0); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1147 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1148 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1149 | /* If the mapping script exited successfully, try to |
| 1150 | * grab a line of output and use that as the name of the |
| 1151 | * logical interface. */ |
| 1152 | char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1153 | |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 1154 | if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1155 | /* If we are able to read a line of output from the script, |
| 1156 | * remove any trailing whitespace and use this value |
| 1157 | * as the name of the logical interface. */ |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 1158 | char *pch = new_logical + bb_strlen(new_logical) - 1; |
| 1159 | |
| 1160 | while (pch >= new_logical && isspace(*pch)) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1161 | *(pch--) = '\0'; |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1162 | |
| 1163 | free(logical); |
| 1164 | logical = new_logical; |
| 1165 | } else { |
| 1166 | /* If we are UNABLE to read a line of output, discard are |
| 1167 | * freshly allocated memory. */ |
| 1168 | free(new_logical); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1169 | } |
| 1170 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1171 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1172 | fclose(out); |
| 1173 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1174 | return logical; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1175 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1176 | #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1177 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1178 | 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] | 1179 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1180 | unsigned short iface_len = bb_strlen(iface); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1181 | llist_t *search = state_list; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1182 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1183 | while (search) { |
| 1184 | if ((strncmp(search->data, iface, iface_len) == 0) && |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1185 | (search->data[iface_len] == '=')) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1186 | return(search); |
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 | search = search->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1189 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1190 | return(NULL); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | extern int ifupdown_main(int argc, char **argv) |
| 1194 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1195 | int (*cmds) (struct interface_defn_t *) = NULL; |
| 1196 | struct interfaces_file_t *defn; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1197 | FILE *state_fp = NULL; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1198 | llist_t *state_list = NULL; |
| 1199 | llist_t *target_list = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1200 | char *interfaces = "/etc/network/interfaces"; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1201 | const char *statefile = "/var/run/ifstate"; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1202 | |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 1203 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1204 | int run_mappings = 1; |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 1205 | #endif |
| 1206 | int do_all = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1207 | int force = 0; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1208 | int any_failures = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1209 | int i; |
| 1210 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1211 | if (bb_applet_name[2] == 'u') { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1212 | /* ifup command */ |
| 1213 | cmds = iface_up; |
| 1214 | } else { |
| 1215 | /* ifdown command */ |
| 1216 | cmds = iface_down; |
| 1217 | } |
| 1218 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1219 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1220 | while ((i = getopt(argc, argv, "i:hvnamf")) != -1) |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1221 | #else |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1222 | while ((i = getopt(argc, argv, "i:hvnaf")) != -1) |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1223 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1224 | { |
| 1225 | switch (i) { |
| 1226 | case 'i': /* interfaces */ |
| 1227 | interfaces = bb_xstrdup(optarg); |
| 1228 | break; |
| 1229 | case 'v': /* verbose */ |
| 1230 | verbose = 1; |
| 1231 | break; |
| 1232 | case 'a': /* all */ |
| 1233 | do_all = 1; |
| 1234 | break; |
| 1235 | case 'n': /* no-act */ |
| 1236 | no_act = 1; |
| 1237 | break; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1238 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1239 | case 'm': /* no-mappings */ |
| 1240 | run_mappings = 0; |
| 1241 | break; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1242 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1243 | case 'f': /* force */ |
| 1244 | force = 1; |
| 1245 | break; |
| 1246 | default: |
| 1247 | bb_show_usage(); |
| 1248 | break; |
| 1249 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1250 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1251 | |
| 1252 | if (argc - optind > 0) { |
| 1253 | if (do_all) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1254 | bb_show_usage(); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1255 | } |
| 1256 | } else { |
| 1257 | if (!do_all) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1258 | bb_show_usage(); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1259 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1260 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1261 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1262 | debug_noise("reading %s file:\n", interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1263 | defn = read_interfaces(interfaces); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1264 | debug_noise("\ndone reading %s\n\n", interfaces); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1265 | |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 1266 | if (!defn) { |
| 1267 | exit(EXIT_FAILURE); |
| 1268 | } |
| 1269 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1270 | if (no_act) { |
| 1271 | state_fp = fopen(statefile, "r"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1274 | /* Create a list of interfaces to work on */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1275 | if (do_all) { |
| 1276 | if (cmds == iface_up) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1277 | target_list = defn->autointerfaces; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1278 | } else { |
| 1279 | #if 0 |
| 1280 | /* iface_down */ |
| 1281 | llist_t *new_item; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1282 | const llist_t *list = state_list; |
| 1283 | while (list) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1284 | new_item = xmalloc(sizeof(llist_t)); |
| 1285 | new_item->data = strdup(list->data); |
| 1286 | new_item->link = NULL; |
| 1287 | list = target_list; |
| 1288 | if (list == NULL) |
| 1289 | target_list = new_item; |
| 1290 | else { |
| 1291 | while (list->link) { |
| 1292 | list = list->link; |
| 1293 | } |
| 1294 | list = new_item; |
| 1295 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1296 | list = list->link; |
| 1297 | } |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1298 | target_list = defn->autointerfaces; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1299 | #else |
| 1300 | |
| 1301 | /* iface_down */ |
| 1302 | const llist_t *list = state_list; |
| 1303 | while (list) { |
| 1304 | target_list = llist_add_to_end(target_list, strdup(list->data)); |
| 1305 | list = list->link; |
| 1306 | } |
| 1307 | target_list = defn->autointerfaces; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1308 | #endif |
| 1309 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1310 | } else { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1311 | target_list = llist_add_to_end(target_list, argv[optind]); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1315 | /* Update the interfaces */ |
| 1316 | while (target_list) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1317 | llist_t *iface_list; |
| 1318 | struct interface_defn_t *currif; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1319 | char *iface; |
| 1320 | char *liface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1321 | char *pch; |
| 1322 | int okay = 0; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1323 | int cmds_ret; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1324 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1325 | iface = strdup(target_list->data); |
| 1326 | target_list = target_list->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1327 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1328 | pch = strchr(iface, '='); |
| 1329 | if (pch) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1330 | *pch = '\0'; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1331 | liface = strdup(pch + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1332 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1333 | liface = strdup(iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1334 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1335 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1336 | if (!force) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1337 | const llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1338 | |
| 1339 | if (cmds == iface_up) { |
| 1340 | /* ifup */ |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1341 | if (iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1342 | bb_error_msg("interface %s already configured", iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1343 | continue; |
| 1344 | } |
| 1345 | } else { |
| 1346 | /* ifdown */ |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1347 | if (iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1348 | bb_error_msg("interface %s not configured", iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1349 | continue; |
| 1350 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1351 | } |
| 1352 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1353 | |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1354 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1355 | if ((cmds == iface_up) && run_mappings) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1356 | struct mapping_defn_t *currmap; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1357 | |
| 1358 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
| 1359 | |
| 1360 | for (i = 0; i < currmap->n_matches; i++) { |
| 1361 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
| 1362 | continue; |
| 1363 | if (verbose) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1364 | printf("Running mapping script %s on %s\n", currmap->script, liface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1365 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1366 | liface = run_mapping(iface, currmap); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1367 | break; |
| 1368 | } |
| 1369 | } |
| 1370 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1371 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1372 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1373 | |
| 1374 | iface_list = defn->ifaces; |
| 1375 | while (iface_list) { |
| 1376 | currif = (struct interface_defn_t *) iface_list->data; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1377 | if (strcmp(liface, currif->iface) == 0) { |
| 1378 | char *oldiface = currif->iface; |
| 1379 | |
| 1380 | okay = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1381 | currif->iface = iface; |
| 1382 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1383 | 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] | 1384 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1385 | /* 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^] | 1386 | cmds_ret = cmds(currif); |
| 1387 | if (cmds_ret == -1) { |
Glenn L McGrath | 469a1ea | 2004-07-21 12:21:39 +0000 | [diff] [blame] | 1388 | 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] | 1389 | liface, currif->address_family->name); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1390 | any_failures += 1; |
| 1391 | } else if (cmds_ret == 0) { |
| 1392 | any_failures += 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1393 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1394 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1395 | currif->iface = oldiface; |
| 1396 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1397 | iface_list = iface_list->link; |
| 1398 | } |
| 1399 | if (verbose) { |
| 1400 | printf("\n"); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | if (!okay && !force) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1404 | bb_error_msg("Ignoring unknown interface %s", liface); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1405 | any_failures += 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1406 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1407 | llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1408 | |
| 1409 | if (cmds == iface_up) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1410 | char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1411 | sprintf(newiface, "%s=%s", iface, liface); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1412 | if (iface_state == NULL) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1413 | state_list = llist_add_to_end(state_list, newiface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1414 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1415 | free(iface_state->data); |
| 1416 | iface_state->data = newiface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } else if (cmds == iface_down) { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1419 | /* Remove an interface from the linked list */ |
| 1420 | if (iface_state) { |
| 1421 | /* This needs to be done better */ |
| 1422 | free(iface_state->data); |
| 1423 | free(iface_state->link); |
| 1424 | if (iface_state->link) { |
| 1425 | iface_state->data = iface_state->link->data; |
| 1426 | iface_state->link = iface_state->link->link; |
| 1427 | } else { |
| 1428 | iface_state->data = NULL; |
| 1429 | iface_state->link = NULL; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1430 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1431 | } |
| 1432 | } |
| 1433 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1436 | /* Actually write the new state */ |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1437 | if (!no_act) { |
| 1438 | |
| 1439 | if (state_fp) |
| 1440 | fclose(state_fp); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1441 | state_fp = bb_xfopen(statefile, "a+"); |
Eric Andersen | 66a3af9 | 2003-01-27 17:41:19 +0000 | [diff] [blame] | 1442 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1443 | if (ftruncate(fileno(state_fp), 0) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1444 | bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno)); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
| 1447 | rewind(state_fp); |
| 1448 | |
| 1449 | while (state_list) { |
| 1450 | if (state_list->data) { |
| 1451 | fputs(state_list->data, state_fp); |
| 1452 | fputc('\n', state_fp); |
| 1453 | } |
| 1454 | state_list = state_list->link; |
| 1455 | } |
| 1456 | fflush(state_fp); |
| 1457 | } |
| 1458 | |
| 1459 | /* Cleanup */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1460 | if (state_fp != NULL) { |
| 1461 | fclose(state_fp); |
| 1462 | state_fp = NULL; |
| 1463 | } |
| 1464 | |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame^] | 1465 | if (any_failures) |
| 1466 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1467 | return 0; |
| 1468 | } |