ifupdown.c 32 KB

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