interface.c 37 KB

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