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