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