ncd.c 43 KB

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