ifupdown.c 34 KB

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