interface.c 36 KB

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