device.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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_release_cb(void *ctx, struct safe_list *list)
  401. {
  402. struct device_user *dep = container_of(list, struct device_user, list);
  403. if (!dep->dev || !dep->claimed)
  404. return 0;
  405. device_release(dep);
  406. return 0;
  407. }
  408. static int device_broadcast_cb(void *ctx, struct safe_list *list)
  409. {
  410. struct device_user *dep = container_of(list, struct device_user, list);
  411. int *ev = ctx;
  412. /* device might have been removed by an earlier callback */
  413. if (!dep->dev)
  414. return 0;
  415. if (dep->cb)
  416. dep->cb(dep, *ev);
  417. return 0;
  418. }
  419. void device_broadcast_event(struct device *dev, enum device_event ev)
  420. {
  421. int dev_ev = ev;
  422. safe_list_for_each(&dev->aliases, device_broadcast_cb, &dev_ev);
  423. safe_list_for_each(&dev->users, device_broadcast_cb, &dev_ev);
  424. }
  425. static void
  426. device_fill_default_settings(struct device *dev)
  427. {
  428. struct device_settings *s = &dev->settings;
  429. struct ether_addr *ea;
  430. if (!(s->flags & DEV_OPT_MACADDR)) {
  431. ea = config_get_default_macaddr(dev->ifname);
  432. if (ea) {
  433. memcpy(s->macaddr, ea, 6);
  434. s->flags |= DEV_OPT_DEFAULT_MACADDR;
  435. }
  436. }
  437. }
  438. int device_claim(struct device_user *dep)
  439. {
  440. struct device *dev = dep->dev;
  441. int ret = 0;
  442. if (dep->claimed)
  443. return 0;
  444. if (!dev)
  445. return -1;
  446. dep->claimed = true;
  447. D(DEVICE, "Claim %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active + 1);
  448. if (++dev->active != 1)
  449. return 0;
  450. device_broadcast_event(dev, DEV_EVENT_SETUP);
  451. device_fill_default_settings(dev);
  452. if (dev->external) {
  453. /* Get ifindex for external claimed devices so a valid */
  454. /* ifindex is in place avoiding possible race conditions */
  455. device_set_ifindex(dev, system_if_resolve(dev));
  456. if (!dev->ifindex)
  457. ret = -1;
  458. system_if_get_settings(dev, &dev->orig_settings);
  459. } else
  460. ret = dev->set_state(dev, true);
  461. if (ret == 0)
  462. device_broadcast_event(dev, DEV_EVENT_UP);
  463. else {
  464. D(DEVICE, "claim %s %s failed: %d\n", dev->type->name, dev->ifname, ret);
  465. dev->active = 0;
  466. dep->claimed = false;
  467. }
  468. return ret;
  469. }
  470. void device_release(struct device_user *dep)
  471. {
  472. struct device *dev = dep->dev;
  473. if (!dep->claimed)
  474. return;
  475. dep->claimed = false;
  476. dev->active--;
  477. D(DEVICE, "Release %s %s, new active count: %d\n", dev->type->name, dev->ifname, dev->active);
  478. assert(dev->active >= 0);
  479. if (dev->active)
  480. return;
  481. device_broadcast_event(dev, DEV_EVENT_TEARDOWN);
  482. if (!dev->external)
  483. dev->set_state(dev, false);
  484. if (dev->active)
  485. return;
  486. device_broadcast_event(dev, DEV_EVENT_DOWN);
  487. }
  488. int device_check_state(struct device *dev)
  489. {
  490. if (!dev->type->check_state)
  491. return simple_device_type.check_state(dev);
  492. return dev->type->check_state(dev);
  493. }
  494. int device_init_virtual(struct device *dev, struct device_type *type, const char *name)
  495. {
  496. assert(dev);
  497. assert(type);
  498. D(DEVICE, "Initialize device '%s'\n", name ? name : "");
  499. INIT_SAFE_LIST(&dev->users);
  500. INIT_SAFE_LIST(&dev->aliases);
  501. dev->type = type;
  502. if (name) {
  503. int ret;
  504. ret = device_set_ifname(dev, name);
  505. if (ret < 0)
  506. return ret;
  507. }
  508. if (!dev->set_state)
  509. dev->set_state = set_device_state;
  510. return 0;
  511. }
  512. int device_init(struct device *dev, struct device_type *type, const char *ifname)
  513. {
  514. int ret;
  515. ret = device_init_virtual(dev, type, ifname);
  516. if (ret < 0)
  517. return ret;
  518. dev->avl.key = dev->ifname;
  519. ret = avl_insert(&devices, &dev->avl);
  520. if (ret < 0)
  521. return ret;
  522. system_if_clear_state(dev);
  523. return 0;
  524. }
  525. static struct device *
  526. device_create_default(const char *name, bool external)
  527. {
  528. struct device *dev;
  529. if (!external && system_if_force_external(name))
  530. return NULL;
  531. D(DEVICE, "Create simple device '%s'\n", name);
  532. dev = calloc(1, sizeof(*dev));
  533. if (!dev)
  534. return NULL;
  535. dev->external = external;
  536. dev->set_state = simple_device_set_state;
  537. if (device_init(dev, &simple_device_type, name) < 0) {
  538. device_cleanup(dev);
  539. free(dev);
  540. return NULL;
  541. }
  542. dev->default_config = true;
  543. if (external)
  544. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  545. device_check_state(dev);
  546. return dev;
  547. }
  548. struct device *
  549. device_find(const char *name)
  550. {
  551. struct device *dev;
  552. return avl_find_element(&devices, name, dev, avl);
  553. }
  554. struct device *
  555. __device_get(const char *name, int create, bool check_vlan)
  556. {
  557. struct device *dev;
  558. dev = avl_find_element(&devices, name, dev, avl);
  559. if (!dev && check_vlan && strchr(name, '.'))
  560. return get_vlan_device_chain(name, create);
  561. if (name[0] == '@')
  562. return device_alias_get(name + 1);
  563. if (dev) {
  564. if (create > 1 && !dev->external) {
  565. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  566. dev->external = true;
  567. device_set_present(dev, true);
  568. }
  569. return dev;
  570. }
  571. if (!create)
  572. return NULL;
  573. return device_create_default(name, create > 1);
  574. }
  575. static void
  576. device_delete(struct device *dev)
  577. {
  578. if (!dev->avl.key)
  579. return;
  580. D(DEVICE, "Delete device '%s' from list\n", dev->ifname);
  581. avl_delete(&devices, &dev->avl);
  582. dev->avl.key = NULL;
  583. }
  584. static int device_cleanup_cb(void *ctx, struct safe_list *list)
  585. {
  586. struct device_user *dep = container_of(list, struct device_user, list);
  587. if (dep->cb)
  588. dep->cb(dep, DEV_EVENT_REMOVE);
  589. device_release(dep);
  590. return 0;
  591. }
  592. void device_cleanup(struct device *dev)
  593. {
  594. D(DEVICE, "Clean up device '%s'\n", dev->ifname);
  595. safe_list_for_each(&dev->users, device_cleanup_cb, NULL);
  596. safe_list_for_each(&dev->aliases, device_cleanup_cb, NULL);
  597. device_delete(dev);
  598. }
  599. static void __device_set_present(struct device *dev, bool state)
  600. {
  601. if (dev->present == state)
  602. return;
  603. dev->present = state;
  604. device_broadcast_event(dev, state ? DEV_EVENT_ADD : DEV_EVENT_REMOVE);
  605. }
  606. void
  607. device_refresh_present(struct device *dev)
  608. {
  609. bool state = dev->sys_present;
  610. if (dev->disabled || dev->deferred)
  611. state = false;
  612. __device_set_present(dev, state);
  613. }
  614. void
  615. device_set_auth_status(struct device *dev, bool value)
  616. {
  617. if (dev->auth_status == value)
  618. return;
  619. dev->auth_status = value;
  620. if (!dev->present)
  621. return;
  622. if (dev->auth_status) {
  623. device_broadcast_event(dev, DEV_EVENT_AUTH_UP);
  624. return;
  625. }
  626. device_broadcast_event(dev, DEV_EVENT_LINK_DOWN);
  627. if (!dev->link_active)
  628. return;
  629. device_broadcast_event(dev, DEV_EVENT_LINK_UP);
  630. }
  631. void device_set_present(struct device *dev, bool state)
  632. {
  633. if (dev->sys_present == state)
  634. return;
  635. D(DEVICE, "%s '%s' %s present\n", dev->type->name, dev->ifname, state ? "is now" : "is no longer" );
  636. dev->sys_present = state;
  637. device_refresh_present(dev);
  638. if (!state)
  639. safe_list_for_each(&dev->users, device_release_cb, NULL);
  640. }
  641. void device_set_link(struct device *dev, bool state)
  642. {
  643. if (dev->link_active == state)
  644. return;
  645. netifd_log_message(L_NOTICE, "%s '%s' link is %s\n", dev->type->name, dev->ifname, state ? "up" : "down" );
  646. dev->link_active = state;
  647. if (!state)
  648. dev->auth_status = false;
  649. device_broadcast_event(dev, state ? DEV_EVENT_LINK_UP : DEV_EVENT_LINK_DOWN);
  650. }
  651. void device_set_ifindex(struct device *dev, int ifindex)
  652. {
  653. if (dev->ifindex == ifindex)
  654. return;
  655. dev->ifindex = ifindex;
  656. device_broadcast_event(dev, DEV_EVENT_UPDATE_IFINDEX);
  657. }
  658. int device_set_ifname(struct device *dev, const char *name)
  659. {
  660. int ret = 0;
  661. if (!strcmp(dev->ifname, name))
  662. return 0;
  663. if (strlen(name) > sizeof(dev->ifname) - 1)
  664. return -1;
  665. if (dev->avl.key)
  666. avl_delete(&devices, &dev->avl);
  667. strcpy(dev->ifname, name);
  668. if (dev->avl.key)
  669. ret = avl_insert(&devices, &dev->avl);
  670. if (ret == 0)
  671. device_broadcast_event(dev, DEV_EVENT_UPDATE_IFNAME);
  672. return ret;
  673. }
  674. static int device_refcount(struct device *dev)
  675. {
  676. struct list_head *list;
  677. int count = 0;
  678. list_for_each(list, &dev->users.list)
  679. count++;
  680. list_for_each(list, &dev->aliases.list)
  681. count++;
  682. return count;
  683. }
  684. static void
  685. __device_add_user(struct device_user *dep, struct device *dev)
  686. {
  687. struct safe_list *head;
  688. dep->dev = dev;
  689. if (dep->alias)
  690. head = &dev->aliases;
  691. else
  692. head = &dev->users;
  693. safe_list_add(&dep->list, head);
  694. D(DEVICE, "Add user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
  695. if (dep->cb && dev->present) {
  696. dep->cb(dep, DEV_EVENT_ADD);
  697. if (dev->active)
  698. dep->cb(dep, DEV_EVENT_UP);
  699. if (dev->link_active)
  700. dep->cb(dep, DEV_EVENT_LINK_UP);
  701. }
  702. }
  703. void device_add_user(struct device_user *dep, struct device *dev)
  704. {
  705. if (dep->dev == dev)
  706. return;
  707. if (dep->dev)
  708. device_remove_user(dep);
  709. if (!dev)
  710. return;
  711. __device_add_user(dep, dev);
  712. }
  713. static void
  714. device_free(struct device *dev)
  715. {
  716. __devlock++;
  717. free(dev->config);
  718. device_cleanup(dev);
  719. dev->type->free(dev);
  720. __devlock--;
  721. }
  722. static void
  723. __device_free_unused(struct uloop_timeout *timeout)
  724. {
  725. struct device *dev, *tmp;
  726. avl_for_each_element_safe(&devices, dev, avl, tmp) {
  727. if (!safe_list_empty(&dev->users) ||
  728. !safe_list_empty(&dev->aliases) ||
  729. dev->current_config)
  730. continue;
  731. device_free(dev);
  732. }
  733. }
  734. void device_free_unused(void)
  735. {
  736. static struct uloop_timeout free_timer = {
  737. .cb = __device_free_unused,
  738. };
  739. uloop_timeout_set(&free_timer, 1);
  740. }
  741. void device_remove_user(struct device_user *dep)
  742. {
  743. struct device *dev = dep->dev;
  744. if (!dep->dev)
  745. return;
  746. dep->hotplug = false;
  747. if (dep->claimed)
  748. device_release(dep);
  749. safe_list_del(&dep->list);
  750. dep->dev = NULL;
  751. D(DEVICE, "Remove user for device '%s', refcount=%d\n", dev->ifname, device_refcount(dev));
  752. device_free_unused();
  753. }
  754. void
  755. device_init_pending(void)
  756. {
  757. struct device *dev, *tmp;
  758. avl_for_each_element_safe(&devices, dev, avl, tmp) {
  759. if (!dev->config_pending)
  760. continue;
  761. dev->type->config_init(dev);
  762. dev->config_pending = false;
  763. device_check_state(dev);
  764. }
  765. }
  766. bool
  767. device_check_ip6segmentrouting(void)
  768. {
  769. struct device *dev;
  770. bool ip6segmentrouting = false;
  771. avl_for_each_element(&devices, dev, avl)
  772. ip6segmentrouting |= dev->settings.ip6segmentrouting;
  773. return ip6segmentrouting;
  774. }
  775. static enum dev_change_type
  776. device_set_config(struct device *dev, struct device_type *type,
  777. struct blob_attr *attr)
  778. {
  779. struct blob_attr *tb[__DEV_ATTR_MAX];
  780. const struct uci_blob_param_list *cfg = type->config_params;
  781. if (type != dev->type)
  782. return DEV_CONFIG_RECREATE;
  783. if (dev->type->reload)
  784. return dev->type->reload(dev, attr);
  785. if (uci_blob_check_equal(dev->config, attr, cfg))
  786. return DEV_CONFIG_NO_CHANGE;
  787. if (cfg == &device_attr_list) {
  788. memset(tb, 0, sizeof(tb));
  789. if (attr)
  790. blobmsg_parse(dev_attrs, __DEV_ATTR_MAX, tb,
  791. blob_data(attr), blob_len(attr));
  792. device_init_settings(dev, tb);
  793. return DEV_CONFIG_RESTART;
  794. } else
  795. return DEV_CONFIG_RECREATE;
  796. }
  797. enum dev_change_type
  798. device_apply_config(struct device *dev, struct device_type *type,
  799. struct blob_attr *config)
  800. {
  801. enum dev_change_type change;
  802. change = device_set_config(dev, type, config);
  803. if (dev->external) {
  804. system_if_apply_settings(dev, &dev->settings, dev->settings.flags);
  805. change = DEV_CONFIG_APPLIED;
  806. }
  807. switch (change) {
  808. case DEV_CONFIG_RESTART:
  809. case DEV_CONFIG_APPLIED:
  810. D(DEVICE, "Device '%s': config applied\n", dev->ifname);
  811. config = blob_memdup(config);
  812. free(dev->config);
  813. dev->config = config;
  814. if (change == DEV_CONFIG_RESTART && dev->present) {
  815. int ret = 0;
  816. device_set_present(dev, false);
  817. if (dev->active && !dev->external) {
  818. ret = dev->set_state(dev, false);
  819. if (!ret)
  820. ret = dev->set_state(dev, true);
  821. }
  822. if (!ret)
  823. device_set_present(dev, true);
  824. }
  825. break;
  826. case DEV_CONFIG_NO_CHANGE:
  827. D(DEVICE, "Device '%s': no configuration change\n", dev->ifname);
  828. break;
  829. case DEV_CONFIG_RECREATE:
  830. break;
  831. }
  832. return change;
  833. }
  834. static void
  835. device_replace(struct device *dev, struct device *odev)
  836. {
  837. struct device_user *dep;
  838. __devlock++;
  839. if (odev->present)
  840. device_set_present(odev, false);
  841. while (!list_empty(&odev->users.list)) {
  842. dep = list_first_entry(&odev->users.list, struct device_user, list.list);
  843. device_release(dep);
  844. if (!dep->dev)
  845. continue;
  846. safe_list_del(&dep->list);
  847. __device_add_user(dep, dev);
  848. }
  849. __devlock--;
  850. device_free(odev);
  851. }
  852. void
  853. device_reset_config(void)
  854. {
  855. struct device *dev;
  856. avl_for_each_element(&devices, dev, avl)
  857. dev->current_config = false;
  858. }
  859. void
  860. device_reset_old(void)
  861. {
  862. struct device *dev, *tmp, *ndev;
  863. avl_for_each_element_safe(&devices, dev, avl, tmp) {
  864. if (dev->current_config || dev->default_config)
  865. continue;
  866. if (dev->type != &simple_device_type)
  867. continue;
  868. ndev = device_create_default(dev->ifname, dev->external);
  869. if (!ndev)
  870. continue;
  871. device_replace(ndev, dev);
  872. }
  873. }
  874. struct device *
  875. device_create(const char *name, struct device_type *type,
  876. struct blob_attr *config)
  877. {
  878. struct device *odev = NULL, *dev;
  879. enum dev_change_type change;
  880. odev = device_find(name);
  881. if (odev) {
  882. odev->current_config = true;
  883. change = device_apply_config(odev, type, config);
  884. switch (change) {
  885. case DEV_CONFIG_RECREATE:
  886. D(DEVICE, "Device '%s': recreate device\n", odev->ifname);
  887. device_delete(odev);
  888. break;
  889. default:
  890. return odev;
  891. }
  892. } else
  893. D(DEVICE, "Create new device '%s' (%s)\n", name, type->name);
  894. config = blob_memdup(config);
  895. if (!config)
  896. return NULL;
  897. dev = type->create(name, type, config);
  898. if (!dev)
  899. return NULL;
  900. dev->current_config = true;
  901. dev->config = config;
  902. if (odev)
  903. device_replace(dev, odev);
  904. if (!config_init && dev->config_pending) {
  905. type->config_init(dev);
  906. dev->config_pending = false;
  907. }
  908. device_check_state(dev);
  909. return dev;
  910. }
  911. void
  912. device_dump_status(struct blob_buf *b, struct device *dev)
  913. {
  914. struct device_settings st;
  915. void *c, *s;
  916. if (!dev) {
  917. avl_for_each_element(&devices, dev, avl) {
  918. if (!dev->present)
  919. continue;
  920. c = blobmsg_open_table(b, dev->ifname);
  921. device_dump_status(b, dev);
  922. blobmsg_close_table(b, c);
  923. }
  924. return;
  925. }
  926. blobmsg_add_u8(b, "external", dev->external);
  927. blobmsg_add_u8(b, "present", dev->present);
  928. blobmsg_add_string(b, "type", dev->type->name);
  929. if (!dev->present)
  930. return;
  931. blobmsg_add_u8(b, "up", !!dev->active);
  932. blobmsg_add_u8(b, "carrier", !!dev->link_active);
  933. blobmsg_add_u8(b, "auth_status", !!dev->auth_status);
  934. if (dev->type->dump_info)
  935. dev->type->dump_info(dev, b);
  936. else
  937. system_if_dump_info(dev, b);
  938. if (dev->active) {
  939. device_merge_settings(dev, &st);
  940. if (st.flags & DEV_OPT_MTU)
  941. blobmsg_add_u32(b, "mtu", st.mtu);
  942. if (st.flags & DEV_OPT_MTU6)
  943. blobmsg_add_u32(b, "mtu6", st.mtu6);
  944. if (st.flags & DEV_OPT_MACADDR)
  945. blobmsg_add_string(b, "macaddr", format_macaddr(st.macaddr));
  946. if (st.flags & DEV_OPT_TXQUEUELEN)
  947. blobmsg_add_u32(b, "txqueuelen", st.txqueuelen);
  948. if (st.flags & DEV_OPT_IPV6)
  949. blobmsg_add_u8(b, "ipv6", st.ipv6);
  950. if (st.flags & DEV_OPT_IP6SEGMENTROUTING)
  951. blobmsg_add_u8(b, "ip6segmentrouting", st.ip6segmentrouting);
  952. if (st.flags & DEV_OPT_PROMISC)
  953. blobmsg_add_u8(b, "promisc", st.promisc);
  954. if (st.flags & DEV_OPT_RPFILTER)
  955. blobmsg_add_u32(b, "rpfilter", st.rpfilter);
  956. if (st.flags & DEV_OPT_ACCEPTLOCAL)
  957. blobmsg_add_u8(b, "acceptlocal", st.acceptlocal);
  958. if (st.flags & DEV_OPT_IGMPVERSION)
  959. blobmsg_add_u32(b, "igmpversion", st.igmpversion);
  960. if (st.flags & DEV_OPT_MLDVERSION)
  961. blobmsg_add_u32(b, "mldversion", st.mldversion);
  962. if (st.flags & DEV_OPT_NEIGHREACHABLETIME) {
  963. blobmsg_add_u32(b, "neigh4reachabletime", st.neigh4reachabletime);
  964. blobmsg_add_u32(b, "neigh6reachabletime", st.neigh6reachabletime);
  965. }
  966. if (st.flags & DEV_OPT_NEIGHGCSTALETIME) {
  967. blobmsg_add_u32(b, "neigh4gcstaletime", st.neigh4gcstaletime);
  968. blobmsg_add_u32(b, "neigh6gcstaletime", st.neigh6gcstaletime);
  969. }
  970. if (st.flags & DEV_OPT_NEIGHLOCKTIME)
  971. blobmsg_add_u32(b, "neigh4locktime", st.neigh4locktime);
  972. if (st.flags & DEV_OPT_DADTRANSMITS)
  973. blobmsg_add_u32(b, "dadtransmits", st.dadtransmits);
  974. if (st.flags & DEV_OPT_MULTICAST_TO_UNICAST)
  975. blobmsg_add_u8(b, "multicast_to_unicast", st.multicast_to_unicast);
  976. if (st.flags & DEV_OPT_MULTICAST_ROUTER)
  977. blobmsg_add_u32(b, "multicast_router", st.multicast_router);
  978. if (st.flags & DEV_OPT_MULTICAST_FAST_LEAVE)
  979. blobmsg_add_u8(b, "multicast_fast_leave", st.multicast_fast_leave);
  980. if (st.flags & DEV_OPT_MULTICAST)
  981. blobmsg_add_u8(b, "multicast", st.multicast);
  982. if (st.flags & DEV_OPT_LEARNING)
  983. blobmsg_add_u8(b, "learning", st.learning);
  984. if (st.flags & DEV_OPT_UNICAST_FLOOD)
  985. blobmsg_add_u8(b, "unicast_flood", st.unicast_flood);
  986. if (st.flags & DEV_OPT_SENDREDIRECTS)
  987. blobmsg_add_u8(b, "sendredirects", st.sendredirects);
  988. if (st.flags & DEV_OPT_DROP_V4_UNICAST_IN_L2_MULTICAST)
  989. blobmsg_add_u8(b, "drop_v4_unicast_in_l2_multicast", st.drop_v4_unicast_in_l2_multicast);
  990. if (st.flags & DEV_OPT_DROP_V6_UNICAST_IN_L2_MULTICAST)
  991. blobmsg_add_u8(b, "drop_v6_unicast_in_l2_multicast", st.drop_v6_unicast_in_l2_multicast);
  992. if (st.flags & DEV_OPT_DROP_GRATUITOUS_ARP)
  993. blobmsg_add_u8(b, "drop_gratuitous_arp", st.drop_gratuitous_arp);
  994. if (st.flags & DEV_OPT_DROP_UNSOLICITED_NA)
  995. blobmsg_add_u8(b, "drop_unsolicited_na", st.drop_unsolicited_na);
  996. if (st.flags & DEV_OPT_ARP_ACCEPT)
  997. blobmsg_add_u8(b, "arp_accept", st.arp_accept);
  998. if (st.flags & DEV_OPT_AUTH)
  999. blobmsg_add_u8(b, "auth", st.auth);
  1000. }
  1001. s = blobmsg_open_table(b, "statistics");
  1002. if (dev->type->dump_stats)
  1003. dev->type->dump_stats(dev, b);
  1004. else
  1005. system_if_dump_stats(dev, b);
  1006. blobmsg_close_table(b, s);
  1007. }
  1008. static void __init simple_device_type_init(void)
  1009. {
  1010. device_type_add(&simple_device_type);
  1011. }
  1012. void device_hotplug_event(const char *name, bool add)
  1013. {
  1014. struct device *dev;
  1015. wireless_device_hotplug_event(name, add);
  1016. dev = device_find(name);
  1017. if (!dev || dev->type != &simple_device_type)
  1018. return;
  1019. device_set_present(dev, add);
  1020. }