ncd.c 44 KB

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