blob: 6aa929a3039af450e57ff794b3379c5117181895 [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>
22#include <getopt.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000023
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
25
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000026#define MAX_OPT_DEPTH 10
27#define EUNBALBRACK 10001
28#define EUNDEFVAR 10002
29#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000030
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000031#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000032#define MAX_INTERFACE_LENGTH 10
33#endif
Eric Andersen8320b422003-04-02 10:13:26 +000034
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000035#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000036
37/* Forward declaration */
38struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000039
Denis Vlasenko097c3242006-11-27 16:59:15 +000040typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000042struct method_t {
43 const char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000044 int (*up)(struct interface_defn_t *ifd, execfn *e);
45 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000046};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000047
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000048struct address_family_t {
49 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000050 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000051 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000052};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000053
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000054struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000055 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000056
57 int max_matches;
58 int n_matches;
59 char **match;
60
61 char *script;
62
63 int max_mappings;
64 int n_mappings;
65 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000066};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000067
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000068struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000069 char *name;
70 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000071};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000072
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000073struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000074 const struct address_family_t *address_family;
75 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000076
Rob Landleye813ddb2006-02-28 03:53:14 +000077 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000078 int max_options;
79 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000080 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000081};
82
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000083struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000084 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000085 llist_t *ifaces;
86 struct mapping_defn_t *mappings;
87};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000088
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000089#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
90enum {
91 OPT_do_all = 0x1,
92 OPT_no_act = 0x2,
93 OPT_verbose = 0x4,
94 OPT_force = 0x8,
95 OPT_no_mappings = 0x10,
96};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000097#define DO_ALL (option_mask32 & OPT_do_all)
98#define NO_ACT (option_mask32 & OPT_no_act)
99#define VERBOSE (option_mask32 & OPT_verbose)
100#define FORCE (option_mask32 & OPT_force)
101#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000102
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000103static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000104
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000105static const char *startup_PATH;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000106
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000107#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
108
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000109static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000110{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000111 /* xasprintf trick will be smaller, but we are often
112 * called with str_length == 1 - don't want to have
113 * THAT much of malloc/freeing! */
114 char *buf = *bufp;
115 int len = (buf ? strlen(buf) : 0);
116 str_length++;
117 buf = xrealloc(buf, len + str_length);
118 /* copies at most str_length-1 chars! */
119 safe_strncpy(buf + len, str, str_length);
120 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000121}
122
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000123static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000124{
125 int i = strncmp(l, r, llen);
126
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000127 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000128 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000129 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000130}
131
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000132static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000133{
134 int i;
135
136 if (strncmpz(id, "iface", idlen) == 0) {
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000137 static char *label_buf;
138 //char *result;
139
140 free(label_buf);
141 label_buf = xstrdup(ifd->iface);
142 // Remove virtual iface suffix - why?
143 // ubuntu's ifup doesn't do this
144 //result = strchrnul(label_buf, ':');
145 //*result = '\0';
Denis Vlasenko05341252006-09-26 20:35:30 +0000146 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000147 }
148 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000149 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000150 }
151 for (i = 0; i < ifd->n_options; i++) {
152 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
153 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000154 }
155 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000156 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000157}
158
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000159#if ENABLE_FEATURE_IFUPDOWN_IP
160static int count_netmask_bits(const char *dotted_quad)
161{
162// int result;
163// unsigned a, b, c, d;
164// /* Found a netmask... Check if it is dotted quad */
165// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
166// return -1;
167// if ((a|b|c|d) >> 8)
168// return -1; /* one of numbers is >= 256 */
169// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
170// d = ~d; /* 11110000 -> 00001111 */
171
172 /* Shorter version */
173 int result;
174 struct in_addr ip;
175 unsigned d;
176
177 if (inet_aton(dotted_quad, &ip) == 0)
178 return -1; /* malformed dotted IP */
179 d = ntohl(ip.s_addr); /* IP in host order */
180 d = ~d; /* 11110000 -> 00001111 */
181 if (d & (d+1)) /* check that it is in 00001111 form */
182 return -1; /* no it is not */
183 result = 32;
184 while (d) {
185 d >>= 1;
186 result--;
187 }
188 return result;
189}
190#endif
191
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000192static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000193{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000194 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
195 int okay[MAX_OPT_DEPTH] = { 1 };
196 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000197 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000198
199 while (*command) {
200 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000201 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000202 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000203 command++;
204 break;
205 case '\\':
206 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000207 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000208 command += 2;
209 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000210 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000211 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000212 }
213 break;
214 case '[':
215 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000216 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000217 okay[opt_depth] = 1;
218 opt_depth++;
219 command += 2;
220 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000221 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000222 command++;
223 }
224 break;
225 case ']':
226 if (command[1] == ']' && opt_depth > 1) {
227 opt_depth--;
228 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000229 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000230 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000231 command += 2;
232 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000233 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000234 command++;
235 }
236 break;
237 case '%':
238 {
239 char *nextpercent;
240 char *varvalue;
241
242 command++;
243 nextpercent = strchr(command, '%');
244 if (!nextpercent) {
245 errno = EUNBALPER;
246 free(result);
247 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000248 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000249
250 varvalue = get_var(command, nextpercent - command, ifd);
251
252 if (varvalue) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000253 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000254 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000255#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000256 /* Sigh... Add a special case for 'ip' to convert from
257 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000258 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000259 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000260 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000261 if (varvalue) {
262 res = count_netmask_bits(varvalue);
263 if (res > 0) {
264 const char *argument = utoa(res);
265 addstr(&result, argument, strlen(argument));
266 command = nextpercent + 1;
267 break;
268 }
Eric Andersen8320b422003-04-02 10:13:26 +0000269 }
Eric Andersen8320b422003-04-02 10:13:26 +0000270 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000271#endif
272 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000273 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000274
275 command = nextpercent + 1;
276 }
277 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000278 }
279 }
280
281 if (opt_depth > 1) {
282 errno = EUNBALBRACK;
283 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000284 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000285 }
286
287 if (!okay[0]) {
288 errno = EUNDEFVAR;
289 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000290 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000291 }
292
Denis Vlasenko05341252006-09-26 20:35:30 +0000293 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294}
295
Denis Vlasenko05341252006-09-26 20:35:30 +0000296/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000297static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000298{
299 char *out;
300 int ret;
301
302 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000303 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000304 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000305 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000306 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000307 /* out == "": parsed ok but not all needed variables known, skip */
308 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000309
310 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000311 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000312 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000313 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000314 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000315}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000316#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000317
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000318#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000319static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000320{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000321#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000322 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000323 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000324 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000325 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000326#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000327 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000328#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000329}
330
Eric Andersen8320b422003-04-02 10:13:26 +0000331static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000332{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000333#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000334 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000335#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000336 return execute("ifconfig %iface% del ::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 static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000341{
Eric Andersen8320b422003-04-02 10:13:26 +0000342 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000343#if ENABLE_FEATURE_IFUPDOWN_IP
344 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
345 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
346 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
347 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000348#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000349 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000350 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000351 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000352#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000353 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000354}
355
Eric Andersen8320b422003-04-02 10:13:26 +0000356static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000358#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000359 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000360#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000361 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000362#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363}
364
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000365#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000366static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367{
Eric Andersen8320b422003-04-02 10:13:26 +0000368 int result;
369 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000370 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000371 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000372 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000373 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000374 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375}
376
Eric Andersen8320b422003-04-02 10:13:26 +0000377static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378{
Denis Vlasenko05341252006-09-26 20:35:30 +0000379 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000380}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000381#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382
Denis Vlasenko05341252006-09-26 20:35:30 +0000383static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000384#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000386#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387 { "static", static_up6, static_down6, },
388 { "loopback", loopback_up6, loopback_down6, },
389};
390
Denis Vlasenko05341252006-09-26 20:35:30 +0000391static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000392 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000393 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394 methods6
395};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000396#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000398#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000399static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000400{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000401#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000402 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000403 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000404 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000405 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000406#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000407 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000408#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409}
410
Eric Andersen8320b422003-04-02 10:13:26 +0000411static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000412{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000413#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000414 int result;
415 result = execute("ip addr flush dev %iface%", ifd, exec);
416 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000417 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000418#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000419 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000420#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000421}
422
Eric Andersen8320b422003-04-02 10:13:26 +0000423static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000424{
Eric Andersen8320b422003-04-02 10:13:26 +0000425 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000426#if ENABLE_FEATURE_IFUPDOWN_IP
427 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
428 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
429 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
430 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000431 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000432#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000433 /* ifconfig said to set iface up before it processes hw %hwaddress%,
434 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000435 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000436 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000437 result += execute("ifconfig %iface% %address% netmask %netmask%"
438 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000439 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000440 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000441 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000442#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000443}
444
Eric Andersen8320b422003-04-02 10:13:26 +0000445static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446{
Eric Andersen8320b422003-04-02 10:13:26 +0000447 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000448#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000449 result = execute("ip addr flush dev %iface%", ifd, exec);
450 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000451#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000452 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000453 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000454#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000455 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000456}
457
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000458#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000459struct dhcp_client_t
460{
461 const char *name;
462 const char *startcmd;
463 const char *stopcmd;
464};
465
466static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000467 { "dhcpcd",
468 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
469 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000470 },
471 { "dhclient",
472 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
473 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
474 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000475 { "pump",
476 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
477 "pump -i %iface% -k",
478 },
479 { "udhcpc",
480 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000481 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000482 },
483};
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000484#endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000485
Eric Andersen8320b422003-04-02 10:13:26 +0000486static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000487{
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000488#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenko856be772007-08-17 08:29:48 +0000489 int i;
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000490#if ENABLE_FEATURE_IFUPDOWN_IP
491 /* ip doesn't up iface when it configures it (unlike ifconfig) */
492 if (!execute("ip link set %iface% up", ifd, exec))
493 return 0;
494#endif
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000495 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000496 if (exists_execable(ext_dhcp_clients[i].name))
497 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
498 }
499 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000500 return 0;
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000501#elif ENABLE_APP_UDHCPC
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000502#if ENABLE_FEATURE_IFUPDOWN_IP
503 /* ip doesn't up iface when it configures it (unlike ifconfig) */
504 if (!execute("ip link set %iface% up", ifd, exec))
505 return 0;
506#endif
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000507 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
508 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
509 ifd, exec);
510#else
511 return 0; /* no dhcp support */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000512#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513}
514
Eric Andersen8320b422003-04-02 10:13:26 +0000515static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000516{
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000517#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000518 int i;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000519 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000520 if (exists_execable(ext_dhcp_clients[i].name))
521 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
522 }
523 bb_error_msg("no dhcp clients found, using static interface shutdown");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000524 return static_down(ifd, exec);
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000525#elif ENABLE_APP_UDHCPC
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000526 return execute("kill "
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000527 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
528#else
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000529 return 0; /* no dhcp support */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000530#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000531}
532
Denis Vlasenko68404f12008-03-17 09:00:54 +0000533static int manual_up_down(struct interface_defn_t *ifd ATTRIBUTE_UNUSED, execfn *exec ATTRIBUTE_UNUSED)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000534{
535 return 1;
536}
537
Eric Andersen8320b422003-04-02 10:13:26 +0000538static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000540 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000541 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
542 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000543}
544
Eric Andersen8320b422003-04-02 10:13:26 +0000545static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000547 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548}
549
Eric Andersen8320b422003-04-02 10:13:26 +0000550static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000551{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000552 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553}
554
Eric Andersen8320b422003-04-02 10:13:26 +0000555static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000557 return execute("start-stop-daemon --start -x wvdial "
558 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000559}
560
Eric Andersen8320b422003-04-02 10:13:26 +0000561static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000563 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000564 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565}
566
Denis Vlasenko05341252006-09-26 20:35:30 +0000567static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000568 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000569 { "wvdial", wvdial_up, wvdial_down, },
570 { "ppp", ppp_up, ppp_down, },
571 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000572 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000573 { "dhcp", dhcp_up, dhcp_down, },
574 { "loopback", loopback_up, loopback_down, },
575};
576
Denis Vlasenko05341252006-09-26 20:35:30 +0000577static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000578 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000579 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000580 methods
581};
582
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000583#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000584
Glenn L McGrath85737042003-01-14 23:26:57 +0000585static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000586{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000587 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000588 char *word;
589
Glenn L McGrath85737042003-01-14 23:26:57 +0000590 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000591 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000592
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000593 /* Stop on EOL */
594 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000595 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000596
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000597 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000598 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000599
600 /* Unless we are already at NUL, store NUL and advance */
601 if (word[length] != '\0')
602 word[length++] = '\0';
603
Glenn L McGrath85737042003-01-14 23:26:57 +0000604 *buf = word + length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000605
606 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000607}
608
Denis Vlasenko05341252006-09-26 20:35:30 +0000609static 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 +0000610{
611 int i;
612
Denis Vlasenko736230e2006-11-20 19:40:36 +0000613 if (!name)
614 return NULL;
615
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000616 for (i = 0; af[i]; i++) {
617 if (strcmp(af[i]->name, name) == 0) {
618 return af[i];
619 }
620 }
621 return NULL;
622}
623
Denis Vlasenko05341252006-09-26 20:35:30 +0000624static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000625{
626 int i;
627
Denis Vlasenko736230e2006-11-20 19:40:36 +0000628 if (!name)
629 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000630 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000631 for (i = 0; i < af->n_methods; i++) {
632 if (strcmp(af->method[i].name, name) == 0) {
633 return &af->method[i];
634 }
635 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000636 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000637}
638
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000639static const llist_t *find_list_string(const llist_t *list, const char *string)
640{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000641 if (string == NULL)
642 return NULL;
643
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000644 while (list) {
645 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000646 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000647 }
648 list = list->link;
649 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000650 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000651}
652
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000653static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000655 /* Let's try to be compatible.
656 *
657 * "man 5 interfaces" says:
658 * Lines starting with "#" are ignored. Note that end-of-line
659 * comments are NOT supported, comments must be on a line of their own.
660 * A line may be extended across multiple lines by making
661 * the last character a backslash.
662 *
663 * Seen elsewhere in example config file:
664 * A "#" character in the very first column makes the rest of the line
665 * be ignored. Blank lines are ignored. Lines may be indented freely.
666 * A "\" character at the very end of the line indicates the next line
667 * should be treated as a continuation of the current one.
668 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000669#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000670 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000671#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000672 struct interface_defn_t *currif = NULL;
673 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000675 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000676 char *first_word;
677 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000678 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
679
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000680 defn = xzalloc(sizeof(*defn));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000681 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000682
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000683 while ((buf = xmalloc_getline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000684#if ENABLE_DESKTOP
685 /* Trailing "\" concatenates lines */
686 char *p;
687 while ((p = last_char_is(buf, '\\')) != NULL) {
688 *p = '\0';
689 rest_of_line = xmalloc_getline(f);
690 if (!rest_of_line)
691 break;
692 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000693 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000694 free(rest_of_line);
695 buf = p;
696 }
697#endif
698 rest_of_line = buf;
699 first_word = next_word(&rest_of_line);
700 if (!first_word || *buf == '#') {
701 free(buf);
702 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000703 }
704
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000705 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000706#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000707 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000708
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000709 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000710 if (currmap->n_matches >= currmap->max_matches) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000711 currmap->max_matches = currmap->max_matches * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000712 currmap->match = xrealloc(currmap->match,
713 sizeof(*currmap->match) * currmap->max_matches);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000715 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000716 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000717 /*currmap->max_mappings = 0; - done by xzalloc */
718 /*currmap->n_mappings = 0;*/
719 /*currmap->mapping = NULL;*/
720 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000721 {
Eric Andersen8320b422003-04-02 10:13:26 +0000722 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000723 while (*where != NULL) {
724 where = &(*where)->next;
725 }
726 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000727 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 }
Eric Andersen8320b422003-04-02 10:13:26 +0000729 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000730#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000731 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000732 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000733 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000734#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000735 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000736#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000737#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000738 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000739#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000740 NULL
741 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000742 char *iface_name;
743 char *address_family_name;
744 char *method_name;
745 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000747 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000748 iface_name = next_word(&rest_of_line);
749 address_family_name = next_word(&rest_of_line);
750 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000752 if (method_name == NULL)
753 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000754
755 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000756 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000757
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000758 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000759 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000760
761 currif->iface = xstrdup(iface_name);
762
763 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000764 if (!currif->address_family)
765 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000766
767 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000768 if (!currif->method)
769 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000770
771 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
772 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000773 if ((strcmp(tmp->iface, currif->iface) == 0)
774 && (tmp->address_family == currif->address_family)
775 ) {
776 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000777 }
778 }
779 llist_add_to_end(&(defn->ifaces), (char*)currif);
780
781 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000782 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000783 } else if (strcmp(first_word, "auto") == 0) {
784 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000785
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000786 /* Check the interface isnt already listed */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000787 if (find_list_string(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000788 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000789 }
790
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000791 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000792 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
793 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000794 }
795 currently_processing = NONE;
796 } else {
797 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000798 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000799 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000800 bb_error_msg_and_die("option with empty value \"%s\"", buf);
801
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000802 if (strcmp(first_word, "up") != 0
803 && strcmp(first_word, "down") != 0
804 && strcmp(first_word, "pre-up") != 0
805 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000806 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000807 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000808 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000809 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000810 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000811 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000812 }
813 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000814 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000815 currif->option = xrealloc(currif->option,
816 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000817 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000818 debug_noise("\t%s=%s\n", first_word, rest_of_line);
819 currif->option[currif->n_options].name = xstrdup(first_word);
820 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000821 currif->n_options++;
822 break;
823 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000824#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000825 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000826 if (currmap->script != NULL)
827 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000828 currmap->script = xstrdup(next_word(&rest_of_line));
829 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000830 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000831 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000832 currmap->mapping = xrealloc(currmap->mapping,
833 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000834 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000835 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000836 currmap->n_mappings++;
837 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000838 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000839 }
840#endif
841 break;
842 case NONE:
843 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000844 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000845 }
846 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000847 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000848 } /* while (fgets) */
849
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000850 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000851 /* ferror does NOT set errno! */
852 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000853 }
854 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000855
856 return defn;
857}
858
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000859static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000860{
861 char *result;
862 char *here;
863 char *there;
864
Rob Landleyd921b2e2006-08-03 15:41:12 +0000865 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000866
867 for (here = there = result; *there != '=' && *there; there++) {
868 if (*there == '-')
869 *there = '_';
870 if (isalpha(*there))
871 *there = toupper(*there);
872
873 if (isalnum(*there) || *there == '_') {
874 *here = *there;
875 here++;
876 }
877 }
Rob Landleya3896512006-05-07 20:20:34 +0000878 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000879
880 return result;
881}
882
Rob Landleye813ddb2006-02-28 03:53:14 +0000883static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000884{
885 char **environend;
886 int i;
887 const int n_env_entries = iface->n_options + 5;
888 char **ppch;
889
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000890 if (my_environ != NULL) {
891 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892 free(*ppch);
893 *ppch = NULL;
894 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000895 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000896 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000897 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
898 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000899
900 for (i = 0; i < iface->n_options; i++) {
901 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000902 || strcmp(iface->option[i].name, "down") == 0
903 || strcmp(iface->option[i].name, "pre-up") == 0
904 || strcmp(iface->option[i].name, "post-down") == 0
905 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906 continue;
907 }
908 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000909 }
910
911 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000912 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000913 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000915 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000916}
917
918static int doit(char *str)
919{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000920 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000921 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000922 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000923 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924 pid_t child;
925 int status;
926
927 fflush(NULL);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000928 child = fork();
929 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000930 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000931 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000932 case 0: /* child */
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000933 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
Denis Vlasenko05341252006-09-26 20:35:30 +0000934 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000936 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000937 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
938 return 0;
939 }
940 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000941 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000942}
943
Rob Landleye813ddb2006-02-28 03:53:14 +0000944static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000945{
946 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000947 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948 for (i = 0; i < ifd->n_options; i++) {
949 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000950 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951 return 0;
952 }
953 }
954 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000955
Rob Landleyd921b2e2006-08-03 15:41:12 +0000956 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000957 /* heh, we don't bother free'ing it */
958 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959}
960
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000961static int check(char *str)
962{
Eric Andersen8320b422003-04-02 10:13:26 +0000963 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964}
965
Eric Andersen8320b422003-04-02 10:13:26 +0000966static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000967{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000968 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000969 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000970 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000971 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000972 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000973 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000974}
975
976static int iface_down(struct interface_defn_t *iface)
977{
Eric Andersen8320b422003-04-02 10:13:26 +0000978 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000979 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000980 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000981 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000982 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000983 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000984}
985
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000986#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000987static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000988{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000989 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +0000990 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000991 pid_t pid;
992
Denis Vlasenko37188322008-02-16 13:20:56 +0000993 xpiped_pair(infd);
994 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000995
996 fflush(NULL);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000997 pid = fork();
998
999 switch (pid) {
1000 case -1: /* failure */
1001 bb_perror_msg_and_die("fork");
1002 case 0: /* child */
1003 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001004 close(infd.wr);
1005 close(outfd.rd);
1006 xmove_fd(infd.rd, 0);
1007 xmove_fd(outfd.wr, 1);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001008 BB_EXECVP(command, argv);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001009 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001010 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001011 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001012 close(infd.rd);
1013 close(outfd.wr);
1014 *in = fdopen(infd.wr, "w");
1015 *out = fdopen(outfd.rd, "r");
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001016 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001017}
1018
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001019static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020{
1021 FILE *in, *out;
1022 int i, status;
1023 pid_t pid;
1024
Rob Landleyd921b2e2006-08-03 15:41:12 +00001025 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001027 /* Run the mapping script. Never fails. */
1028 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001029
1030 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031 for (i = 0; i < map->n_mappings; i++) {
1032 fprintf(in, "%s\n", map->mapping[i]);
1033 }
1034 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001035 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001036
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001037 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001038 /* If the mapping script exited successfully, try to
1039 * grab a line of output and use that as the name of the
1040 * logical interface. */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001041 char *new_logical = xmalloc_getline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001042
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001043 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001044 /* If we are able to read a line of output from the script,
1045 * remove any trailing whitespace and use this value
1046 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001047 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001048
1049 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001050 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001051
1052 free(logical);
1053 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001054 }
1055 }
Eric Andersen8a931792003-07-03 10:20:29 +00001056
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001057 fclose(out);
1058
Eric Andersen8a931792003-07-03 10:20:29 +00001059 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001060}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001061#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001063static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001064{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001065 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001066 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001068 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001069 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001070 && (search->data[iface_len] == '=')
1071 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001072 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001074 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001076 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001077}
1078
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001079/* read the previous state from the state file */
1080static llist_t *read_iface_state(void)
1081{
1082 llist_t *state_list = NULL;
Denis Vlasenko2a86a612007-07-19 21:49:30 +00001083 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001084
1085 if (state_fp) {
1086 char *start, *end_ptr;
1087 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1088 /* We should only need to check for a single character */
1089 end_ptr = start + strcspn(start, " \t\n");
1090 *end_ptr = '\0';
1091 llist_add_to(&state_list, start);
1092 }
1093 fclose(state_fp);
1094 }
1095 return state_list;
1096}
1097
1098
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001099int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00001100int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001101{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001102 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001103 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001104 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001105 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001106 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001107
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001108 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001109 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001110 /* ifup command */
1111 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001112 }
1113
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001114 getopt32(argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001116 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001117 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001118 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001119 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001120
Eric Andersen8320b422003-04-02 10:13:26 +00001121 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001123 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001125 startup_PATH = getenv("PATH");
1126 if (!startup_PATH) startup_PATH = "";
1127
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001128 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001129 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001130 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001131 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001132 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001133 }
1134
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001135 /* Update the interfaces */
1136 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001137 llist_t *iface_list;
1138 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001139 char *iface;
1140 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001141 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001142 bool okay = 0;
1143 unsigned cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001144
Rob Landleyd921b2e2006-08-03 15:41:12 +00001145 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001148 pch = strchr(iface, '=');
1149 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001150 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001151 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001152 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001153 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001155
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001156 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001157 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001158 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001159
1160 if (cmds == iface_up) {
1161 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001162 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001163 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 continue;
1165 }
1166 } else {
1167 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001168 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001169 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 continue;
1171 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001172 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001173 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001175
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001176#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001177 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001178 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179
1180 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001181 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001182 for (i = 0; i < currmap->n_matches; i++) {
1183 if (fnmatch(currmap->match[i], liface, 0) != 0)
1184 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001185 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001186 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 }
Eric Andersen8a931792003-07-03 10:20:29 +00001188 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001189 break;
1190 }
1191 }
1192 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001193#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194
Eric Andersen8320b422003-04-02 10:13:26 +00001195 iface_list = defn->ifaces;
1196 while (iface_list) {
1197 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001198 if (strcmp(liface, currif->iface) == 0) {
1199 char *oldiface = currif->iface;
1200
1201 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001202 currif->iface = iface;
1203
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001204 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001206 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001207 cmds_ret = cmds(currif);
1208 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001209 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001210 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001211 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001212 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001213 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001214 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001215
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001216 currif->iface = oldiface;
1217 }
Eric Andersen8320b422003-04-02 10:13:26 +00001218 iface_list = iface_list->link;
1219 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001220 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001221 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001222 }
1223
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001224 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001225 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001226 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001227 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001228 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001229 FILE *state_fp;
1230 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001231 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001232 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001233
1234 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001235 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001236 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001237 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001239 free(iface_state->data);
1240 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001241 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001242 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001243 /* Remove an interface from state_list */
1244 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001245 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001246 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001248 /* Actually write the new state */
Denis Vlasenko2a86a612007-07-19 21:49:30 +00001249 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
Denis Vlasenkof92df582007-05-02 22:22:23 +00001250 state = state_list;
1251 while (state) {
1252 if (state->data) {
1253 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001254 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001255 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001256 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001257 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001258 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001259 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 }
1261
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001262 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001263}