3
0

ifupdown.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  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. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  28. */
  29. /* TODO: standardise execute() return codes to return 0 for success and 1 for failure */
  30. #include <sys/stat.h>
  31. #include <sys/utsname.h>
  32. #include <sys/wait.h>
  33. #include <ctype.h>
  34. #include <errno.h>
  35. #include <fcntl.h>
  36. #include <fnmatch.h>
  37. #include <getopt.h>
  38. #include <stdarg.h>
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <unistd.h>
  43. #include "libbb.h"
  44. #define MAX_OPT_DEPTH 10
  45. #define EUNBALBRACK 10001
  46. #define EUNDEFVAR 10002
  47. #define EUNBALPER 10000
  48. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  49. #define MAX_INTERFACE_LENGTH 10
  50. #endif
  51. #if 0
  52. #define debug_noise(fmt, args...) printf(fmt, ## args)
  53. #else
  54. #define debug_noise(fmt, args...)
  55. #endif
  56. /* Forward declaration */
  57. struct interface_defn_t;
  58. typedef int (execfn)(char *command);
  59. typedef int (command_set)(struct interface_defn_t *ifd, execfn *e);
  60. extern llist_t *llist_add_to_end(llist_t *list_head, char *data)
  61. {
  62. llist_t *new_item, *tmp, *prev;
  63. new_item = xmalloc(sizeof(llist_t));
  64. new_item->data = data;
  65. new_item->link = NULL;
  66. prev = NULL;
  67. tmp = list_head;
  68. while(tmp) {
  69. prev = tmp;
  70. tmp = tmp->link;
  71. }
  72. if (prev) {
  73. prev->link = new_item;
  74. } else {
  75. list_head = new_item;
  76. }
  77. return(list_head);
  78. }
  79. struct method_t
  80. {
  81. char *name;
  82. command_set *up;
  83. command_set *down;
  84. };
  85. struct address_family_t
  86. {
  87. char *name;
  88. int n_methods;
  89. struct method_t *method;
  90. };
  91. struct mapping_defn_t
  92. {
  93. struct mapping_defn_t *next;
  94. int max_matches;
  95. int n_matches;
  96. char **match;
  97. char *script;
  98. int max_mappings;
  99. int n_mappings;
  100. char **mapping;
  101. };
  102. struct variable_t
  103. {
  104. char *name;
  105. char *value;
  106. };
  107. struct interface_defn_t
  108. {
  109. struct interface_defn_t *prev;
  110. struct interface_defn_t *next;
  111. char *iface;
  112. struct address_family_t *address_family;
  113. struct method_t *method;
  114. int automatic;
  115. int max_options;
  116. int n_options;
  117. struct variable_t *option;
  118. };
  119. struct interfaces_file_t
  120. {
  121. llist_t *autointerfaces;
  122. llist_t *ifaces;
  123. struct mapping_defn_t *mappings;
  124. };
  125. static char no_act = 0;
  126. static char verbose = 0;
  127. static char **__myenviron = NULL;
  128. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  129. static unsigned int count_bits(unsigned int a)
  130. {
  131. unsigned int result;
  132. result = (a & 0x55) + ((a >> 1) & 0x55);
  133. result = (result & 0x33) + ((result >> 2) & 0x33);
  134. return((result & 0x0F) + ((result >> 4) & 0x0F));
  135. }
  136. static int count_netmask_bits(char *dotted_quad)
  137. {
  138. unsigned int result, a, b, c, d;
  139. /* Found a netmask... Check if it is dotted quad */
  140. if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4)
  141. return -1;
  142. result = count_bits(a);
  143. result += count_bits(b);
  144. result += count_bits(c);
  145. result += count_bits(d);
  146. return ((int)result);
  147. }
  148. #endif
  149. static void addstr(char **buf, size_t *len, size_t *pos, char *str, size_t str_length)
  150. {
  151. if (*pos + str_length >= *len) {
  152. char *newbuf;
  153. newbuf = xrealloc(*buf, *len * 2 + str_length + 1);
  154. *buf = newbuf;
  155. *len = *len * 2 + str_length + 1;
  156. }
  157. while (str_length-- >= 1) {
  158. (*buf)[(*pos)++] = *str;
  159. str++;
  160. }
  161. (*buf)[*pos] = '\0';
  162. }
  163. static int strncmpz(char *l, char *r, size_t llen)
  164. {
  165. int i = strncmp(l, r, llen);
  166. if (i == 0) {
  167. return(-r[llen]);
  168. } else {
  169. return(i);
  170. }
  171. }
  172. static char *get_var(char *id, size_t idlen, struct interface_defn_t *ifd)
  173. {
  174. int i;
  175. if (strncmpz(id, "iface", idlen) == 0) {
  176. char *result;
  177. static char label_buf[20];
  178. strncpy(label_buf, ifd->iface, 19);
  179. label_buf[19]=0;
  180. result = strchr(label_buf, ':');
  181. if (result) {
  182. *result=0;
  183. }
  184. return( label_buf);
  185. } else if (strncmpz(id, "label", idlen) == 0) {
  186. return (ifd->iface);
  187. } else {
  188. for (i = 0; i < ifd->n_options; i++) {
  189. if (strncmpz(id, ifd->option[i].name, idlen) == 0) {
  190. return (ifd->option[i].value);
  191. }
  192. }
  193. }
  194. return(NULL);
  195. }
  196. static char *parse(char *command, struct interface_defn_t *ifd)
  197. {
  198. char *result = NULL;
  199. size_t pos = 0, len = 0;
  200. size_t old_pos[MAX_OPT_DEPTH] = { 0 };
  201. int okay[MAX_OPT_DEPTH] = { 1 };
  202. int opt_depth = 1;
  203. while (*command) {
  204. switch (*command) {
  205. default:
  206. addstr(&result, &len, &pos, command, 1);
  207. command++;
  208. break;
  209. case '\\':
  210. if (command[1]) {
  211. addstr(&result, &len, &pos, command + 1, 1);
  212. command += 2;
  213. } else {
  214. addstr(&result, &len, &pos, command, 1);
  215. command++;
  216. }
  217. break;
  218. case '[':
  219. if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) {
  220. old_pos[opt_depth] = pos;
  221. okay[opt_depth] = 1;
  222. opt_depth++;
  223. command += 2;
  224. } else {
  225. addstr(&result, &len, &pos, "[", 1);
  226. command++;
  227. }
  228. break;
  229. case ']':
  230. if (command[1] == ']' && opt_depth > 1) {
  231. opt_depth--;
  232. if (!okay[opt_depth]) {
  233. pos = old_pos[opt_depth];
  234. result[pos] = '\0';
  235. }
  236. command += 2;
  237. } else {
  238. addstr(&result, &len, &pos, "]", 1);
  239. command++;
  240. }
  241. break;
  242. case '%':
  243. {
  244. char *nextpercent;
  245. char *varvalue;
  246. command++;
  247. nextpercent = strchr(command, '%');
  248. if (!nextpercent) {
  249. errno = EUNBALPER;
  250. free(result);
  251. return (NULL);
  252. }
  253. varvalue = get_var(command, nextpercent - command, ifd);
  254. if (varvalue) {
  255. addstr(&result, &len, &pos, varvalue, bb_strlen(varvalue));
  256. } else {
  257. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  258. /* Sigh... Add a special case for 'ip' to convert from
  259. * dotted quad to bit count style netmasks. */
  260. if (strncmp(command, "bnmask", 6)==0) {
  261. int res;
  262. varvalue = get_var("netmask", 7, ifd);
  263. if (varvalue && (res=count_netmask_bits(varvalue)) > 0) {
  264. char argument[255];
  265. sprintf(argument, "%d", res);
  266. addstr(&result, &len, &pos, argument, bb_strlen(argument));
  267. command = nextpercent + 1;
  268. break;
  269. }
  270. }
  271. #endif
  272. okay[opt_depth - 1] = 0;
  273. }
  274. command = nextpercent + 1;
  275. }
  276. break;
  277. }
  278. }
  279. if (opt_depth > 1) {
  280. errno = EUNBALBRACK;
  281. free(result);
  282. return(NULL);
  283. }
  284. if (!okay[0]) {
  285. errno = EUNDEFVAR;
  286. free(result);
  287. return(NULL);
  288. }
  289. return(result);
  290. }
  291. static int execute(char *command, struct interface_defn_t *ifd, execfn *exec)
  292. {
  293. char *out;
  294. int ret;
  295. out = parse(command, ifd);
  296. if (!out) {
  297. return(0);
  298. }
  299. ret = (*exec) (out);
  300. free(out);
  301. if (ret != 1) {
  302. return(0);
  303. }
  304. return(1);
  305. }
  306. #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
  307. static int static_up_ipx(struct interface_defn_t *ifd, execfn *exec)
  308. {
  309. return(execute("ipx_interface add %iface% %frame% %netnum%", ifd, exec));
  310. }
  311. static int static_down_ipx(struct interface_defn_t *ifd, execfn *exec)
  312. {
  313. return(execute("ipx_interface del %iface% %frame%", ifd, exec));
  314. }
  315. static int dynamic_up(struct interface_defn_t *ifd, execfn *exec)
  316. {
  317. return(execute("ipx_interface add %iface% %frame%", ifd, exec));
  318. }
  319. static int dynamic_down(struct interface_defn_t *ifd, execfn *exec)
  320. {
  321. return(execute("ipx_interface del %iface% %frame%", ifd, exec));
  322. }
  323. static struct method_t methods_ipx[] = {
  324. { "dynamic", dynamic_up, dynamic_down, },
  325. { "static", static_up_ipx, static_down_ipx, },
  326. };
  327. struct address_family_t addr_ipx = {
  328. "ipx",
  329. sizeof(methods_ipx) / sizeof(struct method_t),
  330. methods_ipx
  331. };
  332. #endif /* IFUP_FEATURE_IPX */
  333. #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
  334. static int loopback_up6(struct interface_defn_t *ifd, execfn *exec)
  335. {
  336. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  337. int result;
  338. result =execute("ip addr add ::1 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% add ::1", ifd, exec));
  343. #endif
  344. }
  345. static int loopback_down6(struct interface_defn_t *ifd, execfn *exec)
  346. {
  347. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  348. return(execute("ip link set %iface% down", ifd, exec));
  349. #else
  350. return(execute("ifconfig %iface% del ::1", ifd, exec));
  351. #endif
  352. }
  353. static int static_up6(struct interface_defn_t *ifd, execfn *exec)
  354. {
  355. int result;
  356. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  357. result = execute("ip addr add %address%/%netmask% dev %iface% [[label %label%]]", ifd, exec);
  358. result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
  359. result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
  360. #else
  361. result = execute("ifconfig %iface% [[media %media%]] [[hw %hwaddress%]] [[mtu %mtu%]] up", ifd, exec);
  362. result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec);
  363. result += execute("[[ route -A inet6 add ::/0 gw %gateway% ]]", ifd, exec);
  364. #endif
  365. return ((result == 3) ? 3 : 0);
  366. }
  367. static int static_down6(struct interface_defn_t *ifd, execfn *exec)
  368. {
  369. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  370. return(execute("ip link set %iface% down", ifd, exec));
  371. #else
  372. return(execute("ifconfig %iface% down", ifd, exec));
  373. #endif
  374. }
  375. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  376. static int v4tunnel_up(struct interface_defn_t *ifd, execfn *exec)
  377. {
  378. int result;
  379. result = execute("ip tunnel add %iface% mode sit remote "
  380. "%endpoint% [[local %local%]] [[ttl %ttl%]]", ifd, exec);
  381. result += execute("ip link set %iface% up", ifd, exec);
  382. result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec);
  383. result += execute("[[ ip route add ::/0 via %gateway% ]]", ifd, exec);
  384. return ((result == 4) ? 4 : 0);
  385. }
  386. static int v4tunnel_down(struct interface_defn_t * ifd, execfn * exec)
  387. {
  388. return( execute("ip tunnel del %iface%", ifd, exec));
  389. }
  390. #endif
  391. static struct method_t methods6[] = {
  392. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  393. { "v4tunnel", v4tunnel_up, v4tunnel_down, },
  394. #endif
  395. { "static", static_up6, static_down6, },
  396. { "loopback", loopback_up6, loopback_down6, },
  397. };
  398. static struct address_family_t addr_inet6 = {
  399. "inet6",
  400. sizeof(methods6) / sizeof(struct method_t),
  401. methods6
  402. };
  403. #endif /* CONFIG_FEATURE_IFUPDOWN_IPV6 */
  404. #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
  405. static int loopback_up(struct interface_defn_t *ifd, execfn *exec)
  406. {
  407. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  408. int result;
  409. result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec);
  410. result += execute("ip link set %iface% up", ifd, exec);
  411. return ((result == 2) ? 2 : 0);
  412. #else
  413. return( execute("ifconfig %iface% 127.0.0.1 up", ifd, exec));
  414. #endif
  415. }
  416. static int loopback_down(struct interface_defn_t *ifd, execfn *exec)
  417. {
  418. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  419. int result;
  420. result = execute("ip addr flush dev %iface%", ifd, exec);
  421. result += execute("ip link set %iface% down", ifd, exec);
  422. return ((result == 2) ? 2 : 0);
  423. #else
  424. return( execute("ifconfig %iface% 127.0.0.1 down", ifd, exec));
  425. #endif
  426. }
  427. static int static_up(struct interface_defn_t *ifd, execfn *exec)
  428. {
  429. int result;
  430. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  431. result = execute("ip addr add %address%/%bnmask% [[broadcast %broadcast%]] "
  432. "dev %iface% [[peer %pointopoint%]] [[label %label%]]", ifd, exec);
  433. result += execute("ip link set [[mtu %mtu%]] [[address %hwaddress%]] %iface% up", ifd, exec);
  434. result += execute("[[ ip route add default via %gateway% dev %iface% ]]", ifd, exec);
  435. return ((result == 3) ? 3 : 0);
  436. #else
  437. result = execute("ifconfig %iface% %address% netmask %netmask% "
  438. "[[broadcast %broadcast%]] [[pointopoint %pointopoint%]] "
  439. "[[media %media%]] [[mtu %mtu%]] [[hw %hwaddress%]] up",
  440. ifd, exec);
  441. result += execute("[[ route add default gw %gateway% %iface% ]]", ifd, exec);
  442. return ((result == 2) ? 2 : 0);
  443. #endif
  444. }
  445. static int static_down(struct interface_defn_t *ifd, execfn *exec)
  446. {
  447. int result;
  448. #ifdef CONFIG_FEATURE_IFUPDOWN_IP
  449. result = execute("ip addr flush dev %iface%", ifd, exec);
  450. result += execute("ip link set %iface% down", ifd, exec);
  451. #else
  452. result = execute("[[ route del default gw %gateway% %iface% ]]", ifd, exec);
  453. result += execute("ifconfig %iface% down", ifd, exec);
  454. #endif
  455. return ((result == 2) ? 2 : 0);
  456. }
  457. static int execable(char *program)
  458. {
  459. struct stat buf;
  460. if (0 == stat(program, &buf)) {
  461. if (S_ISREG(buf.st_mode) && (S_IXUSR & buf.st_mode)) {
  462. return(1);
  463. }
  464. }
  465. return(0);
  466. }
  467. static int dhcp_up(struct interface_defn_t *ifd, execfn *exec)
  468. {
  469. if (execable("/sbin/udhcpc")) {
  470. return( execute("udhcpc -n -p /var/run/udhcpc.%iface%.pid -i "
  471. "%iface% [[-H %hostname%]] [[-c %clientid%]]", ifd, exec));
  472. } else if (execable("/sbin/pump")) {
  473. return( execute("pump -i %iface% [[-h %hostname%]] [[-l %leasehours%]]", ifd, exec));
  474. } else if (execable("/sbin/dhclient")) {
  475. return( execute("dhclient -pf /var/run/dhclient.%iface%.pid %iface%", ifd, exec));
  476. } else if (execable("/sbin/dhcpcd")) {
  477. return( execute("dhcpcd [[-h %hostname%]] [[-i %vendor%]] [[-I %clientid%]] "
  478. "[[-l %leasetime%]] %iface%", ifd, exec));
  479. }
  480. return(0);
  481. }
  482. static int dhcp_down(struct interface_defn_t *ifd, execfn *exec)
  483. {
  484. int result = 0;
  485. if (execable("/sbin/udhcpc")) {
  486. /* SIGUSR2 forces udhcpc to release the current lease and go inactive,
  487. * and SIGTERM causes udhcpc to exit. Signals are queued and processed
  488. * sequentially so we don't need to sleep */
  489. result = execute("kill -USR2 `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
  490. result += execute("kill -TERM `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", ifd, exec);
  491. } else if (execable("/sbin/pump")) {
  492. result = execute("pump -i %iface% -k", ifd, exec);
  493. } else if (execable("/sbin/dhclient")) {
  494. result = execute("kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", ifd, exec);
  495. } else if (execable("/sbin/dhcpcd")) {
  496. result = execute("dhcpcd -k %iface%", ifd, exec);
  497. }
  498. return (result || static_down(ifd, exec));
  499. }
  500. static int bootp_up(struct interface_defn_t *ifd, execfn *exec)
  501. {
  502. return( execute("bootpc [[--bootfile %bootfile%]] --dev %iface% "
  503. "[[--server %server%]] [[--hwaddr %hwaddr%]] "
  504. "--returniffail --serverbcast", ifd, exec));
  505. }
  506. static int ppp_up(struct interface_defn_t *ifd, execfn *exec)
  507. {
  508. return( execute("pon [[%provider%]]", ifd, exec));
  509. }
  510. static int ppp_down(struct interface_defn_t *ifd, execfn *exec)
  511. {
  512. return( execute("poff [[%provider%]]", ifd, exec));
  513. }
  514. static int wvdial_up(struct interface_defn_t *ifd, execfn *exec)
  515. {
  516. return( execute("/sbin/start-stop-daemon --start -x /usr/bin/wvdial "
  517. "-p /var/run/wvdial.%iface% -b -m -- [[ %provider% ]]", ifd, exec));
  518. }
  519. static int wvdial_down(struct interface_defn_t *ifd, execfn *exec)
  520. {
  521. return( execute("/sbin/start-stop-daemon --stop -x /usr/bin/wvdial "
  522. "-p /var/run/wvdial.%iface% -s 2", ifd, exec));
  523. }
  524. static struct method_t methods[] =
  525. {
  526. { "wvdial", wvdial_up, wvdial_down, },
  527. { "ppp", ppp_up, ppp_down, },
  528. { "static", static_up, static_down, },
  529. { "bootp", bootp_up, static_down, },
  530. { "dhcp", dhcp_up, dhcp_down, },
  531. { "loopback", loopback_up, loopback_down, },
  532. };
  533. static struct address_family_t addr_inet =
  534. {
  535. "inet",
  536. sizeof(methods) / sizeof(struct method_t),
  537. methods
  538. };
  539. #endif /* ifdef CONFIG_FEATURE_IFUPDOWN_IPV4 */
  540. static char *next_word(char **buf)
  541. {
  542. unsigned short length;
  543. char *word;
  544. if ((buf == NULL) || (*buf == NULL) || (**buf == '\0')) {
  545. return NULL;
  546. }
  547. /* Skip over leading whitespace */
  548. word = *buf;
  549. while (isspace(*word)) {
  550. ++word;
  551. }
  552. /* Skip over comments */
  553. if (*word == '#') {
  554. return(NULL);
  555. }
  556. /* Find the length of this word */
  557. length = strcspn(word, " \t\n");
  558. if (length == 0) {
  559. return(NULL);
  560. }
  561. *buf = word + length;
  562. /*DBU:[dave@cray.com] if we are already at EOL dont't increment beyond it */
  563. if (**buf) {
  564. **buf = '\0';
  565. (*buf)++;
  566. }
  567. return word;
  568. }
  569. static struct address_family_t *get_address_family(struct address_family_t *af[], char *name)
  570. {
  571. int i;
  572. for (i = 0; af[i]; i++) {
  573. if (strcmp(af[i]->name, name) == 0) {
  574. return af[i];
  575. }
  576. }
  577. return NULL;
  578. }
  579. static struct method_t *get_method(struct address_family_t *af, char *name)
  580. {
  581. int i;
  582. for (i = 0; i < af->n_methods; i++) {
  583. if (strcmp(af->method[i].name, name) == 0) {
  584. return &af->method[i];
  585. }
  586. }
  587. return(NULL);
  588. }
  589. static int duplicate_if(struct interface_defn_t *ifa, struct interface_defn_t *ifb)
  590. {
  591. if (strcmp(ifa->iface, ifb->iface) != 0) {
  592. return(0);
  593. }
  594. if (ifa->address_family != ifb->address_family) {
  595. return(0);
  596. }
  597. return(1);
  598. }
  599. static const llist_t *find_list_string(const llist_t *list, const char *string)
  600. {
  601. while (list) {
  602. if (strcmp(list->data, string) == 0) {
  603. return(list);
  604. }
  605. list = list->link;
  606. }
  607. return(NULL);
  608. }
  609. static struct interfaces_file_t *read_interfaces(const char *filename)
  610. {
  611. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  612. struct mapping_defn_t *currmap = NULL;
  613. #endif
  614. struct interface_defn_t *currif = NULL;
  615. struct interfaces_file_t *defn;
  616. FILE *f;
  617. char *firstword;
  618. char *buf;
  619. enum { NONE, IFACE, MAPPING } currently_processing = NONE;
  620. defn = xmalloc(sizeof(struct interfaces_file_t));
  621. defn->autointerfaces = NULL;
  622. defn->mappings = NULL;
  623. defn->ifaces = NULL;
  624. f = bb_xfopen(filename, "r");
  625. while ((buf = bb_get_chomped_line_from_file(f)) != NULL) {
  626. char *buf_ptr = buf;
  627. firstword = next_word(&buf_ptr);
  628. if (firstword == NULL) {
  629. free(buf);
  630. continue; /* blank line */
  631. }
  632. if (strcmp(firstword, "mapping") == 0) {
  633. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  634. currmap = xmalloc(sizeof(struct mapping_defn_t));
  635. currmap->max_matches = 0;
  636. currmap->n_matches = 0;
  637. currmap->match = NULL;
  638. while ((firstword = next_word(&buf_ptr)) != NULL) {
  639. if (currmap->max_matches == currmap->n_matches) {
  640. currmap->max_matches = currmap->max_matches * 2 + 1;
  641. currmap->match = xrealloc(currmap->match, sizeof(currmap->match) * currmap->max_matches);
  642. }
  643. currmap->match[currmap->n_matches++] = bb_xstrdup(firstword);
  644. }
  645. currmap->max_mappings = 0;
  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(firstword, "iface") == 0) {
  661. {
  662. char *iface_name;
  663. char *address_family_name;
  664. char *method_name;
  665. struct address_family_t *addr_fams[] = {
  666. #ifdef CONFIG_FEATURE_IFUPDOWN_IPV4
  667. &addr_inet,
  668. #endif
  669. #ifdef CONFIG_FEATURE_IFUPDOWN_IPV6
  670. &addr_inet6,
  671. #endif
  672. #ifdef CONFIG_FEATURE_IFUPDOWN_IPX
  673. &addr_ipx,
  674. #endif
  675. NULL
  676. };
  677. currif = xmalloc(sizeof(struct interface_defn_t));
  678. iface_name = next_word(&buf_ptr);
  679. address_family_name = next_word(&buf_ptr);
  680. method_name = next_word(&buf_ptr);
  681. if (buf_ptr == NULL) {
  682. bb_error_msg("too few parameters for line \"%s\"", buf);
  683. return NULL;
  684. }
  685. /* ship any trailing whitespace */
  686. while (isspace(*buf_ptr)) {
  687. ++buf_ptr;
  688. }
  689. if (buf_ptr[0] != '\0') {
  690. bb_error_msg("too many parameters \"%s\"", buf);
  691. return NULL;
  692. }
  693. currif->iface = bb_xstrdup(iface_name);
  694. currif->address_family = get_address_family(addr_fams, address_family_name);
  695. if (!currif->address_family) {
  696. bb_error_msg("unknown address type \"%s\"", address_family_name);
  697. return NULL;
  698. }
  699. currif->method = get_method(currif->address_family, method_name);
  700. if (!currif->method) {
  701. bb_error_msg("unknown method \"%s\"", method_name);
  702. return NULL;
  703. }
  704. currif->automatic = 1;
  705. currif->max_options = 0;
  706. currif->n_options = 0;
  707. currif->option = NULL;
  708. {
  709. struct interface_defn_t *tmp;
  710. llist_t *iface_list;
  711. iface_list = defn->ifaces;
  712. while (iface_list) {
  713. tmp = (struct interface_defn_t *) iface_list->data;
  714. if (duplicate_if(tmp, currif)) {
  715. bb_error_msg("duplicate interface \"%s\"", tmp->iface);
  716. return NULL;
  717. }
  718. iface_list = iface_list->link;
  719. }
  720. defn->ifaces = llist_add_to_end(defn->ifaces, (char*)currif);
  721. }
  722. debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name);
  723. }
  724. currently_processing = IFACE;
  725. } else if (strcmp(firstword, "auto") == 0) {
  726. while ((firstword = next_word(&buf_ptr)) != NULL) {
  727. /* Check the interface isnt already listed */
  728. if (find_list_string(defn->autointerfaces, firstword)) {
  729. bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf);
  730. }
  731. /* Add the interface to the list */
  732. defn->autointerfaces = llist_add_to_end(defn->autointerfaces, strdup(firstword));
  733. debug_noise("\nauto %s\n", firstword);
  734. }
  735. currently_processing = NONE;
  736. } else {
  737. switch (currently_processing) {
  738. case IFACE:
  739. {
  740. int i;
  741. if (bb_strlen(buf_ptr) == 0) {
  742. bb_error_msg("option with empty value \"%s\"", buf);
  743. return NULL;
  744. }
  745. if (strcmp(firstword, "up") != 0
  746. && strcmp(firstword, "down") != 0
  747. && strcmp(firstword, "pre-up") != 0
  748. && strcmp(firstword, "post-down") != 0) {
  749. for (i = 0; i < currif->n_options; i++) {
  750. if (strcmp(currif->option[i].name, firstword) == 0) {
  751. bb_error_msg("duplicate option \"%s\"", buf);
  752. return NULL;
  753. }
  754. }
  755. }
  756. }
  757. if (currif->n_options >= currif->max_options) {
  758. struct variable_t *opt;
  759. currif->max_options = currif->max_options + 10;
  760. opt = xrealloc(currif->option, sizeof(*opt) * currif->max_options);
  761. currif->option = opt;
  762. }
  763. currif->option[currif->n_options].name = bb_xstrdup(firstword);
  764. currif->option[currif->n_options].value = bb_xstrdup(buf_ptr);
  765. if (!currif->option[currif->n_options].name) {
  766. perror(filename);
  767. return NULL;
  768. }
  769. if (!currif->option[currif->n_options].value) {
  770. perror(filename);
  771. return NULL;
  772. }
  773. debug_noise("\t%s=%s\n", currif->option[currif->n_options].name,
  774. currif->option[currif->n_options].value);
  775. currif->n_options++;
  776. break;
  777. case MAPPING:
  778. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  779. if (strcmp(firstword, "script") == 0) {
  780. if (currmap->script != NULL) {
  781. bb_error_msg("duplicate script in mapping \"%s\"", buf);
  782. return NULL;
  783. } else {
  784. currmap->script = bb_xstrdup(next_word(&buf_ptr));
  785. }
  786. } else if (strcmp(firstword, "map") == 0) {
  787. if (currmap->max_mappings == currmap->n_mappings) {
  788. currmap->max_mappings = currmap->max_mappings * 2 + 1;
  789. currmap->mapping = xrealloc(currmap->mapping, sizeof(char *) * currmap->max_mappings);
  790. }
  791. currmap->mapping[currmap->n_mappings] = bb_xstrdup(next_word(&buf_ptr));
  792. currmap->n_mappings++;
  793. } else {
  794. bb_error_msg("misplaced option \"%s\"", buf);
  795. return NULL;
  796. }
  797. #endif
  798. break;
  799. case NONE:
  800. default:
  801. bb_error_msg("misplaced option \"%s\"", buf);
  802. return NULL;
  803. }
  804. }
  805. free(buf);
  806. }
  807. if (ferror(f) != 0) {
  808. bb_perror_msg_and_die("%s", filename);
  809. }
  810. fclose(f);
  811. return defn;
  812. }
  813. static char *setlocalenv(char *format, char *name, char *value)
  814. {
  815. char *result;
  816. char *here;
  817. char *there;
  818. result = xmalloc(bb_strlen(format) + bb_strlen(name) + bb_strlen(value) + 1);
  819. sprintf(result, format, name, value);
  820. for (here = there = result; *there != '=' && *there; there++) {
  821. if (*there == '-')
  822. *there = '_';
  823. if (isalpha(*there))
  824. *there = toupper(*there);
  825. if (isalnum(*there) || *there == '_') {
  826. *here = *there;
  827. here++;
  828. }
  829. }
  830. memmove(here, there, bb_strlen(there) + 1);
  831. return result;
  832. }
  833. static void set_environ(struct interface_defn_t *iface, char *mode)
  834. {
  835. char **environend;
  836. int i;
  837. const int n_env_entries = iface->n_options + 5;
  838. char **ppch;
  839. if (__myenviron != NULL) {
  840. for (ppch = __myenviron; *ppch; ppch++) {
  841. free(*ppch);
  842. *ppch = NULL;
  843. }
  844. free(__myenviron);
  845. __myenviron = NULL;
  846. }
  847. __myenviron = xmalloc(sizeof(char *) * (n_env_entries + 1 /* for final NULL */ ));
  848. environend = __myenviron;
  849. *environend = NULL;
  850. for (i = 0; i < iface->n_options; i++) {
  851. if (strcmp(iface->option[i].name, "up") == 0
  852. || strcmp(iface->option[i].name, "down") == 0
  853. || strcmp(iface->option[i].name, "pre-up") == 0
  854. || strcmp(iface->option[i].name, "post-down") == 0) {
  855. continue;
  856. }
  857. *(environend++) = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value);
  858. *environend = NULL;
  859. }
  860. *(environend++) = setlocalenv("%s=%s", "IFACE", iface->iface);
  861. *environend = NULL;
  862. *(environend++) = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name);
  863. *environend = NULL;
  864. *(environend++) = setlocalenv("%s=%s", "METHOD", iface->method->name);
  865. *environend = NULL;
  866. *(environend++) = setlocalenv("%s=%s", "MODE", mode);
  867. *environend = NULL;
  868. *(environend++) = setlocalenv("%s=%s", "PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin");
  869. *environend = NULL;
  870. }
  871. static int doit(char *str)
  872. {
  873. if (verbose || no_act) {
  874. printf("%s\n", str);
  875. }
  876. if (!no_act) {
  877. pid_t child;
  878. int status;
  879. fflush(NULL);
  880. switch (child = fork()) {
  881. case -1: /* failure */
  882. return 0;
  883. case 0: /* child */
  884. execle(DEFAULT_SHELL, DEFAULT_SHELL, "-c", str, NULL, __myenviron);
  885. exit(127);
  886. }
  887. waitpid(child, &status, 0);
  888. if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
  889. return 0;
  890. }
  891. }
  892. return (1);
  893. }
  894. static int execute_all(struct interface_defn_t *ifd, execfn *exec, const char *opt)
  895. {
  896. int i;
  897. char *buf;
  898. for (i = 0; i < ifd->n_options; i++) {
  899. if (strcmp(ifd->option[i].name, opt) == 0) {
  900. if (!(*exec) (ifd->option[i].value)) {
  901. return 0;
  902. }
  903. }
  904. }
  905. bb_xasprintf(&buf, "run-parts /etc/network/if-%s.d", opt);
  906. if ((*exec)(buf) != 1) {
  907. return 0;
  908. }
  909. return 1;
  910. }
  911. static int check(char *str) {
  912. return str != NULL;
  913. }
  914. static int iface_up(struct interface_defn_t *iface)
  915. {
  916. if (!iface->method->up(iface,check)) return -1;
  917. set_environ(iface, "start");
  918. if (!execute_all(iface, doit, "pre-up")) return 0;
  919. if (!iface->method->up(iface, doit)) return 0;
  920. if (!execute_all(iface, doit, "up")) return 0;
  921. return 1;
  922. }
  923. static int iface_down(struct interface_defn_t *iface)
  924. {
  925. if (!iface->method->down(iface,check)) return -1;
  926. set_environ(iface, "stop");
  927. if (!execute_all(iface, doit, "down")) return 0;
  928. if (!iface->method->down(iface, doit)) return 0;
  929. if (!execute_all(iface, doit, "post-down")) return 0;
  930. return 1;
  931. }
  932. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  933. static int popen2(FILE **in, FILE **out, char *command, ...)
  934. {
  935. va_list ap;
  936. char *argv[11] = { command };
  937. int argc;
  938. int infd[2], outfd[2];
  939. pid_t pid;
  940. argc = 1;
  941. va_start(ap, command);
  942. while ((argc < 10) && (argv[argc] = va_arg(ap, char *))) {
  943. argc++;
  944. }
  945. argv[argc] = NULL; /* make sure */
  946. va_end(ap);
  947. if (pipe(infd) != 0) {
  948. return 0;
  949. }
  950. if (pipe(outfd) != 0) {
  951. close(infd[0]);
  952. close(infd[1]);
  953. return 0;
  954. }
  955. fflush(NULL);
  956. switch (pid = fork()) {
  957. case -1: /* failure */
  958. close(infd[0]);
  959. close(infd[1]);
  960. close(outfd[0]);
  961. close(outfd[1]);
  962. return 0;
  963. case 0: /* child */
  964. dup2(infd[0], 0);
  965. dup2(outfd[1], 1);
  966. close(infd[0]);
  967. close(infd[1]);
  968. close(outfd[0]);
  969. close(outfd[1]);
  970. execvp(command, argv);
  971. exit(127);
  972. default: /* parent */
  973. *in = fdopen(infd[1], "w");
  974. *out = fdopen(outfd[0], "r");
  975. close(infd[0]);
  976. close(outfd[1]);
  977. return pid;
  978. }
  979. /* unreached */
  980. }
  981. static char *run_mapping(char *physical, struct mapping_defn_t * map)
  982. {
  983. FILE *in, *out;
  984. int i, status;
  985. pid_t pid;
  986. char *logical = bb_xstrdup(physical);
  987. /* Run the mapping script. */
  988. pid = popen2(&in, &out, map->script, physical, NULL);
  989. /* popen2() returns 0 on failure. */
  990. if (pid == 0)
  991. return logical;
  992. /* Write mappings to stdin of mapping script. */
  993. for (i = 0; i < map->n_mappings; i++) {
  994. fprintf(in, "%s\n", map->mapping[i]);
  995. }
  996. fclose(in);
  997. waitpid(pid, &status, 0);
  998. if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
  999. /* If the mapping script exited successfully, try to
  1000. * grab a line of output and use that as the name of the
  1001. * logical interface. */
  1002. char *new_logical = (char *)xmalloc(MAX_INTERFACE_LENGTH);
  1003. if (fgets(new_logical, MAX_INTERFACE_LENGTH, out)) {
  1004. /* If we are able to read a line of output from the script,
  1005. * remove any trailing whitespace and use this value
  1006. * as the name of the logical interface. */
  1007. char *pch = new_logical + bb_strlen(new_logical) - 1;
  1008. while (pch >= new_logical && isspace(*pch))
  1009. *(pch--) = '\0';
  1010. free(logical);
  1011. logical = new_logical;
  1012. } else {
  1013. /* If we are UNABLE to read a line of output, discard are
  1014. * freshly allocated memory. */
  1015. free(new_logical);
  1016. }
  1017. }
  1018. fclose(out);
  1019. return logical;
  1020. }
  1021. #endif /* CONFIG_FEATURE_IFUPDOWN_MAPPING */
  1022. static llist_t *find_iface_state(llist_t *state_list, const char *iface)
  1023. {
  1024. unsigned short iface_len = bb_strlen(iface);
  1025. llist_t *search = state_list;
  1026. while (search) {
  1027. if ((strncmp(search->data, iface, iface_len) == 0) &&
  1028. (search->data[iface_len] == '=')) {
  1029. return(search);
  1030. }
  1031. search = search->link;
  1032. }
  1033. return(NULL);
  1034. }
  1035. extern int ifupdown_main(int argc, char **argv)
  1036. {
  1037. int (*cmds) (struct interface_defn_t *) = NULL;
  1038. struct interfaces_file_t *defn;
  1039. FILE *state_fp = NULL;
  1040. llist_t *state_list = NULL;
  1041. llist_t *target_list = NULL;
  1042. const char *interfaces = "/etc/network/interfaces";
  1043. const char *statefile = "/var/run/ifstate";
  1044. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  1045. int run_mappings = 1;
  1046. #endif
  1047. int do_all = 0;
  1048. int force = 0;
  1049. int any_failures = 0;
  1050. int i;
  1051. if (bb_applet_name[2] == 'u') {
  1052. /* ifup command */
  1053. cmds = iface_up;
  1054. } else {
  1055. /* ifdown command */
  1056. cmds = iface_down;
  1057. }
  1058. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  1059. while ((i = getopt(argc, argv, "i:hvnamf")) != -1)
  1060. #else
  1061. while ((i = getopt(argc, argv, "i:hvnaf")) != -1)
  1062. #endif
  1063. {
  1064. switch (i) {
  1065. case 'i': /* interfaces */
  1066. interfaces = optarg;
  1067. break;
  1068. case 'v': /* verbose */
  1069. verbose = 1;
  1070. break;
  1071. case 'a': /* all */
  1072. do_all = 1;
  1073. break;
  1074. case 'n': /* no-act */
  1075. no_act = 1;
  1076. break;
  1077. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  1078. case 'm': /* no-mappings */
  1079. run_mappings = 0;
  1080. break;
  1081. #endif
  1082. case 'f': /* force */
  1083. force = 1;
  1084. break;
  1085. default:
  1086. bb_show_usage();
  1087. break;
  1088. }
  1089. }
  1090. if (argc - optind > 0) {
  1091. if (do_all) {
  1092. bb_show_usage();
  1093. }
  1094. } else {
  1095. if (!do_all) {
  1096. bb_show_usage();
  1097. }
  1098. }
  1099. debug_noise("reading %s file:\n", interfaces);
  1100. defn = read_interfaces(interfaces);
  1101. debug_noise("\ndone reading %s\n\n", interfaces);
  1102. if (!defn) {
  1103. exit(EXIT_FAILURE);
  1104. }
  1105. if (no_act) {
  1106. state_fp = fopen(statefile, "r");
  1107. }
  1108. /* Create a list of interfaces to work on */
  1109. if (do_all) {
  1110. if (cmds == iface_up) {
  1111. target_list = defn->autointerfaces;
  1112. } else {
  1113. #if 0
  1114. /* iface_down */
  1115. llist_t *new_item;
  1116. const llist_t *list = state_list;
  1117. while (list) {
  1118. new_item = xmalloc(sizeof(llist_t));
  1119. new_item->data = strdup(list->data);
  1120. new_item->link = NULL;
  1121. list = target_list;
  1122. if (list == NULL)
  1123. target_list = new_item;
  1124. else {
  1125. while (list->link) {
  1126. list = list->link;
  1127. }
  1128. list = new_item;
  1129. }
  1130. list = list->link;
  1131. }
  1132. target_list = defn->autointerfaces;
  1133. #else
  1134. /* iface_down */
  1135. const llist_t *list = state_list;
  1136. while (list) {
  1137. target_list = llist_add_to_end(target_list, strdup(list->data));
  1138. list = list->link;
  1139. }
  1140. target_list = defn->autointerfaces;
  1141. #endif
  1142. }
  1143. } else {
  1144. target_list = llist_add_to_end(target_list, argv[optind]);
  1145. }
  1146. /* Update the interfaces */
  1147. while (target_list) {
  1148. llist_t *iface_list;
  1149. struct interface_defn_t *currif;
  1150. char *iface;
  1151. char *liface;
  1152. char *pch;
  1153. int okay = 0;
  1154. int cmds_ret;
  1155. iface = strdup(target_list->data);
  1156. target_list = target_list->link;
  1157. pch = strchr(iface, '=');
  1158. if (pch) {
  1159. *pch = '\0';
  1160. liface = strdup(pch + 1);
  1161. } else {
  1162. liface = strdup(iface);
  1163. }
  1164. if (!force) {
  1165. const llist_t *iface_state = find_iface_state(state_list, iface);
  1166. if (cmds == iface_up) {
  1167. /* ifup */
  1168. if (iface_state) {
  1169. bb_error_msg("interface %s already configured", iface);
  1170. continue;
  1171. }
  1172. } else {
  1173. /* ifdown */
  1174. if (iface_state) {
  1175. bb_error_msg("interface %s not configured", iface);
  1176. continue;
  1177. }
  1178. }
  1179. }
  1180. #ifdef CONFIG_FEATURE_IFUPDOWN_MAPPING
  1181. if ((cmds == iface_up) && run_mappings) {
  1182. struct mapping_defn_t *currmap;
  1183. for (currmap = defn->mappings; currmap; currmap = currmap->next) {
  1184. for (i = 0; i < currmap->n_matches; i++) {
  1185. if (fnmatch(currmap->match[i], liface, 0) != 0)
  1186. continue;
  1187. if (verbose) {
  1188. printf("Running mapping script %s on %s\n", currmap->script, liface);
  1189. }
  1190. liface = run_mapping(iface, currmap);
  1191. break;
  1192. }
  1193. }
  1194. }
  1195. #endif
  1196. iface_list = defn->ifaces;
  1197. while (iface_list) {
  1198. currif = (struct interface_defn_t *) iface_list->data;
  1199. if (strcmp(liface, currif->iface) == 0) {
  1200. char *oldiface = currif->iface;
  1201. okay = 1;
  1202. currif->iface = iface;
  1203. debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name);
  1204. /* Call the cmds function pointer, does either iface_up() or iface_down() */
  1205. cmds_ret = cmds(currif);
  1206. if (cmds_ret == -1) {
  1207. bb_error_msg("Don't seem to have all the variables for %s/%s.",
  1208. liface, currif->address_family->name);
  1209. any_failures += 1;
  1210. } else if (cmds_ret == 0) {
  1211. any_failures += 1;
  1212. }
  1213. currif->iface = oldiface;
  1214. }
  1215. iface_list = iface_list->link;
  1216. }
  1217. if (verbose) {
  1218. printf("\n");
  1219. }
  1220. if (!okay && !force) {
  1221. bb_error_msg("Ignoring unknown interface %s", liface);
  1222. any_failures += 1;
  1223. } else {
  1224. llist_t *iface_state = find_iface_state(state_list, iface);
  1225. if (cmds == iface_up) {
  1226. char *newiface = xmalloc(bb_strlen(iface) + 1 + bb_strlen(liface) + 1);
  1227. sprintf(newiface, "%s=%s", iface, liface);
  1228. if (iface_state == NULL) {
  1229. state_list = llist_add_to_end(state_list, newiface);
  1230. } else {
  1231. free(iface_state->data);
  1232. iface_state->data = newiface;
  1233. }
  1234. } else if (cmds == iface_down) {
  1235. /* Remove an interface from the linked list */
  1236. if (iface_state) {
  1237. /* This needs to be done better */
  1238. free(iface_state->data);
  1239. free(iface_state->link);
  1240. if (iface_state->link) {
  1241. iface_state->data = iface_state->link->data;
  1242. iface_state->link = iface_state->link->link;
  1243. } else {
  1244. iface_state->data = NULL;
  1245. iface_state->link = NULL;
  1246. }
  1247. }
  1248. }
  1249. }
  1250. }
  1251. /* Actually write the new state */
  1252. if (!no_act) {
  1253. if (state_fp)
  1254. fclose(state_fp);
  1255. state_fp = bb_xfopen(statefile, "a+");
  1256. if (ftruncate(fileno(state_fp), 0) < 0) {
  1257. bb_error_msg_and_die("failed to truncate statefile %s: %s", statefile, strerror(errno));
  1258. }
  1259. rewind(state_fp);
  1260. while (state_list) {
  1261. if (state_list->data) {
  1262. fputs(state_list->data, state_fp);
  1263. fputc('\n', state_fp);
  1264. }
  1265. state_list = state_list->link;
  1266. }
  1267. fflush(state_fp);
  1268. }
  1269. /* Cleanup */
  1270. if (state_fp != NULL) {
  1271. fclose(state_fp);
  1272. state_fp = NULL;
  1273. }
  1274. if (any_failures)
  1275. return 1;
  1276. return 0;
  1277. }