ifupdown.c 35 KB

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