blob: 2b748f11df7e2f023cfa6b3dfde85a31623b3b84 [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
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000018#include <sys/utsname.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000019#include <fnmatch.h>
20#include <getopt.h>
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000021
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000022#include "libbb.h"
23
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000024#define MAX_OPT_DEPTH 10
25#define EUNBALBRACK 10001
26#define EUNDEFVAR 10002
27#define EUNBALPER 10000
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000028
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000029#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen233b1702003-06-05 19:37:01 +000030#define MAX_INTERFACE_LENGTH 10
31#endif
Eric Andersen8320b422003-04-02 10:13:26 +000032
Denis Vlasenkofcfe8342006-12-18 21:02:00 +000033#define debug_noise(args...) /*fprintf(stderr, args)*/
Eric Andersen8320b422003-04-02 10:13:26 +000034
35/* Forward declaration */
36struct interface_defn_t;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000037
Denis Vlasenko097c3242006-11-27 16:59:15 +000038typedef int execfn(char *command);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000039
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000040struct method_t {
41 const 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
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000046struct address_family_t {
47 const char *name;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000048 int n_methods;
Denis Vlasenko05341252006-09-26 20:35:30 +000049 const struct method_t *method;
Eric Andersen8320b422003-04-02 10:13:26 +000050};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000051
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000052struct mapping_defn_t {
Eric Andersen8320b422003-04-02 10:13:26 +000053 struct mapping_defn_t *next;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000054
55 int max_matches;
56 int n_matches;
57 char **match;
58
59 char *script;
60
61 int max_mappings;
62 int n_mappings;
63 char **mapping;
Eric Andersen8320b422003-04-02 10:13:26 +000064};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000065
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000066struct variable_t {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000067 char *name;
68 char *value;
Eric Andersen8320b422003-04-02 10:13:26 +000069};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000070
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000071struct interface_defn_t {
Denis Vlasenko05341252006-09-26 20:35:30 +000072 const struct address_family_t *address_family;
73 const struct method_t *method;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000074
Rob Landleye813ddb2006-02-28 03:53:14 +000075 char *iface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000076 int max_options;
77 int n_options;
Eric Andersen8320b422003-04-02 10:13:26 +000078 struct variable_t *option;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000079};
80
Denis Vlasenko89ef65f2007-01-29 23:43:18 +000081struct interfaces_file_t {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +000082 llist_t *autointerfaces;
Eric Andersen8320b422003-04-02 10:13:26 +000083 llist_t *ifaces;
84 struct mapping_defn_t *mappings;
85};
Glenn L McGrath021fa7d2002-11-09 09:34:15 +000086
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +000087#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
88enum {
89 OPT_do_all = 0x1,
90 OPT_no_act = 0x2,
91 OPT_verbose = 0x4,
92 OPT_force = 0x8,
93 OPT_no_mappings = 0x10,
94};
Denis Vlasenkoc12f5302006-10-06 09:49:47 +000095#define DO_ALL (option_mask32 & OPT_do_all)
96#define NO_ACT (option_mask32 & OPT_no_act)
97#define VERBOSE (option_mask32 & OPT_verbose)
98#define FORCE (option_mask32 & OPT_force)
99#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000100
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000101static char **my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000102
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000103static const char *startup_PATH;
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000104
Mike Frysingerb049c0e2006-06-20 23:03:27 +0000105#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
106
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000107#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000108
Denis Vlasenko097c3242006-11-27 16:59:15 +0000109static unsigned count_bits(unsigned a)
Eric Andersen8320b422003-04-02 10:13:26 +0000110{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000111 unsigned result;
Eric Andersen8320b422003-04-02 10:13:26 +0000112 result = (a & 0x55) + ((a >> 1) & 0x55);
113 result = (result & 0x33) + ((result >> 2) & 0x33);
Denis Vlasenko097c3242006-11-27 16:59:15 +0000114 return (result & 0x0F) + ((result >> 4) & 0x0F);
Eric Andersen8320b422003-04-02 10:13:26 +0000115}
116
Eric Andersen66a3af92003-01-27 17:41:19 +0000117static int count_netmask_bits(char *dotted_quad)
118{
Denis Vlasenko097c3242006-11-27 16:59:15 +0000119 unsigned result, a, b, c, d;
Eric Andersen66a3af92003-01-27 17:41:19 +0000120 /* Found a netmask... Check if it is dotted quad */
121 if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
122 return -1;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000123 // FIXME: will be confused by e.g. 255.0.255.0
Eric Andersen8320b422003-04-02 10:13:26 +0000124 result = count_bits(a);
125 result += count_bits(b);
126 result += count_bits(c);
127 result += count_bits(d);
Denis Vlasenko05341252006-09-26 20:35:30 +0000128 return (int)result;
Eric Andersen66a3af92003-01-27 17:41:19 +0000129}
130#endif
131
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000132static void addstr(char **bufp, const char *str, size_t str_length)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000133{
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000134 /* xasprintf trick will be smaller, but we are often
135 * called with str_length == 1 - don't want to have
136 * THAT much of malloc/freeing! */
137 char *buf = *bufp;
138 int len = (buf ? strlen(buf) : 0);
139 str_length++;
140 buf = xrealloc(buf, len + str_length);
141 /* copies at most str_length-1 chars! */
142 safe_strncpy(buf + len, str, str_length);
143 *bufp = buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000144}
145
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000146static int strncmpz(const char *l, const char *r, size_t llen)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000147{
148 int i = strncmp(l, r, llen);
149
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000150 if (i == 0)
Denis Vlasenko05341252006-09-26 20:35:30 +0000151 return -r[llen];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000152 return i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000153}
154
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000155static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000156{
157 int i;
158
159 if (strncmpz(id, "iface", idlen) == 0) {
Eric Andersen8320b422003-04-02 10:13:26 +0000160 char *result;
161 static char label_buf[20];
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000162 safe_strncpy(label_buf, ifd->iface, sizeof(label_buf));
Eric Andersen8320b422003-04-02 10:13:26 +0000163 result = strchr(label_buf, ':');
164 if (result) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000165 *result = '\0';
Eric Andersen8320b422003-04-02 10:13:26 +0000166 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000167 return label_buf;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000168 }
169 if (strncmpz(id, "label", idlen) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000170 return ifd->iface;
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000171 }
172 for (i = 0; i < ifd->n_options; i++) {
173 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
174 return ifd->option[i].value;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000175 }
176 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000177 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000178}
179
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000180static char *parse(const char *command, struct interface_defn_t *ifd)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000181{
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000182 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
183 int okay[MAX_OPT_DEPTH] = { 1 };
184 int opt_depth = 1;
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000185 char *result = NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000186
187 while (*command) {
188 switch (*command) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000189 default:
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000190 addstr(&result, command, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000191 command++;
192 break;
193 case '\\':
194 if (command[1]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000195 addstr(&result, command + 1, 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000196 command += 2;
197 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000198 addstr(&result, command, 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000199 command++;
Denis Vlasenko05341252006-09-26 20:35:30 +0000200 }
201 break;
202 case '[':
203 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000204 old_pos[opt_depth] = result ? strlen(result) : 0;
Denis Vlasenko05341252006-09-26 20:35:30 +0000205 okay[opt_depth] = 1;
206 opt_depth++;
207 command += 2;
208 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000209 addstr(&result, "[", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000210 command++;
211 }
212 break;
213 case ']':
214 if (command[1] == ']' && opt_depth > 1) {
215 opt_depth--;
216 if (!okay[opt_depth]) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000217 result[old_pos[opt_depth]] = '\0';
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000218 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000219 command += 2;
220 } else {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000221 addstr(&result, "]", 1);
Denis Vlasenko05341252006-09-26 20:35:30 +0000222 command++;
223 }
224 break;
225 case '%':
226 {
227 char *nextpercent;
228 char *varvalue;
229
230 command++;
231 nextpercent = strchr(command, '%');
232 if (!nextpercent) {
233 errno = EUNBALPER;
234 free(result);
235 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000236 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000237
238 varvalue = get_var(command, nextpercent - command, ifd);
239
240 if (varvalue) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000241 addstr(&result, varvalue, strlen(varvalue));
Eric Andersen8320b422003-04-02 10:13:26 +0000242 } else {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000243#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000244 /* Sigh... Add a special case for 'ip' to convert from
245 * dotted quad to bit count style netmasks. */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000246 if (strncmp(command, "bnmask", 6) == 0) {
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000247 unsigned res;
Denis Vlasenko05341252006-09-26 20:35:30 +0000248 varvalue = get_var("netmask", 7, ifd);
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000249 if (varvalue && (res = count_netmask_bits(varvalue)) > 0) {
250 const char *argument = utoa(res);
251 addstr(&result, argument, strlen(argument));
Denis Vlasenko05341252006-09-26 20:35:30 +0000252 command = nextpercent + 1;
253 break;
Eric Andersen8320b422003-04-02 10:13:26 +0000254 }
Eric Andersen8320b422003-04-02 10:13:26 +0000255 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000256#endif
257 okay[opt_depth - 1] = 0;
Eric Andersen8320b422003-04-02 10:13:26 +0000258 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000259
260 command = nextpercent + 1;
261 }
262 break;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000263 }
264 }
265
266 if (opt_depth > 1) {
267 errno = EUNBALBRACK;
268 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000269 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000270 }
271
272 if (!okay[0]) {
273 errno = EUNDEFVAR;
274 free(result);
Denis Vlasenko05341252006-09-26 20:35:30 +0000275 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000276 }
277
Denis Vlasenko05341252006-09-26 20:35:30 +0000278 return result;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000279}
280
Denis Vlasenko05341252006-09-26 20:35:30 +0000281/* execute() returns 1 for success and 0 for failure */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000282static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000283{
284 char *out;
285 int ret;
286
287 out = parse(command, ifd);
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000288 if (!out) {
Denis Vlasenko2375d752006-12-19 23:15:46 +0000289 /* parse error? */
Denis Vlasenko05341252006-09-26 20:35:30 +0000290 return 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000291 }
Denis Vlasenko2375d752006-12-19 23:15:46 +0000292 /* out == "": parsed ok but not all needed variables known, skip */
293 ret = out[0] ? (*exec)(out) : 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000294
295 free(out);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000296 if (ret != 1) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000297 return 0;
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000298 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000299 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000300}
Bernhard Reutner-Fischerd42ef282005-12-14 14:13:15 +0000301#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000302
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000303#if ENABLE_FEATURE_IFUPDOWN_IPV6
Eric Andersen8320b422003-04-02 10:13:26 +0000304static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000305{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000306#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000307 int result;
Denis Vlasenko05341252006-09-26 20:35:30 +0000308 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000309 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000310 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000311#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000312 return execute("ifconfig %iface% add ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000313#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000314}
315
Eric Andersen8320b422003-04-02 10:13:26 +0000316static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000317{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000318#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000319 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000320#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000321 return execute("ifconfig %iface% del ::1", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000322#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000323}
324
Eric Andersen8320b422003-04-02 10:13:26 +0000325static int static_up6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000326{
Eric Andersen8320b422003-04-02 10:13:26 +0000327 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000328#if ENABLE_FEATURE_IFUPDOWN_IP
329 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
330 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
331 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
332 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000333#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000334 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000335 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000336 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000337#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000338 return ((result == 3) ? 3 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000339}
340
Eric Andersen8320b422003-04-02 10:13:26 +0000341static int static_down6(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000342{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000343#if ENABLE_FEATURE_IFUPDOWN_IP
Denis Vlasenko05341252006-09-26 20:35:30 +0000344 return execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000345#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000346 return execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000347#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000348}
349
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000350#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000351static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000352{
Eric Andersen8320b422003-04-02 10:13:26 +0000353 int result;
354 result = execute("ip tunnel add %iface% mode sit remote "
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000355 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000356 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath62b031f2003-08-29 07:47:52 +0000357 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000358 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000359 return ((result == 4) ? 4 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000360}
361
Eric Andersen8320b422003-04-02 10:13:26 +0000362static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000363{
Denis Vlasenko05341252006-09-26 20:35:30 +0000364 return execute("ip tunnel del %iface%", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000365}
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000366#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000367
Denis Vlasenko05341252006-09-26 20:35:30 +0000368static const struct method_t methods6[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000369#if ENABLE_FEATURE_IFUPDOWN_IP
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000370 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000371#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000372 { "static", static_up6, static_down6, },
373 { "loopback", loopback_up6, loopback_down6, },
374};
375
Denis Vlasenko05341252006-09-26 20:35:30 +0000376static const struct address_family_t addr_inet6 = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000377 "inet6",
Eric Andersen8320b422003-04-02 10:13:26 +0000378 sizeof(methods6) / sizeof(struct method_t),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000379 methods6
380};
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000381#endif /* FEATURE_IFUPDOWN_IPV6 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000382
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000383#if ENABLE_FEATURE_IFUPDOWN_IPV4
Eric Andersen8320b422003-04-02 10:13:26 +0000384static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000385{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000386#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000387 int result;
Eric Andersenfdd2a0f2003-08-06 09:23:44 +0000388 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
Eric Andersen8a931792003-07-03 10:20:29 +0000389 result += execute("ip link set %iface% up", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000390 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000391#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000392 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000393#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000394}
395
Eric Andersen8320b422003-04-02 10:13:26 +0000396static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000397{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000398#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000399 int result;
400 result = execute("ip addr flush dev %iface%", ifd, exec);
401 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000402 return ((result == 2) ? 2 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000403#else
Denis Vlasenko05341252006-09-26 20:35:30 +0000404 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000405#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000406}
407
Eric Andersen8320b422003-04-02 10:13:26 +0000408static int static_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000409{
Eric Andersen8320b422003-04-02 10:13:26 +0000410 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000411#if ENABLE_FEATURE_IFUPDOWN_IP
412 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
413 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
414 result += execute("ip link set[[ mtu %mtu%]][[ address %hwaddress%]] %iface% up", ifd, exec);
415 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000416 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000417#else
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000418 /* ifconfig said to set iface up before it processes hw %hwaddress%,
419 * which then of course fails. Thus we run two separate ifconfig */
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000420 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000421 ifd, exec);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000422 result += execute("ifconfig %iface% %address% netmask %netmask%"
423 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000424 ifd, exec);
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000425 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
Denis Vlasenkoa741b772006-11-23 15:08:37 +0000426 return ((result == 3) ? 3 : 0);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000427#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000428}
429
Eric Andersen8320b422003-04-02 10:13:26 +0000430static int static_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000431{
Eric Andersen8320b422003-04-02 10:13:26 +0000432 int result;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000433#if ENABLE_FEATURE_IFUPDOWN_IP
Eric Andersen8320b422003-04-02 10:13:26 +0000434 result = execute("ip addr flush dev %iface%", ifd, exec);
435 result += execute("ip link set %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000436#else
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000437 result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec);
Eric Andersen8320b422003-04-02 10:13:26 +0000438 result += execute("ifconfig %iface% down", ifd, exec);
Glenn L McGrathd66370c2003-01-13 21:40:38 +0000439#endif
Glenn L McGrath0177ce12004-07-21 23:56:31 +0000440 return ((result == 2) ? 2 : 0);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000441}
442
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000443#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000444struct dhcp_client_t
445{
446 const char *name;
447 const char *startcmd;
448 const char *stopcmd;
449};
450
451static const struct dhcp_client_t ext_dhcp_clients[] = {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000452 { "dhcpcd",
453 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
454 "dhcpcd -k %iface%",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000455 },
456 { "dhclient",
457 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
458 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
459 },
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000460 { "pump",
461 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
462 "pump -i %iface% -k",
463 },
464 { "udhcpc",
465 "udhcpc -R -n -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
466 "kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000467 },
468};
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000469#endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000470
Eric Andersen8320b422003-04-02 10:13:26 +0000471static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000472{
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000473#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000474 int i, nclients = sizeof(ext_dhcp_clients) / sizeof(ext_dhcp_clients[0]);
475 for (i = 0; i < nclients; i++) {
476 if (exists_execable(ext_dhcp_clients[i].name))
477 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
478 }
479 bb_error_msg("no dhcp clients found");
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000480 return 0;
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000481#elif ENABLE_APP_UDHCPC
482 return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
483 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]]",
484 ifd, exec);
485#else
486 return 0; /* no dhcp support */
Denis Vlasenko4b924f32007-05-30 00:29:55 +0000487#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000488}
489
Eric Andersen8320b422003-04-02 10:13:26 +0000490static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000491{
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000492#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000493 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 Vlasenkoeda43d72007-05-02 22:04:38 +0000500#elif ENABLE_APP_UDHCPC
501 return execute("kill -TERM "
502 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
503#else
504 return 0; /* no support for dhcp */
Denis Vlasenkof6f43df2006-10-11 22:16:56 +0000505#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000506}
507
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000508static int manual_up_down(struct interface_defn_t *ifd, execfn *exec)
509{
510 return 1;
511}
512
Eric Andersen8320b422003-04-02 10:13:26 +0000513static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000514{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000515 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
516 "[[ --server %server%]][[ --hwaddr %hwaddr%]] "
Denis Vlasenko05341252006-09-26 20:35:30 +0000517 "--returniffail --serverbcast", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000518}
519
Eric Andersen8320b422003-04-02 10:13:26 +0000520static int ppp_up(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("pon[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000523}
524
Eric Andersen8320b422003-04-02 10:13:26 +0000525static int ppp_down(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("poff[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000528}
529
Eric Andersen8320b422003-04-02 10:13:26 +0000530static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000531{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000532 return execute("start-stop-daemon --start -x wvdial "
533 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000534}
535
Eric Andersen8320b422003-04-02 10:13:26 +0000536static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000537{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000538 return execute("start-stop-daemon --stop -x wvdial "
Denis Vlasenko05341252006-09-26 20:35:30 +0000539 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000540}
541
Denis Vlasenko05341252006-09-26 20:35:30 +0000542static const struct method_t methods[] = {
Denis Vlasenko1c3577f2006-10-02 20:57:10 +0000543 { "manual", manual_up_down, manual_up_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000544 { "wvdial", wvdial_up, wvdial_down, },
545 { "ppp", ppp_up, ppp_down, },
546 { "static", static_up, static_down, },
Eric Andersen373bc1e2004-07-30 14:31:01 +0000547 { "bootp", bootp_up, static_down, },
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000548 { "dhcp", dhcp_up, dhcp_down, },
549 { "loopback", loopback_up, loopback_down, },
550};
551
Denis Vlasenko05341252006-09-26 20:35:30 +0000552static const struct address_family_t addr_inet = {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000553 "inet",
Denis Vlasenkoeda43d72007-05-02 22:04:38 +0000554 sizeof(methods) / sizeof(methods[0]),
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000555 methods
556};
557
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000558#endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000559
Glenn L McGrath85737042003-01-14 23:26:57 +0000560static char *next_word(char **buf)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000561{
Glenn L McGrath85737042003-01-14 23:26:57 +0000562 unsigned short length;
563 char *word;
564
Denis Vlasenko93ad1c22006-11-23 15:07:38 +0000565 if (!buf || !*buf || !**buf) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000566 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000567 }
568
Glenn L McGrath85737042003-01-14 23:26:57 +0000569 /* Skip over leading whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000570 word = skip_whitespace(*buf);
Eric Andersen3c8bca32003-06-20 10:02:29 +0000571
572 /* Skip over comments */
573 if (*word == '#') {
Denis Vlasenko05341252006-09-26 20:35:30 +0000574 return NULL;
Eric Andersen3c8bca32003-06-20 10:02:29 +0000575 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000576
Glenn L McGrath85737042003-01-14 23:26:57 +0000577 /* Find the length of this word */
578 length = strcspn(word, " \t\n");
579 if (length == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000580 return NULL;
Glenn L McGrath85737042003-01-14 23:26:57 +0000581 }
582 *buf = word + length;
Eric Andersen28942662003-04-19 23:15:06 +0000583 /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
584 if (**buf) {
585 **buf = '\0';
586 (*buf)++;
587 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000588
589 return word;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000590}
591
Denis Vlasenko05341252006-09-26 20:35:30 +0000592static 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 +0000593{
594 int i;
595
Denis Vlasenko736230e2006-11-20 19:40:36 +0000596 if (!name)
597 return NULL;
598
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000599 for (i = 0; af[i]; i++) {
600 if (strcmp(af[i]->name, name) == 0) {
601 return af[i];
602 }
603 }
604 return NULL;
605}
606
Denis Vlasenko05341252006-09-26 20:35:30 +0000607static const struct method_t *get_method(const struct address_family_t *af, char *name)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000608{
609 int i;
610
Denis Vlasenko736230e2006-11-20 19:40:36 +0000611 if (!name)
612 return NULL;
613
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000614 for (i = 0; i < af->n_methods; i++) {
615 if (strcmp(af->method[i].name, name) == 0) {
616 return &af->method[i];
617 }
618 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000619 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000620}
621
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000622static const llist_t *find_list_string(const llist_t *list, const char *string)
623{
Denis Vlasenko736230e2006-11-20 19:40:36 +0000624 if (string == NULL)
625 return NULL;
626
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000627 while (list) {
628 if (strcmp(list->data, string) == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000629 return list;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000630 }
631 list = list->link;
632 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000633 return NULL;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000634}
635
Glenn L McGrathd4004ee2004-09-14 17:24:59 +0000636static struct interfaces_file_t *read_interfaces(const char *filename)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000637{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000638#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Eric Andersen8320b422003-04-02 10:13:26 +0000639 struct mapping_defn_t *currmap = NULL;
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000640#endif
Eric Andersen8320b422003-04-02 10:13:26 +0000641 struct interface_defn_t *currif = NULL;
642 struct interfaces_file_t *defn;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000643 FILE *f;
Glenn L McGrath85737042003-01-14 23:26:57 +0000644 char *firstword;
645 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000646
647 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
648
Rob Landleya6e131d2006-05-29 06:43:55 +0000649 defn = xzalloc(sizeof(struct interfaces_file_t));
Glenn L McGrath85737042003-01-14 23:26:57 +0000650
Rob Landleyd921b2e2006-08-03 15:41:12 +0000651 f = xfopen(filename, "r");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000652
Denis Vlasenko2d5ca602006-10-12 22:43:20 +0000653 while ((buf = xmalloc_getline(f)) != NULL) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000654 char *buf_ptr = buf;
Glenn L McGrath398ff9d2002-12-06 11:51:46 +0000655
Glenn L McGrath85737042003-01-14 23:26:57 +0000656 firstword = next_word(&buf_ptr);
657 if (firstword == NULL) {
Eric Andersen3c8bca32003-06-20 10:02:29 +0000658 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000659 continue; /* blank line */
660 }
661
662 if (strcmp(firstword, "mapping") == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000663#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Rob Landleya6e131d2006-05-29 06:43:55 +0000664 currmap = xzalloc(sizeof(struct mapping_defn_t));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000665
Glenn L McGrath85737042003-01-14 23:26:57 +0000666 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000667 if (currmap->max_matches == currmap->n_matches) {
668 currmap->max_matches = currmap->max_matches * 2 + 1;
669 currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
670 }
671
Rob Landleyd921b2e2006-08-03 15:41:12 +0000672 currmap->match[currmap->n_matches++] = xstrdup(firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000673 }
674 currmap->max_mappings = 0;
675 currmap->n_mappings = 0;
676 currmap->mapping = NULL;
677 currmap->script = NULL;
678 {
Eric Andersen8320b422003-04-02 10:13:26 +0000679 struct mapping_defn_t **where = &defn->mappings;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000680 while (*where != NULL) {
681 where = &(*where)->next;
682 }
683 *where = currmap;
684 currmap->next = NULL;
685 }
Eric Andersen8320b422003-04-02 10:13:26 +0000686 debug_noise("Added mapping\n");
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +0000687#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000688 currently_processing = MAPPING;
689 } else if (strcmp(firstword, "iface") == 0) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000690 static const struct address_family_t *const addr_fams[] = {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000691#if ENABLE_FEATURE_IFUPDOWN_IPV4
Denis Vlasenko05341252006-09-26 20:35:30 +0000692 &addr_inet,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000693#endif
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000694#if ENABLE_FEATURE_IFUPDOWN_IPV6
Denis Vlasenko05341252006-09-26 20:35:30 +0000695 &addr_inet6,
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000696#endif
Denis Vlasenko05341252006-09-26 20:35:30 +0000697 NULL
698 };
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000699
Denis Vlasenko05341252006-09-26 20:35:30 +0000700 char *iface_name;
701 char *address_family_name;
702 char *method_name;
703 llist_t *iface_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000704
Denis Vlasenko05341252006-09-26 20:35:30 +0000705 currif = xzalloc(sizeof(struct interface_defn_t));
706 iface_name = next_word(&buf_ptr);
707 address_family_name = next_word(&buf_ptr);
708 method_name = next_word(&buf_ptr);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000709
Denis Vlasenko05341252006-09-26 20:35:30 +0000710 if (buf_ptr == NULL) {
711 bb_error_msg("too few parameters for line \"%s\"", buf);
712 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000713 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000714
715 /* ship any trailing whitespace */
Denis Vlasenkod18a3a22006-10-25 12:46:03 +0000716 buf_ptr = skip_whitespace(buf_ptr);
Denis Vlasenko05341252006-09-26 20:35:30 +0000717
718 if (buf_ptr[0] != '\0') {
719 bb_error_msg("too many parameters \"%s\"", buf);
720 return NULL;
721 }
722
723 currif->iface = xstrdup(iface_name);
724
725 currif->address_family = get_address_family(addr_fams, address_family_name);
726 if (!currif->address_family) {
727 bb_error_msg("unknown address type \"%s\"", address_family_name);
728 return NULL;
729 }
730
731 currif->method = get_method(currif->address_family, method_name);
732 if (!currif->method) {
733 bb_error_msg("unknown method \"%s\"", method_name);
734 return NULL;
735 }
736
737 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
738 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
739 if ((strcmp(tmp->iface, currif->iface) == 0) &&
740 (tmp->address_family == currif->address_family)) {
741 bb_error_msg("duplicate interface \"%s\"", tmp->iface);
742 return NULL;
743 }
744 }
745 llist_add_to_end(&(defn->ifaces), (char*)currif);
746
747 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000748 currently_processing = IFACE;
749 } else if (strcmp(firstword, "auto") == 0) {
Glenn L McGrath85737042003-01-14 23:26:57 +0000750 while ((firstword = next_word(&buf_ptr)) != NULL) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000751
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000752 /* Check the interface isnt already listed */
753 if (find_list_string(defn->autointerfaces, firstword)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000754 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000755 }
756
Glenn L McGrath8e49caa2002-12-08 01:23:39 +0000757 /* Add the interface to the list */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000758 llist_add_to_end(&(defn->autointerfaces), xstrdup(firstword));
Eric Andersen8320b422003-04-02 10:13:26 +0000759 debug_noise("\nauto %s\n", firstword);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000760 }
761 currently_processing = NONE;
762 } else {
763 switch (currently_processing) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000764 case IFACE:
765 {
766 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000767
Denis Vlasenko05341252006-09-26 20:35:30 +0000768 if (strlen(buf_ptr) == 0) {
769 bb_error_msg("option with empty value \"%s\"", buf);
770 return NULL;
771 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000772
Denis Vlasenko05341252006-09-26 20:35:30 +0000773 if (strcmp(firstword, "up") != 0
774 && strcmp(firstword, "down") != 0
775 && strcmp(firstword, "pre-up") != 0
776 && strcmp(firstword, "post-down") != 0) {
777 for (i = 0; i < currif->n_options; i++) {
778 if (strcmp(currif->option[i].name, firstword) == 0) {
779 bb_error_msg("duplicate option \"%s\"", buf);
780 return NULL;
Eric Andersen8320b422003-04-02 10:13:26 +0000781 }
782 }
783 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000784 }
785 if (currif->n_options >= currif->max_options) {
786 struct variable_t *opt;
Eric Andersen8320b422003-04-02 10:13:26 +0000787
Denis Vlasenko05341252006-09-26 20:35:30 +0000788 currif->max_options = currif->max_options + 10;
789 opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
790 currif->option = opt;
791 }
792 currif->option[currif->n_options].name = xstrdup(firstword);
793 currif->option[currif->n_options].value = xstrdup(buf_ptr);
794 if (!currif->option[currif->n_options].name) {
795 perror(filename);
796 return NULL;
797 }
798 if (!currif->option[currif->n_options].value) {
799 perror(filename);
800 return NULL;
801 }
802 debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
803 currif->option[currif->n_options].value);
804 currif->n_options++;
805 break;
806 case MAPPING:
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000807#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko05341252006-09-26 20:35:30 +0000808 if (strcmp(firstword, "script") == 0) {
809 if (currmap->script != NULL) {
810 bb_error_msg("duplicate script in mapping \"%s\"", buf);
Eric Andersen8320b422003-04-02 10:13:26 +0000811 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000812 } else {
813 currmap->script = xstrdup(next_word(&buf_ptr));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000814 }
Denis Vlasenko05341252006-09-26 20:35:30 +0000815 } else if (strcmp(firstword, "map") == 0) {
816 if (currmap->max_mappings == currmap->n_mappings) {
817 currmap->max_mappings = currmap->max_mappings * 2 + 1;
818 currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
819 }
820 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&buf_ptr));
821 currmap->n_mappings++;
822 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000823 bb_error_msg("misplaced option \"%s\"", buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000824 return NULL;
Denis Vlasenko05341252006-09-26 20:35:30 +0000825 }
826#endif
827 break;
828 case NONE:
829 default:
830 bb_error_msg("misplaced option \"%s\"", buf);
831 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000832 }
833 }
Glenn L McGrath85737042003-01-14 23:26:57 +0000834 free(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000835 }
836 if (ferror(f) != 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000837 bb_perror_msg_and_die("%s", filename);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000838 }
839 fclose(f);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000840
841 return defn;
842}
843
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000844static char *setlocalenv(const char *format, const char *name, const char *value)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000845{
846 char *result;
847 char *here;
848 char *there;
849
Rob Landleyd921b2e2006-08-03 15:41:12 +0000850 result = xasprintf(format, name, value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000851
852 for (here = there = result; *there != '=' && *there; there++) {
853 if (*there == '-')
854 *there = '_';
855 if (isalpha(*there))
856 *there = toupper(*there);
857
858 if (isalnum(*there) || *there == '_') {
859 *here = *there;
860 here++;
861 }
862 }
Rob Landleya3896512006-05-07 20:20:34 +0000863 memmove(here, there, strlen(there) + 1);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000864
865 return result;
866}
867
Rob Landleye813ddb2006-02-28 03:53:14 +0000868static void set_environ(struct interface_defn_t *iface, const char *mode)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000869{
870 char **environend;
871 int i;
872 const int n_env_entries = iface->n_options + 5;
873 char **ppch;
874
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000875 if (my_environ != NULL) {
876 for (ppch = my_environ; *ppch; ppch++) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000877 free(*ppch);
878 *ppch = NULL;
879 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000880 free(my_environ);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000881 }
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000882 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
883 environend = my_environ;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000884
885 for (i = 0; i < iface->n_options; i++) {
886 if (strcmp(iface->option[i].name, "up") == 0
Eric Andersen8320b422003-04-02 10:13:26 +0000887 || strcmp(iface->option[i].name, "down") == 0
888 || strcmp(iface->option[i].name, "pre-up") == 0
889 || strcmp(iface->option[i].name, "post-down") == 0) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000890 continue;
891 }
892 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000893 }
894
895 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000896 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000897 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000898 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
Denis Vlasenko2f4399c2006-09-27 14:14:51 +0000899 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000900}
901
902static int doit(char *str)
903{
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000904 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000905 puts(str);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000906 }
Denis Vlasenkoc12f5302006-10-06 09:49:47 +0000907 if (!(option_mask32 & OPT_no_act)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000908 pid_t child;
909 int status;
910
911 fflush(NULL);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000912 child = fork();
913 switch (child) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000914 case -1: /* failure */
Denis Vlasenko05341252006-09-26 20:35:30 +0000915 return 0;
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000916 case 0: /* child */
Denis Vlasenko8cd1a282006-12-19 23:01:33 +0000917 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
Denis Vlasenko05341252006-09-26 20:35:30 +0000918 exit(127);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000919 }
920 waitpid(child, &status, 0);
921 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
922 return 0;
923 }
924 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +0000925 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000926}
927
Rob Landleye813ddb2006-02-28 03:53:14 +0000928static int execute_all(struct interface_defn_t *ifd, const char *opt)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000929{
930 int i;
Eric Anderseneb213bd2003-09-12 08:39:05 +0000931 char *buf;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000932 for (i = 0; i < ifd->n_options; i++) {
933 if (strcmp(ifd->option[i].name, opt) == 0) {
Rob Landleye813ddb2006-02-28 03:53:14 +0000934 if (!doit(ifd->option[i].value)) {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000935 return 0;
936 }
937 }
938 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000939
Rob Landleyd921b2e2006-08-03 15:41:12 +0000940 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
Denis Vlasenko2375d752006-12-19 23:15:46 +0000941 /* heh, we don't bother free'ing it */
942 return doit(buf);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000943}
944
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000945static int check(char *str)
946{
Eric Andersen8320b422003-04-02 10:13:26 +0000947 return str != NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000948}
949
Eric Andersen8320b422003-04-02 10:13:26 +0000950static int iface_up(struct interface_defn_t *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000951{
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000952 if (!iface->method->up(iface, check)) return -1;
Eric Andersen8320b422003-04-02 10:13:26 +0000953 set_environ(iface, "start");
Rob Landleye813ddb2006-02-28 03:53:14 +0000954 if (!execute_all(iface, "pre-up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000955 if (!iface->method->up(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000956 if (!execute_all(iface, "up")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000957 return 1;
Eric Andersen8320b422003-04-02 10:13:26 +0000958}
959
960static int iface_down(struct interface_defn_t *iface)
961{
Eric Andersen8320b422003-04-02 10:13:26 +0000962 if (!iface->method->down(iface,check)) return -1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000963 set_environ(iface, "stop");
Rob Landleye813ddb2006-02-28 03:53:14 +0000964 if (!execute_all(iface, "down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000965 if (!iface->method->down(iface, doit)) return 0;
Rob Landleye813ddb2006-02-28 03:53:14 +0000966 if (!execute_all(iface, "post-down")) return 0;
Eric Andersen658f8b12003-12-19 10:46:00 +0000967 return 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000968}
969
Denis Vlasenkofcfe8342006-12-18 21:02:00 +0000970#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Glenn L McGrath021fa7d2002-11-09 09:34:15 +0000971static int popen2(FILE **in, FILE **out, char *command, ...)
972{
973 va_list ap;
974 char *argv[11] = { command };
975 int argc;
976 int infd[2], outfd[2];
977 pid_t pid;
978
979 argc = 1;
980 va_start(ap, command);
981 while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
982 argc++;
983 }
984 argv[argc] = NULL; /* make sure */
985 va_end(ap);
986
987 if (pipe(infd) != 0) {
988 return 0;
989 }
990
991 if (pipe(outfd) != 0) {
992 close(infd[0]);
993 close(infd[1]);
994 return 0;
995 }
996
997 fflush(NULL);
998 switch (pid = fork()) {
Denis Vlasenko05341252006-09-26 20:35:30 +0000999 case -1: /* failure */
1000 close(infd[0]);
1001 close(infd[1]);
1002 close(outfd[0]);
1003 close(outfd[1]);
1004 return 0;
1005 case 0: /* child */
1006 dup2(infd[0], 0);
1007 dup2(outfd[1], 1);
1008 close(infd[0]);
1009 close(infd[1]);
1010 close(outfd[0]);
1011 close(outfd[1]);
Denis Vlasenko1d76f432007-02-06 01:20:12 +00001012 BB_EXECVP(command, argv);
Denis Vlasenko05341252006-09-26 20:35:30 +00001013 exit(127);
1014 default: /* parent */
1015 *in = fdopen(infd[1], "w");
1016 *out = fdopen(outfd[0], "r");
1017 close(infd[0]);
1018 close(outfd[1]);
1019 return pid;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001020 }
1021 /* unreached */
1022}
1023
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001024static char *run_mapping(char *physical, struct mapping_defn_t * map)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001025{
1026 FILE *in, *out;
1027 int i, status;
1028 pid_t pid;
1029
Rob Landleyd921b2e2006-08-03 15:41:12 +00001030 char *logical = xstrdup(physical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001031
Eric Andersen8a931792003-07-03 10:20:29 +00001032 /* Run the mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001033 pid = popen2(&in, &out, map->script, physical, NULL);
Eric Andersen8a931792003-07-03 10:20:29 +00001034
1035 /* popen2() returns 0 on failure. */
1036 if (pid == 0)
1037 return logical;
1038
1039 /* Write mappings to stdin of mapping script. */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001040 for (i = 0; i < map->n_mappings; i++) {
1041 fprintf(in, "%s\n", map->mapping[i]);
1042 }
1043 fclose(in);
1044 waitpid(pid, &status, 0);
Eric Andersen8a931792003-07-03 10:20:29 +00001045
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001046 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
Eric Andersen8a931792003-07-03 10:20:29 +00001047 /* If the mapping script exited successfully, try to
1048 * grab a line of output and use that as the name of the
1049 * logical interface. */
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001050 char *new_logical = xmalloc(MAX_INTERFACE_LENGTH);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001051
Eric Andersen233b1702003-06-05 19:37:01 +00001052 if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
Eric Andersen8a931792003-07-03 10:20:29 +00001053 /* If we are able to read a line of output from the script,
1054 * remove any trailing whitespace and use this value
1055 * as the name of the logical interface. */
Rob Landleya3896512006-05-07 20:20:34 +00001056 char *pch = new_logical + strlen(new_logical) - 1;
Eric Andersen233b1702003-06-05 19:37:01 +00001057
1058 while (pch >= new_logical && isspace(*pch))
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001059 *(pch--) = '\0';
Eric Andersen8a931792003-07-03 10:20:29 +00001060
1061 free(logical);
1062 logical = new_logical;
1063 } else {
Rob Landleya6e131d2006-05-29 06:43:55 +00001064 /* If we are UNABLE to read a line of output, discard our
Eric Andersen8a931792003-07-03 10:20:29 +00001065 * freshly allocated memory. */
1066 free(new_logical);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001067 }
1068 }
Eric Andersen8a931792003-07-03 10:20:29 +00001069
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001070 fclose(out);
1071
Eric Andersen8a931792003-07-03 10:20:29 +00001072 return logical;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001073}
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001074#endif /* FEATURE_IFUPDOWN_MAPPING */
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001075
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001076static llist_t *find_iface_state(llist_t *state_list, const char *iface)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001077{
Rob Landleya3896512006-05-07 20:20:34 +00001078 unsigned short iface_len = strlen(iface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001079 llist_t *search = state_list;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001080
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001081 while (search) {
Denis Vlasenkoeda43d72007-05-02 22:04:38 +00001082 if ((strncmp(search->data, iface, iface_len) == 0)
1083 && (search->data[iface_len] == '=')) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001084 return search;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001085 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001086 search = search->link;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001087 }
Denis Vlasenko05341252006-09-26 20:35:30 +00001088 return NULL;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001089}
1090
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001091/* read the previous state from the state file */
1092static llist_t *read_iface_state(void)
1093{
1094 llist_t *state_list = NULL;
1095 FILE *state_fp = fopen("/var/run/ifstate", "r");
1096
1097 if (state_fp) {
1098 char *start, *end_ptr;
1099 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1100 /* We should only need to check for a single character */
1101 end_ptr = start + strcspn(start, " \t\n");
1102 *end_ptr = '\0';
1103 llist_add_to(&state_list, start);
1104 }
1105 fclose(state_fp);
1106 }
1107 return state_list;
1108}
1109
1110
Denis Vlasenko06af2162007-02-03 17:28:39 +00001111int ifupdown_main(int argc, char **argv);
Rob Landleydfba7412006-03-06 20:47:33 +00001112int ifupdown_main(int argc, char **argv)
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001113{
Denis Vlasenko05341252006-09-26 20:35:30 +00001114 int (*cmds)(struct interface_defn_t *) = NULL;
Eric Andersen8320b422003-04-02 10:13:26 +00001115 struct interfaces_file_t *defn;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001116 llist_t *target_list = NULL;
Glenn L McGrathd4004ee2004-09-14 17:24:59 +00001117 const char *interfaces = "/etc/network/interfaces";
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001118 bool any_failures = 0;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001119
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001120 cmds = iface_down;
Denis Vlasenko8f8f2682006-10-03 21:00:43 +00001121 if (applet_name[2] == 'u') {
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001122 /* ifup command */
1123 cmds = iface_up;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001124 }
1125
Denis Vlasenkoc12f5302006-10-06 09:49:47 +00001126 getopt32(argc, argv, OPTION_STR, &interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001127 if (argc - optind > 0) {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001128 if (DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001129 } else {
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001130 if (!DO_ALL) bb_show_usage();
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001131 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001132
Eric Andersen8320b422003-04-02 10:13:26 +00001133 debug_noise("reading %s file:\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001134 defn = read_interfaces(interfaces);
Eric Andersen8320b422003-04-02 10:13:26 +00001135 debug_noise("\ndone reading %s\n\n", interfaces);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001136
Eric Andersen3c8bca32003-06-20 10:02:29 +00001137 if (!defn) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001138 return EXIT_FAILURE;
Eric Andersen3c8bca32003-06-20 10:02:29 +00001139 }
1140
Denis Vlasenko2f4399c2006-09-27 14:14:51 +00001141 startup_PATH = getenv("PATH");
1142 if (!startup_PATH) startup_PATH = "";
1143
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001144 /* Create a list of interfaces to work on */
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001145 if (DO_ALL) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001146 target_list = defn->autointerfaces;
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) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001173 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001174 const llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001175
1176 if (cmds == iface_up) {
1177 /* ifup */
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001178 if (iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001179 bb_error_msg("interface %s already configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001180 continue;
1181 }
1182 } else {
1183 /* ifdown */
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001184 if (!iface_state) {
Manuel Novoa III cad53642003-03-19 09:13:01 +00001185 bb_error_msg("interface %s not configured", iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001186 continue;
1187 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001188 }
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001189 llist_free(state_list, free);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001190 }
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001191
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001192#if ENABLE_FEATURE_IFUPDOWN_MAPPING
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001193 if ((cmds == iface_up) && !NO_MAPPINGS) {
Eric Andersen8320b422003-04-02 10:13:26 +00001194 struct mapping_defn_t *currmap;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001195
1196 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
Denis Vlasenko4e33e072006-10-16 18:24:57 +00001197 int i;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001198 for (i = 0; i < currmap->n_matches; i++) {
1199 if (fnmatch(currmap->match[i], liface, 0) != 0)
1200 continue;
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001201 if (VERBOSE) {
Eric Andersen8320b422003-04-02 10:13:26 +00001202 printf("Running mapping script %s on %s\n", currmap->script, liface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001203 }
Eric Andersen8a931792003-07-03 10:20:29 +00001204 liface = run_mapping(iface, currmap);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001205 break;
1206 }
1207 }
1208 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001209#endif
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001210
Eric Andersen8320b422003-04-02 10:13:26 +00001211 iface_list = defn->ifaces;
1212 while (iface_list) {
1213 currif = (struct interface_defn_t *) iface_list->data;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001214 if (strcmp(liface, currif->iface) == 0) {
1215 char *oldiface = currif->iface;
1216
1217 okay = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001218 currif->iface = iface;
1219
Denis Vlasenko8cd1a282006-12-19 23:01:33 +00001220 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001221
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001222 /* Call the cmds function pointer, does either iface_up() or iface_down() */
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001223 cmds_ret = cmds(currif);
1224 if (cmds_ret == -1) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001225 bb_error_msg("don't seem to have all the variables for %s/%s",
Eric Andersen8320b422003-04-02 10:13:26 +00001226 liface, currif->address_family->name);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001227 any_failures = 1;
Glenn L McGrath0177ce12004-07-21 23:56:31 +00001228 } else if (cmds_ret == 0) {
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001229 any_failures = 1;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001230 }
Glenn L McGrathcdbe5e52002-12-06 08:35:55 +00001231
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001232 currif->iface = oldiface;
1233 }
Eric Andersen8320b422003-04-02 10:13:26 +00001234 iface_list = iface_list->link;
1235 }
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001236 if (VERBOSE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001237 puts("");
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001238 }
1239
Denis Vlasenko7f1f5b02006-09-23 12:49:01 +00001240 if (!okay && !FORCE) {
Denis Vlasenko05341252006-09-26 20:35:30 +00001241 bb_error_msg("ignoring unknown interface %s", liface);
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001242 any_failures = 1;
Denis Vlasenkof92df582007-05-02 22:22:23 +00001243 } else if (!NO_ACT) {
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001244 /* update the state file */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001245 FILE *state_fp;
1246 llist_t *state;
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001247 llist_t *state_list = read_iface_state();
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001248 llist_t *iface_state = find_iface_state(state_list, iface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001249
1250 if (cmds == iface_up) {
Bernhard Reutner-Fischer16deb862007-03-19 19:54:56 +00001251 char * const newiface = xasprintf("%s=%s", iface, liface);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001252 if (iface_state == NULL) {
Rob Landley8bb50782006-05-26 23:44:51 +00001253 llist_add_to_end(&state_list, newiface);
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001254 } else {
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001255 free(iface_state->data);
1256 iface_state->data = newiface;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001257 }
Rob Landleye813ddb2006-02-28 03:53:14 +00001258 } else {
Denis Vlasenkoc115fdb2007-03-06 22:53:10 +00001259 /* Remove an interface from state_list */
1260 llist_unlink(&state_list, iface_state);
Rob Landleya6e131d2006-05-29 06:43:55 +00001261 free(llist_pop(&iface_state));
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001262 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001263
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001264 /* Actually write the new state */
Denis Vlasenkof92df582007-05-02 22:22:23 +00001265 state_fp = xfopen("/var/run/ifstate", "w");
1266 state = state_list;
1267 while (state) {
1268 if (state->data) {
1269 fprintf(state_fp, "%s\n", state->data);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001270 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001271 state = state->link;
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001272 }
Denis Vlasenkof92df582007-05-02 22:22:23 +00001273 fclose(state_fp);
Denis Vlasenkobd100b72007-05-02 21:38:44 +00001274 llist_free(state_list, free);
Glenn L McGrath8e49caa2002-12-08 01:23:39 +00001275 }
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001276 }
1277
Denis Vlasenkofcfe8342006-12-18 21:02:00 +00001278 return any_failures;
Glenn L McGrath021fa7d2002-11-09 09:34:15 +00001279}