interface.c 36 KB

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