ifupdown.c 31 KB

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