blob: c12391863acef2ec95e998c56c32b2715a925433 [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
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000034#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000035
36/* Forward declaration */
37struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000038
Denis Vlasenko097c3242006-11-27 16:59:15 +000039typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000040
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000041struct method_t {
42 const char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000043 int (*up)(struct interface_defn_t *ifd, execfn *e);
44 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000045};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000046
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000047struct address_family_t {
48 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000049 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000050 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000051};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000052
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000053struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000054 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000055
56 int max_matches;
57 int n_matches;
58 char **match;
59
60 char *script;
61
62 int max_mappings;
63 int n_mappings;
64 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000065};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000067struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000068 char *name;
69 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000070};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000071
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000072struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000073 const struct address_family_t *address_family;
74 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075
Rob Landleye813ddb2006-02-28 03:53:14 +000076 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000077 int max_options;
78 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000079 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080};
81
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000082struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000083 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000084 llist_t *ifaces;
85 struct mapping_defn_t *mappings;
86};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000087
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000088#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
89enum {
90 OPT_do_all = 0x1,
91 OPT_no_act = 0x2,
92 OPT_verbose = 0x4,
93 OPT_force = 0x8,
94 OPT_no_mappings = 0x10,
95};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000096#define DO_ALL (option_mask32 & OPT_do_all)
97#define NO_ACT (option_mask32 & OPT_no_act)
98#define VERBOSE (option_mask32 & OPT_verbose)
99#define FORCE (option_mask32 & OPT_force)
100#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000101
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000102static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000103
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000104static const char *startup_PATH;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000105
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000106#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
107
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000108static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000109{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000110 /* xasprintf trick will be smaller, but we are often
111 * called with str_length == 1 - don't want to have
112 * THAT much of malloc/freeing! */
113 char *buf = *bufp;
114 int len = (buf ? strlen(buf) : 0);
115 str_length++;
116 buf = xrealloc(buf, len + str_length);
117 /* copies at most str_length-1 chars! */
118 safe_strncpy(buf + len, str, str_length);
119 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000120}
121
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000122static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000123{
124 int i = strncmp(l, r, llen);
125
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000126 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000127 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000128 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000129}
130
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000131static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000132{
133 int i;
134
135 if (strncmpz(id, "iface", idlen) == 0) {
Denis Vlasenkoff2b6d22007-11-23 03:39:45 +0000136 static char *label_buf;
137 //char *result;
138
139 free(label_buf);
140 label_buf = xstrdup(ifd->iface);
141 // Remove virtual iface suffix - why?
142 // ubuntu's ifup doesn't do this
143 //result = strchrnul(label_buf, ':');
144 //*result = '\0';
Denis Vlasenko05341252006-09-26 20:35:30 +0000145 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000146 }
147 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000148 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000149 }
150 for (i = 0; i < ifd->n_options; i++) {
151 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
152 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000153 }
154 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000155 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000156}
157
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000158#if ENABLE_FEATURE_IFUPDOWN_IP
159static int count_netmask_bits(const char *dotted_quad)
160{
161// int result;
162// unsigned a, b, c, d;
163// /* Found a netmask... Check if it is dotted quad */
164// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
165// return -1;
166// if ((a|b|c|d) >> 8)
167// return -1; /* one of numbers is >= 256 */
168// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
169// d = ~d; /* 11110000 -> 00001111 */
170
171 /* Shorter version */
172 int result;
173 struct in_addr ip;
174 unsigned d;
175
176 if (inet_aton(dotted_quad, &ip) == 0)
177 return -1; /* malformed dotted IP */
178 d = ntohl(ip.s_addr); /* IP in host order */
179 d = ~d; /* 11110000 -> 00001111 */
180 if (d & (d+1)) /* check that it is in 00001111 form */
181 return -1; /* no it is not */
182 result = 32;
183 while (d) {
184 d >>= 1;
185 result--;
186 }
187 return result;
188}
189#endif
190
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000191static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000192{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000193 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
194 int okay[MAX_OPT_DEPTH] = { 1 };
195 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000196 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000197
198 while (*command) {
199 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000200 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000201 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000202 command++;
203 break;
204 case '\\':
205 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000206 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000207 command += 2;
208 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000209 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000210 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000211 }
212 break;
213 case '[':
214 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000215 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000216 okay[opt_depth] = 1;
217 opt_depth++;
218 command += 2;
219 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000220 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000221 command++;
222 }
223 break;
224 case ']':
225 if (command[1] == ']' && opt_depth > 1) {
226 opt_depth--;
227 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000228 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000229 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000230 command += 2;
231 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000232 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000233 command++;
234 }
235 break;
236 case '%':
237 {
238 char *nextpercent;
239 char *varvalue;
240
241 command++;
242 nextpercent = strchr(command, '%');
243 if (!nextpercent) {
244 errno = EUNBALPER;
245 free(result);
246 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000247 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000248
249 varvalue = get_var(command, nextpercent - command, ifd);
250
251 if (varvalue) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000252 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000253 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000254#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000255 /* Sigh... Add a special case for 'ip' to convert from
256 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000257 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000258 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000259 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000260 if (varvalue) {
261 res = count_netmask_bits(varvalue);
262 if (res > 0) {
263 const char *argument = utoa(res);
264 addstr(&result, argument, strlen(argument));
265 command = nextpercent + 1;
266 break;
267 }
Eric Andersen8320b422003-04-02 10:13:26 +0000268 }
Eric Andersen8320b422003-04-02 10:13:26 +0000269 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000270#endif
271 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000272 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000273
274 command = nextpercent + 1;
275 }
276 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000277 }
278 }
279
280 if (opt_depth > 1) {
281 errno = EUNBALBRACK;
282 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000283 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000284 }
285
286 if (!okay[0]) {
287 errno = EUNDEFVAR;
288 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000289 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000290 }
291
Denis Vlasenko05341252006-09-26 20:35:30 +0000292 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000293}
294
Denis Vlasenko05341252006-09-26 20:35:30 +0000295/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000296static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000297{
298 char *out;
299 int ret;
300
301 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000302 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000303 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000304 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000305 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000306 /* out == "": parsed ok but not all needed variables known, skip */
307 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000308
309 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000310 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000311 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000312 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000313 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000314}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000315#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000317#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000318static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000319{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000320#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000321 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000322 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000323 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000324 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000325#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000326 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000327#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000328}
329
Eric Andersen8320b422003-04-02 10:13:26 +0000330static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000332#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000333 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000334#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000335 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000336#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000337}
338
Eric Andersen8320b422003-04-02 10:13:26 +0000339static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000340{
Eric Andersen8320b422003-04-02 10:13:26 +0000341 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000342#if ENABLE_FEATURE_IFUPDOWN_IP
343 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
344 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
345 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
346 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000347#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000348 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000349 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000350 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000351#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000352 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000353}
354
Eric Andersen8320b422003-04-02 10:13:26 +0000355static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000356{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000357#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000358 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000359#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000360 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000361#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362}
363
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000364#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000365static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366{
Eric Andersen8320b422003-04-02 10:13:26 +0000367 int result;
368 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000369 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000370 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000371 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000372 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000373 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000374}
375
Eric Andersen8320b422003-04-02 10:13:26 +0000376static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377{
Denis Vlasenko05341252006-09-26 20:35:30 +0000378 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000380#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000381
Denis Vlasenko05341252006-09-26 20:35:30 +0000382static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000383#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000385#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000386 { "static", static_up6, static_down6, },
387 { "loopback", loopback_up6, loopback_down6, },
388};
389
Denis Vlasenko05341252006-09-26 20:35:30 +0000390static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000391 "inet6",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000392 ARRAY_SIZE(methods6),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000393 methods6
394};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000395#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000396
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000397#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000398static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000399{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000400#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000401 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000402 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000403 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000404 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000405#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000406 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000407#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000408}
409
Eric Andersen8320b422003-04-02 10:13:26 +0000410static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000411{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000412#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000413 int result;
414 result = execute("ip addr flush dev %iface%", ifd, exec);
415 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000416 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000418 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000419#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000420}
421
Eric Andersen8320b422003-04-02 10:13:26 +0000422static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000423{
Eric Andersen8320b422003-04-02 10:13:26 +0000424 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000425#if ENABLE_FEATURE_IFUPDOWN_IP
426 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
427 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
428 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
429 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000430 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000431#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000432 /* ifconfig said to set iface up before it processes hw %hwaddress%,
433 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000434 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000435 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000436 result += execute("ifconfig %iface% %address% netmask %netmask%"
437 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000438 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000439 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000440 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000441#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000442}
443
Eric Andersen8320b422003-04-02 10:13:26 +0000444static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000445{
Eric Andersen8320b422003-04-02 10:13:26 +0000446 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000447#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000448 result = execute("ip addr flush dev %iface%", ifd, exec);
449 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000450#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000451 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000452 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000453#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000454 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000455}
456
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000457#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000458struct dhcp_client_t
459{
460 const char *name;
461 const char *startcmd;
462 const char *stopcmd;
463};
464
465static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000466 { "dhcpcd",
467 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
468 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000469 },
470 { "dhclient",
471 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
472 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
473 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000474 { "pump",
475 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
476 "pump -i %iface% -k",
477 },
478 { "udhcpc",
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000479 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
480 "[[ -s %script%]][[ %udhcpc_opts%]]",
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
Denis Vlasenko85c24712008-03-17 09:04:04 +0000486#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Eric Andersen8320b422003-04-02 10:13:26 +0000487static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000488{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000489 unsigned 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 Vlasenko85c24712008-03-17 09:04:04 +0000501}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000502#elif ENABLE_APP_UDHCPC
Denis Vlasenko85c24712008-03-17 09:04:04 +0000503static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
504{
Denis Vlasenko6cd84da2007-07-21 14:57:54 +0000505#if ENABLE_FEATURE_IFUPDOWN_IP
506 /* ip doesn't up iface when it configures it (unlike ifconfig) */
507 if (!execute("ip link set %iface% up", ifd, exec))
508 return 0;
509#endif
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000510 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
Denis Vlasenko2e4c3c42008-04-02 13:04:19 +0000511 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000512 ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000514#else
515static int dhcp_up(struct interface_defn_t *ifd ATTRIBUTE_UNUSED,
516 execfn *exec ATTRIBUTE_UNUSED)
517{
518 return 0; /* no dhcp support */
519}
520#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000521
Denis Vlasenko85c24712008-03-17 09:04:04 +0000522#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Eric Andersen8320b422003-04-02 10:13:26 +0000523static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000524{
Denis Vlasenko6b06cb82008-05-15 21:30:45 +0000525 unsigned i;
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000526 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000527 if (exists_execable(ext_dhcp_clients[i].name))
528 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
529 }
530 bb_error_msg("no dhcp clients found, using static interface shutdown");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000531 return static_down(ifd, exec);
Denis Vlasenko85c24712008-03-17 09:04:04 +0000532}
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000533#elif ENABLE_APP_UDHCPC
Denis Vlasenko85c24712008-03-17 09:04:04 +0000534static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
535{
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000536 return execute("kill "
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000537 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000538}
Denis Vlasenko85c24712008-03-17 09:04:04 +0000539#else
540static int dhcp_down(struct interface_defn_t *ifd ATTRIBUTE_UNUSED,
541 execfn *exec ATTRIBUTE_UNUSED)
542{
543 return 0; /* no dhcp support */
544}
545#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000546
Denis Vlasenko68404f12008-03-17 09:00:54 +0000547static int manual_up_down(struct interface_defn_t *ifd ATTRIBUTE_UNUSED, execfn *exec ATTRIBUTE_UNUSED)
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000548{
549 return 1;
550}
551
Eric Andersen8320b422003-04-02 10:13:26 +0000552static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000554 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
Denis Vlasenko1caca342007-08-02 10:14:29 +0000555 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
556 " --returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000557}
558
Eric Andersen8320b422003-04-02 10:13:26 +0000559static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000560{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000561 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562}
563
Eric Andersen8320b422003-04-02 10:13:26 +0000564static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000565{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000566 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000567}
568
Eric Andersen8320b422003-04-02 10:13:26 +0000569static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000570{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000571 return execute("start-stop-daemon --start -x wvdial "
572 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000573}
574
Eric Andersen8320b422003-04-02 10:13:26 +0000575static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000577 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000578 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000579}
580
Denis Vlasenko05341252006-09-26 20:35:30 +0000581static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000582 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000583 { "wvdial", wvdial_up, wvdial_down, },
584 { "ppp", ppp_up, ppp_down, },
585 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000586 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000587 { "dhcp", dhcp_up, dhcp_down, },
588 { "loopback", loopback_up, loopback_down, },
589};
590
Denis Vlasenko05341252006-09-26 20:35:30 +0000591static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000592 "inet",
Denis Vlasenko80b8b392007-06-25 10:55:35 +0000593 ARRAY_SIZE(methods),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594 methods
595};
596
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000597#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000598
Glenn L McGrath85737042003-01-14 23:26:57 +0000599static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000600{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000601 unsigned length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000602 char *word;
603
Glenn L McGrath85737042003-01-14 23:26:57 +0000604 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000605 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000606
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000607 /* Stop on EOL */
608 if (*word == '\0')
Denis Vlasenko05341252006-09-26 20:35:30 +0000609 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000610
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000611 /* Find the length of this word (can't be 0) */
Glenn L McGrath85737042003-01-14 23:26:57 +0000612 length = strcspn(word, " \t\n");
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000613
614 /* Unless we are already at NUL, store NUL and advance */
615 if (word[length] != '\0')
616 word[length++] = '\0';
617
Glenn L McGrath85737042003-01-14 23:26:57 +0000618 *buf = word + length;
Glenn L McGrath85737042003-01-14 23:26:57 +0000619
620 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621}
622
Denis Vlasenko05341252006-09-26 20:35:30 +0000623static 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 +0000624{
625 int i;
626
Denis Vlasenko736230e2006-11-20 19:40:36 +0000627 if (!name)
628 return NULL;
629
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000630 for (i = 0; af[i]; i++) {
631 if (strcmp(af[i]->name, name) == 0) {
632 return af[i];
633 }
634 }
635 return NULL;
636}
637
Denis Vlasenko05341252006-09-26 20:35:30 +0000638static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000639{
640 int i;
641
Denis Vlasenko736230e2006-11-20 19:40:36 +0000642 if (!name)
643 return NULL;
Bernhard Reutner-Fischere747f622007-10-06 20:47:53 +0000644 /* TODO: use index_in_str_array() */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000645 for (i = 0; i < af->n_methods; i++) {
646 if (strcmp(af->method[i].name, name) == 0) {
647 return &af->method[i];
648 }
649 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000650 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000651}
652
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000653static const llist_t *find_list_string(const llist_t *list, const char *string)
654{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000655 if (string == NULL)
656 return NULL;
657
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000658 while (list) {
659 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000660 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000661 }
662 list = list->link;
663 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000664 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000665}
666
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000667static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000669 /* Let's try to be compatible.
670 *
671 * "man 5 interfaces" says:
672 * Lines starting with "#" are ignored. Note that end-of-line
673 * comments are NOT supported, comments must be on a line of their own.
674 * A line may be extended across multiple lines by making
675 * the last character a backslash.
676 *
677 * Seen elsewhere in example config file:
678 * A "#" character in the very first column makes the rest of the line
679 * be ignored. Blank lines are ignored. Lines may be indented freely.
680 * A "\" character at the very end of the line indicates the next line
681 * should be treated as a continuation of the current one.
682 */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000683#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000684 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000685#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000686 struct interface_defn_t *currif = NULL;
687 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000689 char *buf;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000690 char *first_word;
691 char *rest_of_line;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000692 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
693
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000694 defn = xzalloc(sizeof(*defn));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000695 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000696
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000697 while ((buf = xmalloc_fgetline(f)) != NULL) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000698#if ENABLE_DESKTOP
699 /* Trailing "\" concatenates lines */
700 char *p;
701 while ((p = last_char_is(buf, '\\')) != NULL) {
702 *p = '\0';
Denis Vlasenko8ee649a2008-03-26 20:04:27 +0000703 rest_of_line = xmalloc_fgetline(f);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000704 if (!rest_of_line)
705 break;
706 p = xasprintf("%s%s", buf, rest_of_line);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000707 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000708 free(rest_of_line);
709 buf = p;
710 }
711#endif
712 rest_of_line = buf;
713 first_word = next_word(&rest_of_line);
714 if (!first_word || *buf == '#') {
715 free(buf);
716 continue; /* blank/comment line */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000717 }
718
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000719 if (strcmp(first_word, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000720#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000721 currmap = xzalloc(sizeof(*currmap));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000722
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000723 while ((first_word = next_word(&rest_of_line)) != NULL) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000724 if (currmap->n_matches >= currmap->max_matches) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000725 currmap->max_matches = currmap->max_matches * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000726 currmap->match = xrealloc(currmap->match,
727 sizeof(*currmap->match) * currmap->max_matches);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000728 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000729 currmap->match[currmap->n_matches++] = xstrdup(first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000730 }
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000731 /*currmap->max_mappings = 0; - done by xzalloc */
732 /*currmap->n_mappings = 0;*/
733 /*currmap->mapping = NULL;*/
734 /*currmap->script = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000735 {
Eric Andersen8320b422003-04-02 10:13:26 +0000736 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000737 while (*where != NULL) {
738 where = &(*where)->next;
739 }
740 *where = currmap;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000741 /*currmap->next = NULL;*/
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000742 }
Eric Andersen8320b422003-04-02 10:13:26 +0000743 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000744#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000745 currently_processing = MAPPING;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000746 } else if (strcmp(first_word, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000747 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000748#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000749 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000750#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000751#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000752 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000753#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000754 NULL
755 };
Denis Vlasenko05341252006-09-26 20:35:30 +0000756 char *iface_name;
757 char *address_family_name;
758 char *method_name;
759 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000761 currif = xzalloc(sizeof(*currif));
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000762 iface_name = next_word(&rest_of_line);
763 address_family_name = next_word(&rest_of_line);
764 method_name = next_word(&rest_of_line);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000765
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000766 if (method_name == NULL)
767 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000768
769 /* ship any trailing whitespace */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000770 rest_of_line = skip_whitespace(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000771
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000772 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000773 bb_error_msg_and_die("too many parameters \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000774
775 currif->iface = xstrdup(iface_name);
776
777 currif->address_family = get_address_family(addr_fams, address_family_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000778 if (!currif->address_family)
779 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000780
781 currif->method = get_method(currif->address_family, method_name);
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000782 if (!currif->method)
783 bb_error_msg_and_die("unknown method \"%s\"", method_name);
Denis Vlasenko05341252006-09-26 20:35:30 +0000784
785 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
786 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000787 if ((strcmp(tmp->iface, currif->iface) == 0)
788 && (tmp->address_family == currif->address_family)
789 ) {
790 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
Denis Vlasenko05341252006-09-26 20:35:30 +0000791 }
792 }
793 llist_add_to_end(&(defn->ifaces), (char*)currif);
794
795 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000796 currently_processing = IFACE;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000797 } else if (strcmp(first_word, "auto") == 0) {
798 while ((first_word = next_word(&rest_of_line)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000799
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000800 /* Check the interface isnt already listed */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000801 if (find_list_string(defn->autointerfaces, first_word)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000802 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000803 }
804
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000805 /* Add the interface to the list */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000806 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
807 debug_noise("\nauto %s\n", first_word);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000808 }
809 currently_processing = NONE;
810 } else {
811 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000812 case IFACE:
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000813 if (rest_of_line[0] == '\0')
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000814 bb_error_msg_and_die("option with empty value \"%s\"", buf);
815
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000816 if (strcmp(first_word, "up") != 0
817 && strcmp(first_word, "down") != 0
818 && strcmp(first_word, "pre-up") != 0
819 && strcmp(first_word, "post-down") != 0
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000820 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000821 int i;
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000822 for (i = 0; i < currif->n_options; i++) {
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000823 if (strcmp(currif->option[i].name, first_word) == 0)
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000824 bb_error_msg_and_die("duplicate option \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000825 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000826 }
827 if (currif->n_options >= currif->max_options) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000828 currif->max_options += 10;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000829 currif->option = xrealloc(currif->option,
830 sizeof(*currif->option) * currif->max_options);
Denis Vlasenko05341252006-09-26 20:35:30 +0000831 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000832 debug_noise("\t%s=%s\n", first_word, rest_of_line);
833 currif->option[currif->n_options].name = xstrdup(first_word);
834 currif->option[currif->n_options].value = xstrdup(rest_of_line);
Denis Vlasenko05341252006-09-26 20:35:30 +0000835 currif->n_options++;
836 break;
837 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000838#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000839 if (strcmp(first_word, "script") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000840 if (currmap->script != NULL)
841 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000842 currmap->script = xstrdup(next_word(&rest_of_line));
843 } else if (strcmp(first_word, "map") == 0) {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000844 if (currmap->n_mappings >= currmap->max_mappings) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000845 currmap->max_mappings = currmap->max_mappings * 2 + 1;
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000846 currmap->mapping = xrealloc(currmap->mapping,
847 sizeof(char *) * currmap->max_mappings);
Denis Vlasenko05341252006-09-26 20:35:30 +0000848 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000849 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
Denis Vlasenko05341252006-09-26 20:35:30 +0000850 currmap->n_mappings++;
851 } else {
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000852 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Denis Vlasenko05341252006-09-26 20:35:30 +0000853 }
854#endif
855 break;
856 case NONE:
857 default:
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +0000858 bb_error_msg_and_die("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859 }
860 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000861 free(buf);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +0000862 } /* while (fgets) */
863
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864 if (ferror(f) != 0) {
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000865 /* ferror does NOT set errno! */
866 bb_error_msg_and_die("%s: I/O error", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000867 }
868 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000869
870 return defn;
871}
872
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000873static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000874{
875 char *result;
876 char *here;
877 char *there;
878
Rob Landleyd921b2e2006-08-03 15:41:12 +0000879 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000880
881 for (here = there = result; *there != '=' && *there; there++) {
882 if (*there == '-')
883 *there = '_';
884 if (isalpha(*there))
885 *there = toupper(*there);
886
887 if (isalnum(*there) || *there == '_') {
888 *here = *there;
889 here++;
890 }
891 }
Rob Landleya3896512006-05-07 20:20:34 +0000892 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893
894 return result;
895}
896
Rob Landleye813ddb2006-02-28 03:53:14 +0000897static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000898{
899 char **environend;
900 int i;
901 const int n_env_entries = iface->n_options + 5;
902 char **ppch;
903
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000904 if (my_environ != NULL) {
905 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906 free(*ppch);
907 *ppch = NULL;
908 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000909 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000910 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000911 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
912 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000913
914 for (i = 0; i < iface->n_options; i++) {
915 if (strcmp(iface->option[i].name, "up") == 0
Denis Vlasenkoa4836912007-07-03 08:26:24 +0000916 || strcmp(iface->option[i].name, "down") == 0
917 || strcmp(iface->option[i].name, "pre-up") == 0
918 || strcmp(iface->option[i].name, "post-down") == 0
919 ) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000920 continue;
921 }
922 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000923 }
924
925 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000928 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000929 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930}
931
932static int doit(char *str)
933{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000934 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000935 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000937 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938 pid_t child;
939 int status;
940
941 fflush(NULL);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000942 child = vfork();
Denis Vlasenko2375d752006-12-19 23:15:46 +0000943 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000944 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000945 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000946 case 0: /* child */
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000947 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
Denis Vlasenko0764a7f2008-03-19 16:24:17 +0000948 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949 }
Denis Vlasenkofb0eba72008-01-02 19:55:04 +0000950 safe_waitpid(child, &status, 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
952 return 0;
953 }
954 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000955 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000956}
957
Rob Landleye813ddb2006-02-28 03:53:14 +0000958static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000959{
960 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000961 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000962 for (i = 0; i < ifd->n_options; i++) {
963 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000964 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000965 return 0;
966 }
967 }
968 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000969
Rob Landleyd921b2e2006-08-03 15:41:12 +0000970 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000971 /* heh, we don't bother free'ing it */
972 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000973}
974
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000975static int check(char *str)
976{
Eric Andersen8320b422003-04-02 10:13:26 +0000977 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000978}
979
Eric Andersen8320b422003-04-02 10:13:26 +0000980static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000981{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000982 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000983 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000984 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000985 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000986 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000987 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000988}
989
990static int iface_down(struct interface_defn_t *iface)
991{
Eric Andersen8320b422003-04-02 10:13:26 +0000992 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000993 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000994 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000995 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000996 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000997 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000998}
999
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001000#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001001static int popen2(FILE **in, FILE **out, char *command, char *param)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001002{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001003 char *argv[3] = { command, param, NULL };
Denis Vlasenko37188322008-02-16 13:20:56 +00001004 struct fd_pair infd, outfd;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001005 pid_t pid;
1006
Denis Vlasenko37188322008-02-16 13:20:56 +00001007 xpiped_pair(infd);
1008 xpiped_pair(outfd);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001009
1010 fflush(NULL);
Denis Vlasenko82604e92008-07-01 15:59:42 +00001011 pid = vfork();
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001012
Denis Vlasenko82604e92008-07-01 15:59:42 +00001013 switch (pid) {
1014 case -1: /* failure */
1015 bb_perror_msg_and_die("vfork");
1016 case 0: /* child */
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001017 /* NB: close _first_, then move fds! */
Denis Vlasenko37188322008-02-16 13:20:56 +00001018 close(infd.wr);
1019 close(outfd.rd);
1020 xmove_fd(infd.rd, 0);
1021 xmove_fd(outfd.wr, 1);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001022 BB_EXECVP(command, argv);
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001023 _exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001024 }
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001025 /* parent */
Denis Vlasenko37188322008-02-16 13:20:56 +00001026 close(infd.rd);
1027 close(outfd.wr);
1028 *in = fdopen(infd.wr, "w");
1029 *out = fdopen(outfd.rd, "r");
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001030 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031}
1032
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001033static char *run_mapping(char *physical, struct mapping_defn_t *map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001034{
1035 FILE *in, *out;
1036 int i, status;
1037 pid_t pid;
1038
Rob Landleyd921b2e2006-08-03 15:41:12 +00001039 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001040
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001041 /* Run the mapping script. Never fails. */
1042 pid = popen2(&in, &out, map->script, physical);
Eric Andersen8a931792003-07-03 10:20:29 +00001043
1044 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001045 for (i = 0; i < map->n_mappings; i++) {
1046 fprintf(in, "%s\n", map->mapping[i]);
1047 }
1048 fclose(in);
Denis Vlasenkofb0eba72008-01-02 19:55:04 +00001049 safe_waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001050
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001051 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001052 /* If the mapping script exited successfully, try to
1053 * grab a line of output and use that as the name of the
1054 * logical interface. */
Denis Vlasenko8ee649a2008-03-26 20:04:27 +00001055 char *new_logical = xmalloc_fgetline(out);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001056
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001057 if (new_logical) {
Eric Andersen8a931792003-07-03 10:20:29 +00001058 /* If we are able to read a line of output from the script,
1059 * remove any trailing whitespace and use this value
1060 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001061 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001062
1063 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001064 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001065
1066 free(logical);
1067 logical = new_logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068 }
1069 }
Eric Andersen8a931792003-07-03 10:20:29 +00001070
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001071 fclose(out);
1072
Eric Andersen8a931792003-07-03 10:20:29 +00001073 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001074}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001075#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001076
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001077static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001078{
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001079 unsigned iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001080 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001081
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001082 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001083 if ((strncmp(search->data, iface, iface_len) == 0)
Denis Vlasenko1e18f1b2008-02-16 13:19:19 +00001084 && (search->data[iface_len] == '=')
1085 ) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001086 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001087 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001088 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001089 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001090 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001091}
1092
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001093/* read the previous state from the state file */
1094static llist_t *read_iface_state(void)
1095{
1096 llist_t *state_list = NULL;
Denis Vlasenko2a86a612007-07-19 21:49:30 +00001097 FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001098
1099 if (state_fp) {
1100 char *start, *end_ptr;
1101 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1102 /* We should only need to check for a single character */
1103 end_ptr = start + strcspn(start, " \t\n");
1104 *end_ptr = '\0';
1105 llist_add_to(&state_list, start);
1106 }
1107 fclose(state_fp);
1108 }
1109 return state_list;
1110}
1111
1112
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001113int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +00001114int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115{
Denis Vlasenkocd5c61c2008-02-15 16:20:26 +00001116 int (*cmds)(struct interface_defn_t *);
Eric Andersen8320b422003-04-02 10:13:26 +00001117 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001118 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001119 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001120 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001121
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001122 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001123 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124 /* ifup command */
1125 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001126 }
1127
Denis Vlasenkofe7cd642007-08-18 15:32:12 +00001128 getopt32(argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001129 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001130 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001131 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001132 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001133 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134
Eric Andersen8320b422003-04-02 10:13:26 +00001135 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001137 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001139 startup_PATH = getenv("PATH");
1140 if (!startup_PATH) startup_PATH = "";
1141
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001142 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001143 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001144 target_list = defn->autointerfaces;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001145 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001146 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147 }
1148
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001149 /* Update the interfaces */
1150 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001151 llist_t *iface_list;
1152 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001153 char *iface;
1154 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001155 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001156 bool okay = 0;
Denis Vlasenko6b06cb82008-05-15 21:30:45 +00001157 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001158
Rob Landleyd921b2e2006-08-03 15:41:12 +00001159 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001160 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001162 pch = strchr(iface, '=');
1163 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001164 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001165 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001167 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001169
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001170 if (!FORCE) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001171 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001172 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001173
1174 if (cmds == iface_up) {
1175 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001176 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001177 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001178 continue;
1179 }
1180 } else {
1181 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001182 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001183 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184 continue;
1185 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001186 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001187 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001189
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001190#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001191 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001192 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001193
1194 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001195 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001196 for (i = 0; i < currmap->n_matches; i++) {
1197 if (fnmatch(currmap->match[i], liface, 0) != 0)
1198 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001199 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001200 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001201 }
Eric Andersen8a931792003-07-03 10:20:29 +00001202 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203 break;
1204 }
1205 }
1206 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001207#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001208
Eric Andersen8320b422003-04-02 10:13:26 +00001209 iface_list = defn->ifaces;
1210 while (iface_list) {
1211 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001212 if (strcmp(liface, currif->iface) == 0) {
1213 char *oldiface = currif->iface;
1214
1215 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001216 currif->iface = iface;
1217
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001218 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001220 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001221 cmds_ret = cmds(currif);
1222 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001223 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001224 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001225 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001226 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001227 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001228 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001229
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 currif->iface = oldiface;
1231 }
Eric Andersen8320b422003-04-02 10:13:26 +00001232 iface_list = iface_list->link;
1233 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001234 if (VERBOSE) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001235 bb_putchar('\n');
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001236 }
1237
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001238 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001239 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001240 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001241 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001242 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001243 FILE *state_fp;
1244 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001245 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001246 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001247
1248 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001249 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001250 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001251 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001252 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001253 free(iface_state->data);
1254 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001255 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001256 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001257 /* Remove an interface from state_list */
1258 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001259 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001260 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001261
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001262 /* Actually write the new state */
Denis Vlasenko2a86a612007-07-19 21:49:30 +00001263 state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
Denis Vlasenkof92df582007-05-02 22:22:23 +00001264 state = state_list;
1265 while (state) {
1266 if (state->data) {
1267 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001268 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001269 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001270 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001271 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001272 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001273 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001274 }
1275
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001276 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001277}