interface.c 33 KB

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