1 | /* vi: set sw=4 ts=4: */
|
---|
2 | /*
|
---|
3 | * ifupdown for busybox
|
---|
4 | * Copyright (c) 2002 Glenn McGrath <bug1@iinet.net.au>
|
---|
5 | * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org>
|
---|
6 | *
|
---|
7 | * Based on ifupdown v 0.6.4 by Anthony Towns
|
---|
8 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au>
|
---|
9 | *
|
---|
10 | * Changes to upstream version
|
---|
11 | * Remove checks for kernel version, assume kernel version 2.2.0 or better.
|
---|
12 | * Lines in the interfaces file cannot wrap.
|
---|
13 | * To adhere to the FHS, the default state file is /var/run/ifstate.
|
---|
14 | *
|
---|
15 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
---|
16 | */
|
---|
17 |
|
---|
18 | /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
|
---|
19 |
|
---|
20 | #include <sys/stat.h>
|
---|
21 | #include <sys/utsname.h>
|
---|
22 | #include <sys/wait.h>
|
---|
23 |
|
---|
24 | #include <ctype.h>
|
---|
25 | #include <errno.h>
|
---|
26 | #include <fcntl.h>
|
---|
27 | #include <fnmatch.h>
|
---|
28 | #include <getopt.h>
|
---|
29 | #include <stdarg.h>
|
---|
30 | #include <stdio.h>
|
---|
31 | #include <stdlib.h>
|
---|
32 | #include <string.h>
|
---|
33 | #include <unistd.h>
|
---|
34 |
|
---|
35 | #include "busybox.h"
|
---|
36 |
|
---|
37 | #define MAX_OPT_DEPTH 10
|
---|
38 | #define EUNBALBRACK 10001
|
---|
39 | #define EUNDEFVAR 10002
|
---|
40 | #define EUNBALPER 10000
|
---|
41 |
|
---|
42 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
43 | #define MAX_INTERFACE_LENGTH 10
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | #if 0
|
---|
47 | #define debug_noise(fmt, args...) printf(fmt, ## args)
|
---|
48 | #else
|
---|
49 | #define debug_noise(fmt, args...)
|
---|
50 | #endif
|
---|
51 |
|
---|
52 | /* Forward declaration */
|
---|
53 | struct interface_defn_t;
|
---|
54 |
|
---|
55 | typedef int (execfn)(char *command);
|
---|
56 |
|
---|
57 | struct method_t
|
---|
58 | {
|
---|
59 | char *name;
|
---|
60 | int (*up)(struct interface_defn_t *ifd, execfn *e);
|
---|
61 | int (*down)(struct interface_defn_t *ifd, execfn *e);
|
---|
62 | };
|
---|
63 |
|
---|
64 | struct address_family_t
|
---|
65 | {
|
---|
66 | char *name;
|
---|
67 | int n_methods;
|
---|
68 | struct method_t *method;
|
---|
69 | };
|
---|
70 |
|
---|
71 | struct mapping_defn_t
|
---|
72 | {
|
---|
73 | struct mapping_defn_t *next;
|
---|
74 |
|
---|
75 | int max_matches;
|
---|
76 | int n_matches;
|
---|
77 | char **match;
|
---|
78 |
|
---|
79 | char *script;
|
---|
80 |
|
---|
81 | int max_mappings;
|
---|
82 | int n_mappings;
|
---|
83 | char **mapping;
|
---|
84 | };
|
---|
85 |
|
---|
86 | struct variable_t
|
---|
87 | {
|
---|
88 | char *name;
|
---|
89 | char *value;
|
---|
90 | };
|
---|
91 |
|
---|
92 | struct interface_defn_t
|
---|
93 | {
|
---|
94 | struct address_family_t *address_family;
|
---|
95 | struct method_t *method;
|
---|
96 |
|
---|
97 | char *iface;
|
---|
98 | int max_options;
|
---|
99 | int n_options;
|
---|
100 | struct variable_t *option;
|
---|
101 | };
|
---|
102 |
|
---|
103 | struct interfaces_file_t
|
---|
104 | {
|
---|
105 | llist_t *autointerfaces;
|
---|
106 | llist_t *ifaces;
|
---|
107 | struct mapping_defn_t *mappings;
|
---|
108 | };
|
---|
109 |
|
---|
110 | static char no_act = 0;
|
---|
111 | static char verbose = 0;
|
---|
112 | static char **__myenviron = NULL;
|
---|
113 |
|
---|
114 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
|
---|
115 |
|
---|
116 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
117 |
|
---|
118 | static unsigned int count_bits(unsigned int a)
|
---|
119 | {
|
---|
120 | unsigned int result;
|
---|
121 | result = (a & 0x55) + ((a >> 1) & 0x55);
|
---|
122 | result = (result & 0x33) + ((result >> 2) & 0x33);
|
---|
123 | return((result & 0x0F) + ((result >> 4) & 0x0F));
|
---|
124 | }
|
---|
125 |
|
---|
126 | static int count_netmask_bits(char *dotted_quad)
|
---|
127 | {
|
---|
128 | unsigned int result, a, b, c, d;
|
---|
129 | /* Found a netmask... Check if it is dotted quad */
|
---|
130 | if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
|
---|
131 | return -1;
|
---|
132 | result = count_bits(a);
|
---|
133 | result += count_bits(b);
|
---|
134 | result += count_bits(c);
|
---|
135 | result += count_bits(d);
|
---|
136 | return ((int)result);
|
---|
137 | }
|
---|
138 | #endif
|
---|
139 |
|
---|
140 | static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
|
---|
141 | {
|
---|
142 | if (*pos + str_length >= *len) {
|
---|
143 | char *newbuf;
|
---|
144 |
|
---|
145 | newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
|
---|
146 | *buf = newbuf;
|
---|
147 | *len = *len * 2 + str_length + 1;
|
---|
148 | }
|
---|
149 |
|
---|
150 | while (str_length-- >= 1) {
|
---|
151 | (*buf)[(*pos)++] = *str;
|
---|
152 | str++;
|
---|
153 | }
|
---|
154 | (*buf)[*pos] = '\0';
|
---|
155 | }
|
---|
156 |
|
---|
157 | static int strncmpz(char *l, char *r, size_t llen)
|
---|
158 | {
|
---|
159 | int i = strncmp(l, r, llen);
|
---|
160 |
|
---|
161 | if (i == 0) {
|
---|
162 | return(-r[llen]);
|
---|
163 | } else {
|
---|
164 | return(i);
|
---|
165 | }
|
---|
166 | }
|
---|
167 |
|
---|
168 | static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
|
---|
169 | {
|
---|
170 | int i;
|
---|
171 |
|
---|
172 | if (strncmpz(id, "iface", idlen) == 0) {
|
---|
173 | char *result;
|
---|
174 | static char label_buf[20];
|
---|
175 | strncpy(label_buf, ifd->iface, 19);
|
---|
176 | label_buf[19]=0;
|
---|
177 | result = strchr(label_buf, ':');
|
---|
178 | if (result) {
|
---|
179 | *result=0;
|
---|
180 | }
|
---|
181 | return( label_buf);
|
---|
182 | } else if (strncmpz(id, "label", idlen) == 0) {
|
---|
183 | return (ifd->iface);
|
---|
184 | } else {
|
---|
185 | for (i = 0; i < ifd->n_options; i++) {
|
---|
186 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
|
---|
187 | return (ifd->option[i].value);
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | return(NULL);
|
---|
193 | }
|
---|
194 |
|
---|
195 | static char *parse(char *command, struct interface_defn_t *ifd)
|
---|
196 | {
|
---|
197 |
|
---|
198 | char *result = NULL;
|
---|
199 | size_t pos = 0, len = 0;
|
---|
200 | size_t old_pos[MAX_OPT_DEPTH] = { 0 };
|
---|
201 | int okay[MAX_OPT_DEPTH] = { 1 };
|
---|
202 | int opt_depth = 1;
|
---|
203 |
|
---|
204 | while (*command) {
|
---|
205 | switch (*command) {
|
---|
206 |
|
---|
207 | default:
|
---|
208 | addstr(&result, &len, &pos, command, 1);
|
---|
209 | command++;
|
---|
210 | break;
|
---|
211 | case '\\':
|
---|
212 | if (command[1]) {
|
---|
213 | addstr(&result, &len, &pos, command + 1, 1);
|
---|
214 | command += 2;
|
---|
215 | } else {
|
---|
216 | addstr(&result, &len, &pos, command, 1);
|
---|
217 | command++;
|
---|
218 | }
|
---|
219 | break;
|
---|
220 | case '[':
|
---|
221 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
|
---|
222 | old_pos[opt_depth] = pos;
|
---|
223 | okay[opt_depth] = 1;
|
---|
224 | opt_depth++;
|
---|
225 | command += 2;
|
---|
226 | } else {
|
---|
227 | addstr(&result, &len, &pos, "[", 1);
|
---|
228 | command++;
|
---|
229 | }
|
---|
230 | break;
|
---|
231 | case ']':
|
---|
232 | if (command[1] == ']' && opt_depth > 1) {
|
---|
233 | opt_depth--;
|
---|
234 | if (!okay[opt_depth]) {
|
---|
235 | pos = old_pos[opt_depth];
|
---|
236 | result[pos] = '\0';
|
---|
237 | }
|
---|
238 | command += 2;
|
---|
239 | } else {
|
---|
240 | addstr(&result, &len, &pos, "]", 1);
|
---|
241 | command++;
|
---|
242 | }
|
---|
243 | break;
|
---|
244 | case '%':
|
---|
245 | {
|
---|
246 | char *nextpercent;
|
---|
247 | char *varvalue;
|
---|
248 |
|
---|
249 | command++;
|
---|
250 | nextpercent = strchr(command, '%');
|
---|
251 | if (!nextpercent) {
|
---|
252 | errno = EUNBALPER;
|
---|
253 | free(result);
|
---|
254 | return (NULL);
|
---|
255 | }
|
---|
256 |
|
---|
257 | varvalue = get_var(command, nextpercent - command, ifd);
|
---|
258 |
|
---|
259 | if (varvalue) {
|
---|
260 | addstr(&result, &len, &pos, varvalue, strlen(varvalue));
|
---|
261 | } else {
|
---|
262 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
263 | /* Sigh... Add a special case for 'ip' to convert from
|
---|
264 | * dotted quad to bit count style netmasks. */
|
---|
265 | if (strncmp(command, "bnmask", 6)==0) {
|
---|
266 | int res;
|
---|
267 | varvalue = get_var("netmask", 7, ifd);
|
---|
268 | if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
|
---|
269 | char argument[255];
|
---|
270 | sprintf(argument, "%d", res);
|
---|
271 | addstr(&result, &len, &pos, argument, strlen(argument));
|
---|
272 | command = nextpercent + 1;
|
---|
273 | break;
|
---|
274 | }
|
---|
275 | }
|
---|
276 | #endif
|
---|
277 | okay[opt_depth - 1] = 0;
|
---|
278 | }
|
---|
279 |
|
---|
280 | command = nextpercent + 1;
|
---|
281 | }
|
---|
282 | break;
|
---|
283 | }
|
---|
284 | }
|
---|
285 |
|
---|
286 | if (opt_depth > 1) {
|
---|
287 | errno = EUNBALBRACK;
|
---|
288 | free(result);
|
---|
289 | return(NULL);
|
---|
290 | }
|
---|
291 |
|
---|
292 | if (!okay[0]) {
|
---|
293 | errno = EUNDEFVAR;
|
---|
294 | free(result);
|
---|
295 | return(NULL);
|
---|
296 | }
|
---|
297 |
|
---|
298 | return(result);
|
---|
299 | }
|
---|
300 |
|
---|
301 | static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
|
---|
302 | {
|
---|
303 | char *out;
|
---|
304 | int ret;
|
---|
305 |
|
---|
306 | out = parse(command, ifd);
|
---|
307 | if (!out) {
|
---|
308 | return(0);
|
---|
309 | }
|
---|
310 | ret = (*exec) (out);
|
---|
311 |
|
---|
312 | free(out);
|
---|
313 | if (ret != 1) {
|
---|
314 | return(0);
|
---|
315 | }
|
---|
316 | return(1);
|
---|
317 | }
|
---|
318 | #endif
|
---|
319 |
|
---|
320 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
|
---|
321 | static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
|
---|
322 | {
|
---|
323 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
324 | int result;
|
---|
325 | result =execute("ip addr add ::1 dev %iface%", ifd, exec);
|
---|
326 | result += execute("ip link set %iface% up", ifd, exec);
|
---|
327 | return ((result == 2) ? 2 : 0);
|
---|
328 | #else
|
---|
329 | return( execute("ifconfig %iface% add ::1", ifd, exec));
|
---|
330 | #endif
|
---|
331 | }
|
---|
332 |
|
---|
333 | static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
|
---|
334 | {
|
---|
335 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
336 | return(execute("ip link set %iface% down", ifd, exec));
|
---|
337 | #else
|
---|
338 | return(execute("ifconfig %iface% del ::1", ifd, exec));
|
---|
339 | #endif
|
---|
340 | }
|
---|
341 |
|
---|
342 | static int static_up6(struct interface_defn_t *ifd, execfn *exec)
|
---|
343 | {
|
---|
344 | int result;
|
---|
345 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
346 | result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
|
---|
347 | result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
|
---|
348 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
|
---|
349 | #else
|
---|
350 | result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
|
---|
351 | result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
|
---|
352 | result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
|
---|
353 | #endif
|
---|
354 | return ((result == 3) ? 3 : 0);
|
---|
355 | }
|
---|
356 |
|
---|
357 | static int static_down6(struct interface_defn_t *ifd, execfn *exec)
|
---|
358 | {
|
---|
359 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
360 | return(execute("ip link set %iface% down", ifd, exec));
|
---|
361 | #else
|
---|
362 | return(execute("ifconfig %iface% down", ifd, exec));
|
---|
363 | #endif
|
---|
364 | }
|
---|
365 |
|
---|
366 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
367 | static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
368 | {
|
---|
369 | int result;
|
---|
370 | result = execute("ip tunnel add %iface% mode sit remote "
|
---|
371 | "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
|
---|
372 | result += execute("ip link set %iface% up", ifd, exec);
|
---|
373 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
|
---|
374 | result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
|
---|
375 | return ((result == 4) ? 4 : 0);
|
---|
376 | }
|
---|
377 |
|
---|
378 | static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
|
---|
379 | {
|
---|
380 | return( execute("ip tunnel del %iface%", ifd, exec));
|
---|
381 | }
|
---|
382 | #endif
|
---|
383 |
|
---|
384 | static struct method_t methods6[] = {
|
---|
385 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
386 | { "v4tunnel", v4tunnel_up, v4tunnel_down, },
|
---|
387 | #endif
|
---|
388 | { "static", static_up6, static_down6, },
|
---|
389 | { "loopback", loopback_up6, loopback_down6, },
|
---|
390 | };
|
---|
391 |
|
---|
392 | static struct address_family_t addr_inet6 = {
|
---|
393 | "inet6",
|
---|
394 | sizeof(methods6) / sizeof(struct method_t),
|
---|
395 | methods6
|
---|
396 | };
|
---|
397 | #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
|
---|
398 |
|
---|
399 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
|
---|
400 | static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
401 | {
|
---|
402 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
403 | int result;
|
---|
404 | result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
|
---|
405 | result += execute("ip link set %iface% up", ifd, exec);
|
---|
406 | return ((result == 2) ? 2 : 0);
|
---|
407 | #else
|
---|
408 | return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
|
---|
409 | #endif
|
---|
410 | }
|
---|
411 |
|
---|
412 | static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
|
---|
413 | {
|
---|
414 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
415 | int result;
|
---|
416 | result = execute("ip addr flush dev %iface%", ifd, exec);
|
---|
417 | result += execute("ip link set %iface% down", ifd, exec);
|
---|
418 | return ((result == 2) ? 2 : 0);
|
---|
419 | #else
|
---|
420 | return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
|
---|
421 | #endif
|
---|
422 | }
|
---|
423 |
|
---|
424 | static int static_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
425 | {
|
---|
426 | int result;
|
---|
427 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
428 | result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
|
---|
429 | "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
|
---|
430 | result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
|
---|
431 | result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
|
---|
432 | return ((result == 3) ? 3 : 0);
|
---|
433 | #else
|
---|
434 | result = execute("ifconfig %iface% %address% netmask %netmask% "
|
---|
435 | "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
|
---|
436 | "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
|
---|
437 | ifd, exec);
|
---|
438 | result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
|
---|
439 | return ((result == 2) ? 2 : 0);
|
---|
440 | #endif
|
---|
441 | }
|
---|
442 |
|
---|
443 | static int static_down(struct interface_defn_t *ifd, execfn *exec)
|
---|
444 | {
|
---|
445 | int result;
|
---|
446 | #ifdef CONFIG_FEATURE_IFUPDOWN_IP
|
---|
447 | result = execute("ip addr flush dev %iface%", ifd, exec);
|
---|
448 | result += execute("ip link set %iface% down", ifd, exec);
|
---|
449 | #else
|
---|
450 | result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
|
---|
451 | result += execute("ifconfig %iface% down", ifd, exec);
|
---|
452 | #endif
|
---|
453 | return ((result == 2) ? 2 : 0);
|
---|
454 | }
|
---|
455 |
|
---|
456 | static int execable(char *program)
|
---|
457 | {
|
---|
458 | struct stat buf;
|
---|
459 | if (0 == stat(program, &buf)) {
|
---|
460 | if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
|
---|
461 | return(1);
|
---|
462 | }
|
---|
463 | }
|
---|
464 | return(0);
|
---|
465 | }
|
---|
466 |
|
---|
467 | static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
468 | {
|
---|
469 | if (execable("/sbin/udhcpc")) {
|
---|
470 | return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
|
---|
471 | "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
|
---|
472 | } else if (execable("/sbin/pump")) {
|
---|
473 | return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
|
---|
474 | } else if (execable("/sbin/dhclient")) {
|
---|
475 | return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
|
---|
476 | } else if (execable("/sbin/dhcpcd")) {
|
---|
477 | return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
|
---|
478 | "[[-l %leasetime%]] %iface%", ifd, exec));
|
---|
479 | }
|
---|
480 | return(0);
|
---|
481 | }
|
---|
482 |
|
---|
483 | static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
|
---|
484 | {
|
---|
485 | int result = 0;
|
---|
486 | if (execable("/sbin/udhcpc")) {
|
---|
487 | /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
|
---|
488 | * and SIGTERM causes udhcpc to exit. Signals are queued and processed
|
---|
489 | * sequentially so we don't need to sleep */
|
---|
490 | result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
|
---|
491 | result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
|
---|
492 | } else if (execable("/sbin/pump")) {
|
---|
493 | result = execute("pump -i %iface% -k", ifd, exec);
|
---|
494 | } else if (execable("/sbin/dhclient")) {
|
---|
495 | result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
|
---|
496 | } else if (execable("/sbin/dhcpcd")) {
|
---|
497 | result = execute("dhcpcd -k %iface%", ifd, exec);
|
---|
498 | }
|
---|
499 | return (result || static_down(ifd, exec));
|
---|
500 | }
|
---|
501 |
|
---|
502 | static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
503 | {
|
---|
504 | return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
|
---|
505 | "[[--server %server%]] [[--hwaddr %hwaddr%]] "
|
---|
506 | "--returniffail --serverbcast", ifd, exec));
|
---|
507 | }
|
---|
508 |
|
---|
509 | static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
510 | {
|
---|
511 | return( execute("pon [[%provider%]]", ifd, exec));
|
---|
512 | }
|
---|
513 |
|
---|
514 | static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
|
---|
515 | {
|
---|
516 | return( execute("poff [[%provider%]]", ifd, exec));
|
---|
517 | }
|
---|
518 |
|
---|
519 | static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
|
---|
520 | {
|
---|
521 | return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
|
---|
522 | "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
|
---|
523 | }
|
---|
524 |
|
---|
525 | static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
|
---|
526 | {
|
---|
527 | return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
|
---|
528 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
|
---|
529 | }
|
---|
530 |
|
---|
531 | static struct method_t methods[] =
|
---|
532 | {
|
---|
533 | { "wvdial", wvdial_up, wvdial_down, },
|
---|
534 | { "ppp", ppp_up, ppp_down, },
|
---|
535 | { "static", static_up, static_down, },
|
---|
536 | { "bootp", bootp_up, static_down, },
|
---|
537 | { "dhcp", dhcp_up, dhcp_down, },
|
---|
538 | { "loopback", loopback_up, loopback_down, },
|
---|
539 | };
|
---|
540 |
|
---|
541 | static struct address_family_t addr_inet =
|
---|
542 | {
|
---|
543 | "inet",
|
---|
544 | sizeof(methods) / sizeof(struct method_t),
|
---|
545 | methods
|
---|
546 | };
|
---|
547 |
|
---|
548 | #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
|
---|
549 |
|
---|
550 | static char *next_word(char **buf)
|
---|
551 | {
|
---|
552 | unsigned short length;
|
---|
553 | char *word;
|
---|
554 |
|
---|
555 | if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
|
---|
556 | return NULL;
|
---|
557 | }
|
---|
558 |
|
---|
559 | /* Skip over leading whitespace */
|
---|
560 | word = *buf;
|
---|
561 | while (isspace(*word)) {
|
---|
562 | ++word;
|
---|
563 | }
|
---|
564 |
|
---|
565 | /* Skip over comments */
|
---|
566 | if (*word == '#') {
|
---|
567 | return(NULL);
|
---|
568 | }
|
---|
569 |
|
---|
570 | /* Find the length of this word */
|
---|
571 | length = strcspn(word, " \t\n");
|
---|
572 | if (length == 0) {
|
---|
573 | return(NULL);
|
---|
574 | }
|
---|
575 | *buf = word + length;
|
---|
576 | /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
|
---|
577 | if (**buf) {
|
---|
578 | **buf = '\0';
|
---|
579 | (*buf)++;
|
---|
580 | }
|
---|
581 |
|
---|
582 | return word;
|
---|
583 | }
|
---|
584 |
|
---|
585 | static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
|
---|
586 | {
|
---|
587 | int i;
|
---|
588 |
|
---|
589 | for (i = 0; af[i]; i++) {
|
---|
590 | if (strcmp(af[i]->name, name) == 0) {
|
---|
591 | return af[i];
|
---|
592 | }
|
---|
593 | }
|
---|
594 | return NULL;
|
---|
595 | }
|
---|
596 |
|
---|
597 | static struct method_t *get_method(struct address_family_t *af, char *name)
|
---|
598 | {
|
---|
599 | int i;
|
---|
600 |
|
---|
601 | for (i = 0; i < af->n_methods; i++) {
|
---|
602 | if (strcmp(af->method[i].name, name) == 0) {
|
---|
603 | return &af->method[i];
|
---|
604 | }
|
---|
605 | }
|
---|
606 | return(NULL);
|
---|
607 | }
|
---|
608 |
|
---|
609 | static const llist_t *find_list_string(const llist_t *list, const char *string)
|
---|
610 | {
|
---|
611 | while (list) {
|
---|
612 | if (strcmp(list->data, string) == 0) {
|
---|
613 | return(list);
|
---|
614 | }
|
---|
615 | list = list->link;
|
---|
616 | }
|
---|
617 | return(NULL);
|
---|
618 | }
|
---|
619 |
|
---|
620 | static struct interfaces_file_t *read_interfaces(const char *filename)
|
---|
621 | {
|
---|
622 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
623 | struct mapping_defn_t *currmap = NULL;
|
---|
624 | #endif
|
---|
625 | struct interface_defn_t *currif = NULL;
|
---|
626 | struct interfaces_file_t *defn;
|
---|
627 | FILE *f;
|
---|
628 | char *firstword;
|
---|
629 | char *buf;
|
---|
630 |
|
---|
631 | enum { NONE, IFACE, MAPPING } currently_processing = NONE;
|
---|
632 |
|
---|
633 | defn = xzalloc(sizeof(struct interfaces_file_t));
|
---|
634 |
|
---|
635 | f = bb_xfopen(filename, "r");
|
---|
636 |
|
---|
637 | while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
|
---|
638 | char *buf_ptr = buf;
|
---|
639 |
|
---|
640 | firstword = next_word(&buf_ptr);
|
---|
641 | if (firstword == NULL) {
|
---|
642 | free(buf);
|
---|
643 | continue; /* blank line */
|
---|
644 | }
|
---|
645 |
|
---|
646 | if (strcmp(firstword, "mapping") == 0) {
|
---|
647 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
648 | currmap = xzalloc(sizeof(struct mapping_defn_t));
|
---|
649 |
|
---|
650 | while ((firstword = next_word(&buf_ptr)) != NULL) {
|
---|
651 | if (currmap->max_matches == currmap->n_matches) {
|
---|
652 | currmap->max_matches = currmap->max_matches * 2 + 1;
|
---|
653 | currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
|
---|
654 | }
|
---|
655 |
|
---|
656 | currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
|
---|
657 | }
|
---|
658 | currmap->max_mappings = 0;
|
---|
659 | currmap->n_mappings = 0;
|
---|
660 | currmap->mapping = NULL;
|
---|
661 | currmap->script = NULL;
|
---|
662 | {
|
---|
663 | struct mapping_defn_t **where = &defn->mappings;
|
---|
664 | while (*where != NULL) {
|
---|
665 | where = &(*where)->next;
|
---|
666 | }
|
---|
667 | *where = currmap;
|
---|
668 | currmap->next = NULL;
|
---|
669 | }
|
---|
670 | debug_noise("Added mapping\n");
|
---|
671 | #endif
|
---|
672 | currently_processing = MAPPING;
|
---|
673 | } else if (strcmp(firstword, "iface") == 0) {
|
---|
674 | {
|
---|
675 | char *iface_name;
|
---|
676 | char *address_family_name;
|
---|
677 | char *method_name;
|
---|
678 | struct address_family_t *addr_fams[] = {
|
---|
679 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
|
---|
680 | &addr_inet,
|
---|
681 | #endif
|
---|
682 | #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
|
---|
683 | &addr_inet6,
|
---|
684 | #endif
|
---|
685 | NULL
|
---|
686 | };
|
---|
687 |
|
---|
688 | currif = xzalloc(sizeof(struct interface_defn_t));
|
---|
689 | iface_name = next_word(&buf_ptr);
|
---|
690 | address_family_name = next_word(&buf_ptr);
|
---|
691 | method_name = next_word(&buf_ptr);
|
---|
692 |
|
---|
693 | if (buf_ptr == NULL) {
|
---|
694 | bb_error_msg("too few parameters for line \"%s\"", buf);
|
---|
695 | return NULL;
|
---|
696 | }
|
---|
697 |
|
---|
698 | /* ship any trailing whitespace */
|
---|
699 | while (isspace(*buf_ptr)) {
|
---|
700 | ++buf_ptr;
|
---|
701 | }
|
---|
702 |
|
---|
703 | if (buf_ptr[0] != '\0') {
|
---|
704 | bb_error_msg("too many parameters \"%s\"", buf);
|
---|
705 | return NULL;
|
---|
706 | }
|
---|
707 |
|
---|
708 | currif->iface = bb_xstrdup(iface_name);
|
---|
709 |
|
---|
710 | currif->address_family = get_address_family(addr_fams, address_family_name);
|
---|
711 | if (!currif->address_family) {
|
---|
712 | bb_error_msg("unknown address type \"%s\"", address_family_name);
|
---|
713 | return NULL;
|
---|
714 | }
|
---|
715 |
|
---|
716 | currif->method = get_method(currif->address_family, method_name);
|
---|
717 | if (!currif->method) {
|
---|
718 | bb_error_msg("unknown method \"%s\"", method_name);
|
---|
719 | return NULL;
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | {
|
---|
724 | llist_t *iface_list;
|
---|
725 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
|
---|
726 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
|
---|
727 | if ((strcmp(tmp->iface, currif->iface) == 0) &&
|
---|
728 | (tmp->address_family == currif->address_family)) {
|
---|
729 | bb_error_msg("duplicate interface \"%s\"", tmp->iface);
|
---|
730 | return NULL;
|
---|
731 | }
|
---|
732 | }
|
---|
733 |
|
---|
734 | llist_add_to_end(&(defn->ifaces), (char*)currif);
|
---|
735 | }
|
---|
736 | debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
|
---|
737 | }
|
---|
738 | currently_processing = IFACE;
|
---|
739 | } else if (strcmp(firstword, "auto") == 0) {
|
---|
740 | while ((firstword = next_word(&buf_ptr)) != NULL) {
|
---|
741 |
|
---|
742 | /* Check the interface isnt already listed */
|
---|
743 | if (find_list_string(defn->autointerfaces, firstword)) {
|
---|
744 | bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
|
---|
745 | }
|
---|
746 |
|
---|
747 | /* Add the interface to the list */
|
---|
748 | llist_add_to_end(&(defn->autointerfaces), bb_xstrdup(firstword));
|
---|
749 | debug_noise("\nauto %s\n", firstword);
|
---|
750 | }
|
---|
751 | currently_processing = NONE;
|
---|
752 | } else {
|
---|
753 | switch (currently_processing) {
|
---|
754 | case IFACE:
|
---|
755 | {
|
---|
756 | int i;
|
---|
757 |
|
---|
758 | if (strlen(buf_ptr) == 0) {
|
---|
759 | bb_error_msg("option with empty value \"%s\"", buf);
|
---|
760 | return NULL;
|
---|
761 | }
|
---|
762 |
|
---|
763 | if (strcmp(firstword, "up") != 0
|
---|
764 | && strcmp(firstword, "down") != 0
|
---|
765 | && strcmp(firstword, "pre-up") != 0
|
---|
766 | && strcmp(firstword, "post-down") != 0) {
|
---|
767 | for (i = 0; i < currif->n_options; i++) {
|
---|
768 | if (strcmp(currif->option[i].name, firstword) == 0) {
|
---|
769 | bb_error_msg("duplicate option \"%s\"", buf);
|
---|
770 | return NULL;
|
---|
771 | }
|
---|
772 | }
|
---|
773 | }
|
---|
774 | }
|
---|
775 | if (currif->n_options >= currif->max_options) {
|
---|
776 | struct variable_t *opt;
|
---|
777 |
|
---|
778 | currif->max_options = currif->max_options + 10;
|
---|
779 | opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
|
---|
780 | currif->option = opt;
|
---|
781 | }
|
---|
782 | currif->option[currif->n_options].name = bb_xstrdup(firstword);
|
---|
783 | currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
|
---|
784 | if (!currif->option[currif->n_options].name) {
|
---|
785 | perror(filename);
|
---|
786 | return NULL;
|
---|
787 | }
|
---|
788 | if (!currif->option[currif->n_options].value) {
|
---|
789 | perror(filename);
|
---|
790 | return NULL;
|
---|
791 | }
|
---|
792 | debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
|
---|
793 | currif->option[currif->n_options].value);
|
---|
794 | currif->n_options++;
|
---|
795 | break;
|
---|
796 | case MAPPING:
|
---|
797 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
798 | if (strcmp(firstword, "script") == 0) {
|
---|
799 | if (currmap->script != NULL) {
|
---|
800 | bb_error_msg("duplicate script in mapping \"%s\"", buf);
|
---|
801 | return NULL;
|
---|
802 | } else {
|
---|
803 | currmap->script = bb_xstrdup(next_word(&buf_ptr));
|
---|
804 | }
|
---|
805 | } else if (strcmp(firstword, "map") == 0) {
|
---|
806 | if (currmap->max_mappings == currmap->n_mappings) {
|
---|
807 | currmap->max_mappings = currmap->max_mappings * 2 + 1;
|
---|
808 | currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
|
---|
809 | }
|
---|
810 | currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
|
---|
811 | currmap->n_mappings++;
|
---|
812 | } else {
|
---|
813 | bb_error_msg("misplaced option \"%s\"", buf);
|
---|
814 | return NULL;
|
---|
815 | }
|
---|
816 | #endif
|
---|
817 | break;
|
---|
818 | case NONE:
|
---|
819 | default:
|
---|
820 | bb_error_msg("misplaced option \"%s\"", buf);
|
---|
821 | return NULL;
|
---|
822 | }
|
---|
823 | }
|
---|
824 | free(buf);
|
---|
825 | }
|
---|
826 | if (ferror(f) != 0) {
|
---|
827 | bb_perror_msg_and_die("%s", filename);
|
---|
828 | }
|
---|
829 | fclose(f);
|
---|
830 |
|
---|
831 | return defn;
|
---|
832 | }
|
---|
833 |
|
---|
834 | static char *setlocalenv(char *format, const char *name, const char *value)
|
---|
835 | {
|
---|
836 | char *result;
|
---|
837 | char *here;
|
---|
838 | char *there;
|
---|
839 |
|
---|
840 | result = bb_xasprintf(format, name, value);
|
---|
841 |
|
---|
842 | for (here = there = result; *there != '=' && *there; there++) {
|
---|
843 | if (*there == '-')
|
---|
844 | *there = '_';
|
---|
845 | if (isalpha(*there))
|
---|
846 | *there = toupper(*there);
|
---|
847 |
|
---|
848 | if (isalnum(*there) || *there == '_') {
|
---|
849 | *here = *there;
|
---|
850 | here++;
|
---|
851 | }
|
---|
852 | }
|
---|
853 | memmove(here, there, strlen(there) + 1);
|
---|
854 |
|
---|
855 | return result;
|
---|
856 | }
|
---|
857 |
|
---|
858 | static void set_environ(struct interface_defn_t *iface, const char *mode)
|
---|
859 | {
|
---|
860 | char **environend;
|
---|
861 | int i;
|
---|
862 | const int n_env_entries = iface->n_options + 5;
|
---|
863 | char **ppch;
|
---|
864 |
|
---|
865 | if (__myenviron != NULL) {
|
---|
866 | for (ppch = __myenviron; *ppch; ppch++) {
|
---|
867 | free(*ppch);
|
---|
868 | *ppch = NULL;
|
---|
869 | }
|
---|
870 | free(__myenviron);
|
---|
871 | }
|
---|
872 | __myenviron = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
|
---|
873 | environend = __myenviron;
|
---|
874 |
|
---|
875 | for (i = 0; i < iface->n_options; i++) {
|
---|
876 | if (strcmp(iface->option[i].name, "up") == 0
|
---|
877 | || strcmp(iface->option[i].name, "down") == 0
|
---|
878 | || strcmp(iface->option[i].name, "pre-up") == 0
|
---|
879 | || strcmp(iface->option[i].name, "post-down") == 0) {
|
---|
880 | continue;
|
---|
881 | }
|
---|
882 | *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
|
---|
883 | }
|
---|
884 |
|
---|
885 | *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
|
---|
886 | *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
|
---|
887 | *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
|
---|
888 | *(environend++) = setlocalenv("%s=%s", "MODE", mode);
|
---|
889 | *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
|
---|
890 | }
|
---|
891 |
|
---|
892 | static int doit(char *str)
|
---|
893 | {
|
---|
894 | if (verbose || no_act) {
|
---|
895 | printf("%s\n", str);
|
---|
896 | }
|
---|
897 | if (!no_act) {
|
---|
898 | pid_t child;
|
---|
899 | int status;
|
---|
900 |
|
---|
901 | fflush(NULL);
|
---|
902 | switch (child = fork()) {
|
---|
903 | case -1: /* failure */
|
---|
904 | return 0;
|
---|
905 | case 0: /* child */
|
---|
906 | execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
|
---|
907 | exit(127);
|
---|
908 | }
|
---|
909 | waitpid(child, &status, 0);
|
---|
910 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
---|
911 | return 0;
|
---|
912 | }
|
---|
913 | }
|
---|
914 | return (1);
|
---|
915 | }
|
---|
916 |
|
---|
917 | static int execute_all(struct interface_defn_t *ifd, const char *opt)
|
---|
918 | {
|
---|
919 | int i;
|
---|
920 | char *buf;
|
---|
921 | for (i = 0; i < ifd->n_options; i++) {
|
---|
922 | if (strcmp(ifd->option[i].name, opt) == 0) {
|
---|
923 | if (!doit(ifd->option[i].value)) {
|
---|
924 | return 0;
|
---|
925 | }
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | buf = bb_xasprintf("run-parts /etc/network/if-%s.d", opt);
|
---|
930 | if (doit(buf) != 1) {
|
---|
931 | return 0;
|
---|
932 | }
|
---|
933 | return 1;
|
---|
934 | }
|
---|
935 |
|
---|
936 | static int check(char *str) {
|
---|
937 | return str != NULL;
|
---|
938 | }
|
---|
939 |
|
---|
940 | static int iface_up(struct interface_defn_t *iface)
|
---|
941 | {
|
---|
942 | if (!iface->method->up(iface,check)) return -1;
|
---|
943 | set_environ(iface, "start");
|
---|
944 | if (!execute_all(iface, "pre-up")) return 0;
|
---|
945 | if (!iface->method->up(iface, doit)) return 0;
|
---|
946 | if (!execute_all(iface, "up")) return 0;
|
---|
947 | return 1;
|
---|
948 | }
|
---|
949 |
|
---|
950 | static int iface_down(struct interface_defn_t *iface)
|
---|
951 | {
|
---|
952 | if (!iface->method->down(iface,check)) return -1;
|
---|
953 | set_environ(iface, "stop");
|
---|
954 | if (!execute_all(iface, "down")) return 0;
|
---|
955 | if (!iface->method->down(iface, doit)) return 0;
|
---|
956 | if (!execute_all(iface, "post-down")) return 0;
|
---|
957 | return 1;
|
---|
958 | }
|
---|
959 |
|
---|
960 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
961 | static int popen2(FILE **in, FILE **out, char *command, ...)
|
---|
962 | {
|
---|
963 | va_list ap;
|
---|
964 | char *argv[11] = { command };
|
---|
965 | int argc;
|
---|
966 | int infd[2], outfd[2];
|
---|
967 | pid_t pid;
|
---|
968 |
|
---|
969 | argc = 1;
|
---|
970 | va_start(ap, command);
|
---|
971 | while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
|
---|
972 | argc++;
|
---|
973 | }
|
---|
974 | argv[argc] = NULL; /* make sure */
|
---|
975 | va_end(ap);
|
---|
976 |
|
---|
977 | if (pipe(infd) != 0) {
|
---|
978 | return 0;
|
---|
979 | }
|
---|
980 |
|
---|
981 | if (pipe(outfd) != 0) {
|
---|
982 | close(infd[0]);
|
---|
983 | close(infd[1]);
|
---|
984 | return 0;
|
---|
985 | }
|
---|
986 |
|
---|
987 | fflush(NULL);
|
---|
988 | switch (pid = fork()) {
|
---|
989 | case -1: /* failure */
|
---|
990 | close(infd[0]);
|
---|
991 | close(infd[1]);
|
---|
992 | close(outfd[0]);
|
---|
993 | close(outfd[1]);
|
---|
994 | return 0;
|
---|
995 | case 0: /* child */
|
---|
996 | dup2(infd[0], 0);
|
---|
997 | dup2(outfd[1], 1);
|
---|
998 | close(infd[0]);
|
---|
999 | close(infd[1]);
|
---|
1000 | close(outfd[0]);
|
---|
1001 | close(outfd[1]);
|
---|
1002 | execvp(command, argv);
|
---|
1003 | exit(127);
|
---|
1004 | default: /* parent */
|
---|
1005 | *in = fdopen(infd[1], "w");
|
---|
1006 | *out = fdopen(outfd[0], "r");
|
---|
1007 | close(infd[0]);
|
---|
1008 | close(outfd[1]);
|
---|
1009 | return pid;
|
---|
1010 | }
|
---|
1011 | /* unreached */
|
---|
1012 | }
|
---|
1013 |
|
---|
1014 | static char *run_mapping(char *physical, struct mapping_defn_t * map)
|
---|
1015 | {
|
---|
1016 | FILE *in, *out;
|
---|
1017 | int i, status;
|
---|
1018 | pid_t pid;
|
---|
1019 |
|
---|
1020 | char *logical = bb_xstrdup(physical);
|
---|
1021 |
|
---|
1022 | /* Run the mapping script. */
|
---|
1023 | pid = popen2(&in, &out, map->script, physical, NULL);
|
---|
1024 |
|
---|
1025 | /* popen2() returns 0 on failure. */
|
---|
1026 | if (pid == 0)
|
---|
1027 | return logical;
|
---|
1028 |
|
---|
1029 | /* Write mappings to stdin of mapping script. */
|
---|
1030 | for (i = 0; i < map->n_mappings; i++) {
|
---|
1031 | fprintf(in, "%s\n", map->mapping[i]);
|
---|
1032 | }
|
---|
1033 | fclose(in);
|
---|
1034 | waitpid(pid, &status, 0);
|
---|
1035 |
|
---|
1036 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
|
---|
1037 | /* If the mapping script exited successfully, try to
|
---|
1038 | * grab a line of output and use that as the name of the
|
---|
1039 | * logical interface. */
|
---|
1040 | char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
|
---|
1041 |
|
---|
1042 | if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
|
---|
1043 | /* If we are able to read a line of output from the script,
|
---|
1044 | * remove any trailing whitespace and use this value
|
---|
1045 | * as the name of the logical interface. */
|
---|
1046 | char *pch = new_logical + strlen(new_logical) - 1;
|
---|
1047 |
|
---|
1048 | while (pch >= new_logical && isspace(*pch))
|
---|
1049 | *(pch--) = '\0';
|
---|
1050 |
|
---|
1051 | free(logical);
|
---|
1052 | logical = new_logical;
|
---|
1053 | } else {
|
---|
1054 | /* If we are UNABLE to read a line of output, discard our
|
---|
1055 | * freshly allocated memory. */
|
---|
1056 | free(new_logical);
|
---|
1057 | }
|
---|
1058 | }
|
---|
1059 |
|
---|
1060 | fclose(out);
|
---|
1061 |
|
---|
1062 | return logical;
|
---|
1063 | }
|
---|
1064 | #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
|
---|
1065 |
|
---|
1066 | static llist_t *find_iface_state(llist_t *state_list, const char *iface)
|
---|
1067 | {
|
---|
1068 | unsigned short iface_len = strlen(iface);
|
---|
1069 | llist_t *search = state_list;
|
---|
1070 |
|
---|
1071 | while (search) {
|
---|
1072 | if ((strncmp(search->data, iface, iface_len) == 0) &&
|
---|
1073 | (search->data[iface_len] == '=')) {
|
---|
1074 | return(search);
|
---|
1075 | }
|
---|
1076 | search = search->link;
|
---|
1077 | }
|
---|
1078 | return(NULL);
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | int ifupdown_main(int argc, char **argv)
|
---|
1082 | {
|
---|
1083 | int (*cmds) (struct interface_defn_t *) = NULL;
|
---|
1084 | struct interfaces_file_t *defn;
|
---|
1085 | llist_t *state_list = NULL;
|
---|
1086 | llist_t *target_list = NULL;
|
---|
1087 | const char *interfaces = "/etc/network/interfaces";
|
---|
1088 | const char *statefile = "/var/run/ifstate";
|
---|
1089 |
|
---|
1090 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
1091 | int run_mappings = 1;
|
---|
1092 | #endif
|
---|
1093 | int do_all = 0;
|
---|
1094 | int force = 0;
|
---|
1095 | int any_failures = 0;
|
---|
1096 | int i;
|
---|
1097 |
|
---|
1098 | if (bb_applet_name[2] == 'u') {
|
---|
1099 | /* ifup command */
|
---|
1100 | cmds = iface_up;
|
---|
1101 | } else {
|
---|
1102 | /* ifdown command */
|
---|
1103 | cmds = iface_down;
|
---|
1104 | }
|
---|
1105 |
|
---|
1106 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
1107 | while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
|
---|
1108 | #else
|
---|
1109 | while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
|
---|
1110 | #endif
|
---|
1111 | {
|
---|
1112 | switch (i) {
|
---|
1113 | case 'i': /* interfaces */
|
---|
1114 | interfaces = optarg;
|
---|
1115 | break;
|
---|
1116 | case 'v': /* verbose */
|
---|
1117 | verbose = 1;
|
---|
1118 | break;
|
---|
1119 | case 'a': /* all */
|
---|
1120 | do_all = 1;
|
---|
1121 | break;
|
---|
1122 | case 'n': /* no-act */
|
---|
1123 | no_act = 1;
|
---|
1124 | break;
|
---|
1125 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
1126 | case 'm': /* no-mappings */
|
---|
1127 | run_mappings = 0;
|
---|
1128 | break;
|
---|
1129 | #endif
|
---|
1130 | case 'f': /* force */
|
---|
1131 | force = 1;
|
---|
1132 | break;
|
---|
1133 | default:
|
---|
1134 | bb_show_usage();
|
---|
1135 | break;
|
---|
1136 | }
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | if (argc - optind > 0) {
|
---|
1140 | if (do_all) {
|
---|
1141 | bb_show_usage();
|
---|
1142 | }
|
---|
1143 | } else {
|
---|
1144 | if (!do_all) {
|
---|
1145 | bb_show_usage();
|
---|
1146 | }
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | debug_noise("reading %s file:\n", interfaces);
|
---|
1150 | defn = read_interfaces(interfaces);
|
---|
1151 | debug_noise("\ndone reading %s\n\n", interfaces);
|
---|
1152 |
|
---|
1153 | if (!defn) {
|
---|
1154 | exit(EXIT_FAILURE);
|
---|
1155 | }
|
---|
1156 |
|
---|
1157 | /* Create a list of interfaces to work on */
|
---|
1158 | if (do_all) {
|
---|
1159 | if (cmds == iface_up) {
|
---|
1160 | target_list = defn->autointerfaces;
|
---|
1161 | } else {
|
---|
1162 | /* iface_down */
|
---|
1163 | const llist_t *list = state_list;
|
---|
1164 | while (list) {
|
---|
1165 | llist_add_to_end(&target_list, bb_xstrdup(list->data));
|
---|
1166 | list = list->link;
|
---|
1167 | }
|
---|
1168 | target_list = defn->autointerfaces;
|
---|
1169 | }
|
---|
1170 | } else {
|
---|
1171 | llist_add_to_end(&target_list, argv[optind]);
|
---|
1172 | }
|
---|
1173 |
|
---|
1174 |
|
---|
1175 | /* Update the interfaces */
|
---|
1176 | while (target_list) {
|
---|
1177 | llist_t *iface_list;
|
---|
1178 | struct interface_defn_t *currif;
|
---|
1179 | char *iface;
|
---|
1180 | char *liface;
|
---|
1181 | char *pch;
|
---|
1182 | int okay = 0;
|
---|
1183 | int cmds_ret;
|
---|
1184 |
|
---|
1185 | iface = bb_xstrdup(target_list->data);
|
---|
1186 | target_list = target_list->link;
|
---|
1187 |
|
---|
1188 | pch = strchr(iface, '=');
|
---|
1189 | if (pch) {
|
---|
1190 | *pch = '\0';
|
---|
1191 | liface = bb_xstrdup(pch + 1);
|
---|
1192 | } else {
|
---|
1193 | liface = bb_xstrdup(iface);
|
---|
1194 | }
|
---|
1195 |
|
---|
1196 | if (!force) {
|
---|
1197 | const llist_t *iface_state = find_iface_state(state_list, iface);
|
---|
1198 |
|
---|
1199 | if (cmds == iface_up) {
|
---|
1200 | /* ifup */
|
---|
1201 | if (iface_state) {
|
---|
1202 | bb_error_msg("interface %s already configured", iface);
|
---|
1203 | continue;
|
---|
1204 | }
|
---|
1205 | } else {
|
---|
1206 | /* ifdown */
|
---|
1207 | if (iface_state) {
|
---|
1208 | bb_error_msg("interface %s not configured", iface);
|
---|
1209 | continue;
|
---|
1210 | }
|
---|
1211 | }
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
|
---|
1215 | if ((cmds == iface_up) && run_mappings) {
|
---|
1216 | struct mapping_defn_t *currmap;
|
---|
1217 |
|
---|
1218 | for (currmap = defn->mappings; currmap; currmap = currmap->next) {
|
---|
1219 |
|
---|
1220 | for (i = 0; i < currmap->n_matches; i++) {
|
---|
1221 | if (fnmatch(currmap->match[i], liface, 0) != 0)
|
---|
1222 | continue;
|
---|
1223 | if (verbose) {
|
---|
1224 | printf("Running mapping script %s on %s\n", currmap->script, liface);
|
---|
1225 | }
|
---|
1226 | liface = run_mapping(iface, currmap);
|
---|
1227 | break;
|
---|
1228 | }
|
---|
1229 | }
|
---|
1230 | }
|
---|
1231 | #endif
|
---|
1232 |
|
---|
1233 |
|
---|
1234 | iface_list = defn->ifaces;
|
---|
1235 | while (iface_list) {
|
---|
1236 | currif = (struct interface_defn_t *) iface_list->data;
|
---|
1237 | if (strcmp(liface, currif->iface) == 0) {
|
---|
1238 | char *oldiface = currif->iface;
|
---|
1239 |
|
---|
1240 | okay = 1;
|
---|
1241 | currif->iface = iface;
|
---|
1242 |
|
---|
1243 | debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
|
---|
1244 |
|
---|
1245 | /* Call the cmds function pointer, does either iface_up() or iface_down() */
|
---|
1246 | cmds_ret = cmds(currif);
|
---|
1247 | if (cmds_ret == -1) {
|
---|
1248 | bb_error_msg("Don't seem to have all the variables for %s/%s.",
|
---|
1249 | liface, currif->address_family->name);
|
---|
1250 | any_failures += 1;
|
---|
1251 | } else if (cmds_ret == 0) {
|
---|
1252 | any_failures += 1;
|
---|
1253 | }
|
---|
1254 |
|
---|
1255 | currif->iface = oldiface;
|
---|
1256 | }
|
---|
1257 | iface_list = iface_list->link;
|
---|
1258 | }
|
---|
1259 | if (verbose) {
|
---|
1260 | printf("\n");
|
---|
1261 | }
|
---|
1262 |
|
---|
1263 | if (!okay && !force) {
|
---|
1264 | bb_error_msg("Ignoring unknown interface %s", liface);
|
---|
1265 | any_failures += 1;
|
---|
1266 | } else {
|
---|
1267 | llist_t *iface_state = find_iface_state(state_list, iface);
|
---|
1268 |
|
---|
1269 | if (cmds == iface_up) {
|
---|
1270 | char *newiface = bb_xasprintf("%s=%s", iface, liface);
|
---|
1271 | if (iface_state == NULL) {
|
---|
1272 | llist_add_to_end(&state_list, newiface);
|
---|
1273 | } else {
|
---|
1274 | free(iface_state->data);
|
---|
1275 | iface_state->data = newiface;
|
---|
1276 | }
|
---|
1277 | } else {
|
---|
1278 | /* Remove an interface from the linked list */
|
---|
1279 | free(llist_pop(&iface_state));
|
---|
1280 | }
|
---|
1281 | }
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | /* Actually write the new state */
|
---|
1285 | if (!no_act) {
|
---|
1286 | FILE *state_fp = NULL;
|
---|
1287 |
|
---|
1288 | state_fp = bb_xfopen(statefile, "w");
|
---|
1289 | while (state_list) {
|
---|
1290 | if (state_list->data) {
|
---|
1291 | fputs(state_list->data, state_fp);
|
---|
1292 | fputc('\n', state_fp);
|
---|
1293 | }
|
---|
1294 | state_list = state_list->link;
|
---|
1295 | }
|
---|
1296 | fclose(state_fp);
|
---|
1297 | }
|
---|
1298 |
|
---|
1299 | if (any_failures)
|
---|
1300 | return 1;
|
---|
1301 | return 0;
|
---|
1302 | }
|
---|