interface.c 36 KB

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