ncd.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. /**
  2. * @file ncd.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <misc/version.h>
  27. #include <misc/loggers_string.h>
  28. #include <misc/loglevel.h>
  29. #include <misc/offset.h>
  30. #include <misc/read_file.h>
  31. #include <misc/string_begins_with.h>
  32. #include <misc/ipaddr.h>
  33. #include <structure/LinkedList2.h>
  34. #include <system/BLog.h>
  35. #include <system/BReactor.h>
  36. #include <system/BProcess.h>
  37. #include <system/BSignal.h>
  38. #include <system/BSocket.h>
  39. #include <dhcpclient/BDHCPClient.h>
  40. #include <ncdconfig/NCDConfigParser.h>
  41. #include <ncd/NCDIfConfig.h>
  42. #include <ncd/NCDInterfaceModule.h>
  43. #ifndef BADVPN_USE_WINAPI
  44. #include <system/BLog_syslog.h>
  45. #endif
  46. #include <ncd/ncd.h>
  47. #include <generated/blog_channel_ncd.h>
  48. #define LOGGER_STDOUT 1
  49. #define LOGGER_SYSLOG 2
  50. #define INTERFACE_STATE_WAITDEPS 1
  51. #define INTERFACE_STATE_RESETTING 2
  52. #define INTERFACE_STATE_WAITMODULE 3
  53. #define INTERFACE_STATE_DHCP 4
  54. #define INTERFACE_STATE_FINISHED 5
  55. // interface modules
  56. extern const struct NCDInterfaceModule ncd_interface_physical;
  57. // interface modules list
  58. const struct NCDInterfaceModule *interface_modules[] = {
  59. &ncd_interface_physical,
  60. NULL
  61. };
  62. // command-line options
  63. struct {
  64. int help;
  65. int version;
  66. int logger;
  67. #ifndef BADVPN_USE_WINAPI
  68. char *logger_syslog_facility;
  69. char *logger_syslog_ident;
  70. #endif
  71. int loglevel;
  72. int loglevels[BLOG_NUM_CHANNELS];
  73. char *config_file;
  74. } options;
  75. struct interface {
  76. LinkedList2Node list_node; // node in interfaces
  77. struct NCDConfig_interfaces *conf;
  78. const struct NCDInterfaceModule *module;
  79. int state;
  80. BTimer reset_timer;
  81. LinkedList2 deps_out;
  82. LinkedList2 deps_in;
  83. int have_module_instance;
  84. NCDInterfaceModuleInst module_instance;
  85. int have_dhcp;
  86. BDHCPClient dhcp;
  87. LinkedList2 ipv4_addresses;
  88. LinkedList2 ipv4_routes;
  89. LinkedList2 ipv4_dns_servers;
  90. };
  91. struct dependency {
  92. struct interface *src;
  93. struct interface *dst;
  94. LinkedList2Node src_node;
  95. LinkedList2Node dst_node;
  96. };
  97. struct ipv4_addr_entry {
  98. LinkedList2Node list_node; // node in interface.ipv4_addresses
  99. struct ipv4_ifaddr ifaddr;
  100. };
  101. struct ipv4_route_entry {
  102. LinkedList2Node list_node; // node in interface.ipv4_routes
  103. struct ipv4_ifaddr dest;
  104. int have_gateway;
  105. uint32_t gateway;
  106. int metric;
  107. };
  108. struct ipv4_dns_entry {
  109. LinkedList2Node list_node; // node in interface.ipv4_dns_servers
  110. uint32_t addr;
  111. int priority;
  112. };
  113. // reactor
  114. BReactor ss;
  115. // process manager
  116. BProcessManager manager;
  117. // configuration
  118. struct NCDConfig_interfaces *configuration;
  119. // interfaces
  120. LinkedList2 interfaces;
  121. // number of DNS servers
  122. size_t num_ipv4_dns_servers;
  123. static void terminate (void);
  124. static void print_help (const char *name);
  125. static void print_version (void);
  126. static int parse_arguments (int argc, char *argv[]);
  127. static void signal_handler (void *unused);
  128. static void load_interfaces (struct NCDConfig_interfaces *conf);
  129. static void free_interfaces (void);
  130. static int set_dns_servers (void);
  131. static int dns_qsort_comparator (const void *v1, const void *v2);
  132. static struct interface * find_interface (const char *name);
  133. static int interface_init (struct NCDConfig_interfaces *conf);
  134. static void interface_free (struct interface *iface);
  135. static void interface_down_to (struct interface *iface, int state);
  136. static void interface_reset (struct interface *iface);
  137. static void interface_start (struct interface *iface);
  138. static void interface_module_up (struct interface *iface);
  139. static void interface_log (struct interface *iface, int level, const char *fmt, ...);
  140. static void interface_reset_timer_handler (struct interface *iface);
  141. static int interface_module_init (struct interface *iface);
  142. static void interface_module_free (struct interface *iface);
  143. static void interface_module_handler_event (struct interface *iface, int event);
  144. static void interface_module_handler_error (struct interface *iface);
  145. static void interface_dhcp_handler (struct interface *iface, int event);
  146. static void interface_remove_dependencies (struct interface *iface);
  147. static int interface_add_dependency (struct interface *iface, struct interface *dst);
  148. static void remove_dependency (struct dependency *d);
  149. static int interface_dependencies_satisfied (struct interface *iface);
  150. static void interface_satisfy_incoming_depenencies (struct interface *iface);
  151. static void interface_unsatisfy_incoming_depenencies (struct interface *iface);
  152. static int interface_configure_ipv4 (struct interface *iface);
  153. static void interface_deconfigure_ipv4 (struct interface *iface);
  154. static int interface_add_ipv4_addresses (struct interface *iface);
  155. static void interface_remove_ipv4_addresses (struct interface *iface);
  156. static int interface_add_ipv4_routes (struct interface *iface);
  157. static void interface_remove_ipv4_routes (struct interface *iface);
  158. static int interface_add_ipv4_dns_servers (struct interface *iface);
  159. static void interface_remove_ipv4_dns_servers (struct interface *iface);
  160. static int interface_add_ipv4_addr (struct interface *iface, struct ipv4_ifaddr ifaddr);
  161. static void interface_remove_ipv4_addr (struct interface *iface, struct ipv4_addr_entry *entry);
  162. static int interface_add_ipv4_route (struct interface *iface, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric);
  163. static void interface_remove_ipv4_route (struct interface *iface, struct ipv4_route_entry *entry);
  164. static struct ipv4_addr_entry * interface_add_ipv4_addr_entry (struct interface *iface, struct ipv4_ifaddr ifaddr);
  165. static void interface_remove_ipv4_addr_entry (struct interface *iface, struct ipv4_addr_entry *entry);
  166. static struct ipv4_route_entry * interface_add_ipv4_route_entry (struct interface *iface, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric);
  167. static void interface_remove_ipv4_route_entry (struct interface *iface, struct ipv4_route_entry *entry);
  168. static struct ipv4_dns_entry * interface_add_ipv4_dns_entry (struct interface *iface, uint32_t addr, int priority);
  169. static void interface_remove_ipv4_dns_entry (struct interface *iface, struct ipv4_dns_entry *entry);
  170. int main (int argc, char **argv)
  171. {
  172. if (argc <= 0) {
  173. return 1;
  174. }
  175. // parse command-line arguments
  176. if (!parse_arguments(argc, argv)) {
  177. fprintf(stderr, "Failed to parse arguments\n");
  178. print_help(argv[0]);
  179. goto fail0;
  180. }
  181. // handle --help and --version
  182. if (options.help) {
  183. print_version();
  184. print_help(argv[0]);
  185. return 0;
  186. }
  187. if (options.version) {
  188. print_version();
  189. return 0;
  190. }
  191. // initialize logger
  192. switch (options.logger) {
  193. case LOGGER_STDOUT:
  194. BLog_InitStdout();
  195. break;
  196. #ifndef BADVPN_USE_WINAPI
  197. case LOGGER_SYSLOG:
  198. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  199. fprintf(stderr, "Failed to initialize syslog logger\n");
  200. goto fail0;
  201. }
  202. break;
  203. #endif
  204. default:
  205. ASSERT(0);
  206. }
  207. // configure logger channels
  208. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  209. if (options.loglevels[i] >= 0) {
  210. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  211. }
  212. else if (options.loglevel >= 0) {
  213. BLog_SetChannelLoglevel(i, options.loglevel);
  214. }
  215. }
  216. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  217. // initialize sockets
  218. if (BSocket_GlobalInit() < 0) {
  219. BLog(BLOG_ERROR, "BSocket_GlobalInit failed");
  220. goto fail1;
  221. }
  222. // init time
  223. BTime_Init();
  224. // init reactor
  225. if (!BReactor_Init(&ss)) {
  226. BLog(BLOG_ERROR, "BReactor_Init failed");
  227. goto fail1;
  228. }
  229. // init process manager
  230. if (!BProcessManager_Init(&manager, &ss)) {
  231. BLog(BLOG_ERROR, "BProcessManager_Init failed");
  232. goto fail1a;
  233. }
  234. // setup signal handler
  235. if (!BSignal_Init(&ss, signal_handler, NULL)) {
  236. BLog(BLOG_ERROR, "BSignal_Init failed");
  237. goto fail2;
  238. }
  239. // read config file
  240. uint8_t *file;
  241. size_t file_len;
  242. if (!read_file(options.config_file, &file, &file_len)) {
  243. BLog(BLOG_ERROR, "failed to read config file");
  244. goto fail3;
  245. }
  246. // parse config file
  247. if (!NCDConfigParser_Parse((char *)file, file_len, &configuration)) {
  248. BLog(BLOG_ERROR, "NCDConfigParser_Parse failed");
  249. free(file);
  250. goto fail3;
  251. }
  252. // fee config file memory
  253. free(file);
  254. // init interfaces list
  255. LinkedList2_Init(&interfaces);
  256. // set no DNS servers
  257. num_ipv4_dns_servers = 0;
  258. if (!set_dns_servers()) {
  259. BLog(BLOG_ERROR, "failed to set no DNS servers");
  260. goto fail5;
  261. }
  262. // init interfaces
  263. load_interfaces(configuration);
  264. goto event_loop;
  265. fail5:
  266. NCDConfig_free_interfaces(configuration);
  267. fail3:
  268. BSignal_Finish();
  269. fail2:
  270. BProcessManager_Free(&manager);
  271. fail1a:
  272. BReactor_Free(&ss);
  273. fail1:
  274. BLog(BLOG_ERROR, "initialization failed");
  275. BLog_Free();
  276. fail0:
  277. // finish objects
  278. DebugObjectGlobal_Finish();
  279. return 1;
  280. event_loop:
  281. // enter event loop
  282. BLog(BLOG_NOTICE, "entering event loop");
  283. int ret = BReactor_Exec(&ss);
  284. // free reactor
  285. BReactor_Free(&ss);
  286. // free logger
  287. BLog(BLOG_NOTICE, "exiting");
  288. BLog_Free();
  289. // finish objects
  290. DebugObjectGlobal_Finish();
  291. return ret;
  292. }
  293. void terminate (void)
  294. {
  295. BLog(BLOG_NOTICE, "tearing down");
  296. // free interfaces
  297. free_interfaces();
  298. // free configuration
  299. NCDConfig_free_interfaces(configuration);
  300. // remove signal handler
  301. BSignal_Finish();
  302. // free process manager
  303. BProcessManager_Free(&manager);
  304. // exit reactor
  305. BReactor_Quit(&ss, 1);
  306. }
  307. void print_help (const char *name)
  308. {
  309. printf(
  310. "Usage:\n"
  311. " %s\n"
  312. " [--help]\n"
  313. " [--version]\n"
  314. " [--logger <"LOGGERS_STRING">]\n"
  315. #ifndef BADVPN_USE_WINAPI
  316. " (logger=syslog?\n"
  317. " [--syslog-facility <string>]\n"
  318. " [--syslog-ident <string>]\n"
  319. " )\n"
  320. #endif
  321. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  322. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  323. " --config-file <file>\n",
  324. name
  325. );
  326. }
  327. void print_version (void)
  328. {
  329. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  330. }
  331. int parse_arguments (int argc, char *argv[])
  332. {
  333. if (argc <= 0) {
  334. return 0;
  335. }
  336. options.help = 0;
  337. options.version = 0;
  338. options.logger = LOGGER_STDOUT;
  339. #ifndef BADVPN_USE_WINAPI
  340. options.logger_syslog_facility = "daemon";
  341. options.logger_syslog_ident = argv[0];
  342. #endif
  343. options.loglevel = -1;
  344. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  345. options.loglevels[i] = -1;
  346. }
  347. options.config_file = NULL;
  348. for (int i = 1; i < argc; i++) {
  349. char *arg = argv[i];
  350. if (!strcmp(arg, "--help")) {
  351. options.help = 1;
  352. }
  353. else if (!strcmp(arg, "--version")) {
  354. options.version = 1;
  355. }
  356. else if (!strcmp(arg, "--logger")) {
  357. if (1 >= argc - i) {
  358. fprintf(stderr, "%s: requires an argument\n", arg);
  359. return 0;
  360. }
  361. char *arg2 = argv[i + 1];
  362. if (!strcmp(arg2, "stdout")) {
  363. options.logger = LOGGER_STDOUT;
  364. }
  365. #ifndef BADVPN_USE_WINAPI
  366. else if (!strcmp(arg2, "syslog")) {
  367. options.logger = LOGGER_SYSLOG;
  368. }
  369. #endif
  370. else {
  371. fprintf(stderr, "%s: wrong argument\n", arg);
  372. return 0;
  373. }
  374. i++;
  375. }
  376. #ifndef BADVPN_USE_WINAPI
  377. else if (!strcmp(arg, "--syslog-facility")) {
  378. if (1 >= argc - i) {
  379. fprintf(stderr, "%s: requires an argument\n", arg);
  380. return 0;
  381. }
  382. options.logger_syslog_facility = argv[i + 1];
  383. i++;
  384. }
  385. else if (!strcmp(arg, "--syslog-ident")) {
  386. if (1 >= argc - i) {
  387. fprintf(stderr, "%s: requires an argument\n", arg);
  388. return 0;
  389. }
  390. options.logger_syslog_ident = argv[i + 1];
  391. i++;
  392. }
  393. #endif
  394. else if (!strcmp(arg, "--loglevel")) {
  395. if (1 >= argc - i) {
  396. fprintf(stderr, "%s: requires an argument\n", arg);
  397. return 0;
  398. }
  399. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  400. fprintf(stderr, "%s: wrong argument\n", arg);
  401. return 0;
  402. }
  403. i++;
  404. }
  405. else if (!strcmp(arg, "--channel-loglevel")) {
  406. if (2 >= argc - i) {
  407. fprintf(stderr, "%s: requires two arguments\n", arg);
  408. return 0;
  409. }
  410. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  411. if (channel < 0) {
  412. fprintf(stderr, "%s: wrong channel argument\n", arg);
  413. return 0;
  414. }
  415. int loglevel = parse_loglevel(argv[i + 2]);
  416. if (loglevel < 0) {
  417. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  418. return 0;
  419. }
  420. options.loglevels[channel] = loglevel;
  421. i += 2;
  422. }
  423. else if (!strcmp(arg, "--config-file")) {
  424. if (1 >= argc - i) {
  425. fprintf(stderr, "%s: requires an argument\n", arg);
  426. return 0;
  427. }
  428. options.config_file = argv[i + 1];
  429. i++;
  430. }
  431. else {
  432. fprintf(stderr, "unknown option: %s\n", arg);
  433. return 0;
  434. }
  435. }
  436. if (options.help || options.version) {
  437. return 1;
  438. }
  439. if (!options.config_file) {
  440. fprintf(stderr, "--config-file is required\n");
  441. return 0;
  442. }
  443. return 1;
  444. }
  445. void signal_handler (void *unused)
  446. {
  447. BLog(BLOG_NOTICE, "termination requested");
  448. terminate();
  449. return;
  450. }
  451. void load_interfaces (struct NCDConfig_interfaces *conf)
  452. {
  453. while (conf) {
  454. interface_init(conf);
  455. conf = conf->next;
  456. }
  457. }
  458. void free_interfaces (void)
  459. {
  460. // remove in reverse order so we don't have to remove incoming dependencies
  461. LinkedList2Node *node;
  462. while (node = LinkedList2_GetLast(&interfaces)) {
  463. struct interface *iface = UPPER_OBJECT(node, struct interface, list_node);
  464. interface_free(iface);
  465. }
  466. }
  467. struct dns_sort_entry {
  468. uint32_t addr;
  469. int priority;
  470. };
  471. int set_dns_servers (void)
  472. {
  473. // collect servers
  474. struct dns_sort_entry servers[num_ipv4_dns_servers];
  475. size_t num_servers = 0;
  476. LinkedList2Iterator if_it;
  477. LinkedList2Iterator_InitForward(&if_it, &interfaces);
  478. LinkedList2Node *if_node;
  479. while (if_node = LinkedList2Iterator_Next(&if_it)) {
  480. struct interface *iface = UPPER_OBJECT(if_node, struct interface, list_node);
  481. LinkedList2Iterator serv_it;
  482. LinkedList2Iterator_InitForward(&serv_it, &iface->ipv4_dns_servers);
  483. LinkedList2Node *serv_node;
  484. while (serv_node = LinkedList2Iterator_Next(&serv_it)) {
  485. struct ipv4_dns_entry *dns = UPPER_OBJECT(serv_node, struct ipv4_dns_entry, list_node);
  486. servers[num_servers].addr = dns->addr;
  487. servers[num_servers].priority= dns->priority;
  488. num_servers++;
  489. }
  490. }
  491. ASSERT(num_servers == num_ipv4_dns_servers)
  492. // sort by priority
  493. qsort(servers, num_servers, sizeof(servers[0]), dns_qsort_comparator);
  494. // copy addresses into an array
  495. uint32_t addrs[num_servers];
  496. for (size_t i = 0; i < num_servers; i++) {
  497. addrs[i] = servers[i].addr;
  498. }
  499. // set servers
  500. if (!NCDIfConfig_set_dns_servers(addrs, num_servers)) {
  501. BLog(BLOG_ERROR, "failed to set DNS servers");
  502. return 0;
  503. }
  504. return 1;
  505. }
  506. int dns_qsort_comparator (const void *v1, const void *v2)
  507. {
  508. const struct dns_sort_entry *e1 = v1;
  509. const struct dns_sort_entry *e2 = v2;
  510. if (e1->priority < e2->priority) {
  511. return -1;
  512. }
  513. if (e1->priority > e2->priority) {
  514. return 1;
  515. }
  516. return 0;
  517. }
  518. struct interface * find_interface (const char *name)
  519. {
  520. LinkedList2Iterator it;
  521. LinkedList2Iterator_InitForward(&it, &interfaces);
  522. LinkedList2Node *node;
  523. while (node = LinkedList2Iterator_Next(&it)) {
  524. struct interface *iface = UPPER_OBJECT(node, struct interface, list_node);
  525. if (!strcmp(iface->conf->name, name)) {
  526. LinkedList2Iterator_Free(&it);
  527. return iface;
  528. }
  529. }
  530. return NULL;
  531. }
  532. int interface_init (struct NCDConfig_interfaces *conf)
  533. {
  534. // check for existing interface
  535. if (find_interface(conf->name)) {
  536. BLog(BLOG_ERROR, "interface %s already exists", conf->name);
  537. goto fail0;
  538. }
  539. // allocate interface entry
  540. struct interface *iface = malloc(sizeof(*iface));
  541. if (!iface) {
  542. BLog(BLOG_ERROR, "malloc failed for interface %s", conf->name);
  543. goto fail0;
  544. }
  545. // set conf
  546. iface->conf = conf;
  547. // find interface module
  548. struct NCDConfig_statements *type_st = NCDConfig_find_statement(conf->statements, "type");
  549. if (!type_st) {
  550. interface_log(iface, BLOG_ERROR, "missing type");
  551. goto fail1;
  552. }
  553. char *type;
  554. if (!NCDConfig_statement_has_one_arg(type_st, &type)) {
  555. interface_log(iface, BLOG_ERROR, "type: wrong arity");
  556. goto fail1;
  557. }
  558. const struct NCDInterfaceModule **m;
  559. for (m = interface_modules; *m; m++) {
  560. if (!strcmp((*m)->type, type)) {
  561. break;
  562. }
  563. }
  564. if (!*m) {
  565. interface_log(iface, BLOG_ERROR, "type: unknown value");
  566. goto fail1;
  567. }
  568. iface->module = *m;
  569. // init reset timer
  570. BTimer_Init(&iface->reset_timer, INTERFACE_RETRY_TIME, (BTimer_handler)interface_reset_timer_handler, iface);
  571. // init outgoing dependencies list
  572. LinkedList2_Init(&iface->deps_out);
  573. // init outgoing dependencies
  574. struct NCDConfig_statements *need_st = conf->statements;
  575. while (need_st = NCDConfig_find_statement(need_st, "need")) {
  576. char *need_ifname;
  577. if (!NCDConfig_statement_has_one_arg(need_st, &need_ifname)) {
  578. interface_log(iface, BLOG_ERROR, "need: wrong arity");
  579. goto fail2;
  580. }
  581. struct interface *need_if = find_interface(need_ifname);
  582. if (!need_if) {
  583. interface_log(iface, BLOG_ERROR, "need: %s: unknown interface", need_ifname);
  584. goto fail2;
  585. }
  586. if (!interface_add_dependency(iface, need_if)) {
  587. interface_log(iface, BLOG_ERROR, "need: %s: failed to add dependency", need_ifname);
  588. goto fail2;
  589. }
  590. need_st = need_st->next;
  591. }
  592. // init incoming dependencies list
  593. LinkedList2_Init(&iface->deps_in);
  594. // set no module instance
  595. iface->have_module_instance = 0;
  596. // set no DHCP
  597. iface->have_dhcp = 0;
  598. // init ipv4 addresses list
  599. LinkedList2_Init(&iface->ipv4_addresses);
  600. // init ipv4 routes list
  601. LinkedList2_Init(&iface->ipv4_routes);
  602. // init ipv4 dns servers list
  603. LinkedList2_Init(&iface->ipv4_dns_servers);
  604. // insert to interfaces list
  605. LinkedList2_Append(&interfaces, &iface->list_node);
  606. interface_start(iface);
  607. return 1;
  608. fail2:
  609. interface_remove_dependencies(iface);
  610. fail1:
  611. free(iface);
  612. fail0:
  613. return 0;
  614. }
  615. void interface_free (struct interface *iface)
  616. {
  617. ASSERT(LinkedList2_IsEmpty(&iface->deps_in))
  618. // deconfigure IPv4
  619. interface_deconfigure_ipv4(iface);
  620. // free DHCP
  621. if (iface->have_dhcp) {
  622. BDHCPClient_Free(&iface->dhcp);
  623. }
  624. // free module instance
  625. if (iface->have_module_instance) {
  626. interface_module_free(iface);
  627. }
  628. // remove from interfaces list
  629. LinkedList2_Remove(&interfaces, &iface->list_node);
  630. // remove outgoing dependencies
  631. interface_remove_dependencies(iface);
  632. // stop reset timer
  633. BReactor_RemoveTimer(&ss, &iface->reset_timer);
  634. // free memory
  635. free(iface);
  636. }
  637. void interface_down_to (struct interface *iface, int state)
  638. {
  639. ASSERT(state >= INTERFACE_STATE_WAITDEPS)
  640. if (state < INTERFACE_STATE_FINISHED) {
  641. // unsatisfy incoming dependencies
  642. interface_unsatisfy_incoming_depenencies(iface);
  643. // deconfigure IPv4
  644. interface_deconfigure_ipv4(iface);
  645. }
  646. // deconfigure DHCP
  647. if (state < INTERFACE_STATE_DHCP && iface->have_dhcp) {
  648. BDHCPClient_Free(&iface->dhcp);
  649. iface->have_dhcp = 0;
  650. }
  651. // free module instance
  652. if (state < INTERFACE_STATE_WAITMODULE && iface->have_module_instance) {
  653. interface_module_free(iface);
  654. }
  655. // start/stop reset timer
  656. if (state == INTERFACE_STATE_RESETTING) {
  657. BReactor_SetTimer(&ss, &iface->reset_timer);
  658. } else {
  659. BReactor_RemoveTimer(&ss, &iface->reset_timer);
  660. }
  661. // set state
  662. iface->state = state;
  663. }
  664. void interface_reset (struct interface *iface)
  665. {
  666. interface_log(iface, BLOG_INFO, "will try again later");
  667. interface_down_to(iface, INTERFACE_STATE_RESETTING);
  668. }
  669. void interface_start (struct interface *iface)
  670. {
  671. ASSERT(!BTimer_IsRunning(&iface->reset_timer))
  672. ASSERT(!iface->have_module_instance)
  673. ASSERT(!iface->have_dhcp)
  674. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
  675. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
  676. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
  677. // check dependencies
  678. if (!interface_dependencies_satisfied(iface)) {
  679. interface_log(iface, BLOG_INFO, "waiting for dependencies");
  680. // waiting for dependencies
  681. iface->state = INTERFACE_STATE_WAITDEPS;
  682. return;
  683. }
  684. // init module
  685. if (!interface_module_init(iface)) {
  686. goto fail;
  687. }
  688. interface_log(iface, BLOG_INFO, "waiting for module");
  689. // waiting for module
  690. iface->state = INTERFACE_STATE_WAITMODULE;
  691. return;
  692. fail:
  693. interface_reset(iface);
  694. }
  695. void interface_module_up (struct interface *iface)
  696. {
  697. ASSERT(iface->have_module_instance)
  698. ASSERT(!iface->have_dhcp)
  699. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
  700. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
  701. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
  702. // check for DHCP
  703. struct NCDConfig_statements *dhcp_st = NCDConfig_find_statement(iface->conf->statements, "dhcp");
  704. if (dhcp_st) {
  705. if (dhcp_st->args) {
  706. interface_log(iface, BLOG_ERROR, "dhcp: wrong arity");
  707. goto fail;
  708. }
  709. // init DHCP client
  710. if (!BDHCPClient_Init(&iface->dhcp, iface->conf->name, &ss, (BDHCPClient_handler)interface_dhcp_handler, iface)) {
  711. interface_log(iface, BLOG_ERROR, "BDHCPClient_Init failed");
  712. goto fail;
  713. }
  714. // set have DHCP
  715. iface->have_dhcp = 1;
  716. // set state
  717. iface->state = INTERFACE_STATE_DHCP;
  718. return;
  719. }
  720. // configure IPv4
  721. if (!interface_configure_ipv4(iface)) {
  722. goto fail;
  723. }
  724. // set state finished
  725. iface->state = INTERFACE_STATE_FINISHED;
  726. // satisfy incoming dependencies
  727. interface_satisfy_incoming_depenencies(iface);
  728. return;
  729. fail:
  730. interface_reset(iface);
  731. }
  732. void interface_log (struct interface *iface, int level, const char *fmt, ...)
  733. {
  734. va_list vl;
  735. va_start(vl, fmt);
  736. BLog_Append("interface %s: ", iface->conf->name);
  737. BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
  738. va_end(vl);
  739. }
  740. void interface_reset_timer_handler (struct interface *iface)
  741. {
  742. ASSERT(iface->state == INTERFACE_STATE_RESETTING)
  743. // start interface
  744. interface_start(iface);
  745. }
  746. int interface_module_init (struct interface *iface)
  747. {
  748. ASSERT(!iface->have_module_instance)
  749. if (!NCDInterfaceModuleInst_Init(
  750. &iface->module_instance, iface->module, &ss, &manager, iface->conf,
  751. (NCDInterfaceModule_handler_event)interface_module_handler_event,
  752. (NCDInterfaceModule_handler_error)interface_module_handler_error,
  753. iface
  754. )) {
  755. interface_log(iface, BLOG_ERROR, "failed to initialize module instance");
  756. return 0;
  757. }
  758. iface->have_module_instance = 1;
  759. return 1;
  760. }
  761. void interface_module_free (struct interface *iface)
  762. {
  763. ASSERT(iface->have_module_instance)
  764. NCDInterfaceModuleInst_Free(&iface->module_instance);
  765. iface->have_module_instance = 0;
  766. }
  767. void interface_module_handler_event (struct interface *iface, int event)
  768. {
  769. ASSERT(iface->have_module_instance)
  770. ASSERT(iface->state >= INTERFACE_STATE_WAITMODULE)
  771. switch (event) {
  772. case NCDINTERFACEMODULE_EVENT_UP: {
  773. ASSERT(iface->state == INTERFACE_STATE_WAITMODULE)
  774. interface_log(iface, BLOG_INFO, "module up");
  775. interface_module_up(iface);
  776. } break;
  777. case NCDINTERFACEMODULE_EVENT_DOWN: {
  778. ASSERT(iface->state > INTERFACE_STATE_WAITMODULE)
  779. interface_log(iface, BLOG_INFO, "module down");
  780. interface_down_to(iface, INTERFACE_STATE_WAITMODULE);
  781. } break;
  782. default:
  783. ASSERT(0);
  784. }
  785. }
  786. void interface_module_handler_error (struct interface *iface)
  787. {
  788. ASSERT(iface->have_module_instance)
  789. ASSERT(iface->state >= INTERFACE_STATE_WAITMODULE)
  790. interface_log(iface, BLOG_INFO, "module error");
  791. interface_reset(iface);
  792. }
  793. void interface_dhcp_handler (struct interface *iface, int event)
  794. {
  795. ASSERT(iface->have_dhcp)
  796. switch (event) {
  797. case BDHCPCLIENT_EVENT_UP: {
  798. ASSERT(iface->state == INTERFACE_STATE_DHCP)
  799. interface_log(iface, BLOG_INFO, "DHCP up");
  800. // configure IPv4
  801. if (!interface_configure_ipv4(iface)) {
  802. goto fail;
  803. }
  804. // set state
  805. iface->state = INTERFACE_STATE_FINISHED;
  806. // satisfy incoming dependencies
  807. interface_satisfy_incoming_depenencies(iface);
  808. return;
  809. fail:
  810. interface_reset(iface);
  811. } break;
  812. case BDHCPCLIENT_EVENT_DOWN: {
  813. ASSERT(iface->state == INTERFACE_STATE_FINISHED)
  814. interface_log(iface, BLOG_INFO, "DHCP down");
  815. interface_down_to(iface, INTERFACE_STATE_DHCP);
  816. } break;
  817. default:
  818. ASSERT(0);
  819. }
  820. }
  821. void interface_remove_dependencies (struct interface *iface)
  822. {
  823. LinkedList2Node *node;
  824. while (node = LinkedList2_GetFirst(&iface->deps_out)) {
  825. struct dependency *d = UPPER_OBJECT(node, struct dependency, src_node);
  826. ASSERT(d->src == iface)
  827. remove_dependency(d);
  828. }
  829. }
  830. int interface_add_dependency (struct interface *iface, struct interface *dst_iface)
  831. {
  832. // allocate entry
  833. struct dependency *d = malloc(sizeof(*d));
  834. if (!d) {
  835. return 0;
  836. }
  837. // set src and dst
  838. d->src = iface;
  839. d->dst = dst_iface;
  840. // insert to src list
  841. LinkedList2_Append(&iface->deps_out, &d->src_node);
  842. // insert to dst list
  843. LinkedList2_Append(&dst_iface->deps_in, &d->dst_node);
  844. return 1;
  845. }
  846. void remove_dependency (struct dependency *d)
  847. {
  848. // remove from dst list
  849. LinkedList2_Remove(&d->dst->deps_in, &d->dst_node);
  850. // remove from src list
  851. LinkedList2_Remove(&d->src->deps_out, &d->src_node);
  852. // free entry
  853. free(d);
  854. }
  855. int interface_dependencies_satisfied (struct interface *iface)
  856. {
  857. LinkedList2Iterator it;
  858. LinkedList2Iterator_InitForward(&it, &iface->deps_out);
  859. LinkedList2Node *node;
  860. while (node = LinkedList2Iterator_Next(&it)) {
  861. struct dependency *d = UPPER_OBJECT(node, struct dependency, src_node);
  862. ASSERT(d->src == iface)
  863. if (d->dst->state != INTERFACE_STATE_FINISHED) {
  864. LinkedList2Iterator_Free(&it);
  865. return 0;
  866. }
  867. }
  868. return 1;
  869. }
  870. void interface_satisfy_incoming_depenencies (struct interface *iface)
  871. {
  872. LinkedList2Iterator it;
  873. LinkedList2Iterator_InitForward(&it, &iface->deps_in);
  874. LinkedList2Node *node;
  875. while (node = LinkedList2Iterator_Next(&it)) {
  876. struct dependency *d = UPPER_OBJECT(node, struct dependency, dst_node);
  877. ASSERT(d->dst == iface)
  878. if (d->src->state == INTERFACE_STATE_WAITDEPS) {
  879. interface_log(d->src, BLOG_INFO, "dependency %s satisfied", iface->conf->name);
  880. interface_start(d->src);
  881. }
  882. }
  883. }
  884. void interface_unsatisfy_incoming_depenencies (struct interface *iface)
  885. {
  886. LinkedList2Iterator it;
  887. LinkedList2Iterator_InitForward(&it, &iface->deps_in);
  888. LinkedList2Node *node;
  889. while (node = LinkedList2Iterator_Next(&it)) {
  890. struct dependency *d = UPPER_OBJECT(node, struct dependency, dst_node);
  891. ASSERT(d->dst == iface)
  892. if (d->src->state > INTERFACE_STATE_WAITDEPS) {
  893. interface_log(d->src, BLOG_INFO, "dependency %s no longer satisfied", iface->conf->name);
  894. interface_down_to(d->src, INTERFACE_STATE_WAITDEPS);
  895. }
  896. }
  897. }
  898. int interface_configure_ipv4 (struct interface *iface)
  899. {
  900. ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
  901. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
  902. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
  903. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
  904. // configure addresses
  905. if (!interface_add_ipv4_addresses(iface)) {
  906. return 0;
  907. }
  908. // configure routes
  909. if (!interface_add_ipv4_routes(iface)) {
  910. return 0;
  911. }
  912. // configure DNS servers
  913. if (!interface_add_ipv4_dns_servers(iface)) {
  914. return 0;
  915. }
  916. return 1;
  917. }
  918. void interface_deconfigure_ipv4 (struct interface *iface)
  919. {
  920. // remove DNS servers
  921. interface_remove_ipv4_dns_servers(iface);
  922. // remove routes
  923. interface_remove_ipv4_routes(iface);
  924. // remove addresses
  925. interface_remove_ipv4_addresses(iface);
  926. }
  927. int interface_add_ipv4_addresses (struct interface *iface)
  928. {
  929. ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
  930. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_addresses))
  931. struct NCDConfig_statements *st = iface->conf->statements;
  932. while (st = NCDConfig_find_statement(st, "ipv4.addr")) {
  933. // get address string
  934. char *addrstr;
  935. if (!NCDConfig_statement_has_one_arg(st, &addrstr)) {
  936. interface_log(iface, BLOG_ERROR, "ipv4.addr: wrong arity");
  937. return 0;
  938. }
  939. struct ipv4_ifaddr ifaddr;
  940. // is this a DHCP address?
  941. if (!strcmp(addrstr, "dhcp_addr")) {
  942. // check if DHCP is enabled
  943. if (!iface->have_dhcp) {
  944. interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: DHCP not enabled", addrstr);
  945. return 0;
  946. }
  947. // get address and mask
  948. uint32_t addr;
  949. BDHCPClient_GetClientIP(&iface->dhcp, &addr);
  950. uint32_t mask;
  951. BDHCPClient_GetClientMask(&iface->dhcp, &mask);
  952. // convert to ifaddr
  953. if (!ipaddr_ipv4_ifaddr_from_addr_mask(addr, mask, &ifaddr)) {
  954. interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: wrong mask", addrstr);
  955. return 0;
  956. }
  957. } else {
  958. // parse address string
  959. if (!ipaddr_parse_ipv4_ifaddr(addrstr, &ifaddr)) {
  960. interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: wrong format", addrstr);
  961. return 0;
  962. }
  963. }
  964. // add this address
  965. if (!interface_add_ipv4_addr(iface, ifaddr)) {
  966. interface_log(iface, BLOG_ERROR, "ipv4.addr: %s: failed to add", addrstr);
  967. return 0;
  968. }
  969. st = st->next;
  970. }
  971. return 1;
  972. }
  973. void interface_remove_ipv4_addresses (struct interface *iface)
  974. {
  975. LinkedList2Node *node;
  976. while (node = LinkedList2_GetLast(&iface->ipv4_addresses)) {
  977. struct ipv4_addr_entry *entry = UPPER_OBJECT(node, struct ipv4_addr_entry, list_node);
  978. interface_remove_ipv4_addr(iface, entry);
  979. }
  980. }
  981. int interface_add_ipv4_routes (struct interface *iface)
  982. {
  983. ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
  984. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_routes))
  985. struct NCDConfig_statements *st = iface->conf->statements;
  986. while (st = NCDConfig_find_statement(st, "ipv4.route")) {
  987. // read statement
  988. char *dest_str;
  989. char *gateway_str;
  990. char *metric_str;
  991. if (!NCDConfig_statement_has_three_args(st, &dest_str, &gateway_str, &metric_str)) {
  992. interface_log(iface, BLOG_ERROR, "ipv4.route: wrong arity");
  993. return 0;
  994. }
  995. // parse dest string
  996. struct ipv4_ifaddr dest;
  997. if (!ipaddr_parse_ipv4_ifaddr(dest_str, &dest)) {
  998. interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): dest: wrong format", dest_str, gateway_str, metric_str);
  999. return 0;
  1000. }
  1001. int have_gateway;
  1002. uint32_t gateway;
  1003. // is gateway a DHCP router?
  1004. if (!strcmp(gateway_str, "dhcp_router")) {
  1005. // check if DHCP is enabled
  1006. if (!iface->have_dhcp) {
  1007. interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: DHCP not enabled", dest_str, gateway_str, metric_str);
  1008. return 0;
  1009. }
  1010. // obtain gateway from DHCP
  1011. if (!BDHCPClient_GetRouter(&iface->dhcp, &gateway)) {
  1012. interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: DHCP did not provide a router", dest_str, gateway_str, metric_str);
  1013. return 0;
  1014. }
  1015. have_gateway = 1;
  1016. }
  1017. else if (!strcmp(gateway_str, "none")) {
  1018. // no gateway
  1019. have_gateway = 0;
  1020. }
  1021. else {
  1022. // parse gateway string
  1023. if (!ipaddr_parse_ipv4_addr(gateway_str, strlen(gateway_str), &gateway)) {
  1024. interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): gateway: wrong format", dest_str, gateway_str, metric_str);
  1025. return 0;
  1026. }
  1027. have_gateway = 1;
  1028. }
  1029. // parse metric string
  1030. int metric = atoi(metric_str);
  1031. // add this address
  1032. if (!interface_add_ipv4_route(iface, dest, (have_gateway ? &gateway : NULL), metric)) {
  1033. interface_log(iface, BLOG_ERROR, "ipv4.route: (%s,%s,%s): failed to add", dest_str, gateway_str, metric_str);
  1034. return 0;
  1035. }
  1036. st = st->next;
  1037. }
  1038. return 1;
  1039. }
  1040. void interface_remove_ipv4_routes (struct interface *iface)
  1041. {
  1042. LinkedList2Node *node;
  1043. while (node = LinkedList2_GetLast(&iface->ipv4_routes)) {
  1044. struct ipv4_route_entry *entry = UPPER_OBJECT(node, struct ipv4_route_entry, list_node);
  1045. interface_remove_ipv4_route(iface, entry);
  1046. }
  1047. }
  1048. int interface_add_ipv4_dns_servers (struct interface *iface)
  1049. {
  1050. ASSERT(!(iface->have_dhcp) || BDHCPClient_IsUp(&iface->dhcp))
  1051. ASSERT(LinkedList2_IsEmpty(&iface->ipv4_dns_servers))
  1052. // read servers into entries
  1053. struct NCDConfig_statements *st = iface->conf->statements;
  1054. while (st = NCDConfig_find_statement(st, "ipv4.dns")) {
  1055. // read statement
  1056. char *addr_str;
  1057. char *priority_str;
  1058. if (!NCDConfig_statement_has_two_args(st, &addr_str, &priority_str)) {
  1059. interface_log(iface, BLOG_ERROR, "ipv4.dns: wrong arity");
  1060. return 0;
  1061. }
  1062. // parse priority string
  1063. int priority = atoi(priority_str);
  1064. // are these DHCP DNS servers?
  1065. if (!strcmp(addr_str, "dhcp_dns_servers")) {
  1066. // get servers from DHCP
  1067. uint32_t addrs[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
  1068. int num_addrs = BDHCPClient_GetDNS(&iface->dhcp, addrs, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
  1069. // add entries
  1070. for (int i = 0; i < num_addrs; i++) {
  1071. // add entry
  1072. if (!interface_add_ipv4_dns_entry(iface, addrs[i], priority)) {
  1073. interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): failed to add entry", addr_str, priority_str);
  1074. return 0;
  1075. }
  1076. }
  1077. } else {
  1078. // parse addr string
  1079. uint32_t addr;
  1080. if (!ipaddr_parse_ipv4_addr(addr_str, strlen(addr_str), &addr)) {
  1081. interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): addr: wrong format", addr_str, priority_str);
  1082. return 0;
  1083. }
  1084. // add entry
  1085. if (!interface_add_ipv4_dns_entry(iface, addr, priority)) {
  1086. interface_log(iface, BLOG_ERROR, "ipv4.dns: (%s,%s): failed to add entry", addr_str, priority_str);
  1087. return 0;
  1088. }
  1089. }
  1090. st = st->next;
  1091. }
  1092. // set servers
  1093. if (!set_dns_servers()) {
  1094. return 0;
  1095. }
  1096. return 1;
  1097. }
  1098. void interface_remove_ipv4_dns_servers (struct interface *iface)
  1099. {
  1100. // remove entries
  1101. LinkedList2Node *node;
  1102. while (node = LinkedList2_GetFirst(&iface->ipv4_dns_servers)) {
  1103. struct ipv4_dns_entry *entry = UPPER_OBJECT(node, struct ipv4_dns_entry, list_node);
  1104. interface_remove_ipv4_dns_entry(iface, entry);
  1105. }
  1106. // set servers
  1107. set_dns_servers();
  1108. }
  1109. int interface_add_ipv4_addr (struct interface *iface, struct ipv4_ifaddr ifaddr)
  1110. {
  1111. // add address entry
  1112. struct ipv4_addr_entry *entry = interface_add_ipv4_addr_entry(iface, ifaddr);
  1113. if (!entry) {
  1114. interface_log(iface, BLOG_ERROR, "failed to add ipv4 address entry");
  1115. return 0;
  1116. }
  1117. // assign the address
  1118. if (!NCDIfConfig_add_ipv4_addr(iface->conf->name, ifaddr)) {
  1119. interface_log(iface, BLOG_ERROR, "failed to assign ipv4 address");
  1120. interface_remove_ipv4_addr_entry(iface, entry);
  1121. return 0;
  1122. }
  1123. return 1;
  1124. }
  1125. void interface_remove_ipv4_addr (struct interface *iface, struct ipv4_addr_entry *entry)
  1126. {
  1127. // remove the address
  1128. if (!NCDIfConfig_remove_ipv4_addr(iface->conf->name, entry->ifaddr)) {
  1129. interface_log(iface, BLOG_ERROR, "failed to remove ipv4 address");
  1130. }
  1131. // remove address entry
  1132. interface_remove_ipv4_addr_entry(iface, entry);
  1133. }
  1134. int interface_add_ipv4_route (struct interface *iface, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric)
  1135. {
  1136. // add address entry
  1137. struct ipv4_route_entry *entry = interface_add_ipv4_route_entry(iface, dest, gateway, metric);
  1138. if (!entry) {
  1139. interface_log(iface, BLOG_ERROR, "failed to add ipv4 route entry");
  1140. return 0;
  1141. }
  1142. // add the route
  1143. if (!NCDIfConfig_add_ipv4_route(dest, gateway, metric, iface->conf->name)) {
  1144. interface_log(iface, BLOG_ERROR, "failed to add ipv4 route");
  1145. interface_remove_ipv4_route_entry(iface, entry);
  1146. return 0;
  1147. }
  1148. return 1;
  1149. }
  1150. void interface_remove_ipv4_route (struct interface *iface, struct ipv4_route_entry *entry)
  1151. {
  1152. // remove the route
  1153. if (!NCDIfConfig_remove_ipv4_route(entry->dest, (entry->have_gateway ? &entry->gateway : NULL), entry->metric, iface->conf->name)) {
  1154. interface_log(iface, BLOG_ERROR, "failed to remove ipv4 route");
  1155. }
  1156. // remove address entry
  1157. interface_remove_ipv4_route_entry(iface, entry);
  1158. }
  1159. struct ipv4_addr_entry * interface_add_ipv4_addr_entry (struct interface *iface, struct ipv4_ifaddr ifaddr)
  1160. {
  1161. // allocate entry
  1162. struct ipv4_addr_entry *entry = malloc(sizeof(*entry));
  1163. if (!entry) {
  1164. return NULL;
  1165. }
  1166. // set ifaddr
  1167. entry->ifaddr = ifaddr;
  1168. // add to list
  1169. LinkedList2_Append(&iface->ipv4_addresses, &entry->list_node);
  1170. return entry;
  1171. }
  1172. void interface_remove_ipv4_addr_entry (struct interface *iface, struct ipv4_addr_entry *entry)
  1173. {
  1174. // remove from list
  1175. LinkedList2_Remove(&iface->ipv4_addresses, &entry->list_node);
  1176. // free entry
  1177. free(entry);
  1178. }
  1179. struct ipv4_route_entry * interface_add_ipv4_route_entry (struct interface *iface, struct ipv4_ifaddr dest, const uint32_t *gateway, int metric)
  1180. {
  1181. // allocate entry
  1182. struct ipv4_route_entry *entry = malloc(sizeof(*entry));
  1183. if (!entry) {
  1184. return NULL;
  1185. }
  1186. // set info
  1187. entry->dest = dest;
  1188. if (gateway) {
  1189. entry->have_gateway = 1;
  1190. entry->gateway = *gateway;
  1191. } else {
  1192. entry->have_gateway = 0;
  1193. }
  1194. entry->metric = metric;
  1195. // add to list
  1196. LinkedList2_Append(&iface->ipv4_routes, &entry->list_node);
  1197. return entry;
  1198. }
  1199. void interface_remove_ipv4_route_entry (struct interface *iface, struct ipv4_route_entry *entry)
  1200. {
  1201. // remove from list
  1202. LinkedList2_Remove(&iface->ipv4_routes, &entry->list_node);
  1203. // free entry
  1204. free(entry);
  1205. }
  1206. struct ipv4_dns_entry * interface_add_ipv4_dns_entry (struct interface *iface, uint32_t addr, int priority)
  1207. {
  1208. // allocate entry
  1209. struct ipv4_dns_entry *entry = malloc(sizeof(*entry));
  1210. if (!entry) {
  1211. return NULL;
  1212. }
  1213. // set info
  1214. entry->addr = addr;
  1215. entry->priority = priority;
  1216. // add to list
  1217. LinkedList2_Append(&iface->ipv4_dns_servers, &entry->list_node);
  1218. // increment number of DNS servers
  1219. num_ipv4_dns_servers++;
  1220. return entry;
  1221. }
  1222. void interface_remove_ipv4_dns_entry (struct interface *iface, struct ipv4_dns_entry *entry)
  1223. {
  1224. // decrement number of DNS servers
  1225. num_ipv4_dns_servers--;
  1226. // remove from list
  1227. LinkedList2_Remove(&iface->ipv4_dns_servers, &entry->list_node);
  1228. // free entry
  1229. free(entry);
  1230. }