device.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  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 <assert.h>
  18. #include <sys/types.h>
  19. #include <sys/socket.h>
  20. #include <net/ethernet.h>
  21. #ifdef linux
  22. #include <netinet/ether.h>
  23. #endif
  24. #include <libubox/list.h>
  25. #include "netifd.h"
  26. #include "system.h"
  27. #include "config.h"
  28. static struct list_head devtypes = LIST_HEAD_INIT(devtypes);
  29. static struct avl_tree devices;
  30. static const struct blobmsg_policy dev_attrs[__DEV_ATTR_MAX] = {
  31. [DEV_ATTR_TYPE] = { .name = "type", .type = BLOBMSG_TYPE_STRING },
  32. [DEV_ATTR_MTU] = { .name = "mtu", .type = BLOBMSG_TYPE_INT32 },
  33. [DEV_ATTR_MTU6] = { .name = "mtu6", .type = BLOBMSG_TYPE_INT32 },
  34. [DEV_ATTR_MACADDR] = { .name = "macaddr", .type = BLOBMSG_TYPE_STRING },
  35. [DEV_ATTR_TXQUEUELEN] = { .name = "txqueuelen", .type = BLOBMSG_TYPE_INT32 },
  36. [DEV_ATTR_ENABLED] = { .name = "enabled", .type = BLOBMSG_TYPE_BOOL },
  37. [DEV_ATTR_IPV6] = { .name = "ipv6", .type = BLOBMSG_TYPE_BOOL },
  38. [DEV_ATTR_PROMISC] = { .name = "promisc", .type = BLOBMSG_TYPE_BOOL },
  39. [DEV_ATTR_RPFILTER] = { .name = "rpfilter", .type = BLOBMSG_TYPE_STRING },
  40. [DEV_ATTR_ACCEPTLOCAL] = { .name = "acceptlocal", .type = BLOBMSG_TYPE_BOOL },
  41. [DEV_ATTR_IGMPVERSION] = { .name = "igmpversion", .type = BLOBMSG_TYPE_INT32 },
  42. [DEV_ATTR_MLDVERSION] = { .name = "mldversion", .type = BLOBMSG_TYPE_INT32 },
  43. [DEV_ATTR_NEIGHREACHABLETIME] = { .name = "neighreachabletime", .type = BLOBMSG_TYPE_INT32 },
  44. [DEV_ATTR_NEIGHGCSTALETIME] = { .name = "neighgcstaletime", .type = BLOBMSG_TYPE_INT32 },
  45. [DEV_ATTR_DADTRANSMITS] = { .name = "dadtransmits", .type = BLOBMSG_TYPE_INT32 },
  46. [DEV_ATTR_MULTICAST_TO_UNICAST] = { .name = "multicast_to_unicast", .type = BLOBMSG_TYPE_BOOL },
  47. [DEV_ATTR_MULTICAST_ROUTER] = { .name = "multicast_router", .type = BLOBMSG_TYPE_INT32 },
  48. [DEV_ATTR_MULTICAST_FAST_LEAVE] = { .name = "multicast_fast_leave", . type = BLOBMSG_TYPE_BOOL },
  49. [DEV_ATTR_MULTICAST] = { .name ="multicast", .type = BLOBMSG_TYPE_BOOL },
  50. [DEV_ATTR_LEARNING] = { .name ="learning", .type = BLOBMSG_TYPE_BOOL },
  51. [DEV_ATTR_UNICAST_FLOOD] = { .name ="unicast_flood", .type = BLOBMSG_TYPE_BOOL },
  52. [DEV_ATTR_SENDREDIRECTS] = { .name = "sendredirects", .type = BLOBMSG_TYPE_BOOL },
  53. [DEV_ATTR_NEIGHLOCKTIME] = { .name = "neighlocktime", .type = BLOBMSG_TYPE_INT32 },
  54. [DEV_ATTR_ISOLATE] = { .name = "isolate", .type = BLOBMSG_TYPE_BOOL },
  55. };
  56. const struct uci_blob_param_list device_attr_list = {
  57. .n_params = __DEV_ATTR_MAX,
  58. .params = dev_attrs,
  59. };
  60. static int __devlock = 0;
  61. int device_type_add(struct device_type *devtype)
  62. {
  63. if (device_type_get(devtype->name)) {
  64. netifd_log_message(L_WARNING, "Device handler '%s' already exists\n",
  65. devtype->name);
  66. return 1;
  67. }
  68. netifd_log_message(L_NOTICE, "Added device handler type: %s\n",
  69. devtype->name);
  70. list_add(&devtype->list, &devtypes);
  71. return 0;
  72. }
  73. /* Retrieve the device type for the given name. If 'bridge' is true, the type
  74. * must have bridge capabilities
  75. */
  76. struct device_type *
  77. device_type_get(const char *tname)
  78. {
  79. struct device_type *cur;
  80. list_for_each_entry(cur, &devtypes, list)
  81. if (!strcmp(cur->name, tname))
  82. return cur;
  83. return NULL;
  84. }
  85. void device_lock(void)
  86. {
  87. __devlock++;
  88. }
  89. void device_unlock(void)
  90. {
  91. __devlock--;
  92. if (!__devlock)
  93. device_free_unused(NULL);
  94. }
  95. static int set_device_state(struct device *dev, bool state)
  96. {
  97. if (state) {
  98. /* Get ifindex for all devices being enabled so a valid */
  99. /* ifindex is in place avoiding possible race conditions */
  100. device_set_ifindex(dev, system_if_resolve(dev));
  101. if (!dev->ifindex)
  102. return -1;
  103. system_if_up(dev);
  104. }
  105. else
  106. system_if_down(dev);
  107. return 0;
  108. }
  109. static int
  110. simple_device_set_state(struct device *dev, bool state)
  111. {
  112. struct device *pdev;
  113. int ret = 0;
  114. pdev = dev->parent.dev;
  115. if (state && !pdev) {
  116. pdev = system_if_get_parent(dev);
  117. if (pdev)
  118. device_add_user(&dev->parent, pdev);
  119. }
  120. if (pdev) {
  121. if (state)
  122. ret = device_claim(&dev->parent);
  123. else
  124. device_release(&dev->parent);
  125. if (ret < 0)
  126. return ret;
  127. }
  128. return set_device_state(dev, state);
  129. }
  130. static struct device *
  131. simple_device_create(const char *name, struct device_type *devtype,
  132. struct blob_attr *attr)
  133. {
  134. struct blob_attr *tb[__DEV_ATTR_MAX];
  135. struct device *dev = NULL;
  136. /* device type is unused for simple devices */
  137. devtype = NULL;
  138. blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb, blob_data(attr), blob_len(attr));
  139. dev = device_get(name, true);
  140. if (!dev)
  141. return NULL;
  142. dev->set_state = simple_device_set_state;
  143. device_init_settings(dev, tb);
  144. return dev;
  145. }
  146. static void simple_device_free(struct device *dev)
  147. {
  148. if (dev->parent.dev)
  149. device_remove_user(&dev->parent);
  150. free(dev);
  151. }
  152. struct device_type simple_device_type = {
  153. .name = "Network device",
  154. .config_params = &device_attr_list,
  155. .create = simple_device_create,
  156. .check_state = system_if_check,
  157. .free = simple_device_free,
  158. };
  159. void
  160. device_merge_settings(struct device *dev, struct device_settings *n)
  161. {
  162. struct device_settings *os = &dev->orig_settings;
  163. struct device_settings *s = &dev->settings;
  164. memset(n, 0, sizeof(*n));
  165. n->mtu = s->flags & DEV_OPT_MTU ? s->mtu : os->mtu;
  166. n->mtu6 = s->flags & DEV_OPT_MTU6 ? s->mtu6 : os->mtu6;
  167. n->txqueuelen = s->flags & DEV_OPT_TXQUEUELEN ?
  168. s->txqueuelen : os->txqueuelen;
  169. memcpy(n->macaddr,
  170. (s->flags & DEV_OPT_MACADDR ? s->macaddr : os->macaddr),
  171. sizeof(n->macaddr));
  172. n->ipv6 = s->flags & DEV_OPT_IPV6 ? s->ipv6 : os->ipv6;
  173. n->promisc = s->flags & DEV_OPT_PROMISC ? s->promisc : os->promisc;
  174. n->rpfilter = s->flags & DEV_OPT_RPFILTER ? s->rpfilter : os->rpfilter;
  175. n->acceptlocal = s->flags & DEV_OPT_ACCEPTLOCAL ? s->acceptlocal : os->acceptlocal;
  176. n->igmpversion = s->flags & DEV_OPT_IGMPVERSION ? s->igmpversion : os->igmpversion;
  177. n->mldversion = s->flags & DEV_OPT_MLDVERSION ? s->mldversion : os->mldversion;
  178. n->neigh4reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
  179. s->neigh4reachabletime : os->neigh4reachabletime;
  180. n->neigh6reachabletime = s->flags & DEV_OPT_NEIGHREACHABLETIME ?
  181. s->neigh6reachabletime : os->neigh6reachabletime;
  182. n->neigh4gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
  183. s->neigh4gcstaletime : os->neigh4gcstaletime;
  184. n->neigh6gcstaletime = s->flags & DEV_OPT_NEIGHGCSTALETIME ?
  185. s->neigh6gcstaletime : os->neigh6gcstaletime;
  186. n->neigh4locktime = s->flags & DEV_OPT_NEIGHLOCKTIME ?
  187. s->neigh4locktime : os->neigh4locktime;
  188. n->dadtransmits = s->flags & DEV_OPT_DADTRANSMITS ?
  189. s->dadtransmits : os->dadtransmits;
  190. n->multicast = s->flags & DEV_OPT_MULTICAST ?
  191. s->multicast : os->multicast;
  192. n->multicast_to_unicast = s->multicast_to_unicast;
  193. n->multicast_router = s->multicast_router;
  194. n->multicast_fast_leave = s->multicast_fast_leave;
  195. n->learning = s->learning;
  196. n->unicast_flood = s->unicast_flood;
  197. n->sendredirects = s->flags & DEV_OPT_SENDREDIRECTS ?
  198. s->sendredirects : os->sendredirects;
  199. n->flags = s->flags | os->flags | os->valid_flags;
  200. }
  201. void
  202. device_init_settings(struct device *dev, struct blob_attr **tb)
  203. {
  204. struct device_settings *s = &dev->settings;
  205. struct blob_attr *cur;
  206. struct ether_addr *ea;
  207. bool disabled = false;
  208. s->flags = 0;
  209. if ((cur = tb[DEV_ATTR_ENABLED]))
  210. disabled = !blobmsg_get_bool(cur);
  211. if ((cur = tb[DEV_ATTR_MTU]) && blobmsg_get_u32(cur) >= 68) {
  212. s->mtu = blobmsg_get_u32(cur);
  213. s->flags |= DEV_OPT_MTU;
  214. }
  215. if ((cur = tb[DEV_ATTR_MTU6]) && blobmsg_get_u32(cur) >= 1280) {
  216. s->mtu6 = blobmsg_get_u32(cur);
  217. s->flags |= DEV_OPT_MTU6;
  218. }
  219. if ((cur = tb[DEV_ATTR_TXQUEUELEN])) {
  220. s->txqueuelen = blobmsg_get_u32(cur);
  221. s->flags |= DEV_OPT_TXQUEUELEN;
  222. }
  223. if ((cur = tb[DEV_ATTR_MACADDR])) {
  224. ea = ether_aton(blobmsg_data(cur));
  225. if (ea) {
  226. memcpy(s->macaddr, ea, 6);
  227. s->flags |= DEV_OPT_MACADDR;
  228. }
  229. }
  230. if ((cur = tb[DEV_ATTR_IPV6])) {
  231. s->ipv6 = blobmsg_get_bool(cur);
  232. s->flags |= DEV_OPT_IPV6;
  233. }
  234. if ((cur = tb[DEV_ATTR_PROMISC])) {
  235. s->promisc = blobmsg_get_bool(cur);
  236. s->flags |= DEV_OPT_PROMISC;
  237. }
  238. if ((cur = tb[DEV_ATTR_RPFILTER])) {
  239. if (system_resolve_rpfilter(blobmsg_data(cur), &s->rpfilter))
  240. s->flags |= DEV_OPT_RPFILTER;
  241. else
  242. DPRINTF("Failed to resolve rpfilter: %s\n", (char *) blobmsg_data(cur));
  243. }
  244. if ((cur = tb[DEV_ATTR_ACCEPTLOCAL])) {
  245. s->acceptlocal = blobmsg_get_bool(cur);
  246. s->flags |= DEV_OPT_ACCEPTLOCAL;
  247. }
  248. if ((cur = tb[DEV_ATTR_IGMPVERSION])) {
  249. s->igmpversion = blobmsg_get_u32(cur);
  250. if (s->igmpversion >= 1 && s->igmpversion <= 3)
  251. s->flags |= DEV_OPT_IGMPVERSION;
  252. else
  253. DPRINTF("Failed to resolve igmpversion: %d\n", blobmsg_get_u32(cur));
  254. }
  255. if ((cur = tb[DEV_ATTR_MLDVERSION])) {
  256. s->mldversion = blobmsg_get_u32(cur);
  257. if (s->mldversion >= 1 && s->mldversion <= 2)
  258. s->flags |= DEV_OPT_MLDVERSION;
  259. else
  260. DPRINTF("Failed to resolve mldversion: %d\n", blobmsg_get_u32(cur));
  261. }
  262. if ((cur = tb[DEV_ATTR_NEIGHREACHABLETIME])) {
  263. s->neigh6reachabletime = s->neigh4reachabletime = blobmsg_get_u32(cur);
  264. s->flags |= DEV_OPT_NEIGHREACHABLETIME;
  265. }
  266. if ((cur = tb[DEV_ATTR_NEIGHGCSTALETIME])) {
  267. s->neigh6gcstaletime = s->neigh4gcstaletime = blobmsg_get_u32(cur);
  268. s->flags |= DEV_OPT_NEIGHGCSTALETIME;
  269. }
  270. if ((cur = tb[DEV_ATTR_NEIGHLOCKTIME])) {
  271. s->neigh4locktime = blobmsg_get_u32(cur);
  272. s->flags |= DEV_OPT_NEIGHLOCKTIME;
  273. }
  274. if ((cur = tb[DEV_ATTR_DADTRANSMITS])) {
  275. s->dadtransmits = blobmsg_get_u32(cur);
  276. s->flags |= DEV_OPT_DADTRANSMITS;
  277. }
  278. if ((cur = tb[DEV_ATTR_MULTICAST_TO_UNICAST])) {
  279. s->multicast_to_unicast = blobmsg_get_bool(cur);
  280. s->flags |= DEV_OPT_MULTICAST_TO_UNICAST;
  281. }
  282. if ((cur = tb[DEV_ATTR_MULTICAST_ROUTER])) {
  283. s->multicast_router = blobmsg_get_u32(cur);
  284. if (s->multicast_router <= 2)
  285. s->flags |= DEV_OPT_MULTICAST_ROUTER;
  286. else
  287. DPRINTF("Invalid value: %d - (Use 0: never, 1: learn, 2: always)\n", blobmsg_get_u32(cur));
  288. }
  289. if ((cur = tb[DEV_ATTR_MULTICAST_FAST_LEAVE])) {
  290. s->multicast_fast_leave = blobmsg_get_bool(cur);
  291. s->flags |= DEV_OPT_MULTICAST_FAST_LEAVE;
  292. }
  293. if ((cur = tb[DEV_ATTR_MULTICAST])) {
  294. s->multicast = blobmsg_get_bool(cur);
  295. s->flags |= DEV_OPT_MULTICAST;
  296. }
  297. if ((cur = tb[DEV_ATTR_LEARNING])) {
  298. s->learning = blobmsg_get_bool(cur);
  299. s->flags |= DEV_OPT_LEARNING;
  300. }
  301. if ((cur = tb[DEV_ATTR_UNICAST_FLOOD])) {
  302. s->unicast_flood = blobmsg_get_bool(cur);
  303. s->flags |= DEV_OPT_UNICAST_FLOOD;
  304. }
  305. if ((cur = tb[DEV_ATTR_SENDREDIRECTS])) {
  306. s->sendredirects = blobmsg_get_bool(cur);
  307. s->flags |= DEV_OPT_SENDREDIRECTS;
  308. }
  309. if ((cur = tb[DEV_ATTR_ISOLATE])) {
  310. s->isolate = blobmsg_get_bool(cur);
  311. s->flags |= DEV_OPT_ISOLATE;
  312. }
  313. device_set_disabled(dev, disabled);
  314. }
  315. static void __init dev_init(void)
  316. {
  317. avl_init(&devices, avl_strcmp, true, NULL);
  318. }
  319. static int device_broadcast_cb(void *ctx, struct safe_list *list)
  320. {
  321. struct device_user *dep = container_of(list, struct device_user, list);
  322. int *ev = ctx;
  323. /* device might have been removed by an earlier callback */
  324. if (!dep->dev)
  325. return 0;
  326. if (dep->cb)
  327. dep->cb(dep, *ev);
  328. return 0;
  329. }
  330. void device_broadcast_event(struct device *dev, enum device_event ev)
  331. {
  332. int dev_ev = ev;
  333. safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
  334. safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
  335. }
  336. int device_claim(struct device_user *dep)
  337. {
  338. struct device *dev = dep->dev;
  339. int ret = 0;
  340. if (dep->claimed)
  341. return 0;
  342. if (!dev)
  343. return -1;
  344. dep->claimed = true;
  345. D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
  346. if (++dev->active != 1)
  347. return 0;
  348. device_broadcast_event(dev, DEV_EVENT_SETUP);
  349. if (dev->external) {
  350. /* Get ifindex for external claimed devices so a valid */
  351. /* ifindex is in place avoiding possible race conditions */
  352. device_set_ifindex(dev, system_if_resolve(dev));
  353. if (!dev->ifindex)
  354. ret = -1;
  355. system_if_get_settings(dev, &dev->orig_settings);
  356. } else
  357. ret = dev->set_state(dev, true);
  358. if (ret == 0)
  359. device_broadcast_event(dev, DEV_EVENT_UP);
  360. else {
  361. D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
  362. dev->active = 0;
  363. dep->claimed = false;
  364. }
  365. return ret;
  366. }
  367. void device_release(struct device_user *dep)
  368. {
  369. struct device *dev = dep->dev;
  370. if (!dep->claimed)
  371. return;
  372. dep->claimed = false;
  373. dev->active--;
  374. D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
  375. assert(dev->active >= 0);
  376. if (dev->active)
  377. return;
  378. device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
  379. if (!dev->external)
  380. dev->set_state(dev, false);
  381. if (dev->active)
  382. return;
  383. device_broadcast_event(dev, DEV_EVENT_DOWN);
  384. }
  385. int device_check_state(struct device *dev)
  386. {
  387. if (!dev->type->check_state)
  388. return simple_device_type.check_state(dev);
  389. return dev->type->check_state(dev);
  390. }
  391. int device_init_virtual(struct device *dev, struct device_type *type, const char *name)
  392. {
  393. assert(dev);
  394. assert(type);
  395. D(DEVICE, "Initialize device '%s'\n", name ? name : "");
  396. INIT_SAFE_LIST(&dev->users);
  397. INIT_SAFE_LIST(&dev->aliases);
  398. dev->type = type;
  399. if (name) {
  400. int ret;
  401. ret = device_set_ifname(dev, name);
  402. if (ret < 0)
  403. return ret;
  404. }
  405. if (!dev->set_state)
  406. dev->set_state = set_device_state;
  407. return 0;
  408. }
  409. int device_init(struct device *dev, struct device_type *type, const char *ifname)
  410. {
  411. int ret;
  412. ret = device_init_virtual(dev, type, ifname);
  413. if (ret < 0)
  414. return ret;
  415. dev->avl.key = dev->ifname;
  416. ret = avl_insert(&devices, &dev->avl);
  417. if (ret < 0)
  418. return ret;
  419. system_if_clear_state(dev);
  420. device_check_state(dev);
  421. return 0;
  422. }
  423. static struct device *
  424. device_create_default(const char *name, bool external)
  425. {
  426. struct device *dev;
  427. if (!external && system_if_force_external(name))
  428. return NULL;
  429. D(DEVICE, "Create simple device '%s'\n", name);
  430. dev = calloc(1, sizeof(*dev));
  431. if (!dev)
  432. return NULL;
  433. dev->external = external;
  434. dev->set_state = simple_device_set_state;
  435. if (device_init(dev, &simple_device_type, name) < 0) {
  436. device_cleanup(dev);
  437. free(dev);
  438. return NULL;
  439. }
  440. dev->default_config = true;
  441. if (external)
  442. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  443. return dev;
  444. }
  445. struct device *
  446. device_find(const char *name)
  447. {
  448. struct device *dev;
  449. return avl_find_element(&devices, name, dev, avl);
  450. }
  451. struct device *
  452. device_get(const char *name, int create)
  453. {
  454. struct device *dev;
  455. if (strchr(name, '.'))
  456. return get_vlan_device_chain(name, create);
  457. if (name[0] == '@')
  458. return device_alias_get(name + 1);
  459. dev = avl_find_element(&devices, name, dev, avl);
  460. if (dev) {
  461. if (create > 1 && !dev->external) {
  462. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  463. dev->external = true;
  464. device_set_present(dev, true);
  465. }
  466. return dev;
  467. }
  468. if (!create)
  469. return NULL;
  470. return device_create_default(name, create > 1);
  471. }
  472. static void
  473. device_delete(struct device *dev)
  474. {
  475. if (!dev->avl.key)
  476. return;
  477. D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
  478. avl_delete(&devices, &dev->avl);
  479. dev->avl.key = NULL;
  480. }
  481. static int device_cleanup_cb(void *ctx, struct safe_list *list)
  482. {
  483. struct device_user *dep = container_of(list, struct device_user, list);
  484. if (dep->cb)
  485. dep->cb(dep, DEV_EVENT_REMOVE);
  486. device_release(dep);
  487. return 0;
  488. }
  489. void device_cleanup(struct device *dev)
  490. {
  491. D(DEVICE, "Clean up device '%s'\n", dev->ifname);
  492. safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
  493. safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
  494. device_delete(dev);
  495. }
  496. static void __device_set_present(struct device *dev, bool state)
  497. {
  498. if (dev->present == state)
  499. return;
  500. dev->present = state;
  501. device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
  502. }
  503. void
  504. device_refresh_present(struct device *dev)
  505. {
  506. bool state = dev->sys_present;
  507. if (dev->disabled || dev->deferred)
  508. state = false;
  509. __device_set_present(dev, state);
  510. }
  511. void device_set_present(struct device *dev, bool state)
  512. {
  513. if (dev->sys_present == state)
  514. return;
  515. D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
  516. dev->sys_present = state;
  517. device_refresh_present(dev);
  518. }
  519. void device_set_link(struct device *dev, bool state)
  520. {
  521. if (dev->link_active == state)
  522. return;
  523. netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
  524. dev->link_active = state;
  525. device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
  526. }
  527. void device_set_ifindex(struct device *dev, int ifindex)
  528. {
  529. if (dev->ifindex == ifindex)
  530. return;
  531. dev->ifindex = ifindex;
  532. device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
  533. }
  534. int device_set_ifname(struct device *dev, const char *name)
  535. {
  536. int ret = 0;
  537. if (!strcmp(dev->ifname, name))
  538. return 0;
  539. if (strlen(name) > sizeof(dev->ifname) - 1)
  540. return -1;
  541. if (dev->avl.key)
  542. avl_delete(&devices, &dev->avl);
  543. strcpy(dev->ifname, name);
  544. if (dev->avl.key)
  545. ret = avl_insert(&devices, &dev->avl);
  546. if (ret == 0)
  547. device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
  548. return ret;
  549. }
  550. static int device_refcount(struct device *dev)
  551. {
  552. struct list_head *list;
  553. int count = 0;
  554. list_for_each(list, &dev->users.list)
  555. count++;
  556. list_for_each(list, &dev->aliases.list)
  557. count++;
  558. return count;
  559. }
  560. static void
  561. __device_add_user(struct device_user *dep, struct device *dev)
  562. {
  563. struct safe_list *head;
  564. dep->dev = dev;
  565. if (dep->alias)
  566. head = &dev->aliases;
  567. else
  568. head = &dev->users;
  569. safe_list_add(&dep->list, head);
  570. D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
  571. if (dep->cb && dev->present) {
  572. dep->cb(dep, DEV_EVENT_ADD);
  573. if (dev->active)
  574. dep->cb(dep, DEV_EVENT_UP);
  575. if (dev->link_active)
  576. dep->cb(dep, DEV_EVENT_LINK_UP);
  577. }
  578. }
  579. void device_add_user(struct device_user *dep, struct device *dev)
  580. {
  581. if (dep->dev == dev)
  582. return;
  583. if (dep->dev)
  584. device_remove_user(dep);
  585. if (!dev)
  586. return;
  587. __device_add_user(dep, dev);
  588. }
  589. static void
  590. device_free(struct device *dev)
  591. {
  592. __devlock++;
  593. free(dev->config);
  594. device_cleanup(dev);
  595. dev->type->free(dev);
  596. __devlock--;
  597. }
  598. static void
  599. __device_free_unused(struct device *dev)
  600. {
  601. if (!safe_list_empty(&dev->users) ||
  602. !safe_list_empty(&dev->aliases) ||
  603. dev->current_config || __devlock)
  604. return;
  605. device_free(dev);
  606. }
  607. void device_remove_user(struct device_user *dep)
  608. {
  609. struct device *dev = dep->dev;
  610. if (!dep->dev)
  611. return;
  612. dep->hotplug = false;
  613. if (dep->claimed)
  614. device_release(dep);
  615. safe_list_del(&dep->list);
  616. dep->dev = NULL;
  617. D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
  618. __device_free_unused(dev);
  619. }
  620. void
  621. device_free_unused(struct device *dev)
  622. {
  623. struct device *tmp;
  624. if (dev)
  625. return __device_free_unused(dev);
  626. avl_for_each_element_safe(&devices, dev, avl, tmp)
  627. __device_free_unused(dev);
  628. }
  629. void
  630. device_init_pending(void)
  631. {
  632. struct device *dev, *tmp;
  633. avl_for_each_element_safe(&devices, dev, avl, tmp) {
  634. if (!dev->config_pending)
  635. continue;
  636. dev->type->config_init(dev);
  637. dev->config_pending = false;
  638. }
  639. }
  640. static enum dev_change_type
  641. device_set_config(struct device *dev, struct device_type *type,
  642. struct blob_attr *attr)
  643. {
  644. struct blob_attr *tb[__DEV_ATTR_MAX];
  645. const struct uci_blob_param_list *cfg = type->config_params;
  646. if (type != dev->type)
  647. return DEV_CONFIG_RECREATE;
  648. if (dev->type->reload)
  649. return dev->type->reload(dev, attr);
  650. if (uci_blob_check_equal(dev->config, attr, cfg))
  651. return DEV_CONFIG_NO_CHANGE;
  652. if (cfg == &device_attr_list) {
  653. memset(tb, 0, sizeof(tb));
  654. if (attr)
  655. blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
  656. blob_data(attr), blob_len(attr));
  657. device_init_settings(dev, tb);
  658. return DEV_CONFIG_RESTART;
  659. } else
  660. return DEV_CONFIG_RECREATE;
  661. }
  662. enum dev_change_type
  663. device_apply_config(struct device *dev, struct device_type *type,
  664. struct blob_attr *config)
  665. {
  666. enum dev_change_type change;
  667. change = device_set_config(dev, type, config);
  668. if (dev->external) {
  669. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  670. change = DEV_CONFIG_APPLIED;
  671. }
  672. switch (change) {
  673. case DEV_CONFIG_RESTART:
  674. case DEV_CONFIG_APPLIED:
  675. D(DEVICE, "Device '%s': config applied\n", dev->ifname);
  676. config = blob_memdup(config);
  677. free(dev->config);
  678. dev->config = config;
  679. if (change == DEV_CONFIG_RESTART && dev->present) {
  680. int ret = 0;
  681. device_set_present(dev, false);
  682. if (dev->active && !dev->external) {
  683. ret = dev->set_state(dev, false);
  684. if (!ret)
  685. ret = dev->set_state(dev, true);
  686. }
  687. if (!ret)
  688. device_set_present(dev, true);
  689. }
  690. break;
  691. case DEV_CONFIG_NO_CHANGE:
  692. D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
  693. break;
  694. case DEV_CONFIG_RECREATE:
  695. break;
  696. }
  697. return change;
  698. }
  699. static void
  700. device_replace(struct device *dev, struct device *odev)
  701. {
  702. struct device_user *dep, *tmp;
  703. __devlock++;
  704. if (odev->present)
  705. device_set_present(odev, false);
  706. list_for_each_entry_safe(dep, tmp, &odev->users.list, list.list) {
  707. device_release(dep);
  708. safe_list_del(&dep->list);
  709. __device_add_user(dep, dev);
  710. }
  711. __devlock--;
  712. device_free(odev);
  713. }
  714. void
  715. device_reset_config(void)
  716. {
  717. struct device *dev;
  718. avl_for_each_element(&devices, dev, avl)
  719. dev->current_config = false;
  720. }
  721. void
  722. device_reset_old(void)
  723. {
  724. struct device *dev, *tmp, *ndev;
  725. avl_for_each_element_safe(&devices, dev, avl, tmp) {
  726. if (dev->current_config || dev->default_config)
  727. continue;
  728. if (dev->type != &simple_device_type)
  729. continue;
  730. ndev = device_create_default(dev->ifname, dev->external);
  731. if (!ndev)
  732. continue;
  733. device_replace(ndev, dev);
  734. }
  735. }
  736. struct device *
  737. device_create(const char *name, struct device_type *type,
  738. struct blob_attr *config)
  739. {
  740. struct device *odev = NULL, *dev;
  741. enum dev_change_type change;
  742. odev = device_find(name);
  743. if (odev) {
  744. odev->current_config = true;
  745. change = device_apply_config(odev, type, config);
  746. switch (change) {
  747. case DEV_CONFIG_RECREATE:
  748. D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
  749. device_delete(odev);
  750. break;
  751. default:
  752. return odev;
  753. }
  754. } else
  755. D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
  756. config = blob_memdup(config);
  757. if (!config)
  758. return NULL;
  759. dev = type->create(name, type, config);
  760. if (!dev)
  761. return NULL;
  762. dev->current_config = true;
  763. dev->config = config;
  764. if (odev)
  765. device_replace(dev, odev);
  766. if (!config_init && dev->config_pending) {
  767. type->config_init(dev);
  768. dev->config_pending = false;
  769. }
  770. return dev;
  771. }
  772. void
  773. device_dump_status(struct blob_buf *b, struct device *dev)
  774. {
  775. struct device_settings st;
  776. void *c, *s;
  777. if (!dev) {
  778. avl_for_each_element(&devices, dev, avl) {
  779. if (!dev->present)
  780. continue;
  781. c = blobmsg_open_table(b, dev->ifname);
  782. device_dump_status(b, dev);
  783. blobmsg_close_table(b, c);
  784. }
  785. return;
  786. }
  787. blobmsg_add_u8(b, "external", dev->external);
  788. blobmsg_add_u8(b, "present", dev->present);
  789. blobmsg_add_string(b, "type", dev->type->name);
  790. if (!dev->present)
  791. return;
  792. blobmsg_add_u8(b, "up", !!dev->active);
  793. blobmsg_add_u8(b, "carrier", !!dev->link_active);
  794. if (dev->type->dump_info)
  795. dev->type->dump_info(dev, b);
  796. else
  797. system_if_dump_info(dev, b);
  798. if (dev->active) {
  799. device_merge_settings(dev, &st);
  800. if (st.flags & DEV_OPT_MTU)
  801. blobmsg_add_u32(b, "mtu", st.mtu);
  802. if (st.flags & DEV_OPT_MTU6)
  803. blobmsg_add_u32(b, "mtu6", st.mtu6);
  804. if (st.flags & DEV_OPT_MACADDR)
  805. blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
  806. if (st.flags & DEV_OPT_TXQUEUELEN)
  807. blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
  808. if (st.flags & DEV_OPT_IPV6)
  809. blobmsg_add_u8(b, "ipv6", st.ipv6);
  810. if (st.flags & DEV_OPT_PROMISC)
  811. blobmsg_add_u8(b, "promisc", st.promisc);
  812. if (st.flags & DEV_OPT_RPFILTER)
  813. blobmsg_add_u32(b, "rpfilter", st.rpfilter);
  814. if (st.flags & DEV_OPT_ACCEPTLOCAL)
  815. blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
  816. if (st.flags & DEV_OPT_IGMPVERSION)
  817. blobmsg_add_u32(b, "igmpversion", st.igmpversion);
  818. if (st.flags & DEV_OPT_MLDVERSION)
  819. blobmsg_add_u32(b, "mldversion", st.mldversion);
  820. if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
  821. blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
  822. blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
  823. }
  824. if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
  825. blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
  826. blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
  827. }
  828. if (st.flags & DEV_OPT_NEIGHLOCKTIME)
  829. blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime);
  830. if (st.flags & DEV_OPT_DADTRANSMITS)
  831. blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
  832. if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
  833. blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
  834. if (st.flags & DEV_OPT_MULTICAST_ROUTER)
  835. blobmsg_add_u32(b, "multicast_router", st.multicast_router);
  836. if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
  837. blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
  838. if (st.flags & DEV_OPT_MULTICAST)
  839. blobmsg_add_u8(b, "multicast", st.multicast);
  840. if (st.flags & DEV_OPT_LEARNING)
  841. blobmsg_add_u8(b, "learning", st.learning);
  842. if (st.flags & DEV_OPT_UNICAST_FLOOD)
  843. blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
  844. if (st.flags & DEV_OPT_SENDREDIRECTS)
  845. blobmsg_add_u8(b, "sendredirects", st.sendredirects);
  846. }
  847. s = blobmsg_open_table(b, "statistics");
  848. if (dev->type->dump_stats)
  849. dev->type->dump_stats(dev, b);
  850. else
  851. system_if_dump_stats(dev, b);
  852. blobmsg_close_table(b, s);
  853. }
  854. static void __init simple_device_type_init(void)
  855. {
  856. device_type_add(&simple_device_type);
  857. }