ncd.c 45 KB

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