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