interface.c 37 KB

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