interface.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  1. /*
  2. * netifd - network interface daemon
  3. * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2
  7. * as published by the Free Software Foundation
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. */
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <sys/types.h>
  18. #include <sys/wait.h>
  19. #include "netifd.h"
  20. #include "device.h"
  21. #include "interface.h"
  22. #include "interface-ip.h"
  23. #include "proto.h"
  24. #include "ubus.h"
  25. #include "config.h"
  26. #include "system.h"
  27. struct vlist_tree interfaces;
  28. static LIST_HEAD(iface_all_users);
  29. enum {
  30. IFACE_ATTR_DEVICE,
  31. IFACE_ATTR_IFNAME, /* Backward compatibility */
  32. IFACE_ATTR_PROTO,
  33. IFACE_ATTR_AUTO,
  34. IFACE_ATTR_ZONE,
  35. IFACE_ATTR_JAIL,
  36. IFACE_ATTR_JAIL_DEVICE,
  37. IFACE_ATTR_JAIL_IFNAME,
  38. IFACE_ATTR_HOST_DEVICE,
  39. IFACE_ATTR_DEFAULTROUTE,
  40. IFACE_ATTR_PEERDNS,
  41. IFACE_ATTR_DNS,
  42. IFACE_ATTR_DNS_SEARCH,
  43. IFACE_ATTR_DNS_METRIC,
  44. IFACE_ATTR_METRIC,
  45. IFACE_ATTR_INTERFACE,
  46. IFACE_ATTR_IP6ASSIGN,
  47. IFACE_ATTR_IP6HINT,
  48. IFACE_ATTR_IP4TABLE,
  49. IFACE_ATTR_IP6TABLE,
  50. IFACE_ATTR_IP6CLASS,
  51. IFACE_ATTR_DELEGATE,
  52. IFACE_ATTR_IP6IFACEID,
  53. IFACE_ATTR_FORCE_LINK,
  54. IFACE_ATTR_IP6WEIGHT,
  55. IFACE_ATTR_MAX
  56. };
  57. static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
  58. [IFACE_ATTR_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
  59. [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
  60. [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
  61. [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
  62. [IFACE_ATTR_ZONE] = { .name = "zone", .type = BLOBMSG_TYPE_STRING },
  63. [IFACE_ATTR_JAIL] = { .name = "jail", .type = BLOBMSG_TYPE_STRING },
  64. [IFACE_ATTR_JAIL_DEVICE] = { .name = "jail_device", .type = BLOBMSG_TYPE_STRING },
  65. [IFACE_ATTR_JAIL_IFNAME] = { .name = "jail_ifname", .type = BLOBMSG_TYPE_STRING },
  66. [IFACE_ATTR_HOST_DEVICE] = { .name = "host_device", .type = BLOBMSG_TYPE_STRING },
  67. [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
  68. [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
  69. [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
  70. [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
  71. [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
  72. [IFACE_ATTR_DNS_METRIC] = { .name = "dns_metric", .type = BLOBMSG_TYPE_INT32 },
  73. [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  74. [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
  75. [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
  76. [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
  77. [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
  78. [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
  79. [IFACE_ATTR_DELEGATE] = { .name = "delegate", .type = BLOBMSG_TYPE_BOOL },
  80. [IFACE_ATTR_IP6IFACEID] = { .name = "ip6ifaceid", .type = BLOBMSG_TYPE_STRING },
  81. [IFACE_ATTR_FORCE_LINK] = { .name = "force_link", .type = BLOBMSG_TYPE_BOOL },
  82. [IFACE_ATTR_IP6WEIGHT] = { .name = "ip6weight", .type = BLOBMSG_TYPE_INT32 },
  83. };
  84. const struct uci_blob_param_list interface_attr_list = {
  85. .n_params = IFACE_ATTR_MAX,
  86. .params = iface_attrs,
  87. };
  88. static void
  89. interface_set_main_dev(struct interface *iface, struct device *dev);
  90. static void
  91. interface_event(struct interface *iface, enum interface_event ev);
  92. static void
  93. interface_error_flush(struct interface *iface)
  94. {
  95. struct interface_error *error, *tmp;
  96. list_for_each_entry_safe(error, tmp, &iface->errors, list) {
  97. list_del(&error->list);
  98. free(error);
  99. }
  100. }
  101. static bool
  102. interface_force_link(struct interface *iface)
  103. {
  104. struct device *dev = iface->main_dev.dev;
  105. if (dev && dev->settings.auth)
  106. return false;
  107. return iface->force_link;
  108. }
  109. static void
  110. interface_clear_errors(struct interface *iface)
  111. {
  112. /* don't flush the errors in case the configured protocol handler matches the
  113. running protocol handler and is having the last error capability */
  114. if (!(iface->proto &&
  115. (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
  116. (iface->proto->handler->name == iface->proto_handler->name)))
  117. interface_error_flush(iface);
  118. }
  119. void interface_add_error(struct interface *iface, const char *subsystem,
  120. const char *code, const char **data, int n_data)
  121. {
  122. struct interface_error *error;
  123. int i, len = 0;
  124. int *datalen = NULL;
  125. char *dest, *d_subsys, *d_code;
  126. /* if the configured protocol handler has the last error support capability,
  127. errors should only be added if the running protocol handler matches the
  128. configured one */
  129. if (iface->proto &&
  130. (iface->proto->handler->flags & PROTO_FLAG_LASTERROR) &&
  131. (iface->proto->handler->name != iface->proto_handler->name))
  132. return;
  133. if (n_data) {
  134. len = n_data * sizeof(char *);
  135. datalen = alloca(len);
  136. for (i = 0; i < n_data; i++) {
  137. datalen[i] = strlen(data[i]) + 1;
  138. len += datalen[i];
  139. }
  140. }
  141. error = calloc_a(sizeof(*error) + sizeof(char *) + len,
  142. &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
  143. &d_code, code ? strlen(code) + 1 : 0);
  144. if (!error)
  145. return;
  146. /* Only keep the last flagged error, prevent this list grows unlimitted in case the
  147. protocol can't be established (e.g auth failure) */
  148. if (iface->proto_handler->flags & PROTO_FLAG_LASTERROR)
  149. interface_error_flush(iface);
  150. list_add_tail(&error->list, &iface->errors);
  151. dest = (char *) &error->data[n_data + 1];
  152. for (i = 0; i < n_data; i++) {
  153. error->data[i] = dest;
  154. memcpy(dest, data[i], datalen[i]);
  155. dest += datalen[i];
  156. }
  157. error->data[n_data] = NULL;
  158. if (subsystem)
  159. error->subsystem = strcpy(d_subsys, subsystem);
  160. if (code)
  161. error->code = strcpy(d_code, code);
  162. }
  163. static void
  164. interface_data_del(struct interface *iface, struct interface_data *data)
  165. {
  166. avl_delete(&iface->data, &data->node);
  167. free(data);
  168. }
  169. static void
  170. interface_data_flush(struct interface *iface)
  171. {
  172. struct interface_data *d, *tmp;
  173. avl_for_each_element_safe(&iface->data, d, node, tmp)
  174. interface_data_del(iface, d);
  175. }
  176. int
  177. interface_add_data(struct interface *iface, const struct blob_attr *data)
  178. {
  179. struct interface_data *n, *o;
  180. if (!blobmsg_check_attr(data, true))
  181. return UBUS_STATUS_INVALID_ARGUMENT;
  182. const char *name = blobmsg_name(data);
  183. unsigned len = blob_pad_len(data);
  184. o = avl_find_element(&iface->data, name, o, node);
  185. if (o) {
  186. if (blob_pad_len(o->data) == len && !memcmp(o->data, data, len))
  187. return 0;
  188. interface_data_del(iface, o);
  189. }
  190. n = calloc(1, sizeof(*n) + len);
  191. if (!n)
  192. return UBUS_STATUS_UNKNOWN_ERROR;
  193. memcpy(n->data, data, len);
  194. n->node.key = blobmsg_name(n->data);
  195. avl_insert(&iface->data, &n->node);
  196. iface->updated |= IUF_DATA;
  197. return 0;
  198. }
  199. int interface_parse_data(struct interface *iface, const struct blob_attr *attr)
  200. {
  201. struct blob_attr *cur;
  202. size_t rem;
  203. int ret;
  204. iface->updated = 0;
  205. blob_for_each_attr(cur, attr, rem) {
  206. ret = interface_add_data(iface, cur);
  207. if (ret)
  208. return ret;
  209. }
  210. if (iface->updated && iface->state == IFS_UP)
  211. interface_event(iface, IFEV_UPDATE);
  212. return 0;
  213. }
  214. static void
  215. interface_event(struct interface *iface, enum interface_event ev)
  216. {
  217. struct interface_user *dep, *tmp;
  218. struct device *adev = NULL;
  219. list_for_each_entry_safe(dep, tmp, &iface->users, list)
  220. dep->cb(dep, iface, ev);
  221. list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
  222. dep->cb(dep, iface, ev);
  223. switch (ev) {
  224. case IFEV_UP:
  225. interface_error_flush(iface);
  226. adev = iface->l3_dev.dev;
  227. fallthrough;
  228. case IFEV_DOWN:
  229. case IFEV_UP_FAILED:
  230. alias_notify_device(iface->name, adev);
  231. break;
  232. default:
  233. break;
  234. }
  235. }
  236. static void
  237. interface_flush_state(struct interface *iface)
  238. {
  239. if (iface->l3_dev.dev)
  240. device_release(&iface->l3_dev);
  241. interface_data_flush(iface);
  242. }
  243. static void
  244. mark_interface_down(struct interface *iface)
  245. {
  246. enum interface_state state = iface->state;
  247. if (state == IFS_DOWN)
  248. return;
  249. iface->link_up_event = false;
  250. iface->state = IFS_DOWN;
  251. switch (state) {
  252. case IFS_UP:
  253. case IFS_TEARDOWN:
  254. interface_event(iface, IFEV_DOWN);
  255. break;
  256. case IFS_SETUP:
  257. interface_event(iface, IFEV_UP_FAILED);
  258. break;
  259. default:
  260. break;
  261. }
  262. interface_ip_set_enabled(&iface->config_ip, false);
  263. interface_ip_set_enabled(&iface->proto_ip, false);
  264. interface_ip_flush(&iface->proto_ip);
  265. interface_flush_state(iface);
  266. system_flush_routes();
  267. }
  268. static inline void
  269. __set_config_state(struct interface *iface, enum interface_config_state s)
  270. {
  271. iface->config_state = s;
  272. }
  273. static void
  274. __interface_set_down(struct interface *iface, bool force)
  275. {
  276. enum interface_state state = iface->state;
  277. switch (state) {
  278. case IFS_UP:
  279. case IFS_SETUP:
  280. iface->state = IFS_TEARDOWN;
  281. if (iface->dynamic)
  282. __set_config_state(iface, IFC_REMOVE);
  283. if (state == IFS_UP)
  284. interface_event(iface, IFEV_DOWN);
  285. interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
  286. if (force)
  287. interface_flush_state(iface);
  288. break;
  289. case IFS_DOWN:
  290. if (iface->main_dev.dev)
  291. device_release(&iface->main_dev);
  292. break;
  293. case IFS_TEARDOWN:
  294. default:
  295. break;
  296. }
  297. }
  298. static int
  299. __interface_set_up(struct interface *iface)
  300. {
  301. int ret;
  302. netifd_log_message(L_NOTICE, "Interface '%s' is setting up now\n", iface->name);
  303. iface->state = IFS_SETUP;
  304. ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
  305. if (ret)
  306. mark_interface_down(iface);
  307. return ret;
  308. }
  309. static void
  310. interface_check_state(struct interface *iface)
  311. {
  312. bool link_state = iface->link_state || interface_force_link(iface);
  313. switch (iface->state) {
  314. case IFS_UP:
  315. case IFS_SETUP:
  316. if (!iface->enabled || !link_state) {
  317. iface->state = IFS_TEARDOWN;
  318. if (iface->dynamic)
  319. __set_config_state(iface, IFC_REMOVE);
  320. interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
  321. }
  322. break;
  323. case IFS_DOWN:
  324. if (!iface->available)
  325. return;
  326. if (iface->autostart && iface->enabled && link_state && !config_init)
  327. __interface_set_up(iface);
  328. break;
  329. default:
  330. break;
  331. }
  332. }
  333. static void
  334. interface_set_enabled(struct interface *iface, bool new_state)
  335. {
  336. if (iface->enabled == new_state)
  337. return;
  338. netifd_log_message(L_NOTICE, "Interface '%s' is %s\n", iface->name, new_state ? "enabled" : "disabled");
  339. iface->enabled = new_state;
  340. interface_check_state(iface);
  341. }
  342. static void
  343. interface_set_link_state(struct interface *iface, bool new_state)
  344. {
  345. if (iface->link_state == new_state)
  346. return;
  347. netifd_log_message(L_NOTICE, "Interface '%s' has link connectivity %s\n", iface->name, new_state ? "" : "loss");
  348. iface->link_state = new_state;
  349. interface_check_state(iface);
  350. if (new_state && interface_force_link(iface) &&
  351. iface->state == IFS_UP && !iface->link_up_event) {
  352. interface_event(iface, IFEV_LINK_UP);
  353. iface->link_up_event = true;
  354. }
  355. }
  356. static void
  357. interface_ext_dev_cb(struct device_user *dep, enum device_event ev)
  358. {
  359. if (ev == DEV_EVENT_REMOVE)
  360. device_remove_user(dep);
  361. }
  362. static void
  363. interface_main_dev_cb(struct device_user *dep, enum device_event ev)
  364. {
  365. struct interface *iface;
  366. iface = container_of(dep, struct interface, main_dev);
  367. switch (ev) {
  368. case DEV_EVENT_ADD:
  369. interface_set_available(iface, true);
  370. break;
  371. case DEV_EVENT_REMOVE:
  372. interface_set_available(iface, false);
  373. if (dep->dev && dep->dev->external)
  374. interface_set_main_dev(iface, NULL);
  375. break;
  376. case DEV_EVENT_UP:
  377. interface_set_enabled(iface, true);
  378. break;
  379. case DEV_EVENT_DOWN:
  380. interface_set_enabled(iface, false);
  381. break;
  382. case DEV_EVENT_AUTH_UP:
  383. case DEV_EVENT_LINK_UP:
  384. case DEV_EVENT_LINK_DOWN:
  385. interface_set_link_state(iface, device_link_active(dep->dev));
  386. break;
  387. case DEV_EVENT_TOPO_CHANGE:
  388. interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
  389. return;
  390. default:
  391. break;
  392. }
  393. }
  394. static void
  395. interface_l3_dev_cb(struct device_user *dep, enum device_event ev)
  396. {
  397. struct interface *iface;
  398. iface = container_of(dep, struct interface, l3_dev);
  399. if (iface->l3_dev.dev == iface->main_dev.dev)
  400. return;
  401. switch (ev) {
  402. case DEV_EVENT_LINK_DOWN:
  403. if (iface->proto_handler->flags & PROTO_FLAG_TEARDOWN_ON_L3_LINK_DOWN)
  404. interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, false);
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. void
  411. interface_set_available(struct interface *iface, bool new_state)
  412. {
  413. if (iface->available == new_state)
  414. return;
  415. D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
  416. iface->available = new_state;
  417. if (new_state) {
  418. if (iface->autostart && !config_init)
  419. interface_set_up(iface);
  420. } else
  421. __interface_set_down(iface, true);
  422. }
  423. void
  424. interface_add_user(struct interface_user *dep, struct interface *iface)
  425. {
  426. if (!iface) {
  427. list_add(&dep->list, &iface_all_users);
  428. return;
  429. }
  430. dep->iface = iface;
  431. list_add(&dep->list, &iface->users);
  432. if (iface->state == IFS_UP)
  433. dep->cb(dep, iface, IFEV_UP);
  434. }
  435. void
  436. interface_remove_user(struct interface_user *dep)
  437. {
  438. list_del_init(&dep->list);
  439. dep->iface = NULL;
  440. }
  441. static void
  442. interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
  443. {
  444. struct blob_attr *cur;
  445. size_t rem;
  446. blobmsg_for_each_attr(cur, list, rem) {
  447. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  448. continue;
  449. if (!blobmsg_check_attr(cur, false))
  450. continue;
  451. struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
  452. memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
  453. list_add(&c->head, &iface->assignment_classes);
  454. }
  455. }
  456. static void
  457. interface_clear_assignment_classes(struct interface *iface)
  458. {
  459. while (!list_empty(&iface->assignment_classes)) {
  460. struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
  461. struct interface_assignment_class, head);
  462. list_del(&c->head);
  463. free(c);
  464. }
  465. }
  466. static void
  467. interface_merge_assignment_data(struct interface *old, struct interface *new)
  468. {
  469. bool changed = (old->assignment_hint != new->assignment_hint ||
  470. old->assignment_length != new->assignment_length ||
  471. old->assignment_iface_id_selection != new->assignment_iface_id_selection ||
  472. old->assignment_weight != new->assignment_weight ||
  473. (old->assignment_iface_id_selection == IFID_FIXED &&
  474. memcmp(&old->assignment_fixed_iface_id, &new->assignment_fixed_iface_id,
  475. sizeof(old->assignment_fixed_iface_id))) ||
  476. list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
  477. struct interface_assignment_class *c;
  478. list_for_each_entry(c, &new->assignment_classes, head) {
  479. /* Compare list entries one-by-one to see if there was a change */
  480. if (list_empty(&old->assignment_classes)) /* The new list is longer */
  481. changed = true;
  482. if (changed)
  483. break;
  484. struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
  485. struct interface_assignment_class, head);
  486. if (strcmp(c_old->name, c->name)) /* An entry didn't match */
  487. break;
  488. list_del(&c_old->head);
  489. free(c_old);
  490. }
  491. /* The old list was longer than the new one or the last entry didn't match */
  492. if (!list_empty(&old->assignment_classes)) {
  493. interface_clear_assignment_classes(old);
  494. changed = true;
  495. }
  496. list_splice_init(&new->assignment_classes, &old->assignment_classes);
  497. if (changed) {
  498. old->assignment_hint = new->assignment_hint;
  499. old->assignment_length = new->assignment_length;
  500. old->assignment_iface_id_selection = new->assignment_iface_id_selection;
  501. old->assignment_fixed_iface_id = new->assignment_fixed_iface_id;
  502. old->assignment_weight = new->assignment_weight;
  503. interface_refresh_assignments(true);
  504. }
  505. }
  506. static void
  507. interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  508. {
  509. struct interface *alias = container_of(dep, struct interface, parent_iface);
  510. struct device *dev = iface->l3_dev.dev;
  511. switch (ev) {
  512. case IFEV_UP:
  513. if (!dev)
  514. return;
  515. interface_set_main_dev(alias, dev);
  516. interface_set_available(alias, true);
  517. break;
  518. case IFEV_DOWN:
  519. case IFEV_UP_FAILED:
  520. interface_set_available(alias, false);
  521. interface_set_main_dev(alias, NULL);
  522. break;
  523. case IFEV_FREE:
  524. interface_remove_user(dep);
  525. break;
  526. default:
  527. break;
  528. }
  529. }
  530. static void
  531. interface_set_device_config(struct interface *iface, struct device *dev)
  532. {
  533. if (!dev || !dev->default_config)
  534. return;
  535. if (!iface->device_config &&
  536. (!dev->iface_config || dev->config_iface != iface))
  537. return;
  538. dev->config_iface = iface;
  539. dev->iface_config = iface->device_config;
  540. device_apply_config(dev, dev->type, iface->config);
  541. }
  542. static void
  543. interface_claim_device(struct interface *iface)
  544. {
  545. struct interface *parent;
  546. struct device *dev = NULL;
  547. if (iface->parent_iface.iface)
  548. interface_remove_user(&iface->parent_iface);
  549. if (iface->parent_ifname) {
  550. parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
  551. iface->parent_iface.cb = interface_alias_cb;
  552. interface_add_user(&iface->parent_iface, parent);
  553. } else if (iface->device &&
  554. !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
  555. dev = device_get(iface->device, true);
  556. interface_set_device_config(iface, dev);
  557. } else {
  558. dev = iface->ext_dev.dev;
  559. }
  560. if (dev)
  561. interface_set_main_dev(iface, dev);
  562. if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
  563. interface_set_available(iface, true);
  564. }
  565. static void
  566. interface_cleanup_state(struct interface *iface)
  567. {
  568. interface_set_available(iface, false);
  569. interface_flush_state(iface);
  570. interface_clear_errors(iface);
  571. interface_set_proto_state(iface, NULL);
  572. interface_set_main_dev(iface, NULL);
  573. interface_set_l3_dev(iface, NULL);
  574. }
  575. static void
  576. interface_cleanup(struct interface *iface)
  577. {
  578. struct interface_user *dep, *tmp;
  579. uloop_timeout_cancel(&iface->remove_timer);
  580. device_remove_user(&iface->ext_dev);
  581. if (iface->parent_iface.iface)
  582. interface_remove_user(&iface->parent_iface);
  583. list_for_each_entry_safe(dep, tmp, &iface->users, list)
  584. interface_remove_user(dep);
  585. interface_clear_assignment_classes(iface);
  586. interface_ip_flush(&iface->config_ip);
  587. interface_cleanup_state(iface);
  588. }
  589. static void
  590. interface_do_free(struct interface *iface)
  591. {
  592. interface_event(iface, IFEV_FREE);
  593. interface_cleanup(iface);
  594. free(iface->config);
  595. netifd_ubus_remove_interface(iface);
  596. avl_delete(&interfaces.avl, &iface->node.avl);
  597. if (iface->jail)
  598. free(iface->jail);
  599. if (iface->jail_device)
  600. free(iface->jail_device);
  601. if (iface->host_device)
  602. free(iface->host_device);
  603. free(iface);
  604. }
  605. static void
  606. interface_do_reload(struct interface *iface)
  607. {
  608. interface_event(iface, IFEV_RELOAD);
  609. interface_cleanup_state(iface);
  610. proto_init_interface(iface, iface->config);
  611. interface_claim_device(iface);
  612. }
  613. static void
  614. interface_handle_config_change(struct interface *iface)
  615. {
  616. enum interface_config_state state = iface->config_state;
  617. iface->config_state = IFC_NORMAL;
  618. switch(state) {
  619. case IFC_NORMAL:
  620. break;
  621. case IFC_RELOAD:
  622. interface_do_reload(iface);
  623. break;
  624. case IFC_REMOVE:
  625. interface_do_free(iface);
  626. return;
  627. }
  628. if (iface->autostart)
  629. interface_set_up(iface);
  630. }
  631. static void
  632. interface_proto_event_cb(struct interface_proto_state *state, enum interface_proto_event ev)
  633. {
  634. struct interface *iface = state->iface;
  635. switch (ev) {
  636. case IFPEV_UP:
  637. if (iface->state != IFS_SETUP) {
  638. if (iface->state == IFS_UP && iface->updated)
  639. interface_event(iface, IFEV_UPDATE);
  640. return;
  641. }
  642. if (!iface->l3_dev.dev)
  643. interface_set_l3_dev(iface, iface->main_dev.dev);
  644. interface_ip_set_enabled(&iface->config_ip, true);
  645. interface_ip_set_enabled(&iface->proto_ip, true);
  646. system_flush_routes();
  647. iface->state = IFS_UP;
  648. iface->start_time = system_get_rtime();
  649. interface_event(iface, IFEV_UP);
  650. netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
  651. break;
  652. case IFPEV_DOWN:
  653. if (iface->state == IFS_DOWN)
  654. return;
  655. netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
  656. mark_interface_down(iface);
  657. interface_write_resolv_conf(iface->jail);
  658. if (iface->main_dev.dev)
  659. device_release(&iface->main_dev);
  660. if (iface->l3_dev.dev)
  661. device_remove_user(&iface->l3_dev);
  662. interface_handle_config_change(iface);
  663. return;
  664. case IFPEV_LINK_LOST:
  665. if (iface->state != IFS_UP)
  666. return;
  667. netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
  668. mark_interface_down(iface);
  669. iface->state = IFS_SETUP;
  670. break;
  671. default:
  672. return;
  673. }
  674. interface_write_resolv_conf(iface->jail);
  675. }
  676. void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
  677. {
  678. if (iface->proto) {
  679. iface->proto->free(iface->proto);
  680. iface->proto = NULL;
  681. }
  682. iface->state = IFS_DOWN;
  683. iface->proto = state;
  684. if (!state)
  685. return;
  686. state->proto_event = interface_proto_event_cb;
  687. state->iface = iface;
  688. }
  689. struct interface *
  690. interface_alloc(const char *name, struct blob_attr *config, bool dynamic)
  691. {
  692. struct interface *iface;
  693. struct blob_attr *tb[IFACE_ATTR_MAX];
  694. struct blob_attr *cur;
  695. const char *proto_name = NULL;
  696. char *iface_name;
  697. bool force_link = false;
  698. iface = calloc_a(sizeof(*iface), &iface_name, strlen(name) + 1);
  699. iface->name = strcpy(iface_name, name);
  700. INIT_LIST_HEAD(&iface->errors);
  701. INIT_LIST_HEAD(&iface->users);
  702. INIT_LIST_HEAD(&iface->hotplug_list);
  703. INIT_LIST_HEAD(&iface->assignment_classes);
  704. interface_ip_init(iface);
  705. avl_init(&iface->data, avl_strcmp, false, NULL);
  706. iface->config_ip.enabled = false;
  707. iface->main_dev.cb = interface_main_dev_cb;
  708. iface->l3_dev.cb = interface_l3_dev_cb;
  709. iface->ext_dev.cb = interface_ext_dev_cb;
  710. blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
  711. blob_data(config), blob_len(config));
  712. iface->zone = NULL;
  713. if ((cur = tb[IFACE_ATTR_ZONE]))
  714. iface->zone = strdup(blobmsg_get_string(cur));
  715. if ((cur = tb[IFACE_ATTR_PROTO]))
  716. proto_name = blobmsg_data(cur);
  717. proto_attach_interface(iface, proto_name);
  718. if (iface->proto_handler->flags & PROTO_FLAG_FORCE_LINK_DEFAULT)
  719. force_link = true;
  720. iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
  721. iface->force_link = blobmsg_get_bool_default(tb[IFACE_ATTR_FORCE_LINK], force_link);
  722. iface->dynamic = dynamic;
  723. iface->proto_ip.no_defaultroute =
  724. !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
  725. iface->proto_ip.no_dns =
  726. !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
  727. if ((cur = tb[IFACE_ATTR_DNS]))
  728. interface_add_dns_server_list(&iface->config_ip, cur);
  729. if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
  730. interface_add_dns_search_list(&iface->config_ip, cur);
  731. if ((cur = tb[IFACE_ATTR_DNS_METRIC]))
  732. iface->dns_metric = blobmsg_get_u32(cur);
  733. if ((cur = tb[IFACE_ATTR_METRIC]))
  734. iface->metric = blobmsg_get_u32(cur);
  735. if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
  736. iface->assignment_length = blobmsg_get_u32(cur);
  737. /* defaults */
  738. iface->assignment_iface_id_selection = IFID_FIXED;
  739. iface->assignment_fixed_iface_id = in6addr_any;
  740. iface->assignment_fixed_iface_id.s6_addr[15] = 1;
  741. if ((cur = tb[IFACE_ATTR_IP6IFACEID])) {
  742. const char *ifaceid = blobmsg_data(cur);
  743. if (!strcmp(ifaceid, "random")) {
  744. iface->assignment_iface_id_selection = IFID_RANDOM;
  745. }
  746. else if (!strcmp(ifaceid, "eui64")) {
  747. iface->assignment_iface_id_selection = IFID_EUI64;
  748. }
  749. else {
  750. /* we expect an IPv6 address with network id zero here -> fixed iface id
  751. if we cannot parse -> revert to iface id 1 */
  752. if (inet_pton(AF_INET6,ifaceid,&iface->assignment_fixed_iface_id) != 1 ||
  753. iface->assignment_fixed_iface_id.s6_addr32[0] != 0 ||
  754. iface->assignment_fixed_iface_id.s6_addr32[1] != 0) {
  755. iface->assignment_fixed_iface_id = in6addr_any;
  756. iface->assignment_fixed_iface_id.s6_addr[15] = 1;
  757. netifd_log_message(L_WARNING, "Failed to parse ip6ifaceid for interface '%s', \
  758. falling back to iface id 1.\n", iface->name);
  759. }
  760. }
  761. }
  762. iface->assignment_hint = -1;
  763. if ((cur = tb[IFACE_ATTR_IP6HINT]))
  764. iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
  765. ~((1 << (64 - iface->assignment_length)) - 1);
  766. if ((cur = tb[IFACE_ATTR_IP6CLASS]))
  767. interface_add_assignment_classes(iface, cur);
  768. if ((cur = tb[IFACE_ATTR_IP6WEIGHT]))
  769. iface->assignment_weight = blobmsg_get_u32(cur);
  770. if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
  771. if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
  772. DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
  773. }
  774. if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
  775. if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
  776. DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
  777. }
  778. iface->proto_ip.no_delegation = !blobmsg_get_bool_default(tb[IFACE_ATTR_DELEGATE], true);
  779. iface->config_autostart = iface->autostart;
  780. iface->jail = NULL;
  781. if ((cur = tb[IFACE_ATTR_JAIL])) {
  782. iface->jail = strdup(blobmsg_get_string(cur));
  783. iface->autostart = false;
  784. }
  785. iface->jail_device = NULL;
  786. if ((cur = tb[IFACE_ATTR_JAIL_DEVICE]))
  787. iface->jail_device = strdup(blobmsg_get_string(cur));
  788. else if ((cur = tb[IFACE_ATTR_JAIL_IFNAME]))
  789. iface->jail_device = strdup(blobmsg_get_string(cur));
  790. iface->host_device = NULL;
  791. if ((cur = tb[IFACE_ATTR_HOST_DEVICE]))
  792. iface->host_device = strdup(blobmsg_get_string(cur));
  793. return iface;
  794. }
  795. static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
  796. {
  797. struct blob_attr *tb[IFACE_ATTR_MAX];
  798. struct blob_attr *cur;
  799. char *name = NULL;
  800. blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
  801. blob_data(config), blob_len(config));
  802. if (alias) {
  803. if ((cur = tb[IFACE_ATTR_INTERFACE]))
  804. iface->parent_ifname = blobmsg_data(cur);
  805. if (!iface->parent_ifname)
  806. return false;
  807. } else {
  808. cur = tb[IFACE_ATTR_DEVICE];
  809. if (!cur)
  810. cur = tb[IFACE_ATTR_IFNAME];
  811. if (cur)
  812. iface->device = blobmsg_data(cur);
  813. }
  814. if (iface->dynamic) {
  815. name = strdup(iface->name);
  816. if (!name)
  817. return false;
  818. }
  819. iface->config = config;
  820. vlist_add(&interfaces, &iface->node, iface->name);
  821. if (name) {
  822. iface = vlist_find(&interfaces, name, iface, node);
  823. free(name);
  824. /* Don't delete dynamic interface on reload */
  825. if (iface)
  826. iface->node.version = -1;
  827. }
  828. return true;
  829. }
  830. bool
  831. interface_add(struct interface *iface, struct blob_attr *config)
  832. {
  833. return __interface_add(iface, config, false);
  834. }
  835. bool
  836. interface_add_alias(struct interface *iface, struct blob_attr *config)
  837. {
  838. if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
  839. return false;
  840. return __interface_add(iface, config, true);
  841. }
  842. void
  843. interface_set_l3_dev(struct interface *iface, struct device *dev)
  844. {
  845. bool enabled = iface->config_ip.enabled;
  846. bool claimed = iface->l3_dev.claimed;
  847. if (iface->l3_dev.dev == dev)
  848. return;
  849. interface_ip_set_enabled(&iface->config_ip, false);
  850. interface_ip_set_enabled(&iface->proto_ip, false);
  851. interface_ip_flush(&iface->proto_ip);
  852. device_add_user(&iface->l3_dev, dev);
  853. if (dev) {
  854. if (claimed) {
  855. if (device_claim(&iface->l3_dev) < 0)
  856. return;
  857. }
  858. interface_ip_set_enabled(&iface->config_ip, enabled);
  859. interface_ip_set_enabled(&iface->proto_ip, enabled);
  860. }
  861. }
  862. static void
  863. interface_set_main_dev(struct interface *iface, struct device *dev)
  864. {
  865. bool claimed = iface->l3_dev.claimed;
  866. if (iface->main_dev.dev == dev)
  867. return;
  868. interface_set_available(iface, false);
  869. device_add_user(&iface->main_dev, dev);
  870. if (!dev) {
  871. interface_set_link_state(iface, false);
  872. return;
  873. }
  874. if (claimed) {
  875. if (device_claim(&iface->l3_dev) < 0)
  876. return;
  877. }
  878. if (!iface->l3_dev.dev)
  879. interface_set_l3_dev(iface, dev);
  880. }
  881. static int
  882. interface_remove_link(struct interface *iface, struct device *dev,
  883. struct blob_attr *vlan)
  884. {
  885. struct device *mdev = iface->main_dev.dev;
  886. if (mdev && mdev->hotplug_ops)
  887. return mdev->hotplug_ops->del(mdev, dev, vlan);
  888. if (dev == iface->ext_dev.dev)
  889. device_remove_user(&iface->ext_dev);
  890. if (!iface->main_dev.hotplug)
  891. return UBUS_STATUS_INVALID_ARGUMENT;
  892. if (dev != iface->main_dev.dev)
  893. return UBUS_STATUS_INVALID_ARGUMENT;
  894. interface_set_main_dev(iface, NULL);
  895. return 0;
  896. }
  897. static int
  898. interface_add_link(struct interface *iface, struct device *dev,
  899. struct blob_attr *vlan, bool link_ext)
  900. {
  901. struct device *mdev = iface->main_dev.dev;
  902. if (mdev == dev)
  903. return 0;
  904. if (iface->main_dev.hotplug)
  905. device_remove_user(&iface->main_dev);
  906. if (mdev) {
  907. if (mdev->hotplug_ops)
  908. return mdev->hotplug_ops->add(mdev, dev, vlan);
  909. else
  910. return UBUS_STATUS_NOT_SUPPORTED;
  911. }
  912. if (link_ext)
  913. device_add_user(&iface->ext_dev, dev);
  914. interface_set_main_dev(iface, dev);
  915. iface->main_dev.hotplug = true;
  916. return 0;
  917. }
  918. int
  919. interface_handle_link(struct interface *iface, const char *name,
  920. struct blob_attr *vlan, bool add, bool link_ext)
  921. {
  922. struct device *dev;
  923. dev = device_get(name, add ? (link_ext ? 2 : 1) : 0);
  924. if (!dev)
  925. return UBUS_STATUS_NOT_FOUND;
  926. if (!add)
  927. return interface_remove_link(iface, dev, vlan);
  928. interface_set_device_config(iface, dev);
  929. if (!link_ext)
  930. device_set_present(dev, true);
  931. return interface_add_link(iface, dev, vlan, link_ext);
  932. }
  933. void
  934. interface_set_up(struct interface *iface)
  935. {
  936. int ret;
  937. const char *error = NULL;
  938. iface->autostart = true;
  939. if (iface->state != IFS_DOWN)
  940. return;
  941. interface_clear_errors(iface);
  942. if (iface->available) {
  943. if (iface->main_dev.dev) {
  944. ret = device_claim(&iface->main_dev);
  945. if (!ret)
  946. interface_check_state(iface);
  947. else
  948. error = "DEVICE_CLAIM_FAILED";
  949. } else {
  950. ret = __interface_set_up(iface);
  951. if (ret)
  952. error = "SETUP_FAILED";
  953. }
  954. } else
  955. error = "NO_DEVICE";
  956. if (error)
  957. interface_add_error(iface, "interface", error, NULL, 0);
  958. }
  959. void
  960. interface_set_down(struct interface *iface)
  961. {
  962. if (!iface) {
  963. vlist_for_each_element(&interfaces, iface, node)
  964. __interface_set_down(iface, false);
  965. } else {
  966. iface->autostart = false;
  967. __interface_set_down(iface, false);
  968. }
  969. }
  970. int
  971. interface_renew(struct interface *iface)
  972. {
  973. if (iface->state == IFS_TEARDOWN || iface->state == IFS_DOWN)
  974. return -1;
  975. return interface_proto_event(iface->proto, PROTO_CMD_RENEW, false);
  976. }
  977. void
  978. interface_start_pending(void)
  979. {
  980. struct interface *iface;
  981. vlist_for_each_element(&interfaces, iface, node) {
  982. if (iface->autostart)
  983. interface_set_up(iface);
  984. }
  985. }
  986. void
  987. interface_start_jail(int netns_fd, const char *jail)
  988. {
  989. struct interface *iface;
  990. vlist_for_each_element(&interfaces, iface, node) {
  991. if (!iface->jail || strcmp(iface->jail, jail))
  992. continue;
  993. system_link_netns_move(iface->main_dev.dev, netns_fd, iface->jail_device);
  994. }
  995. }
  996. void
  997. interface_stop_jail(int netns_fd)
  998. {
  999. struct interface *iface;
  1000. char *orig_ifname;
  1001. vlist_for_each_element(&interfaces, iface, node) {
  1002. orig_ifname = iface->host_device;
  1003. interface_set_down(iface);
  1004. system_link_netns_move(iface->main_dev.dev, netns_fd, orig_ifname);
  1005. }
  1006. }
  1007. static void
  1008. set_config_state(struct interface *iface, enum interface_config_state s)
  1009. {
  1010. __set_config_state(iface, s);
  1011. if (iface->state == IFS_DOWN)
  1012. interface_handle_config_change(iface);
  1013. else
  1014. __interface_set_down(iface, false);
  1015. }
  1016. void
  1017. interface_update_start(struct interface *iface, const bool keep_old)
  1018. {
  1019. iface->updated = 0;
  1020. if (!keep_old)
  1021. interface_ip_update_start(&iface->proto_ip);
  1022. }
  1023. void
  1024. interface_update_complete(struct interface *iface)
  1025. {
  1026. interface_ip_update_complete(&iface->proto_ip);
  1027. }
  1028. static void
  1029. interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
  1030. {
  1031. vlist_simple_replace(&new->dns_servers, &old->dns_servers);
  1032. vlist_simple_replace(&new->dns_search, &old->dns_search);
  1033. }
  1034. static bool
  1035. interface_device_config_changed(struct interface *if_old, struct interface *if_new)
  1036. {
  1037. struct blob_attr *ntb[__DEV_ATTR_MAX];
  1038. struct blob_attr *otb[__DEV_ATTR_MAX];
  1039. struct device *dev = if_old->main_dev.dev;
  1040. unsigned long diff = 0;
  1041. BUILD_BUG_ON(sizeof(diff) < __DEV_ATTR_MAX / 8);
  1042. if (!dev)
  1043. return false;
  1044. if (if_old->device_config != if_new->device_config)
  1045. return true;
  1046. if (!if_new->device_config)
  1047. return false;
  1048. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, otb,
  1049. blob_data(if_old->config), blob_len(if_old->config));
  1050. blobmsg_parse(device_attr_list.params, __DEV_ATTR_MAX, ntb,
  1051. blob_data(if_new->config), blob_len(if_new->config));
  1052. uci_blob_diff(ntb, otb, &device_attr_list, &diff);
  1053. return diff;
  1054. }
  1055. static void
  1056. interface_change_config(struct interface *if_old, struct interface *if_new)
  1057. {
  1058. struct blob_attr *old_config = if_old->config;
  1059. bool reload = false, reload_ip = false, update_prefix_delegation = false;
  1060. #define FIELD_CHANGED_STR(field) \
  1061. ((!!if_old->field != !!if_new->field) || \
  1062. (if_old->field && \
  1063. strcmp(if_old->field, if_new->field) != 0))
  1064. if (FIELD_CHANGED_STR(parent_ifname)) {
  1065. if (if_old->parent_iface.iface)
  1066. interface_remove_user(&if_old->parent_iface);
  1067. reload = true;
  1068. }
  1069. if (!reload && interface_device_config_changed(if_old, if_new))
  1070. reload = true;
  1071. if (FIELD_CHANGED_STR(device) ||
  1072. if_old->proto_handler != if_new->proto_handler)
  1073. reload = true;
  1074. if (!if_old->proto_handler->config_params)
  1075. D(INTERFACE, "No config parameters for interface '%s'\n",
  1076. if_old->name);
  1077. else if (!uci_blob_check_equal(if_old->config, if_new->config,
  1078. if_old->proto_handler->config_params))
  1079. reload = true;
  1080. #define UPDATE(field, __var) ({ \
  1081. bool __changed = (if_old->field != if_new->field); \
  1082. if_old->field = if_new->field; \
  1083. __var |= __changed; \
  1084. })
  1085. if_old->config = if_new->config;
  1086. if (if_old->config_autostart != if_new->config_autostart) {
  1087. if (if_old->config_autostart)
  1088. reload = true;
  1089. if_old->autostart = if_new->config_autostart;
  1090. }
  1091. if_old->device_config = if_new->device_config;
  1092. if_old->config_autostart = if_new->config_autostart;
  1093. if (if_old->jail)
  1094. free(if_old->jail);
  1095. if_old->jail = if_new->jail;
  1096. if (if_old->jail)
  1097. if_old->autostart = false;
  1098. if (if_old->jail_device)
  1099. free(if_old->jail_device);
  1100. if_old->jail_device = if_new->jail_device;
  1101. if (if_old->host_device)
  1102. free(if_old->host_device);
  1103. if_old->host_device = if_new->host_device;
  1104. if_old->device = if_new->device;
  1105. if_old->parent_ifname = if_new->parent_ifname;
  1106. if_old->dynamic = if_new->dynamic;
  1107. if_old->proto_handler = if_new->proto_handler;
  1108. if_old->force_link = if_new->force_link;
  1109. if_old->dns_metric = if_new->dns_metric;
  1110. if (if_old->proto_ip.no_delegation != if_new->proto_ip.no_delegation) {
  1111. if_old->proto_ip.no_delegation = if_new->proto_ip.no_delegation;
  1112. update_prefix_delegation = true;
  1113. }
  1114. if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
  1115. interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
  1116. UPDATE(metric, reload_ip);
  1117. UPDATE(proto_ip.no_defaultroute, reload_ip);
  1118. UPDATE(ip4table, reload_ip);
  1119. UPDATE(ip6table, reload_ip);
  1120. interface_merge_assignment_data(if_old, if_new);
  1121. #undef UPDATE
  1122. if (reload) {
  1123. D(INTERFACE, "Reload interface '%s' because of config changes\n",
  1124. if_old->name);
  1125. interface_clear_errors(if_old);
  1126. set_config_state(if_old, IFC_RELOAD);
  1127. goto out;
  1128. }
  1129. if (reload_ip) {
  1130. bool config_ip_enabled = if_old->config_ip.enabled;
  1131. bool proto_ip_enabled = if_old->proto_ip.enabled;
  1132. interface_ip_set_enabled(&if_old->config_ip, false);
  1133. interface_ip_set_enabled(&if_old->proto_ip, false);
  1134. interface_ip_set_enabled(&if_old->proto_ip, proto_ip_enabled);
  1135. interface_ip_set_enabled(&if_old->config_ip, config_ip_enabled);
  1136. }
  1137. if (update_prefix_delegation)
  1138. interface_update_prefix_delegation(&if_old->proto_ip);
  1139. interface_write_resolv_conf(if_old->jail);
  1140. if (if_old->main_dev.dev)
  1141. interface_check_state(if_old);
  1142. out:
  1143. if_new->config = NULL;
  1144. interface_cleanup(if_new);
  1145. free(old_config);
  1146. free(if_new);
  1147. }
  1148. static void
  1149. interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
  1150. struct vlist_node *node_old)
  1151. {
  1152. struct interface *if_old = container_of(node_old, struct interface, node);
  1153. struct interface *if_new = container_of(node_new, struct interface, node);
  1154. if (node_old && node_new) {
  1155. D(INTERFACE, "Update interface '%s'\n", if_new->name);
  1156. interface_change_config(if_old, if_new);
  1157. } else if (node_old) {
  1158. D(INTERFACE, "Remove interface '%s'\n", if_old->name);
  1159. set_config_state(if_old, IFC_REMOVE);
  1160. } else if (node_new) {
  1161. D(INTERFACE, "Create interface '%s'\n", if_new->name);
  1162. interface_event(if_new, IFEV_CREATE);
  1163. proto_init_interface(if_new, if_new->config);
  1164. interface_claim_device(if_new);
  1165. netifd_ubus_add_interface(if_new);
  1166. }
  1167. }
  1168. static void __init
  1169. interface_init_list(void)
  1170. {
  1171. vlist_init(&interfaces, avl_strcmp, interface_update);
  1172. interfaces.keep_old = true;
  1173. interfaces.no_delete = true;
  1174. }