blob: adbc37e43e4daad06541ff63cf03ef13ad90f218 [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
Glenn L McGrathc6992fe2004-04-25 05:11:19 +00004 * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
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.
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000013 * To adhere to the FHS, the default state file is /var/run/ifstate.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000014 *
Rob Landley1b751c82005-10-28 09:24:33 +000015 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000016 */
17
Rob Landleyd921b2e2006-08-03 15:41:12 +000018#include "busybox.h"
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000019#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000020#include <fnmatch.h>
21#include <getopt.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000022
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000023#define MAX_OPT_DEPTH 10
24#define EUNBALBRACK 10001
25#define EUNDEFVAR 10002
26#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000027
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000028#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000029#define MAX_INTERFACE_LENGTH 10
30#endif
Eric Andersen8320b422003-04-02 10:13:26 +000031
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000032#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000033
34/* Forward declaration */
35struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000036
Denis Vlasenko097c3242006-11-27 16:59:15 +000037typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000038
Eric Andersen8320b422003-04-02 10:13:26 +000039struct method_t
40{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000041 char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000042 int (*up)(struct interface_defn_t *ifd, execfn *e);
43 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000044};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000045
Eric Andersen8320b422003-04-02 10:13:26 +000046struct address_family_t
47{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048 char *name;
49 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
Eric Andersen8320b422003-04-02 10:13:26 +000053struct mapping_defn_t
54{
55 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
Eric Andersen8320b422003-04-02 10:13:26 +000068struct variable_t
69{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070 char *name;
71 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000072};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000073
Eric Andersenc7bda1c2004-03-15 08:29:22 +000074struct interface_defn_t
Eric Andersen8320b422003-04-02 10:13:26 +000075{
Denis Vlasenko05341252006-09-26 20:35:30 +000076 const struct address_family_t *address_family;
77 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000078
Rob Landleye813ddb2006-02-28 03:53:14 +000079 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000080 int max_options;
81 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000082 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000083};
84
Eric Andersen8320b422003-04-02 10:13:26 +000085struct interfaces_file_t
86{
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000087 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000088 llist_t *ifaces;
89 struct mapping_defn_t *mappings;
90};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000091
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000092#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
93enum {
94 OPT_do_all = 0x1,
95 OPT_no_act = 0x2,
96 OPT_verbose = 0x4,
97 OPT_force = 0x8,
98 OPT_no_mappings = 0x10,
99};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000100#define DO_ALL (option_mask32 & OPT_do_all)
101#define NO_ACT (option_mask32 & OPT_no_act)
102#define VERBOSE (option_mask32 & OPT_verbose)
103#define FORCE (option_mask32 & OPT_force)
104#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000105
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000106static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000107
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000108static char *startup_PATH;
109
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000110#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
111
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000112#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000113
Denis Vlasenko097c3242006-11-27 16:59:15 +0000114static unsigned count_bits(unsigned a)
Eric Andersen8320b422003-04-02 10:13:26 +0000115{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000116 unsigned result;
Eric Andersen8320b422003-04-02 10:13:26 +0000117 result = (a & 0x55) + ((a >> 1) & 0x55);
118 result = (result & 0x33) + ((result >> 2) & 0x33);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000119 return (result & 0x0F) + ((result >> 4) & 0x0F);
Eric Andersen8320b422003-04-02 10:13:26 +0000120}
121
Eric Andersen66a3af92003-01-27 17:41:19 +0000122static int count_netmask_bits(char *dotted_quad)
123{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000124 unsigned result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000125 /* Found a netmask... Check if it is dotted quad */
126 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
127 return -1;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000128 // FIXME: will be confused by e.g. 255.0.255.0
Eric Andersen8320b422003-04-02 10:13:26 +0000129 result = count_bits(a);
130 result += count_bits(b);
131 result += count_bits(c);
132 result += count_bits(d);
Denis Vlasenko05341252006-09-26 20:35:30 +0000133 return (int)result;
Eric Andersen66a3af92003-01-27 17:41:19 +0000134}
135#endif
136
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000137static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000138{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000139 /* xasprintf trick will be smaller, but we are often
140 * called with str_length == 1 - don't want to have
141 * THAT much of malloc/freeing! */
142 char *buf = *bufp;
143 int len = (buf ? strlen(buf) : 0);
144 str_length++;
145 buf = xrealloc(buf, len + str_length);
146 /* copies at most str_length-1 chars! */
147 safe_strncpy(buf + len, str, str_length);
148 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000149}
150
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000151static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000152{
153 int i = strncmp(l, r, llen);
154
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000155 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000156 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000157 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000158}
159
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000160static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000161{
162 int i;
163
164 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000165 char *result;
166 static char label_buf[20];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000167 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
Eric Andersen8320b422003-04-02 10:13:26 +0000168 result = strchr(label_buf, ':');
169 if (result) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000170 *result = '\0';
Eric Andersen8320b422003-04-02 10:13:26 +0000171 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000172 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000173 }
174 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000175 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000176 }
177 for (i = 0; i < ifd->n_options; i++) {
178 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
179 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000180 }
181 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000182 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000183}
184
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000185static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000186{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000187 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
188 int okay[MAX_OPT_DEPTH] = { 1 };
189 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000190 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000191
192 while (*command) {
193 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000194 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000195 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000196 command++;
197 break;
198 case '\\':
199 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000200 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000201 command += 2;
202 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000203 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000204 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000205 }
206 break;
207 case '[':
208 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000209 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000210 okay[opt_depth] = 1;
211 opt_depth++;
212 command += 2;
213 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000214 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000215 command++;
216 }
217 break;
218 case ']':
219 if (command[1] == ']' && opt_depth > 1) {
220 opt_depth--;
221 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000222 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000223 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000224 command += 2;
225 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000226 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000227 command++;
228 }
229 break;
230 case '%':
231 {
232 char *nextpercent;
233 char *varvalue;
234
235 command++;
236 nextpercent = strchr(command, '%');
237 if (!nextpercent) {
238 errno = EUNBALPER;
239 free(result);
240 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000241 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000242
243 varvalue = get_var(command, nextpercent - command, ifd);
244
245 if (varvalue) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000246 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000247 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000248#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000249 /* Sigh... Add a special case for 'ip' to convert from
250 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000251 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000252 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000253 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000254 if (varvalue && (res = count_netmask_bits(varvalue)) > 0) {
255 const char *argument = utoa(res);
256 addstr(&result, argument, strlen(argument));
Denis Vlasenko05341252006-09-26 20:35:30 +0000257 command = nextpercent + 1;
258 break;
Eric Andersen8320b422003-04-02 10:13:26 +0000259 }
Eric Andersen8320b422003-04-02 10:13:26 +0000260 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000261#endif
262 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000263 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000264
265 command = nextpercent + 1;
266 }
267 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000268 }
269 }
270
271 if (opt_depth > 1) {
272 errno = EUNBALBRACK;
273 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000274 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000275 }
276
277 if (!okay[0]) {
278 errno = EUNDEFVAR;
279 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000280 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000281 }
282
Denis Vlasenko05341252006-09-26 20:35:30 +0000283 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000284}
285
Denis Vlasenko05341252006-09-26 20:35:30 +0000286/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000287static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000288{
289 char *out;
290 int ret;
291
292 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000293 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000294 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000295 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000296 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000297 /* out == "": parsed ok but not all needed variables known, skip */
298 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000299
300 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000301 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000302 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000303 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000304 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000305}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000306#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000307
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000308#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000309static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000310{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000311#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000312 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000313 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000314 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000315 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000316#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000317 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000318#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000319}
320
Eric Andersen8320b422003-04-02 10:13:26 +0000321static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000322{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000323#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000324 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000325#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000326 return execute("ifconfig %iface% del ::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 static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000331{
Eric Andersen8320b422003-04-02 10:13:26 +0000332 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000333#if ENABLE_FEATURE_IFUPDOWN_IP
334 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
335 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
336 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
337 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000338#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000339 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000340 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000341 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000342#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000343 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000344}
345
Eric Andersen8320b422003-04-02 10:13:26 +0000346static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000348#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000349 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000350#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000351 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000352#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000353}
354
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000355#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000356static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000357{
Eric Andersen8320b422003-04-02 10:13:26 +0000358 int result;
359 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000360 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000361 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000362 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000363 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000364 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000365}
366
Eric Andersen8320b422003-04-02 10:13:26 +0000367static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000368{
Denis Vlasenko05341252006-09-26 20:35:30 +0000369 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000371#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372
Denis Vlasenko05341252006-09-26 20:35:30 +0000373static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000374#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000375 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000376#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377 { "static", static_up6, static_down6, },
378 { "loopback", loopback_up6, loopback_down6, },
379};
380
Denis Vlasenko05341252006-09-26 20:35:30 +0000381static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000383 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384 methods6
385};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000386#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000387
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000388#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000389static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000390{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000391#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000392 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000393 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000394 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000395 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000396#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000397 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000398#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000399}
400
Eric Andersen8320b422003-04-02 10:13:26 +0000401static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000402{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000403#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000404 int result;
405 result = execute("ip addr flush dev %iface%", ifd, exec);
406 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000407 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000408#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000409 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000410#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000411}
412
Eric Andersen8320b422003-04-02 10:13:26 +0000413static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000414{
Eric Andersen8320b422003-04-02 10:13:26 +0000415 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000416#if ENABLE_FEATURE_IFUPDOWN_IP
417 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
418 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
419 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
420 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000421 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000422#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000423 /* ifconfig said to set iface up before it processes hw %hwaddress%,
424 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000425 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000426 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000427 result += execute("ifconfig %iface% %address% netmask %netmask%"
428 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000429 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000430 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000431 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000432#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000433}
434
Eric Andersen8320b422003-04-02 10:13:26 +0000435static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000436{
Eric Andersen8320b422003-04-02 10:13:26 +0000437 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000438#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000439 result = execute("ip addr flush dev %iface%", ifd, exec);
440 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000441#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000442 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000443 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000444#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000445 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000446}
447
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000448#if !ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000449struct dhcp_client_t
450{
451 const char *name;
452 const char *startcmd;
453 const char *stopcmd;
454};
455
456static const struct dhcp_client_t ext_dhcp_clients[] = {
457 { "udhcpc",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000458 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000459 "kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
460 },
461 { "pump",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000462 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000463 "pump -i %iface% -k",
464 },
465 { "dhclient",
466 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
467 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
468 },
469 { "dhcpcd",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000470 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000471 "dhcpcd -k %iface%",
472 },
473};
474#endif
475
Eric Andersen8320b422003-04-02 10:13:26 +0000476static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000477{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000478#if ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000479 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000480 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000481 ifd, exec);
482#else
483 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
484 for (i = 0; i < nclients; i++) {
485 if (exists_execable(ext_dhcp_clients[i].name))
486 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
487 }
488 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000489 return 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000490#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000491}
492
Eric Andersen8320b422003-04-02 10:13:26 +0000493static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000494{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000495#if ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000496 return execute("kill -TERM "
497 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
498#else
499 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
500 for (i = 0; i < nclients; i++) {
501 if (exists_execable(ext_dhcp_clients[i].name))
502 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
503 }
504 bb_error_msg("no dhcp clients found, using static interface shutdown");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000505 return static_down(ifd, exec);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000506#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000507}
508
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000509static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
510{
511 return 1;
512}
513
Eric Andersen8320b422003-04-02 10:13:26 +0000514static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000515{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000516 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
517 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
Denis Vlasenko05341252006-09-26 20:35:30 +0000518 "--returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000519}
520
Eric Andersen8320b422003-04-02 10:13:26 +0000521static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000522{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000523 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000524}
525
Eric Andersen8320b422003-04-02 10:13:26 +0000526static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000527{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000528 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529}
530
Eric Andersen8320b422003-04-02 10:13:26 +0000531static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000532{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000533 return execute("start-stop-daemon --start -x wvdial "
534 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000535}
536
Eric Andersen8320b422003-04-02 10:13:26 +0000537static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000538{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000539 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000540 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000541}
542
Denis Vlasenko05341252006-09-26 20:35:30 +0000543static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000544 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000545 { "wvdial", wvdial_up, wvdial_down, },
546 { "ppp", ppp_up, ppp_down, },
547 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000548 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000549 { "dhcp", dhcp_up, dhcp_down, },
550 { "loopback", loopback_up, loopback_down, },
551};
552
Denis Vlasenko05341252006-09-26 20:35:30 +0000553static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000554 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000555 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556 methods
557};
558
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000559#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000560
Glenn L McGrath85737042003-01-14 23:26:57 +0000561static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562{
Glenn L McGrath85737042003-01-14 23:26:57 +0000563 unsigned short length;
564 char *word;
565
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000566 if (!buf || !*buf || !**buf) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000567 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000568 }
569
Glenn L McGrath85737042003-01-14 23:26:57 +0000570 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000571 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000572
573 /* Skip over comments */
574 if (*word == '#') {
Denis Vlasenko05341252006-09-26 20:35:30 +0000575 return NULL;
Eric Andersen3c8bca32003-06-20 10:02:29 +0000576 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000577
Glenn L McGrath85737042003-01-14 23:26:57 +0000578 /* Find the length of this word */
579 length = strcspn(word, " \t\n");
580 if (length == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000581 return NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000582 }
583 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000584 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
585 if (**buf) {
586 **buf = '\0';
587 (*buf)++;
588 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000589
590 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000591}
592
Denis Vlasenko05341252006-09-26 20:35:30 +0000593static 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 +0000594{
595 int i;
596
Denis Vlasenko736230e2006-11-20 19:40:36 +0000597 if (!name)
598 return NULL;
599
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000600 for (i = 0; af[i]; i++) {
601 if (strcmp(af[i]->name, name) == 0) {
602 return af[i];
603 }
604 }
605 return NULL;
606}
607
Denis Vlasenko05341252006-09-26 20:35:30 +0000608static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000609{
610 int i;
611
Denis Vlasenko736230e2006-11-20 19:40:36 +0000612 if (!name)
613 return NULL;
614
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615 for (i = 0; i < af->n_methods; i++) {
616 if (strcmp(af->method[i].name, name) == 0) {
617 return &af->method[i];
618 }
619 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000620 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000621}
622
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000623static const llist_t *find_list_string(const llist_t *list, const char *string)
624{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000625 if (string == NULL)
626 return NULL;
627
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000628 while (list) {
629 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000630 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000631 }
632 list = list->link;
633 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000634 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000635}
636
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000637static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000638{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000639#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000640 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000641#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000642 struct interface_defn_t *currif = NULL;
643 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000644 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000645 char *firstword;
646 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000647
648 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
649
Rob Landleya6e131d2006-05-29 06:43:55 +0000650 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000651
Rob Landleyd921b2e2006-08-03 15:41:12 +0000652 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000653
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000654 while ((buf = xmalloc_getline(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000655 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000656
Glenn L McGrath85737042003-01-14 23:26:57 +0000657 firstword = next_word(&buf_ptr);
658 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000659 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660 continue; /* blank line */
661 }
662
663 if (strcmp(firstword, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000664#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000665 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000666
Glenn L McGrath85737042003-01-14 23:26:57 +0000667 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668 if (currmap->max_matches == currmap->n_matches) {
669 currmap->max_matches = currmap->max_matches * 2 + 1;
670 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
671 }
672
Rob Landleyd921b2e2006-08-03 15:41:12 +0000673 currmap->match[currmap->n_matches++] = xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000674 }
675 currmap->max_mappings = 0;
676 currmap->n_mappings = 0;
677 currmap->mapping = NULL;
678 currmap->script = NULL;
679 {
Eric Andersen8320b422003-04-02 10:13:26 +0000680 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000681 while (*where != NULL) {
682 where = &(*where)->next;
683 }
684 *where = currmap;
685 currmap->next = NULL;
686 }
Eric Andersen8320b422003-04-02 10:13:26 +0000687 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000688#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000689 currently_processing = MAPPING;
690 } else if (strcmp(firstword, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000691 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000692#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000693 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000694#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000695#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000696 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000697#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000698 NULL
699 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000700
Denis Vlasenko05341252006-09-26 20:35:30 +0000701 char *iface_name;
702 char *address_family_name;
703 char *method_name;
704 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000705
Denis Vlasenko05341252006-09-26 20:35:30 +0000706 currif = xzalloc(sizeof(struct interface_defn_t));
707 iface_name = next_word(&buf_ptr);
708 address_family_name = next_word(&buf_ptr);
709 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000710
Denis Vlasenko05341252006-09-26 20:35:30 +0000711 if (buf_ptr == NULL) {
712 bb_error_msg("too few parameters for line \"%s\"", buf);
713 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000714 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000715
716 /* ship any trailing whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000717 buf_ptr = skip_whitespace(buf_ptr);
Denis Vlasenko05341252006-09-26 20:35:30 +0000718
719 if (buf_ptr[0] != '\0') {
720 bb_error_msg("too many parameters \"%s\"", buf);
721 return NULL;
722 }
723
724 currif->iface = xstrdup(iface_name);
725
726 currif->address_family = get_address_family(addr_fams, address_family_name);
727 if (!currif->address_family) {
728 bb_error_msg("unknown address type \"%s\"", address_family_name);
729 return NULL;
730 }
731
732 currif->method = get_method(currif->address_family, method_name);
733 if (!currif->method) {
734 bb_error_msg("unknown method \"%s\"", method_name);
735 return NULL;
736 }
737
738 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
739 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
740 if ((strcmp(tmp->iface, currif->iface) == 0) &&
741 (tmp->address_family == currif->address_family)) {
742 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
743 return NULL;
744 }
745 }
746 llist_add_to_end(&(defn->ifaces), (char*)currif);
747
748 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000749 currently_processing = IFACE;
750 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000751 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000752
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000753 /* Check the interface isnt already listed */
754 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000755 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000756 }
757
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000758 /* Add the interface to the list */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000759 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000760 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000761 }
762 currently_processing = NONE;
763 } else {
764 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000765 case IFACE:
766 {
767 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000768
Denis Vlasenko05341252006-09-26 20:35:30 +0000769 if (strlen(buf_ptr) == 0) {
770 bb_error_msg("option with empty value \"%s\"", buf);
771 return NULL;
772 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000773
Denis Vlasenko05341252006-09-26 20:35:30 +0000774 if (strcmp(firstword, "up") != 0
775 && strcmp(firstword, "down") != 0
776 && strcmp(firstword, "pre-up") != 0
777 && strcmp(firstword, "post-down") != 0) {
778 for (i = 0; i < currif->n_options; i++) {
779 if (strcmp(currif->option[i].name, firstword) == 0) {
780 bb_error_msg("duplicate option \"%s\"", buf);
781 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000782 }
783 }
784 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000785 }
786 if (currif->n_options >= currif->max_options) {
787 struct variable_t *opt;
Eric Andersen8320b422003-04-02 10:13:26 +0000788
Denis Vlasenko05341252006-09-26 20:35:30 +0000789 currif->max_options = currif->max_options + 10;
790 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
791 currif->option = opt;
792 }
793 currif->option[currif->n_options].name = xstrdup(firstword);
794 currif->option[currif->n_options].value = xstrdup(buf_ptr);
795 if (!currif->option[currif->n_options].name) {
796 perror(filename);
797 return NULL;
798 }
799 if (!currif->option[currif->n_options].value) {
800 perror(filename);
801 return NULL;
802 }
803 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
804 currif->option[currif->n_options].value);
805 currif->n_options++;
806 break;
807 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000808#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko05341252006-09-26 20:35:30 +0000809 if (strcmp(firstword, "script") == 0) {
810 if (currmap->script != NULL) {
811 bb_error_msg("duplicate script in mapping \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000812 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000813 } else {
814 currmap->script = xstrdup(next_word(&buf_ptr));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000815 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000816 } else if (strcmp(firstword, "map") == 0) {
817 if (currmap->max_mappings == currmap->n_mappings) {
818 currmap->max_mappings = currmap->max_mappings * 2 + 1;
819 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
820 }
821 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
822 currmap->n_mappings++;
823 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000824 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000825 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000826 }
827#endif
828 break;
829 case NONE:
830 default:
831 bb_error_msg("misplaced option \"%s\"", buf);
832 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833 }
834 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000835 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000836 }
837 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000838 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000839 }
840 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000841
842 return defn;
843}
844
Rob Landleye813ddb2006-02-28 03:53:14 +0000845static char *setlocalenv(char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000846{
847 char *result;
848 char *here;
849 char *there;
850
Rob Landleyd921b2e2006-08-03 15:41:12 +0000851 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000852
853 for (here = there = result; *there != '=' && *there; there++) {
854 if (*there == '-')
855 *there = '_';
856 if (isalpha(*there))
857 *there = toupper(*there);
858
859 if (isalnum(*there) || *there == '_') {
860 *here = *there;
861 here++;
862 }
863 }
Rob Landleya3896512006-05-07 20:20:34 +0000864 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000865
866 return result;
867}
868
Rob Landleye813ddb2006-02-28 03:53:14 +0000869static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000870{
871 char **environend;
872 int i;
873 const int n_env_entries = iface->n_options + 5;
874 char **ppch;
875
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000876 if (my_environ != NULL) {
877 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000878 free(*ppch);
879 *ppch = NULL;
880 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000881 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000882 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000883 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
884 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885
886 for (i = 0; i < iface->n_options; i++) {
887 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000888 || strcmp(iface->option[i].name, "down") == 0
889 || strcmp(iface->option[i].name, "pre-up") == 0
890 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000891 continue;
892 }
893 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000894 }
895
896 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000897 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000898 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000899 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000900 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000901}
902
903static int doit(char *str)
904{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000905 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000906 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000907 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000908 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000909 pid_t child;
910 int status;
911
912 fflush(NULL);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000913 child = fork();
914 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000915 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000916 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000917 case 0: /* child */
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000918 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
Denis Vlasenko05341252006-09-26 20:35:30 +0000919 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000920 }
921 waitpid(child, &status, 0);
922 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
923 return 0;
924 }
925 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000926 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927}
928
Rob Landleye813ddb2006-02-28 03:53:14 +0000929static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930{
931 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000932 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000933 for (i = 0; i < ifd->n_options; i++) {
934 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000935 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000936 return 0;
937 }
938 }
939 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000940
Rob Landleyd921b2e2006-08-03 15:41:12 +0000941 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000942 /* heh, we don't bother free'ing it */
943 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000944}
945
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000946static int check(char *str)
947{
Eric Andersen8320b422003-04-02 10:13:26 +0000948 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000949}
950
Eric Andersen8320b422003-04-02 10:13:26 +0000951static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000952{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000953 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000954 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000955 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000956 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000957 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000958 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000959}
960
961static int iface_down(struct interface_defn_t *iface)
962{
Eric Andersen8320b422003-04-02 10:13:26 +0000963 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000964 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000965 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000966 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000967 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000968 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000969}
970
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000971#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000972static int popen2(FILE **in, FILE **out, char *command, ...)
973{
974 va_list ap;
975 char *argv[11] = { command };
976 int argc;
977 int infd[2], outfd[2];
978 pid_t pid;
979
980 argc = 1;
981 va_start(ap, command);
982 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
983 argc++;
984 }
985 argv[argc] = NULL; /* make sure */
986 va_end(ap);
987
988 if (pipe(infd) != 0) {
989 return 0;
990 }
991
992 if (pipe(outfd) != 0) {
993 close(infd[0]);
994 close(infd[1]);
995 return 0;
996 }
997
998 fflush(NULL);
999 switch (pid = fork()) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001000 case -1: /* failure */
1001 close(infd[0]);
1002 close(infd[1]);
1003 close(outfd[0]);
1004 close(outfd[1]);
1005 return 0;
1006 case 0: /* child */
1007 dup2(infd[0], 0);
1008 dup2(outfd[1], 1);
1009 close(infd[0]);
1010 close(infd[1]);
1011 close(outfd[0]);
1012 close(outfd[1]);
1013 execvp(command, argv);
1014 exit(127);
1015 default: /* parent */
1016 *in = fdopen(infd[1], "w");
1017 *out = fdopen(outfd[0], "r");
1018 close(infd[0]);
1019 close(outfd[1]);
1020 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001021 }
1022 /* unreached */
1023}
1024
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001025static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026{
1027 FILE *in, *out;
1028 int i, status;
1029 pid_t pid;
1030
Rob Landleyd921b2e2006-08-03 15:41:12 +00001031 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001032
Eric Andersen8a931792003-07-03 10:20:29 +00001033 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001034 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001035
1036 /* popen2() returns 0 on failure. */
1037 if (pid == 0)
1038 return logical;
1039
1040 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041 for (i = 0; i < map->n_mappings; i++) {
1042 fprintf(in, "%s\n", map->mapping[i]);
1043 }
1044 fclose(in);
1045 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001046
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001047 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001048 /* If the mapping script exited successfully, try to
1049 * grab a line of output and use that as the name of the
1050 * logical interface. */
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001051 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001052
Eric Andersen233b1702003-06-05 19:37:01 +00001053 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001054 /* If we are able to read a line of output from the script,
1055 * remove any trailing whitespace and use this value
1056 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001057 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001058
1059 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001060 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001061
1062 free(logical);
1063 logical = new_logical;
1064 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001065 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001066 * freshly allocated memory. */
1067 free(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{
Rob Landleya3896512006-05-07 20:20:34 +00001079 unsigned short 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) {
1083 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001084 (search->data[iface_len] == '=')) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001085 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001086 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001087 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001088 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001089 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001090}
1091
Rob Landleydfba7412006-03-06 20:47:33 +00001092int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001093{
Denis Vlasenko05341252006-09-26 20:35:30 +00001094 int (*cmds)(struct interface_defn_t *) = NULL;
Eric Andersen8320b422003-04-02 10:13:26 +00001095 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001096 llist_t *state_list = NULL;
1097 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001098 const char *interfaces = "/etc/network/interfaces";
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001099 int any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001100
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001101 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001102 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001103 /* ifup command */
1104 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001105 }
1106
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001107 getopt32(argc, argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001108 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001109 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001110 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001111 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001112 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001113
Eric Andersen8320b422003-04-02 10:13:26 +00001114 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001115 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001116 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001117
Eric Andersen3c8bca32003-06-20 10:02:29 +00001118 if (!defn) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001119 return EXIT_FAILURE;
Eric Andersen3c8bca32003-06-20 10:02:29 +00001120 }
1121
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001122 startup_PATH = getenv("PATH");
1123 if (!startup_PATH) startup_PATH = "";
1124
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001125 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001126 if (DO_ALL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001128 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001129 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001130 /* iface_down */
1131 const llist_t *list = state_list;
1132 while (list) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001133 llist_add_to_end(&target_list, xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001134 list = list->link;
1135 }
1136 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001137 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001138 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001139 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001140 }
1141
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001142 /* Update the interfaces */
1143 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001144 llist_t *iface_list;
1145 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001146 char *iface;
1147 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001148 char *pch;
1149 int okay = 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001150 int cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001151
Rob Landleyd921b2e2006-08-03 15:41:12 +00001152 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001153 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001154
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001155 pch = strchr(iface, '=');
1156 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001157 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001158 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001159 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001160 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001161 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001162
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001163 if (!FORCE) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001164 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001165
1166 if (cmds == iface_up) {
1167 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001168 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001169 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 continue;
1171 }
1172 } else {
1173 /* ifdown */
Eric Andersen66a3af92003-01-27 17:41:19 +00001174 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001175 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001176 continue;
1177 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001178 }
1179 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001180
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001181#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001182 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001183 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001184
1185 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001186 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 for (i = 0; i < currmap->n_matches; i++) {
1188 if (fnmatch(currmap->match[i], liface, 0) != 0)
1189 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001190 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001191 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001192 }
Eric Andersen8a931792003-07-03 10:20:29 +00001193 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001194 break;
1195 }
1196 }
1197 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001198#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001199
Eric Andersen8320b422003-04-02 10:13:26 +00001200 iface_list = defn->ifaces;
1201 while (iface_list) {
1202 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203 if (strcmp(liface, currif->iface) == 0) {
1204 char *oldiface = currif->iface;
1205
1206 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001207 currif->iface = iface;
1208
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001209 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001211 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001212 cmds_ret = cmds(currif);
1213 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001214 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001215 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001216 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001217 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001218 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001219 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001220
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221 currif->iface = oldiface;
1222 }
Eric Andersen8320b422003-04-02 10:13:26 +00001223 iface_list = iface_list->link;
1224 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001225 if (VERBOSE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001226 puts("");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001227 }
1228
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001229 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001230 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001231 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001233 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001234
1235 if (cmds == iface_up) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001236 char *newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001237 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001238 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001239 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001240 free(iface_state->data);
1241 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001242 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001243 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001244 /* Remove an interface from the linked list */
Rob Landleya6e131d2006-05-29 06:43:55 +00001245 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001246 }
1247 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001248 }
1249
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001250 /* Actually write the new state */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001251 if (!NO_ACT) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001252 FILE *state_fp;
Eric Andersen66a3af92003-01-27 17:41:19 +00001253
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001254 state_fp = xfopen("/var/run/ifstate", "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001255 while (state_list) {
1256 if (state_list->data) {
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001257 fprintf(state_fp, "%s\n", state_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001258 }
1259 state_list = state_list->link;
1260 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001261 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262 }
1263
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001264 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001265}