source: MondoRescue/branches/3.0/mindi-busybox/networking/ifupdown.c@ 3085

Last change on this file since 3085 was 2725, checked in by Bruno Cornec, 13 years ago
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File size: 34.2 KB
Line 
1/* vi: set sw=4 ts=4: */
2/*
3 * ifupdown for busybox
4 * Copyright (c) 2002 Glenn McGrath
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 * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build
15 * configuration.
16 *
17 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
18 */
19
20#include "libbb.h"
21/* After libbb.h, since it needs sys/types.h on some systems */
22#include <sys/utsname.h>
23#include <fnmatch.h>
24
25#define MAX_OPT_DEPTH 10
26#define EUNBALBRACK 10001
27#define EUNDEFVAR 10002
28#define EUNBALPER 10000
29
30#if ENABLE_FEATURE_IFUPDOWN_MAPPING
31#define MAX_INTERFACE_LENGTH 10
32#endif
33
34#define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS
35
36#define debug_noise(args...) /*fprintf(stderr, args)*/
37
38/* Forward declaration */
39struct interface_defn_t;
40
41typedef int execfn(char *command);
42
43struct method_t {
44 const char *name;
45 int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
46 int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC;
47};
48
49struct address_family_t {
50 const char *name;
51 int n_methods;
52 const struct method_t *method;
53};
54
55struct mapping_defn_t {
56 struct mapping_defn_t *next;
57
58 int max_matches;
59 int n_matches;
60 char **match;
61
62 char *script;
63
64 int max_mappings;
65 int n_mappings;
66 char **mapping;
67};
68
69struct variable_t {
70 char *name;
71 char *value;
72};
73
74struct interface_defn_t {
75 const struct address_family_t *address_family;
76 const struct method_t *method;
77
78 char *iface;
79 int max_options;
80 int n_options;
81 struct variable_t *option;
82};
83
84struct interfaces_file_t {
85 llist_t *autointerfaces;
86 llist_t *ifaces;
87 struct mapping_defn_t *mappings;
88};
89
90
91#define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:"
92enum {
93 OPT_do_all = 0x1,
94 OPT_no_act = 0x2,
95 OPT_verbose = 0x4,
96 OPT_force = 0x8,
97 OPT_no_mappings = 0x10,
98};
99#define DO_ALL (option_mask32 & OPT_do_all)
100#define NO_ACT (option_mask32 & OPT_no_act)
101#define VERBOSE (option_mask32 & OPT_verbose)
102#define FORCE (option_mask32 & OPT_force)
103#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
104
105
106struct globals {
107 char **my_environ;
108 const char *startup_PATH;
109} FIX_ALIASING;
110#define G (*(struct globals*)&bb_common_bufsiz1)
111#define INIT_G() do { } while (0)
112
113
114#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
115
116static void addstr(char **bufp, const char *str, size_t str_length)
117{
118 /* xasprintf trick will be smaller, but we are often
119 * called with str_length == 1 - don't want to have
120 * THAT much of malloc/freeing! */
121 char *buf = *bufp;
122 int len = (buf ? strlen(buf) : 0);
123 str_length++;
124 buf = xrealloc(buf, len + str_length);
125 /* copies at most str_length-1 chars! */
126 safe_strncpy(buf + len, str, str_length);
127 *bufp = buf;
128}
129
130static int strncmpz(const char *l, const char *r, size_t llen)
131{
132 int i = strncmp(l, r, llen);
133
134 if (i == 0)
135 return - (unsigned char)r[llen];
136 return i;
137}
138
139static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
140{
141 int i;
142
143 if (strncmpz(id, "iface", idlen) == 0) {
144 // ubuntu's ifup doesn't do this:
145 //static char *label_buf;
146 //char *result;
147 //free(label_buf);
148 //label_buf = xstrdup(ifd->iface);
149 // Remove virtual iface suffix
150 //result = strchrnul(label_buf, ':');
151 //*result = '\0';
152 //return label_buf;
153
154 return ifd->iface;
155 }
156 if (strncmpz(id, "label", idlen) == 0) {
157 return ifd->iface;
158 }
159 for (i = 0; i < ifd->n_options; i++) {
160 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
161 return ifd->option[i].value;
162 }
163 }
164 return NULL;
165}
166
167# if ENABLE_FEATURE_IFUPDOWN_IP
168static int count_netmask_bits(const char *dotted_quad)
169{
170// int result;
171// unsigned a, b, c, d;
172// /* Found a netmask... Check if it is dotted quad */
173// if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
174// return -1;
175// if ((a|b|c|d) >> 8)
176// return -1; /* one of numbers is >= 256 */
177// d |= (a << 24) | (b << 16) | (c << 8); /* IP */
178// d = ~d; /* 11110000 -> 00001111 */
179
180 /* Shorter version */
181 int result;
182 struct in_addr ip;
183 unsigned d;
184
185 if (inet_aton(dotted_quad, &ip) == 0)
186 return -1; /* malformed dotted IP */
187 d = ntohl(ip.s_addr); /* IP in host order */
188 d = ~d; /* 11110000 -> 00001111 */
189 if (d & (d+1)) /* check that it is in 00001111 form */
190 return -1; /* no it is not */
191 result = 32;
192 while (d) {
193 d >>= 1;
194 result--;
195 }
196 return result;
197}
198# endif
199
200static char *parse(const char *command, struct interface_defn_t *ifd)
201{
202 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
203 int okay[MAX_OPT_DEPTH] = { 1 };
204 int opt_depth = 1;
205 char *result = NULL;
206
207 while (*command) {
208 switch (*command) {
209 default:
210 addstr(&result, command, 1);
211 command++;
212 break;
213 case '\\':
214 if (command[1]) {
215 addstr(&result, command + 1, 1);
216 command += 2;
217 } else {
218 addstr(&result, command, 1);
219 command++;
220 }
221 break;
222 case '[':
223 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
224 old_pos[opt_depth] = result ? strlen(result) : 0;
225 okay[opt_depth] = 1;
226 opt_depth++;
227 command += 2;
228 } else {
229 addstr(&result, "[", 1);
230 command++;
231 }
232 break;
233 case ']':
234 if (command[1] == ']' && opt_depth > 1) {
235 opt_depth--;
236 if (!okay[opt_depth]) {
237 result[old_pos[opt_depth]] = '\0';
238 }
239 command += 2;
240 } else {
241 addstr(&result, "]", 1);
242 command++;
243 }
244 break;
245 case '%':
246 {
247 char *nextpercent;
248 char *varvalue;
249
250 command++;
251 nextpercent = strchr(command, '%');
252 if (!nextpercent) {
253 errno = EUNBALPER;
254 free(result);
255 return NULL;
256 }
257
258 varvalue = get_var(command, nextpercent - command, ifd);
259
260 if (varvalue) {
261# if ENABLE_FEATURE_IFUPDOWN_IP
262 /* "hwaddress <class> <address>":
263 * unlike ifconfig, ip doesnt want <class>
264 * (usually "ether" keyword). Skip it. */
265 if (strncmp(command, "hwaddress", 9) == 0) {
266 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
267 }
268# endif
269 addstr(&result, varvalue, strlen(varvalue));
270 } else {
271# if ENABLE_FEATURE_IFUPDOWN_IP
272 /* Sigh... Add a special case for 'ip' to convert from
273 * dotted quad to bit count style netmasks. */
274 if (strncmp(command, "bnmask", 6) == 0) {
275 unsigned res;
276 varvalue = get_var("netmask", 7, ifd);
277 if (varvalue) {
278 res = count_netmask_bits(varvalue);
279 if (res > 0) {
280 const char *argument = utoa(res);
281 addstr(&result, argument, strlen(argument));
282 command = nextpercent + 1;
283 break;
284 }
285 }
286 }
287# endif
288 okay[opt_depth - 1] = 0;
289 }
290
291 command = nextpercent + 1;
292 }
293 break;
294 }
295 }
296
297 if (opt_depth > 1) {
298 errno = EUNBALBRACK;
299 free(result);
300 return NULL;
301 }
302
303 if (!okay[0]) {
304 errno = EUNDEFVAR;
305 free(result);
306 return NULL;
307 }
308
309 return result;
310}
311
312/* execute() returns 1 for success and 0 for failure */
313static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
314{
315 char *out;
316 int ret;
317
318 out = parse(command, ifd);
319 if (!out) {
320 /* parse error? */
321 return 0;
322 }
323 /* out == "": parsed ok but not all needed variables known, skip */
324 ret = out[0] ? (*exec)(out) : 1;
325
326 free(out);
327 if (ret != 1) {
328 return 0;
329 }
330 return 1;
331}
332
333#endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
334
335
336#if ENABLE_FEATURE_IFUPDOWN_IPV6
337
338static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec)
339{
340# if ENABLE_FEATURE_IFUPDOWN_IP
341 int result;
342 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
343 result += execute("ip link set %iface% up", ifd, exec);
344 return ((result == 2) ? 2 : 0);
345# else
346 return execute("ifconfig %iface% add ::1", ifd, exec);
347# endif
348}
349
350static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec)
351{
352# if ENABLE_FEATURE_IFUPDOWN_IP
353 return execute("ip link set %iface% down", ifd, exec);
354# else
355 return execute("ifconfig %iface% del ::1", ifd, exec);
356# endif
357}
358
359static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
360{
361 return 1;
362}
363
364static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
365{
366 int result;
367# if ENABLE_FEATURE_IFUPDOWN_IP
368 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
369 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
370 /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
371 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
372# else
373 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
374 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
375 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
376# endif
377 return ((result == 3) ? 3 : 0);
378}
379
380static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
381{
382# if ENABLE_FEATURE_IFUPDOWN_IP
383 return execute("ip link set %iface% down", ifd, exec);
384# else
385 return execute("ifconfig %iface% down", ifd, exec);
386# endif
387}
388
389# if ENABLE_FEATURE_IFUPDOWN_IP
390static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
391{
392 int result;
393 result = execute("ip tunnel add %iface% mode sit remote "
394 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
395 result += execute("ip link set %iface% up", ifd, exec);
396 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
397 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
398 return ((result == 4) ? 4 : 0);
399}
400
401static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
402{
403 return execute("ip tunnel del %iface%", ifd, exec);
404}
405# endif
406
407static const struct method_t methods6[] = {
408# if ENABLE_FEATURE_IFUPDOWN_IP
409 { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
410# endif
411 { "static" , static_up6 , static_down6 , },
412 { "manual" , manual_up_down6 , manual_up_down6 , },
413 { "loopback" , loopback_up6 , loopback_down6 , },
414};
415
416static const struct address_family_t addr_inet6 = {
417 "inet6",
418 ARRAY_SIZE(methods6),
419 methods6
420};
421
422#endif /* FEATURE_IFUPDOWN_IPV6 */
423
424
425#if ENABLE_FEATURE_IFUPDOWN_IPV4
426
427static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
428{
429# if ENABLE_FEATURE_IFUPDOWN_IP
430 int result;
431 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
432 result += execute("ip link set %iface% up", ifd, exec);
433 return ((result == 2) ? 2 : 0);
434# else
435 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
436# endif
437}
438
439static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
440{
441# if ENABLE_FEATURE_IFUPDOWN_IP
442 int result;
443 result = execute("ip addr flush dev %iface%", ifd, exec);
444 result += execute("ip link set %iface% down", ifd, exec);
445 return ((result == 2) ? 2 : 0);
446# else
447 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
448# endif
449}
450
451static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
452{
453 int result;
454# if ENABLE_FEATURE_IFUPDOWN_IP
455 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
456 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
457 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
458 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
459 return ((result == 3) ? 3 : 0);
460# else
461 /* ifconfig said to set iface up before it processes hw %hwaddress%,
462 * which then of course fails. Thus we run two separate ifconfig */
463 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
464 ifd, exec);
465 result += execute("ifconfig %iface% %address% netmask %netmask%"
466 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
467 ifd, exec);
468 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
469 return ((result == 3) ? 3 : 0);
470# endif
471}
472
473static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
474{
475 int result;
476# if ENABLE_FEATURE_IFUPDOWN_IP
477 result = execute("ip addr flush dev %iface%", ifd, exec);
478 result += execute("ip link set %iface% down", ifd, exec);
479# else
480 /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
481 /* Bringing the interface down deletes the routes in itself.
482 Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
483 result = 1;
484 result += execute("ifconfig %iface% down", ifd, exec);
485# endif
486 return ((result == 2) ? 2 : 0);
487}
488
489# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
490struct dhcp_client_t {
491 const char *name;
492 const char *startcmd;
493 const char *stopcmd;
494};
495
496static const struct dhcp_client_t ext_dhcp_clients[] = {
497 { "dhcpcd",
498 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
499 "dhcpcd -k %iface%",
500 },
501 { "dhclient",
502 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
503 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
504 },
505 { "pump",
506 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
507 "pump -i %iface% -k",
508 },
509 { "udhcpc",
510 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
511 "[[ -s %script%]][[ %udhcpc_opts%]]",
512 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
513 },
514};
515# endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
516
517# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
518static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
519{
520 unsigned i;
521# if ENABLE_FEATURE_IFUPDOWN_IP
522 /* ip doesn't up iface when it configures it (unlike ifconfig) */
523 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
524 return 0;
525# else
526 /* needed if we have hwaddress on dhcp iface */
527 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
528 return 0;
529# endif
530 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
531 if (exists_execable(ext_dhcp_clients[i].name))
532 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
533 }
534 bb_error_msg("no dhcp clients found");
535 return 0;
536}
537# elif ENABLE_UDHCPC
538static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
539{
540# if ENABLE_FEATURE_IFUPDOWN_IP
541 /* ip doesn't up iface when it configures it (unlike ifconfig) */
542 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
543 return 0;
544# else
545 /* needed if we have hwaddress on dhcp iface */
546 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
547 return 0;
548# endif
549 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
550 "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
551 ifd, exec);
552}
553# else
554static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
555 execfn *exec UNUSED_PARAM)
556{
557 return 0; /* no dhcp support */
558}
559# endif
560
561# if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
562static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
563{
564 int result = 0;
565 unsigned i;
566
567 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
568 if (exists_execable(ext_dhcp_clients[i].name)) {
569 result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
570 if (result)
571 break;
572 }
573 }
574
575 if (!result)
576 bb_error_msg("warning: no dhcp clients found and stopped");
577
578 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
579 and it may come back up because udhcpc is still shutting down */
580 usleep(100000);
581 result += static_down(ifd, exec);
582 return ((result == 3) ? 3 : 0);
583}
584# elif ENABLE_UDHCPC
585static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
586{
587 int result;
588 result = execute(
589 "test -f /var/run/udhcpc.%iface%.pid && "
590 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
591 ifd, exec);
592 /* Also bring the hardware interface down since
593 killing the dhcp client alone doesn't do it.
594 This enables consecutive ifup->ifdown->ifup */
595 /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
596 and it may come back up because udhcpc is still shutting down */
597 usleep(100000);
598 result += static_down(ifd, exec);
599 return ((result == 3) ? 3 : 0);
600}
601# else
602static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
603 execfn *exec UNUSED_PARAM)
604{
605 return 0; /* no dhcp support */
606}
607# endif
608
609static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
610{
611 return 1;
612}
613
614static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
615{
616 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
617 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
618 " --returniffail --serverbcast", ifd, exec);
619}
620
621static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
622{
623 return execute("pon[[ %provider%]]", ifd, exec);
624}
625
626static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
627{
628 return execute("poff[[ %provider%]]", ifd, exec);
629}
630
631static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
632{
633 return execute("start-stop-daemon --start -x wvdial "
634 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
635}
636
637static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
638{
639 return execute("start-stop-daemon --stop -x wvdial "
640 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
641}
642
643static const struct method_t methods[] = {
644 { "manual" , manual_up_down, manual_up_down, },
645 { "wvdial" , wvdial_up , wvdial_down , },
646 { "ppp" , ppp_up , ppp_down , },
647 { "static" , static_up , static_down , },
648 { "bootp" , bootp_up , static_down , },
649 { "dhcp" , dhcp_up , dhcp_down , },
650 { "loopback", loopback_up , loopback_down , },
651};
652
653static const struct address_family_t addr_inet = {
654 "inet",
655 ARRAY_SIZE(methods),
656 methods
657};
658
659#endif /* FEATURE_IFUPDOWN_IPV4 */
660
661
662/* Returns pointer to the next word, or NULL.
663 * In 1st case, advances *buf to the word after this one.
664 */
665static char *next_word(char **buf)
666{
667 unsigned length;
668 char *word;
669
670 /* Skip over leading whitespace */
671 word = skip_whitespace(*buf);
672
673 /* Stop on EOL */
674 if (*word == '\0')
675 return NULL;
676
677 /* Find the length of this word (can't be 0) */
678 length = strcspn(word, " \t\n");
679
680 /* Unless we are already at NUL, store NUL and advance */
681 if (word[length] != '\0')
682 word[length++] = '\0';
683
684 *buf = skip_whitespace(word + length);
685
686 return word;
687}
688
689static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
690{
691 int i;
692
693 if (!name)
694 return NULL;
695
696 for (i = 0; af[i]; i++) {
697 if (strcmp(af[i]->name, name) == 0) {
698 return af[i];
699 }
700 }
701 return NULL;
702}
703
704static const struct method_t *get_method(const struct address_family_t *af, char *name)
705{
706 int i;
707
708 if (!name)
709 return NULL;
710 /* TODO: use index_in_str_array() */
711 for (i = 0; i < af->n_methods; i++) {
712 if (strcmp(af->method[i].name, name) == 0) {
713 return &af->method[i];
714 }
715 }
716 return NULL;
717}
718
719static struct interfaces_file_t *read_interfaces(const char *filename)
720{
721 /* Let's try to be compatible.
722 *
723 * "man 5 interfaces" says:
724 * Lines starting with "#" are ignored. Note that end-of-line
725 * comments are NOT supported, comments must be on a line of their own.
726 * A line may be extended across multiple lines by making
727 * the last character a backslash.
728 *
729 * Seen elsewhere in example config file:
730 * A first non-blank "#" character makes the rest of the line
731 * be ignored. Blank lines are ignored. Lines may be indented freely.
732 * A "\" character at the very end of the line indicates the next line
733 * should be treated as a continuation of the current one.
734 */
735#if ENABLE_FEATURE_IFUPDOWN_MAPPING
736 struct mapping_defn_t *currmap = NULL;
737#endif
738 struct interface_defn_t *currif = NULL;
739 struct interfaces_file_t *defn;
740 FILE *f;
741 char *buf;
742 char *first_word;
743 char *rest_of_line;
744 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
745
746 defn = xzalloc(sizeof(*defn));
747 f = xfopen_for_read(filename);
748
749 while ((buf = xmalloc_fgetline(f)) != NULL) {
750#if ENABLE_DESKTOP
751 /* Trailing "\" concatenates lines */
752 char *p;
753 while ((p = last_char_is(buf, '\\')) != NULL) {
754 *p = '\0';
755 rest_of_line = xmalloc_fgetline(f);
756 if (!rest_of_line)
757 break;
758 p = xasprintf("%s%s", buf, rest_of_line);
759 free(buf);
760 free(rest_of_line);
761 buf = p;
762 }
763#endif
764 rest_of_line = buf;
765 first_word = next_word(&rest_of_line);
766 if (!first_word || *first_word == '#') {
767 free(buf);
768 continue; /* blank/comment line */
769 }
770
771 if (strcmp(first_word, "mapping") == 0) {
772#if ENABLE_FEATURE_IFUPDOWN_MAPPING
773 currmap = xzalloc(sizeof(*currmap));
774
775 while ((first_word = next_word(&rest_of_line)) != NULL) {
776 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
777 currmap->match[currmap->n_matches++] = xstrdup(first_word);
778 }
779 /*currmap->max_mappings = 0; - done by xzalloc */
780 /*currmap->n_mappings = 0;*/
781 /*currmap->mapping = NULL;*/
782 /*currmap->script = NULL;*/
783 {
784 struct mapping_defn_t **where = &defn->mappings;
785 while (*where != NULL) {
786 where = &(*where)->next;
787 }
788 *where = currmap;
789 /*currmap->next = NULL;*/
790 }
791 debug_noise("Added mapping\n");
792#endif
793 currently_processing = MAPPING;
794 } else if (strcmp(first_word, "iface") == 0) {
795 static const struct address_family_t *const addr_fams[] = {
796#if ENABLE_FEATURE_IFUPDOWN_IPV4
797 &addr_inet,
798#endif
799#if ENABLE_FEATURE_IFUPDOWN_IPV6
800 &addr_inet6,
801#endif
802 NULL
803 };
804 char *iface_name;
805 char *address_family_name;
806 char *method_name;
807 llist_t *iface_list;
808
809 currif = xzalloc(sizeof(*currif));
810 iface_name = next_word(&rest_of_line);
811 address_family_name = next_word(&rest_of_line);
812 method_name = next_word(&rest_of_line);
813
814 if (method_name == NULL)
815 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
816
817 /* ship any trailing whitespace */
818 rest_of_line = skip_whitespace(rest_of_line);
819
820 if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
821 bb_error_msg_and_die("too many parameters \"%s\"", buf);
822
823 currif->iface = xstrdup(iface_name);
824
825 currif->address_family = get_address_family(addr_fams, address_family_name);
826 if (!currif->address_family)
827 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
828
829 currif->method = get_method(currif->address_family, method_name);
830 if (!currif->method)
831 bb_error_msg_and_die("unknown method \"%s\"", method_name);
832
833 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
834 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
835 if ((strcmp(tmp->iface, currif->iface) == 0)
836 && (tmp->address_family == currif->address_family)
837 ) {
838 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
839 }
840 }
841 llist_add_to_end(&(defn->ifaces), (char*)currif);
842
843 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
844 currently_processing = IFACE;
845 } else if (strcmp(first_word, "auto") == 0) {
846 while ((first_word = next_word(&rest_of_line)) != NULL) {
847
848 /* Check the interface isnt already listed */
849 if (llist_find_str(defn->autointerfaces, first_word)) {
850 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
851 }
852
853 /* Add the interface to the list */
854 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
855 debug_noise("\nauto %s\n", first_word);
856 }
857 currently_processing = NONE;
858 } else {
859 switch (currently_processing) {
860 case IFACE:
861 if (rest_of_line[0] == '\0')
862 bb_error_msg_and_die("option with empty value \"%s\"", buf);
863
864 if (strcmp(first_word, "up") != 0
865 && strcmp(first_word, "down") != 0
866 && strcmp(first_word, "pre-up") != 0
867 && strcmp(first_word, "post-down") != 0
868 ) {
869 int i;
870 for (i = 0; i < currif->n_options; i++) {
871 if (strcmp(currif->option[i].name, first_word) == 0)
872 bb_error_msg_and_die("duplicate option \"%s\"", buf);
873 }
874 }
875 if (currif->n_options >= currif->max_options) {
876 currif->max_options += 10;
877 currif->option = xrealloc(currif->option,
878 sizeof(*currif->option) * currif->max_options);
879 }
880 debug_noise("\t%s=%s\n", first_word, rest_of_line);
881 currif->option[currif->n_options].name = xstrdup(first_word);
882 currif->option[currif->n_options].value = xstrdup(rest_of_line);
883 currif->n_options++;
884 break;
885 case MAPPING:
886#if ENABLE_FEATURE_IFUPDOWN_MAPPING
887 if (strcmp(first_word, "script") == 0) {
888 if (currmap->script != NULL)
889 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
890 currmap->script = xstrdup(next_word(&rest_of_line));
891 } else if (strcmp(first_word, "map") == 0) {
892 if (currmap->n_mappings >= currmap->max_mappings) {
893 currmap->max_mappings = currmap->max_mappings * 2 + 1;
894 currmap->mapping = xrealloc(currmap->mapping,
895 sizeof(char *) * currmap->max_mappings);
896 }
897 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
898 currmap->n_mappings++;
899 } else {
900 bb_error_msg_and_die("misplaced option \"%s\"", buf);
901 }
902#endif
903 break;
904 case NONE:
905 default:
906 bb_error_msg_and_die("misplaced option \"%s\"", buf);
907 }
908 }
909 free(buf);
910 } /* while (fgets) */
911
912 if (ferror(f) != 0) {
913 /* ferror does NOT set errno! */
914 bb_error_msg_and_die("%s: I/O error", filename);
915 }
916 fclose(f);
917
918 return defn;
919}
920
921static char *setlocalenv(const char *format, const char *name, const char *value)
922{
923 char *result;
924 char *dst;
925 char *src;
926 char c;
927
928 result = xasprintf(format, name, value);
929
930 for (dst = src = result; (c = *src) != '=' && c; src++) {
931 if (c == '-')
932 c = '_';
933 if (c >= 'a' && c <= 'z')
934 c -= ('a' - 'A');
935 if (isalnum(c) || c == '_')
936 *dst++ = c;
937 }
938 overlapping_strcpy(dst, src);
939
940 return result;
941}
942
943static void set_environ(struct interface_defn_t *iface, const char *mode)
944{
945 int i;
946 char **pp;
947
948 if (G.my_environ != NULL) {
949 for (pp = G.my_environ; *pp; pp++) {
950 free(*pp);
951 }
952 free(G.my_environ);
953 }
954
955 /* note: last element will stay NULL: */
956 G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
957 pp = G.my_environ;
958
959 for (i = 0; i < iface->n_options; i++) {
960 if (strcmp(iface->option[i].name, "up") == 0
961 || strcmp(iface->option[i].name, "down") == 0
962 || strcmp(iface->option[i].name, "pre-up") == 0
963 || strcmp(iface->option[i].name, "post-down") == 0
964 ) {
965 continue;
966 }
967 *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
968 }
969
970 *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
971 *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
972 *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
973 *pp++ = setlocalenv("%s=%s", "MODE", mode);
974 if (G.startup_PATH)
975 *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
976}
977
978static int doit(char *str)
979{
980 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
981 puts(str);
982 }
983 if (!(option_mask32 & OPT_no_act)) {
984 pid_t child;
985 int status;
986
987 fflush_all();
988 child = vfork();
989 switch (child) {
990 case -1: /* failure */
991 return 0;
992 case 0: /* child */
993 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
994 _exit(127);
995 }
996 safe_waitpid(child, &status, 0);
997 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
998 return 0;
999 }
1000 }
1001 return 1;
1002}
1003
1004static int execute_all(struct interface_defn_t *ifd, const char *opt)
1005{
1006 int i;
1007 char *buf;
1008 for (i = 0; i < ifd->n_options; i++) {
1009 if (strcmp(ifd->option[i].name, opt) == 0) {
1010 if (!doit(ifd->option[i].value)) {
1011 return 0;
1012 }
1013 }
1014 }
1015
1016 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
1017 /* heh, we don't bother free'ing it */
1018 return doit(buf);
1019}
1020
1021static int check(char *str)
1022{
1023 return str != NULL;
1024}
1025
1026static int iface_up(struct interface_defn_t *iface)
1027{
1028 if (!iface->method->up(iface, check)) return -1;
1029 set_environ(iface, "start");
1030 if (!execute_all(iface, "pre-up")) return 0;
1031 if (!iface->method->up(iface, doit)) return 0;
1032 if (!execute_all(iface, "up")) return 0;
1033 return 1;
1034}
1035
1036static int iface_down(struct interface_defn_t *iface)
1037{
1038 if (!iface->method->down(iface,check)) return -1;
1039 set_environ(iface, "stop");
1040 if (!execute_all(iface, "down")) return 0;
1041 if (!iface->method->down(iface, doit)) return 0;
1042 if (!execute_all(iface, "post-down")) return 0;
1043 return 1;
1044}
1045
1046#if ENABLE_FEATURE_IFUPDOWN_MAPPING
1047static int popen2(FILE **in, FILE **out, char *command, char *param)
1048{
1049 char *argv[3] = { command, param, NULL };
1050 struct fd_pair infd, outfd;
1051 pid_t pid;
1052
1053 xpiped_pair(infd);
1054 xpiped_pair(outfd);
1055
1056 fflush_all();
1057 pid = xvfork();
1058
1059 if (pid == 0) {
1060 /* Child */
1061 /* NB: close _first_, then move fds! */
1062 close(infd.wr);
1063 close(outfd.rd);
1064 xmove_fd(infd.rd, 0);
1065 xmove_fd(outfd.wr, 1);
1066 BB_EXECVP_or_die(argv);
1067 }
1068 /* parent */
1069 close(infd.rd);
1070 close(outfd.wr);
1071 *in = xfdopen_for_write(infd.wr);
1072 *out = xfdopen_for_read(outfd.rd);
1073 return pid;
1074}
1075
1076static char *run_mapping(char *physical, struct mapping_defn_t *map)
1077{
1078 FILE *in, *out;
1079 int i, status;
1080 pid_t pid;
1081
1082 char *logical = xstrdup(physical);
1083
1084 /* Run the mapping script. Never fails. */
1085 pid = popen2(&in, &out, map->script, physical);
1086
1087 /* Write mappings to stdin of mapping script. */
1088 for (i = 0; i < map->n_mappings; i++) {
1089 fprintf(in, "%s\n", map->mapping[i]);
1090 }
1091 fclose(in);
1092 safe_waitpid(pid, &status, 0);
1093
1094 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1095 /* If the mapping script exited successfully, try to
1096 * grab a line of output and use that as the name of the
1097 * logical interface. */
1098 char *new_logical = xmalloc_fgetline(out);
1099
1100 if (new_logical) {
1101 /* If we are able to read a line of output from the script,
1102 * remove any trailing whitespace and use this value
1103 * as the name of the logical interface. */
1104 char *pch = new_logical + strlen(new_logical) - 1;
1105
1106 while (pch >= new_logical && isspace(*pch))
1107 *(pch--) = '\0';
1108
1109 free(logical);
1110 logical = new_logical;
1111 }
1112 }
1113
1114 fclose(out);
1115
1116 return logical;
1117}
1118#endif /* FEATURE_IFUPDOWN_MAPPING */
1119
1120static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1121{
1122 unsigned iface_len = strlen(iface);
1123 llist_t *search = state_list;
1124
1125 while (search) {
1126 if ((strncmp(search->data, iface, iface_len) == 0)
1127 && (search->data[iface_len] == '=')
1128 ) {
1129 return search;
1130 }
1131 search = search->link;
1132 }
1133 return NULL;
1134}
1135
1136/* read the previous state from the state file */
1137static llist_t *read_iface_state(void)
1138{
1139 llist_t *state_list = NULL;
1140 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1141
1142 if (state_fp) {
1143 char *start, *end_ptr;
1144 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1145 /* We should only need to check for a single character */
1146 end_ptr = start + strcspn(start, " \t\n");
1147 *end_ptr = '\0';
1148 llist_add_to(&state_list, start);
1149 }
1150 fclose(state_fp);
1151 }
1152 return state_list;
1153}
1154
1155
1156int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1157int ifupdown_main(int argc UNUSED_PARAM, char **argv)
1158{
1159 int (*cmds)(struct interface_defn_t *);
1160 struct interfaces_file_t *defn;
1161 llist_t *target_list = NULL;
1162 const char *interfaces = "/etc/network/interfaces";
1163 bool any_failures = 0;
1164
1165 INIT_G();
1166
1167 G.startup_PATH = getenv("PATH");
1168
1169 cmds = iface_down;
1170 if (applet_name[2] == 'u') {
1171 /* ifup command */
1172 cmds = iface_up;
1173 }
1174
1175 getopt32(argv, OPTION_STR, &interfaces);
1176 argv += optind;
1177 if (argv[0]) {
1178 if (DO_ALL) bb_show_usage();
1179 } else {
1180 if (!DO_ALL) bb_show_usage();
1181 }
1182
1183 debug_noise("reading %s file:\n", interfaces);
1184 defn = read_interfaces(interfaces);
1185 debug_noise("\ndone reading %s\n\n", interfaces);
1186
1187 /* Create a list of interfaces to work on */
1188 if (DO_ALL) {
1189 target_list = defn->autointerfaces;
1190 } else {
1191 llist_add_to_end(&target_list, argv[0]);
1192 }
1193
1194 /* Update the interfaces */
1195 while (target_list) {
1196 llist_t *iface_list;
1197 struct interface_defn_t *currif;
1198 char *iface;
1199 char *liface;
1200 char *pch;
1201 bool okay = 0;
1202 int cmds_ret;
1203
1204 iface = xstrdup(target_list->data);
1205 target_list = target_list->link;
1206
1207 pch = strchr(iface, '=');
1208 if (pch) {
1209 *pch = '\0';
1210 liface = xstrdup(pch + 1);
1211 } else {
1212 liface = xstrdup(iface);
1213 }
1214
1215 if (!FORCE) {
1216 llist_t *state_list = read_iface_state();
1217 const llist_t *iface_state = find_iface_state(state_list, iface);
1218
1219 if (cmds == iface_up) {
1220 /* ifup */
1221 if (iface_state) {
1222 bb_error_msg("interface %s already configured", iface);
1223 continue;
1224 }
1225 } else {
1226 /* ifdown */
1227 if (!iface_state) {
1228 bb_error_msg("interface %s not configured", iface);
1229 continue;
1230 }
1231 }
1232 llist_free(state_list, free);
1233 }
1234
1235#if ENABLE_FEATURE_IFUPDOWN_MAPPING
1236 if ((cmds == iface_up) && !NO_MAPPINGS) {
1237 struct mapping_defn_t *currmap;
1238
1239 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1240 int i;
1241 for (i = 0; i < currmap->n_matches; i++) {
1242 if (fnmatch(currmap->match[i], liface, 0) != 0)
1243 continue;
1244 if (VERBOSE) {
1245 printf("Running mapping script %s on %s\n", currmap->script, liface);
1246 }
1247 liface = run_mapping(iface, currmap);
1248 break;
1249 }
1250 }
1251 }
1252#endif
1253
1254 iface_list = defn->ifaces;
1255 while (iface_list) {
1256 currif = (struct interface_defn_t *) iface_list->data;
1257 if (strcmp(liface, currif->iface) == 0) {
1258 char *oldiface = currif->iface;
1259
1260 okay = 1;
1261 currif->iface = iface;
1262
1263 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1264
1265 /* Call the cmds function pointer, does either iface_up() or iface_down() */
1266 cmds_ret = cmds(currif);
1267 if (cmds_ret == -1) {
1268 bb_error_msg("don't seem to have all the variables for %s/%s",
1269 liface, currif->address_family->name);
1270 any_failures = 1;
1271 } else if (cmds_ret == 0) {
1272 any_failures = 1;
1273 }
1274
1275 currif->iface = oldiface;
1276 }
1277 iface_list = iface_list->link;
1278 }
1279 if (VERBOSE) {
1280 bb_putchar('\n');
1281 }
1282
1283 if (!okay && !FORCE) {
1284 bb_error_msg("ignoring unknown interface %s", liface);
1285 any_failures = 1;
1286 } else if (!NO_ACT) {
1287 /* update the state file */
1288 FILE *state_fp;
1289 llist_t *state;
1290 llist_t *state_list = read_iface_state();
1291 llist_t *iface_state = find_iface_state(state_list, iface);
1292
1293 if (cmds == iface_up) {
1294 char * const newiface = xasprintf("%s=%s", iface, liface);
1295 if (iface_state == NULL) {
1296 llist_add_to_end(&state_list, newiface);
1297 } else {
1298 free(iface_state->data);
1299 iface_state->data = newiface;
1300 }
1301 } else {
1302 /* Remove an interface from state_list */
1303 llist_unlink(&state_list, iface_state);
1304 free(llist_pop(&iface_state));
1305 }
1306
1307 /* Actually write the new state */
1308 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1309 state = state_list;
1310 while (state) {
1311 if (state->data) {
1312 fprintf(state_fp, "%s\n", state->data);
1313 }
1314 state = state->link;
1315 }
1316 fclose(state_fp);
1317 llist_free(state_list, free);
1318 }
1319 }
1320
1321 return any_failures;
1322}
Note: See TracBrowser for help on using the repository browser.