device.c 35 KB

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