2
0

interface.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  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 "netifd.h"
  18. #include "device.h"
  19. #include "interface.h"
  20. #include "interface-ip.h"
  21. #include "proto.h"
  22. #include "ubus.h"
  23. #include "config.h"
  24. #include "system.h"
  25. struct vlist_tree interfaces;
  26. static LIST_HEAD(iface_all_users);
  27. static unsigned int interface_serial = 0;
  28. enum {
  29. IFACE_ATTR_IFNAME,
  30. IFACE_ATTR_PROTO,
  31. IFACE_ATTR_AUTO,
  32. IFACE_ATTR_DEFAULTROUTE,
  33. IFACE_ATTR_PEERDNS,
  34. IFACE_ATTR_DNS,
  35. IFACE_ATTR_DNS_SEARCH,
  36. IFACE_ATTR_METRIC,
  37. IFACE_ATTR_INTERFACE,
  38. IFACE_ATTR_IP6ASSIGN,
  39. IFACE_ATTR_IP6HINT,
  40. IFACE_ATTR_IP4TABLE,
  41. IFACE_ATTR_IP6TABLE,
  42. IFACE_ATTR_IP6CLASS,
  43. IFACE_ATTR_MAX
  44. };
  45. static const struct blobmsg_policy iface_attrs[IFACE_ATTR_MAX] = {
  46. [IFACE_ATTR_PROTO] = { .name = "proto", .type = BLOBMSG_TYPE_STRING },
  47. [IFACE_ATTR_IFNAME] = { .name = "ifname", .type = BLOBMSG_TYPE_STRING },
  48. [IFACE_ATTR_AUTO] = { .name = "auto", .type = BLOBMSG_TYPE_BOOL },
  49. [IFACE_ATTR_DEFAULTROUTE] = { .name = "defaultroute", .type = BLOBMSG_TYPE_BOOL },
  50. [IFACE_ATTR_PEERDNS] = { .name = "peerdns", .type = BLOBMSG_TYPE_BOOL },
  51. [IFACE_ATTR_METRIC] = { .name = "metric", .type = BLOBMSG_TYPE_INT32 },
  52. [IFACE_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
  53. [IFACE_ATTR_DNS_SEARCH] = { .name = "dns_search", .type = BLOBMSG_TYPE_ARRAY },
  54. [IFACE_ATTR_INTERFACE] = { .name = "interface", .type = BLOBMSG_TYPE_STRING },
  55. [IFACE_ATTR_IP6ASSIGN] = { .name = "ip6assign", .type = BLOBMSG_TYPE_INT32 },
  56. [IFACE_ATTR_IP6HINT] = { .name = "ip6hint", .type = BLOBMSG_TYPE_STRING },
  57. [IFACE_ATTR_IP4TABLE] = { .name = "ip4table", .type = BLOBMSG_TYPE_STRING },
  58. [IFACE_ATTR_IP6TABLE] = { .name = "ip6table", .type = BLOBMSG_TYPE_STRING },
  59. [IFACE_ATTR_IP6CLASS] = { .name = "ip6class", .type = BLOBMSG_TYPE_ARRAY },
  60. };
  61. static const struct uci_blob_param_info iface_attr_info[IFACE_ATTR_MAX] = {
  62. [IFACE_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
  63. [IFACE_ATTR_IP6CLASS] = { .type = BLOBMSG_TYPE_STRING },
  64. };
  65. const struct uci_blob_param_list interface_attr_list = {
  66. .n_params = IFACE_ATTR_MAX,
  67. .params = iface_attrs,
  68. .info = iface_attr_info,
  69. };
  70. static void
  71. interface_clear_errors(struct interface *iface)
  72. {
  73. struct interface_error *error, *tmp;
  74. list_for_each_entry_safe(error, tmp, &iface->errors, list) {
  75. list_del(&error->list);
  76. free(error);
  77. }
  78. }
  79. void interface_add_error(struct interface *iface, const char *subsystem,
  80. const char *code, const char **data, int n_data)
  81. {
  82. struct interface_error *error;
  83. int i, len = 0;
  84. int *datalen = NULL;
  85. char *dest, *d_subsys, *d_code;
  86. if (n_data) {
  87. len = n_data * sizeof(char *);
  88. datalen = alloca(len);
  89. for (i = 0; i < n_data; i++) {
  90. datalen[i] = strlen(data[i]) + 1;
  91. len += datalen[i];
  92. }
  93. }
  94. error = calloc_a(sizeof(*error) + sizeof(char *) + len,
  95. &d_subsys, subsystem ? strlen(subsystem) + 1 : 0,
  96. &d_code, code ? strlen(code) + 1 : 0);
  97. if (!error)
  98. return;
  99. list_add_tail(&error->list, &iface->errors);
  100. dest = (char *) &error->data[n_data + 1];
  101. for (i = 0; i < n_data; i++) {
  102. error->data[i] = dest;
  103. memcpy(dest, data[i], datalen[i]);
  104. dest += datalen[i];
  105. }
  106. error->data[n_data++] = NULL;
  107. if (subsystem)
  108. error->subsystem = strcpy(d_subsys, subsystem);
  109. if (code)
  110. error->code = strcpy(d_code, code);
  111. }
  112. static void
  113. interface_data_del(struct interface *iface, struct interface_data *data)
  114. {
  115. avl_delete(&iface->data, &data->node);
  116. free(data);
  117. }
  118. static void
  119. interface_data_flush(struct interface *iface)
  120. {
  121. struct interface_data *d, *tmp;
  122. avl_for_each_element_safe(&iface->data, d, node, tmp)
  123. interface_data_del(iface, d);
  124. }
  125. int
  126. interface_add_data(struct interface *iface, const struct blob_attr *data)
  127. {
  128. struct interface_data *n, *o;
  129. if (!blobmsg_check_attr(data, true))
  130. return UBUS_STATUS_INVALID_ARGUMENT;
  131. n = calloc(1, sizeof(*n) + blob_pad_len(data));
  132. memcpy(n->data, data, blob_pad_len(data));
  133. n->node.key = blobmsg_name(data);
  134. o = avl_find_element(&iface->data, n->node.key, o, node);
  135. if (o)
  136. interface_data_del(iface, o);
  137. avl_insert(&iface->data, &n->node);
  138. return 0;
  139. }
  140. static void
  141. interface_event(struct interface *iface, enum interface_event ev)
  142. {
  143. struct interface_user *dep, *tmp;
  144. struct device *adev = NULL;
  145. list_for_each_entry_safe(dep, tmp, &iface->users, list)
  146. dep->cb(dep, iface, ev);
  147. list_for_each_entry_safe(dep, tmp, &iface_all_users, list)
  148. dep->cb(dep, iface, ev);
  149. switch (ev) {
  150. case IFEV_UP:
  151. adev = iface->l3_dev.dev;
  152. /* fall through */
  153. case IFEV_DOWN:
  154. alias_notify_device(iface->name, adev);
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. static void
  161. interface_flush_state(struct interface *iface)
  162. {
  163. if (iface->l3_dev.dev)
  164. device_release(&iface->l3_dev);
  165. interface_data_flush(iface);
  166. }
  167. static void
  168. mark_interface_down(struct interface *iface)
  169. {
  170. enum interface_state state = iface->state;
  171. iface->state = IFS_DOWN;
  172. if (state == IFS_UP)
  173. interface_event(iface, IFEV_DOWN);
  174. interface_ip_set_enabled(&iface->config_ip, false);
  175. interface_ip_flush(&iface->proto_ip);
  176. interface_flush_state(iface);
  177. system_flush_routes();
  178. }
  179. void
  180. __interface_set_down(struct interface *iface, bool force)
  181. {
  182. if (iface->state == IFS_DOWN ||
  183. iface->state == IFS_TEARDOWN)
  184. return;
  185. if (iface->state == IFS_UP)
  186. interface_event(iface, IFEV_DOWN);
  187. iface->state = IFS_TEARDOWN;
  188. interface_proto_event(iface->proto, PROTO_CMD_TEARDOWN, force);
  189. if (force)
  190. interface_flush_state(iface);
  191. }
  192. static void
  193. interface_cb(struct device_user *dep, enum device_event ev)
  194. {
  195. struct interface *iface;
  196. bool new_state;
  197. iface = container_of(dep, struct interface, main_dev);
  198. switch (ev) {
  199. case DEV_EVENT_ADD:
  200. new_state = true;
  201. break;
  202. case DEV_EVENT_REMOVE:
  203. new_state = false;
  204. break;
  205. default:
  206. return;
  207. }
  208. interface_set_available(iface, new_state);
  209. if (!new_state && dep->dev->external)
  210. interface_set_main_dev(iface, NULL);
  211. }
  212. void
  213. interface_set_available(struct interface *iface, bool new_state)
  214. {
  215. if (iface->available == new_state)
  216. return;
  217. D(INTERFACE, "Interface '%s', available=%d\n", iface->name, new_state);
  218. iface->available = new_state;
  219. if (new_state) {
  220. if (iface->autostart && !config_init)
  221. interface_set_up(iface);
  222. } else
  223. __interface_set_down(iface, true);
  224. }
  225. void
  226. interface_add_user(struct interface_user *dep, struct interface *iface)
  227. {
  228. if (!iface) {
  229. list_add(&dep->list, &iface_all_users);
  230. return;
  231. }
  232. dep->iface = iface;
  233. list_add(&dep->list, &iface->users);
  234. if (iface->state == IFS_UP)
  235. dep->cb(dep, iface, IFEV_UP);
  236. }
  237. void
  238. interface_remove_user(struct interface_user *dep)
  239. {
  240. list_del_init(&dep->list);
  241. dep->iface = NULL;
  242. }
  243. static void
  244. interface_add_assignment_classes(struct interface *iface, struct blob_attr *list)
  245. {
  246. struct blob_attr *cur;
  247. int rem;
  248. blobmsg_for_each_attr(cur, list, rem) {
  249. if (blobmsg_type(cur) != BLOBMSG_TYPE_STRING)
  250. continue;
  251. if (!blobmsg_check_attr(cur, NULL))
  252. continue;
  253. struct interface_assignment_class *c = malloc(sizeof(*c) + blobmsg_data_len(cur));
  254. memcpy(c->name, blobmsg_data(cur), blobmsg_data_len(cur));
  255. list_add(&c->head, &iface->assignment_classes);
  256. }
  257. }
  258. static void
  259. interface_clear_assignment_classes(struct interface *iface)
  260. {
  261. while (!list_empty(&iface->assignment_classes)) {
  262. struct interface_assignment_class *c = list_first_entry(&iface->assignment_classes,
  263. struct interface_assignment_class, head);
  264. list_del(&c->head);
  265. free(c);
  266. }
  267. }
  268. static void
  269. interface_merge_assignment_data(struct interface *old, struct interface *new)
  270. {
  271. bool changed = (old->assignment_hint != new->assignment_hint ||
  272. old->assignment_length != new->assignment_length ||
  273. list_empty(&old->assignment_classes) != list_empty(&new->assignment_classes));
  274. struct interface_assignment_class *c;
  275. list_for_each_entry(c, &new->assignment_classes, head) {
  276. // Compare list entries one-by-one to see if there was a change
  277. if (list_empty(&old->assignment_classes)) // The new list is longer
  278. changed = true;
  279. if (changed)
  280. break;
  281. struct interface_assignment_class *c_old = list_first_entry(&old->assignment_classes,
  282. struct interface_assignment_class, head);
  283. if (strcmp(c_old->name, c->name)) // An entry didn't match
  284. break;
  285. list_del(&c_old->head);
  286. free(c_old);
  287. }
  288. // The old list was longer than the new one or the last entry didn't match
  289. if (!list_empty(&old->assignment_classes)) {
  290. interface_clear_assignment_classes(old);
  291. changed = true;
  292. }
  293. list_splice_init(&new->assignment_classes, &old->assignment_classes);
  294. if (changed) {
  295. old->assignment_hint = new->assignment_hint;
  296. old->assignment_length = new->assignment_length;
  297. interface_refresh_assignments(true);
  298. }
  299. }
  300. static void
  301. interface_alias_cb(struct interface_user *dep, struct interface *iface, enum interface_event ev)
  302. {
  303. struct interface *alias = container_of(dep, struct interface, parent_iface);
  304. struct device *dev = iface->l3_dev.dev;
  305. switch (ev) {
  306. case IFEV_UP:
  307. if (!dev)
  308. return;
  309. interface_set_main_dev(alias, dev);
  310. interface_set_available(alias, true);
  311. break;
  312. case IFEV_DOWN:
  313. interface_set_available(alias, false);
  314. interface_set_main_dev(alias, NULL);
  315. break;
  316. case IFEV_FREE:
  317. interface_remove_user(dep);
  318. break;
  319. case IFEV_RELOAD:
  320. break;
  321. }
  322. }
  323. static void
  324. interface_claim_device(struct interface *iface)
  325. {
  326. struct interface *parent;
  327. struct device *dev = NULL;
  328. if (iface->parent_iface.iface)
  329. interface_remove_user(&iface->parent_iface);
  330. if (iface->parent_ifname) {
  331. parent = vlist_find(&interfaces, iface->parent_ifname, parent, node);
  332. iface->parent_iface.cb = interface_alias_cb;
  333. interface_add_user(&iface->parent_iface, parent);
  334. } else if (iface->ifname &&
  335. !(iface->proto_handler->flags & PROTO_FLAG_NODEV)) {
  336. dev = device_get(iface->ifname, true);
  337. }
  338. if (dev)
  339. interface_set_main_dev(iface, dev);
  340. if (iface->proto_handler->flags & PROTO_FLAG_INIT_AVAILABLE)
  341. interface_set_available(iface, true);
  342. }
  343. static void
  344. interface_cleanup_state(struct interface *iface)
  345. {
  346. interface_set_available(iface, false);
  347. interface_flush_state(iface);
  348. interface_clear_errors(iface);
  349. interface_set_proto_state(iface, NULL);
  350. if (iface->main_dev.dev)
  351. interface_set_main_dev(iface, NULL);
  352. }
  353. static void
  354. interface_cleanup(struct interface *iface)
  355. {
  356. struct interface_user *dep, *tmp;
  357. if (iface->parent_iface.iface)
  358. interface_remove_user(&iface->parent_iface);
  359. list_for_each_entry_safe(dep, tmp, &iface->users, list)
  360. interface_remove_user(dep);
  361. interface_clear_assignment_classes(iface);
  362. interface_ip_flush(&iface->config_ip);
  363. interface_cleanup_state(iface);
  364. }
  365. static void
  366. interface_do_free(struct interface *iface)
  367. {
  368. interface_event(iface, IFEV_FREE);
  369. interface_cleanup(iface);
  370. free(iface->config);
  371. netifd_ubus_remove_interface(iface);
  372. avl_delete(&interfaces.avl, &iface->node.avl);
  373. free(iface);
  374. }
  375. static void
  376. interface_do_reload(struct interface *iface)
  377. {
  378. interface_event(iface, IFEV_RELOAD);
  379. interface_cleanup_state(iface);
  380. proto_init_interface(iface, iface->config);
  381. interface_claim_device(iface);
  382. }
  383. static void
  384. interface_handle_config_change(struct interface *iface)
  385. {
  386. enum interface_config_state state = iface->config_state;
  387. iface->config_state = IFC_NORMAL;
  388. switch(state) {
  389. case IFC_NORMAL:
  390. break;
  391. case IFC_RELOAD:
  392. interface_do_reload(iface);
  393. break;
  394. case IFC_REMOVE:
  395. interface_do_free(iface);
  396. return;
  397. }
  398. if (iface->autostart && iface->available)
  399. interface_set_up(iface);
  400. }
  401. static void
  402. interface_proto_cb(struct interface_proto_state *state, enum interface_proto_event ev)
  403. {
  404. struct interface *iface = state->iface;
  405. switch (ev) {
  406. case IFPEV_UP:
  407. if (iface->state != IFS_SETUP)
  408. return;
  409. interface_ip_set_enabled(&iface->config_ip, true);
  410. system_flush_routes();
  411. iface->state = IFS_UP;
  412. iface->start_time = system_get_rtime();
  413. interface_event(iface, IFEV_UP);
  414. netifd_log_message(L_NOTICE, "Interface '%s' is now up\n", iface->name);
  415. break;
  416. case IFPEV_DOWN:
  417. if (iface->state == IFS_DOWN)
  418. return;
  419. netifd_log_message(L_NOTICE, "Interface '%s' is now down\n", iface->name);
  420. mark_interface_down(iface);
  421. if (iface->main_dev.dev)
  422. device_release(&iface->main_dev);
  423. interface_handle_config_change(iface);
  424. break;
  425. case IFPEV_LINK_LOST:
  426. if (iface->state != IFS_UP)
  427. return;
  428. netifd_log_message(L_NOTICE, "Interface '%s' has lost the connection\n", iface->name);
  429. mark_interface_down(iface);
  430. iface->state = IFS_SETUP;
  431. break;
  432. }
  433. interface_write_resolv_conf();
  434. }
  435. void interface_set_proto_state(struct interface *iface, struct interface_proto_state *state)
  436. {
  437. if (iface->proto) {
  438. iface->proto->free(iface->proto);
  439. iface->proto = NULL;
  440. }
  441. iface->state = IFS_DOWN;
  442. iface->proto = state;
  443. if (!state)
  444. return;
  445. state->proto_event = interface_proto_cb;
  446. state->iface = iface;
  447. }
  448. void
  449. interface_init(struct interface *iface, const char *name,
  450. struct blob_attr *config)
  451. {
  452. struct blob_attr *tb[IFACE_ATTR_MAX];
  453. struct blob_attr *cur;
  454. const char *proto_name = NULL;
  455. strncpy(iface->name, name, sizeof(iface->name) - 1);
  456. INIT_LIST_HEAD(&iface->errors);
  457. INIT_LIST_HEAD(&iface->users);
  458. INIT_LIST_HEAD(&iface->hotplug_list);
  459. INIT_LIST_HEAD(&iface->assignment_classes);
  460. interface_ip_init(iface);
  461. avl_init(&iface->data, avl_strcmp, false, NULL);
  462. iface->config_ip.enabled = false;
  463. iface->main_dev.cb = interface_cb;
  464. blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
  465. blob_data(config), blob_len(config));
  466. if ((cur = tb[IFACE_ATTR_PROTO]))
  467. proto_name = blobmsg_data(cur);
  468. proto_attach_interface(iface, proto_name);
  469. iface->autostart = blobmsg_get_bool_default(tb[IFACE_ATTR_AUTO], true);
  470. iface->proto_ip.no_defaultroute =
  471. !blobmsg_get_bool_default(tb[IFACE_ATTR_DEFAULTROUTE], true);
  472. iface->proto_ip.no_dns =
  473. !blobmsg_get_bool_default(tb[IFACE_ATTR_PEERDNS], true);
  474. if ((cur = tb[IFACE_ATTR_DNS]))
  475. interface_add_dns_server_list(&iface->config_ip, cur);
  476. if ((cur = tb[IFACE_ATTR_DNS_SEARCH]))
  477. interface_add_dns_search_list(&iface->config_ip, cur);
  478. if ((cur = tb[IFACE_ATTR_METRIC]))
  479. iface->metric = blobmsg_get_u32(cur);
  480. if ((cur = tb[IFACE_ATTR_IP6ASSIGN]))
  481. iface->assignment_length = blobmsg_get_u32(cur);
  482. iface->assignment_hint = -1;
  483. if ((cur = tb[IFACE_ATTR_IP6HINT]))
  484. iface->assignment_hint = strtol(blobmsg_get_string(cur), NULL, 16) &
  485. ~((1 << (64 - iface->assignment_length)) - 1);
  486. if ((cur = tb[IFACE_ATTR_IP6CLASS]))
  487. interface_add_assignment_classes(iface, cur);
  488. if ((cur = tb[IFACE_ATTR_IP4TABLE])) {
  489. if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip4table))
  490. DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
  491. }
  492. // Set a default exteranl routing table for IPv6 to do source-based-filtering
  493. struct interface *iface_old = vlist_find(&interfaces, name, iface_old, node);
  494. if (iface_old && iface_old->ip6table > 1000 && iface_old->ip6table < 2000)
  495. iface->ip6table = iface_old->ip6table;
  496. else
  497. iface->ip6table = 1000 + ++interface_serial;
  498. if ((cur = tb[IFACE_ATTR_IP6TABLE])) {
  499. if (!system_resolve_rt_table(blobmsg_data(cur), &iface->ip6table))
  500. DPRINTF("Failed to resolve routing table: %s\n", (char *) blobmsg_data(cur));
  501. }
  502. iface->config_autostart = iface->autostart;
  503. }
  504. static bool __interface_add(struct interface *iface, struct blob_attr *config, bool alias)
  505. {
  506. struct blob_attr *tb[IFACE_ATTR_MAX];
  507. struct blob_attr *cur;
  508. blobmsg_parse(iface_attrs, IFACE_ATTR_MAX, tb,
  509. blob_data(config), blob_len(config));
  510. if (alias) {
  511. if ((cur = tb[IFACE_ATTR_INTERFACE]))
  512. iface->parent_ifname = blobmsg_data(cur);
  513. if (!iface->parent_ifname)
  514. return false;
  515. } else {
  516. if ((cur = tb[IFACE_ATTR_IFNAME]))
  517. iface->ifname = blobmsg_data(cur);
  518. }
  519. iface->config = config;
  520. vlist_add(&interfaces, &iface->node, iface->name);
  521. return true;
  522. }
  523. void
  524. interface_add(struct interface *iface, struct blob_attr *config)
  525. {
  526. __interface_add(iface, config, false);
  527. }
  528. bool
  529. interface_add_alias(struct interface *iface, struct blob_attr *config)
  530. {
  531. if (iface->proto_handler->flags & PROTO_FLAG_NODEV)
  532. return false;
  533. return __interface_add(iface, config, true);
  534. }
  535. void
  536. interface_set_l3_dev(struct interface *iface, struct device *dev)
  537. {
  538. bool enabled = iface->config_ip.enabled;
  539. bool claimed = iface->l3_dev.claimed;
  540. if (iface->l3_dev.dev == dev)
  541. return;
  542. interface_ip_set_enabled(&iface->config_ip, false);
  543. interface_ip_flush(&iface->proto_ip);
  544. device_add_user(&iface->l3_dev, dev);
  545. if (dev) {
  546. if (claimed)
  547. device_claim(&iface->l3_dev);
  548. interface_ip_set_enabled(&iface->config_ip, enabled);
  549. }
  550. }
  551. void
  552. interface_set_main_dev(struct interface *iface, struct device *dev)
  553. {
  554. bool set_l3 = (iface->main_dev.dev == iface->l3_dev.dev);
  555. bool claimed = iface->l3_dev.claimed;
  556. if (iface->main_dev.dev == dev)
  557. return;
  558. if (set_l3)
  559. interface_set_l3_dev(iface, dev);
  560. device_add_user(&iface->main_dev, dev);
  561. if (claimed)
  562. device_claim(&iface->l3_dev);
  563. if (!iface->l3_dev.dev)
  564. interface_set_l3_dev(iface, dev);
  565. }
  566. int
  567. interface_remove_link(struct interface *iface, struct device *dev)
  568. {
  569. struct device *mdev = iface->main_dev.dev;
  570. if (mdev && mdev->hotplug_ops)
  571. return mdev->hotplug_ops->del(mdev, dev);
  572. if (!iface->main_dev.hotplug)
  573. return UBUS_STATUS_INVALID_ARGUMENT;
  574. if (dev != iface->main_dev.dev)
  575. return UBUS_STATUS_INVALID_ARGUMENT;
  576. device_remove_user(&iface->main_dev);
  577. return 0;
  578. }
  579. int
  580. interface_add_link(struct interface *iface, struct device *dev)
  581. {
  582. struct device *mdev = iface->main_dev.dev;
  583. if (mdev == dev)
  584. return 0;
  585. if (iface->main_dev.hotplug)
  586. device_remove_user(&iface->main_dev);
  587. if (mdev) {
  588. if (mdev->hotplug_ops)
  589. return mdev->hotplug_ops->add(mdev, dev);
  590. else
  591. return UBUS_STATUS_NOT_SUPPORTED;
  592. }
  593. interface_set_main_dev(iface, dev);
  594. iface->main_dev.hotplug = true;
  595. return 0;
  596. }
  597. int
  598. interface_set_up(struct interface *iface)
  599. {
  600. int ret;
  601. iface->autostart = true;
  602. if (iface->state != IFS_DOWN)
  603. return 0;
  604. interface_clear_errors(iface);
  605. if (!iface->available) {
  606. interface_add_error(iface, "interface", "NO_DEVICE", NULL, 0);
  607. return -1;
  608. }
  609. if (iface->main_dev.dev) {
  610. ret = device_claim(&iface->main_dev);
  611. if (ret)
  612. return ret;
  613. }
  614. iface->state = IFS_SETUP;
  615. ret = interface_proto_event(iface->proto, PROTO_CMD_SETUP, false);
  616. if (ret) {
  617. mark_interface_down(iface);
  618. return ret;
  619. }
  620. return 0;
  621. }
  622. int
  623. interface_set_down(struct interface *iface)
  624. {
  625. if (!iface) {
  626. vlist_for_each_element(&interfaces, iface, node)
  627. __interface_set_down(iface, false);
  628. } else {
  629. iface->autostart = false;
  630. __interface_set_down(iface, false);
  631. }
  632. return 0;
  633. }
  634. void
  635. interface_start_pending(void)
  636. {
  637. struct interface *iface;
  638. vlist_for_each_element(&interfaces, iface, node) {
  639. if (iface->available && iface->autostart)
  640. interface_set_up(iface);
  641. }
  642. }
  643. static void
  644. set_config_state(struct interface *iface, enum interface_config_state s)
  645. {
  646. iface->config_state = s;
  647. if (iface->state == IFS_DOWN)
  648. interface_handle_config_change(iface);
  649. else
  650. __interface_set_down(iface, false);
  651. }
  652. void
  653. interface_update_start(struct interface *iface)
  654. {
  655. interface_ip_update_start(&iface->proto_ip);
  656. }
  657. void
  658. interface_update_complete(struct interface *iface)
  659. {
  660. interface_ip_update_complete(&iface->proto_ip);
  661. }
  662. static void
  663. interface_replace_dns(struct interface_ip_settings *new, struct interface_ip_settings *old)
  664. {
  665. vlist_simple_replace(&new->dns_servers, &old->dns_servers);
  666. vlist_simple_replace(&new->dns_search, &old->dns_search);
  667. }
  668. static void
  669. interface_change_config(struct interface *if_old, struct interface *if_new)
  670. {
  671. struct blob_attr *old_config = if_old->config;
  672. bool reload = false, reload_ip = false;
  673. #define FIELD_CHANGED_STR(field) \
  674. ((!!if_old->field != !!if_new->field) || \
  675. (if_old->field && \
  676. strcmp(if_old->field, if_new->field) != 0))
  677. if (FIELD_CHANGED_STR(parent_ifname)) {
  678. if (if_old->parent_iface.iface)
  679. interface_remove_user(&if_old->parent_iface);
  680. reload = true;
  681. }
  682. if (FIELD_CHANGED_STR(ifname) ||
  683. if_old->proto_handler != if_new->proto_handler)
  684. reload = true;
  685. if (!if_old->proto_handler->config_params)
  686. D(INTERFACE, "No config parameters for interface '%s'\n",
  687. if_old->name);
  688. else if (!uci_blob_check_equal(if_old->config, if_new->config,
  689. if_old->proto_handler->config_params))
  690. reload = true;
  691. #define UPDATE(field, __var) ({ \
  692. bool __changed = (if_old->field != if_new->field); \
  693. if_old->field = if_new->field; \
  694. __var |= __changed; \
  695. })
  696. if_old->config = if_new->config;
  697. if (!if_old->config_autostart && if_new->config_autostart)
  698. if_old->autostart = true;
  699. if_old->device_config = if_new->device_config;
  700. if_old->config_autostart = if_new->config_autostart;
  701. if_old->ifname = if_new->ifname;
  702. if_old->parent_ifname = if_new->parent_ifname;
  703. if_old->proto_handler = if_new->proto_handler;
  704. if_old->proto_ip.no_dns = if_new->proto_ip.no_dns;
  705. interface_replace_dns(&if_old->config_ip, &if_new->config_ip);
  706. UPDATE(metric, reload_ip);
  707. UPDATE(proto_ip.no_defaultroute, reload_ip);
  708. UPDATE(ip4table, reload_ip);
  709. UPDATE(ip6table, reload_ip);
  710. interface_merge_assignment_data(if_old, if_new);
  711. #undef UPDATE
  712. if (reload) {
  713. D(INTERFACE, "Reload interface '%s because of config changes\n",
  714. if_old->name);
  715. interface_clear_errors(if_old);
  716. set_config_state(if_old, IFC_RELOAD);
  717. goto out;
  718. }
  719. if (reload_ip) {
  720. interface_ip_set_enabled(&if_old->config_ip, false);
  721. interface_ip_set_enabled(&if_old->proto_ip, false);
  722. interface_ip_set_enabled(&if_old->proto_ip, if_new->proto_ip.enabled);
  723. interface_ip_set_enabled(&if_old->config_ip, if_new->config_ip.enabled);
  724. }
  725. interface_write_resolv_conf();
  726. out:
  727. if_new->config = NULL;
  728. interface_cleanup(if_new);
  729. free(old_config);
  730. free(if_new);
  731. }
  732. static void
  733. interface_update(struct vlist_tree *tree, struct vlist_node *node_new,
  734. struct vlist_node *node_old)
  735. {
  736. struct interface *if_old = container_of(node_old, struct interface, node);
  737. struct interface *if_new = container_of(node_new, struct interface, node);
  738. if (node_old && node_new) {
  739. D(INTERFACE, "Update interface '%s'\n", if_new->name);
  740. interface_change_config(if_old, if_new);
  741. } else if (node_old) {
  742. D(INTERFACE, "Remove interface '%s'\n", if_old->name);
  743. set_config_state(if_old, IFC_REMOVE);
  744. } else if (node_new) {
  745. D(INTERFACE, "Create interface '%s'\n", if_new->name);
  746. proto_init_interface(if_new, if_new->config);
  747. interface_claim_device(if_new);
  748. netifd_ubus_add_interface(if_new);
  749. }
  750. }
  751. static void __init
  752. interface_init_list(void)
  753. {
  754. vlist_init(&interfaces, avl_strcmp, interface_update);
  755. interfaces.keep_old = true;
  756. interfaces.no_delete = true;
  757. }