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