ifupdown.c 31 KB

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