ifupdown.c 36 KB

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