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