1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20#include <sys/utsname.h>
21#include <fnmatch.h>
22
23#include "libbb.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...)
37
38
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);
46 int (*down)(struct interface_defn_t *ifd, execfn *e);
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#define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
91enum {
92 OPT_do_all = 0x1,
93 OPT_no_act = 0x2,
94 OPT_verbose = 0x4,
95 OPT_force = 0x8,
96 OPT_no_mappings = 0x10,
97};
98#define DO_ALL (option_mask32 & OPT_do_all)
99#define NO_ACT (option_mask32 & OPT_no_act)
100#define VERBOSE (option_mask32 & OPT_verbose)
101#define FORCE (option_mask32 & OPT_force)
102#define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
103
104static char **my_environ;
105
106static const char *startup_PATH;
107
108#if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
109
110static void addstr(char **bufp, const char *str, size_t str_length)
111{
112
113
114
115 char *buf = *bufp;
116 int len = (buf ? strlen(buf) : 0);
117 str_length++;
118 buf = xrealloc(buf, len + str_length);
119
120 safe_strncpy(buf + len, str, str_length);
121 *bufp = buf;
122}
123
124static int strncmpz(const char *l, const char *r, size_t llen)
125{
126 int i = strncmp(l, r, llen);
127
128 if (i == 0)
129 return -r[llen];
130 return i;
131}
132
133static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
134{
135 int i;
136
137 if (strncmpz(id, "iface", idlen) == 0) {
138 static char *label_buf;
139
140
141 free(label_buf);
142 label_buf = xstrdup(ifd->iface);
143
144
145
146
147 return label_buf;
148 }
149 if (strncmpz(id, "label", idlen) == 0) {
150 return ifd->iface;
151 }
152 for (i = 0; i < ifd->n_options; i++) {
153 if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
154 return ifd->option[i].value;
155 }
156 }
157 return NULL;
158}
159
160#if ENABLE_FEATURE_IFUPDOWN_IP
161static int count_netmask_bits(const char *dotted_quad)
162{
163
164
165
166
167
168
169
170
171
172
173
174 int result;
175 struct in_addr ip;
176 unsigned d;
177
178 if (inet_aton(dotted_quad, &ip) == 0)
179 return -1;
180 d = ntohl(ip.s_addr);
181 d = ~d;
182 if (d & (d+1))
183 return -1;
184 result = 32;
185 while (d) {
186 d >>= 1;
187 result--;
188 }
189 return result;
190}
191#endif
192
193static char *parse(const char *command, struct interface_defn_t *ifd)
194{
195 size_t old_pos[MAX_OPT_DEPTH] = { 0 };
196 int okay[MAX_OPT_DEPTH] = { 1 };
197 int opt_depth = 1;
198 char *result = NULL;
199
200 while (*command) {
201 switch (*command) {
202 default:
203 addstr(&result, command, 1);
204 command++;
205 break;
206 case '\\':
207 if (command[1]) {
208 addstr(&result, command + 1, 1);
209 command += 2;
210 } else {
211 addstr(&result, command, 1);
212 command++;
213 }
214 break;
215 case '[':
216 if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
217 old_pos[opt_depth] = result ? strlen(result) : 0;
218 okay[opt_depth] = 1;
219 opt_depth++;
220 command += 2;
221 } else {
222 addstr(&result, "[", 1);
223 command++;
224 }
225 break;
226 case ']':
227 if (command[1] == ']' && opt_depth > 1) {
228 opt_depth--;
229 if (!okay[opt_depth]) {
230 result[old_pos[opt_depth]] = '\0';
231 }
232 command += 2;
233 } else {
234 addstr(&result, "]", 1);
235 command++;
236 }
237 break;
238 case '%':
239 {
240 char *nextpercent;
241 char *varvalue;
242
243 command++;
244 nextpercent = strchr(command, '%');
245 if (!nextpercent) {
246 errno = EUNBALPER;
247 free(result);
248 return NULL;
249 }
250
251 varvalue = get_var(command, nextpercent - command, ifd);
252
253 if (varvalue) {
254#if ENABLE_FEATURE_IFUPDOWN_IP
255
256
257
258 if (strncmp(command, "hwaddress", 9) == 0) {
259 varvalue = skip_whitespace(skip_non_whitespace(varvalue));
260 }
261#endif
262 addstr(&result, varvalue, strlen(varvalue));
263 } else {
264#if ENABLE_FEATURE_IFUPDOWN_IP
265
266
267 if (strncmp(command, "bnmask", 6) == 0) {
268 unsigned res;
269 varvalue = get_var("netmask", 7, ifd);
270 if (varvalue) {
271 res = count_netmask_bits(varvalue);
272 if (res > 0) {
273 const char *argument = utoa(res);
274 addstr(&result, argument, strlen(argument));
275 command = nextpercent + 1;
276 break;
277 }
278 }
279 }
280#endif
281 okay[opt_depth - 1] = 0;
282 }
283
284 command = nextpercent + 1;
285 }
286 break;
287 }
288 }
289
290 if (opt_depth > 1) {
291 errno = EUNBALBRACK;
292 free(result);
293 return NULL;
294 }
295
296 if (!okay[0]) {
297 errno = EUNDEFVAR;
298 free(result);
299 return NULL;
300 }
301
302 return result;
303}
304
305
306static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec)
307{
308 char *out;
309 int ret;
310
311 out = parse(command, ifd);
312 if (!out) {
313
314 return 0;
315 }
316
317 ret = out[0] ? (*exec)(out) : 1;
318
319 free(out);
320 if (ret != 1) {
321 return 0;
322 }
323 return 1;
324}
325#endif
326
327#if ENABLE_FEATURE_IFUPDOWN_IPV6
328static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
329{
330#if ENABLE_FEATURE_IFUPDOWN_IP
331 int result;
332 result = execute("ip addr add ::1 dev %iface%", ifd, exec);
333 result += execute("ip link set %iface% up", ifd, exec);
334 return ((result == 2) ? 2 : 0);
335#else
336 return execute("ifconfig %iface% add ::1", ifd, exec);
337#endif
338}
339
340static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
341{
342#if ENABLE_FEATURE_IFUPDOWN_IP
343 return execute("ip link set %iface% down", ifd, exec);
344#else
345 return execute("ifconfig %iface% del ::1", ifd, exec);
346#endif
347}
348
349static int static_up6(struct interface_defn_t *ifd, execfn *exec)
350{
351 int result;
352#if ENABLE_FEATURE_IFUPDOWN_IP
353 result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
354 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
355
356 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
357#else
358 result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
359 result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
360 result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
361#endif
362 return ((result == 3) ? 3 : 0);
363}
364
365static int static_down6(struct interface_defn_t *ifd, execfn *exec)
366{
367#if ENABLE_FEATURE_IFUPDOWN_IP
368 return execute("ip link set %iface% down", ifd, exec);
369#else
370 return execute("ifconfig %iface% down", ifd, exec);
371#endif
372}
373
374#if ENABLE_FEATURE_IFUPDOWN_IP
375static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
376{
377 int result;
378 result = execute("ip tunnel add %iface% mode sit remote "
379 "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
380 result += execute("ip link set %iface% up", ifd, exec);
381 result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
382 result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
383 return ((result == 4) ? 4 : 0);
384}
385
386static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
387{
388 return execute("ip tunnel del %iface%", ifd, exec);
389}
390#endif
391
392static const struct method_t methods6[] = {
393#if ENABLE_FEATURE_IFUPDOWN_IP
394 { "v4tunnel", v4tunnel_up, v4tunnel_down, },
395#endif
396 { "static", static_up6, static_down6, },
397 { "loopback", loopback_up6, loopback_down6, },
398};
399
400static const struct address_family_t addr_inet6 = {
401 "inet6",
402 ARRAY_SIZE(methods6),
403 methods6
404};
405#endif
406
407#if ENABLE_FEATURE_IFUPDOWN_IPV4
408static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
409{
410#if ENABLE_FEATURE_IFUPDOWN_IP
411 int result;
412 result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
413 result += execute("ip link set %iface% up", ifd, exec);
414 return ((result == 2) ? 2 : 0);
415#else
416 return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
417#endif
418}
419
420static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
421{
422#if ENABLE_FEATURE_IFUPDOWN_IP
423 int result;
424 result = execute("ip addr flush dev %iface%", ifd, exec);
425 result += execute("ip link set %iface% down", ifd, exec);
426 return ((result == 2) ? 2 : 0);
427#else
428 return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
429#endif
430}
431
432static int static_up(struct interface_defn_t *ifd, execfn *exec)
433{
434 int result;
435#if ENABLE_FEATURE_IFUPDOWN_IP
436 result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
437 "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
438 result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
439 result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
440 return ((result == 3) ? 3 : 0);
441#else
442
443
444 result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
445 ifd, exec);
446 result += execute("ifconfig %iface% %address% netmask %netmask%"
447 "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
448 ifd, exec);
449 result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
450 return ((result == 3) ? 3 : 0);
451#endif
452}
453
454static int static_down(struct interface_defn_t *ifd, execfn *exec)
455{
456 int result;
457#if ENABLE_FEATURE_IFUPDOWN_IP
458 result = execute("ip addr flush dev %iface%", ifd, exec);
459 result += execute("ip link set %iface% down", ifd, exec);
460#else
461
462
463
464 result = 1;
465 result += execute("ifconfig %iface% down", ifd, exec);
466#endif
467 return ((result == 2) ? 2 : 0);
468}
469
470#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
471struct dhcp_client_t
472{
473 const char *name;
474 const char *startcmd;
475 const char *stopcmd;
476};
477
478static const struct dhcp_client_t ext_dhcp_clients[] = {
479 { "dhcpcd",
480 "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %clientid%]][[ -l %leasetime%]] %iface%",
481 "dhcpcd -k %iface%",
482 },
483 { "dhclient",
484 "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
485 "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
486 },
487 { "pump",
488 "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
489 "pump -i %iface% -k",
490 },
491 { "udhcpc",
492 "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %clientid%]]"
493 "[[ -s %script%]][[ %udhcpc_opts%]]",
494 "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
495 },
496};
497#endif
498
499#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
500static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
501{
502 unsigned i;
503#if ENABLE_FEATURE_IFUPDOWN_IP
504
505 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
506 return 0;
507#else
508
509 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
510 return 0;
511#endif
512 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
513 if (exists_execable(ext_dhcp_clients[i].name))
514 return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
515 }
516 bb_error_msg("no dhcp clients found");
517 return 0;
518}
519#elif ENABLE_APP_UDHCPC
520static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
521{
522#if ENABLE_FEATURE_IFUPDOWN_IP
523
524 if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
525 return 0;
526#else
527
528 if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
529 return 0;
530#endif
531 return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
532 "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
533 ifd, exec);
534}
535#else
536static int dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
537 execfn *exec UNUSED_PARAM)
538{
539 return 0;
540}
541#endif
542
543#if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
544static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
545{
546 int result = 0;
547 unsigned i;
548
549 for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
550 if (exists_execable(ext_dhcp_clients[i].name)) {
551 result += execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
552 if (result)
553 break;
554 }
555 }
556
557 if (!result)
558 bb_error_msg("warning: no dhcp clients found and stopped");
559
560
561
562 usleep(100000);
563 result += static_down(ifd, exec);
564 return ((result == 3) ? 3 : 0);
565}
566#elif ENABLE_APP_UDHCPC
567static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
568{
569 int result;
570 result = execute("kill "
571 "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
572
573
574
575
576
577 usleep(100000);
578 result += static_down(ifd, exec);
579 return ((result == 3) ? 3 : 0);
580}
581#else
582static int dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
583 execfn *exec UNUSED_PARAM)
584{
585 return 0;
586}
587#endif
588
589static int manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
590{
591 return 1;
592}
593
594static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
595{
596 return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
597 "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
598 " --returniffail --serverbcast", ifd, exec);
599}
600
601static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
602{
603 return execute("pon[[ %provider%]]", ifd, exec);
604}
605
606static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
607{
608 return execute("poff[[ %provider%]]", ifd, exec);
609}
610
611static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
612{
613 return execute("start-stop-daemon --start -x wvdial "
614 "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
615}
616
617static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
618{
619 return execute("start-stop-daemon --stop -x wvdial "
620 "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
621}
622
623static const struct method_t methods[] = {
624 { "manual", manual_up_down, manual_up_down, },
625 { "wvdial", wvdial_up, wvdial_down, },
626 { "ppp", ppp_up, ppp_down, },
627 { "static", static_up, static_down, },
628 { "bootp", bootp_up, static_down, },
629 { "dhcp", dhcp_up, dhcp_down, },
630 { "loopback", loopback_up, loopback_down, },
631};
632
633static const struct address_family_t addr_inet = {
634 "inet",
635 ARRAY_SIZE(methods),
636 methods
637};
638
639#endif
640
641static char *next_word(char **buf)
642{
643 unsigned length;
644 char *word;
645
646
647 word = skip_whitespace(*buf);
648
649
650 if (*word == '\0')
651 return NULL;
652
653
654 length = strcspn(word, " \t\n");
655
656
657 if (word[length] != '\0')
658 word[length++] = '\0';
659
660 *buf = word + length;
661
662 return word;
663}
664
665static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
666{
667 int i;
668
669 if (!name)
670 return NULL;
671
672 for (i = 0; af[i]; i++) {
673 if (strcmp(af[i]->name, name) == 0) {
674 return af[i];
675 }
676 }
677 return NULL;
678}
679
680static const struct method_t *get_method(const struct address_family_t *af, char *name)
681{
682 int i;
683
684 if (!name)
685 return NULL;
686
687 for (i = 0; i < af->n_methods; i++) {
688 if (strcmp(af->method[i].name, name) == 0) {
689 return &af->method[i];
690 }
691 }
692 return NULL;
693}
694
695static struct interfaces_file_t *read_interfaces(const char *filename)
696{
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711#if ENABLE_FEATURE_IFUPDOWN_MAPPING
712 struct mapping_defn_t *currmap = NULL;
713#endif
714 struct interface_defn_t *currif = NULL;
715 struct interfaces_file_t *defn;
716 FILE *f;
717 char *buf;
718 char *first_word;
719 char *rest_of_line;
720 enum { NONE, IFACE, MAPPING } currently_processing = NONE;
721
722 defn = xzalloc(sizeof(*defn));
723 f = xfopen_for_read(filename);
724
725 while ((buf = xmalloc_fgetline(f)) != NULL) {
726#if ENABLE_DESKTOP
727
728 char *p;
729 while ((p = last_char_is(buf, '\\')) != NULL) {
730 *p = '\0';
731 rest_of_line = xmalloc_fgetline(f);
732 if (!rest_of_line)
733 break;
734 p = xasprintf("%s%s", buf, rest_of_line);
735 free(buf);
736 free(rest_of_line);
737 buf = p;
738 }
739#endif
740 rest_of_line = buf;
741 first_word = next_word(&rest_of_line);
742 if (!first_word || *first_word == '#') {
743 free(buf);
744 continue;
745 }
746
747 if (strcmp(first_word, "mapping") == 0) {
748#if ENABLE_FEATURE_IFUPDOWN_MAPPING
749 currmap = xzalloc(sizeof(*currmap));
750
751 while ((first_word = next_word(&rest_of_line)) != NULL) {
752 currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
753 currmap->match[currmap->n_matches++] = xstrdup(first_word);
754 }
755
756
757
758
759 {
760 struct mapping_defn_t **where = &defn->mappings;
761 while (*where != NULL) {
762 where = &(*where)->next;
763 }
764 *where = currmap;
765
766 }
767 debug_noise("Added mapping\n");
768#endif
769 currently_processing = MAPPING;
770 } else if (strcmp(first_word, "iface") == 0) {
771 static const struct address_family_t *const addr_fams[] = {
772#if ENABLE_FEATURE_IFUPDOWN_IPV4
773 &addr_inet,
774#endif
775#if ENABLE_FEATURE_IFUPDOWN_IPV6
776 &addr_inet6,
777#endif
778 NULL
779 };
780 char *iface_name;
781 char *address_family_name;
782 char *method_name;
783 llist_t *iface_list;
784
785 currif = xzalloc(sizeof(*currif));
786 iface_name = next_word(&rest_of_line);
787 address_family_name = next_word(&rest_of_line);
788 method_name = next_word(&rest_of_line);
789
790 if (method_name == NULL)
791 bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
792
793
794 rest_of_line = skip_whitespace(rest_of_line);
795
796 if (rest_of_line[0] != '\0' )
797 bb_error_msg_and_die("too many parameters \"%s\"", buf);
798
799 currif->iface = xstrdup(iface_name);
800
801 currif->address_family = get_address_family(addr_fams, address_family_name);
802 if (!currif->address_family)
803 bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
804
805 currif->method = get_method(currif->address_family, method_name);
806 if (!currif->method)
807 bb_error_msg_and_die("unknown method \"%s\"", method_name);
808
809 for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
810 struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
811 if ((strcmp(tmp->iface, currif->iface) == 0)
812 && (tmp->address_family == currif->address_family)
813 ) {
814 bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
815 }
816 }
817 llist_add_to_end(&(defn->ifaces), (char*)currif);
818
819 debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
820 currently_processing = IFACE;
821 } else if (strcmp(first_word, "auto") == 0) {
822 while ((first_word = next_word(&rest_of_line)) != NULL) {
823
824
825 if (llist_find_str(defn->autointerfaces, first_word)) {
826 bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
827 }
828
829
830 llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
831 debug_noise("\nauto %s\n", first_word);
832 }
833 currently_processing = NONE;
834 } else {
835 switch (currently_processing) {
836 case IFACE:
837 if (rest_of_line[0] == '\0')
838 bb_error_msg_and_die("option with empty value \"%s\"", buf);
839
840 if (strcmp(first_word, "up") != 0
841 && strcmp(first_word, "down") != 0
842 && strcmp(first_word, "pre-up") != 0
843 && strcmp(first_word, "post-down") != 0
844 ) {
845 int i;
846 for (i = 0; i < currif->n_options; i++) {
847 if (strcmp(currif->option[i].name, first_word) == 0)
848 bb_error_msg_and_die("duplicate option \"%s\"", buf);
849 }
850 }
851 if (currif->n_options >= currif->max_options) {
852 currif->max_options += 10;
853 currif->option = xrealloc(currif->option,
854 sizeof(*currif->option) * currif->max_options);
855 }
856 debug_noise("\t%s=%s\n", first_word, rest_of_line);
857 currif->option[currif->n_options].name = xstrdup(first_word);
858 currif->option[currif->n_options].value = xstrdup(rest_of_line);
859 currif->n_options++;
860 break;
861 case MAPPING:
862#if ENABLE_FEATURE_IFUPDOWN_MAPPING
863 if (strcmp(first_word, "script") == 0) {
864 if (currmap->script != NULL)
865 bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
866 currmap->script = xstrdup(next_word(&rest_of_line));
867 } else if (strcmp(first_word, "map") == 0) {
868 if (currmap->n_mappings >= currmap->max_mappings) {
869 currmap->max_mappings = currmap->max_mappings * 2 + 1;
870 currmap->mapping = xrealloc(currmap->mapping,
871 sizeof(char *) * currmap->max_mappings);
872 }
873 currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
874 currmap->n_mappings++;
875 } else {
876 bb_error_msg_and_die("misplaced option \"%s\"", buf);
877 }
878#endif
879 break;
880 case NONE:
881 default:
882 bb_error_msg_and_die("misplaced option \"%s\"", buf);
883 }
884 }
885 free(buf);
886 }
887
888 if (ferror(f) != 0) {
889
890 bb_error_msg_and_die("%s: I/O error", filename);
891 }
892 fclose(f);
893
894 return defn;
895}
896
897static char *setlocalenv(const char *format, const char *name, const char *value)
898{
899 char *result;
900 char *here;
901 char *there;
902
903 result = xasprintf(format, name, value);
904
905 for (here = there = result; *there != '=' && *there; there++) {
906 if (*there == '-')
907 *there = '_';
908 if (isalpha(*there))
909 *there = toupper(*there);
910
911 if (isalnum(*there) || *there == '_') {
912 *here = *there;
913 here++;
914 }
915 }
916 memmove(here, there, strlen(there) + 1);
917
918 return result;
919}
920
921static void set_environ(struct interface_defn_t *iface, const char *mode)
922{
923 char **environend;
924 int i;
925 const int n_env_entries = iface->n_options + 5;
926 char **ppch;
927
928 if (my_environ != NULL) {
929 for (ppch = my_environ; *ppch; ppch++) {
930 free(*ppch);
931 *ppch = NULL;
932 }
933 free(my_environ);
934 }
935 my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 ));
936 environend = my_environ;
937
938 for (i = 0; i < iface->n_options; i++) {
939 if (strcmp(iface->option[i].name, "up") == 0
940 || strcmp(iface->option[i].name, "down") == 0
941 || strcmp(iface->option[i].name, "pre-up") == 0
942 || strcmp(iface->option[i].name, "post-down") == 0
943 ) {
944 continue;
945 }
946 *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
947 }
948
949 *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
950 *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
951 *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
952 *(environend++) = setlocalenv("%s=%s", "MODE", mode);
953 *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
954}
955
956static int doit(char *str)
957{
958 if (option_mask32 & (OPT_no_act|OPT_verbose)) {
959 puts(str);
960 }
961 if (!(option_mask32 & OPT_no_act)) {
962 pid_t child;
963 int status;
964
965 fflush(NULL);
966 child = vfork();
967 switch (child) {
968 case -1:
969 return 0;
970 case 0:
971 execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, my_environ);
972 _exit(127);
973 }
974 safe_waitpid(child, &status, 0);
975 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
976 return 0;
977 }
978 }
979 return 1;
980}
981
982static int execute_all(struct interface_defn_t *ifd, const char *opt)
983{
984 int i;
985 char *buf;
986 for (i = 0; i < ifd->n_options; i++) {
987 if (strcmp(ifd->option[i].name, opt) == 0) {
988 if (!doit(ifd->option[i].value)) {
989 return 0;
990 }
991 }
992 }
993
994 buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
995
996 return doit(buf);
997}
998
999static int check(char *str)
1000{
1001 return str != NULL;
1002}
1003
1004static int iface_up(struct interface_defn_t *iface)
1005{
1006 if (!iface->method->up(iface, check)) return -1;
1007 set_environ(iface, "start");
1008 if (!execute_all(iface, "pre-up")) return 0;
1009 if (!iface->method->up(iface, doit)) return 0;
1010 if (!execute_all(iface, "up")) return 0;
1011 return 1;
1012}
1013
1014static int iface_down(struct interface_defn_t *iface)
1015{
1016 if (!iface->method->down(iface,check)) return -1;
1017 set_environ(iface, "stop");
1018 if (!execute_all(iface, "down")) return 0;
1019 if (!iface->method->down(iface, doit)) return 0;
1020 if (!execute_all(iface, "post-down")) return 0;
1021 return 1;
1022}
1023
1024#if ENABLE_FEATURE_IFUPDOWN_MAPPING
1025static int popen2(FILE **in, FILE **out, char *command, char *param)
1026{
1027 char *argv[3] = { command, param, NULL };
1028 struct fd_pair infd, outfd;
1029 pid_t pid;
1030
1031 xpiped_pair(infd);
1032 xpiped_pair(outfd);
1033
1034 fflush(NULL);
1035 pid = vfork();
1036
1037 switch (pid) {
1038 case -1:
1039 bb_perror_msg_and_die("vfork");
1040 case 0:
1041
1042 close(infd.wr);
1043 close(outfd.rd);
1044 xmove_fd(infd.rd, 0);
1045 xmove_fd(outfd.wr, 1);
1046 BB_EXECVP(command, argv);
1047 _exit(127);
1048 }
1049
1050 close(infd.rd);
1051 close(outfd.wr);
1052 *in = fdopen(infd.wr, "w");
1053 *out = fdopen(outfd.rd, "r");
1054 return pid;
1055}
1056
1057static char *run_mapping(char *physical, struct mapping_defn_t *map)
1058{
1059 FILE *in, *out;
1060 int i, status;
1061 pid_t pid;
1062
1063 char *logical = xstrdup(physical);
1064
1065
1066 pid = popen2(&in, &out, map->script, physical);
1067
1068
1069 for (i = 0; i < map->n_mappings; i++) {
1070 fprintf(in, "%s\n", map->mapping[i]);
1071 }
1072 fclose(in);
1073 safe_waitpid(pid, &status, 0);
1074
1075 if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
1076
1077
1078
1079 char *new_logical = xmalloc_fgetline(out);
1080
1081 if (new_logical) {
1082
1083
1084
1085 char *pch = new_logical + strlen(new_logical) - 1;
1086
1087 while (pch >= new_logical && isspace(*pch))
1088 *(pch--) = '\0';
1089
1090 free(logical);
1091 logical = new_logical;
1092 }
1093 }
1094
1095 fclose(out);
1096
1097 return logical;
1098}
1099#endif
1100
1101static llist_t *find_iface_state(llist_t *state_list, const char *iface)
1102{
1103 unsigned iface_len = strlen(iface);
1104 llist_t *search = state_list;
1105
1106 while (search) {
1107 if ((strncmp(search->data, iface, iface_len) == 0)
1108 && (search->data[iface_len] == '=')
1109 ) {
1110 return search;
1111 }
1112 search = search->link;
1113 }
1114 return NULL;
1115}
1116
1117
1118static llist_t *read_iface_state(void)
1119{
1120 llist_t *state_list = NULL;
1121 FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
1122
1123 if (state_fp) {
1124 char *start, *end_ptr;
1125 while ((start = xmalloc_fgets(state_fp)) != NULL) {
1126
1127 end_ptr = start + strcspn(start, " \t\n");
1128 *end_ptr = '\0';
1129 llist_add_to(&state_list, start);
1130 }
1131 fclose(state_fp);
1132 }
1133 return state_list;
1134}
1135
1136
1137int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1138int ifupdown_main(int argc, char **argv)
1139{
1140 int (*cmds)(struct interface_defn_t *);
1141 struct interfaces_file_t *defn;
1142 llist_t *target_list = NULL;
1143 const char *interfaces = "/etc/network/interfaces";
1144 bool any_failures = 0;
1145
1146 cmds = iface_down;
1147 if (applet_name[2] == 'u') {
1148
1149 cmds = iface_up;
1150 }
1151
1152 getopt32(argv, OPTION_STR, &interfaces);
1153 if (argc - optind > 0) {
1154 if (DO_ALL) bb_show_usage();
1155 } else {
1156 if (!DO_ALL) bb_show_usage();
1157 }
1158
1159 debug_noise("reading %s file:\n", interfaces);
1160 defn = read_interfaces(interfaces);
1161 debug_noise("\ndone reading %s\n\n", interfaces);
1162
1163 startup_PATH = getenv("PATH");
1164 if (!startup_PATH) startup_PATH = "";
1165
1166
1167 if (DO_ALL) {
1168 target_list = defn->autointerfaces;
1169 } else {
1170 llist_add_to_end(&target_list, argv[optind]);
1171 }
1172
1173
1174 while (target_list) {
1175 llist_t *iface_list;
1176 struct interface_defn_t *currif;
1177 char *iface;
1178 char *liface;
1179 char *pch;
1180 bool okay = 0;
1181 int cmds_ret;
1182
1183 iface = xstrdup(target_list->data);
1184 target_list = target_list->link;
1185
1186 pch = strchr(iface, '=');
1187 if (pch) {
1188 *pch = '\0';
1189 liface = xstrdup(pch + 1);
1190 } else {
1191 liface = xstrdup(iface);
1192 }
1193
1194 if (!FORCE) {
1195 llist_t *state_list = read_iface_state();
1196 const llist_t *iface_state = find_iface_state(state_list, iface);
1197
1198 if (cmds == iface_up) {
1199
1200 if (iface_state) {
1201 bb_error_msg("interface %s already configured", iface);
1202 continue;
1203 }
1204 } else {
1205
1206 if (!iface_state) {
1207 bb_error_msg("interface %s not configured", iface);
1208 continue;
1209 }
1210 }
1211 llist_free(state_list, free);
1212 }
1213
1214#if ENABLE_FEATURE_IFUPDOWN_MAPPING
1215 if ((cmds == iface_up) && !NO_MAPPINGS) {
1216 struct mapping_defn_t *currmap;
1217
1218 for (currmap = defn->mappings; currmap; currmap = currmap->next) {
1219 int i;
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 iface_list = defn->ifaces;
1234 while (iface_list) {
1235 currif = (struct interface_defn_t *) iface_list->data;
1236 if (strcmp(liface, currif->iface) == 0) {
1237 char *oldiface = currif->iface;
1238
1239 okay = 1;
1240 currif->iface = iface;
1241
1242 debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
1243
1244
1245 cmds_ret = cmds(currif);
1246 if (cmds_ret == -1) {
1247 bb_error_msg("don't seem to have all the variables for %s/%s",
1248 liface, currif->address_family->name);
1249 any_failures = 1;
1250 } else if (cmds_ret == 0) {
1251 any_failures = 1;
1252 }
1253
1254 currif->iface = oldiface;
1255 }
1256 iface_list = iface_list->link;
1257 }
1258 if (VERBOSE) {
1259 bb_putchar('\n');
1260 }
1261
1262 if (!okay && !FORCE) {
1263 bb_error_msg("ignoring unknown interface %s", liface);
1264 any_failures = 1;
1265 } else if (!NO_ACT) {
1266
1267 FILE *state_fp;
1268 llist_t *state;
1269 llist_t *state_list = read_iface_state();
1270 llist_t *iface_state = find_iface_state(state_list, iface);
1271
1272 if (cmds == iface_up) {
1273 char * const newiface = xasprintf("%s=%s", iface, liface);
1274 if (iface_state == NULL) {
1275 llist_add_to_end(&state_list, newiface);
1276 } else {
1277 free(iface_state->data);
1278 iface_state->data = newiface;
1279 }
1280 } else {
1281
1282 llist_unlink(&state_list, iface_state);
1283 free(llist_pop(&iface_state));
1284 }
1285
1286
1287 state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
1288 state = state_list;
1289 while (state) {
1290 if (state->data) {
1291 fprintf(state_fp, "%s\n", state->data);
1292 }
1293 state = state->link;
1294 }
1295 fclose(state_fp);
1296 llist_free(state_list, free);
1297 }
1298 }
1299
1300 return any_failures;
1301}
1302