2
0

device.c 31 KB

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