ifupdown.c 36 KB

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