ifupdown.c 35 KB

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