ifupdown.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  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 (strncmp(command, "hwaddress", 9) == 0) {
  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 (strncmp(command, "bnmask", 6) == 0) {
  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%]][[ prio %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%[[ prio %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 (exists_execable(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 (exists_execable(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. /* Returns pointer to the next word, or NULL.
  606. * In 1st case, advances *buf to the word after this one.
  607. */
  608. static char *next_word(char **buf)
  609. {
  610. unsigned length;
  611. char *word;
  612. /* Skip over leading whitespace */
  613. word = skip_whitespace(*buf);
  614. /* Stop on EOL */
  615. if (*word == '\0')
  616. return NULL;
  617. /* Find the length of this word (can't be 0) */
  618. length = strcspn(word, " \t\n");
  619. /* Unless we are already at NUL, store NUL and advance */
  620. if (word[length] != '\0')
  621. word[length++] = '\0';
  622. *buf = skip_whitespace(word + length);
  623. return word;
  624. }
  625. static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
  626. {
  627. int i;
  628. if (!name)
  629. return NULL;
  630. for (i = 0; af[i]; i++) {
  631. if (strcmp(af[i]->name, name) == 0) {
  632. return af[i];
  633. }
  634. }
  635. return NULL;
  636. }
  637. static const struct method_t *get_method(const struct address_family_t *af, char *name)
  638. {
  639. int i;
  640. if (!name)
  641. return NULL;
  642. /* TODO: use index_in_str_array() */
  643. for (i = 0; i < af->n_methods; i++) {
  644. if (strcmp(af->method[i].name, name) == 0) {
  645. return &af->method[i];
  646. }
  647. }
  648. return NULL;
  649. }
  650. static struct interfaces_file_t *read_interfaces(const char *filename)
  651. {
  652. /* Let's try to be compatible.
  653. *
  654. * "man 5 interfaces" says:
  655. * Lines starting with "#" are ignored. Note that end-of-line
  656. * comments are NOT supported, comments must be on a line of their own.
  657. * A line may be extended across multiple lines by making
  658. * the last character a backslash.
  659. *
  660. * Seen elsewhere in example config file:
  661. * A first non-blank "#" character makes the rest of the line
  662. * be ignored. Blank lines are ignored. Lines may be indented freely.
  663. * A "\" character at the very end of the line indicates the next line
  664. * should be treated as a continuation of the current one.
  665. */
  666. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  667. struct mapping_defn_t *currmap = NULL;
  668. #endif
  669. struct interface_defn_t *currif = NULL;
  670. struct interfaces_file_t *defn;
  671. FILE *f;
  672. char *buf;
  673. char *first_word;
  674. char *rest_of_line;
  675. enum { NONE, IFACE, MAPPING } currently_processing = NONE;
  676. defn = xzalloc(sizeof(*defn));
  677. f = xfopen_for_read(filename);
  678. while ((buf = xmalloc_fgetline(f)) != NULL) {
  679. #if ENABLE_DESKTOP
  680. /* Trailing "\" concatenates lines */
  681. char *p;
  682. while ((p = last_char_is(buf, '\\')) != NULL) {
  683. *p = '\0';
  684. rest_of_line = xmalloc_fgetline(f);
  685. if (!rest_of_line)
  686. break;
  687. p = xasprintf("%s%s", buf, rest_of_line);
  688. free(buf);
  689. free(rest_of_line);
  690. buf = p;
  691. }
  692. #endif
  693. rest_of_line = buf;
  694. first_word = next_word(&rest_of_line);
  695. if (!first_word || *first_word == '#') {
  696. free(buf);
  697. continue; /* blank/comment line */
  698. }
  699. if (strcmp(first_word, "mapping") == 0) {
  700. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  701. currmap = xzalloc(sizeof(*currmap));
  702. while ((first_word = next_word(&rest_of_line)) != NULL) {
  703. currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches);
  704. currmap->match[currmap->n_matches++] = xstrdup(first_word);
  705. }
  706. /*currmap->n_mappings = 0;*/
  707. /*currmap->mapping = NULL;*/
  708. /*currmap->script = NULL;*/
  709. {
  710. struct mapping_defn_t **where = &defn->mappings;
  711. while (*where != NULL) {
  712. where = &(*where)->next;
  713. }
  714. *where = currmap;
  715. /*currmap->next = NULL;*/
  716. }
  717. debug_noise("Added mapping\n");
  718. #endif
  719. currently_processing = MAPPING;
  720. } else if (strcmp(first_word, "iface") == 0) {
  721. static const struct address_family_t *const addr_fams[] = {
  722. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  723. &addr_inet,
  724. #endif
  725. #if ENABLE_FEATURE_IFUPDOWN_IPV6
  726. &addr_inet6,
  727. #endif
  728. NULL
  729. };
  730. char *iface_name;
  731. char *address_family_name;
  732. char *method_name;
  733. llist_t *iface_list;
  734. currif = xzalloc(sizeof(*currif));
  735. iface_name = next_word(&rest_of_line);
  736. address_family_name = next_word(&rest_of_line);
  737. method_name = next_word(&rest_of_line);
  738. if (method_name == NULL)
  739. bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
  740. /* ship any trailing whitespace */
  741. rest_of_line = skip_whitespace(rest_of_line);
  742. if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
  743. bb_error_msg_and_die("too many parameters \"%s\"", buf);
  744. currif->iface = xstrdup(iface_name);
  745. currif->address_family = get_address_family(addr_fams, address_family_name);
  746. if (!currif->address_family)
  747. bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
  748. currif->method = get_method(currif->address_family, method_name);
  749. if (!currif->method)
  750. bb_error_msg_and_die("unknown method \"%s\"", method_name);
  751. for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
  752. struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
  753. if ((strcmp(tmp->iface, currif->iface) == 0)
  754. && (tmp->address_family == currif->address_family)
  755. ) {
  756. bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
  757. }
  758. }
  759. llist_add_to_end(&(defn->ifaces), (char*)currif);
  760. debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
  761. currently_processing = IFACE;
  762. } else if (strcmp(first_word, "auto") == 0) {
  763. while ((first_word = next_word(&rest_of_line)) != NULL) {
  764. /* Check the interface isnt already listed */
  765. if (llist_find_str(defn->autointerfaces, first_word)) {
  766. bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
  767. }
  768. /* Add the interface to the list */
  769. llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
  770. debug_noise("\nauto %s\n", first_word);
  771. }
  772. currently_processing = NONE;
  773. } else {
  774. switch (currently_processing) {
  775. case IFACE:
  776. if (rest_of_line[0] == '\0')
  777. bb_error_msg_and_die("option with empty value \"%s\"", buf);
  778. if (strcmp(first_word, "post-up") == 0)
  779. first_word += 5; /* "up" */
  780. else if (strcmp(first_word, "pre-down") == 0)
  781. first_word += 4; /* "down" */
  782. /* If not one of "up", "down",... words... */
  783. if (index_in_strings(keywords_up_down, first_word) < 0) {
  784. int i;
  785. for (i = 0; i < currif->n_options; i++) {
  786. if (strcmp(currif->option[i].name, first_word) == 0)
  787. bb_error_msg_and_die("duplicate option \"%s\"", buf);
  788. }
  789. }
  790. debug_noise("\t%s=%s\n", first_word, rest_of_line);
  791. currif->option = xrealloc_vector(currif->option, 4, currif->n_options);
  792. currif->option[currif->n_options].name = xstrdup(first_word);
  793. currif->option[currif->n_options].value = xstrdup(rest_of_line);
  794. currif->n_options++;
  795. break;
  796. case MAPPING:
  797. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  798. if (strcmp(first_word, "script") == 0) {
  799. if (currmap->script != NULL)
  800. bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
  801. currmap->script = xstrdup(next_word(&rest_of_line));
  802. } else if (strcmp(first_word, "map") == 0) {
  803. currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings);
  804. currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
  805. currmap->n_mappings++;
  806. } else {
  807. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  808. }
  809. #endif
  810. break;
  811. case NONE:
  812. default:
  813. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  814. }
  815. }
  816. free(buf);
  817. } /* while (fgets) */
  818. if (ferror(f) != 0) {
  819. /* ferror does NOT set errno! */
  820. bb_error_msg_and_die("%s: I/O error", filename);
  821. }
  822. fclose(f);
  823. return defn;
  824. }
  825. static char *setlocalenv(const char *format, const char *name, const char *value)
  826. {
  827. char *result;
  828. char *dst;
  829. char *src;
  830. char c;
  831. result = xasprintf(format, name, value);
  832. for (dst = src = result; (c = *src) != '=' && c; src++) {
  833. if (c == '-')
  834. c = '_';
  835. if (c >= 'a' && c <= 'z')
  836. c -= ('a' - 'A');
  837. if (isalnum(c) || c == '_')
  838. *dst++ = c;
  839. }
  840. overlapping_strcpy(dst, src);
  841. return result;
  842. }
  843. static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt)
  844. {
  845. int i;
  846. char **pp;
  847. if (G.my_environ != NULL) {
  848. for (pp = G.my_environ; *pp; pp++) {
  849. free(*pp);
  850. }
  851. free(G.my_environ);
  852. }
  853. /* note: last element will stay NULL: */
  854. G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7));
  855. pp = G.my_environ;
  856. for (i = 0; i < iface->n_options; i++) {
  857. if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) {
  858. continue;
  859. }
  860. *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
  861. }
  862. *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface);
  863. *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
  864. *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name);
  865. *pp++ = setlocalenv("%s=%s", "MODE", mode);
  866. *pp++ = setlocalenv("%s=%s", "PHASE", opt);
  867. if (G.startup_PATH)
  868. *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH);
  869. }
  870. static int doit(char *str)
  871. {
  872. if (option_mask32 & (OPT_no_act|OPT_verbose)) {
  873. puts(str);
  874. }
  875. if (!(option_mask32 & OPT_no_act)) {
  876. pid_t child;
  877. int status;
  878. fflush_all();
  879. child = vfork();
  880. if (child < 0) /* failure */
  881. return 0;
  882. if (child == 0) { /* child */
  883. execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ);
  884. _exit(127);
  885. }
  886. safe_waitpid(child, &status, 0);
  887. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  888. return 0;
  889. }
  890. }
  891. return 1;
  892. }
  893. static int execute_all(struct interface_defn_t *ifd, const char *opt)
  894. {
  895. int i;
  896. char *buf;
  897. for (i = 0; i < ifd->n_options; i++) {
  898. if (strcmp(ifd->option[i].name, opt) == 0) {
  899. if (!doit(ifd->option[i].value)) {
  900. return 0;
  901. }
  902. }
  903. }
  904. buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
  905. /* heh, we don't bother free'ing it */
  906. return doit(buf);
  907. }
  908. static int check(char *str)
  909. {
  910. return str != NULL;
  911. }
  912. static int iface_up(struct interface_defn_t *iface)
  913. {
  914. if (!iface->method->up(iface, check)) return -1;
  915. set_environ(iface, "start", "pre-up");
  916. if (!execute_all(iface, "pre-up")) return 0;
  917. if (!iface->method->up(iface, doit)) return 0;
  918. set_environ(iface, "start", "post-up");
  919. if (!execute_all(iface, "up")) return 0;
  920. return 1;
  921. }
  922. static int iface_down(struct interface_defn_t *iface)
  923. {
  924. if (!iface->method->down(iface, check)) return -1;
  925. set_environ(iface, "stop", "pre-down");
  926. if (!execute_all(iface, "down")) return 0;
  927. if (!iface->method->down(iface, doit)) return 0;
  928. set_environ(iface, "stop", "post-down");
  929. if (!execute_all(iface, "post-down")) return 0;
  930. return 1;
  931. }
  932. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  933. static int popen2(FILE **in, FILE **out, char *command, char *param)
  934. {
  935. char *argv[3] = { command, param, NULL };
  936. struct fd_pair infd, outfd;
  937. pid_t pid;
  938. xpiped_pair(infd);
  939. xpiped_pair(outfd);
  940. fflush_all();
  941. pid = xvfork();
  942. if (pid == 0) {
  943. /* Child */
  944. /* NB: close _first_, then move fds! */
  945. close(infd.wr);
  946. close(outfd.rd);
  947. xmove_fd(infd.rd, 0);
  948. xmove_fd(outfd.wr, 1);
  949. BB_EXECVP_or_die(argv);
  950. }
  951. /* parent */
  952. close(infd.rd);
  953. close(outfd.wr);
  954. *in = xfdopen_for_write(infd.wr);
  955. *out = xfdopen_for_read(outfd.rd);
  956. return pid;
  957. }
  958. static char *run_mapping(char *physical, struct mapping_defn_t *map)
  959. {
  960. FILE *in, *out;
  961. int i, status;
  962. pid_t pid;
  963. char *logical = xstrdup(physical);
  964. /* Run the mapping script. Never fails. */
  965. pid = popen2(&in, &out, map->script, physical);
  966. /* Write mappings to stdin of mapping script. */
  967. for (i = 0; i < map->n_mappings; i++) {
  968. fprintf(in, "%s\n", map->mapping[i]);
  969. }
  970. fclose(in);
  971. safe_waitpid(pid, &status, 0);
  972. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  973. /* If the mapping script exited successfully, try to
  974. * grab a line of output and use that as the name of the
  975. * logical interface. */
  976. char *new_logical = xmalloc_fgetline(out);
  977. if (new_logical) {
  978. /* If we are able to read a line of output from the script,
  979. * remove any trailing whitespace and use this value
  980. * as the name of the logical interface. */
  981. char *pch = new_logical + strlen(new_logical) - 1;
  982. while (pch >= new_logical && isspace(*pch))
  983. *(pch--) = '\0';
  984. free(logical);
  985. logical = new_logical;
  986. }
  987. }
  988. fclose(out);
  989. return logical;
  990. }
  991. #endif /* FEATURE_IFUPDOWN_MAPPING */
  992. static llist_t *find_iface_state(llist_t *state_list, const char *iface)
  993. {
  994. unsigned iface_len = strlen(iface);
  995. llist_t *search = state_list;
  996. while (search) {
  997. if ((strncmp(search->data, iface, iface_len) == 0)
  998. && (search->data[iface_len] == '=')
  999. ) {
  1000. return search;
  1001. }
  1002. search = search->link;
  1003. }
  1004. return NULL;
  1005. }
  1006. /* read the previous state from the state file */
  1007. static llist_t *read_iface_state(void)
  1008. {
  1009. llist_t *state_list = NULL;
  1010. FILE *state_fp = fopen_for_read(CONFIG_IFUPDOWN_IFSTATE_PATH);
  1011. if (state_fp) {
  1012. char *start, *end_ptr;
  1013. while ((start = xmalloc_fgets(state_fp)) != NULL) {
  1014. /* We should only need to check for a single character */
  1015. end_ptr = start + strcspn(start, " \t\n");
  1016. *end_ptr = '\0';
  1017. llist_add_to(&state_list, start);
  1018. }
  1019. fclose(state_fp);
  1020. }
  1021. return state_list;
  1022. }
  1023. int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  1024. int ifupdown_main(int argc UNUSED_PARAM, char **argv)
  1025. {
  1026. int (*cmds)(struct interface_defn_t *);
  1027. struct interfaces_file_t *defn;
  1028. llist_t *target_list = NULL;
  1029. const char *interfaces = "/etc/network/interfaces";
  1030. bool any_failures = 0;
  1031. INIT_G();
  1032. G.startup_PATH = getenv("PATH");
  1033. G.shell = xstrdup(get_shell_name());
  1034. cmds = iface_down;
  1035. if (applet_name[2] == 'u') {
  1036. /* ifup command */
  1037. cmds = iface_up;
  1038. }
  1039. getopt32(argv, OPTION_STR, &interfaces);
  1040. argv += optind;
  1041. if (argv[0]) {
  1042. if (DO_ALL) bb_show_usage();
  1043. } else {
  1044. if (!DO_ALL) bb_show_usage();
  1045. }
  1046. debug_noise("reading %s file:\n", interfaces);
  1047. defn = read_interfaces(interfaces);
  1048. debug_noise("\ndone reading %s\n\n", interfaces);
  1049. /* Create a list of interfaces to work on */
  1050. if (DO_ALL) {
  1051. target_list = defn->autointerfaces;
  1052. } else {
  1053. llist_add_to_end(&target_list, argv[0]);
  1054. }
  1055. /* Update the interfaces */
  1056. while (target_list) {
  1057. llist_t *iface_list;
  1058. struct interface_defn_t *currif;
  1059. char *iface;
  1060. char *liface;
  1061. char *pch;
  1062. bool okay = 0;
  1063. int cmds_ret;
  1064. iface = xstrdup(target_list->data);
  1065. target_list = target_list->link;
  1066. pch = strchr(iface, '=');
  1067. if (pch) {
  1068. *pch = '\0';
  1069. liface = xstrdup(pch + 1);
  1070. } else {
  1071. liface = xstrdup(iface);
  1072. }
  1073. if (!FORCE) {
  1074. llist_t *state_list = read_iface_state();
  1075. const llist_t *iface_state = find_iface_state(state_list, iface);
  1076. if (cmds == iface_up) {
  1077. /* ifup */
  1078. if (iface_state) {
  1079. bb_error_msg("interface %s already configured", iface);
  1080. goto next;
  1081. }
  1082. } else {
  1083. /* ifdown */
  1084. if (!iface_state) {
  1085. bb_error_msg("interface %s not configured", iface);
  1086. goto next;
  1087. }
  1088. }
  1089. llist_free(state_list, free);
  1090. }
  1091. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  1092. if ((cmds == iface_up) && !NO_MAPPINGS) {
  1093. struct mapping_defn_t *currmap;
  1094. for (currmap = defn->mappings; currmap; currmap = currmap->next) {
  1095. int i;
  1096. for (i = 0; i < currmap->n_matches; i++) {
  1097. if (fnmatch(currmap->match[i], liface, 0) != 0)
  1098. continue;
  1099. if (VERBOSE) {
  1100. printf("Running mapping script %s on %s\n", currmap->script, liface);
  1101. }
  1102. liface = run_mapping(iface, currmap);
  1103. break;
  1104. }
  1105. }
  1106. }
  1107. #endif
  1108. iface_list = defn->ifaces;
  1109. while (iface_list) {
  1110. currif = (struct interface_defn_t *) iface_list->data;
  1111. if (strcmp(liface, currif->iface) == 0) {
  1112. char *oldiface = currif->iface;
  1113. okay = 1;
  1114. currif->iface = iface;
  1115. debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
  1116. /* Call the cmds function pointer, does either iface_up() or iface_down() */
  1117. cmds_ret = cmds(currif);
  1118. if (cmds_ret == -1) {
  1119. bb_error_msg("don't seem to have all the variables for %s/%s",
  1120. liface, currif->address_family->name);
  1121. any_failures = 1;
  1122. } else if (cmds_ret == 0) {
  1123. any_failures = 1;
  1124. }
  1125. currif->iface = oldiface;
  1126. }
  1127. iface_list = iface_list->link;
  1128. }
  1129. if (VERBOSE) {
  1130. bb_putchar('\n');
  1131. }
  1132. if (!okay && !FORCE) {
  1133. bb_error_msg("ignoring unknown interface %s", liface);
  1134. any_failures = 1;
  1135. } else if (!NO_ACT) {
  1136. /* update the state file */
  1137. FILE *state_fp;
  1138. llist_t *state;
  1139. llist_t *state_list = read_iface_state();
  1140. llist_t *iface_state = find_iface_state(state_list, iface);
  1141. if (cmds == iface_up && !any_failures) {
  1142. char *newiface = xasprintf("%s=%s", iface, liface);
  1143. if (!iface_state) {
  1144. llist_add_to_end(&state_list, newiface);
  1145. } else {
  1146. free(iface_state->data);
  1147. iface_state->data = newiface;
  1148. }
  1149. } else {
  1150. /* Remove an interface from state_list */
  1151. llist_unlink(&state_list, iface_state);
  1152. free(llist_pop(&iface_state));
  1153. }
  1154. /* Actually write the new state */
  1155. state_fp = xfopen_for_write(CONFIG_IFUPDOWN_IFSTATE_PATH);
  1156. state = state_list;
  1157. while (state) {
  1158. if (state->data) {
  1159. fprintf(state_fp, "%s\n", state->data);
  1160. }
  1161. state = state->link;
  1162. }
  1163. fclose(state_fp);
  1164. llist_free(state_list, free);
  1165. }
  1166. next:
  1167. free(iface);
  1168. free(liface);
  1169. }
  1170. return any_failures;
  1171. }