interface.c 35 KB

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