ifupdown.c 36 KB

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