ncd.c 42 KB

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