ifupdown.c 36 KB

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