blob: 887c2eea5adef110f0c1bf95797e031e5c2c51cb [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
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000039struct method_t {
40 const char *name;
Rob Landleye813ddb2006-02-28 03:53:14 +000041 int (*up)(struct interface_defn_t *ifd, execfn *e);
42 int (*down)(struct interface_defn_t *ifd, execfn *e);
Eric Andersen8320b422003-04-02 10:13:26 +000043};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000044
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000045struct address_family_t {
46 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000047 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000048 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000049};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000050
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000051struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000052 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000053
54 int max_matches;
55 int n_matches;
56 char **match;
57
58 char *script;
59
60 int max_mappings;
61 int n_mappings;
62 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000063};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000064
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000065struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000066 char *name;
67 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000068};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000069
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000070struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000071 const struct address_family_t *address_family;
72 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000073
Rob Landleye813ddb2006-02-28 03:53:14 +000074 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000075 int max_options;
76 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000077 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000078};
79
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000080struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000081 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000082 llist_t *ifaces;
83 struct mapping_defn_t *mappings;
84};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000085
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000086#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
87enum {
88 OPT_do_all = 0x1,
89 OPT_no_act = 0x2,
90 OPT_verbose = 0x4,
91 OPT_force = 0x8,
92 OPT_no_mappings = 0x10,
93};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000094#define DO_ALL (option_mask32 & OPT_do_all)
95#define NO_ACT (option_mask32 & OPT_no_act)
96#define VERBOSE (option_mask32 & OPT_verbose)
97#define FORCE (option_mask32 & OPT_force)
98#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000099
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000100static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000101
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000102static const char *startup_PATH;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000103
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000104#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
105
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000106#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000107
Denis Vlasenko097c3242006-11-27 16:59:15 +0000108static unsigned count_bits(unsigned a)
Eric Andersen8320b422003-04-02 10:13:26 +0000109{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000110 unsigned result;
Eric Andersen8320b422003-04-02 10:13:26 +0000111 result = (a & 0x55) + ((a >> 1) & 0x55);
112 result = (result & 0x33) + ((result >> 2) & 0x33);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000113 return (result & 0x0F) + ((result >> 4) & 0x0F);
Eric Andersen8320b422003-04-02 10:13:26 +0000114}
115
Eric Andersen66a3af92003-01-27 17:41:19 +0000116static int count_netmask_bits(char *dotted_quad)
117{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000118 unsigned result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000119 /* Found a netmask... Check if it is dotted quad */
120 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
121 return -1;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000122 // FIXME: will be confused by e.g. 255.0.255.0
Eric Andersen8320b422003-04-02 10:13:26 +0000123 result = count_bits(a);
124 result += count_bits(b);
125 result += count_bits(c);
126 result += count_bits(d);
Denis Vlasenko05341252006-09-26 20:35:30 +0000127 return (int)result;
Eric Andersen66a3af92003-01-27 17:41:19 +0000128}
129#endif
130
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000131static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000132{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000133 /* xasprintf trick will be smaller, but we are often
134 * called with str_length == 1 - don't want to have
135 * THAT much of malloc/freeing! */
136 char *buf = *bufp;
137 int len = (buf ? strlen(buf) : 0);
138 str_length++;
139 buf = xrealloc(buf, len + str_length);
140 /* copies at most str_length-1 chars! */
141 safe_strncpy(buf + len, str, str_length);
142 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000143}
144
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000145static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000146{
147 int i = strncmp(l, r, llen);
148
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000149 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000150 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000151 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000152}
153
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000154static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000155{
156 int i;
157
158 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000159 char *result;
160 static char label_buf[20];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000161 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
Eric Andersen8320b422003-04-02 10:13:26 +0000162 result = strchr(label_buf, ':');
163 if (result) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000164 *result = '\0';
Eric Andersen8320b422003-04-02 10:13:26 +0000165 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000166 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000167 }
168 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000169 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000170 }
171 for (i = 0; i < ifd->n_options; i++) {
172 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
173 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000174 }
175 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000176 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000177}
178
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000179static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000180{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000181 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
182 int okay[MAX_OPT_DEPTH] = { 1 };
183 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000184 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000185
186 while (*command) {
187 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000188 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000189 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000190 command++;
191 break;
192 case '\\':
193 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000194 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000195 command += 2;
196 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000197 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000198 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000199 }
200 break;
201 case '[':
202 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000203 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000204 okay[opt_depth] = 1;
205 opt_depth++;
206 command += 2;
207 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000208 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000209 command++;
210 }
211 break;
212 case ']':
213 if (command[1] == ']' && opt_depth > 1) {
214 opt_depth--;
215 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000216 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000217 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000218 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 {
226 char *nextpercent;
227 char *varvalue;
228
229 command++;
230 nextpercent = strchr(command, '%');
231 if (!nextpercent) {
232 errno = EUNBALPER;
233 free(result);
234 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000235 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000236
237 varvalue = get_var(command, nextpercent - command, ifd);
238
239 if (varvalue) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000240 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000241 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000242#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000243 /* Sigh... Add a special case for 'ip' to convert from
244 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000245 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000246 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000247 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000248 if (varvalue && (res = count_netmask_bits(varvalue)) > 0) {
249 const char *argument = utoa(res);
250 addstr(&result, argument, strlen(argument));
Denis Vlasenko05341252006-09-26 20:35:30 +0000251 command = nextpercent + 1;
252 break;
Eric Andersen8320b422003-04-02 10:13:26 +0000253 }
Eric Andersen8320b422003-04-02 10:13:26 +0000254 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000255#endif
256 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000257 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000258
259 command = nextpercent + 1;
260 }
261 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000262 }
263 }
264
265 if (opt_depth > 1) {
266 errno = EUNBALBRACK;
267 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000268 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000269 }
270
271 if (!okay[0]) {
272 errno = EUNDEFVAR;
273 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000274 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000275 }
276
Denis Vlasenko05341252006-09-26 20:35:30 +0000277 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000278}
279
Denis Vlasenko05341252006-09-26 20:35:30 +0000280/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000281static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000282{
283 char *out;
284 int ret;
285
286 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000287 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000288 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000289 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000290 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000291 /* out == "": parsed ok but not all needed variables known, skip */
292 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000293
294 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000295 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000296 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000297 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000298 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000299}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000300#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000301
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000302#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000303static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000304{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000305#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000306 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000307 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000308 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000309 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000310#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000311 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000312#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000313}
314
Eric Andersen8320b422003-04-02 10:13:26 +0000315static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000316{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000317#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000318 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000319#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000320 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000321#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000322}
323
Eric Andersen8320b422003-04-02 10:13:26 +0000324static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000325{
Eric Andersen8320b422003-04-02 10:13:26 +0000326 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000327#if ENABLE_FEATURE_IFUPDOWN_IP
328 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
329 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
330 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
331 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000332#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000333 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000334 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000335 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000336#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000337 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000338}
339
Eric Andersen8320b422003-04-02 10:13:26 +0000340static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000341{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000342#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000343 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000344#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000345 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000346#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000347}
348
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000349#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000350static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000351{
Eric Andersen8320b422003-04-02 10:13:26 +0000352 int result;
353 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000354 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000355 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000356 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000357 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000358 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000359}
360
Eric Andersen8320b422003-04-02 10:13:26 +0000361static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000362{
Denis Vlasenko05341252006-09-26 20:35:30 +0000363 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000364}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000365#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000366
Denis Vlasenko05341252006-09-26 20:35:30 +0000367static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000368#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000369 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000370#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000371 { "static", static_up6, static_down6, },
372 { "loopback", loopback_up6, loopback_down6, },
373};
374
Denis Vlasenko05341252006-09-26 20:35:30 +0000375static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000376 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000377 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000378 methods6
379};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000380#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000381
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000382#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000383static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000384{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000385#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000386 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000387 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000388 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000389 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000390#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000391 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000392#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000393}
394
Eric Andersen8320b422003-04-02 10:13:26 +0000395static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000396{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000397#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000398 int result;
399 result = execute("ip addr flush dev %iface%", ifd, exec);
400 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000401 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000402#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000403 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000404#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000405}
406
Eric Andersen8320b422003-04-02 10:13:26 +0000407static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000408{
Eric Andersen8320b422003-04-02 10:13:26 +0000409 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000410#if ENABLE_FEATURE_IFUPDOWN_IP
411 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
412 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
413 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
414 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000415 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000416#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000417 /* ifconfig said to set iface up before it processes hw %hwaddress%,
418 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000419 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000420 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000421 result += execute("ifconfig %iface% %address% netmask %netmask%"
422 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000423 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000424 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000425 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000426#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000427}
428
Eric Andersen8320b422003-04-02 10:13:26 +0000429static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000430{
Eric Andersen8320b422003-04-02 10:13:26 +0000431 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000432#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000433 result = execute("ip addr flush dev %iface%", ifd, exec);
434 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000435#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000436 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000437 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000438#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000439 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000440}
441
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000442#if !ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000443struct dhcp_client_t
444{
445 const char *name;
446 const char *startcmd;
447 const char *stopcmd;
448};
449
450static const struct dhcp_client_t ext_dhcp_clients[] = {
451 { "udhcpc",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000452 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000453 "kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
454 },
455 { "pump",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000456 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000457 "pump -i %iface% -k",
458 },
459 { "dhclient",
460 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
461 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
462 },
463 { "dhcpcd",
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000464 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000465 "dhcpcd -k %iface%",
466 },
467};
468#endif
469
Eric Andersen8320b422003-04-02 10:13:26 +0000470static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000471{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000472#if ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000473 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000474 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000475 ifd, exec);
476#else
477 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
478 for (i = 0; i < nclients; i++) {
479 if (exists_execable(ext_dhcp_clients[i].name))
480 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
481 }
482 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000483 return 0;
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000484#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000485}
486
Eric Andersen8320b422003-04-02 10:13:26 +0000487static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000488{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000489#if ENABLE_APP_UDHCPC
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000490 return execute("kill -TERM "
491 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
492#else
493 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
494 for (i = 0; i < nclients; i++) {
495 if (exists_execable(ext_dhcp_clients[i].name))
496 return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
497 }
498 bb_error_msg("no dhcp clients found, using static interface shutdown");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000499 return static_down(ifd, exec);
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000500#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000501}
502
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000503static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
504{
505 return 1;
506}
507
Eric Andersen8320b422003-04-02 10:13:26 +0000508static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000509{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000510 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
511 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
Denis Vlasenko05341252006-09-26 20:35:30 +0000512 "--returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000513}
514
Eric Andersen8320b422003-04-02 10:13:26 +0000515static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000516{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000517 return execute("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000518}
519
Eric Andersen8320b422003-04-02 10:13:26 +0000520static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000521{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000522 return execute("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000523}
524
Eric Andersen8320b422003-04-02 10:13:26 +0000525static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000526{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000527 return execute("start-stop-daemon --start -x wvdial "
528 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000529}
530
Eric Andersen8320b422003-04-02 10:13:26 +0000531static int wvdial_down(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 --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000534 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000535}
536
Denis Vlasenko05341252006-09-26 20:35:30 +0000537static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000538 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000539 { "wvdial", wvdial_up, wvdial_down, },
540 { "ppp", ppp_up, ppp_down, },
541 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000542 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000543 { "dhcp", dhcp_up, dhcp_down, },
544 { "loopback", loopback_up, loopback_down, },
545};
546
Denis Vlasenko05341252006-09-26 20:35:30 +0000547static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548 "inet",
Eric Andersen8320b422003-04-02 10:13:26 +0000549 sizeof(methods) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000550 methods
551};
552
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000553#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000554
Glenn L McGrath85737042003-01-14 23:26:57 +0000555static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000556{
Glenn L McGrath85737042003-01-14 23:26:57 +0000557 unsigned short length;
558 char *word;
559
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000560 if (!buf || !*buf || !**buf) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000561 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000562 }
563
Glenn L McGrath85737042003-01-14 23:26:57 +0000564 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000565 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000566
567 /* Skip over comments */
568 if (*word == '#') {
Denis Vlasenko05341252006-09-26 20:35:30 +0000569 return NULL;
Eric Andersen3c8bca32003-06-20 10:02:29 +0000570 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000571
Glenn L McGrath85737042003-01-14 23:26:57 +0000572 /* Find the length of this word */
573 length = strcspn(word, " \t\n");
574 if (length == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000575 return NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000576 }
577 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000578 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
579 if (**buf) {
580 **buf = '\0';
581 (*buf)++;
582 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000583
584 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000585}
586
Denis Vlasenko05341252006-09-26 20:35:30 +0000587static 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 +0000588{
589 int i;
590
Denis Vlasenko736230e2006-11-20 19:40:36 +0000591 if (!name)
592 return NULL;
593
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000594 for (i = 0; af[i]; i++) {
595 if (strcmp(af[i]->name, name) == 0) {
596 return af[i];
597 }
598 }
599 return NULL;
600}
601
Denis Vlasenko05341252006-09-26 20:35:30 +0000602static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000603{
604 int i;
605
Denis Vlasenko736230e2006-11-20 19:40:36 +0000606 if (!name)
607 return NULL;
608
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000609 for (i = 0; i < af->n_methods; i++) {
610 if (strcmp(af->method[i].name, name) == 0) {
611 return &af->method[i];
612 }
613 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000614 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000615}
616
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000617static const llist_t *find_list_string(const llist_t *list, const char *string)
618{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000619 if (string == NULL)
620 return NULL;
621
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000622 while (list) {
623 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000624 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000625 }
626 list = list->link;
627 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000628 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000629}
630
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000631static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000632{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000633#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000634 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000635#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000636 struct interface_defn_t *currif = NULL;
637 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000638 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000639 char *firstword;
640 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000641
642 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
643
Rob Landleya6e131d2006-05-29 06:43:55 +0000644 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000645
Rob Landleyd921b2e2006-08-03 15:41:12 +0000646 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000647
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000648 while ((buf = xmalloc_getline(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000649 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000650
Glenn L McGrath85737042003-01-14 23:26:57 +0000651 firstword = next_word(&buf_ptr);
652 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000653 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000654 continue; /* blank line */
655 }
656
657 if (strcmp(firstword, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000658#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000659 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000660
Glenn L McGrath85737042003-01-14 23:26:57 +0000661 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000662 if (currmap->max_matches == currmap->n_matches) {
663 currmap->max_matches = currmap->max_matches * 2 + 1;
664 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
665 }
666
Rob Landleyd921b2e2006-08-03 15:41:12 +0000667 currmap->match[currmap->n_matches++] = xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000668 }
669 currmap->max_mappings = 0;
670 currmap->n_mappings = 0;
671 currmap->mapping = NULL;
672 currmap->script = NULL;
673 {
Eric Andersen8320b422003-04-02 10:13:26 +0000674 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000675 while (*where != NULL) {
676 where = &(*where)->next;
677 }
678 *where = currmap;
679 currmap->next = NULL;
680 }
Eric Andersen8320b422003-04-02 10:13:26 +0000681 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000682#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000683 currently_processing = MAPPING;
684 } else if (strcmp(firstword, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000685 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000686#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000687 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000689#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000690 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000691#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000692 NULL
693 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000694
Denis Vlasenko05341252006-09-26 20:35:30 +0000695 char *iface_name;
696 char *address_family_name;
697 char *method_name;
698 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000699
Denis Vlasenko05341252006-09-26 20:35:30 +0000700 currif = xzalloc(sizeof(struct interface_defn_t));
701 iface_name = next_word(&buf_ptr);
702 address_family_name = next_word(&buf_ptr);
703 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000704
Denis Vlasenko05341252006-09-26 20:35:30 +0000705 if (buf_ptr == NULL) {
706 bb_error_msg("too few parameters for line \"%s\"", buf);
707 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000708 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000709
710 /* ship any trailing whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000711 buf_ptr = skip_whitespace(buf_ptr);
Denis Vlasenko05341252006-09-26 20:35:30 +0000712
713 if (buf_ptr[0] != '\0') {
714 bb_error_msg("too many parameters \"%s\"", buf);
715 return NULL;
716 }
717
718 currif->iface = xstrdup(iface_name);
719
720 currif->address_family = get_address_family(addr_fams, address_family_name);
721 if (!currif->address_family) {
722 bb_error_msg("unknown address type \"%s\"", address_family_name);
723 return NULL;
724 }
725
726 currif->method = get_method(currif->address_family, method_name);
727 if (!currif->method) {
728 bb_error_msg("unknown method \"%s\"", method_name);
729 return NULL;
730 }
731
732 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
733 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
734 if ((strcmp(tmp->iface, currif->iface) == 0) &&
735 (tmp->address_family == currif->address_family)) {
736 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
737 return NULL;
738 }
739 }
740 llist_add_to_end(&(defn->ifaces), (char*)currif);
741
742 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000743 currently_processing = IFACE;
744 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000745 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000746
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000747 /* Check the interface isnt already listed */
748 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000749 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000750 }
751
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000752 /* Add the interface to the list */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000753 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000754 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000755 }
756 currently_processing = NONE;
757 } else {
758 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000759 case IFACE:
760 {
761 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000762
Denis Vlasenko05341252006-09-26 20:35:30 +0000763 if (strlen(buf_ptr) == 0) {
764 bb_error_msg("option with empty value \"%s\"", buf);
765 return NULL;
766 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000767
Denis Vlasenko05341252006-09-26 20:35:30 +0000768 if (strcmp(firstword, "up") != 0
769 && strcmp(firstword, "down") != 0
770 && strcmp(firstword, "pre-up") != 0
771 && strcmp(firstword, "post-down") != 0) {
772 for (i = 0; i < currif->n_options; i++) {
773 if (strcmp(currif->option[i].name, firstword) == 0) {
774 bb_error_msg("duplicate option \"%s\"", buf);
775 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000776 }
777 }
778 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000779 }
780 if (currif->n_options >= currif->max_options) {
781 struct variable_t *opt;
Eric Andersen8320b422003-04-02 10:13:26 +0000782
Denis Vlasenko05341252006-09-26 20:35:30 +0000783 currif->max_options = currif->max_options + 10;
784 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
785 currif->option = opt;
786 }
787 currif->option[currif->n_options].name = xstrdup(firstword);
788 currif->option[currif->n_options].value = xstrdup(buf_ptr);
789 if (!currif->option[currif->n_options].name) {
790 perror(filename);
791 return NULL;
792 }
793 if (!currif->option[currif->n_options].value) {
794 perror(filename);
795 return NULL;
796 }
797 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
798 currif->option[currif->n_options].value);
799 currif->n_options++;
800 break;
801 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000802#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko05341252006-09-26 20:35:30 +0000803 if (strcmp(firstword, "script") == 0) {
804 if (currmap->script != NULL) {
805 bb_error_msg("duplicate script in mapping \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000806 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000807 } else {
808 currmap->script = xstrdup(next_word(&buf_ptr));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000809 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000810 } else if (strcmp(firstword, "map") == 0) {
811 if (currmap->max_mappings == currmap->n_mappings) {
812 currmap->max_mappings = currmap->max_mappings * 2 + 1;
813 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
814 }
815 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
816 currmap->n_mappings++;
817 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000818 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000819 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000820 }
821#endif
822 break;
823 case NONE:
824 default:
825 bb_error_msg("misplaced option \"%s\"", buf);
826 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000827 }
828 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000829 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000830 }
831 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000832 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000833 }
834 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835
836 return defn;
837}
838
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000839static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840{
841 char *result;
842 char *here;
843 char *there;
844
Rob Landleyd921b2e2006-08-03 15:41:12 +0000845 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000846
847 for (here = there = result; *there != '=' && *there; there++) {
848 if (*there == '-')
849 *there = '_';
850 if (isalpha(*there))
851 *there = toupper(*there);
852
853 if (isalnum(*there) || *there == '_') {
854 *here = *there;
855 here++;
856 }
857 }
Rob Landleya3896512006-05-07 20:20:34 +0000858 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000859
860 return result;
861}
862
Rob Landleye813ddb2006-02-28 03:53:14 +0000863static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864{
865 char **environend;
866 int i;
867 const int n_env_entries = iface->n_options + 5;
868 char **ppch;
869
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000870 if (my_environ != NULL) {
871 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000872 free(*ppch);
873 *ppch = NULL;
874 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000875 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000876 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000877 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
878 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000879
880 for (i = 0; i < iface->n_options; i++) {
881 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000882 || strcmp(iface->option[i].name, "down") == 0
883 || strcmp(iface->option[i].name, "pre-up") == 0
884 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000885 continue;
886 }
887 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000888 }
889
890 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000891 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000892 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000894 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000895}
896
897static int doit(char *str)
898{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000899 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000900 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000901 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000902 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000903 pid_t child;
904 int status;
905
906 fflush(NULL);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000907 child = fork();
908 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000909 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000910 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000911 case 0: /* child */
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000912 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
Denis Vlasenko05341252006-09-26 20:35:30 +0000913 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000914 }
915 waitpid(child, &status, 0);
916 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
917 return 0;
918 }
919 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000920 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000921}
922
Rob Landleye813ddb2006-02-28 03:53:14 +0000923static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000924{
925 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000926 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000927 for (i = 0; i < ifd->n_options; i++) {
928 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000929 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000930 return 0;
931 }
932 }
933 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000934
Rob Landleyd921b2e2006-08-03 15:41:12 +0000935 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000936 /* heh, we don't bother free'ing it */
937 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000938}
939
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000940static int check(char *str)
941{
Eric Andersen8320b422003-04-02 10:13:26 +0000942 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943}
944
Eric Andersen8320b422003-04-02 10:13:26 +0000945static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000946{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000947 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000948 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000949 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000950 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000951 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000952 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000953}
954
955static int iface_down(struct interface_defn_t *iface)
956{
Eric Andersen8320b422003-04-02 10:13:26 +0000957 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000958 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000959 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000960 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000961 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000962 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000963}
964
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000965#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000966static int popen2(FILE **in, FILE **out, char *command, ...)
967{
968 va_list ap;
969 char *argv[11] = { command };
970 int argc;
971 int infd[2], outfd[2];
972 pid_t pid;
973
974 argc = 1;
975 va_start(ap, command);
976 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
977 argc++;
978 }
979 argv[argc] = NULL; /* make sure */
980 va_end(ap);
981
982 if (pipe(infd) != 0) {
983 return 0;
984 }
985
986 if (pipe(outfd) != 0) {
987 close(infd[0]);
988 close(infd[1]);
989 return 0;
990 }
991
992 fflush(NULL);
993 switch (pid = fork()) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000994 case -1: /* failure */
995 close(infd[0]);
996 close(infd[1]);
997 close(outfd[0]);
998 close(outfd[1]);
999 return 0;
1000 case 0: /* child */
1001 dup2(infd[0], 0);
1002 dup2(outfd[1], 1);
1003 close(infd[0]);
1004 close(infd[1]);
1005 close(outfd[0]);
1006 close(outfd[1]);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001007 BB_EXECVP(command, argv);
Denis Vlasenko05341252006-09-26 20:35:30 +00001008 exit(127);
1009 default: /* parent */
1010 *in = fdopen(infd[1], "w");
1011 *out = fdopen(outfd[0], "r");
1012 close(infd[0]);
1013 close(outfd[1]);
1014 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001015 }
1016 /* unreached */
1017}
1018
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001019static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020{
1021 FILE *in, *out;
1022 int i, status;
1023 pid_t pid;
1024
Rob Landleyd921b2e2006-08-03 15:41:12 +00001025 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001026
Eric Andersen8a931792003-07-03 10:20:29 +00001027 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001028 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001029
1030 /* popen2() returns 0 on failure. */
1031 if (pid == 0)
1032 return logical;
1033
1034 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001035 for (i = 0; i < map->n_mappings; i++) {
1036 fprintf(in, "%s\n", map->mapping[i]);
1037 }
1038 fclose(in);
1039 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001040
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001041 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001042 /* If the mapping script exited successfully, try to
1043 * grab a line of output and use that as the name of the
1044 * logical interface. */
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001045 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001046
Eric Andersen233b1702003-06-05 19:37:01 +00001047 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001048 /* If we are able to read a line of output from the script,
1049 * remove any trailing whitespace and use this value
1050 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001051 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001052
1053 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001054 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001055
1056 free(logical);
1057 logical = new_logical;
1058 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001059 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001060 * freshly allocated memory. */
1061 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001062 }
1063 }
Eric Andersen8a931792003-07-03 10:20:29 +00001064
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001065 fclose(out);
1066
Eric Andersen8a931792003-07-03 10:20:29 +00001067 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001068}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001069#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001071static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001072{
Rob Landleya3896512006-05-07 20:20:34 +00001073 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001074 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001076 while (search) {
1077 if ((strncmp(search->data, iface, iface_len) == 0) &&
Eric Andersen8320b422003-04-02 10:13:26 +00001078 (search->data[iface_len] == '=')) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001079 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001080 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001081 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001082 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001083 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001084}
1085
Denis Vlasenko06af2162007-02-03 17:28:39 +00001086int ifupdown_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +00001087int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001088{
Denis Vlasenko05341252006-09-26 20:35:30 +00001089 int (*cmds)(struct interface_defn_t *) = NULL;
Eric Andersen8320b422003-04-02 10:13:26 +00001090 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001091 llist_t *state_list = NULL;
1092 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001093 const char *interfaces = "/etc/network/interfaces";
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001094 FILE *state_fp;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001095 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001096
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001097 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001098 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001099 /* ifup command */
1100 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001101 }
1102
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001103 getopt32(argc, argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001104 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001105 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001106 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001107 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001108 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001109
Eric Andersen8320b422003-04-02 10:13:26 +00001110 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001111 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001112 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001113
Eric Andersen3c8bca32003-06-20 10:02:29 +00001114 if (!defn) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001115 return EXIT_FAILURE;
Eric Andersen3c8bca32003-06-20 10:02:29 +00001116 }
1117
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001118 startup_PATH = getenv("PATH");
1119 if (!startup_PATH) startup_PATH = "";
1120
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001121 /* Read the previous state from the state file */
Denis Vlasenko2b52b272007-03-09 22:48:08 +00001122 state_fp = fopen("/var/run/ifstate", "r");
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001123 if (state_fp) {
1124 char *start, *end_ptr;
1125 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1126 /* We should only need to check for a single character */
1127 end_ptr = start + strcspn(start, " \t\n");
1128 *end_ptr = '\0';
1129 llist_add_to(&state_list, start);
1130 }
1131 fclose(state_fp);
1132 }
1133
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001134 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001135 if (DO_ALL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136 if (cmds == iface_up) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001137 target_list = defn->autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +00001138 } else {
Eric Andersen8320b422003-04-02 10:13:26 +00001139 /* iface_down */
1140 const llist_t *list = state_list;
1141 while (list) {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001142 llist_add_to_end(&target_list, xstrdup(list->data));
Eric Andersen8320b422003-04-02 10:13:26 +00001143 list = list->link;
1144 }
1145 target_list = defn->autointerfaces;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00001146 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001147 } else {
Rob Landley8bb50782006-05-26 23:44:51 +00001148 llist_add_to_end(&target_list, argv[optind]);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001149 }
1150
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001151 /* Update the interfaces */
1152 while (target_list) {
Eric Andersen8320b422003-04-02 10:13:26 +00001153 llist_t *iface_list;
1154 struct interface_defn_t *currif;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001155 char *iface;
1156 char *liface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001157 char *pch;
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001158 bool okay = 0;
1159 unsigned cmds_ret;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001160
Rob Landleyd921b2e2006-08-03 15:41:12 +00001161 iface = xstrdup(target_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001162 target_list = target_list->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001163
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001164 pch = strchr(iface, '=');
1165 if (pch) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001166 *pch = '\0';
Rob Landleyd921b2e2006-08-03 15:41:12 +00001167 liface = xstrdup(pch + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001168 } else {
Rob Landleyd921b2e2006-08-03 15:41:12 +00001169 liface = xstrdup(iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001170 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001171
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001172 if (!FORCE) {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001173 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001174
1175 if (cmds == iface_up) {
1176 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001177 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001178 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001179 continue;
1180 }
1181 } else {
1182 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001183 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001184 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001185 continue;
1186 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001187 }
1188 }
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 Vlasenko05341252006-09-26 20:35:30 +00001235 puts("");
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;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001241 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001242 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001243
1244 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001245 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001246 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001247 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001248 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001249 free(iface_state->data);
1250 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001251 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001252 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001253 /* Remove an interface from state_list */
1254 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001255 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001256 }
1257 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001258 }
1259
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001260 /* Actually write the new state */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001261 if (!NO_ACT) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001262 state_fp = xfopen("/var/run/ifstate", "w");
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001263 while (state_list) {
1264 if (state_list->data) {
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001265 fprintf(state_fp, "%s\n", state_list->data);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001266 }
1267 state_list = state_list->link;
1268 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001269 fclose(state_fp);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001270 }
1271
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001272 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001273}