3
0

ifupdown.c 42 KB

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