ifupdown.c 33 KB

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