interface.c 32 KB

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