3
0

ifupdown.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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 "libbb.h"
  22. #define MAX_OPT_DEPTH 10
  23. #define EUNBALBRACK 10001
  24. #define EUNDEFVAR 10002
  25. #define EUNBALPER 10000
  26. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  27. #define MAX_INTERFACE_LENGTH 10
  28. #endif
  29. #define debug_noise(args...) /*fprintf(stderr, args)*/
  30. /* Forward declaration */
  31. struct interface_defn_t;
  32. typedef int execfn(char *command);
  33. struct method_t {
  34. const char *name;
  35. int (*up)(struct interface_defn_t *ifd, execfn *e);
  36. int (*down)(struct interface_defn_t *ifd, execfn *e);
  37. };
  38. struct address_family_t {
  39. const char *name;
  40. int n_methods;
  41. const struct method_t *method;
  42. };
  43. struct mapping_defn_t {
  44. struct mapping_defn_t *next;
  45. int max_matches;
  46. int n_matches;
  47. char **match;
  48. char *script;
  49. int max_mappings;
  50. int n_mappings;
  51. char **mapping;
  52. };
  53. struct variable_t {
  54. char *name;
  55. char *value;
  56. };
  57. struct interface_defn_t {
  58. const struct address_family_t *address_family;
  59. const struct method_t *method;
  60. char *iface;
  61. int max_options;
  62. int n_options;
  63. struct variable_t *option;
  64. };
  65. struct interfaces_file_t {
  66. llist_t *autointerfaces;
  67. llist_t *ifaces;
  68. struct mapping_defn_t *mappings;
  69. };
  70. #define OPTION_STR "anvf" USE_FEATURE_IFUPDOWN_MAPPING("m") "i:"
  71. enum {
  72. OPT_do_all = 0x1,
  73. OPT_no_act = 0x2,
  74. OPT_verbose = 0x4,
  75. OPT_force = 0x8,
  76. OPT_no_mappings = 0x10,
  77. };
  78. #define DO_ALL (option_mask32 & OPT_do_all)
  79. #define NO_ACT (option_mask32 & OPT_no_act)
  80. #define VERBOSE (option_mask32 & OPT_verbose)
  81. #define FORCE (option_mask32 & OPT_force)
  82. #define NO_MAPPINGS (option_mask32 & OPT_no_mappings)
  83. static char **my_environ;
  84. static const char *startup_PATH;
  85. #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6
  86. static void addstr(char **bufp, const char *str, size_t str_length)
  87. {
  88. /* xasprintf trick will be smaller, but we are often
  89. * called with str_length == 1 - don't want to have
  90. * THAT much of malloc/freeing! */
  91. char *buf = *bufp;
  92. int len = (buf ? strlen(buf) : 0);
  93. str_length++;
  94. buf = xrealloc(buf, len + str_length);
  95. /* copies at most str_length-1 chars! */
  96. safe_strncpy(buf + len, str, str_length);
  97. *bufp = buf;
  98. }
  99. static int strncmpz(const char *l, const char *r, size_t llen)
  100. {
  101. int i = strncmp(l, r, llen);
  102. if (i == 0)
  103. return -r[llen];
  104. return i;
  105. }
  106. static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd)
  107. {
  108. int i;
  109. if (strncmpz(id, "iface", idlen) == 0) {
  110. static char *label_buf;
  111. //char *result;
  112. free(label_buf);
  113. label_buf = xstrdup(ifd->iface);
  114. // Remove virtual iface suffix - why?
  115. // ubuntu's ifup doesn't do this
  116. //result = strchrnul(label_buf, ':');
  117. //*result = '\0';
  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%]]"
  424. "[[ -s %script%]][[ %udhcpc_opts%]]",
  425. "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null",
  426. },
  427. };
  428. #endif /* ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCPC */
  429. #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  430. static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  431. {
  432. unsigned i;
  433. #if ENABLE_FEATURE_IFUPDOWN_IP
  434. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  435. if (!execute("ip link set %iface% up", ifd, exec))
  436. return 0;
  437. #endif
  438. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  439. if (exists_execable(ext_dhcp_clients[i].name))
  440. return execute(ext_dhcp_clients[i].startcmd, ifd, exec);
  441. }
  442. bb_error_msg("no dhcp clients found");
  443. return 0;
  444. }
  445. #elif ENABLE_APP_UDHCPC
  446. static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  447. {
  448. #if ENABLE_FEATURE_IFUPDOWN_IP
  449. /* ip doesn't up iface when it configures it (unlike ifconfig) */
  450. if (!execute("ip link set %iface% up", ifd, exec))
  451. return 0;
  452. #endif
  453. return execute("udhcpc -R -n -p /var/run/udhcpc.%iface%.pid "
  454. "-i %iface%[[ -H %hostname%]][[ -c %clientid%]][[ -s %script%]][[ %udhcpc_opts%]]",
  455. ifd, exec);
  456. }
  457. #else
  458. static int dhcp_up(struct interface_defn_t *ifd ATTRIBUTE_UNUSED,
  459. execfn *exec ATTRIBUTE_UNUSED)
  460. {
  461. return 0; /* no dhcp support */
  462. }
  463. #endif
  464. #if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP
  465. static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  466. {
  467. unsigned i;
  468. for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) {
  469. if (exists_execable(ext_dhcp_clients[i].name))
  470. return execute(ext_dhcp_clients[i].stopcmd, ifd, exec);
  471. }
  472. bb_error_msg("no dhcp clients found, using static interface shutdown");
  473. return static_down(ifd, exec);
  474. }
  475. #elif ENABLE_APP_UDHCPC
  476. static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  477. {
  478. return execute("kill "
  479. "`cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
  480. }
  481. #else
  482. static int dhcp_down(struct interface_defn_t *ifd ATTRIBUTE_UNUSED,
  483. execfn *exec ATTRIBUTE_UNUSED)
  484. {
  485. return 0; /* no dhcp support */
  486. }
  487. #endif
  488. static int manual_up_down(struct interface_defn_t *ifd ATTRIBUTE_UNUSED, execfn *exec ATTRIBUTE_UNUSED)
  489. {
  490. return 1;
  491. }
  492. static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
  493. {
  494. return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%"
  495. "[[ --server %server%]][[ --hwaddr %hwaddr%]]"
  496. " --returniffail --serverbcast", ifd, exec);
  497. }
  498. static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
  499. {
  500. return execute("pon[[ %provider%]]", ifd, exec);
  501. }
  502. static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
  503. {
  504. return execute("poff[[ %provider%]]", ifd, exec);
  505. }
  506. static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
  507. {
  508. return execute("start-stop-daemon --start -x wvdial "
  509. "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec);
  510. }
  511. static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
  512. {
  513. return execute("start-stop-daemon --stop -x wvdial "
  514. "-p /var/run/wvdial.%iface% -s 2", ifd, exec);
  515. }
  516. static const struct method_t methods[] = {
  517. { "manual", manual_up_down, manual_up_down, },
  518. { "wvdial", wvdial_up, wvdial_down, },
  519. { "ppp", ppp_up, ppp_down, },
  520. { "static", static_up, static_down, },
  521. { "bootp", bootp_up, static_down, },
  522. { "dhcp", dhcp_up, dhcp_down, },
  523. { "loopback", loopback_up, loopback_down, },
  524. };
  525. static const struct address_family_t addr_inet = {
  526. "inet",
  527. ARRAY_SIZE(methods),
  528. methods
  529. };
  530. #endif /* if ENABLE_FEATURE_IFUPDOWN_IPV4 */
  531. static char *next_word(char **buf)
  532. {
  533. unsigned length;
  534. char *word;
  535. /* Skip over leading whitespace */
  536. word = skip_whitespace(*buf);
  537. /* Stop on EOL */
  538. if (*word == '\0')
  539. return NULL;
  540. /* Find the length of this word (can't be 0) */
  541. length = strcspn(word, " \t\n");
  542. /* Unless we are already at NUL, store NUL and advance */
  543. if (word[length] != '\0')
  544. word[length++] = '\0';
  545. *buf = word + length;
  546. return word;
  547. }
  548. static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name)
  549. {
  550. int i;
  551. if (!name)
  552. return NULL;
  553. for (i = 0; af[i]; i++) {
  554. if (strcmp(af[i]->name, name) == 0) {
  555. return af[i];
  556. }
  557. }
  558. return NULL;
  559. }
  560. static const struct method_t *get_method(const struct address_family_t *af, char *name)
  561. {
  562. int i;
  563. if (!name)
  564. return NULL;
  565. /* TODO: use index_in_str_array() */
  566. for (i = 0; i < af->n_methods; i++) {
  567. if (strcmp(af->method[i].name, name) == 0) {
  568. return &af->method[i];
  569. }
  570. }
  571. return NULL;
  572. }
  573. static const llist_t *find_list_string(const llist_t *list, const char *string)
  574. {
  575. if (string == NULL)
  576. return NULL;
  577. while (list) {
  578. if (strcmp(list->data, string) == 0) {
  579. return list;
  580. }
  581. list = list->link;
  582. }
  583. return NULL;
  584. }
  585. static struct interfaces_file_t *read_interfaces(const char *filename)
  586. {
  587. /* Let's try to be compatible.
  588. *
  589. * "man 5 interfaces" says:
  590. * Lines starting with "#" are ignored. Note that end-of-line
  591. * comments are NOT supported, comments must be on a line of their own.
  592. * A line may be extended across multiple lines by making
  593. * the last character a backslash.
  594. *
  595. * Seen elsewhere in example config file:
  596. * A "#" character in the very first column makes the rest of the line
  597. * be ignored. Blank lines are ignored. Lines may be indented freely.
  598. * A "\" character at the very end of the line indicates the next line
  599. * should be treated as a continuation of the current one.
  600. */
  601. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  602. struct mapping_defn_t *currmap = NULL;
  603. #endif
  604. struct interface_defn_t *currif = NULL;
  605. struct interfaces_file_t *defn;
  606. FILE *f;
  607. char *buf;
  608. char *first_word;
  609. char *rest_of_line;
  610. enum { NONE, IFACE, MAPPING } currently_processing = NONE;
  611. defn = xzalloc(sizeof(*defn));
  612. f = xfopen(filename, "r");
  613. while ((buf = xmalloc_fgetline(f)) != NULL) {
  614. #if ENABLE_DESKTOP
  615. /* Trailing "\" concatenates lines */
  616. char *p;
  617. while ((p = last_char_is(buf, '\\')) != NULL) {
  618. *p = '\0';
  619. rest_of_line = xmalloc_fgetline(f);
  620. if (!rest_of_line)
  621. break;
  622. p = xasprintf("%s%s", buf, rest_of_line);
  623. free(buf);
  624. free(rest_of_line);
  625. buf = p;
  626. }
  627. #endif
  628. rest_of_line = buf;
  629. first_word = next_word(&rest_of_line);
  630. if (!first_word || *buf == '#') {
  631. free(buf);
  632. continue; /* blank/comment line */
  633. }
  634. if (strcmp(first_word, "mapping") == 0) {
  635. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  636. currmap = xzalloc(sizeof(*currmap));
  637. while ((first_word = next_word(&rest_of_line)) != NULL) {
  638. if (currmap->n_matches >= currmap->max_matches) {
  639. currmap->max_matches = currmap->max_matches * 2 + 1;
  640. currmap->match = xrealloc(currmap->match,
  641. sizeof(*currmap->match) * currmap->max_matches);
  642. }
  643. currmap->match[currmap->n_matches++] = xstrdup(first_word);
  644. }
  645. /*currmap->max_mappings = 0; - done by xzalloc */
  646. /*currmap->n_mappings = 0;*/
  647. /*currmap->mapping = NULL;*/
  648. /*currmap->script = NULL;*/
  649. {
  650. struct mapping_defn_t **where = &defn->mappings;
  651. while (*where != NULL) {
  652. where = &(*where)->next;
  653. }
  654. *where = currmap;
  655. /*currmap->next = NULL;*/
  656. }
  657. debug_noise("Added mapping\n");
  658. #endif
  659. currently_processing = MAPPING;
  660. } else if (strcmp(first_word, "iface") == 0) {
  661. static const struct address_family_t *const addr_fams[] = {
  662. #if ENABLE_FEATURE_IFUPDOWN_IPV4
  663. &addr_inet,
  664. #endif
  665. #if ENABLE_FEATURE_IFUPDOWN_IPV6
  666. &addr_inet6,
  667. #endif
  668. NULL
  669. };
  670. char *iface_name;
  671. char *address_family_name;
  672. char *method_name;
  673. llist_t *iface_list;
  674. currif = xzalloc(sizeof(*currif));
  675. iface_name = next_word(&rest_of_line);
  676. address_family_name = next_word(&rest_of_line);
  677. method_name = next_word(&rest_of_line);
  678. if (method_name == NULL)
  679. bb_error_msg_and_die("too few parameters for line \"%s\"", buf);
  680. /* ship any trailing whitespace */
  681. rest_of_line = skip_whitespace(rest_of_line);
  682. if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */)
  683. bb_error_msg_and_die("too many parameters \"%s\"", buf);
  684. currif->iface = xstrdup(iface_name);
  685. currif->address_family = get_address_family(addr_fams, address_family_name);
  686. if (!currif->address_family)
  687. bb_error_msg_and_die("unknown address type \"%s\"", address_family_name);
  688. currif->method = get_method(currif->address_family, method_name);
  689. if (!currif->method)
  690. bb_error_msg_and_die("unknown method \"%s\"", method_name);
  691. for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) {
  692. struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data;
  693. if ((strcmp(tmp->iface, currif->iface) == 0)
  694. && (tmp->address_family == currif->address_family)
  695. ) {
  696. bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface);
  697. }
  698. }
  699. llist_add_to_end(&(defn->ifaces), (char*)currif);
  700. debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
  701. currently_processing = IFACE;
  702. } else if (strcmp(first_word, "auto") == 0) {
  703. while ((first_word = next_word(&rest_of_line)) != NULL) {
  704. /* Check the interface isnt already listed */
  705. if (find_list_string(defn->autointerfaces, first_word)) {
  706. bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
  707. }
  708. /* Add the interface to the list */
  709. llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word));
  710. debug_noise("\nauto %s\n", first_word);
  711. }
  712. currently_processing = NONE;
  713. } else {
  714. switch (currently_processing) {
  715. case IFACE:
  716. if (rest_of_line[0] == '\0')
  717. bb_error_msg_and_die("option with empty value \"%s\"", buf);
  718. if (strcmp(first_word, "up") != 0
  719. && strcmp(first_word, "down") != 0
  720. && strcmp(first_word, "pre-up") != 0
  721. && strcmp(first_word, "post-down") != 0
  722. ) {
  723. int i;
  724. for (i = 0; i < currif->n_options; i++) {
  725. if (strcmp(currif->option[i].name, first_word) == 0)
  726. bb_error_msg_and_die("duplicate option \"%s\"", buf);
  727. }
  728. }
  729. if (currif->n_options >= currif->max_options) {
  730. currif->max_options += 10;
  731. currif->option = xrealloc(currif->option,
  732. sizeof(*currif->option) * currif->max_options);
  733. }
  734. debug_noise("\t%s=%s\n", first_word, rest_of_line);
  735. currif->option[currif->n_options].name = xstrdup(first_word);
  736. currif->option[currif->n_options].value = xstrdup(rest_of_line);
  737. currif->n_options++;
  738. break;
  739. case MAPPING:
  740. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  741. if (strcmp(first_word, "script") == 0) {
  742. if (currmap->script != NULL)
  743. bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf);
  744. currmap->script = xstrdup(next_word(&rest_of_line));
  745. } else if (strcmp(first_word, "map") == 0) {
  746. if (currmap->n_mappings >= currmap->max_mappings) {
  747. currmap->max_mappings = currmap->max_mappings * 2 + 1;
  748. currmap->mapping = xrealloc(currmap->mapping,
  749. sizeof(char *) * currmap->max_mappings);
  750. }
  751. currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line));
  752. currmap->n_mappings++;
  753. } else {
  754. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  755. }
  756. #endif
  757. break;
  758. case NONE:
  759. default:
  760. bb_error_msg_and_die("misplaced option \"%s\"", buf);
  761. }
  762. }
  763. free(buf);
  764. } /* while (fgets) */
  765. if (ferror(f) != 0) {
  766. /* ferror does NOT set errno! */
  767. bb_error_msg_and_die("%s: I/O error", filename);
  768. }
  769. fclose(f);
  770. return defn;
  771. }
  772. static char *setlocalenv(const char *format, const char *name, const char *value)
  773. {
  774. char *result;
  775. char *here;
  776. char *there;
  777. result = xasprintf(format, name, value);
  778. for (here = there = result; *there != '=' && *there; there++) {
  779. if (*there == '-')
  780. *there = '_';
  781. if (isalpha(*there))
  782. *there = toupper(*there);
  783. if (isalnum(*there) || *there == '_') {
  784. *here = *there;
  785. here++;
  786. }
  787. }
  788. memmove(here, there, strlen(there) + 1);
  789. return result;
  790. }
  791. static void set_environ(struct interface_defn_t *iface, const char *mode)
  792. {
  793. char **environend;
  794. int i;
  795. const int n_env_entries = iface->n_options + 5;
  796. char **ppch;
  797. if (my_environ != NULL) {
  798. for (ppch = my_environ; *ppch; ppch++) {
  799. free(*ppch);
  800. *ppch = NULL;
  801. }
  802. free(my_environ);
  803. }
  804. my_environ = xzalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
  805. environend = my_environ;
  806. for (i = 0; i < iface->n_options; i++) {
  807. if (strcmp(iface->option[i].name, "up") == 0
  808. || strcmp(iface->option[i].name, "down") == 0
  809. || strcmp(iface->option[i].name, "pre-up") == 0
  810. || strcmp(iface->option[i].name, "post-down") == 0
  811. ) {
  812. continue;
  813. }
  814. *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
  815. }
  816. *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
  817. *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
  818. *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
  819. *(environend++) = setlocalenv("%s=%s", "MODE", mode);
  820. *(environend++) = setlocalenv("%s=%s", "PATH", startup_PATH);
  821. }
  822. static int doit(char *str)
  823. {
  824. if (option_mask32 & (OPT_no_act|OPT_verbose)) {
  825. puts(str);
  826. }
  827. if (!(option_mask32 & OPT_no_act)) {
  828. pid_t child;
  829. int status;
  830. fflush(NULL);
  831. child = vfork();
  832. switch (child) {
  833. case -1: /* failure */
  834. return 0;
  835. case 0: /* child */
  836. execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, my_environ);
  837. _exit(127);
  838. }
  839. safe_waitpid(child, &status, 0);
  840. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  841. return 0;
  842. }
  843. }
  844. return 1;
  845. }
  846. static int execute_all(struct interface_defn_t *ifd, const char *opt)
  847. {
  848. int i;
  849. char *buf;
  850. for (i = 0; i < ifd->n_options; i++) {
  851. if (strcmp(ifd->option[i].name, opt) == 0) {
  852. if (!doit(ifd->option[i].value)) {
  853. return 0;
  854. }
  855. }
  856. }
  857. buf = xasprintf("run-parts /etc/network/if-%s.d", opt);
  858. /* heh, we don't bother free'ing it */
  859. return doit(buf);
  860. }
  861. static int check(char *str)
  862. {
  863. return str != NULL;
  864. }
  865. static int iface_up(struct interface_defn_t *iface)
  866. {
  867. if (!iface->method->up(iface, check)) return -1;
  868. set_environ(iface, "start");
  869. if (!execute_all(iface, "pre-up")) return 0;
  870. if (!iface->method->up(iface, doit)) return 0;
  871. if (!execute_all(iface, "up")) return 0;
  872. return 1;
  873. }
  874. static int iface_down(struct interface_defn_t *iface)
  875. {
  876. if (!iface->method->down(iface,check)) return -1;
  877. set_environ(iface, "stop");
  878. if (!execute_all(iface, "down")) return 0;
  879. if (!iface->method->down(iface, doit)) return 0;
  880. if (!execute_all(iface, "post-down")) return 0;
  881. return 1;
  882. }
  883. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  884. static int popen2(FILE **in, FILE **out, char *command, char *param)
  885. {
  886. char *argv[3] = { command, param, NULL };
  887. struct fd_pair infd, outfd;
  888. pid_t pid;
  889. xpiped_pair(infd);
  890. xpiped_pair(outfd);
  891. fflush(NULL);
  892. pid = vfork();
  893. switch (pid) {
  894. case -1: /* failure */
  895. bb_perror_msg_and_die("vfork");
  896. case 0: /* child */
  897. /* NB: close _first_, then move fds! */
  898. close(infd.wr);
  899. close(outfd.rd);
  900. xmove_fd(infd.rd, 0);
  901. xmove_fd(outfd.wr, 1);
  902. BB_EXECVP(command, argv);
  903. _exit(127);
  904. }
  905. /* parent */
  906. close(infd.rd);
  907. close(outfd.wr);
  908. *in = fdopen(infd.wr, "w");
  909. *out = fdopen(outfd.rd, "r");
  910. return pid;
  911. }
  912. static char *run_mapping(char *physical, struct mapping_defn_t *map)
  913. {
  914. FILE *in, *out;
  915. int i, status;
  916. pid_t pid;
  917. char *logical = xstrdup(physical);
  918. /* Run the mapping script. Never fails. */
  919. pid = popen2(&in, &out, map->script, physical);
  920. /* Write mappings to stdin of mapping script. */
  921. for (i = 0; i < map->n_mappings; i++) {
  922. fprintf(in, "%s\n", map->mapping[i]);
  923. }
  924. fclose(in);
  925. safe_waitpid(pid, &status, 0);
  926. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  927. /* If the mapping script exited successfully, try to
  928. * grab a line of output and use that as the name of the
  929. * logical interface. */
  930. char *new_logical = xmalloc_fgetline(out);
  931. if (new_logical) {
  932. /* If we are able to read a line of output from the script,
  933. * remove any trailing whitespace and use this value
  934. * as the name of the logical interface. */
  935. char *pch = new_logical + strlen(new_logical) - 1;
  936. while (pch >= new_logical && isspace(*pch))
  937. *(pch--) = '\0';
  938. free(logical);
  939. logical = new_logical;
  940. }
  941. }
  942. fclose(out);
  943. return logical;
  944. }
  945. #endif /* FEATURE_IFUPDOWN_MAPPING */
  946. static llist_t *find_iface_state(llist_t *state_list, const char *iface)
  947. {
  948. unsigned iface_len = strlen(iface);
  949. llist_t *search = state_list;
  950. while (search) {
  951. if ((strncmp(search->data, iface, iface_len) == 0)
  952. && (search->data[iface_len] == '=')
  953. ) {
  954. return search;
  955. }
  956. search = search->link;
  957. }
  958. return NULL;
  959. }
  960. /* read the previous state from the state file */
  961. static llist_t *read_iface_state(void)
  962. {
  963. llist_t *state_list = NULL;
  964. FILE *state_fp = fopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "r");
  965. if (state_fp) {
  966. char *start, *end_ptr;
  967. while ((start = xmalloc_fgets(state_fp)) != NULL) {
  968. /* We should only need to check for a single character */
  969. end_ptr = start + strcspn(start, " \t\n");
  970. *end_ptr = '\0';
  971. llist_add_to(&state_list, start);
  972. }
  973. fclose(state_fp);
  974. }
  975. return state_list;
  976. }
  977. int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  978. int ifupdown_main(int argc, char **argv)
  979. {
  980. int (*cmds)(struct interface_defn_t *);
  981. struct interfaces_file_t *defn;
  982. llist_t *target_list = NULL;
  983. const char *interfaces = "/etc/network/interfaces";
  984. bool any_failures = 0;
  985. cmds = iface_down;
  986. if (applet_name[2] == 'u') {
  987. /* ifup command */
  988. cmds = iface_up;
  989. }
  990. getopt32(argv, OPTION_STR, &interfaces);
  991. if (argc - optind > 0) {
  992. if (DO_ALL) bb_show_usage();
  993. } else {
  994. if (!DO_ALL) bb_show_usage();
  995. }
  996. debug_noise("reading %s file:\n", interfaces);
  997. defn = read_interfaces(interfaces);
  998. debug_noise("\ndone reading %s\n\n", interfaces);
  999. startup_PATH = getenv("PATH");
  1000. if (!startup_PATH) startup_PATH = "";
  1001. /* Create a list of interfaces to work on */
  1002. if (DO_ALL) {
  1003. target_list = defn->autointerfaces;
  1004. } else {
  1005. llist_add_to_end(&target_list, argv[optind]);
  1006. }
  1007. /* Update the interfaces */
  1008. while (target_list) {
  1009. llist_t *iface_list;
  1010. struct interface_defn_t *currif;
  1011. char *iface;
  1012. char *liface;
  1013. char *pch;
  1014. bool okay = 0;
  1015. int cmds_ret;
  1016. iface = xstrdup(target_list->data);
  1017. target_list = target_list->link;
  1018. pch = strchr(iface, '=');
  1019. if (pch) {
  1020. *pch = '\0';
  1021. liface = xstrdup(pch + 1);
  1022. } else {
  1023. liface = xstrdup(iface);
  1024. }
  1025. if (!FORCE) {
  1026. llist_t *state_list = read_iface_state();
  1027. const llist_t *iface_state = find_iface_state(state_list, iface);
  1028. if (cmds == iface_up) {
  1029. /* ifup */
  1030. if (iface_state) {
  1031. bb_error_msg("interface %s already configured", iface);
  1032. continue;
  1033. }
  1034. } else {
  1035. /* ifdown */
  1036. if (!iface_state) {
  1037. bb_error_msg("interface %s not configured", iface);
  1038. continue;
  1039. }
  1040. }
  1041. llist_free(state_list, free);
  1042. }
  1043. #if ENABLE_FEATURE_IFUPDOWN_MAPPING
  1044. if ((cmds == iface_up) && !NO_MAPPINGS) {
  1045. struct mapping_defn_t *currmap;
  1046. for (currmap = defn->mappings; currmap; currmap = currmap->next) {
  1047. int i;
  1048. for (i = 0; i < currmap->n_matches; i++) {
  1049. if (fnmatch(currmap->match[i], liface, 0) != 0)
  1050. continue;
  1051. if (VERBOSE) {
  1052. printf("Running mapping script %s on %s\n", currmap->script, liface);
  1053. }
  1054. liface = run_mapping(iface, currmap);
  1055. break;
  1056. }
  1057. }
  1058. }
  1059. #endif
  1060. iface_list = defn->ifaces;
  1061. while (iface_list) {
  1062. currif = (struct interface_defn_t *) iface_list->data;
  1063. if (strcmp(liface, currif->iface) == 0) {
  1064. char *oldiface = currif->iface;
  1065. okay = 1;
  1066. currif->iface = iface;
  1067. debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
  1068. /* Call the cmds function pointer, does either iface_up() or iface_down() */
  1069. cmds_ret = cmds(currif);
  1070. if (cmds_ret == -1) {
  1071. bb_error_msg("don't seem to have all the variables for %s/%s",
  1072. liface, currif->address_family->name);
  1073. any_failures = 1;
  1074. } else if (cmds_ret == 0) {
  1075. any_failures = 1;
  1076. }
  1077. currif->iface = oldiface;
  1078. }
  1079. iface_list = iface_list->link;
  1080. }
  1081. if (VERBOSE) {
  1082. bb_putchar('\n');
  1083. }
  1084. if (!okay && !FORCE) {
  1085. bb_error_msg("ignoring unknown interface %s", liface);
  1086. any_failures = 1;
  1087. } else if (!NO_ACT) {
  1088. /* update the state file */
  1089. FILE *state_fp;
  1090. llist_t *state;
  1091. llist_t *state_list = read_iface_state();
  1092. llist_t *iface_state = find_iface_state(state_list, iface);
  1093. if (cmds == iface_up) {
  1094. char * const newiface = xasprintf("%s=%s", iface, liface);
  1095. if (iface_state == NULL) {
  1096. llist_add_to_end(&state_list, newiface);
  1097. } else {
  1098. free(iface_state->data);
  1099. iface_state->data = newiface;
  1100. }
  1101. } else {
  1102. /* Remove an interface from state_list */
  1103. llist_unlink(&state_list, iface_state);
  1104. free(llist_pop(&iface_state));
  1105. }
  1106. /* Actually write the new state */
  1107. state_fp = xfopen(CONFIG_IFUPDOWN_IFSTATE_PATH, "w");
  1108. state = state_list;
  1109. while (state) {
  1110. if (state->data) {
  1111. fprintf(state_fp, "%s\n", state->data);
  1112. }
  1113. state = state->link;
  1114. }
  1115. fclose(state_fp);
  1116. llist_free(state_list, free);
  1117. }
  1118. }
  1119. return any_failures;
  1120. }