ifupdown.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322
  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 GPLv2 or later, see file LICENSE in this source tree.
  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 /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */
  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 manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
  310. {
  311. return 1;
  312. }
  313. static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec)
  314. {
  315. int result;
  316. # if ENABLE_FEATURE_IFUPDOWN_IP
  317. result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec);
  318. result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
  319. /* Was: "[[ ip ....%gateway% ]]". Removed extra spaces w/o checking */
  320. result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
  321. # else
  322. result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec);
  323. result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
  324. result += execute("[[route -A inet6 add ::/0 gw %gateway%]]", ifd, exec);
  325. # endif
  326. return ((result == 3) ? 3 : 0);
  327. }
  328. static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec)
  329. {
  330. # if ENABLE_FEATURE_IFUPDOWN_IP
  331. return execute("ip link set %iface% down", ifd, exec);
  332. # else
  333. return execute("ifconfig %iface% down", ifd, exec);
  334. # endif
  335. }
  336. # if ENABLE_FEATURE_IFUPDOWN_IP
  337. static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
  338. {
  339. int result;
  340. result = execute("ip tunnel add %iface% mode sit remote "
  341. "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec);
  342. result += execute("ip link set %iface% up", ifd, exec);
  343. result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
  344. result += execute("[[ip route add ::/0 via %gateway%]]", ifd, exec);
  345. return ((result == 4) ? 4 : 0);
  346. }
  347. static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
  348. {
  349. return execute("ip tunnel del %iface%", ifd, exec);
  350. }
  351. # endif
  352. static const struct method_t methods6[] = {
  353. # if ENABLE_FEATURE_IFUPDOWN_IP
  354. { "v4tunnel" , v4tunnel_up , v4tunnel_down , },
  355. # endif
  356. { "static" , static_up6 , static_down6 , },
  357. { "manual" , manual_up_down6 , manual_up_down6 , },
  358. { "loopback" , loopback_up6 , loopback_down6 , },
  359. };
  360. static const struct address_family_t addr_inet6 = {
  361. "inet6",
  362. ARRAY_SIZE(methods6),
  363. methods6
  364. };
  365. #endif /* FEATURE_IFUPDOWN_IPV6 */
  366. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  367. static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec)
  368. {
  369. # if ENABLE_FEATURE_IFUPDOWN_IP
  370. int result;
  371. result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
  372. result += execute("ip link set %iface% up", ifd, exec);
  373. return ((result == 2) ? 2 : 0);
  374. # else
  375. return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec);
  376. # endif
  377. }
  378. static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec)
  379. {
  380. # if ENABLE_FEATURE_IFUPDOWN_IP
  381. int result;
  382. result = execute("ip addr flush dev %iface%", ifd, exec);
  383. result += execute("ip link set %iface% down", ifd, exec);
  384. return ((result == 2) ? 2 : 0);
  385. # else
  386. return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec);
  387. # endif
  388. }
  389. static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec)
  390. {
  391. int result;
  392. # if ENABLE_FEATURE_IFUPDOWN_IP
  393. result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] "
  394. "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec);
  395. result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec);
  396. result += execute("[[ip route add default via %gateway% dev %iface%]]", ifd, exec);
  397. return ((result == 3) ? 3 : 0);
  398. # else
  399. /* ifconfig said to set iface up before it processes hw %hwaddress%,
  400. * which then of course fails. Thus we run two separate ifconfig */
  401. result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up",
  402. ifd, exec);
  403. result += execute("ifconfig %iface% %address% netmask %netmask%"
  404. "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]] ",
  405. ifd, exec);
  406. result += execute("[[route add default gw %gateway% %iface%]]", ifd, exec);
  407. return ((result == 3) ? 3 : 0);
  408. # endif
  409. }
  410. static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec)
  411. {
  412. int result;
  413. # if ENABLE_FEATURE_IFUPDOWN_IP
  414. result = execute("ip addr flush dev %iface%", ifd, exec);
  415. result += execute("ip link set %iface% down", ifd, exec);
  416. # else
  417. /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */
  418. /* Bringing the interface down deletes the routes in itself.
  419. Otherwise this fails if we reference 'gateway' when using this from dhcp_down */
  420. result = 1;
  421. result += execute("ifconfig %iface% down", ifd, exec);
  422. # endif
  423. return ((result == 2) ? 2 : 0);
  424. }
  425. # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  426. struct dhcp_client_t {
  427. const char *name;
  428. const char *startcmd;
  429. const char *stopcmd;
  430. };
  431. static const struct dhcp_client_t ext_dhcp_clients[] = {
  432. { "dhcpcd",
  433. "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%",
  434. "dhcpcd -k %iface%",
  435. },
  436. { "dhclient",
  437. "dhclient -pf /var/run/dhclient.%iface%.pid %iface%",
  438. "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null",
  439. },
  440. { "pump",
  441. "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]",
  442. "pump -i %iface% -k",
  443. },
  444. { "udhcpc",
  445. "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -H %hostname%]][[ -c %client%]]"
  446. "[[ -s %script%]][[ %udhcpc_opts%]]",
  447. "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
  448. },
  449. };
  450. # endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
  451. # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  452. static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  453. {
  454. unsigned i;
  455. # if ENABLE_FEATURE_IFUPDOWN_IP
  456. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  457. if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
  458. return 0;
  459. # else
  460. /* needed if we have hwaddress on dhcp iface */
  461. if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
  462. return 0;
  463. # endif
  464. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  465. if (exists_execable(ext_dhcp_clients[i].name))
  466. return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
  467. }
  468. bb_error_msg("no dhcp clients found");
  469. return 0;
  470. }
  471. # elif ENABLE_UDHCPC
  472. static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  473. {
  474. # if ENABLE_FEATURE_IFUPDOWN_IP
  475. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  476. if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec))
  477. return 0;
  478. # else
  479. /* needed if we have hwaddress on dhcp iface */
  480. if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec))
  481. return 0;
  482. # endif
  483. return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid "
  484. "-i %iface%[[ -H %hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]",
  485. ifd, exec);
  486. }
  487. # else
  488. static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM,
  489. execfn *exec UNUSED_PARAM)
  490. {
  491. return 0; /* no dhcp support */
  492. }
  493. # endif
  494. # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  495. static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  496. {
  497. int result = 0;
  498. unsigned i;
  499. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  500. if (exists_execable(ext_dhcp_clients[i].name)) {
  501. result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
  502. if (result)
  503. break;
  504. }
  505. }
  506. if (!result)
  507. bb_error_msg("warning: no dhcp clients found and stopped");
  508. /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
  509. and it may come back up because udhcpc is still shutting down */
  510. usleep(100000);
  511. result += static_down(ifd, exec);
  512. return ((result == 3) ? 3 : 0);
  513. }
  514. # elif ENABLE_UDHCPC
  515. static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  516. {
  517. int result;
  518. result = execute(
  519. "test -f /var/run/udhcpc.%iface%.pid && "
  520. "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
  521. ifd, exec);
  522. /* Also bring the hardware interface down since
  523. killing the dhcp client alone doesn't do it.
  524. This enables consecutive ifup->ifdown->ifup */
  525. /* Sleep a bit, otherwise static_down tries to bring down interface too soon,
  526. and it may come back up because udhcpc is still shutting down */
  527. usleep(100000);
  528. result += static_down(ifd, exec);
  529. return ((result == 3) ? 3 : 0);
  530. }
  531. # else
  532. static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM,
  533. execfn *exec UNUSED_PARAM)
  534. {
  535. return 0; /* no dhcp support */
  536. }
  537. # endif
  538. static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM)
  539. {
  540. return 1;
  541. }
  542. static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec)
  543. {
  544. return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
  545. "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
  546. " --returniffail --serverbcast", ifd, exec);
  547. }
  548. static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec)
  549. {
  550. return execute("pon[[ %provider%]]", ifd, exec);
  551. }
  552. static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec)
  553. {
  554. return execute("poff[[ %provider%]]", ifd, exec);
  555. }
  556. static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec)
  557. {
  558. return execute("start-stop-daemon --start -x wvdial "
  559. "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
  560. }
  561. static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec)
  562. {
  563. return execute("start-stop-daemon --stop -x wvdial "
  564. "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
  565. }
  566. static const struct method_t methods[] = {
  567. { "manual" , manual_up_down, manual_up_down, },
  568. { "wvdial" , wvdial_up , wvdial_down , },
  569. { "ppp" , ppp_up , ppp_down , },
  570. { "static" , static_up , static_down , },
  571. { "bootp" , bootp_up , static_down , },
  572. { "dhcp" , dhcp_up , dhcp_down , },
  573. { "loopback", loopback_up , loopback_down , },
  574. };
  575. static const struct address_family_t addr_inet = {
  576. "inet",
  577. ARRAY_SIZE(methods),
  578. methods
  579. };
  580. #endif /* FEATURE_IFUPDOWN_IPV4 */
  581. /* Returns pointer to the next word, or NULL.
  582. * In 1st case, advances *buf to the word after this one.
  583. */
  584. static char *next_word(char **buf)
  585. {
  586. unsigned length;
  587. char *word;
  588. /* Skip over leading whitespace */
  589. word = skip_whitespace(*buf);
  590. /* Stop on EOL */
  591. if (*word == '\0')
  592. return NULL;
  593. /* Find the length of this word (can't be 0) */
  594. length = strcspn(word, " \t\n");
  595. /* Unless we are already at NUL, store NUL and advance */
  596. if (word[length] != '\0')
  597. word[length++] = '\0';
  598. *buf = skip_whitespace(word + length);
  599. return word;
  600. }
  601. static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
  602. {
  603. int i;
  604. if (!name)
  605. return NULL;
  606. for (i = 0; af[i]; i++) {
  607. if (strcmp(af[i]->name, name) == 0) {
  608. return af[i];
  609. }
  610. }
  611. return NULL;
  612. }
  613. static const struct method_t *get_method(const struct address_family_t *af, char *name)
  614. {
  615. int i;
  616. if (!name)
  617. return NULL;
  618. /* TODO: use index_in_str_array() */
  619. for (i = 0; i < af->n_methods; i++) {
  620. if (strcmp(af->method[i].name, name) == 0) {
  621. return &af->method[i];
  622. }
  623. }
  624. return NULL;
  625. }
  626. static struct interfaces_file_t *read_interfaces(const char *filename)
  627. {
  628. /* Let's try to be compatible.
  629. *
  630. * "man 5 interfaces" says:
  631. * Lines starting with "#" are ignored. Note that end-of-line
  632. * comments are NOT supported, comments must be on a line of their own.
  633. * A line may be extended across multiple lines by making
  634. * the last character a backslash.
  635. *
  636. * Seen elsewhere in example config file:
  637. * A first non-blank "#" character makes the rest of the line
  638. * be ignored. Blank lines are ignored. Lines may be indented freely.
  639. * A "\" character at the very end of the line indicates the next line
  640. * should be treated as a continuation of the current one.
  641. */
  642. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  643. struct mapping_defn_t *currmap = NULL;
  644. #endif
  645. struct interface_defn_t *currif = NULL;
  646. struct interfaces_file_t *defn;
  647. FILE *f;
  648. char *buf;
  649. char *first_word;
  650. char *rest_of_line;
  651. enum { NONE, IFACE, MAPPING } currently_processing = NONE;
  652. defn = xzalloc(sizeof(*defn));
  653. f = xfopen_for_read(filename);
  654. while ((buf = xmalloc_fgetline(f)) != NULL) {
  655. #if ENABLE_DESKTOP
  656. /* Trailing "\" concatenates lines */
  657. char *p;
  658. while ((p = last_char_is(buf, '\\')) != NULL) {
  659. *p = '\0';
  660. rest_of_line = xmalloc_fgetline(f);
  661. if (!rest_of_line)
  662. break;
  663. p = xasprintf("%s%s", buf, rest_of_line);
  664. free(buf);
  665. free(rest_of_line);
  666. buf = p;
  667. }
  668. #endif
  669. rest_of_line = buf;
  670. first_word = next_word(&rest_of_line);
  671. if (!first_word || *first_word == '#') {
  672. free(buf);
  673. continue; /* blank/comment line */
  674. }
  675. if (strcmp(first_word, "mapping") == 0) {
  676. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  677. currmap = xzalloc(sizeof(*currmap));
  678. while ((first_word = next_word(&rest_of_line)) != NULL) {
  679. currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
  680. currmap->match[currmap->n_matches++] = xstrdup(first_word);
  681. }
  682. /*currmap->max_mappings = 0; - done by xzalloc */
  683. /*currmap->n_mappings = 0;*/
  684. /*currmap->mapping = NULL;*/
  685. /*currmap->script = NULL;*/
  686. {
  687. struct mapping_defn_t **where = &defn->mappings;
  688. while (*where != NULL) {
  689. where = &(*where)->next;
  690. }
  691. *where = currmap;
  692. /*currmap->next = NULL;*/
  693. }
  694. debug_noise("Added mapping\n");
  695. #endif
  696. currently_processing = MAPPING;
  697. } else if (strcmp(first_word, "iface") == 0) {
  698. static const struct address_family_t *const addr_fams[] = {
  699. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  700. &addr_inet,
  701. #endif
  702. #if ENABLE_FEATURE_IFUPDOWN_IPV6
  703. &addr_inet6,
  704. #endif
  705. NULL
  706. };
  707. char *iface_name;
  708. char *address_family_name;
  709. char *method_name;
  710. llist_t *iface_list;
  711. currif = xzalloc(sizeof(*currif));
  712. iface_name = next_word(&rest_of_line);
  713. address_family_name = next_word(&rest_of_line);
  714. method_name = next_word(&rest_of_line);
  715. if (method_name == NULL)
  716. bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
  717. /* ship any trailing whitespace */
  718. rest_of_line = skip_whitespace(rest_of_line);
  719. if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
  720. bb_error_msg_and_die("too many parameters \"%s\"", buf);
  721. currif->iface = xstrdup(iface_name);
  722. currif->address_family = get_address_family(addr_fams, address_family_name);
  723. if (!currif->address_family)
  724. bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
  725. currif->method = get_method(currif->address_family, method_name);
  726. if (!currif->method)
  727. bb_error_msg_and_die("unknown method \"%s\"", method_name);
  728. for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
  729. struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
  730. if ((strcmp(tmp->iface, currif->iface) == 0)
  731. && (tmp->address_family == currif->address_family)
  732. ) {
  733. bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
  734. }
  735. }
  736. llist_add_to_end(&(defn->ifaces), (char*)currif);
  737. debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
  738. currently_processing = IFACE;
  739. } else if (strcmp(first_word, "auto") == 0) {
  740. while ((first_word = next_word(&rest_of_line)) != NULL) {
  741. /* Check the interface isnt already listed */
  742. if (llist_find_str(defn->autointerfaces, first_word)) {
  743. bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
  744. }
  745. /* Add the interface to the list */
  746. llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
  747. debug_noise("\nauto %s\n", first_word);
  748. }
  749. currently_processing = NONE;
  750. } else {
  751. switch (currently_processing) {
  752. case IFACE:
  753. if (rest_of_line[0] == '\0')
  754. bb_error_msg_and_die("option with empty value \"%s\"", buf);
  755. if (strcmp(first_word, "up") != 0
  756. && strcmp(first_word, "down") != 0
  757. && strcmp(first_word, "pre-up") != 0
  758. && strcmp(first_word, "post-down") != 0
  759. ) {
  760. int i;
  761. for (i = 0; i < currif->n_options; i++) {
  762. if (strcmp(currif->option[i].name, first_word) == 0)
  763. bb_error_msg_and_die("duplicate option \"%s\"", buf);
  764. }
  765. }
  766. if (currif->n_options >= currif->max_options) {
  767. currif->max_options += 10;
  768. currif->option = xrealloc(currif->option,
  769. sizeof(*currif->option) * currif->max_options);
  770. }
  771. debug_noise("\t%s=%s\n", first_word, rest_of_line);
  772. currif->option[currif->n_options].name = xstrdup(first_word);
  773. currif->option[currif->n_options].value = xstrdup(rest_of_line);
  774. currif->n_options++;
  775. break;
  776. case MAPPING:
  777. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  778. if (strcmp(first_word, "script") == 0) {
  779. if (currmap->script != NULL)
  780. bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
  781. currmap->script = xstrdup(next_word(&rest_of_line));
  782. } else if (strcmp(first_word, "map") == 0) {
  783. if (currmap->n_mappings >= currmap->max_mappings) {
  784. currmap->max_mappings = currmap->max_mappings * 2 + 1;
  785. currmap->mapping = xrealloc(currmap->mapping,
  786. sizeof(char *) * currmap->max_mappings);
  787. }
  788. currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
  789. currmap->n_mappings++;
  790. } else {
  791. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  792. }
  793. #endif
  794. break;
  795. case NONE:
  796. default:
  797. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  798. }
  799. }
  800. free(buf);
  801. } /* while (fgets) */
  802. if (ferror(f) != 0) {
  803. /* ferror does NOT set errno! */
  804. bb_error_msg_and_die("%s: I/O error", filename);
  805. }
  806. fclose(f);
  807. return defn;
  808. }
  809. static char *setlocalenv(const char *format, const char *name, const char *value)
  810. {
  811. char *result;
  812. char *dst;
  813. char *src;
  814. char c;
  815. result = xasprintf(format, name, value);
  816. for (dst = src = result; (c = *src) != '=' && c; src++) {
  817. if (c == '-')
  818. c = '_';
  819. if (c >= 'a' && c <= 'z')
  820. c -= ('a' - 'A');
  821. if (isalnum(c) || c == '_')
  822. *dst++ = c;
  823. }
  824. overlapping_strcpy(dst, src);
  825. return result;
  826. }
  827. static void set_environ(struct interface_defn_t *iface, const char *mode)
  828. {
  829. int i;
  830. char **pp;
  831. if (G.my_environ != NULL) {
  832. for (pp = G.my_environ; *pp; pp++) {
  833. free(*pp);
  834. }
  835. free(G.my_environ);
  836. }
  837. /* note: last element will stay NULL: */
  838. G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 6));
  839. pp = G.my_environ;
  840. for (i = 0; i < iface->n_options; i++) {
  841. if (strcmp(iface->option[i].name, "up") == 0
  842. || strcmp(iface->option[i].name, "down") == 0
  843. || strcmp(iface->option[i].name, "pre-up") == 0
  844. || strcmp(iface->option[i].name, "post-down") == 0
  845. ) {
  846. continue;
  847. }
  848. *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
  849. }
  850. *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
  851. *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
  852. *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
  853. *pp++ = setlocalenv("%s=%s", "MODE", mode);
  854. if (G.startup_PATH)
  855. *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
  856. }
  857. static int doit(char *str)
  858. {
  859. if (option_mask32 & (OPT_no_act|OPT_verbose)) {
  860. puts(str);
  861. }
  862. if (!(option_mask32 & OPT_no_act)) {
  863. pid_t child;
  864. int status;
  865. fflush_all();
  866. child = vfork();
  867. switch (child) {
  868. case -1: /* failure */
  869. return 0;
  870. case 0: /* child */
  871. execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, (char *) NULL, G.my_environ);
  872. _exit(127);
  873. }
  874. safe_waitpid(child, &status, 0);
  875. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  876. return 0;
  877. }
  878. }
  879. return 1;
  880. }
  881. static int execute_all(struct interface_defn_t *ifd, const char *opt)
  882. {
  883. int i;
  884. char *buf;
  885. for (i = 0; i < ifd->n_options; i++) {
  886. if (strcmp(ifd->option[i].name, opt) == 0) {
  887. if (!doit(ifd->option[i].value)) {
  888. return 0;
  889. }
  890. }
  891. }
  892. buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
  893. /* heh, we don't bother free'ing it */
  894. return doit(buf);
  895. }
  896. static int check(char *str)
  897. {
  898. return str != NULL;
  899. }
  900. static int iface_up(struct interface_defn_t *iface)
  901. {
  902. if (!iface->method->up(iface, check)) return -1;
  903. set_environ(iface, "start");
  904. if (!execute_all(iface, "pre-up")) return 0;
  905. if (!iface->method->up(iface, doit)) return 0;
  906. if (!execute_all(iface, "up")) return 0;
  907. return 1;
  908. }
  909. static int iface_down(struct interface_defn_t *iface)
  910. {
  911. if (!iface->method->down(iface,check)) return -1;
  912. set_environ(iface, "stop");
  913. if (!execute_all(iface, "down")) return 0;
  914. if (!iface->method->down(iface, doit)) return 0;
  915. if (!execute_all(iface, "post-down")) return 0;
  916. return 1;
  917. }
  918. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  919. static int popen2(FILE **in, FILE **out, char *command, char *param)
  920. {
  921. char *argv[3] = { command, param, NULL };
  922. struct fd_pair infd, outfd;
  923. pid_t pid;
  924. xpiped_pair(infd);
  925. xpiped_pair(outfd);
  926. fflush_all();
  927. pid = xvfork();
  928. if (pid == 0) {
  929. /* Child */
  930. /* NB: close _first_, then move fds! */
  931. close(infd.wr);
  932. close(outfd.rd);
  933. xmove_fd(infd.rd, 0);
  934. xmove_fd(outfd.wr, 1);
  935. BB_EXECVP_or_die(argv);
  936. }
  937. /* parent */
  938. close(infd.rd);
  939. close(outfd.wr);
  940. *in = xfdopen_for_write(infd.wr);
  941. *out = xfdopen_for_read(outfd.rd);
  942. return pid;
  943. }
  944. static char *run_mapping(char *physical, struct mapping_defn_t *map)
  945. {
  946. FILE *in, *out;
  947. int i, status;
  948. pid_t pid;
  949. char *logical = xstrdup(physical);
  950. /* Run the mapping script. Never fails. */
  951. pid = popen2(&in, &out, map->script, physical);
  952. /* Write mappings to stdin of mapping script. */
  953. for (i = 0; i < map->n_mappings; i++) {
  954. fprintf(in, "%s\n", map->mapping[i]);
  955. }
  956. fclose(in);
  957. safe_waitpid(pid, &status, 0);
  958. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  959. /* If the mapping script exited successfully, try to
  960. * grab a line of output and use that as the name of the
  961. * logical interface. */
  962. char *new_logical = xmalloc_fgetline(out);
  963. if (new_logical) {
  964. /* If we are able to read a line of output from the script,
  965. * remove any trailing whitespace and use this value
  966. * as the name of the logical interface. */
  967. char *pch = new_logical + strlen(new_logical) - 1;
  968. while (pch >= new_logical && isspace(*pch))
  969. *(pch--) = '\0';
  970. free(logical);
  971. logical = new_logical;
  972. }
  973. }
  974. fclose(out);
  975. return logical;
  976. }
  977. #endif /* FEATURE_IFUPDOWN_MAPPING */
  978. static llist_t *find_iface_state(llist_t *state_list, const char *iface)
  979. {
  980. unsigned iface_len = strlen(iface);
  981. llist_t *search = state_list;
  982. while (search) {
  983. if ((strncmp(search->data, iface, iface_len) == 0)
  984. && (search->data[iface_len] == '=')
  985. ) {
  986. return search;
  987. }
  988. search = search->link;
  989. }
  990. return NULL;
  991. }
  992. /* read the previous state from the state file */
  993. static llist_t *read_iface_state(void)
  994. {
  995. llist_t *state_list = NULL;
  996. FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
  997. if (state_fp) {
  998. char *start, *end_ptr;
  999. while ((start = xmalloc_fgets(state_fp)) != NULL) {
  1000. /* We should only need to check for a single character */
  1001. end_ptr = start + strcspn(start, " \t\n");
  1002. *end_ptr = '\0';
  1003. llist_add_to(&state_list, start);
  1004. }
  1005. fclose(state_fp);
  1006. }
  1007. return state_list;
  1008. }
  1009. int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1010. int ifupdown_main(int argc UNUSED_PARAM, char **argv)
  1011. {
  1012. int (*cmds)(struct interface_defn_t *);
  1013. struct interfaces_file_t *defn;
  1014. llist_t *target_list = NULL;
  1015. const char *interfaces = "/etc/network/interfaces";
  1016. bool any_failures = 0;
  1017. INIT_G();
  1018. G.startup_PATH = getenv("PATH");
  1019. cmds = iface_down;
  1020. if (applet_name[2] == 'u') {
  1021. /* ifup command */
  1022. cmds = iface_up;
  1023. }
  1024. getopt32(argv, OPTION_STR, &interfaces);
  1025. argv += optind;
  1026. if (argv[0]) {
  1027. if (DO_ALL) bb_show_usage();
  1028. } else {
  1029. if (!DO_ALL) bb_show_usage();
  1030. }
  1031. debug_noise("reading %s file:\n", interfaces);
  1032. defn = read_interfaces(interfaces);
  1033. debug_noise("\ndone reading %s\n\n", interfaces);
  1034. /* Create a list of interfaces to work on */
  1035. if (DO_ALL) {
  1036. target_list = defn->autointerfaces;
  1037. } else {
  1038. llist_add_to_end(&target_list, argv[0]);
  1039. }
  1040. /* Update the interfaces */
  1041. while (target_list) {
  1042. llist_t *iface_list;
  1043. struct interface_defn_t *currif;
  1044. char *iface;
  1045. char *liface;
  1046. char *pch;
  1047. bool okay = 0;
  1048. int cmds_ret;
  1049. iface = xstrdup(target_list->data);
  1050. target_list = target_list->link;
  1051. pch = strchr(iface, '=');
  1052. if (pch) {
  1053. *pch = '\0';
  1054. liface = xstrdup(pch + 1);
  1055. } else {
  1056. liface = xstrdup(iface);
  1057. }
  1058. if (!FORCE) {
  1059. llist_t *state_list = read_iface_state();
  1060. const llist_t *iface_state = find_iface_state(state_list, iface);
  1061. if (cmds == iface_up) {
  1062. /* ifup */
  1063. if (iface_state) {
  1064. bb_error_msg("interface %s already configured", iface);
  1065. continue;
  1066. }
  1067. } else {
  1068. /* ifdown */
  1069. if (!iface_state) {
  1070. bb_error_msg("interface %s not configured", iface);
  1071. continue;
  1072. }
  1073. }
  1074. llist_free(state_list, free);
  1075. }
  1076. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  1077. if ((cmds == iface_up) && !NO_MAPPINGS) {
  1078. struct mapping_defn_t *currmap;
  1079. for (currmap = defn->mappings; currmap; currmap = currmap->next) {
  1080. int i;
  1081. for (i = 0; i < currmap->n_matches; i++) {
  1082. if (fnmatch(currmap->match[i], liface, 0) != 0)
  1083. continue;
  1084. if (VERBOSE) {
  1085. printf("Running mapping script %s on %s\n", currmap->script, liface);
  1086. }
  1087. liface = run_mapping(iface, currmap);
  1088. break;
  1089. }
  1090. }
  1091. }
  1092. #endif
  1093. iface_list = defn->ifaces;
  1094. while (iface_list) {
  1095. currif = (struct interface_defn_t *) iface_list->data;
  1096. if (strcmp(liface, currif->iface) == 0) {
  1097. char *oldiface = currif->iface;
  1098. okay = 1;
  1099. currif->iface = iface;
  1100. debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
  1101. /* Call the cmds function pointer, does either iface_up() or iface_down() */
  1102. cmds_ret = cmds(currif);
  1103. if (cmds_ret == -1) {
  1104. bb_error_msg("don't seem to have all the variables for %s/%s",
  1105. liface, currif->address_family->name);
  1106. any_failures = 1;
  1107. } else if (cmds_ret == 0) {
  1108. any_failures = 1;
  1109. }
  1110. currif->iface = oldiface;
  1111. }
  1112. iface_list = iface_list->link;
  1113. }
  1114. if (VERBOSE) {
  1115. bb_putchar('\n');
  1116. }
  1117. if (!okay && !FORCE) {
  1118. bb_error_msg("ignoring unknown interface %s", liface);
  1119. any_failures = 1;
  1120. } else if (!NO_ACT) {
  1121. /* update the state file */
  1122. FILE *state_fp;
  1123. llist_t *state;
  1124. llist_t *state_list = read_iface_state();
  1125. llist_t *iface_state = find_iface_state(state_list, iface);
  1126. if (cmds == iface_up) {
  1127. char * const newiface = xasprintf("%s=%s", iface, liface);
  1128. if (iface_state == NULL) {
  1129. llist_add_to_end(&state_list, newiface);
  1130. } else {
  1131. free(iface_state->data);
  1132. iface_state->data = newiface;
  1133. }
  1134. } else {
  1135. /* Remove an interface from state_list */
  1136. llist_unlink(&state_list, iface_state);
  1137. free(llist_pop(&iface_state));
  1138. }
  1139. /* Actually write the new state */
  1140. state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
  1141. state = state_list;
  1142. while (state) {
  1143. if (state->data) {
  1144. fprintf(state_fp, "%s\n", state->data);
  1145. }
  1146. state = state->link;
  1147. }
  1148. fclose(state_fp);
  1149. llist_free(state_list, free);
  1150. }
  1151. }
  1152. return any_failures;
  1153. }