ifupdown.c 34 KB

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