ifupdown.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  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. }