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