blob: c9371cce88b1ea5e50f43e8903ad279d480d8bbc [file] [log] [blame]
Eric Andersen8320b422003-04-02 10:13:26 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00002/*
3 * ifupdown for busybox
Denis Vlasenko0beaff82007-09-21 13:16:32 +00004 * Copyright (c) 2002 Glenn McGrath
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00006 *
7 * Based on ifupdown v 0.6.4 by Anthony Towns
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00008 * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
9 *
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000010 * Changes to upstream version
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000011 * Remove checks for kernel version, assume kernel version 2.2.0 or better.
Glenn L McGrath398ff9d2002-12-06 11:51:46 +000012 * Lines in the interfaces file cannot wrap.
Denis Vlasenko2a86a612007-07-19 21:49:30 +000013 * 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 McGrath021fa7d2002-11-09 09:34:15 +000016 *
Rob Landley1b751c82005-10-28 09:24:33 +000017 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000018 */
19
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000021#include <fnmatch.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000023#include "libbb.h"
24
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000025#define MAX_OPT_DEPTH 10
26#define EUNBALBRACK 10001
27#define EUNDEFVAR 10002
28#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000029
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000030#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000031#define MAX_INTERFACE_LENGTH 10
32#endif
Eric Andersen8320b422003-04-02 10:13:26 +000033
Wade Berrier142c5cb2008-11-14 21:18:45 +000034#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
35
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000036#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000037
38/* Forward declaration */
39struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000040
Denis Vlasenko097c3242006-11-27 16:59:15 +000041typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000042
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000043struct method_t {
44 const char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000045 int (*up)(struct interface_defn_t *ifd, execfn *e);
46 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000047};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000049struct address_family_t {
50 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000051 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000052 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000053};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000055struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000056 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000057
58 int max_matches;
59 int n_matches;
60 char **match;
61
62 char *script;
63
64 int max_mappings;
65 int n_mappings;
66 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000067};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000068
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000069struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070 char *name;
71 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000072};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000073
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000074struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000075 const struct address_family_t *address_family;
76 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077
Rob Landleye813ddb2006-02-28 03:53:14 +000078 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000079 int max_options;
80 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000081 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000082};
83
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000084struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000085 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000086 llist_t *ifaces;
87 struct mapping_defn_t *mappings;
88};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000089
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000090#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
91enum {
92 OPT_do_all = 0x1,
93 OPT_no_act = 0x2,
94 OPT_verbose = 0x4,
95 OPT_force = 0x8,
96 OPT_no_mappings = 0x10,
97};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000098#define DO_ALL (option_mask32 & OPT_do_all)
99#define NO_ACT (option_mask32 & OPT_no_act)
100#define VERBOSE (option_mask32 & OPT_verbose)
101#define FORCE (option_mask32 & OPT_force)
102#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000103
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000104static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000105
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000106static const char *startup_PATH;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000107
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000108#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
109
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000110static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000111{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000112 /* xasprintf trick will be smaller, but we are often
113 * called with str_length == 1 - don't want to have
114 * THAT much of malloc/freeing! */
115 char *buf = *bufp;
116 int len = (buf ? strlen(buf) : 0);
117 str_length++;
118 buf = xrealloc(buf, len + str_length);
119 /* copies at most str_length-1 chars! */
120 safe_strncpy(buf + len, str, str_length);
121 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000122}
123
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000124static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000125{
126 int i = strncmp(l, r, llen);
127
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000128 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000129 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000130 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000131}
132
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000133static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000134{
135 int i;
136
137 if (strncmpz(id, "iface", idlen) == 0) {
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000138 static char *label_buf;
139 //char *result;
140
141 free(label_buf);
142 label_buf = xstrdup(ifd->iface);
143 // Remove virtual iface suffix - why?
144 // ubuntu's ifup doesn't do this
145 //result = strchrnul(label_buf, ':');
146 //*result = '\0';
Denis Vlasenko05341252006-09-26 20:35:30 +0000147 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000148 }
149 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000150 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000151 }
152 for (i = 0; i < ifd->n_options; i++) {
153 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
154 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000155 }
156 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000157 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000158}
159
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000160#if ENABLE_FEATURE_IFUPDOWN_IP
161static int count_netmask_bits(const char *dotted_quad)
162{
163// int result;
164// unsigned a, b, c, d;
165// /* Found a netmask... Check if it is dotted quad */
166// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
167// return -1;
168// if ((a|b|c|d) >> 8)
169// return -1; /* one of numbers is >= 256 */
170// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
171// d = ~d; /* 11110000 -> 00001111 */
172
173 /* Shorter version */
174 int result;
175 struct in_addr ip;
176 unsigned d;
177
178 if (inet_aton(dotted_quad, &ip) == 0)
179 return -1; /* malformed dotted IP */
180 d = ntohl(ip.s_addr); /* IP in host order */
181 d = ~d; /* 11110000 -> 00001111 */
182 if (d & (d+1)) /* check that it is in 00001111 form */
183 return -1; /* no it is not */
184 result = 32;
185 while (d) {
186 d >>= 1;
187 result--;
188 }
189 return result;
190}
191#endif
192
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000193static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000194{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000195 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
196 int okay[MAX_OPT_DEPTH] = { 1 };
197 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000198 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000199
200 while (*command) {
201 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000202 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000203 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000204 command++;
205 break;
206 case '\\':
207 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000208 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000209 command += 2;
210 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000211 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000212 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000213 }
214 break;
215 case '[':
216 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000217 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000218 okay[opt_depth] = 1;
219 opt_depth++;
220 command += 2;
221 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000222 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000223 command++;
224 }
225 break;
226 case ']':
227 if (command[1] == ']' && opt_depth > 1) {
228 opt_depth--;
229 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000230 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000231 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000232 command += 2;
233 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000234 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000235 command++;
236 }
237 break;
238 case '%':
239 {
240 char *nextpercent;
241 char *varvalue;
242
243 command++;
244 nextpercent = strchr(command, '%');
245 if (!nextpercent) {
246 errno = EUNBALPER;
247 free(result);
248 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000249 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000250
251 varvalue = get_var(command, nextpercent - command, ifd);
252
253 if (varvalue) {
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000254#if ENABLE_FEATURE_IFUPDOWN_IP
255 /* "hwaddress <class> <address>":
256 * unlike ifconfig, ip doesnt want <class>
257 * (usually "ether" keyword). Skip it. */
258 if (strncmp(command, "hwaddress", 9) == 0) {
259 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
260 }
261#endif
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000262 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000263 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000264#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000265 /* Sigh... Add a special case for 'ip' to convert from
266 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000267 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000268 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000269 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000270 if (varvalue) {
271 res = count_netmask_bits(varvalue);
272 if (res > 0) {
273 const char *argument = utoa(res);
274 addstr(&result, argument, strlen(argument));
275 command = nextpercent + 1;
276 break;
277 }
Eric Andersen8320b422003-04-02 10:13:26 +0000278 }
Eric Andersen8320b422003-04-02 10:13:26 +0000279 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000280#endif
281 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000282 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000283
284 command = nextpercent + 1;
285 }
286 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000287 }
288 }
289
290 if (opt_depth > 1) {
291 errno = EUNBALBRACK;
292 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000293 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294 }
295
296 if (!okay[0]) {
297 errno = EUNDEFVAR;
298 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000299 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000300 }
301
Denis Vlasenko05341252006-09-26 20:35:30 +0000302 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000303}
304
Denis Vlasenko05341252006-09-26 20:35:30 +0000305/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000306static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000307{
308 char *out;
309 int ret;
310
311 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000312 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000313 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000314 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000316 /* out == "": parsed ok but not all needed variables known, skip */
317 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000318
319 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000320 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000321 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000322 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000323 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000324}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000325#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000326
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000327#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000328static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000330#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000331 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000332 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000333 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000334 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000335#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000336 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000337#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000338}
339
Eric Andersen8320b422003-04-02 10:13:26 +0000340static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000341{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000342#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000343 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000344#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000345 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000346#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347}
348
Eric Andersen8320b422003-04-02 10:13:26 +0000349static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000350{
Eric Andersen8320b422003-04-02 10:13:26 +0000351 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000352#if ENABLE_FEATURE_IFUPDOWN_IP
353 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000354 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000355 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
356 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000357#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000358 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000359 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000360 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000361#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000362 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363}
364
Eric Andersen8320b422003-04-02 10:13:26 +0000365static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000367#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000368 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000369#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000370 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000371#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372}
373
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000374#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000375static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000376{
Eric Andersen8320b422003-04-02 10:13:26 +0000377 int result;
378 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000379 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000380 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000381 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000382 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000383 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384}
385
Eric Andersen8320b422003-04-02 10:13:26 +0000386static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387{
Denis Vlasenko05341252006-09-26 20:35:30 +0000388 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000389}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000390#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391
Denis Vlasenko05341252006-09-26 20:35:30 +0000392static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000393#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000395#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000396 { "static", static_up6, static_down6, },
397 { "loopback", loopback_up6, loopback_down6, },
398};
399
Denis Vlasenko05341252006-09-26 20:35:30 +0000400static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000401 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000402 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000403 methods6
404};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000405#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000406
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000407#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000408static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000410#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000411 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000412 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000413 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000414 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000415#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000416 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000418}
419
Eric Andersen8320b422003-04-02 10:13:26 +0000420static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000421{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000422#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000423 int result;
424 result = execute("ip addr flush dev %iface%", ifd, exec);
425 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000426 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000427#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000428 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000429#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000430}
431
Eric Andersen8320b422003-04-02 10:13:26 +0000432static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000433{
Eric Andersen8320b422003-04-02 10:13:26 +0000434 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000435#if ENABLE_FEATURE_IFUPDOWN_IP
436 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
437 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000438 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000439 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000440 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000441#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000442 /* ifconfig said to set iface up before it processes hw %hwaddress%,
443 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000444 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000445 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000446 result += execute("ifconfig %iface% %address% netmask %netmask%"
447 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000448 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000449 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000450 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000451#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000452}
453
Eric Andersen8320b422003-04-02 10:13:26 +0000454static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000455{
Eric Andersen8320b422003-04-02 10:13:26 +0000456 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000457#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000458 result = execute("ip addr flush dev %iface%", ifd, exec);
459 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000460#else
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000461 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
462 /* Bringing the interface down deletes the routes in itself.
463 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
464 result = 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000465 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000466#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000467 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000468}
469
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000470#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000471struct dhcp_client_t
472{
473 const char *name;
474 const char *startcmd;
475 const char *stopcmd;
476};
477
478static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000479 { "dhcpcd",
480 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
481 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000482 },
483 { "dhclient",
484 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
485 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
486 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000487 { "pump",
488 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
489 "pump -i %iface% -k",
490 },
491 { "udhcpc",
Wade Berrier142c5cb2008-11-14 21:18:45 +0000492 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000493 "[[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000494 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000495 },
496};
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000497#endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000498
Denis Vlasenko85c24712008-03-17 09:04:04 +0000499#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Eric Andersen8320b422003-04-02 10:13:26 +0000500static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000501{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000502 unsigned i;
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000503#if ENABLE_FEATURE_IFUPDOWN_IP
504 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000505 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000506 return 0;
507#else
508 /* needed if we have hwaddress on dhcp iface */
509 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000510 return 0;
511#endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000512 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000513 if (exists_execable(ext_dhcp_clients[i].name))
514 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
515 }
516 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000517 return 0;
Denis Vlasenko85c24712008-03-17 09:04:04 +0000518}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000519#elif ENABLE_APP_UDHCPC
Denis Vlasenko85c24712008-03-17 09:04:04 +0000520static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
521{
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000522#if ENABLE_FEATURE_IFUPDOWN_IP
523 /* ip doesn't up iface when it configures it (unlike ifconfig) */
Denis Vlasenko44d5dce2008-11-01 00:10:51 +0000524 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
Denis Vlasenkoee777c42008-08-12 01:35:34 +0000525 return 0;
526#else
527 /* needed if we have hwaddress on dhcp iface */
528 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000529 return 0;
530#endif
Wade Berrier142c5cb2008-11-14 21:18:45 +0000531 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000532 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000533 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000534}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000535#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000536static int dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
537 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000538{
539 return 0; /* no dhcp support */
540}
541#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000542
Denis Vlasenko85c24712008-03-17 09:04:04 +0000543#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Eric Andersen8320b422003-04-02 10:13:26 +0000544static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000546 int result = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000547 unsigned i;
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000548
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000549 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000550 if (exists_execable(ext_dhcp_clients[i].name)) {
551 result += execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
552 if (result)
553 break;
554 }
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000555 }
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000556
557 if (!result)
558 bb_error_msg("warning: no dhcp clients found and stopped");
559
560 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
561 and it may come back up because udhcpc is still shutting down */
562 usleep(100000);
563 result += static_down(ifd, exec);
564 return ((result == 3) ? 3 : 0);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000565}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000566#elif ENABLE_APP_UDHCPC
Denis Vlasenko85c24712008-03-17 09:04:04 +0000567static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
568{
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000569 int result;
570 result = execute("kill "
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000571 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Denis Vlasenko6c4eb442008-08-19 23:02:23 +0000572 /* Also bring the hardware interface down since
573 killing the dhcp client alone doesn't do it.
574 This enables consecutive ifup->ifdown->ifup */
575 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
576 and it may come back up because udhcpc is still shutting down */
577 usleep(100000);
578 result += static_down(ifd, exec);
579 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000581#else
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000582static int dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
583 execfn *exec UNUSED_PARAM)
Denis Vlasenko85c24712008-03-17 09:04:04 +0000584{
585 return 0; /* no dhcp support */
586}
587#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000588
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000589static int manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000590{
591 return 1;
592}
593
Eric Andersen8320b422003-04-02 10:13:26 +0000594static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000595{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000596 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000597 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
598 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599}
600
Eric Andersen8320b422003-04-02 10:13:26 +0000601static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000602{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000603 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000604}
605
Eric Andersen8320b422003-04-02 10:13:26 +0000606static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000607{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000608 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000609}
610
Eric Andersen8320b422003-04-02 10:13:26 +0000611static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000612{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000613 return execute("start-stop-daemon --start -x wvdial "
614 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615}
616
Eric Andersen8320b422003-04-02 10:13:26 +0000617static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000618{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000619 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000620 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621}
622
Denis Vlasenko05341252006-09-26 20:35:30 +0000623static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000624 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000625 { "wvdial", wvdial_up, wvdial_down, },
626 { "ppp", ppp_up, ppp_down, },
627 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000628 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000629 { "dhcp", dhcp_up, dhcp_down, },
630 { "loopback", loopback_up, loopback_down, },
631};
632
Denis Vlasenko05341252006-09-26 20:35:30 +0000633static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000634 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000635 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000636 methods
637};
638
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000639#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000640
Glenn L McGrath85737042003-01-14 23:26:57 +0000641static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000642{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000643 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000644 char *word;
645
Glenn L McGrath85737042003-01-14 23:26:57 +0000646 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000647 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000648
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000649 /* Stop on EOL */
650 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000651 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000652
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000653 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000654 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000655
656 /* Unless we are already at NUL, store NUL and advance */
657 if (word[length] != '\0')
658 word[length++] = '\0';
659
Glenn L McGrath85737042003-01-14 23:26:57 +0000660 *buf = word + length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000661
662 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000663}
664
Denis Vlasenko05341252006-09-26 20:35:30 +0000665static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000666{
667 int i;
668
Denis Vlasenko736230e2006-11-20 19:40:36 +0000669 if (!name)
670 return NULL;
671
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000672 for (i = 0; af[i]; i++) {
673 if (strcmp(af[i]->name, name) == 0) {
674 return af[i];
675 }
676 }
677 return NULL;
678}
679
Denis Vlasenko05341252006-09-26 20:35:30 +0000680static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681{
682 int i;
683
Denis Vlasenko736230e2006-11-20 19:40:36 +0000684 if (!name)
685 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000686 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000687 for (i = 0; i < af->n_methods; i++) {
688 if (strcmp(af->method[i].name, name) == 0) {
689 return &af->method[i];
690 }
691 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000692 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693}
694
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000695static const llist_t *find_list_string(const llist_t *list, const char *string)
696{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000697 if (string == NULL)
698 return NULL;
699
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000700 while (list) {
701 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000702 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000703 }
704 list = list->link;
705 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000706 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000707}
708
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000709static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000710{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000711 /* Let's try to be compatible.
712 *
713 * "man 5 interfaces" says:
714 * Lines starting with "#" are ignored. Note that end-of-line
715 * comments are NOT supported, comments must be on a line of their own.
716 * A line may be extended across multiple lines by making
717 * the last character a backslash.
718 *
719 * Seen elsewhere in example config file:
Denis Vlasenko49382922008-07-08 03:25:25 +0000720 * A first non-blank "#" character makes the rest of the line
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000721 * be ignored. Blank lines are ignored. Lines may be indented freely.
722 * A "\" character at the very end of the line indicates the next line
723 * should be treated as a continuation of the current one.
724 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000725#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000726 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000727#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000728 struct interface_defn_t *currif = NULL;
729 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000730 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000731 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000732 char *first_word;
733 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000734 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
735
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000736 defn = xzalloc(sizeof(*defn));
Denis Vlasenko5415c852008-07-21 23:05:26 +0000737 f = xfopen_for_read(filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000738
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000739 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000740#if ENABLE_DESKTOP
741 /* Trailing "\" concatenates lines */
742 char *p;
743 while ((p = last_char_is(buf, '\\')) != NULL) {
744 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000745 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000746 if (!rest_of_line)
747 break;
748 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000749 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000750 free(rest_of_line);
751 buf = p;
752 }
753#endif
754 rest_of_line = buf;
755 first_word = next_word(&rest_of_line);
Denis Vlasenko49382922008-07-08 03:25:25 +0000756 if (!first_word || *first_word == '#') {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000757 free(buf);
758 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000759 }
760
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000761 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000762#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000763 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000764
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000765 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkodeeed592008-07-08 05:14:36 +0000766 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000767 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000768 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000769 /*currmap->max_mappings = 0; - done by xzalloc */
770 /*currmap->n_mappings = 0;*/
771 /*currmap->mapping = NULL;*/
772 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000773 {
Eric Andersen8320b422003-04-02 10:13:26 +0000774 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000775 while (*where != NULL) {
776 where = &(*where)->next;
777 }
778 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000779 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000780 }
Eric Andersen8320b422003-04-02 10:13:26 +0000781 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000782#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000783 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000784 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000785 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000786#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000787 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000788#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000789#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000790 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000791#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000792 NULL
793 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000794 char *iface_name;
795 char *address_family_name;
796 char *method_name;
797 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000798
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000799 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000800 iface_name = next_word(&rest_of_line);
801 address_family_name = next_word(&rest_of_line);
802 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000803
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000804 if (method_name == NULL)
805 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000806
807 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000808 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000809
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000810 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000811 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000812
813 currif->iface = xstrdup(iface_name);
814
815 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000816 if (!currif->address_family)
817 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000818
819 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000820 if (!currif->method)
821 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000822
823 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
824 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000825 if ((strcmp(tmp->iface, currif->iface) == 0)
826 && (tmp->address_family == currif->address_family)
827 ) {
828 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000829 }
830 }
831 llist_add_to_end(&(defn->ifaces), (char*)currif);
832
833 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000834 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000835 } else if (strcmp(first_word, "auto") == 0) {
836 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000837
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000838 /* Check the interface isnt already listed */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000839 if (find_list_string(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000840 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000841 }
842
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000843 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000844 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
845 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000846 }
847 currently_processing = NONE;
848 } else {
849 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000850 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000851 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000852 bb_error_msg_and_die("option with empty value \"%s\"", buf);
853
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000854 if (strcmp(first_word, "up") != 0
855 && strcmp(first_word, "down") != 0
856 && strcmp(first_word, "pre-up") != 0
857 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000858 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000859 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000860 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000861 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000862 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000863 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000864 }
865 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000866 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000867 currif->option = xrealloc(currif->option,
868 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000869 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000870 debug_noise("\t%s=%s\n", first_word, rest_of_line);
871 currif->option[currif->n_options].name = xstrdup(first_word);
872 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000873 currif->n_options++;
874 break;
875 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000876#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000877 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000878 if (currmap->script != NULL)
879 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000880 currmap->script = xstrdup(next_word(&rest_of_line));
881 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000882 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000883 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000884 currmap->mapping = xrealloc(currmap->mapping,
885 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000886 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000887 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000888 currmap->n_mappings++;
889 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000890 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000891 }
892#endif
893 break;
894 case NONE:
895 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000896 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000897 }
898 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000899 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000900 } /* while (fgets) */
901
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000902 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000903 /* ferror does NOT set errno! */
904 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000905 }
906 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000907
908 return defn;
909}
910
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000911static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000912{
913 char *result;
914 char *here;
915 char *there;
916
Rob Landleyd921b2e2006-08-03 15:41:12 +0000917 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000918
919 for (here = there = result; *there != '=' && *there; there++) {
920 if (*there == '-')
921 *there = '_';
922 if (isalpha(*there))
923 *there = toupper(*there);
924
925 if (isalnum(*there) || *there == '_') {
926 *here = *there;
927 here++;
928 }
929 }
Rob Landleya3896512006-05-07 20:20:34 +0000930 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000931
932 return result;
933}
934
Rob Landleye813ddb2006-02-28 03:53:14 +0000935static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936{
937 char **environend;
938 int i;
939 const int n_env_entries = iface->n_options + 5;
940 char **ppch;
941
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000942 if (my_environ != NULL) {
943 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000944 free(*ppch);
945 *ppch = NULL;
946 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000947 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000949 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
950 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951
952 for (i = 0; i < iface->n_options; i++) {
953 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000954 || strcmp(iface->option[i].name, "down") == 0
955 || strcmp(iface->option[i].name, "pre-up") == 0
956 || strcmp(iface->option[i].name, "post-down") == 0
957 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958 continue;
959 }
960 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000961 }
962
963 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000965 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000966 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000967 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000968}
969
970static int doit(char *str)
971{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000972 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000973 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000974 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000975 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000976 pid_t child;
977 int status;
978
979 fflush(NULL);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000980 child = vfork();
Denis Vlasenko2375d752006-12-19 23:15:46 +0000981 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000982 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000983 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000984 case 0: /* child */
Denis Vlasenkof09f4e02009-02-26 12:29:59 +0000985 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000986 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000987 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000988 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000989 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
990 return 0;
991 }
992 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000993 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000994}
995
Rob Landleye813ddb2006-02-28 03:53:14 +0000996static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000997{
998 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000999 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001000 for (i = 0; i < ifd->n_options; i++) {
1001 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +00001002 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001003 return 0;
1004 }
1005 }
1006 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001007
Rob Landleyd921b2e2006-08-03 15:41:12 +00001008 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +00001009 /* heh, we don't bother free'ing it */
1010 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001011}
1012
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001013static int check(char *str)
1014{
Eric Andersen8320b422003-04-02 10:13:26 +00001015 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001016}
1017
Eric Andersen8320b422003-04-02 10:13:26 +00001018static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001019{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001020 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +00001021 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +00001022 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001023 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001024 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001025 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +00001026}
1027
1028static int iface_down(struct interface_defn_t *iface)
1029{
Eric Andersen8320b422003-04-02 10:13:26 +00001030 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +00001032 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001033 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +00001034 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +00001035 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001036}
1037
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001038#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001039static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001040{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001041 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001042 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001043 pid_t pid;
1044
Denis Vlasenko37188322008-02-16 13:20:56 +00001045 xpiped_pair(infd);
1046 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047
1048 fflush(NULL);
Denis Vlasenko82604e92008-07-01 15:59:42 +00001049 pid = vfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001050
Denis Vlasenko82604e92008-07-01 15:59:42 +00001051 switch (pid) {
1052 case -1: /* failure */
1053 bb_perror_msg_and_die("vfork");
1054 case 0: /* child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001055 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001056 close(infd.wr);
1057 close(outfd.rd);
1058 xmove_fd(infd.rd, 0);
1059 xmove_fd(outfd.wr, 1);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001060 BB_EXECVP(command, argv);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001061 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001063 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001064 close(infd.rd);
1065 close(outfd.wr);
1066 *in = fdopen(infd.wr, "w");
1067 *out = fdopen(outfd.rd, "r");
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001068 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001069}
1070
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001071static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001072{
1073 FILE *in, *out;
1074 int i, status;
1075 pid_t pid;
1076
Rob Landleyd921b2e2006-08-03 15:41:12 +00001077 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001079 /* Run the mapping script. Never fails. */
1080 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001081
1082 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001083 for (i = 0; i < map->n_mappings; i++) {
1084 fprintf(in, "%s\n", map->mapping[i]);
1085 }
1086 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001087 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001088
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001089 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001090 /* If the mapping script exited successfully, try to
1091 * grab a line of output and use that as the name of the
1092 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001093 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001094
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001095 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001096 /* If we are able to read a line of output from the script,
1097 * remove any trailing whitespace and use this value
1098 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001099 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001100
1101 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001102 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001103
1104 free(logical);
1105 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001106 }
1107 }
Eric Andersen8a931792003-07-03 10:20:29 +00001108
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109 fclose(out);
1110
Eric Andersen8a931792003-07-03 10:20:29 +00001111 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001112}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001113#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001114
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001115static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001116{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001117 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001118 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001120 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001121 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001122 && (search->data[iface_len] == '=')
1123 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001124 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001125 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001126 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001128 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129}
1130
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001131/* read the previous state from the state file */
1132static llist_t *read_iface_state(void)
1133{
1134 llist_t *state_list = NULL;
Denis Vlasenko5415c852008-07-21 23:05:26 +00001135 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001136
1137 if (state_fp) {
1138 char *start, *end_ptr;
1139 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1140 /* We should only need to check for a single character */
1141 end_ptr = start + strcspn(start, " \t\n");
1142 *end_ptr = '\0';
1143 llist_add_to(&state_list, start);
1144 }
1145 fclose(state_fp);
1146 }
1147 return state_list;
1148}
1149
1150
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001151int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00001152int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001153{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001154 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001155 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001156 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001157 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001158 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001159
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001160 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001161 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001162 /* ifup command */
1163 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 }
1165
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001166 getopt32(argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001167 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001168 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001169 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001170 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001171 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172
Eric Andersen8320b422003-04-02 10:13:26 +00001173 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001175 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001177 startup_PATH = getenv("PATH");
1178 if (!startup_PATH) startup_PATH = "";
1179
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001180 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001181 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001182 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001183 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001184 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001185 }
1186
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001187 /* Update the interfaces */
1188 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001189 llist_t *iface_list;
1190 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001191 char *iface;
1192 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001194 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001195 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001196
Rob Landleyd921b2e2006-08-03 15:41:12 +00001197 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001198 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001200 pch = strchr(iface, '=');
1201 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001203 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001204 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001205 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001206 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001207
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001208 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001209 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001210 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001211
1212 if (cmds == iface_up) {
1213 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001214 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001215 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001216 continue;
1217 }
1218 } else {
1219 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001220 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001221 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 continue;
1223 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001224 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001225 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001226 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001227
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001228#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001229 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001230 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001231
1232 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001233 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001234 for (i = 0; i < currmap->n_matches; i++) {
1235 if (fnmatch(currmap->match[i], liface, 0) != 0)
1236 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001237 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001238 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239 }
Eric Andersen8a931792003-07-03 10:20:29 +00001240 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001241 break;
1242 }
1243 }
1244 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001245#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001246
Eric Andersen8320b422003-04-02 10:13:26 +00001247 iface_list = defn->ifaces;
1248 while (iface_list) {
1249 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001250 if (strcmp(liface, currif->iface) == 0) {
1251 char *oldiface = currif->iface;
1252
1253 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 currif->iface = iface;
1255
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001256 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001258 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001259 cmds_ret = cmds(currif);
1260 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001261 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001262 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001263 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001264 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001265 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001266 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001267
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001268 currif->iface = oldiface;
1269 }
Eric Andersen8320b422003-04-02 10:13:26 +00001270 iface_list = iface_list->link;
1271 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001272 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001273 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274 }
1275
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001276 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001277 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001278 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001279 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001280 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001281 FILE *state_fp;
1282 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001283 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001284 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001285
1286 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001287 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001288 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001289 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001290 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001291 free(iface_state->data);
1292 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001293 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001294 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001295 /* Remove an interface from state_list */
1296 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001297 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001298 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001299
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001300 /* Actually write the new state */
Denis Vlasenko5415c852008-07-21 23:05:26 +00001301 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
Denis Vlasenkof92df582007-05-02 22:22:23 +00001302 state = state_list;
1303 while (state) {
1304 if (state->data) {
1305 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001306 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001307 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001308 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001309 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001310 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001311 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001312 }
1313
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001314 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001315}